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