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; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 60 | |
| 61 | dev = extract_programmer_param("dev"); |
| 62 | if (!dev || !strlen(dev)) { |
| 63 | msg_perr("No SPI device given. Use flashrom -p " |
| 64 | "linux_spi:dev=/dev/spidevX.Y\n"); |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | p = extract_programmer_param("speed"); |
| 69 | if (p && strlen(p)) { |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 70 | speed = (uint32_t)strtoul(p, &endp, 10) * 1024; |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 71 | if (p == endp) { |
| 72 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
| 73 | return 1; |
| 74 | } |
| 75 | } |
| 76 | |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 77 | msg_pdbg("Using device %s\n", dev); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 78 | if ((fd = open(dev, O_RDWR)) == -1) { |
| 79 | msg_perr("%s: failed to open %s: %s\n", __func__, |
| 80 | dev, strerror(errno)); |
| 81 | return 1; |
| 82 | } |
| 83 | |
Sven Schnelle | cb24ddb | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 84 | if (speed > 0) { |
| 85 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) { |
| 86 | msg_perr("%s: failed to set speed %dHz: %s\n", |
| 87 | __func__, speed, strerror(errno)); |
| 88 | close(fd); |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | msg_pdbg("Using %d kHz clock\n", speed); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | if (register_shutdown(linux_spi_shutdown, NULL)) |
| 96 | return 1; |
| 97 | |
| 98 | register_spi_programmer(&spi_programmer_linux); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int linux_spi_shutdown(void *data) |
| 104 | { |
| 105 | if (fd != -1) { |
| 106 | close(fd); |
| 107 | fd = -1; |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 112 | static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt, |
| 113 | unsigned int readcnt, |
| 114 | const unsigned char *txbuf, |
| 115 | unsigned char *rxbuf) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 116 | { |
| 117 | struct spi_ioc_transfer msg[2] = { |
| 118 | { |
Uwe Hermann | 48446c8 | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 119 | .tx_buf = (uint64_t)(ptrdiff_t)txbuf, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 120 | .len = writecnt, |
| 121 | }, |
| 122 | { |
Uwe Hermann | 48446c8 | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 123 | .rx_buf = (uint64_t)(ptrdiff_t)rxbuf, |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 124 | .len = readcnt, |
| 125 | }, |
| 126 | }; |
| 127 | |
| 128 | if (fd == -1) |
| 129 | return -1; |
| 130 | |
| 131 | if (ioctl(fd, SPI_IOC_MESSAGE(2), msg) == -1) { |
| 132 | msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno)); |
| 133 | return -1; |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 138 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 139 | unsigned int start, unsigned int len) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 140 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 141 | return spi_read_chunked(flash, buf, start, len, |
| 142 | (unsigned int)getpagesize()); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 145 | static int linux_spi_write_256(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 146 | unsigned int start, unsigned int len) |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 147 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 148 | return spi_write_chunked(flash, buf, start, len, |
| 149 | ((unsigned int)getpagesize()) - 4); |
Sven Schnelle | 5ce5f70 | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 150 | } |