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 
do_command(uint8_t * req,int req_len,uint8_t * res,int res_len)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 
gpio_open(void)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 
gpio_set_dir(uint8_t direction)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 
gpio_set_value(uint8_t value)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 
spi_open(void)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 
spi_set_speed(uint32_t speed)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 
spi_set_mode(uint8_t mode)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 
spi_set_cs(uint8_t cs)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 
spi_start_io(uint8_t read_follows,uint32_t write_len)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 
spi_tx_end(uint8_t read_follows,uint32_t tx_len)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 
digilent_spi_send_command(struct flashctx * flash,unsigned int writecnt,unsigned int readcnt,const unsigned char * writearr,unsigned char * readarr)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 	.features	= SPI_MASTER_4BA,
317 	.max_data_read	= 252,
318 	.max_data_write	= 252,
319 	.command	= digilent_spi_send_command,
320 	.multicommand	= default_spi_send_multicommand,
321 	.read		= default_spi_read,
322 	.write_256	= default_spi_write_256,
323 	.write_aai	= default_spi_write_aai,
324 };
325 
326 
digilent_spi_shutdown(void * data)327 static int digilent_spi_shutdown(void *data)
328 {
329 	if (reset_board)
330 		gpio_set_dir(0);
331 
332 	libusb_close(handle);
333 	handle = NULL;
334 
335 	return 0;
336 }
337 
default_reset(void)338 static bool default_reset(void)
339 {
340 	char board[17];
341 
342 	libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR,
343 	                        GET_BOARD_TYPE, 0, 0,
344 	                        (unsigned char *)board, sizeof(board) - 1, USB_TIMEOUT);
345 	board[sizeof(board) -1] = '\0';
346 
347 	if (strcmp(board, "iCE40") == 0)
348 		return true;
349 
350 	msg_pwarn("%s: unknown board '%s' not attempting a reset. "
351 	          "Override with '-p digilent_spi=reset=1'.\n", __func__, board);
352 	return false;
353 }
354 
355 struct digilent_spispeeds {
356         const char *const name;
357         const int speed;
358 };
359 
360 static const struct digilent_spispeeds spispeeds[] = {
361 	{ "4M",		4000000 },
362 	{ "2M",		2000000 },
363 	{ "1M",		1000000 },
364 	{ "500k",	500000 },
365 	{ "250k",	250000 },
366 	{ "125k",	125000 },
367 	{ "62.5k",	62500 },
368 	{ NULL,		0 },
369 };
370 
digilent_spi_init(void)371 int digilent_spi_init(void)
372 {
373 	char *p;
374 	uint32_t speed_hz = spispeeds[0].speed;
375 	int i;
376 
377 	if (handle != NULL) {
378 		msg_cerr("%s: handle already set! Please report a bug at flashrom@flashrom.org\n", __func__);
379 		return -1;
380 	}
381 
382 	int32_t ret = libusb_init(NULL);
383 	if (ret < 0) {
384 		msg_perr("%s: couldn't initialize libusb!\n", __func__);
385 		return -1;
386 	}
387 
388 #if LIBUSB_API_VERSION < 0x01000106
389 	libusb_set_debug(NULL, 3);
390 #else
391 	libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO);
392 #endif
393 
394 	uint16_t vid = devs_digilent_spi[0].vendor_id;
395 	uint16_t pid = devs_digilent_spi[0].device_id;
396 	handle = libusb_open_device_with_vid_pid(NULL, vid, pid);
397 	if (handle == NULL) {
398 		msg_perr("%s: couldn't open device %04x:%04x.\n", __func__, vid, pid);
399 		return -1;
400 	}
401 
402 	ret = libusb_claim_interface(handle, 0);
403 	if (ret != 0) {
404 		msg_perr("%s: failed to claim interface 0: '%s'\n", __func__, libusb_error_name(ret));
405 		goto close_handle;
406 	}
407 
408 	p = extract_programmer_param("spispeed");
409 	if (p) {
410 		for (i = 0; spispeeds[i].name; ++i) {
411 			if (!strcasecmp(spispeeds[i].name, p)) {
412 				speed_hz = spispeeds[i].speed;
413 				break;
414 			}
415 		}
416 		if (!spispeeds[i].name) {
417 			msg_perr("Error: Invalid spispeed value: '%s'.\n", p);
418 			free(p);
419 			goto close_handle;
420 		}
421 		free(p);
422 	}
423 
424 	p = extract_programmer_param("reset");
425 	if (p && strlen(p))
426 		reset_board = (p[0] == '1');
427 	else
428 		reset_board = default_reset();
429 	free(p);
430 
431 	if (reset_board) {
432 		if (gpio_open() != 0)
433 			goto close_handle;
434 		if (gpio_set_dir(1) != 0)
435 			goto close_handle;
436 		if (gpio_set_value(0) != 0)
437 			goto close_handle;
438 	}
439 
440 	if (spi_open() != 0)
441 		goto close_handle;
442 	if (spi_set_speed(speed_hz) != 0)
443 		goto close_handle;
444 	if (spi_set_mode(0x00) != 0)
445 		goto close_handle;
446 
447 	register_shutdown(digilent_spi_shutdown, NULL);
448 	register_spi_master(&spi_master_digilent_spi);
449 
450 	return 0;
451 
452 close_handle:
453 	libusb_close(handle);
454 	handle = NULL;
455 	return -1;
456 }
457