blob: fb066c2a6a82910dc12600c9edb77379ce1a3dd4 [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.
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000014 */
15
16#include <stdio.h>
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000017#include <strings.h>
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000018#include <string.h>
19#include <stdlib.h>
20#include <ctype.h>
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +000021#include <unistd.h>
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000022#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000023#include "programmer.h"
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000024#include "spi.h"
25
26/* Change this to #define if you want to test without a serial implementation */
27#undef FAKE_COMMUNICATION
28
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -060029struct buspirate_speeds {
Carl-Daniel Hailfingerc4224842011-06-09 20:06:34 +000030 const char *name;
31 const int speed;
32};
33
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -060034#define BP_DEFAULTBAUD 115200
35
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000036#ifndef FAKE_COMMUNICATION
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000037static int buspirate_serialport_setup(char *dev)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000038{
39 /* 115200bps, 8 databits, no parity, 1 stopbit */
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -060040 sp_fd = sp_openserport(dev, BP_DEFAULTBAUD);
Elyes HAOUAS124ef382018-03-27 12:15:09 +020041 if (sp_fd == SER_INV_FD)
Niklas Söderlund2a95e872012-07-30 19:42:33 +000042 return 1;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000043 return 0;
44}
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000045#else
46#define buspirate_serialport_setup(...) 0
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +000047#define serialport_shutdown(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000048#define serialport_write(...) 0
49#define serialport_read(...) 0
Patrick Georgi3b6237d2010-01-06 19:09:40 +000050#define sp_flush_incoming(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000051#endif
52
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +000053static unsigned char *bp_commbuf = NULL;
54static int bp_commbufsize = 0;
55
56static int buspirate_commbuf_grow(int bufsize)
57{
58 unsigned char *tmpbuf;
59
60 /* Never shrink. realloc() calls are expensive. */
61 if (bufsize <= bp_commbufsize)
62 return 0;
63
64 tmpbuf = realloc(bp_commbuf, bufsize);
65 if (!tmpbuf) {
66 /* Keep the existing buffer because memory is already tight. */
67 msg_perr("Out of memory!\n");
68 return ERROR_OOM;
69 }
70
71 bp_commbuf = tmpbuf;
72 bp_commbufsize = bufsize;
73 return 0;
74}
75
Uwe Hermann91f4afa2011-07-28 08:13:25 +000076static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt,
77 unsigned int readcnt)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000078{
79 int i, ret = 0;
80
Sean Nelson84f7bce2010-01-09 23:56:41 +000081 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000082 if (!writecnt && !readcnt) {
Sean Nelson84f7bce2010-01-09 23:56:41 +000083 msg_perr("Zero length command!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000084 return 1;
85 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +000086 if (writecnt)
87 msg_pspew("Sending");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000088 for (i = 0; i < writecnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000089 msg_pspew(" 0x%02x", buf[i]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000090#ifdef FAKE_COMMUNICATION
91 /* Placate the caller for now. */
92 if (readcnt) {
93 buf[0] = 0x01;
94 memset(buf + 1, 0xff, readcnt - 1);
95 }
96 ret = 0;
97#else
98 if (writecnt)
99 ret = serialport_write(buf, writecnt);
100 if (ret)
101 return ret;
102 if (readcnt)
103 ret = serialport_read(buf, readcnt);
104 if (ret)
105 return ret;
106#endif
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000107 if (readcnt)
108 msg_pspew(", receiving");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000109 for (i = 0; i < readcnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000110 msg_pspew(" 0x%02x", buf[i]);
111 msg_pspew("\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000112 return 0;
113}
114
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000115static int buspirate_wait_for_string(unsigned char *buf, char *key)
116{
117 unsigned int keylen = strlen(key);
118 int ret;
119
120 ret = buspirate_sendrecv(buf, 0, keylen);
121 while (!ret) {
122 if (!memcmp(buf, key, keylen))
123 return 0;
124 memmove(buf, buf + 1, keylen - 1);
125 ret = buspirate_sendrecv(buf + keylen - 1, 0, 1);
126 }
127 return ret;
128}
129
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000130static int buspirate_spi_send_command_v1(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
131 const unsigned char *writearr, unsigned char *readarr);
132static int buspirate_spi_send_command_v2(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
133 const unsigned char *writearr, unsigned char *readarr);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000134
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000135static struct spi_master spi_master_buspirate = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100136 .features = SPI_MASTER_4BA,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000137 .max_data_read = MAX_DATA_UNSPECIFIED,
138 .max_data_write = MAX_DATA_UNSPECIFIED,
139 .command = NULL,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000140 .multicommand = default_spi_send_multicommand,
141 .read = default_spi_read,
142 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000143 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000144};
145
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600146static const struct buspirate_speeds spispeeds[] = {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000147 {"30k", 0x0},
148 {"125k", 0x1},
149 {"250k", 0x2},
150 {"1M", 0x3},
151 {"2M", 0x4},
152 {"2.6M", 0x5},
153 {"4M", 0x6},
154 {"8M", 0x7},
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600155 {NULL, 0x0}
156};
157
158static const struct buspirate_speeds serialspeeds[] = {
159 {"115200", 115200},
160 {"230400", 230400},
161 {"250000", 250000},
162 {"2000000", 2000000},
163 {"2M", 2000000},
164 {NULL, 0}
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000165};
166
David Hendricks8bb20212011-06-14 01:35:36 +0000167static int buspirate_spi_shutdown(void *data)
168{
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000169 int ret = 0, ret2 = 0;
170 /* 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 +0000171
172 /* Exit raw SPI mode (enter raw bitbang mode) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000173 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000174 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000175 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000176 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000177 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000178 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
179 goto out_shutdown;
180 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
181 if (bp_commbuf[0] != '1') {
182 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000183 ret = 1;
184 goto out_shutdown;
David Hendricks8bb20212011-06-14 01:35:36 +0000185 }
186 /* Reset Bus Pirate (return to user terminal) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000187 bp_commbuf[0] = 0x0f;
188 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
David Hendricks8bb20212011-06-14 01:35:36 +0000189
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000190out_shutdown:
David Hendricks8bb20212011-06-14 01:35:36 +0000191 /* Shut down serial port communication */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000192 ret2 = serialport_shutdown(NULL);
193 /* Keep the oldest error, it is probably the best indicator. */
194 if (ret2 && !ret)
195 ret = ret2;
196 bp_commbufsize = 0;
197 free(bp_commbuf);
198 bp_commbuf = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000199 if (ret)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000200 msg_pdbg("Bus Pirate shutdown failed.\n");
201 else
202 msg_pdbg("Bus Pirate shutdown completed.\n");
David Hendricks8bb20212011-06-14 01:35:36 +0000203
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000204 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +0000205}
206
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000207#define BP_FWVERSION(a,b) ((a) << 8 | (b))
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600208#define BP_HWVERSION(a,b) BP_FWVERSION(a,b)
209
210/**
211 * The Bus Pirate's PIC microcontroller supports custom baud rates by manually specifying a
212 * clock divisor that can be calculated with the formula (16000000 / (4 * baud)) - 1.
213 */
214#define BP_DIVISOR(baud) ((4000000/(baud)) - 1)
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000215
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000216int buspirate_spi_init(void)
217{
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000218 char *tmp;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000219 char *dev;
220 int i;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600221 int cnt;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000222 unsigned int fw_version_major = 0;
223 unsigned int fw_version_minor = 0;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600224 unsigned int hw_version_major = 0;
225 unsigned int hw_version_minor = 0;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000226 int spispeed = 0x7;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600227 int serialspeed_index = -1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000228 int ret = 0;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000229 int pullup = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000230
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000231 dev = extract_programmer_param("dev");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000232 if (dev && !strlen(dev)) {
233 free(dev);
234 dev = NULL;
235 }
236 if (!dev) {
237 msg_perr("No serial device given. Use flashrom -p buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000238 return 1;
239 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000240
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000241 tmp = extract_programmer_param("spispeed");
242 if (tmp) {
243 for (i = 0; spispeeds[i].name; i++) {
244 if (!strncasecmp(spispeeds[i].name, tmp, strlen(spispeeds[i].name))) {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000245 spispeed = spispeeds[i].speed;
246 break;
247 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000248 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000249 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000250 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000251 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000252 free(tmp);
253
Elyes HAOUASe2c90c42018-08-18 09:04:41 +0200254 /* Extract serialspeed parameter */
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600255 tmp = extract_programmer_param("serialspeed");
256 if (tmp) {
257 for (i = 0; serialspeeds[i].name; i++) {
258 if (!strncasecmp(serialspeeds[i].name, tmp, strlen(serialspeeds[i].name))) {
259 serialspeed_index = i;
260 break;
261 }
262 }
263 if (!serialspeeds[i].name)
264 msg_perr("Invalid serial speed %s, using default.\n", tmp);
265 }
266 free(tmp);
267
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000268 tmp = extract_programmer_param("pullups");
269 if (tmp) {
270 if (strcasecmp("on", tmp) == 0)
271 pullup = 1;
272 else if (strcasecmp("off", tmp) == 0)
273 ; // ignore
274 else
275 msg_perr("Invalid pullups state, not using them.\n");
276 }
277 free(tmp);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000278
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000279 /* Default buffer size is 19: 16 bytes data, 3 bytes control. */
280#define DEFAULT_BUFSIZE (16 + 3)
281 bp_commbuf = malloc(DEFAULT_BUFSIZE);
282 if (!bp_commbuf) {
283 bp_commbufsize = 0;
284 msg_perr("Out of memory!\n");
Stefan Reinauer18385912014-04-26 16:12:45 +0000285 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000286 return ERROR_OOM;
287 }
288 bp_commbufsize = DEFAULT_BUFSIZE;
289
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000290 ret = buspirate_serialport_setup(dev);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000291 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000292 if (ret) {
293 bp_commbufsize = 0;
294 free(bp_commbuf);
295 bp_commbuf = NULL;
296 return ret;
297 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000298
Stefan Reinauer18385912014-04-26 16:12:45 +0000299 if (register_shutdown(buspirate_spi_shutdown, NULL) != 0) {
300 bp_commbufsize = 0;
301 free(bp_commbuf);
302 bp_commbuf = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000303 return 1;
Stefan Reinauer18385912014-04-26 16:12:45 +0000304 }
David Hendricks8bb20212011-06-14 01:35:36 +0000305
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000306 /* This is the brute force version, but it should work.
307 * It is likely to fail if a previous flashrom run was aborted during a write with the new SPI commands
308 * in firmware v5.5 because that firmware may wait for up to 4096 bytes of input before responding to
309 * 0x00 again. The obvious workaround (sending 4096 bytes of \0) may cause significant startup delays.
310 */
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000311 for (i = 0; i < 20; i++) {
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000312 /* Enter raw bitbang mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000313 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000314 /* Send the command, don't read the response. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000315 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000316 if (ret)
317 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000318 /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any
319 * response which came in over serial. Unfortunately that does not work reliably on Linux
320 * with FTDI USB-serial.
321 */
322 //sp_flush_incoming();
323 /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence
324 * of 0x00 too fast apparently triggers such an UART input buffer overflow.
325 */
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000326 internal_sleep(10000);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000327 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000328 /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */
329 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000330 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000331
332 /* Reset the Bus Pirate. */
333 bp_commbuf[0] = 0x0f;
334 /* Send the command, don't read the response. */
335 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
336 return ret;
337 if ((ret = buspirate_wait_for_string(bp_commbuf, "irate ")))
338 return ret;
339 /* Read the hardware version string. Last byte of the buffer is reserved for \0. */
340 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
341 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
342 return ret;
343 if (strchr("\r\n\t ", bp_commbuf[i]))
344 break;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000345 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000346 bp_commbuf[i] = '\0';
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600347 msg_pdbg("Detected Bus Pirate hardware ");
348 if (bp_commbuf[0] != 'v')
349 msg_pdbg("(unknown version number format)");
350 else if (!strchr("0123456789", bp_commbuf[1]))
351 msg_pdbg("(unknown version number format)");
352 else {
353 hw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
354 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
355 tmp++;
356 hw_version_minor = strtoul(tmp, NULL, 10);
357 msg_pdbg("%u.%u", hw_version_major, hw_version_minor);
358 }
359 msg_pdbg2(" (\"%s\")", bp_commbuf);
360 msg_pdbg("\n");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000361
362 if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware ")))
363 return ret;
364 /* Read the firmware version string. Last byte of the buffer is reserved for \0. */
365 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
366 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
367 return ret;
368 if (strchr("\r\n\t ", bp_commbuf[i]))
369 break;
370 }
371 bp_commbuf[i] = '\0';
372 msg_pdbg("Detected Bus Pirate firmware ");
373 if (bp_commbuf[0] != 'v')
374 msg_pdbg("(unknown version number format)");
375 else if (!strchr("0123456789", bp_commbuf[1]))
376 msg_pdbg("(unknown version number format)");
377 else {
378 fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
379 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
380 tmp++;
381 fw_version_minor = strtoul(tmp, NULL, 10);
382 msg_pdbg("%u.%u", fw_version_major, fw_version_minor);
383 }
384 msg_pdbg2(" (\"%s\")", bp_commbuf);
385 msg_pdbg("\n");
386
387 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
388 return ret;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600389
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000390 /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */
391 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) {
392 msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n");
393 msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n");
394 return SPI_PROGRAMMER_ERROR;
395 }
396
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000397 /* Use fast SPI mode in firmware 5.5 and newer. */
398 if (BP_FWVERSION(fw_version_major, fw_version_minor) >= BP_FWVERSION(5, 5)) {
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600399 msg_pdbg("Using SPI command set v2.\n");
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000400 /* Sensible default buffer size. */
401 if (buspirate_commbuf_grow(260 + 5))
402 return ERROR_OOM;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000403 spi_master_buspirate.max_data_read = 2048;
404 spi_master_buspirate.max_data_write = 256;
405 spi_master_buspirate.command = buspirate_spi_send_command_v2;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000406 } else {
407 msg_pinfo("Bus Pirate firmware 5.4 and older does not support fast SPI access.\n");
408 msg_pinfo("Reading/writing a flash chip may take hours.\n");
409 msg_pinfo("It is recommended to upgrade to firmware 5.5 or newer.\n");
410 /* Sensible default buffer size. */
411 if (buspirate_commbuf_grow(16 + 3))
412 return ERROR_OOM;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000413 spi_master_buspirate.max_data_read = 12;
414 spi_master_buspirate.max_data_write = 12;
415 spi_master_buspirate.command = buspirate_spi_send_command_v1;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000416 }
417
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000418 /* Workaround for broken speed settings in firmware 6.1 and older. */
419 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2))
420 if (spispeed > 0x4) {
421 msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. "
422 "Limiting speed to 2 MHz.\n");
423 msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n");
424 spispeed = 0x4;
425 }
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600426
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000427 /* This works because speeds numbering starts at 0 and is contiguous. */
428 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
429
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600430 /* Set 2M baud serial speed by default on hardware 3.0 and newer if a custom speed was not set */
431 if (serialspeed_index == -1 && BP_HWVERSION(hw_version_major, hw_version_minor) >= BP_HWVERSION(3, 0)) {
432 serialspeed_index = ARRAY_SIZE(serialspeeds) - 2;
433 msg_pdbg("Bus Pirate v3 or newer detected. Set serial speed to 2M baud.\n");
434 }
435
436 /* Set custom serial speed if specified */
437 if (serialspeed_index != -1) {
438 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(5, 5)) {
439 /* This feature requires firmware 5.5 or newer */
440 msg_perr("Bus Pirate firmware 5.4 and older does not support custom serial speeds."
441 "Using default speed of 115200 baud.\n");
442 } else if (serialspeeds[serialspeed_index].speed != BP_DEFAULTBAUD) {
443 /* Set the serial speed to match the user's choice if it doesn't already */
444
445 if (BP_HWVERSION(hw_version_major, hw_version_minor) < BP_HWVERSION(3, 0))
446 msg_pwarn("Increased serial speeds may not work on older (<3.0) Bus Pirates."
447 " Continue at your own risk.\n");
448
449 /* Enter baud rate configuration mode */
450 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "b\n");
451 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
452 return ret;
453 if ((ret = buspirate_wait_for_string(bp_commbuf, ">")))
454 return ret;
455
456 /* Enter manual clock divisor entry mode */
457 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "10\n");
458 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
459 return ret;
460 if ((ret = buspirate_wait_for_string(bp_commbuf, ">")))
461 return ret;
462
463 /* Set the clock divisor to the value calculated from the user's input */
464 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "%d\n",
465 BP_DIVISOR(serialspeeds[serialspeed_index].speed));
466
467 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
468 return ret;
469 sleep(1);
470
471 /* Reconfigure the host's serial baud rate to the new value */
472 if ((ret = serialport_config(sp_fd, serialspeeds[serialspeed_index].speed))) {
473 msg_perr("Unable to configure system baud rate to specified value.");
474 return ret;
475 }
476
477 /* Return to the main prompt */
478 bp_commbuf[0] = ' ';
479 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
480 return ret;
481 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
482 return ret;
483
484 msg_pdbg("Serial speed is %d baud\n", serialspeeds[serialspeed_index].speed);
485 }
486
487 }
488
489
490
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000491 /* Enter raw bitbang mode */
492 for (i = 0; i < 20; i++) {
493 bp_commbuf[0] = 0x00;
494 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
495 return ret;
496 }
497 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
498 return ret;
499 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
500 return ret;
501 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
502 if (bp_commbuf[0] != '1') {
503 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000504 return 1;
505 }
506 /* Enter raw SPI mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000507 bp_commbuf[0] = 0x01;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000508 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Richard Hughesd82be7b2018-12-19 15:38:51 +0000509 if (ret)
510 return 1;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000511 if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI")))
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000512 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000513 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
514 return ret;
515 msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]);
516 if (bp_commbuf[0] != '1') {
517 msg_perr("Can't handle raw SPI mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000518 return 1;
519 }
520
521 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000522 bp_commbuf[0] = 0x40 | 0x0b;
523 if (pullup == 1) {
524 bp_commbuf[0] |= (1 << 2);
525 msg_pdbg("Enabling pull-up resistors.\n");
526 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000527 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000528 if (ret)
529 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000530 if (bp_commbuf[0] != 0x01) {
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000531 msg_perr("Protocol error while setting power/CS/AUX(/Pull-up resistors)!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000532 return 1;
533 }
534
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000535 /* Set SPI speed */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000536 bp_commbuf[0] = 0x60 | spispeed;
537 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000538 if (ret)
539 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000540 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000541 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000542 return 1;
543 }
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600544
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000545 /* Set SPI config: output type, idle, clock edge, sample */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000546 bp_commbuf[0] = 0x80 | 0xa;
Maxime Vincent2099c642017-11-24 13:04:08 +0100547 if (pullup == 1) {
548 bp_commbuf[0] &= ~(1 << 3);
549 msg_pdbg("Pull-ups enabled, so using HiZ pin output! (Open-Drain mode)\n");
550 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000551 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000552 if (ret)
553 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000554 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000555 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000556 return 1;
557 }
558
559 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000560 bp_commbuf[0] = 0x03;
561 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000562 if (ret)
563 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000564 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000565 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000566 return 1;
567 }
568
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000569 register_spi_master(&spi_master_buspirate);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000570
571 return 0;
572}
573
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000574static int buspirate_spi_send_command_v1(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
575 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000576{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000577 unsigned int i = 0;
578 int ret = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000579
580 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
581 return SPI_INVALID_LENGTH;
582
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000583 /* 3 bytes extra for CS#, len, CS#. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000584 if (buspirate_commbuf_grow(writecnt + readcnt + 3))
585 return ERROR_OOM;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000586
587 /* Assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000588 bp_commbuf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000589
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000590 bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1);
591 memcpy(bp_commbuf + i, writearr, writecnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000592 i += writecnt;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000593 memset(bp_commbuf + i, 0, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000594
595 i += readcnt;
596 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000597 bp_commbuf[i++] = 0x03;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000598
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000599 ret = buspirate_sendrecv(bp_commbuf, i, i);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000600
601 if (ret) {
602 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000603 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000604 }
605
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000606 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000607 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000608 return SPI_GENERIC_ERROR;
609 }
610
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000611 if (bp_commbuf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000612 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000613 return SPI_GENERIC_ERROR;
614 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000615
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000616 if (bp_commbuf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000617 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000618 return SPI_GENERIC_ERROR;
619 }
620
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000621 /* Skip CS#, length, writearr. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000622 memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000623
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000624 return ret;
625}
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000626
627static int buspirate_spi_send_command_v2(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
628 const unsigned char *writearr, unsigned char *readarr)
629{
630 int i = 0, ret = 0;
631
632 if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096)
633 return SPI_INVALID_LENGTH;
634
635 /* 5 bytes extra for command, writelen, readlen.
636 * 1 byte extra for Ack/Nack.
637 */
638 if (buspirate_commbuf_grow(max(writecnt + 5, readcnt + 1)))
639 return ERROR_OOM;
640
641 /* Combined SPI write/read. */
642 bp_commbuf[i++] = 0x04;
643 bp_commbuf[i++] = (writecnt >> 8) & 0xff;
644 bp_commbuf[i++] = writecnt & 0xff;
645 bp_commbuf[i++] = (readcnt >> 8) & 0xff;
646 bp_commbuf[i++] = readcnt & 0xff;
647 memcpy(bp_commbuf + i, writearr, writecnt);
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600648
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000649 ret = buspirate_sendrecv(bp_commbuf, i + writecnt, 1 + readcnt);
650
651 if (ret) {
652 msg_perr("Bus Pirate communication error!\n");
653 return SPI_GENERIC_ERROR;
654 }
655
656 if (bp_commbuf[0] != 0x01) {
657 msg_perr("Protocol error while sending SPI write/read!\n");
658 return SPI_GENERIC_ERROR;
659 }
660
661 /* Skip Ack. */
662 memcpy(readarr, bp_commbuf + 1, readcnt);
663
664 return ret;
Nico Huberdeeac7e2017-04-22 00:09:42 +0200665}