blob: 6eb4bca05fab4e0c1883a9f9b261d57335cefff1 [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
16#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
Nico Huber9cecc7e2018-12-22 00:08:50 +010019#include <stdint.h>
20#include <inttypes.h>
Gwenhael Goavec-Merou8cd0c732015-11-14 02:55:12 +000021#include <fcntl.h>
Sven Schnelle5ce5f702011-09-03 18:37:52 +000022#include <errno.h>
23#include <ctype.h>
24#include <unistd.h>
Gwenhael Goavec-Merou8cd0c732015-11-14 02:55:12 +000025#include <sys/ioctl.h>
Stefan Tauner8868db32012-03-13 00:18:19 +000026#include <linux/types.h>
Sven Schnelle5ce5f702011-09-03 18:37:52 +000027#include "flash.h"
Sven Schnelle5ce5f702011-09-03 18:37:52 +000028#include "programmer.h"
29#include "spi.h"
Fabrice Fontainee0ceedf2019-07-17 19:04:12 +020030/*
31 * Linux versions prior to v4.14-rc7 may need linux/ioctl.h included here due
32 * to missing from linux/spi/spidev.h. This was fixed in the following commit:
33 * a2b4a79b88b2 spi: uapi: spidev: add missing ioctl header
34 */
35#include <linux/ioctl.h>
36#include <linux/spi/spidev.h>
Sven Schnelle5ce5f702011-09-03 18:37:52 +000037
Stefan Tauner23e10b82016-01-23 16:16:49 +000038/* Devices known to work with this module (FIXME: export as struct dev_entry):
39 * Beagle Bone Black
40 * Raspberry Pi
41 * HummingBoard
42 */
43
Keno Fischer1f33cb52015-11-15 14:58:25 +000044#define BUF_SIZE_FROM_SYSFS "/sys/module/spidev/parameters/bufsiz"
Anastasia Klimchuk84f03922021-04-13 11:26:25 +100045
46struct linux_spi_data {
47 int fd;
48 size_t max_kernel_buf_size;
49};
Sven Schnelle5ce5f702011-09-03 18:37:52 +000050
51static int linux_spi_shutdown(void *data);
Nico Huber610c1aa2023-02-15 02:56:05 +010052static int linux_spi_send_command(
53 const struct spi_master *, unsigned int writecnt, unsigned int readcnt,
54 const unsigned char *txbuf, unsigned char *rxbuf);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000055
Nico Huber03f3a6d2021-05-11 17:53:34 +020056static const struct spi_master spi_master_linux = {
Nico Huber1cf407b2017-11-10 20:18:23 +010057 .features = SPI_MASTER_4BA,
Nico Huber522a86d2023-04-28 20:59:21 +000058 .max_data_read = MAX_DATA_UNSPECIFIED,
59 .max_data_write = MAX_DATA_UNSPECIFIED,
Sven Schnelle5ce5f702011-09-03 18:37:52 +000060 .command = linux_spi_send_command,
61 .multicommand = default_spi_send_multicommand,
Nico Huber522a86d2023-04-28 20:59:21 +000062 .read = default_spi_read,
63 .write_256 = default_spi_write_256,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +100064 .shutdown = linux_spi_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +053065 .probe_opcode = default_spi_probe_opcode,
Sven Schnelle5ce5f702011-09-03 18:37:52 +000066};
67
Anastasia Klimchukc19ef9b2021-04-12 16:52:58 +100068/* Read max buffer size from sysfs, or use page size as fallback. */
Anastasia Klimchuk2a329662021-04-19 11:12:01 +100069static size_t get_max_kernel_buf_size()
70{
Anastasia Klimchukc19ef9b2021-04-12 16:52:58 +100071 size_t result = 0;
72 FILE *fp;
73 fp = fopen(BUF_SIZE_FROM_SYSFS, "r");
74 if (!fp) {
75 msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
76 goto out;
77 }
78
79 char buf[10];
80 if (!fgets(buf, sizeof(buf), fp)) {
81 if (feof(fp))
82 msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS);
83 else
84 msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
85 goto out;
86 }
87
88 long int tmp;
89 errno = 0;
90 tmp = strtol(buf, NULL, 0);
91 if ((tmp < 0) || errno) {
92 msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS);
93 } else {
94 msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS);
95 result = (size_t)tmp;
96 }
97
98out:
99 if (fp)
100 fclose(fp);
101
102 if (!result) {
103 msg_pdbg("%s: Using page size as max buffer size.\n", __func__);
104 result = (size_t)getpagesize();
105 }
106 return result;
107}
108
Nico Hubere3a26882023-01-11 21:45:51 +0100109static int linux_spi_init(struct flashprog_programmer *const prog)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000110{
111 char *p, *endp, *dev;
Nico Huber9cecc7e2018-12-22 00:08:50 +0100112 uint32_t speed_hz = 2 * 1000 * 1000;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000113 /* FIXME: make the following configurable by CLI options. */
114 /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */
115 const uint8_t mode = SPI_MODE_0;
116 const uint8_t bits = 8;
Anastasia Klimchuk2a329662021-04-19 11:12:01 +1000117 int fd;
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000118 size_t max_kernel_buf_size;
119 struct linux_spi_data *spi_data;
Nico Huber522a86d2023-04-28 20:59:21 +0000120 struct spi_master spi_master = spi_master_linux;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000121
Stefan Tauner0554ca52013-07-25 22:54:25 +0000122 p = extract_programmer_param("spispeed");
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000123 if (p && strlen(p)) {
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +0000124 speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000;
Nico Huber9cecc7e2018-12-22 00:08:50 +0100125 if (p == endp || speed_hz == 0) {
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000126 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
Stefan Tauner0554ca52013-07-25 22:54:25 +0000127 free(p);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000128 return 1;
129 }
Nico Huber9cecc7e2018-12-22 00:08:50 +0100130 } else {
131 msg_pinfo("Using default %"PRIu32
132 "kHz clock. Use 'spispeed' parameter to override.\n",
133 speed_hz / 1000);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000134 }
Stefan Tauner0554ca52013-07-25 22:54:25 +0000135 free(p);
136
137 dev = extract_programmer_param("dev");
138 if (!dev || !strlen(dev)) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200139 msg_perr("No SPI device given. Use flashprog -p "
Stefan Tauner0554ca52013-07-25 22:54:25 +0000140 "linux_spi:dev=/dev/spidevX.Y\n");
141 free(dev);
142 return 1;
143 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000144
Sven Schnellecb24ddb2011-09-07 20:48:34 +0000145 msg_pdbg("Using device %s\n", dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000146 if ((fd = open(dev, O_RDWR)) == -1) {
147 msg_perr("%s: failed to open %s: %s\n", __func__,
148 dev, strerror(errno));
Stefan Tauner0554ca52013-07-25 22:54:25 +0000149 free(dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000150 return 1;
151 }
Stefan Tauner0554ca52013-07-25 22:54:25 +0000152 free(dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000153
Nico Huber9cecc7e2018-12-22 00:08:50 +0100154 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) {
155 msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n",
156 __func__, speed_hz, strerror(errno));
Anastasia Klimchukf876c0f2021-04-13 10:15:26 +1000157 goto init_err;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000158 }
Nico Huber9cecc7e2018-12-22 00:08:50 +0100159 msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000160
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000161 if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
162 msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n",
163 __func__, mode, strerror(errno));
Anastasia Klimchukf876c0f2021-04-13 10:15:26 +1000164 goto init_err;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000165 }
166
167 if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
168 msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n",
169 __func__, bits == 0 ? 8 : bits, strerror(errno));
Anastasia Klimchukf876c0f2021-04-13 10:15:26 +1000170 goto init_err;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000171 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000172
Anastasia Klimchukc19ef9b2021-04-12 16:52:58 +1000173 max_kernel_buf_size = get_max_kernel_buf_size();
Keno Fischer1f33cb52015-11-15 14:58:25 +0000174 msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size);
Anastasia Klimchukc19ef9b2021-04-12 16:52:58 +1000175
Nico Huber522a86d2023-04-28 20:59:21 +0000176 /* Older kernels use a single buffer for combined input and output
177 data. So account for longest possible command + address, too. */
178 spi_master.max_data_read = max_kernel_buf_size - 5;
179 spi_master.max_data_write = max_kernel_buf_size - 5;
180
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000181 spi_data = calloc(1, sizeof(*spi_data));
182 if (!spi_data) {
183 msg_perr("Unable to allocated space for SPI master data\n");
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000184 goto init_err;
185 }
186 spi_data->fd = fd;
187 spi_data->max_kernel_buf_size = max_kernel_buf_size;
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000188
Nico Huber522a86d2023-04-28 20:59:21 +0000189 return register_spi_master(&spi_master, 0, spi_data);
Anastasia Klimchukf876c0f2021-04-13 10:15:26 +1000190
191init_err:
Anastasia Klimchuk2a329662021-04-19 11:12:01 +1000192 close(fd);
193 return 1;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000194}
195
196static int linux_spi_shutdown(void *data)
197{
Anastasia Klimchuk2a329662021-04-19 11:12:01 +1000198 struct linux_spi_data *spi_data = data;
199 close(spi_data->fd);
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000200 free(spi_data);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000201 return 0;
202}
203
Nico Huber610c1aa2023-02-15 02:56:05 +0100204static int linux_spi_send_command(
205 const struct spi_master *mst, unsigned int writecnt, unsigned int readcnt,
206 const unsigned char *txbuf, unsigned char *rxbuf)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000207{
Nico Huber610c1aa2023-02-15 02:56:05 +0100208 struct linux_spi_data *spi_data = mst->data;
Michael Karcher8371d722012-03-06 22:17:06 +0000209 int iocontrol_code;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000210 struct spi_ioc_transfer msg[2] = {
211 {
David Riley18f50972014-08-05 22:16:01 +0000212 .tx_buf = (uint64_t)(uintptr_t)txbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000213 .len = writecnt,
214 },
215 {
David Riley18f50972014-08-05 22:16:01 +0000216 .rx_buf = (uint64_t)(uintptr_t)rxbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000217 .len = readcnt,
218 },
219 };
220
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000221 if (spi_data->fd == -1)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000222 return -1;
Michael Karcher8371d722012-03-06 22:17:06 +0000223 /* The implementation currently does not support requests that
224 don't start with sending a command. */
225 if (writecnt == 0)
226 return SPI_INVALID_LENGTH;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000227
Michael Karcher8371d722012-03-06 22:17:06 +0000228 /* Just submit the first (write) request in case there is nothing
229 to read. Otherwise submit both requests. */
230 if (readcnt == 0)
231 iocontrol_code = SPI_IOC_MESSAGE(1);
232 else
233 iocontrol_code = SPI_IOC_MESSAGE(2);
234
Anastasia Klimchuk84f03922021-04-13 11:26:25 +1000235 if (ioctl(spi_data->fd, iocontrol_code, msg) == -1) {
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000236 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
237 return -1;
238 }
239 return 0;
240}
241
Thomas Heijligencc853d82021-05-04 15:32:17 +0200242const struct programmer_entry programmer_linux_spi = {
243 .name = "linux_spi",
244 .type = OTHER,
245 .devs.note = "Device files /dev/spidev*.*\n",
246 .init = linux_spi_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +0200247};