| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2016 Marc Schink <flashrom-dev@marcschink.de> |
| 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 J-Link hardware by SEGGER. |
| 19 | * See https://www.segger.com/ for more info. |
| 20 | */ |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdint.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | #include <libjaylink/libjaylink.h> |
| 28 | |
| 29 | #include "flash.h" |
| 30 | #include "programmer.h" |
| 31 | #include "spi.h" |
| 32 | |
| 33 | /* |
| 34 | * Maximum number of bytes that can be transferred at once via the JTAG |
| 35 | * interface, see jaylink_jtag_io(). |
| 36 | */ |
| Marc Schink | a8c0b68 | 2020-08-22 11:29:22 +0200 | [diff] [blame] | 37 | #define JTAG_MAX_TRANSFER_SIZE (32768 / 8) |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Default base frequency in Hz. Used when the base frequency can not be |
| 41 | * retrieved from the device. |
| 42 | */ |
| 43 | #define DEFAULT_FREQ 16000000 |
| 44 | |
| 45 | /* |
| 46 | * Default frequency divider. Used when the frequency divider can not be |
| 47 | * retrieved from the device. |
| 48 | */ |
| 49 | #define DEFAULT_FREQ_DIV 4 |
| 50 | |
| 51 | /* Minimum target voltage required for operation in mV. */ |
| 52 | #define MIN_TARGET_VOLTAGE 1200 |
| 53 | |
| Anastasia Klimchuk | 9e698f9 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 54 | struct jlink_spi_data { |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 55 | struct jaylink_context *ctx; |
| 56 | struct jaylink_device_handle *devh; |
| Anastasia Klimchuk | 9e698f9 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 57 | bool reset_cs; |
| 58 | bool enable_target_power; |
| 59 | }; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 60 | |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 61 | static bool assert_cs(struct jlink_spi_data *jlink_data) |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 62 | { |
| 63 | int ret; |
| 64 | |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 65 | if (jlink_data->reset_cs) { |
| 66 | ret = jaylink_clear_reset(jlink_data->devh); |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 67 | |
| 68 | if (ret != JAYLINK_OK) { |
| 69 | msg_perr("jaylink_clear_reset() failed: %s.\n", jaylink_strerror(ret)); |
| 70 | return false; |
| 71 | } |
| 72 | } else { |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 73 | ret = jaylink_jtag_clear_trst(jlink_data->devh); |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 74 | |
| 75 | if (ret != JAYLINK_OK) { |
| 76 | msg_perr("jaylink_jtag_clear_trst() failed: %s.\n", jaylink_strerror(ret)); |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 84 | static bool deassert_cs(struct jlink_spi_data *jlink_data) |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 85 | { |
| 86 | int ret; |
| 87 | |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 88 | if (jlink_data->reset_cs) { |
| 89 | ret = jaylink_set_reset(jlink_data->devh); |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 90 | |
| 91 | if (ret != JAYLINK_OK) { |
| 92 | msg_perr("jaylink_set_reset() failed: %s.\n", jaylink_strerror(ret)); |
| 93 | return false; |
| 94 | } |
| 95 | } else { |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 96 | ret = jaylink_jtag_set_trst(jlink_data->devh); |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 97 | |
| 98 | if (ret != JAYLINK_OK) { |
| 99 | msg_perr("jaylink_jtag_set_trst() failed: %s.\n", jaylink_strerror(ret)); |
| 100 | return false; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
| Nico Huber | 610c1aa | 2023-02-15 02:56:05 +0100 | [diff] [blame] | 107 | static int jlink_spi_send_command( |
| 108 | const struct spi_master *mst, unsigned int writecnt, unsigned int readcnt, |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 109 | const unsigned char *writearr, unsigned char *readarr) |
| 110 | { |
| 111 | uint32_t length; |
| 112 | uint8_t *buffer; |
| Nico Huber | 610c1aa | 2023-02-15 02:56:05 +0100 | [diff] [blame] | 113 | struct jlink_spi_data *jlink_data = mst->data; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 114 | |
| 115 | length = writecnt + readcnt; |
| 116 | |
| 117 | if (length > JTAG_MAX_TRANSFER_SIZE) |
| 118 | return SPI_INVALID_LENGTH; |
| 119 | |
| 120 | buffer = malloc(length); |
| 121 | |
| 122 | if (!buffer) { |
| 123 | msg_perr("Memory allocation failed.\n"); |
| 124 | return SPI_GENERIC_ERROR; |
| 125 | } |
| 126 | |
| 127 | /* Reverse all bytes because the device transfers data LSB first. */ |
| 128 | reverse_bytes(buffer, writearr, writecnt); |
| 129 | |
| 130 | memset(buffer + writecnt, 0x00, readcnt); |
| 131 | |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 132 | if (!assert_cs(jlink_data)) { |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 133 | free(buffer); |
| 134 | return SPI_PROGRAMMER_ERROR; |
| 135 | } |
| 136 | |
| 137 | int ret; |
| 138 | |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 139 | ret = jaylink_jtag_io(jlink_data->devh, |
| Anastasia Klimchuk | 9e698f9 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 140 | buffer, buffer, buffer, length * 8, JAYLINK_JTAG_VERSION_2); |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 141 | |
| 142 | if (ret != JAYLINK_OK) { |
| Angel Pons | 281ed26 | 2021-04-16 10:55:03 +0200 | [diff] [blame] | 143 | msg_perr("jaylink_jtag_io() failed: %s.\n", jaylink_strerror(ret)); |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 144 | free(buffer); |
| 145 | return SPI_PROGRAMMER_ERROR; |
| 146 | } |
| 147 | |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 148 | if (!deassert_cs(jlink_data)) { |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 149 | free(buffer); |
| 150 | return SPI_PROGRAMMER_ERROR; |
| 151 | } |
| 152 | |
| 153 | /* Reverse all bytes because the device transfers data LSB first. */ |
| 154 | reverse_bytes(readarr, buffer + writecnt, readcnt); |
| 155 | free(buffer); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| Anastasia Klimchuk | c63d918 | 2021-07-06 16:18:44 +1000 | [diff] [blame] | 160 | static int jlink_spi_shutdown(void *data); |
| 161 | |
| Nico Huber | 03f3a6d | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 162 | static const struct spi_master spi_master_jlink_spi = { |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 163 | /* Maximum data read size in one go (excluding opcode+address). */ |
| 164 | .max_data_read = JTAG_MAX_TRANSFER_SIZE - 5, |
| 165 | /* Maximum data write size in one go (excluding opcode+address). */ |
| 166 | .max_data_write = JTAG_MAX_TRANSFER_SIZE - 5, |
| 167 | .command = jlink_spi_send_command, |
| 168 | .multicommand = default_spi_send_multicommand, |
| 169 | .read = default_spi_read, |
| 170 | .write_256 = default_spi_write_256, |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 171 | .features = SPI_MASTER_4BA, |
| Anastasia Klimchuk | c63d918 | 2021-07-06 16:18:44 +1000 | [diff] [blame] | 172 | .shutdown = jlink_spi_shutdown, |
| Aarya Chaumal | 0cea753 | 2022-07-04 18:21:50 +0530 | [diff] [blame] | 173 | .probe_opcode = default_spi_probe_opcode, |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | static int jlink_spi_shutdown(void *data) |
| 177 | { |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 178 | struct jlink_spi_data *jlink_data = data; |
| 179 | if (jlink_data->devh) { |
| 180 | if (jlink_data->enable_target_power) { |
| 181 | int ret = jaylink_set_target_power(jlink_data->devh, false); |
| Marc Schink | 137f02f | 2020-08-23 16:19:44 +0200 | [diff] [blame] | 182 | |
| 183 | if (ret != JAYLINK_OK) { |
| 184 | msg_perr("jaylink_set_target_power() failed: %s.\n", |
| 185 | jaylink_strerror(ret)); |
| 186 | } |
| 187 | } |
| 188 | |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 189 | jaylink_close(jlink_data->devh); |
| Marc Schink | 137f02f | 2020-08-23 16:19:44 +0200 | [diff] [blame] | 190 | } |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 191 | |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 192 | jaylink_exit(jlink_data->ctx); |
| Anastasia Klimchuk | 9e698f9 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 193 | |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 194 | /* jlink_data->ctx, jlink_data->devh are freed by jaylink_close and jaylink_exit */ |
| 195 | free(jlink_data); |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
| Nico Huber | e3a2688 | 2023-01-11 21:45:51 +0100 | [diff] [blame] | 199 | static int jlink_spi_init(struct flashprog_programmer *const prog) |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 200 | { |
| 201 | char *arg; |
| 202 | unsigned long speed = 0; |
| Anastasia Klimchuk | 9e698f9 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 203 | struct jaylink_context *jaylink_ctx = NULL; |
| 204 | struct jaylink_device_handle *jaylink_devh = NULL; |
| 205 | bool reset_cs; |
| 206 | bool enable_target_power; |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 207 | struct jlink_spi_data *jlink_data = NULL; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 208 | |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 209 | arg = extract_programmer_param("spispeed"); |
| 210 | |
| 211 | if (arg) { |
| 212 | char *endptr; |
| 213 | |
| 214 | errno = 0; |
| 215 | speed = strtoul(arg, &endptr, 10); |
| 216 | |
| 217 | if (*endptr != '\0' || errno != 0) { |
| 218 | msg_perr("Invalid SPI speed specified: %s.\n", arg); |
| 219 | free(arg); |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | if (speed < 1) { |
| 224 | msg_perr("SPI speed must be at least 1 kHz.\n"); |
| 225 | free(arg); |
| 226 | return 1; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | free(arg); |
| 231 | |
| 232 | int ret; |
| 233 | bool use_serial_number; |
| 234 | uint32_t serial_number; |
| 235 | |
| 236 | arg = extract_programmer_param("serial"); |
| 237 | |
| 238 | if (arg) { |
| 239 | if (!strlen(arg)) { |
| Angel Pons | 281ed26 | 2021-04-16 10:55:03 +0200 | [diff] [blame] | 240 | msg_perr("Empty serial number specified.\n"); |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 241 | free(arg); |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | ret = jaylink_parse_serial_number(arg, &serial_number); |
| 246 | |
| 247 | if (ret == JAYLINK_ERR) { |
| 248 | msg_perr("Invalid serial number specified: %s.\n", arg); |
| 249 | free(arg); |
| 250 | return 1; |
| 251 | } if (ret != JAYLINK_OK) { |
| 252 | msg_perr("jaylink_parse_serial_number() failed: %s.\n", jaylink_strerror(ret)); |
| 253 | free(arg); |
| 254 | return 1; |
| 255 | } |
| 256 | |
| 257 | use_serial_number = true; |
| 258 | } else { |
| 259 | use_serial_number = false; |
| 260 | } |
| 261 | |
| 262 | free(arg); |
| 263 | |
| 264 | reset_cs = true; |
| 265 | arg = extract_programmer_param("cs"); |
| 266 | |
| 267 | if (arg) { |
| 268 | if (!strcasecmp(arg, "reset")) { |
| 269 | reset_cs = true; |
| 270 | } else if (!strcasecmp(arg, "trst")) { |
| 271 | reset_cs = false; |
| 272 | } else { |
| 273 | msg_perr("Invalid chip select pin specified: '%s'.\n", arg); |
| 274 | free(arg); |
| 275 | return 1; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | free(arg); |
| 280 | |
| 281 | if (reset_cs) |
| 282 | msg_pdbg("Using RESET as chip select signal.\n"); |
| 283 | else |
| 284 | msg_pdbg("Using TRST as chip select signal.\n"); |
| 285 | |
| Marc Schink | 137f02f | 2020-08-23 16:19:44 +0200 | [diff] [blame] | 286 | enable_target_power = false; |
| 287 | arg = extract_programmer_param("power"); |
| 288 | |
| 289 | if (arg) { |
| 290 | if (!strcasecmp(arg, "on")) { |
| 291 | enable_target_power = true; |
| 292 | } else { |
| 293 | msg_perr("Invalid value for 'power' argument: '%s'.\n", arg); |
| 294 | free(arg); |
| 295 | return 1; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | free(arg); |
| 300 | |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 301 | ret = jaylink_init(&jaylink_ctx); |
| 302 | |
| 303 | if (ret != JAYLINK_OK) { |
| 304 | msg_perr("jaylink_init() failed: %s.\n", jaylink_strerror(ret)); |
| 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | ret = jaylink_discovery_scan(jaylink_ctx, 0); |
| 309 | |
| 310 | if (ret != JAYLINK_OK) { |
| Angel Pons | 281ed26 | 2021-04-16 10:55:03 +0200 | [diff] [blame] | 311 | msg_perr("jaylink_discovery_scan() failed: %s.\n", jaylink_strerror(ret)); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 312 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | struct jaylink_device **devs; |
| 316 | |
| 317 | ret = jaylink_get_devices(jaylink_ctx, &devs, NULL); |
| 318 | |
| 319 | if (ret != JAYLINK_OK) { |
| 320 | msg_perr("jaylink_get_devices() failed: %s.\n", jaylink_strerror(ret)); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 321 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | if (!use_serial_number) |
| 325 | msg_pdbg("No device selected, using first device.\n"); |
| 326 | |
| 327 | size_t i; |
| 328 | struct jaylink_device *dev; |
| 329 | bool device_found = false; |
| 330 | |
| 331 | for (i = 0; devs[i]; i++) { |
| 332 | if (use_serial_number) { |
| 333 | uint32_t tmp; |
| 334 | |
| 335 | ret = jaylink_device_get_serial_number(devs[i], &tmp); |
| 336 | |
| 337 | if (ret == JAYLINK_ERR_NOT_AVAILABLE) { |
| 338 | continue; |
| 339 | } else if (ret != JAYLINK_OK) { |
| 340 | msg_pwarn("jaylink_device_get_serial_number() failed: %s.\n", |
| 341 | jaylink_strerror(ret)); |
| 342 | continue; |
| 343 | } |
| 344 | |
| 345 | if (serial_number != tmp) |
| 346 | continue; |
| 347 | } |
| 348 | |
| 349 | ret = jaylink_open(devs[i], &jaylink_devh); |
| 350 | |
| 351 | if (ret == JAYLINK_OK) { |
| 352 | dev = devs[i]; |
| 353 | device_found = true; |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | jaylink_devh = NULL; |
| 358 | } |
| 359 | |
| 360 | jaylink_free_devices(devs, true); |
| 361 | |
| 362 | if (!device_found) { |
| 363 | msg_perr("No J-Link device found.\n"); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 364 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | size_t length; |
| 368 | char *firmware_version; |
| 369 | |
| 370 | ret = jaylink_get_firmware_version(jaylink_devh, &firmware_version, |
| 371 | &length); |
| 372 | |
| 373 | if (ret != JAYLINK_OK) { |
| 374 | msg_perr("jaylink_get_firmware_version() failed: %s.\n", jaylink_strerror(ret)); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 375 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 376 | } else if (length > 0) { |
| 377 | msg_pdbg("Firmware: %s\n", firmware_version); |
| 378 | free(firmware_version); |
| 379 | } |
| 380 | |
| 381 | ret = jaylink_device_get_serial_number(dev, &serial_number); |
| 382 | |
| 383 | if (ret == JAYLINK_OK) { |
| 384 | msg_pdbg("S/N: %" PRIu32 "\n", serial_number); |
| 385 | } else if (ret == JAYLINK_ERR_NOT_AVAILABLE) { |
| 386 | msg_pdbg("S/N: N/A\n"); |
| 387 | } else { |
| 388 | msg_perr("jaylink_device_get_serial_number() failed: %s.\n", jaylink_strerror(ret)); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 389 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| Angel Pons | 7e13456 | 2021-06-07 13:29:13 +0200 | [diff] [blame] | 392 | uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE] = { 0 }; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 393 | |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 394 | ret = jaylink_get_caps(jaylink_devh, caps); |
| 395 | |
| 396 | if (ret != JAYLINK_OK) { |
| 397 | msg_perr("jaylink_get_caps() failed: %s.\n", jaylink_strerror(ret)); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 398 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) { |
| 402 | ret = jaylink_get_extended_caps(jaylink_devh, caps); |
| 403 | |
| 404 | if (ret != JAYLINK_OK) { |
| Angel Pons | 281ed26 | 2021-04-16 10:55:03 +0200 | [diff] [blame] | 405 | msg_perr("jaylink_get_extended_caps() failed: %s.\n", jaylink_strerror(ret)); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 406 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
| Marc Schink | 137f02f | 2020-08-23 16:19:44 +0200 | [diff] [blame] | 410 | if (enable_target_power) { |
| 411 | if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) { |
| 412 | msg_perr("Device does not support target power.\n"); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 413 | goto init_err; |
| Marc Schink | 137f02f | 2020-08-23 16:19:44 +0200 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 417 | uint32_t ifaces; |
| 418 | |
| 419 | ret = jaylink_get_available_interfaces(jaylink_devh, &ifaces); |
| 420 | |
| 421 | if (ret != JAYLINK_OK) { |
| 422 | msg_perr("jaylink_get_available_interfaces() failed: %s.\n", jaylink_strerror(ret)); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 423 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | if (!(ifaces & (1 << JAYLINK_TIF_JTAG))) { |
| 427 | msg_perr("Device does not support JTAG interface.\n"); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 428 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | ret = jaylink_select_interface(jaylink_devh, JAYLINK_TIF_JTAG, NULL); |
| 432 | |
| 433 | if (ret != JAYLINK_OK) { |
| 434 | msg_perr("jaylink_select_interface() failed: %s.\n", jaylink_strerror(ret)); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 435 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| Marc Schink | 137f02f | 2020-08-23 16:19:44 +0200 | [diff] [blame] | 438 | if (enable_target_power) { |
| 439 | ret = jaylink_set_target_power(jaylink_devh, true); |
| 440 | |
| 441 | if (ret != JAYLINK_OK) { |
| 442 | msg_perr("jaylink_set_target_power() failed: %s.\n", jaylink_strerror(ret)); |
| 443 | return 1; |
| 444 | } |
| 445 | |
| 446 | /* Wait some time until the target is powered up. */ |
| 447 | internal_sleep(10000); |
| 448 | } |
| 449 | |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 450 | struct jaylink_hardware_status hwstat; |
| 451 | |
| 452 | ret = jaylink_get_hardware_status(jaylink_devh, &hwstat); |
| 453 | |
| 454 | if (ret != JAYLINK_OK) { |
| 455 | msg_perr("jaylink_get_hardware_status() failed: %s.\n", jaylink_strerror(ret)); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 456 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | msg_pdbg("VTarget: %u.%03u V\n", hwstat.target_voltage / 1000, |
| 460 | hwstat.target_voltage % 1000); |
| 461 | |
| 462 | if (hwstat.target_voltage < MIN_TARGET_VOLTAGE) { |
| 463 | msg_perr("Target voltage is below %u.%03u V. You need to attach VTref to the I/O voltage of " |
| 464 | "the chip.\n", MIN_TARGET_VOLTAGE / 1000, MIN_TARGET_VOLTAGE % 1000); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 465 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | struct jaylink_speed device_speeds; |
| 469 | |
| 470 | device_speeds.freq = DEFAULT_FREQ; |
| 471 | device_speeds.div = DEFAULT_FREQ_DIV; |
| 472 | |
| 473 | if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) { |
| 474 | ret = jaylink_get_speeds(jaylink_devh, &device_speeds); |
| 475 | |
| 476 | if (ret != JAYLINK_OK) { |
| 477 | msg_perr("jaylink_get_speeds() failed: %s.\n", jaylink_strerror(ret)); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 478 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | device_speeds.freq /= 1000; |
| 483 | |
| 484 | msg_pdbg("Maximum SPI speed: %" PRIu32 " kHz\n", device_speeds.freq / device_speeds.div); |
| 485 | |
| 486 | if (!speed) { |
| 487 | speed = device_speeds.freq / device_speeds.div; |
| 488 | msg_pdbg("SPI speed not specified, using %lu kHz.\n", speed); |
| 489 | } |
| 490 | |
| 491 | if (speed > (device_speeds.freq / device_speeds.div)) { |
| 492 | msg_perr("Specified SPI speed of %lu kHz is too high. Maximum is %" PRIu32 " kHz.\n", speed, |
| 493 | device_speeds.freq / device_speeds.div); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 494 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | ret = jaylink_set_speed(jaylink_devh, speed); |
| 498 | |
| 499 | if (ret != JAYLINK_OK) { |
| 500 | msg_perr("jaylink_set_speed() failed: %s.\n", jaylink_strerror(ret)); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 501 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | msg_pdbg("SPI speed: %lu kHz\n", speed); |
| 505 | |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 506 | jlink_data = calloc(1, sizeof(*jlink_data)); |
| 507 | if (!jlink_data) { |
| Anastasia Klimchuk | 9e698f9 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 508 | msg_perr("Unable to allocate space for SPI master data\n"); |
| 509 | goto init_err; |
| 510 | } |
| 511 | |
| 512 | /* jaylink_ctx, jaylink_devh are allocated by jaylink_init and jaylink_open */ |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 513 | jlink_data->ctx = jaylink_ctx; |
| 514 | jlink_data->devh = jaylink_devh; |
| 515 | jlink_data->reset_cs = reset_cs; |
| 516 | jlink_data->enable_target_power = enable_target_power; |
| Anastasia Klimchuk | 9e698f9 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 517 | |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 518 | /* Ensure that the CS signal is not active initially. */ |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 519 | if (!deassert_cs(jlink_data)) |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 520 | goto init_err; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 521 | |
| Nico Huber | 89569d6 | 2023-01-12 23:31:40 +0100 | [diff] [blame] | 522 | return register_spi_master(&spi_master_jlink_spi, 0, jlink_data); |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 523 | |
| 524 | init_err: |
| 525 | if (jaylink_devh) |
| 526 | jaylink_close(jaylink_devh); |
| 527 | |
| 528 | jaylink_exit(jaylink_ctx); |
| 529 | |
| Anastasia Klimchuk | 9e698f9 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 530 | /* jaylink_ctx, jaylink_devh are freed by jaylink_close and jaylink_exit */ |
| Anastasia Klimchuk | e4a9791 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 531 | if (jlink_data) |
| 532 | free(jlink_data); |
| Anastasia Klimchuk | 9e698f9 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 533 | |
| Anastasia Klimchuk | 00c2e80 | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 534 | return 1; |
| Marc Schink | 3578ec6 | 2016-03-17 16:23:03 +0100 | [diff] [blame] | 535 | } |
| Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 536 | |
| 537 | const struct programmer_entry programmer_jlink_spi = { |
| 538 | .name = "jlink_spi", |
| 539 | .type = OTHER, |
| 540 | .init = jlink_spi_init, |
| 541 | .devs.note = "SEGGER J-Link and compatible devices\n", |
| Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 542 | }; |