Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2018 Lubomir Rintel <lkundrak@v3.sk> |
| 5 | * |
| 6 | * Based on ft2232_spi.c: |
| 7 | * |
| 8 | * Copyright (C) 2011 asbokid <ballymunboy@gmail.com> |
| 9 | * Copyright (C) 2014 Pluto Yang <yangyj.ee@gmail.com> |
| 10 | * Copyright (C) 2015-2016 Stefan Tauner |
| 11 | * Copyright (C) 2015 Urja Rannikko <urjaman@gmail.com> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 | */ |
| 27 | |
| 28 | /* |
| 29 | * The reverse-engineered protocol description was obtained from the |
| 30 | * iceBurn project <https://github.com/davidcarne/iceBurn> by |
| 31 | * David Carne <davidcarne@gmail.com>. |
| 32 | */ |
| 33 | |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <libusb.h> |
| 37 | #include "programmer.h" |
| 38 | |
| 39 | /* This is pretty much arbitrarily chosen. After one second without a |
| 40 | * response we can be pretty sure we're not going to succeed. */ |
| 41 | #define USB_TIMEOUT 1000 |
| 42 | |
| 43 | #define CMD_WRITE_EP 0x01 |
| 44 | #define CMD_READ_EP 0x82 |
| 45 | #define DATA_WRITE_EP 0x03 |
| 46 | #define DATA_READ_EP 0x84 |
| 47 | |
| 48 | static struct libusb_device_handle *handle = NULL; |
| 49 | static bool reset_board; |
| 50 | |
| 51 | #define DIGILENT_VID 0x1443 |
| 52 | #define DIGILENT_JTAG_PID 0x0007 |
| 53 | |
| 54 | const struct dev_entry devs_digilent_spi[] = { |
| 55 | { DIGILENT_VID, DIGILENT_JTAG_PID, OK, "Digilent", "Development board JTAG" }, |
| 56 | { 0 }, |
| 57 | }; |
| 58 | |
| 59 | /* Control endpoint commands. */ |
| 60 | enum { |
| 61 | GET_BOARD_TYPE = 0xe2, |
| 62 | GET_BOARD_SERIAL = 0xe4, |
| 63 | }; |
| 64 | |
| 65 | /* Command bulk endpoint command groups. */ |
| 66 | enum { |
| 67 | CMD_GPIO = 0x03, |
| 68 | CMD_BOARD = 0x04, |
| 69 | CMD_SPI = 0x06, |
| 70 | }; |
| 71 | |
| 72 | /* GPIO subcommands. */ |
| 73 | enum { |
| 74 | CMD_GPIO_OPEN = 0x00, |
| 75 | CMD_GPIO_CLOSE = 0x01, |
| 76 | CMD_GPIO_SET_DIR = 0x04, |
| 77 | CMD_GPIO_SET_VAL = 0x06, |
| 78 | }; |
| 79 | |
| 80 | /* Board subcommands. */ |
| 81 | enum { |
| 82 | CMD_BOARD_OPEN = 0x00, |
| 83 | CMD_BOARD_CLOSE = 0x01, |
| 84 | CMD_BOARD_SET_REG = 0x04, |
| 85 | CMD_BOARD_GET_REG = 0x05, |
| 86 | CMD_BOARD_PL_STAT = 0x85, |
| 87 | }; |
| 88 | |
| 89 | /* SPI subcommands. */ |
| 90 | enum { |
| 91 | CMD_SPI_OPEN = 0x00, |
| 92 | CMD_SPI_CLOSE = 0x01, |
| 93 | CMD_SPI_SET_SPEED = 0x03, |
| 94 | CMD_SPI_SET_MODE = 0x05, |
| 95 | CMD_SPI_SET_CS = 0x06, |
| 96 | CMD_SPI_START_IO = 0x07, |
| 97 | CMD_SPI_TX_END = 0x87, |
| 98 | }; |
| 99 | |
| 100 | static int do_command(uint8_t *req, int req_len, uint8_t *res, int res_len) |
| 101 | { |
| 102 | int tx_len = 0; |
| 103 | int ret; |
| 104 | |
| 105 | req[0] = req_len - 1; |
| 106 | ret = libusb_bulk_transfer(handle, CMD_WRITE_EP, req, req_len, &tx_len, USB_TIMEOUT); |
| 107 | if (ret) { |
| 108 | msg_perr("Failed to issue a command: '%s'\n", libusb_error_name(ret)); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | if (tx_len != req_len) { |
| 113 | msg_perr("Short write issuing a command\n"); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | ret = libusb_bulk_transfer(handle, CMD_READ_EP, res, res_len, &tx_len, USB_TIMEOUT); |
| 118 | if (ret) { |
| 119 | msg_perr("Failed to get a response: '%s'\n", libusb_error_name(ret)); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | if (tx_len != res_len) { |
| 124 | msg_perr("Short read getting a response\n"); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | if (res[0] != res_len -1) { |
| 129 | msg_perr("Response indicates incorrect length.\n"); |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int gpio_open(void) |
| 137 | { |
| 138 | uint8_t req[] = { 0x00, CMD_GPIO, CMD_GPIO_OPEN, 0x00 }; |
| 139 | uint8_t res[2]; |
| 140 | |
| 141 | return do_command(req, sizeof(req), res, sizeof(res)); |
| 142 | } |
| 143 | |
| 144 | static int gpio_set_dir(uint8_t direction) |
| 145 | { |
| 146 | uint8_t req[] = { 0x00, CMD_GPIO, CMD_GPIO_SET_DIR, 0x00, |
| 147 | direction, 0x00, 0x00, 0x00 }; |
| 148 | uint8_t res[6]; |
| 149 | |
| 150 | return do_command(req, sizeof(req), res, sizeof(res)); |
| 151 | } |
| 152 | |
| 153 | static int gpio_set_value(uint8_t value) |
| 154 | { |
| 155 | uint8_t req[] = { 0x00, CMD_GPIO, CMD_GPIO_SET_VAL, 0x00, |
| 156 | value, 0x00, 0x00, 0x00 }; |
| 157 | uint8_t res[2]; |
| 158 | |
| 159 | return do_command(req, sizeof(req), res, sizeof(res)); |
| 160 | } |
| 161 | |
| 162 | static int spi_open(void) |
| 163 | { |
| 164 | uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_OPEN, 0x00 }; |
| 165 | uint8_t res[2]; |
| 166 | |
| 167 | return do_command(req, sizeof(req), res, sizeof(res)); |
| 168 | } |
| 169 | |
| 170 | static int spi_set_speed(uint32_t speed) |
| 171 | { |
| 172 | uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_SET_SPEED, 0x00, |
| 173 | (speed) & 0xff, |
| 174 | (speed >> 8) & 0xff, |
| 175 | (speed >> 16) & 0xff, |
| 176 | (speed >> 24) & 0xff }; |
| 177 | uint8_t res[6]; |
| 178 | uint32_t real_speed; |
| 179 | int ret; |
| 180 | |
| 181 | ret = do_command(req, sizeof(req), res, sizeof(res)); |
| 182 | if (ret) |
| 183 | return ret; |
| 184 | |
| 185 | real_speed = (res[5] << 24) | (res[4] << 16) | (res[3] << 8) | res[2]; |
| 186 | if (real_speed != speed) |
| 187 | msg_pwarn("SPI speed set to %d instead of %d\n", real_speed, speed); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static int spi_set_mode(uint8_t mode) |
| 193 | { |
| 194 | uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_SET_MODE, 0x00, mode }; |
| 195 | uint8_t res[2]; |
| 196 | |
| 197 | return do_command(req, sizeof(req), res, sizeof(res)); |
| 198 | } |
| 199 | |
| 200 | static int spi_set_cs(uint8_t cs) |
| 201 | { |
| 202 | uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_SET_CS, 0x00, cs }; |
| 203 | uint8_t res[2]; |
| 204 | |
| 205 | return do_command(req, sizeof(req), res, sizeof(res)); |
| 206 | } |
| 207 | |
| 208 | static int spi_start_io(uint8_t read_follows, uint32_t write_len) |
| 209 | { |
| 210 | uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_START_IO, 0x00, |
| 211 | 0x00, 0x00, /* meaning unknown */ |
| 212 | read_follows, |
| 213 | (write_len) & 0xff, |
| 214 | (write_len >> 8) & 0xff, |
| 215 | (write_len >> 16) & 0xff, |
| 216 | (write_len >> 24) & 0xff }; |
| 217 | uint8_t res[2]; |
| 218 | |
| 219 | return do_command(req, sizeof(req), res, sizeof(res)); |
| 220 | } |
| 221 | |
| 222 | static int spi_tx_end(uint8_t read_follows, uint32_t tx_len) |
| 223 | { |
| 224 | uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_TX_END, 0x00 }; |
| 225 | uint8_t res[read_follows ? 10 : 6]; |
| 226 | int ret; |
| 227 | uint32_t count; |
| 228 | |
| 229 | ret = do_command(req, sizeof(req), res, sizeof(res)); |
| 230 | if (ret != 0) |
| 231 | return ret; |
| 232 | |
| 233 | if ((res[1] & 0x80) == 0) { |
| 234 | msg_perr("%s: response missing a write count\n", __func__); |
| 235 | return -1; |
| 236 | } |
| 237 | |
| 238 | count = res[2] | (res[3] << 8) | (res[4] << 16) | res[5] << 24; |
| 239 | if (count != tx_len) { |
| 240 | msg_perr("%s: wrote only %d bytes instead of %d\n", __func__, count, tx_len); |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | if (read_follows) { |
| 245 | if ((res[1] & 0x40) == 0) { |
| 246 | msg_perr("%s: response missing a read count\n", __func__); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | count = res[6] | (res[7] << 8) | (res[8] << 16) | res[9] << 24; |
| 251 | if (count != tx_len) { |
| 252 | msg_perr("%s: read only %d bytes instead of %d\n", __func__, count, tx_len); |
| 253 | return -1; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int digilent_spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
| 261 | const unsigned char *writearr, unsigned char *readarr) |
| 262 | { |
| 263 | int ret; |
| 264 | int len = writecnt + readcnt; |
| 265 | int tx_len = 0; |
| 266 | uint8_t buf[len]; |
| 267 | uint8_t read_follows = readcnt > 0 ? 1 : 0; |
| 268 | |
| 269 | memcpy(buf, writearr, writecnt); |
| 270 | memset(buf + writecnt, 0xff, readcnt); |
| 271 | |
| 272 | ret = spi_set_cs(0); |
| 273 | if (ret != 0) |
| 274 | return ret; |
| 275 | |
| 276 | ret = spi_start_io(read_follows, writecnt); |
| 277 | if (ret != 0) |
| 278 | return ret; |
| 279 | |
| 280 | ret = libusb_bulk_transfer(handle, DATA_WRITE_EP, buf, len, &tx_len, USB_TIMEOUT); |
| 281 | if (ret != 0) { |
| 282 | msg_perr("%s: failed to write data: '%s'\n", __func__, libusb_error_name(ret)); |
| 283 | return -1; |
| 284 | } |
| 285 | if (tx_len != len) { |
| 286 | msg_perr("%s: short write\n", __func__); |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | if (read_follows) { |
| 291 | ret = libusb_bulk_transfer(handle, DATA_READ_EP, buf, len, &tx_len, USB_TIMEOUT); |
| 292 | if (ret != 0) { |
| 293 | msg_perr("%s: failed to read data: '%s'\n", __func__, libusb_error_name(ret)); |
| 294 | return -1; |
| 295 | } |
| 296 | if (tx_len != len) { |
| 297 | msg_perr("%s: short read\n", __func__); |
| 298 | return -1; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | ret = spi_tx_end(read_follows, len); |
| 303 | if (ret != 0) |
| 304 | return ret; |
| 305 | |
| 306 | ret = spi_set_cs(1); |
| 307 | if (ret != 0) |
| 308 | return ret; |
| 309 | |
| 310 | memcpy(readarr, &buf[writecnt], readcnt); |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static const struct spi_master spi_master_digilent_spi = { |
| 316 | .type = SPI_CONTROLLER_DIGILENT_SPI, |
| 317 | .features = SPI_MASTER_4BA, |
| 318 | .max_data_read = 252, |
| 319 | .max_data_write = 252, |
| 320 | .command = digilent_spi_send_command, |
| 321 | .multicommand = default_spi_send_multicommand, |
| 322 | .read = default_spi_read, |
| 323 | .write_256 = default_spi_write_256, |
| 324 | .write_aai = default_spi_write_aai, |
| 325 | }; |
| 326 | |
| 327 | |
| 328 | static int digilent_spi_shutdown(void *data) |
| 329 | { |
| 330 | if (reset_board) |
| 331 | gpio_set_dir(0); |
| 332 | |
| 333 | libusb_close(handle); |
| 334 | handle = NULL; |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static bool default_reset(void) |
| 340 | { |
| 341 | char board[17]; |
| 342 | |
| 343 | libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR, |
| 344 | GET_BOARD_TYPE, 0, 0, |
| 345 | (unsigned char *)board, sizeof(board) - 1, USB_TIMEOUT); |
| 346 | board[sizeof(board) -1] = '\0'; |
| 347 | |
| 348 | if (strcmp(board, "iCE40") == 0) |
| 349 | return true; |
| 350 | |
| 351 | msg_pwarn("%s: unknown board '%s' not attempting a reset. " |
| 352 | "Override with '-p digilent_spi=reset=1'.\n", __func__, board); |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | struct digilent_spispeeds { |
| 357 | const char *const name; |
| 358 | const int speed; |
| 359 | }; |
| 360 | |
| 361 | static const struct digilent_spispeeds spispeeds[] = { |
| 362 | { "4M", 4000000 }, |
| 363 | { "2M", 2000000 }, |
| 364 | { "1M", 1000000 }, |
| 365 | { "500k", 500000 }, |
| 366 | { "250k", 250000 }, |
| 367 | { "125k", 125000 }, |
| 368 | { "62.5k", 62500 }, |
| 369 | { NULL, 0 }, |
| 370 | }; |
| 371 | |
| 372 | int digilent_spi_init(void) |
| 373 | { |
| 374 | char *p; |
| 375 | uint32_t speed_hz = spispeeds[0].speed; |
| 376 | int i; |
| 377 | |
| 378 | if (handle != NULL) { |
| 379 | msg_cerr("%s: handle already set! Please report a bug at flashrom@flashrom.org\n", __func__); |
| 380 | return -1; |
| 381 | } |
| 382 | |
| 383 | int32_t ret = libusb_init(NULL); |
| 384 | if (ret < 0) { |
| 385 | msg_perr("%s: couldn't initialize libusb!\n", __func__); |
| 386 | return -1; |
| 387 | } |
| 388 | |
Arthur Heymans | f3ce951 | 2018-07-17 02:16:44 +0200 | [diff] [blame] | 389 | #if LIBUSB_API_VERSION < 0x01000106 |
Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 390 | libusb_set_debug(NULL, 3); |
Arthur Heymans | f3ce951 | 2018-07-17 02:16:44 +0200 | [diff] [blame] | 391 | #else |
| 392 | libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO); |
| 393 | #endif |
Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 394 | |
| 395 | uint16_t vid = devs_digilent_spi[0].vendor_id; |
| 396 | uint16_t pid = devs_digilent_spi[0].device_id; |
| 397 | handle = libusb_open_device_with_vid_pid(NULL, vid, pid); |
| 398 | if (handle == NULL) { |
| 399 | msg_perr("%s: couldn't open device %04x:%04x.\n", __func__, vid, pid); |
| 400 | return -1; |
| 401 | } |
| 402 | |
| 403 | ret = libusb_claim_interface(handle, 0); |
| 404 | if (ret != 0) { |
| 405 | msg_perr("%s: failed to claim interface 0: '%s'\n", __func__, libusb_error_name(ret)); |
| 406 | goto close_handle; |
| 407 | } |
| 408 | |
| 409 | p = extract_programmer_param("spispeed"); |
| 410 | if (p) { |
| 411 | for (i = 0; spispeeds[i].name; ++i) { |
| 412 | if (!strcasecmp(spispeeds[i].name, p)) { |
| 413 | speed_hz = spispeeds[i].speed; |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | if (!spispeeds[i].name) { |
| 418 | msg_perr("Error: Invalid spispeed value: '%s'.\n", p); |
| 419 | free(p); |
| 420 | goto close_handle; |
| 421 | } |
| 422 | free(p); |
| 423 | } |
| 424 | |
| 425 | p = extract_programmer_param("reset"); |
| 426 | if (p && strlen(p)) |
| 427 | reset_board = (p[0] == '1'); |
| 428 | else |
| 429 | reset_board = default_reset(); |
| 430 | free(p); |
| 431 | |
| 432 | if (reset_board) { |
| 433 | if (gpio_open() != 0) |
| 434 | goto close_handle; |
| 435 | if (gpio_set_dir(1) != 0) |
| 436 | goto close_handle; |
| 437 | if (gpio_set_value(0) != 0) |
| 438 | goto close_handle; |
| 439 | } |
| 440 | |
| 441 | if (spi_open() != 0) |
| 442 | goto close_handle; |
| 443 | if (spi_set_speed(speed_hz) != 0) |
| 444 | goto close_handle; |
| 445 | if (spi_set_mode(0x00) != 0) |
| 446 | goto close_handle; |
| 447 | |
| 448 | register_shutdown(digilent_spi_shutdown, NULL); |
| 449 | register_spi_master(&spi_master_digilent_spi); |
| 450 | |
| 451 | return 0; |
| 452 | |
| 453 | close_handle: |
| 454 | libusb_close(handle); |
| 455 | handle = NULL; |
| 456 | return -1; |
| 457 | } |