blob: f1a0be1d63d01a920a0fd836c05d36acc385b294 [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"
Sean Nelson14ba6682010-02-26 05:48:29 +000026#include "chipdrivers.h"
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000027#include "spi.h"
28
29/* Change this to #define if you want to test without a serial implementation */
30#undef FAKE_COMMUNICATION
31
32#ifndef FAKE_COMMUNICATION
33int buspirate_serialport_setup(char *dev)
34{
35 /* 115200bps, 8 databits, no parity, 1 stopbit */
36 sp_fd = sp_openserport(dev, 115200);
37 return 0;
38}
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000039#else
40#define buspirate_serialport_setup(...) 0
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +000041#define serialport_shutdown(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000042#define serialport_write(...) 0
43#define serialport_read(...) 0
Patrick Georgi3b6237d2010-01-06 19:09:40 +000044#define sp_flush_incoming(...) 0
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000045#endif
46
47int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, unsigned int readcnt)
48{
49 int i, ret = 0;
50
Sean Nelson84f7bce2010-01-09 23:56:41 +000051 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000052 if (!writecnt && !readcnt) {
Sean Nelson84f7bce2010-01-09 23:56:41 +000053 msg_perr("Zero length command!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000054 return 1;
55 }
Sean Nelson84f7bce2010-01-09 23:56:41 +000056 msg_pspew("Sending");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000057 for (i = 0; i < writecnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000058 msg_pspew(" 0x%02x", buf[i]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000059#ifdef FAKE_COMMUNICATION
60 /* Placate the caller for now. */
61 if (readcnt) {
62 buf[0] = 0x01;
63 memset(buf + 1, 0xff, readcnt - 1);
64 }
65 ret = 0;
66#else
67 if (writecnt)
68 ret = serialport_write(buf, writecnt);
69 if (ret)
70 return ret;
71 if (readcnt)
72 ret = serialport_read(buf, readcnt);
73 if (ret)
74 return ret;
75#endif
Sean Nelson84f7bce2010-01-09 23:56:41 +000076 msg_pspew(", receiving");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000077 for (i = 0; i < readcnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000078 msg_pspew(" 0x%02x", buf[i]);
79 msg_pspew("\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000080 return 0;
81}
82
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +000083static const struct buspirate_spispeeds spispeeds[] = {
84 {"30k", 0x0},
85 {"125k", 0x1},
86 {"250k", 0x2},
87 {"1M", 0x3},
88 {"2M", 0x4},
89 {"2.6M", 0x5},
90 {"4M", 0x6},
91 {"8M", 0x7},
92 {NULL, 0x0}
93};
94
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000095int buspirate_spi_init(void)
96{
97 unsigned char buf[512];
98 int ret = 0;
99 int i;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000100 char *dev = NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000101 char *speed = NULL;
102 int spispeed = 0x7;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000103
104 if (programmer_param && !strlen(programmer_param)) {
105 free(programmer_param);
106 programmer_param = NULL;
107 }
108 if (programmer_param) {
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000109 dev = extract_param(&programmer_param, "dev=", ",:");
110 speed = extract_param(&programmer_param, "spispeed=", ",:");
111 if (strlen(programmer_param))
Sean Nelson84f7bce2010-01-09 23:56:41 +0000112 msg_perr("Unhandled programmer parameters: %s\n",
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000113 programmer_param);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000114 free(programmer_param);
115 programmer_param = NULL;
116 }
117 if (!dev) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000118 msg_perr("No serial device given. Use flashrom -p "
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000119 "buspiratespi:dev=/dev/ttyUSB0\n");
120 return 1;
121 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000122 if (speed) {
123 for (i = 0; spispeeds[i].name; i++)
124 if (!strncasecmp(spispeeds[i].name, speed,
125 strlen(spispeeds[i].name))) {
126 spispeed = spispeeds[i].speed;
127 break;
128 }
129 if (!spispeeds[i].name)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000130 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000131 }
132 /* This works because speeds numbering starts at 0 and is contiguous. */
Sean Nelson84f7bce2010-01-09 23:56:41 +0000133 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000134
135 ret = buspirate_serialport_setup(dev);
136 if (ret)
137 return ret;
138
139 /* This is the brute force version, but it should work. */
140 for (i = 0; i < 19; i++) {
141 /* Enter raw bitbang mode */
142 buf[0] = 0x00;
143 /* Send the command, don't read the response. */
144 ret = buspirate_sendrecv(buf, 1, 0);
145 if (ret)
146 return ret;
147 /* Read any response and discard it. */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000148 sp_flush_incoming();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000149 }
150 /* Enter raw bitbang mode */
151 buf[0] = 0x00;
152 ret = buspirate_sendrecv(buf, 1, 5);
153 if (ret)
154 return ret;
155 if (memcmp(buf, "BBIO", 4)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000156 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000157 return 1;
158 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000159 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000160 if (buf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000161 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000162 buf[4]);
163 return 1;
164 }
165 /* Enter raw SPI mode */
166 buf[0] = 0x01;
167 ret = buspirate_sendrecv(buf, 1, 4);
168 if (memcmp(buf, "SPI", 3)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000169 msg_perr("Entering raw SPI mode failed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000170 return 1;
171 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000172 msg_pdbg("Raw SPI mode version %c\n", buf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000173 if (buf[3] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000174 msg_perr("Can't handle raw SPI mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000175 buf[3]);
176 return 1;
177 }
178
179 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
180 buf[0] = 0x40 | 0xb;
181 ret = buspirate_sendrecv(buf, 1, 1);
182 if (ret)
183 return 1;
184 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000185 msg_perr("Protocol error while setting power/CS/AUX!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000186 return 1;
187 }
188
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000189 /* Set SPI speed */
190 buf[0] = 0x60 | spispeed;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000191 ret = buspirate_sendrecv(buf, 1, 1);
192 if (ret)
193 return 1;
194 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000195 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000196 return 1;
197 }
198
199 /* Set SPI config: output type, idle, clock edge, sample */
200 buf[0] = 0x80 | 0xa;
201 ret = buspirate_sendrecv(buf, 1, 1);
202 if (ret)
203 return 1;
204 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000205 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000206 return 1;
207 }
208
209 /* De-assert CS# */
210 buf[0] = 0x03;
211 ret = buspirate_sendrecv(buf, 1, 1);
212 if (ret)
213 return 1;
214 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000215 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000216 return 1;
217 }
218
219 buses_supported = CHIP_BUSTYPE_SPI;
220 spi_controller = SPI_CONTROLLER_BUSPIRATE;
221
222 return 0;
223}
224
225int buspirate_spi_shutdown(void)
226{
227 unsigned char buf[5];
228 int ret = 0;
229
230 /* Exit raw SPI mode (enter raw bitbang mode) */
231 buf[0] = 0x00;
232 ret = buspirate_sendrecv(buf, 1, 5);
233 if (ret)
234 return ret;
235 if (memcmp(buf, "BBIO", 4)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000236 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000237 return 1;
238 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000239 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000240 if (buf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000241 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000242 buf[4]);
243 return 1;
244 }
245 /* Reset Bus Pirate (return to user terminal) */
246 buf[0] = 0x0f;
247 ret = buspirate_sendrecv(buf, 1, 0);
248 if (ret)
249 return ret;
250
251 /* Shut down serial port communication */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000252 ret = serialport_shutdown();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000253 if (ret)
254 return ret;
Sean Nelson84f7bce2010-01-09 23:56:41 +0000255 msg_pdbg("Bus Pirate shutdown completed.\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000256
257 return 0;
258}
259
260int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt,
261 const unsigned char *writearr, unsigned char *readarr)
262{
263 static unsigned char *buf = NULL;
264 int i = 0, ret = 0;
265
266 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
267 return SPI_INVALID_LENGTH;
268
269 /* +2 is pretty arbitrary. */
270 buf = realloc(buf, writecnt + readcnt + 2);
271 if (!buf) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000272 msg_perr("Out of memory!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000273 exit(1); // -1
274 }
275
276 /* Assert CS# */
277 buf[i++] = 0x02;
278 ret = buspirate_sendrecv(buf, 1, 1);
279 if (ret)
280 return SPI_GENERIC_ERROR;
281 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000282 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000283 return SPI_GENERIC_ERROR;
284 }
285
286 i = 0;
287 buf[i++] = 0x10 | (writecnt + readcnt - 1);
288 memcpy(buf + i, writearr, writecnt);
289 i += writecnt;
290 memset(buf + i, 0, readcnt);
291 ret = buspirate_sendrecv(buf, i + readcnt, i + readcnt);
292 if (ret)
293 return SPI_GENERIC_ERROR;
294 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000295 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000296 return SPI_GENERIC_ERROR;
297 }
298 memcpy(readarr, buf + i, readcnt);
299
300 i = 0;
301 /* De-assert CS# */
302 buf[i++] = 0x03;
303 ret = buspirate_sendrecv(buf, 1, 1);
304 if (ret)
305 return SPI_GENERIC_ERROR;
306 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000307 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000308 return SPI_GENERIC_ERROR;
309 }
310
311 return ret;
312}
313
314int buspirate_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
315{
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000316 return spi_read_chunked(flash, buf, start, len, 12);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000317}
318
319/* We could do 12-byte writes, but for now we use the generic 1-byte code. */