uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2011 Sven Schnelle <svens@stackframe.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <sys/fcntl.h> |
| 24 | #include <errno.h> |
| 25 | #include <ctype.h> |
| 26 | #include <unistd.h> |
| 27 | #include <linux/spi/spidev.h> |
| 28 | #include <sys/ioctl.h> |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 31 | #include "flash.h" |
| 32 | #include "chipdrivers.h" |
| 33 | #include "programmer.h" |
| 34 | #include "spi.h" |
| 35 | |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 36 | |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 37 | |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 38 | /* TODO: this information should come from the SPI master driver. */ |
| 39 | #define SPI_DMA_SIZE 64 |
| 40 | |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 41 | #ifndef SPIDEV_MAJOR |
| 42 | #define SPIDEV_MAJOR 153 /* refer to kernel/files/drivers/spi/spidev.c */ |
| 43 | #endif |
| 44 | |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 45 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 46 | static int fd = -1; |
| 47 | |
| 48 | static int linux_spi_shutdown(void *data); |
| 49 | static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 50 | const unsigned char *txbuf, unsigned char *rxbuf); |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 51 | static int linux_spi_read(struct flashchip *flash, uint8_t *buf, |
| 52 | unsigned int start, unsigned int len); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 53 | static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 54 | unsigned int start, unsigned int len); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 55 | |
| 56 | static const struct spi_programmer spi_programmer_linux = { |
| 57 | .type = SPI_CONTROLLER_LINUX, |
| 58 | .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 59 | .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 60 | .command = linux_spi_send_command, |
| 61 | .multicommand = default_spi_send_multicommand, |
| 62 | .read = linux_spi_read, |
| 63 | .write_256 = linux_spi_write_256, |
| 64 | }; |
| 65 | |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 66 | |
| 67 | /* Detect the SPI chip behind /dev/spidevX.Y. |
| 68 | * We scan every /dev/spidevX.0 and if it's /dev/spidevX.1 is a MTD device |
| 69 | * (by looking /sys/bus/spi/devices/spidevX.1/modalias), then we assume the |
| 70 | * spidevX.0 is the SPI flash. |
| 71 | */ |
| 72 | static char* linux_spi_probe(void) |
| 73 | { |
David Hendricks | 0c0a7e6 | 2012-06-12 23:41:05 +0900 | [diff] [blame] | 74 | char filename[] = "/sys/bus/spi/devices/spiX.0/modalias"; |
| 75 | int X = strchr(filename, 'X') - filename; /* offset to the X char */ |
| 76 | int i; /* for for-loop */ |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 77 | |
David Hendricks | 0c0a7e6 | 2012-06-12 23:41:05 +0900 | [diff] [blame] | 78 | for (i = '0'; i <= '9'; i++) { |
David Hendricks | 8aaf686 | 2013-06-05 14:30:50 -0700 | [diff] [blame^] | 79 | int fd, bytes_read; |
| 80 | char buf[32]; |
David Hendricks | 0c0a7e6 | 2012-06-12 23:41:05 +0900 | [diff] [blame] | 81 | |
| 82 | filename[X] = i; |
| 83 | if ((fd = open(filename, O_RDONLY)) < 0 || |
David Hendricks | 8aaf686 | 2013-06-05 14:30:50 -0700 | [diff] [blame^] | 84 | (bytes_read = read(fd, buf, sizeof(buf) - 1) < 0)) { |
David Hendricks | 0c0a7e6 | 2012-06-12 23:41:05 +0900 | [diff] [blame] | 85 | msg_pdbg("read(%s) < 0, try next.\n", filename); |
Louis Yung-Chieh Lo | eaa44fc | 2012-05-24 15:43:50 +0800 | [diff] [blame] | 86 | continue; |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 87 | } |
David Hendricks | 8aaf686 | 2013-06-05 14:30:50 -0700 | [diff] [blame^] | 88 | buf[bytes_read] = '\0'; |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 89 | |
David Hendricks | 8aaf686 | 2013-06-05 14:30:50 -0700 | [diff] [blame^] | 90 | if (strstr(buf, "spidev")) { |
David Hendricks | 0c0a7e6 | 2012-06-12 23:41:05 +0900 | [diff] [blame] | 91 | static char name[] = "/dev/spidevX.0"; |
| 92 | *strchr(name, 'X') = i; |
| 93 | msg_pdbg("Detected linux_spi:dev=%s\n", name); |
| 94 | return name; |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 95 | } |
David Hendricks | 0c0a7e6 | 2012-06-12 23:41:05 +0900 | [diff] [blame] | 96 | |
| 97 | close(fd); |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 104 | /* |
| 105 | * This is used when /dev/spidevX.Y is not created yet, for example, when |
| 106 | * udev is not started. |
| 107 | */ |
| 108 | static int manual_mknod(const char *dev) |
| 109 | { |
| 110 | char cmd[256]; |
| 111 | |
| 112 | msg_pdbg("Creating SPI device node %s...\n", dev); |
| 113 | strcpy(cmd, "modprobe spidev"); |
| 114 | msg_pdbg("CMD: [%s]\n", cmd); |
Simon Glass | 7ef84ff | 2013-02-10 17:13:41 -0800 | [diff] [blame] | 115 | if (system(cmd)) { |
| 116 | msg_perr("%s: failed to run '%s': %s\n", __func__, |
| 117 | cmd, strerror(errno)); |
| 118 | return -1; |
| 119 | } |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 120 | snprintf(cmd, sizeof(cmd), "mknod %s c %d 0", dev, SPIDEV_MAJOR); |
| 121 | msg_pdbg("CMD: [%s]\n", cmd); |
Simon Glass | 7ef84ff | 2013-02-10 17:13:41 -0800 | [diff] [blame] | 122 | if (system(cmd)) { |
| 123 | msg_perr("%s: failed to run '%s': %s\n", __func__, |
| 124 | cmd, strerror(errno)); |
| 125 | return -1; |
| 126 | } |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 127 | |
| 128 | if ((fd = open(dev, O_RDWR)) == -1) { |
| 129 | msg_perr("%s: failed to open %s: %s\n", __func__, |
| 130 | dev, strerror(errno)); |
| 131 | } |
| 132 | |
| 133 | return fd; |
| 134 | } |
| 135 | |
| 136 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 137 | int linux_spi_init(void) |
| 138 | { |
| 139 | char *p, *endp, *dev; |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 140 | uint32_t speed = 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 141 | |
| 142 | dev = extract_programmer_param("dev"); |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 143 | if (!dev) { |
| 144 | dev = linux_spi_probe(); |
| 145 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 146 | if (!dev || !strlen(dev)) { |
| 147 | msg_perr("No SPI device given. Use flashrom -p " |
| 148 | "linux_spi:dev=/dev/spidevX.Y\n"); |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | p = extract_programmer_param("speed"); |
| 153 | if (p && strlen(p)) { |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 154 | speed = (uint32_t)strtoul(p, &endp, 10) * 1024; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 155 | if (p == endp) { |
| 156 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
| 157 | return 1; |
| 158 | } |
| 159 | } |
| 160 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 161 | msg_pdbg("Using device %s\n", dev); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 162 | if ((fd = open(dev, O_RDWR)) == -1) { |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 163 | msg_pdbg("%s: failed to open %s: %s\n", __func__, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 164 | dev, strerror(errno)); |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 165 | |
| 166 | if (manual_mknod(dev) == -1) { // global fd is effected. |
| 167 | return 1; |
| 168 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 169 | } |
| 170 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 171 | if (speed > 0) { |
| 172 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) { |
| 173 | msg_perr("%s: failed to set speed %dHz: %s\n", |
| 174 | __func__, speed, strerror(errno)); |
| 175 | close(fd); |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | msg_pdbg("Using %d kHz clock\n", speed); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | if (register_shutdown(linux_spi_shutdown, NULL)) |
| 183 | return 1; |
| 184 | |
| 185 | register_spi_programmer(&spi_programmer_linux); |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static int linux_spi_shutdown(void *data) |
| 191 | { |
| 192 | if (fd != -1) { |
| 193 | close(fd); |
| 194 | fd = -1; |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 200 | const unsigned char *txbuf, unsigned char *rxbuf) |
| 201 | { |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 202 | int msg_start = 0, msg_count = 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 203 | struct spi_ioc_transfer msg[2] = { |
| 204 | { |
uwe | 230909b | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 205 | .tx_buf = (uint64_t)(ptrdiff_t)txbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 206 | .len = writecnt, |
| 207 | }, |
| 208 | { |
uwe | 230909b | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 209 | .rx_buf = (uint64_t)(ptrdiff_t)rxbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 210 | .len = readcnt, |
| 211 | }, |
| 212 | }; |
| 213 | |
| 214 | if (fd == -1) |
| 215 | return -1; |
| 216 | |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 217 | /* Only pass necessary msg[] to ioctl() to avoid the empty message |
Louis Yung-Chieh Lo | b121eff | 2012-05-22 23:31:03 +0800 | [diff] [blame] | 218 | * which drives an un-expected CS line and clocks. */ |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 219 | if (writecnt) { |
| 220 | msg_start = 0; /* tx: msg[0] */ |
| 221 | msg_count++; |
| 222 | if (readcnt) { |
| 223 | msg_count++; |
| 224 | } |
Louis Yung-Chieh Lo | b121eff | 2012-05-22 23:31:03 +0800 | [diff] [blame] | 225 | } else if (readcnt) { |
| 226 | msg_start = 1; /* rx: msg[1] */ |
| 227 | msg_count++; |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 228 | } else { |
Louis Yung-Chieh Lo | b121eff | 2012-05-22 23:31:03 +0800 | [diff] [blame] | 229 | msg_cerr("%s: both writecnt and readcnt are 0.\n", |
| 230 | __func__); |
| 231 | return -1; |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) { |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 235 | msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno)); |
| 236 | return -1; |
| 237 | } |
| 238 | return 0; |
| 239 | } |
| 240 | |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 241 | static int linux_spi_read(struct flashchip *flash, uint8_t *buf, |
| 242 | unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 243 | { |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 244 | return spi_read_chunked(flash, buf, start, len, SPI_DMA_SIZE); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 248 | unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 249 | { |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 250 | return spi_write_chunked(flash, buf, start, len, |
| 251 | SPI_DMA_SIZE - 5 /* WREN, Page Program */); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 252 | } |