blob: 59092ad281096a80c600332a202a59dd089f6e19 [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 Klimchuk339cb202021-04-19 11:12:01 +100078 struct linux_spi_data *spi_data = data;
79 close(spi_data->fd);
Anastasia Klimchuk15315d62021-04-13 11:26:25 +100080 free(spi_data);
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110081 return 0;
82}
83
Nikolai Artemiev552a3182020-06-15 15:41:49 +100084static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
85 unsigned int readcnt,
86 const unsigned char *txbuf,
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110087 unsigned char *rxbuf)
88{
Anastasia Klimchuk15315d62021-04-13 11:26:25 +100089 struct linux_spi_data *spi_data = flash->mst->spi.data;
Anastasia Klimchukad79bd92021-02-15 15:04:20 +110090 int iocontrol_code;
91 struct spi_ioc_transfer msg[2] = {
92 {
93 .tx_buf = (uint64_t)(uintptr_t)txbuf,
94 .len = writecnt,
95 },
96 {
97 .rx_buf = (uint64_t)(uintptr_t)rxbuf,
98 .len = readcnt,
99 },
100 };
101
Anastasia Klimchuk15315d62021-04-13 11:26:25 +1000102 if (spi_data->fd == -1)
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100103 return -1;
104 /* The implementation currently does not support requests that
105 don't start with sending a command. */
106 if (writecnt == 0)
107 return SPI_INVALID_LENGTH;
108
109 /* Just submit the first (write) request in case there is nothing
110 to read. Otherwise submit both requests. */
111 if (readcnt == 0)
112 iocontrol_code = SPI_IOC_MESSAGE(1);
113 else
114 iocontrol_code = SPI_IOC_MESSAGE(2);
115
Anastasia Klimchuk15315d62021-04-13 11:26:25 +1000116 if (ioctl(spi_data->fd, iocontrol_code, msg) == -1) {
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100117 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
118 return -1;
119 }
120 return 0;
121}
uwe7df6dda2011-09-03 18:37:52 +0000122
Nico Huber2ef004f2021-05-11 17:53:34 +0200123static const struct spi_master spi_master_linux = {
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000124 .features = SPI_MASTER_4BA,
uwe7df6dda2011-09-03 18:37:52 +0000125 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
126 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
127 .command = linux_spi_send_command,
128 .multicommand = default_spi_send_multicommand,
129 .read = linux_spi_read,
130 .write_256 = linux_spi_write_256,
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100131 .write_aai = default_spi_write_aai,
uwe7df6dda2011-09-03 18:37:52 +0000132};
133
Aileen Cheng5556b942020-07-06 09:12:48 -0700134static char *check_sysfs(void)
135{
Edward O'Callaghan7e8f3132020-11-13 23:06:49 +1100136 unsigned i;
Aileen Cheng5556b942020-07-06 09:12:48 -0700137 const char *sysfs_path = NULL;
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000138 char *devfs_path = NULL;
Aileen Cheng5556b942020-07-06 09:12:48 -0700139 char *p;
Nikolai Artemievf8cafd22021-02-12 11:45:11 +1100140 const char *modalias[] = {
Aileen Cheng5556b942020-07-06 09:12:48 -0700141 "spi:spidev", /* raw access over SPI bus (newer kernels) */
142 "spidev", /* raw access over SPI bus (older kernels) */
143 "m25p80", /* generic MTD device */
144 };
145
146 for (i = 0; i < ARRAY_SIZE(modalias); i++) {
147 int major, minor;
148
149 /* Path should look like: /sys/blah/spiX.Y/modalias */
150 sysfs_path = scanft(LINUX_SPI_SYSFS_ROOT,
151 MODALIAS_FILE, modalias[i], 1);
152 if (!sysfs_path)
153 continue;
154
155 p = (char *)sysfs_path + strlen(LINUX_SPI_SYSFS_ROOT);
156 if (p[0] == '/')
157 p++;
158
159 if (sscanf(p, "spi%u.%u", &major, &minor) == 2) {
160 msg_pdbg("Found SPI device %s on spi%u.%u\n",
161 modalias[i], major, minor);
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000162 devfs_path = calloc(1, DEVFS_PATH_LEN);
163 snprintf(devfs_path, DEVFS_PATH_LEN, "/dev/spidev%u.%u", major, minor);
Aileen Cheng5556b942020-07-06 09:12:48 -0700164 free((void *)sysfs_path);
165 break;
166 }
167 free((void *)sysfs_path);
168 }
169
170 if (i == ARRAY_SIZE(modalias))
171 return NULL;
172 return devfs_path;
173}
174
Aileen Cheng5556b942020-07-06 09:12:48 -0700175static char *linux_spi_probe(void)
176{
177 char *ret;
178
Aileen Cheng5556b942020-07-06 09:12:48 -0700179 ret = check_sysfs();
180 if (ret)
181 return ret;
182
183 return NULL;
184}
185
Anastasia Klimchuka7cd03d2021-04-12 16:52:58 +1000186/* Read max buffer size from sysfs, or use page size as fallback. */
Anastasia Klimchuk339cb202021-04-19 11:12:01 +1000187static size_t get_max_kernel_buf_size()
188{
Anastasia Klimchuka7cd03d2021-04-12 16:52:58 +1000189 size_t result = 0;
190 FILE *fp;
191 fp = fopen(BUF_SIZE_FROM_SYSFS, "r");
192 if (!fp) {
193 msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
194 goto out;
195 }
196
197 char buf[10];
198 if (!fgets(buf, sizeof(buf), fp)) {
199 if (feof(fp))
200 msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS);
201 else
202 msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
203 goto out;
204 }
205
206 long int tmp;
207 errno = 0;
208 tmp = strtol(buf, NULL, 0);
209 if ((tmp < 0) || errno) {
210 msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS);
211 } else {
212 msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS);
213 result = (size_t)tmp;
214 }
215
216out:
217 if (fp)
218 fclose(fp);
219
220 if (!result) {
221 msg_pdbg("%s: Using page size as max buffer size.\n", __func__);
222 result = (size_t)getpagesize();
223 }
224 return result;
225}
226
David Hendricksac1d25c2016-08-09 17:00:58 -0700227int linux_spi_init(void)
uwe7df6dda2011-09-03 18:37:52 +0000228{
David Hendricks98b3c572016-11-30 01:50:08 +0000229 char *p, *endp, *dev;
Nico Huber4b941a92018-12-22 00:08:50 +0100230 uint32_t speed_hz = 2 * 1000 * 1000;
Stefan Tauner180d8992012-03-03 18:09:33 +0000231 /* FIXME: make the following configurable by CLI options. */
232 /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */
233 const uint8_t mode = SPI_MODE_0;
234 const uint8_t bits = 8;
Anastasia Klimchuk339cb202021-04-19 11:12:01 +1000235 int fd;
Anastasia Klimchuk15315d62021-04-13 11:26:25 +1000236 size_t max_kernel_buf_size;
237 struct linux_spi_data *spi_data;
uwe7df6dda2011-09-03 18:37:52 +0000238
Nikolai Artemiev9d0d3522020-06-15 15:58:04 +1000239 p = extract_programmer_param("spispeed");
uwe7df6dda2011-09-03 18:37:52 +0000240 if (p && strlen(p)) {
Alexandru Gagniuc18fe18f2014-03-19 17:17:06 +0000241 speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000;
Nico Huber4b941a92018-12-22 00:08:50 +0100242 if (p == endp || speed_hz == 0) {
uwe7df6dda2011-09-03 18:37:52 +0000243 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000244 free(p);
David Hendricks98b3c572016-11-30 01:50:08 +0000245 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000246 }
Nico Huber4b941a92018-12-22 00:08:50 +0100247 } else {
248 msg_pinfo("Using default %"PRIu32
249 "kHz clock. Use 'spispeed' parameter to override.\n",
250 speed_hz / 1000);
uwe7df6dda2011-09-03 18:37:52 +0000251 }
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000252 free(p);
253
254 dev = extract_programmer_param("dev");
255 if (!dev)
256 dev = linux_spi_probe();
257 if (!dev || !strlen(dev)) {
258 msg_perr("No SPI device found. Use flashrom -p "
259 "linux_spi:dev=/dev/spidevX.Y\n");
260 free(dev);
261 return 1;
262 }
uwe7df6dda2011-09-03 18:37:52 +0000263
uwe6b716f02011-09-07 20:48:34 +0000264 msg_pdbg("Using device %s\n", dev);
uwe7df6dda2011-09-03 18:37:52 +0000265 if ((fd = open(dev, O_RDWR)) == -1) {
Nikolai Artemiev648f1272020-05-19 15:02:46 +1000266 msg_perr("%s: failed to open %s: %s\n", __func__,
uwe7df6dda2011-09-03 18:37:52 +0000267 dev, strerror(errno));
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000268 free(dev);
Nikolai Artemiev648f1272020-05-19 15:02:46 +1000269 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000270 }
Nikolai Artemiev8ac099f2020-08-31 11:58:11 +1000271 free(dev);
uwe7df6dda2011-09-03 18:37:52 +0000272
Nico Huber4b941a92018-12-22 00:08:50 +0100273 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) {
274 msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n",
275 __func__, speed_hz, strerror(errno));
Anastasia Klimchukb8911bb2021-04-13 10:15:26 +1000276 goto init_err;
uwe7df6dda2011-09-03 18:37:52 +0000277 }
Nico Huber4b941a92018-12-22 00:08:50 +0100278 msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000);
uwe7df6dda2011-09-03 18:37:52 +0000279
Stefan Tauner180d8992012-03-03 18:09:33 +0000280 if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
281 msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n",
282 __func__, mode, strerror(errno));
Anastasia Klimchukb8911bb2021-04-13 10:15:26 +1000283 goto init_err;
Stefan Tauner180d8992012-03-03 18:09:33 +0000284 }
285
286 if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
287 msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n",
288 __func__, bits == 0 ? 8 : bits, strerror(errno));
Anastasia Klimchukb8911bb2021-04-13 10:15:26 +1000289 goto init_err;
Stefan Tauner180d8992012-03-03 18:09:33 +0000290 }
uwe7df6dda2011-09-03 18:37:52 +0000291
Anastasia Klimchuka7cd03d2021-04-12 16:52:58 +1000292 max_kernel_buf_size = get_max_kernel_buf_size();
Keno Fischer556e4782015-11-15 14:58:25 +0000293 msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size);
Anastasia Klimchuka7cd03d2021-04-12 16:52:58 +1000294
Anastasia Klimchuk15315d62021-04-13 11:26:25 +1000295 spi_data = calloc(1, sizeof(*spi_data));
296 if (!spi_data) {
297 msg_perr("Unable to allocated space for SPI master data\n");
Anastasia Klimchuk15315d62021-04-13 11:26:25 +1000298 goto init_err;
299 }
300 spi_data->fd = fd;
301 spi_data->max_kernel_buf_size = max_kernel_buf_size;
Anastasia Klimchuk15315d62021-04-13 11:26:25 +1000302
303 if (register_shutdown(linux_spi_shutdown, spi_data)) {
304 free(spi_data);
Anastasia Klimchukb8911bb2021-04-13 10:15:26 +1000305 goto init_err;
306 }
Nico Huber2ef004f2021-05-11 17:53:34 +0200307 register_spi_master(&spi_master_linux, spi_data);
David Hendricks98b3c572016-11-30 01:50:08 +0000308 return 0;
Anastasia Klimchukb8911bb2021-04-13 10:15:26 +1000309
310init_err:
Anastasia Klimchuk339cb202021-04-19 11:12:01 +1000311 close(fd);
312 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000313}
314
Stefan Tauneree310a42012-03-13 00:18:19 +0000315#endif // CONFIG_LINUX_SPI == 1