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> |
uwe | 7df6dda | 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 | 16cb681 | 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> |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 40 | |
Nikolai Artemiev | 552a318 | 2020-06-15 15:41:49 +1000 | [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 | */ |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 46 | |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 47 | #define BUF_SIZE_FROM_SYSFS "/sys/module/spidev/parameters/bufsiz" |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 48 | |
| 49 | struct linux_spi_data { |
| 50 | int fd; |
| 51 | size_t max_kernel_buf_size; |
| 52 | }; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 53 | |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 54 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
| 55 | { |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 56 | struct linux_spi_data *spi_data = flash->mst->spi.data; |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 57 | /* Older kernels use a single buffer for combined input and output |
| 58 | data. So account for longest possible command + address, too. */ |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 59 | return spi_read_chunked(flash, buf, start, len, spi_data->max_kernel_buf_size - 5); |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
| 63 | { |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 64 | struct linux_spi_data *spi_data = flash->mst->spi.data; |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 65 | /* 5 bytes must be reserved for longest possible command + address. */ |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 66 | return spi_write_chunked(flash, buf, start, len, spi_data->max_kernel_buf_size - 5); |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static int linux_spi_shutdown(void *data) |
| 70 | { |
Anastasia Klimchuk | 339cb20 | 2021-04-19 11:12:01 +1000 | [diff] [blame] | 71 | struct linux_spi_data *spi_data = data; |
| 72 | close(spi_data->fd); |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 73 | free(spi_data); |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
Nikolai Artemiev | 552a318 | 2020-06-15 15:41:49 +1000 | [diff] [blame] | 77 | static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
| 78 | unsigned int readcnt, |
| 79 | const unsigned char *txbuf, |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 80 | unsigned char *rxbuf) |
| 81 | { |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 82 | struct linux_spi_data *spi_data = flash->mst->spi.data; |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 83 | int iocontrol_code; |
| 84 | struct spi_ioc_transfer msg[2] = { |
| 85 | { |
| 86 | .tx_buf = (uint64_t)(uintptr_t)txbuf, |
| 87 | .len = writecnt, |
| 88 | }, |
| 89 | { |
| 90 | .rx_buf = (uint64_t)(uintptr_t)rxbuf, |
| 91 | .len = readcnt, |
| 92 | }, |
| 93 | }; |
| 94 | |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 95 | if (spi_data->fd == -1) |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 96 | return -1; |
| 97 | /* The implementation currently does not support requests that |
| 98 | don't start with sending a command. */ |
| 99 | if (writecnt == 0) |
| 100 | return SPI_INVALID_LENGTH; |
| 101 | |
| 102 | /* Just submit the first (write) request in case there is nothing |
| 103 | to read. Otherwise submit both requests. */ |
| 104 | if (readcnt == 0) |
| 105 | iocontrol_code = SPI_IOC_MESSAGE(1); |
| 106 | else |
| 107 | iocontrol_code = SPI_IOC_MESSAGE(2); |
| 108 | |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 109 | if (ioctl(spi_data->fd, iocontrol_code, msg) == -1) { |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 110 | msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno)); |
| 111 | return -1; |
| 112 | } |
| 113 | return 0; |
| 114 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 115 | |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 116 | static const struct spi_master spi_master_linux = { |
Edward O'Callaghan | a6673bd | 2019-06-24 15:22:28 +1000 | [diff] [blame] | 117 | .features = SPI_MASTER_4BA, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 118 | .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 119 | .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 120 | .command = linux_spi_send_command, |
| 121 | .multicommand = default_spi_send_multicommand, |
| 122 | .read = linux_spi_read, |
| 123 | .write_256 = linux_spi_write_256, |
Edward O'Callaghan | eeaac6b | 2020-10-12 19:51:56 +1100 | [diff] [blame] | 124 | .write_aai = default_spi_write_aai, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
Anastasia Klimchuk | a7cd03d | 2021-04-12 16:52:58 +1000 | [diff] [blame] | 127 | /* Read max buffer size from sysfs, or use page size as fallback. */ |
Anastasia Klimchuk | 339cb20 | 2021-04-19 11:12:01 +1000 | [diff] [blame] | 128 | static size_t get_max_kernel_buf_size() |
| 129 | { |
Anastasia Klimchuk | a7cd03d | 2021-04-12 16:52:58 +1000 | [diff] [blame] | 130 | size_t result = 0; |
| 131 | FILE *fp; |
| 132 | fp = fopen(BUF_SIZE_FROM_SYSFS, "r"); |
| 133 | if (!fp) { |
| 134 | msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno)); |
| 135 | goto out; |
| 136 | } |
| 137 | |
| 138 | char buf[10]; |
| 139 | if (!fgets(buf, sizeof(buf), fp)) { |
| 140 | if (feof(fp)) |
| 141 | msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS); |
| 142 | else |
| 143 | msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno)); |
| 144 | goto out; |
| 145 | } |
| 146 | |
| 147 | long int tmp; |
| 148 | errno = 0; |
| 149 | tmp = strtol(buf, NULL, 0); |
| 150 | if ((tmp < 0) || errno) { |
| 151 | msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS); |
| 152 | } else { |
| 153 | msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS); |
| 154 | result = (size_t)tmp; |
| 155 | } |
| 156 | |
| 157 | out: |
| 158 | if (fp) |
| 159 | fclose(fp); |
| 160 | |
| 161 | if (!result) { |
| 162 | msg_pdbg("%s: Using page size as max buffer size.\n", __func__); |
| 163 | result = (size_t)getpagesize(); |
| 164 | } |
| 165 | return result; |
| 166 | } |
| 167 | |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 168 | int linux_spi_init(void) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 169 | { |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 170 | char *p, *endp, *dev; |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 171 | uint32_t speed_hz = 2 * 1000 * 1000; |
Stefan Tauner | 180d899 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 172 | /* FIXME: make the following configurable by CLI options. */ |
| 173 | /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */ |
| 174 | const uint8_t mode = SPI_MODE_0; |
| 175 | const uint8_t bits = 8; |
Anastasia Klimchuk | 339cb20 | 2021-04-19 11:12:01 +1000 | [diff] [blame] | 176 | int fd; |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 177 | size_t max_kernel_buf_size; |
| 178 | struct linux_spi_data *spi_data; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 179 | |
Nikolai Artemiev | 9d0d352 | 2020-06-15 15:58:04 +1000 | [diff] [blame] | 180 | p = extract_programmer_param("spispeed"); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 181 | if (p && strlen(p)) { |
Alexandru Gagniuc | 18fe18f | 2014-03-19 17:17:06 +0000 | [diff] [blame] | 182 | speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000; |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 183 | if (p == endp || speed_hz == 0) { |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 184 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
Nikolai Artemiev | 8ac099f | 2020-08-31 11:58:11 +1000 | [diff] [blame] | 185 | free(p); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 186 | return 1; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 187 | } |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 188 | } else { |
| 189 | msg_pinfo("Using default %"PRIu32 |
| 190 | "kHz clock. Use 'spispeed' parameter to override.\n", |
| 191 | speed_hz / 1000); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 192 | } |
Nikolai Artemiev | 8ac099f | 2020-08-31 11:58:11 +1000 | [diff] [blame] | 193 | free(p); |
| 194 | |
| 195 | dev = extract_programmer_param("dev"); |
Nikolai Artemiev | 8ac099f | 2020-08-31 11:58:11 +1000 | [diff] [blame] | 196 | if (!dev || !strlen(dev)) { |
Nikolai Artemiev | 6e8c531 | 2021-05-18 16:24:49 +1000 | [diff] [blame^] | 197 | msg_perr("No SPI device given. Use flashrom -p " |
Nikolai Artemiev | 8ac099f | 2020-08-31 11:58:11 +1000 | [diff] [blame] | 198 | "linux_spi:dev=/dev/spidevX.Y\n"); |
| 199 | free(dev); |
| 200 | return 1; |
| 201 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 202 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 203 | msg_pdbg("Using device %s\n", dev); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 204 | if ((fd = open(dev, O_RDWR)) == -1) { |
Nikolai Artemiev | 648f127 | 2020-05-19 15:02:46 +1000 | [diff] [blame] | 205 | msg_perr("%s: failed to open %s: %s\n", __func__, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 206 | dev, strerror(errno)); |
Nikolai Artemiev | 8ac099f | 2020-08-31 11:58:11 +1000 | [diff] [blame] | 207 | free(dev); |
Nikolai Artemiev | 648f127 | 2020-05-19 15:02:46 +1000 | [diff] [blame] | 208 | return 1; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 209 | } |
Nikolai Artemiev | 8ac099f | 2020-08-31 11:58:11 +1000 | [diff] [blame] | 210 | free(dev); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 211 | |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 212 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) { |
| 213 | msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n", |
| 214 | __func__, speed_hz, strerror(errno)); |
Anastasia Klimchuk | b8911bb | 2021-04-13 10:15:26 +1000 | [diff] [blame] | 215 | goto init_err; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 216 | } |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame] | 217 | msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 218 | |
Stefan Tauner | 180d899 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 219 | if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) { |
| 220 | msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n", |
| 221 | __func__, mode, strerror(errno)); |
Anastasia Klimchuk | b8911bb | 2021-04-13 10:15:26 +1000 | [diff] [blame] | 222 | goto init_err; |
Stefan Tauner | 180d899 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) { |
| 226 | msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n", |
| 227 | __func__, bits == 0 ? 8 : bits, strerror(errno)); |
Anastasia Klimchuk | b8911bb | 2021-04-13 10:15:26 +1000 | [diff] [blame] | 228 | goto init_err; |
Stefan Tauner | 180d899 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 229 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 230 | |
Anastasia Klimchuk | a7cd03d | 2021-04-12 16:52:58 +1000 | [diff] [blame] | 231 | max_kernel_buf_size = get_max_kernel_buf_size(); |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 232 | msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size); |
Anastasia Klimchuk | a7cd03d | 2021-04-12 16:52:58 +1000 | [diff] [blame] | 233 | |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 234 | spi_data = calloc(1, sizeof(*spi_data)); |
| 235 | if (!spi_data) { |
| 236 | msg_perr("Unable to allocated space for SPI master data\n"); |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 237 | goto init_err; |
| 238 | } |
| 239 | spi_data->fd = fd; |
| 240 | spi_data->max_kernel_buf_size = max_kernel_buf_size; |
Anastasia Klimchuk | 15315d6 | 2021-04-13 11:26:25 +1000 | [diff] [blame] | 241 | |
| 242 | if (register_shutdown(linux_spi_shutdown, spi_data)) { |
| 243 | free(spi_data); |
Anastasia Klimchuk | b8911bb | 2021-04-13 10:15:26 +1000 | [diff] [blame] | 244 | goto init_err; |
| 245 | } |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 246 | register_spi_master(&spi_master_linux, spi_data); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 247 | return 0; |
Anastasia Klimchuk | b8911bb | 2021-04-13 10:15:26 +1000 | [diff] [blame] | 248 | |
| 249 | init_err: |
Anastasia Klimchuk | 339cb20 | 2021-04-19 11:12:01 +1000 | [diff] [blame] | 250 | close(fd); |
| 251 | return 1; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Stefan Tauner | ee310a4 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 254 | #endif // CONFIG_LINUX_SPI == 1 |