Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [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 | 9e29ca9 | 2020-08-22 11:29:22 +0200 | [diff] [blame] | 37 | #define JTAG_MAX_TRANSFER_SIZE (32768 / 8) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [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 | 722b059 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 54 | struct jlink_spi_data { |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 55 | struct jaylink_context *ctx; |
| 56 | struct jaylink_device_handle *devh; |
Anastasia Klimchuk | 722b059 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 57 | bool reset_cs; |
| 58 | }; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 59 | |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 60 | static bool assert_cs(struct jlink_spi_data *jlink_data) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 61 | { |
| 62 | int ret; |
| 63 | |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 64 | if (jlink_data->reset_cs) { |
| 65 | ret = jaylink_clear_reset(jlink_data->devh); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 66 | |
| 67 | if (ret != JAYLINK_OK) { |
| 68 | msg_perr("jaylink_clear_reset() failed: %s.\n", jaylink_strerror(ret)); |
| 69 | return false; |
| 70 | } |
| 71 | } else { |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 72 | ret = jaylink_jtag_clear_trst(jlink_data->devh); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 73 | |
| 74 | if (ret != JAYLINK_OK) { |
| 75 | msg_perr("jaylink_jtag_clear_trst() failed: %s.\n", jaylink_strerror(ret)); |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 83 | static bool deassert_cs(struct jlink_spi_data *jlink_data) |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 84 | { |
| 85 | int ret; |
| 86 | |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 87 | if (jlink_data->reset_cs) { |
| 88 | ret = jaylink_set_reset(jlink_data->devh); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 89 | |
| 90 | if (ret != JAYLINK_OK) { |
| 91 | msg_perr("jaylink_set_reset() failed: %s.\n", jaylink_strerror(ret)); |
| 92 | return false; |
| 93 | } |
| 94 | } else { |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 95 | ret = jaylink_jtag_set_trst(jlink_data->devh); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 96 | |
| 97 | if (ret != JAYLINK_OK) { |
| 98 | msg_perr("jaylink_jtag_set_trst() failed: %s.\n", jaylink_strerror(ret)); |
| 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
Edward O'Callaghan | cf8d8f6 | 2020-10-13 13:35:53 +1100 | [diff] [blame] | 106 | static int jlink_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 107 | const unsigned char *writearr, unsigned char *readarr) |
| 108 | { |
| 109 | uint32_t length; |
| 110 | uint8_t *buffer; |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 111 | struct jlink_spi_data *jlink_data = flash->mst->spi.data; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 112 | |
| 113 | length = writecnt + readcnt; |
| 114 | |
| 115 | if (length > JTAG_MAX_TRANSFER_SIZE) |
| 116 | return SPI_INVALID_LENGTH; |
| 117 | |
| 118 | buffer = malloc(length); |
| 119 | |
| 120 | if (!buffer) { |
| 121 | msg_perr("Memory allocation failed.\n"); |
| 122 | return SPI_GENERIC_ERROR; |
| 123 | } |
| 124 | |
| 125 | /* Reverse all bytes because the device transfers data LSB first. */ |
| 126 | reverse_bytes(buffer, writearr, writecnt); |
| 127 | |
| 128 | memset(buffer + writecnt, 0x00, readcnt); |
| 129 | |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 130 | if (!assert_cs(jlink_data)) { |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 131 | free(buffer); |
| 132 | return SPI_PROGRAMMER_ERROR; |
| 133 | } |
| 134 | |
| 135 | int ret; |
| 136 | |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 137 | ret = jaylink_jtag_io(jlink_data->devh, |
Anastasia Klimchuk | 722b059 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 138 | buffer, buffer, buffer, length * 8, JAYLINK_JTAG_VERSION_2); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 139 | |
| 140 | if (ret != JAYLINK_OK) { |
Angel Pons | 1f9868c | 2021-04-16 10:55:03 +0200 | [diff] [blame] | 141 | msg_perr("jaylink_jtag_io() failed: %s.\n", jaylink_strerror(ret)); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 142 | free(buffer); |
| 143 | return SPI_PROGRAMMER_ERROR; |
| 144 | } |
| 145 | |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 146 | if (!deassert_cs(jlink_data)) { |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 147 | free(buffer); |
| 148 | return SPI_PROGRAMMER_ERROR; |
| 149 | } |
| 150 | |
| 151 | /* Reverse all bytes because the device transfers data LSB first. */ |
| 152 | reverse_bytes(readarr, buffer + writecnt, readcnt); |
| 153 | free(buffer); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 158 | static const struct spi_master spi_master_jlink_spi = { |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 159 | /* Maximum data read size in one go (excluding opcode+address). */ |
| 160 | .max_data_read = JTAG_MAX_TRANSFER_SIZE - 5, |
| 161 | /* Maximum data write size in one go (excluding opcode+address). */ |
| 162 | .max_data_write = JTAG_MAX_TRANSFER_SIZE - 5, |
| 163 | .command = jlink_spi_send_command, |
| 164 | .multicommand = default_spi_send_multicommand, |
| 165 | .read = default_spi_read, |
| 166 | .write_256 = default_spi_write_256, |
| 167 | .write_aai = default_spi_write_aai, |
| 168 | .features = SPI_MASTER_4BA, |
| 169 | }; |
| 170 | |
| 171 | static int jlink_spi_shutdown(void *data) |
| 172 | { |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 173 | struct jlink_spi_data *jlink_data = data; |
| 174 | if (jlink_data->devh) |
| 175 | jaylink_close(jlink_data->devh); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 176 | |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 177 | jaylink_exit(jlink_data->ctx); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 178 | |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 179 | /* jlink_data->ctx, jlink_data->devh are freed by jaylink_close and jaylink_exit */ |
| 180 | free(jlink_data); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | int jlink_spi_init(void) |
| 185 | { |
| 186 | char *arg; |
| 187 | unsigned long speed = 0; |
Anastasia Klimchuk | 722b059 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 188 | struct jaylink_context *jaylink_ctx = NULL; |
| 189 | struct jaylink_device_handle *jaylink_devh = NULL; |
| 190 | bool reset_cs; |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 191 | struct jlink_spi_data *jlink_data = NULL; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 192 | |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 193 | arg = extract_programmer_param("spispeed"); |
| 194 | |
| 195 | if (arg) { |
| 196 | char *endptr; |
| 197 | |
| 198 | errno = 0; |
| 199 | speed = strtoul(arg, &endptr, 10); |
| 200 | |
| 201 | if (*endptr != '\0' || errno != 0) { |
| 202 | msg_perr("Invalid SPI speed specified: %s.\n", arg); |
| 203 | free(arg); |
| 204 | return 1; |
| 205 | } |
| 206 | |
| 207 | if (speed < 1) { |
| 208 | msg_perr("SPI speed must be at least 1 kHz.\n"); |
| 209 | free(arg); |
| 210 | return 1; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | free(arg); |
| 215 | |
| 216 | int ret; |
| 217 | bool use_serial_number; |
| 218 | uint32_t serial_number; |
| 219 | |
| 220 | arg = extract_programmer_param("serial"); |
| 221 | |
| 222 | if (arg) { |
| 223 | if (!strlen(arg)) { |
Angel Pons | 1f9868c | 2021-04-16 10:55:03 +0200 | [diff] [blame] | 224 | msg_perr("Empty serial number specified.\n"); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 225 | free(arg); |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | ret = jaylink_parse_serial_number(arg, &serial_number); |
| 230 | |
| 231 | if (ret == JAYLINK_ERR) { |
| 232 | msg_perr("Invalid serial number specified: %s.\n", arg); |
| 233 | free(arg); |
| 234 | return 1; |
| 235 | } if (ret != JAYLINK_OK) { |
| 236 | msg_perr("jaylink_parse_serial_number() failed: %s.\n", jaylink_strerror(ret)); |
| 237 | free(arg); |
| 238 | return 1; |
| 239 | } |
| 240 | |
| 241 | use_serial_number = true; |
| 242 | } else { |
| 243 | use_serial_number = false; |
| 244 | } |
| 245 | |
| 246 | free(arg); |
| 247 | |
| 248 | reset_cs = true; |
| 249 | arg = extract_programmer_param("cs"); |
| 250 | |
| 251 | if (arg) { |
| 252 | if (!strcasecmp(arg, "reset")) { |
| 253 | reset_cs = true; |
| 254 | } else if (!strcasecmp(arg, "trst")) { |
| 255 | reset_cs = false; |
| 256 | } else { |
| 257 | msg_perr("Invalid chip select pin specified: '%s'.\n", arg); |
| 258 | free(arg); |
| 259 | return 1; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | free(arg); |
| 264 | |
| 265 | if (reset_cs) |
| 266 | msg_pdbg("Using RESET as chip select signal.\n"); |
| 267 | else |
| 268 | msg_pdbg("Using TRST as chip select signal.\n"); |
| 269 | |
| 270 | ret = jaylink_init(&jaylink_ctx); |
| 271 | |
| 272 | if (ret != JAYLINK_OK) { |
| 273 | msg_perr("jaylink_init() failed: %s.\n", jaylink_strerror(ret)); |
| 274 | return 1; |
| 275 | } |
| 276 | |
| 277 | ret = jaylink_discovery_scan(jaylink_ctx, 0); |
| 278 | |
| 279 | if (ret != JAYLINK_OK) { |
Angel Pons | 1f9868c | 2021-04-16 10:55:03 +0200 | [diff] [blame] | 280 | msg_perr("jaylink_discovery_scan() failed: %s.\n", jaylink_strerror(ret)); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 281 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | struct jaylink_device **devs; |
| 285 | |
| 286 | ret = jaylink_get_devices(jaylink_ctx, &devs, NULL); |
| 287 | |
| 288 | if (ret != JAYLINK_OK) { |
| 289 | msg_perr("jaylink_get_devices() failed: %s.\n", jaylink_strerror(ret)); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 290 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | if (!use_serial_number) |
| 294 | msg_pdbg("No device selected, using first device.\n"); |
| 295 | |
| 296 | size_t i; |
| 297 | struct jaylink_device *dev; |
| 298 | bool device_found = false; |
| 299 | |
| 300 | for (i = 0; devs[i]; i++) { |
| 301 | if (use_serial_number) { |
| 302 | uint32_t tmp; |
| 303 | |
| 304 | ret = jaylink_device_get_serial_number(devs[i], &tmp); |
| 305 | |
| 306 | if (ret == JAYLINK_ERR_NOT_AVAILABLE) { |
| 307 | continue; |
| 308 | } else if (ret != JAYLINK_OK) { |
| 309 | msg_pwarn("jaylink_device_get_serial_number() failed: %s.\n", |
| 310 | jaylink_strerror(ret)); |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | if (serial_number != tmp) |
| 315 | continue; |
| 316 | } |
| 317 | |
| 318 | ret = jaylink_open(devs[i], &jaylink_devh); |
| 319 | |
| 320 | if (ret == JAYLINK_OK) { |
| 321 | dev = devs[i]; |
| 322 | device_found = true; |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | jaylink_devh = NULL; |
| 327 | } |
| 328 | |
| 329 | jaylink_free_devices(devs, true); |
| 330 | |
| 331 | if (!device_found) { |
| 332 | msg_perr("No J-Link device found.\n"); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 333 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | size_t length; |
| 337 | char *firmware_version; |
| 338 | |
| 339 | ret = jaylink_get_firmware_version(jaylink_devh, &firmware_version, |
| 340 | &length); |
| 341 | |
| 342 | if (ret != JAYLINK_OK) { |
| 343 | msg_perr("jaylink_get_firmware_version() failed: %s.\n", jaylink_strerror(ret)); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 344 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 345 | } else if (length > 0) { |
| 346 | msg_pdbg("Firmware: %s\n", firmware_version); |
| 347 | free(firmware_version); |
| 348 | } |
| 349 | |
| 350 | ret = jaylink_device_get_serial_number(dev, &serial_number); |
| 351 | |
| 352 | if (ret == JAYLINK_OK) { |
| 353 | msg_pdbg("S/N: %" PRIu32 "\n", serial_number); |
| 354 | } else if (ret == JAYLINK_ERR_NOT_AVAILABLE) { |
| 355 | msg_pdbg("S/N: N/A\n"); |
| 356 | } else { |
| 357 | msg_perr("jaylink_device_get_serial_number() failed: %s.\n", jaylink_strerror(ret)); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 358 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE]; |
| 362 | |
| 363 | memset(caps, 0, sizeof(caps)); |
| 364 | ret = jaylink_get_caps(jaylink_devh, caps); |
| 365 | |
| 366 | if (ret != JAYLINK_OK) { |
| 367 | msg_perr("jaylink_get_caps() failed: %s.\n", jaylink_strerror(ret)); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 368 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) { |
| 372 | ret = jaylink_get_extended_caps(jaylink_devh, caps); |
| 373 | |
| 374 | if (ret != JAYLINK_OK) { |
Angel Pons | 1f9868c | 2021-04-16 10:55:03 +0200 | [diff] [blame] | 375 | msg_perr("jaylink_get_extended_caps() failed: %s.\n", jaylink_strerror(ret)); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 376 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | uint32_t ifaces; |
| 381 | |
| 382 | ret = jaylink_get_available_interfaces(jaylink_devh, &ifaces); |
| 383 | |
| 384 | if (ret != JAYLINK_OK) { |
| 385 | msg_perr("jaylink_get_available_interfaces() failed: %s.\n", jaylink_strerror(ret)); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 386 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (!(ifaces & (1 << JAYLINK_TIF_JTAG))) { |
| 390 | msg_perr("Device does not support JTAG interface.\n"); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 391 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | ret = jaylink_select_interface(jaylink_devh, JAYLINK_TIF_JTAG, NULL); |
| 395 | |
| 396 | if (ret != JAYLINK_OK) { |
| 397 | msg_perr("jaylink_select_interface() failed: %s.\n", jaylink_strerror(ret)); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 398 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | struct jaylink_hardware_status hwstat; |
| 402 | |
| 403 | ret = jaylink_get_hardware_status(jaylink_devh, &hwstat); |
| 404 | |
| 405 | if (ret != JAYLINK_OK) { |
| 406 | msg_perr("jaylink_get_hardware_status() failed: %s.\n", jaylink_strerror(ret)); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 407 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | msg_pdbg("VTarget: %u.%03u V\n", hwstat.target_voltage / 1000, |
| 411 | hwstat.target_voltage % 1000); |
| 412 | |
| 413 | if (hwstat.target_voltage < MIN_TARGET_VOLTAGE) { |
| 414 | msg_perr("Target voltage is below %u.%03u V. You need to attach VTref to the I/O voltage of " |
| 415 | "the chip.\n", MIN_TARGET_VOLTAGE / 1000, MIN_TARGET_VOLTAGE % 1000); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 416 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | struct jaylink_speed device_speeds; |
| 420 | |
| 421 | device_speeds.freq = DEFAULT_FREQ; |
| 422 | device_speeds.div = DEFAULT_FREQ_DIV; |
| 423 | |
| 424 | if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) { |
| 425 | ret = jaylink_get_speeds(jaylink_devh, &device_speeds); |
| 426 | |
| 427 | if (ret != JAYLINK_OK) { |
| 428 | msg_perr("jaylink_get_speeds() failed: %s.\n", jaylink_strerror(ret)); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 429 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | device_speeds.freq /= 1000; |
| 434 | |
| 435 | msg_pdbg("Maximum SPI speed: %" PRIu32 " kHz\n", device_speeds.freq / device_speeds.div); |
| 436 | |
| 437 | if (!speed) { |
| 438 | speed = device_speeds.freq / device_speeds.div; |
| 439 | msg_pdbg("SPI speed not specified, using %lu kHz.\n", speed); |
| 440 | } |
| 441 | |
| 442 | if (speed > (device_speeds.freq / device_speeds.div)) { |
| 443 | msg_perr("Specified SPI speed of %lu kHz is too high. Maximum is %" PRIu32 " kHz.\n", speed, |
| 444 | device_speeds.freq / device_speeds.div); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 445 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | ret = jaylink_set_speed(jaylink_devh, speed); |
| 449 | |
| 450 | if (ret != JAYLINK_OK) { |
| 451 | msg_perr("jaylink_set_speed() failed: %s.\n", jaylink_strerror(ret)); |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 452 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | msg_pdbg("SPI speed: %lu kHz\n", speed); |
| 456 | |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 457 | jlink_data = calloc(1, sizeof(*jlink_data)); |
| 458 | if (!jlink_data) { |
Anastasia Klimchuk | 722b059 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 459 | msg_perr("Unable to allocate space for SPI master data\n"); |
| 460 | goto init_err; |
| 461 | } |
| 462 | |
| 463 | /* jaylink_ctx, jaylink_devh are allocated by jaylink_init and jaylink_open */ |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 464 | jlink_data->ctx = jaylink_ctx; |
| 465 | jlink_data->devh = jaylink_devh; |
| 466 | jlink_data->reset_cs = reset_cs; |
Anastasia Klimchuk | 722b059 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 467 | |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 468 | /* Ensure that the CS signal is not active initially. */ |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 469 | if (!deassert_cs(jlink_data)) |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 470 | goto init_err; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 471 | |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 472 | if (register_shutdown(jlink_spi_shutdown, jlink_data)) |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 473 | goto init_err; |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 474 | register_spi_master(&spi_master_jlink_spi, jlink_data); |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 475 | |
| 476 | return 0; |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 477 | |
| 478 | init_err: |
| 479 | if (jaylink_devh) |
| 480 | jaylink_close(jaylink_devh); |
| 481 | |
| 482 | jaylink_exit(jaylink_ctx); |
| 483 | |
Anastasia Klimchuk | 722b059 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 484 | /* jaylink_ctx, jaylink_devh are freed by jaylink_close and jaylink_exit */ |
Anastasia Klimchuk | c2bf241 | 2021-04-26 10:58:16 +1000 | [diff] [blame] | 485 | if (jlink_data) |
| 486 | free(jlink_data); |
Anastasia Klimchuk | 722b059 | 2021-04-21 12:21:49 +1000 | [diff] [blame] | 487 | |
Anastasia Klimchuk | e9a57ad | 2021-04-14 09:52:36 +1000 | [diff] [blame] | 488 | return 1; |
Edward O'Callaghan | 611253c | 2019-09-09 00:22:07 +1000 | [diff] [blame] | 489 | } |