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