blob: 848868c7df6ec03958b0276a2efc9a33fb4fa195 [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);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +000042 /* FIXME: Error checking */
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000043 return 0;
44}
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000045#else
46#define buspirate_serialport_setup(...) 0
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +000047#define serialport_shutdown(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000048#define serialport_write(...) 0
49#define serialport_read(...) 0
Patrick Georgi3b6237d2010-01-06 19:09:40 +000050#define sp_flush_incoming(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000051#endif
52
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +000053static unsigned char *bp_commbuf = NULL;
54static int bp_commbufsize = 0;
55
56static int buspirate_commbuf_grow(int bufsize)
57{
58 unsigned char *tmpbuf;
59
60 /* Never shrink. realloc() calls are expensive. */
61 if (bufsize <= bp_commbufsize)
62 return 0;
63
64 tmpbuf = realloc(bp_commbuf, bufsize);
65 if (!tmpbuf) {
66 /* Keep the existing buffer because memory is already tight. */
67 msg_perr("Out of memory!\n");
68 return ERROR_OOM;
69 }
70
71 bp_commbuf = tmpbuf;
72 bp_commbufsize = bufsize;
73 return 0;
74}
75
Uwe Hermann91f4afa2011-07-28 08:13:25 +000076static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt,
77 unsigned int readcnt)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000078{
79 int i, ret = 0;
80
Sean Nelson84f7bce2010-01-09 23:56:41 +000081 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000082 if (!writecnt && !readcnt) {
Sean Nelson84f7bce2010-01-09 23:56:41 +000083 msg_perr("Zero length command!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000084 return 1;
85 }
Sean Nelson84f7bce2010-01-09 23:56:41 +000086 msg_pspew("Sending");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000087 for (i = 0; i < writecnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000088 msg_pspew(" 0x%02x", buf[i]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000089#ifdef FAKE_COMMUNICATION
90 /* Placate the caller for now. */
91 if (readcnt) {
92 buf[0] = 0x01;
93 memset(buf + 1, 0xff, readcnt - 1);
94 }
95 ret = 0;
96#else
97 if (writecnt)
98 ret = serialport_write(buf, writecnt);
99 if (ret)
100 return ret;
101 if (readcnt)
102 ret = serialport_read(buf, readcnt);
103 if (ret)
104 return ret;
105#endif
Sean Nelson84f7bce2010-01-09 23:56:41 +0000106 msg_pspew(", receiving");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000107 for (i = 0; i < readcnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000108 msg_pspew(" 0x%02x", buf[i]);
109 msg_pspew("\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000110 return 0;
111}
112
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000113static int buspirate_spi_send_command(struct flashctx *flash,
114 unsigned int writecnt,
115 unsigned int readcnt,
116 const unsigned char *writearr,
117 unsigned char *readarr);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000118
119static const struct spi_programmer spi_programmer_buspirate = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000120 .type = SPI_CONTROLLER_BUSPIRATE,
121 .max_data_read = 12,
122 .max_data_write = 12,
123 .command = buspirate_spi_send_command,
124 .multicommand = default_spi_send_multicommand,
125 .read = default_spi_read,
126 .write_256 = default_spi_write_256,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000127};
128
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000129static const struct buspirate_spispeeds spispeeds[] = {
130 {"30k", 0x0},
131 {"125k", 0x1},
132 {"250k", 0x2},
133 {"1M", 0x3},
134 {"2M", 0x4},
135 {"2.6M", 0x5},
136 {"4M", 0x6},
137 {"8M", 0x7},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000138 {NULL, 0x0},
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000139};
140
David Hendricks8bb20212011-06-14 01:35:36 +0000141static int buspirate_spi_shutdown(void *data)
142{
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000143 int ret = 0, ret2 = 0;
144 /* 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 +0000145
146 /* Exit raw SPI mode (enter raw bitbang mode) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000147 bp_commbuf[0] = 0x00;
148 ret = buspirate_sendrecv(bp_commbuf, 1, 5);
David Hendricks8bb20212011-06-14 01:35:36 +0000149 if (ret)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000150 goto out_shutdown;
151 if (memcmp(bp_commbuf, "BBIO", 4)) {
David Hendricks8bb20212011-06-14 01:35:36 +0000152 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000153 ret = 1;
154 goto out_shutdown;
David Hendricks8bb20212011-06-14 01:35:36 +0000155 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000156 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[4]);
157 if (bp_commbuf[4] != '1') {
158 msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[4]);
159 ret = 1;
160 goto out_shutdown;
David Hendricks8bb20212011-06-14 01:35:36 +0000161 }
162 /* Reset Bus Pirate (return to user terminal) */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000163 bp_commbuf[0] = 0x0f;
164 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
David Hendricks8bb20212011-06-14 01:35:36 +0000165
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000166out_shutdown:
David Hendricks8bb20212011-06-14 01:35:36 +0000167 /* Shut down serial port communication */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000168 ret2 = serialport_shutdown(NULL);
169 /* Keep the oldest error, it is probably the best indicator. */
170 if (ret2 && !ret)
171 ret = ret2;
172 bp_commbufsize = 0;
173 free(bp_commbuf);
174 bp_commbuf = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000175 if (ret)
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000176 msg_pdbg("Bus Pirate shutdown failed.\n");
177 else
178 msg_pdbg("Bus Pirate shutdown completed.\n");
David Hendricks8bb20212011-06-14 01:35:36 +0000179
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000180 return ret;
David Hendricks8bb20212011-06-14 01:35:36 +0000181}
182
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000183int buspirate_spi_init(void)
184{
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000185 char *dev = NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000186 char *speed = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000187 int spispeed = 0x7;
188 int ret = 0;
189 int i;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000190
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000191 dev = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000192 if (!dev || !strlen(dev)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000193 msg_perr("No serial device given. Use flashrom -p "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000194 "buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000195 return 1;
196 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000197
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000198 speed = extract_programmer_param("spispeed");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000199 if (speed) {
200 for (i = 0; spispeeds[i].name; i++)
201 if (!strncasecmp(spispeeds[i].name, speed,
202 strlen(spispeeds[i].name))) {
203 spispeed = spispeeds[i].speed;
204 break;
205 }
206 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000207 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000208 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000209 free(speed);
210
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000211 /* This works because speeds numbering starts at 0 and is contiguous. */
Sean Nelson84f7bce2010-01-09 23:56:41 +0000212 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000213
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000214 /* Default buffer size is 19: 16 bytes data, 3 bytes control. */
215#define DEFAULT_BUFSIZE (16 + 3)
216 bp_commbuf = malloc(DEFAULT_BUFSIZE);
217 if (!bp_commbuf) {
218 bp_commbufsize = 0;
219 msg_perr("Out of memory!\n");
220 return ERROR_OOM;
221 }
222 bp_commbufsize = DEFAULT_BUFSIZE;
223
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000224 ret = buspirate_serialport_setup(dev);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000225 free(dev);
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000226 if (ret) {
227 bp_commbufsize = 0;
228 free(bp_commbuf);
229 bp_commbuf = NULL;
230 return ret;
231 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000232
David Hendricks8bb20212011-06-14 01:35:36 +0000233 if (register_shutdown(buspirate_spi_shutdown, NULL))
234 return 1;
235
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000236 /* This is the brute force version, but it should work. */
237 for (i = 0; i < 19; i++) {
238 /* Enter raw bitbang mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000239 bp_commbuf[0] = 0x00;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000240 /* Send the command, don't read the response. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000241 ret = buspirate_sendrecv(bp_commbuf, 1, 0);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000242 if (ret)
243 return ret;
244 /* Read any response and discard it. */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000245 sp_flush_incoming();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000246 }
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000247 /* USB is slow. The Bus Pirate is even slower. Apparently the flush
248 * action above is too fast or too early. Some stuff still remains in
249 * the pipe after the flush above, and one additional flush is not
250 * sufficient either. Use a 1.5 ms delay inside the loop to make
251 * mostly sure that at least one USB frame had time to arrive.
252 * Looping only 5 times is not sufficient and causes the
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000253 * occasional failure.
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000254 * Folding the delay into the loop above is not reliable either.
255 */
256 for (i = 0; i < 10; i++) {
257 usleep(1500);
258 /* Read any response and discard it. */
259 sp_flush_incoming();
260 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000261 /* Enter raw bitbang mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000262 bp_commbuf[0] = 0x00;
263 ret = buspirate_sendrecv(bp_commbuf, 1, 5);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000264 if (ret)
265 return ret;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000266 if (memcmp(bp_commbuf, "BBIO", 4)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000267 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000268 msg_pdbg("Got %02x%02x%02x%02x%02x\n",
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000269 bp_commbuf[0], bp_commbuf[1], bp_commbuf[2],
270 bp_commbuf[3], bp_commbuf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000271 return 1;
272 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000273 msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[4]);
274 if (bp_commbuf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000275 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000276 bp_commbuf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000277 return 1;
278 }
279 /* Enter raw SPI mode */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000280 bp_commbuf[0] = 0x01;
281 ret = buspirate_sendrecv(bp_commbuf, 1, 4);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000282 if (ret)
283 return ret;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000284 if (memcmp(bp_commbuf, "SPI", 3)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000285 msg_perr("Entering raw SPI mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000286 msg_pdbg("Got %02x%02x%02x%02x\n",
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000287 bp_commbuf[0], bp_commbuf[1], bp_commbuf[2],
288 bp_commbuf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000289 return 1;
290 }
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000291 msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[3]);
292 if (bp_commbuf[3] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000293 msg_perr("Can't handle raw SPI mode version %c!\n",
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000294 bp_commbuf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000295 return 1;
296 }
297
298 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000299 bp_commbuf[0] = 0x40 | 0xb;
300 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000301 if (ret)
302 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000303 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000304 msg_perr("Protocol error while setting power/CS/AUX!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000305 return 1;
306 }
307
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000308 /* Set SPI speed */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000309 bp_commbuf[0] = 0x60 | spispeed;
310 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000311 if (ret)
312 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000313 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000314 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000315 return 1;
316 }
317
318 /* Set SPI config: output type, idle, clock edge, sample */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000319 bp_commbuf[0] = 0x80 | 0xa;
320 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000321 if (ret)
322 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000323 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000324 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000325 return 1;
326 }
327
328 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000329 bp_commbuf[0] = 0x03;
330 ret = buspirate_sendrecv(bp_commbuf, 1, 1);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000331 if (ret)
332 return 1;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000333 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000334 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000335 return 1;
336 }
337
Michael Karcherb9dbe482011-05-11 17:07:07 +0000338 register_spi_programmer(&spi_programmer_buspirate);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000339
340 return 0;
341}
342
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000343static int buspirate_spi_send_command(struct flashctx *flash,
344 unsigned int writecnt,
345 unsigned int readcnt,
346 const unsigned char *writearr,
347 unsigned char *readarr)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000348{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000349 unsigned int i = 0;
350 int ret = 0;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000351
352 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
353 return SPI_INVALID_LENGTH;
354
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000355 /* 3 bytes extra for CS#, len, CS#. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000356 if (buspirate_commbuf_grow(writecnt + readcnt + 3))
357 return ERROR_OOM;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000358
359 /* Assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000360 bp_commbuf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000361
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000362 bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1);
363 memcpy(bp_commbuf + i, writearr, writecnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000364 i += writecnt;
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000365 memset(bp_commbuf + i, 0, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000366
367 i += readcnt;
368 /* De-assert CS# */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000369 bp_commbuf[i++] = 0x03;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000370
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000371 ret = buspirate_sendrecv(bp_commbuf, i, i);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000372
373 if (ret) {
374 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000375 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000376 }
377
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000378 if (bp_commbuf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000379 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000380 return SPI_GENERIC_ERROR;
381 }
382
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000383 if (bp_commbuf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000384 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000385 return SPI_GENERIC_ERROR;
386 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000387
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000388 if (bp_commbuf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000389 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000390 return SPI_GENERIC_ERROR;
391 }
392
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000393 /* Skip CS#, length, writearr. */
Carl-Daniel Hailfinger316fdfb2012-06-08 15:27:47 +0000394 memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt);
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000395
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000396 return ret;
397}