Add source code for new ULINK driver
[openocd.git] / src / jtag / drivers / ulink.c
1 /***************************************************************************
2 * Copyright (C) 2011 by Martin Schmoelzer *
3 * <martin.schmoelzer@student.tuwien.ac.at> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <jtag/interface.h>
26 #include <jtag/commands.h>
27 #include <target/image.h>
28 #include <helper/types.h>
29 #include "usb_common.h"
30 #include "OpenULINK/include/msgtypes.h"
31
32 /** USB Vendor ID of ULINK device in unconfigured state (no firmware loaded
33 * yet) or with OpenULINK firmware. */
34 #define ULINK_VID 0xC251
35
36 /** USB Product ID of ULINK device in unconfigured state (no firmware loaded
37 * yet) or with OpenULINK firmware. */
38 #define ULINK_PID 0x2710
39
40 /** Address of EZ-USB CPU Control & Status register. This register can be
41 * written by issuing a Control EP0 vendor request. */
42 #define CPUCS_REG 0x7F92
43
44 /** USB Control EP0 bRequest: "Firmware Load". */
45 #define REQUEST_FIRMWARE_LOAD 0xA0
46
47 /** Value to write into CPUCS to put EZ-USB into reset. */
48 #define CPU_RESET 0x01
49
50 /** Value to write into CPUCS to put EZ-USB out of reset. */
51 #define CPU_START 0x00
52
53 /** Base address of firmware in EZ-USB code space. */
54 #define FIRMWARE_ADDR 0x0000
55
56 /** USB interface number */
57 #define USB_INTERFACE 0
58
59 /** libusb timeout in ms */
60 #define USB_TIMEOUT 5000
61
62 /** Delay (in microseconds) to wait while EZ-USB performs ReNumeration. */
63 #define ULINK_RENUMERATION_DELAY 1500000
64
65 /** Location of OpenULINK firmware image. TODO: Provide some way of modifying
66 * this path, maybe in a separate OpenOCD command? */
67 #define ULINK_FIRMWARE_FILE PKGLIBDIR "/OpenULINK/ulink_firmware.hex"
68
69 /** Maximum size of a single firmware section. Entire EZ-USB code space = 8kB */
70 #define SECTION_BUFFERSIZE 8192
71
72 /** Tuning of OpenOCD SCAN commands split into multiple OpenULINK commands. */
73 #define SPLIT_SCAN_THRESHOLD 10
74
75 /** ULINK hardware type */
76 enum ulink_type
77 {
78 /** Original ULINK adapter, based on Cypress EZ-USB (AN2131):
79 * Full JTAG support, no SWD support. */
80 ULINK_1,
81
82 /** Newer ULINK adapter, based on NXP LPC2148. Currently unsupported. */
83 ULINK_2,
84
85 /** Newer ULINK adapter, based on EZ-USB FX2 + FPGA. Currently unsupported. */
86 ULINK_PRO,
87
88 /** Newer ULINK adapter, possibly based on ULINK 2. Currently unsupported. */
89 ULINK_ME
90 };
91
92 enum ulink_payload_direction
93 {
94 PAYLOAD_DIRECTION_OUT,
95 PAYLOAD_DIRECTION_IN
96 };
97
98 /**
99 * OpenULINK command (OpenULINK command queue element).
100 *
101 * For the OUT direction payload, things are quite easy: Payload is stored
102 * in a rather small array (up to 63 bytes), the payload is always allocated
103 * by the function generating the command and freed by ulink_clear_queue().
104 *
105 * For the IN direction payload, things get a little bit more complicated:
106 * The maximum IN payload size for a single command is 64 bytes. Assume that
107 * a single OpenOCD command needs to scan 256 bytes. This results in the
108 * generation of four OpenULINK commands. The function generating these
109 * commands shall allocate an uint8_t[256] array. Each command's #payload_in
110 * pointer shall point to the corresponding offset where IN data shall be
111 * placed, while #payload_in_start shall point to the first element of the 256
112 * byte array.
113 * - first command: #payload_in_start + 0
114 * - second command: #payload_in_start + 64
115 * - third command: #payload_in_start + 128
116 * - fourth command: #payload_in_start + 192
117 *
118 * The last command sets #needs_postprocessing to true.
119 */
120 struct ulink_cmd {
121 uint8_t id; ///< ULINK command ID
122
123 uint8_t *payload_out; ///< OUT direction payload data
124 uint8_t payload_out_size; ///< OUT direction payload size for this command
125
126 uint8_t *payload_in_start; ///< Pointer to first element of IN payload array
127 uint8_t *payload_in; ///< Pointer where IN payload shall be stored
128 uint8_t payload_in_size; ///< IN direction payload size for this command
129
130 /** Indicates if this command needs post-processing */
131 bool needs_postprocessing;
132
133 /** Indicates if ulink_clear_queue() should free payload_in_start */
134 bool free_payload_in_start;
135
136 /** Pointer to corresponding OpenOCD command for post-processing */
137 struct jtag_command *cmd_origin;
138
139 struct ulink_cmd *next; ///< Pointer to next command (linked list)
140 };
141
142 typedef struct ulink_cmd ulink_cmd_t;
143
144 /** Describes one driver instance */
145 struct ulink
146 {
147 struct usb_dev_handle *usb_handle;
148 enum ulink_type type;
149
150 int commands_in_queue; ///< Number of commands in queue
151 ulink_cmd_t *queue_start; ///< Pointer to first command in queue
152 ulink_cmd_t *queue_end; ///< Pointer to last command in queue
153 };
154
155 /**************************** Function Prototypes *****************************/
156
157 /* USB helper functions */
158 int ulink_usb_open(struct ulink **device);
159 int ulink_usb_close(struct ulink **device);
160
161 /* ULINK MCU (Cypress EZ-USB) specific functions */
162 int ulink_cpu_reset(struct ulink *device, char reset_bit);
163 int ulink_load_firmware_and_renumerate(struct ulink **device, char *filename,
164 uint32_t delay);
165 int ulink_load_firmware(struct ulink *device, char *filename);
166 int ulink_write_firmware_section(struct ulink *device,
167 struct image *firmware_image, int section_index);
168
169 /* Generic helper functions */
170 void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals);
171
172 /* OpenULINK command generation helper functions */
173 int ulink_allocate_payload(ulink_cmd_t *ulink_cmd, int size,
174 enum ulink_payload_direction direction);
175
176 /* OpenULINK command queue helper functions */
177 int ulink_get_queue_size(struct ulink *device,
178 enum ulink_payload_direction direction);
179 void ulink_clear_queue(struct ulink *device);
180 int ulink_append_queue(struct ulink *device, ulink_cmd_t *ulink_cmd);
181 int ulink_execute_queued_commands(struct ulink *device, int timeout);
182
183 #ifdef _DEBUG_JTAG_IO_
184 const char * ulink_cmd_id_string(uint8_t id);
185 void ulink_print_command(ulink_cmd_t *ulink_cmd);
186 void ulink_print_queue(struct ulink *device);
187 #endif
188
189 int ulink_append_scan_cmd(struct ulink *device, enum scan_type scan_type,
190 int scan_size_bits, uint8_t *tdi, uint8_t *tdo_start, uint8_t *tdo,
191 uint8_t tms_count_start, uint8_t tms_sequence_start, uint8_t tms_count_end,
192 uint8_t tms_sequence_end, struct jtag_command *origin, bool postprocess);
193 int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count,
194 uint8_t sequence);
195 int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count);
196 int ulink_append_get_signals_cmd(struct ulink *device);
197 int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low,
198 uint8_t high);
199 int ulink_append_sleep_cmd(struct ulink *device, uint32_t us);
200 int ulink_append_configure_tck_cmd(struct ulink *device, uint8_t delay_scan,
201 uint8_t delay_tck, uint8_t delay_tms);
202 int ulink_append_led_cmd(struct ulink *device, uint8_t led_state);
203 int ulink_append_test_cmd(struct ulink *device);
204
205 /* Interface between OpenULINK and OpenOCD */
206 int ulink_queue_scan(struct ulink *device, struct jtag_command *cmd);
207 int ulink_queue_statemove(struct ulink *device);
208 int ulink_queue_reset(struct ulink *device, struct jtag_command *cmd);
209 int ulink_queue_runtest(struct ulink *device, struct jtag_command *cmd);
210 int ulink_queue_tlr_reset(struct ulink *device, struct jtag_command *cmd);
211 int ulink_queue_pathmove(struct ulink *device, struct jtag_command *cmd);
212 int ulink_queue_sleep(struct ulink *device, struct jtag_command *cmd);
213
214 static void ulink_set_end_state(tap_state_t endstate);
215
216 int ulink_post_process_scan(ulink_cmd_t *ulink_cmd);
217 int ulink_post_process_queue(struct ulink *device);
218
219 /* JTAG driver functions (registered in struct jtag_interface) */
220 static int ulink_execute_queue(void);
221 static int ulink_khz(int khz, int *jtag_speed);
222 static int ulink_speed(int speed);
223 static int ulink_speed_div(int speed, int *khz);
224 static int ulink_init(void);
225 static int ulink_quit(void);
226
227 /****************************** Global Variables ******************************/
228
229 struct ulink *ulink_handle;
230
231 /**************************** USB helper functions ****************************/
232
233 /**
234 * Opens the ULINK device and claims its USB interface.
235 *
236 * @param device pointer to struct ulink identifying ULINK driver instance.
237 * @return on success: ERROR_OK
238 * @return on failure: ERROR_FAIL
239 */
240 int ulink_usb_open(struct ulink **device)
241 {
242 int ret;
243 struct usb_dev_handle *usb_handle;
244
245 /* Currently, only original ULINK is supported */
246 uint16_t vids[] = { ULINK_VID, 0 };
247 uint16_t pids[] = { ULINK_PID, 0 };
248
249 ret = jtag_usb_open(vids, pids, &usb_handle);
250
251 if (ret != ERROR_OK) {
252 return ret;
253 }
254
255 ret = usb_claim_interface(usb_handle, 0);
256
257 if (ret != 0) {
258 return ret;
259 }
260
261 (*device)->usb_handle = usb_handle;
262 (*device)->type = ULINK_1;
263
264 return ERROR_OK;
265 }
266
267 /**
268 * Releases the ULINK interface and closes the USB device handle.
269 *
270 * @param device pointer to struct ulink identifying ULINK driver instance.
271 * @return on success: ERROR_OK
272 * @return on failure: ERROR_FAIL
273 */
274 int ulink_usb_close(struct ulink **device)
275 {
276 if (usb_release_interface((*device)->usb_handle, 0) != 0) {
277 return ERROR_FAIL;
278 }
279
280 if (usb_close((*device)->usb_handle) != 0) {
281 return ERROR_FAIL;
282 }
283
284 (*device)->usb_handle = NULL;
285
286 return ERROR_OK;
287 }
288
289 /******************* ULINK CPU (EZ-USB) specific functions ********************/
290
291 /**
292 * Writes '0' or '1' to the CPUCS register, putting the EZ-USB CPU into reset
293 * or out of reset.
294 *
295 * @param device pointer to struct ulink identifying ULINK driver instance.
296 * @param reset_bit 0 to put CPU into reset, 1 to put CPU out of reset.
297 * @return on success: ERROR_OK
298 * @return on failure: ERROR_FAIL
299 */
300 int ulink_cpu_reset(struct ulink *device, char reset_bit)
301 {
302 int ret;
303
304 ret = usb_control_msg(device->usb_handle,
305 (USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE),
306 REQUEST_FIRMWARE_LOAD, CPUCS_REG, 0, &reset_bit, 1, USB_TIMEOUT);
307
308 /* usb_control_msg() returns the number of bytes transferred during the
309 * DATA stage of the control transfer - must be exactly 1 in this case! */
310 if (ret != 1) {
311 return ERROR_FAIL;
312 }
313 return ERROR_OK;
314 }
315
316 /**
317 * Puts the ULINK's EZ-USB microcontroller into reset state, downloads
318 * the firmware image, resumes the microcontroller and re-enumerates
319 * USB devices.
320 *
321 * @param device pointer to struct ulink identifying ULINK driver instance.
322 * The usb_handle member will be modified during re-enumeration.
323 * @param filename path to the Intel HEX file containing the firmware image.
324 * @param delay the delay to wait for the device to re-enumerate.
325 * @return on success: ERROR_OK
326 * @return on failure: ERROR_FAIL
327 */
328 int ulink_load_firmware_and_renumerate(struct ulink **device,
329 char *filename, uint32_t delay)
330 {
331 int ret;
332
333 /* Basic process: After downloading the firmware, the ULINK will disconnect
334 * itself and re-connect after a short amount of time so we have to close
335 * the handle and re-enumerate USB devices */
336
337 ret = ulink_load_firmware(*device, filename);
338 if (ret != ERROR_OK) {
339 return ret;
340 }
341
342 ret = ulink_usb_close(device);
343 if (ret != ERROR_OK) {
344 return ret;
345 }
346
347 usleep(delay);
348
349 ret = ulink_usb_open(device);
350 if (ret != ERROR_OK) {
351 return ret;
352 }
353
354 return ERROR_OK;
355 }
356
357 /**
358 * Downloads a firmware image to the ULINK's EZ-USB microcontroller
359 * over the USB bus.
360 *
361 * @param device pointer to struct ulink identifying ULINK driver instance.
362 * @param filename an absolute or relative path to the Intel HEX file
363 * containing the firmware image.
364 * @return on success: ERROR_OK
365 * @return on failure: ERROR_FAIL
366 */
367 int ulink_load_firmware(struct ulink *device, char *filename)
368 {
369 struct image ulink_firmware_image;
370 int ret, i;
371
372 ret = ulink_cpu_reset(device, CPU_RESET);
373 if (ret != ERROR_OK) {
374 LOG_ERROR("Could not halt ULINK CPU");
375 return ret;
376 }
377
378 ulink_firmware_image.base_address = 0;
379 ulink_firmware_image.base_address_set = 0;
380
381 ret = image_open(&ulink_firmware_image, ULINK_FIRMWARE_FILE, "ihex");
382 if (ret != ERROR_OK) {
383 return ret;
384 }
385
386 /* Download all sections in the image to ULINK */
387 for (i = 0; i < ulink_firmware_image.num_sections; i++) {
388 ret = ulink_write_firmware_section(device, &ulink_firmware_image, i);
389 if (ret != ERROR_OK) {
390 return ret;
391 }
392 }
393
394 image_close(&ulink_firmware_image);
395
396 ret = ulink_cpu_reset(device, CPU_START);
397 if (ret != ERROR_OK) {
398 LOG_ERROR("Could not restart ULINK CPU");
399 return ret;
400 }
401
402 return ERROR_OK;
403 }
404
405 /**
406 * Send one contiguous firmware section to the ULINK's EZ-USB microcontroller
407 * over the USB bus.
408 *
409 * @param device pointer to struct ulink identifying ULINK driver instance.
410 * @param firmware_image pointer to the firmware image that contains the section
411 * which should be sent to the ULINK's EZ-USB microcontroller.
412 * @param section_index index of the section within the firmware image.
413 * @return on success: ERROR_OK
414 * @return on failure: ERROR_FAIL
415 */
416 int ulink_write_firmware_section(struct ulink *device,
417 struct image *firmware_image, int section_index)
418 {
419 uint16_t addr, size, bytes_remaining, chunk_size;
420 uint8_t data[SECTION_BUFFERSIZE];
421 uint8_t *data_ptr = data;
422 size_t size_read;
423 int ret;
424
425 size = (uint16_t)firmware_image->sections[section_index].size;
426 addr = (uint16_t)firmware_image->sections[section_index].base_address;
427
428 LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr,
429 size);
430
431 if (data == NULL) {
432 return ERROR_FAIL;
433 }
434
435 /* Copy section contents to local buffer */
436 ret = image_read_section(firmware_image, section_index, 0, size, data,
437 &size_read);
438
439 if ((ret != ERROR_OK) || (size_read != size)) {
440 /* Propagating the return code would return '0' (misleadingly indicating
441 * successful execution of the function) if only the size check fails. */
442 return ERROR_FAIL;
443 }
444
445 bytes_remaining = size;
446
447 /* Send section data in chunks of up to 64 bytes to ULINK */
448 while (bytes_remaining > 0) {
449 if (bytes_remaining > 64) {
450 chunk_size = 64;
451 }
452 else {
453 chunk_size = bytes_remaining;
454 }
455
456 ret = usb_control_msg(device->usb_handle,
457 (USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE),
458 REQUEST_FIRMWARE_LOAD, addr, FIRMWARE_ADDR, (char *)data_ptr,
459 chunk_size, USB_TIMEOUT);
460
461 if (ret != (int)chunk_size) {
462 /* Abort if libusb sent less data than requested */
463 return ERROR_FAIL;
464 }
465
466 bytes_remaining -= chunk_size;
467 addr += chunk_size;
468 data_ptr += chunk_size;
469 }
470
471 return ERROR_OK;
472 }
473
474 /************************** Generic helper functions **************************/
475
476 /**
477 * Print state of interesting signals via LOG_INFO().
478 *
479 * @param input_signals input signal states as returned by CMD_GET_SIGNALS
480 * @param output_signals output signal states as returned by CMD_GET_SIGNALS
481 */
482 void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals)
483 {
484 LOG_INFO("ULINK signal states: TDI: %i, TDO: %i, TMS: %i, TCK: %i, TRST: %i,"
485 " SRST: %i",
486 (output_signals & SIGNAL_TDI ? 1 : 0),
487 (input_signals & SIGNAL_TDO ? 1 : 0),
488 (output_signals & SIGNAL_TMS ? 1 : 0),
489 (output_signals & SIGNAL_TCK ? 1 : 0),
490 (output_signals & SIGNAL_TRST ? 0 : 1), // TRST and RESET are inverted
491 (output_signals & SIGNAL_RESET ? 0 : 1)); // by hardware
492 }
493
494 /**************** OpenULINK command generation helper functions ***************/
495
496 /**
497 * Allocate and initialize space in memory for OpenULINK command payload.
498 *
499 * @param ulink_cmd pointer to command whose payload should be allocated.
500 * @param size the amount of memory to allocate (bytes).
501 * @param direction which payload to allocate.
502 * @return on success: ERROR_OK
503 * @return on failure: ERROR_FAIL
504 */
505 int ulink_allocate_payload(ulink_cmd_t *ulink_cmd, int size,
506 enum ulink_payload_direction direction)
507 {
508 uint8_t *payload;
509
510 payload = calloc(size, sizeof(uint8_t));
511
512 if (payload == NULL) {
513 LOG_ERROR("Could not allocate OpenULINK command payload: out of memory");
514 return ERROR_FAIL;
515 }
516
517 switch (direction) {
518 case PAYLOAD_DIRECTION_OUT:
519 if (ulink_cmd->payload_out != NULL) {
520 LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
521 return ERROR_FAIL;
522 }
523 else {
524 ulink_cmd->payload_out = payload;
525 ulink_cmd->payload_out_size = size;
526 }
527 break;
528 case PAYLOAD_DIRECTION_IN:
529 if (ulink_cmd->payload_in_start != NULL) {
530 LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
531 return ERROR_FAIL;
532 }
533 else {
534 ulink_cmd->payload_in_start = payload;
535 ulink_cmd->payload_in = payload;
536 ulink_cmd->payload_in_size = size;
537
538 /* By default, free payload_in_start in ulink_clear_queue(). Commands
539 * that do not want this behavior (e. g. split scans) must turn it off
540 * separately! */
541 ulink_cmd->free_payload_in_start = true;
542 }
543 break;
544 }
545
546 return ERROR_OK;
547 }
548
549 /****************** OpenULINK command queue helper functions ******************/
550
551 /**
552 * Get the current number of bytes in the queue, including command IDs.
553 *
554 * @param device pointer to struct ulink identifying ULINK driver instance.
555 * @param direction the transfer direction for which to get byte count.
556 * @return the number of bytes currently stored in the queue for the specified
557 * direction.
558 */
559 int ulink_get_queue_size(struct ulink *device,
560 enum ulink_payload_direction direction)
561 {
562 ulink_cmd_t *current = device->queue_start;
563 int sum = 0;
564
565 while (current != NULL) {
566 switch (direction) {
567 case PAYLOAD_DIRECTION_OUT:
568 sum += current->payload_out_size + 1; // + 1 byte for Command ID
569 break;
570 case PAYLOAD_DIRECTION_IN:
571 sum += current->payload_in_size;
572 break;
573 }
574
575 current = current->next;
576 }
577
578 return sum;
579 }
580
581 /**
582 * Clear the OpenULINK command queue.
583 *
584 * @param device pointer to struct ulink identifying ULINK driver instance.
585 * @return on success: ERROR_OK
586 * @return on failure: ERROR_FAIL
587 */
588 void ulink_clear_queue(struct ulink *device)
589 {
590 ulink_cmd_t *current = device->queue_start;
591 ulink_cmd_t *next = NULL;
592
593 while (current != NULL) {
594 /* Save pointer to next element */
595 next = current->next;
596
597 /* Free payloads: OUT payload can be freed immediately */
598 free(current->payload_out);
599 current->payload_out = NULL;
600
601 /* IN payload MUST be freed ONLY if no other commands use the
602 * payload_in_start buffer */
603 if (current->free_payload_in_start == true) {
604 free(current->payload_in_start);
605 current->payload_in_start = NULL;
606 current->payload_in = NULL;
607 }
608
609 /* Free queue element */
610 free(current);
611
612 /* Proceed with next element */
613 current = next;
614 }
615
616 device->commands_in_queue = 0;
617 device->queue_start = NULL;
618 device->queue_end = NULL;
619 }
620
621 /**
622 * Add a command to the OpenULINK command queue.
623 *
624 * @param device pointer to struct ulink identifying ULINK driver instance.
625 * @param ulink_cmd pointer to command that shall be appended to the OpenULINK
626 * command queue.
627 * @return on success: ERROR_OK
628 * @return on failure: ERROR_FAIL
629 */
630 int ulink_append_queue(struct ulink *device, ulink_cmd_t *ulink_cmd)
631 {
632 int newsize_out, newsize_in;
633 int ret;
634
635 newsize_out = ulink_get_queue_size(device, PAYLOAD_DIRECTION_OUT) + 1
636 + ulink_cmd->payload_out_size;
637
638 newsize_in = ulink_get_queue_size(device, PAYLOAD_DIRECTION_IN)
639 + ulink_cmd->payload_in_size;
640
641 /* Check if the current command can be appended to the queue */
642 if ((newsize_out > 64) || (newsize_in > 64)) {
643 /* New command does not fit. Execute all commands in queue before starting
644 * new queue with the current command as first entry. */
645 ret = ulink_execute_queued_commands(device, USB_TIMEOUT);
646 if (ret != ERROR_OK) {
647 return ret;
648 }
649
650 ret = ulink_post_process_queue(device);
651 if (ret != ERROR_OK) {
652 return ret;
653 }
654
655 ulink_clear_queue(device);
656 }
657
658 if (device->queue_start == NULL) {
659 /* Queue was empty */
660 device->commands_in_queue = 1;
661
662 device->queue_start = ulink_cmd;
663 device->queue_end = ulink_cmd;
664 }
665 else {
666 /* There are already commands in the queue */
667 device->commands_in_queue++;
668
669 device->queue_end->next = ulink_cmd;
670 device->queue_end = ulink_cmd;
671 }
672
673 return ERROR_OK;
674 }
675
676 /**
677 * Sends all queued OpenULINK commands to the ULINK for execution.
678 *
679 * @param device pointer to struct ulink identifying ULINK driver instance.
680 * @return on success: ERROR_OK
681 * @return on failure: ERROR_FAIL
682 */
683 int ulink_execute_queued_commands(struct ulink *device, int timeout)
684 {
685 ulink_cmd_t *current;
686 int ret, i, index_out, index_in, count_out, count_in;
687 uint8_t buffer[64];
688
689 #ifdef _DEBUG_JTAG_IO_
690 ulink_print_queue(device);
691 #endif
692
693 index_out = 0;
694 count_out = 0;
695 count_in = 0;
696
697 for (current = device->queue_start; current; current = current->next) {
698 /* Add command to packet */
699 buffer[index_out] = current->id;
700 index_out++;
701 count_out++;
702
703 for (i = 0; i < current->payload_out_size; i++) {
704 buffer[index_out + i] = current->payload_out[i];
705 }
706 index_out += current->payload_out_size;
707 count_in += current->payload_in_size;
708 count_out += current->payload_out_size;
709 }
710
711 /* Send packet to ULINK */
712 ret = usb_bulk_write(device->usb_handle, (2 | USB_ENDPOINT_OUT),
713 (char *)buffer, count_out, timeout);
714 if (ret < 0) {
715 return ERROR_FAIL;
716 }
717 if (ret != count_out) {
718 return ERROR_FAIL;
719 }
720
721 /* Wait for response if commands contain IN payload data */
722 if (count_in > 0) {
723 ret = usb_bulk_read(device->usb_handle, (2 | USB_ENDPOINT_IN),
724 (char *)buffer, 64, timeout);
725 if (ret < 0) {
726 return ERROR_FAIL;
727 }
728 if (ret != count_in) {
729 return ERROR_FAIL;
730 }
731
732 /* Write back IN payload data */
733 index_in = 0;
734 for (current = device->queue_start; current; current = current->next) {
735 for (i = 0; i < current->payload_in_size; i++) {
736 current->payload_in[i] = buffer[index_in];
737 index_in++;
738 }
739 }
740 }
741
742 return ERROR_OK;
743 }
744
745 #ifdef _DEBUG_JTAG_IO_
746
747 /**
748 * Convert an OpenULINK command ID (\a id) to a human-readable string.
749 *
750 * @param id the OpenULINK command ID.
751 * @return the corresponding human-readable string.
752 */
753 const char * ulink_cmd_id_string(uint8_t id)
754 {
755 switch (id) {
756 case CMD_SCAN_IN:
757 return "CMD_SCAN_IN";
758 break;
759 case CMD_SLOW_SCAN_IN:
760 return "CMD_SLOW_SCAN_IN";
761 break;
762 case CMD_SCAN_OUT:
763 return "CMD_SCAN_OUT";
764 break;
765 case CMD_SLOW_SCAN_OUT:
766 return "CMD_SLOW_SCAN_OUT";
767 break;
768 case CMD_SCAN_IO:
769 return "CMD_SCAN_IO";
770 break;
771 case CMD_SLOW_SCAN_IO:
772 return "CMD_SLOW_SCAN_IO";
773 break;
774 case CMD_CLOCK_TMS:
775 return "CMD_CLOCK_TMS";
776 break;
777 case CMD_SLOW_CLOCK_TMS:
778 return "CMD_SLOW_CLOCK_TMS";
779 break;
780 case CMD_CLOCK_TCK:
781 return "CMD_CLOCK_TCK";
782 break;
783 case CMD_SLEEP_US:
784 return "CMD_SLEEP_US";
785 break;
786 case CMD_SLEEP_MS:
787 return "CMD_SLEEP_MS";
788 break;
789 case CMD_GET_SIGNALS:
790 return "CMD_GET_SIGNALS";
791 break;
792 case CMD_SET_SIGNALS:
793 return "CMD_SET_SIGNALS";
794 break;
795 case CMD_CONFIGURE_TCK_FREQ:
796 return "CMD_CONFIGURE_TCK_FREQ";
797 break;
798 case CMD_SET_LEDS:
799 return "CMD_SET_LEDS";
800 break;
801 case CMD_TEST:
802 return "CMD_TEST";
803 break;
804 default:
805 return "CMD_UNKNOWN";
806 break;
807 }
808 }
809
810 /**
811 * Print one OpenULINK command to stdout.
812 *
813 * @param ulink_cmd pointer to OpenULINK command.
814 */
815 void ulink_print_command(ulink_cmd_t *ulink_cmd)
816 {
817 int i;
818
819 printf(" %-22s | OUT size = %i, bytes = 0x", ulink_cmd_id_string(ulink_cmd->id),
820 ulink_cmd->payload_out_size);
821
822 for (i = 0; i < ulink_cmd->payload_out_size; i++) {
823 printf("%02X ", ulink_cmd->payload_out[i]);
824 }
825 printf("\n | IN size = %i\n", ulink_cmd->payload_in_size);
826 }
827
828 /**
829 * Print the OpenULINK command queue to stdout.
830 *
831 * @param device pointer to struct ulink identifying ULINK driver instance.
832 */
833 void ulink_print_queue(struct ulink *device)
834 {
835 ulink_cmd_t *current;
836
837 printf("OpenULINK command queue:\n");
838
839 for (current = device->queue_start; current; current = current->next) {
840 ulink_print_command(current);
841 }
842 }
843
844 #endif /* _DEBUG_JTAG_IO_ */
845
846 /**
847 * Perform JTAG scan
848 *
849 * Creates and appends a JTAG scan command to the OpenULINK command queue.
850 * A JTAG scan consists of three steps:
851 * - Move to the desired SHIFT state, depending on scan type (IR/DR scan).
852 * - Shift TDI data into the JTAG chain, optionally reading the TDO pin.
853 * - Move to the desired end state.
854 *
855 * @param device pointer to struct ulink identifying ULINK driver instance.
856 * @param scan_type the type of the scan (IN, OUT, IO (bidirectional)).
857 * @param scan_size_bits number of bits to shift into the JTAG chain.
858 * @param tdi pointer to array containing TDI data.
859 * @param tdo_start pointer to first element of array where TDO data shall be
860 * stored. See #ulink_cmd for details.
861 * @param tdo pointer to array where TDO data shall be stored
862 * @param tms_count_start number of TMS state transitions to perform BEFORE
863 * shifting data into the JTAG chain.
864 * @param tms_sequence_start sequence of TMS state transitions that will be
865 * performed BEFORE shifting data into the JTAG chain.
866 * @param tms_count_end number of TMS state transitions to perform AFTER
867 * shifting data into the JTAG chain.
868 * @param tms_sequence_end sequence of TMS state transitions that will be
869 * performed AFTER shifting data into the JTAG chain.
870 * @param origin pointer to OpenOCD command that generated this scan command.
871 * @param postprocess whether this command needs to be post-processed after
872 * execution.
873 * @return on success: ERROR_OK
874 * @return on failure: ERROR_FAIL
875 */
876 int ulink_append_scan_cmd(struct ulink *device, enum scan_type scan_type,
877 int scan_size_bits, uint8_t *tdi, uint8_t *tdo_start, uint8_t *tdo,
878 uint8_t tms_count_start, uint8_t tms_sequence_start, uint8_t tms_count_end,
879 uint8_t tms_sequence_end, struct jtag_command *origin, bool postprocess)
880 {
881 ulink_cmd_t *cmd = calloc(1, sizeof(ulink_cmd_t));
882 int ret, i, scan_size_bytes;
883 uint8_t bits_last_byte;
884
885 if (cmd == NULL) {
886 return ERROR_FAIL;
887 }
888
889 /* Check size of command. USB buffer can hold 64 bytes, 1 byte is command ID,
890 * 5 bytes are setup data -> 58 remaining payload bytes for TDI data */
891 if (scan_size_bits > (58 * 8)) {
892 LOG_ERROR("BUG: Tried to create CMD_SCAN_IO OpenULINK command with too"
893 " large payload");
894 return ERROR_FAIL;
895 }
896
897 scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
898
899 bits_last_byte = scan_size_bits % 8;
900 if (bits_last_byte == 0) {
901 bits_last_byte = 8;
902 }
903
904 /* Allocate out_payload depending on scan type */
905 // TODO: set command ID depending on interface speed settings (slow scan)
906 switch (scan_type) {
907 case SCAN_IN:
908 cmd->id = CMD_SCAN_IN;
909 ret = ulink_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT);
910 break;
911 case SCAN_OUT:
912 cmd->id = CMD_SCAN_OUT;
913 ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
914 break;
915 case SCAN_IO:
916 cmd->id = CMD_SCAN_IO;
917 ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
918 break;
919 default:
920 LOG_ERROR("BUG: ulink_append_scan_cmd() encountered an unknown scan type");
921 ret = ERROR_FAIL;
922 }
923
924 if (ret != ERROR_OK) {
925 return ret;
926 }
927
928 /* Build payload_out that is common to all scan types */
929 cmd->payload_out[0] = scan_size_bytes & 0xFF;
930 cmd->payload_out[1] = bits_last_byte & 0xFF;
931 cmd->payload_out[2] = ((tms_count_start & 0x0F) << 4) | (tms_count_end & 0x0F);
932 cmd->payload_out[3] = tms_sequence_start;
933 cmd->payload_out[4] = tms_sequence_end;
934
935 /* Setup payload_out for types with OUT transfer */
936 if ((scan_type == SCAN_OUT) || (scan_type == SCAN_IO)) {
937 for (i = 0; i < scan_size_bytes; i++) {
938 cmd->payload_out[i + 5] = tdi[i];
939 }
940 }
941
942 /* Setup payload_in pointers for types with IN transfer */
943 if ((scan_type == SCAN_IN) || (scan_type == SCAN_IO)) {
944 cmd->payload_in_start = tdo_start;
945 cmd->payload_in = tdo;
946 cmd->payload_in_size = scan_size_bytes;
947 }
948
949 cmd->needs_postprocessing = postprocess;
950 cmd->cmd_origin = origin;
951
952 /* For scan commands, we free payload_in_start only when the command is
953 * the last in a series of split commands or a stand-alone command */
954 cmd->free_payload_in_start = postprocess;
955
956 return ulink_append_queue(device, cmd);
957 }
958
959 /**
960 * Perform TAP state transitions
961 *
962 * @param device pointer to struct ulink identifying ULINK driver instance.
963 * @param count defines the number of TCK clock cycles generated (up to 8).
964 * @param sequence defines the TMS pin levels for each state transition. The
965 * Least-Significant Bit is read first.
966 * @return on success: ERROR_OK
967 * @return on failure: ERROR_FAIL
968 */
969 int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count,
970 uint8_t sequence)
971 {
972 ulink_cmd_t *cmd = calloc(1, sizeof(ulink_cmd_t));
973 int ret;
974
975 if (cmd == NULL) {
976 return ERROR_FAIL;
977 }
978
979 cmd->id = CMD_CLOCK_TMS;
980
981 /* CMD_CLOCK_TMS has two OUT payload bytes and zero IN payload bytes */
982 ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
983 if (ret != ERROR_OK) {
984 return ret;
985 }
986
987 cmd->payload_out[0] = count;
988 cmd->payload_out[1] = sequence;
989
990 return ulink_append_queue(device, cmd);
991 }
992
993 /**
994 * Generate a defined amount of TCK clock cycles
995 *
996 * All other JTAG signals are left unchanged.
997 *
998 * @param device pointer to struct ulink identifying ULINK driver instance.
999 * @param count the number of TCK clock cycles to generate.
1000 * @return on success: ERROR_OK
1001 * @return on failure: ERROR_FAIL
1002 */
1003 int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count)
1004 {
1005 ulink_cmd_t *cmd = calloc(1, sizeof(ulink_cmd_t));
1006 int ret;
1007
1008 if (cmd == NULL) {
1009 return ERROR_FAIL;
1010 }
1011
1012 cmd->id = CMD_CLOCK_TCK;
1013
1014 /* CMD_CLOCK_TCK has two OUT payload bytes and zero IN payload bytes */
1015 ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
1016 if (ret != ERROR_OK) {
1017 return ret;
1018 }
1019
1020 cmd->payload_out[0] = count & 0xff;
1021 cmd->payload_out[1] = (count >> 8) & 0xff;
1022
1023 return ulink_append_queue(device, cmd);
1024 }
1025
1026 /**
1027 * Read JTAG signals.
1028 *
1029 * @param device pointer to struct ulink identifying ULINK driver instance.
1030 * @return on success: ERROR_OK
1031 * @return on failure: ERROR_FAIL
1032 */
1033 int ulink_append_get_signals_cmd(struct ulink *device)
1034 {
1035 ulink_cmd_t *cmd = calloc(1, sizeof(ulink_cmd_t));
1036 int ret;
1037
1038 if (cmd == NULL) {
1039 return ERROR_FAIL;
1040 }
1041
1042 cmd->id = CMD_GET_SIGNALS;
1043 cmd->needs_postprocessing = true;
1044
1045 /* CMD_GET_SIGNALS has two IN payload bytes */
1046 ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_IN);
1047
1048 if (ret != ERROR_OK) {
1049 return ret;
1050 }
1051
1052 return ulink_append_queue(device, cmd);
1053 }
1054
1055 /**
1056 * Arbitrarily set JTAG output signals.
1057 *
1058 * @param device pointer to struct ulink identifying ULINK driver instance.
1059 * @param low defines which signals will be de-asserted. Each bit corresponds
1060 * to a JTAG signal:
1061 * - SIGNAL_TDI
1062 * - SIGNAL_TMS
1063 * - SIGNAL_TCK
1064 * - SIGNAL_TRST
1065 * - SIGNAL_BRKIN
1066 * - SIGNAL_RESET
1067 * - SIGNAL_OCDSE
1068 * @param high defines which signals will be asserted.
1069 * @return on success: ERROR_OK
1070 * @return on failure: ERROR_FAIL
1071 */
1072 int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low,
1073 uint8_t high)
1074 {
1075 ulink_cmd_t *cmd = calloc(1, sizeof(ulink_cmd_t));
1076 int ret;
1077
1078 if (cmd == NULL) {
1079 return ERROR_FAIL;
1080 }
1081
1082 cmd->id = CMD_SET_SIGNALS;
1083
1084 /* CMD_SET_SIGNALS has two OUT payload bytes and zero IN payload bytes */
1085 ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
1086
1087 if (ret != ERROR_OK) {
1088 return ret;
1089 }
1090
1091 cmd->payload_out[0] = low;
1092 cmd->payload_out[1] = high;
1093
1094 return ulink_append_queue(device, cmd);;
1095 }
1096
1097 /**
1098 * Sleep for a pre-defined number of microseconds
1099 *
1100 * @param device pointer to struct ulink identifying ULINK driver instance.
1101 * @param us the number microseconds to sleep.
1102 * @return on success: ERROR_OK
1103 * @return on failure: ERROR_FAIL
1104 */
1105 int ulink_append_sleep_cmd(struct ulink *device, uint32_t us)
1106 {
1107 ulink_cmd_t *cmd = calloc(1, sizeof(ulink_cmd_t));
1108 int ret;
1109
1110 if (cmd == NULL) {
1111 return ERROR_FAIL;
1112 }
1113
1114 cmd->id = CMD_SLEEP_US;
1115
1116 /* CMD_SLEEP_US has two OUT payload bytes and zero IN payload bytes */
1117 ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
1118
1119 if (ret != ERROR_OK) {
1120 return ret;
1121 }
1122
1123 cmd->payload_out[0] = us & 0x00ff;
1124 cmd->payload_out[1] = (us >> 8) & 0x00ff;
1125
1126 return ulink_append_queue(device, cmd);
1127 }
1128
1129 /**
1130 * Set TCK delay counters
1131 *
1132 * @param device pointer to struct ulink identifying ULINK driver instance.
1133 * @param delay_scan delay count top value in jtag_slow_scan() functions
1134 * @param delay_tck delay count top value in jtag_clock_tck() function
1135 * @param delay_tms delay count top value in jtag_slow_clock_tms() function
1136 * @return on success: ERROR_OK
1137 * @return on failure: ERROR_FAIL
1138 */
1139 int ulink_append_configure_tck_cmd(struct ulink *device, uint8_t delay_scan,
1140 uint8_t delay_tck, uint8_t delay_tms)
1141 {
1142 ulink_cmd_t *cmd = calloc(1, sizeof(ulink_cmd_t));
1143 int ret;
1144
1145 if (cmd == NULL) {
1146 return ERROR_FAIL;
1147 }
1148
1149 cmd->id = CMD_CONFIGURE_TCK_FREQ;
1150
1151 /* CMD_CONFIGURE_TCK_FREQ has three OUT payload bytes and zero
1152 * IN payload bytes */
1153 ret = ulink_allocate_payload(cmd, 3, PAYLOAD_DIRECTION_OUT);
1154 if (ret != ERROR_OK) {
1155 return ret;
1156 }
1157
1158 cmd->payload_out[0] = delay_scan;
1159 cmd->payload_out[1] = delay_tck;
1160 cmd->payload_out[2] = delay_tms;
1161
1162 return ulink_append_queue(device, cmd);
1163 }
1164
1165 /**
1166 * Turn on/off ULINK LEDs.
1167 *
1168 * @param device pointer to struct ulink identifying ULINK driver instance.
1169 * @param led_state which LED(s) to turn on or off. The following bits
1170 * influence the LEDS:
1171 * - Bit 0: Turn COM LED on
1172 * - Bit 1: Turn RUN LED on
1173 * - Bit 2: Turn COM LED off
1174 * - Bit 3: Turn RUN LED off
1175 * If both the on-bit and the off-bit for the same LED is set, the LED is
1176 * turned off.
1177 * @return on success: ERROR_OK
1178 * @return on failure: ERROR_FAIL
1179 */
1180 int ulink_append_led_cmd(struct ulink *device, uint8_t led_state)
1181 {
1182 ulink_cmd_t *cmd = calloc(1, sizeof(ulink_cmd_t));
1183 int ret;
1184
1185 if (cmd == NULL) {
1186 return ERROR_FAIL;
1187 }
1188
1189 cmd->id = CMD_SET_LEDS;
1190
1191 /* CMD_SET_LEDS has one OUT payload byte and zero IN payload bytes */
1192 ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT);
1193 if (ret != ERROR_OK) {
1194 return ret;
1195 }
1196
1197 cmd->payload_out[0] = led_state;
1198
1199 return ulink_append_queue(device, cmd);
1200 }
1201
1202 /**
1203 * Test command. Used to check if the ULINK device is ready to accept new
1204 * commands.
1205 *
1206 * @param device pointer to struct ulink identifying ULINK driver instance.
1207 * @return on success: ERROR_OK
1208 * @return on failure: ERROR_FAIL
1209 */
1210 int ulink_append_test_cmd(struct ulink *device)
1211 {
1212 ulink_cmd_t *cmd = calloc(1, sizeof(ulink_cmd_t));
1213 int ret;
1214
1215 if (cmd == NULL) {
1216 return ERROR_FAIL;
1217 }
1218
1219 cmd->id = CMD_TEST;
1220
1221 /* CMD_TEST has one OUT payload byte and zero IN payload bytes */
1222 ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT);
1223 if (ret != ERROR_OK) {
1224 return ret;
1225 }
1226
1227 cmd->payload_out[0] = 0xAA;
1228
1229 return ulink_append_queue(device, cmd);
1230 }
1231
1232 /******************* Interface between OpenULINK and OpenOCD ******************/
1233
1234 /**
1235 * Perform a scan operation on a JTAG register.
1236 *
1237 * @param device pointer to struct ulink identifying ULINK driver instance.
1238 * @param cmd pointer to the command that shall be executed.
1239 * @return on success: ERROR_OK
1240 * @return on failure: ERROR_FAIL
1241 */
1242 int ulink_queue_scan(struct ulink *device, struct jtag_command *cmd)
1243 {
1244 uint32_t scan_size_bits, scan_size_bytes, bits_last_scan;
1245 uint32_t scans_max_payload, bytecount;
1246 uint8_t *tdi_buffer_start = NULL, *tdi_buffer = NULL;
1247 uint8_t *tdo_buffer_start = NULL, *tdo_buffer = NULL;
1248
1249 uint8_t first_tms_count, first_tms_sequence;
1250 uint8_t last_tms_count, last_tms_sequence;
1251
1252 uint8_t tms_count_pause, tms_sequence_pause;
1253 uint8_t tms_count_resume, tms_sequence_resume;
1254
1255 uint8_t tms_count_start, tms_sequence_start;
1256 uint8_t tms_count_end, tms_sequence_end;
1257
1258 enum scan_type type;
1259 int ret;
1260
1261 /* Determine scan size */
1262 scan_size_bits = jtag_scan_size(cmd->cmd.scan);
1263 scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
1264
1265 /* Determine scan type (IN/OUT/IO) */
1266 type = jtag_scan_type(cmd->cmd.scan);
1267
1268 /* Determine number of scan commands */
1269 scans_max_payload = scan_size_bytes / 58;
1270
1271 /* Determine size of last shift command */
1272 bits_last_scan = scan_size_bits - (scans_max_payload * 58 * 8);
1273
1274 /* Allocate TDO buffer if required */
1275 if ((type == SCAN_IN) || (type == SCAN_IO)) {
1276 tdo_buffer_start = calloc(sizeof(uint8_t), scan_size_bytes);
1277
1278 if (tdo_buffer_start == NULL) {
1279 return ERROR_FAIL;
1280 }
1281
1282 tdo_buffer = tdo_buffer_start;
1283 }
1284
1285 /* Fill TDI buffer if required */
1286 if ((type == SCAN_OUT) || (type == SCAN_IO)) {
1287 jtag_build_buffer(cmd->cmd.scan, &tdi_buffer_start);
1288 tdi_buffer = tdi_buffer_start;
1289 }
1290
1291 /* Get TAP state transitions */
1292 if (cmd->cmd.scan->ir_scan) {
1293 ulink_set_end_state(TAP_IRSHIFT);
1294 first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1295 first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1296
1297 tap_set_state(TAP_IRSHIFT);
1298 tap_set_end_state(cmd->cmd.scan->end_state);
1299 last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1300 last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1301
1302 /* TAP state transitions for split scans */
1303 tms_count_pause = tap_get_tms_path_len(TAP_IRSHIFT, TAP_IRPAUSE);
1304 tms_sequence_pause = tap_get_tms_path(TAP_IRSHIFT, TAP_IRPAUSE);
1305 tms_count_resume = tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRSHIFT);
1306 tms_sequence_resume = tap_get_tms_path(TAP_IRPAUSE, TAP_IRSHIFT);
1307 }
1308 else {
1309 ulink_set_end_state(TAP_DRSHIFT);
1310 first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1311 first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1312
1313 tap_set_state(TAP_DRSHIFT);
1314 tap_set_end_state(cmd->cmd.scan->end_state);
1315 last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1316 last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1317
1318 /* TAP state transitions for split scans */
1319 tms_count_pause = tap_get_tms_path_len(TAP_DRSHIFT, TAP_DRPAUSE);
1320 tms_sequence_pause = tap_get_tms_path(TAP_DRSHIFT, TAP_DRPAUSE);
1321 tms_count_resume = tap_get_tms_path_len(TAP_DRPAUSE, TAP_DRSHIFT);
1322 tms_sequence_resume = tap_get_tms_path(TAP_DRPAUSE, TAP_DRSHIFT);
1323 }
1324
1325 /* Generate scan commands with full payload */
1326 bytecount = scan_size_bytes;
1327 while (bytecount > 0) {
1328 if (bytecount == scan_size_bytes) {
1329 /* This is the first scan */
1330 tms_count_start = first_tms_count;
1331 tms_sequence_start = first_tms_sequence;
1332 }
1333 else {
1334 /* Resume from previous scan */
1335 tms_count_start = tms_count_resume;
1336 tms_sequence_start = tms_sequence_resume;
1337 }
1338
1339 if (bytecount > 58) { /* Full scan, at least one scan will follow */
1340 tms_count_end = tms_count_pause;
1341 tms_sequence_end = tms_sequence_pause;
1342
1343 ret = ulink_append_scan_cmd(device, type, 58 * 8, tdi_buffer,
1344 tdo_buffer_start, tdo_buffer, tms_count_start, tms_sequence_start,
1345 tms_count_end, tms_sequence_end, cmd, false);
1346
1347 bytecount -= 58;
1348
1349 /* Update TDI and TDO buffer pointers */
1350 if (tdi_buffer_start != NULL) {
1351 tdi_buffer += 58;
1352 }
1353 if (tdo_buffer_start != NULL) {
1354 tdo_buffer += 58;
1355 }
1356 }
1357 else if (bytecount == 58) { /* Full scan, no further scans */
1358 tms_count_end = last_tms_count;
1359 tms_sequence_end = last_tms_sequence;
1360
1361 ret = ulink_append_scan_cmd(device, type, 58 * 8, tdi_buffer,
1362 tdo_buffer_start, tdo_buffer, tms_count_start, tms_sequence_start,
1363 tms_count_end, tms_sequence_end, cmd, true);
1364
1365 bytecount = 0;
1366 }
1367 else { /* Scan with less than maximum payload, no further scans */
1368 tms_count_end = last_tms_count;
1369 tms_sequence_end = last_tms_sequence;
1370
1371 ret = ulink_append_scan_cmd(device, type, bits_last_scan, tdi_buffer,
1372 tdo_buffer_start, tdo_buffer, tms_count_start, tms_sequence_start,
1373 tms_count_end, tms_sequence_end, cmd, true);
1374
1375 bytecount = 0;
1376 }
1377
1378 if (ret != ERROR_OK) {
1379 free(tdi_buffer_start);
1380 return ret;
1381 }
1382 }
1383
1384 free(tdi_buffer_start);
1385
1386 /* Set current state to the end state requested by the command */
1387 tap_set_state(cmd->cmd.scan->end_state);
1388
1389 return ERROR_OK;
1390 }
1391
1392 /**
1393 * Sets the end state follower (see interface.h) if \a endstate is a stable
1394 * state.
1395 *
1396 * @param endstate the state the end state follower should be set to.
1397 */
1398 static void ulink_set_end_state(tap_state_t endstate)
1399 {
1400 if (tap_is_state_stable(endstate)) {
1401 tap_set_end_state(endstate);
1402 }
1403 else {
1404 LOG_ERROR("BUG: %s is not a valid end state", tap_state_name(endstate));
1405 exit( EXIT_FAILURE);
1406 }
1407 }
1408
1409 /**
1410 * Move from the current TAP state to the current TAP end state.
1411 *
1412 * @param device pointer to struct ulink identifying ULINK driver instance.
1413 * @return on success: ERROR_OK
1414 * @return on failure: ERROR_FAIL
1415 */
1416 int ulink_queue_statemove(struct ulink *device)
1417 {
1418 uint8_t tms_sequence, tms_count;
1419 int ret;
1420
1421 if (tap_get_state() == tap_get_end_state()) {
1422 /* Do nothing if we are already there */
1423 return ERROR_OK;
1424 }
1425
1426 tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1427 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1428
1429 ret = ulink_append_clock_tms_cmd(device, tms_count, tms_sequence);
1430
1431 if (ret == ERROR_OK) {
1432 tap_set_state(tap_get_end_state());
1433 }
1434
1435 return ret;
1436 }
1437
1438 /**
1439 * Execute a JTAG_RESET command
1440 *
1441 * @param cmd pointer to the command that shall be executed.
1442 * @return on success: ERROR_OK
1443 * @return on failure: ERROR_FAIL
1444 */
1445 int ulink_queue_reset(struct ulink *device, struct jtag_command *cmd)
1446 {
1447 uint8_t low = 0, high = 0;
1448
1449 if (cmd->cmd.reset->trst) {
1450 tap_set_state(TAP_RESET);
1451 high |= SIGNAL_TRST;
1452 }
1453 else {
1454 low |= SIGNAL_TRST;
1455 }
1456
1457 if (cmd->cmd.reset->srst) {
1458 high |= SIGNAL_RESET;
1459 }
1460 else {
1461 low |= SIGNAL_RESET;
1462 }
1463
1464 return ulink_append_set_signals_cmd(device, low, high);
1465 }
1466
1467 /**
1468 * Run Test.
1469 *
1470 * Generate TCK clock cycles while remaining
1471 * in the Run-Test/Idle state.
1472 *
1473 * @param device pointer to struct ulink identifying ULINK driver instance.
1474 * @param cmd pointer to the command that shall be executed.
1475 * @return on success: ERROR_OK
1476 * @return on failure: ERROR_FAIL
1477 */
1478 int ulink_queue_runtest(struct ulink *device, struct jtag_command *cmd)
1479 {
1480 int ret;
1481
1482 /* Only perform statemove if the TAP currently isn't in the TAP_IDLE state */
1483 if (tap_get_state() != TAP_IDLE) {
1484 ulink_set_end_state(TAP_IDLE);
1485 ulink_queue_statemove(device);
1486 }
1487
1488 /* Generate the clock cycles */
1489 ret = ulink_append_clock_tck_cmd(device, cmd->cmd.runtest->num_cycles);
1490 if (ret != ERROR_OK) {
1491 return ret;
1492 }
1493
1494 /* Move to end state specified in command */
1495 if (cmd->cmd.runtest->end_state != tap_get_state()) {
1496 tap_set_end_state(cmd->cmd.runtest->end_state);
1497 ulink_queue_statemove(device);
1498 }
1499
1500 return ERROR_OK;
1501 }
1502
1503 /**
1504 * Move the TAP into the Test Logic Reset state.
1505 *
1506 * @param device pointer to struct ulink identifying ULINK driver instance.
1507 * @param cmd pointer to the command that shall be executed.
1508 * @return on success: ERROR_OK
1509 * @return on failure: ERROR_FAIL
1510 */
1511 int ulink_queue_tlr_reset(struct ulink *device, struct jtag_command *cmd)
1512 {
1513 int ret;
1514
1515 ret = ulink_append_clock_tms_cmd(device, 5, 0xff);
1516
1517 if (ret == ERROR_OK) {
1518 tap_set_state(TAP_RESET);
1519 }
1520
1521 return ret;
1522 }
1523
1524 /**
1525 * Move to one TAP state or several states in succession.
1526 *
1527 * @param device pointer to struct ulink identifying ULINK driver instance.
1528 * @param cmd pointer to the command that shall be executed.
1529 * @return on success: ERROR_OK
1530 * @return on failure: ERROR_FAIL
1531 */
1532 int ulink_queue_pathmove(struct ulink *device, struct jtag_command *cmd)
1533 {
1534 // TODO: Implement this!
1535 return ERROR_OK;
1536 }
1537
1538 /**
1539 * Sleep for a specific amount of time.
1540 *
1541 * @param device pointer to struct ulink identifying ULINK driver instance.
1542 * @param cmd pointer to the command that shall be executed.
1543 * @return on success: ERROR_OK
1544 * @return on failure: ERROR_FAIL
1545 */
1546 int ulink_queue_sleep(struct ulink *device, struct jtag_command *cmd)
1547 {
1548 /* IMPORTANT! Due to the time offset in command execution introduced by
1549 * command queueing, this needs to be implemented in the ULINK device */
1550 return ulink_append_sleep_cmd(device, cmd->cmd.sleep->us);
1551 }
1552
1553 /**
1554 * Post-process JTAG_SCAN command
1555 *
1556 * @param ulink_cmd pointer to OpenULINK command that shall be processed.
1557 * @return on success: ERROR_OK
1558 * @return on failure: ERROR_FAIL
1559 */
1560 int ulink_post_process_scan(ulink_cmd_t *ulink_cmd)
1561 {
1562 struct jtag_command *cmd = ulink_cmd->cmd_origin;
1563 int ret;
1564
1565 switch (jtag_scan_type(cmd->cmd.scan)) {
1566 case SCAN_IN:
1567 case SCAN_IO:
1568 ret = jtag_read_buffer(ulink_cmd->payload_in_start, cmd->cmd.scan);
1569 break;
1570 case SCAN_OUT:
1571 /* Nothing to do for OUT scans */
1572 ret = ERROR_OK;
1573 break;
1574 default:
1575 LOG_ERROR("BUG: ulink_post_process_scan() encountered an unknown"
1576 " JTAG scan type");
1577 ret = ERROR_FAIL;
1578 break;
1579 }
1580
1581 return ret;
1582 }
1583
1584 /**
1585 * Perform post-processing of commands after OpenULINK queue has been executed.
1586 *
1587 * @param device pointer to struct ulink identifying ULINK driver instance.
1588 * @return on success: ERROR_OK
1589 * @return on failure: ERROR_FAIL
1590 */
1591 int ulink_post_process_queue(struct ulink *device)
1592 {
1593 ulink_cmd_t *current;
1594 struct jtag_command *openocd_cmd;
1595 int ret;
1596
1597 current = device->queue_start;
1598
1599 while (current != NULL) {
1600 openocd_cmd = current->cmd_origin;
1601
1602 /* Check if a corresponding OpenOCD command is stored for this
1603 * OpenULINK command */
1604 if ((current->needs_postprocessing == true) && (openocd_cmd != NULL)) {
1605 switch (openocd_cmd->type) {
1606 case JTAG_SCAN:
1607 ret = ulink_post_process_scan(current);
1608 break;
1609 case JTAG_RUNTEST:
1610 case JTAG_TLR_RESET:
1611 case JTAG_PATHMOVE:
1612 case JTAG_RESET:
1613 case JTAG_SLEEP:
1614 /* Nothing to do for these commands */
1615 ret = ERROR_OK;
1616 break;
1617 default:
1618 ret = ERROR_FAIL;
1619 LOG_ERROR("BUG: ulink_post_process_queue() encountered unknown JTAG "
1620 "command type");
1621 }
1622
1623 if (ret != ERROR_OK) {
1624 return ret;
1625 }
1626 }
1627
1628 current = current->next;
1629 }
1630
1631 return ERROR_OK;
1632 }
1633
1634 /**************************** JTAG driver functions ***************************/
1635
1636 /**
1637 * Executes the JTAG Command Queue.
1638 *
1639 * This is done in three stages: First, all OpenOCD commands are processed into
1640 * queued OpenULINK commands. Next, the OpenULINK command queue is sent to the
1641 * ULINK device and data received from the ULINK device is cached. Finally,
1642 * the post-processing function writes back data to the corresponding OpenOCD
1643 * commands.
1644 *
1645 * @return on success: ERROR_OK
1646 * @return on failure: ERROR_FAIL
1647 */
1648 static int ulink_execute_queue(void)
1649 {
1650 struct jtag_command *cmd = jtag_command_queue;
1651 int ret;
1652
1653 while (cmd) {
1654 switch (cmd->type) {
1655 case JTAG_SCAN:
1656 ret = ulink_queue_scan(ulink_handle, cmd);
1657 break;
1658 case JTAG_RUNTEST:
1659 ret = ulink_queue_runtest(ulink_handle, cmd);
1660 break;
1661 case JTAG_TLR_RESET:
1662 ret = ulink_queue_tlr_reset(ulink_handle, cmd);
1663 break;
1664 case JTAG_PATHMOVE:
1665 ret = ulink_queue_pathmove(ulink_handle, cmd);
1666 break;
1667 case JTAG_RESET:
1668 ret = ulink_queue_reset(ulink_handle, cmd);
1669 break;
1670 case JTAG_SLEEP:
1671 ret = ulink_queue_sleep(ulink_handle, cmd);
1672 break;
1673 default:
1674 ret = ERROR_FAIL;
1675 LOG_ERROR("BUG: encountered unknown JTAG command type");
1676 }
1677
1678 cmd = cmd->next;
1679 }
1680
1681 if (ulink_handle->commands_in_queue > 0) {
1682 ret = ulink_execute_queued_commands(ulink_handle, USB_TIMEOUT);
1683 if (ret != ERROR_OK) {
1684 return ret;
1685 }
1686
1687 ret = ulink_post_process_queue(ulink_handle);
1688 if (ret != ERROR_OK) {
1689 return ret;
1690 }
1691
1692 ulink_clear_queue(ulink_handle);
1693 }
1694
1695 return ERROR_OK;
1696 }
1697
1698 /**
1699 * Set the TCK frequency of the ULINK adapter.
1700 *
1701 * @param khz ???
1702 * @param jtag_speed ???
1703 * @return on success: ERROR_OK
1704 * @return on failure: ERROR_FAIL
1705 */
1706 static int ulink_khz(int khz, int *jtag_speed)
1707 {
1708 if (khz == 0) {
1709 LOG_ERROR("RCLK not supported");
1710 return ERROR_FAIL;
1711 }
1712
1713 LOG_INFO("ulink_khz: %i kHz", khz);
1714
1715 /* ULINK maximum TCK frequency is ~ 150 kHz */
1716 if (khz > 150) {
1717 return ERROR_FAIL;
1718 }
1719
1720 *jtag_speed = 0;
1721
1722 return ERROR_OK;
1723 }
1724
1725 /**
1726 * Set the TCK frequency of the ULINK adapter.
1727 *
1728 * @param speed ???
1729 * @return on success: ERROR_OK
1730 * @return on failure: ERROR_FAIL
1731 */
1732 static int ulink_speed(int speed)
1733 {
1734 return ERROR_OK;
1735 }
1736
1737 /**
1738 *
1739 */
1740 static int ulink_speed_div(int speed, int *khz)
1741 {
1742 LOG_INFO("ulink_speed_div: %i", speed);
1743
1744 switch (speed) {
1745 case 0:
1746 *khz = 150;
1747 break;
1748 case 1:
1749 *khz = 100;
1750 break;
1751 }
1752
1753 return ERROR_OK;
1754 }
1755
1756 /**
1757 * Initiates the firmware download to the ULINK adapter and prepares
1758 * the USB handle.
1759 *
1760 * @return on success: ERROR_OK
1761 * @return on failure: ERROR_FAIL
1762 */
1763 static int ulink_init(void)
1764 {
1765 int ret;
1766 char str_manufacturer[20];
1767 bool download_firmware = false;
1768 uint8_t *dummy;
1769 uint8_t input_signals, output_signals;
1770
1771 ulink_handle = calloc(1, sizeof(struct ulink));
1772 if (ulink_handle == NULL) {
1773 return ERROR_FAIL;
1774 }
1775
1776 usb_init();
1777
1778 ret = ulink_usb_open(&ulink_handle);
1779 if (ret != ERROR_OK) {
1780 LOG_ERROR("Could not open ULINK device");
1781 return ret;
1782 }
1783
1784 /* Get String Descriptor to determine if firmware needs to be loaded */
1785 ret = usb_get_string_simple(ulink_handle->usb_handle, 1, str_manufacturer, 20);
1786 if (ret < 0) {
1787 /* Could not get descriptor -> Unconfigured or original Keil firmware */
1788 download_firmware = true;
1789 }
1790 else {
1791 /* We got a String Descriptor, check if it is the correct one */
1792 if (strncmp(str_manufacturer, "OpenULINK", 9) != 0) {
1793 download_firmware = true;
1794 }
1795 }
1796
1797 if (download_firmware == true) {
1798 LOG_INFO("Loading OpenULINK firmware. This is reversible by power-cycling"
1799 " ULINK device.");
1800 ret = ulink_load_firmware_and_renumerate(&ulink_handle,
1801 ULINK_FIRMWARE_FILE, ULINK_RENUMERATION_DELAY);
1802 if (ret != ERROR_OK) {
1803 LOG_ERROR("Could not download firmware and re-numerate ULINK");
1804 return ret;
1805 }
1806 }
1807 else {
1808 LOG_INFO("ULINK device is already running OpenULINK firmware");
1809 }
1810
1811 /* Initialize OpenULINK command queue */
1812 ulink_clear_queue(ulink_handle);
1813
1814 /* Issue one test command with short timeout */
1815 ret = ulink_append_test_cmd(ulink_handle);
1816 if (ret != ERROR_OK) {
1817 return ret;
1818 }
1819
1820 ret = ulink_execute_queued_commands(ulink_handle, 200);
1821 if (ret != ERROR_OK) {
1822 /* Sending test command failed. The ULINK device may be forever waiting for
1823 * the host to fetch an USB Bulk IN packet (e. g. OpenOCD crashed or was
1824 * shut down by the user via Ctrl-C. Try to retrieve this Bulk IN packet. */
1825 dummy = calloc(64, sizeof(uint8_t));
1826
1827 ret = usb_bulk_read(ulink_handle->usb_handle, (2 | USB_ENDPOINT_IN),
1828 (char *)dummy, 64, 200);
1829
1830 free(dummy);
1831
1832 if (ret < 0) {
1833 /* Bulk IN transfer failed -> unrecoverable error condition */
1834 LOG_ERROR("Cannot communicate with ULINK device. Disconnect ULINK from "
1835 "the USB port and re-connect, then re-run OpenOCD");
1836 return ERROR_FAIL;
1837 }
1838 #ifdef _DEBUG_USB_COMMS_
1839 else {
1840 /* Successfully received Bulk IN packet -> continue */
1841 LOG_INFO("Recovered from lost Bulk IN packet");
1842 }
1843 #endif
1844 }
1845 ulink_clear_queue(ulink_handle);
1846
1847 ulink_append_get_signals_cmd(ulink_handle);
1848 ulink_execute_queued_commands(ulink_handle, 200);
1849
1850 /* Post-process the single CMD_GET_SIGNALS command */
1851 input_signals = ulink_handle->queue_start->payload_in[0];
1852 output_signals = ulink_handle->queue_start->payload_in[1];
1853
1854 ulink_print_signal_states(input_signals, output_signals);
1855
1856 ulink_clear_queue(ulink_handle);
1857
1858 return ERROR_OK;
1859 }
1860
1861 /**
1862 * Closes the USB handle for the ULINK device.
1863 *
1864 * @return on success: ERROR_OK
1865 * @return on failure: ERROR_FAIL
1866 */
1867 static int ulink_quit(void)
1868 {
1869 int ret;
1870
1871 ret = ulink_usb_close(&ulink_handle);
1872 free(ulink_handle);
1873
1874 return ret;
1875 }
1876
1877 /*************************** Command Registration **************************/
1878
1879 struct jtag_interface ulink_interface = {
1880 .name = "ulink",
1881 .transports = jtag_only,
1882
1883 .execute_queue = ulink_execute_queue,
1884 .khz = ulink_khz,
1885 .speed = ulink_speed,
1886 .speed_div = ulink_speed_div,
1887
1888 .init = ulink_init,
1889 .quit = ulink_quit
1890 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)