blob: 87b022fe81816d4b6c88db2d0449e6c23b6190fb [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{
Nico Huber519be662018-12-23 20:03:35 +010079 unsigned int i;
80 int ret = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000081
Sean Nelson84f7bce2010-01-09 23:56:41 +000082 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000083 if (!writecnt && !readcnt) {
Sean Nelson84f7bce2010-01-09 23:56:41 +000084 msg_perr("Zero length command!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000085 return 1;
86 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +000087 if (writecnt)
88 msg_pspew("Sending");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000089 for (i = 0; i < writecnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000090 msg_pspew(" 0x%02x", buf[i]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000091#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 Hailfingera16a8922012-08-17 17:30:43 +0000108 if (readcnt)
109 msg_pspew(", receiving");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000110 for (i = 0; i < readcnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000111 msg_pspew(" 0x%02x", buf[i]);
112 msg_pspew("\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000113 return 0;
114}
115
Jacob Garber4a84ec22019-07-25 19:12:31 -0600116static int buspirate_wait_for_string(unsigned char *buf, const char *key)
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000117{
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
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000131static struct spi_master spi_master_buspirate = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100132 .features = SPI_MASTER_4BA,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000133 .max_data_read = MAX_DATA_UNSPECIFIED,
134 .max_data_write = MAX_DATA_UNSPECIFIED,
135 .command = NULL,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000136 .multicommand = default_spi_send_multicommand,
137 .read = default_spi_read,
138 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000139 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000140};
141
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600142static const struct buspirate_speeds spispeeds[] = {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000143 {"30k", 0x0},
144 {"125k", 0x1},
145 {"250k", 0x2},
146 {"1M", 0x3},
147 {"2M", 0x4},
148 {"2.6M", 0x5},
149 {"4M", 0x6},
150 {"8M", 0x7},
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600151 {NULL, 0x0}
152};
153
154static const struct buspirate_speeds serialspeeds[] = {
155 {"115200", 115200},
156 {"230400", 230400},
157 {"250000", 250000},
158 {"2000000", 2000000},
159 {"2M", 2000000},
160 {NULL, 0}
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000161};
162
David Hendricks8bb20212011-06-14 01:35:36 +0000163static int buspirate_spi_shutdown(void *data)
164{
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000165 int ret = 0, ret2 = 0;
166 /* 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 +0000167
168 /* Exit raw SPI mode (enter raw bitbang mode) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000169 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000170 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000171 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000172 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000173 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000174 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
175 goto out_shutdown;
176 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
177 if (bp_commbuf[0] != '1') {
178 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000179 ret = 1;
180 goto out_shutdown;
David Hendricks8bb20212011-06-14 01:35:36 +0000181 }
182 /* Reset Bus Pirate (return to user terminal) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000183 bp_commbuf[0] = 0x0f;
184 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
David Hendricks8bb20212011-06-14 01:35:36 +0000185
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000186out_shutdown:
David Hendricks8bb20212011-06-14 01:35:36 +0000187 /* Shut down serial port communication */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000188 ret2 = serialport_shutdown(NULL);
189 /* Keep the oldest error, it is probably the best indicator. */
190 if (ret2 && !ret)
191 ret = ret2;
192 bp_commbufsize = 0;
193 free(bp_commbuf);
194 bp_commbuf = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000195 if (ret)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000196 msg_pdbg("Bus Pirate shutdown failed.\n");
197 else
198 msg_pdbg("Bus Pirate shutdown completed.\n");
David Hendricks8bb20212011-06-14 01:35:36 +0000199
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000200 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +0000201}
202
Anastasia Klimchukf1391c72021-02-15 15:04:20 +1100203static int buspirate_spi_send_command_v1(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
204 const unsigned char *writearr, unsigned char *readarr)
205{
206 unsigned int i = 0;
207 int ret = 0;
208
209 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
210 return SPI_INVALID_LENGTH;
211
212 /* 3 bytes extra for CS#, len, CS#. */
213 if (buspirate_commbuf_grow(writecnt + readcnt + 3))
214 return ERROR_OOM;
215
216 /* Assert CS# */
217 bp_commbuf[i++] = 0x02;
218
219 bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1);
220 memcpy(bp_commbuf + i, writearr, writecnt);
221 i += writecnt;
222 memset(bp_commbuf + i, 0, readcnt);
223
224 i += readcnt;
225 /* De-assert CS# */
226 bp_commbuf[i++] = 0x03;
227
228 ret = buspirate_sendrecv(bp_commbuf, i, i);
229
230 if (ret) {
231 msg_perr("Bus Pirate communication error!\n");
232 return SPI_GENERIC_ERROR;
233 }
234
235 if (bp_commbuf[0] != 0x01) {
236 msg_perr("Protocol error while lowering CS#!\n");
237 return SPI_GENERIC_ERROR;
238 }
239
240 if (bp_commbuf[1] != 0x01) {
241 msg_perr("Protocol error while reading/writing SPI!\n");
242 return SPI_GENERIC_ERROR;
243 }
244
245 if (bp_commbuf[i - 1] != 0x01) {
246 msg_perr("Protocol error while raising CS#!\n");
247 return SPI_GENERIC_ERROR;
248 }
249
250 /* Skip CS#, length, writearr. */
251 memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt);
252
253 return ret;
254}
255
256static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
257 const unsigned char *writearr, unsigned char *readarr)
258{
259 int i = 0, ret = 0;
260
261 if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096)
262 return SPI_INVALID_LENGTH;
263
264 /* 5 bytes extra for command, writelen, readlen.
265 * 1 byte extra for Ack/Nack.
266 */
267 if (buspirate_commbuf_grow(max(writecnt + 5, readcnt + 1)))
268 return ERROR_OOM;
269
270 /* Combined SPI write/read. */
271 bp_commbuf[i++] = 0x04;
272 bp_commbuf[i++] = (writecnt >> 8) & 0xff;
273 bp_commbuf[i++] = writecnt & 0xff;
274 bp_commbuf[i++] = (readcnt >> 8) & 0xff;
275 bp_commbuf[i++] = readcnt & 0xff;
276 memcpy(bp_commbuf + i, writearr, writecnt);
277
278 ret = buspirate_sendrecv(bp_commbuf, i + writecnt, 1 + readcnt);
279
280 if (ret) {
281 msg_perr("Bus Pirate communication error!\n");
282 return SPI_GENERIC_ERROR;
283 }
284
285 if (bp_commbuf[0] != 0x01) {
286 msg_perr("Protocol error while sending SPI write/read!\n");
287 return SPI_GENERIC_ERROR;
288 }
289
290 /* Skip Ack. */
291 memcpy(readarr, bp_commbuf + 1, readcnt);
292
293 return ret;
294}
295
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000296#define BP_FWVERSION(a,b) ((a) << 8 | (b))
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600297#define BP_HWVERSION(a,b) BP_FWVERSION(a,b)
298
299/**
300 * The Bus Pirate's PIC microcontroller supports custom baud rates by manually specifying a
301 * clock divisor that can be calculated with the formula (16000000 / (4 * baud)) - 1.
302 */
303#define BP_DIVISOR(baud) ((4000000/(baud)) - 1)
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000304
Thomas Heijligen4f5169d2021-05-04 15:32:17 +0200305static int buspirate_spi_init(void)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000306{
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000307 char *tmp;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000308 char *dev;
309 int i;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600310 int cnt;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000311 unsigned int fw_version_major = 0;
312 unsigned int fw_version_minor = 0;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600313 unsigned int hw_version_major = 0;
314 unsigned int hw_version_minor = 0;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000315 int spispeed = 0x7;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600316 int serialspeed_index = -1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000317 int ret = 0;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000318 int pullup = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000319
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000320 dev = extract_programmer_param("dev");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000321 if (dev && !strlen(dev)) {
322 free(dev);
323 dev = NULL;
324 }
325 if (!dev) {
326 msg_perr("No serial device given. Use flashrom -p buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000327 return 1;
328 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000329
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000330 tmp = extract_programmer_param("spispeed");
331 if (tmp) {
332 for (i = 0; spispeeds[i].name; i++) {
333 if (!strncasecmp(spispeeds[i].name, tmp, strlen(spispeeds[i].name))) {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000334 spispeed = spispeeds[i].speed;
335 break;
336 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000337 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000338 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000339 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000340 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000341 free(tmp);
342
Elyes HAOUASe2c90c42018-08-18 09:04:41 +0200343 /* Extract serialspeed parameter */
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600344 tmp = extract_programmer_param("serialspeed");
345 if (tmp) {
346 for (i = 0; serialspeeds[i].name; i++) {
347 if (!strncasecmp(serialspeeds[i].name, tmp, strlen(serialspeeds[i].name))) {
348 serialspeed_index = i;
349 break;
350 }
351 }
352 if (!serialspeeds[i].name)
353 msg_perr("Invalid serial speed %s, using default.\n", tmp);
354 }
355 free(tmp);
356
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000357 tmp = extract_programmer_param("pullups");
358 if (tmp) {
359 if (strcasecmp("on", tmp) == 0)
360 pullup = 1;
361 else if (strcasecmp("off", tmp) == 0)
362 ; // ignore
363 else
364 msg_perr("Invalid pullups state, not using them.\n");
365 }
366 free(tmp);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000367
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000368 /* Default buffer size is 19: 16 bytes data, 3 bytes control. */
369#define DEFAULT_BUFSIZE (16 + 3)
370 bp_commbuf = malloc(DEFAULT_BUFSIZE);
371 if (!bp_commbuf) {
372 bp_commbufsize = 0;
373 msg_perr("Out of memory!\n");
Stefan Reinauer18385912014-04-26 16:12:45 +0000374 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000375 return ERROR_OOM;
376 }
377 bp_commbufsize = DEFAULT_BUFSIZE;
378
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000379 ret = buspirate_serialport_setup(dev);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000380 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000381 if (ret) {
382 bp_commbufsize = 0;
383 free(bp_commbuf);
384 bp_commbuf = NULL;
385 return ret;
386 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000387
Stefan Reinauer18385912014-04-26 16:12:45 +0000388 if (register_shutdown(buspirate_spi_shutdown, NULL) != 0) {
389 bp_commbufsize = 0;
390 free(bp_commbuf);
391 bp_commbuf = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000392 return 1;
Stefan Reinauer18385912014-04-26 16:12:45 +0000393 }
David Hendricks8bb20212011-06-14 01:35:36 +0000394
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000395 /* This is the brute force version, but it should work.
396 * It is likely to fail if a previous flashrom run was aborted during a write with the new SPI commands
397 * in firmware v5.5 because that firmware may wait for up to 4096 bytes of input before responding to
398 * 0x00 again. The obvious workaround (sending 4096 bytes of \0) may cause significant startup delays.
399 */
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000400 for (i = 0; i < 20; i++) {
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000401 /* Enter raw bitbang mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000402 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000403 /* Send the command, don't read the response. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000404 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000405 if (ret)
406 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000407 /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any
408 * response which came in over serial. Unfortunately that does not work reliably on Linux
409 * with FTDI USB-serial.
410 */
411 //sp_flush_incoming();
412 /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence
413 * of 0x00 too fast apparently triggers such an UART input buffer overflow.
414 */
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000415 internal_sleep(10000);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000416 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000417 /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */
418 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000419 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000420
421 /* Reset the Bus Pirate. */
422 bp_commbuf[0] = 0x0f;
423 /* Send the command, don't read the response. */
424 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
425 return ret;
426 if ((ret = buspirate_wait_for_string(bp_commbuf, "irate ")))
427 return ret;
428 /* Read the hardware version string. Last byte of the buffer is reserved for \0. */
429 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
430 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
431 return ret;
432 if (strchr("\r\n\t ", bp_commbuf[i]))
433 break;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000434 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000435 bp_commbuf[i] = '\0';
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600436 msg_pdbg("Detected Bus Pirate hardware ");
437 if (bp_commbuf[0] != 'v')
438 msg_pdbg("(unknown version number format)");
439 else if (!strchr("0123456789", bp_commbuf[1]))
440 msg_pdbg("(unknown version number format)");
441 else {
442 hw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
443 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
444 tmp++;
445 hw_version_minor = strtoul(tmp, NULL, 10);
446 msg_pdbg("%u.%u", hw_version_major, hw_version_minor);
447 }
448 msg_pdbg2(" (\"%s\")", bp_commbuf);
449 msg_pdbg("\n");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000450
451 if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware ")))
452 return ret;
453 /* Read the firmware version string. Last byte of the buffer is reserved for \0. */
454 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
455 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
456 return ret;
457 if (strchr("\r\n\t ", bp_commbuf[i]))
458 break;
459 }
460 bp_commbuf[i] = '\0';
461 msg_pdbg("Detected Bus Pirate firmware ");
462 if (bp_commbuf[0] != 'v')
463 msg_pdbg("(unknown version number format)");
464 else if (!strchr("0123456789", bp_commbuf[1]))
465 msg_pdbg("(unknown version number format)");
466 else {
467 fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
468 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
469 tmp++;
470 fw_version_minor = strtoul(tmp, NULL, 10);
471 msg_pdbg("%u.%u", fw_version_major, fw_version_minor);
472 }
473 msg_pdbg2(" (\"%s\")", bp_commbuf);
474 msg_pdbg("\n");
475
476 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
477 return ret;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600478
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000479 /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */
480 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) {
481 msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n");
482 msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n");
483 return SPI_PROGRAMMER_ERROR;
484 }
485
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000486 /* Use fast SPI mode in firmware 5.5 and newer. */
487 if (BP_FWVERSION(fw_version_major, fw_version_minor) >= BP_FWVERSION(5, 5)) {
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600488 msg_pdbg("Using SPI command set v2.\n");
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000489 /* Sensible default buffer size. */
490 if (buspirate_commbuf_grow(260 + 5))
491 return ERROR_OOM;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000492 spi_master_buspirate.max_data_read = 2048;
493 spi_master_buspirate.max_data_write = 256;
494 spi_master_buspirate.command = buspirate_spi_send_command_v2;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000495 } else {
496 msg_pinfo("Bus Pirate firmware 5.4 and older does not support fast SPI access.\n");
497 msg_pinfo("Reading/writing a flash chip may take hours.\n");
498 msg_pinfo("It is recommended to upgrade to firmware 5.5 or newer.\n");
499 /* Sensible default buffer size. */
500 if (buspirate_commbuf_grow(16 + 3))
501 return ERROR_OOM;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000502 spi_master_buspirate.max_data_read = 12;
503 spi_master_buspirate.max_data_write = 12;
504 spi_master_buspirate.command = buspirate_spi_send_command_v1;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000505 }
506
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000507 /* Workaround for broken speed settings in firmware 6.1 and older. */
508 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2))
509 if (spispeed > 0x4) {
510 msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. "
511 "Limiting speed to 2 MHz.\n");
512 msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n");
513 spispeed = 0x4;
514 }
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600515
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000516 /* This works because speeds numbering starts at 0 and is contiguous. */
517 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
518
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600519 /* Set 2M baud serial speed by default on hardware 3.0 and newer if a custom speed was not set */
520 if (serialspeed_index == -1 && BP_HWVERSION(hw_version_major, hw_version_minor) >= BP_HWVERSION(3, 0)) {
521 serialspeed_index = ARRAY_SIZE(serialspeeds) - 2;
522 msg_pdbg("Bus Pirate v3 or newer detected. Set serial speed to 2M baud.\n");
523 }
524
525 /* Set custom serial speed if specified */
526 if (serialspeed_index != -1) {
527 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(5, 5)) {
528 /* This feature requires firmware 5.5 or newer */
529 msg_perr("Bus Pirate firmware 5.4 and older does not support custom serial speeds."
530 "Using default speed of 115200 baud.\n");
531 } else if (serialspeeds[serialspeed_index].speed != BP_DEFAULTBAUD) {
532 /* Set the serial speed to match the user's choice if it doesn't already */
533
534 if (BP_HWVERSION(hw_version_major, hw_version_minor) < BP_HWVERSION(3, 0))
535 msg_pwarn("Increased serial speeds may not work on older (<3.0) Bus Pirates."
536 " Continue at your own risk.\n");
537
538 /* Enter baud rate configuration mode */
539 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "b\n");
540 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
541 return ret;
542 if ((ret = buspirate_wait_for_string(bp_commbuf, ">")))
543 return ret;
544
545 /* Enter manual clock divisor entry mode */
546 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "10\n");
547 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
548 return ret;
549 if ((ret = buspirate_wait_for_string(bp_commbuf, ">")))
550 return ret;
551
552 /* Set the clock divisor to the value calculated from the user's input */
553 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "%d\n",
554 BP_DIVISOR(serialspeeds[serialspeed_index].speed));
555
556 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
557 return ret;
558 sleep(1);
559
560 /* Reconfigure the host's serial baud rate to the new value */
561 if ((ret = serialport_config(sp_fd, serialspeeds[serialspeed_index].speed))) {
562 msg_perr("Unable to configure system baud rate to specified value.");
563 return ret;
564 }
565
566 /* Return to the main prompt */
567 bp_commbuf[0] = ' ';
568 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
569 return ret;
570 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
571 return ret;
572
573 msg_pdbg("Serial speed is %d baud\n", serialspeeds[serialspeed_index].speed);
574 }
575
576 }
577
578
579
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000580 /* Enter raw bitbang mode */
581 for (i = 0; i < 20; i++) {
582 bp_commbuf[0] = 0x00;
583 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
584 return ret;
585 }
586 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
587 return ret;
588 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
589 return ret;
590 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
591 if (bp_commbuf[0] != '1') {
592 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000593 return 1;
594 }
595 /* Enter raw SPI mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000596 bp_commbuf[0] = 0x01;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000597 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Richard Hughesd82be7b2018-12-19 15:38:51 +0000598 if (ret)
599 return 1;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000600 if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI")))
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000601 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000602 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
603 return ret;
604 msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]);
605 if (bp_commbuf[0] != '1') {
606 msg_perr("Can't handle raw SPI mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000607 return 1;
608 }
609
610 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000611 bp_commbuf[0] = 0x40 | 0x0b;
612 if (pullup == 1) {
613 bp_commbuf[0] |= (1 << 2);
614 msg_pdbg("Enabling pull-up resistors.\n");
615 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000616 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000617 if (ret)
618 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000619 if (bp_commbuf[0] != 0x01) {
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000620 msg_perr("Protocol error while setting power/CS/AUX(/Pull-up resistors)!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000621 return 1;
622 }
623
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000624 /* Set SPI speed */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000625 bp_commbuf[0] = 0x60 | spispeed;
626 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000627 if (ret)
628 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000629 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000630 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000631 return 1;
632 }
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600633
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000634 /* Set SPI config: output type, idle, clock edge, sample */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000635 bp_commbuf[0] = 0x80 | 0xa;
Maxime Vincent2099c642017-11-24 13:04:08 +0100636 if (pullup == 1) {
637 bp_commbuf[0] &= ~(1 << 3);
638 msg_pdbg("Pull-ups enabled, so using HiZ pin output! (Open-Drain mode)\n");
639 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000640 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000641 if (ret)
642 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000643 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000644 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000645 return 1;
646 }
647
648 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000649 bp_commbuf[0] = 0x03;
650 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000651 if (ret)
652 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000653 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000654 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000655 return 1;
656 }
657
Nico Huber7e496852021-05-11 17:38:14 +0200658 register_spi_master(&spi_master_buspirate, NULL);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000659
660 return 0;
661}
Thomas Heijligen4f5169d2021-05-04 15:32:17 +0200662
663const struct programmer_entry programmer_buspirate_spi = {
664 .name = "buspirate_spi",
665 .type = OTHER,
666 /* FIXME */
667 .devs.note = "Dangerous Prototypes Bus Pirate\n",
668 .init = buspirate_spi_init,
669 .map_flash_region = fallback_map,
670 .unmap_flash_region = fallback_unmap,
671 .delay = internal_delay,
672};