blob: 9890d48297a4c93f41edb4e328c1afb2d3996ad9 [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>
21#include <stdint.h>
22#include <string.h>
23#include <stdlib.h>
24#include <ctype.h>
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +000025#include <unistd.h>
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000026#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000027#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000028#include "programmer.h"
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000029#include "spi.h"
30
31/* Change this to #define if you want to test without a serial implementation */
32#undef FAKE_COMMUNICATION
33
34#ifndef FAKE_COMMUNICATION
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000035static int buspirate_serialport_setup(char *dev)
Carl-Daniel Hailfinger5cca01f2009-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}
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000041#else
42#define buspirate_serialport_setup(...) 0
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +000043#define serialport_shutdown(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000044#define serialport_write(...) 0
45#define serialport_read(...) 0
Patrick Georgi3b6237d2010-01-06 19:09:40 +000046#define sp_flush_incoming(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000047#endif
48
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000049static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, unsigned int readcnt)
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000050{
51 int i, ret = 0;
52
Sean Nelson84f7bce2010-01-09 23:56:41 +000053 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000054 if (!writecnt && !readcnt) {
Sean Nelson84f7bce2010-01-09 23:56:41 +000055 msg_perr("Zero length command!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000056 return 1;
57 }
Sean Nelson84f7bce2010-01-09 23:56:41 +000058 msg_pspew("Sending");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000059 for (i = 0; i < writecnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000060 msg_pspew(" 0x%02x", buf[i]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000061#ifdef FAKE_COMMUNICATION
62 /* Placate the caller for now. */
63 if (readcnt) {
64 buf[0] = 0x01;
65 memset(buf + 1, 0xff, readcnt - 1);
66 }
67 ret = 0;
68#else
69 if (writecnt)
70 ret = serialport_write(buf, writecnt);
71 if (ret)
72 return ret;
73 if (readcnt)
74 ret = serialport_read(buf, readcnt);
75 if (ret)
76 return ret;
77#endif
Sean Nelson84f7bce2010-01-09 23:56:41 +000078 msg_pspew(", receiving");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000079 for (i = 0; i < readcnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000080 msg_pspew(" 0x%02x", buf[i]);
81 msg_pspew("\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000082 return 0;
83}
84
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +000085static const struct buspirate_spispeeds spispeeds[] = {
86 {"30k", 0x0},
87 {"125k", 0x1},
88 {"250k", 0x2},
89 {"1M", 0x3},
90 {"2M", 0x4},
91 {"2.6M", 0x5},
92 {"4M", 0x6},
93 {"8M", 0x7},
94 {NULL, 0x0}
95};
96
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000097int buspirate_spi_init(void)
98{
99 unsigned char buf[512];
100 int ret = 0;
101 int i;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000102 char *dev = NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000103 char *speed = NULL;
104 int spispeed = 0x7;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000105
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000106 dev = extract_programmer_param("dev");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000107 if (!dev || !strlen(dev)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000108 msg_perr("No serial device given. Use flashrom -p "
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000109 "buspirate_spi:dev=/dev/ttyUSB0\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000110 return 1;
111 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000112
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000113 speed = extract_programmer_param("spispeed");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000114 if (speed) {
115 for (i = 0; spispeeds[i].name; i++)
116 if (!strncasecmp(spispeeds[i].name, speed,
117 strlen(spispeeds[i].name))) {
118 spispeed = spispeeds[i].speed;
119 break;
120 }
121 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000122 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000123 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000124 free(speed);
125
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000126 /* This works because speeds numbering starts at 0 and is contiguous. */
Sean Nelson84f7bce2010-01-09 23:56:41 +0000127 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000128
129 ret = buspirate_serialport_setup(dev);
130 if (ret)
131 return ret;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000132 free(dev);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000133
134 /* This is the brute force version, but it should work. */
135 for (i = 0; i < 19; i++) {
136 /* Enter raw bitbang mode */
137 buf[0] = 0x00;
138 /* Send the command, don't read the response. */
139 ret = buspirate_sendrecv(buf, 1, 0);
140 if (ret)
141 return ret;
142 /* Read any response and discard it. */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000143 sp_flush_incoming();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000144 }
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000145 /* USB is slow. The Bus Pirate is even slower. Apparently the flush
146 * action above is too fast or too early. Some stuff still remains in
147 * the pipe after the flush above, and one additional flush is not
148 * sufficient either. Use a 1.5 ms delay inside the loop to make
149 * mostly sure that at least one USB frame had time to arrive.
150 * Looping only 5 times is not sufficient and causes the
151 * ocassional failure.
152 * Folding the delay into the loop above is not reliable either.
153 */
154 for (i = 0; i < 10; i++) {
155 usleep(1500);
156 /* Read any response and discard it. */
157 sp_flush_incoming();
158 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000159 /* Enter raw bitbang mode */
160 buf[0] = 0x00;
161 ret = buspirate_sendrecv(buf, 1, 5);
162 if (ret)
163 return ret;
164 if (memcmp(buf, "BBIO", 4)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000165 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000166 msg_pdbg("Got %02x%02x%02x%02x%02x\n",
167 buf[0], buf[1], buf[2], buf[3], buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000168 return 1;
169 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000170 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000171 if (buf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000172 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000173 buf[4]);
174 return 1;
175 }
176 /* Enter raw SPI mode */
177 buf[0] = 0x01;
178 ret = buspirate_sendrecv(buf, 1, 4);
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000179 if (ret)
180 return ret;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000181 if (memcmp(buf, "SPI", 3)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000182 msg_perr("Entering raw SPI mode failed!\n");
Carl-Daniel Hailfingerd2f007f2010-09-16 22:34:25 +0000183 msg_pdbg("Got %02x%02x%02x%02x\n",
184 buf[0], buf[1], buf[2], buf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000185 return 1;
186 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000187 msg_pdbg("Raw SPI mode version %c\n", buf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000188 if (buf[3] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000189 msg_perr("Can't handle raw SPI mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000190 buf[3]);
191 return 1;
192 }
193
194 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
195 buf[0] = 0x40 | 0xb;
196 ret = buspirate_sendrecv(buf, 1, 1);
197 if (ret)
198 return 1;
199 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000200 msg_perr("Protocol error while setting power/CS/AUX!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000201 return 1;
202 }
203
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000204 /* Set SPI speed */
205 buf[0] = 0x60 | spispeed;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000206 ret = buspirate_sendrecv(buf, 1, 1);
207 if (ret)
208 return 1;
209 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000210 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000211 return 1;
212 }
213
214 /* Set SPI config: output type, idle, clock edge, sample */
215 buf[0] = 0x80 | 0xa;
216 ret = buspirate_sendrecv(buf, 1, 1);
217 if (ret)
218 return 1;
219 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000220 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000221 return 1;
222 }
223
224 /* De-assert CS# */
225 buf[0] = 0x03;
226 ret = buspirate_sendrecv(buf, 1, 1);
227 if (ret)
228 return 1;
229 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000230 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000231 return 1;
232 }
233
234 buses_supported = CHIP_BUSTYPE_SPI;
235 spi_controller = SPI_CONTROLLER_BUSPIRATE;
236
237 return 0;
238}
239
240int buspirate_spi_shutdown(void)
241{
242 unsigned char buf[5];
243 int ret = 0;
244
245 /* Exit raw SPI mode (enter raw bitbang mode) */
246 buf[0] = 0x00;
247 ret = buspirate_sendrecv(buf, 1, 5);
248 if (ret)
249 return ret;
250 if (memcmp(buf, "BBIO", 4)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000251 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000252 return 1;
253 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000254 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000255 if (buf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000256 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000257 buf[4]);
258 return 1;
259 }
260 /* Reset Bus Pirate (return to user terminal) */
261 buf[0] = 0x0f;
262 ret = buspirate_sendrecv(buf, 1, 0);
263 if (ret)
264 return ret;
265
266 /* Shut down serial port communication */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000267 ret = serialport_shutdown();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000268 if (ret)
269 return ret;
Sean Nelson84f7bce2010-01-09 23:56:41 +0000270 msg_pdbg("Bus Pirate shutdown completed.\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000271
272 return 0;
273}
274
275int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt,
276 const unsigned char *writearr, unsigned char *readarr)
277{
278 static unsigned char *buf = NULL;
279 int i = 0, ret = 0;
280
281 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
282 return SPI_INVALID_LENGTH;
283
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000284 /* 3 bytes extra for CS#, len, CS#. */
285 buf = realloc(buf, writecnt + readcnt + 3);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000286 if (!buf) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000287 msg_perr("Out of memory!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000288 exit(1); // -1
289 }
290
291 /* Assert CS# */
292 buf[i++] = 0x02;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000293
294 buf[i++] = 0x10 | (writecnt + readcnt - 1);
295 memcpy(buf + i, writearr, writecnt);
296 i += writecnt;
297 memset(buf + i, 0, readcnt);
298
299 i += readcnt;
300 /* De-assert CS# */
301 buf[i++] = 0x03;
302
303 ret = buspirate_sendrecv(buf, i, i);
304
305 if (ret) {
306 msg_perr("Bus Pirate communication error!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000307 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000308 }
309
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000310 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000311 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000312 return SPI_GENERIC_ERROR;
313 }
314
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000315 if (buf[1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000316 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000317 return SPI_GENERIC_ERROR;
318 }
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000319
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000320 if (buf[i - 1] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000321 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000322 return SPI_GENERIC_ERROR;
323 }
324
Carl-Daniel Hailfinger04e18be2010-07-29 16:24:09 +0000325 /* Skip CS#, length, writearr. */
326 memcpy(readarr, buf + 2 + writecnt, readcnt);
327
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000328 return ret;
329}
330
331int buspirate_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
332{
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000333 return spi_read_chunked(flash, buf, start, len, 12);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000334}
335
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000336int buspirate_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger408e47a2010-03-22 03:30:58 +0000337{
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000338 return spi_write_chunked(flash, buf, start, len, 12);
Carl-Daniel Hailfinger408e47a2010-03-22 03:30:58 +0000339}