blob: fcb1431e042d1750b32d11902b3c1bf939e64a5c [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
Keno Fischer556e4782015-11-15 14:58:25 +000054#define BUF_SIZE_FROM_SYSFS "/sys/module/spidev/parameters/bufsiz"
Anastasia Klimchuk15315d62021-04-13 11:26:25 +100055
56struct linux_spi_data {
57 int fd;
58 size_t max_kernel_buf_size;
59};
uwe7df6dda2011-09-03 18:37:52 +000060
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110061static int linux_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
62{
Anastasia Klimchuk15315d62021-04-13 11:26:25 +100063 struct linux_spi_data *spi_data = flash->mst->spi.data;
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110064 /* Older kernels use a single buffer for combined input and output
65 data. So account for longest possible command + address, too. */
Anastasia Klimchuk15315d62021-04-13 11:26:25 +100066 return spi_read_chunked(flash, buf, start, len, spi_data->max_kernel_buf_size - 5);
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110067}
68
69static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
70{
Anastasia Klimchuk15315d62021-04-13 11:26:25 +100071 struct linux_spi_data *spi_data = flash->mst->spi.data;
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110072 /* 5 bytes must be reserved for longest possible command + address. */
Anastasia Klimchuk15315d62021-04-13 11:26:25 +100073 return spi_write_chunked(flash, buf, start, len, spi_data->max_kernel_buf_size - 5);
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110074}
75
76static int linux_spi_shutdown(void *data)
77{
Anastasia Klimchuk15315d62021-04-13 11:26:25 +100078 struct linux_spi_data *spi_data = (struct linux_spi_data *) data;
79 if (spi_data->fd != -1)
80 close(spi_data->fd);
81
82 free(spi_data);
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110083 return 0;
84}
85
Nikolai Artemiev552a3182020-06-15 15:41:49 +100086static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
87 unsigned int readcnt,
88 const unsigned char *txbuf,
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110089 unsigned char *rxbuf)
90{
Anastasia Klimchuk15315d62021-04-13 11:26:25 +100091 struct linux_spi_data *spi_data = flash->mst->spi.data;
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110092 int iocontrol_code;
93 struct spi_ioc_transfer msg[2] = {
94 {
95 .tx_buf = (uint64_t)(uintptr_t)txbuf,
96 .len = writecnt,
97 },
98 {
99 .rx_buf = (uint64_t)(uintptr_t)rxbuf,
100 .len = readcnt,
101 },
102 };
103
Anastasia Klimchuk15315d62021-04-13 11:26:25 +1000104 if (spi_data->fd == -1)
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100105 return -1;
106 /* The implementation currently does not support requests that
107 don't start with sending a command. */
108 if (writecnt == 0)
109 return SPI_INVALID_LENGTH;
110
111 /* Just submit the first (write) request in case there is nothing
112 to read. Otherwise submit both requests. */
113 if (readcnt == 0)
114 iocontrol_code = SPI_IOC_MESSAGE(1);
115 else
116 iocontrol_code = SPI_IOC_MESSAGE(2);
117
Anastasia Klimchuk15315d62021-04-13 11:26:25 +1000118 if (ioctl(spi_data->fd, iocontrol_code, msg) == -1) {
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100119 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
120 return -1;
121 }
122 return 0;
123}
uwe7df6dda2011-09-03 18:37:52 +0000124
Anastasia Klimchuk15315d62021-04-13 11:26:25 +1000125static struct spi_master spi_master_linux = {
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000126 .features = SPI_MASTER_4BA,
uwe7df6dda2011-09-03 18:37:52 +0000127 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
128 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
129 .command = linux_spi_send_command,
130 .multicommand = default_spi_send_multicommand,
131 .read = linux_spi_read,
132 .write_256 = linux_spi_write_256,
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100133 .write_aai = default_spi_write_aai,
uwe7df6dda2011-09-03 18:37:52 +0000134};
135
Aileen Cheng5556b942020-07-06 09:12:48 -0700136static char *check_sysfs(void)
137{
Edward O'Callaghan7e8f3132020-11-13 23:06:49 +1100138 unsigned i;
Aileen Cheng5556b942020-07-06 09:12:48 -0700139 const char *sysfs_path = NULL;
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000140 char *devfs_path = NULL;
Aileen Cheng5556b942020-07-06 09:12:48 -0700141 char *p;
Nikolai Artemievf8cafd22021-02-12 11:45:11 +1100142 const char *modalias[] = {
Aileen Cheng5556b942020-07-06 09:12:48 -0700143 "spi:spidev", /* raw access over SPI bus (newer kernels) */
144 "spidev", /* raw access over SPI bus (older kernels) */
145 "m25p80", /* generic MTD device */
146 };
147
148 for (i = 0; i < ARRAY_SIZE(modalias); i++) {
149 int major, minor;
150
151 /* Path should look like: /sys/blah/spiX.Y/modalias */
152 sysfs_path = scanft(LINUX_SPI_SYSFS_ROOT,
153 MODALIAS_FILE, modalias[i], 1);
154 if (!sysfs_path)
155 continue;
156
157 p = (char *)sysfs_path + strlen(LINUX_SPI_SYSFS_ROOT);
158 if (p[0] == '/')
159 p++;
160
161 if (sscanf(p, "spi%u.%u", &major, &minor) == 2) {
162 msg_pdbg("Found SPI device %s on spi%u.%u\n",
163 modalias[i], major, minor);
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000164 devfs_path = calloc(1, DEVFS_PATH_LEN);
165 snprintf(devfs_path, DEVFS_PATH_LEN, "/dev/spidev%u.%u", major, minor);
Aileen Cheng5556b942020-07-06 09:12:48 -0700166 free((void *)sysfs_path);
167 break;
168 }
169 free((void *)sysfs_path);
170 }
171
172 if (i == ARRAY_SIZE(modalias))
173 return NULL;
174 return devfs_path;
175}
176
David Hendricks2d069182015-10-30 22:52:57 -0700177static char *check_fdt(void)
178{
179 unsigned int bus, cs;
180
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000181 char *devfs_path = calloc(1, DEVFS_PATH_LEN);
David Hendricks2d069182015-10-30 22:52:57 -0700182 if (fdt_find_spi_nor_flash(&bus, &cs) < 0)
183 return NULL;
184
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000185 snprintf(devfs_path, DEVFS_PATH_LEN, "/dev/spidev%u.%u", bus, cs);
David Hendricks2d069182015-10-30 22:52:57 -0700186 return devfs_path;
187}
188
Aileen Cheng5556b942020-07-06 09:12:48 -0700189static char *linux_spi_probe(void)
190{
191 char *ret;
192
193 ret = check_fdt();
194 if (ret)
195 return ret;
196
197 ret = check_sysfs();
198 if (ret)
199 return ret;
200
201 return NULL;
202}
203
Anastasia Klimchuka7cd03d2021-04-12 16:52:58 +1000204/* Read max buffer size from sysfs, or use page size as fallback. */
205static size_t get_max_kernel_buf_size() {
206 size_t result = 0;
207 FILE *fp;
208 fp = fopen(BUF_SIZE_FROM_SYSFS, "r");
209 if (!fp) {
210 msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
211 goto out;
212 }
213
214 char buf[10];
215 if (!fgets(buf, sizeof(buf), fp)) {
216 if (feof(fp))
217 msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS);
218 else
219 msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
220 goto out;
221 }
222
223 long int tmp;
224 errno = 0;
225 tmp = strtol(buf, NULL, 0);
226 if ((tmp < 0) || errno) {
227 msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS);
228 } else {
229 msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS);
230 result = (size_t)tmp;
231 }
232
233out:
234 if (fp)
235 fclose(fp);
236
237 if (!result) {
238 msg_pdbg("%s: Using page size as max buffer size.\n", __func__);
239 result = (size_t)getpagesize();
240 }
241 return result;
242}
243
David Hendricksac1d25c2016-08-09 17:00:58 -0700244int linux_spi_init(void)
uwe7df6dda2011-09-03 18:37:52 +0000245{
David Hendricks98b3c572016-11-30 01:50:08 +0000246 char *p, *endp, *dev;
Nico Huber4b941a92018-12-22 00:08:50 +0100247 uint32_t speed_hz = 2 * 1000 * 1000;
Stefan Tauner180d8992012-03-03 18:09:33 +0000248 /* FIXME: make the following configurable by CLI options. */
249 /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */
250 const uint8_t mode = SPI_MODE_0;
251 const uint8_t bits = 8;
Anastasia Klimchuk15315d62021-04-13 11:26:25 +1000252 int fd = -1;
253 size_t max_kernel_buf_size;
254 struct linux_spi_data *spi_data;
Anastasia Klimchukb8911bb2021-04-13 10:15:26 +1000255 int ret = 0;
uwe7df6dda2011-09-03 18:37:52 +0000256
Nikolai Artemiev9d0d3522020-06-15 15:58:04 +1000257 p = extract_programmer_param("spispeed");
uwe7df6dda2011-09-03 18:37:52 +0000258 if (p && strlen(p)) {
Alexandru Gagniuc18fe18f2014-03-19 17:17:06 +0000259 speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000;
Nico Huber4b941a92018-12-22 00:08:50 +0100260 if (p == endp || speed_hz == 0) {
uwe7df6dda2011-09-03 18:37:52 +0000261 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000262 free(p);
David Hendricks98b3c572016-11-30 01:50:08 +0000263 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000264 }
Nico Huber4b941a92018-12-22 00:08:50 +0100265 } else {
266 msg_pinfo("Using default %"PRIu32
267 "kHz clock. Use 'spispeed' parameter to override.\n",
268 speed_hz / 1000);
uwe7df6dda2011-09-03 18:37:52 +0000269 }
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000270 free(p);
271
272 dev = extract_programmer_param("dev");
273 if (!dev)
274 dev = linux_spi_probe();
275 if (!dev || !strlen(dev)) {
276 msg_perr("No SPI device found. Use flashrom -p "
277 "linux_spi:dev=/dev/spidevX.Y\n");
278 free(dev);
279 return 1;
280 }
uwe7df6dda2011-09-03 18:37:52 +0000281
uwe6b716f02011-09-07 20:48:34 +0000282 msg_pdbg("Using device %s\n", dev);
uwe7df6dda2011-09-03 18:37:52 +0000283 if ((fd = open(dev, O_RDWR)) == -1) {
Nikolai Artemiev648f1272020-05-19 15:02:46 +1000284 msg_perr("%s: failed to open %s: %s\n", __func__,
uwe7df6dda2011-09-03 18:37:52 +0000285 dev, strerror(errno));
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000286 free(dev);
Nikolai Artemiev648f1272020-05-19 15:02:46 +1000287 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000288 }
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000289 free(dev);
uwe7df6dda2011-09-03 18:37:52 +0000290
Nico Huber4b941a92018-12-22 00:08:50 +0100291 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) {
292 msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n",
293 __func__, speed_hz, strerror(errno));
Anastasia Klimchukb8911bb2021-04-13 10:15:26 +1000294 ret = 1;
295 goto init_err;
uwe7df6dda2011-09-03 18:37:52 +0000296 }
Nico Huber4b941a92018-12-22 00:08:50 +0100297 msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000);
uwe7df6dda2011-09-03 18:37:52 +0000298
Stefan Tauner180d8992012-03-03 18:09:33 +0000299 if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
300 msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n",
301 __func__, mode, strerror(errno));
Anastasia Klimchukb8911bb2021-04-13 10:15:26 +1000302 ret = 1;
303 goto init_err;
Stefan Tauner180d8992012-03-03 18:09:33 +0000304 }
305
306 if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
307 msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n",
308 __func__, bits == 0 ? 8 : bits, strerror(errno));
Anastasia Klimchukb8911bb2021-04-13 10:15:26 +1000309 ret = 1;
310 goto init_err;
Stefan Tauner180d8992012-03-03 18:09:33 +0000311 }
uwe7df6dda2011-09-03 18:37:52 +0000312
Anastasia Klimchuka7cd03d2021-04-12 16:52:58 +1000313 max_kernel_buf_size = get_max_kernel_buf_size();
Keno Fischer556e4782015-11-15 14:58:25 +0000314 msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size);
Anastasia Klimchuka7cd03d2021-04-12 16:52:58 +1000315
Anastasia Klimchuk15315d62021-04-13 11:26:25 +1000316 spi_data = calloc(1, sizeof(*spi_data));
317 if (!spi_data) {
318 msg_perr("Unable to allocated space for SPI master data\n");
319 ret = SPI_GENERIC_ERROR;
320 goto init_err;
321 }
322 spi_data->fd = fd;
323 spi_data->max_kernel_buf_size = max_kernel_buf_size;
324 spi_master_linux.data = spi_data;
325
326 if (register_shutdown(linux_spi_shutdown, spi_data)) {
327 free(spi_data);
Anastasia Klimchukb8911bb2021-04-13 10:15:26 +1000328 ret = 1;
329 goto init_err;
330 }
Keno Fischer556e4782015-11-15 14:58:25 +0000331 register_spi_master(&spi_master_linux);
David Hendricks98b3c572016-11-30 01:50:08 +0000332 return 0;
Anastasia Klimchukb8911bb2021-04-13 10:15:26 +1000333
334init_err:
335 if (fd != -1) {
336 close(fd);
337 fd = -1;
338 }
339 return ret;
uwe7df6dda2011-09-03 18:37:52 +0000340}
341
Stefan Tauneree310a42012-03-13 00:18:19 +0000342#endif // CONFIG_LINUX_SPI == 1