blob: fd0368ff56717b671bd6fc683aac9b0022a632d9 [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 *
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
20#include <stdio.h>
21#include <string.h>
22#include <stdlib.h>
23#include <sys/fcntl.h>
24#include <errno.h>
25#include <ctype.h>
26#include <unistd.h>
27#include <linux/spi/spidev.h>
28#include <sys/ioctl.h>
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080029#include <sys/stat.h>
30#include <sys/types.h>
David Hendricks98bbc262013-10-30 21:38:58 -070031#include "file.h"
uwe7df6dda2011-09-03 18:37:52 +000032#include "flash.h"
33#include "chipdrivers.h"
34#include "programmer.h"
35#include "spi.h"
36
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +080037
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080038
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +080039/* TODO: this information should come from the SPI master driver. */
40#define SPI_DMA_SIZE 64
41
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +080042#ifndef SPIDEV_MAJOR
43#define SPIDEV_MAJOR 153 /* refer to kernel/files/drivers/spi/spidev.c */
44#endif
45
David Hendricks98bbc262013-10-30 21:38:58 -070046#define MODALIAS_FILE "modalias"
47#define LINUX_SPI_SYSFS_ROOT "/sys/bus/spi/devices"
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +080048
uwe7df6dda2011-09-03 18:37:52 +000049static int fd = -1;
50
51static int linux_spi_shutdown(void *data);
52static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt,
53 const unsigned char *txbuf, unsigned char *rxbuf);
stefanctc5eb8a92011-11-23 09:13:48 +000054static int linux_spi_read(struct flashchip *flash, uint8_t *buf,
55 unsigned int start, unsigned int len);
uwe7df6dda2011-09-03 18:37:52 +000056static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000057 unsigned int start, unsigned int len);
uwe7df6dda2011-09-03 18:37:52 +000058
59static const struct spi_programmer spi_programmer_linux = {
60 .type = SPI_CONTROLLER_LINUX,
61 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
62 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
63 .command = linux_spi_send_command,
64 .multicommand = default_spi_send_multicommand,
65 .read = linux_spi_read,
66 .write_256 = linux_spi_write_256,
67};
68
David Hendricks98bbc262013-10-30 21:38:58 -070069static char devfs_path[32]; /* at least big enough to fit /dev/spidevX.Y */
70static char *linux_spi_probe(void)
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080071{
David Hendricks98bbc262013-10-30 21:38:58 -070072 int i;
73 const char *sysfs_path = NULL;
74 char *p;
75 char *modalias[] = {
David Hendricksdca1adf2013-11-01 13:31:57 -070076 "spi:spidev", /* raw access over SPI bus (newer kernels) */
David Hendricks6ed26852013-11-12 12:53:40 -080077 "spidev", /* raw access over SPI bus (older kernels) */
78 "m25p80", /* generic MTD device */
David Hendricks98bbc262013-10-30 21:38:58 -070079 };
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080080
David Hendricks98bbc262013-10-30 21:38:58 -070081 for (i = 0; i < ARRAY_SIZE(modalias); i++) {
82 int major, minor;
David Hendricks0c0a7e62012-06-12 23:41:05 +090083
David Hendricks98bbc262013-10-30 21:38:58 -070084 /* Path should look like: /sys/blah/spiX.Y/modalias */
85 sysfs_path = scanft(LINUX_SPI_SYSFS_ROOT,
86 MODALIAS_FILE, modalias[i], 2);
87 if (!sysfs_path)
Louis Yung-Chieh Loeaa44fc2012-05-24 15:43:50 +080088 continue;
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080089
David Hendricks98bbc262013-10-30 21:38:58 -070090 p = (char *)sysfs_path + strlen(LINUX_SPI_SYSFS_ROOT);
91 if (p[0] == '/')
92 p++;
David Hendricks0c0a7e62012-06-12 23:41:05 +090093
David Hendricks98bbc262013-10-30 21:38:58 -070094 if (sscanf(p, "spi%u.%u", &major, &minor) == 2) {
David Hendricksd02ee0f2013-11-01 13:18:48 -070095 msg_pdbg("Found SPI device %s on spi%u.%u\n",
David Hendricks98bbc262013-10-30 21:38:58 -070096 modalias[i], major, minor);
97 sprintf(devfs_path, "/dev/spidev%u.%u", major, minor);
98 free((void *)sysfs_path);
99 break;
100 }
101 free((void *)sysfs_path);
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +0800102 }
103
David Hendricks98bbc262013-10-30 21:38:58 -0700104 if (i == ARRAY_SIZE(modalias))
105 return NULL;
106 return devfs_path;
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +0800107}
108
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800109/*
110 * This is used when /dev/spidevX.Y is not created yet, for example, when
111 * udev is not started.
112 */
113static int manual_mknod(const char *dev)
114{
115 char cmd[256];
116
117 msg_pdbg("Creating SPI device node %s...\n", dev);
118 strcpy(cmd, "modprobe spidev");
119 msg_pdbg("CMD: [%s]\n", cmd);
Simon Glass7ef84ff2013-02-10 17:13:41 -0800120 if (system(cmd)) {
121 msg_perr("%s: failed to run '%s': %s\n", __func__,
122 cmd, strerror(errno));
123 return -1;
124 }
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800125 snprintf(cmd, sizeof(cmd), "mknod %s c %d 0", dev, SPIDEV_MAJOR);
126 msg_pdbg("CMD: [%s]\n", cmd);
Simon Glass7ef84ff2013-02-10 17:13:41 -0800127 if (system(cmd)) {
128 msg_perr("%s: failed to run '%s': %s\n", __func__,
129 cmd, strerror(errno));
130 return -1;
131 }
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800132
133 if ((fd = open(dev, O_RDWR)) == -1) {
134 msg_perr("%s: failed to open %s: %s\n", __func__,
135 dev, strerror(errno));
136 }
137
138 return fd;
139}
140
141
uwe7df6dda2011-09-03 18:37:52 +0000142int linux_spi_init(void)
143{
144 char *p, *endp, *dev;
uwe6b716f02011-09-07 20:48:34 +0000145 uint32_t speed = 0;
uwe7df6dda2011-09-03 18:37:52 +0000146
David Hendricksba0827a2013-05-03 20:25:40 -0700147 /*
148 * FIXME: There might be other programmers with flash memory (such as
149 * an EC) connected via SPI. For now we rely on the device's driver to
150 * distinguish it and assume generic SPI implies host.
151 */
152 if (alias && alias->type != ALIAS_HOST)
153 return 1;
154
uwe7df6dda2011-09-03 18:37:52 +0000155 dev = extract_programmer_param("dev");
David Hendricks98bbc262013-10-30 21:38:58 -0700156 if (!dev)
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +0800157 dev = linux_spi_probe();
uwe7df6dda2011-09-03 18:37:52 +0000158 if (!dev || !strlen(dev)) {
David Hendricks98bbc262013-10-30 21:38:58 -0700159 msg_perr("No SPI device found. Use flashrom -p "
uwe7df6dda2011-09-03 18:37:52 +0000160 "linux_spi:dev=/dev/spidevX.Y\n");
161 return 1;
162 }
163
164 p = extract_programmer_param("speed");
165 if (p && strlen(p)) {
uwe6b716f02011-09-07 20:48:34 +0000166 speed = (uint32_t)strtoul(p, &endp, 10) * 1024;
uwe7df6dda2011-09-03 18:37:52 +0000167 if (p == endp) {
168 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
169 return 1;
170 }
171 }
172
uwe6b716f02011-09-07 20:48:34 +0000173 msg_pdbg("Using device %s\n", dev);
uwe7df6dda2011-09-03 18:37:52 +0000174 if ((fd = open(dev, O_RDWR)) == -1) {
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800175 msg_pdbg("%s: failed to open %s: %s\n", __func__,
uwe7df6dda2011-09-03 18:37:52 +0000176 dev, strerror(errno));
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800177
178 if (manual_mknod(dev) == -1) { // global fd is effected.
179 return 1;
180 }
uwe7df6dda2011-09-03 18:37:52 +0000181 }
182
uwe6b716f02011-09-07 20:48:34 +0000183 if (speed > 0) {
184 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
185 msg_perr("%s: failed to set speed %dHz: %s\n",
186 __func__, speed, strerror(errno));
187 close(fd);
188 return 1;
189 }
190
191 msg_pdbg("Using %d kHz clock\n", speed);
uwe7df6dda2011-09-03 18:37:52 +0000192 }
193
194 if (register_shutdown(linux_spi_shutdown, NULL))
195 return 1;
196
197 register_spi_programmer(&spi_programmer_linux);
198
199 return 0;
200}
201
202static int linux_spi_shutdown(void *data)
203{
204 if (fd != -1) {
205 close(fd);
206 fd = -1;
207 }
208 return 0;
209}
210
211static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt,
212 const unsigned char *txbuf, unsigned char *rxbuf)
213{
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800214 int msg_start = 0, msg_count = 0;
uwe7df6dda2011-09-03 18:37:52 +0000215 struct spi_ioc_transfer msg[2] = {
216 {
uwe230909b2011-09-06 18:17:02 +0000217 .tx_buf = (uint64_t)(ptrdiff_t)txbuf,
uwe7df6dda2011-09-03 18:37:52 +0000218 .len = writecnt,
219 },
220 {
uwe230909b2011-09-06 18:17:02 +0000221 .rx_buf = (uint64_t)(ptrdiff_t)rxbuf,
uwe7df6dda2011-09-03 18:37:52 +0000222 .len = readcnt,
223 },
224 };
225
226 if (fd == -1)
227 return -1;
228
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800229 /* Only pass necessary msg[] to ioctl() to avoid the empty message
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800230 * which drives an un-expected CS line and clocks. */
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800231 if (writecnt) {
232 msg_start = 0; /* tx: msg[0] */
233 msg_count++;
234 if (readcnt) {
235 msg_count++;
236 }
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800237 } else if (readcnt) {
238 msg_start = 1; /* rx: msg[1] */
239 msg_count++;
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800240 } else {
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800241 msg_cerr("%s: both writecnt and readcnt are 0.\n",
242 __func__);
243 return -1;
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800244 }
245
246 if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) {
uwe7df6dda2011-09-03 18:37:52 +0000247 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
248 return -1;
249 }
250 return 0;
251}
252
stefanctc5eb8a92011-11-23 09:13:48 +0000253static int linux_spi_read(struct flashchip *flash, uint8_t *buf,
254 unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000255{
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800256 return spi_read_chunked(flash, buf, start, len, SPI_DMA_SIZE);
uwe7df6dda2011-09-03 18:37:52 +0000257}
258
259static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000260 unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000261{
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800262 return spi_write_chunked(flash, buf, start, len,
263 SPI_DMA_SIZE - 5 /* WREN, Page Program */);
uwe7df6dda2011-09-03 18:37:52 +0000264}