blob: 2909a3ecd620cfd8f0117bd0b4681abff9157b25 [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>
Felix Singercf6b7082022-08-18 23:57:31 +020019#include <stdbool.h>
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000020#include <stdlib.h>
21#include <ctype.h>
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +000022#include <unistd.h>
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000023#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000024#include "programmer.h"
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000025#include "spi.h"
26
27/* Change this to #define if you want to test without a serial implementation */
28#undef FAKE_COMMUNICATION
29
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -060030struct buspirate_speeds {
Carl-Daniel Hailfingerc4224842011-06-09 20:06:34 +000031 const char *name;
32 const int speed;
33};
34
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -060035#define BP_DEFAULTBAUD 115200
36
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000037#ifndef FAKE_COMMUNICATION
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000038static int buspirate_serialport_setup(char *dev)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000039{
40 /* 115200bps, 8 databits, no parity, 1 stopbit */
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -060041 sp_fd = sp_openserport(dev, BP_DEFAULTBAUD);
Elyes HAOUAS124ef382018-03-27 12:15:09 +020042 if (sp_fd == SER_INV_FD)
Niklas Söderlund2a95e872012-07-30 19:42:33 +000043 return 1;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000044 return 0;
45}
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000046#else
47#define buspirate_serialport_setup(...) 0
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +000048#define serialport_shutdown(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000049#define serialport_write(...) 0
50#define serialport_read(...) 0
Patrick Georgi3b6237d2010-01-06 19:09:40 +000051#define sp_flush_incoming(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000052#endif
53
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +100054struct bp_spi_data {
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +100055 unsigned char *commbuf;
56 int commbufsize;
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +100057};
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +000058
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +100059static int buspirate_commbuf_grow(int bufsize, unsigned char **bp_commbuf, int *bp_commbufsize)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +000060{
61 unsigned char *tmpbuf;
62
63 /* Never shrink. realloc() calls are expensive. */
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +100064 if (bufsize <= *bp_commbufsize)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +000065 return 0;
66
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +100067 tmpbuf = realloc(*bp_commbuf, bufsize);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +000068 if (!tmpbuf) {
69 /* Keep the existing buffer because memory is already tight. */
70 msg_perr("Out of memory!\n");
71 return ERROR_OOM;
72 }
73
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +100074 *bp_commbuf = tmpbuf;
75 *bp_commbufsize = bufsize;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +000076 return 0;
77}
78
Uwe Hermann91f4afa2011-07-28 08:13:25 +000079static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt,
80 unsigned int readcnt)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000081{
Nico Huber519be662018-12-23 20:03:35 +010082 unsigned int i;
83 int ret = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000084
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
Jacob Garber4a84ec22019-07-25 19:12:31 -0600119static int buspirate_wait_for_string(unsigned char *buf, const char *key)
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000120{
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
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000134static int buspirate_spi_send_command_v1(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000135 const unsigned char *writearr, unsigned char *readarr);
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000136static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000137 const unsigned char *writearr, unsigned char *readarr);
Anastasia Klimchukc63d9182021-07-06 16:18:44 +1000138static int buspirate_spi_shutdown(void *data);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000139
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000140static struct spi_master spi_master_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,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +1000148 .shutdown = buspirate_spi_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +0530149 .probe_opcode = default_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000150};
151
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600152static const struct buspirate_speeds spispeeds[] = {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000153 {"30k", 0x0},
154 {"125k", 0x1},
155 {"250k", 0x2},
156 {"1M", 0x3},
157 {"2M", 0x4},
158 {"2.6M", 0x5},
159 {"4M", 0x6},
160 {"8M", 0x7},
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600161 {NULL, 0x0}
162};
163
164static const struct buspirate_speeds serialspeeds[] = {
165 {"115200", 115200},
166 {"230400", 230400},
167 {"250000", 250000},
168 {"2000000", 2000000},
169 {"2M", 2000000},
170 {NULL, 0}
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000171};
172
David Hendricks8bb20212011-06-14 01:35:36 +0000173static int buspirate_spi_shutdown(void *data)
174{
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000175 struct bp_spi_data *bp_data = data;
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000176 unsigned char *const bp_commbuf = bp_data->commbuf;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000177 int ret = 0, ret2 = 0;
178 /* 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 +0000179
180 /* Exit raw SPI mode (enter raw bitbang mode) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000181 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000182 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000183 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000184 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000185 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000186 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
187 goto out_shutdown;
188 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
189 if (bp_commbuf[0] != '1') {
190 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000191 ret = 1;
192 goto out_shutdown;
David Hendricks8bb20212011-06-14 01:35:36 +0000193 }
194 /* Reset Bus Pirate (return to user terminal) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000195 bp_commbuf[0] = 0x0f;
196 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
David Hendricks8bb20212011-06-14 01:35:36 +0000197
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000198out_shutdown:
David Hendricks8bb20212011-06-14 01:35:36 +0000199 /* Shut down serial port communication */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000200 ret2 = serialport_shutdown(NULL);
201 /* Keep the oldest error, it is probably the best indicator. */
202 if (ret2 && !ret)
203 ret = ret2;
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000204
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000205 free(bp_commbuf);
David Hendricks8bb20212011-06-14 01:35:36 +0000206 if (ret)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000207 msg_pdbg("Bus Pirate shutdown failed.\n");
208 else
209 msg_pdbg("Bus Pirate shutdown completed.\n");
David Hendricks8bb20212011-06-14 01:35:36 +0000210
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000211 free(data);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000212 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +0000213}
214
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000215#define BP_FWVERSION(a,b) ((a) << 8 | (b))
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600216#define BP_HWVERSION(a,b) BP_FWVERSION(a,b)
217
218/**
219 * The Bus Pirate's PIC microcontroller supports custom baud rates by manually specifying a
220 * clock divisor that can be calculated with the formula (16000000 / (4 * baud)) - 1.
221 */
222#define BP_DIVISOR(baud) ((4000000/(baud)) - 1)
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000223
Thomas Heijligencc853d82021-05-04 15:32:17 +0200224static int buspirate_spi_init(void)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000225{
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000226 char *tmp;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000227 char *dev;
228 int i;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600229 int cnt;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000230 unsigned int fw_version_major = 0;
231 unsigned int fw_version_minor = 0;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600232 unsigned int hw_version_major = 0;
233 unsigned int hw_version_minor = 0;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000234 int spispeed = 0x7;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600235 int serialspeed_index = -1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000236 int ret = 0;
Felix Singercf6b7082022-08-18 23:57:31 +0200237 bool pullup = false;
238 bool psu = false;
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000239 unsigned char *bp_commbuf;
240 int bp_commbufsize;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000241
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000242 dev = extract_programmer_param("dev");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000243 if (dev && !strlen(dev)) {
244 free(dev);
245 dev = NULL;
246 }
247 if (!dev) {
248 msg_perr("No serial device given. Use flashrom -p buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000249 return 1;
250 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000251
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000252 tmp = extract_programmer_param("spispeed");
253 if (tmp) {
254 for (i = 0; spispeeds[i].name; i++) {
255 if (!strncasecmp(spispeeds[i].name, tmp, strlen(spispeeds[i].name))) {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000256 spispeed = spispeeds[i].speed;
257 break;
258 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000259 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000260 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000261 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000262 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000263 free(tmp);
264
Elyes HAOUASe2c90c42018-08-18 09:04:41 +0200265 /* Extract serialspeed parameter */
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600266 tmp = extract_programmer_param("serialspeed");
267 if (tmp) {
268 for (i = 0; serialspeeds[i].name; i++) {
269 if (!strncasecmp(serialspeeds[i].name, tmp, strlen(serialspeeds[i].name))) {
270 serialspeed_index = i;
271 break;
272 }
273 }
274 if (!serialspeeds[i].name)
275 msg_perr("Invalid serial speed %s, using default.\n", tmp);
276 }
277 free(tmp);
278
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000279 tmp = extract_programmer_param("pullups");
280 if (tmp) {
281 if (strcasecmp("on", tmp) == 0)
Felix Singercf6b7082022-08-18 23:57:31 +0200282 pullup = true;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000283 else if (strcasecmp("off", tmp) == 0)
284 ; // ignore
285 else
286 msg_perr("Invalid pullups state, not using them.\n");
287 }
288 free(tmp);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000289
Jeremy Kerr98bdcb42021-05-23 17:58:06 +0800290 tmp = extract_programmer_param("psus");
291 if (tmp) {
292 if (strcasecmp("on", tmp) == 0)
Felix Singercf6b7082022-08-18 23:57:31 +0200293 psu = true;
Jeremy Kerr98bdcb42021-05-23 17:58:06 +0800294 else if (strcasecmp("off", tmp) == 0)
295 ; // ignore
296 else
297 msg_perr("Invalid psus state, not enabling.\n");
298 }
299 free(tmp);
300
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000301 /* Default buffer size is 19: 16 bytes data, 3 bytes control. */
302#define DEFAULT_BUFSIZE (16 + 3)
303 bp_commbuf = malloc(DEFAULT_BUFSIZE);
304 if (!bp_commbuf) {
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000305 msg_perr("Out of memory!\n");
Stefan Reinauer18385912014-04-26 16:12:45 +0000306 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000307 return ERROR_OOM;
308 }
309 bp_commbufsize = DEFAULT_BUFSIZE;
310
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000311 ret = buspirate_serialport_setup(dev);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000312 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000313 if (ret) {
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000314 free(bp_commbuf);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000315 return ret;
316 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000317
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000318
319 struct bp_spi_data *bp_data = calloc(1, sizeof(*bp_data));
320 if (!bp_data) {
321 msg_perr("Unable to allocate space for SPI master data\n");
322 free(bp_commbuf);
323 return 1;
324 }
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000325 bp_data->commbuf = bp_commbuf;
326 bp_data->commbufsize = bp_commbufsize;
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000327
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000328 /* This is the brute force version, but it should work.
329 * It is likely to fail if a previous flashrom run was aborted during a write with the new SPI commands
330 * in firmware v5.5 because that firmware may wait for up to 4096 bytes of input before responding to
331 * 0x00 again. The obvious workaround (sending 4096 bytes of \0) may cause significant startup delays.
332 */
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000333 for (i = 0; i < 20; i++) {
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000334 /* Enter raw bitbang mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000335 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000336 /* Send the command, don't read the response. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000337 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000338 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000339 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000340 /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any
341 * response which came in over serial. Unfortunately that does not work reliably on Linux
342 * with FTDI USB-serial.
343 */
344 //sp_flush_incoming();
345 /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence
346 * of 0x00 too fast apparently triggers such an UART input buffer overflow.
347 */
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000348 internal_sleep(10000);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000349 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000350 /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */
351 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000352 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000353
354 /* Reset the Bus Pirate. */
355 bp_commbuf[0] = 0x0f;
356 /* Send the command, don't read the response. */
357 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000358 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000359 if ((ret = buspirate_wait_for_string(bp_commbuf, "irate ")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000360 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000361 /* Read the hardware version string. Last byte of the buffer is reserved for \0. */
362 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
363 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000364 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000365 if (strchr("\r\n\t ", bp_commbuf[i]))
366 break;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000367 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000368 bp_commbuf[i] = '\0';
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600369 msg_pdbg("Detected Bus Pirate hardware ");
370 if (bp_commbuf[0] != 'v')
371 msg_pdbg("(unknown version number format)");
372 else if (!strchr("0123456789", bp_commbuf[1]))
373 msg_pdbg("(unknown version number format)");
374 else {
375 hw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
376 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
377 tmp++;
378 hw_version_minor = strtoul(tmp, NULL, 10);
379 msg_pdbg("%u.%u", hw_version_major, hw_version_minor);
380 }
381 msg_pdbg2(" (\"%s\")", bp_commbuf);
382 msg_pdbg("\n");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000383
384 if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware ")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000385 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000386 /* Read the firmware version string. Last byte of the buffer is reserved for \0. */
387 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
388 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000389 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000390 if (strchr("\r\n\t ", bp_commbuf[i]))
391 break;
392 }
393 bp_commbuf[i] = '\0';
394 msg_pdbg("Detected Bus Pirate firmware ");
395 if (bp_commbuf[0] != 'v')
396 msg_pdbg("(unknown version number format)");
397 else if (!strchr("0123456789", bp_commbuf[1]))
398 msg_pdbg("(unknown version number format)");
399 else {
400 fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
401 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
402 tmp++;
403 fw_version_minor = strtoul(tmp, NULL, 10);
404 msg_pdbg("%u.%u", fw_version_major, fw_version_minor);
405 }
406 msg_pdbg2(" (\"%s\")", bp_commbuf);
407 msg_pdbg("\n");
408
409 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000410 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600411
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000412 /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */
413 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) {
414 msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n");
415 msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000416 ret = SPI_PROGRAMMER_ERROR;
417 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000418 }
419
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000420 /* Use fast SPI mode in firmware 5.5 and newer. */
421 if (BP_FWVERSION(fw_version_major, fw_version_minor) >= BP_FWVERSION(5, 5)) {
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600422 msg_pdbg("Using SPI command set v2.\n");
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000423 /* Sensible default buffer size. */
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000424 if (buspirate_commbuf_grow(260 + 5, &bp_commbuf, &bp_commbufsize)) {
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000425 ret = ERROR_OOM;
426 goto init_err_cleanup_exit;
427 }
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000428 bp_data->commbuf = bp_commbuf;
429 bp_data->commbufsize = bp_commbufsize;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000430 spi_master_buspirate.max_data_read = 2048;
431 spi_master_buspirate.max_data_write = 256;
432 spi_master_buspirate.command = buspirate_spi_send_command_v2;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000433 } else {
434 msg_pinfo("Bus Pirate firmware 5.4 and older does not support fast SPI access.\n");
435 msg_pinfo("Reading/writing a flash chip may take hours.\n");
436 msg_pinfo("It is recommended to upgrade to firmware 5.5 or newer.\n");
437 /* Sensible default buffer size. */
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000438 if (buspirate_commbuf_grow(16 + 3, &bp_commbuf, &bp_commbufsize)) {
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000439 ret = ERROR_OOM;
440 goto init_err_cleanup_exit;
441 }
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000442 bp_data->commbuf = bp_commbuf;
443 bp_data->commbufsize = bp_commbufsize;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000444 spi_master_buspirate.max_data_read = 12;
445 spi_master_buspirate.max_data_write = 12;
446 spi_master_buspirate.command = buspirate_spi_send_command_v1;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000447 }
448
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000449 /* Workaround for broken speed settings in firmware 6.1 and older. */
450 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2))
451 if (spispeed > 0x4) {
452 msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. "
453 "Limiting speed to 2 MHz.\n");
454 msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n");
455 spispeed = 0x4;
456 }
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600457
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000458 /* This works because speeds numbering starts at 0 and is contiguous. */
459 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
460
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600461 /* Set 2M baud serial speed by default on hardware 3.0 and newer if a custom speed was not set */
462 if (serialspeed_index == -1 && BP_HWVERSION(hw_version_major, hw_version_minor) >= BP_HWVERSION(3, 0)) {
463 serialspeed_index = ARRAY_SIZE(serialspeeds) - 2;
464 msg_pdbg("Bus Pirate v3 or newer detected. Set serial speed to 2M baud.\n");
465 }
466
467 /* Set custom serial speed if specified */
468 if (serialspeed_index != -1) {
469 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(5, 5)) {
470 /* This feature requires firmware 5.5 or newer */
471 msg_perr("Bus Pirate firmware 5.4 and older does not support custom serial speeds."
472 "Using default speed of 115200 baud.\n");
473 } else if (serialspeeds[serialspeed_index].speed != BP_DEFAULTBAUD) {
474 /* Set the serial speed to match the user's choice if it doesn't already */
475
476 if (BP_HWVERSION(hw_version_major, hw_version_minor) < BP_HWVERSION(3, 0))
477 msg_pwarn("Increased serial speeds may not work on older (<3.0) Bus Pirates."
478 " Continue at your own risk.\n");
479
480 /* Enter baud rate configuration mode */
481 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "b\n");
482 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000483 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600484 if ((ret = buspirate_wait_for_string(bp_commbuf, ">")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000485 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600486
487 /* Enter manual clock divisor entry mode */
488 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "10\n");
489 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000490 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600491 if ((ret = buspirate_wait_for_string(bp_commbuf, ">")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000492 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600493
494 /* Set the clock divisor to the value calculated from the user's input */
495 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "%d\n",
496 BP_DIVISOR(serialspeeds[serialspeed_index].speed));
497
498 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000499 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600500 sleep(1);
501
502 /* Reconfigure the host's serial baud rate to the new value */
503 if ((ret = serialport_config(sp_fd, serialspeeds[serialspeed_index].speed))) {
504 msg_perr("Unable to configure system baud rate to specified value.");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000505 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600506 }
507
508 /* Return to the main prompt */
509 bp_commbuf[0] = ' ';
510 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000511 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600512 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000513 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600514
515 msg_pdbg("Serial speed is %d baud\n", serialspeeds[serialspeed_index].speed);
516 }
517
518 }
519
520
521
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000522 /* Enter raw bitbang mode */
523 for (i = 0; i < 20; i++) {
524 bp_commbuf[0] = 0x00;
525 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000526 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000527 }
528 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000529 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000530 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000531 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000532 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
533 if (bp_commbuf[0] != '1') {
534 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000535 ret = 1;
536 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000537 }
538 /* Enter raw SPI mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000539 bp_commbuf[0] = 0x01;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000540 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000541 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000542 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000543 if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000544 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000545 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000546 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000547 msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]);
548 if (bp_commbuf[0] != '1') {
549 msg_perr("Can't handle raw SPI mode version %c!\n", bp_commbuf[0]);
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000550 ret = 1;
551 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000552 }
553
554 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000555 bp_commbuf[0] = 0x40 | 0x0b;
Felix Singercf6b7082022-08-18 23:57:31 +0200556 if (pullup) {
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000557 bp_commbuf[0] |= (1 << 2);
558 msg_pdbg("Enabling pull-up resistors.\n");
559 }
Felix Singercf6b7082022-08-18 23:57:31 +0200560 if (psu) {
Jeremy Kerr98bdcb42021-05-23 17:58:06 +0800561 bp_commbuf[0] |= (1 << 3);
562 msg_pdbg("Enabling PSUs.\n");
563 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000564 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000565 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000566 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000567 if (bp_commbuf[0] != 0x01) {
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000568 msg_perr("Protocol error while setting power/CS/AUX(/Pull-up resistors)!\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000569 ret = 1;
570 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000571 }
572
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000573 /* Set SPI speed */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000574 bp_commbuf[0] = 0x60 | spispeed;
575 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000576 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000577 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000578 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000579 msg_perr("Protocol error while setting SPI speed!\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000580 ret = 1;
581 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000582 }
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600583
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000584 /* Set SPI config: output type, idle, clock edge, sample */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000585 bp_commbuf[0] = 0x80 | 0xa;
Felix Singercf6b7082022-08-18 23:57:31 +0200586 if (pullup) {
Maxime Vincent2099c642017-11-24 13:04:08 +0100587 bp_commbuf[0] &= ~(1 << 3);
588 msg_pdbg("Pull-ups enabled, so using HiZ pin output! (Open-Drain mode)\n");
589 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000590 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000591 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000592 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000593 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000594 msg_perr("Protocol error while setting SPI config!\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000595 ret = 1;
596 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000597 }
598
599 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000600 bp_commbuf[0] = 0x03;
601 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000602 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000603 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000604 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000605 msg_perr("Protocol error while raising CS#!\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000606 ret = 1;
607 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000608 }
609
Anastasia Klimchukc63d9182021-07-06 16:18:44 +1000610 return register_spi_master(&spi_master_buspirate, bp_data);
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000611
612init_err_cleanup_exit:
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000613 buspirate_spi_shutdown(bp_data);
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000614 return ret;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000615}
616
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000617static int buspirate_spi_send_command_v1(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000618 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000619{
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000620 struct bp_spi_data *bp_data = flash->mst->spi.data;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000621 unsigned int i = 0;
622 int ret = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000623
624 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
625 return SPI_INVALID_LENGTH;
626
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000627 /* 3 bytes extra for CS#, len, CS#. */
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000628 if (buspirate_commbuf_grow(writecnt + readcnt + 3, &bp_data->commbuf, &bp_data->commbufsize))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000629 return ERROR_OOM;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000630
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000631 unsigned char *const bp_commbuf = bp_data->commbuf;
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000632
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000633 /* Assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000634 bp_commbuf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000635
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000636 bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1);
637 memcpy(bp_commbuf + i, writearr, writecnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000638 i += writecnt;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000639 memset(bp_commbuf + i, 0, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000640
641 i += readcnt;
642 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000643 bp_commbuf[i++] = 0x03;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000644
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000645 ret = buspirate_sendrecv(bp_commbuf, i, i);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000646
647 if (ret) {
648 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000649 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000650 }
651
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000652 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000653 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000654 return SPI_GENERIC_ERROR;
655 }
656
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000657 if (bp_commbuf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000658 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000659 return SPI_GENERIC_ERROR;
660 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000661
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000662 if (bp_commbuf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000663 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000664 return SPI_GENERIC_ERROR;
665 }
666
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000667 /* Skip CS#, length, writearr. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000668 memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000669
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000670 return ret;
671}
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000672
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000673static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000674 const unsigned char *writearr, unsigned char *readarr)
675{
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000676 struct bp_spi_data *bp_data = flash->mst->spi.data;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000677 int i = 0, ret = 0;
678
679 if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096)
680 return SPI_INVALID_LENGTH;
681
682 /* 5 bytes extra for command, writelen, readlen.
683 * 1 byte extra for Ack/Nack.
684 */
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000685 if (buspirate_commbuf_grow(max(writecnt + 5, readcnt + 1), &bp_data->commbuf, &bp_data->commbufsize))
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000686 return ERROR_OOM;
687
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000688 unsigned char *const bp_commbuf = bp_data->commbuf;
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000689
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000690 /* Combined SPI write/read. */
691 bp_commbuf[i++] = 0x04;
692 bp_commbuf[i++] = (writecnt >> 8) & 0xff;
693 bp_commbuf[i++] = writecnt & 0xff;
694 bp_commbuf[i++] = (readcnt >> 8) & 0xff;
695 bp_commbuf[i++] = readcnt & 0xff;
696 memcpy(bp_commbuf + i, writearr, writecnt);
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600697
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000698 ret = buspirate_sendrecv(bp_commbuf, i + writecnt, 1 + readcnt);
699
700 if (ret) {
701 msg_perr("Bus Pirate communication error!\n");
702 return SPI_GENERIC_ERROR;
703 }
704
705 if (bp_commbuf[0] != 0x01) {
706 msg_perr("Protocol error while sending SPI write/read!\n");
707 return SPI_GENERIC_ERROR;
708 }
709
710 /* Skip Ack. */
711 memcpy(readarr, bp_commbuf + 1, readcnt);
712
713 return ret;
Nico Huberdeeac7e2017-04-22 00:09:42 +0200714}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200715
716const struct programmer_entry programmer_buspirate_spi = {
717 .name = "buspirate_spi",
718 .type = OTHER,
719 /* FIXME */
720 .devs.note = "Dangerous Prototypes Bus Pirate\n",
721 .init = buspirate_spi_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +0200722};