blob: 3ac0929b61e473ddd8cec9e90f4ba7acde079c84 [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
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000114int buspirate_spi_init(void)
115{
116 unsigned char buf[512];
117 int ret = 0;
118 int i;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000119 char *dev = NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000120 char *speed = NULL;
121 int spispeed = 0x7;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000122
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000123 dev = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000124 if (!dev || !strlen(dev)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000125 msg_perr("No serial device given. Use flashrom -p "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000126 "buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000127 return 1;
128 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000129
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000130 speed = extract_programmer_param("spispeed");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000131 if (speed) {
132 for (i = 0; spispeeds[i].name; i++)
133 if (!strncasecmp(spispeeds[i].name, speed,
134 strlen(spispeeds[i].name))) {
135 spispeed = spispeeds[i].speed;
136 break;
137 }
138 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000139 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000140 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000141 free(speed);
142
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000143 /* This works because speeds numbering starts at 0 and is contiguous. */
Sean Nelson84f7bce2010-01-09 23:56:41 +0000144 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000145
146 ret = buspirate_serialport_setup(dev);
147 if (ret)
148 return ret;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000149 free(dev);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000150
151 /* This is the brute force version, but it should work. */
152 for (i = 0; i < 19; i++) {
153 /* Enter raw bitbang mode */
154 buf[0] = 0x00;
155 /* Send the command, don't read the response. */
156 ret = buspirate_sendrecv(buf, 1, 0);
157 if (ret)
158 return ret;
159 /* Read any response and discard it. */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000160 sp_flush_incoming();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000161 }
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000162 /* USB is slow. The Bus Pirate is even slower. Apparently the flush
163 * action above is too fast or too early. Some stuff still remains in
164 * the pipe after the flush above, and one additional flush is not
165 * sufficient either. Use a 1.5 ms delay inside the loop to make
166 * mostly sure that at least one USB frame had time to arrive.
167 * Looping only 5 times is not sufficient and causes the
168 * ocassional failure.
169 * Folding the delay into the loop above is not reliable either.
170 */
171 for (i = 0; i < 10; i++) {
172 usleep(1500);
173 /* Read any response and discard it. */
174 sp_flush_incoming();
175 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000176 /* Enter raw bitbang mode */
177 buf[0] = 0x00;
178 ret = buspirate_sendrecv(buf, 1, 5);
179 if (ret)
180 return ret;
181 if (memcmp(buf, "BBIO", 4)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000182 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000183 msg_pdbg("Got %02x%02x%02x%02x%02x\n",
184 buf[0], buf[1], buf[2], buf[3], buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000185 return 1;
186 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000187 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000188 if (buf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000189 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000190 buf[4]);
191 return 1;
192 }
193 /* Enter raw SPI mode */
194 buf[0] = 0x01;
195 ret = buspirate_sendrecv(buf, 1, 4);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000196 if (ret)
197 return ret;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000198 if (memcmp(buf, "SPI", 3)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000199 msg_perr("Entering raw SPI mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000200 msg_pdbg("Got %02x%02x%02x%02x\n",
201 buf[0], buf[1], buf[2], buf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000202 return 1;
203 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000204 msg_pdbg("Raw SPI mode version %c\n", buf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000205 if (buf[3] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000206 msg_perr("Can't handle raw SPI mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000207 buf[3]);
208 return 1;
209 }
210
211 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
212 buf[0] = 0x40 | 0xb;
213 ret = buspirate_sendrecv(buf, 1, 1);
214 if (ret)
215 return 1;
216 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000217 msg_perr("Protocol error while setting power/CS/AUX!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000218 return 1;
219 }
220
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000221 /* Set SPI speed */
222 buf[0] = 0x60 | spispeed;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000223 ret = buspirate_sendrecv(buf, 1, 1);
224 if (ret)
225 return 1;
226 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000227 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000228 return 1;
229 }
230
231 /* Set SPI config: output type, idle, clock edge, sample */
232 buf[0] = 0x80 | 0xa;
233 ret = buspirate_sendrecv(buf, 1, 1);
234 if (ret)
235 return 1;
236 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000237 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000238 return 1;
239 }
240
241 /* De-assert CS# */
242 buf[0] = 0x03;
243 ret = buspirate_sendrecv(buf, 1, 1);
244 if (ret)
245 return 1;
246 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000247 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000248 return 1;
249 }
250
Michael Karcherb9dbe482011-05-11 17:07:07 +0000251 register_spi_programmer(&spi_programmer_buspirate);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000252
253 return 0;
254}
255
256int buspirate_spi_shutdown(void)
257{
258 unsigned char buf[5];
259 int ret = 0;
260
261 /* Exit raw SPI mode (enter raw bitbang mode) */
262 buf[0] = 0x00;
263 ret = buspirate_sendrecv(buf, 1, 5);
264 if (ret)
265 return ret;
266 if (memcmp(buf, "BBIO", 4)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000267 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000268 return 1;
269 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000270 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000271 if (buf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000272 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000273 buf[4]);
274 return 1;
275 }
276 /* Reset Bus Pirate (return to user terminal) */
277 buf[0] = 0x0f;
278 ret = buspirate_sendrecv(buf, 1, 0);
279 if (ret)
280 return ret;
281
282 /* Shut down serial port communication */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000283 ret = serialport_shutdown();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000284 if (ret)
285 return ret;
Sean Nelson84f7bce2010-01-09 23:56:41 +0000286 msg_pdbg("Bus Pirate shutdown completed.\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000287
288 return 0;
289}
290
Michael Karcherb9dbe482011-05-11 17:07:07 +0000291static int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000292 const unsigned char *writearr, unsigned char *readarr)
293{
294 static unsigned char *buf = NULL;
295 int i = 0, ret = 0;
296
297 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
298 return SPI_INVALID_LENGTH;
299
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000300 /* 3 bytes extra for CS#, len, CS#. */
301 buf = realloc(buf, writecnt + readcnt + 3);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000302 if (!buf) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000303 msg_perr("Out of memory!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000304 exit(1); // -1
305 }
306
307 /* Assert CS# */
308 buf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000309
310 buf[i++] = 0x10 | (writecnt + readcnt - 1);
311 memcpy(buf + i, writearr, writecnt);
312 i += writecnt;
313 memset(buf + i, 0, readcnt);
314
315 i += readcnt;
316 /* De-assert CS# */
317 buf[i++] = 0x03;
318
319 ret = buspirate_sendrecv(buf, i, i);
320
321 if (ret) {
322 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000323 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000324 }
325
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000326 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000327 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000328 return SPI_GENERIC_ERROR;
329 }
330
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000331 if (buf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000332 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000333 return SPI_GENERIC_ERROR;
334 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000335
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000336 if (buf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000337 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000338 return SPI_GENERIC_ERROR;
339 }
340
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000341 /* Skip CS#, length, writearr. */
342 memcpy(readarr, buf + 2 + writecnt, readcnt);
343
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000344 return ret;
345}