Convert DEBUG_JTAG_IO to LOG_DEBUG_IO
[openocd.git] / src / jtag / drivers / bitbang.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
21
22 /* 2014-12: Addition of the SWD protocol support is based on the initial work
23 * by Paul Fertser and modifications by Jean-Christian de Rivaz. */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "bitbang.h"
30 #include <jtag/interface.h>
31 #include <jtag/commands.h>
32
33 /**
34 * Function bitbang_stableclocks
35 * issues a number of clock cycles while staying in a stable state.
36 * Because the TMS value required to stay in the RESET state is a 1, whereas
37 * the TMS value required to stay in any of the other stable states is a 0,
38 * this function checks the current stable state to decide on the value of TMS
39 * to use.
40 */
41 static int bitbang_stableclocks(int num_cycles);
42
43 static void bitbang_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk);
44
45 struct bitbang_interface *bitbang_interface;
46
47 /* DANGER!!!! clock absolutely *MUST* be 0 in idle or reset won't work!
48 *
49 * Set this to 1 and str912 reset halt will fail.
50 *
51 * If someone can submit a patch with an explanation it will be greatly
52 * appreciated, but as far as I can tell (ØH) DCLK is generated upon
53 * clk = 0 in TAP_IDLE. Good luck deducing that from the ARM documentation!
54 * The ARM documentation uses the term "DCLK is asserted while in the TAP_IDLE
55 * state". With hardware there is no such thing as *while* in a state. There
56 * are only edges. So clk => 0 is in fact a very subtle state transition that
57 * happens *while* in the TAP_IDLE state. "#&¤"#¤&"#&"#&
58 *
59 * For "reset halt" the last thing that happens before srst is asserted
60 * is that the breakpoint is set up. If DCLK is not wiggled one last
61 * time before the reset, then the breakpoint is not set up and
62 * "reset halt" will fail to halt.
63 *
64 */
65 #define CLOCK_IDLE() 0
66
67 /* The bitbang driver leaves the TCK 0 when in idle */
68 static void bitbang_end_state(tap_state_t state)
69 {
70 assert(tap_is_state_stable(state));
71 tap_set_end_state(state);
72 }
73
74 static int bitbang_state_move(int skip)
75 {
76 int i = 0, tms = 0;
77 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
78 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
79
80 for (i = skip; i < tms_count; i++) {
81 tms = (tms_scan >> i) & 1;
82 if (bitbang_interface->write(0, tms, 0) != ERROR_OK)
83 return ERROR_FAIL;
84 if (bitbang_interface->write(1, tms, 0) != ERROR_OK)
85 return ERROR_FAIL;
86 }
87 if (bitbang_interface->write(CLOCK_IDLE(), tms, 0) != ERROR_OK)
88 return ERROR_FAIL;
89
90 tap_set_state(tap_get_end_state());
91 return ERROR_OK;
92 }
93
94 /**
95 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
96 * (or SWD) state machine.
97 */
98 static int bitbang_execute_tms(struct jtag_command *cmd)
99 {
100 unsigned num_bits = cmd->cmd.tms->num_bits;
101 const uint8_t *bits = cmd->cmd.tms->bits;
102
103 LOG_DEBUG_IO("TMS: %d bits", num_bits);
104
105 int tms = 0;
106 for (unsigned i = 0; i < num_bits; i++) {
107 tms = ((bits[i/8] >> (i % 8)) & 1);
108 if (bitbang_interface->write(0, tms, 0) != ERROR_OK)
109 return ERROR_FAIL;
110 if (bitbang_interface->write(1, tms, 0) != ERROR_OK)
111 return ERROR_FAIL;
112 }
113 if (bitbang_interface->write(CLOCK_IDLE(), tms, 0) != ERROR_OK)
114 return ERROR_FAIL;
115
116 return ERROR_OK;
117 }
118
119 static int bitbang_path_move(struct pathmove_command *cmd)
120 {
121 int num_states = cmd->num_states;
122 int state_count;
123 int tms = 0;
124
125 state_count = 0;
126 while (num_states) {
127 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
128 tms = 0;
129 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
130 tms = 1;
131 else {
132 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
133 tap_state_name(tap_get_state()),
134 tap_state_name(cmd->path[state_count]));
135 exit(-1);
136 }
137
138 if (bitbang_interface->write(0, tms, 0) != ERROR_OK)
139 return ERROR_FAIL;
140 if (bitbang_interface->write(1, tms, 0) != ERROR_OK)
141 return ERROR_FAIL;
142
143 tap_set_state(cmd->path[state_count]);
144 state_count++;
145 num_states--;
146 }
147
148 if (bitbang_interface->write(CLOCK_IDLE(), tms, 0) != ERROR_OK)
149 return ERROR_FAIL;
150
151 tap_set_end_state(tap_get_state());
152 return ERROR_OK;
153 }
154
155 static int bitbang_runtest(int num_cycles)
156 {
157 int i;
158
159 tap_state_t saved_end_state = tap_get_end_state();
160
161 /* only do a state_move when we're not already in IDLE */
162 if (tap_get_state() != TAP_IDLE) {
163 bitbang_end_state(TAP_IDLE);
164 if (bitbang_state_move(0) != ERROR_OK)
165 return ERROR_FAIL;
166 }
167
168 /* execute num_cycles */
169 for (i = 0; i < num_cycles; i++) {
170 if (bitbang_interface->write(0, 0, 0) != ERROR_OK)
171 return ERROR_FAIL;
172 if (bitbang_interface->write(1, 0, 0) != ERROR_OK)
173 return ERROR_FAIL;
174 }
175 if (bitbang_interface->write(CLOCK_IDLE(), 0, 0) != ERROR_OK)
176 return ERROR_FAIL;
177
178 /* finish in end_state */
179 bitbang_end_state(saved_end_state);
180 if (tap_get_state() != tap_get_end_state())
181 if (bitbang_state_move(0) != ERROR_OK)
182 return ERROR_FAIL;
183
184 return ERROR_OK;
185 }
186
187 static int bitbang_stableclocks(int num_cycles)
188 {
189 int tms = (tap_get_state() == TAP_RESET ? 1 : 0);
190 int i;
191
192 /* send num_cycles clocks onto the cable */
193 for (i = 0; i < num_cycles; i++) {
194 if (bitbang_interface->write(1, tms, 0) != ERROR_OK)
195 return ERROR_FAIL;
196 if (bitbang_interface->write(0, tms, 0) != ERROR_OK)
197 return ERROR_FAIL;
198 }
199
200 return ERROR_OK;
201 }
202
203 static int bitbang_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
204 unsigned scan_size)
205 {
206 tap_state_t saved_end_state = tap_get_end_state();
207 unsigned bit_cnt;
208
209 if (!((!ir_scan &&
210 (tap_get_state() == TAP_DRSHIFT)) ||
211 (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) {
212 if (ir_scan)
213 bitbang_end_state(TAP_IRSHIFT);
214 else
215 bitbang_end_state(TAP_DRSHIFT);
216
217 if (bitbang_state_move(0) != ERROR_OK)
218 return ERROR_FAIL;
219 bitbang_end_state(saved_end_state);
220 }
221
222 size_t buffered = 0;
223 for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++) {
224 int tms = (bit_cnt == scan_size-1) ? 1 : 0;
225 int tdi;
226 int bytec = bit_cnt/8;
227 int bcval = 1 << (bit_cnt % 8);
228
229 /* if we're just reading the scan, but don't care about the output
230 * default to outputting 'low', this also makes valgrind traces more readable,
231 * as it removes the dependency on an uninitialised value
232 */
233 tdi = 0;
234 if ((type != SCAN_IN) && (buffer[bytec] & bcval))
235 tdi = 1;
236
237 if (bitbang_interface->write(0, tms, tdi) != ERROR_OK)
238 return ERROR_FAIL;
239
240 if (type != SCAN_OUT) {
241 if (bitbang_interface->buf_size) {
242 if (bitbang_interface->sample() != ERROR_OK)
243 return ERROR_FAIL;
244 buffered++;
245 } else {
246 switch (bitbang_interface->read()) {
247 case BB_LOW:
248 buffer[bytec] &= ~bcval;
249 break;
250 case BB_HIGH:
251 buffer[bytec] |= bcval;
252 break;
253 default:
254 return ERROR_FAIL;
255 }
256 }
257 }
258
259 if (bitbang_interface->write(1, tms, tdi) != ERROR_OK)
260 return ERROR_FAIL;
261
262 if (type != SCAN_OUT && bitbang_interface->buf_size &&
263 (buffered == bitbang_interface->buf_size ||
264 bit_cnt == scan_size - 1)) {
265 for (unsigned i = bit_cnt + 1 - buffered; i <= bit_cnt; i++) {
266 switch (bitbang_interface->read_sample()) {
267 case BB_LOW:
268 buffer[i/8] &= ~(1 << (i % 8));
269 break;
270 case BB_HIGH:
271 buffer[i/8] |= 1 << (i % 8);
272 break;
273 default:
274 return ERROR_FAIL;
275 }
276 }
277 buffered = 0;
278 }
279 }
280
281 if (tap_get_state() != tap_get_end_state()) {
282 /* we *KNOW* the above loop transitioned out of
283 * the shift state, so we skip the first state
284 * and move directly to the end state.
285 */
286 if (bitbang_state_move(1) != ERROR_OK)
287 return ERROR_FAIL;
288 }
289 return ERROR_OK;
290 }
291
292 int bitbang_execute_queue(void)
293 {
294 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
295 int scan_size;
296 enum scan_type type;
297 uint8_t *buffer;
298 int retval;
299
300 if (!bitbang_interface) {
301 LOG_ERROR("BUG: Bitbang interface called, but not yet initialized");
302 exit(-1);
303 }
304
305 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
306 * that wasn't handled by a caller-provided error handler
307 */
308 retval = ERROR_OK;
309
310 if (bitbang_interface->blink) {
311 if (bitbang_interface->blink(1) != ERROR_OK)
312 return ERROR_FAIL;
313 }
314
315 while (cmd) {
316 switch (cmd->type) {
317 case JTAG_RESET:
318 LOG_DEBUG_IO("reset trst: %i srst %i",
319 cmd->cmd.reset->trst,
320 cmd->cmd.reset->srst);
321 if ((cmd->cmd.reset->trst == 1) ||
322 (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
323 tap_set_state(TAP_RESET);
324 if (bitbang_interface->reset(cmd->cmd.reset->trst,
325 cmd->cmd.reset->srst) != ERROR_OK)
326 return ERROR_FAIL;
327 break;
328 case JTAG_RUNTEST:
329 LOG_DEBUG_IO("runtest %i cycles, end in %s",
330 cmd->cmd.runtest->num_cycles,
331 tap_state_name(cmd->cmd.runtest->end_state));
332 bitbang_end_state(cmd->cmd.runtest->end_state);
333 if (bitbang_runtest(cmd->cmd.runtest->num_cycles) != ERROR_OK)
334 return ERROR_FAIL;
335 break;
336
337 case JTAG_STABLECLOCKS:
338 /* this is only allowed while in a stable state. A check for a stable
339 * state was done in jtag_add_clocks()
340 */
341 if (bitbang_stableclocks(cmd->cmd.stableclocks->num_cycles) != ERROR_OK)
342 return ERROR_FAIL;
343 break;
344
345 case JTAG_TLR_RESET:
346 LOG_DEBUG_IO("statemove end in %s",
347 tap_state_name(cmd->cmd.statemove->end_state));
348 bitbang_end_state(cmd->cmd.statemove->end_state);
349 if (bitbang_state_move(0) != ERROR_OK)
350 return ERROR_FAIL;
351 break;
352 case JTAG_PATHMOVE:
353 LOG_DEBUG_IO("pathmove: %i states, end in %s",
354 cmd->cmd.pathmove->num_states,
355 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
356 if (bitbang_path_move(cmd->cmd.pathmove) != ERROR_OK)
357 return ERROR_FAIL;
358 break;
359 case JTAG_SCAN:
360 bitbang_end_state(cmd->cmd.scan->end_state);
361 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
362 LOG_DEBUG_IO("%s scan %d bits; end in %s",
363 (cmd->cmd.scan->ir_scan) ? "IR" : "DR",
364 scan_size,
365 tap_state_name(cmd->cmd.scan->end_state));
366 type = jtag_scan_type(cmd->cmd.scan);
367 if (bitbang_scan(cmd->cmd.scan->ir_scan, type, buffer,
368 scan_size) != ERROR_OK)
369 return ERROR_FAIL;
370 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
371 retval = ERROR_JTAG_QUEUE_FAILED;
372 if (buffer)
373 free(buffer);
374 break;
375 case JTAG_SLEEP:
376 LOG_DEBUG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
377 jtag_sleep(cmd->cmd.sleep->us);
378 break;
379 case JTAG_TMS:
380 retval = bitbang_execute_tms(cmd);
381 break;
382 default:
383 LOG_ERROR("BUG: unknown JTAG command type encountered");
384 exit(-1);
385 }
386 cmd = cmd->next;
387 }
388 if (bitbang_interface->blink) {
389 if (bitbang_interface->blink(0) != ERROR_OK)
390 return ERROR_FAIL;
391 }
392
393 return retval;
394 }
395
396
397 bool swd_mode;
398 static int queued_retval;
399
400 static int bitbang_swd_init(void)
401 {
402 LOG_DEBUG("bitbang_swd_init");
403 swd_mode = true;
404 return ERROR_OK;
405 }
406
407 static void bitbang_exchange(bool rnw, uint8_t buf[], unsigned int offset, unsigned int bit_cnt)
408 {
409 LOG_DEBUG("bitbang_exchange");
410 int tdi;
411
412 for (unsigned int i = offset; i < bit_cnt + offset; i++) {
413 int bytec = i/8;
414 int bcval = 1 << (i % 8);
415 tdi = !rnw && (buf[bytec] & bcval);
416
417 bitbang_interface->write(0, 0, tdi);
418
419 if (rnw && buf) {
420 if (bitbang_interface->swdio_read())
421 buf[bytec] |= bcval;
422 else
423 buf[bytec] &= ~bcval;
424 }
425
426 bitbang_interface->write(1, 0, tdi);
427 }
428 }
429
430 int bitbang_swd_switch_seq(enum swd_special_seq seq)
431 {
432 LOG_DEBUG("bitbang_swd_switch_seq");
433
434 switch (seq) {
435 case LINE_RESET:
436 LOG_DEBUG("SWD line reset");
437 bitbang_exchange(false, (uint8_t *)swd_seq_line_reset, 0, swd_seq_line_reset_len);
438 break;
439 case JTAG_TO_SWD:
440 LOG_DEBUG("JTAG-to-SWD");
441 bitbang_exchange(false, (uint8_t *)swd_seq_jtag_to_swd, 0, swd_seq_jtag_to_swd_len);
442 break;
443 case SWD_TO_JTAG:
444 LOG_DEBUG("SWD-to-JTAG");
445 bitbang_exchange(false, (uint8_t *)swd_seq_swd_to_jtag, 0, swd_seq_swd_to_jtag_len);
446 break;
447 default:
448 LOG_ERROR("Sequence %d not supported", seq);
449 return ERROR_FAIL;
450 }
451
452 return ERROR_OK;
453 }
454
455 void bitbang_switch_to_swd(void)
456 {
457 LOG_DEBUG("bitbang_switch_to_swd");
458 bitbang_exchange(false, (uint8_t *)swd_seq_jtag_to_swd, 0, swd_seq_jtag_to_swd_len);
459 }
460
461 static void swd_clear_sticky_errors(void)
462 {
463 bitbang_swd_write_reg(swd_cmd(false, false, DP_ABORT),
464 STKCMPCLR | STKERRCLR | WDERRCLR | ORUNERRCLR, 0);
465 }
466
467 static void bitbang_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
468 {
469 LOG_DEBUG("bitbang_swd_read_reg");
470 assert(cmd & SWD_CMD_RnW);
471
472 if (queued_retval != ERROR_OK) {
473 LOG_DEBUG("Skip bitbang_swd_read_reg because queued_retval=%d", queued_retval);
474 return;
475 }
476
477 for (;;) {
478 uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)];
479
480 cmd |= SWD_CMD_START | (1 << 7);
481 bitbang_exchange(false, &cmd, 0, 8);
482
483 bitbang_interface->swdio_drive(false);
484 bitbang_exchange(true, trn_ack_data_parity_trn, 0, 1 + 3 + 32 + 1 + 1);
485 bitbang_interface->swdio_drive(true);
486
487 int ack = buf_get_u32(trn_ack_data_parity_trn, 1, 3);
488 uint32_t data = buf_get_u32(trn_ack_data_parity_trn, 1 + 3, 32);
489 int parity = buf_get_u32(trn_ack_data_parity_trn, 1 + 3 + 32, 1);
490
491 LOG_DEBUG("%s %s %s reg %X = %08"PRIx32,
492 ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
493 cmd & SWD_CMD_APnDP ? "AP" : "DP",
494 cmd & SWD_CMD_RnW ? "read" : "write",
495 (cmd & SWD_CMD_A32) >> 1,
496 data);
497
498 switch (ack) {
499 case SWD_ACK_OK:
500 if (parity != parity_u32(data)) {
501 LOG_DEBUG("Wrong parity detected");
502 queued_retval = ERROR_FAIL;
503 return;
504 }
505 if (value)
506 *value = data;
507 if (cmd & SWD_CMD_APnDP)
508 bitbang_exchange(true, NULL, 0, ap_delay_clk);
509 return;
510 case SWD_ACK_WAIT:
511 LOG_DEBUG("SWD_ACK_WAIT");
512 swd_clear_sticky_errors();
513 break;
514 case SWD_ACK_FAULT:
515 LOG_DEBUG("SWD_ACK_FAULT");
516 queued_retval = ack;
517 return;
518 default:
519 LOG_DEBUG("No valid acknowledge: ack=%d", ack);
520 queued_retval = ack;
521 return;
522 }
523 }
524 }
525
526 static void bitbang_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
527 {
528 LOG_DEBUG("bitbang_swd_write_reg");
529 assert(!(cmd & SWD_CMD_RnW));
530
531 if (queued_retval != ERROR_OK) {
532 LOG_DEBUG("Skip bitbang_swd_write_reg because queued_retval=%d", queued_retval);
533 return;
534 }
535
536 for (;;) {
537 uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)];
538 buf_set_u32(trn_ack_data_parity_trn, 1 + 3 + 1, 32, value);
539 buf_set_u32(trn_ack_data_parity_trn, 1 + 3 + 1 + 32, 1, parity_u32(value));
540
541 cmd |= SWD_CMD_START | (1 << 7);
542 bitbang_exchange(false, &cmd, 0, 8);
543
544 bitbang_interface->swdio_drive(false);
545 bitbang_exchange(true, trn_ack_data_parity_trn, 0, 1 + 3 + 1);
546 bitbang_interface->swdio_drive(true);
547 bitbang_exchange(false, trn_ack_data_parity_trn, 1 + 3 + 1, 32 + 1);
548
549 int ack = buf_get_u32(trn_ack_data_parity_trn, 1, 3);
550 LOG_DEBUG("%s %s %s reg %X = %08"PRIx32,
551 ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
552 cmd & SWD_CMD_APnDP ? "AP" : "DP",
553 cmd & SWD_CMD_RnW ? "read" : "write",
554 (cmd & SWD_CMD_A32) >> 1,
555 buf_get_u32(trn_ack_data_parity_trn, 1 + 3 + 1, 32));
556
557 switch (ack) {
558 case SWD_ACK_OK:
559 if (cmd & SWD_CMD_APnDP)
560 bitbang_exchange(true, NULL, 0, ap_delay_clk);
561 return;
562 case SWD_ACK_WAIT:
563 LOG_DEBUG("SWD_ACK_WAIT");
564 swd_clear_sticky_errors();
565 break;
566 case SWD_ACK_FAULT:
567 LOG_DEBUG("SWD_ACK_FAULT");
568 queued_retval = ack;
569 return;
570 default:
571 LOG_DEBUG("No valid acknowledge: ack=%d", ack);
572 queued_retval = ack;
573 return;
574 }
575 }
576 }
577
578 static int bitbang_swd_run_queue(void)
579 {
580 LOG_DEBUG("bitbang_swd_run_queue");
581 /* A transaction must be followed by another transaction or at least 8 idle cycles to
582 * ensure that data is clocked through the AP. */
583 bitbang_exchange(true, NULL, 0, 8);
584
585 int retval = queued_retval;
586 queued_retval = ERROR_OK;
587 LOG_DEBUG("SWD queue return value: %02x", retval);
588 return retval;
589 }
590
591 const struct swd_driver bitbang_swd = {
592 .init = bitbang_swd_init,
593 .switch_seq = bitbang_swd_switch_seq,
594 .read_reg = bitbang_swd_read_reg,
595 .write_reg = bitbang_swd_write_reg,
596 .run = bitbang_swd_run_queue,
597 };