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 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 54 | static const struct dev_entry devs_digilent_spi[] = { |
Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 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 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 260 | static int digilent_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 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 | |
Anastasia Klimchuk | c63d918 | 2021-07-06 16:18:44 +1000 | [diff] [blame] | 315 | static int digilent_spi_shutdown(void *data); |
| 316 | |
Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 317 | static const struct spi_master spi_master_digilent_spi = { |
Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 318 | .features = SPI_MASTER_4BA, |
| 319 | .max_data_read = 252, |
| 320 | .max_data_write = 252, |
| 321 | .command = digilent_spi_send_command, |
| 322 | .multicommand = default_spi_send_multicommand, |
| 323 | .read = default_spi_read, |
| 324 | .write_256 = default_spi_write_256, |
| 325 | .write_aai = default_spi_write_aai, |
Anastasia Klimchuk | c63d918 | 2021-07-06 16:18:44 +1000 | [diff] [blame] | 326 | .shutdown = digilent_spi_shutdown, |
Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | |
| 330 | static int digilent_spi_shutdown(void *data) |
| 331 | { |
| 332 | if (reset_board) |
| 333 | gpio_set_dir(0); |
| 334 | |
| 335 | libusb_close(handle); |
| 336 | handle = NULL; |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static bool default_reset(void) |
| 342 | { |
| 343 | char board[17]; |
| 344 | |
| 345 | libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR, |
| 346 | GET_BOARD_TYPE, 0, 0, |
| 347 | (unsigned char *)board, sizeof(board) - 1, USB_TIMEOUT); |
| 348 | board[sizeof(board) -1] = '\0'; |
| 349 | |
| 350 | if (strcmp(board, "iCE40") == 0) |
| 351 | return true; |
| 352 | |
| 353 | msg_pwarn("%s: unknown board '%s' not attempting a reset. " |
| 354 | "Override with '-p digilent_spi=reset=1'.\n", __func__, board); |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | struct digilent_spispeeds { |
| 359 | const char *const name; |
| 360 | const int speed; |
| 361 | }; |
| 362 | |
| 363 | static const struct digilent_spispeeds spispeeds[] = { |
| 364 | { "4M", 4000000 }, |
| 365 | { "2M", 2000000 }, |
| 366 | { "1M", 1000000 }, |
| 367 | { "500k", 500000 }, |
| 368 | { "250k", 250000 }, |
| 369 | { "125k", 125000 }, |
| 370 | { "62.5k", 62500 }, |
| 371 | { NULL, 0 }, |
| 372 | }; |
| 373 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 374 | static int digilent_spi_init(void) |
Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 375 | { |
| 376 | char *p; |
| 377 | uint32_t speed_hz = spispeeds[0].speed; |
| 378 | int i; |
| 379 | |
| 380 | if (handle != NULL) { |
Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 381 | msg_cerr("%s: handle already set!\n" |
| 382 | "Please report a bug at flashrom-stable@flashrom.org\n", |
| 383 | __func__); |
Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 384 | return -1; |
| 385 | } |
| 386 | |
| 387 | int32_t ret = libusb_init(NULL); |
| 388 | if (ret < 0) { |
| 389 | msg_perr("%s: couldn't initialize libusb!\n", __func__); |
| 390 | return -1; |
| 391 | } |
| 392 | |
Arthur Heymans | f3ce951 | 2018-07-17 02:16:44 +0200 | [diff] [blame] | 393 | #if LIBUSB_API_VERSION < 0x01000106 |
Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 394 | libusb_set_debug(NULL, 3); |
Arthur Heymans | f3ce951 | 2018-07-17 02:16:44 +0200 | [diff] [blame] | 395 | #else |
| 396 | libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO); |
| 397 | #endif |
Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 398 | |
| 399 | uint16_t vid = devs_digilent_spi[0].vendor_id; |
| 400 | uint16_t pid = devs_digilent_spi[0].device_id; |
| 401 | handle = libusb_open_device_with_vid_pid(NULL, vid, pid); |
| 402 | if (handle == NULL) { |
| 403 | msg_perr("%s: couldn't open device %04x:%04x.\n", __func__, vid, pid); |
| 404 | return -1; |
| 405 | } |
| 406 | |
| 407 | ret = libusb_claim_interface(handle, 0); |
| 408 | if (ret != 0) { |
| 409 | msg_perr("%s: failed to claim interface 0: '%s'\n", __func__, libusb_error_name(ret)); |
| 410 | goto close_handle; |
| 411 | } |
| 412 | |
| 413 | p = extract_programmer_param("spispeed"); |
| 414 | if (p) { |
| 415 | for (i = 0; spispeeds[i].name; ++i) { |
| 416 | if (!strcasecmp(spispeeds[i].name, p)) { |
| 417 | speed_hz = spispeeds[i].speed; |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | if (!spispeeds[i].name) { |
| 422 | msg_perr("Error: Invalid spispeed value: '%s'.\n", p); |
| 423 | free(p); |
| 424 | goto close_handle; |
| 425 | } |
| 426 | free(p); |
| 427 | } |
| 428 | |
| 429 | p = extract_programmer_param("reset"); |
| 430 | if (p && strlen(p)) |
| 431 | reset_board = (p[0] == '1'); |
| 432 | else |
| 433 | reset_board = default_reset(); |
| 434 | free(p); |
| 435 | |
| 436 | if (reset_board) { |
| 437 | if (gpio_open() != 0) |
| 438 | goto close_handle; |
| 439 | if (gpio_set_dir(1) != 0) |
| 440 | goto close_handle; |
| 441 | if (gpio_set_value(0) != 0) |
| 442 | goto close_handle; |
| 443 | } |
| 444 | |
| 445 | if (spi_open() != 0) |
| 446 | goto close_handle; |
| 447 | if (spi_set_speed(speed_hz) != 0) |
| 448 | goto close_handle; |
| 449 | if (spi_set_mode(0x00) != 0) |
| 450 | goto close_handle; |
| 451 | |
Anastasia Klimchuk | c63d918 | 2021-07-06 16:18:44 +1000 | [diff] [blame] | 452 | return register_spi_master(&spi_master_digilent_spi, NULL); |
Lubomir Rintel | b2154e8 | 2018-01-14 17:35:33 +0100 | [diff] [blame] | 453 | |
| 454 | close_handle: |
| 455 | libusb_close(handle); |
| 456 | handle = NULL; |
| 457 | return -1; |
| 458 | } |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 459 | |
| 460 | const struct programmer_entry programmer_digilent_spi = { |
| 461 | .name = "digilent_spi", |
| 462 | .type = USB, |
| 463 | .devs.dev = devs_digilent_spi, |
| 464 | .init = digilent_spi_init, |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 465 | }; |