blob: eabc789a766dca76e7c2bbdb4be68a8cd23eabae [file] [log] [blame]
Marc Schink3578ec62016-03-17 16:23:03 +01001/*
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 Schinka8c0b682020-08-22 11:29:22 +020037#define JTAG_MAX_TRANSFER_SIZE (32768 / 8)
Marc Schink3578ec62016-03-17 16:23:03 +010038
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
54static struct jaylink_context *jaylink_ctx;
55static struct jaylink_device_handle *jaylink_devh;
56static bool reset_cs;
Marc Schink137f02f2020-08-23 16:19:44 +020057static bool enable_target_power;
Marc Schink3578ec62016-03-17 16:23:03 +010058
59static bool assert_cs(void)
60{
61 int ret;
62
63 if (reset_cs) {
64 ret = jaylink_clear_reset(jaylink_devh);
65
66 if (ret != JAYLINK_OK) {
67 msg_perr("jaylink_clear_reset() failed: %s.\n", jaylink_strerror(ret));
68 return false;
69 }
70 } else {
71 ret = jaylink_jtag_clear_trst(jaylink_devh);
72
73 if (ret != JAYLINK_OK) {
74 msg_perr("jaylink_jtag_clear_trst() failed: %s.\n", jaylink_strerror(ret));
75 return false;
76 }
77 }
78
79 return true;
80}
81
82static bool deassert_cs(void)
83{
84 int ret;
85
86 if (reset_cs) {
87 ret = jaylink_set_reset(jaylink_devh);
88
89 if (ret != JAYLINK_OK) {
90 msg_perr("jaylink_set_reset() failed: %s.\n", jaylink_strerror(ret));
91 return false;
92 }
93 } else {
94 ret = jaylink_jtag_set_trst(jaylink_devh);
95
96 if (ret != JAYLINK_OK) {
97 msg_perr("jaylink_jtag_set_trst() failed: %s.\n", jaylink_strerror(ret));
98 return false;
99 }
100 }
101
102 return true;
103}
104
Edward O'Callaghan5eca4272020-04-12 17:27:53 +1000105static int jlink_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
Marc Schink3578ec62016-03-17 16:23:03 +0100106 const unsigned char *writearr, unsigned char *readarr)
107{
108 uint32_t length;
109 uint8_t *buffer;
110
111 length = writecnt + readcnt;
112
113 if (length > JTAG_MAX_TRANSFER_SIZE)
114 return SPI_INVALID_LENGTH;
115
116 buffer = malloc(length);
117
118 if (!buffer) {
119 msg_perr("Memory allocation failed.\n");
120 return SPI_GENERIC_ERROR;
121 }
122
123 /* Reverse all bytes because the device transfers data LSB first. */
124 reverse_bytes(buffer, writearr, writecnt);
125
126 memset(buffer + writecnt, 0x00, readcnt);
127
128 if (!assert_cs()) {
129 free(buffer);
130 return SPI_PROGRAMMER_ERROR;
131 }
132
133 int ret;
134
135 ret = jaylink_jtag_io(jaylink_devh, buffer, buffer, buffer, length * 8, JAYLINK_JTAG_VERSION_2);
136
137 if (ret != JAYLINK_OK) {
Angel Pons281ed262021-04-16 10:55:03 +0200138 msg_perr("jaylink_jtag_io() failed: %s.\n", jaylink_strerror(ret));
Marc Schink3578ec62016-03-17 16:23:03 +0100139 free(buffer);
140 return SPI_PROGRAMMER_ERROR;
141 }
142
143 if (!deassert_cs()) {
144 free(buffer);
145 return SPI_PROGRAMMER_ERROR;
146 }
147
148 /* Reverse all bytes because the device transfers data LSB first. */
149 reverse_bytes(readarr, buffer + writecnt, readcnt);
150 free(buffer);
151
152 return 0;
153}
154
155static const struct spi_master spi_master_jlink_spi = {
Marc Schink3578ec62016-03-17 16:23:03 +0100156 /* Maximum data read size in one go (excluding opcode+address). */
157 .max_data_read = JTAG_MAX_TRANSFER_SIZE - 5,
158 /* Maximum data write size in one go (excluding opcode+address). */
159 .max_data_write = JTAG_MAX_TRANSFER_SIZE - 5,
160 .command = jlink_spi_send_command,
161 .multicommand = default_spi_send_multicommand,
162 .read = default_spi_read,
163 .write_256 = default_spi_write_256,
164 .write_aai = default_spi_write_aai,
165 .features = SPI_MASTER_4BA,
166};
167
168static int jlink_spi_shutdown(void *data)
169{
Marc Schink137f02f2020-08-23 16:19:44 +0200170 if (jaylink_devh) {
171 if (enable_target_power) {
172 int ret = jaylink_set_target_power(jaylink_devh, false);
173
174 if (ret != JAYLINK_OK) {
175 msg_perr("jaylink_set_target_power() failed: %s.\n",
176 jaylink_strerror(ret));
177 }
178 }
179
Marc Schink3578ec62016-03-17 16:23:03 +0100180 jaylink_close(jaylink_devh);
Marc Schink137f02f2020-08-23 16:19:44 +0200181 }
Marc Schink3578ec62016-03-17 16:23:03 +0100182
183 jaylink_exit(jaylink_ctx);
Marc Schink3578ec62016-03-17 16:23:03 +0100184 return 0;
185}
186
Thomas Heijligencc853d82021-05-04 15:32:17 +0200187static int jlink_spi_init(void)
Marc Schink3578ec62016-03-17 16:23:03 +0100188{
189 char *arg;
190 unsigned long speed = 0;
191
192 register_shutdown(jlink_spi_shutdown, NULL);
193
194 arg = extract_programmer_param("spispeed");
195
196 if (arg) {
197 char *endptr;
198
199 errno = 0;
200 speed = strtoul(arg, &endptr, 10);
201
202 if (*endptr != '\0' || errno != 0) {
203 msg_perr("Invalid SPI speed specified: %s.\n", arg);
204 free(arg);
205 return 1;
206 }
207
208 if (speed < 1) {
209 msg_perr("SPI speed must be at least 1 kHz.\n");
210 free(arg);
211 return 1;
212 }
213 }
214
215 free(arg);
216
217 int ret;
218 bool use_serial_number;
219 uint32_t serial_number;
220
221 arg = extract_programmer_param("serial");
222
223 if (arg) {
224 if (!strlen(arg)) {
Angel Pons281ed262021-04-16 10:55:03 +0200225 msg_perr("Empty serial number specified.\n");
Marc Schink3578ec62016-03-17 16:23:03 +0100226 free(arg);
227 return 1;
228 }
229
230 ret = jaylink_parse_serial_number(arg, &serial_number);
231
232 if (ret == JAYLINK_ERR) {
233 msg_perr("Invalid serial number specified: %s.\n", arg);
234 free(arg);
235 return 1;
236 } if (ret != JAYLINK_OK) {
237 msg_perr("jaylink_parse_serial_number() failed: %s.\n", jaylink_strerror(ret));
238 free(arg);
239 return 1;
240 }
241
242 use_serial_number = true;
243 } else {
244 use_serial_number = false;
245 }
246
247 free(arg);
248
249 reset_cs = true;
250 arg = extract_programmer_param("cs");
251
252 if (arg) {
253 if (!strcasecmp(arg, "reset")) {
254 reset_cs = true;
255 } else if (!strcasecmp(arg, "trst")) {
256 reset_cs = false;
257 } else {
258 msg_perr("Invalid chip select pin specified: '%s'.\n", arg);
259 free(arg);
260 return 1;
261 }
262 }
263
264 free(arg);
265
266 if (reset_cs)
267 msg_pdbg("Using RESET as chip select signal.\n");
268 else
269 msg_pdbg("Using TRST as chip select signal.\n");
270
Marc Schink137f02f2020-08-23 16:19:44 +0200271 enable_target_power = false;
272 arg = extract_programmer_param("power");
273
274 if (arg) {
275 if (!strcasecmp(arg, "on")) {
276 enable_target_power = true;
277 } else {
278 msg_perr("Invalid value for 'power' argument: '%s'.\n", arg);
279 free(arg);
280 return 1;
281 }
282 }
283
284 free(arg);
285
Marc Schink3578ec62016-03-17 16:23:03 +0100286 ret = jaylink_init(&jaylink_ctx);
287
288 if (ret != JAYLINK_OK) {
289 msg_perr("jaylink_init() failed: %s.\n", jaylink_strerror(ret));
290 return 1;
291 }
292
293 ret = jaylink_discovery_scan(jaylink_ctx, 0);
294
295 if (ret != JAYLINK_OK) {
Angel Pons281ed262021-04-16 10:55:03 +0200296 msg_perr("jaylink_discovery_scan() failed: %s.\n", jaylink_strerror(ret));
Marc Schink3578ec62016-03-17 16:23:03 +0100297 return 1;
298 }
299
300 struct jaylink_device **devs;
301
302 ret = jaylink_get_devices(jaylink_ctx, &devs, NULL);
303
304 if (ret != JAYLINK_OK) {
305 msg_perr("jaylink_get_devices() failed: %s.\n", jaylink_strerror(ret));
306 return 1;
307 }
308
309 if (!use_serial_number)
310 msg_pdbg("No device selected, using first device.\n");
311
312 size_t i;
313 struct jaylink_device *dev;
314 bool device_found = false;
315
316 for (i = 0; devs[i]; i++) {
317 if (use_serial_number) {
318 uint32_t tmp;
319
320 ret = jaylink_device_get_serial_number(devs[i], &tmp);
321
322 if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
323 continue;
324 } else if (ret != JAYLINK_OK) {
325 msg_pwarn("jaylink_device_get_serial_number() failed: %s.\n",
326 jaylink_strerror(ret));
327 continue;
328 }
329
330 if (serial_number != tmp)
331 continue;
332 }
333
334 ret = jaylink_open(devs[i], &jaylink_devh);
335
336 if (ret == JAYLINK_OK) {
337 dev = devs[i];
338 device_found = true;
339 break;
340 }
341
342 jaylink_devh = NULL;
343 }
344
345 jaylink_free_devices(devs, true);
346
347 if (!device_found) {
348 msg_perr("No J-Link device found.\n");
349 return 1;
350 }
351
352 size_t length;
353 char *firmware_version;
354
355 ret = jaylink_get_firmware_version(jaylink_devh, &firmware_version,
356 &length);
357
358 if (ret != JAYLINK_OK) {
359 msg_perr("jaylink_get_firmware_version() failed: %s.\n", jaylink_strerror(ret));
360 return 1;
361 } else if (length > 0) {
362 msg_pdbg("Firmware: %s\n", firmware_version);
363 free(firmware_version);
364 }
365
366 ret = jaylink_device_get_serial_number(dev, &serial_number);
367
368 if (ret == JAYLINK_OK) {
369 msg_pdbg("S/N: %" PRIu32 "\n", serial_number);
370 } else if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
371 msg_pdbg("S/N: N/A\n");
372 } else {
373 msg_perr("jaylink_device_get_serial_number() failed: %s.\n", jaylink_strerror(ret));
374 return 1;
375 }
376
Angel Pons7e134562021-06-07 13:29:13 +0200377 uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE] = { 0 };
Marc Schink3578ec62016-03-17 16:23:03 +0100378
Marc Schink3578ec62016-03-17 16:23:03 +0100379 ret = jaylink_get_caps(jaylink_devh, caps);
380
381 if (ret != JAYLINK_OK) {
382 msg_perr("jaylink_get_caps() failed: %s.\n", jaylink_strerror(ret));
383 return 1;
384 }
385
386 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
387 ret = jaylink_get_extended_caps(jaylink_devh, caps);
388
389 if (ret != JAYLINK_OK) {
Angel Pons281ed262021-04-16 10:55:03 +0200390 msg_perr("jaylink_get_extended_caps() failed: %s.\n", jaylink_strerror(ret));
Marc Schink3578ec62016-03-17 16:23:03 +0100391 return 1;
392 }
393 }
394
Marc Schink137f02f2020-08-23 16:19:44 +0200395 if (enable_target_power) {
396 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
397 msg_perr("Device does not support target power.\n");
398 return 1;
399 }
400 }
401
Marc Schink3578ec62016-03-17 16:23:03 +0100402 uint32_t ifaces;
403
404 ret = jaylink_get_available_interfaces(jaylink_devh, &ifaces);
405
406 if (ret != JAYLINK_OK) {
407 msg_perr("jaylink_get_available_interfaces() failed: %s.\n", jaylink_strerror(ret));
408 return 1;
409 }
410
411 if (!(ifaces & (1 << JAYLINK_TIF_JTAG))) {
412 msg_perr("Device does not support JTAG interface.\n");
413 return 1;
414 }
415
416 ret = jaylink_select_interface(jaylink_devh, JAYLINK_TIF_JTAG, NULL);
417
418 if (ret != JAYLINK_OK) {
419 msg_perr("jaylink_select_interface() failed: %s.\n", jaylink_strerror(ret));
420 return 1;
421 }
422
Marc Schink137f02f2020-08-23 16:19:44 +0200423 if (enable_target_power) {
424 ret = jaylink_set_target_power(jaylink_devh, true);
425
426 if (ret != JAYLINK_OK) {
427 msg_perr("jaylink_set_target_power() failed: %s.\n", jaylink_strerror(ret));
428 return 1;
429 }
430
431 /* Wait some time until the target is powered up. */
432 internal_sleep(10000);
433 }
434
Marc Schink3578ec62016-03-17 16:23:03 +0100435 struct jaylink_hardware_status hwstat;
436
437 ret = jaylink_get_hardware_status(jaylink_devh, &hwstat);
438
439 if (ret != JAYLINK_OK) {
440 msg_perr("jaylink_get_hardware_status() failed: %s.\n", jaylink_strerror(ret));
441 return 1;
442 }
443
444 msg_pdbg("VTarget: %u.%03u V\n", hwstat.target_voltage / 1000,
445 hwstat.target_voltage % 1000);
446
447 if (hwstat.target_voltage < MIN_TARGET_VOLTAGE) {
448 msg_perr("Target voltage is below %u.%03u V. You need to attach VTref to the I/O voltage of "
449 "the chip.\n", MIN_TARGET_VOLTAGE / 1000, MIN_TARGET_VOLTAGE % 1000);
450 return 1;
451 }
452
453 struct jaylink_speed device_speeds;
454
455 device_speeds.freq = DEFAULT_FREQ;
456 device_speeds.div = DEFAULT_FREQ_DIV;
457
458 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) {
459 ret = jaylink_get_speeds(jaylink_devh, &device_speeds);
460
461 if (ret != JAYLINK_OK) {
462 msg_perr("jaylink_get_speeds() failed: %s.\n", jaylink_strerror(ret));
463 return 1;
464 }
465 }
466
467 device_speeds.freq /= 1000;
468
469 msg_pdbg("Maximum SPI speed: %" PRIu32 " kHz\n", device_speeds.freq / device_speeds.div);
470
471 if (!speed) {
472 speed = device_speeds.freq / device_speeds.div;
473 msg_pdbg("SPI speed not specified, using %lu kHz.\n", speed);
474 }
475
476 if (speed > (device_speeds.freq / device_speeds.div)) {
477 msg_perr("Specified SPI speed of %lu kHz is too high. Maximum is %" PRIu32 " kHz.\n", speed,
478 device_speeds.freq / device_speeds.div);
479 return 1;
480 }
481
482 ret = jaylink_set_speed(jaylink_devh, speed);
483
484 if (ret != JAYLINK_OK) {
485 msg_perr("jaylink_set_speed() failed: %s.\n", jaylink_strerror(ret));
486 return 1;
487 }
488
489 msg_pdbg("SPI speed: %lu kHz\n", speed);
490
491 /* Ensure that the CS signal is not active initially. */
492 if (!deassert_cs())
493 return 1;
494
495 register_spi_master(&spi_master_jlink_spi);
496
497 return 0;
498}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200499
500const struct programmer_entry programmer_jlink_spi = {
501 .name = "jlink_spi",
502 .type = OTHER,
503 .init = jlink_spi_init,
504 .devs.note = "SEGGER J-Link and compatible devices\n",
505 .map_flash_region = fallback_map,
506 .unmap_flash_region = fallback_unmap,
507 .delay = internal_delay,
508};