blob: d551b00bd02be9248083ff44ac9541f3afac79ad [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.
14 *
uwe7df6dda2011-09-03 18:37:52 +000015 */
16
17#include <stdio.h>
18#include <string.h>
19#include <stdlib.h>
20#include <sys/fcntl.h>
21#include <errno.h>
22#include <ctype.h>
23#include <unistd.h>
24#include <linux/spi/spidev.h>
25#include <sys/ioctl.h>
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080026#include <sys/stat.h>
27#include <sys/types.h>
David Hendricks98bbc262013-10-30 21:38:58 -070028#include "file.h"
uwe7df6dda2011-09-03 18:37:52 +000029#include "flash.h"
30#include "chipdrivers.h"
31#include "programmer.h"
32#include "spi.h"
33
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +080034
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080035
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +080036/* TODO: this information should come from the SPI master driver. */
37#define SPI_DMA_SIZE 64
38
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +080039#ifndef SPIDEV_MAJOR
40#define SPIDEV_MAJOR 153 /* refer to kernel/files/drivers/spi/spidev.c */
41#endif
42
David Hendricks98bbc262013-10-30 21:38:58 -070043#define MODALIAS_FILE "modalias"
44#define LINUX_SPI_SYSFS_ROOT "/sys/bus/spi/devices"
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +080045
uwe7df6dda2011-09-03 18:37:52 +000046static int fd = -1;
47
David Hendricks93784b42016-08-09 17:00:38 -070048static int linux_spi_shutdown(void *data);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070049static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
uwe7df6dda2011-09-03 18:37:52 +000050 const unsigned char *txbuf, unsigned char *rxbuf);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070051static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000052 unsigned int start, unsigned int len);
Patrick Georgiab8353e2017-02-03 18:32:01 +010053static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000054 unsigned int start, unsigned int len);
uwe7df6dda2011-09-03 18:37:52 +000055
Patrick Georgif4f1e2f2017-03-10 17:38:40 +010056static const struct spi_master spi_master_linux = {
uwe7df6dda2011-09-03 18:37:52 +000057 .type = SPI_CONTROLLER_LINUX,
Edward O'Callaghana6673bd2019-06-24 15:22:28 +100058 .features = SPI_MASTER_4BA,
uwe7df6dda2011-09-03 18:37:52 +000059 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
60 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
61 .command = linux_spi_send_command,
62 .multicommand = default_spi_send_multicommand,
63 .read = linux_spi_read,
64 .write_256 = linux_spi_write_256,
65};
66
David Hendricks98bbc262013-10-30 21:38:58 -070067static char devfs_path[32]; /* at least big enough to fit /dev/spidevX.Y */
David Hendricks2d069182015-10-30 22:52:57 -070068static char *check_sysfs(void)
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080069{
David Hendricks98bbc262013-10-30 21:38:58 -070070 int i;
71 const char *sysfs_path = NULL;
72 char *p;
73 char *modalias[] = {
David Hendricksdca1adf2013-11-01 13:31:57 -070074 "spi:spidev", /* raw access over SPI bus (newer kernels) */
David Hendricks6ed26852013-11-12 12:53:40 -080075 "spidev", /* raw access over SPI bus (older kernels) */
76 "m25p80", /* generic MTD device */
David Hendricks98bbc262013-10-30 21:38:58 -070077 };
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080078
David Hendricks98bbc262013-10-30 21:38:58 -070079 for (i = 0; i < ARRAY_SIZE(modalias); i++) {
80 int major, minor;
David Hendricks0c0a7e62012-06-12 23:41:05 +090081
David Hendricks98bbc262013-10-30 21:38:58 -070082 /* Path should look like: /sys/blah/spiX.Y/modalias */
83 sysfs_path = scanft(LINUX_SPI_SYSFS_ROOT,
David Hendrickse94c7a82013-11-12 14:22:36 -080084 MODALIAS_FILE, modalias[i], 1);
David Hendricks98bbc262013-10-30 21:38:58 -070085 if (!sysfs_path)
Louis Yung-Chieh Loeaa44fc2012-05-24 15:43:50 +080086 continue;
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080087
David Hendricks98bbc262013-10-30 21:38:58 -070088 p = (char *)sysfs_path + strlen(LINUX_SPI_SYSFS_ROOT);
89 if (p[0] == '/')
90 p++;
David Hendricks0c0a7e62012-06-12 23:41:05 +090091
David Hendricks98bbc262013-10-30 21:38:58 -070092 if (sscanf(p, "spi%u.%u", &major, &minor) == 2) {
David Hendricksd02ee0f2013-11-01 13:18:48 -070093 msg_pdbg("Found SPI device %s on spi%u.%u\n",
David Hendricks98bbc262013-10-30 21:38:58 -070094 modalias[i], major, minor);
95 sprintf(devfs_path, "/dev/spidev%u.%u", major, minor);
96 free((void *)sysfs_path);
97 break;
98 }
99 free((void *)sysfs_path);
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +0800100 }
101
David Hendricks98bbc262013-10-30 21:38:58 -0700102 if (i == ARRAY_SIZE(modalias))
103 return NULL;
104 return devfs_path;
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +0800105}
106
David Hendricks2d069182015-10-30 22:52:57 -0700107static char *check_fdt(void)
108{
109 unsigned int bus, cs;
110
111 if (fdt_find_spi_nor_flash(&bus, &cs) < 0)
112 return NULL;
113
114 sprintf(devfs_path, "/dev/spidev%u.%u", bus, cs);
115 return devfs_path;
116}
117
118static char *linux_spi_probe(void)
119{
120 char *ret;
121
122 ret = check_fdt();
123 if (ret)
124 return ret;
125
126 ret = check_sysfs();
127 if (ret)
128 return ret;
129
130 return NULL;
131}
132
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800133/*
134 * This is used when /dev/spidevX.Y is not created yet, for example, when
135 * udev is not started.
136 */
137static int manual_mknod(const char *dev)
138{
139 char cmd[256];
140
141 msg_pdbg("Creating SPI device node %s...\n", dev);
142 strcpy(cmd, "modprobe spidev");
143 msg_pdbg("CMD: [%s]\n", cmd);
Simon Glass7ef84ff2013-02-10 17:13:41 -0800144 if (system(cmd)) {
145 msg_perr("%s: failed to run '%s': %s\n", __func__,
146 cmd, strerror(errno));
147 return -1;
148 }
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800149 snprintf(cmd, sizeof(cmd), "mknod %s c %d 0", dev, SPIDEV_MAJOR);
150 msg_pdbg("CMD: [%s]\n", cmd);
Simon Glass7ef84ff2013-02-10 17:13:41 -0800151 if (system(cmd)) {
152 msg_perr("%s: failed to run '%s': %s\n", __func__,
153 cmd, strerror(errno));
154 return -1;
155 }
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800156
157 if ((fd = open(dev, O_RDWR)) == -1) {
158 msg_perr("%s: failed to open %s: %s\n", __func__,
159 dev, strerror(errno));
160 }
161
162 return fd;
163}
164
165
David Hendricksac1d25c2016-08-09 17:00:58 -0700166int linux_spi_init(void)
uwe7df6dda2011-09-03 18:37:52 +0000167{
David Hendricks98b3c572016-11-30 01:50:08 +0000168 char *p, *endp, *dev;
uwe6b716f02011-09-07 20:48:34 +0000169 uint32_t speed = 0;
uwe7df6dda2011-09-03 18:37:52 +0000170
David Hendricksba0827a2013-05-03 20:25:40 -0700171 /*
172 * FIXME: There might be other programmers with flash memory (such as
173 * an EC) connected via SPI. For now we rely on the device's driver to
174 * distinguish it and assume generic SPI implies host.
175 */
176 if (alias && alias->type != ALIAS_HOST)
177 return 1;
178
uwe7df6dda2011-09-03 18:37:52 +0000179 dev = extract_programmer_param("dev");
David Hendricks98bbc262013-10-30 21:38:58 -0700180 if (!dev)
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +0800181 dev = linux_spi_probe();
uwe7df6dda2011-09-03 18:37:52 +0000182 if (!dev || !strlen(dev)) {
David Hendricks98bbc262013-10-30 21:38:58 -0700183 msg_perr("No SPI device found. Use flashrom -p "
uwe7df6dda2011-09-03 18:37:52 +0000184 "linux_spi:dev=/dev/spidevX.Y\n");
David Hendricks98b3c572016-11-30 01:50:08 +0000185 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000186 }
187
188 p = extract_programmer_param("speed");
189 if (p && strlen(p)) {
uwe6b716f02011-09-07 20:48:34 +0000190 speed = (uint32_t)strtoul(p, &endp, 10) * 1024;
uwe7df6dda2011-09-03 18:37:52 +0000191 if (p == endp) {
192 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
David Hendricks98b3c572016-11-30 01:50:08 +0000193 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000194 }
195 }
196
uwe6b716f02011-09-07 20:48:34 +0000197 msg_pdbg("Using device %s\n", dev);
uwe7df6dda2011-09-03 18:37:52 +0000198 if ((fd = open(dev, O_RDWR)) == -1) {
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800199 msg_pdbg("%s: failed to open %s: %s\n", __func__,
uwe7df6dda2011-09-03 18:37:52 +0000200 dev, strerror(errno));
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800201
202 if (manual_mknod(dev) == -1) { // global fd is effected.
David Hendricks98b3c572016-11-30 01:50:08 +0000203 return 1;
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800204 }
uwe7df6dda2011-09-03 18:37:52 +0000205 }
206
uwe6b716f02011-09-07 20:48:34 +0000207 if (speed > 0) {
208 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
209 msg_perr("%s: failed to set speed %dHz: %s\n",
210 __func__, speed, strerror(errno));
211 close(fd);
David Hendricks98b3c572016-11-30 01:50:08 +0000212 return 1;
uwe6b716f02011-09-07 20:48:34 +0000213 }
214
215 msg_pdbg("Using %d kHz clock\n", speed);
uwe7df6dda2011-09-03 18:37:52 +0000216 }
217
David Hendricks98b3c572016-11-30 01:50:08 +0000218 if (register_shutdown(linux_spi_shutdown, NULL))
219 return 1;
uwe7df6dda2011-09-03 18:37:52 +0000220
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100221 register_spi_master(&spi_master_linux);
uwe7df6dda2011-09-03 18:37:52 +0000222
David Hendricks98b3c572016-11-30 01:50:08 +0000223 return 0;
uwe7df6dda2011-09-03 18:37:52 +0000224}
225
David Hendricks93784b42016-08-09 17:00:38 -0700226static int linux_spi_shutdown(void *data)
uwe7df6dda2011-09-03 18:37:52 +0000227{
228 if (fd != -1) {
229 close(fd);
230 fd = -1;
231 }
232 return 0;
233}
234
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700235static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
uwe7df6dda2011-09-03 18:37:52 +0000236 const unsigned char *txbuf, unsigned char *rxbuf)
237{
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800238 int msg_start = 0, msg_count = 0;
uwe7df6dda2011-09-03 18:37:52 +0000239 struct spi_ioc_transfer msg[2] = {
240 {
David Riley13343b82014-07-25 11:37:56 -0700241 .tx_buf = (uint64_t)(uintptr_t)txbuf,
uwe7df6dda2011-09-03 18:37:52 +0000242 .len = writecnt,
243 },
244 {
David Riley13343b82014-07-25 11:37:56 -0700245 .rx_buf = (uint64_t)(uintptr_t)rxbuf,
uwe7df6dda2011-09-03 18:37:52 +0000246 .len = readcnt,
247 },
248 };
249
250 if (fd == -1)
251 return -1;
252
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800253 /* Only pass necessary msg[] to ioctl() to avoid the empty message
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800254 * which drives an un-expected CS line and clocks. */
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800255 if (writecnt) {
256 msg_start = 0; /* tx: msg[0] */
257 msg_count++;
258 if (readcnt) {
259 msg_count++;
260 }
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800261 } else if (readcnt) {
262 msg_start = 1; /* rx: msg[1] */
263 msg_count++;
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800264 } else {
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800265 msg_cerr("%s: both writecnt and readcnt are 0.\n",
266 __func__);
267 return -1;
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800268 }
269
270 if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) {
uwe7df6dda2011-09-03 18:37:52 +0000271 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
272 return -1;
273 }
274 return 0;
275}
276
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700277static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000278 unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000279{
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800280 return spi_read_chunked(flash, buf, start, len, SPI_DMA_SIZE);
uwe7df6dda2011-09-03 18:37:52 +0000281}
282
Patrick Georgiab8353e2017-02-03 18:32:01 +0100283static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000284 unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000285{
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800286 return spi_write_chunked(flash, buf, start, len,
287 SPI_DMA_SIZE - 5 /* WREN, Page Program */);
uwe7df6dda2011-09-03 18:37:52 +0000288}