blob: 68b669dfd4e5e0a69b138d1e117f3aa293f0f39f [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>
uwe7df6dda2011-09-03 18:37:52 +000031#include "flash.h"
32#include "chipdrivers.h"
33#include "programmer.h"
34#include "spi.h"
35
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +080036
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080037
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +080038/* TODO: this information should come from the SPI master driver. */
39#define SPI_DMA_SIZE 64
40
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +080041#ifndef SPIDEV_MAJOR
42#define SPIDEV_MAJOR 153 /* refer to kernel/files/drivers/spi/spidev.c */
43#endif
44
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +080045
uwe7df6dda2011-09-03 18:37:52 +000046static int fd = -1;
47
48static int linux_spi_shutdown(void *data);
49static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt,
50 const unsigned char *txbuf, unsigned char *rxbuf);
stefanctc5eb8a92011-11-23 09:13:48 +000051static int linux_spi_read(struct flashchip *flash, uint8_t *buf,
52 unsigned int start, unsigned int len);
uwe7df6dda2011-09-03 18:37:52 +000053static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000054 unsigned int start, unsigned int len);
uwe7df6dda2011-09-03 18:37:52 +000055
56static const struct spi_programmer spi_programmer_linux = {
57 .type = SPI_CONTROLLER_LINUX,
58 .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,
64};
65
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080066
67/* Detect the SPI chip behind /dev/spidevX.Y.
68 * We scan every /dev/spidevX.0 and if it's /dev/spidevX.1 is a MTD device
69 * (by looking /sys/bus/spi/devices/spidevX.1/modalias), then we assume the
70 * spidevX.0 is the SPI flash.
71 */
72static char* linux_spi_probe(void)
73{
David Hendricks0c0a7e62012-06-12 23:41:05 +090074 char filename[] = "/sys/bus/spi/devices/spiX.0/modalias";
75 int X = strchr(filename, 'X') - filename; /* offset to the X char */
76 int i; /* for for-loop */
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080077
David Hendricks0c0a7e62012-06-12 23:41:05 +090078 for (i = '0'; i <= '9'; i++) {
David Hendricks8aaf6862013-06-05 14:30:50 -070079 int fd, bytes_read;
80 char buf[32];
David Hendricks0c0a7e62012-06-12 23:41:05 +090081
82 filename[X] = i;
83 if ((fd = open(filename, O_RDONLY)) < 0 ||
David Hendricks8aaf6862013-06-05 14:30:50 -070084 (bytes_read = read(fd, buf, sizeof(buf) - 1) < 0)) {
David Hendricks0c0a7e62012-06-12 23:41:05 +090085 msg_pdbg("read(%s) < 0, try next.\n", filename);
Louis Yung-Chieh Loeaa44fc2012-05-24 15:43:50 +080086 continue;
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080087 }
David Hendricks8aaf6862013-06-05 14:30:50 -070088 buf[bytes_read] = '\0';
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080089
David Hendricks8aaf6862013-06-05 14:30:50 -070090 if (strstr(buf, "spidev")) {
David Hendricks0c0a7e62012-06-12 23:41:05 +090091 static char name[] = "/dev/spidevX.0";
92 *strchr(name, 'X') = i;
93 msg_pdbg("Detected linux_spi:dev=%s\n", name);
94 return name;
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080095 }
David Hendricks0c0a7e62012-06-12 23:41:05 +090096
97 close(fd);
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080098 }
99
100 return NULL;
101}
102
103
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800104/*
105 * This is used when /dev/spidevX.Y is not created yet, for example, when
106 * udev is not started.
107 */
108static int manual_mknod(const char *dev)
109{
110 char cmd[256];
111
112 msg_pdbg("Creating SPI device node %s...\n", dev);
113 strcpy(cmd, "modprobe spidev");
114 msg_pdbg("CMD: [%s]\n", cmd);
Simon Glass7ef84ff2013-02-10 17:13:41 -0800115 if (system(cmd)) {
116 msg_perr("%s: failed to run '%s': %s\n", __func__,
117 cmd, strerror(errno));
118 return -1;
119 }
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800120 snprintf(cmd, sizeof(cmd), "mknod %s c %d 0", dev, SPIDEV_MAJOR);
121 msg_pdbg("CMD: [%s]\n", cmd);
Simon Glass7ef84ff2013-02-10 17:13:41 -0800122 if (system(cmd)) {
123 msg_perr("%s: failed to run '%s': %s\n", __func__,
124 cmd, strerror(errno));
125 return -1;
126 }
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800127
128 if ((fd = open(dev, O_RDWR)) == -1) {
129 msg_perr("%s: failed to open %s: %s\n", __func__,
130 dev, strerror(errno));
131 }
132
133 return fd;
134}
135
136
uwe7df6dda2011-09-03 18:37:52 +0000137int linux_spi_init(void)
138{
139 char *p, *endp, *dev;
uwe6b716f02011-09-07 20:48:34 +0000140 uint32_t speed = 0;
uwe7df6dda2011-09-03 18:37:52 +0000141
142 dev = extract_programmer_param("dev");
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +0800143 if (!dev) {
144 dev = linux_spi_probe();
145 }
uwe7df6dda2011-09-03 18:37:52 +0000146 if (!dev || !strlen(dev)) {
147 msg_perr("No SPI device given. Use flashrom -p "
148 "linux_spi:dev=/dev/spidevX.Y\n");
149 return 1;
150 }
151
152 p = extract_programmer_param("speed");
153 if (p && strlen(p)) {
uwe6b716f02011-09-07 20:48:34 +0000154 speed = (uint32_t)strtoul(p, &endp, 10) * 1024;
uwe7df6dda2011-09-03 18:37:52 +0000155 if (p == endp) {
156 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
157 return 1;
158 }
159 }
160
uwe6b716f02011-09-07 20:48:34 +0000161 msg_pdbg("Using device %s\n", dev);
uwe7df6dda2011-09-03 18:37:52 +0000162 if ((fd = open(dev, O_RDWR)) == -1) {
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800163 msg_pdbg("%s: failed to open %s: %s\n", __func__,
uwe7df6dda2011-09-03 18:37:52 +0000164 dev, strerror(errno));
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800165
166 if (manual_mknod(dev) == -1) { // global fd is effected.
167 return 1;
168 }
uwe7df6dda2011-09-03 18:37:52 +0000169 }
170
uwe6b716f02011-09-07 20:48:34 +0000171 if (speed > 0) {
172 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
173 msg_perr("%s: failed to set speed %dHz: %s\n",
174 __func__, speed, strerror(errno));
175 close(fd);
176 return 1;
177 }
178
179 msg_pdbg("Using %d kHz clock\n", speed);
uwe7df6dda2011-09-03 18:37:52 +0000180 }
181
182 if (register_shutdown(linux_spi_shutdown, NULL))
183 return 1;
184
185 register_spi_programmer(&spi_programmer_linux);
186
187 return 0;
188}
189
190static int linux_spi_shutdown(void *data)
191{
192 if (fd != -1) {
193 close(fd);
194 fd = -1;
195 }
196 return 0;
197}
198
199static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt,
200 const unsigned char *txbuf, unsigned char *rxbuf)
201{
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800202 int msg_start = 0, msg_count = 0;
uwe7df6dda2011-09-03 18:37:52 +0000203 struct spi_ioc_transfer msg[2] = {
204 {
uwe230909b2011-09-06 18:17:02 +0000205 .tx_buf = (uint64_t)(ptrdiff_t)txbuf,
uwe7df6dda2011-09-03 18:37:52 +0000206 .len = writecnt,
207 },
208 {
uwe230909b2011-09-06 18:17:02 +0000209 .rx_buf = (uint64_t)(ptrdiff_t)rxbuf,
uwe7df6dda2011-09-03 18:37:52 +0000210 .len = readcnt,
211 },
212 };
213
214 if (fd == -1)
215 return -1;
216
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800217 /* Only pass necessary msg[] to ioctl() to avoid the empty message
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800218 * which drives an un-expected CS line and clocks. */
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800219 if (writecnt) {
220 msg_start = 0; /* tx: msg[0] */
221 msg_count++;
222 if (readcnt) {
223 msg_count++;
224 }
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800225 } else if (readcnt) {
226 msg_start = 1; /* rx: msg[1] */
227 msg_count++;
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800228 } else {
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800229 msg_cerr("%s: both writecnt and readcnt are 0.\n",
230 __func__);
231 return -1;
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800232 }
233
234 if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) {
uwe7df6dda2011-09-03 18:37:52 +0000235 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
236 return -1;
237 }
238 return 0;
239}
240
stefanctc5eb8a92011-11-23 09:13:48 +0000241static int linux_spi_read(struct flashchip *flash, uint8_t *buf,
242 unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000243{
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800244 return spi_read_chunked(flash, buf, start, len, SPI_DMA_SIZE);
uwe7df6dda2011-09-03 18:37:52 +0000245}
246
247static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000248 unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000249{
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800250 return spi_write_chunked(flash, buf, start, len,
251 SPI_DMA_SIZE - 5 /* WREN, Page Program */);
uwe7df6dda2011-09-03 18:37:52 +0000252}