blob: 17d003e8173796dac041167c90d6536ba7f84725 [file] [log] [blame]
Sven Schnelle5ce5f702011-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>
29#include "flash.h"
30#include "chipdrivers.h"
31#include "programmer.h"
32#include "spi.h"
33
34static int fd = -1;
35
36static int linux_spi_shutdown(void *data);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000037static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt,
38 unsigned int readcnt,
39 const unsigned char *txbuf,
40 unsigned char *rxbuf);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000041static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000042 unsigned int start, unsigned int len);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000043static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000044 unsigned int start, unsigned int len);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000045
46static const struct spi_programmer spi_programmer_linux = {
47 .type = SPI_CONTROLLER_LINUX,
48 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
49 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
50 .command = linux_spi_send_command,
51 .multicommand = default_spi_send_multicommand,
52 .read = linux_spi_read,
53 .write_256 = linux_spi_write_256,
54};
55
56int linux_spi_init(void)
57{
58 char *p, *endp, *dev;
Sven Schnellecb24ddb2011-09-07 20:48:34 +000059 uint32_t speed = 0;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +000060 /* FIXME: make the following configurable by CLI options. */
61 /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */
62 const uint8_t mode = SPI_MODE_0;
63 const uint8_t bits = 8;
Sven Schnelle5ce5f702011-09-03 18:37:52 +000064
65 dev = extract_programmer_param("dev");
66 if (!dev || !strlen(dev)) {
67 msg_perr("No SPI device given. Use flashrom -p "
68 "linux_spi:dev=/dev/spidevX.Y\n");
69 return 1;
70 }
71
72 p = extract_programmer_param("speed");
73 if (p && strlen(p)) {
Sven Schnellecb24ddb2011-09-07 20:48:34 +000074 speed = (uint32_t)strtoul(p, &endp, 10) * 1024;
Sven Schnelle5ce5f702011-09-03 18:37:52 +000075 if (p == endp) {
76 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
77 return 1;
78 }
79 }
80
Sven Schnellecb24ddb2011-09-07 20:48:34 +000081 msg_pdbg("Using device %s\n", dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000082 if ((fd = open(dev, O_RDWR)) == -1) {
83 msg_perr("%s: failed to open %s: %s\n", __func__,
84 dev, strerror(errno));
85 return 1;
86 }
87
Stefan Tauner2c3e9d52012-03-03 18:09:33 +000088 if (register_shutdown(linux_spi_shutdown, NULL))
89 return 1;
90 /* We rely on the shutdown function for cleanup from here on. */
91
Sven Schnellecb24ddb2011-09-07 20:48:34 +000092 if (speed > 0) {
93 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
Stefan Tauner2c3e9d52012-03-03 18:09:33 +000094 msg_perr("%s: failed to set speed to %d Hz: %s\n",
Sven Schnellecb24ddb2011-09-07 20:48:34 +000095 __func__, speed, strerror(errno));
Sven Schnellecb24ddb2011-09-07 20:48:34 +000096 return 1;
97 }
98
99 msg_pdbg("Using %d kHz clock\n", speed);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000100 }
101
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000102 if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
103 msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n",
104 __func__, mode, strerror(errno));
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000105 return 1;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000106 }
107
108 if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
109 msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n",
110 __func__, bits == 0 ? 8 : bits, strerror(errno));
111 return 1;
112 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000113
114 register_spi_programmer(&spi_programmer_linux);
115
116 return 0;
117}
118
119static int linux_spi_shutdown(void *data)
120{
121 if (fd != -1) {
122 close(fd);
123 fd = -1;
124 }
125 return 0;
126}
127
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000128static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt,
129 unsigned int readcnt,
130 const unsigned char *txbuf,
131 unsigned char *rxbuf)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000132{
Michael Karcher8371d722012-03-06 22:17:06 +0000133 int iocontrol_code;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000134 struct spi_ioc_transfer msg[2] = {
135 {
Uwe Hermann48446c82011-09-06 18:17:02 +0000136 .tx_buf = (uint64_t)(ptrdiff_t)txbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000137 .len = writecnt,
138 },
139 {
Uwe Hermann48446c82011-09-06 18:17:02 +0000140 .rx_buf = (uint64_t)(ptrdiff_t)rxbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000141 .len = readcnt,
142 },
143 };
144
145 if (fd == -1)
146 return -1;
Michael Karcher8371d722012-03-06 22:17:06 +0000147 /* The implementation currently does not support requests that
148 don't start with sending a command. */
149 if (writecnt == 0)
150 return SPI_INVALID_LENGTH;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000151
Michael Karcher8371d722012-03-06 22:17:06 +0000152 /* Just submit the first (write) request in case there is nothing
153 to read. Otherwise submit both requests. */
154 if (readcnt == 0)
155 iocontrol_code = SPI_IOC_MESSAGE(1);
156 else
157 iocontrol_code = SPI_IOC_MESSAGE(2);
158
159 if (ioctl(fd, iocontrol_code, msg) == -1) {
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000160 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
161 return -1;
162 }
163 return 0;
164}
165
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000166static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000167 unsigned int start, unsigned int len)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000168{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000169 return spi_read_chunked(flash, buf, start, len,
170 (unsigned int)getpagesize());
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000171}
172
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000173static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000174 unsigned int start, unsigned int len)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000175{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000176 return spi_write_chunked(flash, buf, start, len,
177 ((unsigned int)getpagesize()) - 4);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000178}