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