blob: f8254155277f8058f01caa7aa360a6b84b59eb3b [file] [log] [blame]
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +00004 * Copyright (C) 2009, 2010, 2011, 2012 Carl-Daniel Hailfinger
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +00005 *
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 Hailfinger1c6d2ff2012-08-27 00:44:42 +000021#include <strings.h>
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000022#include <string.h>
23#include <stdlib.h>
24#include <ctype.h>
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +000025#include <unistd.h>
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000026#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000027#include "programmer.h"
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000028#include "spi.h"
29
30/* Change this to #define if you want to test without a serial implementation */
31#undef FAKE_COMMUNICATION
32
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -060033struct buspirate_speeds {
Carl-Daniel Hailfingerc4224842011-06-09 20:06:34 +000034 const char *name;
35 const int speed;
36};
37
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -060038#define BP_DEFAULTBAUD 115200
39
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000040#ifndef FAKE_COMMUNICATION
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000041static int buspirate_serialport_setup(char *dev)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000042{
43 /* 115200bps, 8 databits, no parity, 1 stopbit */
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -060044 sp_fd = sp_openserport(dev, BP_DEFAULTBAUD);
Stefan Tauneracfc4c62012-11-30 16:46:45 +000045 if (sp_fd == SER_INV_FD)
Niklas Söderlund2a95e872012-07-30 19:42:33 +000046 return 1;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000047 return 0;
48}
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000049#else
50#define buspirate_serialport_setup(...) 0
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +000051#define serialport_shutdown(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000052#define serialport_write(...) 0
53#define serialport_read(...) 0
Patrick Georgi3b6237d2010-01-06 19:09:40 +000054#define sp_flush_incoming(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000055#endif
56
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +000057static unsigned char *bp_commbuf = NULL;
58static int bp_commbufsize = 0;
59
60static int buspirate_commbuf_grow(int bufsize)
61{
62 unsigned char *tmpbuf;
63
64 /* Never shrink. realloc() calls are expensive. */
65 if (bufsize <= bp_commbufsize)
66 return 0;
67
68 tmpbuf = realloc(bp_commbuf, bufsize);
69 if (!tmpbuf) {
70 /* Keep the existing buffer because memory is already tight. */
71 msg_perr("Out of memory!\n");
72 return ERROR_OOM;
73 }
74
75 bp_commbuf = tmpbuf;
76 bp_commbufsize = bufsize;
77 return 0;
78}
79
Uwe Hermann91f4afa2011-07-28 08:13:25 +000080static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt,
81 unsigned int readcnt)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000082{
83 int i, ret = 0;
84
Sean Nelson84f7bce2010-01-09 23:56:41 +000085 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000086 if (!writecnt && !readcnt) {
Sean Nelson84f7bce2010-01-09 23:56:41 +000087 msg_perr("Zero length command!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000088 return 1;
89 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +000090 if (writecnt)
91 msg_pspew("Sending");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000092 for (i = 0; i < writecnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000093 msg_pspew(" 0x%02x", buf[i]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000094#ifdef FAKE_COMMUNICATION
95 /* Placate the caller for now. */
96 if (readcnt) {
97 buf[0] = 0x01;
98 memset(buf + 1, 0xff, readcnt - 1);
99 }
100 ret = 0;
101#else
102 if (writecnt)
103 ret = serialport_write(buf, writecnt);
104 if (ret)
105 return ret;
106 if (readcnt)
107 ret = serialport_read(buf, readcnt);
108 if (ret)
109 return ret;
110#endif
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000111 if (readcnt)
112 msg_pspew(", receiving");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000113 for (i = 0; i < readcnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000114 msg_pspew(" 0x%02x", buf[i]);
115 msg_pspew("\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000116 return 0;
117}
118
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000119static int buspirate_wait_for_string(unsigned char *buf, char *key)
120{
121 unsigned int keylen = strlen(key);
122 int ret;
123
124 ret = buspirate_sendrecv(buf, 0, keylen);
125 while (!ret) {
126 if (!memcmp(buf, key, keylen))
127 return 0;
128 memmove(buf, buf + 1, keylen - 1);
129 ret = buspirate_sendrecv(buf + keylen - 1, 0, 1);
130 }
131 return ret;
132}
133
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000134static int buspirate_spi_send_command_v1(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
135 const unsigned char *writearr, unsigned char *readarr);
136static int buspirate_spi_send_command_v2(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
137 const unsigned char *writearr, unsigned char *readarr);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000138
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000139static struct spi_master spi_master_buspirate = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000140 .type = SPI_CONTROLLER_BUSPIRATE,
Nico Huber1cf407b2017-11-10 20:18:23 +0100141 .features = SPI_MASTER_4BA,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000142 .max_data_read = MAX_DATA_UNSPECIFIED,
143 .max_data_write = MAX_DATA_UNSPECIFIED,
144 .command = NULL,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000145 .multicommand = default_spi_send_multicommand,
146 .read = default_spi_read,
147 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000148 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000149};
150
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600151static const struct buspirate_speeds spispeeds[] = {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000152 {"30k", 0x0},
153 {"125k", 0x1},
154 {"250k", 0x2},
155 {"1M", 0x3},
156 {"2M", 0x4},
157 {"2.6M", 0x5},
158 {"4M", 0x6},
159 {"8M", 0x7},
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600160 {NULL, 0x0}
161};
162
163static const struct buspirate_speeds serialspeeds[] = {
164 {"115200", 115200},
165 {"230400", 230400},
166 {"250000", 250000},
167 {"2000000", 2000000},
168 {"2M", 2000000},
169 {NULL, 0}
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000170};
171
David Hendricks8bb20212011-06-14 01:35:36 +0000172static int buspirate_spi_shutdown(void *data)
173{
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000174 int ret = 0, ret2 = 0;
175 /* No need to allocate a buffer here, we know that bp_commbuf is at least DEFAULT_BUFSIZE big. */
David Hendricks8bb20212011-06-14 01:35:36 +0000176
177 /* Exit raw SPI mode (enter raw bitbang mode) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000178 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000179 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000180 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000181 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000182 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000183 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
184 goto out_shutdown;
185 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
186 if (bp_commbuf[0] != '1') {
187 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000188 ret = 1;
189 goto out_shutdown;
David Hendricks8bb20212011-06-14 01:35:36 +0000190 }
191 /* Reset Bus Pirate (return to user terminal) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000192 bp_commbuf[0] = 0x0f;
193 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
David Hendricks8bb20212011-06-14 01:35:36 +0000194
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000195out_shutdown:
David Hendricks8bb20212011-06-14 01:35:36 +0000196 /* Shut down serial port communication */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000197 ret2 = serialport_shutdown(NULL);
198 /* Keep the oldest error, it is probably the best indicator. */
199 if (ret2 && !ret)
200 ret = ret2;
201 bp_commbufsize = 0;
202 free(bp_commbuf);
203 bp_commbuf = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000204 if (ret)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000205 msg_pdbg("Bus Pirate shutdown failed.\n");
206 else
207 msg_pdbg("Bus Pirate shutdown completed.\n");
David Hendricks8bb20212011-06-14 01:35:36 +0000208
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000209 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +0000210}
211
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000212#define BP_FWVERSION(a,b) ((a) << 8 | (b))
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600213#define BP_HWVERSION(a,b) BP_FWVERSION(a,b)
214
215/**
216 * The Bus Pirate's PIC microcontroller supports custom baud rates by manually specifying a
217 * clock divisor that can be calculated with the formula (16000000 / (4 * baud)) - 1.
218 */
219#define BP_DIVISOR(baud) ((4000000/(baud)) - 1)
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000220
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000221int buspirate_spi_init(void)
222{
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000223 char *tmp;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000224 char *dev;
225 int i;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600226 int cnt;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000227 unsigned int fw_version_major = 0;
228 unsigned int fw_version_minor = 0;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600229 unsigned int hw_version_major = 0;
230 unsigned int hw_version_minor = 0;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000231 int spispeed = 0x7;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600232 int serialspeed_index = -1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000233 int ret = 0;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000234 int pullup = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000235
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000236 dev = extract_programmer_param("dev");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000237 if (dev && !strlen(dev)) {
238 free(dev);
239 dev = NULL;
240 }
241 if (!dev) {
242 msg_perr("No serial device given. Use flashrom -p buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000243 return 1;
244 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000245
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000246 tmp = extract_programmer_param("spispeed");
247 if (tmp) {
248 for (i = 0; spispeeds[i].name; i++) {
249 if (!strncasecmp(spispeeds[i].name, tmp, strlen(spispeeds[i].name))) {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000250 spispeed = spispeeds[i].speed;
251 break;
252 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000253 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000254 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000255 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000256 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000257 free(tmp);
258
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600259 /* Extract serialspeed paramater */
260 tmp = extract_programmer_param("serialspeed");
261 if (tmp) {
262 for (i = 0; serialspeeds[i].name; i++) {
263 if (!strncasecmp(serialspeeds[i].name, tmp, strlen(serialspeeds[i].name))) {
264 serialspeed_index = i;
265 break;
266 }
267 }
268 if (!serialspeeds[i].name)
269 msg_perr("Invalid serial speed %s, using default.\n", tmp);
270 }
271 free(tmp);
272
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000273 tmp = extract_programmer_param("pullups");
274 if (tmp) {
275 if (strcasecmp("on", tmp) == 0)
276 pullup = 1;
277 else if (strcasecmp("off", tmp) == 0)
278 ; // ignore
279 else
280 msg_perr("Invalid pullups state, not using them.\n");
281 }
282 free(tmp);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000283
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000284 /* Default buffer size is 19: 16 bytes data, 3 bytes control. */
285#define DEFAULT_BUFSIZE (16 + 3)
286 bp_commbuf = malloc(DEFAULT_BUFSIZE);
287 if (!bp_commbuf) {
288 bp_commbufsize = 0;
289 msg_perr("Out of memory!\n");
Stefan Reinauer18385912014-04-26 16:12:45 +0000290 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000291 return ERROR_OOM;
292 }
293 bp_commbufsize = DEFAULT_BUFSIZE;
294
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000295 ret = buspirate_serialport_setup(dev);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000296 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000297 if (ret) {
298 bp_commbufsize = 0;
299 free(bp_commbuf);
300 bp_commbuf = NULL;
301 return ret;
302 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000303
Stefan Reinauer18385912014-04-26 16:12:45 +0000304 if (register_shutdown(buspirate_spi_shutdown, NULL) != 0) {
305 bp_commbufsize = 0;
306 free(bp_commbuf);
307 bp_commbuf = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000308 return 1;
Stefan Reinauer18385912014-04-26 16:12:45 +0000309 }
David Hendricks8bb20212011-06-14 01:35:36 +0000310
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000311 /* This is the brute force version, but it should work.
312 * It is likely to fail if a previous flashrom run was aborted during a write with the new SPI commands
313 * in firmware v5.5 because that firmware may wait for up to 4096 bytes of input before responding to
314 * 0x00 again. The obvious workaround (sending 4096 bytes of \0) may cause significant startup delays.
315 */
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000316 for (i = 0; i < 20; i++) {
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000317 /* Enter raw bitbang mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000318 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000319 /* Send the command, don't read the response. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000320 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000321 if (ret)
322 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000323 /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any
324 * response which came in over serial. Unfortunately that does not work reliably on Linux
325 * with FTDI USB-serial.
326 */
327 //sp_flush_incoming();
328 /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence
329 * of 0x00 too fast apparently triggers such an UART input buffer overflow.
330 */
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000331 internal_sleep(10000);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000332 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000333 /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */
334 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000335 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000336
337 /* Reset the Bus Pirate. */
338 bp_commbuf[0] = 0x0f;
339 /* Send the command, don't read the response. */
340 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
341 return ret;
342 if ((ret = buspirate_wait_for_string(bp_commbuf, "irate ")))
343 return ret;
344 /* Read the hardware version string. Last byte of the buffer is reserved for \0. */
345 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
346 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
347 return ret;
348 if (strchr("\r\n\t ", bp_commbuf[i]))
349 break;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000350 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000351 bp_commbuf[i] = '\0';
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600352 msg_pdbg("Detected Bus Pirate hardware ");
353 if (bp_commbuf[0] != 'v')
354 msg_pdbg("(unknown version number format)");
355 else if (!strchr("0123456789", bp_commbuf[1]))
356 msg_pdbg("(unknown version number format)");
357 else {
358 hw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
359 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
360 tmp++;
361 hw_version_minor = strtoul(tmp, NULL, 10);
362 msg_pdbg("%u.%u", hw_version_major, hw_version_minor);
363 }
364 msg_pdbg2(" (\"%s\")", bp_commbuf);
365 msg_pdbg("\n");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000366
367 if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware ")))
368 return ret;
369 /* Read the firmware version string. Last byte of the buffer is reserved for \0. */
370 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
371 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
372 return ret;
373 if (strchr("\r\n\t ", bp_commbuf[i]))
374 break;
375 }
376 bp_commbuf[i] = '\0';
377 msg_pdbg("Detected Bus Pirate firmware ");
378 if (bp_commbuf[0] != 'v')
379 msg_pdbg("(unknown version number format)");
380 else if (!strchr("0123456789", bp_commbuf[1]))
381 msg_pdbg("(unknown version number format)");
382 else {
383 fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
384 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
385 tmp++;
386 fw_version_minor = strtoul(tmp, NULL, 10);
387 msg_pdbg("%u.%u", fw_version_major, fw_version_minor);
388 }
389 msg_pdbg2(" (\"%s\")", bp_commbuf);
390 msg_pdbg("\n");
391
392 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
393 return ret;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600394
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000395 /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */
396 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) {
397 msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n");
398 msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n");
399 return SPI_PROGRAMMER_ERROR;
400 }
401
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000402 /* Use fast SPI mode in firmware 5.5 and newer. */
403 if (BP_FWVERSION(fw_version_major, fw_version_minor) >= BP_FWVERSION(5, 5)) {
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600404 msg_pdbg("Using SPI command set v2.\n");
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000405 /* Sensible default buffer size. */
406 if (buspirate_commbuf_grow(260 + 5))
407 return ERROR_OOM;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000408 spi_master_buspirate.max_data_read = 2048;
409 spi_master_buspirate.max_data_write = 256;
410 spi_master_buspirate.command = buspirate_spi_send_command_v2;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000411 } else {
412 msg_pinfo("Bus Pirate firmware 5.4 and older does not support fast SPI access.\n");
413 msg_pinfo("Reading/writing a flash chip may take hours.\n");
414 msg_pinfo("It is recommended to upgrade to firmware 5.5 or newer.\n");
415 /* Sensible default buffer size. */
416 if (buspirate_commbuf_grow(16 + 3))
417 return ERROR_OOM;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000418 spi_master_buspirate.max_data_read = 12;
419 spi_master_buspirate.max_data_write = 12;
420 spi_master_buspirate.command = buspirate_spi_send_command_v1;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000421 }
422
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000423 /* Workaround for broken speed settings in firmware 6.1 and older. */
424 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2))
425 if (spispeed > 0x4) {
426 msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. "
427 "Limiting speed to 2 MHz.\n");
428 msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n");
429 spispeed = 0x4;
430 }
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600431
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000432 /* This works because speeds numbering starts at 0 and is contiguous. */
433 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
434
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600435 /* Set 2M baud serial speed by default on hardware 3.0 and newer if a custom speed was not set */
436 if (serialspeed_index == -1 && BP_HWVERSION(hw_version_major, hw_version_minor) >= BP_HWVERSION(3, 0)) {
437 serialspeed_index = ARRAY_SIZE(serialspeeds) - 2;
438 msg_pdbg("Bus Pirate v3 or newer detected. Set serial speed to 2M baud.\n");
439 }
440
441 /* Set custom serial speed if specified */
442 if (serialspeed_index != -1) {
443 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(5, 5)) {
444 /* This feature requires firmware 5.5 or newer */
445 msg_perr("Bus Pirate firmware 5.4 and older does not support custom serial speeds."
446 "Using default speed of 115200 baud.\n");
447 } else if (serialspeeds[serialspeed_index].speed != BP_DEFAULTBAUD) {
448 /* Set the serial speed to match the user's choice if it doesn't already */
449
450 if (BP_HWVERSION(hw_version_major, hw_version_minor) < BP_HWVERSION(3, 0))
451 msg_pwarn("Increased serial speeds may not work on older (<3.0) Bus Pirates."
452 " Continue at your own risk.\n");
453
454 /* Enter baud rate configuration mode */
455 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "b\n");
456 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
457 return ret;
458 if ((ret = buspirate_wait_for_string(bp_commbuf, ">")))
459 return ret;
460
461 /* Enter manual clock divisor entry mode */
462 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "10\n");
463 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
464 return ret;
465 if ((ret = buspirate_wait_for_string(bp_commbuf, ">")))
466 return ret;
467
468 /* Set the clock divisor to the value calculated from the user's input */
469 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "%d\n",
470 BP_DIVISOR(serialspeeds[serialspeed_index].speed));
471
472 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
473 return ret;
474 sleep(1);
475
476 /* Reconfigure the host's serial baud rate to the new value */
477 if ((ret = serialport_config(sp_fd, serialspeeds[serialspeed_index].speed))) {
478 msg_perr("Unable to configure system baud rate to specified value.");
479 return ret;
480 }
481
482 /* Return to the main prompt */
483 bp_commbuf[0] = ' ';
484 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
485 return ret;
486 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
487 return ret;
488
489 msg_pdbg("Serial speed is %d baud\n", serialspeeds[serialspeed_index].speed);
490 }
491
492 }
493
494
495
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000496 /* Enter raw bitbang mode */
497 for (i = 0; i < 20; i++) {
498 bp_commbuf[0] = 0x00;
499 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
500 return ret;
501 }
502 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
503 return ret;
504 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
505 return ret;
506 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
507 if (bp_commbuf[0] != '1') {
508 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000509 return 1;
510 }
511 /* Enter raw SPI mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000512 bp_commbuf[0] = 0x01;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000513 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
514 if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI")))
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000515 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000516 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
517 return ret;
518 msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]);
519 if (bp_commbuf[0] != '1') {
520 msg_perr("Can't handle raw SPI mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000521 return 1;
522 }
523
524 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000525 bp_commbuf[0] = 0x40 | 0x0b;
526 if (pullup == 1) {
527 bp_commbuf[0] |= (1 << 2);
528 msg_pdbg("Enabling pull-up resistors.\n");
529 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000530 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000531 if (ret)
532 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000533 if (bp_commbuf[0] != 0x01) {
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000534 msg_perr("Protocol error while setting power/CS/AUX(/Pull-up resistors)!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000535 return 1;
536 }
537
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000538 /* Set SPI speed */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000539 bp_commbuf[0] = 0x60 | spispeed;
540 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000541 if (ret)
542 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000543 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000544 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000545 return 1;
546 }
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600547
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000548 /* Set SPI config: output type, idle, clock edge, sample */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000549 bp_commbuf[0] = 0x80 | 0xa;
550 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000551 if (ret)
552 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000553 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000554 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000555 return 1;
556 }
557
558 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000559 bp_commbuf[0] = 0x03;
560 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000561 if (ret)
562 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000563 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000564 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000565 return 1;
566 }
567
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000568 register_spi_master(&spi_master_buspirate);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000569
570 return 0;
571}
572
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000573static int buspirate_spi_send_command_v1(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
574 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000575{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000576 unsigned int i = 0;
577 int ret = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000578
579 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
580 return SPI_INVALID_LENGTH;
581
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000582 /* 3 bytes extra for CS#, len, CS#. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000583 if (buspirate_commbuf_grow(writecnt + readcnt + 3))
584 return ERROR_OOM;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000585
586 /* Assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000587 bp_commbuf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000588
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000589 bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1);
590 memcpy(bp_commbuf + i, writearr, writecnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000591 i += writecnt;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000592 memset(bp_commbuf + i, 0, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000593
594 i += readcnt;
595 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000596 bp_commbuf[i++] = 0x03;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000597
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000598 ret = buspirate_sendrecv(bp_commbuf, i, i);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000599
600 if (ret) {
601 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000602 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000603 }
604
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000605 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000606 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000607 return SPI_GENERIC_ERROR;
608 }
609
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000610 if (bp_commbuf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000611 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000612 return SPI_GENERIC_ERROR;
613 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000614
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000615 if (bp_commbuf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000616 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000617 return SPI_GENERIC_ERROR;
618 }
619
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000620 /* Skip CS#, length, writearr. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000621 memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000622
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000623 return ret;
624}
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000625
626static int buspirate_spi_send_command_v2(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
627 const unsigned char *writearr, unsigned char *readarr)
628{
629 int i = 0, ret = 0;
630
631 if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096)
632 return SPI_INVALID_LENGTH;
633
634 /* 5 bytes extra for command, writelen, readlen.
635 * 1 byte extra for Ack/Nack.
636 */
637 if (buspirate_commbuf_grow(max(writecnt + 5, readcnt + 1)))
638 return ERROR_OOM;
639
640 /* Combined SPI write/read. */
641 bp_commbuf[i++] = 0x04;
642 bp_commbuf[i++] = (writecnt >> 8) & 0xff;
643 bp_commbuf[i++] = writecnt & 0xff;
644 bp_commbuf[i++] = (readcnt >> 8) & 0xff;
645 bp_commbuf[i++] = readcnt & 0xff;
646 memcpy(bp_commbuf + i, writearr, writecnt);
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600647
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000648 ret = buspirate_sendrecv(bp_commbuf, i + writecnt, 1 + readcnt);
649
650 if (ret) {
651 msg_perr("Bus Pirate communication error!\n");
652 return SPI_GENERIC_ERROR;
653 }
654
655 if (bp_commbuf[0] != 0x01) {
656 msg_perr("Protocol error while sending SPI write/read!\n");
657 return SPI_GENERIC_ERROR;
658 }
659
660 /* Skip Ack. */
661 memcpy(readarr, bp_commbuf + 1, readcnt);
662
663 return ret;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600664}