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