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 | |
| 117 | static 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 | |
| 128 | static 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 | |
| 179 | cleanup_fail: |
| 180 | free(rxtx_buffer); |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | static 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 | |
Nico Huber | 03f3a6d | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 192 | static const struct spi_master spi_master_dirtyjtag_spi = { |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 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, |
Anastasia Klimchuk | c63d918 | 2021-07-06 16:18:44 +1000 | [diff] [blame^] | 201 | .shutdown = dirtyjtag_spi_shutdown, |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | static int dirtyjtag_spi_init(void) |
| 205 | { |
| 206 | struct libusb_device_handle *handle = NULL; |
| 207 | struct dirtyjtag_spi_data *djtag_data = NULL; |
| 208 | |
| 209 | djtag_data = calloc(1, sizeof(struct dirtyjtag_spi_data)); |
| 210 | if (djtag_data == NULL) { |
| 211 | msg_perr("%s: failed to allocate internal driver data structure\n", __func__); |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | int ret = libusb_init(&djtag_data->libusb_ctx); |
| 216 | if (ret < 0) { |
| 217 | msg_perr("%s: couldn't initialize libusb!\n", __func__); |
| 218 | goto cleanup_djtag_struct; |
| 219 | } |
| 220 | |
| 221 | #if LIBUSB_API_VERSION < 0x01000106 |
| 222 | libusb_set_debug(djtag_data->libusb_ctx, 3); |
| 223 | #else |
| 224 | libusb_set_option(djtag_data->libusb_ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO); |
| 225 | #endif |
| 226 | |
| 227 | uint16_t vid = devs_dirtyjtag_spi[0].vendor_id; |
| 228 | uint16_t pid = devs_dirtyjtag_spi[0].device_id; |
| 229 | handle = libusb_open_device_with_vid_pid(djtag_data->libusb_ctx, vid, pid); |
| 230 | if (handle == NULL) { |
| 231 | msg_perr("%s: couldn't open device %04x:%04x.\n", __func__, vid, pid); |
| 232 | goto cleanup_libusb_ctx; |
| 233 | } |
| 234 | |
| 235 | ret = libusb_detach_kernel_driver(handle, 0); |
| 236 | if (ret != 0 && ret != LIBUSB_ERROR_NOT_FOUND) { |
| 237 | msg_pwarn("Cannot detach the existing USB driver. Claiming the interface may fail. %s\n", |
| 238 | libusb_error_name(ret)); |
| 239 | } |
| 240 | |
| 241 | ret = libusb_claim_interface(handle, 0); |
| 242 | if (ret != 0) { |
| 243 | msg_perr("%s: failed to claim interface 0: '%s'\n", __func__, libusb_error_name(ret)); |
| 244 | goto cleanup_libusb_handle; |
| 245 | } |
| 246 | |
| 247 | djtag_data->libusb_handle = handle; |
| 248 | |
| 249 | unsigned long int freq = 100; |
| 250 | char *tmp = extract_programmer_param("spispeed"); |
| 251 | if (tmp) { |
| 252 | char *units = tmp; |
| 253 | |
| 254 | errno = 0; |
| 255 | freq = strtoul(tmp, &units, 0); |
| 256 | if (errno) { |
| 257 | msg_perr("Invalid frequency \"%s\", %s\n", tmp, strerror(errno)); |
| 258 | free(tmp); |
| 259 | goto cleanup_libusb_handle; |
| 260 | } |
| 261 | |
| 262 | if (!strcasecmp(units, "hz")) { |
| 263 | freq /= 1000; |
| 264 | } else if (!strcasecmp(units, "khz")) { |
| 265 | /* Do nothing, already in the right unit */ |
| 266 | } else if (!strcasecmp(units, "mhz")) { |
| 267 | freq *= 1000; |
| 268 | } else { |
| 269 | msg_perr("Invalid unit: %s, use hz, khz or mhz\n", units); |
| 270 | free(tmp); |
| 271 | goto cleanup_libusb_handle; |
| 272 | } |
| 273 | |
| 274 | if (freq > UINT16_MAX) { |
| 275 | msg_perr("%s: Frequency set above DJTAG1 limits (%d kHz)", __func__, UINT16_MAX); |
| 276 | free(tmp); |
| 277 | goto cleanup_libusb_handle; |
| 278 | } |
| 279 | |
| 280 | msg_pinfo("%s: programmer speed set to %lu kHz\n", __func__, freq); |
| 281 | } |
| 282 | free(tmp); |
| 283 | |
| 284 | uint8_t commands[] = { |
| 285 | CMD_SETSIG, /* Set TDI/TCK to low, SRST/TRST/TMS to high */ |
| 286 | SIG_TDI | SIG_TMS | SIG_TCK | SIG_SRST | SIG_TRST, |
| 287 | SIG_SRST | SIG_TRST | SIG_TMS, |
| 288 | |
| 289 | CMD_FREQ, /* Set frequency */ |
| 290 | (freq >> 8) & 0xff, |
| 291 | freq & 0xff, |
| 292 | |
| 293 | CMD_STOP, |
| 294 | }; |
| 295 | ret = dirtyjtag_send(djtag_data, commands, sizeof(commands)); |
| 296 | if (ret != 0) { |
| 297 | msg_perr("%s: failed to configure DirtyJTAG into initialized state\n", __func__); |
| 298 | goto cleanup_libusb_handle; |
| 299 | } |
| 300 | |
Nico Huber | 03f3a6d | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 301 | return register_spi_master(&spi_master_dirtyjtag_spi, djtag_data); |
Jean THOMAS | e28d8e4 | 2022-10-11 17:54:30 +0200 | [diff] [blame] | 302 | |
| 303 | cleanup_libusb_handle: |
| 304 | libusb_attach_kernel_driver(handle, 0); |
| 305 | libusb_close(handle); |
| 306 | |
| 307 | cleanup_libusb_ctx: |
| 308 | libusb_exit(djtag_data->libusb_ctx); |
| 309 | |
| 310 | cleanup_djtag_struct: |
| 311 | free(djtag_data); |
| 312 | return -1; |
| 313 | } |
| 314 | |
| 315 | const struct programmer_entry programmer_dirtyjtag_spi = { |
| 316 | .name = "dirtyjtag_spi", |
| 317 | .type = USB, |
| 318 | .devs.dev = devs_dirtyjtag_spi, |
| 319 | .init = dirtyjtag_spi_init, |
| 320 | .map_flash_region = fallback_map, |
| 321 | .unmap_flash_region = fallback_unmap, |
| 322 | .delay = internal_delay, |
| 323 | }; |