blob: f4d2f0b3dd6477d1b68b5a1b6cced329be39fe6b [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,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000149};
150
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600151static const struct buspirate_speeds spispeeds[] = {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000152 {"30k", 0x0},
153 {"125k", 0x1},
154 {"250k", 0x2},
155 {"1M", 0x3},
156 {"2M", 0x4},
157 {"2.6M", 0x5},
158 {"4M", 0x6},
159 {"8M", 0x7},
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600160 {NULL, 0x0}
161};
162
163static const struct buspirate_speeds serialspeeds[] = {
164 {"115200", 115200},
165 {"230400", 230400},
166 {"250000", 250000},
167 {"2000000", 2000000},
168 {"2M", 2000000},
169 {NULL, 0}
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000170};
171
David Hendricks8bb20212011-06-14 01:35:36 +0000172static int buspirate_spi_shutdown(void *data)
173{
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000174 struct bp_spi_data *bp_data = data;
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000175 unsigned char *const bp_commbuf = bp_data->commbuf;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000176 int ret = 0, ret2 = 0;
177 /* 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 +0000178
179 /* Exit raw SPI mode (enter raw bitbang mode) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000180 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000181 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000182 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000183 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000184 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000185 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
186 goto out_shutdown;
187 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
188 if (bp_commbuf[0] != '1') {
189 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000190 ret = 1;
191 goto out_shutdown;
David Hendricks8bb20212011-06-14 01:35:36 +0000192 }
193 /* Reset Bus Pirate (return to user terminal) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000194 bp_commbuf[0] = 0x0f;
195 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
David Hendricks8bb20212011-06-14 01:35:36 +0000196
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000197out_shutdown:
David Hendricks8bb20212011-06-14 01:35:36 +0000198 /* Shut down serial port communication */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000199 ret2 = serialport_shutdown(NULL);
200 /* Keep the oldest error, it is probably the best indicator. */
201 if (ret2 && !ret)
202 ret = ret2;
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000203
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000204 free(bp_commbuf);
David Hendricks8bb20212011-06-14 01:35:36 +0000205 if (ret)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000206 msg_pdbg("Bus Pirate shutdown failed.\n");
207 else
208 msg_pdbg("Bus Pirate shutdown completed.\n");
David Hendricks8bb20212011-06-14 01:35:36 +0000209
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000210 free(data);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000211 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +0000212}
213
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000214#define BP_FWVERSION(a,b) ((a) << 8 | (b))
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600215#define BP_HWVERSION(a,b) BP_FWVERSION(a,b)
216
217/**
218 * The Bus Pirate's PIC microcontroller supports custom baud rates by manually specifying a
219 * clock divisor that can be calculated with the formula (16000000 / (4 * baud)) - 1.
220 */
221#define BP_DIVISOR(baud) ((4000000/(baud)) - 1)
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000222
Thomas Heijligencc853d82021-05-04 15:32:17 +0200223static int buspirate_spi_init(void)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000224{
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000225 char *tmp;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000226 char *dev;
227 int i;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600228 int cnt;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000229 unsigned int fw_version_major = 0;
230 unsigned int fw_version_minor = 0;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600231 unsigned int hw_version_major = 0;
232 unsigned int hw_version_minor = 0;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000233 int spispeed = 0x7;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600234 int serialspeed_index = -1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000235 int ret = 0;
Felix Singercf6b7082022-08-18 23:57:31 +0200236 bool pullup = false;
237 bool psu = false;
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000238 unsigned char *bp_commbuf;
239 int bp_commbufsize;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000240
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000241 dev = extract_programmer_param("dev");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000242 if (dev && !strlen(dev)) {
243 free(dev);
244 dev = NULL;
245 }
246 if (!dev) {
247 msg_perr("No serial device given. Use flashrom -p buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000248 return 1;
249 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000250
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000251 tmp = extract_programmer_param("spispeed");
252 if (tmp) {
253 for (i = 0; spispeeds[i].name; i++) {
254 if (!strncasecmp(spispeeds[i].name, tmp, strlen(spispeeds[i].name))) {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000255 spispeed = spispeeds[i].speed;
256 break;
257 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000258 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000259 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000260 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000261 }
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000262 free(tmp);
263
Elyes HAOUASe2c90c42018-08-18 09:04:41 +0200264 /* Extract serialspeed parameter */
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600265 tmp = extract_programmer_param("serialspeed");
266 if (tmp) {
267 for (i = 0; serialspeeds[i].name; i++) {
268 if (!strncasecmp(serialspeeds[i].name, tmp, strlen(serialspeeds[i].name))) {
269 serialspeed_index = i;
270 break;
271 }
272 }
273 if (!serialspeeds[i].name)
274 msg_perr("Invalid serial speed %s, using default.\n", tmp);
275 }
276 free(tmp);
277
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000278 tmp = extract_programmer_param("pullups");
279 if (tmp) {
280 if (strcasecmp("on", tmp) == 0)
Felix Singercf6b7082022-08-18 23:57:31 +0200281 pullup = true;
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000282 else if (strcasecmp("off", tmp) == 0)
283 ; // ignore
284 else
285 msg_perr("Invalid pullups state, not using them.\n");
286 }
287 free(tmp);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000288
Jeremy Kerr98bdcb42021-05-23 17:58:06 +0800289 tmp = extract_programmer_param("psus");
290 if (tmp) {
291 if (strcasecmp("on", tmp) == 0)
Felix Singercf6b7082022-08-18 23:57:31 +0200292 psu = true;
Jeremy Kerr98bdcb42021-05-23 17:58:06 +0800293 else if (strcasecmp("off", tmp) == 0)
294 ; // ignore
295 else
296 msg_perr("Invalid psus state, not enabling.\n");
297 }
298 free(tmp);
299
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000300 /* Default buffer size is 19: 16 bytes data, 3 bytes control. */
301#define DEFAULT_BUFSIZE (16 + 3)
302 bp_commbuf = malloc(DEFAULT_BUFSIZE);
303 if (!bp_commbuf) {
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000304 msg_perr("Out of memory!\n");
Stefan Reinauer18385912014-04-26 16:12:45 +0000305 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000306 return ERROR_OOM;
307 }
308 bp_commbufsize = DEFAULT_BUFSIZE;
309
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000310 ret = buspirate_serialport_setup(dev);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000311 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000312 if (ret) {
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000313 free(bp_commbuf);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000314 return ret;
315 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000316
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000317
318 struct bp_spi_data *bp_data = calloc(1, sizeof(*bp_data));
319 if (!bp_data) {
320 msg_perr("Unable to allocate space for SPI master data\n");
321 free(bp_commbuf);
322 return 1;
323 }
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000324 bp_data->commbuf = bp_commbuf;
325 bp_data->commbufsize = bp_commbufsize;
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000326
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000327 /* This is the brute force version, but it should work.
328 * It is likely to fail if a previous flashrom run was aborted during a write with the new SPI commands
329 * in firmware v5.5 because that firmware may wait for up to 4096 bytes of input before responding to
330 * 0x00 again. The obvious workaround (sending 4096 bytes of \0) may cause significant startup delays.
331 */
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000332 for (i = 0; i < 20; i++) {
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000333 /* Enter raw bitbang mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000334 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000335 /* Send the command, don't read the response. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000336 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000337 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000338 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000339 /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any
340 * response which came in over serial. Unfortunately that does not work reliably on Linux
341 * with FTDI USB-serial.
342 */
343 //sp_flush_incoming();
344 /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence
345 * of 0x00 too fast apparently triggers such an UART input buffer overflow.
346 */
Maksim Kuleshov73dc0db2013-04-05 08:06:10 +0000347 internal_sleep(10000);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000348 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000349 /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */
350 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000351 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000352
353 /* Reset the Bus Pirate. */
354 bp_commbuf[0] = 0x0f;
355 /* Send the command, don't read the response. */
356 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000357 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000358 if ((ret = buspirate_wait_for_string(bp_commbuf, "irate ")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000359 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000360 /* Read the hardware version string. Last byte of the buffer is reserved for \0. */
361 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
362 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000363 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000364 if (strchr("\r\n\t ", bp_commbuf[i]))
365 break;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000366 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000367 bp_commbuf[i] = '\0';
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600368 msg_pdbg("Detected Bus Pirate hardware ");
369 if (bp_commbuf[0] != 'v')
370 msg_pdbg("(unknown version number format)");
371 else if (!strchr("0123456789", bp_commbuf[1]))
372 msg_pdbg("(unknown version number format)");
373 else {
374 hw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
375 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
376 tmp++;
377 hw_version_minor = strtoul(tmp, NULL, 10);
378 msg_pdbg("%u.%u", hw_version_major, hw_version_minor);
379 }
380 msg_pdbg2(" (\"%s\")", bp_commbuf);
381 msg_pdbg("\n");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000382
383 if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware ")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000384 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000385 /* Read the firmware version string. Last byte of the buffer is reserved for \0. */
386 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
387 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000388 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000389 if (strchr("\r\n\t ", bp_commbuf[i]))
390 break;
391 }
392 bp_commbuf[i] = '\0';
393 msg_pdbg("Detected Bus Pirate firmware ");
394 if (bp_commbuf[0] != 'v')
395 msg_pdbg("(unknown version number format)");
396 else if (!strchr("0123456789", bp_commbuf[1]))
397 msg_pdbg("(unknown version number format)");
398 else {
399 fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
400 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
401 tmp++;
402 fw_version_minor = strtoul(tmp, NULL, 10);
403 msg_pdbg("%u.%u", fw_version_major, fw_version_minor);
404 }
405 msg_pdbg2(" (\"%s\")", bp_commbuf);
406 msg_pdbg("\n");
407
408 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000409 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600410
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000411 /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */
412 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) {
413 msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n");
414 msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000415 ret = SPI_PROGRAMMER_ERROR;
416 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000417 }
418
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000419 /* Use fast SPI mode in firmware 5.5 and newer. */
420 if (BP_FWVERSION(fw_version_major, fw_version_minor) >= BP_FWVERSION(5, 5)) {
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600421 msg_pdbg("Using SPI command set v2.\n");
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000422 /* Sensible default buffer size. */
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000423 if (buspirate_commbuf_grow(260 + 5, &bp_commbuf, &bp_commbufsize)) {
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000424 ret = ERROR_OOM;
425 goto init_err_cleanup_exit;
426 }
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000427 bp_data->commbuf = bp_commbuf;
428 bp_data->commbufsize = bp_commbufsize;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000429 spi_master_buspirate.max_data_read = 2048;
430 spi_master_buspirate.max_data_write = 256;
431 spi_master_buspirate.command = buspirate_spi_send_command_v2;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000432 } else {
433 msg_pinfo("Bus Pirate firmware 5.4 and older does not support fast SPI access.\n");
434 msg_pinfo("Reading/writing a flash chip may take hours.\n");
435 msg_pinfo("It is recommended to upgrade to firmware 5.5 or newer.\n");
436 /* Sensible default buffer size. */
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000437 if (buspirate_commbuf_grow(16 + 3, &bp_commbuf, &bp_commbufsize)) {
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000438 ret = ERROR_OOM;
439 goto init_err_cleanup_exit;
440 }
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000441 bp_data->commbuf = bp_commbuf;
442 bp_data->commbufsize = bp_commbufsize;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000443 spi_master_buspirate.max_data_read = 12;
444 spi_master_buspirate.max_data_write = 12;
445 spi_master_buspirate.command = buspirate_spi_send_command_v1;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000446 }
447
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000448 /* Workaround for broken speed settings in firmware 6.1 and older. */
449 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2))
450 if (spispeed > 0x4) {
451 msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. "
452 "Limiting speed to 2 MHz.\n");
453 msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n");
454 spispeed = 0x4;
455 }
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600456
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000457 /* This works because speeds numbering starts at 0 and is contiguous. */
458 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
459
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600460 /* Set 2M baud serial speed by default on hardware 3.0 and newer if a custom speed was not set */
461 if (serialspeed_index == -1 && BP_HWVERSION(hw_version_major, hw_version_minor) >= BP_HWVERSION(3, 0)) {
462 serialspeed_index = ARRAY_SIZE(serialspeeds) - 2;
463 msg_pdbg("Bus Pirate v3 or newer detected. Set serial speed to 2M baud.\n");
464 }
465
466 /* Set custom serial speed if specified */
467 if (serialspeed_index != -1) {
468 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(5, 5)) {
469 /* This feature requires firmware 5.5 or newer */
470 msg_perr("Bus Pirate firmware 5.4 and older does not support custom serial speeds."
471 "Using default speed of 115200 baud.\n");
472 } else if (serialspeeds[serialspeed_index].speed != BP_DEFAULTBAUD) {
473 /* Set the serial speed to match the user's choice if it doesn't already */
474
475 if (BP_HWVERSION(hw_version_major, hw_version_minor) < BP_HWVERSION(3, 0))
476 msg_pwarn("Increased serial speeds may not work on older (<3.0) Bus Pirates."
477 " Continue at your own risk.\n");
478
479 /* Enter baud rate configuration mode */
480 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "b\n");
481 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000482 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600483 if ((ret = buspirate_wait_for_string(bp_commbuf, ">")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000484 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600485
486 /* Enter manual clock divisor entry mode */
487 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "10\n");
488 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000489 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600490 if ((ret = buspirate_wait_for_string(bp_commbuf, ">")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000491 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600492
493 /* Set the clock divisor to the value calculated from the user's input */
494 cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "%d\n",
495 BP_DIVISOR(serialspeeds[serialspeed_index].speed));
496
497 if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000498 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600499 sleep(1);
500
501 /* Reconfigure the host's serial baud rate to the new value */
502 if ((ret = serialport_config(sp_fd, serialspeeds[serialspeed_index].speed))) {
503 msg_perr("Unable to configure system baud rate to specified value.");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000504 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600505 }
506
507 /* Return to the main prompt */
508 bp_commbuf[0] = ' ';
509 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000510 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600511 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000512 goto init_err_cleanup_exit;
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600513
514 msg_pdbg("Serial speed is %d baud\n", serialspeeds[serialspeed_index].speed);
515 }
516
517 }
518
519
520
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000521 /* Enter raw bitbang mode */
522 for (i = 0; i < 20; i++) {
523 bp_commbuf[0] = 0x00;
524 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000525 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000526 }
527 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000528 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000529 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000530 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000531 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
532 if (bp_commbuf[0] != '1') {
533 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000534 ret = 1;
535 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000536 }
537 /* Enter raw SPI mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000538 bp_commbuf[0] = 0x01;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000539 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000540 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000541 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000542 if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI")))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000543 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000544 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000545 goto init_err_cleanup_exit;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000546 msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]);
547 if (bp_commbuf[0] != '1') {
548 msg_perr("Can't handle raw SPI mode version %c!\n", bp_commbuf[0]);
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000549 ret = 1;
550 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000551 }
552
553 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000554 bp_commbuf[0] = 0x40 | 0x0b;
Felix Singercf6b7082022-08-18 23:57:31 +0200555 if (pullup) {
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000556 bp_commbuf[0] |= (1 << 2);
557 msg_pdbg("Enabling pull-up resistors.\n");
558 }
Felix Singercf6b7082022-08-18 23:57:31 +0200559 if (psu) {
Jeremy Kerr98bdcb42021-05-23 17:58:06 +0800560 bp_commbuf[0] |= (1 << 3);
561 msg_pdbg("Enabling PSUs.\n");
562 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000563 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000564 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000565 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000566 if (bp_commbuf[0] != 0x01) {
Brian Salcedo30dfdba2013-01-03 20:44:30 +0000567 msg_perr("Protocol error while setting power/CS/AUX(/Pull-up resistors)!\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000568 ret = 1;
569 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000570 }
571
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000572 /* Set SPI speed */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000573 bp_commbuf[0] = 0x60 | spispeed;
574 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000575 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000576 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000577 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000578 msg_perr("Protocol error while setting SPI speed!\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000579 ret = 1;
580 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000581 }
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600582
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000583 /* Set SPI config: output type, idle, clock edge, sample */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000584 bp_commbuf[0] = 0x80 | 0xa;
Felix Singercf6b7082022-08-18 23:57:31 +0200585 if (pullup) {
Maxime Vincent2099c642017-11-24 13:04:08 +0100586 bp_commbuf[0] &= ~(1 << 3);
587 msg_pdbg("Pull-ups enabled, so using HiZ pin output! (Open-Drain mode)\n");
588 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000589 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000590 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000591 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000592 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000593 msg_perr("Protocol error while setting SPI config!\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000594 ret = 1;
595 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000596 }
597
598 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000599 bp_commbuf[0] = 0x03;
600 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Anastasia Klimchukee6d5482021-06-24 16:25:43 +1000601 if (ret)
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000602 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000603 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000604 msg_perr("Protocol error while raising CS#!\n");
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000605 ret = 1;
606 goto init_err_cleanup_exit;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000607 }
608
Anastasia Klimchukc63d9182021-07-06 16:18:44 +1000609 return register_spi_master(&spi_master_buspirate, bp_data);
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000610
611init_err_cleanup_exit:
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000612 buspirate_spi_shutdown(bp_data);
Anastasia Klimchuk7dcb6f32021-05-24 15:12:10 +1000613 return ret;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000614}
615
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000616static int buspirate_spi_send_command_v1(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000617 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000618{
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000619 struct bp_spi_data *bp_data = flash->mst->spi.data;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000620 unsigned int i = 0;
621 int ret = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000622
623 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
624 return SPI_INVALID_LENGTH;
625
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000626 /* 3 bytes extra for CS#, len, CS#. */
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000627 if (buspirate_commbuf_grow(writecnt + readcnt + 3, &bp_data->commbuf, &bp_data->commbufsize))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000628 return ERROR_OOM;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000629
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000630 unsigned char *const bp_commbuf = bp_data->commbuf;
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000631
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000632 /* Assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000633 bp_commbuf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000634
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000635 bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1);
636 memcpy(bp_commbuf + i, writearr, writecnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000637 i += writecnt;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000638 memset(bp_commbuf + i, 0, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000639
640 i += readcnt;
641 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000642 bp_commbuf[i++] = 0x03;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000643
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000644 ret = buspirate_sendrecv(bp_commbuf, i, i);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000645
646 if (ret) {
647 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000648 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000649 }
650
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000651 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000652 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000653 return SPI_GENERIC_ERROR;
654 }
655
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000656 if (bp_commbuf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000657 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000658 return SPI_GENERIC_ERROR;
659 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000660
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000661 if (bp_commbuf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000662 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000663 return SPI_GENERIC_ERROR;
664 }
665
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000666 /* Skip CS#, length, writearr. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000667 memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000668
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000669 return ret;
670}
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000671
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000672static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000673 const unsigned char *writearr, unsigned char *readarr)
674{
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000675 struct bp_spi_data *bp_data = flash->mst->spi.data;
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000676 int i = 0, ret = 0;
677
678 if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096)
679 return SPI_INVALID_LENGTH;
680
681 /* 5 bytes extra for command, writelen, readlen.
682 * 1 byte extra for Ack/Nack.
683 */
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000684 if (buspirate_commbuf_grow(max(writecnt + 5, readcnt + 1), &bp_data->commbuf, &bp_data->commbufsize))
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000685 return ERROR_OOM;
686
Anastasia Klimchuk346d54d2021-06-10 09:57:51 +1000687 unsigned char *const bp_commbuf = bp_data->commbuf;
Anastasia Klimchukd8f8c992021-05-06 07:47:04 +1000688
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000689 /* Combined SPI write/read. */
690 bp_commbuf[i++] = 0x04;
691 bp_commbuf[i++] = (writecnt >> 8) & 0xff;
692 bp_commbuf[i++] = writecnt & 0xff;
693 bp_commbuf[i++] = (readcnt >> 8) & 0xff;
694 bp_commbuf[i++] = readcnt & 0xff;
695 memcpy(bp_commbuf + i, writearr, writecnt);
Shawn Anastasio2b5adfb2017-12-31 00:17:15 -0600696
Carl-Daniel Hailfinger64263c72012-11-01 23:38:51 +0000697 ret = buspirate_sendrecv(bp_commbuf, i + writecnt, 1 + readcnt);
698
699 if (ret) {
700 msg_perr("Bus Pirate communication error!\n");
701 return SPI_GENERIC_ERROR;
702 }
703
704 if (bp_commbuf[0] != 0x01) {
705 msg_perr("Protocol error while sending SPI write/read!\n");
706 return SPI_GENERIC_ERROR;
707 }
708
709 /* Skip Ack. */
710 memcpy(readarr, bp_commbuf + 1, readcnt);
711
712 return ret;
Nico Huberdeeac7e2017-04-22 00:09:42 +0200713}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200714
715const struct programmer_entry programmer_buspirate_spi = {
716 .name = "buspirate_spi",
717 .type = OTHER,
718 /* FIXME */
719 .devs.note = "Dangerous Prototypes Bus Pirate\n",
720 .init = buspirate_spi_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +0200721};