Charles Hardin <ckhardin@gmail.com> move tcl stuff nearer to where it belongs.
[openocd.git] / src / jtag / jtag.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "replacements.h"
25
26 #include "jtag.h"
27
28 #include "command.h"
29 #include "log.h"
30
31 #include "stdlib.h"
32 #include "string.h"
33 #include <unistd.h>
34
35 /* note that this is not marked as static as it must be available from outside jtag.c for those
36 that implement the jtag_xxx() minidriver layer
37 */
38 int jtag_error=ERROR_OK;
39
40
41 char* tap_state_strings[16] =
42 {
43 "tlr",
44 "sds", "cd", "sd", "e1d", "pd", "e2d", "ud",
45 "rti",
46 "sis", "ci", "si", "e1i", "pi", "e2i", "ui"
47 };
48
49 typedef struct cmd_queue_page_s
50 {
51 void *address;
52 size_t used;
53 struct cmd_queue_page_s *next;
54 } cmd_queue_page_t;
55
56 #define CMD_QUEUE_PAGE_SIZE (1024 * 1024)
57 static cmd_queue_page_t *cmd_queue_pages = NULL;
58
59 /* tap_move[i][j]: tap movement command to go from state i to state j
60 * 0: Test-Logic-Reset
61 * 1: Run-Test/Idle
62 * 2: Shift-DR
63 * 3: Pause-DR
64 * 4: Shift-IR
65 * 5: Pause-IR
66 *
67 * SD->SD and SI->SI have to be caught in interface specific code
68 */
69 u8 tap_move[6][6] =
70 {
71 /* TLR RTI SD PD SI PI */
72 {0x7f, 0x00, 0x17, 0x0a, 0x1b, 0x16}, /* TLR */
73 {0x7f, 0x00, 0x25, 0x05, 0x2b, 0x0b}, /* RTI */
74 {0x7f, 0x31, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
75 {0x7f, 0x30, 0x20, 0x17, 0x1e, 0x2f}, /* PD */
76 {0x7f, 0x31, 0x07, 0x17, 0x00, 0x01}, /* SI */
77 {0x7f, 0x30, 0x1c, 0x17, 0x20, 0x2f} /* PI */
78 };
79
80 int tap_move_map[16] = {
81 0, -1, -1, 2, -1, 3, -1, -1,
82 1, -1, -1, 4, -1, 5, -1, -1
83 };
84
85 tap_transition_t tap_transitions[16] =
86 {
87 {TAP_TLR, TAP_RTI}, /* TLR */
88 {TAP_SIS, TAP_CD}, /* SDS */
89 {TAP_E1D, TAP_SD}, /* CD */
90 {TAP_E1D, TAP_SD}, /* SD */
91 {TAP_UD, TAP_PD}, /* E1D */
92 {TAP_E2D, TAP_PD}, /* PD */
93 {TAP_UD, TAP_SD}, /* E2D */
94 {TAP_SDS, TAP_RTI}, /* UD */
95 {TAP_SDS, TAP_RTI}, /* RTI */
96 {TAP_TLR, TAP_CI}, /* SIS */
97 {TAP_E1I, TAP_SI}, /* CI */
98 {TAP_E1I, TAP_SI}, /* SI */
99 {TAP_UI, TAP_PI}, /* E1I */
100 {TAP_E2I, TAP_PI}, /* PI */
101 {TAP_UI, TAP_SI}, /* E2I */
102 {TAP_SDS, TAP_RTI} /* UI */
103 };
104
105 char* jtag_event_strings[] =
106 {
107 "JTAG controller reset (TLR or TRST)"
108 };
109
110 /* kludge!!!! these are just global variables that the
111 * interface use internally. They really belong
112 * inside the drivers, but we don't want to break
113 * linking the drivers!!!!
114 */
115 enum tap_state end_state = TAP_TLR;
116 enum tap_state cur_state = TAP_TLR;
117 int jtag_trst = 0;
118 int jtag_srst = 0;
119
120 jtag_command_t *jtag_command_queue = NULL;
121 jtag_command_t **last_comand_pointer = &jtag_command_queue;
122 jtag_device_t *jtag_devices = NULL;
123 int jtag_num_devices = 0;
124 int jtag_ir_scan_size = 0;
125 enum reset_types jtag_reset_config = RESET_NONE;
126 enum tap_state cmd_queue_end_state = TAP_TLR;
127 enum tap_state cmd_queue_cur_state = TAP_TLR;
128
129 int jtag_verify_capture_ir = 1;
130
131 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
132 int jtag_nsrst_delay = 0; /* default to no nSRST delay */
133 int jtag_ntrst_delay = 0; /* default to no nTRST delay */
134
135 /* maximum number of JTAG devices expected in the chain
136 */
137 #define JTAG_MAX_CHAIN_SIZE 20
138
139 /* callbacks to inform high-level handlers about JTAG state changes */
140 jtag_event_callback_t *jtag_event_callbacks;
141
142 /* speed in kHz*/
143 static int speed1 = 0, speed2 = 0;
144 /* flag if the kHz speed was defined */
145 static int hasKHz = 0;
146
147 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
148 */
149
150 #if BUILD_ECOSBOARD == 1
151 extern jtag_interface_t eCosBoard_interface;
152 #endif
153
154 #if BUILD_PARPORT == 1
155 extern jtag_interface_t parport_interface;
156 #endif
157
158 #if BUILD_DUMMY == 1
159 extern jtag_interface_t dummy_interface;
160 #endif
161
162 #if BUILD_FT2232_FTD2XX == 1
163 extern jtag_interface_t ft2232_interface;
164 #endif
165
166 #if BUILD_FT2232_LIBFTDI == 1
167 extern jtag_interface_t ft2232_interface;
168 #endif
169
170 #if BUILD_AMTJTAGACCEL == 1
171 extern jtag_interface_t amt_jtagaccel_interface;
172 #endif
173
174 #if BUILD_EP93XX == 1
175 extern jtag_interface_t ep93xx_interface;
176 #endif
177
178 #if BUILD_AT91RM9200 == 1
179 extern jtag_interface_t at91rm9200_interface;
180 #endif
181
182 #if BUILD_GW16012 == 1
183 extern jtag_interface_t gw16012_interface;
184 #endif
185
186 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
187 extern jtag_interface_t presto_interface;
188 #endif
189
190 #if BUILD_USBPROG == 1
191 extern jtag_interface_t usbprog_interface;
192 #endif
193
194 #if BUILD_JLINK == 1
195 extern jtag_interface_t jlink_interface;
196 #endif
197
198 jtag_interface_t *jtag_interfaces[] = {
199 #if BUILD_ECOSBOARD == 1
200 &eCosBoard_interface,
201 #endif
202 #if BUILD_PARPORT == 1
203 &parport_interface,
204 #endif
205 #if BUILD_DUMMY == 1
206 &dummy_interface,
207 #endif
208 #if BUILD_FT2232_FTD2XX == 1
209 &ft2232_interface,
210 #endif
211 #if BUILD_FT2232_LIBFTDI == 1
212 &ft2232_interface,
213 #endif
214 #if BUILD_AMTJTAGACCEL == 1
215 &amt_jtagaccel_interface,
216 #endif
217 #if BUILD_EP93XX == 1
218 &ep93xx_interface,
219 #endif
220 #if BUILD_AT91RM9200 == 1
221 &at91rm9200_interface,
222 #endif
223 #if BUILD_GW16012 == 1
224 &gw16012_interface,
225 #endif
226 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
227 &presto_interface,
228 #endif
229 #if BUILD_USBPROG == 1
230 &usbprog_interface,
231 #endif
232 #if BUILD_JLINK == 1
233 &jlink_interface,
234 #endif
235 NULL,
236 };
237
238 jtag_interface_t *jtag = NULL;
239
240 /* configuration */
241 jtag_interface_t *jtag_interface = NULL;
242 int jtag_speed = 0;
243 int jtag_speed_post_reset = 0;
244
245
246 /* forward declarations */
247 void jtag_add_pathmove(int num_states, enum tap_state *path);
248 void jtag_add_runtest(int num_cycles, enum tap_state endstate);
249 void jtag_add_end_state(enum tap_state endstate);
250 void jtag_add_sleep(u32 us);
251 int jtag_execute_queue(void);
252 int jtag_cancel_queue(void);
253
254 /* jtag commands */
255 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
256 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
257 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
258 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
259 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
260 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
261 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
262
263 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
264
265 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
266 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
267 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
268 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
269 int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
270
271 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
272
273 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
274 {
275 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
276
277 if (callback == NULL)
278 {
279 return ERROR_INVALID_ARGUMENTS;
280 }
281
282 if (*callbacks_p)
283 {
284 while ((*callbacks_p)->next)
285 callbacks_p = &((*callbacks_p)->next);
286 callbacks_p = &((*callbacks_p)->next);
287 }
288
289 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
290 (*callbacks_p)->callback = callback;
291 (*callbacks_p)->priv = priv;
292 (*callbacks_p)->next = NULL;
293
294 return ERROR_OK;
295 }
296
297 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
298 {
299 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
300
301 if (callback == NULL)
302 {
303 return ERROR_INVALID_ARGUMENTS;
304 }
305
306 while (*callbacks_p)
307 {
308 jtag_event_callback_t **next = &((*callbacks_p)->next);
309 if ((*callbacks_p)->callback == callback)
310 {
311 free(*callbacks_p);
312 *callbacks_p = *next;
313 }
314 callbacks_p = next;
315 }
316
317 return ERROR_OK;
318 }
319
320 int jtag_call_event_callbacks(enum jtag_event event)
321 {
322 jtag_event_callback_t *callback = jtag_event_callbacks;
323
324 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
325
326 while (callback)
327 {
328 callback->callback(event, callback->priv);
329 callback = callback->next;
330 }
331
332 return ERROR_OK;
333 }
334
335 /* returns a pointer to the pointer of the last command in queue
336 * this may be a pointer to the root pointer (jtag_command_queue)
337 * or to the next member of the last but one command
338 */
339 jtag_command_t** jtag_get_last_command_p(void)
340 {
341 /* jtag_command_t *cmd = jtag_command_queue;
342
343 if (cmd)
344 while (cmd->next)
345 cmd = cmd->next;
346 else
347 return &jtag_command_queue;
348
349 return &cmd->next;*/
350
351 return last_comand_pointer;
352 }
353
354 /* returns a pointer to the n-th device in the scan chain */
355 jtag_device_t* jtag_get_device(int num)
356 {
357 jtag_device_t *device = jtag_devices;
358 int i = 0;
359
360 while (device)
361 {
362 if (num == i)
363 return device;
364 device = device->next;
365 i++;
366 }
367
368 LOG_ERROR("jtag device number %d not defined", num);
369 exit(-1);
370 }
371
372 void* cmd_queue_alloc(size_t size)
373 {
374 cmd_queue_page_t **p_page = &cmd_queue_pages;
375 int offset;
376 u8 *t;
377
378 if (*p_page)
379 {
380 while ((*p_page)->next)
381 p_page = &((*p_page)->next);
382 if (CMD_QUEUE_PAGE_SIZE - (*p_page)->used < size)
383 p_page = &((*p_page)->next);
384 }
385
386 if (!*p_page)
387 {
388 *p_page = malloc(sizeof(cmd_queue_page_t));
389 (*p_page)->used = 0;
390 (*p_page)->address = malloc(CMD_QUEUE_PAGE_SIZE);
391 (*p_page)->next = NULL;
392 }
393
394 offset = (*p_page)->used;
395 (*p_page)->used += size;
396
397 t=(u8 *)((*p_page)->address);
398 return t + offset;
399 }
400
401 void cmd_queue_free()
402 {
403 cmd_queue_page_t *page = cmd_queue_pages;
404
405 while (page)
406 {
407 cmd_queue_page_t *last = page;
408 free(page->address);
409 page = page->next;
410 free(last);
411 }
412
413 cmd_queue_pages = NULL;
414 }
415
416 static void jtag_prelude1()
417 {
418 if (jtag_trst == 1)
419 {
420 LOG_WARNING("JTAG command queued, while TRST is low (TAP in reset)");
421 jtag_error=ERROR_JTAG_TRST_ASSERTED;
422 return;
423 }
424
425 if (cmd_queue_end_state == TAP_TLR)
426 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
427 }
428
429 static void jtag_prelude(enum tap_state state)
430 {
431 jtag_prelude1();
432
433 if (state != -1)
434 jtag_add_end_state(state);
435
436 cmd_queue_cur_state = cmd_queue_end_state;
437 }
438
439 void jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
440 {
441 int retval;
442
443 jtag_prelude(state);
444
445 retval=interface_jtag_add_ir_scan(num_fields, fields, cmd_queue_end_state);
446 if (retval!=ERROR_OK)
447 jtag_error=retval;
448 }
449
450 int MINIDRIVER(interface_jtag_add_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
451 {
452 jtag_command_t **last_cmd;
453 jtag_device_t *device;
454 int i, j;
455 int scan_size = 0;
456
457
458 last_cmd = jtag_get_last_command_p();
459
460 /* allocate memory for a new list member */
461 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
462 (*last_cmd)->next = NULL;
463 last_comand_pointer = &((*last_cmd)->next);
464 (*last_cmd)->type = JTAG_SCAN;
465
466 /* allocate memory for ir scan command */
467 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
468 (*last_cmd)->cmd.scan->ir_scan = 1;
469 (*last_cmd)->cmd.scan->num_fields = jtag_num_devices; /* one field per device */
470 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(jtag_num_devices * sizeof(scan_field_t));
471 (*last_cmd)->cmd.scan->end_state = state;
472
473 for (i = 0; i < jtag_num_devices; i++)
474 {
475 int found = 0;
476 device = jtag_get_device(i);
477 scan_size = device->ir_length;
478 (*last_cmd)->cmd.scan->fields[i].device = i;
479 (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
480 (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
481 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL; /* disable verification by default */
482
483 /* search the list */
484 for (j = 0; j < num_fields; j++)
485 {
486 if (i == fields[j].device)
487 {
488 found = 1;
489 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
490 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
491
492 if (jtag_verify_capture_ir)
493 {
494 if (fields[j].in_handler==NULL)
495 {
496 jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
497 } else
498 {
499 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
500 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
501 (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected;
502 (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
503 }
504 }
505
506 device->bypass = 0;
507 break;
508 }
509 }
510
511 if (!found)
512 {
513 /* if a device isn't listed, set it to BYPASS */
514 (*last_cmd)->cmd.scan->fields[i].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
515 (*last_cmd)->cmd.scan->fields[i].out_mask = NULL;
516 device->bypass = 1;
517
518 }
519
520 /* update device information */
521 buf_cpy((*last_cmd)->cmd.scan->fields[i].out_value, jtag_get_device(i)->cur_instr, scan_size);
522 }
523
524 return ERROR_OK;
525 }
526
527 void jtag_add_plain_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state)
528 {
529 int retval;
530
531 jtag_prelude(state);
532
533 retval=interface_jtag_add_plain_ir_scan(num_fields, fields, cmd_queue_end_state);
534 if (retval!=ERROR_OK)
535 jtag_error=retval;
536 }
537
538 int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
539 {
540 int i;
541 jtag_command_t **last_cmd;
542
543 last_cmd = jtag_get_last_command_p();
544
545 /* allocate memory for a new list member */
546 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
547 (*last_cmd)->next = NULL;
548 last_comand_pointer = &((*last_cmd)->next);
549 (*last_cmd)->type = JTAG_SCAN;
550
551 /* allocate memory for ir scan command */
552 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
553 (*last_cmd)->cmd.scan->ir_scan = 1;
554 (*last_cmd)->cmd.scan->num_fields = num_fields;
555 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
556 (*last_cmd)->cmd.scan->end_state = state;
557
558 for (i = 0; i < num_fields; i++)
559 {
560 int num_bits = fields[i].num_bits;
561 int num_bytes = CEIL(fields[i].num_bits, 8);
562 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
563 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
564 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
565 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
566 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
567 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
568 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
569 (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
570 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
571 }
572 return ERROR_OK;
573 }
574
575 void jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
576 {
577 int retval;
578
579 jtag_prelude(state);
580
581 retval=interface_jtag_add_dr_scan(num_fields, fields, cmd_queue_end_state);
582 if (retval!=ERROR_OK)
583 jtag_error=retval;
584 }
585
586 int MINIDRIVER(interface_jtag_add_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
587 {
588 int i, j;
589 int bypass_devices = 0;
590 int field_count = 0;
591 int scan_size;
592
593 jtag_command_t **last_cmd = jtag_get_last_command_p();
594 jtag_device_t *device = jtag_devices;
595
596 /* count devices in bypass */
597 while (device)
598 {
599 if (device->bypass)
600 bypass_devices++;
601 device = device->next;
602 }
603 if (bypass_devices >= jtag_num_devices)
604 {
605 LOG_ERROR("all devices in bypass");
606 return ERROR_JTAG_DEVICE_ERROR;
607 }
608
609 /* allocate memory for a new list member */
610 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
611 last_comand_pointer = &((*last_cmd)->next);
612 (*last_cmd)->next = NULL;
613 (*last_cmd)->type = JTAG_SCAN;
614
615 /* allocate memory for dr scan command */
616 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
617 (*last_cmd)->cmd.scan->ir_scan = 0;
618 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
619 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
620 (*last_cmd)->cmd.scan->end_state = state;
621
622 for (i = 0; i < jtag_num_devices; i++)
623 {
624 int found = 0;
625 (*last_cmd)->cmd.scan->fields[field_count].device = i;
626
627 for (j = 0; j < num_fields; j++)
628 {
629 if (i == fields[j].device)
630 {
631 found = 1;
632 scan_size = fields[j].num_bits;
633 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
634 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
635 (*last_cmd)->cmd.scan->fields[field_count].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
636 (*last_cmd)->cmd.scan->fields[field_count].in_value = fields[j].in_value;
637 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = fields[j].in_check_value;
638 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = fields[j].in_check_mask;
639 (*last_cmd)->cmd.scan->fields[field_count].in_handler = fields[j].in_handler;
640 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = fields[j].in_handler_priv;
641 }
642 }
643 if (!found)
644 {
645 #ifdef _DEBUG_JTAG_IO_
646 /* if a device isn't listed, the BYPASS register should be selected */
647 if (!jtag_get_device(i)->bypass)
648 {
649 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
650 exit(-1);
651 }
652 #endif
653 /* program the scan field to 1 bit length, and ignore it's value */
654 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
655 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
656 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
657 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
658 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
659 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
660 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
661 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
662 }
663 else
664 {
665 #ifdef _DEBUG_JTAG_IO_
666 /* if a device is listed, the BYPASS register must not be selected */
667 if (jtag_get_device(i)->bypass)
668 {
669 LOG_ERROR("BUG: scan data for a device in BYPASS");
670 exit(-1);
671 }
672 #endif
673 }
674 }
675 return ERROR_OK;
676 }
677
678 void MINIDRIVER(interface_jtag_add_dr_out)(int device_num,
679 int num_fields,
680 const int *num_bits,
681 const u32 *value,
682 enum tap_state end_state)
683 {
684 int i;
685 int field_count = 0;
686 int scan_size;
687 int bypass_devices = 0;
688
689 jtag_command_t **last_cmd = jtag_get_last_command_p();
690 jtag_device_t *device = jtag_devices;
691 /* count devices in bypass */
692 while (device)
693 {
694 if (device->bypass)
695 bypass_devices++;
696 device = device->next;
697 }
698
699 /* allocate memory for a new list member */
700 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
701 last_comand_pointer = &((*last_cmd)->next);
702 (*last_cmd)->next = NULL;
703 (*last_cmd)->type = JTAG_SCAN;
704
705 /* allocate memory for dr scan command */
706 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
707 (*last_cmd)->cmd.scan->ir_scan = 0;
708 (*last_cmd)->cmd.scan->num_fields = num_fields + bypass_devices;
709 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc((num_fields + bypass_devices) * sizeof(scan_field_t));
710 (*last_cmd)->cmd.scan->end_state = end_state;
711
712 for (i = 0; i < jtag_num_devices; i++)
713 {
714 (*last_cmd)->cmd.scan->fields[field_count].device = i;
715
716 if (i == device_num)
717 {
718 int j;
719 #ifdef _DEBUG_JTAG_IO_
720 /* if a device is listed, the BYPASS register must not be selected */
721 if (jtag_get_device(i)->bypass)
722 {
723 LOG_ERROR("BUG: scan data for a device in BYPASS");
724 exit(-1);
725 }
726 #endif
727 for (j = 0; j < num_fields; j++)
728 {
729 u8 out_value[4];
730 scan_size = num_bits[j];
731 buf_set_u32(out_value, 0, scan_size, value[j]);
732 (*last_cmd)->cmd.scan->fields[field_count].num_bits = scan_size;
733 (*last_cmd)->cmd.scan->fields[field_count].out_value = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
734 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
735 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
736 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
737 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
738 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
739 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
740 }
741 } else
742 {
743 #ifdef _DEBUG_JTAG_IO_
744 /* if a device isn't listed, the BYPASS register should be selected */
745 if (!jtag_get_device(i)->bypass)
746 {
747 LOG_ERROR("BUG: no scan data for a device not in BYPASS");
748 exit(-1);
749 }
750 #endif
751 /* program the scan field to 1 bit length, and ignore it's value */
752 (*last_cmd)->cmd.scan->fields[field_count].num_bits = 1;
753 (*last_cmd)->cmd.scan->fields[field_count].out_value = NULL;
754 (*last_cmd)->cmd.scan->fields[field_count].out_mask = NULL;
755 (*last_cmd)->cmd.scan->fields[field_count].in_value = NULL;
756 (*last_cmd)->cmd.scan->fields[field_count].in_check_value = NULL;
757 (*last_cmd)->cmd.scan->fields[field_count].in_check_mask = NULL;
758 (*last_cmd)->cmd.scan->fields[field_count].in_handler = NULL;
759 (*last_cmd)->cmd.scan->fields[field_count++].in_handler_priv = NULL;
760 }
761 }
762 }
763
764
765
766
767 void jtag_add_plain_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state)
768 {
769 int retval;
770
771 jtag_prelude(state);
772
773 retval=interface_jtag_add_plain_dr_scan(num_fields, fields, cmd_queue_end_state);
774 if (retval!=ERROR_OK)
775 jtag_error=retval;
776 }
777
778 int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int num_fields, scan_field_t *fields, enum tap_state state)
779 {
780 int i;
781 jtag_command_t **last_cmd = jtag_get_last_command_p();
782
783 /* allocate memory for a new list member */
784 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
785 last_comand_pointer = &((*last_cmd)->next);
786 (*last_cmd)->next = NULL;
787 (*last_cmd)->type = JTAG_SCAN;
788
789 /* allocate memory for scan command */
790 (*last_cmd)->cmd.scan = cmd_queue_alloc(sizeof(scan_command_t));
791 (*last_cmd)->cmd.scan->ir_scan = 0;
792 (*last_cmd)->cmd.scan->num_fields = num_fields;
793 (*last_cmd)->cmd.scan->fields = cmd_queue_alloc(num_fields * sizeof(scan_field_t));
794 (*last_cmd)->cmd.scan->end_state = state;
795
796 for (i = 0; i < num_fields; i++)
797 {
798 int num_bits = fields[i].num_bits;
799 int num_bytes = CEIL(fields[i].num_bits, 8);
800 (*last_cmd)->cmd.scan->fields[i].device = fields[i].device;
801 (*last_cmd)->cmd.scan->fields[i].num_bits = num_bits;
802 (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
803 (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[i].out_mask, cmd_queue_alloc(num_bytes), num_bits);
804 (*last_cmd)->cmd.scan->fields[i].in_value = fields[i].in_value;
805 (*last_cmd)->cmd.scan->fields[i].in_check_value = fields[i].in_check_value;
806 (*last_cmd)->cmd.scan->fields[i].in_check_mask = fields[i].in_check_mask;
807 (*last_cmd)->cmd.scan->fields[i].in_handler = fields[i].in_handler;
808 (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[i].in_handler_priv;
809 }
810
811 return ERROR_OK;
812 }
813
814 void jtag_add_tlr()
815 {
816 jtag_prelude(TAP_TLR);
817
818 int retval;
819 retval=interface_jtag_add_tlr();
820 if (retval!=ERROR_OK)
821 jtag_error=retval;
822 }
823
824 int MINIDRIVER(interface_jtag_add_tlr)()
825 {
826 enum tap_state state = TAP_TLR;
827 jtag_command_t **last_cmd = jtag_get_last_command_p();
828
829 /* allocate memory for a new list member */
830 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
831 last_comand_pointer = &((*last_cmd)->next);
832 (*last_cmd)->next = NULL;
833 (*last_cmd)->type = JTAG_STATEMOVE;
834
835 (*last_cmd)->cmd.statemove = cmd_queue_alloc(sizeof(statemove_command_t));
836 (*last_cmd)->cmd.statemove->end_state = state;
837
838
839 return ERROR_OK;
840 }
841
842 void jtag_add_pathmove(int num_states, enum tap_state *path)
843 {
844 enum tap_state cur_state=cmd_queue_cur_state;
845 int i;
846 int retval;
847
848 /* the last state has to be a stable state */
849 if (tap_move_map[path[num_states - 1]] == -1)
850 {
851 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
852 exit(-1);
853 }
854
855 for (i=0; i<num_states; i++)
856 {
857 if ((tap_transitions[cur_state].low != path[i])&&
858 (tap_transitions[cur_state].high != path[i]))
859 {
860 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[path[i]]);
861 exit(-1);
862 }
863 cur_state = path[i];
864 }
865
866 jtag_prelude1();
867
868 cmd_queue_cur_state = path[num_states - 1];
869
870 retval=interface_jtag_add_pathmove(num_states, path);
871 if (retval!=ERROR_OK)
872 jtag_error=retval;
873 }
874
875
876 int MINIDRIVER(interface_jtag_add_pathmove)(int num_states, enum tap_state *path)
877 {
878 jtag_command_t **last_cmd = jtag_get_last_command_p();
879 int i;
880
881 /* allocate memory for a new list member */
882 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
883 last_comand_pointer = &((*last_cmd)->next);
884 (*last_cmd)->next = NULL;
885 (*last_cmd)->type = JTAG_PATHMOVE;
886
887 (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
888 (*last_cmd)->cmd.pathmove->num_states = num_states;
889 (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
890
891 for (i = 0; i < num_states; i++)
892 (*last_cmd)->cmd.pathmove->path[i] = path[i];
893
894 return ERROR_OK;
895 }
896
897 int MINIDRIVER(interface_jtag_add_runtest)(int num_cycles, enum tap_state state)
898 {
899 jtag_command_t **last_cmd = jtag_get_last_command_p();
900
901 /* allocate memory for a new list member */
902 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
903 (*last_cmd)->next = NULL;
904 last_comand_pointer = &((*last_cmd)->next);
905 (*last_cmd)->type = JTAG_RUNTEST;
906
907 (*last_cmd)->cmd.runtest = cmd_queue_alloc(sizeof(runtest_command_t));
908 (*last_cmd)->cmd.runtest->num_cycles = num_cycles;
909 (*last_cmd)->cmd.runtest->end_state = state;
910
911 return ERROR_OK;
912 }
913
914 void jtag_add_runtest(int num_cycles, enum tap_state state)
915 {
916 int retval;
917
918 jtag_prelude(state);
919
920 /* executed by sw or hw fifo */
921 retval=interface_jtag_add_runtest(num_cycles, cmd_queue_end_state);
922 if (retval!=ERROR_OK)
923 jtag_error=retval;
924 }
925
926 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
927 {
928 int trst_with_tlr = 0;
929 int retval;
930
931 /* FIX!!! there are *many* different cases here. A better
932 * approach is needed for legal combinations of transitions...
933 */
934 if ((jtag_reset_config & RESET_HAS_SRST)&&
935 (jtag_reset_config & RESET_HAS_TRST)&&
936 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
937 {
938 if (((req_tlr_or_trst&&!jtag_trst)||
939 (!req_tlr_or_trst&&jtag_trst))&&
940 ((req_srst&&!jtag_srst)||
941 (!req_srst&&jtag_srst)))
942 {
943 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
944 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
945 }
946 }
947
948 /* Make sure that jtag_reset_config allows the requested reset */
949 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
950 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
951 {
952 LOG_ERROR("BUG: requested reset would assert trst");
953 jtag_error=ERROR_FAIL;
954 return;
955 }
956
957 /* if TRST pulls SRST, we reset with TAP T-L-R */
958 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
959 {
960 trst_with_tlr = 1;
961 }
962
963 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
964 {
965 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
966 jtag_error=ERROR_FAIL;
967 return;
968 }
969
970 if (req_tlr_or_trst)
971 {
972 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
973 {
974 jtag_trst = 1;
975 } else
976 {
977 trst_with_tlr = 1;
978 }
979 } else
980 {
981 jtag_trst = 0;
982 }
983
984 jtag_srst = req_srst;
985
986 retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
987 if (retval!=ERROR_OK)
988 {
989 jtag_error=retval;
990 return;
991 }
992
993 if (jtag_srst)
994 {
995 LOG_DEBUG("SRST line asserted");
996 }
997 else
998 {
999 LOG_DEBUG("SRST line released");
1000 if (jtag_nsrst_delay)
1001 jtag_add_sleep(jtag_nsrst_delay * 1000);
1002 }
1003
1004 if (trst_with_tlr)
1005 {
1006 LOG_DEBUG("JTAG reset with TLR instead of TRST");
1007 jtag_add_end_state(TAP_TLR);
1008 jtag_add_tlr();
1009 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1010 return;
1011 }
1012
1013 if (jtag_trst)
1014 {
1015 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
1016 * and inform possible listeners about this
1017 */
1018 LOG_DEBUG("TRST line asserted");
1019 cmd_queue_cur_state = TAP_TLR;
1020 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
1021 }
1022 else
1023 {
1024 if (jtag_ntrst_delay)
1025 jtag_add_sleep(jtag_ntrst_delay * 1000);
1026 }
1027 }
1028
1029 int MINIDRIVER(interface_jtag_add_reset)(int req_trst, int req_srst)
1030 {
1031 jtag_command_t **last_cmd = jtag_get_last_command_p();
1032
1033 /* allocate memory for a new list member */
1034 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1035 (*last_cmd)->next = NULL;
1036 last_comand_pointer = &((*last_cmd)->next);
1037 (*last_cmd)->type = JTAG_RESET;
1038
1039 (*last_cmd)->cmd.reset = cmd_queue_alloc(sizeof(reset_command_t));
1040 (*last_cmd)->cmd.reset->trst = req_trst;
1041 (*last_cmd)->cmd.reset->srst = req_srst;
1042
1043
1044 return ERROR_OK;
1045 }
1046
1047 void jtag_add_end_state(enum tap_state state)
1048 {
1049 cmd_queue_end_state = state;
1050 if ((cmd_queue_end_state == TAP_SD)||(cmd_queue_end_state == TAP_SD))
1051 {
1052 LOG_ERROR("BUG: TAP_SD/SI can't be end state. Calling code should use a larger scan field");
1053 }
1054 }
1055
1056 int MINIDRIVER(interface_jtag_add_sleep)(u32 us)
1057 {
1058 jtag_command_t **last_cmd = jtag_get_last_command_p();
1059
1060 /* allocate memory for a new list member */
1061 *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
1062 (*last_cmd)->next = NULL;
1063 last_comand_pointer = &((*last_cmd)->next);
1064 (*last_cmd)->type = JTAG_SLEEP;
1065
1066 (*last_cmd)->cmd.sleep = cmd_queue_alloc(sizeof(sleep_command_t));
1067 (*last_cmd)->cmd.sleep->us = us;
1068
1069 return ERROR_OK;
1070 }
1071
1072 void jtag_add_sleep(u32 us)
1073 {
1074 int retval=interface_jtag_add_sleep(us);
1075 if (retval!=ERROR_OK)
1076 jtag_error=retval;
1077 return;
1078 }
1079
1080 int jtag_scan_size(scan_command_t *cmd)
1081 {
1082 int bit_count = 0;
1083 int i;
1084
1085 /* count bits in scan command */
1086 for (i = 0; i < cmd->num_fields; i++)
1087 {
1088 bit_count += cmd->fields[i].num_bits;
1089 }
1090
1091 return bit_count;
1092 }
1093
1094 int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
1095 {
1096 int bit_count = 0;
1097 int i;
1098
1099 bit_count = jtag_scan_size(cmd);
1100 *buffer = malloc(CEIL(bit_count, 8));
1101
1102 bit_count = 0;
1103
1104 for (i = 0; i < cmd->num_fields; i++)
1105 {
1106 if (cmd->fields[i].out_value)
1107 {
1108 #ifdef _DEBUG_JTAG_IO_
1109 char* char_buf = buf_to_str(cmd->fields[i].out_value, (cmd->fields[i].num_bits > 64) ? 64 : cmd->fields[i].num_bits, 16);
1110 #endif
1111 buf_set_buf(cmd->fields[i].out_value, 0, *buffer, bit_count, cmd->fields[i].num_bits);
1112 #ifdef _DEBUG_JTAG_IO_
1113 LOG_DEBUG("fields[%i].out_value: 0x%s", i, char_buf);
1114 free(char_buf);
1115 #endif
1116 }
1117
1118 bit_count += cmd->fields[i].num_bits;
1119 }
1120
1121 return bit_count;
1122
1123 }
1124
1125 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
1126 {
1127 int i;
1128 int bit_count = 0;
1129 int retval;
1130
1131 /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
1132 retval = ERROR_OK;
1133
1134 for (i = 0; i < cmd->num_fields; i++)
1135 {
1136 /* if neither in_value nor in_handler
1137 * are specified we don't have to examine this field
1138 */
1139 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1140 {
1141 int num_bits = cmd->fields[i].num_bits;
1142 u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
1143
1144 #ifdef _DEBUG_JTAG_IO_
1145 char *char_buf;
1146
1147 char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1148 LOG_DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
1149 free(char_buf);
1150 #endif
1151
1152 if (cmd->fields[i].in_value)
1153 {
1154 buf_cpy(captured, cmd->fields[i].in_value, num_bits);
1155
1156 if (cmd->fields[i].in_handler)
1157 {
1158 if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1159 {
1160 LOG_WARNING("in_handler reported a failed check");
1161 retval = ERROR_JTAG_QUEUE_FAILED;
1162 }
1163 }
1164 }
1165
1166 /* no in_value specified, but a handler takes care of the scanned data */
1167 if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
1168 {
1169 if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
1170 {
1171 /* We're going to call the error:handler later, but if the in_handler
1172 * reported an error we report this failure upstream
1173 */
1174 LOG_WARNING("in_handler reported a failed check");
1175 retval = ERROR_JTAG_QUEUE_FAILED;
1176 }
1177 }
1178
1179 free(captured);
1180 }
1181 bit_count += cmd->fields[i].num_bits;
1182 }
1183
1184 return retval;
1185 }
1186
1187 int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
1188 {
1189 int retval = ERROR_OK;
1190 int num_bits = field->num_bits;
1191
1192 int compare_failed = 0;
1193
1194 if (field->in_check_mask)
1195 compare_failed = buf_cmp_mask(captured, field->in_check_value, field->in_check_mask, num_bits);
1196 else
1197 compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
1198
1199 if (compare_failed)
1200 {
1201 /* An error handler could have caught the failing check
1202 * only report a problem when there wasn't a handler, or if the handler
1203 * acknowledged the error
1204 */
1205 if (compare_failed)
1206 {
1207 char *captured_char = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
1208 char *in_check_value_char = buf_to_str(field->in_check_value, (num_bits > 64) ? 64 : num_bits, 16);
1209
1210 if (field->in_check_mask)
1211 {
1212 char *in_check_mask_char;
1213 in_check_mask_char = buf_to_str(field->in_check_mask, (num_bits > 64) ? 64 : num_bits, 16);
1214 LOG_WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s check_mask: 0x%s", captured_char, in_check_value_char, in_check_mask_char);
1215 free(in_check_mask_char);
1216 }
1217 else
1218 {
1219 LOG_WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
1220 }
1221
1222 free(captured_char);
1223 free(in_check_value_char);
1224
1225 retval = ERROR_JTAG_QUEUE_FAILED;
1226 }
1227
1228 }
1229 return retval;
1230 }
1231
1232 /*
1233 set up checking of this field using the in_handler. The values passed in must be valid until
1234 after jtag_execute() has completed.
1235 */
1236 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
1237 {
1238 if (value)
1239 field->in_handler = jtag_check_value;
1240 else
1241 field->in_handler = NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
1242 field->in_handler_priv = NULL;
1243 field->in_check_value = value;
1244 field->in_check_mask = mask;
1245 }
1246
1247 enum scan_type jtag_scan_type(scan_command_t *cmd)
1248 {
1249 int i;
1250 int type = 0;
1251
1252 for (i = 0; i < cmd->num_fields; i++)
1253 {
1254 if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
1255 type |= SCAN_IN;
1256 if (cmd->fields[i].out_value)
1257 type |= SCAN_OUT;
1258 }
1259
1260 return type;
1261 }
1262
1263 int MINIDRIVER(interface_jtag_execute_queue)(void)
1264 {
1265 int retval;
1266
1267 if (jtag==NULL)
1268 {
1269 LOG_ERROR("No JTAG interface configured yet. Issue 'init' command in startup scripts before communicating with targets.");
1270 return ERROR_FAIL;
1271 }
1272
1273 retval = jtag->execute_queue();
1274
1275 cmd_queue_free();
1276
1277 jtag_command_queue = NULL;
1278 last_comand_pointer = &jtag_command_queue;
1279
1280 return retval;
1281 }
1282
1283 int jtag_execute_queue(void)
1284 {
1285 int retval=interface_jtag_execute_queue();
1286 if (retval==ERROR_OK)
1287 {
1288 retval=jtag_error;
1289 }
1290 jtag_error=ERROR_OK;
1291 return retval;
1292 }
1293
1294 int jtag_reset_callback(enum jtag_event event, void *priv)
1295 {
1296 jtag_device_t *device = priv;
1297
1298 LOG_DEBUG("-");
1299
1300 if (event == JTAG_TRST_ASSERTED)
1301 {
1302 buf_set_ones(device->cur_instr, device->ir_length);
1303 device->bypass = 1;
1304 }
1305
1306 return ERROR_OK;
1307 }
1308
1309 void jtag_sleep(u32 us)
1310 {
1311 usleep(us);
1312 }
1313
1314 /* Try to examine chain layout according to IEEE 1149.1 §12
1315 */
1316 int jtag_examine_chain()
1317 {
1318 jtag_device_t *device = jtag_devices;
1319 scan_field_t field;
1320 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1321 int i;
1322 int bit_count;
1323 int device_count = 0;
1324 u8 zero_check = 0x0;
1325 u8 one_check = 0xff;
1326
1327 field.device = 0;
1328 field.num_bits = sizeof(idcode_buffer) * 8;
1329 field.out_value = idcode_buffer;
1330 field.out_mask = NULL;
1331 field.in_value = idcode_buffer;
1332 field.in_check_value = NULL;
1333 field.in_check_mask = NULL;
1334 field.in_handler = NULL;
1335 field.in_handler_priv = NULL;
1336
1337 for (i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
1338 {
1339 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
1340 }
1341
1342 jtag_add_plain_dr_scan(1, &field, TAP_TLR);
1343 jtag_execute_queue();
1344
1345 for (i = 0; i < JTAG_MAX_CHAIN_SIZE * 4; i++)
1346 {
1347 zero_check |= idcode_buffer[i];
1348 one_check &= idcode_buffer[i];
1349 }
1350
1351 /* if there wasn't a single non-zero bit or if all bits were one, the scan isn't valid */
1352 if ((zero_check == 0x00) || (one_check == 0xff))
1353 {
1354 LOG_ERROR("JTAG communication failure, check connection, JTAG interface, target power etc.");
1355 return ERROR_JTAG_INIT_FAILED;
1356 }
1357
1358 for (bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1359 {
1360 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1361 if ((idcode & 1) == 0)
1362 {
1363 /* LSB must not be 0, this indicates a device in bypass */
1364 device_count++;
1365
1366 bit_count += 1;
1367 }
1368 else
1369 {
1370 u32 manufacturer;
1371 u32 part;
1372 u32 version;
1373
1374 if (idcode == 0x000000FF)
1375 {
1376 /* End of chain (invalid manufacturer ID) */
1377 break;
1378 }
1379
1380 if (device)
1381 {
1382 device->idcode = idcode;
1383 device = device->next;
1384 }
1385 device_count++;
1386
1387 manufacturer = (idcode & 0xffe) >> 1;
1388 part = (idcode & 0xffff000) >> 12;
1389 version = (idcode & 0xf0000000) >> 28;
1390
1391 LOG_INFO("JTAG device found: 0x%8.8x (Manufacturer: 0x%3.3x, Part: 0x%4.4x, Version: 0x%1.1x)",
1392 idcode, manufacturer, part, version);
1393
1394 bit_count += 32;
1395 }
1396 }
1397
1398 /* see if number of discovered devices matches configuration */
1399 if (device_count != jtag_num_devices)
1400 {
1401 LOG_ERROR("number of discovered devices in JTAG chain (%i) doesn't match configuration (%i)",
1402 device_count, jtag_num_devices);
1403 LOG_ERROR("check the config file and ensure proper JTAG communication (connections, speed, ...)");
1404 return ERROR_JTAG_INIT_FAILED;
1405 }
1406
1407 return ERROR_OK;
1408 }
1409
1410 int jtag_validate_chain()
1411 {
1412 jtag_device_t *device = jtag_devices;
1413 int total_ir_length = 0;
1414 u8 *ir_test = NULL;
1415 scan_field_t field;
1416 int chain_pos = 0;
1417
1418 while (device)
1419 {
1420 total_ir_length += device->ir_length;
1421 device = device->next;
1422 }
1423
1424 total_ir_length += 2;
1425 ir_test = malloc(CEIL(total_ir_length, 8));
1426 buf_set_ones(ir_test, total_ir_length);
1427
1428 field.device = 0;
1429 field.num_bits = total_ir_length;
1430 field.out_value = ir_test;
1431 field.out_mask = NULL;
1432 field.in_value = ir_test;
1433 field.in_check_value = NULL;
1434 field.in_check_mask = NULL;
1435 field.in_handler = NULL;
1436 field.in_handler_priv = NULL;
1437
1438 jtag_add_plain_ir_scan(1, &field, TAP_TLR);
1439 jtag_execute_queue();
1440
1441 device = jtag_devices;
1442 while (device)
1443 {
1444 if (buf_get_u32(ir_test, chain_pos, 2) != 0x1)
1445 {
1446 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1447 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1448 free(cbuf);
1449 free(ir_test);
1450 return ERROR_JTAG_INIT_FAILED;
1451 }
1452 chain_pos += device->ir_length;
1453 device = device->next;
1454 }
1455
1456 if (buf_get_u32(ir_test, chain_pos, 2) != 0x3)
1457 {
1458 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1459 LOG_ERROR("Error validating JTAG scan chain, IR mismatch, scan returned 0x%s", cbuf);
1460 free(cbuf);
1461 free(ir_test);
1462 return ERROR_JTAG_INIT_FAILED;
1463 }
1464
1465 free(ir_test);
1466
1467 return ERROR_OK;
1468 }
1469
1470 int jtag_register_commands(struct command_context_s *cmd_ctx)
1471 {
1472 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1473 COMMAND_CONFIG, NULL);
1474 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1475 COMMAND_ANY, "set jtag speed (if supported) <reset speed> [<post reset speed, default value is reset speed>]");
1476 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1477 COMMAND_ANY, "same as jtag_speed, except it takes maximum khz as arguments. 0 KHz = RTCK.");
1478 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1479 COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
1480 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1481 COMMAND_CONFIG, NULL);
1482 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1483 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
1484 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1485 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
1486
1487 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1488 COMMAND_EXEC, "print current scan chain configuration");
1489
1490 register_command(cmd_ctx, NULL, "endstate", handle_endstate_command,
1491 COMMAND_EXEC, "finish JTAG operations in <tap_state>");
1492 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1493 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1494 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1495 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1496 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1497 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1498 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
1499
1500 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1501 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1502 return ERROR_OK;
1503 }
1504
1505 int jtag_interface_init(struct command_context_s *cmd_ctx)
1506 {
1507 if (jtag)
1508 return ERROR_OK;
1509
1510 if (!jtag_interface)
1511 {
1512 /* nothing was previously specified by "interface" command */
1513 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1514 return ERROR_JTAG_INVALID_INTERFACE;
1515 }
1516 if(hasKHz)
1517 {
1518 /*stay on "reset speed"*/
1519 jtag_interface->khz(speed1, &jtag_speed);
1520 jtag_interface->khz(speed2, &jtag_speed_post_reset);
1521 hasKHz = 0;
1522 }
1523
1524 if (jtag_interface->init() != ERROR_OK)
1525 return ERROR_JTAG_INIT_FAILED;
1526
1527
1528
1529 jtag = jtag_interface;
1530 return ERROR_OK;
1531 }
1532
1533 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1534 {
1535 int validate_tries = 0;
1536 jtag_device_t *device;
1537 int retval;
1538
1539 LOG_DEBUG("Init JTAG chain");
1540
1541 device = jtag_devices;
1542 jtag_ir_scan_size = 0;
1543 jtag_num_devices = 0;
1544 while (device != NULL)
1545 {
1546 jtag_ir_scan_size += device->ir_length;
1547 jtag_num_devices++;
1548 device = device->next;
1549 }
1550
1551 jtag_add_tlr();
1552 if ((retval=jtag_execute_queue())!=ERROR_OK)
1553 return retval;
1554
1555 /* examine chain first, as this could discover the real chain layout */
1556 if (jtag_examine_chain() != ERROR_OK)
1557 {
1558 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1559 }
1560
1561 while (jtag_validate_chain() != ERROR_OK)
1562 {
1563 validate_tries++;
1564
1565 if (validate_tries > 5)
1566 {
1567 LOG_ERROR("Could not validate JTAG chain, exit");
1568 return ERROR_JTAG_INVALID_INTERFACE;
1569 }
1570 usleep(10000);
1571 }
1572
1573 return ERROR_OK;
1574 }
1575
1576 int jtag_init_reset(struct command_context_s *cmd_ctx)
1577 {
1578 int retval;
1579
1580 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1581 return retval;
1582
1583 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / TLR");
1584
1585 /* Reset can happen after a power cycle.
1586 *
1587 * Ideally we would only assert TRST or run TLR before the target reset.
1588 *
1589 * However w/srst_pulls_trst, trst is asserted together with the target
1590 * reset whether we want it or not.
1591 *
1592 * NB! Some targets have JTAG circuitry disabled until a
1593 * trst & srst has been asserted.
1594 *
1595 * NB! here we assume nsrst/ntrst delay are sufficient!
1596 *
1597 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1598 *
1599 */
1600 jtag_add_reset(1, 0); /* TLR or TRST */
1601 if (jtag_reset_config & RESET_HAS_SRST)
1602 {
1603 jtag_add_reset(1, 1);
1604 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
1605 jtag_add_reset(0, 1);
1606 }
1607 jtag_add_reset(0, 0);
1608 if ((retval = jtag_execute_queue()) != ERROR_OK)
1609 return retval;
1610
1611 /* Check that we can communication on the JTAG chain + eventually we want to
1612 * be able to perform enumeration only after OpenOCD has started
1613 * telnet and GDB server
1614 *
1615 * That would allow users to more easily perform any magic they need to before
1616 * reset happens.
1617 */
1618 return jtag_init_inner(cmd_ctx);
1619 }
1620
1621 int jtag_init(struct command_context_s *cmd_ctx)
1622 {
1623 int retval;
1624 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1625 return retval;
1626 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
1627 {
1628 return ERROR_OK;
1629 }
1630 return jtag_init_reset(cmd_ctx);
1631 }
1632
1633
1634 static int default_khz(int khz, int *jtag_speed)
1635 {
1636 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1637 return ERROR_FAIL;
1638 }
1639
1640 static int default_speed_div(int speed, int *khz)
1641 {
1642 return ERROR_FAIL;
1643 }
1644
1645 int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1646 {
1647 int i;
1648
1649 /* check whether the interface is already configured */
1650 if (jtag_interface)
1651 {
1652 LOG_WARNING("Interface already configured, ignoring");
1653 return ERROR_OK;
1654 }
1655
1656 /* interface name is a mandatory argument */
1657 if (argc < 1 || args[0][0] == '\0')
1658 {
1659 return ERROR_COMMAND_SYNTAX_ERROR;
1660 }
1661
1662 for (i=0; jtag_interfaces[i]; i++)
1663 {
1664 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1665 {
1666 if (jtag_interfaces[i]->register_commands(cmd_ctx) != ERROR_OK)
1667 exit(-1);
1668
1669 jtag_interface = jtag_interfaces[i];
1670
1671 if (jtag_interface->khz == NULL)
1672 {
1673 jtag_interface->khz = default_khz;
1674 }
1675 if (jtag_interface->speed_div == NULL)
1676 {
1677 jtag_interface->speed_div = default_speed_div;
1678 }
1679 return ERROR_OK;
1680 }
1681 }
1682
1683 /* no valid interface was found (i.e. the configuration option,
1684 * didn't match one of the compiled-in interfaces
1685 */
1686 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
1687 LOG_ERROR("compiled-in jtag interfaces:");
1688 for (i = 0; jtag_interfaces[i]; i++)
1689 {
1690 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
1691 }
1692
1693 return ERROR_JTAG_INVALID_INTERFACE;
1694 }
1695
1696 int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1697 {
1698 jtag_device_t **last_device_p = &jtag_devices;
1699
1700 if (*last_device_p)
1701 {
1702 while ((*last_device_p)->next)
1703 last_device_p = &((*last_device_p)->next);
1704 last_device_p = &((*last_device_p)->next);
1705 }
1706
1707 if (argc < 3)
1708 return ERROR_OK;
1709
1710 *last_device_p = malloc(sizeof(jtag_device_t));
1711 (*last_device_p)->ir_length = strtoul(args[0], NULL, 0);
1712
1713 (*last_device_p)->expected = malloc((*last_device_p)->ir_length);
1714 buf_set_u32((*last_device_p)->expected, 0, (*last_device_p)->ir_length, strtoul(args[1], NULL, 0));
1715 (*last_device_p)->expected_mask = malloc((*last_device_p)->ir_length);
1716 buf_set_u32((*last_device_p)->expected_mask, 0, (*last_device_p)->ir_length, strtoul(args[2], NULL, 0));
1717
1718 (*last_device_p)->cur_instr = malloc((*last_device_p)->ir_length);
1719 (*last_device_p)->bypass = 1;
1720 buf_set_ones((*last_device_p)->cur_instr, (*last_device_p)->ir_length);
1721
1722 (*last_device_p)->next = NULL;
1723
1724 jtag_register_event_callback(jtag_reset_callback, (*last_device_p));
1725
1726 jtag_num_devices++;
1727
1728 return ERROR_OK;
1729 }
1730
1731 int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1732 {
1733 jtag_device_t *device = jtag_devices;
1734 int device_count = 0;
1735
1736 while (device)
1737 {
1738 u32 expected, expected_mask, cur_instr;
1739 expected = buf_get_u32(device->expected, 0, device->ir_length);
1740 expected_mask = buf_get_u32(device->expected_mask, 0, device->ir_length);
1741 cur_instr = buf_get_u32(device->cur_instr, 0, device->ir_length);
1742 command_print(cmd_ctx, "%i: idcode: 0x%8.8x ir length %i, ir capture 0x%x, ir mask 0x%x, current instruction 0x%x", device_count, device->idcode, device->ir_length, expected, expected_mask, cur_instr);
1743 device = device->next;
1744 device_count++;
1745 }
1746
1747 return ERROR_OK;
1748 }
1749
1750 int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1751 {
1752 if (argc < 1)
1753 return ERROR_COMMAND_SYNTAX_ERROR;
1754
1755 if (argc >= 1)
1756 {
1757 if (strcmp(args[0], "none") == 0)
1758 jtag_reset_config = RESET_NONE;
1759 else if (strcmp(args[0], "trst_only") == 0)
1760 jtag_reset_config = RESET_HAS_TRST;
1761 else if (strcmp(args[0], "srst_only") == 0)
1762 jtag_reset_config = RESET_HAS_SRST;
1763 else if (strcmp(args[0], "trst_and_srst") == 0)
1764 jtag_reset_config = RESET_TRST_AND_SRST;
1765 else
1766 {
1767 LOG_ERROR("invalid reset_config argument, defaulting to none");
1768 jtag_reset_config = RESET_NONE;
1769 return ERROR_INVALID_ARGUMENTS;
1770 }
1771 }
1772
1773 if (argc >= 2)
1774 {
1775 if (strcmp(args[1], "separate") == 0)
1776 {
1777 /* seperate reset lines - default */
1778 } else
1779 {
1780 if (strcmp(args[1], "srst_pulls_trst") == 0)
1781 jtag_reset_config |= RESET_SRST_PULLS_TRST;
1782 else if (strcmp(args[1], "trst_pulls_srst") == 0)
1783 jtag_reset_config |= RESET_TRST_PULLS_SRST;
1784 else if (strcmp(args[1], "combined") == 0)
1785 jtag_reset_config |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1786 else
1787 {
1788 LOG_ERROR("invalid reset_config argument, defaulting to none");
1789 jtag_reset_config = RESET_NONE;
1790 return ERROR_INVALID_ARGUMENTS;
1791 }
1792 }
1793 }
1794
1795 if (argc >= 3)
1796 {
1797 if (strcmp(args[2], "trst_open_drain") == 0)
1798 jtag_reset_config |= RESET_TRST_OPEN_DRAIN;
1799 else if (strcmp(args[2], "trst_push_pull") == 0)
1800 jtag_reset_config &= ~RESET_TRST_OPEN_DRAIN;
1801 else
1802 {
1803 LOG_ERROR("invalid reset_config argument, defaulting to none");
1804 jtag_reset_config = RESET_NONE;
1805 return ERROR_INVALID_ARGUMENTS;
1806 }
1807 }
1808
1809 if (argc >= 4)
1810 {
1811 if (strcmp(args[3], "srst_push_pull") == 0)
1812 jtag_reset_config |= RESET_SRST_PUSH_PULL;
1813 else if (strcmp(args[3], "srst_open_drain") == 0)
1814 jtag_reset_config &= ~RESET_SRST_PUSH_PULL;
1815 else
1816 {
1817 LOG_ERROR("invalid reset_config argument, defaulting to none");
1818 jtag_reset_config = RESET_NONE;
1819 return ERROR_INVALID_ARGUMENTS;
1820 }
1821 }
1822
1823 return ERROR_OK;
1824 }
1825
1826 int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1827 {
1828 if (argc < 1)
1829 {
1830 LOG_ERROR("jtag_nsrst_delay <ms> command takes one required argument");
1831 exit(-1);
1832 }
1833 else
1834 {
1835 jtag_nsrst_delay = strtoul(args[0], NULL, 0);
1836 }
1837
1838 return ERROR_OK;
1839 }
1840
1841 int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1842 {
1843 if (argc < 1)
1844 {
1845 LOG_ERROR("jtag_ntrst_delay <ms> command takes one required argument");
1846 exit(-1);
1847 }
1848 else
1849 {
1850 jtag_ntrst_delay = strtoul(args[0], NULL, 0);
1851 }
1852
1853 return ERROR_OK;
1854 }
1855
1856 int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1857 {
1858 int cur_speed = 0;
1859
1860 if (argc != 0)
1861 {
1862 if ((argc<1) || (argc>2))
1863 return ERROR_COMMAND_SYNTAX_ERROR;
1864
1865 LOG_DEBUG("handle jtag speed");
1866
1867 if (argc >= 1)
1868 cur_speed = jtag_speed = jtag_speed_post_reset = strtoul(args[0], NULL, 0);
1869 if (argc == 2)
1870 cur_speed = jtag_speed_post_reset = strtoul(args[1], NULL, 0);
1871
1872 /* this command can be called during CONFIG,
1873 * in which case jtag isn't initialized */
1874 if (jtag)
1875 {
1876 jtag->speed_div(jtag_speed, &speed1);
1877 jtag->speed_div(jtag_speed_post_reset, &speed2);
1878 jtag->speed(cur_speed);
1879 }
1880 }
1881 command_print(cmd_ctx, "jtag_speed: %d, %d", jtag_speed, jtag_speed_post_reset);
1882
1883 return ERROR_OK;
1884 }
1885
1886 int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1887 {
1888 LOG_DEBUG("handle jtag khz");
1889
1890 if (argc>2)
1891 return ERROR_COMMAND_SYNTAX_ERROR;
1892
1893 if(argc != 0)
1894 {
1895
1896 if (argc >= 1)
1897 speed1 = speed2 = strtoul(args[0], NULL, 0);
1898 if (argc == 2)
1899 speed2 = strtoul(args[1], NULL, 0);
1900
1901 if (jtag != NULL)
1902 {
1903 int cur_speed = 0;
1904 LOG_DEBUG("have interface set up");
1905 int speed_div1, speed_div2;
1906 if (jtag->khz(speed1, &speed_div1)!=ERROR_OK)
1907 {
1908 speed1 = speed2 = 0;
1909 return ERROR_OK;
1910 }
1911 if (jtag->khz(speed2, &speed_div2)!=ERROR_OK)
1912 {
1913 speed1 = speed2 = 0;
1914 return ERROR_OK;
1915 }
1916
1917 if (argc >= 1)
1918 cur_speed = jtag_speed = jtag_speed_post_reset = speed_div1;
1919 if (argc == 2)
1920 cur_speed = jtag_speed_post_reset = speed_div2;
1921
1922 jtag->speed(cur_speed);
1923 } else
1924 {
1925 hasKHz = 1;
1926 }
1927 }
1928 command_print(cmd_ctx, "jtag_khz: %d, %d", speed1, speed2);
1929
1930 return ERROR_OK;
1931 }
1932
1933
1934 int handle_endstate_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1935 {
1936 enum tap_state state;
1937
1938 if (argc < 1)
1939 {
1940 return ERROR_COMMAND_SYNTAX_ERROR;
1941 }
1942 else
1943 {
1944 for (state = 0; state < 16; state++)
1945 {
1946 if (strcmp(args[0], tap_state_strings[state]) == 0)
1947 {
1948 jtag_add_end_state(state);
1949 jtag_execute_queue();
1950 }
1951 }
1952 }
1953 command_print(cmd_ctx, "current endstate: %s", tap_state_strings[cmd_queue_end_state]);
1954
1955 return ERROR_OK;
1956 }
1957
1958 int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1959 {
1960 int trst = -1;
1961 int srst = -1;
1962
1963 if (argc < 2)
1964 {
1965 return ERROR_COMMAND_SYNTAX_ERROR;
1966 }
1967
1968 if (args[0][0] == '1')
1969 trst = 1;
1970 else if (args[0][0] == '0')
1971 trst = 0;
1972 else
1973 {
1974 return ERROR_COMMAND_SYNTAX_ERROR;
1975 }
1976
1977 if (args[1][0] == '1')
1978 srst = 1;
1979 else if (args[1][0] == '0')
1980 srst = 0;
1981 else
1982 {
1983 return ERROR_COMMAND_SYNTAX_ERROR;
1984 }
1985
1986 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
1987 return ERROR_JTAG_INIT_FAILED;
1988
1989 jtag_add_reset(trst, srst);
1990 jtag_execute_queue();
1991
1992 return ERROR_OK;
1993 }
1994
1995 int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1996 {
1997 if (argc < 1)
1998 {
1999 return ERROR_COMMAND_SYNTAX_ERROR;
2000 }
2001
2002 jtag_add_runtest(strtol(args[0], NULL, 0), -1);
2003 jtag_execute_queue();
2004
2005 return ERROR_OK;
2006
2007 }
2008
2009
2010 int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2011 {
2012 int i;
2013 scan_field_t *fields;
2014
2015 if ((argc < 2) || (argc % 2))
2016 {
2017 return ERROR_COMMAND_SYNTAX_ERROR;
2018 }
2019
2020 fields = malloc(sizeof(scan_field_t) * argc / 2);
2021
2022 for (i = 0; i < argc / 2; i++)
2023 {
2024 int device = strtoul(args[i*2], NULL, 0);
2025 int field_size = jtag_get_device(device)->ir_length;
2026 fields[i].device = device;
2027 fields[i].out_value = malloc(CEIL(field_size, 8));
2028 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
2029 fields[i].out_mask = NULL;
2030 fields[i].in_value = NULL;
2031 fields[i].in_check_mask = NULL;
2032 fields[i].in_handler = NULL;
2033 fields[i].in_handler_priv = NULL;
2034 }
2035
2036 jtag_add_ir_scan(argc / 2, fields, -1);
2037 jtag_execute_queue();
2038
2039 for (i = 0; i < argc / 2; i++)
2040 free(fields[i].out_value);
2041
2042 free (fields);
2043
2044 return ERROR_OK;
2045 }
2046
2047 int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
2048 {
2049 int retval;
2050 scan_field_t *fields;
2051 int num_fields;
2052 int field_count = 0;
2053 int i, j, e;
2054 long device;
2055
2056 /* args[1] = device
2057 * args[2] = num_bits
2058 * args[3] = hex string
2059 * ... repeat num bits and hex string ...
2060 */
2061 if ((argc < 4) || ((argc % 2)!=0))
2062 {
2063 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
2064 return JIM_ERR;
2065 }
2066
2067 for (i = 2; i < argc; i+=2)
2068 {
2069 long bits;
2070
2071 e = Jim_GetLong(interp, args[i], &bits);
2072 if (e != JIM_OK)
2073 return e;
2074 }
2075
2076 e = Jim_GetLong(interp, args[1], &device);
2077 if (e != JIM_OK)
2078 return e;
2079
2080 num_fields=(argc-2)/2;
2081 fields = malloc(sizeof(scan_field_t) * num_fields);
2082 for (i = 2; i < argc; i+=2)
2083 {
2084 long bits;
2085 int len;
2086 const char *str;
2087
2088 Jim_GetLong(interp, args[i], &bits);
2089 str = Jim_GetString(args[i+1], &len);
2090
2091 fields[field_count].device = device;
2092 fields[field_count].num_bits = bits;
2093 fields[field_count].out_value = malloc(CEIL(bits, 8));
2094 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
2095 fields[field_count].out_mask = NULL;
2096 fields[field_count].in_value = fields[field_count].out_value;
2097 fields[field_count].in_check_mask = NULL;
2098 fields[field_count].in_check_value = NULL;
2099 fields[field_count].in_handler = NULL;
2100 fields[field_count++].in_handler_priv = NULL;
2101 }
2102
2103 jtag_add_dr_scan(num_fields, fields, -1);
2104 retval = jtag_execute_queue();
2105 if (retval != ERROR_OK)
2106 {
2107 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2108 Jim_AppendStrings(interp, Jim_GetResult(interp), "drscan: jtag execute failed", NULL);
2109 return JIM_ERR;
2110 }
2111
2112 field_count=0;
2113 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
2114 for (i = 2; i < argc; i+=2)
2115 {
2116 long bits;
2117 char *str;
2118
2119 Jim_GetLong(interp, args[i], &bits);
2120 str = buf_to_str(fields[field_count].in_value, bits, 16);
2121 free(fields[field_count].out_value);
2122
2123 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
2124 free(str);
2125 field_count++;
2126 }
2127
2128 Jim_SetResult(interp, list);
2129
2130 free(fields);
2131
2132 return JIM_OK;
2133 }
2134
2135 int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2136 {
2137 if (argc == 1)
2138 {
2139 if (strcmp(args[0], "enable") == 0)
2140 {
2141 jtag_verify_capture_ir = 1;
2142 }
2143 else if (strcmp(args[0], "disable") == 0)
2144 {
2145 jtag_verify_capture_ir = 0;
2146 } else
2147 {
2148 return ERROR_COMMAND_SYNTAX_ERROR;
2149 }
2150 } else if (argc != 0)
2151 {
2152 return ERROR_COMMAND_SYNTAX_ERROR;
2153 }
2154
2155 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
2156
2157 return ERROR_OK;
2158 }

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)