blob: f872309c8742e7f1ed6e6a5e83fb6da31e35a7e8 [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
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000053static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, unsigned int readcnt)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000054{
55 int i, ret = 0;
56
Sean Nelson84f7bce2010-01-09 23:56:41 +000057 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000058 if (!writecnt && !readcnt) {
Sean Nelson84f7bce2010-01-09 23:56:41 +000059 msg_perr("Zero length command!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000060 return 1;
61 }
Sean Nelson84f7bce2010-01-09 23:56:41 +000062 msg_pspew("Sending");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000063 for (i = 0; i < writecnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000064 msg_pspew(" 0x%02x", buf[i]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000065#ifdef FAKE_COMMUNICATION
66 /* Placate the caller for now. */
67 if (readcnt) {
68 buf[0] = 0x01;
69 memset(buf + 1, 0xff, readcnt - 1);
70 }
71 ret = 0;
72#else
73 if (writecnt)
74 ret = serialport_write(buf, writecnt);
75 if (ret)
76 return ret;
77 if (readcnt)
78 ret = serialport_read(buf, readcnt);
79 if (ret)
80 return ret;
81#endif
Sean Nelson84f7bce2010-01-09 23:56:41 +000082 msg_pspew(", receiving");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000083 for (i = 0; i < readcnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000084 msg_pspew(" 0x%02x", buf[i]);
85 msg_pspew("\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000086 return 0;
87}
88
Michael Karcherb9dbe482011-05-11 17:07:07 +000089static int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt,
90 const unsigned char *writearr, unsigned char *readarr);
91
92static const struct spi_programmer spi_programmer_buspirate = {
93 .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,
100};
101
Carl-Daniel Hailfingerd5b28fa2009-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},
111 {NULL, 0x0}
112};
113
David Hendricks8bb20212011-06-14 01:35:36 +0000114static int buspirate_spi_shutdown(void *data)
115{
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
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000149int buspirate_spi_init(void)
150{
151 unsigned char buf[512];
152 int ret = 0;
153 int i;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000154 char *dev = NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000155 char *speed = NULL;
156 int spispeed = 0x7;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000157
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000158 dev = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000159 if (!dev || !strlen(dev)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000160 msg_perr("No serial device given. Use flashrom -p "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000161 "buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000162 return 1;
163 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000164
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000165 speed = extract_programmer_param("spispeed");
Carl-Daniel Hailfingerd5b28fa2009-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)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000174 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000175 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000176 free(speed);
177
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000178 /* This works because speeds numbering starts at 0 and is contiguous. */
Sean Nelson84f7bce2010-01-09 23:56:41 +0000179 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000180
181 ret = buspirate_serialport_setup(dev);
182 if (ret)
183 return ret;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000184 free(dev);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000185
David Hendricks8bb20212011-06-14 01:35:36 +0000186 if (register_shutdown(buspirate_spi_shutdown, NULL))
187 return 1;
188
Carl-Daniel Hailfinger5cca01f2009-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. */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000198 sp_flush_incoming();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000199 }
Carl-Daniel Hailfingerd2f007f2010-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
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000206 * occasional failure.
Carl-Daniel Hailfingerd2f007f2010-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 }
Carl-Daniel Hailfinger5cca01f2009-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)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000220 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-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]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000223 return 1;
224 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000225 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000226 if (buf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000227 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-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);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000234 if (ret)
235 return ret;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000236 if (memcmp(buf, "SPI", 3)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000237 msg_perr("Entering raw SPI mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000238 msg_pdbg("Got %02x%02x%02x%02x\n",
239 buf[0], buf[1], buf[2], buf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000240 return 1;
241 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000242 msg_pdbg("Raw SPI mode version %c\n", buf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000243 if (buf[3] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000244 msg_perr("Can't handle raw SPI mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-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) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000255 msg_perr("Protocol error while setting power/CS/AUX!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000256 return 1;
257 }
258
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000259 /* Set SPI speed */
260 buf[0] = 0x60 | spispeed;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000261 ret = buspirate_sendrecv(buf, 1, 1);
262 if (ret)
263 return 1;
264 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000265 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-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) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000275 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-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) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000285 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000286 return 1;
287 }
288
Michael Karcherb9dbe482011-05-11 17:07:07 +0000289 register_spi_programmer(&spi_programmer_buspirate);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000290
291 return 0;
292}
293
Michael Karcherb9dbe482011-05-11 17:07:07 +0000294static int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000295 const unsigned char *writearr, unsigned char *readarr)
296{
297 static unsigned char *buf = NULL;
298 int i = 0, ret = 0;
299
300 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
301 return SPI_INVALID_LENGTH;
302
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000303 /* 3 bytes extra for CS#, len, CS#. */
304 buf = realloc(buf, writecnt + readcnt + 3);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000305 if (!buf) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000306 msg_perr("Out of memory!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000307 exit(1); // -1
308 }
309
310 /* Assert CS# */
311 buf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000312
313 buf[i++] = 0x10 | (writecnt + readcnt - 1);
314 memcpy(buf + i, writearr, writecnt);
315 i += writecnt;
316 memset(buf + i, 0, readcnt);
317
318 i += readcnt;
319 /* De-assert CS# */
320 buf[i++] = 0x03;
321
322 ret = buspirate_sendrecv(buf, i, i);
323
324 if (ret) {
325 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000326 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000327 }
328
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000329 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000330 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000331 return SPI_GENERIC_ERROR;
332 }
333
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000334 if (buf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000335 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000336 return SPI_GENERIC_ERROR;
337 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000338
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000339 if (buf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000340 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000341 return SPI_GENERIC_ERROR;
342 }
343
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000344 /* Skip CS#, length, writearr. */
345 memcpy(readarr, buf + 2 + writecnt, readcnt);
346
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000347 return ret;
348}