Remove FSF address from GPL notices
[openocd.git] / src / jtag / drivers / driver.c
blobdaf7cd4266ed6733969eb972fbb92b263f5e4876
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, see <http://www.gnu.org/licenses/>. *
27 ***************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #include <jtag/jtag.h>
34 #include <jtag/interface.h>
35 #include <jtag/commands.h>
36 #include <jtag/minidriver.h>
37 #include <helper/command.h>
39 struct jtag_callback_entry {
40 struct jtag_callback_entry *next;
42 jtag_callback_t callback;
43 jtag_callback_data_t data0;
44 jtag_callback_data_t data1;
45 jtag_callback_data_t data2;
46 jtag_callback_data_t data3;
49 static struct jtag_callback_entry *jtag_callback_queue_head;
50 static struct jtag_callback_entry *jtag_callback_queue_tail;
52 static void jtag_callback_queue_reset(void)
54 jtag_callback_queue_head = NULL;
55 jtag_callback_queue_tail = NULL;
58 /**
59 * Copy a struct scan_field for insertion into the queue.
61 * This allocates a new copy of out_value using cmd_queue_alloc.
63 static void cmd_queue_scan_field_clone(struct scan_field *dst, const struct scan_field *src)
65 dst->num_bits = src->num_bits;
66 dst->out_value = buf_cpy(src->out_value, cmd_queue_alloc(DIV_ROUND_UP(src->num_bits, 8)), src->num_bits);
67 dst->in_value = src->in_value;
70 /**
71 * see jtag_add_ir_scan()
74 int interface_jtag_add_ir_scan(struct jtag_tap *active,
75 const struct scan_field *in_fields, tap_state_t state)
77 size_t num_taps = jtag_tap_count_enabled();
79 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
80 struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command));
81 struct scan_field *out_fields = cmd_queue_alloc(num_taps * sizeof(struct scan_field));
83 jtag_queue_command(cmd);
85 cmd->type = JTAG_SCAN;
86 cmd->cmd.scan = scan;
88 scan->ir_scan = true;
89 scan->num_fields = num_taps; /* one field per device */
90 scan->fields = out_fields;
91 scan->end_state = state;
93 struct scan_field *field = out_fields; /* keep track where we insert data */
95 /* loop over all enabled TAPs */
97 for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) {
98 /* search the input field list for fields for the current TAP */
100 if (tap == active) {
101 /* if TAP is listed in input fields, copy the value */
102 tap->bypass = 0;
104 cmd_queue_scan_field_clone(field, in_fields);
105 } else {
106 /* if a TAP isn't listed in input fields, set it to BYPASS */
108 tap->bypass = 1;
110 field->num_bits = tap->ir_length;
111 field->out_value = buf_set_ones(cmd_queue_alloc(DIV_ROUND_UP(tap->ir_length, 8)), tap->ir_length);
112 field->in_value = NULL; /* do not collect input for tap's in bypass */
115 /* update device information */
116 buf_cpy(field->out_value, tap->cur_instr, tap->ir_length);
118 field++;
120 /* paranoia: jtag_tap_count_enabled() and jtag_tap_next_enabled() not in sync */
121 assert(field == out_fields + num_taps);
123 return ERROR_OK;
127 * see jtag_add_dr_scan()
130 int interface_jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields,
131 const struct scan_field *in_fields, tap_state_t state)
133 /* count devices in bypass */
135 size_t bypass_devices = 0;
137 for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) {
138 if (tap->bypass)
139 bypass_devices++;
142 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
143 struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command));
144 struct scan_field *out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(struct scan_field));
146 jtag_queue_command(cmd);
148 cmd->type = JTAG_SCAN;
149 cmd->cmd.scan = scan;
151 scan->ir_scan = false;
152 scan->num_fields = in_num_fields + bypass_devices;
153 scan->fields = out_fields;
154 scan->end_state = state;
156 struct scan_field *field = out_fields; /* keep track where we insert data */
158 /* loop over all enabled TAPs */
160 for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) {
161 /* if TAP is not bypassed insert matching input fields */
163 if (!tap->bypass) {
164 assert(active == tap);
165 #ifndef NDEBUG
166 /* remember initial position for assert() */
167 struct scan_field *start_field = field;
168 #endif /* NDEBUG */
170 for (int j = 0; j < in_num_fields; j++) {
171 cmd_queue_scan_field_clone(field, in_fields + j);
173 field++;
176 assert(field > start_field); /* must have at least one input field per not bypassed TAP */
179 /* if a TAP is bypassed, generated a dummy bit*/
180 else {
181 field->num_bits = 1;
182 field->out_value = NULL;
183 field->in_value = NULL;
185 field++;
189 assert(field == out_fields + scan->num_fields); /* no superfluous input fields permitted */
191 return ERROR_OK;
194 static int jtag_add_plain_scan(int num_bits, const uint8_t *out_bits,
195 uint8_t *in_bits, tap_state_t state, bool ir_scan)
197 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
198 struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command));
199 struct scan_field *out_fields = cmd_queue_alloc(sizeof(struct scan_field));
201 jtag_queue_command(cmd);
203 cmd->type = JTAG_SCAN;
204 cmd->cmd.scan = scan;
206 scan->ir_scan = ir_scan;
207 scan->num_fields = 1;
208 scan->fields = out_fields;
209 scan->end_state = state;
211 out_fields->num_bits = num_bits;
212 out_fields->out_value = buf_cpy(out_bits, cmd_queue_alloc(DIV_ROUND_UP(num_bits, 8)), num_bits);
213 out_fields->in_value = in_bits;
215 return ERROR_OK;
218 int interface_jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
220 return jtag_add_plain_scan(num_bits, out_bits, in_bits, state, false);
223 int interface_jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
225 return jtag_add_plain_scan(num_bits, out_bits, in_bits, state, true);
228 int interface_jtag_add_tlr(void)
230 tap_state_t state = TAP_RESET;
232 /* allocate memory for a new list member */
233 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
235 jtag_queue_command(cmd);
237 cmd->type = JTAG_TLR_RESET;
239 cmd->cmd.statemove = cmd_queue_alloc(sizeof(struct statemove_command));
240 cmd->cmd.statemove->end_state = state;
242 return ERROR_OK;
245 int interface_add_tms_seq(unsigned num_bits, const uint8_t *seq, enum tap_state state)
247 struct jtag_command *cmd;
249 cmd = cmd_queue_alloc(sizeof(struct jtag_command));
250 if (cmd == NULL)
251 return ERROR_FAIL;
253 cmd->type = JTAG_TMS;
254 cmd->cmd.tms = cmd_queue_alloc(sizeof(*cmd->cmd.tms));
255 if (!cmd->cmd.tms)
256 return ERROR_FAIL;
258 /* copy the bits; our caller doesn't guarantee they'll persist */
259 cmd->cmd.tms->num_bits = num_bits;
260 cmd->cmd.tms->bits = buf_cpy(seq,
261 cmd_queue_alloc(DIV_ROUND_UP(num_bits, 8)), num_bits);
262 if (!cmd->cmd.tms->bits)
263 return ERROR_FAIL;
265 jtag_queue_command(cmd);
267 return ERROR_OK;
270 int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
272 /* allocate memory for a new list member */
273 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
275 jtag_queue_command(cmd);
277 cmd->type = JTAG_PATHMOVE;
279 cmd->cmd.pathmove = cmd_queue_alloc(sizeof(struct pathmove_command));
280 cmd->cmd.pathmove->num_states = num_states;
281 cmd->cmd.pathmove->path = cmd_queue_alloc(sizeof(tap_state_t) * num_states);
283 for (int i = 0; i < num_states; i++)
284 cmd->cmd.pathmove->path[i] = path[i];
286 return ERROR_OK;
289 int interface_jtag_add_runtest(int num_cycles, tap_state_t state)
291 /* allocate memory for a new list member */
292 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
294 jtag_queue_command(cmd);
296 cmd->type = JTAG_RUNTEST;
298 cmd->cmd.runtest = cmd_queue_alloc(sizeof(struct runtest_command));
299 cmd->cmd.runtest->num_cycles = num_cycles;
300 cmd->cmd.runtest->end_state = state;
302 return ERROR_OK;
305 int interface_jtag_add_clocks(int num_cycles)
307 /* allocate memory for a new list member */
308 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
310 jtag_queue_command(cmd);
312 cmd->type = JTAG_STABLECLOCKS;
314 cmd->cmd.stableclocks = cmd_queue_alloc(sizeof(struct stableclocks_command));
315 cmd->cmd.stableclocks->num_cycles = num_cycles;
317 return ERROR_OK;
320 int interface_jtag_add_reset(int req_trst, int req_srst)
322 /* allocate memory for a new list member */
323 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
325 jtag_queue_command(cmd);
327 cmd->type = JTAG_RESET;
329 cmd->cmd.reset = cmd_queue_alloc(sizeof(struct reset_command));
330 cmd->cmd.reset->trst = req_trst;
331 cmd->cmd.reset->srst = req_srst;
333 return ERROR_OK;
336 int interface_jtag_add_sleep(uint32_t us)
338 /* allocate memory for a new list member */
339 struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
341 jtag_queue_command(cmd);
343 cmd->type = JTAG_SLEEP;
345 cmd->cmd.sleep = cmd_queue_alloc(sizeof(struct sleep_command));
346 cmd->cmd.sleep->us = us;
348 return ERROR_OK;
351 /* add callback to end of queue */
352 void interface_jtag_add_callback4(jtag_callback_t callback,
353 jtag_callback_data_t data0, jtag_callback_data_t data1,
354 jtag_callback_data_t data2, jtag_callback_data_t data3)
356 struct jtag_callback_entry *entry = cmd_queue_alloc(sizeof(struct jtag_callback_entry));
358 entry->next = NULL;
359 entry->callback = callback;
360 entry->data0 = data0;
361 entry->data1 = data1;
362 entry->data2 = data2;
363 entry->data3 = data3;
365 if (jtag_callback_queue_head == NULL) {
366 jtag_callback_queue_head = entry;
367 jtag_callback_queue_tail = entry;
368 } else {
369 jtag_callback_queue_tail->next = entry;
370 jtag_callback_queue_tail = entry;
374 int interface_jtag_execute_queue(void)
376 static int reentry;
378 assert(reentry == 0);
379 reentry++;
381 int retval = default_interface_jtag_execute_queue();
382 if (retval == ERROR_OK) {
383 struct jtag_callback_entry *entry;
384 for (entry = jtag_callback_queue_head; entry != NULL; entry = entry->next) {
385 retval = entry->callback(entry->data0, entry->data1, entry->data2, entry->data3);
386 if (retval != ERROR_OK)
387 break;
391 jtag_command_queue_reset();
392 jtag_callback_queue_reset();
394 reentry--;
396 return retval;
399 static int jtag_convert_to_callback4(jtag_callback_data_t data0,
400 jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
402 ((jtag_callback1_t)data1)(data0);
403 return ERROR_OK;
406 void interface_jtag_add_callback(jtag_callback1_t callback, jtag_callback_data_t data0)
408 jtag_add_callback4(jtag_convert_to_callback4, data0, (jtag_callback_data_t)callback, 0, 0);
411 void jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0)
413 interface_jtag_add_callback(f, data0);
416 void jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0,
417 jtag_callback_data_t data1, jtag_callback_data_t data2,
418 jtag_callback_data_t data3)
420 interface_jtag_add_callback4(f, data0, data1, data2, data3);