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