blob: 372082300bfe2bf4f473ca0941d6d0edf943d147 [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);
37static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt,
38 const unsigned char *txbuf, unsigned char *rxbuf);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000039static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000040 unsigned int start, unsigned int len);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000041static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +000042 unsigned int start, unsigned int len);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000043
44static const struct spi_programmer spi_programmer_linux = {
45 .type = SPI_CONTROLLER_LINUX,
46 .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */
47 .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */
48 .command = linux_spi_send_command,
49 .multicommand = default_spi_send_multicommand,
50 .read = linux_spi_read,
51 .write_256 = linux_spi_write_256,
52};
53
54int linux_spi_init(void)
55{
56 char *p, *endp, *dev;
Sven Schnellecb24ddb2011-09-07 20:48:34 +000057 uint32_t speed = 0;
Sven Schnelle5ce5f702011-09-03 18:37:52 +000058
59 dev = extract_programmer_param("dev");
60 if (!dev || !strlen(dev)) {
61 msg_perr("No SPI device given. Use flashrom -p "
62 "linux_spi:dev=/dev/spidevX.Y\n");
63 return 1;
64 }
65
66 p = extract_programmer_param("speed");
67 if (p && strlen(p)) {
Sven Schnellecb24ddb2011-09-07 20:48:34 +000068 speed = (uint32_t)strtoul(p, &endp, 10) * 1024;
Sven Schnelle5ce5f702011-09-03 18:37:52 +000069 if (p == endp) {
70 msg_perr("%s: invalid clock: %s kHz\n", __func__, p);
71 return 1;
72 }
73 }
74
Sven Schnellecb24ddb2011-09-07 20:48:34 +000075 msg_pdbg("Using device %s\n", dev);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000076 if ((fd = open(dev, O_RDWR)) == -1) {
77 msg_perr("%s: failed to open %s: %s\n", __func__,
78 dev, strerror(errno));
79 return 1;
80 }
81
Sven Schnellecb24ddb2011-09-07 20:48:34 +000082 if (speed > 0) {
83 if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
84 msg_perr("%s: failed to set speed %dHz: %s\n",
85 __func__, speed, strerror(errno));
86 close(fd);
87 return 1;
88 }
89
90 msg_pdbg("Using %d kHz clock\n", speed);
Sven Schnelle5ce5f702011-09-03 18:37:52 +000091 }
92
93 if (register_shutdown(linux_spi_shutdown, NULL))
94 return 1;
95
96 register_spi_programmer(&spi_programmer_linux);
97
98 return 0;
99}
100
101static int linux_spi_shutdown(void *data)
102{
103 if (fd != -1) {
104 close(fd);
105 fd = -1;
106 }
107 return 0;
108}
109
110static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt,
111 const unsigned char *txbuf, unsigned char *rxbuf)
112{
113 struct spi_ioc_transfer msg[2] = {
114 {
Uwe Hermann48446c82011-09-06 18:17:02 +0000115 .tx_buf = (uint64_t)(ptrdiff_t)txbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000116 .len = writecnt,
117 },
118 {
Uwe Hermann48446c82011-09-06 18:17:02 +0000119 .rx_buf = (uint64_t)(ptrdiff_t)rxbuf,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000120 .len = readcnt,
121 },
122 };
123
124 if (fd == -1)
125 return -1;
126
127 if (ioctl(fd, SPI_IOC_MESSAGE(2), msg) == -1) {
128 msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno));
129 return -1;
130 }
131 return 0;
132}
133
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000134static int linux_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000135 unsigned int start, unsigned int len)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000136{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000137 return spi_read_chunked(flash, buf, start, len, (unsigned)getpagesize());
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000138}
139
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000140static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000141 unsigned int start, unsigned int len)
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000142{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000143 return spi_write_chunked(flash, buf, start, len, ((unsigned)getpagesize()) - 4);
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000144}