-
Notifications
You must be signed in to change notification settings - Fork 89
/
ulink.c
2283 lines (1927 loc) · 64.7 KB
/
ulink.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/***************************************************************************
* Copyright (C) 2011-2013 by Martin Schmoelzer *
* <[email protected]> *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include "helper/system.h"
#include <jtag/interface.h>
#include <jtag/commands.h>
#include <target/image.h>
#include <libusb.h>
#include "libusb_helper.h"
#include "OpenULINK/include/msgtypes.h"
/** USB Vendor ID of ULINK device in unconfigured state (no firmware loaded
* yet) or with OpenULINK firmware. */
#define ULINK_VID 0xC251
/** USB Product ID of ULINK device in unconfigured state (no firmware loaded
* yet) or with OpenULINK firmware. */
#define ULINK_PID 0x2710
/** Address of EZ-USB CPU Control & Status register. This register can be
* written by issuing a Control EP0 vendor request. */
#define CPUCS_REG 0x7F92
/** USB Control EP0 bRequest: "Firmware Load". */
#define REQUEST_FIRMWARE_LOAD 0xA0
/** Value to write into CPUCS to put EZ-USB into reset. */
#define CPU_RESET 0x01
/** Value to write into CPUCS to put EZ-USB out of reset. */
#define CPU_START 0x00
/** Base address of firmware in EZ-USB code space. */
#define FIRMWARE_ADDR 0x0000
/** USB interface number */
#define USB_INTERFACE 0
/** Delay (in microseconds) to wait while EZ-USB performs ReNumeration. */
#define ULINK_RENUMERATION_DELAY 1500000
/** Default location of OpenULINK firmware image. */
#define ULINK_FIRMWARE_FILE PKGDATADIR "/OpenULINK/ulink_firmware.hex"
/** Maximum size of a single firmware section. Entire EZ-USB code space = 8kB */
#define SECTION_BUFFERSIZE 8192
/** Tuning of OpenOCD SCAN commands split into multiple OpenULINK commands. */
#define SPLIT_SCAN_THRESHOLD 10
/** ULINK hardware type */
enum ulink_type {
/** Original ULINK adapter, based on Cypress EZ-USB (AN2131):
* Full JTAG support, no SWD support. */
ULINK_1,
/** Newer ULINK adapter, based on NXP LPC2148. Currently unsupported. */
ULINK_2,
/** Newer ULINK adapter, based on EZ-USB FX2 + FPGA. Currently unsupported. */
ULINK_PRO,
/** Newer ULINK adapter, possibly based on ULINK 2. Currently unsupported. */
ULINK_ME
};
enum ulink_payload_direction {
PAYLOAD_DIRECTION_OUT,
PAYLOAD_DIRECTION_IN
};
enum ulink_delay_type {
DELAY_CLOCK_TCK,
DELAY_CLOCK_TMS,
DELAY_SCAN_IN,
DELAY_SCAN_OUT,
DELAY_SCAN_IO
};
/**
* OpenULINK command (OpenULINK command queue element).
*
* For the OUT direction payload, things are quite easy: Payload is stored
* in a rather small array (up to 63 bytes), the payload is always allocated
* by the function generating the command and freed by ulink_clear_queue().
*
* For the IN direction payload, things get a little bit more complicated:
* The maximum IN payload size for a single command is 64 bytes. Assume that
* a single OpenOCD command needs to scan 256 bytes. This results in the
* generation of four OpenULINK commands. The function generating these
* commands shall allocate an uint8_t[256] array. Each command's #payload_in
* pointer shall point to the corresponding offset where IN data shall be
* placed, while #payload_in_start shall point to the first element of the 256
* byte array.
* - first command: #payload_in_start + 0
* - second command: #payload_in_start + 64
* - third command: #payload_in_start + 128
* - fourth command: #payload_in_start + 192
*
* The last command sets #needs_postprocessing to true.
*/
struct ulink_cmd {
uint8_t id; /**< ULINK command ID */
uint8_t *payload_out; /**< OUT direction payload data */
uint8_t payload_out_size; /**< OUT direction payload size for this command */
uint8_t *payload_in_start; /**< Pointer to first element of IN payload array */
uint8_t *payload_in; /**< Pointer where IN payload shall be stored */
uint8_t payload_in_size; /**< IN direction payload size for this command */
/** Indicates if this command needs post-processing */
bool needs_postprocessing;
/** Indicates if ulink_clear_queue() should free payload_in_start */
bool free_payload_in_start;
/** Pointer to corresponding OpenOCD command for post-processing */
struct jtag_command *cmd_origin;
struct ulink_cmd *next; /**< Pointer to next command (linked list) */
};
/** Describes one driver instance */
struct ulink {
struct libusb_context *libusb_ctx;
struct libusb_device_handle *usb_device_handle;
enum ulink_type type;
unsigned int ep_in; /**< IN endpoint number */
unsigned int ep_out; /**< OUT endpoint number */
int delay_scan_in; /**< Delay value for SCAN_IN commands */
int delay_scan_out; /**< Delay value for SCAN_OUT commands */
int delay_scan_io; /**< Delay value for SCAN_IO commands */
int delay_clock_tck; /**< Delay value for CLOCK_TMS commands */
int delay_clock_tms; /**< Delay value for CLOCK_TCK commands */
int commands_in_queue; /**< Number of commands in queue */
struct ulink_cmd *queue_start; /**< Pointer to first command in queue */
struct ulink_cmd *queue_end; /**< Pointer to last command in queue */
};
/**************************** Function Prototypes *****************************/
/* USB helper functions */
static int ulink_usb_open(struct ulink **device);
static int ulink_usb_close(struct ulink **device);
/* ULINK MCU (Cypress EZ-USB) specific functions */
static int ulink_cpu_reset(struct ulink *device, unsigned char reset_bit);
static int ulink_load_firmware_and_renumerate(struct ulink **device, const char *filename,
uint32_t delay);
static int ulink_load_firmware(struct ulink *device, const char *filename);
static int ulink_write_firmware_section(struct ulink *device,
struct image *firmware_image, int section_index);
/* Generic helper functions */
static void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals);
/* OpenULINK command generation helper functions */
static int ulink_allocate_payload(struct ulink_cmd *ulink_cmd, int size,
enum ulink_payload_direction direction);
/* OpenULINK command queue helper functions */
static int ulink_get_queue_size(struct ulink *device,
enum ulink_payload_direction direction);
static void ulink_clear_queue(struct ulink *device);
static int ulink_append_queue(struct ulink *device, struct ulink_cmd *ulink_cmd);
static int ulink_execute_queued_commands(struct ulink *device, int timeout);
static void ulink_print_queue(struct ulink *device);
static int ulink_append_scan_cmd(struct ulink *device,
enum scan_type scan_type,
int scan_size_bits,
uint8_t *tdi,
uint8_t *tdo_start,
uint8_t *tdo,
uint8_t tms_count_start,
uint8_t tms_sequence_start,
uint8_t tms_count_end,
uint8_t tms_sequence_end,
struct jtag_command *origin,
bool postprocess);
static int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count,
uint8_t sequence);
static int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count);
static int ulink_append_get_signals_cmd(struct ulink *device);
static int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low,
uint8_t high);
static int ulink_append_sleep_cmd(struct ulink *device, uint32_t us);
static int ulink_append_configure_tck_cmd(struct ulink *device,
int delay_scan_in,
int delay_scan_out,
int delay_scan_io,
int delay_tck,
int delay_tms);
static int __attribute__((unused)) ulink_append_led_cmd(struct ulink *device, uint8_t led_state);
static int ulink_append_test_cmd(struct ulink *device);
/* OpenULINK TCK frequency helper functions */
static int ulink_calculate_delay(enum ulink_delay_type type, long f, int *delay);
/* Interface between OpenULINK and OpenOCD */
static void ulink_set_end_state(tap_state_t endstate);
static int ulink_queue_statemove(struct ulink *device);
static int ulink_queue_scan(struct ulink *device, struct jtag_command *cmd);
static int ulink_queue_tlr_reset(struct ulink *device, struct jtag_command *cmd);
static int ulink_queue_runtest(struct ulink *device, struct jtag_command *cmd);
static int ulink_queue_reset(struct ulink *device, struct jtag_command *cmd);
static int ulink_queue_pathmove(struct ulink *device, struct jtag_command *cmd);
static int ulink_queue_sleep(struct ulink *device, struct jtag_command *cmd);
static int ulink_queue_stableclocks(struct ulink *device, struct jtag_command *cmd);
static int ulink_post_process_scan(struct ulink_cmd *ulink_cmd);
static int ulink_post_process_queue(struct ulink *device);
/* adapter driver functions */
static int ulink_execute_queue(struct jtag_command *cmd_queue);
static int ulink_khz(int khz, int *jtag_speed);
static int ulink_speed(int speed);
static int ulink_speed_div(int speed, int *khz);
static int ulink_init(void);
static int ulink_quit(void);
/****************************** Global Variables ******************************/
static struct ulink *ulink_handle;
/**************************** USB helper functions ****************************/
/**
* Opens the ULINK device
*
* Currently, only the original ULINK is supported
*
* @param device pointer to struct ulink identifying ULINK driver instance.
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
static int ulink_usb_open(struct ulink **device)
{
ssize_t num_devices, i;
bool found;
struct libusb_device **usb_devices;
struct libusb_device_descriptor usb_desc;
struct libusb_device_handle *usb_device_handle;
num_devices = libusb_get_device_list((*device)->libusb_ctx, &usb_devices);
if (num_devices <= 0)
return ERROR_FAIL;
found = false;
for (i = 0; i < num_devices; i++) {
if (libusb_get_device_descriptor(usb_devices[i], &usb_desc) != 0)
continue;
else if (usb_desc.idVendor == ULINK_VID && usb_desc.idProduct == ULINK_PID) {
found = true;
break;
}
}
if (!found)
return ERROR_FAIL;
if (libusb_open(usb_devices[i], &usb_device_handle) != 0)
return ERROR_FAIL;
libusb_free_device_list(usb_devices, 1);
(*device)->usb_device_handle = usb_device_handle;
(*device)->type = ULINK_1;
return ERROR_OK;
}
/**
* Releases the ULINK interface and closes the USB device handle.
*
* @param device pointer to struct ulink identifying ULINK driver instance.
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
static int ulink_usb_close(struct ulink **device)
{
if (libusb_release_interface((*device)->usb_device_handle, 0) != 0)
return ERROR_FAIL;
libusb_close((*device)->usb_device_handle);
(*device)->usb_device_handle = NULL;
return ERROR_OK;
}
/******************* ULINK CPU (EZ-USB) specific functions ********************/
/**
* Writes '0' or '1' to the CPUCS register, putting the EZ-USB CPU into reset
* or out of reset.
*
* @param device pointer to struct ulink identifying ULINK driver instance.
* @param reset_bit 0 to put CPU into reset, 1 to put CPU out of reset.
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
static int ulink_cpu_reset(struct ulink *device, unsigned char reset_bit)
{
int ret;
ret = libusb_control_transfer(device->usb_device_handle,
(LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
REQUEST_FIRMWARE_LOAD, CPUCS_REG, 0, &reset_bit, 1, LIBUSB_TIMEOUT_MS);
/* usb_control_msg() returns the number of bytes transferred during the
* DATA stage of the control transfer - must be exactly 1 in this case! */
if (ret != 1)
return ERROR_FAIL;
return ERROR_OK;
}
/**
* Puts the ULINK's EZ-USB microcontroller into reset state, downloads
* the firmware image, resumes the microcontroller and re-enumerates
* USB devices.
*
* @param device pointer to struct ulink identifying ULINK driver instance.
* The usb_handle member will be modified during re-enumeration.
* @param filename path to the Intel HEX file containing the firmware image.
* @param delay the delay to wait for the device to re-enumerate.
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
static int ulink_load_firmware_and_renumerate(struct ulink **device,
const char *filename, uint32_t delay)
{
int ret;
/* Basic process: After downloading the firmware, the ULINK will disconnect
* itself and re-connect after a short amount of time so we have to close
* the handle and re-enumerate USB devices */
ret = ulink_load_firmware(*device, filename);
if (ret != ERROR_OK)
return ret;
ret = ulink_usb_close(device);
if (ret != ERROR_OK)
return ret;
usleep(delay);
ret = ulink_usb_open(device);
if (ret != ERROR_OK)
return ret;
return ERROR_OK;
}
/**
* Downloads a firmware image to the ULINK's EZ-USB microcontroller
* over the USB bus.
*
* @param device pointer to struct ulink identifying ULINK driver instance.
* @param filename an absolute or relative path to the Intel HEX file
* containing the firmware image.
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
static int ulink_load_firmware(struct ulink *device, const char *filename)
{
struct image ulink_firmware_image;
int ret;
ret = ulink_cpu_reset(device, CPU_RESET);
if (ret != ERROR_OK) {
LOG_ERROR("Could not halt ULINK CPU");
return ret;
}
ulink_firmware_image.base_address = 0;
ulink_firmware_image.base_address_set = false;
ret = image_open(&ulink_firmware_image, filename, "ihex");
if (ret != ERROR_OK) {
LOG_ERROR("Could not load firmware image");
return ret;
}
/* Download all sections in the image to ULINK */
for (unsigned int i = 0; i < ulink_firmware_image.num_sections; i++) {
ret = ulink_write_firmware_section(device, &ulink_firmware_image, i);
if (ret != ERROR_OK)
return ret;
}
image_close(&ulink_firmware_image);
ret = ulink_cpu_reset(device, CPU_START);
if (ret != ERROR_OK) {
LOG_ERROR("Could not restart ULINK CPU");
return ret;
}
return ERROR_OK;
}
/**
* Send one contiguous firmware section to the ULINK's EZ-USB microcontroller
* over the USB bus.
*
* @param device pointer to struct ulink identifying ULINK driver instance.
* @param firmware_image pointer to the firmware image that contains the section
* which should be sent to the ULINK's EZ-USB microcontroller.
* @param section_index index of the section within the firmware image.
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
static int ulink_write_firmware_section(struct ulink *device,
struct image *firmware_image, int section_index)
{
uint16_t addr, size, bytes_remaining, chunk_size;
uint8_t data[SECTION_BUFFERSIZE];
uint8_t *data_ptr = data;
size_t size_read;
int ret;
size = (uint16_t)firmware_image->sections[section_index].size;
addr = (uint16_t)firmware_image->sections[section_index].base_address;
LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr,
size);
/* Copy section contents to local buffer */
ret = image_read_section(firmware_image, section_index, 0, size, data,
&size_read);
if ((ret != ERROR_OK) || (size_read != size)) {
/* Propagating the return code would return '0' (misleadingly indicating
* successful execution of the function) if only the size check fails. */
return ERROR_FAIL;
}
bytes_remaining = size;
/* Send section data in chunks of up to 64 bytes to ULINK */
while (bytes_remaining > 0) {
if (bytes_remaining > 64)
chunk_size = 64;
else
chunk_size = bytes_remaining;
ret = libusb_control_transfer(device->usb_device_handle,
(LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
REQUEST_FIRMWARE_LOAD, addr, FIRMWARE_ADDR, (unsigned char *)data_ptr,
chunk_size, LIBUSB_TIMEOUT_MS);
if (ret != (int)chunk_size) {
/* Abort if libusb sent less data than requested */
return ERROR_FAIL;
}
bytes_remaining -= chunk_size;
addr += chunk_size;
data_ptr += chunk_size;
}
return ERROR_OK;
}
/************************** Generic helper functions **************************/
/**
* Print state of interesting signals via LOG_INFO().
*
* @param input_signals input signal states as returned by CMD_GET_SIGNALS
* @param output_signals output signal states as returned by CMD_GET_SIGNALS
*/
static void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals)
{
LOG_INFO("ULINK signal states: TDI: %i, TDO: %i, TMS: %i, TCK: %i, TRST: %i,"
" SRST: %i",
(output_signals & SIGNAL_TDI ? 1 : 0),
(input_signals & SIGNAL_TDO ? 1 : 0),
(output_signals & SIGNAL_TMS ? 1 : 0),
(output_signals & SIGNAL_TCK ? 1 : 0),
(output_signals & SIGNAL_TRST ? 0 : 1), /* Inverted by hardware */
(output_signals & SIGNAL_RESET ? 0 : 1)); /* Inverted by hardware */
}
/**************** OpenULINK command generation helper functions ***************/
/**
* Allocate and initialize space in memory for OpenULINK command payload.
*
* @param ulink_cmd pointer to command whose payload should be allocated.
* @param size the amount of memory to allocate (bytes).
* @param direction which payload to allocate.
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
static int ulink_allocate_payload(struct ulink_cmd *ulink_cmd, int size,
enum ulink_payload_direction direction)
{
uint8_t *payload;
payload = calloc(size, sizeof(uint8_t));
if (!payload) {
LOG_ERROR("Could not allocate OpenULINK command payload: out of memory");
return ERROR_FAIL;
}
switch (direction) {
case PAYLOAD_DIRECTION_OUT:
if (ulink_cmd->payload_out) {
LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
free(payload);
return ERROR_FAIL;
} else {
ulink_cmd->payload_out = payload;
ulink_cmd->payload_out_size = size;
}
break;
case PAYLOAD_DIRECTION_IN:
if (ulink_cmd->payload_in_start) {
LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
free(payload);
return ERROR_FAIL;
} else {
ulink_cmd->payload_in_start = payload;
ulink_cmd->payload_in = payload;
ulink_cmd->payload_in_size = size;
/* By default, free payload_in_start in ulink_clear_queue(). Commands
* that do not want this behavior (e. g. split scans) must turn it off
* separately! */
ulink_cmd->free_payload_in_start = true;
}
break;
}
return ERROR_OK;
}
/****************** OpenULINK command queue helper functions ******************/
/**
* Get the current number of bytes in the queue, including command IDs.
*
* @param device pointer to struct ulink identifying ULINK driver instance.
* @param direction the transfer direction for which to get byte count.
* @return the number of bytes currently stored in the queue for the specified
* direction.
*/
static int ulink_get_queue_size(struct ulink *device,
enum ulink_payload_direction direction)
{
struct ulink_cmd *current = device->queue_start;
int sum = 0;
while (current) {
switch (direction) {
case PAYLOAD_DIRECTION_OUT:
sum += current->payload_out_size + 1; /* + 1 byte for Command ID */
break;
case PAYLOAD_DIRECTION_IN:
sum += current->payload_in_size;
break;
}
current = current->next;
}
return sum;
}
/**
* Clear the OpenULINK command queue.
*
* @param device pointer to struct ulink identifying ULINK driver instance.
*/
static void ulink_clear_queue(struct ulink *device)
{
struct ulink_cmd *current = device->queue_start;
struct ulink_cmd *next = NULL;
while (current) {
/* Save pointer to next element */
next = current->next;
/* Free payloads: OUT payload can be freed immediately */
free(current->payload_out);
current->payload_out = NULL;
/* IN payload MUST be freed ONLY if no other commands use the
* payload_in_start buffer */
if (current->free_payload_in_start == true) {
free(current->payload_in_start);
current->payload_in_start = NULL;
current->payload_in = NULL;
}
/* Free queue element */
free(current);
/* Proceed with next element */
current = next;
}
device->commands_in_queue = 0;
device->queue_start = NULL;
device->queue_end = NULL;
}
/**
* Add a command to the OpenULINK command queue.
*
* @param device pointer to struct ulink identifying ULINK driver instance.
* @param ulink_cmd pointer to command that shall be appended to the OpenULINK
* command queue.
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
static int ulink_append_queue(struct ulink *device, struct ulink_cmd *ulink_cmd)
{
int newsize_out, newsize_in;
int ret = ERROR_OK;
newsize_out = ulink_get_queue_size(device, PAYLOAD_DIRECTION_OUT) + 1
+ ulink_cmd->payload_out_size;
newsize_in = ulink_get_queue_size(device, PAYLOAD_DIRECTION_IN)
+ ulink_cmd->payload_in_size;
/* Check if the current command can be appended to the queue */
if ((newsize_out > 64) || (newsize_in > 64)) {
/* New command does not fit. Execute all commands in queue before starting
* new queue with the current command as first entry. */
ret = ulink_execute_queued_commands(device, LIBUSB_TIMEOUT_MS);
if (ret == ERROR_OK)
ret = ulink_post_process_queue(device);
if (ret == ERROR_OK)
ulink_clear_queue(device);
}
if (!device->queue_start) {
/* Queue was empty */
device->commands_in_queue = 1;
device->queue_start = ulink_cmd;
device->queue_end = ulink_cmd;
} else {
/* There are already commands in the queue */
device->commands_in_queue++;
device->queue_end->next = ulink_cmd;
device->queue_end = ulink_cmd;
}
if (ret != ERROR_OK)
ulink_clear_queue(device);
return ret;
}
/**
* Sends all queued OpenULINK commands to the ULINK for execution.
*
* @param device pointer to struct ulink identifying ULINK driver instance.
* @param timeout
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
static int ulink_execute_queued_commands(struct ulink *device, int timeout)
{
struct ulink_cmd *current;
int ret, i, index_out, index_in, count_out, count_in, transferred;
uint8_t buffer[64];
if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO))
ulink_print_queue(device);
index_out = 0;
count_out = 0;
count_in = 0;
for (current = device->queue_start; current; current = current->next) {
/* Add command to packet */
buffer[index_out] = current->id;
index_out++;
count_out++;
for (i = 0; i < current->payload_out_size; i++)
buffer[index_out + i] = current->payload_out[i];
index_out += current->payload_out_size;
count_in += current->payload_in_size;
count_out += current->payload_out_size;
}
/* Send packet to ULINK */
ret = libusb_bulk_transfer(device->usb_device_handle, device->ep_out,
(unsigned char *)buffer, count_out, &transferred, timeout);
if (ret != 0)
return ERROR_FAIL;
if (transferred != count_out)
return ERROR_FAIL;
/* Wait for response if commands contain IN payload data */
if (count_in > 0) {
ret = libusb_bulk_transfer(device->usb_device_handle, device->ep_in,
(unsigned char *)buffer, 64, &transferred, timeout);
if (ret != 0)
return ERROR_FAIL;
if (transferred != count_in)
return ERROR_FAIL;
/* Write back IN payload data */
index_in = 0;
for (current = device->queue_start; current; current = current->next) {
for (i = 0; i < current->payload_in_size; i++) {
current->payload_in[i] = buffer[index_in];
index_in++;
}
}
}
return ERROR_OK;
}
/**
* Convert an OpenULINK command ID (\a id) to a human-readable string.
*
* @param id the OpenULINK command ID.
* @return the corresponding human-readable string.
*/
static const char *ulink_cmd_id_string(uint8_t id)
{
switch (id) {
case CMD_SCAN_IN:
return "CMD_SCAN_IN";
case CMD_SLOW_SCAN_IN:
return "CMD_SLOW_SCAN_IN";
case CMD_SCAN_OUT:
return "CMD_SCAN_OUT";
case CMD_SLOW_SCAN_OUT:
return "CMD_SLOW_SCAN_OUT";
case CMD_SCAN_IO:
return "CMD_SCAN_IO";
case CMD_SLOW_SCAN_IO:
return "CMD_SLOW_SCAN_IO";
case CMD_CLOCK_TMS:
return "CMD_CLOCK_TMS";
case CMD_SLOW_CLOCK_TMS:
return "CMD_SLOW_CLOCK_TMS";
case CMD_CLOCK_TCK:
return "CMD_CLOCK_TCK";
case CMD_SLOW_CLOCK_TCK:
return "CMD_SLOW_CLOCK_TCK";
case CMD_SLEEP_US:
return "CMD_SLEEP_US";
case CMD_SLEEP_MS:
return "CMD_SLEEP_MS";
case CMD_GET_SIGNALS:
return "CMD_GET_SIGNALS";
case CMD_SET_SIGNALS:
return "CMD_SET_SIGNALS";
case CMD_CONFIGURE_TCK_FREQ:
return "CMD_CONFIGURE_TCK_FREQ";
case CMD_SET_LEDS:
return "CMD_SET_LEDS";
case CMD_TEST:
return "CMD_TEST";
default:
return "CMD_UNKNOWN";
}
}
/**
* Print one OpenULINK command to stdout.
*
* @param ulink_cmd pointer to OpenULINK command.
*/
static void ulink_print_command(struct ulink_cmd *ulink_cmd)
{
int i;
printf(" %-22s | OUT size = %i, bytes = 0x",
ulink_cmd_id_string(ulink_cmd->id), ulink_cmd->payload_out_size);
for (i = 0; i < ulink_cmd->payload_out_size; i++)
printf("%02X ", ulink_cmd->payload_out[i]);
printf("\n | IN size = %i\n",
ulink_cmd->payload_in_size);
}
/**
* Print the OpenULINK command queue to stdout.
*
* @param device pointer to struct ulink identifying ULINK driver instance.
*/
static void ulink_print_queue(struct ulink *device)
{
struct ulink_cmd *current;
printf("OpenULINK command queue:\n");
for (current = device->queue_start; current; current = current->next)
ulink_print_command(current);
}
/**
* Perform JTAG scan
*
* Creates and appends a JTAG scan command to the OpenULINK command queue.
* A JTAG scan consists of three steps:
* - Move to the desired SHIFT state, depending on scan type (IR/DR scan).
* - Shift TDI data into the JTAG chain, optionally reading the TDO pin.
* - Move to the desired end state.
*
* @param device pointer to struct ulink identifying ULINK driver instance.
* @param scan_type the type of the scan (IN, OUT, IO (bidirectional)).
* @param scan_size_bits number of bits to shift into the JTAG chain.
* @param tdi pointer to array containing TDI data.
* @param tdo_start pointer to first element of array where TDO data shall be
* stored. See #ulink_cmd for details.
* @param tdo pointer to array where TDO data shall be stored
* @param tms_count_start number of TMS state transitions to perform BEFORE
* shifting data into the JTAG chain.
* @param tms_sequence_start sequence of TMS state transitions that will be
* performed BEFORE shifting data into the JTAG chain.
* @param tms_count_end number of TMS state transitions to perform AFTER
* shifting data into the JTAG chain.
* @param tms_sequence_end sequence of TMS state transitions that will be
* performed AFTER shifting data into the JTAG chain.
* @param origin pointer to OpenOCD command that generated this scan command.
* @param postprocess whether this command needs to be post-processed after
* execution.
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
static int ulink_append_scan_cmd(struct ulink *device, enum scan_type scan_type,
int scan_size_bits, uint8_t *tdi, uint8_t *tdo_start, uint8_t *tdo,
uint8_t tms_count_start, uint8_t tms_sequence_start, uint8_t tms_count_end,
uint8_t tms_sequence_end, struct jtag_command *origin, bool postprocess)
{
struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
int ret, i, scan_size_bytes;
uint8_t bits_last_byte;
if (!cmd)
return ERROR_FAIL;
/* Check size of command. USB buffer can hold 64 bytes, 1 byte is command ID,
* 5 bytes are setup data -> 58 remaining payload bytes for TDI data */
if (scan_size_bits > (58 * 8)) {
LOG_ERROR("BUG: Tried to create CMD_SCAN_IO OpenULINK command with too"
" large payload");
free(cmd);
return ERROR_FAIL;
}
scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
bits_last_byte = scan_size_bits % 8;
if (bits_last_byte == 0)
bits_last_byte = 8;
/* Allocate out_payload depending on scan type */
switch (scan_type) {
case SCAN_IN:
if (device->delay_scan_in < 0)
cmd->id = CMD_SCAN_IN;
else
cmd->id = CMD_SLOW_SCAN_IN;
ret = ulink_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT);
break;
case SCAN_OUT:
if (device->delay_scan_out < 0)
cmd->id = CMD_SCAN_OUT;
else
cmd->id = CMD_SLOW_SCAN_OUT;
ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
break;
case SCAN_IO:
if (device->delay_scan_io < 0)
cmd->id = CMD_SCAN_IO;
else
cmd->id = CMD_SLOW_SCAN_IO;
ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
break;
default:
LOG_ERROR("BUG: ulink_append_scan_cmd() encountered an unknown scan type");
ret = ERROR_FAIL;
break;
}
if (ret != ERROR_OK) {
free(cmd);
return ret;
}
/* Build payload_out that is common to all scan types */
cmd->payload_out[0] = scan_size_bytes & 0xFF;
cmd->payload_out[1] = bits_last_byte & 0xFF;
cmd->payload_out[2] = ((tms_count_start & 0x0F) << 4) | (tms_count_end & 0x0F);
cmd->payload_out[3] = tms_sequence_start;
cmd->payload_out[4] = tms_sequence_end;
/* Setup payload_out for types with OUT transfer */
if ((scan_type == SCAN_OUT) || (scan_type == SCAN_IO)) {
for (i = 0; i < scan_size_bytes; i++)
cmd->payload_out[i + 5] = tdi[i];
}
/* Setup payload_in pointers for types with IN transfer */
if ((scan_type == SCAN_IN) || (scan_type == SCAN_IO)) {
cmd->payload_in_start = tdo_start;
cmd->payload_in = tdo;
cmd->payload_in_size = scan_size_bytes;
}
cmd->needs_postprocessing = postprocess;
cmd->cmd_origin = origin;
/* For scan commands, we free payload_in_start only when the command is
* the last in a series of split commands or a stand-alone command */
cmd->free_payload_in_start = postprocess;
return ulink_append_queue(device, cmd);
}
/**
* Perform TAP state transitions
*
* @param device pointer to struct ulink identifying ULINK driver instance.
* @param count defines the number of TCK clock cycles generated (up to 8).
* @param sequence defines the TMS pin levels for each state transition. The
* Least-Significant Bit is read first.
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
static int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count,
uint8_t sequence)
{
struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
int ret;
if (!cmd)
return ERROR_FAIL;
if (device->delay_clock_tms < 0)
cmd->id = CMD_CLOCK_TMS;
else
cmd->id = CMD_SLOW_CLOCK_TMS;
/* CMD_CLOCK_TMS has two OUT payload bytes and zero IN payload bytes */
ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
if (ret != ERROR_OK) {
free(cmd);
return ret;
}
cmd->payload_out[0] = count;
cmd->payload_out[1] = sequence;
return ulink_append_queue(device, cmd);
}
/**
* Generate a defined amount of TCK clock cycles
*
* All other JTAG signals are left unchanged.
*
* @param device pointer to struct ulink identifying ULINK driver instance.
* @param count the number of TCK clock cycles to generate.
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
static int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count)
{
struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
int ret;
if (!cmd)
return ERROR_FAIL;