blob: 744874c40de388fd800bc6706f30b1cea06a8bf8 [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.
14 *
hailfinger9c5add72009-11-24 00:20:03 +000015 */
16
17#include <stdio.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,
94 .max_data_read = 12,
95 .max_data_write = 12,
96 .command = buspirate_spi_send_command,
97 .multicommand = default_spi_send_multicommand,
98 .read = default_spi_read,
99 .write_256 = default_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +0000100};
101
hailfinger6e5a52a2009-11-24 18:27:10 +0000102static const struct buspirate_spispeeds spispeeds[] = {
103 {"30k", 0x0},
104 {"125k", 0x1},
105 {"250k", 0x2},
106 {"1M", 0x3},
107 {"2M", 0x4},
108 {"2.6M", 0x5},
109 {"4M", 0x6},
110 {"8M", 0x7},
uwe8d342eb2011-07-28 08:13:25 +0000111 {NULL, 0x0},
hailfinger6e5a52a2009-11-24 18:27:10 +0000112};
113
David Hendricks93784b42016-08-09 17:00:38 -0700114static int buspirate_spi_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +0000115{
116 unsigned char buf[5];
117 int ret = 0;
118
119 /* Exit raw SPI mode (enter raw bitbang mode) */
120 buf[0] = 0x00;
121 ret = buspirate_sendrecv(buf, 1, 5);
122 if (ret)
123 return ret;
124 if (memcmp(buf, "BBIO", 4)) {
125 msg_perr("Entering raw bitbang mode failed!\n");
126 return 1;
127 }
128 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
129 if (buf[4] != '1') {
130 msg_perr("Can't handle raw bitbang mode version %c!\n",
131 buf[4]);
132 return 1;
133 }
134 /* Reset Bus Pirate (return to user terminal) */
135 buf[0] = 0x0f;
136 ret = buspirate_sendrecv(buf, 1, 0);
137 if (ret)
138 return ret;
139
140 /* Shut down serial port communication */
141 ret = serialport_shutdown(NULL);
142 if (ret)
143 return ret;
144 msg_pdbg("Bus Pirate shutdown completed.\n");
145
146 return 0;
147}
148
David Hendricksac1d25c2016-08-09 17:00:58 -0700149int buspirate_spi_init(void)
hailfinger9c5add72009-11-24 00:20:03 +0000150{
151 unsigned char buf[512];
hailfinger9c5add72009-11-24 00:20:03 +0000152 char *dev = NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000153 char *speed = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +0000154 int spispeed = 0x7;
155 int ret = 0;
156 int i;
hailfinger9c5add72009-11-24 00:20:03 +0000157
hailfingerddeb4ac2010-07-08 10:13:37 +0000158 dev = extract_programmer_param("dev");
hailfinger1ef766d2010-07-06 09:55:48 +0000159 if (!dev || !strlen(dev)) {
snelson865ae752010-01-09 23:56:41 +0000160 msg_perr("No serial device given. Use flashrom -p "
hailfinger90c7d542010-05-31 15:27:27 +0000161 "buspirate_spi:dev=/dev/ttyUSB0\n");
hailfinger9c5add72009-11-24 00:20:03 +0000162 return 1;
163 }
hailfinger1ef766d2010-07-06 09:55:48 +0000164
hailfingerddeb4ac2010-07-08 10:13:37 +0000165 speed = extract_programmer_param("spispeed");
hailfinger6e5a52a2009-11-24 18:27:10 +0000166 if (speed) {
167 for (i = 0; spispeeds[i].name; i++)
168 if (!strncasecmp(spispeeds[i].name, speed,
169 strlen(spispeeds[i].name))) {
170 spispeed = spispeeds[i].speed;
171 break;
172 }
173 if (!spispeeds[i].name)
snelson865ae752010-01-09 23:56:41 +0000174 msg_perr("Invalid SPI speed, using default.\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000175 }
hailfinger1ef766d2010-07-06 09:55:48 +0000176 free(speed);
177
hailfinger6e5a52a2009-11-24 18:27:10 +0000178 /* This works because speeds numbering starts at 0 and is contiguous. */
snelson865ae752010-01-09 23:56:41 +0000179 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
hailfinger9c5add72009-11-24 00:20:03 +0000180
181 ret = buspirate_serialport_setup(dev);
182 if (ret)
183 return ret;
hailfinger1ef766d2010-07-06 09:55:48 +0000184 free(dev);
hailfinger9c5add72009-11-24 00:20:03 +0000185
dhendrix0ffc2eb2011-06-14 01:35:36 +0000186 if (register_shutdown(buspirate_spi_shutdown, NULL))
187 return 1;
188
hailfinger9c5add72009-11-24 00:20:03 +0000189 /* This is the brute force version, but it should work. */
190 for (i = 0; i < 19; i++) {
191 /* Enter raw bitbang mode */
192 buf[0] = 0x00;
193 /* Send the command, don't read the response. */
194 ret = buspirate_sendrecv(buf, 1, 0);
195 if (ret)
196 return ret;
197 /* Read any response and discard it. */
oxygene8fede2d2010-01-06 19:09:40 +0000198 sp_flush_incoming();
hailfinger9c5add72009-11-24 00:20:03 +0000199 }
hailfingerfa76f932010-09-16 22:34:25 +0000200 /* USB is slow. The Bus Pirate is even slower. Apparently the flush
201 * action above is too fast or too early. Some stuff still remains in
202 * the pipe after the flush above, and one additional flush is not
203 * sufficient either. Use a 1.5 ms delay inside the loop to make
204 * mostly sure that at least one USB frame had time to arrive.
205 * Looping only 5 times is not sufficient and causes the
stefanct371e7e82011-07-07 19:56:58 +0000206 * occasional failure.
hailfingerfa76f932010-09-16 22:34:25 +0000207 * Folding the delay into the loop above is not reliable either.
208 */
209 for (i = 0; i < 10; i++) {
210 usleep(1500);
211 /* Read any response and discard it. */
212 sp_flush_incoming();
213 }
hailfinger9c5add72009-11-24 00:20:03 +0000214 /* Enter raw bitbang mode */
215 buf[0] = 0x00;
216 ret = buspirate_sendrecv(buf, 1, 5);
217 if (ret)
218 return ret;
219 if (memcmp(buf, "BBIO", 4)) {
snelson865ae752010-01-09 23:56:41 +0000220 msg_perr("Entering raw bitbang mode failed!\n");
hailfingerfa76f932010-09-16 22:34:25 +0000221 msg_pdbg("Got %02x%02x%02x%02x%02x\n",
222 buf[0], buf[1], buf[2], buf[3], buf[4]);
hailfinger9c5add72009-11-24 00:20:03 +0000223 return 1;
224 }
snelson865ae752010-01-09 23:56:41 +0000225 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
hailfinger9c5add72009-11-24 00:20:03 +0000226 if (buf[4] != '1') {
snelson865ae752010-01-09 23:56:41 +0000227 msg_perr("Can't handle raw bitbang mode version %c!\n",
hailfinger9c5add72009-11-24 00:20:03 +0000228 buf[4]);
229 return 1;
230 }
231 /* Enter raw SPI mode */
232 buf[0] = 0x01;
233 ret = buspirate_sendrecv(buf, 1, 4);
hailfingerfa76f932010-09-16 22:34:25 +0000234 if (ret)
235 return ret;
hailfinger9c5add72009-11-24 00:20:03 +0000236 if (memcmp(buf, "SPI", 3)) {
snelson865ae752010-01-09 23:56:41 +0000237 msg_perr("Entering raw SPI mode failed!\n");
hailfingerfa76f932010-09-16 22:34:25 +0000238 msg_pdbg("Got %02x%02x%02x%02x\n",
239 buf[0], buf[1], buf[2], buf[3]);
hailfinger9c5add72009-11-24 00:20:03 +0000240 return 1;
241 }
snelson865ae752010-01-09 23:56:41 +0000242 msg_pdbg("Raw SPI mode version %c\n", buf[3]);
hailfinger9c5add72009-11-24 00:20:03 +0000243 if (buf[3] != '1') {
snelson865ae752010-01-09 23:56:41 +0000244 msg_perr("Can't handle raw SPI mode version %c!\n",
hailfinger9c5add72009-11-24 00:20:03 +0000245 buf[3]);
246 return 1;
247 }
248
249 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
250 buf[0] = 0x40 | 0xb;
251 ret = buspirate_sendrecv(buf, 1, 1);
252 if (ret)
253 return 1;
254 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000255 msg_perr("Protocol error while setting power/CS/AUX!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000256 return 1;
257 }
258
hailfinger6e5a52a2009-11-24 18:27:10 +0000259 /* Set SPI speed */
260 buf[0] = 0x60 | spispeed;
hailfinger9c5add72009-11-24 00:20:03 +0000261 ret = buspirate_sendrecv(buf, 1, 1);
262 if (ret)
263 return 1;
264 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000265 msg_perr("Protocol error while setting SPI speed!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000266 return 1;
267 }
268
269 /* Set SPI config: output type, idle, clock edge, sample */
270 buf[0] = 0x80 | 0xa;
271 ret = buspirate_sendrecv(buf, 1, 1);
272 if (ret)
273 return 1;
274 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000275 msg_perr("Protocol error while setting SPI config!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000276 return 1;
277 }
278
279 /* De-assert CS# */
280 buf[0] = 0x03;
281 ret = buspirate_sendrecv(buf, 1, 1);
282 if (ret)
283 return 1;
284 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000285 msg_perr("Protocol error while raising CS#!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000286 return 1;
287 }
288
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100289 register_spi_master(&spi_master_buspirate);
hailfinger9c5add72009-11-24 00:20:03 +0000290
291 return 0;
292}
293
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700294static int buspirate_spi_send_command(const struct flashctx *flash,
295 unsigned int writecnt,
296 unsigned int readcnt,
297 const unsigned char *writearr,
298 unsigned char *readarr)
hailfinger9c5add72009-11-24 00:20:03 +0000299{
300 static unsigned char *buf = NULL;
stefanctc5eb8a92011-11-23 09:13:48 +0000301 unsigned int i = 0;
302 int ret = 0;
hailfinger9c5add72009-11-24 00:20:03 +0000303
304 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
305 return SPI_INVALID_LENGTH;
306
hailfinger17d3e912010-07-29 16:24:09 +0000307 /* 3 bytes extra for CS#, len, CS#. */
308 buf = realloc(buf, writecnt + readcnt + 3);
hailfinger9c5add72009-11-24 00:20:03 +0000309 if (!buf) {
snelson865ae752010-01-09 23:56:41 +0000310 msg_perr("Out of memory!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000311 exit(1); // -1
312 }
313
314 /* Assert CS# */
315 buf[i++] = 0x02;
hailfinger17d3e912010-07-29 16:24:09 +0000316
317 buf[i++] = 0x10 | (writecnt + readcnt - 1);
318 memcpy(buf + i, writearr, writecnt);
319 i += writecnt;
320 memset(buf + i, 0, readcnt);
321
322 i += readcnt;
323 /* De-assert CS# */
324 buf[i++] = 0x03;
325
326 ret = buspirate_sendrecv(buf, i, i);
327
328 if (ret) {
329 msg_perr("Bus Pirate communication error!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000330 return SPI_GENERIC_ERROR;
hailfinger17d3e912010-07-29 16:24:09 +0000331 }
332
hailfinger9c5add72009-11-24 00:20:03 +0000333 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000334 msg_perr("Protocol error while lowering CS#!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000335 return SPI_GENERIC_ERROR;
336 }
337
hailfinger17d3e912010-07-29 16:24:09 +0000338 if (buf[1] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000339 msg_perr("Protocol error while reading/writing SPI!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000340 return SPI_GENERIC_ERROR;
341 }
hailfinger9c5add72009-11-24 00:20:03 +0000342
hailfinger17d3e912010-07-29 16:24:09 +0000343 if (buf[i - 1] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000344 msg_perr("Protocol error while raising CS#!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000345 return SPI_GENERIC_ERROR;
346 }
347
hailfinger17d3e912010-07-29 16:24:09 +0000348 /* Skip CS#, length, writearr. */
349 memcpy(readarr, buf + 2 + writecnt, readcnt);
350
hailfinger9c5add72009-11-24 00:20:03 +0000351 return ret;
352}