buspirate_spi.c 7.41 KB
Newer Older
1 2 3
/*
 * This file is part of the flashrom project.
 *
4
 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "flash.h"
26
#include "chipdrivers.h"
27 28 29 30 31 32
#include "spi.h"

/* Change this to #define if you want to test without a serial implementation */
#undef FAKE_COMMUNICATION

#ifndef FAKE_COMMUNICATION
33
static int buspirate_serialport_setup(char *dev)
34 35 36 37 38 39 40
{
	/* 115200bps, 8 databits, no parity, 1 stopbit */
	sp_fd = sp_openserport(dev, 115200);
	return 0;
}
#else
#define buspirate_serialport_setup(...) 0
41
#define serialport_shutdown(...) 0
42 43
#define serialport_write(...) 0
#define serialport_read(...) 0
Patrick Georgi's avatar
Patrick Georgi committed
44
#define sp_flush_incoming(...) 0
45 46
#endif

47
static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, unsigned int readcnt)
48 49 50
{
	int i, ret = 0;

51
	msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt);
52
	if (!writecnt && !readcnt) {
53
		msg_perr("Zero length command!\n");
54 55
		return 1;
	}
56
	msg_pspew("Sending");
57
	for (i = 0; i < writecnt; i++)
58
		msg_pspew(" 0x%02x", buf[i]);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#ifdef FAKE_COMMUNICATION
	/* Placate the caller for now. */
	if (readcnt) {
		buf[0] = 0x01;
		memset(buf + 1, 0xff, readcnt - 1);
	}
	ret = 0;
#else
	if (writecnt)
		ret = serialport_write(buf, writecnt);
	if (ret)
		return ret;
	if (readcnt)
		ret = serialport_read(buf, readcnt);
	if (ret)
		return ret;
#endif
76
	msg_pspew(", receiving");
77
	for (i = 0; i < readcnt; i++)
78 79
		msg_pspew(" 0x%02x", buf[i]);
	msg_pspew("\n");
80 81 82
	return 0;
}

83 84 85 86 87 88 89 90 91 92 93 94
static const struct buspirate_spispeeds spispeeds[] = {
	{"30k",		0x0},
	{"125k",	0x1},
	{"250k",	0x2},
	{"1M",		0x3},
	{"2M",		0x4},
	{"2.6M",	0x5},
	{"4M",		0x6},
	{"8M",		0x7},
	{NULL,		0x0}
};

95 96 97 98 99 100
int buspirate_spi_init(void)
{
	unsigned char buf[512];
	int ret = 0;
	int i;
	char *dev = NULL;
101 102
	char *speed = NULL;
	int spispeed = 0x7;
103

104
	dev = extract_programmer_param("dev");
105
	if (!dev || !strlen(dev)) {
106
		msg_perr("No serial device given. Use flashrom -p "
107
			"buspirate_spi:dev=/dev/ttyUSB0\n");
108 109
		return 1;
	}
110

111
	speed = extract_programmer_param("spispeed");
112 113 114 115 116 117 118 119
	if (speed) {
		for (i = 0; spispeeds[i].name; i++)
			if (!strncasecmp(spispeeds[i].name, speed,
			    strlen(spispeeds[i].name))) {
				spispeed = spispeeds[i].speed;
				break;
			}
		if (!spispeeds[i].name)
120
			msg_perr("Invalid SPI speed, using default.\n");
121
	}
122 123
	free(speed);

124
	/* This works because speeds numbering starts at 0 and is contiguous. */
125
	msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name);
126 127 128 129

	ret = buspirate_serialport_setup(dev);
	if (ret)
		return ret;
130
	free(dev);
131 132 133 134 135 136 137 138 139 140

	/* This is the brute force version, but it should work. */
	for (i = 0; i < 19; i++) {
		/* Enter raw bitbang mode */
		buf[0] = 0x00;
		/* Send the command, don't read the response. */
		ret = buspirate_sendrecv(buf, 1, 0);
		if (ret)
			return ret;
		/* Read any response and discard it. */
Patrick Georgi's avatar
Patrick Georgi committed
141
		sp_flush_incoming();
142 143 144 145 146 147 148
	}
	/* Enter raw bitbang mode */
	buf[0] = 0x00;
	ret = buspirate_sendrecv(buf, 1, 5);
	if (ret)
		return ret;
	if (memcmp(buf, "BBIO", 4)) {
149
		msg_perr("Entering raw bitbang mode failed!\n");
150 151
		return 1;
	}
152
	msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
153
	if (buf[4] != '1') {
154
		msg_perr("Can't handle raw bitbang mode version %c!\n",
155 156 157 158 159 160 161
			buf[4]);
		return 1;
	}
	/* Enter raw SPI mode */
	buf[0] = 0x01;
	ret = buspirate_sendrecv(buf, 1, 4);
	if (memcmp(buf, "SPI", 3)) {
162
		msg_perr("Entering raw SPI mode failed!\n");
163 164
		return 1;
	}
165
	msg_pdbg("Raw SPI mode version %c\n", buf[3]);
166
	if (buf[3] != '1') {
167
		msg_perr("Can't handle raw SPI mode version %c!\n",
168 169 170 171 172 173 174 175 176 177
			buf[3]);
		return 1;
	}

	/* Initial setup (SPI peripherals config): Enable power, CS high, AUX */
	buf[0] = 0x40 | 0xb;
	ret = buspirate_sendrecv(buf, 1, 1);
	if (ret)
		return 1;
	if (buf[0] != 0x01) {
178
		msg_perr("Protocol error while setting power/CS/AUX!\n");
179 180 181
		return 1;
	}

182 183
	/* Set SPI speed */
	buf[0] = 0x60 | spispeed;
184 185 186 187
	ret = buspirate_sendrecv(buf, 1, 1);
	if (ret)
		return 1;
	if (buf[0] != 0x01) {
188
		msg_perr("Protocol error while setting SPI speed!\n");
189 190 191 192 193 194 195 196 197
		return 1;
	}
	
	/* Set SPI config: output type, idle, clock edge, sample */
	buf[0] = 0x80 | 0xa;
	ret = buspirate_sendrecv(buf, 1, 1);
	if (ret)
		return 1;
	if (buf[0] != 0x01) {
198
		msg_perr("Protocol error while setting SPI config!\n");
199 200 201 202 203 204 205 206 207
		return 1;
	}

	/* De-assert CS# */
	buf[0] = 0x03;
	ret = buspirate_sendrecv(buf, 1, 1);
	if (ret)
		return 1;
	if (buf[0] != 0x01) {
208
		msg_perr("Protocol error while raising CS#!\n");
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
		return 1;
	}

	buses_supported = CHIP_BUSTYPE_SPI;
	spi_controller = SPI_CONTROLLER_BUSPIRATE;

	return 0;
}

int buspirate_spi_shutdown(void)
{
	unsigned char buf[5];
	int ret = 0;

	/* Exit raw SPI mode (enter raw bitbang mode) */
	buf[0] = 0x00;
	ret = buspirate_sendrecv(buf, 1, 5);
	if (ret)
		return ret;
	if (memcmp(buf, "BBIO", 4)) {
229
		msg_perr("Entering raw bitbang mode failed!\n");
230 231
		return 1;
	}
232
	msg_pdbg("Raw bitbang mode version %c\n", buf[4]);
233
	if (buf[4] != '1') {
234
		msg_perr("Can't handle raw bitbang mode version %c!\n",
235 236 237 238 239 240 241 242 243 244
			buf[4]);
		return 1;
	}
	/* Reset Bus Pirate (return to user terminal) */
	buf[0] = 0x0f;
	ret = buspirate_sendrecv(buf, 1, 0);
	if (ret)
		return ret;

	/* Shut down serial port communication */
245
	ret = serialport_shutdown();
246 247
	if (ret)
		return ret;
248
	msg_pdbg("Bus Pirate shutdown completed.\n");
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

	return 0;
}

int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt,
		const unsigned char *writearr, unsigned char *readarr)
{
	static unsigned char *buf = NULL;
	int i = 0, ret = 0;

	if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
		return SPI_INVALID_LENGTH;

	/* +2 is pretty arbitrary. */
	buf = realloc(buf, writecnt + readcnt + 2);
	if (!buf) {
265
		msg_perr("Out of memory!\n");
266 267 268 269 270 271 272 273 274
		exit(1); // -1
	}

	/* Assert CS# */
	buf[i++] = 0x02;
	ret = buspirate_sendrecv(buf, 1, 1);
	if (ret)
		return SPI_GENERIC_ERROR;
	if (buf[0] != 0x01) {
275
		msg_perr("Protocol error while lowering CS#!\n");
276 277 278 279 280 281 282 283 284 285 286 287
		return SPI_GENERIC_ERROR;
	}

	i = 0;
	buf[i++] = 0x10 | (writecnt + readcnt - 1);
	memcpy(buf + i, writearr, writecnt);
	i += writecnt;
	memset(buf + i, 0, readcnt);
	ret = buspirate_sendrecv(buf, i + readcnt, i + readcnt);
	if (ret)
		return SPI_GENERIC_ERROR;
	if (buf[0] != 0x01) {
288
		msg_perr("Protocol error while reading/writing SPI!\n");
289 290 291 292 293 294 295 296 297 298 299
		return SPI_GENERIC_ERROR;
	}
	memcpy(readarr, buf + i, readcnt);

	i = 0;
	/* De-assert CS# */
	buf[i++] = 0x03;
	ret = buspirate_sendrecv(buf, 1, 1);
	if (ret)
		return SPI_GENERIC_ERROR;
	if (buf[0] != 0x01) {
300
		msg_perr("Protocol error while raising CS#!\n");
301 302 303 304 305 306 307 308
		return SPI_GENERIC_ERROR;
	}

	return ret;
}

int buspirate_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
{
309
	return spi_read_chunked(flash, buf, start, len, 12);
310 311
}

312
int buspirate_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
313 314
{
	spi_disable_blockprotect();
315
	return spi_write_chunked(flash, buf, start, len, 12);
316
}