Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 41 | struct dirtyjtag_spi_data { |
| 42 | struct libusb_context *libusb_ctx; |
| 43 | struct libusb_device_handle *libusb_handle; |
| 44 | }; |
| 45 | |
| 46 | static const struct dev_entry devs_dirtyjtag_spi[] = { |
| 47 | { 0x1209, 0xc0ca, OK, "DirtyJTAG", "JTAG probe" }, |
| 48 | { 0 }, |
| 49 | }; |
| 50 | |
| 51 | static const char dirtyjtag_write_endpoint = 0x01; |
| 52 | static const char dirtyjtag_read_endpoint = 0x82; |
| 53 | static const int dirtyjtag_timeout = 100 * 10; /* 100 ms */ |
| 54 | |
| 55 | enum 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 | |
| 65 | enum 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 | |
| 74 | static 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 | |
| 95 | static 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 | |
Nico Huber | abdb07f | 2023-02-26 17:09:34 +0000 | [diff] [blame^] | 117 | static char *dirtyjtag_info(struct dirtyjtag_spi_data *djtag_data) |
| 118 | { |
| 119 | uint8_t buffer[64] = { |
| 120 | CMD_INFO, |
| 121 | }; |
| 122 | |
| 123 | if (dirtyjtag_send(djtag_data, buffer, 1)) |
| 124 | return NULL; |
| 125 | |
| 126 | const int transferred = dirtyjtag_receive(djtag_data, buffer, sizeof(buffer), -1); |
| 127 | if (transferred < 1) |
| 128 | return NULL; |
| 129 | |
| 130 | return strndup((char *)buffer, transferred); |
| 131 | } |
| 132 | |
| 133 | static unsigned int dirtyjtag_version(const char *info) |
| 134 | { |
| 135 | if (!strncmp(info, "DJTAG1\n", 7)) |
| 136 | return 1; |
| 137 | if (!strncmp(info, "DJTAG2\n", 7)) |
| 138 | return 2; |
| 139 | return 0; |
| 140 | } |
| 141 | |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 142 | static int dirtyjtag_spi_shutdown(void *data) |
| 143 | { |
| 144 | struct dirtyjtag_spi_data *djtag_data = (struct dirtyjtag_spi_data*)data; |
| 145 | libusb_release_interface(djtag_data->libusb_handle, 0); |
| 146 | libusb_attach_kernel_driver(djtag_data->libusb_handle, 0); |
| 147 | libusb_close(djtag_data->libusb_handle); |
| 148 | libusb_exit(djtag_data->libusb_ctx); |
| 149 | free(data); |
| 150 | return 0; |
| 151 | } |
| 152 | |
Nico Huber | abdb07f | 2023-02-26 17:09:34 +0000 | [diff] [blame^] | 153 | static int dirtyjtag_djtag1_spi_send_command(const struct flashctx *flash, |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 154 | unsigned int writecnt, unsigned int readcnt, |
| 155 | const unsigned char *writearr, unsigned char *readarr) |
| 156 | { |
Nico Huber | abdb07f | 2023-02-26 17:09:34 +0000 | [diff] [blame^] | 157 | struct dirtyjtag_spi_data *context = flash->mst->spi.data; |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 158 | const size_t max_xfer_size = 30; // max transfer size in DJTAG1 |
| 159 | size_t len = writecnt + readcnt; |
| 160 | size_t num_xfer = (len + max_xfer_size - 1 ) / max_xfer_size; // ceil(len/max_xfer_size) |
| 161 | size_t i; |
| 162 | |
| 163 | uint8_t *rxtx_buffer = malloc(max_xfer_size * num_xfer); |
| 164 | if (!rxtx_buffer) { |
| 165 | msg_perr("%s: Failed rxtx_buffer allocation\n", __func__); |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | memcpy(rxtx_buffer, writearr, writecnt); |
| 170 | for (i = 0; i < num_xfer; i++) { |
| 171 | const size_t xfer_offset = i * max_xfer_size; |
| 172 | size_t txn_size = max_xfer_size; |
| 173 | if (i == num_xfer-1 && len % max_xfer_size != 0) |
| 174 | txn_size = len % max_xfer_size; |
| 175 | |
| 176 | uint8_t transfer_buffer[32] = { |
| 177 | CMD_XFER, |
| 178 | txn_size * 8 |
| 179 | }; |
| 180 | memcpy(transfer_buffer + 2, rxtx_buffer + xfer_offset, txn_size); |
| 181 | |
| 182 | if (dirtyjtag_send(context, transfer_buffer, sizeof(transfer_buffer))) |
| 183 | goto cleanup_fail; |
| 184 | |
| 185 | if (dirtyjtag_receive(context, transfer_buffer, sizeof(transfer_buffer), 32) < 0) |
| 186 | goto cleanup_fail; |
| 187 | |
| 188 | memcpy(rxtx_buffer + xfer_offset, transfer_buffer, txn_size); |
| 189 | } |
| 190 | memcpy(readarr, rxtx_buffer + writecnt, readcnt); |
| 191 | |
| 192 | free(rxtx_buffer); |
| 193 | |
| 194 | uint8_t tms_reset_buffer[] = { |
| 195 | CMD_SETSIG, |
| 196 | SIG_TMS, |
| 197 | SIG_TMS, |
| 198 | |
| 199 | CMD_STOP, |
| 200 | }; |
| 201 | dirtyjtag_send(context, tms_reset_buffer, sizeof(tms_reset_buffer)); |
| 202 | |
| 203 | return 0; |
| 204 | |
| 205 | cleanup_fail: |
| 206 | free(rxtx_buffer); |
| 207 | return -1; |
| 208 | } |
| 209 | |
Nico Huber | 03f3a6d | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 210 | static const struct spi_master spi_master_dirtyjtag_spi = { |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 211 | .features = SPI_MASTER_4BA, |
| 212 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 213 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 214 | .multicommand = default_spi_send_multicommand, |
| 215 | .read = default_spi_read, |
| 216 | .write_256 = default_spi_write_256, |
Anastasia Klimchuk | c63d918 | 2021-07-06 16:18:44 +1000 | [diff] [blame] | 217 | .shutdown = dirtyjtag_spi_shutdown, |
Aarya Chaumal | 0cea753 | 2022-07-04 18:21:50 +0530 | [diff] [blame] | 218 | .probe_opcode = default_spi_probe_opcode, |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | static int dirtyjtag_spi_init(void) |
| 222 | { |
Nico Huber | abdb07f | 2023-02-26 17:09:34 +0000 | [diff] [blame^] | 223 | struct spi_master dirtyjtag_spi = spi_master_dirtyjtag_spi; |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 224 | struct libusb_device_handle *handle = NULL; |
| 225 | struct dirtyjtag_spi_data *djtag_data = NULL; |
| 226 | |
| 227 | djtag_data = calloc(1, sizeof(struct dirtyjtag_spi_data)); |
| 228 | if (djtag_data == NULL) { |
| 229 | msg_perr("%s: failed to allocate internal driver data structure\n", __func__); |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | int ret = libusb_init(&djtag_data->libusb_ctx); |
| 234 | if (ret < 0) { |
| 235 | msg_perr("%s: couldn't initialize libusb!\n", __func__); |
| 236 | goto cleanup_djtag_struct; |
| 237 | } |
| 238 | |
| 239 | #if LIBUSB_API_VERSION < 0x01000106 |
| 240 | libusb_set_debug(djtag_data->libusb_ctx, 3); |
| 241 | #else |
| 242 | libusb_set_option(djtag_data->libusb_ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO); |
| 243 | #endif |
| 244 | |
| 245 | uint16_t vid = devs_dirtyjtag_spi[0].vendor_id; |
| 246 | uint16_t pid = devs_dirtyjtag_spi[0].device_id; |
| 247 | handle = libusb_open_device_with_vid_pid(djtag_data->libusb_ctx, vid, pid); |
| 248 | if (handle == NULL) { |
| 249 | msg_perr("%s: couldn't open device %04x:%04x.\n", __func__, vid, pid); |
| 250 | goto cleanup_libusb_ctx; |
| 251 | } |
| 252 | |
| 253 | ret = libusb_detach_kernel_driver(handle, 0); |
| 254 | if (ret != 0 && ret != LIBUSB_ERROR_NOT_FOUND) { |
| 255 | msg_pwarn("Cannot detach the existing USB driver. Claiming the interface may fail. %s\n", |
| 256 | libusb_error_name(ret)); |
| 257 | } |
| 258 | |
| 259 | ret = libusb_claim_interface(handle, 0); |
| 260 | if (ret != 0) { |
| 261 | msg_perr("%s: failed to claim interface 0: '%s'\n", __func__, libusb_error_name(ret)); |
| 262 | goto cleanup_libusb_handle; |
| 263 | } |
| 264 | |
| 265 | djtag_data->libusb_handle = handle; |
| 266 | |
| 267 | unsigned long int freq = 100; |
| 268 | char *tmp = extract_programmer_param("spispeed"); |
| 269 | if (tmp) { |
| 270 | char *units = tmp; |
| 271 | |
| 272 | errno = 0; |
| 273 | freq = strtoul(tmp, &units, 0); |
| 274 | if (errno) { |
| 275 | msg_perr("Invalid frequency \"%s\", %s\n", tmp, strerror(errno)); |
| 276 | free(tmp); |
| 277 | goto cleanup_libusb_handle; |
| 278 | } |
| 279 | |
| 280 | if (!strcasecmp(units, "hz")) { |
| 281 | freq /= 1000; |
| 282 | } else if (!strcasecmp(units, "khz")) { |
| 283 | /* Do nothing, already in the right unit */ |
| 284 | } else if (!strcasecmp(units, "mhz")) { |
| 285 | freq *= 1000; |
| 286 | } else { |
| 287 | msg_perr("Invalid unit: %s, use hz, khz or mhz\n", units); |
| 288 | free(tmp); |
| 289 | goto cleanup_libusb_handle; |
| 290 | } |
| 291 | |
| 292 | if (freq > UINT16_MAX) { |
| 293 | msg_perr("%s: Frequency set above DJTAG1 limits (%d kHz)", __func__, UINT16_MAX); |
| 294 | free(tmp); |
| 295 | goto cleanup_libusb_handle; |
| 296 | } |
| 297 | |
| 298 | msg_pinfo("%s: programmer speed set to %lu kHz\n", __func__, freq); |
| 299 | } |
| 300 | free(tmp); |
| 301 | |
Nico Huber | abdb07f | 2023-02-26 17:09:34 +0000 | [diff] [blame^] | 302 | char *const info = dirtyjtag_info(djtag_data); |
| 303 | if (!info) { |
| 304 | msg_perr("Failed to read DirtyJTAG Info.\n"); |
| 305 | goto cleanup_libusb_handle; |
| 306 | } |
| 307 | |
| 308 | msg_pinfo("DirtyJTAG Info: %s\n", info); |
| 309 | const unsigned int djtag_version = dirtyjtag_version(info); |
| 310 | switch (djtag_version) { |
| 311 | default: |
| 312 | msg_pwarn("Warning: Unknown DJTAG version %u. Assuming DJTAG1 compatibility.\n", |
| 313 | djtag_version); |
| 314 | /* fall-through */ |
| 315 | case 1: |
| 316 | dirtyjtag_spi.command = dirtyjtag_djtag1_spi_send_command; |
| 317 | break; |
| 318 | } |
| 319 | free(info); |
| 320 | |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 321 | uint8_t commands[] = { |
| 322 | CMD_SETSIG, /* Set TDI/TCK to low, SRST/TRST/TMS to high */ |
| 323 | SIG_TDI | SIG_TMS | SIG_TCK | SIG_SRST | SIG_TRST, |
| 324 | SIG_SRST | SIG_TRST | SIG_TMS, |
| 325 | |
| 326 | CMD_FREQ, /* Set frequency */ |
| 327 | (freq >> 8) & 0xff, |
| 328 | freq & 0xff, |
| 329 | |
| 330 | CMD_STOP, |
| 331 | }; |
| 332 | ret = dirtyjtag_send(djtag_data, commands, sizeof(commands)); |
| 333 | if (ret != 0) { |
| 334 | msg_perr("%s: failed to configure DirtyJTAG into initialized state\n", __func__); |
| 335 | goto cleanup_libusb_handle; |
| 336 | } |
| 337 | |
Nico Huber | abdb07f | 2023-02-26 17:09:34 +0000 | [diff] [blame^] | 338 | return register_spi_master(&dirtyjtag_spi, djtag_data); |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 339 | |
| 340 | cleanup_libusb_handle: |
| 341 | libusb_attach_kernel_driver(handle, 0); |
| 342 | libusb_close(handle); |
| 343 | |
| 344 | cleanup_libusb_ctx: |
| 345 | libusb_exit(djtag_data->libusb_ctx); |
| 346 | |
| 347 | cleanup_djtag_struct: |
| 348 | free(djtag_data); |
| 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | const struct programmer_entry programmer_dirtyjtag_spi = { |
| 353 | .name = "dirtyjtag_spi", |
| 354 | .type = USB, |
| 355 | .devs.dev = devs_dirtyjtag_spi, |
| 356 | .init = dirtyjtag_spi_init, |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 357 | }; |