blob: ff1aac486af6adbdf00890852840ca03eddb0244 [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"
Sean Nelson14ba6682010-02-26 05:48:29 +000026#include "chipdrivers.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);
43 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
Uwe Hermann91f4afa2011-07-28 08:13:25 +000053static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt,
54 unsigned int readcnt)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000055{
56 int i, ret = 0;
57
Sean Nelson84f7bce2010-01-09 23:56:41 +000058 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000059 if (!writecnt && !readcnt) {
Sean Nelson84f7bce2010-01-09 23:56:41 +000060 msg_perr("Zero length command!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000061 return 1;
62 }
Sean Nelson84f7bce2010-01-09 23:56:41 +000063 msg_pspew("Sending");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000064 for (i = 0; i < writecnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000065 msg_pspew(" 0x%02x", buf[i]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000066#ifdef FAKE_COMMUNICATION
67 /* Placate the caller for now. */
68 if (readcnt) {
69 buf[0] = 0x01;
70 memset(buf + 1, 0xff, readcnt - 1);
71 }
72 ret = 0;
73#else
74 if (writecnt)
75 ret = serialport_write(buf, writecnt);
76 if (ret)
77 return ret;
78 if (readcnt)
79 ret = serialport_read(buf, readcnt);
80 if (ret)
81 return ret;
82#endif
Sean Nelson84f7bce2010-01-09 23:56:41 +000083 msg_pspew(", receiving");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000084 for (i = 0; i < readcnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000085 msg_pspew(" 0x%02x", buf[i]);
86 msg_pspew("\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000087 return 0;
88}
89
Michael Karcherb9dbe482011-05-11 17:07:07 +000090static int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt,
91 const unsigned char *writearr, unsigned char *readarr);
92
93static const struct spi_programmer spi_programmer_buspirate = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +000094 .type = SPI_CONTROLLER_BUSPIRATE,
95 .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,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000101};
102
Carl-Daniel Hailfingerd5b28fa2009-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},
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000112 {NULL, 0x0},
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000113};
114
David Hendricks8bb20212011-06-14 01:35:36 +0000115static int buspirate_spi_shutdown(void *data)
116{
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
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000150int buspirate_spi_init(void)
151{
152 unsigned char buf[512];
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000153 char *dev = NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000154 char *speed = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000155 int spispeed = 0x7;
156 int ret = 0;
157 int i;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000158
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000159 dev = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000160 if (!dev || !strlen(dev)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000161 msg_perr("No serial device given. Use flashrom -p "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000162 "buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000163 return 1;
164 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000165
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000166 speed = extract_programmer_param("spispeed");
Carl-Daniel Hailfingerd5b28fa2009-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)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000175 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000176 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000177 free(speed);
178
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000179 /* This works because speeds numbering starts at 0 and is contiguous. */
Sean Nelson84f7bce2010-01-09 23:56:41 +0000180 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000181
182 ret = buspirate_serialport_setup(dev);
183 if (ret)
184 return ret;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000185 free(dev);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000186
David Hendricks8bb20212011-06-14 01:35:36 +0000187 if (register_shutdown(buspirate_spi_shutdown, NULL))
188 return 1;
189
Carl-Daniel Hailfinger5cca01f2009-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. */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000199 sp_flush_incoming();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000200 }
Carl-Daniel Hailfingerd2f007f2010-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
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000207 * occasional failure.
Carl-Daniel Hailfingerd2f007f2010-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 }
Carl-Daniel Hailfinger5cca01f2009-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)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000221 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-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]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000224 return 1;
225 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000226 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000227 if (buf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000228 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-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);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000235 if (ret)
236 return ret;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000237 if (memcmp(buf, "SPI", 3)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000238 msg_perr("Entering raw SPI mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000239 msg_pdbg("Got %02x%02x%02x%02x\n",
240 buf[0], buf[1], buf[2], buf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000241 return 1;
242 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000243 msg_pdbg("Raw SPI mode version %c\n", buf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000244 if (buf[3] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000245 msg_perr("Can't handle raw SPI mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-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) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000256 msg_perr("Protocol error while setting power/CS/AUX!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000257 return 1;
258 }
259
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000260 /* Set SPI speed */
261 buf[0] = 0x60 | spispeed;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000262 ret = buspirate_sendrecv(buf, 1, 1);
263 if (ret)
264 return 1;
265 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000266 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-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) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000276 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-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) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000286 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000287 return 1;
288 }
289
Michael Karcherb9dbe482011-05-11 17:07:07 +0000290 register_spi_programmer(&spi_programmer_buspirate);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000291
292 return 0;
293}
294
Michael Karcherb9dbe482011-05-11 17:07:07 +0000295static int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000296 const unsigned char *writearr, unsigned char *readarr)
297{
298 static unsigned char *buf = NULL;
299 int i = 0, ret = 0;
300
301 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
302 return SPI_INVALID_LENGTH;
303
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000304 /* 3 bytes extra for CS#, len, CS#. */
305 buf = realloc(buf, writecnt + readcnt + 3);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000306 if (!buf) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000307 msg_perr("Out of memory!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000308 exit(1); // -1
309 }
310
311 /* Assert CS# */
312 buf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000313
314 buf[i++] = 0x10 | (writecnt + readcnt - 1);
315 memcpy(buf + i, writearr, writecnt);
316 i += writecnt;
317 memset(buf + i, 0, readcnt);
318
319 i += readcnt;
320 /* De-assert CS# */
321 buf[i++] = 0x03;
322
323 ret = buspirate_sendrecv(buf, i, i);
324
325 if (ret) {
326 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000327 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000328 }
329
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000330 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000331 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000332 return SPI_GENERIC_ERROR;
333 }
334
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000335 if (buf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000336 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000337 return SPI_GENERIC_ERROR;
338 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000339
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000340 if (buf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000341 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000342 return SPI_GENERIC_ERROR;
343 }
344
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000345 /* Skip CS#, length, writearr. */
346 memcpy(readarr, buf + 2 + writecnt, readcnt);
347
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000348 return ret;
349}