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