blob: 4b6bf48ef8667529ca9e663901b5cdba315b0053 [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++) {
79 int fd;
80 char buf[8]; // 8 is long enough for modalias
81
82 filename[X] = i;
83 if ((fd = open(filename, O_RDONLY)) < 0 ||
84 read(fd, buf, sizeof(buf)) < 0) {
85 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 }
88
David Hendricks0c0a7e62012-06-12 23:41:05 +090089 if (!strncmp(buf, "spidev", 6)) {
90 static char name[] = "/dev/spidevX.0";
91 *strchr(name, 'X') = i;
92 msg_pdbg("Detected linux_spi:dev=%s\n", name);
93 return name;
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080094 }
David Hendricks0c0a7e62012-06-12 23:41:05 +090095
96 close(fd);
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080097 }
98
99 return NULL;
100}
101
102
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800103/*
104 * This is used when /dev/spidevX.Y is not created yet, for example, when
105 * udev is not started.
106 */
107static int manual_mknod(const char *dev)
108{
109 char cmd[256];
110
111 msg_pdbg("Creating SPI device node %s...\n", dev);
112 strcpy(cmd, "modprobe spidev");
113 msg_pdbg("CMD: [%s]\n", cmd);
114 system(cmd);
115 snprintf(cmd, sizeof(cmd), "mknod %s c %d 0", dev, SPIDEV_MAJOR);
116 msg_pdbg("CMD: [%s]\n", cmd);
117 system(cmd);
118
119 if ((fd = open(dev, O_RDWR)) == -1) {
120 msg_perr("%s: failed to open %s: %s\n", __func__,
121 dev, strerror(errno));
122 }
123
124 return fd;
125}
126
127
uwe7df6dda2011-09-03 18:37:52 +0000128int linux_spi_init(void)
129{
130 char *p, *endp, *dev;
uwe6b716f02011-09-07 20:48:34 +0000131 uint32_t speed = 0;
uwe7df6dda2011-09-03 18:37:52 +0000132
133 dev = extract_programmer_param("dev");
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +0800134 if (!dev) {
135 dev = linux_spi_probe();
136 }
uwe7df6dda2011-09-03 18:37:52 +0000137 if (!dev || !strlen(dev)) {
138 msg_perr("No SPI device given. Use flashrom -p "
139 "linux_spi:dev=/dev/spidevX.Y\n");
140 return 1;
141 }
142
143 p = extract_programmer_param("speed");
144 if (p && strlen(p)) {
uwe6b716f02011-09-07 20:48:34 +0000145 speed = (uint32_t)strtoul(p, &endp, 10) * 1024;
uwe7df6dda2011-09-03 18:37:52 +0000146 if (p == endp) {
147 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
148 return 1;
149 }
150 }
151
uwe6b716f02011-09-07 20:48:34 +0000152 msg_pdbg("Using device %s\n", dev);
uwe7df6dda2011-09-03 18:37:52 +0000153 if ((fd = open(dev, O_RDWR)) == -1) {
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800154 msg_pdbg("%s: failed to open %s: %s\n", __func__,
uwe7df6dda2011-09-03 18:37:52 +0000155 dev, strerror(errno));
Louis Yung-Chieh Lo282c2412012-07-27 18:24:37 +0800156
157 if (manual_mknod(dev) == -1) { // global fd is effected.
158 return 1;
159 }
uwe7df6dda2011-09-03 18:37:52 +0000160 }
161
uwe6b716f02011-09-07 20:48:34 +0000162 if (speed > 0) {
163 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
164 msg_perr("%s: failed to set speed %dHz: %s\n",
165 __func__, speed, strerror(errno));
166 close(fd);
167 return 1;
168 }
169
170 msg_pdbg("Using %d kHz clock\n", speed);
uwe7df6dda2011-09-03 18:37:52 +0000171 }
172
173 if (register_shutdown(linux_spi_shutdown, NULL))
174 return 1;
175
176 register_spi_programmer(&spi_programmer_linux);
177
178 return 0;
179}
180
181static int linux_spi_shutdown(void *data)
182{
183 if (fd != -1) {
184 close(fd);
185 fd = -1;
186 }
187 return 0;
188}
189
190static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt,
191 const unsigned char *txbuf, unsigned char *rxbuf)
192{
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800193 int msg_start = 0, msg_count = 0;
uwe7df6dda2011-09-03 18:37:52 +0000194 struct spi_ioc_transfer msg[2] = {
195 {
uwe230909b2011-09-06 18:17:02 +0000196 .tx_buf = (uint64_t)(ptrdiff_t)txbuf,
uwe7df6dda2011-09-03 18:37:52 +0000197 .len = writecnt,
198 },
199 {
uwe230909b2011-09-06 18:17:02 +0000200 .rx_buf = (uint64_t)(ptrdiff_t)rxbuf,
uwe7df6dda2011-09-03 18:37:52 +0000201 .len = readcnt,
202 },
203 };
204
205 if (fd == -1)
206 return -1;
207
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800208 /* Only pass necessary msg[] to ioctl() to avoid the empty message
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800209 * which drives an un-expected CS line and clocks. */
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800210 if (writecnt) {
211 msg_start = 0; /* tx: msg[0] */
212 msg_count++;
213 if (readcnt) {
214 msg_count++;
215 }
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800216 } else if (readcnt) {
217 msg_start = 1; /* rx: msg[1] */
218 msg_count++;
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800219 } else {
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800220 msg_cerr("%s: both writecnt and readcnt are 0.\n",
221 __func__);
222 return -1;
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800223 }
224
225 if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) {
uwe7df6dda2011-09-03 18:37:52 +0000226 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
227 return -1;
228 }
229 return 0;
230}
231
stefanctc5eb8a92011-11-23 09:13:48 +0000232static int linux_spi_read(struct flashchip *flash, uint8_t *buf,
233 unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000234{
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800235 return spi_read_chunked(flash, buf, start, len, SPI_DMA_SIZE);
uwe7df6dda2011-09-03 18:37:52 +0000236}
237
238static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000239 unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000240{
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800241 return spi_write_chunked(flash, buf, start, len,
242 SPI_DMA_SIZE - 5 /* WREN, Page Program */);
uwe7df6dda2011-09-03 18:37:52 +0000243}