Initialize return value.
[openocd.git] / src / jtag / drivers / driver.c
blob49f40246e976f70447ee2fb4d72dadf89cdeb766
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2009 SoftPLC Corporation *
9 * http://softplc.com *
10 * dick@softplc.com *
11 * *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
14 * *
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. *
19 * *
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. *
24 * *
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 ***************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
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;
60 /**
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 /**
74 * see jtag_add_ir_scan()
77 int interface_jtag_add_ir_scan(struct jtag_tap* active, 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;
88 cmd->cmd.scan = scan;
90 scan->ir_scan = true;
91 scan->num_fields = num_taps; /* one field per device */
92 scan->fields = out_fields;
93 scan->end_state = state;
96 struct scan_field * field = out_fields; /* keep track where we insert data */
98 /* loop over all enabled TAPs */
100 for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap))
102 /* search the input field list for fields for the current TAP */
104 if (tap == active)
106 /* if TAP is listed in input fields, copy the value */
107 tap->bypass = 0;
109 cmd_queue_scan_field_clone(field, in_fields);
110 } else
112 /* if a TAP isn't listed in input fields, set it to BYPASS */
114 tap->bypass = 1;
116 field->num_bits = tap->ir_length;
117 field->out_value = buf_set_ones(cmd_queue_alloc(DIV_ROUND_UP(tap->ir_length, 8)), tap->ir_length);
118 field->in_value = NULL; /* do not collect input for tap's in bypass */
121 /* update device information */
122 buf_cpy(field->out_value, tap->cur_instr, tap->ir_length);
124 field++;
127 assert(field == out_fields + num_taps); /* paranoia: jtag_tap_count_enabled() and jtag_tap_next_enabled() not in sync */
129 return ERROR_OK;
133 * see jtag_add_dr_scan()
136 int interface_jtag_add_dr_scan(struct jtag_tap* active, int in_num_fields, const struct scan_field *in_fields, tap_state_t state)
138 /* count devices in bypass */
140 size_t bypass_devices = 0;
142 for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap))
144 if (tap->bypass)
145 bypass_devices++;
148 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
149 struct scan_command * scan = cmd_queue_alloc(sizeof(struct scan_command));
150 struct scan_field * out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(struct scan_field));
152 jtag_queue_command(cmd);
154 cmd->type = JTAG_SCAN;
155 cmd->cmd.scan = scan;
157 scan->ir_scan = false;
158 scan->num_fields = in_num_fields + bypass_devices;
159 scan->fields = out_fields;
160 scan->end_state = state;
163 struct scan_field * field = out_fields; /* keep track where we insert data */
165 /* loop over all enabled TAPs */
167 for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap))
169 /* if TAP is not bypassed insert matching input fields */
171 if (!tap->bypass)
173 assert(active == tap);
174 #ifndef NDEBUG
175 /* remember initial position for assert() */
176 struct scan_field *start_field = field;
177 #endif /* NDEBUG */
179 for (int j = 0; j < in_num_fields; j++)
181 cmd_queue_scan_field_clone(field, in_fields + j);
183 field++;
186 assert(field > start_field); /* must have at least one input field per not bypassed TAP */
189 /* if a TAP is bypassed, generated a dummy bit*/
190 else
192 field->num_bits = 1;
193 field->out_value = NULL;
194 field->in_value = NULL;
196 field++;
200 assert(field == out_fields + scan->num_fields); /* no superfluous input fields permitted */
202 return ERROR_OK;
208 * Generate a DR SCAN using the array of output values passed to the function
210 * This function assumes that the parameter target_tap specifies the one TAP
211 * that is not bypassed. All other TAPs must be bypassed and the function will
212 * generate a dummy 1bit field for them.
214 * For the target_tap a sequence of output-only fields will be generated where
215 * each field has the size num_bits and the field's values are taken from
216 * the array value.
218 * The bypass status of TAPs is set by jtag_add_ir_scan().
221 void interface_jtag_add_dr_out(struct jtag_tap *target_tap,
222 int in_num_fields,
223 const int *num_bits,
224 const uint32_t *value,
225 tap_state_t end_state)
227 /* count devices in bypass */
229 size_t bypass_devices = 0;
231 for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap))
233 if (tap->bypass)
234 bypass_devices++;
238 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
239 struct scan_command * scan = cmd_queue_alloc(sizeof(struct scan_command));
240 struct scan_field * out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(struct scan_field));
242 jtag_queue_command(cmd);
244 cmd->type = JTAG_SCAN;
245 cmd->cmd.scan = scan;
247 scan->ir_scan = false;
248 scan->num_fields = in_num_fields + bypass_devices;
249 scan->fields = out_fields;
250 scan->end_state = end_state;
253 bool target_tap_match = false;
255 struct scan_field * field = out_fields; /* keep track where we insert data */
257 /* loop over all enabled TAPs */
259 for (struct jtag_tap * tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap))
261 /* if TAP is not bypassed insert matching input fields */
263 if (!tap->bypass)
265 assert(tap == target_tap); /* target_tap must match the one not bypassed TAP */
267 target_tap_match = true;
269 for (int j = 0; j < in_num_fields; j++)
271 uint8_t out_value[4];
272 size_t scan_size = num_bits[j];
273 buf_set_u32(out_value, 0, scan_size, value[j]);
275 field->num_bits = scan_size;
276 field->out_value = buf_cpy(out_value, cmd_queue_alloc(DIV_ROUND_UP(scan_size, 8)), scan_size);
277 field->in_value = NULL;
279 field++;
283 /* if a TAP is bypassed, generated a dummy bit*/
284 else
287 field->num_bits = 1;
288 field->out_value = NULL;
289 field->in_value = NULL;
291 field++;
295 assert(target_tap_match); /* target_tap should be enabled and not bypassed */
298 static int jtag_add_plain_scan(int num_bits, const uint8_t *out_bits,
299 uint8_t *in_bits, tap_state_t state, bool ir_scan)
301 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
302 struct scan_command * scan = cmd_queue_alloc(sizeof(struct scan_command));
303 struct scan_field * out_fields = cmd_queue_alloc(sizeof(struct scan_field));
305 jtag_queue_command(cmd);
307 cmd->type = JTAG_SCAN;
308 cmd->cmd.scan = scan;
310 scan->ir_scan = ir_scan;
311 scan->num_fields = 1;
312 scan->fields = out_fields;
313 scan->end_state = state;
315 out_fields->num_bits = num_bits;
316 out_fields->out_value = buf_cpy(out_bits, cmd_queue_alloc(DIV_ROUND_UP(num_bits, 8)), num_bits);
317 out_fields->in_value = in_bits;
319 return ERROR_OK;
322 int interface_jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
324 return jtag_add_plain_scan(num_bits, out_bits, in_bits, state, false);
327 int interface_jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
329 return jtag_add_plain_scan(num_bits, out_bits, in_bits, state, true);
332 int interface_jtag_add_tlr(void)
334 tap_state_t state = TAP_RESET;
336 /* allocate memory for a new list member */
337 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
339 jtag_queue_command(cmd);
341 cmd->type = JTAG_TLR_RESET;
343 cmd->cmd.statemove = cmd_queue_alloc(sizeof(struct statemove_command));
344 cmd->cmd.statemove->end_state = state;
346 return ERROR_OK;
349 int interface_add_tms_seq(unsigned num_bits, const uint8_t *seq, enum tap_state state)
351 struct jtag_command *cmd;
353 cmd = cmd_queue_alloc(sizeof(struct jtag_command));
354 if (cmd == NULL)
355 return ERROR_FAIL;
357 cmd->type = JTAG_TMS;
358 cmd->cmd.tms = cmd_queue_alloc(sizeof(*cmd->cmd.tms));
359 if (!cmd->cmd.tms)
360 return ERROR_FAIL;
362 /* copy the bits; our caller doesn't guarantee they'll persist */
363 cmd->cmd.tms->num_bits = num_bits;
364 cmd->cmd.tms->bits = buf_cpy(seq,
365 cmd_queue_alloc(DIV_ROUND_UP(num_bits, 8)), num_bits);
366 if (!cmd->cmd.tms->bits)
367 return ERROR_FAIL;
369 jtag_queue_command(cmd);
371 return ERROR_OK;
374 int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
376 /* allocate memory for a new list member */
377 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
379 jtag_queue_command(cmd);
381 cmd->type = JTAG_PATHMOVE;
383 cmd->cmd.pathmove = cmd_queue_alloc(sizeof(struct pathmove_command));
384 cmd->cmd.pathmove->num_states = num_states;
385 cmd->cmd.pathmove->path = cmd_queue_alloc(sizeof(tap_state_t) * num_states);
387 for (int i = 0; i < num_states; i++)
388 cmd->cmd.pathmove->path[i] = path[i];
390 return ERROR_OK;
393 int interface_jtag_add_runtest(int num_cycles, tap_state_t state)
395 /* allocate memory for a new list member */
396 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
398 jtag_queue_command(cmd);
400 cmd->type = JTAG_RUNTEST;
402 cmd->cmd.runtest = cmd_queue_alloc(sizeof(struct runtest_command));
403 cmd->cmd.runtest->num_cycles = num_cycles;
404 cmd->cmd.runtest->end_state = state;
406 return ERROR_OK;
409 int interface_jtag_add_clocks(int num_cycles)
411 /* allocate memory for a new list member */
412 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
414 jtag_queue_command(cmd);
416 cmd->type = JTAG_STABLECLOCKS;
418 cmd->cmd.stableclocks = cmd_queue_alloc(sizeof(struct stableclocks_command));
419 cmd->cmd.stableclocks->num_cycles = num_cycles;
421 return ERROR_OK;
424 int interface_jtag_add_reset(int req_trst, int req_srst)
426 /* allocate memory for a new list member */
427 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
429 jtag_queue_command(cmd);
431 cmd->type = JTAG_RESET;
433 cmd->cmd.reset = cmd_queue_alloc(sizeof(struct reset_command));
434 cmd->cmd.reset->trst = req_trst;
435 cmd->cmd.reset->srst = req_srst;
437 return ERROR_OK;
440 int interface_jtag_add_sleep(uint32_t us)
442 /* allocate memory for a new list member */
443 struct jtag_command * cmd = cmd_queue_alloc(sizeof(struct jtag_command));
445 jtag_queue_command(cmd);
447 cmd->type = JTAG_SLEEP;
449 cmd->cmd.sleep = cmd_queue_alloc(sizeof(struct sleep_command));
450 cmd->cmd.sleep->us = us;
452 return ERROR_OK;
455 /* add callback to end of queue */
456 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)
458 struct jtag_callback_entry *entry = cmd_queue_alloc(sizeof(struct jtag_callback_entry));
460 entry->next = NULL;
461 entry->callback = callback;
462 entry->data0 = data0;
463 entry->data1 = data1;
464 entry->data2 = data2;
465 entry->data3 = data3;
467 if (jtag_callback_queue_head == NULL)
469 jtag_callback_queue_head = entry;
470 jtag_callback_queue_tail = entry;
471 } else
473 jtag_callback_queue_tail->next = entry;
474 jtag_callback_queue_tail = entry;
478 int interface_jtag_execute_queue(void)
480 static int reentry = 0;
482 assert(reentry==0);
483 reentry++;
485 int retval = default_interface_jtag_execute_queue();
486 if (retval == ERROR_OK)
488 struct jtag_callback_entry *entry;
489 for (entry = jtag_callback_queue_head; entry != NULL; entry = entry->next)
491 retval = entry->callback(entry->data0, entry->data1, entry->data2, entry->data3);
492 if (retval != ERROR_OK)
493 break;
497 jtag_command_queue_reset();
498 jtag_callback_queue_reset();
500 reentry--;
502 return retval;
505 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)
507 ((jtag_callback1_t)data1)(data0);
508 return ERROR_OK;
511 void interface_jtag_add_callback(jtag_callback1_t callback, jtag_callback_data_t data0)
513 jtag_add_callback4(jtag_convert_to_callback4, data0, (jtag_callback_data_t)callback, 0, 0);
517 /* A minidriver can use use an inline versions of this API level fn */
518 void jtag_add_dr_out(struct jtag_tap* tap,
519 int num_fields, const int* num_bits, const uint32_t* value,
520 tap_state_t end_state)
522 assert(end_state != TAP_RESET);
523 assert(end_state != TAP_INVALID);
525 cmd_queue_cur_state = end_state;
527 interface_jtag_add_dr_out(tap,
528 num_fields, num_bits, value,
529 end_state);
532 void jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0)
534 interface_jtag_add_callback(f, data0);
537 void jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0,
538 jtag_callback_data_t data1, jtag_callback_data_t data2,
539 jtag_callback_data_t data3)
541 interface_jtag_add_callback4(f, data0, data1, data2, data3);