Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 1 | /* |
| 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 Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 25 | #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 |
| 32 | int 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 Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 38 | #else |
| 39 | #define buspirate_serialport_setup(...) 0 |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 40 | #define serialport_shutdown(...) 0 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 41 | #define serialport_write(...) 0 |
| 42 | #define serialport_read(...) 0 |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 43 | #define sp_flush_incoming(...) 0 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, unsigned int readcnt) |
| 47 | { |
| 48 | int i, ret = 0; |
| 49 | |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 50 | msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 51 | if (!writecnt && !readcnt) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 52 | msg_perr("Zero length command!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 53 | return 1; |
| 54 | } |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 55 | msg_pspew("Sending"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 56 | for (i = 0; i < writecnt; i++) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 57 | msg_pspew(" 0x%02x", buf[i]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 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 |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 75 | msg_pspew(", receiving"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 76 | for (i = 0; i < readcnt; i++) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 77 | msg_pspew(" 0x%02x", buf[i]); |
| 78 | msg_pspew("\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 82 | static 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 Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 94 | int buspirate_spi_init(void) |
| 95 | { |
| 96 | unsigned char buf[512]; |
| 97 | int ret = 0; |
| 98 | int i; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 99 | char *dev = NULL; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 100 | char *speed = NULL; |
| 101 | int spispeed = 0x7; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 102 | |
| 103 | if (programmer_param && !strlen(programmer_param)) { |
| 104 | free(programmer_param); |
| 105 | programmer_param = NULL; |
| 106 | } |
| 107 | if (programmer_param) { |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 108 | dev = extract_param(&programmer_param, "dev=", ",:"); |
| 109 | speed = extract_param(&programmer_param, "spispeed=", ",:"); |
| 110 | if (strlen(programmer_param)) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 111 | msg_perr("Unhandled programmer parameters: %s\n", |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 112 | programmer_param); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 113 | free(programmer_param); |
| 114 | programmer_param = NULL; |
| 115 | } |
| 116 | if (!dev) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 117 | msg_perr("No serial device given. Use flashrom -p " |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 118 | "buspiratespi:dev=/dev/ttyUSB0\n"); |
| 119 | return 1; |
| 120 | } |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 121 | 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 Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 129 | msg_perr("Invalid SPI speed, using default.\n"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 130 | } |
| 131 | /* This works because speeds numbering starts at 0 and is contiguous. */ |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 132 | msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 133 | |
| 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 Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 147 | sp_flush_incoming(); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 148 | } |
| 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 Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 155 | msg_perr("Entering raw bitbang mode failed!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 156 | return 1; |
| 157 | } |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 158 | msg_pdbg("Raw bitbang mode version %c\n", buf[4]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 159 | if (buf[4] != '1') { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 160 | msg_perr("Can't handle raw bitbang mode version %c!\n", |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 161 | 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 Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 168 | msg_perr("Entering raw SPI mode failed!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 169 | return 1; |
| 170 | } |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 171 | msg_pdbg("Raw SPI mode version %c\n", buf[3]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 172 | if (buf[3] != '1') { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 173 | msg_perr("Can't handle raw SPI mode version %c!\n", |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 174 | 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 Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 184 | msg_perr("Protocol error while setting power/CS/AUX!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 185 | return 1; |
| 186 | } |
| 187 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 188 | /* Set SPI speed */ |
| 189 | buf[0] = 0x60 | spispeed; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 190 | ret = buspirate_sendrecv(buf, 1, 1); |
| 191 | if (ret) |
| 192 | return 1; |
| 193 | if (buf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 194 | msg_perr("Protocol error while setting SPI speed!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 195 | 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 Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 204 | msg_perr("Protocol error while setting SPI config!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 205 | 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 Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 214 | msg_perr("Protocol error while raising CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 215 | return 1; |
| 216 | } |
| 217 | |
| 218 | buses_supported = CHIP_BUSTYPE_SPI; |
| 219 | spi_controller = SPI_CONTROLLER_BUSPIRATE; |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | int 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 Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 235 | msg_perr("Entering raw bitbang mode failed!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 236 | return 1; |
| 237 | } |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 238 | msg_pdbg("Raw bitbang mode version %c\n", buf[4]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 239 | if (buf[4] != '1') { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 240 | msg_perr("Can't handle raw bitbang mode version %c!\n", |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 241 | 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 Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 251 | ret = serialport_shutdown(); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 252 | if (ret) |
| 253 | return ret; |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 254 | msg_pdbg("Bus Pirate shutdown completed.\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | int 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 Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 271 | msg_perr("Out of memory!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 272 | 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 Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 281 | msg_perr("Protocol error while lowering CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 282 | 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 Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 294 | msg_perr("Protocol error while reading/writing SPI!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 295 | 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 Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 306 | msg_perr("Protocol error while raising CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 307 | return SPI_GENERIC_ERROR; |
| 308 | } |
| 309 | |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | int buspirate_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 314 | { |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 315 | return spi_read_chunked(flash, buf, start, len, 12); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* We could do 12-byte writes, but for now we use the generic 1-byte code. */ |