Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | 5824fbf | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 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> |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <ctype.h> |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 25 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 26 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 27 | #include "programmer.h" |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 28 | #include "spi.h" |
| 29 | |
| 30 | /* Change this to #define if you want to test without a serial implementation */ |
| 31 | #undef FAKE_COMMUNICATION |
| 32 | |
Carl-Daniel Hailfinger | c422484 | 2011-06-09 20:06:34 +0000 | [diff] [blame] | 33 | struct buspirate_spispeeds { |
| 34 | const char *name; |
| 35 | const int speed; |
| 36 | }; |
| 37 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 38 | #ifndef FAKE_COMMUNICATION |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 39 | static int buspirate_serialport_setup(char *dev) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 40 | { |
| 41 | /* 115200bps, 8 databits, no parity, 1 stopbit */ |
| 42 | sp_fd = sp_openserport(dev, 115200); |
| 43 | return 0; |
| 44 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 45 | #else |
| 46 | #define buspirate_serialport_setup(...) 0 |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 47 | #define serialport_shutdown(...) 0 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 48 | #define serialport_write(...) 0 |
| 49 | #define serialport_read(...) 0 |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 50 | #define sp_flush_incoming(...) 0 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame^] | 53 | static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, |
| 54 | unsigned int readcnt) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 55 | { |
| 56 | int i, ret = 0; |
| 57 | |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 58 | msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 59 | if (!writecnt && !readcnt) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 60 | msg_perr("Zero length command!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 61 | return 1; |
| 62 | } |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 63 | msg_pspew("Sending"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 64 | for (i = 0; i < writecnt; i++) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 65 | msg_pspew(" 0x%02x", buf[i]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 66 | #ifdef FAKE_COMMUNICATION |
| 67 | /* Placate the caller for now. */ |
| 68 | if (readcnt) { |
| 69 | buf[0] = 0x01; |
| 70 | memset(buf + 1, 0xff, readcnt - 1); |
| 71 | } |
| 72 | ret = 0; |
| 73 | #else |
| 74 | if (writecnt) |
| 75 | ret = serialport_write(buf, writecnt); |
| 76 | if (ret) |
| 77 | return ret; |
| 78 | if (readcnt) |
| 79 | ret = serialport_read(buf, readcnt); |
| 80 | if (ret) |
| 81 | return ret; |
| 82 | #endif |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 83 | msg_pspew(", receiving"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 84 | for (i = 0; i < readcnt; i++) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 85 | msg_pspew(" 0x%02x", buf[i]); |
| 86 | msg_pspew("\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 90 | static int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 91 | const unsigned char *writearr, unsigned char *readarr); |
| 92 | |
| 93 | static const struct spi_programmer spi_programmer_buspirate = { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame^] | 94 | .type = SPI_CONTROLLER_BUSPIRATE, |
| 95 | .max_data_read = 12, |
| 96 | .max_data_write = 12, |
| 97 | .command = buspirate_spi_send_command, |
| 98 | .multicommand = default_spi_send_multicommand, |
| 99 | .read = default_spi_read, |
| 100 | .write_256 = default_spi_write_256, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 103 | static const struct buspirate_spispeeds spispeeds[] = { |
| 104 | {"30k", 0x0}, |
| 105 | {"125k", 0x1}, |
| 106 | {"250k", 0x2}, |
| 107 | {"1M", 0x3}, |
| 108 | {"2M", 0x4}, |
| 109 | {"2.6M", 0x5}, |
| 110 | {"4M", 0x6}, |
| 111 | {"8M", 0x7}, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame^] | 112 | {NULL, 0x0}, |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 115 | static int buspirate_spi_shutdown(void *data) |
| 116 | { |
| 117 | unsigned char buf[5]; |
| 118 | int ret = 0; |
| 119 | |
| 120 | /* Exit raw SPI mode (enter raw bitbang mode) */ |
| 121 | buf[0] = 0x00; |
| 122 | ret = buspirate_sendrecv(buf, 1, 5); |
| 123 | if (ret) |
| 124 | return ret; |
| 125 | if (memcmp(buf, "BBIO", 4)) { |
| 126 | msg_perr("Entering raw bitbang mode failed!\n"); |
| 127 | return 1; |
| 128 | } |
| 129 | msg_pdbg("Raw bitbang mode version %c\n", buf[4]); |
| 130 | if (buf[4] != '1') { |
| 131 | msg_perr("Can't handle raw bitbang mode version %c!\n", |
| 132 | buf[4]); |
| 133 | return 1; |
| 134 | } |
| 135 | /* Reset Bus Pirate (return to user terminal) */ |
| 136 | buf[0] = 0x0f; |
| 137 | ret = buspirate_sendrecv(buf, 1, 0); |
| 138 | if (ret) |
| 139 | return ret; |
| 140 | |
| 141 | /* Shut down serial port communication */ |
| 142 | ret = serialport_shutdown(NULL); |
| 143 | if (ret) |
| 144 | return ret; |
| 145 | msg_pdbg("Bus Pirate shutdown completed.\n"); |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 150 | int buspirate_spi_init(void) |
| 151 | { |
| 152 | unsigned char buf[512]; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame^] | 153 | int ret = 0, i, spispeed = 0x7; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 154 | char *dev = NULL; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 155 | char *speed = NULL; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 156 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 157 | dev = extract_programmer_param("dev"); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 158 | if (!dev || !strlen(dev)) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 159 | msg_perr("No serial device given. Use flashrom -p " |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 160 | "buspirate_spi:dev=/dev/ttyUSB0\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 161 | return 1; |
| 162 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 163 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 164 | speed = extract_programmer_param("spispeed"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 165 | if (speed) { |
| 166 | for (i = 0; spispeeds[i].name; i++) |
| 167 | if (!strncasecmp(spispeeds[i].name, speed, |
| 168 | strlen(spispeeds[i].name))) { |
| 169 | spispeed = spispeeds[i].speed; |
| 170 | break; |
| 171 | } |
| 172 | if (!spispeeds[i].name) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 173 | msg_perr("Invalid SPI speed, using default.\n"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 174 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 175 | free(speed); |
| 176 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 177 | /* This works because speeds numbering starts at 0 and is contiguous. */ |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 178 | msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 179 | |
| 180 | ret = buspirate_serialport_setup(dev); |
| 181 | if (ret) |
| 182 | return ret; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 183 | free(dev); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 184 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 185 | if (register_shutdown(buspirate_spi_shutdown, NULL)) |
| 186 | return 1; |
| 187 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 188 | /* This is the brute force version, but it should work. */ |
| 189 | for (i = 0; i < 19; i++) { |
| 190 | /* Enter raw bitbang mode */ |
| 191 | buf[0] = 0x00; |
| 192 | /* Send the command, don't read the response. */ |
| 193 | ret = buspirate_sendrecv(buf, 1, 0); |
| 194 | if (ret) |
| 195 | return ret; |
| 196 | /* Read any response and discard it. */ |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 197 | sp_flush_incoming(); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 198 | } |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 199 | /* USB is slow. The Bus Pirate is even slower. Apparently the flush |
| 200 | * action above is too fast or too early. Some stuff still remains in |
| 201 | * the pipe after the flush above, and one additional flush is not |
| 202 | * sufficient either. Use a 1.5 ms delay inside the loop to make |
| 203 | * mostly sure that at least one USB frame had time to arrive. |
| 204 | * Looping only 5 times is not sufficient and causes the |
Cristian Măgherușan-Stanciu | 9932c7b | 2011-07-07 19:56:58 +0000 | [diff] [blame] | 205 | * occasional failure. |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 206 | * Folding the delay into the loop above is not reliable either. |
| 207 | */ |
| 208 | for (i = 0; i < 10; i++) { |
| 209 | usleep(1500); |
| 210 | /* Read any response and discard it. */ |
| 211 | sp_flush_incoming(); |
| 212 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 213 | /* Enter raw bitbang mode */ |
| 214 | buf[0] = 0x00; |
| 215 | ret = buspirate_sendrecv(buf, 1, 5); |
| 216 | if (ret) |
| 217 | return ret; |
| 218 | if (memcmp(buf, "BBIO", 4)) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 219 | msg_perr("Entering raw bitbang mode failed!\n"); |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 220 | msg_pdbg("Got %02x%02x%02x%02x%02x\n", |
| 221 | buf[0], buf[1], buf[2], buf[3], buf[4]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 222 | return 1; |
| 223 | } |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 224 | msg_pdbg("Raw bitbang mode version %c\n", buf[4]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 225 | if (buf[4] != '1') { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 226 | msg_perr("Can't handle raw bitbang mode version %c!\n", |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 227 | buf[4]); |
| 228 | return 1; |
| 229 | } |
| 230 | /* Enter raw SPI mode */ |
| 231 | buf[0] = 0x01; |
| 232 | ret = buspirate_sendrecv(buf, 1, 4); |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 233 | if (ret) |
| 234 | return ret; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 235 | if (memcmp(buf, "SPI", 3)) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 236 | msg_perr("Entering raw SPI mode failed!\n"); |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 237 | msg_pdbg("Got %02x%02x%02x%02x\n", |
| 238 | buf[0], buf[1], buf[2], buf[3]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 239 | return 1; |
| 240 | } |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 241 | msg_pdbg("Raw SPI mode version %c\n", buf[3]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 242 | if (buf[3] != '1') { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 243 | msg_perr("Can't handle raw SPI mode version %c!\n", |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 244 | buf[3]); |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */ |
| 249 | buf[0] = 0x40 | 0xb; |
| 250 | ret = buspirate_sendrecv(buf, 1, 1); |
| 251 | if (ret) |
| 252 | return 1; |
| 253 | if (buf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 254 | msg_perr("Protocol error while setting power/CS/AUX!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 255 | return 1; |
| 256 | } |
| 257 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 258 | /* Set SPI speed */ |
| 259 | buf[0] = 0x60 | spispeed; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 260 | ret = buspirate_sendrecv(buf, 1, 1); |
| 261 | if (ret) |
| 262 | return 1; |
| 263 | if (buf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 264 | msg_perr("Protocol error while setting SPI speed!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 265 | return 1; |
| 266 | } |
| 267 | |
| 268 | /* Set SPI config: output type, idle, clock edge, sample */ |
| 269 | buf[0] = 0x80 | 0xa; |
| 270 | ret = buspirate_sendrecv(buf, 1, 1); |
| 271 | if (ret) |
| 272 | return 1; |
| 273 | if (buf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 274 | msg_perr("Protocol error while setting SPI config!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 275 | return 1; |
| 276 | } |
| 277 | |
| 278 | /* De-assert CS# */ |
| 279 | buf[0] = 0x03; |
| 280 | ret = buspirate_sendrecv(buf, 1, 1); |
| 281 | if (ret) |
| 282 | return 1; |
| 283 | if (buf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 284 | msg_perr("Protocol error while raising CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 285 | return 1; |
| 286 | } |
| 287 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 288 | register_spi_programmer(&spi_programmer_buspirate); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 293 | static int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 294 | const unsigned char *writearr, unsigned char *readarr) |
| 295 | { |
| 296 | static unsigned char *buf = NULL; |
| 297 | int i = 0, ret = 0; |
| 298 | |
| 299 | if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16) |
| 300 | return SPI_INVALID_LENGTH; |
| 301 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 302 | /* 3 bytes extra for CS#, len, CS#. */ |
| 303 | buf = realloc(buf, writecnt + readcnt + 3); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 304 | if (!buf) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 305 | msg_perr("Out of memory!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 306 | exit(1); // -1 |
| 307 | } |
| 308 | |
| 309 | /* Assert CS# */ |
| 310 | buf[i++] = 0x02; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 311 | |
| 312 | buf[i++] = 0x10 | (writecnt + readcnt - 1); |
| 313 | memcpy(buf + i, writearr, writecnt); |
| 314 | i += writecnt; |
| 315 | memset(buf + i, 0, readcnt); |
| 316 | |
| 317 | i += readcnt; |
| 318 | /* De-assert CS# */ |
| 319 | buf[i++] = 0x03; |
| 320 | |
| 321 | ret = buspirate_sendrecv(buf, i, i); |
| 322 | |
| 323 | if (ret) { |
| 324 | msg_perr("Bus Pirate communication error!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 325 | return SPI_GENERIC_ERROR; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 328 | if (buf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 329 | msg_perr("Protocol error while lowering CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 330 | return SPI_GENERIC_ERROR; |
| 331 | } |
| 332 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 333 | if (buf[1] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 334 | msg_perr("Protocol error while reading/writing SPI!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 335 | return SPI_GENERIC_ERROR; |
| 336 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 337 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 338 | if (buf[i - 1] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 339 | msg_perr("Protocol error while raising CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 340 | return SPI_GENERIC_ERROR; |
| 341 | } |
| 342 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 343 | /* Skip CS#, length, writearr. */ |
| 344 | memcpy(readarr, buf + 2 + writecnt, readcnt); |
| 345 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 346 | return ret; |
| 347 | } |