xref: /aosp_15_r20/external/flashrom/digilent_spi.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2018 Lubomir Rintel <[email protected]>
5  *
6  * Based on ft2232_spi.c:
7  *
8  * Copyright (C) 2011 asbokid <[email protected]>
9  * Copyright (C) 2014 Pluto Yang <[email protected]>
10  * Copyright (C) 2015-2016 Stefan Tauner
11  * Copyright (C) 2015 Urja Rannikko <[email protected]>
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 <[email protected]>.
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 struct digilent_spi_data {
49 	struct libusb_device_handle *handle;
50 	bool reset_board;
51 };
52 
53 #define DIGILENT_VID		0x1443
54 #define DIGILENT_JTAG_PID	0x0007
55 
56 static 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 
do_command(uint8_t * req,int req_len,uint8_t * res,int res_len,struct libusb_device_handle * handle)102 static int do_command(uint8_t *req, int req_len, uint8_t *res, int res_len, struct libusb_device_handle *handle)
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 
gpio_open(struct libusb_device_handle * handle)138 static int gpio_open(struct libusb_device_handle *handle)
139 {
140 	uint8_t req[] = { 0x00, CMD_GPIO, CMD_GPIO_OPEN, 0x00 };
141 	uint8_t res[2];
142 
143 	return do_command(req, sizeof(req), res, sizeof(res), handle);
144 }
145 
gpio_set_dir(uint8_t direction,struct libusb_device_handle * handle)146 static int gpio_set_dir(uint8_t direction, struct libusb_device_handle *handle)
147 {
148 	uint8_t req[] = { 0x00, CMD_GPIO, CMD_GPIO_SET_DIR, 0x00,
149 			  direction, 0x00, 0x00, 0x00 };
150 	uint8_t res[6];
151 
152 	return do_command(req, sizeof(req), res, sizeof(res), handle);
153 }
154 
gpio_set_value(uint8_t value,struct libusb_device_handle * handle)155 static int gpio_set_value(uint8_t value, struct libusb_device_handle *handle)
156 {
157 	uint8_t req[] = { 0x00, CMD_GPIO, CMD_GPIO_SET_VAL, 0x00,
158 			  value, 0x00, 0x00, 0x00 };
159 	uint8_t res[2];
160 
161 	return do_command(req, sizeof(req), res, sizeof(res), handle);
162 }
163 
spi_open(struct libusb_device_handle * handle)164 static int spi_open(struct libusb_device_handle *handle)
165 {
166 	uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_OPEN, 0x00 };
167 	uint8_t res[2];
168 
169 	return do_command(req, sizeof(req), res, sizeof(res), handle);
170 }
171 
spi_set_speed(uint32_t speed,struct libusb_device_handle * handle)172 static int spi_set_speed(uint32_t speed, struct libusb_device_handle *handle)
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 
183 	ret = do_command(req, sizeof(req), res, sizeof(res), handle);
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 
spi_set_mode(uint8_t mode,struct libusb_device_handle * handle)194 static int spi_set_mode(uint8_t mode, struct libusb_device_handle *handle)
195 {
196 	uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_SET_MODE, 0x00, mode };
197 	uint8_t res[2];
198 
199 	return do_command(req, sizeof(req), res, sizeof(res), handle);
200 }
201 
spi_set_cs(uint8_t cs,struct libusb_device_handle * handle)202 static int spi_set_cs(uint8_t cs, struct libusb_device_handle *handle)
203 {
204 	uint8_t req[] = { 0x00, CMD_SPI, CMD_SPI_SET_CS, 0x00, cs };
205 	uint8_t res[2];
206 
207 	return do_command(req, sizeof(req), res, sizeof(res), handle);
208 }
209 
spi_start_io(uint8_t read_follows,uint32_t write_len,struct libusb_device_handle * handle)210 static int spi_start_io(uint8_t read_follows, uint32_t write_len, struct libusb_device_handle *handle)
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 
221 	return do_command(req, sizeof(req), res, sizeof(res), handle);
222 }
223 
spi_tx_end(uint8_t read_follows,uint32_t tx_len,struct libusb_device_handle * handle)224 static int spi_tx_end(uint8_t read_follows, uint32_t tx_len, struct libusb_device_handle *handle)
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 
231 	ret = do_command(req, sizeof(req), res, sizeof(res), handle);
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 
digilent_spi_send_command(const struct flashctx * flash,unsigned int writecnt,unsigned int readcnt,const unsigned char * writearr,unsigned char * readarr)262 static int digilent_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
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;
270 	struct digilent_spi_data *digilent_data = flash->mst->spi.data;
271 
272 	memcpy(buf, writearr, writecnt);
273 	memset(buf + writecnt, 0xff, readcnt);
274 
275 	ret = spi_set_cs(0, digilent_data->handle);
276 	if (ret != 0)
277 		return ret;
278 
279 	ret = spi_start_io(read_follows, writecnt, digilent_data->handle);
280 	if (ret != 0)
281 		return ret;
282 
283 	ret = libusb_bulk_transfer(digilent_data->handle, DATA_WRITE_EP, buf, len, &tx_len, USB_TIMEOUT);
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) {
294 		ret = libusb_bulk_transfer(digilent_data->handle, DATA_READ_EP, buf, len, &tx_len, USB_TIMEOUT);
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 
305 	ret = spi_tx_end(read_follows, len, digilent_data->handle);
306 	if (ret != 0)
307 		return ret;
308 
309 	ret = spi_set_cs(1, digilent_data->handle);
310 	if (ret != 0)
311 		return ret;
312 
313 	memcpy(readarr, &buf[writecnt], readcnt);
314 
315 	return 0;
316 }
317 
digilent_spi_shutdown(void * data)318 static int digilent_spi_shutdown(void *data)
319 {
320 	struct digilent_spi_data *digilent_data = data;
321 
322 	if (digilent_data->reset_board)
323 		gpio_set_dir(0, digilent_data->handle);
324 
325 	libusb_close(digilent_data->handle);
326 
327 	free(data);
328 	return 0;
329 }
330 
331 static const struct spi_master spi_master_digilent_spi = {
332 	.features	= SPI_MASTER_4BA,
333 	.max_data_read	= 252,
334 	.max_data_write	= 252,
335 	.command	= digilent_spi_send_command,
336 	.read		= default_spi_read,
337 	.write_256	= default_spi_write_256,
338 	.shutdown	= digilent_spi_shutdown,
339 };
340 
default_reset(struct libusb_device_handle * handle)341 static bool default_reset(struct libusb_device_handle *handle)
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 
digilent_spi_init(const struct programmer_cfg * cfg)374 static int digilent_spi_init(const struct programmer_cfg *cfg)
375 {
376 	char *param_str;
377 	uint32_t speed_hz = spispeeds[0].speed;
378 	int i;
379 	struct libusb_device_handle *handle = NULL;
380 	bool reset_board;
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 	param_str = extract_programmer_param_str(cfg, "spispeed");
409 	if (param_str) {
410 		for (i = 0; spispeeds[i].name; ++i) {
411 			if (!strcasecmp(spispeeds[i].name, param_str)) {
412 				speed_hz = spispeeds[i].speed;
413 				break;
414 			}
415 		}
416 		if (!spispeeds[i].name) {
417 			msg_perr("Error: Invalid spispeed value: '%s'.\n", param_str);
418 			free(param_str);
419 			goto close_handle;
420 		}
421 		free(param_str);
422 	}
423 
424 	param_str = extract_programmer_param_str(cfg, "reset");
425 	if (param_str && strlen(param_str))
426 		reset_board = (param_str[0] == '1');
427 	else
428 		reset_board = default_reset(handle);
429 	free(param_str);
430 
431 
432 	if (reset_board) {
433 		if (gpio_open(handle) != 0)
434 			goto close_handle;
435 		if (gpio_set_dir(1, handle) != 0)
436 			goto close_handle;
437 		if (gpio_set_value(0, handle) != 0)
438 			goto close_handle;
439 	}
440 
441 	if (spi_open(handle) != 0)
442 		goto close_handle;
443 	if (spi_set_speed(speed_hz, handle) != 0)
444 		goto close_handle;
445 	if (spi_set_mode(0x00, handle) != 0)
446 		goto close_handle;
447 
448 	struct digilent_spi_data *digilent_data = calloc(1, sizeof(*digilent_data));
449 	if (!digilent_data) {
450 		msg_perr("Unable to allocate space for SPI master data\n");
451 		goto close_handle;
452 	}
453 	digilent_data->reset_board = reset_board;
454 	digilent_data->handle = handle;
455 
456 	return register_spi_master(&spi_master_digilent_spi, digilent_data);
457 
458 close_handle:
459 	libusb_close(handle);
460 	return -1;
461 }
462 
463 const struct programmer_entry programmer_digilent_spi = {
464 	.name			= "digilent_spi",
465 	.type			= USB,
466 	.devs.dev		= devs_digilent_spi,
467 	.init			= digilent_spi_init,
468 };
469