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); |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 43 | if (sp_fd < 0) |
| 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 | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 137 | static struct spi_programmer spi_programmer_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 | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 204 | char *dev = NULL; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 205 | char *speed = NULL; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 206 | char *tmp; |
| 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; |
| 211 | int i; |
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 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 223 | speed = extract_programmer_param("spispeed"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 224 | if (speed) { |
| 225 | for (i = 0; spispeeds[i].name; i++) |
| 226 | if (!strncasecmp(spispeeds[i].name, speed, |
| 227 | strlen(spispeeds[i].name))) { |
| 228 | spispeed = spispeeds[i].speed; |
| 229 | break; |
| 230 | } |
| 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 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 234 | free(speed); |
| 235 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 236 | /* Default buffer size is 19: 16 bytes data, 3 bytes control. */ |
| 237 | #define DEFAULT_BUFSIZE (16 + 3) |
| 238 | bp_commbuf = malloc(DEFAULT_BUFSIZE); |
| 239 | if (!bp_commbuf) { |
| 240 | bp_commbufsize = 0; |
| 241 | msg_perr("Out of memory!\n"); |
| 242 | return ERROR_OOM; |
| 243 | } |
| 244 | bp_commbufsize = DEFAULT_BUFSIZE; |
| 245 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 246 | ret = buspirate_serialport_setup(dev); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 247 | free(dev); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 248 | if (ret) { |
| 249 | bp_commbufsize = 0; |
| 250 | free(bp_commbuf); |
| 251 | bp_commbuf = NULL; |
| 252 | return ret; |
| 253 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 254 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 255 | if (register_shutdown(buspirate_spi_shutdown, NULL)) |
| 256 | return 1; |
| 257 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 258 | /* This is the brute force version, but it should work. |
| 259 | * It is likely to fail if a previous flashrom run was aborted during a write with the new SPI commands |
| 260 | * in firmware v5.5 because that firmware may wait for up to 4096 bytes of input before responding to |
| 261 | * 0x00 again. The obvious workaround (sending 4096 bytes of \0) may cause significant startup delays. |
| 262 | */ |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 263 | for (i = 0; i < 20; i++) { |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 264 | /* Enter raw bitbang mode */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 265 | bp_commbuf[0] = 0x00; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 266 | /* Send the command, don't read the response. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 267 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 268 | if (ret) |
| 269 | return ret; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 270 | /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any |
| 271 | * response which came in over serial. Unfortunately that does not work reliably on Linux |
| 272 | * with FTDI USB-serial. |
| 273 | */ |
| 274 | //sp_flush_incoming(); |
| 275 | /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence |
| 276 | * of 0x00 too fast apparently triggers such an UART input buffer overflow. |
| 277 | */ |
| 278 | usleep(10000); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 279 | } |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 280 | /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */ |
| 281 | if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO"))) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 282 | return ret; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 283 | |
| 284 | /* Reset the Bus Pirate. */ |
| 285 | bp_commbuf[0] = 0x0f; |
| 286 | /* Send the command, don't read the response. */ |
| 287 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
| 288 | return ret; |
| 289 | if ((ret = buspirate_wait_for_string(bp_commbuf, "irate "))) |
| 290 | return ret; |
| 291 | /* Read the hardware version string. Last byte of the buffer is reserved for \0. */ |
| 292 | for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) { |
| 293 | if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1))) |
| 294 | return ret; |
| 295 | if (strchr("\r\n\t ", bp_commbuf[i])) |
| 296 | break; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 297 | } |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 298 | bp_commbuf[i] = '\0'; |
| 299 | msg_pdbg("Detected Bus Pirate hardware %s\n", bp_commbuf); |
| 300 | |
| 301 | if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware "))) |
| 302 | return ret; |
| 303 | /* Read the firmware version string. Last byte of the buffer is reserved for \0. */ |
| 304 | for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) { |
| 305 | if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1))) |
| 306 | return ret; |
| 307 | if (strchr("\r\n\t ", bp_commbuf[i])) |
| 308 | break; |
| 309 | } |
| 310 | bp_commbuf[i] = '\0'; |
| 311 | msg_pdbg("Detected Bus Pirate firmware "); |
| 312 | if (bp_commbuf[0] != 'v') |
| 313 | msg_pdbg("(unknown version number format)"); |
| 314 | else if (!strchr("0123456789", bp_commbuf[1])) |
| 315 | msg_pdbg("(unknown version number format)"); |
| 316 | else { |
| 317 | fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10); |
| 318 | while ((*tmp != '\0') && !strchr("0123456789", *tmp)) |
| 319 | tmp++; |
| 320 | fw_version_minor = strtoul(tmp, NULL, 10); |
| 321 | msg_pdbg("%u.%u", fw_version_major, fw_version_minor); |
| 322 | } |
| 323 | msg_pdbg2(" (\"%s\")", bp_commbuf); |
| 324 | msg_pdbg("\n"); |
| 325 | |
| 326 | if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>"))) |
| 327 | return ret; |
| 328 | |
| 329 | /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */ |
| 330 | if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) { |
| 331 | msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n"); |
| 332 | msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n"); |
| 333 | return SPI_PROGRAMMER_ERROR; |
| 334 | } |
| 335 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 336 | /* Use fast SPI mode in firmware 5.5 and newer. */ |
| 337 | if (BP_FWVERSION(fw_version_major, fw_version_minor) >= BP_FWVERSION(5, 5)) { |
| 338 | msg_pdbg("Using SPI command set v2.\n"); |
| 339 | /* Sensible default buffer size. */ |
| 340 | if (buspirate_commbuf_grow(260 + 5)) |
| 341 | return ERROR_OOM; |
| 342 | spi_programmer_buspirate.max_data_read = 2048; |
| 343 | spi_programmer_buspirate.max_data_write = 256; |
| 344 | spi_programmer_buspirate.command = buspirate_spi_send_command_v2; |
| 345 | } else { |
| 346 | msg_pinfo("Bus Pirate firmware 5.4 and older does not support fast SPI access.\n"); |
| 347 | msg_pinfo("Reading/writing a flash chip may take hours.\n"); |
| 348 | msg_pinfo("It is recommended to upgrade to firmware 5.5 or newer.\n"); |
| 349 | /* Sensible default buffer size. */ |
| 350 | if (buspirate_commbuf_grow(16 + 3)) |
| 351 | return ERROR_OOM; |
| 352 | spi_programmer_buspirate.max_data_read = 12; |
| 353 | spi_programmer_buspirate.max_data_write = 12; |
| 354 | spi_programmer_buspirate.command = buspirate_spi_send_command_v1; |
| 355 | } |
| 356 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 357 | /* Workaround for broken speed settings in firmware 6.1 and older. */ |
| 358 | if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2)) |
| 359 | if (spispeed > 0x4) { |
| 360 | msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. " |
| 361 | "Limiting speed to 2 MHz.\n"); |
| 362 | msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n"); |
| 363 | spispeed = 0x4; |
| 364 | } |
| 365 | |
| 366 | /* This works because speeds numbering starts at 0 and is contiguous. */ |
| 367 | msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name); |
| 368 | |
| 369 | /* Enter raw bitbang mode */ |
| 370 | for (i = 0; i < 20; i++) { |
| 371 | bp_commbuf[0] = 0x00; |
| 372 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
| 373 | return ret; |
| 374 | } |
| 375 | if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO"))) |
| 376 | return ret; |
| 377 | if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1))) |
| 378 | return ret; |
| 379 | msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]); |
| 380 | if (bp_commbuf[0] != '1') { |
| 381 | 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] | 382 | return 1; |
| 383 | } |
| 384 | /* Enter raw SPI mode */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 385 | bp_commbuf[0] = 0x01; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 386 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
| 387 | if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI"))) |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 388 | return ret; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 389 | if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1))) |
| 390 | return ret; |
| 391 | msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]); |
| 392 | if (bp_commbuf[0] != '1') { |
| 393 | 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] | 394 | return 1; |
| 395 | } |
| 396 | |
| 397 | /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 398 | bp_commbuf[0] = 0x40 | 0xb; |
| 399 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 400 | if (ret) |
| 401 | return 1; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 402 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 403 | msg_perr("Protocol error while setting power/CS/AUX!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 404 | return 1; |
| 405 | } |
| 406 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 407 | /* Set SPI speed */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 408 | bp_commbuf[0] = 0x60 | spispeed; |
| 409 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 410 | if (ret) |
| 411 | return 1; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 412 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 413 | msg_perr("Protocol error while setting SPI speed!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 414 | return 1; |
| 415 | } |
| 416 | |
| 417 | /* Set SPI config: output type, idle, clock edge, sample */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 418 | bp_commbuf[0] = 0x80 | 0xa; |
| 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) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 423 | msg_perr("Protocol error while setting SPI config!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 424 | return 1; |
| 425 | } |
| 426 | |
| 427 | /* De-assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 428 | bp_commbuf[0] = 0x03; |
| 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 raising CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 434 | return 1; |
| 435 | } |
| 436 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 437 | register_spi_programmer(&spi_programmer_buspirate); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 442 | static int buspirate_spi_send_command_v1(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
| 443 | const unsigned char *writearr, unsigned char *readarr) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 444 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 445 | unsigned int i = 0; |
| 446 | int ret = 0; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 447 | |
| 448 | if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16) |
| 449 | return SPI_INVALID_LENGTH; |
| 450 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 451 | /* 3 bytes extra for CS#, len, CS#. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 452 | if (buspirate_commbuf_grow(writecnt + readcnt + 3)) |
| 453 | return ERROR_OOM; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 454 | |
| 455 | /* Assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 456 | bp_commbuf[i++] = 0x02; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 457 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 458 | bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1); |
| 459 | memcpy(bp_commbuf + i, writearr, writecnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 460 | i += writecnt; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 461 | memset(bp_commbuf + i, 0, readcnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 462 | |
| 463 | i += readcnt; |
| 464 | /* De-assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 465 | bp_commbuf[i++] = 0x03; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 466 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 467 | ret = buspirate_sendrecv(bp_commbuf, i, i); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 468 | |
| 469 | if (ret) { |
| 470 | msg_perr("Bus Pirate communication error!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 471 | return SPI_GENERIC_ERROR; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 474 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 475 | msg_perr("Protocol error while lowering CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 476 | return SPI_GENERIC_ERROR; |
| 477 | } |
| 478 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 479 | if (bp_commbuf[1] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 480 | msg_perr("Protocol error while reading/writing SPI!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 481 | return SPI_GENERIC_ERROR; |
| 482 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 483 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 484 | if (bp_commbuf[i - 1] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 485 | msg_perr("Protocol error while raising CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 486 | return SPI_GENERIC_ERROR; |
| 487 | } |
| 488 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 489 | /* Skip CS#, length, writearr. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 490 | memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 491 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 492 | return ret; |
| 493 | } |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 494 | |
| 495 | static int buspirate_spi_send_command_v2(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
| 496 | const unsigned char *writearr, unsigned char *readarr) |
| 497 | { |
| 498 | int i = 0, ret = 0; |
| 499 | |
| 500 | if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096) |
| 501 | return SPI_INVALID_LENGTH; |
| 502 | |
| 503 | /* 5 bytes extra for command, writelen, readlen. |
| 504 | * 1 byte extra for Ack/Nack. |
| 505 | */ |
| 506 | if (buspirate_commbuf_grow(max(writecnt + 5, readcnt + 1))) |
| 507 | return ERROR_OOM; |
| 508 | |
| 509 | /* Combined SPI write/read. */ |
| 510 | bp_commbuf[i++] = 0x04; |
| 511 | bp_commbuf[i++] = (writecnt >> 8) & 0xff; |
| 512 | bp_commbuf[i++] = writecnt & 0xff; |
| 513 | bp_commbuf[i++] = (readcnt >> 8) & 0xff; |
| 514 | bp_commbuf[i++] = readcnt & 0xff; |
| 515 | memcpy(bp_commbuf + i, writearr, writecnt); |
| 516 | |
| 517 | ret = buspirate_sendrecv(bp_commbuf, i + writecnt, 1 + readcnt); |
| 518 | |
| 519 | if (ret) { |
| 520 | msg_perr("Bus Pirate communication error!\n"); |
| 521 | return SPI_GENERIC_ERROR; |
| 522 | } |
| 523 | |
| 524 | if (bp_commbuf[0] != 0x01) { |
| 525 | msg_perr("Protocol error while sending SPI write/read!\n"); |
| 526 | return SPI_GENERIC_ERROR; |
| 527 | } |
| 528 | |
| 529 | /* Skip Ack. */ |
| 530 | memcpy(readarr, bp_commbuf + 1, readcnt); |
| 531 | |
| 532 | return ret; |
| 533 | } |