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 | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009, 2010, 2011, 2012 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 | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 21 | #include <strings.h> |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <ctype.h> |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 26 | #include "flash.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); |
Stefan Tauner | acfc4c6 | 2012-11-30 16:46:45 +0000 | [diff] [blame] | 43 | if (sp_fd == SER_INV_FD) |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 44 | return 1; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 47 | #else |
| 48 | #define buspirate_serialport_setup(...) 0 |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 49 | #define serialport_shutdown(...) 0 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 50 | #define serialport_write(...) 0 |
| 51 | #define serialport_read(...) 0 |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 52 | #define sp_flush_incoming(...) 0 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 55 | static unsigned char *bp_commbuf = NULL; |
| 56 | static int bp_commbufsize = 0; |
| 57 | |
| 58 | static int buspirate_commbuf_grow(int bufsize) |
| 59 | { |
| 60 | unsigned char *tmpbuf; |
| 61 | |
| 62 | /* Never shrink. realloc() calls are expensive. */ |
| 63 | if (bufsize <= bp_commbufsize) |
| 64 | return 0; |
| 65 | |
| 66 | tmpbuf = realloc(bp_commbuf, bufsize); |
| 67 | if (!tmpbuf) { |
| 68 | /* Keep the existing buffer because memory is already tight. */ |
| 69 | msg_perr("Out of memory!\n"); |
| 70 | return ERROR_OOM; |
| 71 | } |
| 72 | |
| 73 | bp_commbuf = tmpbuf; |
| 74 | bp_commbufsize = bufsize; |
| 75 | return 0; |
| 76 | } |
| 77 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 78 | static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, |
| 79 | unsigned int readcnt) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 80 | { |
| 81 | int i, ret = 0; |
| 82 | |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 83 | msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 84 | if (!writecnt && !readcnt) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 85 | msg_perr("Zero length command!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 86 | return 1; |
| 87 | } |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 88 | if (writecnt) |
| 89 | msg_pspew("Sending"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 90 | for (i = 0; i < writecnt; i++) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 91 | msg_pspew(" 0x%02x", buf[i]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 92 | #ifdef FAKE_COMMUNICATION |
| 93 | /* Placate the caller for now. */ |
| 94 | if (readcnt) { |
| 95 | buf[0] = 0x01; |
| 96 | memset(buf + 1, 0xff, readcnt - 1); |
| 97 | } |
| 98 | ret = 0; |
| 99 | #else |
| 100 | if (writecnt) |
| 101 | ret = serialport_write(buf, writecnt); |
| 102 | if (ret) |
| 103 | return ret; |
| 104 | if (readcnt) |
| 105 | ret = serialport_read(buf, readcnt); |
| 106 | if (ret) |
| 107 | return ret; |
| 108 | #endif |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 109 | if (readcnt) |
| 110 | msg_pspew(", receiving"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 111 | for (i = 0; i < readcnt; i++) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 112 | msg_pspew(" 0x%02x", buf[i]); |
| 113 | msg_pspew("\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 117 | static int buspirate_wait_for_string(unsigned char *buf, char *key) |
| 118 | { |
| 119 | unsigned int keylen = strlen(key); |
| 120 | int ret; |
| 121 | |
| 122 | ret = buspirate_sendrecv(buf, 0, keylen); |
| 123 | while (!ret) { |
| 124 | if (!memcmp(buf, key, keylen)) |
| 125 | return 0; |
| 126 | memmove(buf, buf + 1, keylen - 1); |
| 127 | ret = buspirate_sendrecv(buf + keylen - 1, 0, 1); |
| 128 | } |
| 129 | return ret; |
| 130 | } |
| 131 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 132 | static int buspirate_spi_send_command_v1(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
| 133 | const unsigned char *writearr, unsigned char *readarr); |
| 134 | static int buspirate_spi_send_command_v2(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
| 135 | const unsigned char *writearr, unsigned char *readarr); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 136 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 137 | static struct spi_master spi_master_buspirate = { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 138 | .type = SPI_CONTROLLER_BUSPIRATE, |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 139 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 140 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 141 | .command = NULL, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 142 | .multicommand = default_spi_send_multicommand, |
| 143 | .read = default_spi_read, |
| 144 | .write_256 = default_spi_write_256, |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 145 | .write_aai = default_spi_write_aai, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 148 | static const struct buspirate_spispeeds spispeeds[] = { |
| 149 | {"30k", 0x0}, |
| 150 | {"125k", 0x1}, |
| 151 | {"250k", 0x2}, |
| 152 | {"1M", 0x3}, |
| 153 | {"2M", 0x4}, |
| 154 | {"2.6M", 0x5}, |
| 155 | {"4M", 0x6}, |
| 156 | {"8M", 0x7}, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 157 | {NULL, 0x0}, |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 160 | static int buspirate_spi_shutdown(void *data) |
| 161 | { |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 162 | int ret = 0, ret2 = 0; |
| 163 | /* No need to allocate a buffer here, we know that bp_commbuf is at least DEFAULT_BUFSIZE big. */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 164 | |
| 165 | /* Exit raw SPI mode (enter raw bitbang mode) */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 166 | bp_commbuf[0] = 0x00; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 167 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 168 | goto out_shutdown; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 169 | if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO"))) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 170 | goto out_shutdown; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 171 | if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1))) |
| 172 | goto out_shutdown; |
| 173 | msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]); |
| 174 | if (bp_commbuf[0] != '1') { |
| 175 | msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 176 | ret = 1; |
| 177 | goto out_shutdown; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 178 | } |
| 179 | /* Reset Bus Pirate (return to user terminal) */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 180 | bp_commbuf[0] = 0x0f; |
| 181 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 182 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 183 | out_shutdown: |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 184 | /* Shut down serial port communication */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 185 | ret2 = serialport_shutdown(NULL); |
| 186 | /* Keep the oldest error, it is probably the best indicator. */ |
| 187 | if (ret2 && !ret) |
| 188 | ret = ret2; |
| 189 | bp_commbufsize = 0; |
| 190 | free(bp_commbuf); |
| 191 | bp_commbuf = NULL; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 192 | if (ret) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 193 | msg_pdbg("Bus Pirate shutdown failed.\n"); |
| 194 | else |
| 195 | msg_pdbg("Bus Pirate shutdown completed.\n"); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 196 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 197 | return ret; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 200 | #define BP_FWVERSION(a,b) ((a) << 8 | (b)) |
| 201 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 202 | int buspirate_spi_init(void) |
| 203 | { |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 204 | char *tmp; |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 205 | char *dev; |
| 206 | int i; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 207 | unsigned int fw_version_major = 0; |
| 208 | unsigned int fw_version_minor = 0; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 209 | int spispeed = 0x7; |
| 210 | int ret = 0; |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 211 | int pullup = 0; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 212 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 213 | dev = extract_programmer_param("dev"); |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 214 | if (dev && !strlen(dev)) { |
| 215 | free(dev); |
| 216 | dev = NULL; |
| 217 | } |
| 218 | if (!dev) { |
| 219 | msg_perr("No serial device given. Use flashrom -p buspirate_spi:dev=/dev/ttyUSB0\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 220 | return 1; |
| 221 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 222 | |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 223 | tmp = extract_programmer_param("spispeed"); |
| 224 | if (tmp) { |
| 225 | for (i = 0; spispeeds[i].name; i++) { |
| 226 | if (!strncasecmp(spispeeds[i].name, tmp, strlen(spispeeds[i].name))) { |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 227 | spispeed = spispeeds[i].speed; |
| 228 | break; |
| 229 | } |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 230 | } |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 231 | if (!spispeeds[i].name) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 232 | msg_perr("Invalid SPI speed, using default.\n"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 233 | } |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 234 | free(tmp); |
| 235 | |
| 236 | tmp = extract_programmer_param("pullups"); |
| 237 | if (tmp) { |
| 238 | if (strcasecmp("on", tmp) == 0) |
| 239 | pullup = 1; |
| 240 | else if (strcasecmp("off", tmp) == 0) |
| 241 | ; // ignore |
| 242 | else |
| 243 | msg_perr("Invalid pullups state, not using them.\n"); |
| 244 | } |
| 245 | free(tmp); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 246 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 247 | /* Default buffer size is 19: 16 bytes data, 3 bytes control. */ |
| 248 | #define DEFAULT_BUFSIZE (16 + 3) |
| 249 | bp_commbuf = malloc(DEFAULT_BUFSIZE); |
| 250 | if (!bp_commbuf) { |
| 251 | bp_commbufsize = 0; |
| 252 | msg_perr("Out of memory!\n"); |
Stefan Reinauer | 1838591 | 2014-04-26 16:12:45 +0000 | [diff] [blame] | 253 | free(dev); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 254 | return ERROR_OOM; |
| 255 | } |
| 256 | bp_commbufsize = DEFAULT_BUFSIZE; |
| 257 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 258 | ret = buspirate_serialport_setup(dev); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 259 | free(dev); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 260 | if (ret) { |
| 261 | bp_commbufsize = 0; |
| 262 | free(bp_commbuf); |
| 263 | bp_commbuf = NULL; |
| 264 | return ret; |
| 265 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 266 | |
Stefan Reinauer | 1838591 | 2014-04-26 16:12:45 +0000 | [diff] [blame] | 267 | if (register_shutdown(buspirate_spi_shutdown, NULL) != 0) { |
| 268 | bp_commbufsize = 0; |
| 269 | free(bp_commbuf); |
| 270 | bp_commbuf = NULL; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 271 | return 1; |
Stefan Reinauer | 1838591 | 2014-04-26 16:12:45 +0000 | [diff] [blame] | 272 | } |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 273 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 274 | /* This is the brute force version, but it should work. |
| 275 | * It is likely to fail if a previous flashrom run was aborted during a write with the new SPI commands |
| 276 | * in firmware v5.5 because that firmware may wait for up to 4096 bytes of input before responding to |
| 277 | * 0x00 again. The obvious workaround (sending 4096 bytes of \0) may cause significant startup delays. |
| 278 | */ |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 279 | for (i = 0; i < 20; i++) { |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 280 | /* Enter raw bitbang mode */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 281 | bp_commbuf[0] = 0x00; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 282 | /* Send the command, don't read the response. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 283 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 284 | if (ret) |
| 285 | return ret; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 286 | /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any |
| 287 | * response which came in over serial. Unfortunately that does not work reliably on Linux |
| 288 | * with FTDI USB-serial. |
| 289 | */ |
| 290 | //sp_flush_incoming(); |
| 291 | /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence |
| 292 | * of 0x00 too fast apparently triggers such an UART input buffer overflow. |
| 293 | */ |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 294 | internal_sleep(10000); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 295 | } |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 296 | /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */ |
| 297 | if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO"))) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 298 | return ret; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 299 | |
| 300 | /* Reset the Bus Pirate. */ |
| 301 | bp_commbuf[0] = 0x0f; |
| 302 | /* Send the command, don't read the response. */ |
| 303 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
| 304 | return ret; |
| 305 | if ((ret = buspirate_wait_for_string(bp_commbuf, "irate "))) |
| 306 | return ret; |
| 307 | /* Read the hardware version string. Last byte of the buffer is reserved for \0. */ |
| 308 | for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) { |
| 309 | if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1))) |
| 310 | return ret; |
| 311 | if (strchr("\r\n\t ", bp_commbuf[i])) |
| 312 | break; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 313 | } |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 314 | bp_commbuf[i] = '\0'; |
| 315 | msg_pdbg("Detected Bus Pirate hardware %s\n", bp_commbuf); |
| 316 | |
| 317 | if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware "))) |
| 318 | return ret; |
| 319 | /* Read the firmware version string. Last byte of the buffer is reserved for \0. */ |
| 320 | for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) { |
| 321 | if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1))) |
| 322 | return ret; |
| 323 | if (strchr("\r\n\t ", bp_commbuf[i])) |
| 324 | break; |
| 325 | } |
| 326 | bp_commbuf[i] = '\0'; |
| 327 | msg_pdbg("Detected Bus Pirate firmware "); |
| 328 | if (bp_commbuf[0] != 'v') |
| 329 | msg_pdbg("(unknown version number format)"); |
| 330 | else if (!strchr("0123456789", bp_commbuf[1])) |
| 331 | msg_pdbg("(unknown version number format)"); |
| 332 | else { |
| 333 | fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10); |
| 334 | while ((*tmp != '\0') && !strchr("0123456789", *tmp)) |
| 335 | tmp++; |
| 336 | fw_version_minor = strtoul(tmp, NULL, 10); |
| 337 | msg_pdbg("%u.%u", fw_version_major, fw_version_minor); |
| 338 | } |
| 339 | msg_pdbg2(" (\"%s\")", bp_commbuf); |
| 340 | msg_pdbg("\n"); |
| 341 | |
| 342 | if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>"))) |
| 343 | return ret; |
| 344 | |
| 345 | /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */ |
| 346 | if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) { |
| 347 | msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n"); |
| 348 | msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n"); |
| 349 | return SPI_PROGRAMMER_ERROR; |
| 350 | } |
| 351 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 352 | /* Use fast SPI mode in firmware 5.5 and newer. */ |
| 353 | if (BP_FWVERSION(fw_version_major, fw_version_minor) >= BP_FWVERSION(5, 5)) { |
| 354 | msg_pdbg("Using SPI command set v2.\n"); |
| 355 | /* Sensible default buffer size. */ |
| 356 | if (buspirate_commbuf_grow(260 + 5)) |
| 357 | return ERROR_OOM; |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 358 | spi_master_buspirate.max_data_read = 2048; |
| 359 | spi_master_buspirate.max_data_write = 256; |
| 360 | spi_master_buspirate.command = buspirate_spi_send_command_v2; |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 361 | } else { |
| 362 | msg_pinfo("Bus Pirate firmware 5.4 and older does not support fast SPI access.\n"); |
| 363 | msg_pinfo("Reading/writing a flash chip may take hours.\n"); |
| 364 | msg_pinfo("It is recommended to upgrade to firmware 5.5 or newer.\n"); |
| 365 | /* Sensible default buffer size. */ |
| 366 | if (buspirate_commbuf_grow(16 + 3)) |
| 367 | return ERROR_OOM; |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 368 | spi_master_buspirate.max_data_read = 12; |
| 369 | spi_master_buspirate.max_data_write = 12; |
| 370 | spi_master_buspirate.command = buspirate_spi_send_command_v1; |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 373 | /* Workaround for broken speed settings in firmware 6.1 and older. */ |
| 374 | if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2)) |
| 375 | if (spispeed > 0x4) { |
| 376 | msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. " |
| 377 | "Limiting speed to 2 MHz.\n"); |
| 378 | msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n"); |
| 379 | spispeed = 0x4; |
| 380 | } |
| 381 | |
| 382 | /* This works because speeds numbering starts at 0 and is contiguous. */ |
| 383 | msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name); |
| 384 | |
| 385 | /* Enter raw bitbang mode */ |
| 386 | for (i = 0; i < 20; i++) { |
| 387 | bp_commbuf[0] = 0x00; |
| 388 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
| 389 | return ret; |
| 390 | } |
| 391 | if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO"))) |
| 392 | return ret; |
| 393 | if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1))) |
| 394 | return ret; |
| 395 | msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]); |
| 396 | if (bp_commbuf[0] != '1') { |
| 397 | msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 398 | return 1; |
| 399 | } |
| 400 | /* Enter raw SPI mode */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 401 | bp_commbuf[0] = 0x01; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 402 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
| 403 | if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI"))) |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 404 | return ret; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 405 | if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1))) |
| 406 | return ret; |
| 407 | msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]); |
| 408 | if (bp_commbuf[0] != '1') { |
| 409 | msg_perr("Can't handle raw SPI mode version %c!\n", bp_commbuf[0]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 410 | return 1; |
| 411 | } |
| 412 | |
| 413 | /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */ |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 414 | bp_commbuf[0] = 0x40 | 0x0b; |
| 415 | if (pullup == 1) { |
| 416 | bp_commbuf[0] |= (1 << 2); |
| 417 | msg_pdbg("Enabling pull-up resistors.\n"); |
| 418 | } |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 419 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 420 | if (ret) |
| 421 | return 1; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 422 | if (bp_commbuf[0] != 0x01) { |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 423 | msg_perr("Protocol error while setting power/CS/AUX(/Pull-up resistors)!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 424 | return 1; |
| 425 | } |
| 426 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 427 | /* Set SPI speed */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 428 | bp_commbuf[0] = 0x60 | spispeed; |
| 429 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 430 | if (ret) |
| 431 | return 1; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 432 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 433 | msg_perr("Protocol error while setting SPI speed!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 434 | return 1; |
| 435 | } |
| 436 | |
| 437 | /* Set SPI config: output type, idle, clock edge, sample */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 438 | bp_commbuf[0] = 0x80 | 0xa; |
| 439 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 440 | if (ret) |
| 441 | return 1; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 442 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 443 | msg_perr("Protocol error while setting SPI config!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | /* De-assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 448 | bp_commbuf[0] = 0x03; |
| 449 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 450 | if (ret) |
| 451 | return 1; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 452 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 453 | msg_perr("Protocol error while raising CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 454 | return 1; |
| 455 | } |
| 456 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 457 | register_spi_master(&spi_master_buspirate); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 462 | static int buspirate_spi_send_command_v1(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
| 463 | const unsigned char *writearr, unsigned char *readarr) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 464 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 465 | unsigned int i = 0; |
| 466 | int ret = 0; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 467 | |
| 468 | if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16) |
| 469 | return SPI_INVALID_LENGTH; |
| 470 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 471 | /* 3 bytes extra for CS#, len, CS#. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 472 | if (buspirate_commbuf_grow(writecnt + readcnt + 3)) |
| 473 | return ERROR_OOM; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 474 | |
| 475 | /* Assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 476 | bp_commbuf[i++] = 0x02; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 477 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 478 | bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1); |
| 479 | memcpy(bp_commbuf + i, writearr, writecnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 480 | i += writecnt; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 481 | memset(bp_commbuf + i, 0, readcnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 482 | |
| 483 | i += readcnt; |
| 484 | /* De-assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 485 | bp_commbuf[i++] = 0x03; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 486 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 487 | ret = buspirate_sendrecv(bp_commbuf, i, i); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 488 | |
| 489 | if (ret) { |
| 490 | msg_perr("Bus Pirate communication error!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 491 | return SPI_GENERIC_ERROR; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 494 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 495 | msg_perr("Protocol error while lowering CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 496 | return SPI_GENERIC_ERROR; |
| 497 | } |
| 498 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 499 | if (bp_commbuf[1] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 500 | msg_perr("Protocol error while reading/writing SPI!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 501 | return SPI_GENERIC_ERROR; |
| 502 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 503 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 504 | if (bp_commbuf[i - 1] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 505 | msg_perr("Protocol error while raising CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 506 | return SPI_GENERIC_ERROR; |
| 507 | } |
| 508 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 509 | /* Skip CS#, length, writearr. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 510 | memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 511 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 512 | return ret; |
| 513 | } |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 514 | |
| 515 | static int buspirate_spi_send_command_v2(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
| 516 | const unsigned char *writearr, unsigned char *readarr) |
| 517 | { |
| 518 | int i = 0, ret = 0; |
| 519 | |
| 520 | if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096) |
| 521 | return SPI_INVALID_LENGTH; |
| 522 | |
| 523 | /* 5 bytes extra for command, writelen, readlen. |
| 524 | * 1 byte extra for Ack/Nack. |
| 525 | */ |
| 526 | if (buspirate_commbuf_grow(max(writecnt + 5, readcnt + 1))) |
| 527 | return ERROR_OOM; |
| 528 | |
| 529 | /* Combined SPI write/read. */ |
| 530 | bp_commbuf[i++] = 0x04; |
| 531 | bp_commbuf[i++] = (writecnt >> 8) & 0xff; |
| 532 | bp_commbuf[i++] = writecnt & 0xff; |
| 533 | bp_commbuf[i++] = (readcnt >> 8) & 0xff; |
| 534 | bp_commbuf[i++] = readcnt & 0xff; |
| 535 | memcpy(bp_commbuf + i, writearr, writecnt); |
| 536 | |
| 537 | ret = buspirate_sendrecv(bp_commbuf, i + writecnt, 1 + readcnt); |
| 538 | |
| 539 | if (ret) { |
| 540 | msg_perr("Bus Pirate communication error!\n"); |
| 541 | return SPI_GENERIC_ERROR; |
| 542 | } |
| 543 | |
| 544 | if (bp_commbuf[0] != 0x01) { |
| 545 | msg_perr("Protocol error while sending SPI write/read!\n"); |
| 546 | return SPI_GENERIC_ERROR; |
| 547 | } |
| 548 | |
| 549 | /* Skip Ack. */ |
| 550 | memcpy(readarr, bp_commbuf + 1, readcnt); |
| 551 | |
| 552 | return ret; |
| 553 | } |