blob: 961fd820f73c779b386b631e97d00411fcc05e08 [file] [log] [blame]
hailfinger9c5add72009-11-24 00:20:03 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfinger39d159a2010-05-21 23:09:42 +00004 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
hailfinger9c5add72009-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.
hailfinger9c5add72009-11-24 00:20:03 +000014 */
15
16#include <stdio.h>
Edward O'Callaghanb4300ca2019-09-03 16:15:21 +100017#include <strings.h>
hailfinger9c5add72009-11-24 00:20:03 +000018#include <string.h>
19#include <stdlib.h>
20#include <ctype.h>
hailfingerfa76f932010-09-16 22:34:25 +000021#include <unistd.h>
hailfinger9c5add72009-11-24 00:20:03 +000022#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000023#include "programmer.h"
hailfinger9c5add72009-11-24 00:20:03 +000024#include "spi.h"
25
26/* Change this to #define if you want to test without a serial implementation */
27#undef FAKE_COMMUNICATION
28
hailfingere20dc562011-06-09 20:06:34 +000029struct buspirate_spispeeds {
30 const char *name;
31 const int speed;
32};
33
hailfinger9c5add72009-11-24 00:20:03 +000034#ifndef FAKE_COMMUNICATION
hailfinger1ff33dc2010-07-03 11:02:10 +000035static int buspirate_serialport_setup(char *dev)
hailfinger9c5add72009-11-24 00:20:03 +000036{
37 /* 115200bps, 8 databits, no parity, 1 stopbit */
38 sp_fd = sp_openserport(dev, 115200);
39 return 0;
40}
hailfinger9c5add72009-11-24 00:20:03 +000041#else
42#define buspirate_serialport_setup(...) 0
hailfinger852163c2010-01-06 16:09:10 +000043#define serialport_shutdown(...) 0
hailfinger9c5add72009-11-24 00:20:03 +000044#define serialport_write(...) 0
45#define serialport_read(...) 0
oxygene8fede2d2010-01-06 19:09:40 +000046#define sp_flush_incoming(...) 0
hailfinger9c5add72009-11-24 00:20:03 +000047#endif
48
uwe8d342eb2011-07-28 08:13:25 +000049static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt,
50 unsigned int readcnt)
hailfinger9c5add72009-11-24 00:20:03 +000051{
52 int i, ret = 0;
53
snelson865ae752010-01-09 23:56:41 +000054 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
hailfinger9c5add72009-11-24 00:20:03 +000055 if (!writecnt && !readcnt) {
snelson865ae752010-01-09 23:56:41 +000056 msg_perr("Zero length command!\n");
hailfinger9c5add72009-11-24 00:20:03 +000057 return 1;
58 }
snelson865ae752010-01-09 23:56:41 +000059 msg_pspew("Sending");
hailfinger9c5add72009-11-24 00:20:03 +000060 for (i = 0; i < writecnt; i++)
snelson865ae752010-01-09 23:56:41 +000061 msg_pspew(" 0x%02x", buf[i]);
hailfinger9c5add72009-11-24 00:20:03 +000062#ifdef FAKE_COMMUNICATION
63 /* Placate the caller for now. */
64 if (readcnt) {
65 buf[0] = 0x01;
66 memset(buf + 1, 0xff, readcnt - 1);
67 }
68 ret = 0;
69#else
70 if (writecnt)
71 ret = serialport_write(buf, writecnt);
72 if (ret)
73 return ret;
74 if (readcnt)
75 ret = serialport_read(buf, readcnt);
76 if (ret)
77 return ret;
78#endif
snelson865ae752010-01-09 23:56:41 +000079 msg_pspew(", receiving");
hailfinger9c5add72009-11-24 00:20:03 +000080 for (i = 0; i < readcnt; i++)
snelson865ae752010-01-09 23:56:41 +000081 msg_pspew(" 0x%02x", buf[i]);
82 msg_pspew("\n");
hailfinger9c5add72009-11-24 00:20:03 +000083 return 0;
84}
85
Souvik Ghoshd75cd672016-06-17 14:21:39 -070086static int buspirate_spi_send_command(const struct flashctx *flash,
87 unsigned int writecnt,
88 unsigned int readcnt,
89 const unsigned char *writearr,
90 unsigned char *readarr);
mkarcherd264e9e2011-05-11 17:07:07 +000091
Patrick Georgif4f1e2f2017-03-10 17:38:40 +010092static const struct spi_master spi_master_buspirate = {
uwe8d342eb2011-07-28 08:13:25 +000093 .type = SPI_CONTROLLER_BUSPIRATE,
Edward O'Callaghana6673bd2019-06-24 15:22:28 +100094 .features = SPI_MASTER_4BA,
uwe8d342eb2011-07-28 08:13:25 +000095 .max_data_read = 12,
96 .max_data_write = 12,
97 .command = buspirate_spi_send_command,
98 .multicommand = default_spi_send_multicommand,
99 .read = default_spi_read,
100 .write_256 = default_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +0000101};
102
hailfinger6e5a52a2009-11-24 18:27:10 +0000103static const struct buspirate_spispeeds spispeeds[] = {
104 {"30k", 0x0},
105 {"125k", 0x1},
106 {"250k", 0x2},
107 {"1M", 0x3},
108 {"2M", 0x4},
109 {"2.6M", 0x5},
110 {"4M", 0x6},
111 {"8M", 0x7},
uwe8d342eb2011-07-28 08:13:25 +0000112 {NULL, 0x0},
hailfinger6e5a52a2009-11-24 18:27:10 +0000113};
114
David Hendricks93784b42016-08-09 17:00:38 -0700115static int buspirate_spi_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +0000116{
117 unsigned char buf[5];
118 int ret = 0;
119
120 /* Exit raw SPI mode (enter raw bitbang mode) */
121 buf[0] = 0x00;
122 ret = buspirate_sendrecv(buf, 1, 5);
123 if (ret)
124 return ret;
125 if (memcmp(buf, "BBIO", 4)) {
126 msg_perr("Entering raw bitbang mode failed!\n");
127 return 1;
128 }
129 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
130 if (buf[4] != '1') {
131 msg_perr("Can't handle raw bitbang mode version %c!\n",
132 buf[4]);
133 return 1;
134 }
135 /* Reset Bus Pirate (return to user terminal) */
136 buf[0] = 0x0f;
137 ret = buspirate_sendrecv(buf, 1, 0);
138 if (ret)
139 return ret;
140
141 /* Shut down serial port communication */
142 ret = serialport_shutdown(NULL);
143 if (ret)
144 return ret;
145 msg_pdbg("Bus Pirate shutdown completed.\n");
146
147 return 0;
148}
149
David Hendricksac1d25c2016-08-09 17:00:58 -0700150int buspirate_spi_init(void)
hailfinger9c5add72009-11-24 00:20:03 +0000151{
152 unsigned char buf[512];
hailfinger9c5add72009-11-24 00:20:03 +0000153 char *dev = NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000154 char *speed = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +0000155 int spispeed = 0x7;
156 int ret = 0;
157 int i;
hailfinger9c5add72009-11-24 00:20:03 +0000158
hailfingerddeb4ac2010-07-08 10:13:37 +0000159 dev = extract_programmer_param("dev");
hailfinger1ef766d2010-07-06 09:55:48 +0000160 if (!dev || !strlen(dev)) {
snelson865ae752010-01-09 23:56:41 +0000161 msg_perr("No serial device given. Use flashrom -p "
hailfinger90c7d542010-05-31 15:27:27 +0000162 "buspirate_spi:dev=/dev/ttyUSB0\n");
hailfinger9c5add72009-11-24 00:20:03 +0000163 return 1;
164 }
hailfinger1ef766d2010-07-06 09:55:48 +0000165
hailfingerddeb4ac2010-07-08 10:13:37 +0000166 speed = extract_programmer_param("spispeed");
hailfinger6e5a52a2009-11-24 18:27:10 +0000167 if (speed) {
168 for (i = 0; spispeeds[i].name; i++)
169 if (!strncasecmp(spispeeds[i].name, speed,
170 strlen(spispeeds[i].name))) {
171 spispeed = spispeeds[i].speed;
172 break;
173 }
174 if (!spispeeds[i].name)
snelson865ae752010-01-09 23:56:41 +0000175 msg_perr("Invalid SPI speed, using default.\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000176 }
hailfinger1ef766d2010-07-06 09:55:48 +0000177 free(speed);
178
hailfinger6e5a52a2009-11-24 18:27:10 +0000179 /* This works because speeds numbering starts at 0 and is contiguous. */
snelson865ae752010-01-09 23:56:41 +0000180 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
hailfinger9c5add72009-11-24 00:20:03 +0000181
182 ret = buspirate_serialport_setup(dev);
183 if (ret)
184 return ret;
hailfinger1ef766d2010-07-06 09:55:48 +0000185 free(dev);
hailfinger9c5add72009-11-24 00:20:03 +0000186
dhendrix0ffc2eb2011-06-14 01:35:36 +0000187 if (register_shutdown(buspirate_spi_shutdown, NULL))
188 return 1;
189
hailfinger9c5add72009-11-24 00:20:03 +0000190 /* This is the brute force version, but it should work. */
191 for (i = 0; i < 19; i++) {
192 /* Enter raw bitbang mode */
193 buf[0] = 0x00;
194 /* Send the command, don't read the response. */
195 ret = buspirate_sendrecv(buf, 1, 0);
196 if (ret)
197 return ret;
198 /* Read any response and discard it. */
oxygene8fede2d2010-01-06 19:09:40 +0000199 sp_flush_incoming();
hailfinger9c5add72009-11-24 00:20:03 +0000200 }
hailfingerfa76f932010-09-16 22:34:25 +0000201 /* USB is slow. The Bus Pirate is even slower. Apparently the flush
202 * action above is too fast or too early. Some stuff still remains in
203 * the pipe after the flush above, and one additional flush is not
204 * sufficient either. Use a 1.5 ms delay inside the loop to make
205 * mostly sure that at least one USB frame had time to arrive.
206 * Looping only 5 times is not sufficient and causes the
stefanct371e7e82011-07-07 19:56:58 +0000207 * occasional failure.
hailfingerfa76f932010-09-16 22:34:25 +0000208 * Folding the delay into the loop above is not reliable either.
209 */
210 for (i = 0; i < 10; i++) {
211 usleep(1500);
212 /* Read any response and discard it. */
213 sp_flush_incoming();
214 }
hailfinger9c5add72009-11-24 00:20:03 +0000215 /* Enter raw bitbang mode */
216 buf[0] = 0x00;
217 ret = buspirate_sendrecv(buf, 1, 5);
218 if (ret)
219 return ret;
220 if (memcmp(buf, "BBIO", 4)) {
snelson865ae752010-01-09 23:56:41 +0000221 msg_perr("Entering raw bitbang mode failed!\n");
hailfingerfa76f932010-09-16 22:34:25 +0000222 msg_pdbg("Got %02x%02x%02x%02x%02x\n",
223 buf[0], buf[1], buf[2], buf[3], buf[4]);
hailfinger9c5add72009-11-24 00:20:03 +0000224 return 1;
225 }
snelson865ae752010-01-09 23:56:41 +0000226 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
hailfinger9c5add72009-11-24 00:20:03 +0000227 if (buf[4] != '1') {
snelson865ae752010-01-09 23:56:41 +0000228 msg_perr("Can't handle raw bitbang mode version %c!\n",
hailfinger9c5add72009-11-24 00:20:03 +0000229 buf[4]);
230 return 1;
231 }
232 /* Enter raw SPI mode */
233 buf[0] = 0x01;
234 ret = buspirate_sendrecv(buf, 1, 4);
hailfingerfa76f932010-09-16 22:34:25 +0000235 if (ret)
236 return ret;
hailfinger9c5add72009-11-24 00:20:03 +0000237 if (memcmp(buf, "SPI", 3)) {
snelson865ae752010-01-09 23:56:41 +0000238 msg_perr("Entering raw SPI mode failed!\n");
hailfingerfa76f932010-09-16 22:34:25 +0000239 msg_pdbg("Got %02x%02x%02x%02x\n",
240 buf[0], buf[1], buf[2], buf[3]);
hailfinger9c5add72009-11-24 00:20:03 +0000241 return 1;
242 }
snelson865ae752010-01-09 23:56:41 +0000243 msg_pdbg("Raw SPI mode version %c\n", buf[3]);
hailfinger9c5add72009-11-24 00:20:03 +0000244 if (buf[3] != '1') {
snelson865ae752010-01-09 23:56:41 +0000245 msg_perr("Can't handle raw SPI mode version %c!\n",
hailfinger9c5add72009-11-24 00:20:03 +0000246 buf[3]);
247 return 1;
248 }
249
250 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
251 buf[0] = 0x40 | 0xb;
252 ret = buspirate_sendrecv(buf, 1, 1);
253 if (ret)
254 return 1;
255 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000256 msg_perr("Protocol error while setting power/CS/AUX!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000257 return 1;
258 }
259
hailfinger6e5a52a2009-11-24 18:27:10 +0000260 /* Set SPI speed */
261 buf[0] = 0x60 | spispeed;
hailfinger9c5add72009-11-24 00:20:03 +0000262 ret = buspirate_sendrecv(buf, 1, 1);
263 if (ret)
264 return 1;
265 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000266 msg_perr("Protocol error while setting SPI speed!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000267 return 1;
268 }
269
270 /* Set SPI config: output type, idle, clock edge, sample */
271 buf[0] = 0x80 | 0xa;
272 ret = buspirate_sendrecv(buf, 1, 1);
273 if (ret)
274 return 1;
275 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000276 msg_perr("Protocol error while setting SPI config!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000277 return 1;
278 }
279
280 /* De-assert CS# */
281 buf[0] = 0x03;
282 ret = buspirate_sendrecv(buf, 1, 1);
283 if (ret)
284 return 1;
285 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000286 msg_perr("Protocol error while raising CS#!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000287 return 1;
288 }
289
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100290 register_spi_master(&spi_master_buspirate);
hailfinger9c5add72009-11-24 00:20:03 +0000291
292 return 0;
293}
294
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700295static int buspirate_spi_send_command(const struct flashctx *flash,
296 unsigned int writecnt,
297 unsigned int readcnt,
298 const unsigned char *writearr,
299 unsigned char *readarr)
hailfinger9c5add72009-11-24 00:20:03 +0000300{
301 static unsigned char *buf = NULL;
stefanctc5eb8a92011-11-23 09:13:48 +0000302 unsigned int i = 0;
303 int ret = 0;
hailfinger9c5add72009-11-24 00:20:03 +0000304
305 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
306 return SPI_INVALID_LENGTH;
307
hailfinger17d3e912010-07-29 16:24:09 +0000308 /* 3 bytes extra for CS#, len, CS#. */
309 buf = realloc(buf, writecnt + readcnt + 3);
hailfinger9c5add72009-11-24 00:20:03 +0000310 if (!buf) {
snelson865ae752010-01-09 23:56:41 +0000311 msg_perr("Out of memory!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000312 exit(1); // -1
313 }
314
315 /* Assert CS# */
316 buf[i++] = 0x02;
hailfinger17d3e912010-07-29 16:24:09 +0000317
318 buf[i++] = 0x10 | (writecnt + readcnt - 1);
319 memcpy(buf + i, writearr, writecnt);
320 i += writecnt;
321 memset(buf + i, 0, readcnt);
322
323 i += readcnt;
324 /* De-assert CS# */
325 buf[i++] = 0x03;
326
327 ret = buspirate_sendrecv(buf, i, i);
328
329 if (ret) {
330 msg_perr("Bus Pirate communication error!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000331 return SPI_GENERIC_ERROR;
hailfinger17d3e912010-07-29 16:24:09 +0000332 }
333
hailfinger9c5add72009-11-24 00:20:03 +0000334 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000335 msg_perr("Protocol error while lowering CS#!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000336 return SPI_GENERIC_ERROR;
337 }
338
hailfinger17d3e912010-07-29 16:24:09 +0000339 if (buf[1] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000340 msg_perr("Protocol error while reading/writing SPI!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000341 return SPI_GENERIC_ERROR;
342 }
hailfinger9c5add72009-11-24 00:20:03 +0000343
hailfinger17d3e912010-07-29 16:24:09 +0000344 if (buf[i - 1] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000345 msg_perr("Protocol error while raising CS#!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000346 return SPI_GENERIC_ERROR;
347 }
348
hailfinger17d3e912010-07-29 16:24:09 +0000349 /* Skip CS#, length, writearr. */
350 memcpy(readarr, buf + 2 + writecnt, readcnt);
351
hailfinger9c5add72009-11-24 00:20:03 +0000352 return ret;
353}