blob: a488fc33c610eee99b1b128b8bc8f9be554c1879 [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 Hailfinger5cca01f2009-11-24 00:20:03 +000021#include <string.h>
22#include <stdlib.h>
23#include <ctype.h>
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +000024#include <unistd.h>
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000025#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000026#include "programmer.h"
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000027#include "spi.h"
28
29/* Change this to #define if you want to test without a serial implementation */
30#undef FAKE_COMMUNICATION
31
Carl-Daniel Hailfingerc4224842011-06-09 20:06:34 +000032struct buspirate_spispeeds {
33 const char *name;
34 const int speed;
35};
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 */
41 sp_fd = sp_openserport(dev, 115200);
Niklas Söderlund2a95e872012-07-30 19:42:33 +000042 if (sp_fd < 0)
43 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
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +000054static unsigned char *bp_commbuf = NULL;
55static int bp_commbufsize = 0;
56
57static int buspirate_commbuf_grow(int bufsize)
58{
59 unsigned char *tmpbuf;
60
61 /* Never shrink. realloc() calls are expensive. */
62 if (bufsize <= bp_commbufsize)
63 return 0;
64
65 tmpbuf = realloc(bp_commbuf, bufsize);
66 if (!tmpbuf) {
67 /* Keep the existing buffer because memory is already tight. */
68 msg_perr("Out of memory!\n");
69 return ERROR_OOM;
70 }
71
72 bp_commbuf = tmpbuf;
73 bp_commbufsize = bufsize;
74 return 0;
75}
76
Uwe Hermann91f4afa2011-07-28 08:13:25 +000077static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt,
78 unsigned int readcnt)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000079{
80 int i, ret = 0;
81
Sean Nelson84f7bce2010-01-09 23:56:41 +000082 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000083 if (!writecnt && !readcnt) {
Sean Nelson84f7bce2010-01-09 23:56:41 +000084 msg_perr("Zero length command!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000085 return 1;
86 }
Sean Nelson84f7bce2010-01-09 23:56:41 +000087 msg_pspew("Sending");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000088 for (i = 0; i < writecnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000089 msg_pspew(" 0x%02x", buf[i]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000090#ifdef FAKE_COMMUNICATION
91 /* Placate the caller for now. */
92 if (readcnt) {
93 buf[0] = 0x01;
94 memset(buf + 1, 0xff, readcnt - 1);
95 }
96 ret = 0;
97#else
98 if (writecnt)
99 ret = serialport_write(buf, writecnt);
100 if (ret)
101 return ret;
102 if (readcnt)
103 ret = serialport_read(buf, readcnt);
104 if (ret)
105 return ret;
106#endif
Sean Nelson84f7bce2010-01-09 23:56:41 +0000107 msg_pspew(", receiving");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000108 for (i = 0; i < readcnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000109 msg_pspew(" 0x%02x", buf[i]);
110 msg_pspew("\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000111 return 0;
112}
113
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000114static int buspirate_spi_send_command(struct flashctx *flash,
115 unsigned int writecnt,
116 unsigned int readcnt,
117 const unsigned char *writearr,
118 unsigned char *readarr);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000119
120static const struct spi_programmer spi_programmer_buspirate = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000121 .type = SPI_CONTROLLER_BUSPIRATE,
122 .max_data_read = 12,
123 .max_data_write = 12,
124 .command = buspirate_spi_send_command,
125 .multicommand = default_spi_send_multicommand,
126 .read = default_spi_read,
127 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000128 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000129};
130
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000131static const struct buspirate_spispeeds spispeeds[] = {
132 {"30k", 0x0},
133 {"125k", 0x1},
134 {"250k", 0x2},
135 {"1M", 0x3},
136 {"2M", 0x4},
137 {"2.6M", 0x5},
138 {"4M", 0x6},
139 {"8M", 0x7},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000140 {NULL, 0x0},
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000141};
142
David Hendricks8bb20212011-06-14 01:35:36 +0000143static int buspirate_spi_shutdown(void *data)
144{
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000145 int ret = 0, ret2 = 0;
146 /* 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 +0000147
148 /* Exit raw SPI mode (enter raw bitbang mode) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000149 bp_commbuf[0] = 0x00;
150 ret = buspirate_sendrecv(bp_commbuf, 1, 5);
David Hendricks8bb20212011-06-14 01:35:36 +0000151 if (ret)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000152 goto out_shutdown;
153 if (memcmp(bp_commbuf, "BBIO", 4)) {
David Hendricks8bb20212011-06-14 01:35:36 +0000154 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000155 ret = 1;
156 goto out_shutdown;
David Hendricks8bb20212011-06-14 01:35:36 +0000157 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000158 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[4]);
159 if (bp_commbuf[4] != '1') {
160 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[4]);
161 ret = 1;
162 goto out_shutdown;
David Hendricks8bb20212011-06-14 01:35:36 +0000163 }
164 /* Reset Bus Pirate (return to user terminal) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000165 bp_commbuf[0] = 0x0f;
166 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
David Hendricks8bb20212011-06-14 01:35:36 +0000167
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000168out_shutdown:
David Hendricks8bb20212011-06-14 01:35:36 +0000169 /* Shut down serial port communication */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000170 ret2 = serialport_shutdown(NULL);
171 /* Keep the oldest error, it is probably the best indicator. */
172 if (ret2 && !ret)
173 ret = ret2;
174 bp_commbufsize = 0;
175 free(bp_commbuf);
176 bp_commbuf = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000177 if (ret)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000178 msg_pdbg("Bus Pirate shutdown failed.\n");
179 else
180 msg_pdbg("Bus Pirate shutdown completed.\n");
David Hendricks8bb20212011-06-14 01:35:36 +0000181
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000182 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +0000183}
184
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000185int buspirate_spi_init(void)
186{
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000187 char *dev = NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000188 char *speed = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000189 int spispeed = 0x7;
190 int ret = 0;
191 int i;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000192
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000193 dev = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000194 if (!dev || !strlen(dev)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000195 msg_perr("No serial device given. Use flashrom -p "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000196 "buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000197 return 1;
198 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000199
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000200 speed = extract_programmer_param("spispeed");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000201 if (speed) {
202 for (i = 0; spispeeds[i].name; i++)
203 if (!strncasecmp(spispeeds[i].name, speed,
204 strlen(spispeeds[i].name))) {
205 spispeed = spispeeds[i].speed;
206 break;
207 }
208 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000209 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000210 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000211 free(speed);
212
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000213 /* This works because speeds numbering starts at 0 and is contiguous. */
Sean Nelson84f7bce2010-01-09 23:56:41 +0000214 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000215
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000216 /* Default buffer size is 19: 16 bytes data, 3 bytes control. */
217#define DEFAULT_BUFSIZE (16 + 3)
218 bp_commbuf = malloc(DEFAULT_BUFSIZE);
219 if (!bp_commbuf) {
220 bp_commbufsize = 0;
221 msg_perr("Out of memory!\n");
222 return ERROR_OOM;
223 }
224 bp_commbufsize = DEFAULT_BUFSIZE;
225
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000226 ret = buspirate_serialport_setup(dev);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000227 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000228 if (ret) {
229 bp_commbufsize = 0;
230 free(bp_commbuf);
231 bp_commbuf = NULL;
232 return ret;
233 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000234
David Hendricks8bb20212011-06-14 01:35:36 +0000235 if (register_shutdown(buspirate_spi_shutdown, NULL))
236 return 1;
237
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000238 /* This is the brute force version, but it should work. */
239 for (i = 0; i < 19; i++) {
240 /* Enter raw bitbang mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000241 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000242 /* Send the command, don't read the response. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000243 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000244 if (ret)
245 return ret;
246 /* Read any response and discard it. */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000247 sp_flush_incoming();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000248 }
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000249 /* USB is slow. The Bus Pirate is even slower. Apparently the flush
250 * action above is too fast or too early. Some stuff still remains in
251 * the pipe after the flush above, and one additional flush is not
252 * sufficient either. Use a 1.5 ms delay inside the loop to make
253 * mostly sure that at least one USB frame had time to arrive.
254 * Looping only 5 times is not sufficient and causes the
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000255 * occasional failure.
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000256 * Folding the delay into the loop above is not reliable either.
257 */
258 for (i = 0; i < 10; i++) {
259 usleep(1500);
260 /* Read any response and discard it. */
261 sp_flush_incoming();
262 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000263 /* Enter raw bitbang mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000264 bp_commbuf[0] = 0x00;
265 ret = buspirate_sendrecv(bp_commbuf, 1, 5);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000266 if (ret)
267 return ret;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000268 if (memcmp(bp_commbuf, "BBIO", 4)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000269 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000270 msg_pdbg("Got %02x%02x%02x%02x%02x\n",
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000271 bp_commbuf[0], bp_commbuf[1], bp_commbuf[2],
272 bp_commbuf[3], bp_commbuf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000273 return 1;
274 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000275 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[4]);
276 if (bp_commbuf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000277 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000278 bp_commbuf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000279 return 1;
280 }
281 /* Enter raw SPI mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000282 bp_commbuf[0] = 0x01;
283 ret = buspirate_sendrecv(bp_commbuf, 1, 4);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000284 if (ret)
285 return ret;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000286 if (memcmp(bp_commbuf, "SPI", 3)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000287 msg_perr("Entering raw SPI mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000288 msg_pdbg("Got %02x%02x%02x%02x\n",
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000289 bp_commbuf[0], bp_commbuf[1], bp_commbuf[2],
290 bp_commbuf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000291 return 1;
292 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000293 msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[3]);
294 if (bp_commbuf[3] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000295 msg_perr("Can't handle raw SPI mode version %c!\n",
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000296 bp_commbuf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000297 return 1;
298 }
299
300 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000301 bp_commbuf[0] = 0x40 | 0xb;
302 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000303 if (ret)
304 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000305 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000306 msg_perr("Protocol error while setting power/CS/AUX!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000307 return 1;
308 }
309
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000310 /* Set SPI speed */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000311 bp_commbuf[0] = 0x60 | spispeed;
312 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000313 if (ret)
314 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000315 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000316 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000317 return 1;
318 }
319
320 /* Set SPI config: output type, idle, clock edge, sample */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000321 bp_commbuf[0] = 0x80 | 0xa;
322 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000323 if (ret)
324 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000325 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000326 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000327 return 1;
328 }
329
330 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000331 bp_commbuf[0] = 0x03;
332 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000333 if (ret)
334 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000335 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000336 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000337 return 1;
338 }
339
Michael Karcherb9dbe482011-05-11 17:07:07 +0000340 register_spi_programmer(&spi_programmer_buspirate);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000341
342 return 0;
343}
344
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000345static int buspirate_spi_send_command(struct flashctx *flash,
346 unsigned int writecnt,
347 unsigned int readcnt,
348 const unsigned char *writearr,
349 unsigned char *readarr)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000350{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000351 unsigned int i = 0;
352 int ret = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000353
354 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
355 return SPI_INVALID_LENGTH;
356
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000357 /* 3 bytes extra for CS#, len, CS#. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000358 if (buspirate_commbuf_grow(writecnt + readcnt + 3))
359 return ERROR_OOM;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000360
361 /* Assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000362 bp_commbuf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000363
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000364 bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1);
365 memcpy(bp_commbuf + i, writearr, writecnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000366 i += writecnt;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000367 memset(bp_commbuf + i, 0, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000368
369 i += readcnt;
370 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000371 bp_commbuf[i++] = 0x03;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000372
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000373 ret = buspirate_sendrecv(bp_commbuf, i, i);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000374
375 if (ret) {
376 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000377 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000378 }
379
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000380 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000381 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000382 return SPI_GENERIC_ERROR;
383 }
384
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000385 if (bp_commbuf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000386 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000387 return SPI_GENERIC_ERROR;
388 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000389
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000390 if (bp_commbuf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000391 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000392 return SPI_GENERIC_ERROR;
393 }
394
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000395 /* Skip CS#, length, writearr. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000396 memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000397
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000398 return ret;
399}