1 /*
2 * Cable driver fo Analog Devices, Inc. USB ICEs
3 *
4 * Copyright (C) 2010, Analog Devices, Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 *
21 * Written by Paula R. Bertrand, 2010.
22 */
23
24 #include <sysdep.h>
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <unistd.h>
30
31 #include <urjtag/tap_state.h>
32 #include <urjtag/cable.h>
33 #include <urjtag/chain.h>
34 #include <urjtag/cmd.h>
35
36 #include "libiberty.h"
37
38 #include "generic.h"
39 #include "generic_usbconn.h"
40
41 #include <urjtag/usbconn.h>
42 #include "usbconn/libusb.h"
43
44 /*
45 * Internal Structures
46 */
47
48 /* JTAG TMS/TDI Data */
49 typedef struct
50 {
51 uint8_t tms; /* TMS data */
52 uint8_t tdi; /* TDI data */
53 } tap_pairs;
54
55 /* For collecting data */
56 typedef struct
57 {
58 int32_t idx; /* Index where data is to be collected */
59 int32_t pos; /* Bit position where data is to be collected */
60 } dat_dat;
61
62 /* Master scan control structure */
63 typedef struct
64 {
65 int32_t total; /* Max number of tap pointers */
66 int32_t cur_idx; /* Where to add next, or total */
67 int32_t bit_pos; /* Position to place next bit */
68 int32_t num_dat; /* Total posible data collection points */
69 int32_t cur_dat; /* Index to dat array for data to be collected */
70 int32_t rcv_dat; /* Index to retreive collected data */
71 dat_dat *dat; /* Pointer to data collection points */
72 unsigned char *cmd; /* Pointer to command, which encompasses pairs */
73 tap_pairs *pairs; /* Pointer to tap pairs array */
74 } num_tap_pairs;
75
76 /* Cable params_t structure with our data */
77 typedef struct
78 {
79 uint32_t cur_freq; /* JTAG Frequency */
80 uint16_t version; /* ICE-100B Firmware Version */
81 uint32_t default_scanlen; /* #Scan pairs in scan */
82 uint32_t trigger_scanlen; /* High water mark */
83 int32_t tap_pair_start_idx; /* depends on firmware version */
84 int32_t num_rcv_hdr_bytes; /* Number of data bytes in received raw scan data header */
85 int32_t max_raw_data_tx_items; /* depends on firmware version */
86 int32_t wr_ep; /* USB End Write Point */
87 int32_t wr_timeout; /* USB Write Timeout */
88 int32_t wr_buf_sz; /* USB Write Buffer Size */
89 int32_t r_ep; /* USB End Read Point */
90 int32_t r_timeout; /* USB Read Timeout */
91 int32_t r_buf_sz; /* USB Read Buffer Size */
92 num_tap_pairs tap_info; /* For collecting and sending tap scans */
93 char *firmware_filename; /* The update firmware file name */
94 } params_t;
95
96 /* Emulators's USB Data structure */
97 typedef struct
98 {
99 uint32_t command; /* What to do */
100 uint32_t buffer; /* used for Kit only, always initialized to 0 */
101 uint32_t count; /* Amount of data in bytes to send */
102 } usb_command_block;
103
104
105 /*
106 * Internal Prototypes
107 */
108
109 static int perform_scan (urj_cable_t *cable, uint8_t **rdata);
110 static int do_rawscan (urj_cable_t *cable, uint8_t firstpkt, uint8_t lastpkt,
111 int32_t collect_dof, int32_t dif_cnt, uint8_t *raw_buf,
112 uint8_t *out);
113 static int build_clock_scan (urj_cable_t *cable, int32_t *start_idx, int32_t *num_todo_items);
114 static int add_scan_data (urj_cable_t *cable, int32_t num_bits, char *in, char *out);
115 static void get_recv_data (urj_cable_t *cable, int32_t idx, int32_t dat_idx, uint8_t **rcv_dataptr);
116 static uint16_t do_host_cmd (urj_cable_t *cable, uint8_t cmd, uint8_t param, int32_t r_data);
117 static uint32_t do_single_reg_value (urj_cable_t *cable, uint8_t reg, int32_t r_data,
118 int32_t wr_data, uint32_t data);
119
120
121 /*
122 * Debug Macros
123 */
124
125 #if 0 /* set to 1 to output debug info about scans */
126
127 //#define DSP_SCAN_DATA
128 #define DUMP_EACH_RCV_DATA
129 //#define DSP_SCAN_CAUSE
130 #define DEBUG(...) printf(__VA_ARGS__)
131
132 #else
133
134 #define DEBUG(...)
135
136 #endif
137
138
139 /*
140 * Internal Data, defines and Macros
141 */
142 #define ICE_DEFAULT_SCAN_LEN 0x7FF0 /* Max DIF is 0x2AAA8, but DMA is only 16 bits. */
143 #define ICE_TRIGGER_SCAN_LEN 0x7FD8 /* Start checking for RTI/TLR for xmit */
144
145 #define SELECTIVE_RAW_SCAN_HDR_SZ 12
146
147 #define DAT_SZ 0x8000 /* size allocated for reading data */
148 #define DAT_SZ_INC 0x40 /* size to increase if data full */
149
150 /* USB Emulator Commands */
151 #define HOST_GET_FW_VERSION 0x01 /* get the firmware version */
152 #define HOST_REQUEST_RX_DATA 0x02 /* host request to transmit data */
153 #define HOST_REQUEST_TX_DATA 0x04 /* host request to transmit data */
154 #define HOST_GET_SINGLE_REG 0x08 /* set a JTAG register */
155 #define HOST_SET_SINGLE_REG 0x09 /* set a JTAG register */
156 #define HOST_PROGRAM_FLASH 0x0C /* program flash */
157 #define HOST_HARD_RESET_JTAG_CTRLR 0x0E /* do a hard reset on JTAG controller */
158 #define HOST_SET_TRST 0x1F /* changes TRST Line state */
159 #define HOST_GET_TRST 0x20 /* gets TRST Line state */
160 #define HOST_DO_SELECTIVE_RAW_SCAN 0x21 /* Return only data needed */
161
162 /* Registers */
163 #define REG_AUX 0x00
164 #define REG_SCR 0x04
165 #define REG_FREQ 0x40
166
167 #define SCR_DEFAULT 0x30A0461
168 #define SCR_TRST_BIT 0x0000040
169
170 /* Ice USB controls */
171 #define ICE_100B_WRITE_ENDPOINT 0x06
172 #define ICE_100B_READ_ENDPOINT 0x05
173 #define ICE_100B_USB_WRITE_TIMEOUT 10000
174 #define ICE_100B_USB_READ_TIMEOUT 30000
175 #define ICE_100B_WRITE_BUFFER_SIZE 0x9800
176 #define ICE_100B_READ_BUFFER_SIZE 0x8000
177
178 #define ICE100B_DOC_URL \
179 "http://docs.blackfin.uclinux.org/doku.php?id=hw:jtag:ice100b"
180
181 /* frequency settings for ice-100b */
182 #define MAX_FREQ 4 /* size of freq_set */
183 static const uint8_t freq_set[MAX_FREQ] = { 9, 4, 2, 1 };
184 static const uint32_t avail_freqs[MAX_FREQ] = { 5000000, 10000000, 17000000, 25000000 };
185
186
187 /*
188 * Internal Macros
189 */
190
191 #define adi_usb_read_or_ret(p, buf, len) \
192 do { \
193 int __ret, __actual, __size = (len); \
194 __ret = libusb_bulk_transfer (((urj_usbconn_libusb_param_t *)p)->handle, \
195 cable_params->r_ep | LIBUSB_ENDPOINT_IN, \
196 (unsigned char *)(buf), __size, \
197 &__actual, cable_params->r_timeout); \
198 if (__ret || __actual != __size) \
199 { \
200 urj_error_IO_set (_("%s: unable to read from usb to " #buf ": %i;" \
201 "wanted %i bytes but only received %i bytes"), \
202 __func__, __ret, __size, __actual); \
203 return URJ_STATUS_FAIL; \
204 } \
205 } while (0)
206
207 #define adi_usb_write_or_ret(p, buf, len) \
208 do { \
209 int __ret, __actual, __size = (len); \
210 __ret = libusb_bulk_transfer (((urj_usbconn_libusb_param_t *)p)->handle, \
211 cable_params->wr_ep, \
212 (unsigned char *)(buf), __size, \
213 &__actual, cable_params->wr_timeout); \
214 if (__ret || __actual != __size) \
215 { \
216 urj_error_IO_set (_("%s: unable to write from " #buf " to usb: %i;" \
217 "wanted %i bytes but only wrote %i bytes"), \
218 __func__, __ret, __size, __actual); \
219 return URJ_STATUS_FAIL; \
220 } \
221 } while (0)
222
223 /*
224 * System Interface Functions
225 */
226
227 /*
228 * Gets available Frequency index. Currently only used by ICE-100B
229 */
adi_get_freq(uint32_t freq,int arr_sz,const uint32_t * freq_arr)230 static int adi_get_freq (uint32_t freq, int arr_sz, const uint32_t *freq_arr)
231 {
232 int i;
233
234 /* Verify Frequency is valid */
235 for (i = 0; i < arr_sz; i++)
236 {
237 if (freq == freq_arr[i])
238 { /* spot on */
239 break;
240 }
241 else if (freq < freq_arr[i])
242 { /* an in between frequency */
243 if (i > 0)
244 i--;
245 break;
246 }
247 }
248
249 if (i == arr_sz)
250 { /* must of entered something above the max! */
251 i--;
252 }
253
254 return i;
255 }
256
257 /*
258 * Sets ICE-X Frequency
259 */
ice100b_set_freq(urj_cable_t * cable,uint32_t freq)260 static void ice100b_set_freq (urj_cable_t *cable, uint32_t freq)
261 {
262 params_t *params = cable->params;
263
264 /* Verify Frequency is valid */
265 if (freq != params->cur_freq)
266 { /* only change if different from current settings */
267 int idx = adi_get_freq (freq, MAX_FREQ, &avail_freqs[0]);
268
269 if (avail_freqs[idx] != params->cur_freq)
270 { /* only change if different from current settings
271 * this call's frequency may have been not one of
272 * the defined settings, but ends up there */
273 params->cur_freq = freq;
274 do_single_reg_value (cable, REG_FREQ, 1, 1, freq_set[idx]);
275 cable->frequency = params->cur_freq;
276 }
277 }
278 }
279
280 /*
281 * This function sets us up the cable and data
282 */
adi_connect(urj_cable_t * cable,const urj_param_t * params[])283 static int adi_connect (urj_cable_t *cable, const urj_param_t *params[])
284 {
285 int i, ret;
286 params_t *cable_params;
287
288 /* Handle Parameters (if need be)
289 * This will connect us to our Driver, parameters
290 * include VID & PID, */
291 ret = urj_tap_cable_generic_usbconn_connect (cable, params);
292 if (ret != URJ_STATUS_OK)
293 return ret;
294 /* Do our set up with our parameters */
295 cable_params = malloc (sizeof (*cable_params));
296 if (!cable_params)
297 {
298 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("malloc(%zd) fails"),
299 sizeof (*cable_params));
300 cable->link.usb->driver->free (cable->link.usb);
301 return URJ_STATUS_FAIL;
302 }
303
304 cable_params->tap_info.dat = malloc (sizeof (dat_dat) * DAT_SZ);
305 if (!cable_params->tap_info.dat)
306 {
307 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("malloc(%zd) fails"),
308 sizeof (dat_dat) * DAT_SZ);
309 cable->link.usb->driver->free (cable->link.usb);
310 free (cable_params);
311 return URJ_STATUS_FAIL;
312 }
313
314 /* Initialize receive data array to unused */
315 for (i = 0; i < DAT_SZ; ++i)
316 {
317 cable_params->tap_info.dat[i].idx = -1;
318 cable_params->tap_info.dat[i].pos = -1;
319 }
320
321 cable_params->cur_freq = 0;
322 cable_params->version = 0;
323 cable_params->tap_info.total = 0;
324 cable_params->tap_info.cur_idx = 0;
325 cable_params->tap_info.bit_pos = 0;
326 cable_params->tap_info.num_dat = DAT_SZ;
327 cable_params->tap_info.rcv_dat = -1;
328 cable_params->tap_info.cur_dat = -1;
329 cable_params->tap_info.pairs = NULL;
330 cable_params->tap_info.cmd = NULL;
331
332 /* exchange generic cable parameters with our private parameter set */
333 free (cable->params);
334 cable->params = cable_params;
335
336 return URJ_STATUS_OK;
337 }
338
339 struct flash_block
340 {
341 uint32_t addr;
342 int bytes;
343 uint8_t *data;
344 struct flash_block *next;
345 };
346
347 /* Macros for converting between hex and binary. */
348
349 #define NIBBLE(x) (hex_value (x))
350 #define HEX2(buffer) ((NIBBLE ((buffer)[0]) << 4) + NIBBLE ((buffer)[1]))
351 #define HEX4(buffer) ((HEX2 (buffer) << 8) + HEX2 ((buffer) + 2))
352 #define ISHEX(x) (hex_p (x))
353 #define ISHEX2(buffer) (ISHEX ((buffer)[0]) && ISHEX ((buffer)[1]))
354 #define ISHEX4(buffer) (ISHEX2 (buffer) && ISHEX2 ((buffer + 2)))
355
356
357 static int
ice_read_hex_file(const char * filename,struct flash_block ** flash_list)358 ice_read_hex_file (const char *filename, struct flash_block **flash_list)
359 {
360 FILE *hex_file;
361 char *input_line, *p;
362 size_t len;
363 int lineno, i;
364 bool done = false;
365 struct flash_block *last_flash_block = NULL, *q;
366 int base_address = 0;
367
368 hex_file = fopen (filename, FOPEN_R);
369 if (!hex_file)
370 {
371 urj_error_IO_set (_("Unable to open file `%s'"), filename);
372 return URJ_STATUS_FAIL;
373 }
374
375 hex_init ();
376
377 input_line = NULL;
378 len = 0;
379 lineno = 0;
380 while (getline (&input_line, &len, hex_file) != -1 && !done)
381 {
382 int byte_count, address, record_type, checksum;
383 int sum;
384
385 lineno++;
386 p = input_line;
387 /* A line should contain
388 1. start code : 1 character ':'
389 2. byte count : 2 hex digits
390 3. address : 4 hex digits
391 4. record type: 2 hex digits
392 5. data : 2n hex digits
393 6. checksum : 2 hex digits */
394 if (len < 11)
395 {
396 urj_error_set (URJ_ERROR_FIRMWARE,
397 _("Line %d is too short in file %s"),
398 lineno, filename);
399 free (input_line);
400 return URJ_STATUS_FAIL;
401 }
402
403 if (*p++ != ':')
404 {
405 urj_error_set (URJ_ERROR_FIRMWARE,
406 _("Invalid start code on line %d in file %s"),
407 lineno, filename);
408 free (input_line);
409 return URJ_STATUS_FAIL;
410 }
411
412 if (!ISHEX2 (p))
413 {
414 urj_error_set (URJ_ERROR_FIRMWARE,
415 _("Bad byte count on line %d in file %s"),
416 lineno, filename);
417 free (input_line);
418 return URJ_STATUS_FAIL;
419 }
420 byte_count = HEX2 (p);
421 p += 2;
422 sum = byte_count;
423
424 if (!ISHEX4 (p))
425 {
426 urj_error_set (URJ_ERROR_FIRMWARE,
427 _("Bad address on line %d in file %s"),
428 lineno, filename);
429 free (input_line);
430 return URJ_STATUS_FAIL;
431 }
432 address = HEX4 (p);
433 p += 4;
434 sum += (address >> 8) + (address & 0xff);
435 sum &= 0xff;
436
437 /* record type */
438 if (!ISHEX2 (p))
439 goto bad_record_type;
440 record_type = HEX2 (p);
441 p += 2;
442 sum += record_type;
443 sum &= 0xff;
444
445 switch (record_type)
446 {
447 case 0:
448 q = last_flash_block;
449
450 if (!q
451 || q->addr + q->bytes != base_address + address)
452 {
453 q = malloc (sizeof (*q));
454 if (!q)
455 {
456 urj_error_set (URJ_ERROR_OUT_OF_MEMORY,
457 _("malloc (%zd) failed"), sizeof (*q));
458 free (input_line);
459 return URJ_STATUS_FAIL;
460 }
461 q->addr = base_address + address;
462 q->data = NULL;
463 q->bytes = 0;
464 q->next = NULL;
465
466 if (last_flash_block)
467 last_flash_block->next = q;
468 else
469 *flash_list = q;
470 last_flash_block = q;
471 }
472
473 q->data = realloc (q->data, q->bytes + byte_count);
474 if (!q->data)
475 {
476 urj_error_set (URJ_ERROR_OUT_OF_MEMORY,
477 _("realloc (%d) failed"),
478 q->bytes + byte_count);
479 free (input_line);
480 return URJ_STATUS_FAIL;
481 }
482
483 for (i = 0; i < byte_count; i++)
484 {
485 int data;
486
487 if (!ISHEX2 (p))
488 {
489 urj_error_set (URJ_ERROR_FIRMWARE,
490 _("Bad HEX data %c%c on line %d in file %s"),
491 p[0], p[1], lineno, filename);
492 free (input_line);
493 return URJ_STATUS_FAIL;
494 }
495 data = HEX2 (p);
496 q->data[q->bytes] = data;
497 q->bytes++;
498 p += 2;
499 sum += data;
500 }
501 break;
502
503 case 1:
504 done = true;
505 break;
506
507 case 2:
508 /* fall through */
509 case 4:
510 if (!ISHEX4 (p))
511 {
512 urj_error_set (URJ_ERROR_FIRMWARE,
513 _("Bad extended segment address on line %d in file %s"),
514 lineno, filename);
515 free (input_line);
516 return URJ_STATUS_FAIL;
517 }
518 base_address = HEX4 (p);
519 sum += (base_address >> 8) + (base_address & 0xff);
520 sum &= 0xff;
521 base_address <<= (record_type == 2 ? 4 : 16);
522 p += 4;
523 break;
524
525 default:
526 bad_record_type:
527 urj_error_set (URJ_ERROR_FIRMWARE,
528 _("Bad HEX record type on line %d in file %s"),
529 lineno, filename);
530 free (input_line);
531 return URJ_STATUS_FAIL;
532 }
533
534 if (!ISHEX2 (p))
535 {
536 urj_error_set (URJ_ERROR_FIRMWARE,
537 _("Bad HEX checksum on line %d in file %s"),
538 lineno, filename);
539 free (input_line);
540 return URJ_STATUS_FAIL;
541 }
542 checksum = HEX2 (p);
543 if (((sum + checksum) & 0xff) != 0)
544 {
545 urj_error_set (URJ_ERROR_FIRMWARE,
546 _("The checksum is not correct on line %d in file %s"),
547 lineno, filename);
548 free (input_line);
549 return URJ_STATUS_FAIL;
550 }
551 }
552
553 free (input_line);
554 return URJ_STATUS_OK;
555 }
556
557 /* Calculate the CRC using forward CRC-16-CCITT algorithm. */
558
559 static uint16_t
ice_calculate_crc(struct flash_block * p)560 ice_calculate_crc (struct flash_block *p)
561 {
562 uint16_t crc = 0xffff;
563
564 while (p)
565 {
566 int i;
567
568 for (i = 0; i < p->bytes; i++)
569 {
570 uint8_t byte = p->data[i];
571 int j;
572
573 for (j = 0; j < 8; j++)
574 {
575 bool add = ((crc >> 15) != (byte >> 7));
576 crc <<= 1;
577 byte <<= 1;
578 if (add)
579 crc ^= 0x1021;
580 }
581 }
582 p = p->next;
583 }
584
585 return crc;
586 }
587
588 static int
ice_send_flash_data(urj_cable_t * cable,struct flash_block * p,uint16_t crc)589 ice_send_flash_data (urj_cable_t *cable, struct flash_block *p, uint16_t crc)
590 {
591 params_t *cable_params = cable->params;
592 /* ICE_100B_READ_BUFFER_SIZE / 4 (i.e. 0x2000) was chosen by experiments.
593 It might not be related to ICE_100B_READ_BUFFER_SIZE at all. */
594 uint8_t buffer[ICE_100B_READ_BUFFER_SIZE / 4];
595 uint8_t first = 1, last = 0;
596
597 while (p)
598 {
599 int remaining = p->bytes;
600 uint32_t address = p->addr;
601
602 while (remaining)
603 {
604 usb_command_block usb_cmd_blk;
605 uint32_t count;
606
607 urj_log (URJ_LOG_LEVEL_NORMAL, "updating ...\n");
608
609 if (remaining < ICE_100B_READ_BUFFER_SIZE / 4 - 16)
610 count = remaining;
611 else
612 count = ICE_100B_READ_BUFFER_SIZE / 4 - 16;
613 remaining -= count;
614 if (remaining == 0)
615 last = 1;
616
617 buffer[0] = first;
618 buffer[1] = last;
619 buffer[2] = HOST_PROGRAM_FLASH;
620 buffer[3] = 0;
621 memcpy (buffer + 4, &address, 4);
622 memcpy (buffer + 8, &count, 4);
623 memcpy (buffer + 12, &crc, 2);
624 memcpy (buffer + 16, p->data + p->bytes - remaining - count, count);
625
626 usb_cmd_blk.command = HOST_REQUEST_TX_DATA;
627 usb_cmd_blk.count = count + 16;
628 usb_cmd_blk.buffer = 0;
629 adi_usb_write_or_ret (cable->link.usb->params, &usb_cmd_blk, sizeof (usb_cmd_blk));
630
631 adi_usb_write_or_ret (cable->link.usb->params, buffer, usb_cmd_blk.count);
632
633 first = 0;
634
635 address += count;
636 }
637
638 p = p->next;
639 }
640
641 urj_log (URJ_LOG_LEVEL_NORMAL, "done\n");
642
643 return URJ_STATUS_OK;
644 }
645
646 /* The CRC is stored in the output buffer after programming the firmware
647 into the flash. Read the buffer after programming get the CRC. */
648
649 static int
ice_firmware_crc(urj_cable_t * cable,uint16_t * p)650 ice_firmware_crc (urj_cable_t *cable, uint16_t *p)
651 {
652 params_t *cable_params = cable->params;
653 usb_command_block usb_cmd_blk;
654
655 usb_cmd_blk.command = HOST_REQUEST_RX_DATA;
656 usb_cmd_blk.count = 2;
657 usb_cmd_blk.buffer = 0;
658
659 adi_usb_write_or_ret (cable->link.usb->params, &usb_cmd_blk, sizeof (usb_cmd_blk));
660
661 adi_usb_read_or_ret (cable->link.usb->params, p, sizeof (*p));
662
663 return URJ_STATUS_OK;
664 }
665
666 static int
ice_update_firmware(urj_cable_t * cable,const char * filename)667 ice_update_firmware (urj_cable_t *cable, const char *filename)
668 {
669 struct flash_block *flash_list = NULL, *p;
670 unsigned short crc1, crc2;
671 int ret;
672
673 urj_log (URJ_LOG_LEVEL_NORMAL, _("Updating to firmware %s\n"), filename);
674
675 if ((ret = ice_read_hex_file (filename, &flash_list)) != URJ_STATUS_OK)
676 return ret;
677
678 crc1 = ice_calculate_crc (flash_list);
679
680 ret = ice_send_flash_data (cable, flash_list, crc1);
681 if (ret != URJ_STATUS_OK)
682 return ret;
683
684 if ((ret = ice_firmware_crc (cable, &crc2)) != URJ_STATUS_OK)
685 return ret;
686
687 while (flash_list)
688 {
689 p = flash_list->next;
690 free (flash_list->data);
691 free (flash_list);
692 flash_list = p;
693 }
694
695 if (crc1 == crc2)
696 {
697 return URJ_STATUS_OK;
698 }
699 else
700 {
701 urj_error_set (URJ_ERROR_FIRMWARE, _("CRCs do NOT match"));
702 return URJ_STATUS_FAIL;
703 }
704 }
705
706 /*
707 * This function sets us up the cable and data
708 */
ice_connect(urj_cable_t * cable,const urj_param_t * params[])709 static int ice_connect (urj_cable_t *cable, const urj_param_t *params[])
710 {
711 params_t *cable_params;
712 int ret, i;
713
714 if ((ret = adi_connect (cable, params)) != URJ_STATUS_OK)
715 return ret;
716
717 cable_params = cable->params;
718 cable_params->default_scanlen = ICE_DEFAULT_SCAN_LEN;
719 cable_params->trigger_scanlen = ICE_TRIGGER_SCAN_LEN;
720 cable_params->wr_ep = ICE_100B_WRITE_ENDPOINT;
721 cable_params->r_ep = ICE_100B_READ_ENDPOINT;
722 cable_params->wr_timeout = ICE_100B_USB_WRITE_TIMEOUT;
723 cable_params->r_timeout = ICE_100B_USB_READ_TIMEOUT;
724 cable_params->wr_buf_sz = ICE_100B_WRITE_BUFFER_SIZE;
725 cable_params->r_buf_sz = ICE_100B_READ_BUFFER_SIZE;
726 cable_params->firmware_filename = NULL;
727
728 if (params != NULL)
729 for (i = 0; params[i] != NULL; i++)
730 {
731 switch (params[i]->key)
732 {
733 case URJ_CABLE_PARAM_KEY_FIRMWARE:
734 cable_params->firmware_filename = strdup (params[i]->value.string);
735 if (!cable_params->firmware_filename)
736 {
737 urj_log (URJ_LOG_LEVEL_ERROR,
738 _("strdup (%s) fails\n"), params[i]->value.string);
739 return URJ_STATUS_FAIL;
740 }
741 break;
742 default:
743 break;
744 }
745 }
746
747 return URJ_STATUS_OK;
748 }
749
750 /*
751 * This function actually connects us to our ICE
752 */
ice_init(urj_cable_t * cable)753 static int ice_init (urj_cable_t *cable)
754 {
755 params_t *cable_params = cable->params;
756 int ret;
757
758 /* Open usb conn port */
759 if (urj_tap_usbconn_open (cable->link.usb))
760 return URJ_STATUS_FAIL;
761
762 cable_params->version = do_host_cmd (cable, HOST_GET_FW_VERSION, 0, 1);
763 do_host_cmd (cable, HOST_HARD_RESET_JTAG_CTRLR, 0, 0);
764 urj_log (URJ_LOG_LEVEL_NORMAL, _("%s firmware version is %d.%d.%d\n"),
765 cable->driver->name,
766 ((cable_params->version >> 8) & 0xFF),
767 ((cable_params->version >> 4) & 0x0F),
768 ((cable_params->version) & 0x0F));
769
770 if (cable_params->version < 0x0107)
771 {
772 urj_log (URJ_LOG_LEVEL_ERROR,
773 _("The firmware on the ICE-100B needs to be updated. "
774 "Please go to <%s> to learn how to update the firmware.\n"), ICE100B_DOC_URL);
775 }
776
777 if (cable_params->firmware_filename)
778 {
779 ret = ice_update_firmware (cable, cable_params->firmware_filename);
780
781 if (ret == URJ_STATUS_OK)
782 {
783 urj_log (URJ_LOG_LEVEL_NORMAL,
784 _("The firmware has been updated successfully. "
785 "Please unplug the ICE-100B cable and reconnect it to finish the update process.\n"));
786 }
787 else
788 {
789 urj_log_error_describe (URJ_LOG_LEVEL_ERROR);
790 urj_log (URJ_LOG_LEVEL_ERROR,
791 _("The firmware failed to update.\n"));
792 }
793 }
794
795 if (cable_params->version < 0x0107 || cable_params->firmware_filename)
796 {
797 return URJ_STATUS_FAIL;
798 }
799
800 /* Set frequency to lowest value */
801 ice100b_set_freq (cable, avail_freqs[0]);
802 cable_params->tap_pair_start_idx = SELECTIVE_RAW_SCAN_HDR_SZ;
803 cable_params->max_raw_data_tx_items = cable_params->wr_buf_sz - cable_params->tap_pair_start_idx;
804 cable_params->num_rcv_hdr_bytes = cable_params->tap_pair_start_idx;
805
806 return URJ_STATUS_OK;
807 }
808
809 /*
810 * Set our data free!
811 */
ice_cable_free(urj_cable_t * cable)812 static void ice_cable_free (urj_cable_t *cable)
813 {
814 params_t *cable_params = cable->params;
815 num_tap_pairs *tap_info = &cable_params->tap_info;
816
817 if (tap_info->pairs != NULL)
818 {
819 free (tap_info->cmd);
820 free (tap_info->dat);
821 tap_info->dat = NULL;
822 tap_info->pairs = NULL;
823 tap_info->cmd = NULL;
824 tap_info->total = 0;
825 tap_info->cur_idx = 0;
826 tap_info->bit_pos = 0;
827 tap_info->num_dat = 0;
828 tap_info->cur_dat = -1;
829 tap_info->rcv_dat = -1;
830 }
831 free (cable_params->firmware_filename);
832 urj_tap_cable_generic_usbconn_free (cable);
833 }
834
835 /*
836 * This function adds padding of 0 to tdi and tms
837 */
add_zero_padding(num_tap_pairs * tap_ptr,int32_t start_index,uint8_t bit_pos,int32_t pad_amount)838 static void add_zero_padding (num_tap_pairs *tap_ptr, int32_t start_index, uint8_t bit_pos, int32_t pad_amount)
839 {
840 int32_t n = start_index;
841 int32_t count = pad_amount;
842 uint8_t pos = bit_pos;
843
844 /* this clears any additional bits to a byte boundary */
845 while (count--)
846 {
847 tap_ptr->pairs[n].tms &= ~(pos);
848 tap_ptr->pairs[n].tdi &= ~(pos);
849 pos >>= 1;
850 if (!pos)
851 {
852 n++;
853 break;
854 }
855 }
856
857 /* now we just need to set any additional bytes to 0
858 * this should be quicker than continuing with each
859 * individual bit */
860 count /= 8;
861
862 while (count--)
863 {
864 tap_ptr->pairs[n].tms = 0;
865 tap_ptr->pairs[n].tdi = 0;
866 n++;
867 }
868 }
869
870 /*
871 * takes tdi and tms and sends it out right away
872 */
adi_clock(urj_cable_t * cable,int32_t tms,int32_t tdi,int32_t cnt)873 static void adi_clock (urj_cable_t *cable, int32_t tms, int32_t tdi, int32_t cnt)
874 {
875 int32_t n = cnt;
876 int32_t i = 0;
877 int32_t padding = 32 - (cnt % 32);
878 params_t *cable_params = cable->params;
879 num_tap_pairs *tap_info = &cable_params->tap_info;
880 uint8_t firstpkt = 1;
881 uint8_t lastpkt = 1;
882 int32_t collect_data = 0;
883 uint8_t *in = NULL;
884 uint8_t *out = NULL;
885 int32_t dif_cnt = 0;
886 uint8_t bit_set = 0x80;
887
888 if (tap_info->pairs == NULL)
889 {
890 unsigned char *cmd;
891 int32_t new_sz = cnt + padding;
892
893 cmd = malloc ((sizeof (tap_pairs) * new_sz) + 1 + cable_params->tap_pair_start_idx);
894 if (cmd == NULL)
895 {
896 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("malloc(%zd) fails"),
897 (sizeof (tap_pairs) * new_sz) + 1 + cable_params->tap_pair_start_idx);
898 return;
899 }
900
901 /* point our pairs to the space that was allocated */
902 tap_info->pairs = (tap_pairs *)(cmd + cable_params->tap_pair_start_idx); /* new pointer */
903
904 /* initialize some of our structure */
905 tap_info->cur_idx = 1;
906 tap_info->total = cnt + padding;
907 tap_info->num_dat = 0;
908 tap_info->pairs[i].tms = 0;
909 tap_info->pairs[i].tdi = 0;
910
911 /* go through and set tms and tdi to the appropriate values */
912 while (n-- > 0)
913 {
914 tap_info->pairs[i].tms |= tms ? bit_set : 0;
915 tap_info->pairs[i].tdi |= tdi ? bit_set : 0;
916 bit_set >>= 1;
917 if (!bit_set)
918 {
919 /* start over again */
920 bit_set = 0x80;
921 i++;
922 tap_info->pairs[i].tms = 0;
923 tap_info->pairs[i].tdi = 0;
924 }
925 }
926
927 add_zero_padding (tap_info, i, bit_set, padding);
928
929 tap_info->cur_idx = new_sz / 8; /* we scan in multiples of 32 */
930 dif_cnt = (new_sz * 2) / 8; /* dif_cnt is both TMS and TDI so *2 and then figure out how many BYTES */
931 in = (uint8_t *)tap_info->pairs;
932 out = malloc ( 16 ); /* allocate just for header */
933
934 do_rawscan (cable, firstpkt, lastpkt, collect_data, dif_cnt, &in[0] - cable_params->tap_pair_start_idx, out);
935
936 /* free memory */
937 if (tap_info->pairs)
938 {
939 free (cmd);
940 tap_info->pairs = NULL;
941 tap_info->cmd = NULL;
942 }
943
944 free (out);
945
946 /* reset some of our structure variables */
947 tap_info->total = 0;
948 tap_info->cur_idx = 0;
949 tap_info->bit_pos = 0;
950 tap_info->cur_dat = -1;
951 tap_info->rcv_dat = -1;
952 }
953 else
954 {
955 /* should never get here unless someone forgot to free our pairs */
956 urj_warning (_("tap_info->pairs should be NULL but it is not."));
957 return;
958 }
959 }
960
961 /*
962 * Function is unused!!!
963 */
adi_get_tdo(urj_cable_t * cable)964 static int adi_get_tdo (urj_cable_t *cable)
965 {
966 urj_log (URJ_LOG_LEVEL_ERROR, _("%s function is not supported"), __func__);
967
968 return URJ_STATUS_FAIL;
969 }
970
971 /*
972 * Function is unused!!!
973 */
adi_transfer(urj_cable_t * cable,int len,const char * in,char * out)974 static int adi_transfer (urj_cable_t *cable, int len, const char *in, char *out)
975 {
976 urj_log (URJ_LOG_LEVEL_ERROR, _("%s function is not supported"), __func__);
977
978 return URJ_STATUS_FAIL;
979 }
980
981 /*
982 * Creates the scan, sends the scan and collects data
983 */
adi_flush(urj_cable_t * cable,urj_cable_flush_amount_t how_much)984 static void adi_flush (urj_cable_t *cable, urj_cable_flush_amount_t how_much)
985 {
986 params_t *cable_params = cable->params;
987 num_tap_pairs *tap_info = &cable_params->tap_info;
988 int32_t scan_out = (how_much == URJ_TAP_CABLE_COMPLETELY) ? 3 : 0; /* Assigned a number for debug, !0 will do scan */
989 int32_t tdo_idx = -1;
990 int32_t i, j, k, n;
991 uint8_t *tdo_ptr = NULL;
992
993 if (how_much == URJ_TAP_CABLE_OPTIONALLY)
994 {
995 return;
996 }
997 else if (cable->todo.num_items == 0)
998 { /* If we get here, there are no data to collect (since we forced a scan previously) */
999 if (how_much == URJ_TAP_CABLE_COMPLETELY && tap_info->pairs)
1000 {
1001 #ifdef DSP_SCAN_DATA
1002 int32_t x = 0, z;
1003 char tms[64], tdi[64];
1004
1005 DEBUG ("::::Flushing Scan Output\n");
1006 z = 0;
1007 while (z <= tap_info->cur_idx)
1008 {
1009 sprintf (&tms[x], " %2.2X", tap_info->pairs[z].tms);
1010 sprintf (&tdi[x], " %2.2X", tap_info->pairs[z].tdi);
1011 x += 3;
1012 z++;
1013 if ((z & 0x0F) == 0)
1014 {
1015 DEBUG ("\t%s\n\t%s\n\n", tms, tdi);
1016 x = 0;
1017 }
1018 }
1019 if (x > 0)
1020 DEBUG ("\t%s\n\t%s\n\n", tms, tdi);
1021 #endif
1022 tdo_ptr = NULL;
1023 perform_scan (cable, &tdo_ptr);
1024 if (tdo_ptr)
1025 {
1026 free (tdo_ptr);
1027 tdo_ptr = NULL;
1028 }
1029 if (tap_info->pairs)
1030 {
1031 free (tap_info->cmd);
1032 tap_info->pairs = NULL;
1033 tap_info->cmd = NULL;
1034 }
1035 tap_info->total = 0;
1036 tap_info->cur_idx = 0;
1037 tap_info->bit_pos = 0;
1038 tap_info->cur_dat = -1;
1039 tap_info->rcv_dat = -1;
1040 }
1041 return;
1042 }
1043
1044 while (cable->todo.num_items > 0)
1045 {
1046 urj_cable_queue_info_t *ptr_todo = &cable->todo;
1047 urj_cable_queue_info_t *ptr_done = &cable->done;
1048 urj_cable_queue_t *todo_data;
1049 urj_cable_queue_t *done_data;
1050
1051 for (j = i = cable->todo.next_item, n = 0; n < cable->todo.num_items; n++)
1052 {
1053 todo_data = &ptr_todo->data[i];
1054
1055 switch (todo_data->action)
1056 { /* build the scan */
1057 case URJ_TAP_CABLE_CLOCK:
1058 build_clock_scan (cable, &i, &n);
1059 break;
1060 case URJ_TAP_CABLE_GET_TDO:
1061 if (!scan_out)
1062 scan_out = 1; /* Assigned a number for debug, !0 will do scan */
1063 break;
1064 case URJ_TAP_CABLE_SET_SIGNAL:
1065 /* process it after */
1066 break;
1067 case URJ_TAP_CABLE_GET_SIGNAL:
1068 /* process it after */
1069 break;
1070 case URJ_TAP_CABLE_TRANSFER:
1071 add_scan_data (cable, todo_data->arg.transfer.len,
1072 todo_data->arg.transfer.in,
1073 todo_data->arg.transfer.out);
1074 if (!scan_out && todo_data->arg.transfer.out)
1075 scan_out = 2; /* Assigned a number for debug, !0 will do scan */
1076 break;
1077 default:
1078 DEBUG("default action == %d; i = %d, n = %d\n", todo_data->action, i, n);
1079 break;
1080 }
1081
1082 i++;
1083 if (i >= ptr_todo->max_items)
1084 { /* wrap around buffer! */
1085 i = 0;
1086 }
1087 }
1088
1089 if (!scan_out && cable->chain)
1090 { /* Scan out if we reached our trigger point and we have
1091 * access to tap state and it is RTI or TLR */
1092 if (tap_info->cur_idx >= cable_params->trigger_scanlen &&
1093 (cable->chain->state == URJ_TAP_STATE_RUN_TEST_IDLE ||
1094 cable->chain->state == URJ_TAP_STATE_RESET))
1095 {
1096 scan_out = 7; /* Assigned a number for debug, !0 will do scan */
1097 }
1098 else if (tap_info->cur_idx >= cable_params->trigger_scanlen)
1099 { /* Else scan out if we reached our trigger point */
1100 scan_out = 6; /* Assigned a number for debug, !0 will do scan */
1101 }
1102 else if (tap_info->cur_idx >= cable_params->default_scanlen)
1103 {
1104 urj_log (URJ_LOG_LEVEL_ERROR,
1105 _("FAULT! idx overflow!! idx = %d and max should be %#X\n"),
1106 tap_info->cur_idx, cable_params->default_scanlen);
1107 }
1108 }
1109
1110 if (tap_info->pairs && scan_out)
1111 {
1112 #ifdef DSP_SCAN_DATA
1113 int32_t x = 0, z;
1114 char tms[128], tdi[128];
1115
1116 z = 0;
1117 while (z <= tap_info->cur_idx)
1118 {
1119 sprintf (&tms[x], " %2.2X", tap_info->pairs[z].tms);
1120 sprintf (&tdi[x], " %2.2X", tap_info->pairs[z].tdi);
1121 x += 3;
1122 z++;
1123 if ((z & 0x0F) == 0)
1124 {
1125 DEBUG ("\t%s\n\t%s\n\n", tms, tdi);
1126 x = 0;
1127 }
1128 }
1129 if (x > 0)
1130 DEBUG ("\t%s\n\t%s\n\n", tms, tdi);
1131 #endif
1132 #ifdef DSP_SCAN_CAUSE
1133 DEBUG ("Scan Output because ");
1134 switch (scan_out)
1135 {
1136 case 1: DEBUG ("flush to get TDO set"); break;
1137 case 2: DEBUG ("need to read data back"); break;
1138 case 3: DEBUG ("flush is to be complete"); break;
1139 case 4: DEBUG ("state is Run-Test-Idle"); break;
1140 case 5: DEBUG ("state is Test-Logic-Reset"); break;
1141 case 6: DEBUG ("there's a lot of data"); break;
1142 case 7: DEBUG ("there's a lot of data & in a reset or idle state"); break
1143 default: DEBUG ("Duh?"); break;
1144 }
1145 #endif
1146 tdo_ptr = NULL;
1147 perform_scan (cable, &tdo_ptr);
1148 }
1149 #ifdef DSP_SCAN_DATA
1150 else
1151 DEBUG ("Skipping sending out scan, Idx = %d\n", tap_info->cur_idx);
1152 #endif
1153 /* Here we send out the scan/cmd
1154 * need to look at how_much */
1155 while (j != i)
1156 {
1157 todo_data = &ptr_todo->data[j];
1158
1159 switch (todo_data->action)
1160 { /* Pick up data if need be */
1161 case URJ_TAP_CABLE_CLOCK:
1162 /* Nothing needs to be done */
1163 break;
1164 case URJ_TAP_CABLE_GET_TDO:
1165 { /* get last bit */
1166 k = urj_tap_cable_add_queue_item (cable, &cable->done);
1167 done_data = &ptr_done->data[k];
1168
1169 done_data->action = URJ_TAP_CABLE_GET_TDO;
1170 if (tdo_ptr && (tdo_idx != -1) && (tdo_idx <= tap_info->cur_dat))
1171 {
1172 int32_t dat_idx = tap_info->dat[tdo_idx].idx;
1173 int32_t bit_set = tap_info->dat[tdo_idx].pos;
1174
1175 done_data->arg.value.val = (tdo_ptr[dat_idx + cable_params->tap_pair_start_idx] & bit_set) ? 1 : 0;
1176 bit_set >>= 1;
1177
1178 if (!bit_set)
1179 {
1180 bit_set = 0x80;
1181 dat_idx++;
1182 }
1183 tap_info->dat[tdo_idx].idx = dat_idx;
1184 tap_info->dat[tdo_idx].pos = bit_set;
1185 }
1186 else
1187 {
1188 done_data->arg.value.val = (tdo_ptr) ? 1 : 0;
1189 }
1190 }
1191 break;
1192 case URJ_TAP_CABLE_SET_SIGNAL:
1193 /* not currently used, if it will be, revisit */
1194 cable->driver->set_signal (cable, 0xff, todo_data->arg.value.sig);
1195 break;
1196 case URJ_TAP_CABLE_GET_SIGNAL:
1197 /* not currently used, if it will be, revisit */
1198 {
1199 int32_t k = urj_tap_cable_add_queue_item (cable, &cable->done);
1200 done_data = &ptr_done->data[k];
1201
1202 done_data->action = URJ_TAP_CABLE_GET_SIGNAL;
1203 done_data->arg.value.sig = cable->driver->get_signal (cable, done_data->arg.value.sig);
1204 }
1205 break;
1206 case URJ_TAP_CABLE_TRANSFER:
1207 /* set up the get data */
1208 free (todo_data->arg.transfer.in);
1209 todo_data->arg.transfer.in = NULL;
1210 if ((todo_data->arg.transfer.out != NULL) && (tdo_ptr != NULL))
1211 {
1212 int32_t k = urj_tap_cable_add_queue_item (cable, &cable->done);
1213 done_data = &ptr_done->data[k];
1214
1215 get_recv_data (cable, j, tap_info->rcv_dat, &tdo_ptr);
1216 tap_info->rcv_dat++;
1217 done_data->action = URJ_TAP_CABLE_TRANSFER;
1218 done_data->arg.xferred.len = todo_data->arg.transfer.len;
1219 done_data->arg.xferred.res = 0;
1220 done_data->arg.xferred.out = todo_data->arg.transfer.out;
1221 tdo_idx++;
1222 scan_out++;
1223 }
1224 break;
1225 default:
1226 DEBUG("default; j = %d, n = %d - other end\n", j, n);
1227 break;
1228 }
1229
1230 j++;
1231 if (j >= ptr_todo->max_items)
1232 j = 0;
1233
1234 ptr_todo->num_items--;
1235 }
1236
1237 ptr_todo->next_item = i;
1238 }
1239
1240 /* need to free memory */
1241 if (tdo_ptr)
1242 {
1243 free (tdo_ptr);
1244 tdo_ptr = NULL;
1245 }
1246
1247 if (scan_out > 0)
1248 {
1249 if (tap_info->pairs)
1250 {
1251 free (tap_info->cmd);
1252 tap_info->pairs = NULL;
1253 tap_info->cmd = NULL;
1254 }
1255 tap_info->total = 0;
1256 tap_info->cur_idx = 0;
1257 tap_info->bit_pos = 0;
1258 tap_info->cur_dat = -1;
1259 tap_info->rcv_dat = -1;
1260 }
1261 }
1262
1263 /*
1264 * Get TRST state if Firmware version supports it
1265 * TODO: Since when developing this product,
1266 * Functionality has changed. But at this point,
1267 * we only get TRST
1268 */
ice_get_sig(urj_cable_t * cable,urj_pod_sigsel_t sig)1269 static int ice_get_sig (urj_cable_t *cable, urj_pod_sigsel_t sig)
1270 {
1271 params_t *cable_params = cable->params;
1272 if (cable_params->version < 0x0106)
1273 return URJ_STATUS_FAIL;
1274 else
1275 return do_host_cmd (cable, HOST_GET_TRST, 0, 1);
1276 }
1277
1278 /*
1279 * Set TRST state if Firmware version supports it
1280 * TODO: Since when developing this product,
1281 * Functionality has changed. But at this point,
1282 * we only set TRST
1283 */
ice_set_sig(urj_cable_t * cable,int mask,int val)1284 static int ice_set_sig (urj_cable_t *cable, int mask, int val)
1285 {
1286 params_t *cable_params = cable->params;
1287 if (cable_params->version < 0x0106)
1288 {
1289 urj_warning (_("Setting TRST is unavailable for Firmware Versions less than 1.0.6"));
1290 return URJ_STATUS_FAIL;
1291 }
1292 else
1293 return do_host_cmd (cable, HOST_SET_TRST, (val ? 1 : 0), 1);
1294 }
1295
1296 /*
1297 * Takes Data received (rcv_dataptr) and puts it in
1298 * todo date out transfer
1299 */
get_recv_data(urj_cable_t * cable,int32_t idx,int32_t idx_dat,uint8_t ** rcv_dataptr)1300 static void get_recv_data (urj_cable_t *cable, int32_t idx, int32_t idx_dat, uint8_t **rcv_dataptr)
1301 {
1302 params_t *cable_params = cable->params;
1303 int32_t len = cable->todo.data[idx].arg.transfer.len;
1304 char *buf = cable->todo.data[idx].arg.transfer.out;
1305 num_tap_pairs *tap_info = &cable_params->tap_info;
1306 int32_t dat_idx = tap_info->dat[idx_dat].idx;
1307 uint8_t *rcvBuf = (*rcv_dataptr) + cable_params->num_rcv_hdr_bytes+ dat_idx;
1308 int32_t bit_set = tap_info->dat[idx_dat].pos;
1309 int32_t i;
1310
1311 #ifdef DUMP_EACH_RCV_DATA
1312 DEBUG ("Idx = %d; Read len = %d\n", dat_idx, len);
1313 #endif
1314
1315 if ((buf == NULL) || (idx_dat < 0))
1316 {
1317 DEBUG("get_recv_data(): %s\n", (buf == NULL) ? "No output buffer" : "No Received Data");
1318 return;
1319 }
1320
1321 for (i = 0; i < len; i++)
1322 {
1323 *buf++ = (*rcvBuf & bit_set) ? 1 : 0;
1324
1325 #ifdef DUMP_EACH_RCV_DATA
1326 DEBUG ("%d", (int32_t)*(buf - 1));
1327 if (((i + 1) % 64) == 0)
1328 putchar ('\n');
1329 else if (((i + 1) % 8) == 0)
1330 putchar (' ');
1331 #endif
1332 bit_set >>= 1;
1333
1334 if (!bit_set)
1335 {
1336 bit_set = 0x80;
1337 rcvBuf++;
1338 dat_idx++;
1339 }
1340 }
1341
1342 #ifdef DUMP_EACH_RCV_DATA
1343 if ((len & 1) == 1)
1344 DEBUG (" next bit is %d\n", (rcvBuf[dat_idx] & bit_set) ? 1 : 0);
1345 else
1346 putchar ('\n');
1347 #endif
1348
1349 /* this is set for getting the extra TDO bits */
1350 tap_info->dat[idx_dat].idx = dat_idx;
1351 tap_info->dat[idx_dat].pos = bit_set;
1352 }
1353
1354 /*
1355 * This function takes CABLE_TRANSFER todo data,
1356 * and adds it to the tms/tdi scan structure
1357 * If reading data, sets that up too
1358 */
add_scan_data(urj_cable_t * cable,int32_t num_bits,char * in,char * out)1359 static int add_scan_data (urj_cable_t *cable, int32_t num_bits, char *in, char *out)
1360 {
1361 params_t *cable_params = cable->params;
1362 int32_t bit_cnt = num_bits % 8;
1363 int32_t byte_cnt = (num_bits >> 3) + (bit_cnt ? 1 : 0);
1364 int32_t i, bit_set;
1365 tap_pairs *tap_scan = NULL;
1366 int32_t idx;
1367 num_tap_pairs *tap_info = &cable_params->tap_info;
1368
1369 if (in == NULL)
1370 urj_warning (_("NO IN DATA!!!%s"), out ? _(" BUT there is out data!") : "");
1371
1372 if (tap_info->pairs == NULL)
1373 { /* really should never get here, but must not crash system. Would be rude */
1374 int32_t new_sz = cable_params->default_scanlen + 4;
1375 unsigned char *cmd;
1376
1377 cmd = malloc ((sizeof (tap_pairs) * new_sz) + 1 + cable_params->tap_pair_start_idx);
1378 if (cmd == NULL)
1379 {
1380 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("malloc(%zd) fails"),
1381 (sizeof (tap_pairs) * new_sz) + 1 + cable_params->tap_pair_start_idx);
1382 return URJ_STATUS_FAIL;
1383 }
1384
1385 tap_info->cur_dat = -1;
1386 tap_info->rcv_dat = -1;
1387 tap_info->bit_pos = 0x80;
1388 tap_info->total = new_sz;
1389 tap_scan = tap_info->pairs = (tap_pairs *)(cmd + cable_params->tap_pair_start_idx); /* new pointer */
1390 tap_info->cmd = cmd; /* new pointer */
1391 tap_scan->tms = 0;
1392 tap_scan->tdi = 0;
1393 idx = tap_info->cur_idx = 1; /* first pair is 0 */
1394 tap_scan++;
1395 tap_scan->tdi = 0;
1396 tap_scan->tms = 0;
1397 }
1398 else if ((tap_info->total - tap_info->cur_idx) < byte_cnt)
1399 { /* to small, increase size! */
1400 unsigned char *cmd;
1401 int32_t new_sz;
1402
1403 DEBUG("Reallocating scan_data\n");
1404
1405 new_sz = tap_info->total + byte_cnt + 8;
1406 cmd = realloc (tap_info->cmd, (sizeof (tap_pairs) * new_sz) + 4 + cable_params->tap_pair_start_idx);
1407 if (cmd == NULL)
1408 {
1409 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("realloc(%zd) fails"),
1410 (sizeof (tap_pairs) * new_sz) + 4 + cable_params->tap_pair_start_idx);
1411 return URJ_STATUS_FAIL;
1412 }
1413
1414 tap_info->total = new_sz; /* resize size */
1415 tap_scan = tap_info->pairs = (tap_pairs *)(cmd + cable_params->tap_pair_start_idx); /* new pointer */
1416 tap_info->cmd = cmd; /* new pointer */
1417 idx = tap_info->cur_idx; /* to add on */
1418 tap_scan = &tap_info->pairs[idx];
1419 }
1420 else
1421 {
1422 idx = tap_info->cur_idx; /* to add on */
1423 tap_scan = &tap_info->pairs[idx];
1424 }
1425
1426 bit_set = tap_info->bit_pos;
1427
1428 if (out)
1429 { /* Setup where we start to read, can be more than 1 */
1430 if (tap_info->rcv_dat == -1)
1431 {
1432 tap_info->rcv_dat = 0;
1433 }
1434 tap_info->cur_dat++;
1435 if (tap_info->cur_dat >= tap_info->num_dat)
1436 {
1437 int32_t new_sz;
1438 dat_dat *datPtr;
1439
1440 new_sz = tap_info->num_dat + DAT_SZ_INC;
1441 datPtr = realloc (tap_info->dat, sizeof (dat_dat) * new_sz);
1442 if (datPtr == NULL)
1443 {
1444 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("realloc(%zd) fails"),
1445 sizeof (dat_dat) * new_sz);
1446 return URJ_STATUS_FAIL;
1447 }
1448 tap_info->dat = datPtr;
1449 tap_info->num_dat = new_sz;
1450
1451 }
1452 tap_info->dat[tap_info->cur_dat].idx = idx;
1453 tap_info->dat[tap_info->cur_dat].pos = bit_set;
1454 }
1455
1456 /* Build Scan. TMS will always be zero! */
1457 for (i = 0; i < num_bits; i++, in++)
1458 {
1459 tap_scan->tdi |= *in ? bit_set : 0;
1460 bit_set >>= 1;
1461 if (!bit_set)
1462 {
1463 bit_set = 0x80;
1464 idx++;
1465 tap_scan++;
1466 tap_scan->tdi = 0;
1467 tap_scan->tms = 0;
1468 }
1469 }
1470
1471 tap_info->cur_idx = idx;
1472 tap_info->bit_pos = bit_set;
1473
1474 return URJ_STATUS_OK;
1475 }
1476
1477 /*
1478 * This function takes CABLE_CLOCK todo data,
1479 * and builds the tms/tdi scan structure
1480 */
build_clock_scan(urj_cable_t * cable,int32_t * start_idx,int32_t * num_todo_items)1481 static int build_clock_scan (urj_cable_t *cable, int32_t *start_idx, int32_t *num_todo_items)
1482 {
1483 params_t *cable_params = cable->params;
1484 num_tap_pairs *tap_info = &cable_params->tap_info;
1485 urj_cable_queue_info_t *ptr_todo = &cable->todo;
1486 tap_pairs *tap_scan = NULL;
1487 urj_cable_queue_t *scan_data = NULL;
1488 int32_t i, n, idx = tap_info->cur_idx, cur_idx = *start_idx, bit_set;
1489
1490 if (tap_info->pairs == NULL)
1491 { /* Allocate tap_scan */
1492 int32_t new_sz = cable_params->default_scanlen + 4;
1493 unsigned char *cmd;
1494
1495 cmd = malloc ((sizeof (tap_pairs) * new_sz) + 1 + cable_params->tap_pair_start_idx);
1496 if (cmd == NULL)
1497 {
1498 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("malloc(%zd) fails"),
1499 (sizeof (tap_pairs) * new_sz) + 1 + cable_params->tap_pair_start_idx);
1500 return URJ_STATUS_FAIL;
1501 }
1502
1503 tap_info->total = new_sz;
1504 tap_info->bit_pos = 0x80;
1505 tap_info->cur_dat = -1;
1506 tap_info->rcv_dat = -1;
1507 tap_info->cmd = cmd;
1508 tap_info->pairs = (tap_pairs *)(cmd + cable_params->tap_pair_start_idx); /* new pointer */
1509
1510 tap_scan = tap_info->pairs;
1511 tap_scan->tms = 0;
1512 tap_scan->tdi = 0;
1513 tap_scan++;
1514 tap_scan->tms = 0;
1515 tap_scan->tdi = 0;
1516 idx = tap_info->cur_idx = 1; /* put NULL for first one */
1517 }
1518 else if ((tap_info->total - tap_info->cur_idx) < 5)
1519 { /* need more memory, really should never get here */
1520 int32_t new_sz = tap_info->total + 20;
1521 unsigned char *cmd;
1522
1523 DEBUG("Reallocating ClockScan\n");
1524
1525 cmd = realloc (tap_info->cmd, (sizeof (tap_pairs) * new_sz) + 4);
1526 if (cmd == NULL)
1527 {
1528 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("realloc(%zd) fails"),
1529 (sizeof (tap_pairs) * new_sz) + 4);
1530 return URJ_STATUS_FAIL;
1531 }
1532 tap_info->cmd = cmd;
1533 tap_info->pairs = (tap_pairs *)(cmd + cable_params->tap_pair_start_idx); /* new pointer */
1534
1535 tap_scan = tap_info->pairs;
1536 idx = tap_info->cur_idx;
1537 tap_scan = &tap_scan[idx];
1538 tap_info->total = new_sz;
1539 }
1540 else
1541 {
1542 idx = tap_info->cur_idx;
1543 tap_scan = &tap_info->pairs[idx];
1544 }
1545
1546 bit_set = tap_info->bit_pos;
1547 scan_data = &ptr_todo->data[cur_idx];
1548
1549 for (n = *num_todo_items; (n < ptr_todo->num_items) && (scan_data->action == URJ_TAP_CABLE_CLOCK); n++)
1550 { /* for each CABLE_CLOCK todo entry, create scan */
1551 for (i = 0; i < scan_data->arg.clock.n; i++)
1552 {
1553 tap_scan->tms |= scan_data->arg.clock.tms ? bit_set : 0;
1554 tap_scan->tdi |= scan_data->arg.clock.tdi ? bit_set : 0;
1555 bit_set >>= 1;
1556 if (!bit_set)
1557 {
1558 bit_set = 0x80;
1559 idx++; /* filled up, go to the next one */
1560 tap_scan++;
1561 tap_scan->tms = 0;
1562 tap_scan->tdi = 0;
1563 }
1564 }
1565 cur_idx++;
1566 if (cur_idx >= ptr_todo->max_items)
1567 { /* wrap around buffer! */
1568 cur_idx = 0;
1569 }
1570 scan_data = &ptr_todo->data[cur_idx];
1571 }
1572
1573 tap_info->cur_idx = idx;
1574 tap_info->bit_pos = bit_set;
1575 *start_idx = ((cur_idx == 0) ? ptr_todo->max_items : cur_idx) - 1;
1576 *num_todo_items = n - 1;
1577
1578 return URJ_STATUS_OK;
1579 }
1580
1581 /*
1582 * Read & Write Registers
1583 *
1584 * XXX: error handling doesn't quite work with this return
1585 * XXX: probably needs converting from memory arrays to byte shifts
1586 * so we work regardless of host endian
1587 */
do_single_reg_value(urj_cable_t * cable,uint8_t reg,int32_t r_data,int32_t wr_data,uint32_t data)1588 static uint32_t do_single_reg_value (urj_cable_t *cable, uint8_t reg, int32_t r_data, int32_t wr_data, uint32_t data)
1589 {
1590 params_t *cable_params = cable->params;
1591 usb_command_block usb_cmd_blk;
1592 union {
1593 uint8_t b[24];
1594 uint32_t l[6];
1595 } cmd_buffer;
1596 uint32_t count = 0;
1597 int32_t i, size = wr_data ? 8 : 4;
1598
1599 usb_cmd_blk.command = HOST_REQUEST_TX_DATA;
1600 usb_cmd_blk.count = size;
1601 usb_cmd_blk.buffer = 0;
1602
1603 adi_usb_write_or_ret (cable->link.usb->params, &usb_cmd_blk, sizeof (usb_cmd_blk));
1604 i = 0;
1605
1606 /* send HOST_SET_SINGLE_REG command */
1607 cmd_buffer.b[i++] = 1;
1608 cmd_buffer.b[i++] = 0;
1609 cmd_buffer.b[i++] = wr_data ? HOST_SET_SINGLE_REG : HOST_GET_SINGLE_REG;
1610 cmd_buffer.b[i++] = reg;
1611 if (wr_data)
1612 {
1613 cmd_buffer.l[i / 4] = data;
1614 }
1615
1616 adi_usb_write_or_ret (cable->link.usb->params, cmd_buffer.b, size);
1617
1618 if (r_data)
1619 adi_usb_read_or_ret (cable->link.usb->params, &count, sizeof (count));
1620
1621 return count;
1622 }
1623
1624 /*
1625 * Send Host Command.
1626 *
1627 * XXX: error handling doesn't quite work with this return
1628 * XXX: probably needs converting from memory arrays to byte shifts
1629 * so we work regardless of host endian
1630 */
do_host_cmd(urj_cable_t * cable,uint8_t cmd,uint8_t param,int32_t r_data)1631 static uint16_t do_host_cmd (urj_cable_t *cable, uint8_t cmd, uint8_t param, int32_t r_data)
1632 {
1633 params_t *cable_params = cable->params;
1634 usb_command_block usb_cmd_blk;
1635 uint16_t results = 0;
1636 union {
1637 uint8_t b[20];
1638 uint32_t l[20/4];
1639 } cmd_buffer;
1640 int32_t i, size = 4;
1641
1642 usb_cmd_blk.command = HOST_REQUEST_TX_DATA;
1643 usb_cmd_blk.count = 4;
1644 usb_cmd_blk.buffer = 0;
1645
1646 adi_usb_write_or_ret (cable->link.usb->params, &usb_cmd_blk, sizeof (usb_cmd_blk));
1647 i = 0;
1648
1649 /* send command */
1650 cmd_buffer.b[i++] = param;
1651 cmd_buffer.b[i++] = 0;
1652 cmd_buffer.b[i++] = cmd;
1653 cmd_buffer.b[i] = 0;
1654
1655 adi_usb_write_or_ret (cable->link.usb->params, cmd_buffer.b, size);
1656
1657 if (r_data)
1658 {
1659 usb_cmd_blk.command = HOST_REQUEST_RX_DATA;
1660 usb_cmd_blk.count = 2;
1661 usb_cmd_blk.buffer = 0;
1662
1663 adi_usb_write_or_ret (cable->link.usb->params, &usb_cmd_blk, sizeof (usb_cmd_blk));
1664
1665 adi_usb_read_or_ret (cable->link.usb->params, &results, sizeof (results));
1666 }
1667
1668 return results;
1669 }
1670
1671 /*
1672 * Controlling function to do a scan.
1673 * rdata is a pointer to storage for the pointer
1674 * allocated here to return data if needed
1675 */
perform_scan(urj_cable_t * cable,uint8_t ** rdata)1676 static int perform_scan (urj_cable_t *cable, uint8_t **rdata)
1677 {
1678 params_t *cable_params = cable->params;
1679 num_tap_pairs *tap_info = &cable_params->tap_info;
1680 uint8_t firstpkt = 1, lastpkt = 0, *in = NULL, *out = NULL;
1681 int32_t idx, collect_data = 0;
1682 int32_t cur_len = cable_params->tap_info.cur_idx;
1683 int32_t rem_len;
1684
1685 /* Data is scan as 32 bit words, so boundaries are adjusted here */
1686 if (tap_info->bit_pos != 0x80) /* meaning no dangling bits? */
1687 { /* yes, so straighten out! */
1688 cur_len++;
1689 tap_info->pairs[cur_len].tms = 0;
1690 tap_info->pairs[cur_len].tdi = 0;
1691 }
1692
1693 /* Pad with zeros */
1694 cur_len++;
1695 tap_info->pairs[cur_len].tms = 0;
1696 tap_info->pairs[cur_len].tdi = 0;
1697
1698 while (cur_len & 0x03)
1699 { /* expect to be in 32 bit words */
1700 cur_len++;
1701 tap_info->pairs[cur_len].tms = 0;
1702 tap_info->pairs[cur_len].tdi = 0;
1703 }
1704
1705 tap_info->cur_idx = cur_len;
1706 rem_len = cur_len * sizeof (tap_pairs);
1707
1708 if (cur_len > cable_params->default_scanlen)
1709 {
1710 urj_log (URJ_LOG_LEVEL_ERROR,
1711 _("%s: TAP Scan length %d is greater than DIF Memory"),
1712 __func__, tap_info->cur_idx);
1713 return URJ_STATUS_FAIL;
1714 }
1715
1716 if (tap_info->cur_dat != -1)
1717 { /* yes we have data, so allocate for data plus header */
1718 size_t len;
1719
1720 len = cur_len + cable_params->tap_pair_start_idx + 16;
1721 if (tap_info->dat[0].idx > 12)
1722 len -= tap_info->dat[0].idx;
1723
1724 out = malloc (len);
1725 if (out == NULL)
1726 {
1727 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("malloc(%zd) fails"),
1728 len);
1729 return URJ_STATUS_FAIL;
1730 }
1731 *rdata = out;
1732 collect_data = 1;
1733 }
1734 else
1735 { /* no data, so allocate for just header */
1736 out = malloc (cable_params->tap_pair_start_idx + 16);
1737 if (out == NULL)
1738 {
1739 urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("malloc(%d) fails"),
1740 cable_params->tap_pair_start_idx + 16);
1741 return URJ_STATUS_FAIL;
1742 }
1743 collect_data = 0;
1744 }
1745
1746 in = (uint8_t *)tap_info->pairs;
1747 idx = 0;
1748
1749 /* Here if data is too large, we break it up into manageable chunks */
1750 do
1751 {
1752 cur_len = (rem_len >= cable_params->max_raw_data_tx_items) ? cable_params->max_raw_data_tx_items : rem_len;
1753
1754 if (cur_len == rem_len)
1755 lastpkt = 1;
1756
1757 do_rawscan (cable, firstpkt, lastpkt, collect_data, cur_len, &in[idx] - cable_params->tap_pair_start_idx, out);
1758
1759 rem_len -= cur_len;
1760 idx += cur_len;
1761 firstpkt = 0;
1762
1763 } while (rem_len);
1764
1765 if (tap_info->dat[0].idx == -1)
1766 { /* no data to return, so free it */
1767 free (out);
1768 }
1769
1770 return URJ_STATUS_OK;
1771 }
1772
1773 /*
1774 * description of raw scan packet structure:
1775 *
1776 * [0] : first packet flag (do setup work if needed)
1777 * [1] : last packet flag (start the scan and cleanup if needed)
1778 * [2] : command ID
1779 * [3] : collect DOF flag (need to read DOF)
1780 * [4-5] : DIF count
1781 * [6-7] : scan length count
1782 * [8-9] : first scan pair
1783 * [10...] : more scan pairs
1784 *
1785 * Data input:
1786 *
1787 * firstpkt : Is this the first packet of the scan? 0 = NO
1788 * lastpkt : Is this the last packet of the scan? 0 = NO
1789 * collect_dof : Are we collecting data? 0 = NO
1790 * dif_cnt : Number of bytes to send
1791 * *raw_buf : Pointer to Scan Data buffer * cmd to send
1792 * *out : Pointer to Scan Data buffer to receive
1793 *
1794 * XXX: probably needs converting from memory arrays to byte shifts
1795 * so we work regardless of host endian
1796 */
do_rawscan(urj_cable_t * cable,uint8_t firstpkt,uint8_t lastpkt,int32_t collect_dof,int32_t dif_cnt,uint8_t * raw_buf,uint8_t * out)1797 static int do_rawscan (urj_cable_t *cable, uint8_t firstpkt, uint8_t lastpkt,
1798 int32_t collect_dof, int32_t dif_cnt, uint8_t *raw_buf,
1799 uint8_t *out)
1800 {
1801 params_t *cable_params = cable->params;
1802 usb_command_block usb_cmd_blk;
1803 num_tap_pairs *tap_info = &cable_params->tap_info;
1804 int32_t i, dof_start = 0;
1805 uint32_t data;
1806 uint32_t size = cable_params->tap_pair_start_idx + dif_cnt;
1807
1808 usb_cmd_blk.command = HOST_REQUEST_TX_DATA;
1809 usb_cmd_blk.count = size;
1810 usb_cmd_blk.buffer = 0;
1811
1812 /* first send Xmit request with the count of what will be sent */
1813 adi_usb_write_or_ret (cable->link.usb->params, &usb_cmd_blk, sizeof (usb_cmd_blk));
1814 i = 0;
1815
1816 /* send HOST_DO_SELECTIVE_RAW_SCAN command */
1817 raw_buf[i++] = firstpkt;
1818 raw_buf[i++] = lastpkt;
1819 raw_buf[i++] = HOST_DO_SELECTIVE_RAW_SCAN;
1820 if ((collect_dof && lastpkt) && (tap_info->dat[0].idx > 12))
1821 {
1822 int32_t j, offset;
1823
1824 dof_start = tap_info->dat[0].idx;
1825 offset = dof_start & 7;
1826 dof_start -= offset & 7;
1827 tap_info->dat[0].idx = offset;
1828
1829 for (j = 1; j <= tap_info->cur_dat; j++)
1830 {
1831 tap_info->dat[j].idx -= dof_start;
1832 }
1833 }
1834
1835 raw_buf[i++] = collect_dof ? 1 : 0;
1836 data = dif_cnt / 4; /* dif count in longs */
1837 memcpy (raw_buf + i, &data, 4);
1838 data = tap_info->cur_idx / 4; /* count in longs */
1839 memcpy (raw_buf + i + 2, &data, 4);
1840
1841 /* only Ice emulators use this */
1842 memcpy (raw_buf + i + 4, &dof_start, 4);
1843
1844 adi_usb_write_or_ret (cable->link.usb->params, raw_buf, size);
1845
1846 if (lastpkt)
1847 {
1848 int32_t cur_rd_bytes = 0, tot_bytes_rd = 0, rd_bytes_left;
1849
1850 rd_bytes_left = cable_params->num_rcv_hdr_bytes + ((collect_dof) ? (tap_info->cur_idx - dof_start) : 0);
1851
1852 while (tot_bytes_rd < rd_bytes_left)
1853 {
1854 cur_rd_bytes = ((rd_bytes_left - tot_bytes_rd) > cable_params->r_buf_sz) ?
1855 cable_params->r_buf_sz : (rd_bytes_left - tot_bytes_rd);
1856
1857 adi_usb_read_or_ret (cable->link.usb->params, out + tot_bytes_rd, cur_rd_bytes);
1858 tot_bytes_rd += cur_rd_bytes;
1859 }
1860
1861 if (out[0] != 2)
1862 {
1863 urj_log (URJ_LOG_LEVEL_ERROR, _("%s: Scan Error!"), __func__);
1864 return URJ_STATUS_FAIL;
1865 }
1866 }
1867
1868 return URJ_STATUS_OK;
1869 }
1870
1871 static void
ice_cable_help(urj_log_level_t ll,const char * cablename)1872 ice_cable_help (urj_log_level_t ll, const char *cablename)
1873 {
1874 const char *ex_short = "[firmware=FILE]";
1875 const char *ex_desc = "FILE Upgrade the ICE firmware. See this page:\n"
1876 " " ICE100B_DOC_URL "\n";
1877 urj_tap_cable_generic_usbconn_help_ex (ll, cablename, ex_short, ex_desc);
1878 }
1879
1880 /*
1881 * Cable Intefaces
1882 */
1883
1884 /* This is for the ICE-100B emulator */
1885 const urj_cable_driver_t urj_tap_cable_ice100B_driver = {
1886 "ICE-100B",
1887 N_("Analog Devices ICE-X Cable (0x064B)"),
1888 URJ_CABLE_DEVICE_USB,
1889 { .usb = ice_connect, },
1890 urj_tap_cable_generic_disconnect,
1891 ice_cable_free,
1892 ice_init,
1893 urj_tap_cable_generic_usbconn_done,
1894 ice100b_set_freq,
1895 adi_clock,
1896 adi_get_tdo,
1897 adi_transfer,
1898 ice_set_sig,
1899 ice_get_sig,
1900 adi_flush,
1901 ice_cable_help,
1902 URJ_CABLE_QUIRK_ONESHOT
1903 };
1904 URJ_DECLARE_USBCONN_CABLE(0x064B, 0x1225, "libusb", "ICE-100B", ice100B)
1905 URJ_DECLARE_USBCONN_CABLE(0x064B, 0x0225, "libusb", "ICE-100B", ice100Bw)
1906
1907 /*
1908 Local Variables:
1909 mode:C
1910 c-default-style:gnu
1911 indent-tabs-mode:nil
1912 End:
1913 */
1914