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 | |
| 41 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 42 | static int fd = -1; |
| 43 | |
| 44 | static int linux_spi_shutdown(void *data); |
| 45 | static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 46 | const unsigned char *txbuf, unsigned char *rxbuf); |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 47 | static int linux_spi_read(struct flashchip *flash, uint8_t *buf, |
| 48 | unsigned int start, unsigned int len); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 49 | static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 50 | unsigned int start, unsigned int len); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 51 | |
| 52 | static const struct spi_programmer spi_programmer_linux = { |
| 53 | .type = SPI_CONTROLLER_LINUX, |
| 54 | .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 55 | .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 56 | .command = linux_spi_send_command, |
| 57 | .multicommand = default_spi_send_multicommand, |
| 58 | .read = linux_spi_read, |
| 59 | .write_256 = linux_spi_write_256, |
| 60 | }; |
| 61 | |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 62 | |
| 63 | /* Detect the SPI chip behind /dev/spidevX.Y. |
| 64 | * We scan every /dev/spidevX.0 and if it's /dev/spidevX.1 is a MTD device |
| 65 | * (by looking /sys/bus/spi/devices/spidevX.1/modalias), then we assume the |
| 66 | * spidevX.0 is the SPI flash. |
| 67 | */ |
| 68 | static char* linux_spi_probe(void) |
| 69 | { |
| 70 | char name0[] = "/sys/bus/spi/devices/spiX.0"; |
| 71 | char name1[] = "/sys/bus/spi/devices/spiX.1/modalias"; |
| 72 | int X = strchr(name0, 'X') - name0; /* point to the X char */ |
| 73 | int x; /* for for-loop */ |
| 74 | struct stat sb; |
| 75 | |
| 76 | for (x = '0'; x <= '9'; x++) { |
| 77 | name0[X] = x; |
| 78 | if (stat(name0, &sb) < 0) { |
| 79 | msg_pdbg("stat(%s) < 0, stop scanning.\n", name0); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | if ((sb.st_mode & S_IFMT) == S_IFDIR) { |
| 84 | int fd; |
| 85 | char buf[8]; // 8 is long enough for modalias |
| 86 | |
| 87 | name1[X] = x; |
| 88 | if ((fd = open(name1, O_RDONLY)) < 0 || |
| 89 | read(fd, buf, sizeof(buf)) < 0) { |
| 90 | msg_pdbg("read(%s) < 0, try next.\n", name0); |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | if (!strncmp(buf, "m25p80", 6)) { |
| 95 | static char name[] = "/dev/spidevX.0"; |
| 96 | *strchr(name, 'X') = x; |
| 97 | msg_pdbg("Detected linux_spi:dev=%s\n", name); |
| 98 | return name; |
| 99 | } |
| 100 | |
| 101 | close(fd); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 109 | int linux_spi_init(void) |
| 110 | { |
| 111 | char *p, *endp, *dev; |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 112 | uint32_t speed = 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 113 | |
| 114 | dev = extract_programmer_param("dev"); |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 115 | if (!dev) { |
| 116 | dev = linux_spi_probe(); |
| 117 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 118 | if (!dev || !strlen(dev)) { |
| 119 | msg_perr("No SPI device given. Use flashrom -p " |
| 120 | "linux_spi:dev=/dev/spidevX.Y\n"); |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | p = extract_programmer_param("speed"); |
| 125 | if (p && strlen(p)) { |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 126 | speed = (uint32_t)strtoul(p, &endp, 10) * 1024; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 127 | if (p == endp) { |
| 128 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
| 129 | return 1; |
| 130 | } |
| 131 | } |
| 132 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 133 | msg_pdbg("Using device %s\n", dev); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 134 | if ((fd = open(dev, O_RDWR)) == -1) { |
| 135 | msg_perr("%s: failed to open %s: %s\n", __func__, |
| 136 | dev, strerror(errno)); |
| 137 | return 1; |
| 138 | } |
| 139 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 140 | if (speed > 0) { |
| 141 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) { |
| 142 | msg_perr("%s: failed to set speed %dHz: %s\n", |
| 143 | __func__, speed, strerror(errno)); |
| 144 | close(fd); |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | msg_pdbg("Using %d kHz clock\n", speed); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (register_shutdown(linux_spi_shutdown, NULL)) |
| 152 | return 1; |
| 153 | |
| 154 | register_spi_programmer(&spi_programmer_linux); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int linux_spi_shutdown(void *data) |
| 160 | { |
| 161 | if (fd != -1) { |
| 162 | close(fd); |
| 163 | fd = -1; |
| 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 169 | const unsigned char *txbuf, unsigned char *rxbuf) |
| 170 | { |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 171 | int msg_start = 0, msg_count = 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 172 | struct spi_ioc_transfer msg[2] = { |
| 173 | { |
uwe | 230909b | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 174 | .tx_buf = (uint64_t)(ptrdiff_t)txbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 175 | .len = writecnt, |
| 176 | }, |
| 177 | { |
uwe | 230909b | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 178 | .rx_buf = (uint64_t)(ptrdiff_t)rxbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 179 | .len = readcnt, |
| 180 | }, |
| 181 | }; |
| 182 | |
| 183 | if (fd == -1) |
| 184 | return -1; |
| 185 | |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 186 | /* 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] | 187 | * which drives an un-expected CS line and clocks. */ |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 188 | if (writecnt) { |
| 189 | msg_start = 0; /* tx: msg[0] */ |
| 190 | msg_count++; |
| 191 | if (readcnt) { |
| 192 | msg_count++; |
| 193 | } |
Louis Yung-Chieh Lo | b121eff | 2012-05-22 23:31:03 +0800 | [diff] [blame] | 194 | } else if (readcnt) { |
| 195 | msg_start = 1; /* rx: msg[1] */ |
| 196 | msg_count++; |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 197 | } else { |
Louis Yung-Chieh Lo | b121eff | 2012-05-22 23:31:03 +0800 | [diff] [blame] | 198 | msg_cerr("%s: both writecnt and readcnt are 0.\n", |
| 199 | __func__); |
| 200 | return -1; |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) { |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 204 | msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno)); |
| 205 | return -1; |
| 206 | } |
| 207 | return 0; |
| 208 | } |
| 209 | |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 210 | static int linux_spi_read(struct flashchip *flash, uint8_t *buf, |
| 211 | unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 212 | { |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 213 | return spi_read_chunked(flash, buf, start, len, SPI_DMA_SIZE); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 217 | unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 218 | { |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 219 | return spi_write_chunked(flash, buf, start, len, |
| 220 | SPI_DMA_SIZE - 5 /* WREN, Page Program */); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 221 | } |