blob: d24f890b7a0bec9bab7f90c01dc79545be39c350 [file] [log] [blame]
hailfinger9c5add72009-11-24 00:20:03 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfinger39d159a2010-05-21 23:09:42 +00004 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
hailfinger9c5add72009-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>
hailfinger9c5add72009-11-24 00:20:03 +000021#include <string.h>
22#include <stdlib.h>
23#include <ctype.h>
David Hendricksc801adb2010-12-09 16:58:56 -080024#include <unistd.h>
hailfinger9c5add72009-11-24 00:20:03 +000025#include "flash.h"
snelson8913d082010-02-26 05:48:29 +000026#include "chipdrivers.h"
David Hendricks82fd8ae2010-08-04 14:34:54 -070027#include "programmer.h"
hailfinger9c5add72009-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
33#ifndef FAKE_COMMUNICATION
David Hendricks82fd8ae2010-08-04 14:34:54 -070034static int buspirate_serialport_setup(char *dev)
hailfinger9c5add72009-11-24 00:20:03 +000035{
36 /* 115200bps, 8 databits, no parity, 1 stopbit */
37 sp_fd = sp_openserport(dev, 115200);
38 return 0;
39}
hailfinger9c5add72009-11-24 00:20:03 +000040#else
41#define buspirate_serialport_setup(...) 0
hailfinger852163c2010-01-06 16:09:10 +000042#define serialport_shutdown(...) 0
hailfinger9c5add72009-11-24 00:20:03 +000043#define serialport_write(...) 0
44#define serialport_read(...) 0
oxygene8fede2d2010-01-06 19:09:40 +000045#define sp_flush_incoming(...) 0
hailfinger9c5add72009-11-24 00:20:03 +000046#endif
47
David Hendricks82fd8ae2010-08-04 14:34:54 -070048static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, unsigned int readcnt)
hailfinger9c5add72009-11-24 00:20:03 +000049{
50 int i, ret = 0;
51
snelson865ae752010-01-09 23:56:41 +000052 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
hailfinger9c5add72009-11-24 00:20:03 +000053 if (!writecnt && !readcnt) {
snelson865ae752010-01-09 23:56:41 +000054 msg_perr("Zero length command!\n");
hailfinger9c5add72009-11-24 00:20:03 +000055 return 1;
56 }
snelson865ae752010-01-09 23:56:41 +000057 msg_pspew("Sending");
hailfinger9c5add72009-11-24 00:20:03 +000058 for (i = 0; i < writecnt; i++)
snelson865ae752010-01-09 23:56:41 +000059 msg_pspew(" 0x%02x", buf[i]);
hailfinger9c5add72009-11-24 00:20:03 +000060#ifdef FAKE_COMMUNICATION
61 /* Placate the caller for now. */
62 if (readcnt) {
63 buf[0] = 0x01;
64 memset(buf + 1, 0xff, readcnt - 1);
65 }
66 ret = 0;
67#else
68 if (writecnt)
69 ret = serialport_write(buf, writecnt);
70 if (ret)
71 return ret;
72 if (readcnt)
73 ret = serialport_read(buf, readcnt);
74 if (ret)
75 return ret;
76#endif
snelson865ae752010-01-09 23:56:41 +000077 msg_pspew(", receiving");
hailfinger9c5add72009-11-24 00:20:03 +000078 for (i = 0; i < readcnt; i++)
snelson865ae752010-01-09 23:56:41 +000079 msg_pspew(" 0x%02x", buf[i]);
80 msg_pspew("\n");
hailfinger9c5add72009-11-24 00:20:03 +000081 return 0;
82}
83
hailfinger6e5a52a2009-11-24 18:27:10 +000084static const struct buspirate_spispeeds spispeeds[] = {
85 {"30k", 0x0},
86 {"125k", 0x1},
87 {"250k", 0x2},
88 {"1M", 0x3},
89 {"2M", 0x4},
90 {"2.6M", 0x5},
91 {"4M", 0x6},
92 {"8M", 0x7},
93 {NULL, 0x0}
94};
95
hailfinger9c5add72009-11-24 00:20:03 +000096int buspirate_spi_init(void)
97{
98 unsigned char buf[512];
99 int ret = 0;
100 int i;
hailfinger9c5add72009-11-24 00:20:03 +0000101 char *dev = NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000102 char *speed = NULL;
103 int spispeed = 0x7;
hailfinger9c5add72009-11-24 00:20:03 +0000104
David Hendricks82fd8ae2010-08-04 14:34:54 -0700105 dev = extract_programmer_param("dev");
106 if (!dev || !strlen(dev)) {
snelson865ae752010-01-09 23:56:41 +0000107 msg_perr("No serial device given. Use flashrom -p "
hailfinger90c7d542010-05-31 15:27:27 +0000108 "buspirate_spi:dev=/dev/ttyUSB0\n");
hailfinger9c5add72009-11-24 00:20:03 +0000109 return 1;
110 }
David Hendricks82fd8ae2010-08-04 14:34:54 -0700111
112 speed = extract_programmer_param("spispeed");
hailfinger6e5a52a2009-11-24 18:27:10 +0000113 if (speed) {
114 for (i = 0; spispeeds[i].name; i++)
115 if (!strncasecmp(spispeeds[i].name, speed,
116 strlen(spispeeds[i].name))) {
117 spispeed = spispeeds[i].speed;
118 break;
119 }
120 if (!spispeeds[i].name)
snelson865ae752010-01-09 23:56:41 +0000121 msg_perr("Invalid SPI speed, using default.\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000122 }
David Hendricks82fd8ae2010-08-04 14:34:54 -0700123 free(speed);
124
hailfinger6e5a52a2009-11-24 18:27:10 +0000125 /* This works because speeds numbering starts at 0 and is contiguous. */
snelson865ae752010-01-09 23:56:41 +0000126 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
hailfinger9c5add72009-11-24 00:20:03 +0000127
128 ret = buspirate_serialport_setup(dev);
129 if (ret)
130 return ret;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700131 free(dev);
hailfinger9c5add72009-11-24 00:20:03 +0000132
133 /* This is the brute force version, but it should work. */
134 for (i = 0; i < 19; i++) {
135 /* Enter raw bitbang mode */
136 buf[0] = 0x00;
137 /* Send the command, don't read the response. */
138 ret = buspirate_sendrecv(buf, 1, 0);
139 if (ret)
140 return ret;
141 /* Read any response and discard it. */
oxygene8fede2d2010-01-06 19:09:40 +0000142 sp_flush_incoming();
hailfinger9c5add72009-11-24 00:20:03 +0000143 }
David Hendricksc801adb2010-12-09 16:58:56 -0800144 /* USB is slow. The Bus Pirate is even slower. Apparently the flush
145 * action above is too fast or too early. Some stuff still remains in
146 * the pipe after the flush above, and one additional flush is not
147 * sufficient either. Use a 1.5 ms delay inside the loop to make
148 * mostly sure that at least one USB frame had time to arrive.
149 * Looping only 5 times is not sufficient and causes the
150 * ocassional failure.
151 * Folding the delay into the loop above is not reliable either.
152 */
153 for (i = 0; i < 10; i++) {
154 usleep(1500);
155 /* Read any response and discard it. */
156 sp_flush_incoming();
157 }
hailfinger9c5add72009-11-24 00:20:03 +0000158 /* Enter raw bitbang mode */
159 buf[0] = 0x00;
160 ret = buspirate_sendrecv(buf, 1, 5);
161 if (ret)
162 return ret;
163 if (memcmp(buf, "BBIO", 4)) {
snelson865ae752010-01-09 23:56:41 +0000164 msg_perr("Entering raw bitbang mode failed!\n");
David Hendricksc801adb2010-12-09 16:58:56 -0800165 msg_pdbg("Got %02x%02x%02x%02x%02x\n",
166 buf[0], buf[1], buf[2], buf[3], buf[4]);
hailfinger9c5add72009-11-24 00:20:03 +0000167 return 1;
168 }
snelson865ae752010-01-09 23:56:41 +0000169 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
hailfinger9c5add72009-11-24 00:20:03 +0000170 if (buf[4] != '1') {
snelson865ae752010-01-09 23:56:41 +0000171 msg_perr("Can't handle raw bitbang mode version %c!\n",
hailfinger9c5add72009-11-24 00:20:03 +0000172 buf[4]);
173 return 1;
174 }
175 /* Enter raw SPI mode */
176 buf[0] = 0x01;
177 ret = buspirate_sendrecv(buf, 1, 4);
David Hendricksc801adb2010-12-09 16:58:56 -0800178 if (ret)
179 return ret;
hailfinger9c5add72009-11-24 00:20:03 +0000180 if (memcmp(buf, "SPI", 3)) {
snelson865ae752010-01-09 23:56:41 +0000181 msg_perr("Entering raw SPI mode failed!\n");
David Hendricksc801adb2010-12-09 16:58:56 -0800182 msg_pdbg("Got %02x%02x%02x%02x\n",
183 buf[0], buf[1], buf[2], buf[3]);
hailfinger9c5add72009-11-24 00:20:03 +0000184 return 1;
185 }
snelson865ae752010-01-09 23:56:41 +0000186 msg_pdbg("Raw SPI mode version %c\n", buf[3]);
hailfinger9c5add72009-11-24 00:20:03 +0000187 if (buf[3] != '1') {
snelson865ae752010-01-09 23:56:41 +0000188 msg_perr("Can't handle raw SPI mode version %c!\n",
hailfinger9c5add72009-11-24 00:20:03 +0000189 buf[3]);
190 return 1;
191 }
192
193 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
194 buf[0] = 0x40 | 0xb;
195 ret = buspirate_sendrecv(buf, 1, 1);
196 if (ret)
197 return 1;
198 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000199 msg_perr("Protocol error while setting power/CS/AUX!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000200 return 1;
201 }
202
hailfinger6e5a52a2009-11-24 18:27:10 +0000203 /* Set SPI speed */
204 buf[0] = 0x60 | spispeed;
hailfinger9c5add72009-11-24 00:20:03 +0000205 ret = buspirate_sendrecv(buf, 1, 1);
206 if (ret)
207 return 1;
208 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000209 msg_perr("Protocol error while setting SPI speed!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000210 return 1;
211 }
212
213 /* Set SPI config: output type, idle, clock edge, sample */
214 buf[0] = 0x80 | 0xa;
215 ret = buspirate_sendrecv(buf, 1, 1);
216 if (ret)
217 return 1;
218 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000219 msg_perr("Protocol error while setting SPI config!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000220 return 1;
221 }
222
223 /* De-assert CS# */
224 buf[0] = 0x03;
225 ret = buspirate_sendrecv(buf, 1, 1);
226 if (ret)
227 return 1;
228 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000229 msg_perr("Protocol error while raising CS#!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000230 return 1;
231 }
232
233 buses_supported = CHIP_BUSTYPE_SPI;
234 spi_controller = SPI_CONTROLLER_BUSPIRATE;
235
236 return 0;
237}
238
239int buspirate_spi_shutdown(void)
240{
241 unsigned char buf[5];
242 int ret = 0;
243
244 /* Exit raw SPI mode (enter raw bitbang mode) */
245 buf[0] = 0x00;
246 ret = buspirate_sendrecv(buf, 1, 5);
247 if (ret)
248 return ret;
249 if (memcmp(buf, "BBIO", 4)) {
snelson865ae752010-01-09 23:56:41 +0000250 msg_perr("Entering raw bitbang mode failed!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000251 return 1;
252 }
snelson865ae752010-01-09 23:56:41 +0000253 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
hailfinger9c5add72009-11-24 00:20:03 +0000254 if (buf[4] != '1') {
snelson865ae752010-01-09 23:56:41 +0000255 msg_perr("Can't handle raw bitbang mode version %c!\n",
hailfinger9c5add72009-11-24 00:20:03 +0000256 buf[4]);
257 return 1;
258 }
259 /* Reset Bus Pirate (return to user terminal) */
260 buf[0] = 0x0f;
261 ret = buspirate_sendrecv(buf, 1, 0);
262 if (ret)
263 return ret;
264
265 /* Shut down serial port communication */
hailfinger852163c2010-01-06 16:09:10 +0000266 ret = serialport_shutdown();
hailfinger9c5add72009-11-24 00:20:03 +0000267 if (ret)
268 return ret;
snelson865ae752010-01-09 23:56:41 +0000269 msg_pdbg("Bus Pirate shutdown completed.\n");
hailfinger9c5add72009-11-24 00:20:03 +0000270
271 return 0;
272}
273
274int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt,
275 const unsigned char *writearr, unsigned char *readarr)
276{
277 static unsigned char *buf = NULL;
278 int i = 0, ret = 0;
279
280 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
281 return SPI_INVALID_LENGTH;
282
David Hendricks82fd8ae2010-08-04 14:34:54 -0700283 /* 3 bytes extra for CS#, len, CS#. */
284 buf = realloc(buf, writecnt + readcnt + 3);
hailfinger9c5add72009-11-24 00:20:03 +0000285 if (!buf) {
snelson865ae752010-01-09 23:56:41 +0000286 msg_perr("Out of memory!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000287 exit(1); // -1
288 }
289
290 /* Assert CS# */
291 buf[i++] = 0x02;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700292
293 buf[i++] = 0x10 | (writecnt + readcnt - 1);
294 memcpy(buf + i, writearr, writecnt);
295 i += writecnt;
296 memset(buf + i, 0, readcnt);
297
298 i += readcnt;
299 /* De-assert CS# */
300 buf[i++] = 0x03;
301
302 ret = buspirate_sendrecv(buf, i, i);
303
304 if (ret) {
305 msg_perr("Bus Pirate communication error!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000306 return SPI_GENERIC_ERROR;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700307 }
308
hailfinger9c5add72009-11-24 00:20:03 +0000309 if (buf[0] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000310 msg_perr("Protocol error while lowering CS#!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000311 return SPI_GENERIC_ERROR;
312 }
313
David Hendricks82fd8ae2010-08-04 14:34:54 -0700314 if (buf[1] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000315 msg_perr("Protocol error while reading/writing SPI!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000316 return SPI_GENERIC_ERROR;
317 }
hailfinger9c5add72009-11-24 00:20:03 +0000318
David Hendricks82fd8ae2010-08-04 14:34:54 -0700319 if (buf[i - 1] != 0x01) {
snelson865ae752010-01-09 23:56:41 +0000320 msg_perr("Protocol error while raising CS#!\n");
hailfinger9c5add72009-11-24 00:20:03 +0000321 return SPI_GENERIC_ERROR;
322 }
323
David Hendricks82fd8ae2010-08-04 14:34:54 -0700324 /* Skip CS#, length, writearr. */
325 memcpy(readarr, buf + 2 + writecnt, readcnt);
326
hailfinger9c5add72009-11-24 00:20:03 +0000327 return ret;
328}
329
330int buspirate_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
331{
hailfinger6e5a52a2009-11-24 18:27:10 +0000332 return spi_read_chunked(flash, buf, start, len, 12);
hailfinger9c5add72009-11-24 00:20:03 +0000333}
334
David Hendricks82fd8ae2010-08-04 14:34:54 -0700335int buspirate_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger8b82a422010-03-22 03:30:58 +0000336{
David Hendricks82fd8ae2010-08-04 14:34:54 -0700337 return spi_write_chunked(flash, buf, start, len, 12);
hailfinger8b82a422010-03-22 03:30:58 +0000338}