jtag/drivers: send bitstream size to firmware via libusb
[openocd.git] / src / jtag / drivers / angie.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
3 File : angie.c *
4 Contents : OpenOCD driver code for NanoXplore USB-JTAG ANGIE *
5 adapter hardware. *
6 Based on openULINK driver code by: Martin Schmoelzer. *
7 Copyright 2023, Ahmed Errached BOUDJELIDA, NanoXplore SAS. *
8 <aboudjelida@nanoxplore.com> *
9 <ahmederrachedbjld@gmail.com> *
10 ***************************************************************************/
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <math.h>
19 #include "helper/system.h"
20 #include <helper/types.h>
21 #include <jtag/interface.h>
22 #include <jtag/commands.h>
23 #include <target/image.h>
24 #include <libusb.h>
25 #include "libusb_helper.h"
26 #include "angie/include/msgtypes.h"
27
28 /** USB Vendor ID of ANGIE device in unconfigured state (no firmware loaded
29 * yet) or with its firmware. */
30 #define ANGIE_VID 0x584e
31
32 /** USB Product ID of ANGIE device in unconfigured state (no firmware loaded
33 * yet) or with its firmware. */
34 #define ANGIE_PID 0x424e
35 #define ANGIE_PID_2 0x4255
36 #define ANGIE_PID_3 0x4355
37 #define ANGIE_PID_4 0x4a55
38
39 /** Address of EZ-USB ANGIE CPU Control & Status register. This register can be
40 * written by issuing a Control EP0 vendor request. */
41 #define CPUCS_REG 0xE600
42
43 /** USB Control EP0 bRequest: "Firmware Load". */
44 #define REQUEST_FIRMWARE_LOAD 0xA0
45
46 /** Value to write into CPUCS to put EZ-USB ANGIE into reset. */
47 #define CPU_RESET 0x01
48
49 /** Value to write into CPUCS to put EZ-USB ANGIE out of reset. */
50 #define CPU_START 0x00
51
52 /** Base address of firmware in EZ-USB ANGIE code space. */
53 #define FIRMWARE_ADDR 0x0000
54
55 /** USB interface number */
56 #define USB_INTERFACE 0
57
58 /** Delay (in microseconds) to wait while EZ-USB performs ReNumeration. */
59 #define ANGIE_RENUMERATION_DELAY_US 1500000
60
61 /** Default location of ANGIE firmware image. */
62 #define ANGIE_FIRMWARE_FILE PKGDATADIR "/angie/angie_firmware.bin"
63
64 /** Default location of ANGIE firmware image. */
65 #define ANGIE_BITSTREAM_FILE PKGDATADIR "/angie/angie_bitstream.bit"
66
67 /** Maximum size of a single firmware section. Entire EZ-USB ANGIE code space = 16kB */
68 #define SECTION_BUFFERSIZE 16384
69
70 /** Tuning of OpenOCD SCAN commands split into multiple ANGIE commands. */
71 #define SPLIT_SCAN_THRESHOLD 10
72
73 /** ANGIE hardware type */
74 enum angie_type {
75 ANGIE,
76 };
77
78 enum angie_payload_direction {
79 PAYLOAD_DIRECTION_OUT,
80 PAYLOAD_DIRECTION_IN
81 };
82
83 enum angie_delay_type {
84 DELAY_CLOCK_TCK,
85 DELAY_CLOCK_TMS,
86 DELAY_SCAN_IN,
87 DELAY_SCAN_OUT,
88 DELAY_SCAN_IO
89 };
90
91 /**
92 * ANGIE command (ANGIE command queue element).
93 *
94 * For the OUT direction payload, things are quite easy: Payload is stored
95 * in a rather small array (up to 63 bytes), the payload is always allocated
96 * by the function generating the command and freed by angie_clear_queue().
97 *
98 * For the IN direction payload, things get a little bit more complicated:
99 * The maximum IN payload size for a single command is 64 bytes. Assume that
100 * a single OpenOCD command needs to scan 256 bytes. This results in the
101 * generation of four ANGIE commands. The function generating these
102 * commands shall allocate an uint8_t[256] array. Each command's #payload_in
103 * pointer shall point to the corresponding offset where IN data shall be
104 * placed, while #payload_in_start shall point to the first element of the 256
105 * byte array.
106 * - first command: #payload_in_start + 0
107 * - second command: #payload_in_start + 64
108 * - third command: #payload_in_start + 128
109 * - fourth command: #payload_in_start + 192
110 *
111 * The last command sets #needs_postprocessing to true.
112 */
113 struct angie_cmd {
114 uint8_t id; /**< ANGIE command ID */
115
116 uint8_t *payload_out; /**< Pointer where OUT payload shall be stored */
117 uint8_t payload_out_size; /**< OUT direction payload size for this command */
118
119 uint8_t *payload_in_start; /**< Pointer to first element of IN payload array */
120 uint8_t *payload_in; /**< Pointer where IN payload shall be stored */
121 uint8_t payload_in_size; /**< IN direction payload size for this command */
122
123 /** Indicates if this command needs post-processing */
124 bool needs_postprocessing;
125
126 /** Indicates if angie_clear_queue() should free payload_in_start */
127 bool free_payload_in_start;
128
129 /** Pointer to corresponding OpenOCD command for post-processing */
130 struct jtag_command *cmd_origin;
131
132 struct angie_cmd *next; /**< Pointer to next command (linked list) */
133 };
134
135 /** Describes one driver instance */
136 struct angie {
137 struct libusb_context *libusb_ctx;
138 struct libusb_device_handle *usb_device_handle;
139 enum angie_type type;
140
141 unsigned int ep_in; /**< IN endpoint number */
142 unsigned int ep_out; /**< OUT endpoint number */
143
144 /* delay value for "SLOW_CLOCK commands" in [0:255] range in units of 4 us;
145 -1 means no need for delay */
146 int delay_scan_in; /**< Delay value for SCAN_IN commands */
147 int delay_scan_out; /**< Delay value for SCAN_OUT commands */
148 int delay_scan_io; /**< Delay value for SCAN_IO commands */
149 int delay_clock_tck; /**< Delay value for CLOCK_TMS commands */
150 int delay_clock_tms; /**< Delay value for CLOCK_TCK commands */
151
152 int commands_in_queue; /**< Number of commands in queue */
153 struct angie_cmd *queue_start; /**< Pointer to first command in queue */
154 struct angie_cmd *queue_end; /**< Pointer to last command in queue */
155 };
156
157 /**************************** Function Prototypes *****************************/
158
159 /* USB helper functions */
160 static int angie_usb_open(struct angie *device);
161 static int angie_usb_close(struct angie *device);
162
163 /* ANGIE MCU (Cypress EZ-USB) specific functions */
164 static int angie_cpu_reset(struct angie *device, char reset_bit);
165 static int angie_load_firmware_and_renumerate(struct angie *device, const char *filename,
166 uint32_t delay_us);
167 static int angie_load_firmware(struct angie *device, const char *filename);
168 static int angie_load_bitstream(struct angie *device, const char *filename);
169
170 static int angie_write_firmware_section(struct angie *device,
171 struct image *firmware_image, int section_index);
172
173 /* Generic helper functions */
174 static void angie_dump_signal_states(uint8_t input_signals, uint8_t output_signals);
175
176 /* ANGIE command generation helper functions */
177 static int angie_allocate_payload(struct angie_cmd *angie_cmd, int size,
178 enum angie_payload_direction direction);
179
180 /* ANGIE command queue helper functions */
181 static int angie_get_queue_size(struct angie *device,
182 enum angie_payload_direction direction);
183 static void angie_clear_queue(struct angie *device);
184 static int angie_append_queue(struct angie *device, struct angie_cmd *angie_cmd);
185 static int angie_execute_queued_commands(struct angie *device, int timeout_ms);
186
187 static void angie_dump_queue(struct angie *device);
188
189 static int angie_append_scan_cmd(struct angie *device,
190 enum scan_type scan_type,
191 int scan_size_bits,
192 uint8_t *tdi,
193 uint8_t *tdo_start,
194 uint8_t *tdo,
195 uint8_t tms_count_start,
196 uint8_t tms_sequence_start,
197 uint8_t tms_count_end,
198 uint8_t tms_sequence_end,
199 struct jtag_command *origin,
200 bool postprocess);
201 static int angie_append_clock_tms_cmd(struct angie *device, uint8_t count,
202 uint8_t sequence);
203 static int angie_append_clock_tck_cmd(struct angie *device, uint16_t count);
204 static int angie_append_get_signals_cmd(struct angie *device);
205 static int angie_append_set_signals_cmd(struct angie *device, uint8_t low,
206 uint8_t high);
207 static int angie_append_sleep_cmd(struct angie *device, uint32_t us);
208 static int angie_append_configure_tck_cmd(struct angie *device,
209 int delay_scan_in,
210 int delay_scan_out,
211 int delay_scan_io,
212 int delay_tck,
213 int delay_tms);
214 static int angie_append_test_cmd(struct angie *device);
215
216 /* ANGIE TCK frequency helper functions */
217 static int angie_calculate_delay(enum angie_delay_type type, long f, int *delay);
218
219 /* Interface between ANGIE and OpenOCD */
220 static void angie_set_end_state(tap_state_t endstate);
221 static int angie_queue_statemove(struct angie *device);
222
223 static int angie_queue_scan(struct angie *device, struct jtag_command *cmd);
224 static int angie_queue_tlr_reset(struct angie *device, struct jtag_command *cmd);
225 static int angie_queue_runtest(struct angie *device, struct jtag_command *cmd);
226 static int angie_queue_pathmove(struct angie *device, struct jtag_command *cmd);
227 static int angie_queue_sleep(struct angie *device, struct jtag_command *cmd);
228 static int angie_queue_stableclocks(struct angie *device, struct jtag_command *cmd);
229
230 static int angie_post_process_scan(struct angie_cmd *angie_cmd);
231 static int angie_post_process_queue(struct angie *device);
232
233 /* adapter driver functions */
234 static int angie_execute_queue(void);
235 static int angie_khz(int khz, int *jtag_speed);
236 static int angie_speed(int speed);
237 static int angie_speed_div(int speed, int *khz);
238 static int angie_init(void);
239 static int angie_quit(void);
240 static int angie_reset(int trst, int srst);
241
242 /****************************** Global Variables ******************************/
243
244 static struct angie *angie_handle;
245
246 /**************************** USB helper functions ****************************/
247
248 /**
249 * Opens the ANGIE device
250 *
251 * @param device pointer to struct angie identifying ANGIE driver instance.
252 * @return on success: ERROR_OK
253 * @return on failure: ERROR_FAIL
254 */
255 static int angie_usb_open(struct angie *device)
256 {
257 struct libusb_device_handle *usb_device_handle;
258 const uint16_t vids[] = {ANGIE_VID, ANGIE_VID, ANGIE_VID, ANGIE_VID, 0};
259 const uint16_t pids[] = {ANGIE_PID, ANGIE_PID_2, ANGIE_PID_3, ANGIE_PID_4, 0};
260
261 int ret = jtag_libusb_open(vids, pids, NULL, &usb_device_handle, NULL);
262
263 if (ret != ERROR_OK)
264 return ret;
265
266 device->usb_device_handle = usb_device_handle;
267 device->type = ANGIE;
268
269 return ERROR_OK;
270 }
271
272 /**
273 * Releases the ANGIE interface and closes the USB device handle.
274 *
275 * @param device pointer to struct angie identifying ANGIE driver instance.
276 * @return on success: ERROR_OK
277 * @return on failure: ERROR_FAIL
278 */
279 static int angie_usb_close(struct angie *device)
280 {
281 if (device->usb_device_handle) {
282 if (libusb_release_interface(device->usb_device_handle, 0) != 0)
283 return ERROR_FAIL;
284
285 jtag_libusb_close(device->usb_device_handle);
286 device->usb_device_handle = NULL;
287 }
288 return ERROR_OK;
289 }
290
291 /******************* ANGIE CPU (EZ-USB) specific functions ********************/
292
293 /**
294 * Writes '0' or '1' to the CPUCS register, putting the EZ-USB CPU into reset
295 * or out of reset.
296 *
297 * @param device pointer to struct angie identifying ANGIE driver instance.
298 * @param reset_bit 0 to put CPU into reset, 1 to put CPU out of reset.
299 * @return on success: ERROR_OK
300 * @return on failure: ERROR_FAIL
301 */
302 static int angie_cpu_reset(struct angie *device, char reset_bit)
303 {
304 return jtag_libusb_control_transfer(device->usb_device_handle,
305 (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
306 REQUEST_FIRMWARE_LOAD, CPUCS_REG, 0, &reset_bit, 1, LIBUSB_TIMEOUT_MS, NULL);
307 }
308
309 /**
310 * Puts the ANGIE's EZ-USB microcontroller into reset state, downloads
311 * the firmware image, resumes the microcontroller and re-enumerates
312 * USB devices.
313 *
314 * @param device pointer to struct angie identifying ANGIE driver instance.
315 * The usb_handle member will be modified during re-enumeration.
316 * @param filename path to the Intel HEX file containing the firmware image.
317 * @param delay_us the delay to wait for the device to re-enumerate.
318 * @return on success: ERROR_OK
319 * @return on failure: ERROR_FAIL
320 */
321 static int angie_load_firmware_and_renumerate(struct angie *device,
322 const char *filename, uint32_t delay_us)
323 {
324 int ret;
325
326 /* Basic process: After downloading the firmware, the ANGIE will disconnect
327 * itself and re-connect after a short amount of time so we have to close
328 * the handle and re-enumerate USB devices */
329
330 ret = angie_load_firmware(device, filename);
331 if (ret != ERROR_OK)
332 return ret;
333
334 ret = angie_usb_close(device);
335 if (ret != ERROR_OK)
336 return ret;
337
338 usleep(delay_us);
339
340 return angie_usb_open(device);
341 }
342
343 /**
344 * Downloads a firmware image to the ANGIE's EZ-USB microcontroller
345 * over the USB bus.
346 *
347 * @param device pointer to struct angie identifying ANGIE driver instance.
348 * @param filename an absolute or relative path to the Intel HEX file
349 * containing the firmware image.
350 * @return on success: ERROR_OK
351 * @return on failure: ERROR_FAIL
352 */
353 static int angie_load_firmware(struct angie *device, const char *filename)
354 {
355 struct image angie_firmware_image;
356 int ret;
357
358 ret = angie_cpu_reset(device, CPU_RESET);
359 if (ret != ERROR_OK) {
360 LOG_ERROR("Could not halt ANGIE CPU");
361 return ret;
362 }
363
364 angie_firmware_image.base_address = 0;
365 angie_firmware_image.base_address_set = false;
366
367 ret = image_open(&angie_firmware_image, filename, "bin");
368 if (ret != ERROR_OK) {
369 LOG_ERROR("Could not load firmware image");
370 return ret;
371 }
372
373 /* Download all sections in the image to ANGIE */
374 for (unsigned int i = 0; i < angie_firmware_image.num_sections; i++) {
375 ret = angie_write_firmware_section(device, &angie_firmware_image, i);
376 if (ret != ERROR_OK)
377 return ret;
378 }
379
380 image_close(&angie_firmware_image);
381
382 ret = angie_cpu_reset(device, CPU_START);
383 if (ret != ERROR_OK) {
384 LOG_ERROR("Could not restart ANGIE CPU");
385 return ret;
386 }
387
388 return ERROR_OK;
389 }
390
391 /**
392 * Downloads a bitstream file to the ANGIE's FPGA through the EZ-USB microcontroller
393 * over the USB bus.
394 *
395 * @param device pointer to struct angie identifying ANGIE driver instance.
396 * @param filename an absolute or relative path to the Xilinx .bit file
397 * containing the bitstream data.
398 * @return on success: ERROR_OK
399 * @return on failure: ERROR_FAIL
400 */
401 static int angie_load_bitstream(struct angie *device, const char *filename)
402 {
403 int ret, transferred;
404 const char *bitstream_file_path = filename;
405 FILE *bitstream_file = NULL;
406 char *bitstream_data = NULL;
407 size_t bitstream_size = 0;
408 uint8_t gpifcnt[4];
409
410 /* Open the bitstream file */
411 bitstream_file = fopen(bitstream_file_path, "rb");
412 if (!bitstream_file) {
413 LOG_ERROR("Failed to open bitstream file: %s\n", bitstream_file_path);
414 return ERROR_FAIL;
415 }
416
417 /* Get the size of the bitstream file */
418 fseek(bitstream_file, 0, SEEK_END);
419 bitstream_size = ftell(bitstream_file);
420 fseek(bitstream_file, 0, SEEK_SET);
421
422 /* Allocate memory for the bitstream data */
423 bitstream_data = malloc(bitstream_size);
424 if (!bitstream_data) {
425 LOG_ERROR("Failed to allocate memory for bitstream data.");
426 fclose(bitstream_file);
427 return ERROR_FAIL;
428 }
429
430 /* Read the bitstream data from the file */
431 if (fread(bitstream_data, 1, bitstream_size, bitstream_file) != bitstream_size) {
432 LOG_ERROR("Failed to read bitstream data.");
433 free(bitstream_data);
434 fclose(bitstream_file);
435 return ERROR_FAIL;
436 }
437
438 h_u32_to_be(gpifcnt, bitstream_size);
439
440 /* CFGopen */
441 ret = jtag_libusb_control_transfer(device->usb_device_handle,
442 0x00, 0xB0, 0, 0, (char *)gpifcnt, 4, LIBUSB_TIMEOUT_MS, &transferred);
443 if (ret != ERROR_OK) {
444 LOG_ERROR("Failed opencfg");
445 /* Abort if libusb sent less data than requested */
446 return ERROR_FAIL;
447 }
448
449 /* Send the bitstream data to the microcontroller */
450 int actual_length = 0;
451 ret = jtag_libusb_bulk_write(device->usb_device_handle, 0x02, bitstream_data, bitstream_size, 1000, &actual_length);
452 if (ret != ERROR_OK) {
453 LOG_ERROR("Failed to send bitstream data: %s", libusb_strerror(ret));
454 free(bitstream_data);
455 fclose(bitstream_file);
456 return ERROR_FAIL;
457 }
458
459 LOG_INFO("Bitstream sent successfully.");
460
461 /* Clean up */
462 free(bitstream_data);
463 fclose(bitstream_file);
464
465 /* CFGclose */
466 transferred = 0;
467 ret = jtag_libusb_control_transfer(device->usb_device_handle,
468 0x00, 0xB1, 0, 0, NULL, 0, LIBUSB_TIMEOUT_MS, &transferred);
469 if (ret != ERROR_OK) {
470 LOG_INFO("error cfgclose");
471 /* Abort if libusb sent less data than requested */
472 return ERROR_FAIL;
473 }
474 return ERROR_OK;
475 }
476
477 /**
478 * Send one contiguous firmware section to the ANGIE's EZ-USB microcontroller
479 * over the USB bus.
480 *
481 * @param device pointer to struct angie identifying ANGIE driver instance.
482 * @param firmware_image pointer to the firmware image that contains the section
483 * which should be sent to the ANGIE's EZ-USB microcontroller.
484 * @param section_index index of the section within the firmware image.
485 * @return on success: ERROR_OK
486 * @return on failure: ERROR_FAIL
487 */
488 static int angie_write_firmware_section(struct angie *device,
489 struct image *firmware_image, int section_index)
490 {
491 int addr, bytes_remaining, chunk_size;
492 uint8_t data[SECTION_BUFFERSIZE];
493 uint8_t *data_ptr = data;
494 uint16_t size;
495 size_t size_read;
496 int ret, transferred;
497
498 size = (uint16_t)firmware_image->sections[section_index].size;
499 addr = (uint16_t)firmware_image->sections[section_index].base_address;
500
501 LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04" PRIx16 ")", section_index, addr,
502 size);
503
504 /* Copy section contents to local buffer */
505 ret = image_read_section(firmware_image, section_index, 0, size, data,
506 &size_read);
507
508 if (ret != ERROR_OK)
509 return ret;
510 if (size_read != size)
511 return ERROR_FAIL;
512
513 bytes_remaining = size;
514
515 /* Send section data in chunks of up to 64 bytes to ANGIE */
516 while (bytes_remaining > 0) {
517 if (bytes_remaining > 64)
518 chunk_size = 64;
519 else
520 chunk_size = bytes_remaining;
521
522 ret = jtag_libusb_control_transfer(device->usb_device_handle,
523 (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
524 REQUEST_FIRMWARE_LOAD, addr, FIRMWARE_ADDR, (char *)data_ptr,
525 chunk_size, LIBUSB_TIMEOUT_MS, &transferred);
526
527 if (ret != ERROR_OK)
528 return ret;
529
530 if (transferred != chunk_size) {
531 /* Abort if libusb sent less data than requested */
532 return ERROR_FAIL;
533 }
534
535 bytes_remaining -= chunk_size;
536 addr += chunk_size;
537 data_ptr += chunk_size;
538 }
539
540 return ERROR_OK;
541 }
542
543 /************************** Generic helper functions **************************/
544
545 /**
546 * Print state of interesting signals via LOG_INFO().
547 *
548 * @param input_signals input signal states as returned by CMD_GET_SIGNALS
549 * @param output_signals output signal states as returned by CMD_GET_SIGNALS
550 */
551 static void angie_dump_signal_states(uint8_t input_signals, uint8_t output_signals)
552 {
553 LOG_INFO("ANGIE signal states: TDI: %i, TDO: %i, TMS: %i, TCK: %i, TRST: %i "
554 "SRST: %i",
555 (output_signals & SIGNAL_TDI ? 1 : 0),
556 (input_signals & SIGNAL_TDO ? 1 : 0),
557 (output_signals & SIGNAL_TMS ? 1 : 0),
558 (output_signals & SIGNAL_TCK ? 1 : 0),
559 (output_signals & SIGNAL_TRST ? 1 : 0),
560 (output_signals & SIGNAL_SRST ? 1 : 0));
561 }
562
563 /**************** ANGIE command generation helper functions ***************/
564
565 /**
566 * Allocate and initialize space in memory for ANGIE command payload.
567 *
568 * @param angie_cmd pointer to command whose payload should be allocated.
569 * @param size the amount of memory to allocate (bytes).
570 * @param direction which payload to allocate.
571 * @return on success: ERROR_OK
572 * @return on failure: ERROR_FAIL
573 */
574 static int angie_allocate_payload(struct angie_cmd *angie_cmd, int size,
575 enum angie_payload_direction direction)
576 {
577 uint8_t *payload;
578
579 payload = calloc(size, sizeof(uint8_t));
580
581 if (!payload) {
582 LOG_ERROR("Could not allocate ANGIE command payload: out of memory");
583 return ERROR_FAIL;
584 }
585
586 switch (direction) {
587 case PAYLOAD_DIRECTION_OUT:
588 if (angie_cmd->payload_out) {
589 LOG_ERROR("BUG: Duplicate payload allocation for ANGIE command");
590 free(payload);
591 return ERROR_FAIL;
592 }
593 angie_cmd->payload_out = payload;
594 angie_cmd->payload_out_size = size;
595 break;
596 case PAYLOAD_DIRECTION_IN:
597 if (angie_cmd->payload_in_start) {
598 LOG_ERROR("BUG: Duplicate payload allocation for ANGIE command");
599 free(payload);
600 return ERROR_FAIL;
601 }
602
603 angie_cmd->payload_in_start = payload;
604 angie_cmd->payload_in = payload;
605 angie_cmd->payload_in_size = size;
606
607 /* By default, free payload_in_start in angie_clear_queue(). Commands
608 * that do not want this behavior (e. g. split scans) must turn it off
609 * separately! */
610 angie_cmd->free_payload_in_start = true;
611
612 break;
613 }
614
615 return ERROR_OK;
616 }
617
618 /****************** ANGIE command queue helper functions ******************/
619
620 /**
621 * Get the current number of bytes in the queue, including command IDs.
622 *
623 * @param device pointer to struct angie identifying ANGIE driver instance.
624 * @param direction the transfer direction for which to get byte count.
625 * @return the number of bytes currently stored in the queue for the specified
626 * direction.
627 */
628 static int angie_get_queue_size(struct angie *device,
629 enum angie_payload_direction direction)
630 {
631 struct angie_cmd *current = device->queue_start;
632 int sum = 0;
633
634 while (current) {
635 switch (direction) {
636 case PAYLOAD_DIRECTION_OUT:
637 sum += current->payload_out_size + 1; /* + 1 byte for Command ID */
638 break;
639 case PAYLOAD_DIRECTION_IN:
640 sum += current->payload_in_size;
641 break;
642 }
643
644 current = current->next;
645 }
646
647 return sum;
648 }
649
650 /**
651 * Clear the ANGIE command queue.
652 *
653 * @param device pointer to struct angie identifying ANGIE driver instance.
654 */
655 static void angie_clear_queue(struct angie *device)
656 {
657 struct angie_cmd *current = device->queue_start;
658 struct angie_cmd *next = NULL;
659
660 while (current) {
661 /* Save pointer to next element */
662 next = current->next;
663
664 /* Free payloads: OUT payload can be freed immediately */
665 free(current->payload_out);
666 current->payload_out = NULL;
667
668 /* IN payload MUST be freed ONLY if no other commands use the
669 * payload_in_start buffer */
670 if (current->free_payload_in_start) {
671 free(current->payload_in_start);
672 current->payload_in_start = NULL;
673 current->payload_in = NULL;
674 }
675
676 /* Free queue element */
677 free(current);
678
679 /* Proceed with next element */
680 current = next;
681 }
682
683 device->commands_in_queue = 0;
684 device->queue_start = NULL;
685 device->queue_end = NULL;
686 }
687
688 /**
689 * Add a command to the ANGIE command queue.
690 *
691 * @param device pointer to struct angie identifying ANGIE driver instance.
692 * @param angie_cmd pointer to command that shall be appended to the ANGIE
693 * command queue.
694 * @return on success: ERROR_OK
695 * @return on failure: ERROR_FAIL
696 */
697 static int angie_append_queue(struct angie *device, struct angie_cmd *angie_cmd)
698 {
699 int newsize_out, newsize_in;
700 int ret = ERROR_OK;
701
702 newsize_out = angie_get_queue_size(device, PAYLOAD_DIRECTION_OUT) + 1
703 + angie_cmd->payload_out_size;
704
705 newsize_in = angie_get_queue_size(device, PAYLOAD_DIRECTION_IN)
706 + angie_cmd->payload_in_size;
707
708 /* Check if the current command can be appended to the queue */
709 if (newsize_out > 64 || newsize_in > 64) {
710 /* New command does not fit. Execute all commands in queue before starting
711 * new queue with the current command as first entry. */
712 ret = angie_execute_queued_commands(device, LIBUSB_TIMEOUT_MS);
713
714 if (ret == ERROR_OK)
715 ret = angie_post_process_queue(device);
716
717 if (ret == ERROR_OK)
718 angie_clear_queue(device);
719 }
720
721 if (!device->queue_start) {
722 /* Queue was empty */
723 device->commands_in_queue = 1;
724
725 device->queue_start = angie_cmd;
726 device->queue_end = angie_cmd;
727 } else {
728 /* There are already commands in the queue */
729 device->commands_in_queue++;
730
731 device->queue_end->next = angie_cmd;
732 device->queue_end = angie_cmd;
733 }
734
735 if (ret != ERROR_OK)
736 angie_clear_queue(device);
737
738 return ret;
739 }
740
741 /**
742 * Sends all queued ANGIE commands to the ANGIE for execution.
743 *
744 * @param device pointer to struct angie identifying ANGIE driver instance.
745 * @param timeout_ms
746 * @return on success: ERROR_OK
747 * @return on failure: ERROR_FAIL
748 */
749 static int angie_execute_queued_commands(struct angie *device, int timeout_ms)
750 {
751 struct angie_cmd *current;
752 int ret, i, index_out, index_in, count_out, count_in, transferred;
753 uint8_t buffer[64];
754
755 if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO))
756 angie_dump_queue(device);
757
758 index_out = 0;
759 count_out = 0;
760 count_in = 0;
761
762 for (current = device->queue_start; current; current = current->next) {
763 /* Add command to packet */
764 buffer[index_out] = current->id;
765 index_out++;
766 count_out++;
767
768 for (i = 0; i < current->payload_out_size; i++)
769 buffer[index_out + i] = current->payload_out[i];
770 index_out += current->payload_out_size;
771 count_in += current->payload_in_size;
772 count_out += current->payload_out_size;
773 }
774
775 /* Send packet to ANGIE */
776 ret = jtag_libusb_bulk_write(device->usb_device_handle, device->ep_out,
777 (char *)buffer, count_out, timeout_ms, &transferred);
778 if (ret != ERROR_OK)
779 return ret;
780 if (transferred != count_out)
781 return ERROR_FAIL;
782
783 /* Wait for response if commands contain IN payload data */
784 if (count_in > 0) {
785 ret = jtag_libusb_bulk_write(device->usb_device_handle, device->ep_in,
786 (char *)buffer, count_in, timeout_ms, &transferred);
787 if (ret != ERROR_OK)
788 return ret;
789 if (transferred != count_in)
790 return ERROR_FAIL;
791
792 /* Write back IN payload data */
793 index_in = 0;
794 for (current = device->queue_start; current; current = current->next) {
795 for (i = 0; i < current->payload_in_size; i++) {
796 current->payload_in[i] = buffer[index_in];
797 index_in++;
798 }
799 }
800 }
801 return ERROR_OK;
802 }
803
804 /**
805 * Convert an ANGIE command ID (\a id) to a human-readable string.
806 *
807 * @param id the ANGIE command ID.
808 * @return the corresponding human-readable string.
809 */
810 static const char *angie_cmd_id_string(uint8_t id)
811 {
812 switch (id) {
813 case CMD_SCAN_IN:
814 return "CMD_SCAN_IN";
815 case CMD_SLOW_SCAN_IN:
816 return "CMD_SLOW_SCAN_IN";
817 case CMD_SCAN_OUT:
818 return "CMD_SCAN_OUT";
819 case CMD_SLOW_SCAN_OUT:
820 return "CMD_SLOW_SCAN_OUT";
821 case CMD_SCAN_IO:
822 return "CMD_SCAN_IO";
823 case CMD_SLOW_SCAN_IO:
824 return "CMD_SLOW_SCAN_IO";
825 case CMD_CLOCK_TMS:
826 return "CMD_CLOCK_TMS";
827 case CMD_SLOW_CLOCK_TMS:
828 return "CMD_SLOW_CLOCK_TMS";
829 case CMD_CLOCK_TCK:
830 return "CMD_CLOCK_TCK";
831 case CMD_SLOW_CLOCK_TCK:
832 return "CMD_SLOW_CLOCK_TCK";
833 case CMD_SLEEP_US:
834 return "CMD_SLEEP_US";
835 case CMD_SLEEP_MS:
836 return "CMD_SLEEP_MS";
837 case CMD_GET_SIGNALS:
838 return "CMD_GET_SIGNALS";
839 case CMD_SET_SIGNALS:
840 return "CMD_SET_SIGNALS";
841 case CMD_CONFIGURE_TCK_FREQ:
842 return "CMD_CONFIGURE_TCK_FREQ";
843 case CMD_SET_LEDS:
844 return "CMD_SET_LEDS";
845 case CMD_TEST:
846 return "CMD_TEST";
847 default:
848 return "CMD_UNKNOWN";
849 }
850 }
851
852 /**
853 * Print one ANGIE command to stdout.
854 *
855 * @param angie_cmd pointer to ANGIE command.
856 */
857 static void angie_dump_command(struct angie_cmd *angie_cmd)
858 {
859 char hex[64 * 3];
860 for (int i = 0; i < angie_cmd->payload_out_size; i++)
861 sprintf(hex + 3 * i, "%02" PRIX8 " ", angie_cmd->payload_out[i]);
862
863 hex[3 * angie_cmd->payload_out_size - 1] = 0;
864 LOG_DEBUG_IO(" %-22s | OUT size = %" PRIi8 ", bytes = %s",
865 angie_cmd_id_string(angie_cmd->id), angie_cmd->payload_out_size, hex);
866
867 LOG_DEBUG_IO("\n | IN size = %" PRIi8 "\n", angie_cmd->payload_in_size);
868 }
869
870 /**
871 * Print the ANGIE command queue to stdout.
872 *
873 * @param device pointer to struct angie identifying ANGIE driver instance.
874 */
875 static void angie_dump_queue(struct angie *device)
876 {
877 struct angie_cmd *current;
878
879 LOG_DEBUG_IO("ANGIE command queue:\n");
880
881 for (current = device->queue_start; current; current = current->next)
882 angie_dump_command(current);
883 }
884
885 /**
886 * Perform JTAG scan
887 *
888 * Creates and appends a JTAG scan command to the ANGIE command queue.
889 * A JTAG scan consists of three steps:
890 * - Move to the desired SHIFT state, depending on scan type (IR/DR scan).
891 * - Shift TDI data into the JTAG chain, optionally reading the TDO pin.
892 * - Move to the desired end state.
893 *
894 * @param device pointer to struct angie identifying ANGIE driver instance.
895 * @param scan_type the type of the scan (IN, OUT, IO (bidirectional)).
896 * @param scan_size_bits number of bits to shift into the JTAG chain.
897 * @param tdi pointer to array containing TDI data.
898 * @param tdo_start pointer to first element of array where TDO data shall be
899 * stored. See #angie_cmd for details.
900 * @param tdo pointer to array where TDO data shall be stored
901 * @param tms_count_start number of TMS state transitions to perform BEFORE
902 * shifting data into the JTAG chain.
903 * @param tms_sequence_start sequence of TMS state transitions that will be
904 * performed BEFORE shifting data into the JTAG chain.
905 * @param tms_count_end number of TMS state transitions to perform AFTER
906 * shifting data into the JTAG chain.
907 * @param tms_sequence_end sequence of TMS state transitions that will be
908 * performed AFTER shifting data into the JTAG chain.
909 * @param origin pointer to OpenOCD command that generated this scan command.
910 * @param postprocess whether this command needs to be post-processed after
911 * execution.
912 * @return on success: ERROR_OK
913 * @return on failure: ERROR_FAIL
914 */
915 static int angie_append_scan_cmd(struct angie *device, enum scan_type scan_type,
916 int scan_size_bits, uint8_t *tdi, uint8_t *tdo_start, uint8_t *tdo,
917 uint8_t tms_count_start, uint8_t tms_sequence_start, uint8_t tms_count_end,
918 uint8_t tms_sequence_end, struct jtag_command *origin, bool postprocess)
919 {
920 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
921 int ret, i, scan_size_bytes;
922 uint8_t bits_last_byte;
923
924 if (!cmd)
925 return ERROR_FAIL;
926
927 /* Check size of command. USB buffer can hold 64 bytes, 1 byte is command ID,
928 * 5 bytes are setup data -> 58 remaining payload bytes for TDI data */
929 if (scan_size_bits > (58 * 8)) {
930 LOG_ERROR("BUG: Tried to create CMD_SCAN_IO ANGIE command with too"
931 " large payload");
932 free(cmd);
933 return ERROR_FAIL;
934 }
935
936 scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
937
938 bits_last_byte = scan_size_bits % 8;
939 if (bits_last_byte == 0)
940 bits_last_byte = 8;
941
942 /* Allocate out_payload depending on scan type */
943 switch (scan_type) {
944 case SCAN_IN:
945 if (device->delay_scan_in < 0)
946 cmd->id = CMD_SCAN_IN;
947 else
948 cmd->id = CMD_SLOW_SCAN_IN;
949 ret = angie_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_IN);
950 break;
951 case SCAN_OUT:
952 if (device->delay_scan_out < 0)
953 cmd->id = CMD_SCAN_OUT;
954 else
955 cmd->id = CMD_SLOW_SCAN_OUT;
956 ret = angie_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
957 break;
958 case SCAN_IO:
959 if (device->delay_scan_io < 0)
960 cmd->id = CMD_SCAN_IO;
961 else
962 cmd->id = CMD_SLOW_SCAN_IO;
963 ret = angie_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
964 break;
965 default:
966 LOG_ERROR("BUG: 'append scan cmd' encountered an unknown scan type");
967 ret = ERROR_FAIL;
968 break;
969 }
970
971 if (ret != ERROR_OK) {
972 free(cmd);
973 return ret;
974 }
975
976 /* Build payload_out that is common to all scan types */
977 cmd->payload_out[0] = scan_size_bytes & 0xFF;
978 cmd->payload_out[1] = bits_last_byte & 0xFF;
979 cmd->payload_out[2] = ((tms_count_start & 0x0F) << 4) | (tms_count_end & 0x0F);
980 cmd->payload_out[3] = tms_sequence_start;
981 cmd->payload_out[4] = tms_sequence_end;
982
983 /* Setup payload_out for types with OUT transfer */
984 if (scan_type == SCAN_OUT || scan_type == SCAN_IO) {
985 for (i = 0; i < scan_size_bytes; i++)
986 cmd->payload_out[i + 5] = tdi[i];
987 }
988
989 /* Setup payload_in pointers for types with IN transfer */
990 if (scan_type == SCAN_IN || scan_type == SCAN_IO) {
991 cmd->payload_in_start = tdo_start;
992 cmd->payload_in = tdo;
993 cmd->payload_in_size = scan_size_bytes;
994 }
995
996 cmd->needs_postprocessing = postprocess;
997 cmd->cmd_origin = origin;
998
999 /* For scan commands, we free payload_in_start only when the command is
1000 * the last in a series of split commands or a stand-alone command */
1001 cmd->free_payload_in_start = postprocess;
1002
1003 return angie_append_queue(device, cmd);
1004 }
1005
1006 /**
1007 * Perform TAP state transitions
1008 *
1009 * @param device pointer to struct angie identifying ANGIE driver instance.
1010 * @param count defines the number of TCK clock cycles generated (up to 8).
1011 * @param sequence defines the TMS pin levels for each state transition. The
1012 * Least-Significant Bit is read first.
1013 * @return on success: ERROR_OK
1014 * @return on failure: ERROR_FAIL
1015 */
1016 static int angie_append_clock_tms_cmd(struct angie *device, uint8_t count,
1017 uint8_t sequence)
1018 {
1019 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1020 int ret;
1021
1022 if (!cmd) {
1023 LOG_ERROR("Out of memory");
1024 return ERROR_FAIL;
1025 }
1026
1027 if (device->delay_clock_tms < 0)
1028 cmd->id = CMD_CLOCK_TMS;
1029 else
1030 cmd->id = CMD_SLOW_CLOCK_TMS;
1031
1032 /* CMD_CLOCK_TMS has two OUT payload bytes and zero IN payload bytes */
1033 ret = angie_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
1034 if (ret != ERROR_OK) {
1035 free(cmd);
1036 return ret;
1037 }
1038
1039 cmd->payload_out[0] = count;
1040 cmd->payload_out[1] = sequence;
1041
1042 return angie_append_queue(device, cmd);
1043 }
1044
1045 /**
1046 * Generate a defined amount of TCK clock cycles
1047 *
1048 * All other JTAG signals are left unchanged.
1049 *
1050 * @param device pointer to struct angie identifying ANGIE driver instance.
1051 * @param count the number of TCK clock cycles to generate.
1052 * @return on success: ERROR_OK
1053 * @return on failure: ERROR_FAIL
1054 */
1055 static int angie_append_clock_tck_cmd(struct angie *device, uint16_t count)
1056 {
1057 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1058 int ret;
1059
1060 if (!cmd) {
1061 LOG_ERROR("Out of memory");
1062 return ERROR_FAIL;
1063 }
1064
1065 if (device->delay_clock_tck < 0)
1066 cmd->id = CMD_CLOCK_TCK;
1067 else
1068 cmd->id = CMD_SLOW_CLOCK_TCK;
1069
1070 /* CMD_CLOCK_TCK has two OUT payload bytes and zero IN payload bytes */
1071 ret = angie_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
1072 if (ret != ERROR_OK) {
1073 free(cmd);
1074 return ret;
1075 }
1076
1077 cmd->payload_out[0] = count & 0xff;
1078 cmd->payload_out[1] = (count >> 8) & 0xff;
1079
1080 return angie_append_queue(device, cmd);
1081 }
1082
1083 /**
1084 * Read JTAG signals.
1085 *
1086 * @param device pointer to struct angie identifying ANGIE driver instance.
1087 * @return on success: ERROR_OK
1088 * @return on failure: ERROR_FAIL
1089 */
1090 static int angie_append_get_signals_cmd(struct angie *device)
1091 {
1092 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1093 int ret;
1094
1095 if (!cmd) {
1096 LOG_ERROR("Out of memory");
1097 return ERROR_FAIL;
1098 }
1099
1100 cmd->id = CMD_GET_SIGNALS;
1101 cmd->needs_postprocessing = true;
1102
1103 /* CMD_GET_SIGNALS has two IN payload bytes */
1104 ret = angie_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_IN);
1105
1106 if (ret != ERROR_OK) {
1107 free(cmd);
1108 return ret;
1109 }
1110
1111 return angie_append_queue(device, cmd);
1112 }
1113
1114 /**
1115 * Arbitrarily set JTAG output signals.
1116 *
1117 * @param device pointer to struct angie identifying ANGIE driver instance.
1118 * @param low defines which signals will be de-asserted. Each bit corresponds
1119 * to a JTAG signal:
1120 * - SIGNAL_TDI
1121 * - SIGNAL_TMS
1122 * - SIGNAL_TCK
1123 * - SIGNAL_TRST
1124 * - SIGNAL_BRKIN
1125 * - SIGNAL_RESET
1126 * - SIGNAL_OCDSE
1127 * @param high defines which signals will be asserted.
1128 * @return on success: ERROR_OK
1129 * @return on failure: ERROR_FAIL
1130 */
1131 static int angie_append_set_signals_cmd(struct angie *device, uint8_t low,
1132 uint8_t high)
1133 {
1134 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1135 int ret;
1136
1137 if (!cmd) {
1138 LOG_ERROR("Out of memory");
1139 return ERROR_FAIL;
1140 }
1141
1142 cmd->id = CMD_SET_SIGNALS;
1143
1144 /* CMD_SET_SIGNALS has two OUT payload bytes and zero IN payload bytes */
1145 ret = angie_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
1146
1147 if (ret != ERROR_OK) {
1148 free(cmd);
1149 return ret;
1150 }
1151
1152 cmd->payload_out[0] = low;
1153 cmd->payload_out[1] = high;
1154
1155 return angie_append_queue(device, cmd);
1156 }
1157
1158 /**
1159 * Sleep for a pre-defined number of microseconds
1160 *
1161 * @param device pointer to struct angie identifying ANGIE driver instance.
1162 * @param us the number microseconds to sleep.
1163 * @return on success: ERROR_OK
1164 * @return on failure: ERROR_FAIL
1165 */
1166 static int angie_append_sleep_cmd(struct angie *device, uint32_t us)
1167 {
1168 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1169 int ret;
1170
1171 if (!cmd) {
1172 LOG_ERROR("Out of memory");
1173 return ERROR_FAIL;
1174 }
1175
1176 cmd->id = CMD_SLEEP_US;
1177
1178 /* CMD_SLEEP_US has two OUT payload bytes and zero IN payload bytes */
1179 ret = angie_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
1180
1181 if (ret != ERROR_OK) {
1182 free(cmd);
1183 return ret;
1184 }
1185
1186 cmd->payload_out[0] = us & 0x00ff;
1187 cmd->payload_out[1] = (us >> 8) & 0x00ff;
1188
1189 return angie_append_queue(device, cmd);
1190 }
1191
1192 /**
1193 * Set TCK delay counters
1194 *
1195 * @param device pointer to struct angie identifying ANGIE driver instance.
1196 * @param delay_scan_in delay count top value in jtag_slow_scan_in() function.
1197 * @param delay_scan_out delay count top value in jtag_slow_scan_out() function.
1198 * @param delay_scan_io delay count top value in jtag_slow_scan_io() function.
1199 * @param delay_tck delay count top value in jtag_clock_tck() function.
1200 * @param delay_tms delay count top value in jtag_slow_clock_tms() function.
1201 * @return on success: ERROR_OK
1202 * @return on failure: ERROR_FAIL
1203 */
1204 static int angie_append_configure_tck_cmd(struct angie *device, int delay_scan_in,
1205 int delay_scan_out, int delay_scan_io, int delay_tck, int delay_tms)
1206 {
1207 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1208 int ret;
1209
1210 if (!cmd) {
1211 LOG_ERROR("Out of memory");
1212 return ERROR_FAIL;
1213 }
1214
1215 cmd->id = CMD_CONFIGURE_TCK_FREQ;
1216
1217 /* CMD_CONFIGURE_TCK_FREQ has five OUT payload bytes and zero
1218 * IN payload bytes */
1219 ret = angie_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT);
1220 if (ret != ERROR_OK) {
1221 free(cmd);
1222 return ret;
1223 }
1224
1225 if (delay_scan_in < 0)
1226 cmd->payload_out[0] = 0;
1227 else
1228 cmd->payload_out[0] = (uint8_t)delay_scan_in;
1229
1230 if (delay_scan_out < 0)
1231 cmd->payload_out[1] = 0;
1232 else
1233 cmd->payload_out[1] = (uint8_t)delay_scan_out;
1234
1235 if (delay_scan_io < 0)
1236 cmd->payload_out[2] = 0;
1237 else
1238 cmd->payload_out[2] = (uint8_t)delay_scan_io;
1239
1240 if (delay_tck < 0)
1241 cmd->payload_out[3] = 0;
1242 else
1243 cmd->payload_out[3] = (uint8_t)delay_tck;
1244
1245 if (delay_tms < 0)
1246 cmd->payload_out[4] = 0;
1247 else
1248 cmd->payload_out[4] = (uint8_t)delay_tms;
1249
1250 return angie_append_queue(device, cmd);
1251 }
1252
1253 /**
1254 * Test command. Used to check if the ANGIE device is ready to accept new
1255 * commands.
1256 *
1257 * @param device pointer to struct angie identifying ANGIE driver instance.
1258 * @return on success: ERROR_OK
1259 * @return on failure: ERROR_FAIL
1260 */
1261 static int angie_append_test_cmd(struct angie *device)
1262 {
1263 struct angie_cmd *cmd = calloc(1, sizeof(struct angie_cmd));
1264 int ret;
1265
1266 if (!cmd) {
1267 LOG_ERROR("Out of memory");
1268 return ERROR_FAIL;
1269 }
1270
1271 cmd->id = CMD_TEST;
1272
1273 /* CMD_TEST has one OUT payload byte and zero IN payload bytes */
1274 ret = angie_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT);
1275 if (ret != ERROR_OK) {
1276 free(cmd);
1277 return ret;
1278 }
1279
1280 cmd->payload_out[0] = 0xAA;
1281
1282 return angie_append_queue(device, cmd);
1283 }
1284
1285 /****************** ANGIE TCK frequency helper functions ******************/
1286
1287 /**
1288 * Calculate delay values for a given TCK frequency.
1289 *
1290 * The ANGIE firmware uses five different speed values for different
1291 * commands. These speed values are calculated in these functions.
1292 *
1293 * The five different commands which support variable TCK frequency are
1294 * implemented twice in the firmware:
1295 * 1. Maximum possible frequency without any artificial delay
1296 * 2. Variable frequency with artificial linear delay loop
1297 *
1298 * To set the ANGIE to maximum frequency, it is only necessary to use the
1299 * corresponding command IDs. To set the ANGIE to a lower frequency, the
1300 * delay loop top values have to be calculated first. Then, a
1301 * CMD_CONFIGURE_TCK_FREQ command needs to be sent to the ANGIE device.
1302 *
1303 * The delay values are described by linear equations:
1304 * t = k * x + d
1305 * (t = period, k = constant, x = delay value, d = constant)
1306 *
1307 * Thus, the delay can be calculated as in the following equation:
1308 * x = (t - d) / k
1309 *
1310 * The constants in these equations have been determined and validated by
1311 * measuring the frequency resulting from different delay values.
1312 *
1313 * @param type for which command to calculate the delay value.
1314 * @param f TCK frequency for which to calculate the delay value in Hz.
1315 * @param delay where to store resulting delay value.
1316 * @return on success: ERROR_OK
1317 * @return on failure: ERROR_FAIL
1318 */
1319 static int angie_calculate_delay(enum angie_delay_type type, long f, int *delay)
1320 {
1321 float t_us, x, x_ceil;
1322
1323 /* Calculate period of requested TCK frequency */
1324 t_us = 1000000.0 / f;
1325
1326 switch (type) {
1327 case DELAY_CLOCK_TCK:
1328 x = (t_us - 6.0) / 4;
1329 break;
1330 case DELAY_CLOCK_TMS:
1331 x = (t_us - 8.5) / 4;
1332 break;
1333 case DELAY_SCAN_IN:
1334 x = (t_us - 8.8308) / 4;
1335 break;
1336 case DELAY_SCAN_OUT:
1337 x = (t_us - 10.527) / 4;
1338 break;
1339 case DELAY_SCAN_IO:
1340 x = (t_us - 13.132) / 4;
1341 break;
1342 default:
1343 return ERROR_FAIL;
1344 break;
1345 }
1346
1347 /* Check if the delay value is negative. This happens when a frequency is
1348 * requested that is too high for the delay loop implementation. In this
1349 * case, set delay value to zero. */
1350 if (x < 0)
1351 x = 0;
1352
1353 /* We need to convert the exact delay value to an integer. Therefore, we
1354 * round the exact value UP to ensure that the resulting frequency is NOT
1355 * higher than the requested frequency. */
1356 x_ceil = ceilf(x);
1357
1358 /* Check if the value is within limits */
1359 if (x_ceil > 255)
1360 return ERROR_FAIL;
1361
1362 *delay = (int)x_ceil;
1363
1364 return ERROR_OK;
1365 }
1366
1367 /**
1368 * Calculate frequency for a given delay value.
1369 *
1370 * Similar to the #angie_calculate_delay function, this function calculates the
1371 * TCK frequency for a given delay value by using linear equations of the form:
1372 * t = k * x + d
1373 * (t = period, k = constant, x = delay value, d = constant)
1374 *
1375 * @param type for which command to calculate the delay value.
1376 * @param delay value for which to calculate the resulting TCK frequency.
1377 * @return the resulting TCK frequency
1378 */
1379 static long angie_calculate_frequency(enum angie_delay_type type, int delay)
1380 {
1381 float t_us, f_float;
1382
1383 if (delay > 255)
1384 return 0;
1385
1386 switch (type) {
1387 case DELAY_CLOCK_TCK:
1388 if (delay < 0)
1389 t_us = 2.666;
1390 else
1391 t_us = (4.0 * delay) + 6.0;
1392 break;
1393 case DELAY_CLOCK_TMS:
1394 if (delay < 0)
1395 t_us = 5.666;
1396 else
1397 t_us = (4.0 * delay) + 8.5;
1398 break;
1399 case DELAY_SCAN_IN:
1400 if (delay < 0)
1401 t_us = 5.5;
1402 else
1403 t_us = (4.0 * delay) + 8.8308;
1404 break;
1405 case DELAY_SCAN_OUT:
1406 if (delay < 0)
1407 t_us = 7.0;
1408 else
1409 t_us = (4.0 * delay) + 10.527;
1410 break;
1411 case DELAY_SCAN_IO:
1412 if (delay < 0)
1413 t_us = 9.926;
1414 else
1415 t_us = (4.0 * delay) + 13.132;
1416 break;
1417 default:
1418 return 0;
1419 }
1420
1421 f_float = 1000000.0 / t_us;
1422 return roundf(f_float);
1423 }
1424
1425 /******************* Interface between ANGIE and OpenOCD ******************/
1426
1427 /**
1428 * Sets the end state follower (see interface.h) if \a endstate is a stable
1429 * state.
1430 *
1431 * @param endstate the state the end state follower should be set to.
1432 */
1433 static void angie_set_end_state(tap_state_t endstate)
1434 {
1435 if (tap_is_state_stable(endstate))
1436 tap_set_end_state(endstate);
1437 else
1438 LOG_ERROR("BUG: %s is not a valid end state", tap_state_name(endstate));
1439 }
1440
1441 /**
1442 * Move from the current TAP state to the current TAP end state.
1443 *
1444 * @param device pointer to struct angie identifying ANGIE driver instance.
1445 * @return on success: ERROR_OK
1446 * @return on failure: ERROR_FAIL
1447 */
1448 static int angie_queue_statemove(struct angie *device)
1449 {
1450 uint8_t tms_sequence, tms_count;
1451 int ret;
1452
1453 if (tap_get_state() == tap_get_end_state()) {
1454 /* Do nothing if we are already there */
1455 return ERROR_OK;
1456 }
1457
1458 tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1459 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1460
1461 ret = angie_append_clock_tms_cmd(device, tms_count, tms_sequence);
1462
1463 if (ret == ERROR_OK)
1464 tap_set_state(tap_get_end_state());
1465
1466 return ret;
1467 }
1468
1469 /**
1470 * Perform a scan operation on a JTAG register.
1471 *
1472 * @param device pointer to struct angie identifying ANGIE driver instance.
1473 * @param cmd pointer to the command that shall be executed.
1474 * @return on success: ERROR_OK
1475 * @return on failure: ERROR_FAIL
1476 */
1477 static int angie_queue_scan(struct angie *device, struct jtag_command *cmd)
1478 {
1479 uint32_t scan_size_bits, scan_size_bytes, bits_last_scan;
1480 uint32_t scans_max_payload, bytecount;
1481 uint8_t *tdi_buffer_start = NULL, *tdi_buffer = NULL;
1482 uint8_t *tdo_buffer_start = NULL, *tdo_buffer = NULL;
1483
1484 uint8_t first_tms_count, first_tms_sequence;
1485 uint8_t last_tms_count, last_tms_sequence;
1486
1487 uint8_t tms_count_pause, tms_sequence_pause;
1488 uint8_t tms_count_resume, tms_sequence_resume;
1489
1490 uint8_t tms_count_start, tms_sequence_start;
1491 uint8_t tms_count_end, tms_sequence_end;
1492
1493 enum scan_type type;
1494 int ret;
1495
1496 /* Determine scan size */
1497 scan_size_bits = jtag_scan_size(cmd->cmd.scan);
1498 scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
1499
1500 /* Determine scan type (IN/OUT/IO) */
1501 type = jtag_scan_type(cmd->cmd.scan);
1502
1503 /* Determine number of scan commands with maximum payload */
1504 scans_max_payload = scan_size_bytes / 58;
1505
1506 /* Determine size of last shift command */
1507 bits_last_scan = scan_size_bits - (scans_max_payload * 58 * 8);
1508
1509 /* Allocate TDO buffer if required */
1510 if (type == SCAN_IN || type == SCAN_IO) {
1511 tdo_buffer_start = calloc(sizeof(uint8_t), scan_size_bytes);
1512
1513 if (!tdo_buffer_start)
1514 return ERROR_FAIL;
1515
1516 tdo_buffer = tdo_buffer_start;
1517 }
1518
1519 /* Fill TDI buffer if required */
1520 if (type == SCAN_OUT || type == SCAN_IO) {
1521 jtag_build_buffer(cmd->cmd.scan, &tdi_buffer_start);
1522 tdi_buffer = tdi_buffer_start;
1523 }
1524
1525 /* Get TAP state transitions */
1526 if (cmd->cmd.scan->ir_scan) {
1527 angie_set_end_state(TAP_IRSHIFT);
1528 first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1529 first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1530
1531 tap_set_state(TAP_IRSHIFT);
1532 tap_set_end_state(cmd->cmd.scan->end_state);
1533 last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1534 last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1535
1536 /* TAP state transitions for split scans */
1537 tms_count_pause = tap_get_tms_path_len(TAP_IRSHIFT, TAP_IRPAUSE);
1538 tms_sequence_pause = tap_get_tms_path(TAP_IRSHIFT, TAP_IRPAUSE);
1539 tms_count_resume = tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRSHIFT);
1540 tms_sequence_resume = tap_get_tms_path(TAP_IRPAUSE, TAP_IRSHIFT);
1541 } else {
1542 angie_set_end_state(TAP_DRSHIFT);
1543 first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1544 first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1545
1546 tap_set_state(TAP_DRSHIFT);
1547 tap_set_end_state(cmd->cmd.scan->end_state);
1548 last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1549 last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1550
1551 /* TAP state transitions for split scans */
1552 tms_count_pause = tap_get_tms_path_len(TAP_DRSHIFT, TAP_DRPAUSE);
1553 tms_sequence_pause = tap_get_tms_path(TAP_DRSHIFT, TAP_DRPAUSE);
1554 tms_count_resume = tap_get_tms_path_len(TAP_DRPAUSE, TAP_DRSHIFT);
1555 tms_sequence_resume = tap_get_tms_path(TAP_DRPAUSE, TAP_DRSHIFT);
1556 }
1557
1558 /* Generate scan commands */
1559 bytecount = scan_size_bytes;
1560 while (bytecount > 0) {
1561 if (bytecount == scan_size_bytes) {
1562 /* This is the first scan */
1563 tms_count_start = first_tms_count;
1564 tms_sequence_start = first_tms_sequence;
1565 } else {
1566 /* Resume from previous scan */
1567 tms_count_start = tms_count_resume;
1568 tms_sequence_start = tms_sequence_resume;
1569 }
1570
1571 if (bytecount > 58) { /* Full scan, at least one scan will follow */
1572 tms_count_end = tms_count_pause;
1573 tms_sequence_end = tms_sequence_pause;
1574
1575 ret = angie_append_scan_cmd(device,
1576 type,
1577 58 * 8,
1578 tdi_buffer,
1579 tdo_buffer_start,
1580 tdo_buffer,
1581 tms_count_start,
1582 tms_sequence_start,
1583 tms_count_end,
1584 tms_sequence_end,
1585 cmd,
1586 false);
1587
1588 bytecount -= 58;
1589
1590 /* Update TDI and TDO buffer pointers */
1591 if (tdi_buffer_start)
1592 tdi_buffer += 58;
1593 if (tdo_buffer_start)
1594 tdo_buffer += 58;
1595 } else if (bytecount == 58) { /* Full scan, no further scans */
1596 tms_count_end = last_tms_count;
1597 tms_sequence_end = last_tms_sequence;
1598
1599 ret = angie_append_scan_cmd(device,
1600 type,
1601 58 * 8,
1602 tdi_buffer,
1603 tdo_buffer_start,
1604 tdo_buffer,
1605 tms_count_start,
1606 tms_sequence_start,
1607 tms_count_end,
1608 tms_sequence_end,
1609 cmd,
1610 true);
1611
1612 bytecount = 0;
1613 } else {/* Scan with less than maximum payload, no further scans */
1614 tms_count_end = last_tms_count;
1615 tms_sequence_end = last_tms_sequence;
1616
1617 ret = angie_append_scan_cmd(device,
1618 type,
1619 bits_last_scan,
1620 tdi_buffer,
1621 tdo_buffer_start,
1622 tdo_buffer,
1623 tms_count_start,
1624 tms_sequence_start,
1625 tms_count_end,
1626 tms_sequence_end,
1627 cmd,
1628 true);
1629
1630 bytecount = 0;
1631 }
1632
1633 if (ret != ERROR_OK) {
1634 free(tdi_buffer_start);
1635 free(tdo_buffer_start);
1636 return ret;
1637 }
1638 }
1639
1640 free(tdi_buffer_start);
1641
1642 /* Set current state to the end state requested by the command */
1643 tap_set_state(cmd->cmd.scan->end_state);
1644
1645 return ERROR_OK;
1646 }
1647
1648 /**
1649 * Move the TAP into the Test Logic Reset state.
1650 *
1651 * @param device pointer to struct angie identifying ANGIE driver instance.
1652 * @param cmd pointer to the command that shall be executed.
1653 * @return on success: ERROR_OK
1654 * @return on failure: ERROR_FAIL
1655 */
1656 static int angie_queue_tlr_reset(struct angie *device, struct jtag_command *cmd)
1657 {
1658 int ret = angie_append_clock_tms_cmd(device, 5, 0xff);
1659
1660 if (ret == ERROR_OK)
1661 tap_set_state(TAP_RESET);
1662
1663 return ret;
1664 }
1665
1666 /**
1667 * Run Test.
1668 *
1669 * Generate TCK clock cycles while remaining
1670 * in the Run-Test/Idle state.
1671 *
1672 * @param device pointer to struct angie identifying ANGIE driver instance.
1673 * @param cmd pointer to the command that shall be executed.
1674 * @return on success: ERROR_OK
1675 * @return on failure: ERROR_FAIL
1676 */
1677 static int angie_queue_runtest(struct angie *device, struct jtag_command *cmd)
1678 {
1679 int ret;
1680
1681 /* Only perform statemove if the TAP currently isn't in the TAP_IDLE state */
1682 if (tap_get_state() != TAP_IDLE) {
1683 angie_set_end_state(TAP_IDLE);
1684 angie_queue_statemove(device);
1685 }
1686
1687 /* Generate the clock cycles */
1688 ret = angie_append_clock_tck_cmd(device, cmd->cmd.runtest->num_cycles);
1689 if (ret != ERROR_OK)
1690 return ret;
1691
1692 /* Move to end state specified in command */
1693 if (cmd->cmd.runtest->end_state != tap_get_state()) {
1694 tap_set_end_state(cmd->cmd.runtest->end_state);
1695 angie_queue_statemove(device);
1696 }
1697
1698 return ERROR_OK;
1699 }
1700
1701 /**
1702 * Execute a JTAG_RESET command
1703 *
1704 * @param device
1705 * @param trst indicate if trst signal is activated.
1706 * @param srst indicate if srst signal is activated.
1707 * @return on success: ERROR_OK
1708 * @return on failure: ERROR_FAIL
1709 */
1710 static int angie_reset(int trst, int srst)
1711 {
1712 struct angie *device = angie_handle;
1713 uint8_t low = 0, high = 0;
1714
1715 if (trst) {
1716 tap_set_state(TAP_RESET);
1717 low |= SIGNAL_TRST;
1718 } else {
1719 high |= SIGNAL_TRST;
1720 }
1721
1722 if (srst)
1723 low |= SIGNAL_SRST;
1724 else
1725 high |= SIGNAL_SRST;
1726
1727 int ret = angie_append_set_signals_cmd(device, low, high);
1728 if (ret == ERROR_OK)
1729 angie_clear_queue(device);
1730
1731 ret = angie_execute_queued_commands(device, LIBUSB_TIMEOUT_MS);
1732 if (ret == ERROR_OK)
1733 angie_clear_queue(device);
1734
1735 return ret;
1736 }
1737
1738 /**
1739 * Move to one TAP state or several states in succession.
1740 *
1741 * @param device pointer to struct angie identifying ANGIE driver instance.
1742 * @param cmd pointer to the command that shall be executed.
1743 * @return on success: ERROR_OK
1744 * @return on failure: ERROR_FAIL
1745 */
1746 static int angie_queue_pathmove(struct angie *device, struct jtag_command *cmd)
1747 {
1748 int ret, i, num_states, batch_size, state_count;
1749 tap_state_t *path;
1750 uint8_t tms_sequence;
1751
1752 num_states = cmd->cmd.pathmove->num_states;
1753 path = cmd->cmd.pathmove->path;
1754 state_count = 0;
1755
1756 while (num_states > 0) {
1757 tms_sequence = 0;
1758
1759 /* Determine batch size */
1760 if (num_states >= 8)
1761 batch_size = 8;
1762 else
1763 batch_size = num_states;
1764
1765 for (i = 0; i < batch_size; i++) {
1766 if (tap_state_transition(tap_get_state(), false) == path[state_count]) {
1767 /* Append '0' transition: clear bit 'i' in tms_sequence */
1768 buf_set_u32(&tms_sequence, i, 1, 0x0);
1769 } else if (tap_state_transition(tap_get_state(), true)
1770 == path[state_count]) {
1771 /* Append '1' transition: set bit 'i' in tms_sequence */
1772 buf_set_u32(&tms_sequence, i, 1, 0x1);
1773 } else {
1774 /* Invalid state transition */
1775 LOG_ERROR("BUG: %s -> %s isn't a valid TAP state transition",
1776 tap_state_name(tap_get_state()),
1777 tap_state_name(path[state_count]));
1778 return ERROR_FAIL;
1779 }
1780
1781 tap_set_state(path[state_count]);
1782 state_count++;
1783 num_states--;
1784 }
1785
1786 /* Append CLOCK_TMS command to ANGIE command queue */
1787 LOG_INFO("pathmove batch: count = %i, sequence = 0x%" PRIx8 "", batch_size, tms_sequence);
1788 ret = angie_append_clock_tms_cmd(angie_handle, batch_size, tms_sequence);
1789 if (ret != ERROR_OK)
1790 return ret;
1791 }
1792
1793 return ERROR_OK;
1794 }
1795
1796 /**
1797 * Sleep for a specific amount of time.
1798 *
1799 * @param device pointer to struct angie identifying ANGIE driver instance.
1800 * @param cmd pointer to the command that shall be executed.
1801 * @return on success: ERROR_OK
1802 * @return on failure: ERROR_FAIL
1803 */
1804 static int angie_queue_sleep(struct angie *device, struct jtag_command *cmd)
1805 {
1806 /* IMPORTANT! Due to the time offset in command execution introduced by
1807 * command queueing, this needs to be implemented in the ANGIE device */
1808 return angie_append_sleep_cmd(device, cmd->cmd.sleep->us);
1809 }
1810
1811 /**
1812 * Generate TCK cycles while remaining in a stable state.
1813 *
1814 * @param device pointer to struct angie identifying ANGIE driver instance.
1815 * @param cmd pointer to the command that shall be executed.
1816 */
1817 static int angie_queue_stableclocks(struct angie *device, struct jtag_command *cmd)
1818 {
1819 int ret;
1820 unsigned int num_cycles;
1821
1822 if (!tap_is_state_stable(tap_get_state())) {
1823 LOG_ERROR("JTAG_STABLECLOCKS: state not stable");
1824 return ERROR_FAIL;
1825 }
1826
1827 num_cycles = cmd->cmd.stableclocks->num_cycles;
1828
1829 /* TMS stays either high (Test Logic Reset state) or low (all other states) */
1830 if (tap_get_state() == TAP_RESET)
1831 ret = angie_append_set_signals_cmd(device, 0, SIGNAL_TMS);
1832 else
1833 ret = angie_append_set_signals_cmd(device, SIGNAL_TMS, 0);
1834
1835 if (ret != ERROR_OK)
1836 return ret;
1837
1838 while (num_cycles > 0) {
1839 if (num_cycles > 0xFFFF) {
1840 /* ANGIE CMD_CLOCK_TCK can generate up to 0xFFFF (uint16_t) cycles */
1841 ret = angie_append_clock_tck_cmd(device, 0xFFFF);
1842 num_cycles -= 0xFFFF;
1843 } else {
1844 ret = angie_append_clock_tck_cmd(device, num_cycles);
1845 num_cycles = 0;
1846 }
1847
1848 if (ret != ERROR_OK)
1849 return ret;
1850 }
1851
1852 return ERROR_OK;
1853 }
1854
1855 /**
1856 * Post-process JTAG_SCAN command
1857 *
1858 * @param angie_cmd pointer to ANGIE command that shall be processed.
1859 * @return on success: ERROR_OK
1860 * @return on failure: ERROR_FAIL
1861 */
1862 static int angie_post_process_scan(struct angie_cmd *angie_cmd)
1863 {
1864 struct jtag_command *cmd = angie_cmd->cmd_origin;
1865 int ret;
1866
1867 switch (jtag_scan_type(cmd->cmd.scan)) {
1868 case SCAN_IN:
1869 case SCAN_IO:
1870 ret = jtag_read_buffer(angie_cmd->payload_in_start, cmd->cmd.scan);
1871 break;
1872 case SCAN_OUT:
1873 /* Nothing to do for OUT scans */
1874 ret = ERROR_OK;
1875 break;
1876 default:
1877 LOG_ERROR("BUG: angie post process scan encountered an unknown JTAG scan type");
1878 ret = ERROR_FAIL;
1879 break;
1880 }
1881
1882 return ret;
1883 }
1884
1885 /**
1886 * Perform post-processing of commands after ANGIE queue has been executed.
1887 *
1888 * @param device pointer to struct angie identifying ANGIE driver instance.
1889 * @return on success: ERROR_OK
1890 * @return on failure: ERROR_FAIL
1891 */
1892 static int angie_post_process_queue(struct angie *device)
1893 {
1894 struct angie_cmd *current;
1895 struct jtag_command *openocd_cmd;
1896 int ret;
1897
1898 current = device->queue_start;
1899
1900 while (current) {
1901 openocd_cmd = current->cmd_origin;
1902
1903 /* Check if a corresponding OpenOCD command is stored for this
1904 * ANGIE command */
1905 if (current->needs_postprocessing && openocd_cmd) {
1906 switch (openocd_cmd->type) {
1907 case JTAG_SCAN:
1908 ret = angie_post_process_scan(current);
1909 break;
1910 case JTAG_TLR_RESET:
1911 case JTAG_RUNTEST:
1912 case JTAG_PATHMOVE:
1913 case JTAG_SLEEP:
1914 case JTAG_STABLECLOCKS:
1915 /* Nothing to do for these commands */
1916 ret = ERROR_OK;
1917 break;
1918 default:
1919 ret = ERROR_FAIL;
1920 LOG_ERROR("BUG: angie post process queue encountered unknown JTAG "
1921 "command type");
1922 break;
1923 }
1924
1925 if (ret != ERROR_OK)
1926 return ret;
1927 }
1928
1929 current = current->next;
1930 }
1931
1932 return ERROR_OK;
1933 }
1934
1935 /**************************** JTAG driver functions ***************************/
1936
1937 /**
1938 * Executes the JTAG Command Queue.
1939 *
1940 * This is done in three stages: First, all OpenOCD commands are processed into
1941 * queued ANGIE commands. Next, the ANGIE command queue is sent to the
1942 * ANGIE device and data received from the ANGIE device is cached. Finally,
1943 * the post-processing function writes back data to the corresponding OpenOCD
1944 * commands.
1945 *
1946 * @return on success: ERROR_OK
1947 * @return on failure: ERROR_FAIL
1948 */
1949 static int angie_execute_queue(void)
1950 {
1951 struct jtag_command *cmd = jtag_command_queue;
1952 int ret;
1953
1954 while (cmd) {
1955 switch (cmd->type) {
1956 case JTAG_SCAN:
1957 ret = angie_queue_scan(angie_handle, cmd);
1958 break;
1959 case JTAG_TLR_RESET:
1960 ret = angie_queue_tlr_reset(angie_handle, cmd);
1961 break;
1962 case JTAG_RUNTEST:
1963 ret = angie_queue_runtest(angie_handle, cmd);
1964 break;
1965 case JTAG_PATHMOVE:
1966 ret = angie_queue_pathmove(angie_handle, cmd);
1967 break;
1968 case JTAG_SLEEP:
1969 ret = angie_queue_sleep(angie_handle, cmd);
1970 break;
1971 case JTAG_STABLECLOCKS:
1972 ret = angie_queue_stableclocks(angie_handle, cmd);
1973 break;
1974 default:
1975 ret = ERROR_FAIL;
1976 LOG_ERROR("BUG: encountered unknown JTAG command type");
1977 break;
1978 }
1979
1980 if (ret != ERROR_OK)
1981 return ret;
1982
1983 cmd = cmd->next;
1984 }
1985
1986 if (angie_handle->commands_in_queue > 0) {
1987 ret = angie_execute_queued_commands(angie_handle, LIBUSB_TIMEOUT_MS);
1988 if (ret != ERROR_OK)
1989 return ret;
1990
1991 ret = angie_post_process_queue(angie_handle);
1992 if (ret != ERROR_OK)
1993 return ret;
1994
1995 angie_clear_queue(angie_handle);
1996 }
1997
1998 return ERROR_OK;
1999 }
2000
2001 /**
2002 * Set the TCK frequency of the ANGIE adapter.
2003 *
2004 * @param khz desired JTAG TCK frequency.
2005 * @param jtag_speed where to store corresponding adapter-specific speed value.
2006 * @return on success: ERROR_OK
2007 * @return on failure: ERROR_FAIL
2008 */
2009 static int angie_khz(int khz, int *jtag_speed)
2010 {
2011 int ret;
2012
2013 if (khz == 0) {
2014 LOG_ERROR("RCLK not supported");
2015 return ERROR_FAIL;
2016 }
2017
2018 /* CLOCK_TCK commands are decoupled from others. Therefore, the frequency
2019 * setting can be done independently from all other commands. */
2020 if (khz >= 375) {
2021 angie_handle->delay_clock_tck = -1;
2022 } else {
2023 ret = angie_calculate_delay(DELAY_CLOCK_TCK, khz * 1000,
2024 &angie_handle->delay_clock_tck);
2025 if (ret != ERROR_OK)
2026 return ret;
2027 }
2028
2029 /* SCAN_{IN,OUT,IO} commands invoke CLOCK_TMS commands. Therefore, if the
2030 * requested frequency goes below the maximum frequency for SLOW_CLOCK_TMS
2031 * commands, all SCAN commands MUST also use the variable frequency
2032 * implementation! */
2033 if (khz >= 176) {
2034 angie_handle->delay_clock_tms = -1;
2035 angie_handle->delay_scan_in = -1;
2036 angie_handle->delay_scan_out = -1;
2037 angie_handle->delay_scan_io = -1;
2038 } else {
2039 ret = angie_calculate_delay(DELAY_CLOCK_TMS, khz * 1000,
2040 &angie_handle->delay_clock_tms);
2041 if (ret != ERROR_OK)
2042 return ret;
2043
2044 ret = angie_calculate_delay(DELAY_SCAN_IN, khz * 1000,
2045 &angie_handle->delay_scan_in);
2046 if (ret != ERROR_OK)
2047 return ret;
2048
2049 ret = angie_calculate_delay(DELAY_SCAN_OUT, khz * 1000,
2050 &angie_handle->delay_scan_out);
2051 if (ret != ERROR_OK)
2052 return ret;
2053
2054 ret = angie_calculate_delay(DELAY_SCAN_IO, khz * 1000,
2055 &angie_handle->delay_scan_io);
2056 if (ret != ERROR_OK)
2057 return ret;
2058 }
2059
2060 LOG_DEBUG_IO("ANGIE TCK setup: delay_tck = %i (%li Hz),",
2061 angie_handle->delay_clock_tck,
2062 angie_calculate_frequency(DELAY_CLOCK_TCK, angie_handle->delay_clock_tck));
2063 LOG_DEBUG_IO(" delay_tms = %i (%li Hz),",
2064 angie_handle->delay_clock_tms,
2065 angie_calculate_frequency(DELAY_CLOCK_TMS, angie_handle->delay_clock_tms));
2066 LOG_DEBUG_IO(" delay_scan_in = %i (%li Hz),",
2067 angie_handle->delay_scan_in,
2068 angie_calculate_frequency(DELAY_SCAN_IN, angie_handle->delay_scan_in));
2069 LOG_DEBUG_IO(" delay_scan_out = %i (%li Hz),",
2070 angie_handle->delay_scan_out,
2071 angie_calculate_frequency(DELAY_SCAN_OUT, angie_handle->delay_scan_out));
2072 LOG_DEBUG_IO(" delay_scan_io = %i (%li Hz),",
2073 angie_handle->delay_scan_io,
2074 angie_calculate_frequency(DELAY_SCAN_IO, angie_handle->delay_scan_io));
2075
2076 /* Configure the ANGIE device with the new delay values */
2077 ret = angie_append_configure_tck_cmd(angie_handle,
2078 angie_handle->delay_scan_in,
2079 angie_handle->delay_scan_out,
2080 angie_handle->delay_scan_io,
2081 angie_handle->delay_clock_tck,
2082 angie_handle->delay_clock_tms);
2083
2084 if (ret != ERROR_OK)
2085 return ret;
2086
2087 *jtag_speed = khz;
2088
2089 return ERROR_OK;
2090 }
2091
2092 /**
2093 * Set the TCK frequency of the ANGIE adapter.
2094 *
2095 * Because of the way the TCK frequency is set up in the ANGIE firmware,
2096 * there are five different speed settings. To simplify things, the
2097 * adapter-specific speed setting value is identical to the TCK frequency in
2098 * khz.
2099 *
2100 * @param speed desired adapter-specific speed value.
2101 * @return on success: ERROR_OK
2102 * @return on failure: ERROR_FAIL
2103 */
2104 static int angie_speed(int speed)
2105 {
2106 int dummy;
2107
2108 return angie_khz(speed, &dummy);
2109 }
2110
2111 /**
2112 * Convert adapter-specific speed value to corresponding TCK frequency in kHz.
2113 *
2114 * Because of the way the TCK frequency is set up in the ANGIE firmware,
2115 * there are five different speed settings. To simplify things, the
2116 * adapter-specific speed setting value is identical to the TCK frequency in
2117 * khz.
2118 *
2119 * @param speed adapter-specific speed value.
2120 * @param khz where to store corresponding TCK frequency in kHz.
2121 * @return on success: ERROR_OK
2122 * @return on failure: ERROR_FAIL
2123 */
2124 static int angie_speed_div(int speed, int *khz)
2125 {
2126 *khz = speed;
2127
2128 return ERROR_OK;
2129 }
2130
2131 /**
2132 * Initiates the firmware download to the ANGIE adapter and prepares
2133 * the USB handle.
2134 *
2135 * @return on success: ERROR_OK
2136 * @return on failure: ERROR_FAIL
2137 */
2138 static int angie_init(void)
2139 {
2140 int ret, transferred;
2141 char str_manufacturer[20];
2142 bool download_firmware = false;
2143 char dummy[64];
2144 uint8_t input_signals, output_signals;
2145
2146 angie_handle = calloc(1, sizeof(struct angie));
2147
2148 if (!angie_handle) {
2149 LOG_ERROR("Out of memory");
2150 return ERROR_FAIL;
2151 }
2152
2153 ret = angie_usb_open(angie_handle);
2154 if (ret != ERROR_OK) {
2155 LOG_ERROR("Could not open ANGIE device");
2156 free(angie_handle);
2157 angie_handle = NULL;
2158 return ret;
2159 }
2160
2161 /* Get String Descriptor to determine if firmware needs to be loaded */
2162 ret = libusb_get_string_descriptor_ascii(angie_handle->usb_device_handle, 1, (unsigned char *)str_manufacturer, 20);
2163 if (ret < 0) {
2164 /* Could not get descriptor -> Unconfigured or original Keil firmware */
2165 download_firmware = true;
2166 } else {
2167 /* We got a String Descriptor, check if it is the correct one */
2168 if (strncmp(str_manufacturer, "NanoXplore, SAS.", 16) != 0)
2169 download_firmware = true;
2170 }
2171
2172 if (download_firmware) {
2173 LOG_INFO("Loading ANGIE firmware. This is reversible by power-cycling ANGIE device.");
2174
2175 if (libusb_claim_interface(angie_handle->usb_device_handle, 0) != ERROR_OK)
2176 LOG_ERROR("Could not claim interface");
2177
2178 ret = angie_load_firmware_and_renumerate(angie_handle,
2179 ANGIE_FIRMWARE_FILE, ANGIE_RENUMERATION_DELAY_US);
2180 if (ret != ERROR_OK) {
2181 LOG_ERROR("Could not download firmware and re-numerate ANGIE");
2182 angie_quit();
2183 return ret;
2184 }
2185 ret = angie_load_bitstream(angie_handle, ANGIE_BITSTREAM_FILE);
2186 if (ret != ERROR_OK) {
2187 LOG_ERROR("Could not download bitstream");
2188 angie_quit();
2189 return ret;
2190 }
2191 } else {
2192 LOG_INFO("ANGIE device is already running ANGIE firmware");
2193 }
2194
2195 /* Get ANGIE USB IN/OUT endpoints and claim the interface */
2196 ret = jtag_libusb_choose_interface(angie_handle->usb_device_handle,
2197 &angie_handle->ep_in, &angie_handle->ep_out, -1, -1, -1, -1);
2198 if (ret != ERROR_OK) {
2199 angie_quit();
2200 return ret;
2201 }
2202
2203 /* Initialize ANGIE command queue */
2204 angie_clear_queue(angie_handle);
2205
2206 /* Issue one test command with short timeout */
2207 ret = angie_append_test_cmd(angie_handle);
2208 if (ret != ERROR_OK) {
2209 angie_quit();
2210 return ret;
2211 }
2212
2213 ret = angie_execute_queued_commands(angie_handle, 200);
2214 if (ret != ERROR_OK) {
2215 /* Sending test command failed. The ANGIE device may be forever waiting for
2216 * the host to fetch an USB Bulk IN packet (e. g. OpenOCD crashed or was
2217 * shut down by the user via Ctrl-C. Try to retrieve this Bulk IN packet. */
2218
2219 ret = jtag_libusb_bulk_write(angie_handle->usb_device_handle, angie_handle->ep_in,
2220 dummy, 64, 200, &transferred);
2221
2222 if (ret != ERROR_OK || transferred == 0) {
2223 /* Bulk IN transfer failed -> unrecoverable error condition */
2224 LOG_ERROR("Cannot communicate with ANGIE device. Disconnect ANGIE from "
2225 "the USB port and re-connect, then re-run OpenOCD");
2226 angie_quit();
2227 return ERROR_FAIL;
2228 }
2229 /* Successfully received Bulk IN packet -> continue */
2230 LOG_INFO("Recovered from lost Bulk IN packet");
2231 }
2232
2233 angie_clear_queue(angie_handle);
2234
2235 ret = angie_append_get_signals_cmd(angie_handle);
2236 if (ret != ERROR_OK) {
2237 angie_quit();
2238 return ret;
2239 }
2240
2241 ret = angie_execute_queued_commands(angie_handle, 200);
2242 if (ret != ERROR_OK) {
2243 angie_quit();
2244 return ret;
2245 }
2246
2247 /* Post-process the single CMD_GET_SIGNALS command */
2248 input_signals = angie_handle->queue_start->payload_in[0];
2249 output_signals = angie_handle->queue_start->payload_in[1];
2250 angie_dump_signal_states(input_signals, output_signals);
2251
2252 angie_clear_queue(angie_handle);
2253
2254 return ERROR_OK;
2255 }
2256
2257 /**
2258 * Closes the USB handle for the ANGIE device.
2259 *
2260 * @return on success: ERROR_OK
2261 * @return on failure: ERROR_FAIL
2262 */
2263 static int angie_quit(void)
2264 {
2265 int ret = angie_usb_close(angie_handle);
2266 free(angie_handle);
2267 angie_handle = NULL;
2268
2269 return ret;
2270 }
2271
2272 static struct jtag_interface angie_interface = {
2273 .execute_queue = angie_execute_queue,
2274 };
2275
2276 struct adapter_driver angie_adapter_driver = {
2277 .name = "angie",
2278 .transports = jtag_only,
2279
2280 .init = angie_init,
2281 .quit = angie_quit,
2282 .reset = angie_reset,
2283 .speed = angie_speed,
2284 .khz = angie_khz,
2285 .speed_div = angie_speed_div,
2286
2287 .jtag_ops = &angie_interface,
2288 };

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)