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