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> |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 31 | #include "file.h" |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 32 | #include "flash.h" |
| 33 | #include "chipdrivers.h" |
| 34 | #include "programmer.h" |
| 35 | #include "spi.h" |
| 36 | |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 37 | |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 38 | |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 39 | /* TODO: this information should come from the SPI master driver. */ |
| 40 | #define SPI_DMA_SIZE 64 |
| 41 | |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 42 | #ifndef SPIDEV_MAJOR |
| 43 | #define SPIDEV_MAJOR 153 /* refer to kernel/files/drivers/spi/spidev.c */ |
| 44 | #endif |
| 45 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 46 | #define MODALIAS_FILE "modalias" |
| 47 | #define LINUX_SPI_SYSFS_ROOT "/sys/bus/spi/devices" |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 48 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 49 | static int fd = -1; |
| 50 | |
Souvik Ghosh | 052210a | 2016-07-11 12:29:05 -0700 | [diff] [blame] | 51 | static int linux_spi_shutdown(struct flashctx *flash, void *data); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 52 | static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 53 | const unsigned char *txbuf, unsigned char *rxbuf); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 54 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 55 | unsigned int start, unsigned int len); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 56 | static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 57 | unsigned int start, unsigned int len); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 58 | |
| 59 | static const struct spi_programmer spi_programmer_linux = { |
| 60 | .type = SPI_CONTROLLER_LINUX, |
| 61 | .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 62 | .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 63 | .command = linux_spi_send_command, |
| 64 | .multicommand = default_spi_send_multicommand, |
| 65 | .read = linux_spi_read, |
| 66 | .write_256 = linux_spi_write_256, |
| 67 | }; |
| 68 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 69 | static char devfs_path[32]; /* at least big enough to fit /dev/spidevX.Y */ |
David Hendricks | 2d06918 | 2015-10-30 22:52:57 -0700 | [diff] [blame] | 70 | static char *check_sysfs(void) |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 71 | { |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 72 | int i; |
| 73 | const char *sysfs_path = NULL; |
| 74 | char *p; |
| 75 | char *modalias[] = { |
David Hendricks | dca1adf | 2013-11-01 13:31:57 -0700 | [diff] [blame] | 76 | "spi:spidev", /* raw access over SPI bus (newer kernels) */ |
David Hendricks | 6ed2685 | 2013-11-12 12:53:40 -0800 | [diff] [blame] | 77 | "spidev", /* raw access over SPI bus (older kernels) */ |
| 78 | "m25p80", /* generic MTD device */ |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 79 | }; |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 80 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 81 | for (i = 0; i < ARRAY_SIZE(modalias); i++) { |
| 82 | int major, minor; |
David Hendricks | 0c0a7e6 | 2012-06-12 23:41:05 +0900 | [diff] [blame] | 83 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 84 | /* Path should look like: /sys/blah/spiX.Y/modalias */ |
| 85 | sysfs_path = scanft(LINUX_SPI_SYSFS_ROOT, |
David Hendricks | e94c7a8 | 2013-11-12 14:22:36 -0800 | [diff] [blame] | 86 | MODALIAS_FILE, modalias[i], 1); |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 87 | if (!sysfs_path) |
Louis Yung-Chieh Lo | eaa44fc | 2012-05-24 15:43:50 +0800 | [diff] [blame] | 88 | continue; |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 89 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 90 | p = (char *)sysfs_path + strlen(LINUX_SPI_SYSFS_ROOT); |
| 91 | if (p[0] == '/') |
| 92 | p++; |
David Hendricks | 0c0a7e6 | 2012-06-12 23:41:05 +0900 | [diff] [blame] | 93 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 94 | if (sscanf(p, "spi%u.%u", &major, &minor) == 2) { |
David Hendricks | d02ee0f | 2013-11-01 13:18:48 -0700 | [diff] [blame] | 95 | msg_pdbg("Found SPI device %s on spi%u.%u\n", |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 96 | modalias[i], major, minor); |
| 97 | sprintf(devfs_path, "/dev/spidev%u.%u", major, minor); |
| 98 | free((void *)sysfs_path); |
| 99 | break; |
| 100 | } |
| 101 | free((void *)sysfs_path); |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 102 | } |
| 103 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 104 | if (i == ARRAY_SIZE(modalias)) |
| 105 | return NULL; |
| 106 | return devfs_path; |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 107 | } |
| 108 | |
David Hendricks | 2d06918 | 2015-10-30 22:52:57 -0700 | [diff] [blame] | 109 | static char *check_fdt(void) |
| 110 | { |
| 111 | unsigned int bus, cs; |
| 112 | |
| 113 | if (fdt_find_spi_nor_flash(&bus, &cs) < 0) |
| 114 | return NULL; |
| 115 | |
| 116 | sprintf(devfs_path, "/dev/spidev%u.%u", bus, cs); |
| 117 | return devfs_path; |
| 118 | } |
| 119 | |
| 120 | static char *linux_spi_probe(void) |
| 121 | { |
| 122 | char *ret; |
| 123 | |
| 124 | ret = check_fdt(); |
| 125 | if (ret) |
| 126 | return ret; |
| 127 | |
| 128 | ret = check_sysfs(); |
| 129 | if (ret) |
| 130 | return ret; |
| 131 | |
| 132 | return NULL; |
| 133 | } |
| 134 | |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 135 | /* |
| 136 | * This is used when /dev/spidevX.Y is not created yet, for example, when |
| 137 | * udev is not started. |
| 138 | */ |
| 139 | static int manual_mknod(const char *dev) |
| 140 | { |
| 141 | char cmd[256]; |
| 142 | |
| 143 | msg_pdbg("Creating SPI device node %s...\n", dev); |
| 144 | strcpy(cmd, "modprobe spidev"); |
| 145 | msg_pdbg("CMD: [%s]\n", cmd); |
Simon Glass | 7ef84ff | 2013-02-10 17:13:41 -0800 | [diff] [blame] | 146 | if (system(cmd)) { |
| 147 | msg_perr("%s: failed to run '%s': %s\n", __func__, |
| 148 | cmd, strerror(errno)); |
| 149 | return -1; |
| 150 | } |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 151 | snprintf(cmd, sizeof(cmd), "mknod %s c %d 0", dev, SPIDEV_MAJOR); |
| 152 | msg_pdbg("CMD: [%s]\n", cmd); |
Simon Glass | 7ef84ff | 2013-02-10 17:13:41 -0800 | [diff] [blame] | 153 | if (system(cmd)) { |
| 154 | msg_perr("%s: failed to run '%s': %s\n", __func__, |
| 155 | cmd, strerror(errno)); |
| 156 | return -1; |
| 157 | } |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 158 | |
| 159 | if ((fd = open(dev, O_RDWR)) == -1) { |
| 160 | msg_perr("%s: failed to open %s: %s\n", __func__, |
| 161 | dev, strerror(errno)); |
| 162 | } |
| 163 | |
| 164 | return fd; |
| 165 | } |
| 166 | |
| 167 | |
Souvik Ghosh | 63b92f9 | 2016-06-29 18:45:52 -0700 | [diff] [blame] | 168 | int linux_spi_init(struct flashctx *flash) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 169 | { |
| 170 | char *p, *endp, *dev; |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 171 | uint32_t speed = 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 172 | |
David Hendricks | ba0827a | 2013-05-03 20:25:40 -0700 | [diff] [blame] | 173 | /* |
| 174 | * FIXME: There might be other programmers with flash memory (such as |
| 175 | * an EC) connected via SPI. For now we rely on the device's driver to |
| 176 | * distinguish it and assume generic SPI implies host. |
| 177 | */ |
| 178 | if (alias && alias->type != ALIAS_HOST) |
| 179 | return 1; |
| 180 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 181 | dev = extract_programmer_param("dev"); |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 182 | if (!dev) |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 183 | dev = linux_spi_probe(); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 184 | if (!dev || !strlen(dev)) { |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 185 | msg_perr("No SPI device found. Use flashrom -p " |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 186 | "linux_spi:dev=/dev/spidevX.Y\n"); |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | p = extract_programmer_param("speed"); |
| 191 | if (p && strlen(p)) { |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 192 | speed = (uint32_t)strtoul(p, &endp, 10) * 1024; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 193 | if (p == endp) { |
| 194 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
| 195 | return 1; |
| 196 | } |
| 197 | } |
| 198 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 199 | msg_pdbg("Using device %s\n", dev); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 200 | if ((fd = open(dev, O_RDWR)) == -1) { |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 201 | msg_pdbg("%s: failed to open %s: %s\n", __func__, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 202 | dev, strerror(errno)); |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 203 | |
| 204 | if (manual_mknod(dev) == -1) { // global fd is effected. |
| 205 | return 1; |
| 206 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 207 | } |
| 208 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 209 | if (speed > 0) { |
| 210 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) { |
| 211 | msg_perr("%s: failed to set speed %dHz: %s\n", |
| 212 | __func__, speed, strerror(errno)); |
| 213 | close(fd); |
| 214 | return 1; |
| 215 | } |
| 216 | |
| 217 | msg_pdbg("Using %d kHz clock\n", speed); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (register_shutdown(linux_spi_shutdown, NULL)) |
| 221 | return 1; |
| 222 | |
| 223 | register_spi_programmer(&spi_programmer_linux); |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Souvik Ghosh | 052210a | 2016-07-11 12:29:05 -0700 | [diff] [blame] | 228 | static int linux_spi_shutdown(struct flashctx *flash, void *data) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 229 | { |
| 230 | if (fd != -1) { |
| 231 | close(fd); |
| 232 | fd = -1; |
| 233 | } |
| 234 | return 0; |
| 235 | } |
| 236 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 237 | static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 238 | const unsigned char *txbuf, unsigned char *rxbuf) |
| 239 | { |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 240 | int msg_start = 0, msg_count = 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 241 | struct spi_ioc_transfer msg[2] = { |
| 242 | { |
David Riley | 13343b8 | 2014-07-25 11:37:56 -0700 | [diff] [blame] | 243 | .tx_buf = (uint64_t)(uintptr_t)txbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 244 | .len = writecnt, |
| 245 | }, |
| 246 | { |
David Riley | 13343b8 | 2014-07-25 11:37:56 -0700 | [diff] [blame] | 247 | .rx_buf = (uint64_t)(uintptr_t)rxbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 248 | .len = readcnt, |
| 249 | }, |
| 250 | }; |
| 251 | |
| 252 | if (fd == -1) |
| 253 | return -1; |
| 254 | |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 255 | /* 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] | 256 | * which drives an un-expected CS line and clocks. */ |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 257 | if (writecnt) { |
| 258 | msg_start = 0; /* tx: msg[0] */ |
| 259 | msg_count++; |
| 260 | if (readcnt) { |
| 261 | msg_count++; |
| 262 | } |
Louis Yung-Chieh Lo | b121eff | 2012-05-22 23:31:03 +0800 | [diff] [blame] | 263 | } else if (readcnt) { |
| 264 | msg_start = 1; /* rx: msg[1] */ |
| 265 | msg_count++; |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 266 | } else { |
Louis Yung-Chieh Lo | b121eff | 2012-05-22 23:31:03 +0800 | [diff] [blame] | 267 | msg_cerr("%s: both writecnt and readcnt are 0.\n", |
| 268 | __func__); |
| 269 | return -1; |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) { |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 273 | msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno)); |
| 274 | return -1; |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 279 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 280 | unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 281 | { |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 282 | return spi_read_chunked(flash, buf, start, len, SPI_DMA_SIZE); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 285 | static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 286 | unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 287 | { |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 288 | return spi_write_chunked(flash, buf, start, len, |
| 289 | SPI_DMA_SIZE - 5 /* WREN, Page Program */); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 290 | } |