blob: 467d271d6f858f0329672612785dd9222d50ec2c [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];
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000153 int ret = 0, i, spispeed = 0x7;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000154 char *dev = NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000155 char *speed = NULL;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000156
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000157 dev = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000158 if (!dev || !strlen(dev)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000159 msg_perr("No serial device given. Use flashrom -p "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000160 "buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000161 return 1;
162 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000163
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000164 speed = extract_programmer_param("spispeed");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000165 if (speed) {
166 for (i = 0; spispeeds[i].name; i++)
167 if (!strncasecmp(spispeeds[i].name, speed,
168 strlen(spispeeds[i].name))) {
169 spispeed = spispeeds[i].speed;
170 break;
171 }
172 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000173 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000174 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000175 free(speed);
176
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000177 /* This works because speeds numbering starts at 0 and is contiguous. */
Sean Nelson84f7bce2010-01-09 23:56:41 +0000178 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000179
180 ret = buspirate_serialport_setup(dev);
181 if (ret)
182 return ret;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000183 free(dev);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000184
David Hendricks8bb20212011-06-14 01:35:36 +0000185 if (register_shutdown(buspirate_spi_shutdown, NULL))
186 return 1;
187
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000188 /* This is the brute force version, but it should work. */
189 for (i = 0; i < 19; i++) {
190 /* Enter raw bitbang mode */
191 buf[0] = 0x00;
192 /* Send the command, don't read the response. */
193 ret = buspirate_sendrecv(buf, 1, 0);
194 if (ret)
195 return ret;
196 /* Read any response and discard it. */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000197 sp_flush_incoming();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000198 }
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000199 /* USB is slow. The Bus Pirate is even slower. Apparently the flush
200 * action above is too fast or too early. Some stuff still remains in
201 * the pipe after the flush above, and one additional flush is not
202 * sufficient either. Use a 1.5 ms delay inside the loop to make
203 * mostly sure that at least one USB frame had time to arrive.
204 * Looping only 5 times is not sufficient and causes the
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +0000205 * occasional failure.
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000206 * Folding the delay into the loop above is not reliable either.
207 */
208 for (i = 0; i < 10; i++) {
209 usleep(1500);
210 /* Read any response and discard it. */
211 sp_flush_incoming();
212 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000213 /* Enter raw bitbang mode */
214 buf[0] = 0x00;
215 ret = buspirate_sendrecv(buf, 1, 5);
216 if (ret)
217 return ret;
218 if (memcmp(buf, "BBIO", 4)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000219 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000220 msg_pdbg("Got %02x%02x%02x%02x%02x\n",
221 buf[0], buf[1], buf[2], buf[3], buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000222 return 1;
223 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000224 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000225 if (buf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000226 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000227 buf[4]);
228 return 1;
229 }
230 /* Enter raw SPI mode */
231 buf[0] = 0x01;
232 ret = buspirate_sendrecv(buf, 1, 4);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000233 if (ret)
234 return ret;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000235 if (memcmp(buf, "SPI", 3)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000236 msg_perr("Entering raw SPI mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000237 msg_pdbg("Got %02x%02x%02x%02x\n",
238 buf[0], buf[1], buf[2], buf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000239 return 1;
240 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000241 msg_pdbg("Raw SPI mode version %c\n", buf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000242 if (buf[3] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000243 msg_perr("Can't handle raw SPI mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000244 buf[3]);
245 return 1;
246 }
247
248 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
249 buf[0] = 0x40 | 0xb;
250 ret = buspirate_sendrecv(buf, 1, 1);
251 if (ret)
252 return 1;
253 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000254 msg_perr("Protocol error while setting power/CS/AUX!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000255 return 1;
256 }
257
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000258 /* Set SPI speed */
259 buf[0] = 0x60 | spispeed;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000260 ret = buspirate_sendrecv(buf, 1, 1);
261 if (ret)
262 return 1;
263 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000264 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000265 return 1;
266 }
267
268 /* Set SPI config: output type, idle, clock edge, sample */
269 buf[0] = 0x80 | 0xa;
270 ret = buspirate_sendrecv(buf, 1, 1);
271 if (ret)
272 return 1;
273 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000274 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000275 return 1;
276 }
277
278 /* De-assert CS# */
279 buf[0] = 0x03;
280 ret = buspirate_sendrecv(buf, 1, 1);
281 if (ret)
282 return 1;
283 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000284 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000285 return 1;
286 }
287
Michael Karcherb9dbe482011-05-11 17:07:07 +0000288 register_spi_programmer(&spi_programmer_buspirate);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000289
290 return 0;
291}
292
Michael Karcherb9dbe482011-05-11 17:07:07 +0000293static int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000294 const unsigned char *writearr, unsigned char *readarr)
295{
296 static unsigned char *buf = NULL;
297 int i = 0, ret = 0;
298
299 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
300 return SPI_INVALID_LENGTH;
301
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000302 /* 3 bytes extra for CS#, len, CS#. */
303 buf = realloc(buf, writecnt + readcnt + 3);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000304 if (!buf) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000305 msg_perr("Out of memory!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000306 exit(1); // -1
307 }
308
309 /* Assert CS# */
310 buf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000311
312 buf[i++] = 0x10 | (writecnt + readcnt - 1);
313 memcpy(buf + i, writearr, writecnt);
314 i += writecnt;
315 memset(buf + i, 0, readcnt);
316
317 i += readcnt;
318 /* De-assert CS# */
319 buf[i++] = 0x03;
320
321 ret = buspirate_sendrecv(buf, i, i);
322
323 if (ret) {
324 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000325 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000326 }
327
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000328 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000329 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000330 return SPI_GENERIC_ERROR;
331 }
332
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000333 if (buf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000334 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000335 return SPI_GENERIC_ERROR;
336 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000337
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000338 if (buf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000339 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000340 return SPI_GENERIC_ERROR;
341 }
342
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000343 /* Skip CS#, length, writearr. */
344 memcpy(readarr, buf + 2 + writecnt, readcnt);
345
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000346 return ret;
347}