blob: de09e607b03016daca2957572eca4c846398e1a2 [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 <linux/spi/spidev.h>
Gwenhael Goavec-Merou8cd0c732015-11-14 02:55:12 +000030#include <linux/ioctl.h>
Sven Schnelle5ce5f702011-09-03 18:37:52 +000031#include "flash.h"
32#include "chipdrivers.h"
33#include "programmer.h"
34#include "spi.h"
35
Stefan Tauner23e10b82016-01-23 16:16:49 +000036/* Devices known to work with this module (FIXME: export as struct dev_entry):
37 * Beagle Bone Black
38 * Raspberry Pi
39 * HummingBoard
40 */
41
Sven Schnelle5ce5f702011-09-03 18:37:52 +000042static int fd = -1;
Keno Fischer1f33cb52015-11-15 14:58:25 +000043#define BUF_SIZE_FROM_SYSFS "/sys/module/spidev/parameters/bufsiz"
44static size_t max_kernel_buf_size;
Sven Schnelle5ce5f702011-09-03 18:37:52 +000045
46static int linux_spi_shutdown(void *data);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000047static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt,
48 unsigned int readcnt,
49 const unsigned char *txbuf,
50 unsigned char *rxbuf);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000051static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000052 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +000053static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000054 unsigned int start, unsigned int len);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000055
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000056static const struct spi_master spi_master_linux = {
Nico Huber1cf407b2017-11-10 20:18:23 +010057 .features = SPI_MASTER_4BA,
Sven Schnelle5ce5f702011-09-03 18:37:52 +000058 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
59 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
60 .command = linux_spi_send_command,
61 .multicommand = default_spi_send_multicommand,
62 .read = linux_spi_read,
63 .write_256 = linux_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +000064 .write_aai = default_spi_write_aai,
Sven Schnelle5ce5f702011-09-03 18:37:52 +000065};
66
67int linux_spi_init(void)
68{
69 char *p, *endp, *dev;
Nico Huber9cecc7e2018-12-22 00:08:50 +010070 uint32_t speed_hz = 2 * 1000 * 1000;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +000071 /* FIXME: make the following configurable by CLI options. */
72 /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */
73 const uint8_t mode = SPI_MODE_0;
74 const uint8_t bits = 8;
Sven Schnelle5ce5f702011-09-03 18:37:52 +000075
Stefan Tauner0554ca52013-07-25 22:54:25 +000076 p = extract_programmer_param("spispeed");
Sven Schnelle5ce5f702011-09-03 18:37:52 +000077 if (p && strlen(p)) {
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +000078 speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000;
Nico Huber9cecc7e2018-12-22 00:08:50 +010079 if (p == endp || speed_hz == 0) {
Sven Schnelle5ce5f702011-09-03 18:37:52 +000080 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
Stefan Tauner0554ca52013-07-25 22:54:25 +000081 free(p);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000082 return 1;
83 }
Nico Huber9cecc7e2018-12-22 00:08:50 +010084 } else {
85 msg_pinfo("Using default %"PRIu32
86 "kHz clock. Use 'spispeed' parameter to override.\n",
87 speed_hz / 1000);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000088 }
Stefan Tauner0554ca52013-07-25 22:54:25 +000089 free(p);
90
91 dev = extract_programmer_param("dev");
92 if (!dev || !strlen(dev)) {
93 msg_perr("No SPI device given. Use flashrom -p "
94 "linux_spi:dev=/dev/spidevX.Y\n");
95 free(dev);
96 return 1;
97 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +000098
Sven Schnellecb24ddb2011-09-07 20:48:34 +000099 msg_pdbg("Using device %s\n", dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000100 if ((fd = open(dev, O_RDWR)) == -1) {
101 msg_perr("%s: failed to open %s: %s\n", __func__,
102 dev, strerror(errno));
Stefan Tauner0554ca52013-07-25 22:54:25 +0000103 free(dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000104 return 1;
105 }
Stefan Tauner0554ca52013-07-25 22:54:25 +0000106 free(dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000107
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000108 if (register_shutdown(linux_spi_shutdown, NULL))
109 return 1;
110 /* We rely on the shutdown function for cleanup from here on. */
111
Nico Huber9cecc7e2018-12-22 00:08:50 +0100112 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) {
113 msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n",
114 __func__, speed_hz, strerror(errno));
115 return 1;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000116 }
Nico Huber9cecc7e2018-12-22 00:08:50 +0100117 msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000118
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000119 if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
120 msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n",
121 __func__, mode, strerror(errno));
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000122 return 1;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000123 }
124
125 if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
126 msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n",
127 __func__, bits == 0 ? 8 : bits, strerror(errno));
128 return 1;
129 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000130
Keno Fischer1f33cb52015-11-15 14:58:25 +0000131 /* Read max buffer size from sysfs, or use page size as fallback. */
132 FILE *fp;
133 fp = fopen(BUF_SIZE_FROM_SYSFS, "r");
134 if (!fp) {
135 msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
136 goto out;
137 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000138
Keno Fischer1f33cb52015-11-15 14:58:25 +0000139 char buf[10];
140 memset(buf, 0, sizeof(buf));
141 if (!fread(buf, 1, sizeof(buf) - 1, fp)) {
142 if (feof(fp))
143 msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS);
144 else
145 msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
146 goto out;
147 }
148
149 long int tmp;
150 errno = 0;
151 tmp = strtol(buf, NULL, 0);
152 if ((tmp < 0) || errno) {
153 msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS);
154 } else {
155 msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS);
156 max_kernel_buf_size = (size_t)tmp;
157 }
158
159out:
160 if (fp)
161 fclose(fp);
162
163 if (!max_kernel_buf_size) {
164 msg_pdbg("%s: Using page size as max buffer size.\n", __func__);
165 max_kernel_buf_size = (size_t)getpagesize();
166 }
167
168 msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size);
169 register_spi_master(&spi_master_linux);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000170 return 0;
171}
172
173static int linux_spi_shutdown(void *data)
174{
175 if (fd != -1) {
176 close(fd);
177 fd = -1;
178 }
179 return 0;
180}
181
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000182static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt,
183 unsigned int readcnt,
184 const unsigned char *txbuf,
185 unsigned char *rxbuf)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000186{
Michael Karcher8371d722012-03-06 22:17:06 +0000187 int iocontrol_code;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000188 struct spi_ioc_transfer msg[2] = {
189 {
David Riley18f50972014-08-05 22:16:01 +0000190 .tx_buf = (uint64_t)(uintptr_t)txbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000191 .len = writecnt,
192 },
193 {
David Riley18f50972014-08-05 22:16:01 +0000194 .rx_buf = (uint64_t)(uintptr_t)rxbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000195 .len = readcnt,
196 },
197 };
198
199 if (fd == -1)
200 return -1;
Michael Karcher8371d722012-03-06 22:17:06 +0000201 /* The implementation currently does not support requests that
202 don't start with sending a command. */
203 if (writecnt == 0)
204 return SPI_INVALID_LENGTH;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000205
Michael Karcher8371d722012-03-06 22:17:06 +0000206 /* Just submit the first (write) request in case there is nothing
207 to read. Otherwise submit both requests. */
208 if (readcnt == 0)
209 iocontrol_code = SPI_IOC_MESSAGE(1);
210 else
211 iocontrol_code = SPI_IOC_MESSAGE(2);
212
213 if (ioctl(fd, iocontrol_code, msg) == -1) {
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000214 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
215 return -1;
216 }
217 return 0;
218}
219
Keno Fischer1f33cb52015-11-15 14:58:25 +0000220static int linux_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000221{
Nico Huber22418422018-03-08 16:14:15 +0100222 /* Older kernels use a single buffer for combined input and output
223 data. So account for longest possible command + address, too. */
224 return spi_read_chunked(flash, buf, start, len, max_kernel_buf_size - 5);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000225}
226
Mark Marshallf20b7be2014-05-09 21:16:21 +0000227static 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 +0000228{
Keno Fischer1f33cb52015-11-15 14:58:25 +0000229 /* 5 bytes must be reserved for longest possible command + address. */
230 return spi_write_chunked(flash, buf, start, len, max_kernel_buf_size - 5);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000231}
Stefan Tauner8868db32012-03-13 00:18:19 +0000232
233#endif // CONFIG_LINUX_SPI == 1