Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 1 | /* |
| 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 Tauner | 8868db3 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 20 | #if CONFIG_LINUX_SPI == 1 |
| 21 | |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include <stdlib.h> |
Gwenhael Goavec-Merou | 8cd0c73 | 2015-11-14 02:55:12 +0000 | [diff] [blame^] | 25 | #include <fcntl.h> |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 26 | #include <errno.h> |
| 27 | #include <ctype.h> |
| 28 | #include <unistd.h> |
Gwenhael Goavec-Merou | 8cd0c73 | 2015-11-14 02:55:12 +0000 | [diff] [blame^] | 29 | #include <sys/ioctl.h> |
Stefan Tauner | 8868db3 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 30 | #include <linux/types.h> |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 31 | #include <linux/spi/spidev.h> |
Gwenhael Goavec-Merou | 8cd0c73 | 2015-11-14 02:55:12 +0000 | [diff] [blame^] | 32 | #include <linux/ioctl.h> |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 33 | #include "flash.h" |
| 34 | #include "chipdrivers.h" |
| 35 | #include "programmer.h" |
| 36 | #include "spi.h" |
| 37 | |
| 38 | static int fd = -1; |
| 39 | |
| 40 | static int linux_spi_shutdown(void *data); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 41 | static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt, |
| 42 | unsigned int readcnt, |
| 43 | const unsigned char *txbuf, |
| 44 | unsigned char *rxbuf); |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 45 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 46 | unsigned int start, unsigned int len); |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 47 | static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 48 | unsigned int start, unsigned int len); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 49 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 50 | static const struct spi_master spi_master_linux = { |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 51 | .type = SPI_CONTROLLER_LINUX, |
| 52 | .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 53 | .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 54 | .command = linux_spi_send_command, |
| 55 | .multicommand = default_spi_send_multicommand, |
| 56 | .read = linux_spi_read, |
| 57 | .write_256 = linux_spi_write_256, |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 58 | .write_aai = default_spi_write_aai, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | int linux_spi_init(void) |
| 62 | { |
| 63 | char *p, *endp, *dev; |
Alexandru Gagniuc | 69dd09d | 2014-03-19 17:17:06 +0000 | [diff] [blame] | 64 | uint32_t speed_hz = 0; |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 65 | /* FIXME: make the following configurable by CLI options. */ |
| 66 | /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */ |
| 67 | const uint8_t mode = SPI_MODE_0; |
| 68 | const uint8_t bits = 8; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 69 | |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 70 | p = extract_programmer_param("spispeed"); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 71 | if (p && strlen(p)) { |
Alexandru Gagniuc | 69dd09d | 2014-03-19 17:17:06 +0000 | [diff] [blame] | 72 | speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 73 | if (p == endp) { |
| 74 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 75 | free(p); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 76 | return 1; |
| 77 | } |
| 78 | } |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 79 | free(p); |
| 80 | |
| 81 | dev = extract_programmer_param("dev"); |
| 82 | if (!dev || !strlen(dev)) { |
| 83 | msg_perr("No SPI device given. Use flashrom -p " |
| 84 | "linux_spi:dev=/dev/spidevX.Y\n"); |
| 85 | free(dev); |
| 86 | return 1; |
| 87 | } |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 88 | |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 89 | msg_pdbg("Using device %s\n", dev); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 90 | if ((fd = open(dev, O_RDWR)) == -1) { |
| 91 | msg_perr("%s: failed to open %s: %s\n", __func__, |
| 92 | dev, strerror(errno)); |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 93 | free(dev); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 94 | return 1; |
| 95 | } |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 96 | free(dev); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 97 | |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 98 | if (register_shutdown(linux_spi_shutdown, NULL)) |
| 99 | return 1; |
| 100 | /* We rely on the shutdown function for cleanup from here on. */ |
| 101 | |
Alexandru Gagniuc | 69dd09d | 2014-03-19 17:17:06 +0000 | [diff] [blame] | 102 | if (speed_hz > 0) { |
| 103 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) { |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 104 | msg_perr("%s: failed to set speed to %d Hz: %s\n", |
Alexandru Gagniuc | 69dd09d | 2014-03-19 17:17:06 +0000 | [diff] [blame] | 105 | __func__, speed_hz, strerror(errno)); |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 106 | return 1; |
| 107 | } |
| 108 | |
Alexandru Gagniuc | 69dd09d | 2014-03-19 17:17:06 +0000 | [diff] [blame] | 109 | msg_pdbg("Using %d kHz clock\n", speed_hz/1000); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 112 | if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) { |
| 113 | msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n", |
| 114 | __func__, mode, strerror(errno)); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 115 | return 1; |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) { |
| 119 | msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n", |
| 120 | __func__, bits == 0 ? 8 : bits, strerror(errno)); |
| 121 | return 1; |
| 122 | } |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 123 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 124 | register_spi_master(&spi_master_linux); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int linux_spi_shutdown(void *data) |
| 130 | { |
| 131 | if (fd != -1) { |
| 132 | close(fd); |
| 133 | fd = -1; |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 138 | static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt, |
| 139 | unsigned int readcnt, |
| 140 | const unsigned char *txbuf, |
| 141 | unsigned char *rxbuf) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 142 | { |
Michael Karcher | 8371d72 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 143 | int iocontrol_code; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 144 | struct spi_ioc_transfer msg[2] = { |
| 145 | { |
David Riley | 18f5097 | 2014-08-05 22:16:01 +0000 | [diff] [blame] | 146 | .tx_buf = (uint64_t)(uintptr_t)txbuf, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 147 | .len = writecnt, |
| 148 | }, |
| 149 | { |
David Riley | 18f5097 | 2014-08-05 22:16:01 +0000 | [diff] [blame] | 150 | .rx_buf = (uint64_t)(uintptr_t)rxbuf, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 151 | .len = readcnt, |
| 152 | }, |
| 153 | }; |
| 154 | |
| 155 | if (fd == -1) |
| 156 | return -1; |
Michael Karcher | 8371d72 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 157 | /* The implementation currently does not support requests that |
| 158 | don't start with sending a command. */ |
| 159 | if (writecnt == 0) |
| 160 | return SPI_INVALID_LENGTH; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 161 | |
Michael Karcher | 8371d72 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 162 | /* Just submit the first (write) request in case there is nothing |
| 163 | to read. Otherwise submit both requests. */ |
| 164 | if (readcnt == 0) |
| 165 | iocontrol_code = SPI_IOC_MESSAGE(1); |
| 166 | else |
| 167 | iocontrol_code = SPI_IOC_MESSAGE(2); |
| 168 | |
| 169 | if (ioctl(fd, iocontrol_code, msg) == -1) { |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 170 | msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno)); |
| 171 | return -1; |
| 172 | } |
| 173 | return 0; |
| 174 | } |
| 175 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 176 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 177 | unsigned int start, unsigned int len) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 178 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 179 | return spi_read_chunked(flash, buf, start, len, |
| 180 | (unsigned int)getpagesize()); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 183 | static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 184 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 185 | return spi_write_chunked(flash, buf, start, len, |
| 186 | ((unsigned int)getpagesize()) - 4); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 187 | } |
Stefan Tauner | 8868db3 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 188 | |
| 189 | #endif // CONFIG_LINUX_SPI == 1 |