Sven Schnelle | 5ce5f70 | 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. |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Stefan Tauner | 8868db3 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 16 | #if CONFIG_LINUX_SPI == 1 |
| 17 | |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | #include <stdlib.h> |
Nico Huber | 9cecc7e | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <inttypes.h> |
Gwenhael Goavec-Merou | 8cd0c73 | 2015-11-14 02:55:12 +0000 | [diff] [blame] | 23 | #include <fcntl.h> |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 24 | #include <errno.h> |
| 25 | #include <ctype.h> |
| 26 | #include <unistd.h> |
Gwenhael Goavec-Merou | 8cd0c73 | 2015-11-14 02:55:12 +0000 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
Stefan Tauner | 8868db3 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 28 | #include <linux/types.h> |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 29 | #include "flash.h" |
| 30 | #include "chipdrivers.h" |
| 31 | #include "programmer.h" |
| 32 | #include "spi.h" |
Fabrice Fontaine | e0ceedf | 2019-07-17 19:04:12 +0200 | [diff] [blame] | 33 | /* |
| 34 | * Linux versions prior to v4.14-rc7 may need linux/ioctl.h included here due |
| 35 | * to missing from linux/spi/spidev.h. This was fixed in the following commit: |
| 36 | * a2b4a79b88b2 spi: uapi: spidev: add missing ioctl header |
| 37 | */ |
| 38 | #include <linux/ioctl.h> |
| 39 | #include <linux/spi/spidev.h> |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 40 | |
Stefan Tauner | 23e10b8 | 2016-01-23 16:16:49 +0000 | [diff] [blame] | 41 | /* Devices known to work with this module (FIXME: export as struct dev_entry): |
| 42 | * Beagle Bone Black |
| 43 | * Raspberry Pi |
| 44 | * HummingBoard |
| 45 | */ |
| 46 | |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 47 | static int fd = -1; |
Keno Fischer | 1f33cb5 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 48 | #define BUF_SIZE_FROM_SYSFS "/sys/module/spidev/parameters/bufsiz" |
| 49 | static size_t max_kernel_buf_size; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 50 | |
| 51 | static int linux_spi_shutdown(void *data); |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 52 | static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 53 | unsigned int readcnt, |
| 54 | const unsigned char *txbuf, |
| 55 | unsigned char *rxbuf); |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 56 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 57 | unsigned int start, unsigned int len); |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 58 | static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 59 | unsigned int start, unsigned int len); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 60 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 61 | static const struct spi_master spi_master_linux = { |
Nico Huber | 1cf407b | 2017-11-10 20:18:23 +0100 | [diff] [blame] | 62 | .features = SPI_MASTER_4BA, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 63 | .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 64 | .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 65 | .command = linux_spi_send_command, |
| 66 | .multicommand = default_spi_send_multicommand, |
| 67 | .read = linux_spi_read, |
| 68 | .write_256 = linux_spi_write_256, |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 69 | .write_aai = default_spi_write_aai, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Anastasia Klimchuk | c19ef9b | 2021-04-12 16:52:58 +1000 | [diff] [blame] | 72 | /* Read max buffer size from sysfs, or use page size as fallback. */ |
| 73 | static size_t get_max_kernel_buf_size() { |
| 74 | size_t result = 0; |
| 75 | FILE *fp; |
| 76 | fp = fopen(BUF_SIZE_FROM_SYSFS, "r"); |
| 77 | if (!fp) { |
| 78 | msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno)); |
| 79 | goto out; |
| 80 | } |
| 81 | |
| 82 | char buf[10]; |
| 83 | if (!fgets(buf, sizeof(buf), fp)) { |
| 84 | if (feof(fp)) |
| 85 | msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS); |
| 86 | else |
| 87 | msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno)); |
| 88 | goto out; |
| 89 | } |
| 90 | |
| 91 | long int tmp; |
| 92 | errno = 0; |
| 93 | tmp = strtol(buf, NULL, 0); |
| 94 | if ((tmp < 0) || errno) { |
| 95 | msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS); |
| 96 | } else { |
| 97 | msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS); |
| 98 | result = (size_t)tmp; |
| 99 | } |
| 100 | |
| 101 | out: |
| 102 | if (fp) |
| 103 | fclose(fp); |
| 104 | |
| 105 | if (!result) { |
| 106 | msg_pdbg("%s: Using page size as max buffer size.\n", __func__); |
| 107 | result = (size_t)getpagesize(); |
| 108 | } |
| 109 | return result; |
| 110 | } |
| 111 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 112 | static int linux_spi_init(void) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 113 | { |
| 114 | char *p, *endp, *dev; |
Nico Huber | 9cecc7e | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 115 | uint32_t speed_hz = 2 * 1000 * 1000; |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 116 | /* FIXME: make the following configurable by CLI options. */ |
| 117 | /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */ |
| 118 | const uint8_t mode = SPI_MODE_0; |
| 119 | const uint8_t bits = 8; |
Anastasia Klimchuk | f876c0f | 2021-04-13 10:15:26 +1000 | [diff] [blame^] | 120 | int ret = 0; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 121 | |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 122 | p = extract_programmer_param("spispeed"); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 123 | if (p && strlen(p)) { |
Alexandru Gagniuc | 69dd09d | 2014-03-19 17:17:06 +0000 | [diff] [blame] | 124 | speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000; |
Nico Huber | 9cecc7e | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 125 | if (p == endp || speed_hz == 0) { |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 126 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 127 | free(p); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 128 | return 1; |
| 129 | } |
Nico Huber | 9cecc7e | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 130 | } else { |
| 131 | msg_pinfo("Using default %"PRIu32 |
| 132 | "kHz clock. Use 'spispeed' parameter to override.\n", |
| 133 | speed_hz / 1000); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 134 | } |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 135 | free(p); |
| 136 | |
| 137 | dev = extract_programmer_param("dev"); |
| 138 | if (!dev || !strlen(dev)) { |
| 139 | msg_perr("No SPI device given. Use flashrom -p " |
| 140 | "linux_spi:dev=/dev/spidevX.Y\n"); |
| 141 | free(dev); |
| 142 | return 1; |
| 143 | } |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 144 | |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 145 | msg_pdbg("Using device %s\n", dev); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 146 | if ((fd = open(dev, O_RDWR)) == -1) { |
| 147 | msg_perr("%s: failed to open %s: %s\n", __func__, |
| 148 | dev, strerror(errno)); |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 149 | free(dev); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 150 | return 1; |
| 151 | } |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 152 | free(dev); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 153 | |
Nico Huber | 9cecc7e | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 154 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) { |
| 155 | msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n", |
| 156 | __func__, speed_hz, strerror(errno)); |
Anastasia Klimchuk | f876c0f | 2021-04-13 10:15:26 +1000 | [diff] [blame^] | 157 | ret = 1; |
| 158 | goto init_err; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 159 | } |
Nico Huber | 9cecc7e | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 160 | msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 161 | |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 162 | if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) { |
| 163 | msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n", |
| 164 | __func__, mode, strerror(errno)); |
Anastasia Klimchuk | f876c0f | 2021-04-13 10:15:26 +1000 | [diff] [blame^] | 165 | ret = 1; |
| 166 | goto init_err; |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) { |
| 170 | msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n", |
| 171 | __func__, bits == 0 ? 8 : bits, strerror(errno)); |
Anastasia Klimchuk | f876c0f | 2021-04-13 10:15:26 +1000 | [diff] [blame^] | 172 | ret = 1; |
| 173 | goto init_err; |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 174 | } |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 175 | |
Anastasia Klimchuk | c19ef9b | 2021-04-12 16:52:58 +1000 | [diff] [blame] | 176 | max_kernel_buf_size = get_max_kernel_buf_size(); |
Keno Fischer | 1f33cb5 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 177 | msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size); |
Anastasia Klimchuk | c19ef9b | 2021-04-12 16:52:58 +1000 | [diff] [blame] | 178 | |
Anastasia Klimchuk | f876c0f | 2021-04-13 10:15:26 +1000 | [diff] [blame^] | 179 | if (register_shutdown(linux_spi_shutdown, NULL)) { |
| 180 | ret = 1; |
| 181 | goto init_err; |
| 182 | } |
Keno Fischer | 1f33cb5 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 183 | register_spi_master(&spi_master_linux); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 184 | return 0; |
Anastasia Klimchuk | f876c0f | 2021-04-13 10:15:26 +1000 | [diff] [blame^] | 185 | |
| 186 | init_err: |
| 187 | if (fd != -1) { |
| 188 | close(fd); |
| 189 | fd = -1; |
| 190 | } |
| 191 | return ret; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static int linux_spi_shutdown(void *data) |
| 195 | { |
| 196 | if (fd != -1) { |
| 197 | close(fd); |
| 198 | fd = -1; |
| 199 | } |
| 200 | return 0; |
| 201 | } |
| 202 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 203 | static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 204 | unsigned int readcnt, |
| 205 | const unsigned char *txbuf, |
| 206 | unsigned char *rxbuf) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 207 | { |
Michael Karcher | 8371d72 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 208 | int iocontrol_code; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 209 | struct spi_ioc_transfer msg[2] = { |
| 210 | { |
David Riley | 18f5097 | 2014-08-05 22:16:01 +0000 | [diff] [blame] | 211 | .tx_buf = (uint64_t)(uintptr_t)txbuf, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 212 | .len = writecnt, |
| 213 | }, |
| 214 | { |
David Riley | 18f5097 | 2014-08-05 22:16:01 +0000 | [diff] [blame] | 215 | .rx_buf = (uint64_t)(uintptr_t)rxbuf, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 216 | .len = readcnt, |
| 217 | }, |
| 218 | }; |
| 219 | |
| 220 | if (fd == -1) |
| 221 | return -1; |
Michael Karcher | 8371d72 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 222 | /* The implementation currently does not support requests that |
| 223 | don't start with sending a command. */ |
| 224 | if (writecnt == 0) |
| 225 | return SPI_INVALID_LENGTH; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 226 | |
Michael Karcher | 8371d72 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 227 | /* Just submit the first (write) request in case there is nothing |
| 228 | to read. Otherwise submit both requests. */ |
| 229 | if (readcnt == 0) |
| 230 | iocontrol_code = SPI_IOC_MESSAGE(1); |
| 231 | else |
| 232 | iocontrol_code = SPI_IOC_MESSAGE(2); |
| 233 | |
| 234 | if (ioctl(fd, iocontrol_code, msg) == -1) { |
Sven Schnelle | 5ce5f70 | 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 | |
Keno Fischer | 1f33cb5 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 241 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 242 | { |
Nico Huber | 2241842 | 2018-03-08 16:14:15 +0100 | [diff] [blame] | 243 | /* Older kernels use a single buffer for combined input and output |
| 244 | data. So account for longest possible command + address, too. */ |
| 245 | return spi_read_chunked(flash, buf, start, len, max_kernel_buf_size - 5); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 248 | static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 249 | { |
Keno Fischer | 1f33cb5 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 250 | /* 5 bytes must be reserved for longest possible command + address. */ |
| 251 | return spi_write_chunked(flash, buf, start, len, max_kernel_buf_size - 5); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 252 | } |
Stefan Tauner | 8868db3 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 253 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 254 | const struct programmer_entry programmer_linux_spi = { |
| 255 | .name = "linux_spi", |
| 256 | .type = OTHER, |
| 257 | .devs.note = "Device files /dev/spidev*.*\n", |
| 258 | .init = linux_spi_init, |
| 259 | .map_flash_region = fallback_map, |
| 260 | .unmap_flash_region = fallback_unmap, |
| 261 | .delay = internal_delay, |
| 262 | }; |
| 263 | |
Stefan Tauner | 8868db3 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 264 | #endif // CONFIG_LINUX_SPI == 1 |