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 | |
| 134 | int buspirate_spi_init(void) |
| 135 | { |
| 136 | unsigned char buf[512]; |
| 137 | int ret = 0; |
| 138 | int i; |
| 139 | char *devpos = NULL; |
| 140 | char *dev = NULL; |
| 141 | int devlen; |
| 142 | |
| 143 | if (programmer_param && !strlen(programmer_param)) { |
| 144 | free(programmer_param); |
| 145 | programmer_param = NULL; |
| 146 | } |
| 147 | if (programmer_param) { |
| 148 | devpos = strstr(programmer_param, "dev="); |
| 149 | if (devpos) { |
| 150 | devpos += 4; |
| 151 | devlen = strcspn(devpos, ",:"); |
| 152 | if (devlen) { |
| 153 | dev = malloc(devlen + 1); |
| 154 | if (!dev) { |
| 155 | fprintf(stderr, "Out of memory!\n"); |
| 156 | exit(1); |
| 157 | } |
| 158 | strncpy(dev, devpos, devlen); |
| 159 | dev[devlen] = '\0'; |
| 160 | } |
| 161 | } |
| 162 | free(programmer_param); |
| 163 | programmer_param = NULL; |
| 164 | } |
| 165 | if (!dev) { |
| 166 | fprintf(stderr, "No serial device given. Use flashrom -p " |
| 167 | "buspiratespi:dev=/dev/ttyUSB0\n"); |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | ret = buspirate_serialport_setup(dev); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
| 175 | /* This is the brute force version, but it should work. */ |
| 176 | for (i = 0; i < 19; i++) { |
| 177 | /* Enter raw bitbang mode */ |
| 178 | buf[0] = 0x00; |
| 179 | /* Send the command, don't read the response. */ |
| 180 | ret = buspirate_sendrecv(buf, 1, 0); |
| 181 | if (ret) |
| 182 | return ret; |
| 183 | /* Read any response and discard it. */ |
| 184 | ret = buspirate_discard_read(); |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | } |
| 188 | /* Enter raw bitbang mode */ |
| 189 | buf[0] = 0x00; |
| 190 | ret = buspirate_sendrecv(buf, 1, 5); |
| 191 | if (ret) |
| 192 | return ret; |
| 193 | if (memcmp(buf, "BBIO", 4)) { |
| 194 | fprintf(stderr, "Entering raw bitbang mode failed!\n"); |
| 195 | return 1; |
| 196 | } |
| 197 | printf_debug("Raw bitbang mode version %c\n", buf[4]); |
| 198 | if (buf[4] != '1') { |
| 199 | fprintf(stderr, "Can't handle raw bitbang mode version %c!\n", |
| 200 | buf[4]); |
| 201 | return 1; |
| 202 | } |
| 203 | /* Enter raw SPI mode */ |
| 204 | buf[0] = 0x01; |
| 205 | ret = buspirate_sendrecv(buf, 1, 4); |
| 206 | if (memcmp(buf, "SPI", 3)) { |
| 207 | fprintf(stderr, "Entering raw SPI mode failed!\n"); |
| 208 | return 1; |
| 209 | } |
| 210 | printf_debug("Raw SPI mode version %c\n", buf[3]); |
| 211 | if (buf[3] != '1') { |
| 212 | fprintf(stderr, "Can't handle raw SPI mode version %c!\n", |
| 213 | buf[3]); |
| 214 | return 1; |
| 215 | } |
| 216 | |
| 217 | /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */ |
| 218 | buf[0] = 0x40 | 0xb; |
| 219 | ret = buspirate_sendrecv(buf, 1, 1); |
| 220 | if (ret) |
| 221 | return 1; |
| 222 | if (buf[0] != 0x01) { |
| 223 | fprintf(stderr, "Protocol error while setting power/CS/AUX!\n"); |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | /* Set speed to 8 MHz */ |
| 228 | buf[0] = 0x60 | 0x7; |
| 229 | ret = buspirate_sendrecv(buf, 1, 1); |
| 230 | if (ret) |
| 231 | return 1; |
| 232 | if (buf[0] != 0x01) { |
| 233 | fprintf(stderr, "Protocol error while setting SPI speed!\n"); |
| 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | /* Set SPI config: output type, idle, clock edge, sample */ |
| 238 | buf[0] = 0x80 | 0xa; |
| 239 | ret = buspirate_sendrecv(buf, 1, 1); |
| 240 | if (ret) |
| 241 | return 1; |
| 242 | if (buf[0] != 0x01) { |
| 243 | fprintf(stderr, "Protocol error while setting SPI config!\n"); |
| 244 | return 1; |
| 245 | } |
| 246 | |
| 247 | /* De-assert CS# */ |
| 248 | buf[0] = 0x03; |
| 249 | ret = buspirate_sendrecv(buf, 1, 1); |
| 250 | if (ret) |
| 251 | return 1; |
| 252 | if (buf[0] != 0x01) { |
| 253 | fprintf(stderr, "Protocol error while raising CS#!\n"); |
| 254 | return 1; |
| 255 | } |
| 256 | |
| 257 | buses_supported = CHIP_BUSTYPE_SPI; |
| 258 | spi_controller = SPI_CONTROLLER_BUSPIRATE; |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | int buspirate_spi_shutdown(void) |
| 264 | { |
| 265 | unsigned char buf[5]; |
| 266 | int ret = 0; |
| 267 | |
| 268 | /* Exit raw SPI mode (enter raw bitbang mode) */ |
| 269 | buf[0] = 0x00; |
| 270 | ret = buspirate_sendrecv(buf, 1, 5); |
| 271 | if (ret) |
| 272 | return ret; |
| 273 | if (memcmp(buf, "BBIO", 4)) { |
| 274 | fprintf(stderr, "Entering raw bitbang mode failed!\n"); |
| 275 | return 1; |
| 276 | } |
| 277 | printf_debug("Raw bitbang mode version %c\n", buf[4]); |
| 278 | if (buf[4] != '1') { |
| 279 | fprintf(stderr, "Can't handle raw bitbang mode version %c!\n", |
| 280 | buf[4]); |
| 281 | return 1; |
| 282 | } |
| 283 | /* Reset Bus Pirate (return to user terminal) */ |
| 284 | buf[0] = 0x0f; |
| 285 | ret = buspirate_sendrecv(buf, 1, 0); |
| 286 | if (ret) |
| 287 | return ret; |
| 288 | |
| 289 | /* Shut down serial port communication */ |
| 290 | ret = buspirate_serialport_shutdown(); |
| 291 | if (ret) |
| 292 | return ret; |
| 293 | printf_debug("Bus Pirate shutdown completed.\n"); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | int buspirate_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 299 | const unsigned char *writearr, unsigned char *readarr) |
| 300 | { |
| 301 | static unsigned char *buf = NULL; |
| 302 | int i = 0, ret = 0; |
| 303 | |
| 304 | if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16) |
| 305 | return SPI_INVALID_LENGTH; |
| 306 | |
| 307 | /* +2 is pretty arbitrary. */ |
| 308 | buf = realloc(buf, writecnt + readcnt + 2); |
| 309 | if (!buf) { |
| 310 | fprintf(stderr, "Out of memory!\n"); |
| 311 | exit(1); // -1 |
| 312 | } |
| 313 | |
| 314 | /* Assert CS# */ |
| 315 | buf[i++] = 0x02; |
| 316 | ret = buspirate_sendrecv(buf, 1, 1); |
| 317 | if (ret) |
| 318 | return SPI_GENERIC_ERROR; |
| 319 | if (buf[0] != 0x01) { |
| 320 | fprintf(stderr, "Protocol error while lowering CS#!\n"); |
| 321 | return SPI_GENERIC_ERROR; |
| 322 | } |
| 323 | |
| 324 | i = 0; |
| 325 | buf[i++] = 0x10 | (writecnt + readcnt - 1); |
| 326 | memcpy(buf + i, writearr, writecnt); |
| 327 | i += writecnt; |
| 328 | memset(buf + i, 0, readcnt); |
| 329 | ret = buspirate_sendrecv(buf, i + readcnt, i + readcnt); |
| 330 | if (ret) |
| 331 | return SPI_GENERIC_ERROR; |
| 332 | if (buf[0] != 0x01) { |
| 333 | fprintf(stderr, "Protocol error while reading/writing SPI!\n"); |
| 334 | return SPI_GENERIC_ERROR; |
| 335 | } |
| 336 | memcpy(readarr, buf + i, readcnt); |
| 337 | |
| 338 | i = 0; |
| 339 | /* De-assert CS# */ |
| 340 | buf[i++] = 0x03; |
| 341 | ret = buspirate_sendrecv(buf, 1, 1); |
| 342 | if (ret) |
| 343 | return SPI_GENERIC_ERROR; |
| 344 | if (buf[0] != 0x01) { |
| 345 | fprintf(stderr, "Protocol error while raising CS#!\n"); |
| 346 | return SPI_GENERIC_ERROR; |
| 347 | } |
| 348 | |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | int buspirate_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 353 | { |
| 354 | /* Maximum read length is 12 bytes, use 8 for now. */ |
| 355 | return spi_read_chunked(flash, buf, start, len, 8); |
| 356 | } |
| 357 | |
| 358 | /* We could do 12-byte writes, but for now we use the generic 1-byte code. */ |