Flashrom

Flashrom Svn Source Tree

Root/trunk/buspirate_spi.c

1/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
5 *
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 <string.h>
22#include <stdlib.h>
23#include <ctype.h>
24#include <unistd.h>
25#include "flash.h"
26#include "programmer.h"
27#include "spi.h"
28
29/* Change this to #define if you want to test without a serial implementation */
30#undef FAKE_COMMUNICATION
31
32struct buspirate_spispeeds {
33const char *name;
34const int speed;
35};
36
37#ifndef FAKE_COMMUNICATION
38static int buspirate_serialport_setup(char *dev)
39{
40/* 115200bps, 8 databits, no parity, 1 stopbit */
41sp_fd = sp_openserport(dev, 115200);
42return 0;
43}
44#else
45#define buspirate_serialport_setup(...) 0
46#define serialport_shutdown(...) 0
47#define serialport_write(...) 0
48#define serialport_read(...) 0
49#define sp_flush_incoming(...) 0
50#endif
51
52static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt,
53 unsigned int readcnt)
54{
55int i, ret = 0;
56
57msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
58if (!writecnt && !readcnt) {
59msg_perr("Zero length command!\n");
60return 1;
61}
62msg_pspew("Sending");
63for (i = 0; i < writecnt; i++)
64msg_pspew(" 0x%02x", buf[i]);
65#ifdef FAKE_COMMUNICATION
66/* Placate the caller for now. */
67if (readcnt) {
68buf[0] = 0x01;
69memset(buf + 1, 0xff, readcnt - 1);
70}
71ret = 0;
72#else
73if (writecnt)
74ret = serialport_write(buf, writecnt);
75if (ret)
76return ret;
77if (readcnt)
78ret = serialport_read(buf, readcnt);
79if (ret)
80return ret;
81#endif
82msg_pspew(", receiving");
83for (i = 0; i < readcnt; i++)
84msg_pspew(" 0x%02x", buf[i]);
85msg_pspew("\n");
86return 0;
87}
88
89static int buspirate_spi_send_command(struct flashctx *flash,
90 unsigned int writecnt,
91 unsigned int readcnt,
92 const unsigned char *writearr,
93 unsigned char *readarr);
94
95static const struct spi_programmer spi_programmer_buspirate = {
96.type= SPI_CONTROLLER_BUSPIRATE,
97.max_data_read= 12,
98.max_data_write= 12,
99.command= buspirate_spi_send_command,
100.multicommand= default_spi_send_multicommand,
101.read= default_spi_read,
102.write_256= default_spi_write_256,
103};
104
105static const struct buspirate_spispeeds spispeeds[] = {
106{"30k",0x0},
107{"125k",0x1},
108{"250k",0x2},
109{"1M",0x3},
110{"2M",0x4},
111{"2.6M",0x5},
112{"4M",0x6},
113{"8M",0x7},
114{NULL,0x0},
115};
116
117static int buspirate_spi_shutdown(void *data)
118{
119unsigned char buf[5];
120int ret = 0;
121
122/* Exit raw SPI mode (enter raw bitbang mode) */
123buf[0] = 0x00;
124ret = buspirate_sendrecv(buf, 1, 5);
125if (ret)
126return ret;
127if (memcmp(buf, "BBIO", 4)) {
128msg_perr("Entering raw bitbang mode failed!\n");
129return 1;
130}
131msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
132if (buf[4] != '1') {
133msg_perr("Can't handle raw bitbang mode version %c!\n",
134buf[4]);
135return 1;
136}
137/* Reset Bus Pirate (return to user terminal) */
138buf[0] = 0x0f;
139ret = buspirate_sendrecv(buf, 1, 0);
140if (ret)
141return ret;
142
143/* Shut down serial port communication */
144ret = serialport_shutdown(NULL);
145if (ret)
146return ret;
147msg_pdbg("Bus Pirate shutdown completed.\n");
148
149return 0;
150}
151
152int buspirate_spi_init(void)
153{
154unsigned char buf[512];
155char *dev = NULL;
156char *speed = NULL;
157int spispeed = 0x7;
158int ret = 0;
159int i;
160
161dev = extract_programmer_param("dev");
162if (!dev || !strlen(dev)) {
163msg_perr("No serial device given. Use flashrom -p "
164"buspirate_spi:dev=/dev/ttyUSB0\n");
165return 1;
166}
167
168speed = extract_programmer_param("spispeed");
169if (speed) {
170for (i = 0; spispeeds[i].name; i++)
171if (!strncasecmp(spispeeds[i].name, speed,
172 strlen(spispeeds[i].name))) {
173spispeed = spispeeds[i].speed;
174break;
175}
176if (!spispeeds[i].name)
177msg_perr("Invalid SPI speed, using default.\n");
178}
179free(speed);
180
181/* This works because speeds numbering starts at 0 and is contiguous. */
182msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
183
184ret = buspirate_serialport_setup(dev);
185if (ret)
186return ret;
187free(dev);
188
189if (register_shutdown(buspirate_spi_shutdown, NULL))
190return 1;
191
192/* This is the brute force version, but it should work. */
193for (i = 0; i < 19; i++) {
194/* Enter raw bitbang mode */
195buf[0] = 0x00;
196/* Send the command, don't read the response. */
197ret = buspirate_sendrecv(buf, 1, 0);
198if (ret)
199return ret;
200/* Read any response and discard it. */
201sp_flush_incoming();
202}
203/* USB is slow. The Bus Pirate is even slower. Apparently the flush
204 * action above is too fast or too early. Some stuff still remains in
205 * the pipe after the flush above, and one additional flush is not
206 * sufficient either. Use a 1.5 ms delay inside the loop to make
207 * mostly sure that at least one USB frame had time to arrive.
208 * Looping only 5 times is not sufficient and causes the
209 * occasional failure.
210 * Folding the delay into the loop above is not reliable either.
211 */
212for (i = 0; i < 10; i++) {
213usleep(1500);
214/* Read any response and discard it. */
215sp_flush_incoming();
216}
217/* Enter raw bitbang mode */
218buf[0] = 0x00;
219ret = buspirate_sendrecv(buf, 1, 5);
220if (ret)
221return ret;
222if (memcmp(buf, "BBIO", 4)) {
223msg_perr("Entering raw bitbang mode failed!\n");
224msg_pdbg("Got %02x%02x%02x%02x%02x\n",
225 buf[0], buf[1], buf[2], buf[3], buf[4]);
226return 1;
227}
228msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
229if (buf[4] != '1') {
230msg_perr("Can't handle raw bitbang mode version %c!\n",
231buf[4]);
232return 1;
233}
234/* Enter raw SPI mode */
235buf[0] = 0x01;
236ret = buspirate_sendrecv(buf, 1, 4);
237if (ret)
238return ret;
239if (memcmp(buf, "SPI", 3)) {
240msg_perr("Entering raw SPI mode failed!\n");
241msg_pdbg("Got %02x%02x%02x%02x\n",
242 buf[0], buf[1], buf[2], buf[3]);
243return 1;
244}
245msg_pdbg("Raw SPI mode version %c\n", buf[3]);
246if (buf[3] != '1') {
247msg_perr("Can't handle raw SPI mode version %c!\n",
248buf[3]);
249return 1;
250}
251
252/* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
253buf[0] = 0x40 | 0xb;
254ret = buspirate_sendrecv(buf, 1, 1);
255if (ret)
256return 1;
257if (buf[0] != 0x01) {
258msg_perr("Protocol error while setting power/CS/AUX!\n");
259return 1;
260}
261
262/* Set SPI speed */
263buf[0] = 0x60 | spispeed;
264ret = buspirate_sendrecv(buf, 1, 1);
265if (ret)
266return 1;
267if (buf[0] != 0x01) {
268msg_perr("Protocol error while setting SPI speed!\n");
269return 1;
270}
271
272/* Set SPI config: output type, idle, clock edge, sample */
273buf[0] = 0x80 | 0xa;
274ret = buspirate_sendrecv(buf, 1, 1);
275if (ret)
276return 1;
277if (buf[0] != 0x01) {
278msg_perr("Protocol error while setting SPI config!\n");
279return 1;
280}
281
282/* De-assert CS# */
283buf[0] = 0x03;
284ret = buspirate_sendrecv(buf, 1, 1);
285if (ret)
286return 1;
287if (buf[0] != 0x01) {
288msg_perr("Protocol error while raising CS#!\n");
289return 1;
290}
291
292register_spi_programmer(&spi_programmer_buspirate);
293
294return 0;
295}
296
297static int buspirate_spi_send_command(struct flashctx *flash,
298 unsigned int writecnt,
299 unsigned int readcnt,
300 const unsigned char *writearr,
301 unsigned char *readarr)
302{
303static unsigned char *buf = NULL;
304unsigned int i = 0;
305int ret = 0;
306
307if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
308return SPI_INVALID_LENGTH;
309
310/* 3 bytes extra for CS#, len, CS#. */
311buf = realloc(buf, writecnt + readcnt + 3);
312if (!buf) {
313msg_perr("Out of memory!\n");
314exit(1); // -1
315}
316
317/* Assert CS# */
318buf[i++] = 0x02;
319
320buf[i++] = 0x10 | (writecnt + readcnt - 1);
321memcpy(buf + i, writearr, writecnt);
322i += writecnt;
323memset(buf + i, 0, readcnt);
324
325i += readcnt;
326/* De-assert CS# */
327buf[i++] = 0x03;
328
329ret = buspirate_sendrecv(buf, i, i);
330
331if (ret) {
332msg_perr("Bus Pirate communication error!\n");
333return SPI_GENERIC_ERROR;
334}
335
336if (buf[0] != 0x01) {
337msg_perr("Protocol error while lowering CS#!\n");
338return SPI_GENERIC_ERROR;
339}
340
341if (buf[1] != 0x01) {
342msg_perr("Protocol error while reading/writing SPI!\n");
343return SPI_GENERIC_ERROR;
344}
345
346if (buf[i - 1] != 0x01) {
347msg_perr("Protocol error while raising CS#!\n");
348return SPI_GENERIC_ERROR;
349}
350
351/* Skip CS#, length, writearr. */
352memcpy(readarr, buf + 2 + writecnt, readcnt);
353
354return ret;
355}
356

Archive Download this file

Revision: HEAD