blob: 62433d4b884d58e91ff9879e2dc34dee65cc34df [file] [log] [blame]
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 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 <stdint.h>
22#include <string.h>
23#include <stdlib.h>
24#include <ctype.h>
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000025#include "flash.h"
26#include "spi.h"
27
28/* Change this to #define if you want to test without a serial implementation */
29#undef FAKE_COMMUNICATION
30
31#ifndef FAKE_COMMUNICATION
32int buspirate_serialport_setup(char *dev)
33{
34 /* 115200bps, 8 databits, no parity, 1 stopbit */
35 sp_fd = sp_openserport(dev, 115200);
36 return 0;
37}
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000038#else
39#define buspirate_serialport_setup(...) 0
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +000040#define serialport_shutdown(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000041#define serialport_write(...) 0
42#define serialport_read(...) 0
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +000043#define serialport_discard_read(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000044#endif
45
46int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, unsigned int readcnt)
47{
48 int i, ret = 0;
49
50 printf_debug("%s: write %i, read %i\n", __func__, writecnt, readcnt);
51 if (!writecnt && !readcnt) {
52 fprintf(stderr, "Zero length command!\n");
53 return 1;
54 }
55 printf_debug("Sending");
56 for (i = 0; i < writecnt; i++)
57 printf_debug(" 0x%02x", buf[i]);
58#ifdef FAKE_COMMUNICATION
59 /* Placate the caller for now. */
60 if (readcnt) {
61 buf[0] = 0x01;
62 memset(buf + 1, 0xff, readcnt - 1);
63 }
64 ret = 0;
65#else
66 if (writecnt)
67 ret = serialport_write(buf, writecnt);
68 if (ret)
69 return ret;
70 if (readcnt)
71 ret = serialport_read(buf, readcnt);
72 if (ret)
73 return ret;
74#endif
75 printf_debug(", receiving");
76 for (i = 0; i < readcnt; i++)
77 printf_debug(" 0x%02x", buf[i]);
78 printf_debug("\n");
79 return 0;
80}
81
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +000082static const struct buspirate_spispeeds spispeeds[] = {
83 {"30k", 0x0},
84 {"125k", 0x1},
85 {"250k", 0x2},
86 {"1M", 0x3},
87 {"2M", 0x4},
88 {"2.6M", 0x5},
89 {"4M", 0x6},
90 {"8M", 0x7},
91 {NULL, 0x0}
92};
93
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000094int buspirate_spi_init(void)
95{
96 unsigned char buf[512];
97 int ret = 0;
98 int i;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000099 char *dev = NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000100 char *speed = NULL;
101 int spispeed = 0x7;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000102
103 if (programmer_param && !strlen(programmer_param)) {
104 free(programmer_param);
105 programmer_param = NULL;
106 }
107 if (programmer_param) {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000108 dev = extract_param(&programmer_param, "dev=", ",:");
109 speed = extract_param(&programmer_param, "spispeed=", ",:");
110 if (strlen(programmer_param))
111 fprintf(stderr, "Unhandled programmer parameters: %s\n",
112 programmer_param);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000113 free(programmer_param);
114 programmer_param = NULL;
115 }
116 if (!dev) {
117 fprintf(stderr, "No serial device given. Use flashrom -p "
118 "buspiratespi:dev=/dev/ttyUSB0\n");
119 return 1;
120 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000121 if (speed) {
122 for (i = 0; spispeeds[i].name; i++)
123 if (!strncasecmp(spispeeds[i].name, speed,
124 strlen(spispeeds[i].name))) {
125 spispeed = spispeeds[i].speed;
126 break;
127 }
128 if (!spispeeds[i].name)
129 fprintf(stderr, "Invalid SPI speed, using default.\n");
130 }
131 /* This works because speeds numbering starts at 0 and is contiguous. */
132 printf_debug("SPI speed is %sHz\n", spispeeds[spispeed].name);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000133
134 ret = buspirate_serialport_setup(dev);
135 if (ret)
136 return ret;
137
138 /* This is the brute force version, but it should work. */
139 for (i = 0; i < 19; i++) {
140 /* Enter raw bitbang mode */
141 buf[0] = 0x00;
142 /* Send the command, don't read the response. */
143 ret = buspirate_sendrecv(buf, 1, 0);
144 if (ret)
145 return ret;
146 /* Read any response and discard it. */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000147 ret = serialport_discard_read();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000148 if (ret)
149 return ret;
150 }
151 /* Enter raw bitbang mode */
152 buf[0] = 0x00;
153 ret = buspirate_sendrecv(buf, 1, 5);
154 if (ret)
155 return ret;
156 if (memcmp(buf, "BBIO", 4)) {
157 fprintf(stderr, "Entering raw bitbang mode failed!\n");
158 return 1;
159 }
160 printf_debug("Raw bitbang mode version %c\n", buf[4]);
161 if (buf[4] != '1') {
162 fprintf(stderr, "Can't handle raw bitbang mode version %c!\n",
163 buf[4]);
164 return 1;
165 }
166 /* Enter raw SPI mode */
167 buf[0] = 0x01;
168 ret = buspirate_sendrecv(buf, 1, 4);
169 if (memcmp(buf, "SPI", 3)) {
170 fprintf(stderr, "Entering raw SPI mode failed!\n");
171 return 1;
172 }
173 printf_debug("Raw SPI mode version %c\n", buf[3]);
174 if (buf[3] != '1') {
175 fprintf(stderr, "Can't handle raw SPI mode version %c!\n",
176 buf[3]);
177 return 1;
178 }
179
180 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
181 buf[0] = 0x40 | 0xb;
182 ret = buspirate_sendrecv(buf, 1, 1);
183 if (ret)
184 return 1;
185 if (buf[0] != 0x01) {
186 fprintf(stderr, "Protocol error while setting power/CS/AUX!\n");
187 return 1;
188 }
189
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000190 /* Set SPI speed */
191 buf[0] = 0x60 | spispeed;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000192 ret = buspirate_sendrecv(buf, 1, 1);
193 if (ret)
194 return 1;
195 if (buf[0] != 0x01) {
196 fprintf(stderr, "Protocol error while setting SPI speed!\n");
197 return 1;
198 }
199
200 /* Set SPI config: output type, idle, clock edge, sample */
201 buf[0] = 0x80 | 0xa;
202 ret = buspirate_sendrecv(buf, 1, 1);
203 if (ret)
204 return 1;
205 if (buf[0] != 0x01) {
206 fprintf(stderr, "Protocol error while setting SPI config!\n");
207 return 1;
208 }
209
210 /* De-assert CS# */
211 buf[0] = 0x03;
212 ret = buspirate_sendrecv(buf, 1, 1);
213 if (ret)
214 return 1;
215 if (buf[0] != 0x01) {
216 fprintf(stderr, "Protocol error while raising CS#!\n");
217 return 1;
218 }
219
220 buses_supported = CHIP_BUSTYPE_SPI;
221 spi_controller = SPI_CONTROLLER_BUSPIRATE;
222
223 return 0;
224}
225
226int buspirate_spi_shutdown(void)
227{
228 unsigned char buf[5];
229 int ret = 0;
230
231 /* Exit raw SPI mode (enter raw bitbang mode) */
232 buf[0] = 0x00;
233 ret = buspirate_sendrecv(buf, 1, 5);
234 if (ret)
235 return ret;
236 if (memcmp(buf, "BBIO", 4)) {
237 fprintf(stderr, "Entering raw bitbang mode failed!\n");
238 return 1;
239 }
240 printf_debug("Raw bitbang mode version %c\n", buf[4]);
241 if (buf[4] != '1') {
242 fprintf(stderr, "Can't handle raw bitbang mode version %c!\n",
243 buf[4]);
244 return 1;
245 }
246 /* Reset Bus Pirate (return to user terminal) */
247 buf[0] = 0x0f;
248 ret = buspirate_sendrecv(buf, 1, 0);
249 if (ret)
250 return ret;
251
252 /* Shut down serial port communication */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000253 ret = serialport_shutdown();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000254 if (ret)
255 return ret;
256 printf_debug("Bus Pirate shutdown completed.\n");
257
258 return 0;
259}
260
261int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt,
262 const unsigned char *writearr, unsigned char *readarr)
263{
264 static unsigned char *buf = NULL;
265 int i = 0, ret = 0;
266
267 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
268 return SPI_INVALID_LENGTH;
269
270 /* +2 is pretty arbitrary. */
271 buf = realloc(buf, writecnt + readcnt + 2);
272 if (!buf) {
273 fprintf(stderr, "Out of memory!\n");
274 exit(1); // -1
275 }
276
277 /* Assert CS# */
278 buf[i++] = 0x02;
279 ret = buspirate_sendrecv(buf, 1, 1);
280 if (ret)
281 return SPI_GENERIC_ERROR;
282 if (buf[0] != 0x01) {
283 fprintf(stderr, "Protocol error while lowering CS#!\n");
284 return SPI_GENERIC_ERROR;
285 }
286
287 i = 0;
288 buf[i++] = 0x10 | (writecnt + readcnt - 1);
289 memcpy(buf + i, writearr, writecnt);
290 i += writecnt;
291 memset(buf + i, 0, readcnt);
292 ret = buspirate_sendrecv(buf, i + readcnt, i + readcnt);
293 if (ret)
294 return SPI_GENERIC_ERROR;
295 if (buf[0] != 0x01) {
296 fprintf(stderr, "Protocol error while reading/writing SPI!\n");
297 return SPI_GENERIC_ERROR;
298 }
299 memcpy(readarr, buf + i, readcnt);
300
301 i = 0;
302 /* De-assert CS# */
303 buf[i++] = 0x03;
304 ret = buspirate_sendrecv(buf, 1, 1);
305 if (ret)
306 return SPI_GENERIC_ERROR;
307 if (buf[0] != 0x01) {
308 fprintf(stderr, "Protocol error while raising CS#!\n");
309 return SPI_GENERIC_ERROR;
310 }
311
312 return ret;
313}
314
315int buspirate_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
316{
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000317 return spi_read_chunked(flash, buf, start, len, 12);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000318}
319
320/* We could do 12-byte writes, but for now we use the generic 1-byte code. */