blob: 4a0aba522f27603d257b6315aa53530a61979f53 [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
uwe7df6dda2011-09-03 18:37:52 +000051static int fd = -1;
Keno Fischer556e4782015-11-15 14:58:25 +000052#define BUF_SIZE_FROM_SYSFS "/sys/module/spidev/parameters/bufsiz"
53static size_t max_kernel_buf_size;
uwe7df6dda2011-09-03 18:37:52 +000054
David Hendricks93784b42016-08-09 17:00:38 -070055static int linux_spi_shutdown(void *data);
Nikolai Artemiev552a3182020-06-15 15:41:49 +100056static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
57 unsigned int readcnt,
58 const unsigned char *txbuf,
59 unsigned char *rxbuf);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070060static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000061 unsigned int start, unsigned int len);
Patrick Georgiab8353e2017-02-03 18:32:01 +010062static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000063 unsigned int start, unsigned int len);
uwe7df6dda2011-09-03 18:37:52 +000064
Patrick Georgif4f1e2f2017-03-10 17:38:40 +010065static const struct spi_master spi_master_linux = {
Edward O'Callaghana6673bd2019-06-24 15:22:28 +100066 .features = SPI_MASTER_4BA,
uwe7df6dda2011-09-03 18:37:52 +000067 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
68 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
69 .command = linux_spi_send_command,
70 .multicommand = default_spi_send_multicommand,
71 .read = linux_spi_read,
72 .write_256 = linux_spi_write_256,
73};
74
Aileen Cheng5556b942020-07-06 09:12:48 -070075static char devfs_path[32]; /* at least big enough to fit /dev/spidevX.Y */
76static char *check_sysfs(void)
77{
78 int i;
79 const char *sysfs_path = NULL;
80 char *p;
81 char *modalias[] = {
82 "spi:spidev", /* raw access over SPI bus (newer kernels) */
83 "spidev", /* raw access over SPI bus (older kernels) */
84 "m25p80", /* generic MTD device */
85 };
86
87 for (i = 0; i < ARRAY_SIZE(modalias); i++) {
88 int major, minor;
89
90 /* Path should look like: /sys/blah/spiX.Y/modalias */
91 sysfs_path = scanft(LINUX_SPI_SYSFS_ROOT,
92 MODALIAS_FILE, modalias[i], 1);
93 if (!sysfs_path)
94 continue;
95
96 p = (char *)sysfs_path + strlen(LINUX_SPI_SYSFS_ROOT);
97 if (p[0] == '/')
98 p++;
99
100 if (sscanf(p, "spi%u.%u", &major, &minor) == 2) {
101 msg_pdbg("Found SPI device %s on spi%u.%u\n",
102 modalias[i], major, minor);
103 sprintf(devfs_path, "/dev/spidev%u.%u", major, minor);
104 free((void *)sysfs_path);
105 break;
106 }
107 free((void *)sysfs_path);
108 }
109
110 if (i == ARRAY_SIZE(modalias))
111 return NULL;
112 return devfs_path;
113}
114
David Hendricks2d069182015-10-30 22:52:57 -0700115static char *check_fdt(void)
116{
117 unsigned int bus, cs;
118
119 if (fdt_find_spi_nor_flash(&bus, &cs) < 0)
120 return NULL;
121
122 sprintf(devfs_path, "/dev/spidev%u.%u", bus, cs);
123 return devfs_path;
124}
125
Aileen Cheng5556b942020-07-06 09:12:48 -0700126static char *linux_spi_probe(void)
127{
128 char *ret;
129
130 ret = check_fdt();
131 if (ret)
132 return ret;
133
134 ret = check_sysfs();
135 if (ret)
136 return ret;
137
138 return NULL;
139}
140
David Hendricksac1d25c2016-08-09 17:00:58 -0700141int linux_spi_init(void)
uwe7df6dda2011-09-03 18:37:52 +0000142{
David Hendricks98b3c572016-11-30 01:50:08 +0000143 char *p, *endp, *dev;
Nico Huber4b941a92018-12-22 00:08:50 +0100144 uint32_t speed_hz = 2 * 1000 * 1000;
Stefan Tauner180d8992012-03-03 18:09:33 +0000145 /* FIXME: make the following configurable by CLI options. */
146 /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */
147 const uint8_t mode = SPI_MODE_0;
148 const uint8_t bits = 8;
uwe7df6dda2011-09-03 18:37:52 +0000149
David Hendricksba0827a2013-05-03 20:25:40 -0700150 /*
151 * FIXME: There might be other programmers with flash memory (such as
152 * an EC) connected via SPI. For now we rely on the device's driver to
153 * distinguish it and assume generic SPI implies host.
154 */
155 if (alias && alias->type != ALIAS_HOST)
156 return 1;
157
Aileen Cheng5556b942020-07-06 09:12:48 -0700158 dev = extract_programmer_param("dev");
159 if (!dev)
160 dev = linux_spi_probe();
161 if (!dev || !strlen(dev)) {
162 msg_perr("No SPI device found. Use flashrom -p "
163 "linux_spi:dev=/dev/spidevX.Y\n");
164 return 1;
165 }
166
Nikolai Artemiev9d0d3522020-06-15 15:58:04 +1000167 p = extract_programmer_param("spispeed");
uwe7df6dda2011-09-03 18:37:52 +0000168 if (p && strlen(p)) {
Alexandru Gagniuc18fe18f2014-03-19 17:17:06 +0000169 speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000;
Nico Huber4b941a92018-12-22 00:08:50 +0100170 if (p == endp || speed_hz == 0) {
uwe7df6dda2011-09-03 18:37:52 +0000171 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
David Hendricks98b3c572016-11-30 01:50:08 +0000172 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000173 }
Nico Huber4b941a92018-12-22 00:08:50 +0100174 } else {
175 msg_pinfo("Using default %"PRIu32
176 "kHz clock. Use 'spispeed' parameter to override.\n",
177 speed_hz / 1000);
uwe7df6dda2011-09-03 18:37:52 +0000178 }
179
uwe6b716f02011-09-07 20:48:34 +0000180 msg_pdbg("Using device %s\n", dev);
uwe7df6dda2011-09-03 18:37:52 +0000181 if ((fd = open(dev, O_RDWR)) == -1) {
Nikolai Artemiev648f1272020-05-19 15:02:46 +1000182 msg_perr("%s: failed to open %s: %s\n", __func__,
uwe7df6dda2011-09-03 18:37:52 +0000183 dev, strerror(errno));
Aileen Cheng5556b942020-07-06 09:12:48 -0700184
Nikolai Artemiev648f1272020-05-19 15:02:46 +1000185 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000186 }
187
Stefan Tauner180d8992012-03-03 18:09:33 +0000188 if (register_shutdown(linux_spi_shutdown, NULL))
189 return 1;
190 /* We rely on the shutdown function for cleanup from here on. */
191
Nico Huber4b941a92018-12-22 00:08:50 +0100192 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) {
193 msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n",
194 __func__, speed_hz, strerror(errno));
195 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000196 }
Nico Huber4b941a92018-12-22 00:08:50 +0100197 msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000);
uwe7df6dda2011-09-03 18:37:52 +0000198
Stefan Tauner180d8992012-03-03 18:09:33 +0000199 if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
200 msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n",
201 __func__, mode, strerror(errno));
David Hendricks98b3c572016-11-30 01:50:08 +0000202 return 1;
Stefan Tauner180d8992012-03-03 18:09:33 +0000203 }
204
205 if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
206 msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n",
207 __func__, bits == 0 ? 8 : bits, strerror(errno));
208 return 1;
209 }
uwe7df6dda2011-09-03 18:37:52 +0000210
Keno Fischer556e4782015-11-15 14:58:25 +0000211 /* Read max buffer size from sysfs, or use page size as fallback. */
212 FILE *fp;
213 fp = fopen(BUF_SIZE_FROM_SYSFS, "r");
214 if (!fp) {
215 msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
216 goto out;
217 }
uwe7df6dda2011-09-03 18:37:52 +0000218
Keno Fischer556e4782015-11-15 14:58:25 +0000219 char buf[10];
Jacob Garber02df5592019-08-12 14:14:40 -0600220 if (!fgets(buf, sizeof(buf), fp)) {
Keno Fischer556e4782015-11-15 14:58:25 +0000221 if (feof(fp))
222 msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS);
223 else
224 msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
225 goto out;
226 }
227
228 long int tmp;
229 errno = 0;
230 tmp = strtol(buf, NULL, 0);
231 if ((tmp < 0) || errno) {
232 msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS);
233 } else {
234 msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS);
235 max_kernel_buf_size = (size_t)tmp;
236 }
237
238out:
239 if (fp)
240 fclose(fp);
241
242 if (!max_kernel_buf_size) {
243 msg_pdbg("%s: Using page size as max buffer size.\n", __func__);
244 max_kernel_buf_size = (size_t)getpagesize();
245 }
246
247 msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size);
248 register_spi_master(&spi_master_linux);
David Hendricks98b3c572016-11-30 01:50:08 +0000249 return 0;
uwe7df6dda2011-09-03 18:37:52 +0000250}
251
David Hendricks93784b42016-08-09 17:00:38 -0700252static int linux_spi_shutdown(void *data)
uwe7df6dda2011-09-03 18:37:52 +0000253{
254 if (fd != -1) {
255 close(fd);
256 fd = -1;
257 }
258 return 0;
259}
260
Nikolai Artemiev552a3182020-06-15 15:41:49 +1000261static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
262 unsigned int readcnt,
263 const unsigned char *txbuf,
264 unsigned char *rxbuf)
uwe7df6dda2011-09-03 18:37:52 +0000265{
Michael Karcher58dc3682012-03-06 22:17:06 +0000266 int iocontrol_code;
uwe7df6dda2011-09-03 18:37:52 +0000267 struct spi_ioc_transfer msg[2] = {
268 {
David Riley13343b82014-07-25 11:37:56 -0700269 .tx_buf = (uint64_t)(uintptr_t)txbuf,
uwe7df6dda2011-09-03 18:37:52 +0000270 .len = writecnt,
271 },
272 {
David Riley13343b82014-07-25 11:37:56 -0700273 .rx_buf = (uint64_t)(uintptr_t)rxbuf,
uwe7df6dda2011-09-03 18:37:52 +0000274 .len = readcnt,
275 },
276 };
277
278 if (fd == -1)
279 return -1;
Michael Karcher58dc3682012-03-06 22:17:06 +0000280 /* The implementation currently does not support requests that
281 don't start with sending a command. */
282 if (writecnt == 0)
283 return SPI_INVALID_LENGTH;
uwe7df6dda2011-09-03 18:37:52 +0000284
Michael Karcher58dc3682012-03-06 22:17:06 +0000285 /* Just submit the first (write) request in case there is nothing
286 to read. Otherwise submit both requests. */
287 if (readcnt == 0)
288 iocontrol_code = SPI_IOC_MESSAGE(1);
289 else
290 iocontrol_code = SPI_IOC_MESSAGE(2);
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800291
Michael Karcher58dc3682012-03-06 22:17:06 +0000292 if (ioctl(fd, iocontrol_code, msg) == -1) {
uwe7df6dda2011-09-03 18:37:52 +0000293 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
294 return -1;
295 }
296 return 0;
297}
298
Keno Fischer556e4782015-11-15 14:58:25 +0000299static int linux_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000300{
Nico Huberb5ebaaf2018-03-08 16:14:15 +0100301 /* Older kernels use a single buffer for combined input and output
302 data. So account for longest possible command + address, too. */
303 return spi_read_chunked(flash, buf, start, len, max_kernel_buf_size - 5);
uwe7df6dda2011-09-03 18:37:52 +0000304}
305
Nikolai Artemiev552a3182020-06-15 15:41:49 +1000306static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000307{
Keno Fischer556e4782015-11-15 14:58:25 +0000308 /* 5 bytes must be reserved for longest possible command + address. */
309 return spi_write_chunked(flash, buf, start, len, max_kernel_buf_size - 5);
uwe7df6dda2011-09-03 18:37:52 +0000310}
Stefan Tauneree310a42012-03-13 00:18:19 +0000311
312#endif // CONFIG_LINUX_SPI == 1