blob: 6f4c6d06e1e50d4850e0b48d585ba7ef2888ebce [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
Carl-Daniel Hailfingerc4224842011-06-09 20:06:34 +000033struct buspirate_spispeeds {
34 const char *name;
35 const int speed;
36};
37
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000038#ifndef FAKE_COMMUNICATION
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000039static int buspirate_serialport_setup(char *dev)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000040{
41 /* 115200bps, 8 databits, no parity, 1 stopbit */
42 sp_fd = sp_openserport(dev, 115200);
Stefan Tauneracfc4c62012-11-30 16:46:45 +000043 if (sp_fd == SER_INV_FD)
Niklas Söderlund2a95e872012-07-30 19:42:33 +000044 return 1;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000045 return 0;
46}
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000047#else
48#define buspirate_serialport_setup(...) 0
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +000049#define serialport_shutdown(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000050#define serialport_write(...) 0
51#define serialport_read(...) 0
Patrick Georgi3b6237d2010-01-06 19:09:40 +000052#define sp_flush_incoming(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000053#endif
54
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +000055static unsigned char *bp_commbuf = NULL;
56static int bp_commbufsize = 0;
57
58static int buspirate_commbuf_grow(int bufsize)
59{
60 unsigned char *tmpbuf;
61
62 /* Never shrink. realloc() calls are expensive. */
63 if (bufsize <= bp_commbufsize)
64 return 0;
65
66 tmpbuf = realloc(bp_commbuf, bufsize);
67 if (!tmpbuf) {
68 /* Keep the existing buffer because memory is already tight. */
69 msg_perr("Out of memory!\n");
70 return ERROR_OOM;
71 }
72
73 bp_commbuf = tmpbuf;
74 bp_commbufsize = bufsize;
75 return 0;
76}
77
Uwe Hermann91f4afa2011-07-28 08:13:25 +000078static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt,
79 unsigned int readcnt)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000080{
81 int i, ret = 0;
82
Sean Nelson84f7bce2010-01-09 23:56:41 +000083 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000084 if (!writecnt && !readcnt) {
Sean Nelson84f7bce2010-01-09 23:56:41 +000085 msg_perr("Zero length command!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000086 return 1;
87 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +000088 if (writecnt)
89 msg_pspew("Sending");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000090 for (i = 0; i < writecnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000091 msg_pspew(" 0x%02x", buf[i]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000092#ifdef FAKE_COMMUNICATION
93 /* Placate the caller for now. */
94 if (readcnt) {
95 buf[0] = 0x01;
96 memset(buf + 1, 0xff, readcnt - 1);
97 }
98 ret = 0;
99#else
100 if (writecnt)
101 ret = serialport_write(buf, writecnt);
102 if (ret)
103 return ret;
104 if (readcnt)
105 ret = serialport_read(buf, readcnt);
106 if (ret)
107 return ret;
108#endif
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000109 if (readcnt)
110 msg_pspew(", receiving");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000111 for (i = 0; i < readcnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000112 msg_pspew(" 0x%02x", buf[i]);
113 msg_pspew("\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000114 return 0;
115}
116
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000117static int buspirate_wait_for_string(unsigned char *buf, char *key)
118{
119 unsigned int keylen = strlen(key);
120 int ret;
121
122 ret = buspirate_sendrecv(buf, 0, keylen);
123 while (!ret) {
124 if (!memcmp(buf, key, keylen))
125 return 0;
126 memmove(buf, buf + 1, keylen - 1);
127 ret = buspirate_sendrecv(buf + keylen - 1, 0, 1);
128 }
129 return ret;
130}
131
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000132static int buspirate_spi_send_command_v1(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
133 const unsigned char *writearr, unsigned char *readarr);
134static int buspirate_spi_send_command_v2(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
135 const unsigned char *writearr, unsigned char *readarr);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000136
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000137static struct spi_master spi_master_buspirate = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000138 .type = SPI_CONTROLLER_BUSPIRATE,
Nico Huber1cf407b2017-11-10 20:18:23 +0100139 .features = SPI_MASTER_4BA,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000140 .max_data_read = MAX_DATA_UNSPECIFIED,
141 .max_data_write = MAX_DATA_UNSPECIFIED,
142 .command = NULL,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000143 .multicommand = default_spi_send_multicommand,
144 .read = default_spi_read,
145 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000146 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000147};
148
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000149static const struct buspirate_spispeeds spispeeds[] = {
150 {"30k", 0x0},
151 {"125k", 0x1},
152 {"250k", 0x2},
153 {"1M", 0x3},
154 {"2M", 0x4},
155 {"2.6M", 0x5},
156 {"4M", 0x6},
157 {"8M", 0x7},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000158 {NULL, 0x0},
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000159};
160
David Hendricks8bb20212011-06-14 01:35:36 +0000161static int buspirate_spi_shutdown(void *data)
162{
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000163 int ret = 0, ret2 = 0;
164 /* 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 +0000165
166 /* Exit raw SPI mode (enter raw bitbang mode) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000167 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000168 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000169 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000170 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000171 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000172 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
173 goto out_shutdown;
174 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
175 if (bp_commbuf[0] != '1') {
176 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000177 ret = 1;
178 goto out_shutdown;
David Hendricks8bb20212011-06-14 01:35:36 +0000179 }
180 /* Reset Bus Pirate (return to user terminal) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000181 bp_commbuf[0] = 0x0f;
182 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
David Hendricks8bb20212011-06-14 01:35:36 +0000183
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000184out_shutdown:
David Hendricks8bb20212011-06-14 01:35:36 +0000185 /* Shut down serial port communication */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000186 ret2 = serialport_shutdown(NULL);
187 /* Keep the oldest error, it is probably the best indicator. */
188 if (ret2 && !ret)
189 ret = ret2;
190 bp_commbufsize = 0;
191 free(bp_commbuf);
192 bp_commbuf = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000193 if (ret)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000194 msg_pdbg("Bus Pirate shutdown failed.\n");
195 else
196 msg_pdbg("Bus Pirate shutdown completed.\n");
David Hendricks8bb20212011-06-14 01:35:36 +0000197
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000198 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +0000199}
200
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000201#define BP_FWVERSION(a,b) ((a) << 8 | (b))
202
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000203int buspirate_spi_init(void)
204{
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000205 char *tmp;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000206 char *dev;
207 int i;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000208 unsigned int fw_version_major = 0;
209 unsigned int fw_version_minor = 0;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000210 int spispeed = 0x7;
211 int ret = 0;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000212 int pullup = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000213
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000214 dev = extract_programmer_param("dev");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000215 if (dev && !strlen(dev)) {
216 free(dev);
217 dev = NULL;
218 }
219 if (!dev) {
220 msg_perr("No serial device given. Use flashrom -p buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000221 return 1;
222 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000223
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000224 tmp = extract_programmer_param("spispeed");
225 if (tmp) {
226 for (i = 0; spispeeds[i].name; i++) {
227 if (!strncasecmp(spispeeds[i].name, tmp, strlen(spispeeds[i].name))) {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000228 spispeed = spispeeds[i].speed;
229 break;
230 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000231 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000232 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000233 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000234 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000235 free(tmp);
236
237 tmp = extract_programmer_param("pullups");
238 if (tmp) {
239 if (strcasecmp("on", tmp) == 0)
240 pullup = 1;
241 else if (strcasecmp("off", tmp) == 0)
242 ; // ignore
243 else
244 msg_perr("Invalid pullups state, not using them.\n");
245 }
246 free(tmp);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000247
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000248 /* Default buffer size is 19: 16 bytes data, 3 bytes control. */
249#define DEFAULT_BUFSIZE (16 + 3)
250 bp_commbuf = malloc(DEFAULT_BUFSIZE);
251 if (!bp_commbuf) {
252 bp_commbufsize = 0;
253 msg_perr("Out of memory!\n");
Stefan Reinauer18385912014-04-26 16:12:45 +0000254 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000255 return ERROR_OOM;
256 }
257 bp_commbufsize = DEFAULT_BUFSIZE;
258
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000259 ret = buspirate_serialport_setup(dev);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000260 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000261 if (ret) {
262 bp_commbufsize = 0;
263 free(bp_commbuf);
264 bp_commbuf = NULL;
265 return ret;
266 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000267
Stefan Reinauer18385912014-04-26 16:12:45 +0000268 if (register_shutdown(buspirate_spi_shutdown, NULL) != 0) {
269 bp_commbufsize = 0;
270 free(bp_commbuf);
271 bp_commbuf = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000272 return 1;
Stefan Reinauer18385912014-04-26 16:12:45 +0000273 }
David Hendricks8bb20212011-06-14 01:35:36 +0000274
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000275 /* This is the brute force version, but it should work.
276 * It is likely to fail if a previous flashrom run was aborted during a write with the new SPI commands
277 * in firmware v5.5 because that firmware may wait for up to 4096 bytes of input before responding to
278 * 0x00 again. The obvious workaround (sending 4096 bytes of \0) may cause significant startup delays.
279 */
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000280 for (i = 0; i < 20; i++) {
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000281 /* Enter raw bitbang mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000282 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000283 /* Send the command, don't read the response. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000284 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000285 if (ret)
286 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000287 /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any
288 * response which came in over serial. Unfortunately that does not work reliably on Linux
289 * with FTDI USB-serial.
290 */
291 //sp_flush_incoming();
292 /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence
293 * of 0x00 too fast apparently triggers such an UART input buffer overflow.
294 */
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000295 internal_sleep(10000);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000296 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000297 /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */
298 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000299 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000300
301 /* Reset the Bus Pirate. */
302 bp_commbuf[0] = 0x0f;
303 /* Send the command, don't read the response. */
304 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
305 return ret;
306 if ((ret = buspirate_wait_for_string(bp_commbuf, "irate ")))
307 return ret;
308 /* Read the hardware version string. Last byte of the buffer is reserved for \0. */
309 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
310 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
311 return ret;
312 if (strchr("\r\n\t ", bp_commbuf[i]))
313 break;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000314 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000315 bp_commbuf[i] = '\0';
316 msg_pdbg("Detected Bus Pirate hardware %s\n", bp_commbuf);
317
318 if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware ")))
319 return ret;
320 /* Read the firmware version string. Last byte of the buffer is reserved for \0. */
321 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
322 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
323 return ret;
324 if (strchr("\r\n\t ", bp_commbuf[i]))
325 break;
326 }
327 bp_commbuf[i] = '\0';
328 msg_pdbg("Detected Bus Pirate firmware ");
329 if (bp_commbuf[0] != 'v')
330 msg_pdbg("(unknown version number format)");
331 else if (!strchr("0123456789", bp_commbuf[1]))
332 msg_pdbg("(unknown version number format)");
333 else {
334 fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
335 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
336 tmp++;
337 fw_version_minor = strtoul(tmp, NULL, 10);
338 msg_pdbg("%u.%u", fw_version_major, fw_version_minor);
339 }
340 msg_pdbg2(" (\"%s\")", bp_commbuf);
341 msg_pdbg("\n");
342
343 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
344 return ret;
345
346 /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */
347 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) {
348 msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n");
349 msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n");
350 return SPI_PROGRAMMER_ERROR;
351 }
352
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000353 /* Use fast SPI mode in firmware 5.5 and newer. */
354 if (BP_FWVERSION(fw_version_major, fw_version_minor) >= BP_FWVERSION(5, 5)) {
355 msg_pdbg("Using SPI command set v2.\n");
356 /* Sensible default buffer size. */
357 if (buspirate_commbuf_grow(260 + 5))
358 return ERROR_OOM;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000359 spi_master_buspirate.max_data_read = 2048;
360 spi_master_buspirate.max_data_write = 256;
361 spi_master_buspirate.command = buspirate_spi_send_command_v2;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000362 } else {
363 msg_pinfo("Bus Pirate firmware 5.4 and older does not support fast SPI access.\n");
364 msg_pinfo("Reading/writing a flash chip may take hours.\n");
365 msg_pinfo("It is recommended to upgrade to firmware 5.5 or newer.\n");
366 /* Sensible default buffer size. */
367 if (buspirate_commbuf_grow(16 + 3))
368 return ERROR_OOM;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000369 spi_master_buspirate.max_data_read = 12;
370 spi_master_buspirate.max_data_write = 12;
371 spi_master_buspirate.command = buspirate_spi_send_command_v1;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000372 }
373
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000374 /* Workaround for broken speed settings in firmware 6.1 and older. */
375 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2))
376 if (spispeed > 0x4) {
377 msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. "
378 "Limiting speed to 2 MHz.\n");
379 msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n");
380 spispeed = 0x4;
381 }
382
383 /* This works because speeds numbering starts at 0 and is contiguous. */
384 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
385
386 /* Enter raw bitbang mode */
387 for (i = 0; i < 20; i++) {
388 bp_commbuf[0] = 0x00;
389 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
390 return ret;
391 }
392 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
393 return ret;
394 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
395 return ret;
396 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
397 if (bp_commbuf[0] != '1') {
398 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000399 return 1;
400 }
401 /* Enter raw SPI mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000402 bp_commbuf[0] = 0x01;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000403 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
404 if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI")))
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000405 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000406 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
407 return ret;
408 msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]);
409 if (bp_commbuf[0] != '1') {
410 msg_perr("Can't handle raw SPI mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000411 return 1;
412 }
413
414 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000415 bp_commbuf[0] = 0x40 | 0x0b;
416 if (pullup == 1) {
417 bp_commbuf[0] |= (1 << 2);
418 msg_pdbg("Enabling pull-up resistors.\n");
419 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000420 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000421 if (ret)
422 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000423 if (bp_commbuf[0] != 0x01) {
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000424 msg_perr("Protocol error while setting power/CS/AUX(/Pull-up resistors)!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000425 return 1;
426 }
427
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000428 /* Set SPI speed */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000429 bp_commbuf[0] = 0x60 | spispeed;
430 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000431 if (ret)
432 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000433 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000434 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000435 return 1;
436 }
437
438 /* Set SPI config: output type, idle, clock edge, sample */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000439 bp_commbuf[0] = 0x80 | 0xa;
440 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000441 if (ret)
442 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000443 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000444 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000445 return 1;
446 }
447
448 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000449 bp_commbuf[0] = 0x03;
450 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000451 if (ret)
452 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000453 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000454 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000455 return 1;
456 }
457
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000458 register_spi_master(&spi_master_buspirate);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000459
460 return 0;
461}
462
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000463static int buspirate_spi_send_command_v1(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
464 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000465{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000466 unsigned int i = 0;
467 int ret = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000468
469 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
470 return SPI_INVALID_LENGTH;
471
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000472 /* 3 bytes extra for CS#, len, CS#. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000473 if (buspirate_commbuf_grow(writecnt + readcnt + 3))
474 return ERROR_OOM;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000475
476 /* Assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000477 bp_commbuf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000478
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000479 bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1);
480 memcpy(bp_commbuf + i, writearr, writecnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000481 i += writecnt;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000482 memset(bp_commbuf + i, 0, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000483
484 i += readcnt;
485 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000486 bp_commbuf[i++] = 0x03;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000487
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000488 ret = buspirate_sendrecv(bp_commbuf, i, i);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000489
490 if (ret) {
491 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000492 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000493 }
494
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000495 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000496 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000497 return SPI_GENERIC_ERROR;
498 }
499
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000500 if (bp_commbuf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000501 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000502 return SPI_GENERIC_ERROR;
503 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000504
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000505 if (bp_commbuf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000506 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000507 return SPI_GENERIC_ERROR;
508 }
509
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000510 /* Skip CS#, length, writearr. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000511 memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000512
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000513 return ret;
514}
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000515
516static int buspirate_spi_send_command_v2(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
517 const unsigned char *writearr, unsigned char *readarr)
518{
519 int i = 0, ret = 0;
520
521 if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096)
522 return SPI_INVALID_LENGTH;
523
524 /* 5 bytes extra for command, writelen, readlen.
525 * 1 byte extra for Ack/Nack.
526 */
527 if (buspirate_commbuf_grow(max(writecnt + 5, readcnt + 1)))
528 return ERROR_OOM;
529
530 /* Combined SPI write/read. */
531 bp_commbuf[i++] = 0x04;
532 bp_commbuf[i++] = (writecnt >> 8) & 0xff;
533 bp_commbuf[i++] = writecnt & 0xff;
534 bp_commbuf[i++] = (readcnt >> 8) & 0xff;
535 bp_commbuf[i++] = readcnt & 0xff;
536 memcpy(bp_commbuf + i, writearr, writecnt);
537
538 ret = buspirate_sendrecv(bp_commbuf, i + writecnt, 1 + readcnt);
539
540 if (ret) {
541 msg_perr("Bus Pirate communication error!\n");
542 return SPI_GENERIC_ERROR;
543 }
544
545 if (bp_commbuf[0] != 0x01) {
546 msg_perr("Protocol error while sending SPI write/read!\n");
547 return SPI_GENERIC_ERROR;
548 }
549
550 /* Skip Ack. */
551 memcpy(readarr, bp_commbuf + 1, readcnt);
552
553 return ret;
554}