blob: 3edbddf2b652c81e440273ae326c88f9cf5bab66 [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
41
uwe7df6dda2011-09-03 18:37:52 +000042static int fd = -1;
43
44static int linux_spi_shutdown(void *data);
45static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt,
46 const unsigned char *txbuf, unsigned char *rxbuf);
stefanctc5eb8a92011-11-23 09:13:48 +000047static int linux_spi_read(struct flashchip *flash, uint8_t *buf,
48 unsigned int start, unsigned int len);
uwe7df6dda2011-09-03 18:37:52 +000049static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +000050 unsigned int start, unsigned int len);
uwe7df6dda2011-09-03 18:37:52 +000051
52static const struct spi_programmer spi_programmer_linux = {
53 .type = SPI_CONTROLLER_LINUX,
54 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
55 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
56 .command = linux_spi_send_command,
57 .multicommand = default_spi_send_multicommand,
58 .read = linux_spi_read,
59 .write_256 = linux_spi_write_256,
60};
61
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080062
63/* Detect the SPI chip behind /dev/spidevX.Y.
64 * We scan every /dev/spidevX.0 and if it's /dev/spidevX.1 is a MTD device
65 * (by looking /sys/bus/spi/devices/spidevX.1/modalias), then we assume the
66 * spidevX.0 is the SPI flash.
67 */
68static char* linux_spi_probe(void)
69{
David Hendricks0c0a7e62012-06-12 23:41:05 +090070 char filename[] = "/sys/bus/spi/devices/spiX.0/modalias";
71 int X = strchr(filename, 'X') - filename; /* offset to the X char */
72 int i; /* for for-loop */
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080073
David Hendricks0c0a7e62012-06-12 23:41:05 +090074 for (i = '0'; i <= '9'; i++) {
75 int fd;
76 char buf[8]; // 8 is long enough for modalias
77
78 filename[X] = i;
79 if ((fd = open(filename, O_RDONLY)) < 0 ||
80 read(fd, buf, sizeof(buf)) < 0) {
81 msg_pdbg("read(%s) < 0, try next.\n", filename);
Louis Yung-Chieh Loeaa44fc2012-05-24 15:43:50 +080082 continue;
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080083 }
84
David Hendricks0c0a7e62012-06-12 23:41:05 +090085 if (!strncmp(buf, "spidev", 6)) {
86 static char name[] = "/dev/spidevX.0";
87 *strchr(name, 'X') = i;
88 msg_pdbg("Detected linux_spi:dev=%s\n", name);
89 return name;
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080090 }
David Hendricks0c0a7e62012-06-12 23:41:05 +090091
92 close(fd);
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080093 }
94
95 return NULL;
96}
97
98
uwe7df6dda2011-09-03 18:37:52 +000099int linux_spi_init(void)
100{
101 char *p, *endp, *dev;
uwe6b716f02011-09-07 20:48:34 +0000102 uint32_t speed = 0;
uwe7df6dda2011-09-03 18:37:52 +0000103
104 dev = extract_programmer_param("dev");
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +0800105 if (!dev) {
106 dev = linux_spi_probe();
107 }
uwe7df6dda2011-09-03 18:37:52 +0000108 if (!dev || !strlen(dev)) {
109 msg_perr("No SPI device given. Use flashrom -p "
110 "linux_spi:dev=/dev/spidevX.Y\n");
111 return 1;
112 }
113
114 p = extract_programmer_param("speed");
115 if (p && strlen(p)) {
uwe6b716f02011-09-07 20:48:34 +0000116 speed = (uint32_t)strtoul(p, &endp, 10) * 1024;
uwe7df6dda2011-09-03 18:37:52 +0000117 if (p == endp) {
118 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
119 return 1;
120 }
121 }
122
uwe6b716f02011-09-07 20:48:34 +0000123 msg_pdbg("Using device %s\n", dev);
uwe7df6dda2011-09-03 18:37:52 +0000124 if ((fd = open(dev, O_RDWR)) == -1) {
125 msg_perr("%s: failed to open %s: %s\n", __func__,
126 dev, strerror(errno));
127 return 1;
128 }
129
uwe6b716f02011-09-07 20:48:34 +0000130 if (speed > 0) {
131 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
132 msg_perr("%s: failed to set speed %dHz: %s\n",
133 __func__, speed, strerror(errno));
134 close(fd);
135 return 1;
136 }
137
138 msg_pdbg("Using %d kHz clock\n", speed);
uwe7df6dda2011-09-03 18:37:52 +0000139 }
140
141 if (register_shutdown(linux_spi_shutdown, NULL))
142 return 1;
143
144 register_spi_programmer(&spi_programmer_linux);
145
146 return 0;
147}
148
149static int linux_spi_shutdown(void *data)
150{
151 if (fd != -1) {
152 close(fd);
153 fd = -1;
154 }
155 return 0;
156}
157
158static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt,
159 const unsigned char *txbuf, unsigned char *rxbuf)
160{
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800161 int msg_start = 0, msg_count = 0;
uwe7df6dda2011-09-03 18:37:52 +0000162 struct spi_ioc_transfer msg[2] = {
163 {
uwe230909b2011-09-06 18:17:02 +0000164 .tx_buf = (uint64_t)(ptrdiff_t)txbuf,
uwe7df6dda2011-09-03 18:37:52 +0000165 .len = writecnt,
166 },
167 {
uwe230909b2011-09-06 18:17:02 +0000168 .rx_buf = (uint64_t)(ptrdiff_t)rxbuf,
uwe7df6dda2011-09-03 18:37:52 +0000169 .len = readcnt,
170 },
171 };
172
173 if (fd == -1)
174 return -1;
175
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800176 /* Only pass necessary msg[] to ioctl() to avoid the empty message
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800177 * which drives an un-expected CS line and clocks. */
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800178 if (writecnt) {
179 msg_start = 0; /* tx: msg[0] */
180 msg_count++;
181 if (readcnt) {
182 msg_count++;
183 }
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800184 } else if (readcnt) {
185 msg_start = 1; /* rx: msg[1] */
186 msg_count++;
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800187 } else {
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800188 msg_cerr("%s: both writecnt and readcnt are 0.\n",
189 __func__);
190 return -1;
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800191 }
192
193 if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) {
uwe7df6dda2011-09-03 18:37:52 +0000194 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
195 return -1;
196 }
197 return 0;
198}
199
stefanctc5eb8a92011-11-23 09:13:48 +0000200static int linux_spi_read(struct flashchip *flash, uint8_t *buf,
201 unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000202{
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800203 return spi_read_chunked(flash, buf, start, len, SPI_DMA_SIZE);
uwe7df6dda2011-09-03 18:37:52 +0000204}
205
206static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000207 unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000208{
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800209 return spi_write_chunked(flash, buf, start, len,
210 SPI_DMA_SIZE - 5 /* WREN, Page Program */);
uwe7df6dda2011-09-03 18:37:52 +0000211}