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 | * |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Stefan Tauner | ee310a4 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 17 | #if CONFIG_LINUX_SPI == 1 |
| 18 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <stdlib.h> |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame^] | 22 | #include <stdint.h> |
| 23 | #include <inttypes.h> |
Gwenhael Goavec-Merou | f3f3928 | 2015-11-14 02:55:12 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <ctype.h> |
| 27 | #include <unistd.h> |
Gwenhael Goavec-Merou | f3f3928 | 2015-11-14 02:55:12 +0000 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
Stefan Tauner | ee310a4 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 29 | #include <linux/types.h> |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 30 | #include <linux/spi/spidev.h> |
Gwenhael Goavec-Merou | f3f3928 | 2015-11-14 02:55:12 +0000 | [diff] [blame] | 31 | #include <linux/ioctl.h> |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 32 | #include <sys/stat.h> |
| 33 | #include <sys/types.h> |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 34 | #include "file.h" |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 35 | #include "flash.h" |
| 36 | #include "chipdrivers.h" |
| 37 | #include "programmer.h" |
| 38 | #include "spi.h" |
| 39 | |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 40 | |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 41 | |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 42 | /* TODO: this information should come from the SPI master driver. */ |
| 43 | #define SPI_DMA_SIZE 64 |
| 44 | |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 45 | #ifndef SPIDEV_MAJOR |
| 46 | #define SPIDEV_MAJOR 153 /* refer to kernel/files/drivers/spi/spidev.c */ |
| 47 | #endif |
| 48 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 49 | #define MODALIAS_FILE "modalias" |
| 50 | #define LINUX_SPI_SYSFS_ROOT "/sys/bus/spi/devices" |
Louis Yung-Chieh Lo | 469707f | 2012-05-18 16:38:37 +0800 | [diff] [blame] | 51 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 52 | static int fd = -1; |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 53 | #define BUF_SIZE_FROM_SYSFS "/sys/module/spidev/parameters/bufsiz" |
| 54 | static size_t max_kernel_buf_size; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 55 | |
David Hendricks | 93784b4 | 2016-08-09 17:00:38 -0700 | [diff] [blame] | 56 | static int linux_spi_shutdown(void *data); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 57 | static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 58 | const unsigned char *txbuf, unsigned char *rxbuf); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 59 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 60 | unsigned int start, unsigned int len); |
Patrick Georgi | ab8353e | 2017-02-03 18:32:01 +0100 | [diff] [blame] | 61 | static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 62 | unsigned int start, unsigned int len); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 63 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 64 | static const struct spi_master spi_master_linux = { |
Edward O'Callaghan | a6673bd | 2019-06-24 15:22:28 +1000 | [diff] [blame] | 65 | .features = SPI_MASTER_4BA, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 66 | .max_data_read = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 67 | .max_data_write = MAX_DATA_UNSPECIFIED, /* TODO? */ |
| 68 | .command = linux_spi_send_command, |
| 69 | .multicommand = default_spi_send_multicommand, |
| 70 | .read = linux_spi_read, |
| 71 | .write_256 = linux_spi_write_256, |
| 72 | }; |
| 73 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 74 | static char devfs_path[32]; /* at least big enough to fit /dev/spidevX.Y */ |
David Hendricks | 2d06918 | 2015-10-30 22:52:57 -0700 | [diff] [blame] | 75 | static char *check_sysfs(void) |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 76 | { |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 77 | int i; |
| 78 | const char *sysfs_path = NULL; |
| 79 | char *p; |
| 80 | char *modalias[] = { |
David Hendricks | dca1adf | 2013-11-01 13:31:57 -0700 | [diff] [blame] | 81 | "spi:spidev", /* raw access over SPI bus (newer kernels) */ |
David Hendricks | 6ed2685 | 2013-11-12 12:53:40 -0800 | [diff] [blame] | 82 | "spidev", /* raw access over SPI bus (older kernels) */ |
| 83 | "m25p80", /* generic MTD device */ |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 84 | }; |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 85 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 86 | for (i = 0; i < ARRAY_SIZE(modalias); i++) { |
| 87 | int major, minor; |
David Hendricks | 0c0a7e6 | 2012-06-12 23:41:05 +0900 | [diff] [blame] | 88 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 89 | /* Path should look like: /sys/blah/spiX.Y/modalias */ |
| 90 | sysfs_path = scanft(LINUX_SPI_SYSFS_ROOT, |
David Hendricks | e94c7a8 | 2013-11-12 14:22:36 -0800 | [diff] [blame] | 91 | MODALIAS_FILE, modalias[i], 1); |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 92 | if (!sysfs_path) |
Louis Yung-Chieh Lo | eaa44fc | 2012-05-24 15:43:50 +0800 | [diff] [blame] | 93 | continue; |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 94 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 95 | p = (char *)sysfs_path + strlen(LINUX_SPI_SYSFS_ROOT); |
| 96 | if (p[0] == '/') |
| 97 | p++; |
David Hendricks | 0c0a7e6 | 2012-06-12 23:41:05 +0900 | [diff] [blame] | 98 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 99 | if (sscanf(p, "spi%u.%u", &major, &minor) == 2) { |
David Hendricks | d02ee0f | 2013-11-01 13:18:48 -0700 | [diff] [blame] | 100 | msg_pdbg("Found SPI device %s on spi%u.%u\n", |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 101 | modalias[i], major, minor); |
| 102 | sprintf(devfs_path, "/dev/spidev%u.%u", major, minor); |
| 103 | free((void *)sysfs_path); |
| 104 | break; |
| 105 | } |
| 106 | free((void *)sysfs_path); |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 107 | } |
| 108 | |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 109 | if (i == ARRAY_SIZE(modalias)) |
| 110 | return NULL; |
| 111 | return devfs_path; |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 112 | } |
| 113 | |
David Hendricks | 2d06918 | 2015-10-30 22:52:57 -0700 | [diff] [blame] | 114 | static char *check_fdt(void) |
| 115 | { |
| 116 | unsigned int bus, cs; |
| 117 | |
| 118 | if (fdt_find_spi_nor_flash(&bus, &cs) < 0) |
| 119 | return NULL; |
| 120 | |
| 121 | sprintf(devfs_path, "/dev/spidev%u.%u", bus, cs); |
| 122 | return devfs_path; |
| 123 | } |
| 124 | |
| 125 | static char *linux_spi_probe(void) |
| 126 | { |
| 127 | char *ret; |
| 128 | |
| 129 | ret = check_fdt(); |
| 130 | if (ret) |
| 131 | return ret; |
| 132 | |
| 133 | ret = check_sysfs(); |
| 134 | if (ret) |
| 135 | return ret; |
| 136 | |
| 137 | return NULL; |
| 138 | } |
| 139 | |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 140 | int linux_spi_init(void) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 141 | { |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 142 | char *p, *endp, *dev; |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame^] | 143 | uint32_t speed_hz = 2 * 1000 * 1000; |
Stefan Tauner | 180d899 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 144 | /* FIXME: make the following configurable by CLI options. */ |
| 145 | /* SPI mode 0 (beware this also includes: MSB first, CS active low and others */ |
| 146 | const uint8_t mode = SPI_MODE_0; |
| 147 | const uint8_t bits = 8; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 148 | |
David Hendricks | ba0827a | 2013-05-03 20:25:40 -0700 | [diff] [blame] | 149 | /* |
| 150 | * FIXME: There might be other programmers with flash memory (such as |
| 151 | * an EC) connected via SPI. For now we rely on the device's driver to |
| 152 | * distinguish it and assume generic SPI implies host. |
| 153 | */ |
| 154 | if (alias && alias->type != ALIAS_HOST) |
| 155 | return 1; |
| 156 | |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 157 | dev = extract_programmer_param("dev"); |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 158 | if (!dev) |
Louis Yung-Chieh Lo | a20b393 | 2012-05-22 22:14:00 +0800 | [diff] [blame] | 159 | dev = linux_spi_probe(); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 160 | if (!dev || !strlen(dev)) { |
David Hendricks | 98bbc26 | 2013-10-30 21:38:58 -0700 | [diff] [blame] | 161 | msg_perr("No SPI device found. Use flashrom -p " |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 162 | "linux_spi:dev=/dev/spidevX.Y\n"); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 163 | return 1; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | p = extract_programmer_param("speed"); |
| 167 | if (p && strlen(p)) { |
Alexandru Gagniuc | 18fe18f | 2014-03-19 17:17:06 +0000 | [diff] [blame] | 168 | speed_hz = (uint32_t)strtoul(p, &endp, 10) * 1000; |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame^] | 169 | if (p == endp || speed_hz == 0) { |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 170 | msg_perr("%s: invalid clock: %s kHz\n", __func__, p); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 171 | return 1; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 172 | } |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame^] | 173 | } else { |
| 174 | msg_pinfo("Using default %"PRIu32 |
| 175 | "kHz clock. Use 'spispeed' parameter to override.\n", |
| 176 | speed_hz / 1000); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 177 | } |
| 178 | |
uwe | 6b716f0 | 2011-09-07 20:48:34 +0000 | [diff] [blame] | 179 | msg_pdbg("Using device %s\n", dev); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 180 | if ((fd = open(dev, O_RDWR)) == -1) { |
Nikolai Artemiev | 648f127 | 2020-05-19 15:02:46 +1000 | [diff] [blame] | 181 | msg_perr("%s: failed to open %s: %s\n", __func__, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 182 | dev, strerror(errno)); |
Louis Yung-Chieh Lo | 282c241 | 2012-07-27 18:24:37 +0800 | [diff] [blame] | 183 | |
Nikolai Artemiev | 648f127 | 2020-05-19 15:02:46 +1000 | [diff] [blame] | 184 | return 1; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Stefan Tauner | 180d899 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 187 | if (register_shutdown(linux_spi_shutdown, NULL)) |
| 188 | return 1; |
| 189 | /* We rely on the shutdown function for cleanup from here on. */ |
| 190 | |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame^] | 191 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz) == -1) { |
| 192 | msg_perr("%s: failed to set speed to %"PRIu32"Hz: %s\n", |
| 193 | __func__, speed_hz, strerror(errno)); |
| 194 | return 1; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 195 | } |
Nico Huber | 4b941a9 | 2018-12-22 00:08:50 +0100 | [diff] [blame^] | 196 | msg_pdbg("Using %"PRIu32"kHz clock\n", speed_hz / 1000); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 197 | |
Stefan Tauner | 180d899 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 198 | if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) { |
| 199 | msg_perr("%s: failed to set SPI mode to 0x%02x: %s\n", |
| 200 | __func__, mode, strerror(errno)); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 201 | return 1; |
Stefan Tauner | 180d899 | 2012-03-03 18:09:33 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits) == -1) { |
| 205 | msg_perr("%s: failed to set the number of bits per SPI word to %u: %s\n", |
| 206 | __func__, bits == 0 ? 8 : bits, strerror(errno)); |
| 207 | return 1; |
| 208 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 209 | |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 210 | /* Read max buffer size from sysfs, or use page size as fallback. */ |
| 211 | FILE *fp; |
| 212 | fp = fopen(BUF_SIZE_FROM_SYSFS, "r"); |
| 213 | if (!fp) { |
| 214 | msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno)); |
| 215 | goto out; |
| 216 | } |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 217 | |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 218 | char buf[10]; |
| 219 | memset(buf, 0, sizeof(buf)); |
| 220 | if (!fread(buf, 1, sizeof(buf) - 1, fp)) { |
| 221 | if (feof(fp)) |
| 222 | msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS); |
| 223 | else |
| 224 | msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno)); |
| 225 | goto out; |
| 226 | } |
| 227 | |
| 228 | long int tmp; |
| 229 | errno = 0; |
| 230 | tmp = strtol(buf, NULL, 0); |
| 231 | if ((tmp < 0) || errno) { |
| 232 | msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS); |
| 233 | } else { |
| 234 | msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS); |
| 235 | max_kernel_buf_size = (size_t)tmp; |
| 236 | } |
| 237 | |
| 238 | out: |
| 239 | if (fp) |
| 240 | fclose(fp); |
| 241 | |
| 242 | if (!max_kernel_buf_size) { |
| 243 | msg_pdbg("%s: Using page size as max buffer size.\n", __func__); |
| 244 | max_kernel_buf_size = (size_t)getpagesize(); |
| 245 | } |
| 246 | |
| 247 | msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size); |
| 248 | register_spi_master(&spi_master_linux); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 249 | return 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 250 | } |
| 251 | |
David Hendricks | 93784b4 | 2016-08-09 17:00:38 -0700 | [diff] [blame] | 252 | static int linux_spi_shutdown(void *data) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 253 | { |
| 254 | if (fd != -1) { |
| 255 | close(fd); |
| 256 | fd = -1; |
| 257 | } |
| 258 | return 0; |
| 259 | } |
| 260 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 261 | static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 262 | const unsigned char *txbuf, unsigned char *rxbuf) |
| 263 | { |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 264 | int msg_start = 0, msg_count = 0; |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 265 | struct spi_ioc_transfer msg[2] = { |
| 266 | { |
David Riley | 13343b8 | 2014-07-25 11:37:56 -0700 | [diff] [blame] | 267 | .tx_buf = (uint64_t)(uintptr_t)txbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 268 | .len = writecnt, |
| 269 | }, |
| 270 | { |
David Riley | 13343b8 | 2014-07-25 11:37:56 -0700 | [diff] [blame] | 271 | .rx_buf = (uint64_t)(uintptr_t)rxbuf, |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 272 | .len = readcnt, |
| 273 | }, |
| 274 | }; |
| 275 | |
| 276 | if (fd == -1) |
| 277 | return -1; |
| 278 | |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 279 | /* Only pass necessary msg[] to ioctl() to avoid the empty message |
Louis Yung-Chieh Lo | b121eff | 2012-05-22 23:31:03 +0800 | [diff] [blame] | 280 | * which drives an un-expected CS line and clocks. */ |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 281 | if (writecnt) { |
| 282 | msg_start = 0; /* tx: msg[0] */ |
| 283 | msg_count++; |
| 284 | if (readcnt) { |
| 285 | msg_count++; |
| 286 | } |
Louis Yung-Chieh Lo | b121eff | 2012-05-22 23:31:03 +0800 | [diff] [blame] | 287 | } else if (readcnt) { |
| 288 | msg_start = 1; /* rx: msg[1] */ |
| 289 | msg_count++; |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 290 | } else { |
Louis Yung-Chieh Lo | b121eff | 2012-05-22 23:31:03 +0800 | [diff] [blame] | 291 | msg_cerr("%s: both writecnt and readcnt are 0.\n", |
| 292 | __func__); |
| 293 | return -1; |
Louis Yung-Chieh Lo | 327c9e9 | 2012-05-21 12:02:45 +0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | if (ioctl(fd, SPI_IOC_MESSAGE(msg_count), &msg[msg_start]) == -1) { |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 297 | msg_cerr("%s: ioctl: %s\n", __func__, strerror(errno)); |
| 298 | return -1; |
| 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 303 | static int linux_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 304 | { |
Nico Huber | b5ebaaf | 2018-03-08 16:14:15 +0100 | [diff] [blame] | 305 | /* Older kernels use a single buffer for combined input and output |
| 306 | data. So account for longest possible command + address, too. */ |
| 307 | return spi_read_chunked(flash, buf, start, len, max_kernel_buf_size - 5); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Patrick Georgi | ab8353e | 2017-02-03 18:32:01 +0100 | [diff] [blame] | 310 | static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 311 | unsigned int start, unsigned int len) |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 312 | { |
Keno Fischer | 556e478 | 2015-11-15 14:58:25 +0000 | [diff] [blame] | 313 | /* 5 bytes must be reserved for longest possible command + address. */ |
| 314 | return spi_write_chunked(flash, buf, start, len, max_kernel_buf_size - 5); |
uwe | 7df6dda | 2011-09-03 18:37:52 +0000 | [diff] [blame] | 315 | } |
Stefan Tauner | ee310a4 | 2012-03-13 00:18:19 +0000 | [diff] [blame] | 316 | |
| 317 | #endif // CONFIG_LINUX_SPI == 1 |