blob: 695032ca6407135f06a78fe1118f4424f30dfd2f [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
Stefan Tauner8868db32012-03-13 00:18:19 +000020#if CONFIG_LINUX_SPI == 1
21
Sven Schnelle5ce5f702011-09-03 18:37:52 +000022#include <stdio.h>
23#include <string.h>
24#include <stdlib.h>
25#include <sys/fcntl.h>
26#include <errno.h>
27#include <ctype.h>
28#include <unistd.h>
Stefan Tauner8868db32012-03-13 00:18:19 +000029#include <linux/types.h>
Sven Schnelle5ce5f702011-09-03 18:37:52 +000030#include <linux/spi/spidev.h>
31#include <sys/ioctl.h>
32#include "flash.h"
33#include "chipdrivers.h"
34#include "programmer.h"
35#include "spi.h"
36
37static int fd = -1;
38
39static int linux_spi_shutdown(void *data);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000040static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt,
41 unsigned int readcnt,
42 const unsigned char *txbuf,
43 unsigned char *rxbuf);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000044static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000045 unsigned int start, unsigned int len);
Mark Marshallf20b7be2014-05-09 21:16:21 +000046static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000047 unsigned int start, unsigned int len);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000048
49static const struct spi_programmer spi_programmer_linux = {
50 .type = SPI_CONTROLLER_LINUX,
51 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
52 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
53 .command = linux_spi_send_command,
54 .multicommand = default_spi_send_multicommand,
55 .read = linux_spi_read,
56 .write_256 = linux_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +000057 .write_aai = default_spi_write_aai,
Sven Schnelle5ce5f702011-09-03 18:37:52 +000058};
59
60int linux_spi_init(void)
61{
62 char *p, *endp, *dev;
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +000063 uint32_t speed_hz = 0;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +000064 /* FIXME: make the following configurable by CLI options. */
65 /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */
66 const uint8_t mode = SPI_MODE_0;
67 const uint8_t bits = 8;
Sven Schnelle5ce5f702011-09-03 18:37:52 +000068
Stefan Tauner0554ca52013-07-25 22:54:25 +000069 p = extract_programmer_param("spispeed");
Sven Schnelle5ce5f702011-09-03 18:37:52 +000070 if (p && strlen(p)) {
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +000071 speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000;
Sven Schnelle5ce5f702011-09-03 18:37:52 +000072 if (p == endp) {
73 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
Stefan Tauner0554ca52013-07-25 22:54:25 +000074 free(p);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000075 return 1;
76 }
77 }
Stefan Tauner0554ca52013-07-25 22:54:25 +000078 free(p);
79
80 dev = extract_programmer_param("dev");
81 if (!dev || !strlen(dev)) {
82 msg_perr("No SPI device given. Use flashrom -p "
83 "linux_spi:dev=/dev/spidevX.Y\n");
84 free(dev);
85 return 1;
86 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +000087
Sven Schnellecb24ddb2011-09-07 20:48:34 +000088 msg_pdbg("Using device %s\n", dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000089 if ((fd = open(dev, O_RDWR)) == -1) {
90 msg_perr("%s: failed to open %s: %s\n", __func__,
91 dev, strerror(errno));
Stefan Tauner0554ca52013-07-25 22:54:25 +000092 free(dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000093 return 1;
94 }
Stefan Tauner0554ca52013-07-25 22:54:25 +000095 free(dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000096
Stefan Tauner2c3e9d52012-03-03 18:09:33 +000097 if (register_shutdown(linux_spi_shutdown, NULL))
98 return 1;
99 /* We rely on the shutdown function for cleanup from here on. */
100
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +0000101 if (speed_hz > 0) {
102 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) {
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000103 msg_perr("%s: failed to set speed to %d Hz: %s\n",
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +0000104 __func__, speed_hz, strerror(errno));
Sven Schnellecb24ddb2011-09-07 20:48:34 +0000105 return 1;
106 }
107
Alexandru Gagniuc69dd09d2014-03-19 17:17:06 +0000108 msg_pdbg("Using %d kHz clock\n", speed_hz/1000);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000109 }
110
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000111 if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
112 msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n",
113 __func__, mode, strerror(errno));
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000114 return 1;
Stefan Tauner2c3e9d52012-03-03 18:09:33 +0000115 }
116
117 if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) {
118 msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n",
119 __func__, bits == 0 ? 8 : bits, strerror(errno));
120 return 1;
121 }
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000122
123 register_spi_programmer(&spi_programmer_linux);
124
125 return 0;
126}
127
128static int linux_spi_shutdown(void *data)
129{
130 if (fd != -1) {
131 close(fd);
132 fd = -1;
133 }
134 return 0;
135}
136
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000137static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt,
138 unsigned int readcnt,
139 const unsigned char *txbuf,
140 unsigned char *rxbuf)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000141{
Michael Karcher8371d722012-03-06 22:17:06 +0000142 int iocontrol_code;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000143 struct spi_ioc_transfer msg[2] = {
144 {
Uwe Hermann48446c82011-09-06 18:17:02 +0000145 .tx_buf = (uint64_t)(ptrdiff_t)txbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000146 .len = writecnt,
147 },
148 {
Uwe Hermann48446c82011-09-06 18:17:02 +0000149 .rx_buf = (uint64_t)(ptrdiff_t)rxbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000150 .len = readcnt,
151 },
152 };
153
154 if (fd == -1)
155 return -1;
Michael Karcher8371d722012-03-06 22:17:06 +0000156 /* The implementation currently does not support requests that
157 don't start with sending a command. */
158 if (writecnt == 0)
159 return SPI_INVALID_LENGTH;
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000160
Michael Karcher8371d722012-03-06 22:17:06 +0000161 /* Just submit the first (write) request in case there is nothing
162 to read. Otherwise submit both requests. */
163 if (readcnt == 0)
164 iocontrol_code = SPI_IOC_MESSAGE(1);
165 else
166 iocontrol_code = SPI_IOC_MESSAGE(2);
167
168 if (ioctl(fd, iocontrol_code, msg) == -1) {
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000169 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
170 return -1;
171 }
172 return 0;
173}
174
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000175static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000176 unsigned int start, unsigned int len)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000177{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000178 return spi_read_chunked(flash, buf, start, len,
179 (unsigned int)getpagesize());
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000180}
181
Mark Marshallf20b7be2014-05-09 21:16:21 +0000182static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000183{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000184 return spi_write_chunked(flash, buf, start, len,
185 ((unsigned int)getpagesize()) - 4);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000186}
Stefan Tauner8868db32012-03-13 00:18:19 +0000187
188#endif // CONFIG_LINUX_SPI == 1