1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2009 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2009 SoftPLC Corporation *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
29 ***************************************************************************/
34 #include <jtag/jtag.h>
35 #include <jtag/interface.h>
36 #include <jtag/commands.h>
37 #include <jtag/minidriver.h>
38 #include <helper/command.h>
40 struct jtag_callback_entry
42 struct jtag_callback_entry *next;
44 jtag_callback_t callback;
45 jtag_callback_data_t data0;
46 jtag_callback_data_t data1;
47 jtag_callback_data_t data2;
48 jtag_callback_data_t data3;
51 static struct jtag_callback_entry *jtag_callback_queue_head = NULL;
52 static struct jtag_callback_entry *jtag_callback_queue_tail = NULL;
54 static void jtag_callback_queue_reset(void)
56 jtag_callback_queue_head = NULL;
57 jtag_callback_queue_tail = NULL;
61 * Copy a struct scan_field for insertion into the queue.
63 * This allocates a new copy of out_value using cmd_queue_alloc.
65 static void cmd_queue_scan_field_clone(struct scan_field * dst, const struct scan_field * src)
68 dst->num_bits = src->num_bits;
69 dst->out_value = buf_cpy(src->out_value, cmd_queue_alloc(DIV_ROUND_UP(src->num_bits, 8)), src->num_bits);
70 dst->in_value = src->in_value;
75 * see jtag_add_ir_scan()
78 int interface_jtag_add_ir_scan(int in_num_fields, const struct scan_field *in_fields, tap_state_t state)
80 size_t num_taps = jtag_tap_count_enabled();
82 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
83 struct scan_command * scan = cmd_queue_alloc(sizeof(struct scan_command));
84 struct scan_field * out_fields = cmd_queue_alloc(num_taps * sizeof(struct scan_field));
86 jtag_queue_command(cmd);
88 cmd->type = JTAG_SCAN;
92 scan->num_fields = num_taps; /* one field per device */
93 scan->fields = out_fields;
94 scan->end_state = state;
97 struct scan_field * field = out_fields; /* keep track where we insert data */
99 /* loop over all enabled TAPs */
101 for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap))
103 /* search the input field list for fields for the current TAP */
107 for (int j = 0; j < in_num_fields; j++)
109 if (tap != in_fields[j].tap)
112 /* if TAP is listed in input fields, copy the value */
118 assert(in_fields[j].num_bits == tap->ir_length); /* input fields must have the same length as the TAP's IR */
120 cmd_queue_scan_field_clone(field, in_fields + j);
127 /* if a TAP isn't listed in input fields, set it to BYPASS */
132 field->num_bits = tap->ir_length;
133 field->out_value = buf_set_ones(cmd_queue_alloc(DIV_ROUND_UP(tap->ir_length, 8)), tap->ir_length);
134 field->in_value = NULL; /* do not collect input for tap's in bypass */
137 /* update device information */
138 buf_cpy(field->out_value, tap->cur_instr, tap->ir_length);
143 assert(field == out_fields + num_taps); /* paranoia: jtag_tap_count_enabled() and jtag_tap_next_enabled() not in sync */
149 * see jtag_add_plain_ir_scan()
152 int interface_jtag_add_plain_ir_scan(int in_num_fields, const struct scan_field *in_fields, tap_state_t state)
155 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
156 struct scan_command * scan = cmd_queue_alloc(sizeof(struct scan_command));
157 struct scan_field * out_fields = cmd_queue_alloc(in_num_fields * sizeof(struct scan_field));
159 jtag_queue_command(cmd);
161 cmd->type = JTAG_SCAN;
162 cmd->cmd.scan = scan;
164 scan->ir_scan = true;
165 scan->num_fields = in_num_fields;
166 scan->fields = out_fields;
167 scan->end_state = state;
169 for (int i = 0; i < in_num_fields; i++)
170 cmd_queue_scan_field_clone(out_fields + i, in_fields + i);
178 * see jtag_add_dr_scan()
181 int interface_jtag_add_dr_scan(int in_num_fields, const struct scan_field *in_fields, tap_state_t state)
183 /* count devices in bypass */
185 size_t bypass_devices = 0;
187 for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap))
193 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
194 struct scan_command * scan = cmd_queue_alloc(sizeof(struct scan_command));
195 struct scan_field * out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(struct scan_field));
197 jtag_queue_command(cmd);
199 cmd->type = JTAG_SCAN;
200 cmd->cmd.scan = scan;
202 scan->ir_scan = false;
203 scan->num_fields = in_num_fields + bypass_devices;
204 scan->fields = out_fields;
205 scan->end_state = state;
208 struct scan_field * field = out_fields; /* keep track where we insert data */
210 /* loop over all enabled TAPs */
212 for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap))
214 /* if TAP is not bypassed insert matching input fields */
218 struct scan_field * start_field = field; /* keep initial position for assert() */
220 for (int j = 0; j < in_num_fields; j++)
222 if (tap != in_fields[j].tap)
225 cmd_queue_scan_field_clone(field, in_fields + j);
230 assert(field > start_field); /* must have at least one input field per not bypassed TAP */
233 /* if a TAP is bypassed, generated a dummy bit*/
238 field->out_value = NULL;
239 field->in_value = NULL;
245 assert(field == out_fields + scan->num_fields); /* no superfluous input fields permitted */
253 * Generate a DR SCAN using the array of output values passed to the function
255 * This function assumes that the parameter target_tap specifies the one TAP
256 * that is not bypassed. All other TAPs must be bypassed and the function will
257 * generate a dummy 1bit field for them.
259 * For the target_tap a sequence of output-only fields will be generated where
260 * each field has the size num_bits and the field's values are taken from
263 * The bypass status of TAPs is set by jtag_add_ir_scan().
266 void interface_jtag_add_dr_out(struct jtag_tap *target_tap,
269 const uint32_t *value,
270 tap_state_t end_state)
272 /* count devices in bypass */
274 size_t bypass_devices = 0;
276 for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap))
283 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
284 struct scan_command * scan = cmd_queue_alloc(sizeof(struct scan_command));
285 struct scan_field * out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(struct scan_field));
287 jtag_queue_command(cmd);
289 cmd->type = JTAG_SCAN;
290 cmd->cmd.scan = scan;
292 scan->ir_scan = false;
293 scan->num_fields = in_num_fields + bypass_devices;
294 scan->fields = out_fields;
295 scan->end_state = end_state;
298 bool target_tap_match = false;
300 struct scan_field * field = out_fields; /* keep track where we insert data */
302 /* loop over all enabled TAPs */
304 for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap))
306 /* if TAP is not bypassed insert matching input fields */
310 assert(tap == target_tap); /* target_tap must match the one not bypassed TAP */
312 target_tap_match = true;
314 for (int j = 0; j < in_num_fields; j++)
316 uint8_t out_value[4];
317 size_t scan_size = num_bits[j];
318 buf_set_u32(out_value, 0, scan_size, value[j]);
321 field->num_bits = scan_size;
322 field->out_value = buf_cpy(out_value, cmd_queue_alloc(DIV_ROUND_UP(scan_size, 8)), scan_size);
323 field->in_value = NULL;
329 /* if a TAP is bypassed, generated a dummy bit*/
335 field->out_value = NULL;
336 field->in_value = NULL;
342 assert(target_tap_match); /* target_tap should be enabled and not bypassed */
346 * see jtag_add_plain_dr_scan()
349 int interface_jtag_add_plain_dr_scan(int in_num_fields, const struct scan_field *in_fields, tap_state_t state)
351 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
352 struct scan_command * scan = cmd_queue_alloc(sizeof(struct scan_command));
353 struct scan_field * out_fields = cmd_queue_alloc(in_num_fields * sizeof(struct scan_field));
355 jtag_queue_command(cmd);
357 cmd->type = JTAG_SCAN;
358 cmd->cmd.scan = scan;
360 scan->ir_scan = false;
361 scan->num_fields = in_num_fields;
362 scan->fields = out_fields;
363 scan->end_state = state;
365 for (int i = 0; i < in_num_fields; i++)
366 cmd_queue_scan_field_clone(out_fields + i, in_fields + i);
371 int interface_jtag_add_tlr(void)
373 tap_state_t state = TAP_RESET;
375 /* allocate memory for a new list member */
376 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
378 jtag_queue_command(cmd);
380 cmd->type = JTAG_STATEMOVE;
382 cmd->cmd.statemove = cmd_queue_alloc(sizeof(struct statemove_command));
383 cmd->cmd.statemove->end_state = state;
388 int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
390 /* allocate memory for a new list member */
391 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
393 jtag_queue_command(cmd);
395 cmd->type = JTAG_PATHMOVE;
397 cmd->cmd.pathmove = cmd_queue_alloc(sizeof(struct pathmove_command));
398 cmd->cmd.pathmove->num_states = num_states;
399 cmd->cmd.pathmove->path = cmd_queue_alloc(sizeof(tap_state_t) * num_states);
401 for (int i = 0; i < num_states; i++)
402 cmd->cmd.pathmove->path[i] = path[i];
407 int interface_jtag_add_runtest(int num_cycles, tap_state_t state)
409 /* allocate memory for a new list member */
410 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
412 jtag_queue_command(cmd);
414 cmd->type = JTAG_RUNTEST;
416 cmd->cmd.runtest = cmd_queue_alloc(sizeof(struct runtest_command));
417 cmd->cmd.runtest->num_cycles = num_cycles;
418 cmd->cmd.runtest->end_state = state;
423 int interface_jtag_add_clocks(int num_cycles)
425 /* allocate memory for a new list member */
426 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
428 jtag_queue_command(cmd);
430 cmd->type = JTAG_STABLECLOCKS;
432 cmd->cmd.stableclocks = cmd_queue_alloc(sizeof(struct stableclocks_command));
433 cmd->cmd.stableclocks->num_cycles = num_cycles;
438 int interface_jtag_add_reset(int req_trst, int req_srst)
440 /* allocate memory for a new list member */
441 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
443 jtag_queue_command(cmd);
445 cmd->type = JTAG_RESET;
447 cmd->cmd.reset = cmd_queue_alloc(sizeof(struct reset_command));
448 cmd->cmd.reset->trst = req_trst;
449 cmd->cmd.reset->srst = req_srst;
454 int interface_jtag_add_sleep(uint32_t us)
456 /* allocate memory for a new list member */
457 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
459 jtag_queue_command(cmd);
461 cmd->type = JTAG_SLEEP;
463 cmd->cmd.sleep = cmd_queue_alloc(sizeof(struct sleep_command));
464 cmd->cmd.sleep->us = us;
469 /* add callback to end of queue */
470 void interface_jtag_add_callback4(jtag_callback_t callback, jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
472 struct jtag_callback_entry *entry = cmd_queue_alloc(sizeof(struct jtag_callback_entry));
475 entry->callback = callback;
476 entry->data0 = data0;
477 entry->data1 = data1;
478 entry->data2 = data2;
479 entry->data3 = data3;
481 if (jtag_callback_queue_head == NULL)
483 jtag_callback_queue_head = entry;
484 jtag_callback_queue_tail = entry;
487 jtag_callback_queue_tail->next = entry;
488 jtag_callback_queue_tail = entry;
492 int interface_jtag_execute_queue(void)
494 static int reentry = 0;
499 int retval = default_interface_jtag_execute_queue();
500 if (retval == ERROR_OK)
502 struct jtag_callback_entry *entry;
503 for (entry = jtag_callback_queue_head; entry != NULL; entry = entry->next)
505 retval = entry->callback(entry->data0, entry->data1, entry->data2, entry->data3);
506 if (retval != ERROR_OK)
511 jtag_command_queue_reset();
512 jtag_callback_queue_reset();
519 static int jtag_convert_to_callback4(jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
521 ((jtag_callback1_t)data1)(data0);
525 void interface_jtag_add_callback(jtag_callback1_t callback, jtag_callback_data_t data0)
527 jtag_add_callback4(jtag_convert_to_callback4, data0, (jtag_callback_data_t)callback, 0, 0);
531 /* A minidriver can use use an inline versions of this API level fn */
532 void jtag_add_dr_out(struct jtag_tap* tap,
533 int num_fields, const int* num_bits, const uint32_t* value,
534 tap_state_t end_state)
536 assert(end_state != TAP_RESET);
537 assert(end_state != TAP_INVALID);
539 cmd_queue_cur_state = end_state;
541 interface_jtag_add_dr_out(tap,
542 num_fields, num_bits, value,
546 void jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0)
548 interface_jtag_add_callback(f, data0);
551 void jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0,
552 jtag_callback_data_t data1, jtag_callback_data_t data2,
553 jtag_callback_data_t data3)
555 interface_jtag_add_callback4(f, data0, data1, data2, data3);