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> |
| 25 | #include <sys/fcntl.h> |
| 26 | #include <errno.h> |
| 27 | #include <ctype.h> |
| 28 | #include <unistd.h> |
Stefan Tauner | 8868db3 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 29 | #include <linux/types.h> |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 30 | #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 | |
| 37 | static int fd = -1; |
| 38 | |
| 39 | static int linux_spi_shutdown(void *data); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 40 | static 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 Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 44 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 45 | unsigned int start, unsigned int len); |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 46 | static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 47 | unsigned int start, unsigned int len); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 48 | |
| 49 | static 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 Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 57 | .write_aai = default_spi_write_aai, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | int linux_spi_init(void) |
| 61 | { |
| 62 | char *p, *endp, *dev; |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 63 | uint32_t speed = 0; |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 64 | /* 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 Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 68 | |
| 69 | dev = extract_programmer_param("dev"); |
| 70 | if (!dev || !strlen(dev)) { |
| 71 | msg_perr("No SPI device given. Use flashrom -p " |
| 72 | "linux_spi:dev=/dev/spidevX.Y\n"); |
| 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | p = extract_programmer_param("speed"); |
| 77 | if (p && strlen(p)) { |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 78 | speed = (uint32_t)strtoul(p, &endp, 10) * 1024; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 79 | if (p == endp) { |
| 80 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
| 81 | return 1; |
| 82 | } |
| 83 | } |
| 84 | |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 85 | msg_pdbg("Using device %s\n", dev); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 86 | if ((fd = open(dev, O_RDWR)) == -1) { |
| 87 | msg_perr("%s: failed to open %s: %s\n", __func__, |
| 88 | dev, strerror(errno)); |
| 89 | return 1; |
| 90 | } |
| 91 | |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 92 | if (register_shutdown(linux_spi_shutdown, NULL)) |
| 93 | return 1; |
| 94 | /* We rely on the shutdown function for cleanup from here on. */ |
| 95 | |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 96 | if (speed > 0) { |
| 97 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) { |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 98 | msg_perr("%s: failed to set speed to %d Hz: %s\n", |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 99 | __func__, speed, strerror(errno)); |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | msg_pdbg("Using %d kHz clock\n", speed); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 106 | if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) { |
| 107 | msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n", |
| 108 | __func__, mode, strerror(errno)); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 109 | return 1; |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) { |
| 113 | msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n", |
| 114 | __func__, bits == 0 ? 8 : bits, strerror(errno)); |
| 115 | return 1; |
| 116 | } |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 117 | |
| 118 | register_spi_programmer(&spi_programmer_linux); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int linux_spi_shutdown(void *data) |
| 124 | { |
| 125 | if (fd != -1) { |
| 126 | close(fd); |
| 127 | fd = -1; |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 132 | static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt, |
| 133 | unsigned int readcnt, |
| 134 | const unsigned char *txbuf, |
| 135 | unsigned char *rxbuf) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 136 | { |
Michael Karcher | 8371d72 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 137 | int iocontrol_code; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 138 | struct spi_ioc_transfer msg[2] = { |
| 139 | { |
Uwe Hermann | 48446c8 | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 140 | .tx_buf = (uint64_t)(ptrdiff_t)txbuf, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 141 | .len = writecnt, |
| 142 | }, |
| 143 | { |
Uwe Hermann | 48446c8 | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 144 | .rx_buf = (uint64_t)(ptrdiff_t)rxbuf, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 145 | .len = readcnt, |
| 146 | }, |
| 147 | }; |
| 148 | |
| 149 | if (fd == -1) |
| 150 | return -1; |
Michael Karcher | 8371d72 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 151 | /* The implementation currently does not support requests that |
| 152 | don't start with sending a command. */ |
| 153 | if (writecnt == 0) |
| 154 | return SPI_INVALID_LENGTH; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 155 | |
Michael Karcher | 8371d72 | 2012-03-06 22:17:06 +0000 | [diff] [blame] | 156 | /* Just submit the first (write) request in case there is nothing |
| 157 | to read. Otherwise submit both requests. */ |
| 158 | if (readcnt == 0) |
| 159 | iocontrol_code = SPI_IOC_MESSAGE(1); |
| 160 | else |
| 161 | iocontrol_code = SPI_IOC_MESSAGE(2); |
| 162 | |
| 163 | if (ioctl(fd, iocontrol_code, msg) == -1) { |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 164 | msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno)); |
| 165 | return -1; |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 170 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 171 | unsigned int start, unsigned int len) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 172 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 173 | return spi_read_chunked(flash, buf, start, len, |
| 174 | (unsigned int)getpagesize()); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 177 | static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 178 | unsigned int start, unsigned int len) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 179 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 180 | return spi_write_chunked(flash, buf, start, len, |
| 181 | ((unsigned int)getpagesize()) - 4); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 182 | } |
Stefan Tauner | 8868db3 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 183 | |
| 184 | #endif // CONFIG_LINUX_SPI == 1 |