blob: ee788901d38f9bfb2c6abe15a3e8e581b40fd767 [file] [log] [blame]
Lubomir Rintelb2154e82018-01-14 17:35:33 +01001/*
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 Klimchuk2348b182021-05-10 19:29:00 +100048struct digilent_spi_data {
49 struct libusb_device_handle *handle;
50 bool reset_board;
51};
Lubomir Rintelb2154e82018-01-14 17:35:33 +010052
53#define DIGILENT_VID 0x1443
54#define DIGILENT_JTAG_PID 0x0007
55
56const 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. */
62enum {
63 GET_BOARD_TYPE = 0xe2,
64 GET_BOARD_SERIAL = 0xe4,
65};
66
67/* Command bulk endpoint command groups. */
68enum {
69 CMD_GPIO = 0x03,
70 CMD_BOARD = 0x04,
71 CMD_SPI = 0x06,
72};
73
74/* GPIO subcommands. */
75enum {
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. */
83enum {
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. */
92enum {
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 Klimchuk2348b182021-05-10 19:29:00 +1000102static int do_command(uint8_t *req, int req_len, uint8_t *res, int res_len, struct libusb_device_handle *handle)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100103{
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 Klimchuk2348b182021-05-10 19:29:00 +1000138static int gpio_open(struct libusb_device_handle *handle)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100139{
140 uint8_t req[] = { 0x00, CMD_GPIO, CMD_GPIO_OPEN, 0x00 };
141 uint8_t res[2];
142
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000143 return do_command(req, sizeof(req), res, sizeof(res), handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100144}
145
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000146static int gpio_set_dir(uint8_t direction, struct libusb_device_handle *handle)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100147{
148 uint8_t req[] = { 0x00, CMD_GPIO, CMD_GPIO_SET_DIR, 0x00,
149 direction, 0x00, 0x00, 0x00 };
150 uint8_t res[6];
151
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000152 return do_command(req, sizeof(req), res, sizeof(res), handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100153}
154
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000155static int gpio_set_value(uint8_t value, struct libusb_device_handle *handle)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100156{
157 uint8_t req[] = { 0x00, CMD_GPIO, CMD_GPIO_SET_VAL, 0x00,
158 value, 0x00, 0x00, 0x00 };
159 uint8_t res[2];
160
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000161 return do_command(req, sizeof(req), res, sizeof(res), handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100162}
163
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000164static int spi_open(struct libusb_device_handle *handle)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100165{
166 uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_OPEN, 0x00 };
167 uint8_t res[2];
168
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000169 return do_command(req, sizeof(req), res, sizeof(res), handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100170}
171
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000172static int spi_set_speed(uint32_t speed, struct libusb_device_handle *handle)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100173{
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 Klimchuk2348b182021-05-10 19:29:00 +1000183 ret = do_command(req, sizeof(req), res, sizeof(res), handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100184 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 Klimchuk2348b182021-05-10 19:29:00 +1000194static int spi_set_mode(uint8_t mode, struct libusb_device_handle *handle)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100195{
196 uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_SET_MODE, 0x00, mode };
197 uint8_t res[2];
198
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000199 return do_command(req, sizeof(req), res, sizeof(res), handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100200}
201
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000202static int spi_set_cs(uint8_t cs, struct libusb_device_handle *handle)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100203{
204 uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_SET_CS, 0x00, cs };
205 uint8_t res[2];
206
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000207 return do_command(req, sizeof(req), res, sizeof(res), handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100208}
209
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000210static int spi_start_io(uint8_t read_follows, uint32_t write_len, struct libusb_device_handle *handle)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100211{
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 Klimchuk2348b182021-05-10 19:29:00 +1000221 return do_command(req, sizeof(req), res, sizeof(res), handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100222}
223
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000224static int spi_tx_end(uint8_t read_follows, uint32_t tx_len, struct libusb_device_handle *handle)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100225{
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 Klimchuk2348b182021-05-10 19:29:00 +1000231 ret = do_command(req, sizeof(req), res, sizeof(res), handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100232 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'Callaghane4ddc362020-04-12 17:27:53 +1000262static int digilent_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100263 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 Klimchuk2348b182021-05-10 19:29:00 +1000270 struct digilent_spi_data *digilent_data = flash->mst->spi.data;
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100271
272 memcpy(buf, writearr, writecnt);
273 memset(buf + writecnt, 0xff, readcnt);
274
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000275 ret = spi_set_cs(0, digilent_data->handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100276 if (ret != 0)
277 return ret;
278
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000279 ret = spi_start_io(read_follows, writecnt, digilent_data->handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100280 if (ret != 0)
281 return ret;
282
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000283 ret = libusb_bulk_transfer(digilent_data->handle, DATA_WRITE_EP, buf, len, &tx_len, USB_TIMEOUT);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100284 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 Klimchuk2348b182021-05-10 19:29:00 +1000294 ret = libusb_bulk_transfer(digilent_data->handle, DATA_READ_EP, buf, len, &tx_len, USB_TIMEOUT);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100295 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 Klimchuk2348b182021-05-10 19:29:00 +1000305 ret = spi_tx_end(read_follows, len, digilent_data->handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100306 if (ret != 0)
307 return ret;
308
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000309 ret = spi_set_cs(1, digilent_data->handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100310 if (ret != 0)
311 return ret;
312
313 memcpy(readarr, &buf[writecnt], readcnt);
314
315 return 0;
316}
317
Nico Huber90739d12021-05-11 17:53:34 +0200318static const struct spi_master spi_master_digilent_spi = {
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100319 .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
330static int digilent_spi_shutdown(void *data)
331{
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000332 struct digilent_spi_data *digilent_data = data;
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100333
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000334 if (digilent_data->reset_board)
335 gpio_set_dir(0, digilent_data->handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100336
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000337 libusb_close(digilent_data->handle);
338
339 free(data);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100340 return 0;
341}
342
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000343static bool default_reset(struct libusb_device_handle *handle)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100344{
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
360struct digilent_spispeeds {
361 const char *const name;
362 const int speed;
363};
364
365static 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
376int digilent_spi_init(void)
377{
378 char *p;
379 uint32_t speed_hz = spispeeds[0].speed;
380 int i;
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000381 struct libusb_device_handle *handle = NULL;
382 bool reset_board;
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100383
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
Arthur Heymansf3ce9512018-07-17 02:16:44 +0200390#if LIBUSB_API_VERSION < 0x01000106
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100391 libusb_set_debug(NULL, 3);
Arthur Heymansf3ce9512018-07-17 02:16:44 +0200392#else
393 libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO);
394#endif
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100395
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 Klimchuk2348b182021-05-10 19:29:00 +1000430 reset_board = default_reset(handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100431 free(p);
432
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000433
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100434 if (reset_board) {
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000435 if (gpio_open(handle) != 0)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100436 goto close_handle;
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000437 if (gpio_set_dir(1, handle) != 0)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100438 goto close_handle;
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000439 if (gpio_set_value(0, handle) != 0)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100440 goto close_handle;
441 }
442
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000443 if (spi_open(handle) != 0)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100444 goto close_handle;
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000445 if (spi_set_speed(speed_hz, handle) != 0)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100446 goto close_handle;
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000447 if (spi_set_mode(0x00, handle) != 0)
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100448 goto close_handle;
449
Anastasia Klimchuk2348b182021-05-10 19:29:00 +1000450 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 Klimchuk2348b182021-05-10 19:29:00 +1000457
458 register_shutdown(digilent_spi_shutdown, digilent_data);
Nico Huber90739d12021-05-11 17:53:34 +0200459 register_spi_master(&spi_master_digilent_spi, digilent_data);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100460
461 return 0;
462
463close_handle:
464 libusb_close(handle);
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100465 return -1;
466}