1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Ø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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
29 ***************************************************************************/
35 #include <jtag/jtag.h>
36 #include <jtag/interface.h>
37 #include <jtag/commands.h>
38 #include <jtag/minidriver.h>
39 #include <helper/command.h>
41 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;
52 static struct jtag_callback_entry *jtag_callback_queue_tail;
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)
67 dst->num_bits = src->num_bits;
68 dst->out_value = buf_cpy(src->out_value, cmd_queue_alloc(DIV_ROUND_UP(src->num_bits, 8)), src->num_bits);
69 dst->in_value = src->in_value;
73 * see jtag_add_ir_scan()
76 int interface_jtag_add_ir_scan(struct jtag_tap *active,
77 const struct scan_field *in_fields, tap_state_t state)
79 size_t num_taps = jtag_tap_count_enabled();
81 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
82 struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command));
83 struct scan_field *out_fields = cmd_queue_alloc(num_taps * sizeof(struct scan_field));
85 jtag_queue_command(cmd);
87 cmd->type = JTAG_SCAN;
91 scan->num_fields = num_taps; /* one field per device */
92 scan->fields = out_fields;
93 scan->end_state = state;
95 struct scan_field *field = out_fields; /* keep track where we insert data */
97 /* loop over all enabled TAPs */
99 for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) {
100 /* search the input field list for fields for the current TAP */
103 /* if TAP is listed in input fields, copy the value */
106 cmd_queue_scan_field_clone(field, in_fields);
108 /* if a TAP isn't listed in input fields, set it to BYPASS */
112 field->num_bits = tap->ir_length;
113 field->out_value = buf_set_ones(cmd_queue_alloc(DIV_ROUND_UP(tap->ir_length, 8)), tap->ir_length);
114 field->in_value = NULL; /* do not collect input for tap's in bypass */
117 /* update device information */
118 buf_cpy(field->out_value, tap->cur_instr, tap->ir_length);
122 /* paranoia: jtag_tap_count_enabled() and jtag_tap_next_enabled() not in sync */
123 assert(field == out_fields + num_taps);
129 * see jtag_add_dr_scan()
132 int interface_jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields,
133 const struct scan_field *in_fields, tap_state_t state)
135 /* count devices in bypass */
137 size_t bypass_devices = 0;
139 for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) {
144 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
145 struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command));
146 struct scan_field *out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(struct scan_field));
148 jtag_queue_command(cmd);
150 cmd->type = JTAG_SCAN;
151 cmd->cmd.scan = scan;
153 scan->ir_scan = false;
154 scan->num_fields = in_num_fields + bypass_devices;
155 scan->fields = out_fields;
156 scan->end_state = state;
158 struct scan_field *field = out_fields; /* keep track where we insert data */
160 /* loop over all enabled TAPs */
162 for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) {
163 /* if TAP is not bypassed insert matching input fields */
166 assert(active == tap);
168 /* remember initial position for assert() */
169 struct scan_field *start_field = field;
172 for (int j = 0; j < in_num_fields; j++) {
173 cmd_queue_scan_field_clone(field, in_fields + j);
178 assert(field > start_field); /* must have at least one input field per not bypassed TAP */
181 /* if a TAP is bypassed, generated a dummy bit*/
184 field->out_value = NULL;
185 field->in_value = NULL;
191 assert(field == out_fields + scan->num_fields); /* no superfluous input fields permitted */
196 static int jtag_add_plain_scan(int num_bits, const uint8_t *out_bits,
197 uint8_t *in_bits, tap_state_t state, bool ir_scan)
199 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
200 struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command));
201 struct scan_field *out_fields = cmd_queue_alloc(sizeof(struct scan_field));
203 jtag_queue_command(cmd);
205 cmd->type = JTAG_SCAN;
206 cmd->cmd.scan = scan;
208 scan->ir_scan = ir_scan;
209 scan->num_fields = 1;
210 scan->fields = out_fields;
211 scan->end_state = state;
213 out_fields->num_bits = num_bits;
214 out_fields->out_value = buf_cpy(out_bits, cmd_queue_alloc(DIV_ROUND_UP(num_bits, 8)), num_bits);
215 out_fields->in_value = in_bits;
220 int interface_jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
222 return jtag_add_plain_scan(num_bits, out_bits, in_bits, state, false);
225 int interface_jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
227 return jtag_add_plain_scan(num_bits, out_bits, in_bits, state, true);
230 int interface_jtag_add_tlr(void)
232 tap_state_t state = TAP_RESET;
234 /* allocate memory for a new list member */
235 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
237 jtag_queue_command(cmd);
239 cmd->type = JTAG_TLR_RESET;
241 cmd->cmd.statemove = cmd_queue_alloc(sizeof(struct statemove_command));
242 cmd->cmd.statemove->end_state = state;
247 int interface_add_tms_seq(unsigned num_bits, const uint8_t *seq, enum tap_state state)
249 struct jtag_command *cmd;
251 cmd = cmd_queue_alloc(sizeof(struct jtag_command));
255 cmd->type = JTAG_TMS;
256 cmd->cmd.tms = cmd_queue_alloc(sizeof(*cmd->cmd.tms));
260 /* copy the bits; our caller doesn't guarantee they'll persist */
261 cmd->cmd.tms->num_bits = num_bits;
262 cmd->cmd.tms->bits = buf_cpy(seq,
263 cmd_queue_alloc(DIV_ROUND_UP(num_bits, 8)), num_bits);
264 if (!cmd->cmd.tms->bits)
267 jtag_queue_command(cmd);
272 int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
274 /* allocate memory for a new list member */
275 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
277 jtag_queue_command(cmd);
279 cmd->type = JTAG_PATHMOVE;
281 cmd->cmd.pathmove = cmd_queue_alloc(sizeof(struct pathmove_command));
282 cmd->cmd.pathmove->num_states = num_states;
283 cmd->cmd.pathmove->path = cmd_queue_alloc(sizeof(tap_state_t) * num_states);
285 for (int i = 0; i < num_states; i++)
286 cmd->cmd.pathmove->path[i] = path[i];
291 int interface_jtag_add_runtest(int num_cycles, tap_state_t state)
293 /* allocate memory for a new list member */
294 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
296 jtag_queue_command(cmd);
298 cmd->type = JTAG_RUNTEST;
300 cmd->cmd.runtest = cmd_queue_alloc(sizeof(struct runtest_command));
301 cmd->cmd.runtest->num_cycles = num_cycles;
302 cmd->cmd.runtest->end_state = state;
307 int interface_jtag_add_clocks(int num_cycles)
309 /* allocate memory for a new list member */
310 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
312 jtag_queue_command(cmd);
314 cmd->type = JTAG_STABLECLOCKS;
316 cmd->cmd.stableclocks = cmd_queue_alloc(sizeof(struct stableclocks_command));
317 cmd->cmd.stableclocks->num_cycles = num_cycles;
322 int interface_jtag_add_reset(int req_trst, int req_srst)
324 /* allocate memory for a new list member */
325 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
327 jtag_queue_command(cmd);
329 cmd->type = JTAG_RESET;
331 cmd->cmd.reset = cmd_queue_alloc(sizeof(struct reset_command));
332 cmd->cmd.reset->trst = req_trst;
333 cmd->cmd.reset->srst = req_srst;
338 int interface_jtag_add_sleep(uint32_t us)
340 /* allocate memory for a new list member */
341 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
343 jtag_queue_command(cmd);
345 cmd->type = JTAG_SLEEP;
347 cmd->cmd.sleep = cmd_queue_alloc(sizeof(struct sleep_command));
348 cmd->cmd.sleep->us = us;
353 /* add callback to end of queue */
354 void interface_jtag_add_callback4(jtag_callback_t callback,
355 jtag_callback_data_t data0, jtag_callback_data_t data1,
356 jtag_callback_data_t data2, jtag_callback_data_t data3)
358 struct jtag_callback_entry *entry = cmd_queue_alloc(sizeof(struct jtag_callback_entry));
361 entry->callback = callback;
362 entry->data0 = data0;
363 entry->data1 = data1;
364 entry->data2 = data2;
365 entry->data3 = data3;
367 if (jtag_callback_queue_head == NULL) {
368 jtag_callback_queue_head = entry;
369 jtag_callback_queue_tail = entry;
371 jtag_callback_queue_tail->next = entry;
372 jtag_callback_queue_tail = entry;
376 int interface_jtag_execute_queue(void)
380 assert(reentry == 0);
383 int retval = default_interface_jtag_execute_queue();
384 if (retval == ERROR_OK) {
385 struct jtag_callback_entry *entry;
386 for (entry = jtag_callback_queue_head; entry != NULL; entry = entry->next) {
387 retval = entry->callback(entry->data0, entry->data1, entry->data2, entry->data3);
388 if (retval != ERROR_OK)
393 jtag_command_queue_reset();
394 jtag_callback_queue_reset();
401 static int jtag_convert_to_callback4(jtag_callback_data_t data0,
402 jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
404 ((jtag_callback1_t)data1)(data0);
408 void interface_jtag_add_callback(jtag_callback1_t callback, jtag_callback_data_t data0)
410 jtag_add_callback4(jtag_convert_to_callback4, data0, (jtag_callback_data_t)callback, 0, 0);
413 void jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0)
415 interface_jtag_add_callback(f, data0);
418 void jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0,
419 jtag_callback_data_t data1, jtag_callback_data_t data2,
420 jtag_callback_data_t data3)
422 interface_jtag_add_callback4(f, data0, data1, data2, data3);