uwe | 7df6dda | 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 | |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 34 | |
| 35 | /* TODO: this information should come from the SPI master driver. */ |
| 36 | #define SPI_DMA_SIZE 64 |
| 37 | |
| 38 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 39 | static int fd = -1; |
| 40 | |
| 41 | static int linux_spi_shutdown(void *data); |
| 42 | static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 43 | const unsigned char *txbuf, unsigned char *rxbuf); |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 44 | static int linux_spi_read(struct flashchip *flash, uint8_t *buf, |
| 45 | unsigned int start, unsigned int len); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 46 | static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 47 | unsigned int start, unsigned int len); |
uwe | 7df6dda | 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, |
| 57 | }; |
| 58 | |
| 59 | int linux_spi_init(void) |
| 60 | { |
| 61 | char *p, *endp, *dev; |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 62 | uint32_t speed = 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 63 | |
| 64 | dev = extract_programmer_param("dev"); |
| 65 | if (!dev || !strlen(dev)) { |
| 66 | msg_perr("No SPI device given. Use flashrom -p " |
| 67 | "linux_spi:dev=/dev/spidevX.Y\n"); |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | p = extract_programmer_param("speed"); |
| 72 | if (p && strlen(p)) { |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 73 | speed = (uint32_t)strtoul(p, &endp, 10) * 1024; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 74 | if (p == endp) { |
| 75 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
| 76 | return 1; |
| 77 | } |
| 78 | } |
| 79 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 80 | msg_pdbg("Using device %s\n", dev); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 81 | if ((fd = open(dev, O_RDWR)) == -1) { |
| 82 | msg_perr("%s: failed to open %s: %s\n", __func__, |
| 83 | dev, strerror(errno)); |
| 84 | return 1; |
| 85 | } |
| 86 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 87 | if (speed > 0) { |
| 88 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) { |
| 89 | msg_perr("%s: failed to set speed %dHz: %s\n", |
| 90 | __func__, speed, strerror(errno)); |
| 91 | close(fd); |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | msg_pdbg("Using %d kHz clock\n", speed); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | if (register_shutdown(linux_spi_shutdown, NULL)) |
| 99 | return 1; |
| 100 | |
| 101 | register_spi_programmer(&spi_programmer_linux); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int linux_spi_shutdown(void *data) |
| 107 | { |
| 108 | if (fd != -1) { |
| 109 | close(fd); |
| 110 | fd = -1; |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int linux_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 116 | const unsigned char *txbuf, unsigned char *rxbuf) |
| 117 | { |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 118 | int msg_start = 0, msg_count = 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 119 | struct spi_ioc_transfer msg[2] = { |
| 120 | { |
uwe | 230909b | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 121 | .tx_buf = (uint64_t)(ptrdiff_t)txbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 122 | .len = writecnt, |
| 123 | }, |
| 124 | { |
uwe | 230909b | 2011-09-06 18:17:02 +0000 | [diff] [blame] | 125 | .rx_buf = (uint64_t)(ptrdiff_t)rxbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 126 | .len = readcnt, |
| 127 | }, |
| 128 | }; |
| 129 | |
| 130 | if (fd == -1) |
| 131 | return -1; |
| 132 | |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 133 | /* Only pass necessary msg[] to ioctl() to avoid the empty message |
| 134 | * drives un-expected CS line and clocks. */ |
| 135 | if (writecnt) { |
| 136 | msg_start = 0; /* tx: msg[0] */ |
| 137 | msg_count++; |
| 138 | if (readcnt) { |
| 139 | msg_count++; |
| 140 | } |
| 141 | } else { |
| 142 | if (readcnt) { |
| 143 | msg_start = 1; /* rx: msg[1] */ |
| 144 | msg_count++; |
| 145 | } else { |
| 146 | msg_cerr("%s: both writecnt and readcnt are 0.\n", |
| 147 | __func__); |
| 148 | return -1; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) { |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 153 | msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno)); |
| 154 | return -1; |
| 155 | } |
| 156 | return 0; |
| 157 | } |
| 158 | |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 159 | static int linux_spi_read(struct flashchip *flash, uint8_t *buf, |
| 160 | unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 161 | { |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 162 | return spi_read_chunked(flash, buf, start, len, SPI_DMA_SIZE); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static int linux_spi_write_256(struct flashchip *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 166 | unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 167 | { |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 168 | return spi_write_chunked(flash, buf, start, len, |
| 169 | SPI_DMA_SIZE - 5 /* WREN, Page Program */); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 170 | } |