blob: ea43f9d027fc77f0c9b75cd34fd3be118e532323 [file] [log] [blame]
Sven Schnelle5ce5f702011-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.
Sven Schnelle5ce5f702011-09-03 18:37:52 +000014 */
15
Stefan Tauner8868db32012-03-13 00:18:19 +000016#if CONFIG_LINUX_SPI == 1
17
Sven Schnelle5ce5f702011-09-03 18:37:52 +000018#include <stdio.h>
19#include <string.h>
20#include <stdlib.h>
Nico Huber9cecc7e2018-12-22 00:08:50 +010021#include <stdint.h>
22#include <inttypes.h>
Gwenhael Goavec-Merou8cd0c732015-11-14 02:55:12 +000023#include <fcntl.h>
Sven Schnelle5ce5f702011-09-03 18:37:52 +000024#include <errno.h>
25#include <ctype.h>
26#include <unistd.h>
Gwenhael Goavec-Merou8cd0c732015-11-14 02:55:12 +000027#include <sys/ioctl.h>
Stefan Tauner8868db32012-03-13 00:18:19 +000028#include <linux/types.h>
Sven Schnelle5ce5f702011-09-03 18:37:52 +000029#include "flash.h"
30#include "chipdrivers.h"
31#include "programmer.h"
32#include "spi.h"
Fabrice Fontainee0ceedf2019-07-17 19:04:12 +020033/*
34 * Linux versions prior to v4.14-rc7 may need linux/ioctl.h included here due
35 * to missing from linux/spi/spidev.h. This was fixed in the following commit:
36 * a2b4a79b88b2 spi: uapi: spidev: add missing ioctl header
37 */
38#include <linux/ioctl.h>
39#include <linux/spi/spidev.h>
Sven Schnelle5ce5f702011-09-03 18:37:52 +000040
Stefan Tauner23e10b82016-01-23 16:16:49 +000041/* Devices known to work with this module (FIXME: export as struct dev_entry):
42 * Beagle Bone Black
43 * Raspberry Pi
44 * HummingBoard
45 */
46
Keno Fischer1f33cb52015-11-15 14:58:25 +000047#define BUF_SIZE_FROM_SYSFS "/sys/module/spidev/parameters/bufsiz"
Anastasia Klimchuk84f03922021-04-13 11:26:25 +100048
49struct linux_spi_data {
50 int fd;
51 size_t max_kernel_buf_size;
52};
Sven Schnelle5ce5f702011-09-03 18:37:52 +000053
54static int linux_spi_shutdown(void *data);
Edward O'Callaghan5eca4272020-04-12 17:27:53 +100055static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000056 unsigned int readcnt,
57 const unsigned char *txbuf,
58 unsigned char *rxbuf);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000059static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000060 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +000061static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000062 unsigned int start, unsigned int len);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000063
Nico Huber03f3a6d2021-05-11 17:53:34 +020064static const struct spi_master spi_master_linux = {
Nico Huber1cf407b2017-11-10 20:18:23 +010065 .features = SPI_MASTER_4BA,
Sven Schnelle5ce5f702011-09-03 18:37:52 +000066 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
67 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
68 .command = linux_spi_send_command,
69 .multicommand = default_spi_send_multicommand,
70 .read = linux_spi_read,
71 .write_256 = linux_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +000072 .write_aai = default_spi_write_aai,
Sven Schnelle5ce5f702011-09-03 18:37:52 +000073};
74
Anastasia Klimchukc19ef9b2021-04-12 16:52:58 +100075/* Read max buffer size from sysfs, or use page size as fallback. */
Anastasia Klimchuk2a329662021-04-19 11:12:01 +100076static size_t get_max_kernel_buf_size()
77{
Anastasia Klimchukc19ef9b2021-04-12 16:52:58 +100078 size_t result = 0;
79 FILE *fp;
80 fp = fopen(BUF_SIZE_FROM_SYSFS, "r");
81 if (!fp) {
82 msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
83 goto out;
84 }
85
86 char buf[10];
87 if (!fgets(buf, sizeof(buf), fp)) {
88 if (feof(fp))
89 msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS);
90 else
91 msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
92 goto out;
93 }
94
95 long int tmp;
96 errno = 0;
97 tmp = strtol(buf, NULL, 0);
98 if ((tmp < 0) || errno) {
99 msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS);
100 } else {
101 msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS);
102 result = (size_t)tmp;
103 }
104
105out:
106 if (fp)
107 fclose(fp);
108
109 if (!result) {
110 msg_pdbg("%s: Using page size as max buffer size.\n", __func__);
111 result = (size_t)getpagesize();
112 }
113 return result;
114}
115
Thomas Heijligencc853d82021-05-04 15:32:17 +0200116static int linux_spi_init(void)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000117{
118 char *p, *endp, *dev;
Nico Huber9cecc7e2018-12-22 00:08:50 +0100119 uint32_t speed_hz = 2 * 1000 * 1000;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000120 /* FIXME: make the following configurable by CLI options. */
121 /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */
122 const uint8_t mode = SPI_MODE_0;
123 const uint8_t bits = 8;
Anastasia Klimchuk2a329662021-04-19 11:12:01 +1000124 int fd;
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000125 size_t max_kernel_buf_size;
126 struct linux_spi_data *spi_data;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000127
Stefan Tauner0554ca52013-07-25 22:54:25 +0000128 p = extract_programmer_param("spispeed");
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000129 if (p && strlen(p)) {
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +0000130 speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000;
Nico Huber9cecc7e2018-12-22 00:08:50 +0100131 if (p == endp || speed_hz == 0) {
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000132 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
Stefan Tauner0554ca52013-07-25 22:54:25 +0000133 free(p);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000134 return 1;
135 }
Nico Huber9cecc7e2018-12-22 00:08:50 +0100136 } else {
137 msg_pinfo("Using default %"PRIu32
138 "kHz clock. Use 'spispeed' parameter to override.\n",
139 speed_hz / 1000);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000140 }
Stefan Tauner0554ca52013-07-25 22:54:25 +0000141 free(p);
142
143 dev = extract_programmer_param("dev");
144 if (!dev || !strlen(dev)) {
145 msg_perr("No SPI device given. Use flashrom -p "
146 "linux_spi:dev=/dev/spidevX.Y\n");
147 free(dev);
148 return 1;
149 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000150
Sven Schnellecb24ddb2011-09-07 20:48:34 +0000151 msg_pdbg("Using device %s\n", dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000152 if ((fd = open(dev, O_RDWR)) == -1) {
153 msg_perr("%s: failed to open %s: %s\n", __func__,
154 dev, strerror(errno));
Stefan Tauner0554ca52013-07-25 22:54:25 +0000155 free(dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000156 return 1;
157 }
Stefan Tauner0554ca52013-07-25 22:54:25 +0000158 free(dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000159
Nico Huber9cecc7e2018-12-22 00:08:50 +0100160 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) {
161 msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n",
162 __func__, speed_hz, strerror(errno));
Anastasia Klimchukf876c0f2021-04-13 10:15:26 +1000163 goto init_err;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000164 }
Nico Huber9cecc7e2018-12-22 00:08:50 +0100165 msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000166
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000167 if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
168 msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n",
169 __func__, mode, strerror(errno));
Anastasia Klimchukf876c0f2021-04-13 10:15:26 +1000170 goto init_err;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000171 }
172
173 if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
174 msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n",
175 __func__, bits == 0 ? 8 : bits, strerror(errno));
Anastasia Klimchukf876c0f2021-04-13 10:15:26 +1000176 goto init_err;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000177 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000178
Anastasia Klimchukc19ef9b2021-04-12 16:52:58 +1000179 max_kernel_buf_size = get_max_kernel_buf_size();
Keno Fischer1f33cb52015-11-15 14:58:25 +0000180 msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size);
Anastasia Klimchukc19ef9b2021-04-12 16:52:58 +1000181
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000182 spi_data = calloc(1, sizeof(*spi_data));
183 if (!spi_data) {
184 msg_perr("Unable to allocated space for SPI master data\n");
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000185 goto init_err;
186 }
187 spi_data->fd = fd;
188 spi_data->max_kernel_buf_size = max_kernel_buf_size;
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000189
190 if (register_shutdown(linux_spi_shutdown, spi_data)) {
191 free(spi_data);
Anastasia Klimchukf876c0f2021-04-13 10:15:26 +1000192 goto init_err;
193 }
Nico Huber03f3a6d2021-05-11 17:53:34 +0200194 register_spi_master(&spi_master_linux, spi_data);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000195 return 0;
Anastasia Klimchukf876c0f2021-04-13 10:15:26 +1000196
197init_err:
Anastasia Klimchuk2a329662021-04-19 11:12:01 +1000198 close(fd);
199 return 1;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000200}
201
202static int linux_spi_shutdown(void *data)
203{
Anastasia Klimchuk2a329662021-04-19 11:12:01 +1000204 struct linux_spi_data *spi_data = data;
205 close(spi_data->fd);
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000206 free(spi_data);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000207 return 0;
208}
209
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000210static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000211 unsigned int readcnt,
212 const unsigned char *txbuf,
213 unsigned char *rxbuf)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000214{
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000215 struct linux_spi_data *spi_data = flash->mst->spi.data;
Michael Karcher8371d722012-03-06 22:17:06 +0000216 int iocontrol_code;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000217 struct spi_ioc_transfer msg[2] = {
218 {
David Riley18f50972014-08-05 22:16:01 +0000219 .tx_buf = (uint64_t)(uintptr_t)txbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000220 .len = writecnt,
221 },
222 {
David Riley18f50972014-08-05 22:16:01 +0000223 .rx_buf = (uint64_t)(uintptr_t)rxbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000224 .len = readcnt,
225 },
226 };
227
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000228 if (spi_data->fd == -1)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000229 return -1;
Michael Karcher8371d722012-03-06 22:17:06 +0000230 /* The implementation currently does not support requests that
231 don't start with sending a command. */
232 if (writecnt == 0)
233 return SPI_INVALID_LENGTH;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000234
Michael Karcher8371d722012-03-06 22:17:06 +0000235 /* Just submit the first (write) request in case there is nothing
236 to read. Otherwise submit both requests. */
237 if (readcnt == 0)
238 iocontrol_code = SPI_IOC_MESSAGE(1);
239 else
240 iocontrol_code = SPI_IOC_MESSAGE(2);
241
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000242 if (ioctl(spi_data->fd, iocontrol_code, msg) == -1) {
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000243 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
244 return -1;
245 }
246 return 0;
247}
248
Keno Fischer1f33cb52015-11-15 14:58:25 +0000249static int linux_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000250{
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000251 struct linux_spi_data *spi_data = flash->mst->spi.data;
Nico Huber22418422018-03-08 16:14:15 +0100252 /* Older kernels use a single buffer for combined input and output
253 data. So account for longest possible command + address, too. */
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000254 return spi_read_chunked(flash, buf, start, len, spi_data->max_kernel_buf_size - 5);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000255}
256
Mark Marshallf20b7be2014-05-09 21:16:21 +0000257static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000258{
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000259 struct linux_spi_data *spi_data = flash->mst->spi.data;
Keno Fischer1f33cb52015-11-15 14:58:25 +0000260 /* 5 bytes must be reserved for longest possible command + address. */
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000261 return spi_write_chunked(flash, buf, start, len, spi_data->max_kernel_buf_size - 5);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000262}
Stefan Tauner8868db32012-03-13 00:18:19 +0000263
Thomas Heijligencc853d82021-05-04 15:32:17 +0200264const struct programmer_entry programmer_linux_spi = {
265 .name = "linux_spi",
266 .type = OTHER,
267 .devs.note = "Device files /dev/spidev*.*\n",
268 .init = linux_spi_init,
269 .map_flash_region = fallback_map,
270 .unmap_flash_region = fallback_unmap,
271 .delay = internal_delay,
272};
273
Stefan Tauner8868db32012-03-13 00:18:19 +0000274#endif // CONFIG_LINUX_SPI == 1