Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [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 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 48 | struct digilent_spi_data { |
| 49 | struct libusb_device_handle *handle; |
| 50 | bool reset_board; |
| 51 | }; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 52 | |
| 53 | #define DIGILENT_VID 0x1443 |
| 54 | #define DIGILENT_JTAG_PID 0x0007 |
| 55 | |
| 56 | const struct dev_entry devs_digilent_spi[] = { |
| 57 | { DIGILENT_VID, DIGILENT_JTAG_PID, OK, "Digilent", "Development board JTAG" }, |
| 58 | { 0 }, |
| 59 | }; |
| 60 | |
| 61 | /* Control endpoint commands. */ |
| 62 | enum { |
| 63 | GET_BOARD_TYPE = 0xe2, |
| 64 | GET_BOARD_SERIAL = 0xe4, |
| 65 | }; |
| 66 | |
| 67 | /* Command bulk endpoint command groups. */ |
| 68 | enum { |
| 69 | CMD_GPIO = 0x03, |
| 70 | CMD_BOARD = 0x04, |
| 71 | CMD_SPI = 0x06, |
| 72 | }; |
| 73 | |
| 74 | /* GPIO subcommands. */ |
| 75 | enum { |
| 76 | CMD_GPIO_OPEN = 0x00, |
| 77 | CMD_GPIO_CLOSE = 0x01, |
| 78 | CMD_GPIO_SET_DIR = 0x04, |
| 79 | CMD_GPIO_SET_VAL = 0x06, |
| 80 | }; |
| 81 | |
| 82 | /* Board subcommands. */ |
| 83 | enum { |
| 84 | CMD_BOARD_OPEN = 0x00, |
| 85 | CMD_BOARD_CLOSE = 0x01, |
| 86 | CMD_BOARD_SET_REG = 0x04, |
| 87 | CMD_BOARD_GET_REG = 0x05, |
| 88 | CMD_BOARD_PL_STAT = 0x85, |
| 89 | }; |
| 90 | |
| 91 | /* SPI subcommands. */ |
| 92 | enum { |
| 93 | CMD_SPI_OPEN = 0x00, |
| 94 | CMD_SPI_CLOSE = 0x01, |
| 95 | CMD_SPI_SET_SPEED = 0x03, |
| 96 | CMD_SPI_SET_MODE = 0x05, |
| 97 | CMD_SPI_SET_CS = 0x06, |
| 98 | CMD_SPI_START_IO = 0x07, |
| 99 | CMD_SPI_TX_END = 0x87, |
| 100 | }; |
| 101 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 102 | static int do_command(uint8_t *req, int req_len, uint8_t *res, int res_len, struct libusb_device_handle *handle) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 103 | { |
| 104 | int tx_len = 0; |
| 105 | int ret; |
| 106 | |
| 107 | req[0] = req_len - 1; |
| 108 | ret = libusb_bulk_transfer(handle, CMD_WRITE_EP, req, req_len, &tx_len, USB_TIMEOUT); |
| 109 | if (ret) { |
| 110 | msg_perr("Failed to issue a command: '%s'\n", libusb_error_name(ret)); |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | if (tx_len != req_len) { |
| 115 | msg_perr("Short write issuing a command\n"); |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | ret = libusb_bulk_transfer(handle, CMD_READ_EP, res, res_len, &tx_len, USB_TIMEOUT); |
| 120 | if (ret) { |
| 121 | msg_perr("Failed to get a response: '%s'\n", libusb_error_name(ret)); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | if (tx_len != res_len) { |
| 126 | msg_perr("Short read getting a response\n"); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | if (res[0] != res_len -1) { |
| 131 | msg_perr("Response indicates incorrect length.\n"); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 138 | static int gpio_open(struct libusb_device_handle *handle) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 139 | { |
| 140 | uint8_t req[] = { 0x00, CMD_GPIO, CMD_GPIO_OPEN, 0x00 }; |
| 141 | uint8_t res[2]; |
| 142 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 143 | return do_command(req, sizeof(req), res, sizeof(res), handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 144 | } |
| 145 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 146 | static int gpio_set_dir(uint8_t direction, struct libusb_device_handle *handle) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 147 | { |
| 148 | uint8_t req[] = { 0x00, CMD_GPIO, CMD_GPIO_SET_DIR, 0x00, |
| 149 | direction, 0x00, 0x00, 0x00 }; |
| 150 | uint8_t res[6]; |
| 151 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 152 | return do_command(req, sizeof(req), res, sizeof(res), handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 153 | } |
| 154 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 155 | static int gpio_set_value(uint8_t value, struct libusb_device_handle *handle) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 156 | { |
| 157 | uint8_t req[] = { 0x00, CMD_GPIO, CMD_GPIO_SET_VAL, 0x00, |
| 158 | value, 0x00, 0x00, 0x00 }; |
| 159 | uint8_t res[2]; |
| 160 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 161 | return do_command(req, sizeof(req), res, sizeof(res), handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 162 | } |
| 163 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 164 | static int spi_open(struct libusb_device_handle *handle) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 165 | { |
| 166 | uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_OPEN, 0x00 }; |
| 167 | uint8_t res[2]; |
| 168 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 169 | return do_command(req, sizeof(req), res, sizeof(res), handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 170 | } |
| 171 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 172 | static int spi_set_speed(uint32_t speed, struct libusb_device_handle *handle) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 173 | { |
| 174 | uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_SET_SPEED, 0x00, |
| 175 | (speed) & 0xff, |
| 176 | (speed >> 8) & 0xff, |
| 177 | (speed >> 16) & 0xff, |
| 178 | (speed >> 24) & 0xff }; |
| 179 | uint8_t res[6]; |
| 180 | uint32_t real_speed; |
| 181 | int ret; |
| 182 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 183 | ret = do_command(req, sizeof(req), res, sizeof(res), handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 184 | if (ret) |
| 185 | return ret; |
| 186 | |
| 187 | real_speed = (res[5] << 24) | (res[4] << 16) | (res[3] << 8) | res[2]; |
| 188 | if (real_speed != speed) |
| 189 | msg_pwarn("SPI speed set to %d instead of %d\n", real_speed, speed); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 194 | static int spi_set_mode(uint8_t mode, struct libusb_device_handle *handle) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 195 | { |
| 196 | uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_SET_MODE, 0x00, mode }; |
| 197 | uint8_t res[2]; |
| 198 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 199 | return do_command(req, sizeof(req), res, sizeof(res), handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 200 | } |
| 201 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 202 | static int spi_set_cs(uint8_t cs, struct libusb_device_handle *handle) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 203 | { |
| 204 | uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_SET_CS, 0x00, cs }; |
| 205 | uint8_t res[2]; |
| 206 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 207 | return do_command(req, sizeof(req), res, sizeof(res), handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 208 | } |
| 209 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 210 | static int spi_start_io(uint8_t read_follows, uint32_t write_len, struct libusb_device_handle *handle) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 211 | { |
| 212 | uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_START_IO, 0x00, |
| 213 | 0x00, 0x00, /* meaning unknown */ |
| 214 | read_follows, |
| 215 | (write_len) & 0xff, |
| 216 | (write_len >> 8) & 0xff, |
| 217 | (write_len >> 16) & 0xff, |
| 218 | (write_len >> 24) & 0xff }; |
| 219 | uint8_t res[2]; |
| 220 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 221 | return do_command(req, sizeof(req), res, sizeof(res), handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 222 | } |
| 223 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 224 | static int spi_tx_end(uint8_t read_follows, uint32_t tx_len, struct libusb_device_handle *handle) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 225 | { |
| 226 | uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_TX_END, 0x00 }; |
| 227 | uint8_t res[read_follows ? 10 : 6]; |
| 228 | int ret; |
| 229 | uint32_t count; |
| 230 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 231 | ret = do_command(req, sizeof(req), res, sizeof(res), handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 232 | if (ret != 0) |
| 233 | return ret; |
| 234 | |
| 235 | if ((res[1] & 0x80) == 0) { |
| 236 | msg_perr("%s: response missing a write count\n", __func__); |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | count = res[2] | (res[3] << 8) | (res[4] << 16) | res[5] << 24; |
| 241 | if (count != tx_len) { |
| 242 | msg_perr("%s: wrote only %d bytes instead of %d\n", __func__, count, tx_len); |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | if (read_follows) { |
| 247 | if ((res[1] & 0x40) == 0) { |
| 248 | msg_perr("%s: response missing a read count\n", __func__); |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | count = res[6] | (res[7] << 8) | (res[8] << 16) | res[9] << 24; |
| 253 | if (count != tx_len) { |
| 254 | msg_perr("%s: read only %d bytes instead of %d\n", __func__, count, tx_len); |
| 255 | return -1; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
Edward O'Callaghan | cf8d8f6 | 2020-10-13 13:35:53 +1100 | [diff] [blame] | 262 | static int digilent_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 263 | const unsigned char *writearr, unsigned char *readarr) |
| 264 | { |
| 265 | int ret; |
| 266 | int len = writecnt + readcnt; |
| 267 | int tx_len = 0; |
| 268 | uint8_t buf[len]; |
| 269 | uint8_t read_follows = readcnt > 0 ? 1 : 0; |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 270 | struct digilent_spi_data *digilent_data = flash->mst->spi.data; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 271 | |
| 272 | memcpy(buf, writearr, writecnt); |
| 273 | memset(buf + writecnt, 0xff, readcnt); |
| 274 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 275 | ret = spi_set_cs(0, digilent_data->handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 276 | if (ret != 0) |
| 277 | return ret; |
| 278 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 279 | ret = spi_start_io(read_follows, writecnt, digilent_data->handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 280 | if (ret != 0) |
| 281 | return ret; |
| 282 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 283 | ret = libusb_bulk_transfer(digilent_data->handle, DATA_WRITE_EP, buf, len, &tx_len, USB_TIMEOUT); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 284 | if (ret != 0) { |
| 285 | msg_perr("%s: failed to write data: '%s'\n", __func__, libusb_error_name(ret)); |
| 286 | return -1; |
| 287 | } |
| 288 | if (tx_len != len) { |
| 289 | msg_perr("%s: short write\n", __func__); |
| 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | if (read_follows) { |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 294 | ret = libusb_bulk_transfer(digilent_data->handle, DATA_READ_EP, buf, len, &tx_len, USB_TIMEOUT); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 295 | if (ret != 0) { |
| 296 | msg_perr("%s: failed to read data: '%s'\n", __func__, libusb_error_name(ret)); |
| 297 | return -1; |
| 298 | } |
| 299 | if (tx_len != len) { |
| 300 | msg_perr("%s: short read\n", __func__); |
| 301 | return -1; |
| 302 | } |
| 303 | } |
| 304 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 305 | ret = spi_tx_end(read_follows, len, digilent_data->handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 306 | if (ret != 0) |
| 307 | return ret; |
| 308 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 309 | ret = spi_set_cs(1, digilent_data->handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 310 | if (ret != 0) |
| 311 | return ret; |
| 312 | |
| 313 | memcpy(readarr, &buf[writecnt], readcnt); |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 318 | static const struct spi_master spi_master_digilent_spi = { |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 319 | .features = SPI_MASTER_4BA, |
| 320 | .max_data_read = 252, |
| 321 | .max_data_write = 252, |
| 322 | .command = digilent_spi_send_command, |
| 323 | .multicommand = default_spi_send_multicommand, |
| 324 | .read = default_spi_read, |
| 325 | .write_256 = default_spi_write_256, |
| 326 | .write_aai = default_spi_write_aai, |
| 327 | }; |
| 328 | |
| 329 | |
| 330 | static int digilent_spi_shutdown(void *data) |
| 331 | { |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 332 | struct digilent_spi_data *digilent_data = data; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 333 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 334 | if (digilent_data->reset_board) |
| 335 | gpio_set_dir(0, digilent_data->handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 336 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 337 | libusb_close(digilent_data->handle); |
| 338 | |
| 339 | free(data); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 343 | static bool default_reset(struct libusb_device_handle *handle) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 344 | { |
| 345 | char board[17]; |
| 346 | |
| 347 | libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR, |
| 348 | GET_BOARD_TYPE, 0, 0, |
| 349 | (unsigned char *)board, sizeof(board) - 1, USB_TIMEOUT); |
| 350 | board[sizeof(board) -1] = '\0'; |
| 351 | |
| 352 | if (strcmp(board, "iCE40") == 0) |
| 353 | return true; |
| 354 | |
| 355 | msg_pwarn("%s: unknown board '%s' not attempting a reset. " |
| 356 | "Override with '-p digilent_spi=reset=1'.\n", __func__, board); |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | struct digilent_spispeeds { |
| 361 | const char *const name; |
| 362 | const int speed; |
| 363 | }; |
| 364 | |
| 365 | static const struct digilent_spispeeds spispeeds[] = { |
| 366 | { "4M", 4000000 }, |
| 367 | { "2M", 2000000 }, |
| 368 | { "1M", 1000000 }, |
| 369 | { "500k", 500000 }, |
| 370 | { "250k", 250000 }, |
| 371 | { "125k", 125000 }, |
| 372 | { "62.5k", 62500 }, |
| 373 | { NULL, 0 }, |
| 374 | }; |
| 375 | |
| 376 | int digilent_spi_init(void) |
| 377 | { |
| 378 | char *p; |
| 379 | uint32_t speed_hz = spispeeds[0].speed; |
| 380 | int i; |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 381 | struct libusb_device_handle *handle = NULL; |
| 382 | bool reset_board; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 383 | |
| 384 | int32_t ret = libusb_init(NULL); |
| 385 | if (ret < 0) { |
| 386 | msg_perr("%s: couldn't initialize libusb!\n", __func__); |
| 387 | return -1; |
| 388 | } |
| 389 | |
| 390 | #if LIBUSB_API_VERSION < 0x01000106 |
| 391 | libusb_set_debug(NULL, 3); |
| 392 | #else |
| 393 | libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO); |
| 394 | #endif |
| 395 | |
| 396 | uint16_t vid = devs_digilent_spi[0].vendor_id; |
| 397 | uint16_t pid = devs_digilent_spi[0].device_id; |
| 398 | handle = libusb_open_device_with_vid_pid(NULL, vid, pid); |
| 399 | if (handle == NULL) { |
| 400 | msg_perr("%s: couldn't open device %04x:%04x.\n", __func__, vid, pid); |
| 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | ret = libusb_claim_interface(handle, 0); |
| 405 | if (ret != 0) { |
| 406 | msg_perr("%s: failed to claim interface 0: '%s'\n", __func__, libusb_error_name(ret)); |
| 407 | goto close_handle; |
| 408 | } |
| 409 | |
| 410 | p = extract_programmer_param("spispeed"); |
| 411 | if (p) { |
| 412 | for (i = 0; spispeeds[i].name; ++i) { |
| 413 | if (!strcasecmp(spispeeds[i].name, p)) { |
| 414 | speed_hz = spispeeds[i].speed; |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | if (!spispeeds[i].name) { |
| 419 | msg_perr("Error: Invalid spispeed value: '%s'.\n", p); |
| 420 | free(p); |
| 421 | goto close_handle; |
| 422 | } |
| 423 | free(p); |
| 424 | } |
| 425 | |
| 426 | p = extract_programmer_param("reset"); |
| 427 | if (p && strlen(p)) |
| 428 | reset_board = (p[0] == '1'); |
| 429 | else |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 430 | reset_board = default_reset(handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 431 | free(p); |
| 432 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 433 | |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 434 | if (reset_board) { |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 435 | if (gpio_open(handle) != 0) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 436 | goto close_handle; |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 437 | if (gpio_set_dir(1, handle) != 0) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 438 | goto close_handle; |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 439 | if (gpio_set_value(0, handle) != 0) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 440 | goto close_handle; |
| 441 | } |
| 442 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 443 | if (spi_open(handle) != 0) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 444 | goto close_handle; |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 445 | if (spi_set_speed(speed_hz, handle) != 0) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 446 | goto close_handle; |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 447 | if (spi_set_mode(0x00, handle) != 0) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 448 | goto close_handle; |
| 449 | |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 450 | struct digilent_spi_data *digilent_data = calloc(1, sizeof(*digilent_data)); |
| 451 | if (!digilent_data) { |
| 452 | msg_perr("Unable to allocate space for SPI master data\n"); |
| 453 | goto close_handle; |
| 454 | } |
| 455 | digilent_data->reset_board = reset_board; |
| 456 | digilent_data->handle = handle; |
Anastasia Klimchuk | 5e0eec3 | 2021-05-10 19:29:00 +1000 | [diff] [blame] | 457 | |
| 458 | register_shutdown(digilent_spi_shutdown, digilent_data); |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 459 | register_spi_master(&spi_master_digilent_spi, digilent_data); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 460 | |
| 461 | return 0; |
| 462 | |
| 463 | close_handle: |
| 464 | libusb_close(handle); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 465 | return -1; |
| 466 | } |