blob: 054b4ffd0299312f553ce90f006fc9db4969467e [file] [log] [blame]
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00004 * Copyright (C) 2009, 2010 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);
Niklas Söderlund2a95e872012-07-30 19:42:33 +000043 if (sp_fd < 0)
44 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
132static int buspirate_spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
133 const unsigned char *writearr, unsigned char *readarr);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000134
135static const struct spi_programmer spi_programmer_buspirate = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000136 .type = SPI_CONTROLLER_BUSPIRATE,
137 .max_data_read = 12,
138 .max_data_write = 12,
139 .command = buspirate_spi_send_command,
140 .multicommand = default_spi_send_multicommand,
141 .read = default_spi_read,
142 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000143 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000144};
145
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000146static const struct buspirate_spispeeds spispeeds[] = {
147 {"30k", 0x0},
148 {"125k", 0x1},
149 {"250k", 0x2},
150 {"1M", 0x3},
151 {"2M", 0x4},
152 {"2.6M", 0x5},
153 {"4M", 0x6},
154 {"8M", 0x7},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000155 {NULL, 0x0},
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000156};
157
David Hendricks8bb20212011-06-14 01:35:36 +0000158static int buspirate_spi_shutdown(void *data)
159{
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000160 int ret = 0, ret2 = 0;
161 /* 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 +0000162
163 /* Exit raw SPI mode (enter raw bitbang mode) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000164 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000165 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000166 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000167 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000168 goto out_shutdown;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000169 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
170 goto out_shutdown;
171 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
172 if (bp_commbuf[0] != '1') {
173 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000174 ret = 1;
175 goto out_shutdown;
David Hendricks8bb20212011-06-14 01:35:36 +0000176 }
177 /* Reset Bus Pirate (return to user terminal) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000178 bp_commbuf[0] = 0x0f;
179 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
David Hendricks8bb20212011-06-14 01:35:36 +0000180
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000181out_shutdown:
David Hendricks8bb20212011-06-14 01:35:36 +0000182 /* Shut down serial port communication */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000183 ret2 = serialport_shutdown(NULL);
184 /* Keep the oldest error, it is probably the best indicator. */
185 if (ret2 && !ret)
186 ret = ret2;
187 bp_commbufsize = 0;
188 free(bp_commbuf);
189 bp_commbuf = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000190 if (ret)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000191 msg_pdbg("Bus Pirate shutdown failed.\n");
192 else
193 msg_pdbg("Bus Pirate shutdown completed.\n");
David Hendricks8bb20212011-06-14 01:35:36 +0000194
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000195 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +0000196}
197
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000198#define BP_FWVERSION(a,b) ((a) << 8 | (b))
199
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000200int buspirate_spi_init(void)
201{
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000202 char *dev = NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000203 char *speed = NULL;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000204 char *tmp;
205 unsigned int fw_version_major = 0;
206 unsigned int fw_version_minor = 0;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000207 int spispeed = 0x7;
208 int ret = 0;
209 int i;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000210
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000211 dev = extract_programmer_param("dev");
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000212 if (dev && !strlen(dev)) {
213 free(dev);
214 dev = NULL;
215 }
216 if (!dev) {
217 msg_perr("No serial device given. Use flashrom -p buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000218 return 1;
219 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000220
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000221 speed = extract_programmer_param("spispeed");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000222 if (speed) {
223 for (i = 0; spispeeds[i].name; i++)
224 if (!strncasecmp(spispeeds[i].name, speed,
225 strlen(spispeeds[i].name))) {
226 spispeed = spispeeds[i].speed;
227 break;
228 }
229 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000230 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000231 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000232 free(speed);
233
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000234 /* Default buffer size is 19: 16 bytes data, 3 bytes control. */
235#define DEFAULT_BUFSIZE (16 + 3)
236 bp_commbuf = malloc(DEFAULT_BUFSIZE);
237 if (!bp_commbuf) {
238 bp_commbufsize = 0;
239 msg_perr("Out of memory!\n");
240 return ERROR_OOM;
241 }
242 bp_commbufsize = DEFAULT_BUFSIZE;
243
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000244 ret = buspirate_serialport_setup(dev);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000245 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000246 if (ret) {
247 bp_commbufsize = 0;
248 free(bp_commbuf);
249 bp_commbuf = NULL;
250 return ret;
251 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000252
David Hendricks8bb20212011-06-14 01:35:36 +0000253 if (register_shutdown(buspirate_spi_shutdown, NULL))
254 return 1;
255
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000256 /* This is the brute force version, but it should work. */
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000257 for (i = 0; i < 20; i++) {
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000258 /* Enter raw bitbang mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000259 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000260 /* Send the command, don't read the response. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000261 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000262 if (ret)
263 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000264 /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any
265 * response which came in over serial. Unfortunately that does not work reliably on Linux
266 * with FTDI USB-serial.
267 */
268 //sp_flush_incoming();
269 /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence
270 * of 0x00 too fast apparently triggers such an UART input buffer overflow.
271 */
272 usleep(10000);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000273 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000274 /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */
275 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000276 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000277
278 /* Reset the Bus Pirate. */
279 bp_commbuf[0] = 0x0f;
280 /* Send the command, don't read the response. */
281 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
282 return ret;
283 if ((ret = buspirate_wait_for_string(bp_commbuf, "irate ")))
284 return ret;
285 /* Read the hardware version string. Last byte of the buffer is reserved for \0. */
286 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
287 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
288 return ret;
289 if (strchr("\r\n\t ", bp_commbuf[i]))
290 break;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000291 }
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000292 bp_commbuf[i] = '\0';
293 msg_pdbg("Detected Bus Pirate hardware %s\n", bp_commbuf);
294
295 if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware ")))
296 return ret;
297 /* Read the firmware version string. Last byte of the buffer is reserved for \0. */
298 for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) {
299 if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1)))
300 return ret;
301 if (strchr("\r\n\t ", bp_commbuf[i]))
302 break;
303 }
304 bp_commbuf[i] = '\0';
305 msg_pdbg("Detected Bus Pirate firmware ");
306 if (bp_commbuf[0] != 'v')
307 msg_pdbg("(unknown version number format)");
308 else if (!strchr("0123456789", bp_commbuf[1]))
309 msg_pdbg("(unknown version number format)");
310 else {
311 fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10);
312 while ((*tmp != '\0') && !strchr("0123456789", *tmp))
313 tmp++;
314 fw_version_minor = strtoul(tmp, NULL, 10);
315 msg_pdbg("%u.%u", fw_version_major, fw_version_minor);
316 }
317 msg_pdbg2(" (\"%s\")", bp_commbuf);
318 msg_pdbg("\n");
319
320 if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>")))
321 return ret;
322
323 /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */
324 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) {
325 msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n");
326 msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n");
327 return SPI_PROGRAMMER_ERROR;
328 }
329
330 /* Workaround for broken speed settings in firmware 6.1 and older. */
331 if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2))
332 if (spispeed > 0x4) {
333 msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. "
334 "Limiting speed to 2 MHz.\n");
335 msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n");
336 spispeed = 0x4;
337 }
338
339 /* This works because speeds numbering starts at 0 and is contiguous. */
340 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
341
342 /* Enter raw bitbang mode */
343 for (i = 0; i < 20; i++) {
344 bp_commbuf[0] = 0x00;
345 if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0)))
346 return ret;
347 }
348 if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO")))
349 return ret;
350 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
351 return ret;
352 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]);
353 if (bp_commbuf[0] != '1') {
354 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000355 return 1;
356 }
357 /* Enter raw SPI mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000358 bp_commbuf[0] = 0x01;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000359 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
360 if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI")))
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000361 return ret;
Carl-Daniel Hailfingera16a8922012-08-17 17:30:43 +0000362 if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1)))
363 return ret;
364 msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]);
365 if (bp_commbuf[0] != '1') {
366 msg_perr("Can't handle raw SPI mode version %c!\n", bp_commbuf[0]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000367 return 1;
368 }
369
370 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000371 bp_commbuf[0] = 0x40 | 0xb;
372 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000373 if (ret)
374 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000375 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000376 msg_perr("Protocol error while setting power/CS/AUX!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000377 return 1;
378 }
379
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000380 /* Set SPI speed */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000381 bp_commbuf[0] = 0x60 | spispeed;
382 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000383 if (ret)
384 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000385 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000386 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000387 return 1;
388 }
389
390 /* Set SPI config: output type, idle, clock edge, sample */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000391 bp_commbuf[0] = 0x80 | 0xa;
392 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000393 if (ret)
394 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000395 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000396 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000397 return 1;
398 }
399
400 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000401 bp_commbuf[0] = 0x03;
402 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000403 if (ret)
404 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000405 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000406 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000407 return 1;
408 }
409
Michael Karcherb9dbe482011-05-11 17:07:07 +0000410 register_spi_programmer(&spi_programmer_buspirate);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000411
412 return 0;
413}
414
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000415static int buspirate_spi_send_command(struct flashctx *flash,
416 unsigned int writecnt,
417 unsigned int readcnt,
418 const unsigned char *writearr,
419 unsigned char *readarr)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000420{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000421 unsigned int i = 0;
422 int ret = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000423
424 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
425 return SPI_INVALID_LENGTH;
426
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000427 /* 3 bytes extra for CS#, len, CS#. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000428 if (buspirate_commbuf_grow(writecnt + readcnt + 3))
429 return ERROR_OOM;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000430
431 /* Assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000432 bp_commbuf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000433
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000434 bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1);
435 memcpy(bp_commbuf + i, writearr, writecnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000436 i += writecnt;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000437 memset(bp_commbuf + i, 0, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000438
439 i += readcnt;
440 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000441 bp_commbuf[i++] = 0x03;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000442
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000443 ret = buspirate_sendrecv(bp_commbuf, i, i);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000444
445 if (ret) {
446 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000447 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000448 }
449
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000450 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000451 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000452 return SPI_GENERIC_ERROR;
453 }
454
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000455 if (bp_commbuf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000456 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000457 return SPI_GENERIC_ERROR;
458 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000459
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000460 if (bp_commbuf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000461 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000462 return SPI_GENERIC_ERROR;
463 }
464
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000465 /* Skip CS#, length, writearr. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000466 memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000467
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000468 return ret;
469}