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