blob: e648cfac2fd6cc068cce1fc576a8d693787c9f38 [file] [log] [blame]
Jean THOMASe28d8e42022-10-11 17:54:30 +02001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2021-2022 Jean THOMAS <virgule@jeanthomas.me>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17/*
18 * Driver for the DirtyJTAG project.
19 * See https://github.com/jeanthom/dirtyjtag for more info.
20 *
21 * SPI-JTAG Pin Mapping
22 * |=========|==========|
23 * | SPI pin | JTAG pin |
24 * |=========|==========|
25 * | #CS | TMS |
26 * | #WP | SRST |
27 * | #HOLD | TRST |
28 * | MISO | TDO |
29 * | MOSI | TDI |
30 * | CLK | TCK |
31 * |=========|==========|
32 */
33
34#include <assert.h>
35#include <stdlib.h>
36#include <string.h>
37#include <errno.h>
38#include <libusb.h>
39#include "programmer.h"
40
41struct dirtyjtag_spi_data {
42 struct libusb_context *libusb_ctx;
43 struct libusb_device_handle *libusb_handle;
44};
45
46static const struct dev_entry devs_dirtyjtag_spi[] = {
47 { 0x1209, 0xc0ca, OK, "DirtyJTAG", "JTAG probe" },
48 { 0 },
49};
50
51static const char dirtyjtag_write_endpoint = 0x01;
52static const char dirtyjtag_read_endpoint = 0x82;
53static const int dirtyjtag_timeout = 100 * 10; /* 100 ms */
54
55enum dirtyjtag_command_identifier {
56 CMD_STOP = 0x00,
57 CMD_INFO = 0x01,
58 CMD_FREQ = 0x02,
59 CMD_XFER = 0x03,
60 CMD_SETSIG = 0x04,
61 CMD_GETSIG = 0x05,
62 CMD_CLK = 0x06
63};
64
Nico Huber8fbd36f2023-02-26 20:57:32 +000065enum dirtyjtag_command_modifier {
66 /* CMD_XFER */
67 NO_READ = 0x80,
68 EXTEND_LENGTH = 0x40,
69 /* CMD_CLK */
70 READOUT = 0x80,
71};
72
Jean THOMASe28d8e42022-10-11 17:54:30 +020073enum dirtyjtag_signal_identifier {
74 SIG_TCK = 1 << 1,
75 SIG_TDI = 1 << 2,
76 SIG_TDO = 1 << 3,
77 SIG_TMS = 1 << 4,
78 SIG_TRST = 1 << 5,
79 SIG_SRST = 1 << 6
80};
81
82static int dirtyjtag_send(struct dirtyjtag_spi_data *djtag_data, uint8_t *data, size_t len)
83{
84 int transferred;
85 int ret = libusb_bulk_transfer(djtag_data->libusb_handle,
86 dirtyjtag_write_endpoint,
87 data,
88 len,
89 &transferred,
90 dirtyjtag_timeout);
91 if (ret != 0) {
92 msg_perr("%s: failed to send query command\n", __func__);
93 return -1;
94 }
95 if (transferred != (int)len) {
96 msg_perr("%s: failed to send whole packet\n", __func__);
97 return -1;
98 }
99
100 return 0;
101}
102
103static int dirtyjtag_receive(struct dirtyjtag_spi_data *djtag_data, uint8_t *data, size_t buffer_len, int expected)
104{
105 int transferred;
106 int ret = libusb_bulk_transfer(djtag_data->libusb_handle,
107 dirtyjtag_read_endpoint,
108 data,
109 buffer_len,
110 &transferred,
111 dirtyjtag_timeout);
112 if (ret != 0) {
113 msg_perr("%s: Failed to read SPI commands\n", __func__);
114 return -1;
115 }
116
117 if (expected != -1 && transferred != expected) {
118 msg_perr("%s: failed to meet expected\n", __func__);
119 return -1;
120 }
121
122 return transferred;
123}
124
Nico Huberabdb07f2023-02-26 17:09:34 +0000125static char *dirtyjtag_info(struct dirtyjtag_spi_data *djtag_data)
126{
127 uint8_t buffer[64] = {
128 CMD_INFO,
129 };
130
131 if (dirtyjtag_send(djtag_data, buffer, 1))
132 return NULL;
133
134 const int transferred = dirtyjtag_receive(djtag_data, buffer, sizeof(buffer), -1);
135 if (transferred < 1)
136 return NULL;
137
138 return strndup((char *)buffer, transferred);
139}
140
141static unsigned int dirtyjtag_version(const char *info)
142{
143 if (!strncmp(info, "DJTAG1\n", 7))
144 return 1;
145 if (!strncmp(info, "DJTAG2\n", 7))
146 return 2;
147 return 0;
148}
149
Jean THOMASe28d8e42022-10-11 17:54:30 +0200150static int dirtyjtag_spi_shutdown(void *data)
151{
152 struct dirtyjtag_spi_data *djtag_data = (struct dirtyjtag_spi_data*)data;
153 libusb_release_interface(djtag_data->libusb_handle, 0);
154 libusb_attach_kernel_driver(djtag_data->libusb_handle, 0);
155 libusb_close(djtag_data->libusb_handle);
156 libusb_exit(djtag_data->libusb_ctx);
157 free(data);
158 return 0;
159}
160
Nico Huber8fbd36f2023-02-26 20:57:32 +0000161static int dirtyjtag_reset_tms(struct dirtyjtag_spi_data *context)
162{
163 uint8_t tms_reset_buffer[] = {
164 CMD_SETSIG,
165 SIG_TMS,
166 SIG_TMS,
167
168 CMD_STOP,
169 };
170 return dirtyjtag_send(context, tms_reset_buffer, sizeof(tms_reset_buffer));
171}
172
Nico Huberabdb07f2023-02-26 17:09:34 +0000173static int dirtyjtag_djtag1_spi_send_command(const struct flashctx *flash,
Jean THOMASe28d8e42022-10-11 17:54:30 +0200174 unsigned int writecnt, unsigned int readcnt,
175 const unsigned char *writearr, unsigned char *readarr)
176{
Nico Huber9a11cbf2023-01-13 01:19:07 +0100177 struct dirtyjtag_spi_data *context = flash->mst.spi->data;
Jean THOMASe28d8e42022-10-11 17:54:30 +0200178 const size_t max_xfer_size = 30; // max transfer size in DJTAG1
179 size_t len = writecnt + readcnt;
180 size_t num_xfer = (len + max_xfer_size - 1 ) / max_xfer_size; // ceil(len/max_xfer_size)
181 size_t i;
182
183 uint8_t *rxtx_buffer = malloc(max_xfer_size * num_xfer);
184 if (!rxtx_buffer) {
185 msg_perr("%s: Failed rxtx_buffer allocation\n", __func__);
186 return -1;
187 }
188
189 memcpy(rxtx_buffer, writearr, writecnt);
190 for (i = 0; i < num_xfer; i++) {
191 const size_t xfer_offset = i * max_xfer_size;
192 size_t txn_size = max_xfer_size;
193 if (i == num_xfer-1 && len % max_xfer_size != 0)
194 txn_size = len % max_xfer_size;
195
196 uint8_t transfer_buffer[32] = {
197 CMD_XFER,
198 txn_size * 8
199 };
200 memcpy(transfer_buffer + 2, rxtx_buffer + xfer_offset, txn_size);
201
202 if (dirtyjtag_send(context, transfer_buffer, sizeof(transfer_buffer)))
203 goto cleanup_fail;
204
205 if (dirtyjtag_receive(context, transfer_buffer, sizeof(transfer_buffer), 32) < 0)
206 goto cleanup_fail;
207
208 memcpy(rxtx_buffer + xfer_offset, transfer_buffer, txn_size);
209 }
210 memcpy(readarr, rxtx_buffer + writecnt, readcnt);
211
212 free(rxtx_buffer);
213
Nico Huber8fbd36f2023-02-26 20:57:32 +0000214 dirtyjtag_reset_tms(context);
Jean THOMASe28d8e42022-10-11 17:54:30 +0200215
216 return 0;
217
218cleanup_fail:
219 free(rxtx_buffer);
220 return -1;
221}
222
Nico Huber8fbd36f2023-02-26 20:57:32 +0000223static int dirtyjtag_djtag2_spi_send_command(const struct flashctx *flash,
224 unsigned int writecnt, unsigned int readcnt,
225 const unsigned char *writearr, unsigned char *readarr)
226{
Nico Huber9a11cbf2023-01-13 01:19:07 +0100227 struct dirtyjtag_spi_data *const context = flash->mst.spi->data;
Nico Huber8fbd36f2023-02-26 20:57:32 +0000228 const size_t max_xfer_size = 62; /* max transfer size in DJTAG2 */
229 uint8_t transfer_buffer[2 + max_xfer_size]; /* 1B command + 1B len + payload */
230 size_t i;
231
232 i = 0;
233 while (i < writecnt) {
234 const size_t txn_size = MIN(max_xfer_size, writecnt - i);
235
236 transfer_buffer[0] = CMD_XFER | NO_READ;
237 if (txn_size * 8 >= 256)
238 transfer_buffer[0] |= EXTEND_LENGTH;
239 transfer_buffer[1] = (txn_size * 8) % 256;
240 memcpy(transfer_buffer + 2, writearr + i, txn_size);
241
242 if (dirtyjtag_send(context, transfer_buffer, 2 + txn_size))
243 return -1;
244
245 i += txn_size;
246 }
247
248 i = 0;
249 while (i < readcnt) {
250 const size_t rxn_size = MIN(max_xfer_size, readcnt - i);
251
252 transfer_buffer[0] = CMD_XFER;
253 if (rxn_size * 8 >= 256)
254 transfer_buffer[0] |= EXTEND_LENGTH;
255 transfer_buffer[1] = (rxn_size * 8) % 256;
256
257 if (dirtyjtag_send(context, transfer_buffer, 2 + rxn_size))
258 return -1;
259
260 if (dirtyjtag_receive(context, readarr + i, rxn_size, rxn_size) < 0)
261 return -1;
262
263 i += rxn_size;
264 }
265
266 dirtyjtag_reset_tms(context);
267
268 return 0;
269}
270
Nico Huber03f3a6d2021-05-11 17:53:34 +0200271static const struct spi_master spi_master_dirtyjtag_spi = {
Jean THOMASe28d8e42022-10-11 17:54:30 +0200272 .features = SPI_MASTER_4BA,
273 .max_data_read = MAX_DATA_READ_UNLIMITED,
274 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
Jean THOMASe28d8e42022-10-11 17:54:30 +0200275 .multicommand = default_spi_send_multicommand,
276 .read = default_spi_read,
277 .write_256 = default_spi_write_256,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +1000278 .shutdown = dirtyjtag_spi_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +0530279 .probe_opcode = default_spi_probe_opcode,
Jean THOMASe28d8e42022-10-11 17:54:30 +0200280};
281
Nico Hubere3a26882023-01-11 21:45:51 +0100282static int dirtyjtag_spi_init(struct flashprog_programmer *const prog)
Jean THOMASe28d8e42022-10-11 17:54:30 +0200283{
Nico Huberabdb07f2023-02-26 17:09:34 +0000284 struct spi_master dirtyjtag_spi = spi_master_dirtyjtag_spi;
Jean THOMASe28d8e42022-10-11 17:54:30 +0200285 struct libusb_device_handle *handle = NULL;
286 struct dirtyjtag_spi_data *djtag_data = NULL;
287
288 djtag_data = calloc(1, sizeof(struct dirtyjtag_spi_data));
289 if (djtag_data == NULL) {
290 msg_perr("%s: failed to allocate internal driver data structure\n", __func__);
291 return -1;
292 }
293
294 int ret = libusb_init(&djtag_data->libusb_ctx);
295 if (ret < 0) {
296 msg_perr("%s: couldn't initialize libusb!\n", __func__);
297 goto cleanup_djtag_struct;
298 }
299
300#if LIBUSB_API_VERSION < 0x01000106
301 libusb_set_debug(djtag_data->libusb_ctx, 3);
302#else
303 libusb_set_option(djtag_data->libusb_ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO);
304#endif
305
306 uint16_t vid = devs_dirtyjtag_spi[0].vendor_id;
307 uint16_t pid = devs_dirtyjtag_spi[0].device_id;
308 handle = libusb_open_device_with_vid_pid(djtag_data->libusb_ctx, vid, pid);
309 if (handle == NULL) {
310 msg_perr("%s: couldn't open device %04x:%04x.\n", __func__, vid, pid);
311 goto cleanup_libusb_ctx;
312 }
313
314 ret = libusb_detach_kernel_driver(handle, 0);
315 if (ret != 0 && ret != LIBUSB_ERROR_NOT_FOUND) {
316 msg_pwarn("Cannot detach the existing USB driver. Claiming the interface may fail. %s\n",
317 libusb_error_name(ret));
318 }
319
320 ret = libusb_claim_interface(handle, 0);
321 if (ret != 0) {
322 msg_perr("%s: failed to claim interface 0: '%s'\n", __func__, libusb_error_name(ret));
323 goto cleanup_libusb_handle;
324 }
325
326 djtag_data->libusb_handle = handle;
327
328 unsigned long int freq = 100;
329 char *tmp = extract_programmer_param("spispeed");
330 if (tmp) {
331 char *units = tmp;
332
333 errno = 0;
334 freq = strtoul(tmp, &units, 0);
335 if (errno) {
336 msg_perr("Invalid frequency \"%s\", %s\n", tmp, strerror(errno));
337 free(tmp);
338 goto cleanup_libusb_handle;
339 }
340
341 if (!strcasecmp(units, "hz")) {
342 freq /= 1000;
343 } else if (!strcasecmp(units, "khz")) {
344 /* Do nothing, already in the right unit */
345 } else if (!strcasecmp(units, "mhz")) {
346 freq *= 1000;
347 } else {
348 msg_perr("Invalid unit: %s, use hz, khz or mhz\n", units);
349 free(tmp);
350 goto cleanup_libusb_handle;
351 }
352
353 if (freq > UINT16_MAX) {
Nico Huber8fbd36f2023-02-26 20:57:32 +0000354 msg_perr("%s: Frequency set above DJTAG1/2 limits (%d kHz)\n", __func__, UINT16_MAX);
Jean THOMASe28d8e42022-10-11 17:54:30 +0200355 free(tmp);
356 goto cleanup_libusb_handle;
357 }
358
359 msg_pinfo("%s: programmer speed set to %lu kHz\n", __func__, freq);
360 }
361 free(tmp);
362
Nico Huberabdb07f2023-02-26 17:09:34 +0000363 char *const info = dirtyjtag_info(djtag_data);
364 if (!info) {
365 msg_perr("Failed to read DirtyJTAG Info.\n");
366 goto cleanup_libusb_handle;
367 }
368
369 msg_pinfo("DirtyJTAG Info: %s\n", info);
370 const unsigned int djtag_version = dirtyjtag_version(info);
371 switch (djtag_version) {
372 default:
Nico Huber8fbd36f2023-02-26 20:57:32 +0000373 msg_pwarn("Warning: Unknown DJTAG version %u. Assuming DJTAG2 compatibility.\n",
Nico Huberabdb07f2023-02-26 17:09:34 +0000374 djtag_version);
375 /* fall-through */
Nico Huber8fbd36f2023-02-26 20:57:32 +0000376 case 2:
377 dirtyjtag_spi.command = dirtyjtag_djtag2_spi_send_command;
378 break;
Nico Huberabdb07f2023-02-26 17:09:34 +0000379 case 1:
380 dirtyjtag_spi.command = dirtyjtag_djtag1_spi_send_command;
381 break;
382 }
383 free(info);
384
Jean THOMASe28d8e42022-10-11 17:54:30 +0200385 uint8_t commands[] = {
386 CMD_SETSIG, /* Set TDI/TCK to low, SRST/TRST/TMS to high */
387 SIG_TDI | SIG_TMS | SIG_TCK | SIG_SRST | SIG_TRST,
388 SIG_SRST | SIG_TRST | SIG_TMS,
389
390 CMD_FREQ, /* Set frequency */
391 (freq >> 8) & 0xff,
392 freq & 0xff,
393
394 CMD_STOP,
395 };
396 ret = dirtyjtag_send(djtag_data, commands, sizeof(commands));
397 if (ret != 0) {
398 msg_perr("%s: failed to configure DirtyJTAG into initialized state\n", __func__);
399 goto cleanup_libusb_handle;
400 }
401
Nico Huber89569d62023-01-12 23:31:40 +0100402 return register_spi_master(&dirtyjtag_spi, 0, djtag_data);
Jean THOMASe28d8e42022-10-11 17:54:30 +0200403
404cleanup_libusb_handle:
405 libusb_attach_kernel_driver(handle, 0);
406 libusb_close(handle);
407
408cleanup_libusb_ctx:
409 libusb_exit(djtag_data->libusb_ctx);
410
411cleanup_djtag_struct:
412 free(djtag_data);
413 return -1;
414}
415
416const struct programmer_entry programmer_dirtyjtag_spi = {
417 .name = "dirtyjtag_spi",
418 .type = USB,
419 .devs.dev = devs_dirtyjtag_spi,
420 .init = dirtyjtag_spi_init,
Jean THOMASe28d8e42022-10-11 17:54:30 +0200421};