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

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)