blob: 611559ac059a5f0b56a08dab5889f77b7c8472ad [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
Stefan Tauner8868db32012-03-13 00:18:19 +000020#if CONFIG_LINUX_SPI == 1
21
Sven Schnelle5ce5f702011-09-03 18:37:52 +000022#include <stdio.h>
23#include <string.h>
24#include <stdlib.h>
Gwenhael Goavec-Merou8cd0c732015-11-14 02:55:12 +000025#include <fcntl.h>
Sven Schnelle5ce5f702011-09-03 18:37:52 +000026#include <errno.h>
27#include <ctype.h>
28#include <unistd.h>
Gwenhael Goavec-Merou8cd0c732015-11-14 02:55:12 +000029#include <sys/ioctl.h>
Stefan Tauner8868db32012-03-13 00:18:19 +000030#include <linux/types.h>
Sven Schnelle5ce5f702011-09-03 18:37:52 +000031#include <linux/spi/spidev.h>
Gwenhael Goavec-Merou8cd0c732015-11-14 02:55:12 +000032#include <linux/ioctl.h>
Sven Schnelle5ce5f702011-09-03 18:37:52 +000033#include "flash.h"
34#include "chipdrivers.h"
35#include "programmer.h"
36#include "spi.h"
37
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
Sven Schnelle5ce5f702011-09-03 18:37:52 +000044static int fd = -1;
Keno Fischer1f33cb52015-11-15 14:58:25 +000045#define BUF_SIZE_FROM_SYSFS "/sys/module/spidev/parameters/bufsiz"
46static size_t max_kernel_buf_size;
Sven Schnelle5ce5f702011-09-03 18:37:52 +000047
48static int linux_spi_shutdown(void *data);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000049static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt,
50 unsigned int readcnt,
51 const unsigned char *txbuf,
52 unsigned char *rxbuf);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000053static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000054 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +000055static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000056 unsigned int start, unsigned int len);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000057
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000058static const struct spi_master spi_master_linux = {
Sven Schnelle5ce5f702011-09-03 18:37:52 +000059 .type = SPI_CONTROLLER_LINUX,
60 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
61 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
62 .command = linux_spi_send_command,
63 .multicommand = default_spi_send_multicommand,
64 .read = linux_spi_read,
65 .write_256 = linux_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +000066 .write_aai = default_spi_write_aai,
Sven Schnelle5ce5f702011-09-03 18:37:52 +000067};
68
69int linux_spi_init(void)
70{
71 char *p, *endp, *dev;
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +000072 uint32_t speed_hz = 0;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +000073 /* FIXME: make the following configurable by CLI options. */
74 /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */
75 const uint8_t mode = SPI_MODE_0;
76 const uint8_t bits = 8;
Sven Schnelle5ce5f702011-09-03 18:37:52 +000077
Stefan Tauner0554ca52013-07-25 22:54:25 +000078 p = extract_programmer_param("spispeed");
Sven Schnelle5ce5f702011-09-03 18:37:52 +000079 if (p && strlen(p)) {
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +000080 speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000;
Sven Schnelle5ce5f702011-09-03 18:37:52 +000081 if (p == endp) {
82 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
Stefan Tauner0554ca52013-07-25 22:54:25 +000083 free(p);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000084 return 1;
85 }
86 }
Stefan Tauner0554ca52013-07-25 22:54:25 +000087 free(p);
88
89 dev = extract_programmer_param("dev");
90 if (!dev || !strlen(dev)) {
91 msg_perr("No SPI device given. Use flashrom -p "
92 "linux_spi:dev=/dev/spidevX.Y\n");
93 free(dev);
94 return 1;
95 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +000096
Sven Schnellecb24ddb2011-09-07 20:48:34 +000097 msg_pdbg("Using device %s\n", dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000098 if ((fd = open(dev, O_RDWR)) == -1) {
99 msg_perr("%s: failed to open %s: %s\n", __func__,
100 dev, strerror(errno));
Stefan Tauner0554ca52013-07-25 22:54:25 +0000101 free(dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000102 return 1;
103 }
Stefan Tauner0554ca52013-07-25 22:54:25 +0000104 free(dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000105
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000106 if (register_shutdown(linux_spi_shutdown, NULL))
107 return 1;
108 /* We rely on the shutdown function for cleanup from here on. */
109
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +0000110 if (speed_hz > 0) {
111 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) {
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000112 msg_perr("%s: failed to set speed to %d Hz: %s\n",
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +0000113 __func__, speed_hz, strerror(errno));
Sven Schnellecb24ddb2011-09-07 20:48:34 +0000114 return 1;
115 }
116
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +0000117 msg_pdbg("Using %d kHz clock\n", speed_hz/1000);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000118 }
119
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000120 if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
121 msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n",
122 __func__, mode, strerror(errno));
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000123 return 1;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000124 }
125
126 if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
127 msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n",
128 __func__, bits == 0 ? 8 : bits, strerror(errno));
129 return 1;
130 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000131
Keno Fischer1f33cb52015-11-15 14:58:25 +0000132 /* Read max buffer size from sysfs, or use page size as fallback. */
133 FILE *fp;
134 fp = fopen(BUF_SIZE_FROM_SYSFS, "r");
135 if (!fp) {
136 msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
137 goto out;
138 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000139
Keno Fischer1f33cb52015-11-15 14:58:25 +0000140 char buf[10];
141 memset(buf, 0, sizeof(buf));
142 if (!fread(buf, 1, sizeof(buf) - 1, fp)) {
143 if (feof(fp))
144 msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS);
145 else
146 msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno));
147 goto out;
148 }
149
150 long int tmp;
151 errno = 0;
152 tmp = strtol(buf, NULL, 0);
153 if ((tmp < 0) || errno) {
154 msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS);
155 } else {
156 msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS);
157 max_kernel_buf_size = (size_t)tmp;
158 }
159
160out:
161 if (fp)
162 fclose(fp);
163
164 if (!max_kernel_buf_size) {
165 msg_pdbg("%s: Using page size as max buffer size.\n", __func__);
166 max_kernel_buf_size = (size_t)getpagesize();
167 }
168
169 msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size);
170 register_spi_master(&spi_master_linux);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000171 return 0;
172}
173
174static int linux_spi_shutdown(void *data)
175{
176 if (fd != -1) {
177 close(fd);
178 fd = -1;
179 }
180 return 0;
181}
182
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000183static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt,
184 unsigned int readcnt,
185 const unsigned char *txbuf,
186 unsigned char *rxbuf)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000187{
Michael Karcher8371d722012-03-06 22:17:06 +0000188 int iocontrol_code;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000189 struct spi_ioc_transfer msg[2] = {
190 {
David Riley18f50972014-08-05 22:16:01 +0000191 .tx_buf = (uint64_t)(uintptr_t)txbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000192 .len = writecnt,
193 },
194 {
David Riley18f50972014-08-05 22:16:01 +0000195 .rx_buf = (uint64_t)(uintptr_t)rxbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000196 .len = readcnt,
197 },
198 };
199
200 if (fd == -1)
201 return -1;
Michael Karcher8371d722012-03-06 22:17:06 +0000202 /* The implementation currently does not support requests that
203 don't start with sending a command. */
204 if (writecnt == 0)
205 return SPI_INVALID_LENGTH;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000206
Michael Karcher8371d722012-03-06 22:17:06 +0000207 /* Just submit the first (write) request in case there is nothing
208 to read. Otherwise submit both requests. */
209 if (readcnt == 0)
210 iocontrol_code = SPI_IOC_MESSAGE(1);
211 else
212 iocontrol_code = SPI_IOC_MESSAGE(2);
213
214 if (ioctl(fd, iocontrol_code, msg) == -1) {
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000215 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
216 return -1;
217 }
218 return 0;
219}
220
Keno Fischer1f33cb52015-11-15 14:58:25 +0000221static int linux_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000222{
Keno Fischer1f33cb52015-11-15 14:58:25 +0000223 /* Read buffer is fully utilized for data. */
224 return spi_read_chunked(flash, buf, start, len, max_kernel_buf_size);
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