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 | |
| 51 | static int linux_spi_shutdown(void *data); |
| 52 | static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 53 | const unsigned char *txbuf, unsigned char *rxbuf); |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 54 | static int linux_spi_read(struct flashchip *flash, uint8_t *buf, |
| 55 | unsigned int start, unsigned int len); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 56 | static int linux_spi_write_256(struct flashchip *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 */ |
| 70 | static char *linux_spi_probe(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[] = { |
| 76 | "m25p80", /* generic MTD device */ |
David Hendricks | dca1adf | 2013-11-01 13:31:57 -0700 | [diff] [blame] | 77 | "spidev", /* raw access over SPI bus (older kernels) */ |
| 78 | "spi:spidev", /* raw access over SPI bus (newer kernels) */ |
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, |
| 86 | MODALIAS_FILE, modalias[i], 2); |
| 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 | |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 109 | /* |
| 110 | * This is used when /dev/spidevX.Y is not created yet, for example, when |
| 111 | * udev is not started. |
| 112 | */ |
| 113 | static int manual_mknod(const char *dev) |
| 114 | { |
| 115 | char cmd[256]; |
| 116 | |
| 117 | msg_pdbg("Creating SPI device node %s...\n", dev); |
| 118 | strcpy(cmd, "modprobe spidev"); |
| 119 | msg_pdbg("CMD: [%s]\n", cmd); |
Simon Glass | 7ef84ff | 2013-02-10 17:13:41 -0800 | [diff] [blame] | 120 | if (system(cmd)) { |
| 121 | msg_perr("%s: failed to run '%s': %s\n", __func__, |
| 122 | cmd, strerror(errno)); |
| 123 | return -1; |
| 124 | } |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 125 | snprintf(cmd, sizeof(cmd), "mknod %s c %d 0", dev, SPIDEV_MAJOR); |
| 126 | msg_pdbg("CMD: [%s]\n", cmd); |
Simon Glass | 7ef84ff | 2013-02-10 17:13:41 -0800 | [diff] [blame] | 127 | if (system(cmd)) { |
| 128 | msg_perr("%s: failed to run '%s': %s\n", __func__, |
| 129 | cmd, strerror(errno)); |
| 130 | return -1; |
| 131 | } |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 132 | |
| 133 | if ((fd = open(dev, O_RDWR)) == -1) { |
| 134 | msg_perr("%s: failed to open %s: %s\n", __func__, |
| 135 | dev, strerror(errno)); |
| 136 | } |
| 137 | |
| 138 | return fd; |
| 139 | } |
| 140 | |
| 141 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 142 | int linux_spi_init(void) |
| 143 | { |
| 144 | char *p, *endp, *dev; |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 145 | uint32_t speed = 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 146 | |
David Hendricks | ba0827a | 2013-05-03 20:25:40 -0700 | [diff] [blame] | 147 | /* |
| 148 | * FIXME: There might be other programmers with flash memory (such as |
| 149 | * an EC) connected via SPI. For now we rely on the device's driver to |
| 150 | * distinguish it and assume generic SPI implies host. |
| 151 | */ |
| 152 | if (alias && alias->type != ALIAS_HOST) |
| 153 | return 1; |
| 154 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 155 | dev = extract_programmer_param("dev"); |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 156 | if (!dev) |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 157 | dev = linux_spi_probe(); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 158 | if (!dev || !strlen(dev)) { |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 159 | msg_perr("No SPI device found. Use flashrom -p " |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 160 | "linux_spi:dev=/dev/spidevX.Y\n"); |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | p = extract_programmer_param("speed"); |
| 165 | if (p && strlen(p)) { |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 166 | speed = (uint32_t)strtoul(p, &endp, 10) * 1024; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 167 | if (p == endp) { |
| 168 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
| 169 | return 1; |
| 170 | } |
| 171 | } |
| 172 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 173 | msg_pdbg("Using device %s\n", dev); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 174 | if ((fd = open(dev, O_RDWR)) == -1) { |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 175 | msg_pdbg("%s: failed to open %s: %s\n", __func__, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 176 | dev, strerror(errno)); |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 177 | |
| 178 | if (manual_mknod(dev) == -1) { // global fd is effected. |
| 179 | return 1; |
| 180 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 181 | } |
| 182 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 183 | if (speed > 0) { |
| 184 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) { |
| 185 | msg_perr("%s: failed to set speed %dHz: %s\n", |
| 186 | __func__, speed, strerror(errno)); |
| 187 | close(fd); |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | msg_pdbg("Using %d kHz clock\n", speed); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | if (register_shutdown(linux_spi_shutdown, NULL)) |
| 195 | return 1; |
| 196 | |
| 197 | register_spi_programmer(&spi_programmer_linux); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int linux_spi_shutdown(void *data) |
| 203 | { |
| 204 | if (fd != -1) { |
| 205 | close(fd); |
| 206 | fd = -1; |
| 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 212 | const unsigned char *txbuf, unsigned char *rxbuf) |
| 213 | { |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 214 | int msg_start = 0, msg_count = 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 215 | struct spi_ioc_transfer msg[2] = { |
| 216 | { |
uwe | 230909b | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 217 | .tx_buf = (uint64_t)(ptrdiff_t)txbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 218 | .len = writecnt, |
| 219 | }, |
| 220 | { |
uwe | 230909b | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 221 | .rx_buf = (uint64_t)(ptrdiff_t)rxbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 222 | .len = readcnt, |
| 223 | }, |
| 224 | }; |
| 225 | |
| 226 | if (fd == -1) |
| 227 | return -1; |
| 228 | |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 229 | /* 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] | 230 | * which drives an un-expected CS line and clocks. */ |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 231 | if (writecnt) { |
| 232 | msg_start = 0; /* tx: msg[0] */ |
| 233 | msg_count++; |
| 234 | if (readcnt) { |
| 235 | msg_count++; |
| 236 | } |
Louis Yung-Chieh Lo | b121eff | 2012-05-22 23:31:03 +0800 | [diff] [blame] | 237 | } else if (readcnt) { |
| 238 | msg_start = 1; /* rx: msg[1] */ |
| 239 | msg_count++; |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 240 | } else { |
Louis Yung-Chieh Lo | b121eff | 2012-05-22 23:31:03 +0800 | [diff] [blame] | 241 | msg_cerr("%s: both writecnt and readcnt are 0.\n", |
| 242 | __func__); |
| 243 | return -1; |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) { |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 247 | msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno)); |
| 248 | return -1; |
| 249 | } |
| 250 | return 0; |
| 251 | } |
| 252 | |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 253 | static int linux_spi_read(struct flashchip *flash, uint8_t *buf, |
| 254 | unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 255 | { |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 256 | return spi_read_chunked(flash, buf, start, len, SPI_DMA_SIZE); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 260 | unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 261 | { |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 262 | return spi_write_chunked(flash, buf, start, len, |
| 263 | SPI_DMA_SIZE - 5 /* WREN, Page Program */); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 264 | } |