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 | |
| 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 | |
| 34 | static int fd = -1; |
| 35 | |
| 36 | static int linux_spi_shutdown(void *data); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 37 | static 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 Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 41 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 42 | unsigned int start, unsigned int len); |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 43 | static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 44 | unsigned int start, unsigned int len); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 45 | |
| 46 | static 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 | |
| 56 | int linux_spi_init(void) |
| 57 | { |
| 58 | char *p, *endp, *dev; |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 59 | uint32_t speed = 0; |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 60 | /* 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 Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 64 | |
| 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 Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 74 | speed = (uint32_t)strtoul(p, &endp, 10) * 1024; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 75 | if (p == endp) { |
| 76 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
| 77 | return 1; |
| 78 | } |
| 79 | } |
| 80 | |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 81 | msg_pdbg("Using device %s\n", dev); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 82 | 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 Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 88 | if (register_shutdown(linux_spi_shutdown, NULL)) |
| 89 | return 1; |
| 90 | /* We rely on the shutdown function for cleanup from here on. */ |
| 91 | |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 92 | if (speed > 0) { |
| 93 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) { |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 94 | msg_perr("%s: failed to set speed to %d Hz: %s\n", |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 95 | __func__, speed, strerror(errno)); |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | msg_pdbg("Using %d kHz clock\n", speed); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 102 | 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 Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 105 | return 1; |
Stefan Tauner | 2c3e9d5 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 106 | } |
| 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 Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 113 | |
| 114 | register_spi_programmer(&spi_programmer_linux); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static 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 Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 128 | static 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 Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 132 | { |
Michael Karcher | 8371d72 | 2012-03-06 22:17:06 +0000 | [diff] [blame^] | 133 | int iocontrol_code; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 134 | struct spi_ioc_transfer msg[2] = { |
| 135 | { |
Uwe Hermann | 48446c8 | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 136 | .tx_buf = (uint64_t)(ptrdiff_t)txbuf, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 137 | .len = writecnt, |
| 138 | }, |
| 139 | { |
Uwe Hermann | 48446c8 | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 140 | .rx_buf = (uint64_t)(ptrdiff_t)rxbuf, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 141 | .len = readcnt, |
| 142 | }, |
| 143 | }; |
| 144 | |
| 145 | if (fd == -1) |
| 146 | return -1; |
Michael Karcher | 8371d72 | 2012-03-06 22:17:06 +0000 | [diff] [blame^] | 147 | /* 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 Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 151 | |
Michael Karcher | 8371d72 | 2012-03-06 22:17:06 +0000 | [diff] [blame^] | 152 | /* 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 Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 160 | msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno)); |
| 161 | return -1; |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 166 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 167 | unsigned int start, unsigned int len) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 168 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 169 | return spi_read_chunked(flash, buf, start, len, |
| 170 | (unsigned int)getpagesize()); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 173 | static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 174 | unsigned int start, unsigned int len) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 175 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 176 | return spi_write_chunked(flash, buf, start, len, |
| 177 | ((unsigned int)getpagesize()) - 4); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 178 | } |