blob: d5be4ecbf0f68ce9a0612396ea627d15123efcfe [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{
70 char name0[] = "/sys/bus/spi/devices/spiX.0";
71 char name1[] = "/sys/bus/spi/devices/spiX.1/modalias";
72 int X = strchr(name0, 'X') - name0; /* point to the X char */
73 int x; /* for for-loop */
74 struct stat sb;
75
76 for (x = '0'; x <= '9'; x++) {
77 name0[X] = x;
78 if (stat(name0, &sb) < 0) {
Louis Yung-Chieh Loeaa44fc2012-05-24 15:43:50 +080079 msg_pdbg("stat(%s) < 0, try next.\n", name0);
80 continue;
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +080081 }
82
83 if ((sb.st_mode & S_IFMT) == S_IFDIR) {
84 int fd;
85 char buf[8]; // 8 is long enough for modalias
86
87 name1[X] = x;
88 if ((fd = open(name1, O_RDONLY)) < 0 ||
89 read(fd, buf, sizeof(buf)) < 0) {
90 msg_pdbg("read(%s) < 0, try next.\n", name0);
91 continue;
92 }
93
94 if (!strncmp(buf, "m25p80", 6)) {
95 static char name[] = "/dev/spidevX.0";
96 *strchr(name, 'X') = x;
97 msg_pdbg("Detected linux_spi:dev=%s\n", name);
98 return name;
99 }
100
101 close(fd);
102 }
103 }
104
105 return NULL;
106}
107
108
uwe7df6dda2011-09-03 18:37:52 +0000109int linux_spi_init(void)
110{
111 char *p, *endp, *dev;
uwe6b716f02011-09-07 20:48:34 +0000112 uint32_t speed = 0;
uwe7df6dda2011-09-03 18:37:52 +0000113
114 dev = extract_programmer_param("dev");
Louis Yung-Chieh Loa20b3932012-05-22 22:14:00 +0800115 if (!dev) {
116 dev = linux_spi_probe();
117 }
uwe7df6dda2011-09-03 18:37:52 +0000118 if (!dev || !strlen(dev)) {
119 msg_perr("No SPI device given. Use flashrom -p "
120 "linux_spi:dev=/dev/spidevX.Y\n");
121 return 1;
122 }
123
124 p = extract_programmer_param("speed");
125 if (p && strlen(p)) {
uwe6b716f02011-09-07 20:48:34 +0000126 speed = (uint32_t)strtoul(p, &endp, 10) * 1024;
uwe7df6dda2011-09-03 18:37:52 +0000127 if (p == endp) {
128 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
129 return 1;
130 }
131 }
132
uwe6b716f02011-09-07 20:48:34 +0000133 msg_pdbg("Using device %s\n", dev);
uwe7df6dda2011-09-03 18:37:52 +0000134 if ((fd = open(dev, O_RDWR)) == -1) {
135 msg_perr("%s: failed to open %s: %s\n", __func__,
136 dev, strerror(errno));
137 return 1;
138 }
139
uwe6b716f02011-09-07 20:48:34 +0000140 if (speed > 0) {
141 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
142 msg_perr("%s: failed to set speed %dHz: %s\n",
143 __func__, speed, strerror(errno));
144 close(fd);
145 return 1;
146 }
147
148 msg_pdbg("Using %d kHz clock\n", speed);
uwe7df6dda2011-09-03 18:37:52 +0000149 }
150
151 if (register_shutdown(linux_spi_shutdown, NULL))
152 return 1;
153
154 register_spi_programmer(&spi_programmer_linux);
155
156 return 0;
157}
158
159static int linux_spi_shutdown(void *data)
160{
161 if (fd != -1) {
162 close(fd);
163 fd = -1;
164 }
165 return 0;
166}
167
168static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt,
169 const unsigned char *txbuf, unsigned char *rxbuf)
170{
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800171 int msg_start = 0, msg_count = 0;
uwe7df6dda2011-09-03 18:37:52 +0000172 struct spi_ioc_transfer msg[2] = {
173 {
uwe230909b2011-09-06 18:17:02 +0000174 .tx_buf = (uint64_t)(ptrdiff_t)txbuf,
uwe7df6dda2011-09-03 18:37:52 +0000175 .len = writecnt,
176 },
177 {
uwe230909b2011-09-06 18:17:02 +0000178 .rx_buf = (uint64_t)(ptrdiff_t)rxbuf,
uwe7df6dda2011-09-03 18:37:52 +0000179 .len = readcnt,
180 },
181 };
182
183 if (fd == -1)
184 return -1;
185
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800186 /* Only pass necessary msg[] to ioctl() to avoid the empty message
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800187 * which drives an un-expected CS line and clocks. */
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800188 if (writecnt) {
189 msg_start = 0; /* tx: msg[0] */
190 msg_count++;
191 if (readcnt) {
192 msg_count++;
193 }
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800194 } else if (readcnt) {
195 msg_start = 1; /* rx: msg[1] */
196 msg_count++;
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800197 } else {
Louis Yung-Chieh Lob121eff2012-05-22 23:31:03 +0800198 msg_cerr("%s: both writecnt and readcnt are 0.\n",
199 __func__);
200 return -1;
Louis Yung-Chieh Lo327c9e92012-05-21 12:02:45 +0800201 }
202
203 if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) {
uwe7df6dda2011-09-03 18:37:52 +0000204 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
205 return -1;
206 }
207 return 0;
208}
209
stefanctc5eb8a92011-11-23 09:13:48 +0000210static int linux_spi_read(struct flashchip *flash, uint8_t *buf,
211 unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000212{
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800213 return spi_read_chunked(flash, buf, start, len, SPI_DMA_SIZE);
uwe7df6dda2011-09-03 18:37:52 +0000214}
215
216static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000217 unsigned int start, unsigned int len)
uwe7df6dda2011-09-03 18:37:52 +0000218{
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800219 return spi_write_chunked(flash, buf, start, len,
220 SPI_DMA_SIZE - 5 /* WREN, Page Program */);
uwe7df6dda2011-09-03 18:37:52 +0000221}