blob: 5b089523456925349fc538b7b3c1840ac2004a43 [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
65enum dirtyjtag_signal_identifier {
66 SIG_TCK = 1 << 1,
67 SIG_TDI = 1 << 2,
68 SIG_TDO = 1 << 3,
69 SIG_TMS = 1 << 4,
70 SIG_TRST = 1 << 5,
71 SIG_SRST = 1 << 6
72};
73
74static int dirtyjtag_send(struct dirtyjtag_spi_data *djtag_data, uint8_t *data, size_t len)
75{
76 int transferred;
77 int ret = libusb_bulk_transfer(djtag_data->libusb_handle,
78 dirtyjtag_write_endpoint,
79 data,
80 len,
81 &transferred,
82 dirtyjtag_timeout);
83 if (ret != 0) {
84 msg_perr("%s: failed to send query command\n", __func__);
85 return -1;
86 }
87 if (transferred != (int)len) {
88 msg_perr("%s: failed to send whole packet\n", __func__);
89 return -1;
90 }
91
92 return 0;
93}
94
95static int dirtyjtag_receive(struct dirtyjtag_spi_data *djtag_data, uint8_t *data, size_t buffer_len, int expected)
96{
97 int transferred;
98 int ret = libusb_bulk_transfer(djtag_data->libusb_handle,
99 dirtyjtag_read_endpoint,
100 data,
101 buffer_len,
102 &transferred,
103 dirtyjtag_timeout);
104 if (ret != 0) {
105 msg_perr("%s: Failed to read SPI commands\n", __func__);
106 return -1;
107 }
108
109 if (expected != -1 && transferred != expected) {
110 msg_perr("%s: failed to meet expected\n", __func__);
111 return -1;
112 }
113
114 return transferred;
115}
116
117static int dirtyjtag_spi_shutdown(void *data)
118{
119 struct dirtyjtag_spi_data *djtag_data = (struct dirtyjtag_spi_data*)data;
120 libusb_release_interface(djtag_data->libusb_handle, 0);
121 libusb_attach_kernel_driver(djtag_data->libusb_handle, 0);
122 libusb_close(djtag_data->libusb_handle);
123 libusb_exit(djtag_data->libusb_ctx);
124 free(data);
125 return 0;
126}
127
128static int dirtyjtag_djtag1_spi_send_command(struct dirtyjtag_spi_data *context,
129 unsigned int writecnt, unsigned int readcnt,
130 const unsigned char *writearr, unsigned char *readarr)
131{
132 const size_t max_xfer_size = 30; // max transfer size in DJTAG1
133 size_t len = writecnt + readcnt;
134 size_t num_xfer = (len + max_xfer_size - 1 ) / max_xfer_size; // ceil(len/max_xfer_size)
135 size_t i;
136
137 uint8_t *rxtx_buffer = malloc(max_xfer_size * num_xfer);
138 if (!rxtx_buffer) {
139 msg_perr("%s: Failed rxtx_buffer allocation\n", __func__);
140 return -1;
141 }
142
143 memcpy(rxtx_buffer, writearr, writecnt);
144 for (i = 0; i < num_xfer; i++) {
145 const size_t xfer_offset = i * max_xfer_size;
146 size_t txn_size = max_xfer_size;
147 if (i == num_xfer-1 && len % max_xfer_size != 0)
148 txn_size = len % max_xfer_size;
149
150 uint8_t transfer_buffer[32] = {
151 CMD_XFER,
152 txn_size * 8
153 };
154 memcpy(transfer_buffer + 2, rxtx_buffer + xfer_offset, txn_size);
155
156 if (dirtyjtag_send(context, transfer_buffer, sizeof(transfer_buffer)))
157 goto cleanup_fail;
158
159 if (dirtyjtag_receive(context, transfer_buffer, sizeof(transfer_buffer), 32) < 0)
160 goto cleanup_fail;
161
162 memcpy(rxtx_buffer + xfer_offset, transfer_buffer, txn_size);
163 }
164 memcpy(readarr, rxtx_buffer + writecnt, readcnt);
165
166 free(rxtx_buffer);
167
168 uint8_t tms_reset_buffer[] = {
169 CMD_SETSIG,
170 SIG_TMS,
171 SIG_TMS,
172
173 CMD_STOP,
174 };
175 dirtyjtag_send(context, tms_reset_buffer, sizeof(tms_reset_buffer));
176
177 return 0;
178
179cleanup_fail:
180 free(rxtx_buffer);
181 return -1;
182}
183
184static int dirtyjtag_spi_spi_send_command(const struct flashctx *flash,
185 unsigned int writecnt, unsigned int readcnt,
186 const unsigned char *writearr, unsigned char *readarr)
187{
188 struct dirtyjtag_spi_data *djtag_data = flash->mst->spi.data;
189 return dirtyjtag_djtag1_spi_send_command(djtag_data, writecnt, readcnt, writearr, readarr);
190}
191
192static struct spi_master spi_master_dirtyjtag_spi = {
193 .features = SPI_MASTER_4BA,
194 .max_data_read = MAX_DATA_READ_UNLIMITED,
195 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
196 .command = dirtyjtag_spi_spi_send_command,
197 .multicommand = default_spi_send_multicommand,
198 .read = default_spi_read,
199 .write_256 = default_spi_write_256,
200 .write_aai = default_spi_write_aai,
201};
202
203static int dirtyjtag_spi_init(void)
204{
205 struct libusb_device_handle *handle = NULL;
206 struct dirtyjtag_spi_data *djtag_data = NULL;
207
208 djtag_data = calloc(1, sizeof(struct dirtyjtag_spi_data));
209 if (djtag_data == NULL) {
210 msg_perr("%s: failed to allocate internal driver data structure\n", __func__);
211 return -1;
212 }
213
214 int ret = libusb_init(&djtag_data->libusb_ctx);
215 if (ret < 0) {
216 msg_perr("%s: couldn't initialize libusb!\n", __func__);
217 goto cleanup_djtag_struct;
218 }
219
220#if LIBUSB_API_VERSION < 0x01000106
221 libusb_set_debug(djtag_data->libusb_ctx, 3);
222#else
223 libusb_set_option(djtag_data->libusb_ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO);
224#endif
225
226 uint16_t vid = devs_dirtyjtag_spi[0].vendor_id;
227 uint16_t pid = devs_dirtyjtag_spi[0].device_id;
228 handle = libusb_open_device_with_vid_pid(djtag_data->libusb_ctx, vid, pid);
229 if (handle == NULL) {
230 msg_perr("%s: couldn't open device %04x:%04x.\n", __func__, vid, pid);
231 goto cleanup_libusb_ctx;
232 }
233
234 ret = libusb_detach_kernel_driver(handle, 0);
235 if (ret != 0 && ret != LIBUSB_ERROR_NOT_FOUND) {
236 msg_pwarn("Cannot detach the existing USB driver. Claiming the interface may fail. %s\n",
237 libusb_error_name(ret));
238 }
239
240 ret = libusb_claim_interface(handle, 0);
241 if (ret != 0) {
242 msg_perr("%s: failed to claim interface 0: '%s'\n", __func__, libusb_error_name(ret));
243 goto cleanup_libusb_handle;
244 }
245
246 djtag_data->libusb_handle = handle;
247
248 unsigned long int freq = 100;
249 char *tmp = extract_programmer_param("spispeed");
250 if (tmp) {
251 char *units = tmp;
252
253 errno = 0;
254 freq = strtoul(tmp, &units, 0);
255 if (errno) {
256 msg_perr("Invalid frequency \"%s\", %s\n", tmp, strerror(errno));
257 free(tmp);
258 goto cleanup_libusb_handle;
259 }
260
261 if (!strcasecmp(units, "hz")) {
262 freq /= 1000;
263 } else if (!strcasecmp(units, "khz")) {
264 /* Do nothing, already in the right unit */
265 } else if (!strcasecmp(units, "mhz")) {
266 freq *= 1000;
267 } else {
268 msg_perr("Invalid unit: %s, use hz, khz or mhz\n", units);
269 free(tmp);
270 goto cleanup_libusb_handle;
271 }
272
273 if (freq > UINT16_MAX) {
274 msg_perr("%s: Frequency set above DJTAG1 limits (%d kHz)", __func__, UINT16_MAX);
275 free(tmp);
276 goto cleanup_libusb_handle;
277 }
278
279 msg_pinfo("%s: programmer speed set to %lu kHz\n", __func__, freq);
280 }
281 free(tmp);
282
283 uint8_t commands[] = {
284 CMD_SETSIG, /* Set TDI/TCK to low, SRST/TRST/TMS to high */
285 SIG_TDI | SIG_TMS | SIG_TCK | SIG_SRST | SIG_TRST,
286 SIG_SRST | SIG_TRST | SIG_TMS,
287
288 CMD_FREQ, /* Set frequency */
289 (freq >> 8) & 0xff,
290 freq & 0xff,
291
292 CMD_STOP,
293 };
294 ret = dirtyjtag_send(djtag_data, commands, sizeof(commands));
295 if (ret != 0) {
296 msg_perr("%s: failed to configure DirtyJTAG into initialized state\n", __func__);
297 goto cleanup_libusb_handle;
298 }
299
300 if (register_shutdown(dirtyjtag_spi_shutdown, djtag_data))
301 goto cleanup_libusb_handle;
302
303 spi_master_dirtyjtag_spi.data = djtag_data;
304 return register_spi_master(&spi_master_dirtyjtag_spi);
305
306cleanup_libusb_handle:
307 libusb_attach_kernel_driver(handle, 0);
308 libusb_close(handle);
309
310cleanup_libusb_ctx:
311 libusb_exit(djtag_data->libusb_ctx);
312
313cleanup_djtag_struct:
314 free(djtag_data);
315 return -1;
316}
317
318const struct programmer_entry programmer_dirtyjtag_spi = {
319 .name = "dirtyjtag_spi",
320 .type = USB,
321 .devs.dev = devs_dirtyjtag_spi,
322 .init = dirtyjtag_spi_init,
323 .map_flash_region = fallback_map,
324 .unmap_flash_region = fallback_unmap,
325 .delay = internal_delay,
326};