blob: ecdd68986491d9cba3f5ea3adede9493c289ed50 [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
Patrick Georgi3b6237d2010-01-06 19:09:40 +000043#define sp_flush_incoming(...) 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
Sean Nelson84f7bce2010-01-09 23:56:41 +000050 msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000051 if (!writecnt && !readcnt) {
Sean Nelson84f7bce2010-01-09 23:56:41 +000052 msg_perr("Zero length command!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000053 return 1;
54 }
Sean Nelson84f7bce2010-01-09 23:56:41 +000055 msg_pspew("Sending");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000056 for (i = 0; i < writecnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000057 msg_pspew(" 0x%02x", buf[i]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000058#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
Sean Nelson84f7bce2010-01-09 23:56:41 +000075 msg_pspew(", receiving");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000076 for (i = 0; i < readcnt; i++)
Sean Nelson84f7bce2010-01-09 23:56:41 +000077 msg_pspew(" 0x%02x", buf[i]);
78 msg_pspew("\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +000079 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))
Sean Nelson84f7bce2010-01-09 23:56:41 +0000111 msg_perr("Unhandled programmer parameters: %s\n",
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000112 programmer_param);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000113 free(programmer_param);
114 programmer_param = NULL;
115 }
116 if (!dev) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000117 msg_perr("No serial device given. Use flashrom -p "
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000118 "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)
Sean Nelson84f7bce2010-01-09 23:56:41 +0000129 msg_perr("Invalid SPI speed, using default.\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000130 }
131 /* This works because speeds numbering starts at 0 and is contiguous. */
Sean Nelson84f7bce2010-01-09 23:56:41 +0000132 msg_pdbg("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. */
Patrick Georgi3b6237d2010-01-06 19:09:40 +0000147 sp_flush_incoming();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000148 }
149 /* Enter raw bitbang mode */
150 buf[0] = 0x00;
151 ret = buspirate_sendrecv(buf, 1, 5);
152 if (ret)
153 return ret;
154 if (memcmp(buf, "BBIO", 4)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000155 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000156 return 1;
157 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000158 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000159 if (buf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000160 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000161 buf[4]);
162 return 1;
163 }
164 /* Enter raw SPI mode */
165 buf[0] = 0x01;
166 ret = buspirate_sendrecv(buf, 1, 4);
167 if (memcmp(buf, "SPI", 3)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000168 msg_perr("Entering raw SPI mode failed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000169 return 1;
170 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000171 msg_pdbg("Raw SPI mode version %c\n", buf[3]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000172 if (buf[3] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000173 msg_perr("Can't handle raw SPI mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000174 buf[3]);
175 return 1;
176 }
177
178 /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
179 buf[0] = 0x40 | 0xb;
180 ret = buspirate_sendrecv(buf, 1, 1);
181 if (ret)
182 return 1;
183 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000184 msg_perr("Protocol error while setting power/CS/AUX!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000185 return 1;
186 }
187
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000188 /* Set SPI speed */
189 buf[0] = 0x60 | spispeed;
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000190 ret = buspirate_sendrecv(buf, 1, 1);
191 if (ret)
192 return 1;
193 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000194 msg_perr("Protocol error while setting SPI speed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000195 return 1;
196 }
197
198 /* Set SPI config: output type, idle, clock edge, sample */
199 buf[0] = 0x80 | 0xa;
200 ret = buspirate_sendrecv(buf, 1, 1);
201 if (ret)
202 return 1;
203 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000204 msg_perr("Protocol error while setting SPI config!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000205 return 1;
206 }
207
208 /* De-assert CS# */
209 buf[0] = 0x03;
210 ret = buspirate_sendrecv(buf, 1, 1);
211 if (ret)
212 return 1;
213 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000214 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000215 return 1;
216 }
217
218 buses_supported = CHIP_BUSTYPE_SPI;
219 spi_controller = SPI_CONTROLLER_BUSPIRATE;
220
221 return 0;
222}
223
224int buspirate_spi_shutdown(void)
225{
226 unsigned char buf[5];
227 int ret = 0;
228
229 /* Exit raw SPI mode (enter raw bitbang mode) */
230 buf[0] = 0x00;
231 ret = buspirate_sendrecv(buf, 1, 5);
232 if (ret)
233 return ret;
234 if (memcmp(buf, "BBIO", 4)) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000235 msg_perr("Entering raw bitbang mode failed!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000236 return 1;
237 }
Sean Nelson84f7bce2010-01-09 23:56:41 +0000238 msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000239 if (buf[4] != '1') {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000240 msg_perr("Can't handle raw bitbang mode version %c!\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000241 buf[4]);
242 return 1;
243 }
244 /* Reset Bus Pirate (return to user terminal) */
245 buf[0] = 0x0f;
246 ret = buspirate_sendrecv(buf, 1, 0);
247 if (ret)
248 return ret;
249
250 /* Shut down serial port communication */
Carl-Daniel Hailfingerefa151e2010-01-06 16:09:10 +0000251 ret = serialport_shutdown();
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000252 if (ret)
253 return ret;
Sean Nelson84f7bce2010-01-09 23:56:41 +0000254 msg_pdbg("Bus Pirate shutdown completed.\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000255
256 return 0;
257}
258
259int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt,
260 const unsigned char *writearr, unsigned char *readarr)
261{
262 static unsigned char *buf = NULL;
263 int i = 0, ret = 0;
264
265 if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
266 return SPI_INVALID_LENGTH;
267
268 /* +2 is pretty arbitrary. */
269 buf = realloc(buf, writecnt + readcnt + 2);
270 if (!buf) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000271 msg_perr("Out of memory!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000272 exit(1); // -1
273 }
274
275 /* Assert CS# */
276 buf[i++] = 0x02;
277 ret = buspirate_sendrecv(buf, 1, 1);
278 if (ret)
279 return SPI_GENERIC_ERROR;
280 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000281 msg_perr("Protocol error while lowering CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000282 return SPI_GENERIC_ERROR;
283 }
284
285 i = 0;
286 buf[i++] = 0x10 | (writecnt + readcnt - 1);
287 memcpy(buf + i, writearr, writecnt);
288 i += writecnt;
289 memset(buf + i, 0, readcnt);
290 ret = buspirate_sendrecv(buf, i + readcnt, i + readcnt);
291 if (ret)
292 return SPI_GENERIC_ERROR;
293 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000294 msg_perr("Protocol error while reading/writing SPI!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000295 return SPI_GENERIC_ERROR;
296 }
297 memcpy(readarr, buf + i, readcnt);
298
299 i = 0;
300 /* De-assert CS# */
301 buf[i++] = 0x03;
302 ret = buspirate_sendrecv(buf, 1, 1);
303 if (ret)
304 return SPI_GENERIC_ERROR;
305 if (buf[0] != 0x01) {
Sean Nelson84f7bce2010-01-09 23:56:41 +0000306 msg_perr("Protocol error while raising CS#!\n");
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000307 return SPI_GENERIC_ERROR;
308 }
309
310 return ret;
311}
312
313int buspirate_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
314{
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000315 return spi_read_chunked(flash, buf, start, len, 12);
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000316}
317
318/* We could do 12-byte writes, but for now we use the generic 1-byte code. */