blob: 2dd17824d85fa0569dfc411653584ef89654c346 [file] [log] [blame]
uwe7df6dda2011-09-03 18:37:52 +00001/*
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.
uwe7df6dda2011-09-03 18:37:52 +000014 */
15
Stefan Tauneree310a42012-03-13 00:18:19 +000016#if CONFIG_LINUX_SPI == 1
17
uwe7df6dda2011-09-03 18:37:52 +000018#include <stdio.h>
19#include <string.h>
20#include <stdlib.h>
Nico Huber4b941a92018-12-22 00:08:50 +010021#include <stdint.h>
22#include <inttypes.h>
Gwenhael Goavec-Merouf3f39282015-11-14 02:55:12 +000023#include <fcntl.h>
uwe7df6dda2011-09-03 18:37:52 +000024#include <errno.h>
25#include <ctype.h>
26#include <unistd.h>
Gwenhael Goavec-Merouf3f39282015-11-14 02:55:12 +000027#include <sys/ioctl.h>
Stefan Tauneree310a42012-03-13 00:18:19 +000028#include <linux/types.h>
David Hendricks98bbc262013-10-30 21:38:58 -070029#include "file.h"
uwe7df6dda2011-09-03 18:37:52 +000030#include "flash.h"
31#include "chipdrivers.h"
32#include "programmer.h"
33#include "spi.h"
Fabrice Fontaine16cb6812019-07-17 19:04:12 +020034/*
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>
uwe7df6dda2011-09-03 18:37:52 +000041
Nikolai Artemiev552a3182020-06-15 15:41:49 +100042/* 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 Lo282c2412012-07-27 18:24:37 +080047
Aileen Cheng5556b942020-07-06 09:12:48 -070048#define MODALIAS_FILE "modalias"
49#define LINUX_SPI_SYSFS_ROOT "/sys/bus/spi/devices"
50
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +100051/* At least big enough to fit /dev/spidevX.Y */
52#define DEVFS_PATH_LEN 32
53
uwe7df6dda2011-09-03 18:37:52 +000054static int fd = -1;
Keno Fischer556e4782015-11-15 14:58:25 +000055#define BUF_SIZE_FROM_SYSFS "/sys/module/spidev/parameters/bufsiz"
56static size_t max_kernel_buf_size;
uwe7df6dda2011-09-03 18:37:52 +000057
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110058static int linux_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
59{
60 /* Older kernels use a single buffer for combined input and output
61 data. So account for longest possible command + address, too. */
62 return spi_read_chunked(flash, buf, start, len, max_kernel_buf_size - 5);
63}
64
65static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
66{
67 /* 5 bytes must be reserved for longest possible command + address. */
68 return spi_write_chunked(flash, buf, start, len, max_kernel_buf_size - 5);
69}
70
71static int linux_spi_shutdown(void *data)
72{
73 if (fd != -1) {
74 close(fd);
75 fd = -1;
76 }
77 return 0;
78}
79
Nikolai Artemiev552a3182020-06-15 15:41:49 +100080static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
81 unsigned int readcnt,
82 const unsigned char *txbuf,
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110083 unsigned char *rxbuf)
84{
85 int iocontrol_code;
86 struct spi_ioc_transfer msg[2] = {
87 {
88 .tx_buf = (uint64_t)(uintptr_t)txbuf,
89 .len = writecnt,
90 },
91 {
92 .rx_buf = (uint64_t)(uintptr_t)rxbuf,
93 .len = readcnt,
94 },
95 };
96
97 if (fd == -1)
98 return -1;
99 /* The implementation currently does not support requests that
100 don't start with sending a command. */
101 if (writecnt == 0)
102 return SPI_INVALID_LENGTH;
103
104 /* Just submit the first (write) request in case there is nothing
105 to read. Otherwise submit both requests. */
106 if (readcnt == 0)
107 iocontrol_code = SPI_IOC_MESSAGE(1);
108 else
109 iocontrol_code = SPI_IOC_MESSAGE(2);
110
111 if (ioctl(fd, iocontrol_code, msg) == -1) {
112 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
113 return -1;
114 }
115 return 0;
116}
uwe7df6dda2011-09-03 18:37:52 +0000117
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100118static const struct spi_master spi_master_linux = {
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000119 .features = SPI_MASTER_4BA,
uwe7df6dda2011-09-03 18:37:52 +0000120 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
121 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
122 .command = linux_spi_send_command,
123 .multicommand = default_spi_send_multicommand,
124 .read = linux_spi_read,
125 .write_256 = linux_spi_write_256,
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100126 .write_aai = default_spi_write_aai,
uwe7df6dda2011-09-03 18:37:52 +0000127};
128
Aileen Cheng5556b942020-07-06 09:12:48 -0700129static char *check_sysfs(void)
130{
Edward O'Callaghan7e8f3132020-11-13 23:06:49 +1100131 unsigned i;
Aileen Cheng5556b942020-07-06 09:12:48 -0700132 const char *sysfs_path = NULL;
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000133 char *devfs_path = NULL;
Aileen Cheng5556b942020-07-06 09:12:48 -0700134 char *p;
Nikolai Artemievf8cafd22021-02-12 11:45:11 +1100135 const char *modalias[] = {
Aileen Cheng5556b942020-07-06 09:12:48 -0700136 "spi:spidev", /* raw access over SPI bus (newer kernels) */
137 "spidev", /* raw access over SPI bus (older kernels) */
138 "m25p80", /* generic MTD device */
139 };
140
141 for (i = 0; i < ARRAY_SIZE(modalias); i++) {
142 int major, minor;
143
144 /* Path should look like: /sys/blah/spiX.Y/modalias */
145 sysfs_path = scanft(LINUX_SPI_SYSFS_ROOT,
146 MODALIAS_FILE, modalias[i], 1);
147 if (!sysfs_path)
148 continue;
149
150 p = (char *)sysfs_path + strlen(LINUX_SPI_SYSFS_ROOT);
151 if (p[0] == '/')
152 p++;
153
154 if (sscanf(p, "spi%u.%u", &major, &minor) == 2) {
155 msg_pdbg("Found SPI device %s on spi%u.%u\n",
156 modalias[i], major, minor);
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000157 devfs_path = calloc(1, DEVFS_PATH_LEN);
158 snprintf(devfs_path, DEVFS_PATH_LEN, "/dev/spidev%u.%u", major, minor);
Aileen Cheng5556b942020-07-06 09:12:48 -0700159 free((void *)sysfs_path);
160 break;
161 }
162 free((void *)sysfs_path);
163 }
164
165 if (i == ARRAY_SIZE(modalias))
166 return NULL;
167 return devfs_path;
168}
169
David Hendricks2d069182015-10-30 22:52:57 -0700170static char *check_fdt(void)
171{
172 unsigned int bus, cs;
173
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000174 char *devfs_path = calloc(1, DEVFS_PATH_LEN);
David Hendricks2d069182015-10-30 22:52:57 -0700175 if (fdt_find_spi_nor_flash(&bus, &cs) < 0)
176 return NULL;
177
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000178 snprintf(devfs_path, DEVFS_PATH_LEN, "/dev/spidev%u.%u", bus, cs);
David Hendricks2d069182015-10-30 22:52:57 -0700179 return devfs_path;
180}
181
Aileen Cheng5556b942020-07-06 09:12:48 -0700182static char *linux_spi_probe(void)
183{
184 char *ret;
185
186 ret = check_fdt();
187 if (ret)
188 return ret;
189
190 ret = check_sysfs();
191 if (ret)
192 return ret;
193
194 return NULL;
195}
196
David Hendricksac1d25c2016-08-09 17:00:58 -0700197int linux_spi_init(void)
uwe7df6dda2011-09-03 18:37:52 +0000198{
David Hendricks98b3c572016-11-30 01:50:08 +0000199 char *p, *endp, *dev;
Nico Huber4b941a92018-12-22 00:08:50 +0100200 uint32_t speed_hz = 2 * 1000 * 1000;
Stefan Tauner180d8992012-03-03 18:09:33 +0000201 /* FIXME: make the following configurable by CLI options. */
202 /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */
203 const uint8_t mode = SPI_MODE_0;
204 const uint8_t bits = 8;
uwe7df6dda2011-09-03 18:37:52 +0000205
Nikolai Artemiev9d0d3522020-06-15 15:58:04 +1000206 p = extract_programmer_param("spispeed");
uwe7df6dda2011-09-03 18:37:52 +0000207 if (p && strlen(p)) {
Alexandru Gagniuc18fe18f2014-03-19 17:17:06 +0000208 speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000;
Nico Huber4b941a92018-12-22 00:08:50 +0100209 if (p == endp || speed_hz == 0) {
uwe7df6dda2011-09-03 18:37:52 +0000210 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000211 free(p);
David Hendricks98b3c572016-11-30 01:50:08 +0000212 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000213 }
Nico Huber4b941a92018-12-22 00:08:50 +0100214 } else {
215 msg_pinfo("Using default %"PRIu32
216 "kHz clock. Use 'spispeed' parameter to override.\n",
217 speed_hz / 1000);
uwe7df6dda2011-09-03 18:37:52 +0000218 }
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000219 free(p);
220
221 dev = extract_programmer_param("dev");
222 if (!dev)
223 dev = linux_spi_probe();
224 if (!dev || !strlen(dev)) {
225 msg_perr("No SPI device found. Use flashrom -p "
226 "linux_spi:dev=/dev/spidevX.Y\n");
227 free(dev);
228 return 1;
229 }
uwe7df6dda2011-09-03 18:37:52 +0000230
uwe6b716f02011-09-07 20:48:34 +0000231 msg_pdbg("Using device %s\n", dev);
uwe7df6dda2011-09-03 18:37:52 +0000232 if ((fd = open(dev, O_RDWR)) == -1) {
Nikolai Artemiev648f1272020-05-19 15:02:46 +1000233 msg_perr("%s: failed to open %s: %s\n", __func__,
uwe7df6dda2011-09-03 18:37:52 +0000234 dev, strerror(errno));
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000235 free(dev);
Nikolai Artemiev648f1272020-05-19 15:02:46 +1000236 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000237 }
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000238 free(dev);
uwe7df6dda2011-09-03 18:37:52 +0000239
Stefan Tauner180d8992012-03-03 18:09:33 +0000240 if (register_shutdown(linux_spi_shutdown, NULL))
241 return 1;
242 /* We rely on the shutdown function for cleanup from here on. */
243
Nico Huber4b941a92018-12-22 00:08:50 +0100244 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) {
245 msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n",
246 __func__, speed_hz, strerror(errno));
247 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000248 }
Nico Huber4b941a92018-12-22 00:08:50 +0100249 msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000);
uwe7df6dda2011-09-03 18:37:52 +0000250
Stefan Tauner180d8992012-03-03 18:09:33 +0000251 if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
252 msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n",
253 __func__, mode, strerror(errno));
David Hendricks98b3c572016-11-30 01:50:08 +0000254 return 1;
Stefan Tauner180d8992012-03-03 18:09:33 +0000255 }
256
257 if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
258 msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n",
259 __func__, bits == 0 ? 8 : bits, strerror(errno));
260 return 1;
261 }
uwe7df6dda2011-09-03 18:37:52 +0000262
Keno Fischer556e4782015-11-15 14:58:25 +0000263 /* Read max buffer size from sysfs, or use page size as fallback. */
264 FILE *fp;
265 fp = fopen(BUF_SIZE_FROM_SYSFS, "r");
266 if (!fp) {
267 msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
268 goto out;
269 }
uwe7df6dda2011-09-03 18:37:52 +0000270
Keno Fischer556e4782015-11-15 14:58:25 +0000271 char buf[10];
Jacob Garber02df5592019-08-12 14:14:40 -0600272 if (!fgets(buf, sizeof(buf), fp)) {
Keno Fischer556e4782015-11-15 14:58:25 +0000273 if (feof(fp))
274 msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS);
275 else
276 msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
277 goto out;
278 }
279
280 long int tmp;
281 errno = 0;
282 tmp = strtol(buf, NULL, 0);
283 if ((tmp < 0) || errno) {
284 msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS);
285 } else {
286 msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS);
287 max_kernel_buf_size = (size_t)tmp;
288 }
289
290out:
291 if (fp)
292 fclose(fp);
293
294 if (!max_kernel_buf_size) {
295 msg_pdbg("%s: Using page size as max buffer size.\n", __func__);
296 max_kernel_buf_size = (size_t)getpagesize();
297 }
298
299 msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size);
300 register_spi_master(&spi_master_linux);
David Hendricks98b3c572016-11-30 01:50:08 +0000301 return 0;
uwe7df6dda2011-09-03 18:37:52 +0000302}
303
Stefan Tauneree310a42012-03-13 00:18:19 +0000304#endif // CONFIG_LINUX_SPI == 1