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. |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Stefan Tauner | ee310a4 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 16 | #if CONFIG_LINUX_SPI == 1 |
| 17 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | #include <stdlib.h> |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <inttypes.h> |
Gwenhael Goavec-Merou | f3f3928 | 2015-11-14 02:55:12 +0000 | [diff] [blame] | 23 | #include <fcntl.h> |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 24 | #include <errno.h> |
| 25 | #include <ctype.h> |
| 26 | #include <unistd.h> |
Gwenhael Goavec-Merou | f3f3928 | 2015-11-14 02:55:12 +0000 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
Stefan Tauner | ee310a4 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 28 | #include <linux/types.h> |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 29 | #include "file.h" |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 30 | #include "flash.h" |
| 31 | #include "chipdrivers.h" |
| 32 | #include "programmer.h" |
| 33 | #include "spi.h" |
Fabrice Fontaine | 16cb681 | 2019-07-17 19:04:12 +0200 | [diff] [blame] | 34 | /* |
| 35 | * Linux versions prior to v4.14-rc7 may need linux/ioctl.h included here due |
| 36 | * to missing from linux/spi/spidev.h. This was fixed in the following commit: |
| 37 | * a2b4a79b88b2 spi: uapi: spidev: add missing ioctl header |
| 38 | */ |
| 39 | #include <linux/ioctl.h> |
| 40 | #include <linux/spi/spidev.h> |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 41 | |
Nikolai Artemiev | 552a318 | 2020-06-15 15:41:49 +1000 | [diff] [blame] | 42 | /* Devices known to work with this module (FIXME: export as struct dev_entry): |
| 43 | * Beagle Bone Black |
| 44 | * Raspberry Pi |
| 45 | * HummingBoard |
| 46 | */ |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 47 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 48 | static int fd = -1; |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 49 | #define BUF_SIZE_FROM_SYSFS "/sys/module/spidev/parameters/bufsiz" |
| 50 | static size_t max_kernel_buf_size; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 51 | |
David Hendricks | 93784b4 | 2016-08-09 17:00:38 -0700 | [diff] [blame] | 52 | static int linux_spi_shutdown(void *data); |
Nikolai Artemiev | 552a318 | 2020-06-15 15:41:49 +1000 | [diff] [blame] | 53 | static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
| 54 | unsigned int readcnt, |
| 55 | const unsigned char *txbuf, |
| 56 | unsigned char *rxbuf); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 57 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 58 | unsigned int start, unsigned int len); |
Patrick Georgi | ab8353e | 2017-02-03 18:32:01 +0100 | [diff] [blame] | 59 | static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 60 | unsigned int start, unsigned int len); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 61 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 62 | static const struct spi_master spi_master_linux = { |
Edward O'Callaghan | a6673bd | 2019-06-24 15:22:28 +1000 | [diff] [blame] | 63 | .features = SPI_MASTER_4BA, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 64 | .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 65 | .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 66 | .command = linux_spi_send_command, |
| 67 | .multicommand = default_spi_send_multicommand, |
| 68 | .read = linux_spi_read, |
| 69 | .write_256 = linux_spi_write_256, |
| 70 | }; |
| 71 | |
David Hendricks | 2d06918 | 2015-10-30 22:52:57 -0700 | [diff] [blame] | 72 | static char *check_fdt(void) |
| 73 | { |
| 74 | unsigned int bus, cs; |
| 75 | |
| 76 | if (fdt_find_spi_nor_flash(&bus, &cs) < 0) |
| 77 | return NULL; |
| 78 | |
Nikolai Artemiev | ac7ed8b | 2020-06-26 14:33:06 +1000 | [diff] [blame] | 79 | char *devfs_path = malloc(32); |
David Hendricks | 2d06918 | 2015-10-30 22:52:57 -0700 | [diff] [blame] | 80 | sprintf(devfs_path, "/dev/spidev%u.%u", bus, cs); |
| 81 | return devfs_path; |
| 82 | } |
| 83 | |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 84 | int linux_spi_init(void) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 85 | { |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 86 | char *p, *endp, *dev; |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 87 | uint32_t speed_hz = 2 * 1000 * 1000; |
Stefan Tauner | 180d899 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 88 | /* FIXME: make the following configurable by CLI options. */ |
| 89 | /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */ |
| 90 | const uint8_t mode = SPI_MODE_0; |
| 91 | const uint8_t bits = 8; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 92 | |
David Hendricks | ba0827a | 2013-05-03 20:25:40 -0700 | [diff] [blame] | 93 | /* |
| 94 | * FIXME: There might be other programmers with flash memory (such as |
| 95 | * an EC) connected via SPI. For now we rely on the device's driver to |
| 96 | * distinguish it and assume generic SPI implies host. |
| 97 | */ |
| 98 | if (alias && alias->type != ALIAS_HOST) |
| 99 | return 1; |
| 100 | |
Nikolai Artemiev | 9d0d352 | 2020-06-15 15:58:04 +1000 | [diff] [blame] | 101 | p = extract_programmer_param("spispeed"); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 102 | if (p && strlen(p)) { |
Alexandru Gagniuc | 18fe18f | 2014-03-19 17:17:06 +0000 | [diff] [blame] | 103 | speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000; |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 104 | if (p == endp || speed_hz == 0) { |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 105 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
Nikolai Artemiev | ac7ed8b | 2020-06-26 14:33:06 +1000 | [diff] [blame] | 106 | free(p); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 107 | return 1; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 108 | } |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 109 | } else { |
| 110 | msg_pinfo("Using default %"PRIu32 |
| 111 | "kHz clock. Use 'spispeed' parameter to override.\n", |
| 112 | speed_hz / 1000); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 113 | } |
Nikolai Artemiev | ac7ed8b | 2020-06-26 14:33:06 +1000 | [diff] [blame] | 114 | free(p); |
| 115 | |
| 116 | dev = extract_programmer_param("dev"); |
| 117 | if (!dev) |
| 118 | dev = check_fdt(); |
| 119 | if (!dev || !strlen(dev)) { |
| 120 | msg_perr("No SPI device found. Use flashrom -p " |
| 121 | "linux_spi:dev=/dev/spidevX.Y\n"); |
| 122 | free(dev); |
| 123 | return 1; |
| 124 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 125 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 126 | msg_pdbg("Using device %s\n", dev); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 127 | if ((fd = open(dev, O_RDWR)) == -1) { |
Nikolai Artemiev | 648f127 | 2020-05-19 15:02:46 +1000 | [diff] [blame] | 128 | msg_perr("%s: failed to open %s: %s\n", __func__, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 129 | dev, strerror(errno)); |
Nikolai Artemiev | ac7ed8b | 2020-06-26 14:33:06 +1000 | [diff] [blame] | 130 | free(dev); |
Nikolai Artemiev | 648f127 | 2020-05-19 15:02:46 +1000 | [diff] [blame] | 131 | return 1; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 132 | } |
Nikolai Artemiev | ac7ed8b | 2020-06-26 14:33:06 +1000 | [diff] [blame] | 133 | free(dev); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 134 | |
Stefan Tauner | 180d899 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 135 | if (register_shutdown(linux_spi_shutdown, NULL)) |
| 136 | return 1; |
| 137 | /* We rely on the shutdown function for cleanup from here on. */ |
| 138 | |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 139 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) { |
| 140 | msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n", |
| 141 | __func__, speed_hz, strerror(errno)); |
| 142 | return 1; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 143 | } |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 144 | msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 145 | |
Stefan Tauner | 180d899 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 146 | if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) { |
| 147 | msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n", |
| 148 | __func__, mode, strerror(errno)); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 149 | return 1; |
Stefan Tauner | 180d899 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) { |
| 153 | msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n", |
| 154 | __func__, bits == 0 ? 8 : bits, strerror(errno)); |
| 155 | return 1; |
| 156 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 157 | |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 158 | /* Read max buffer size from sysfs, or use page size as fallback. */ |
| 159 | FILE *fp; |
| 160 | fp = fopen(BUF_SIZE_FROM_SYSFS, "r"); |
| 161 | if (!fp) { |
| 162 | msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno)); |
| 163 | goto out; |
| 164 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 165 | |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 166 | char buf[10]; |
Jacob Garber | 02df559 | 2019-08-12 14:14:40 -0600 | [diff] [blame] | 167 | if (!fgets(buf, sizeof(buf), fp)) { |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 168 | if (feof(fp)) |
| 169 | msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS); |
| 170 | else |
| 171 | msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno)); |
| 172 | goto out; |
| 173 | } |
| 174 | |
| 175 | long int tmp; |
| 176 | errno = 0; |
| 177 | tmp = strtol(buf, NULL, 0); |
| 178 | if ((tmp < 0) || errno) { |
| 179 | msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS); |
| 180 | } else { |
| 181 | msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS); |
| 182 | max_kernel_buf_size = (size_t)tmp; |
| 183 | } |
| 184 | |
| 185 | out: |
| 186 | if (fp) |
| 187 | fclose(fp); |
| 188 | |
| 189 | if (!max_kernel_buf_size) { |
| 190 | msg_pdbg("%s: Using page size as max buffer size.\n", __func__); |
| 191 | max_kernel_buf_size = (size_t)getpagesize(); |
| 192 | } |
| 193 | |
| 194 | msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size); |
| 195 | register_spi_master(&spi_master_linux); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 196 | return 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 197 | } |
| 198 | |
David Hendricks | 93784b4 | 2016-08-09 17:00:38 -0700 | [diff] [blame] | 199 | static int linux_spi_shutdown(void *data) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 200 | { |
| 201 | if (fd != -1) { |
| 202 | close(fd); |
| 203 | fd = -1; |
| 204 | } |
| 205 | return 0; |
| 206 | } |
| 207 | |
Nikolai Artemiev | 552a318 | 2020-06-15 15:41:49 +1000 | [diff] [blame] | 208 | static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
| 209 | unsigned int readcnt, |
| 210 | const unsigned char *txbuf, |
| 211 | unsigned char *rxbuf) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 212 | { |
Michael Karcher | 58dc368 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 213 | int iocontrol_code; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 214 | struct spi_ioc_transfer msg[2] = { |
| 215 | { |
David Riley | 13343b8 | 2014-07-25 11:37:56 -0700 | [diff] [blame] | 216 | .tx_buf = (uint64_t)(uintptr_t)txbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 217 | .len = writecnt, |
| 218 | }, |
| 219 | { |
David Riley | 13343b8 | 2014-07-25 11:37:56 -0700 | [diff] [blame] | 220 | .rx_buf = (uint64_t)(uintptr_t)rxbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 221 | .len = readcnt, |
| 222 | }, |
| 223 | }; |
| 224 | |
| 225 | if (fd == -1) |
| 226 | return -1; |
Michael Karcher | 58dc368 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 227 | /* The implementation currently does not support requests that |
| 228 | don't start with sending a command. */ |
| 229 | if (writecnt == 0) |
| 230 | return SPI_INVALID_LENGTH; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 231 | |
Michael Karcher | 58dc368 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 232 | /* Just submit the first (write) request in case there is nothing |
| 233 | to read. Otherwise submit both requests. */ |
| 234 | if (readcnt == 0) |
| 235 | iocontrol_code = SPI_IOC_MESSAGE(1); |
| 236 | else |
| 237 | iocontrol_code = SPI_IOC_MESSAGE(2); |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 238 | |
Michael Karcher | 58dc368 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 239 | if (ioctl(fd, iocontrol_code, msg) == -1) { |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 240 | msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno)); |
| 241 | return -1; |
| 242 | } |
| 243 | return 0; |
| 244 | } |
| 245 | |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 246 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 247 | { |
Nico Huber | b5ebaaf | 2018-03-08 16:14:15 +0100 | [diff] [blame] | 248 | /* Older kernels use a single buffer for combined input and output |
| 249 | data. So account for longest possible command + address, too. */ |
| 250 | return spi_read_chunked(flash, buf, start, len, max_kernel_buf_size - 5); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Nikolai Artemiev | 552a318 | 2020-06-15 15:41:49 +1000 | [diff] [blame] | 253 | static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 254 | { |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 255 | /* 5 bytes must be reserved for longest possible command + address. */ |
| 256 | return spi_write_chunked(flash, buf, start, len, max_kernel_buf_size - 5); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 257 | } |
Stefan Tauner | ee310a4 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 258 | |
| 259 | #endif // CONFIG_LINUX_SPI == 1 |