You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

549 lines
16 KiB

  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. /* 2014-12: Addition of the SWD protocol support is based on the initial work
  22. * by Paul Fertser and modifications by Jean-Christian de Rivaz. */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "bitbang.h"
  27. #include <jtag/interface.h>
  28. #include <jtag/commands.h>
  29. /* YUK! - but this is currently a global.... */
  30. extern struct jtag_interface *jtag_interface;
  31. /**
  32. * Function bitbang_stableclocks
  33. * issues a number of clock cycles while staying in a stable state.
  34. * Because the TMS value required to stay in the RESET state is a 1, whereas
  35. * the TMS value required to stay in any of the other stable states is a 0,
  36. * this function checks the current stable state to decide on the value of TMS
  37. * to use.
  38. */
  39. static void bitbang_stableclocks(int num_cycles);
  40. static void bitbang_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk);
  41. struct bitbang_interface *bitbang_interface;
  42. /* DANGER!!!! clock absolutely *MUST* be 0 in idle or reset won't work!
  43. *
  44. * Set this to 1 and str912 reset halt will fail.
  45. *
  46. * If someone can submit a patch with an explanation it will be greatly
  47. * appreciated, but as far as I can tell (ØH) DCLK is generated upon
  48. * clk = 0 in TAP_IDLE. Good luck deducing that from the ARM documentation!
  49. * The ARM documentation uses the term "DCLK is asserted while in the TAP_IDLE
  50. * state". With hardware there is no such thing as *while* in a state. There
  51. * are only edges. So clk => 0 is in fact a very subtle state transition that
  52. * happens *while* in the TAP_IDLE state. "#&¤"#¤&"#&"#&
  53. *
  54. * For "reset halt" the last thing that happens before srst is asserted
  55. * is that the breakpoint is set up. If DCLK is not wiggled one last
  56. * time before the reset, then the breakpoint is not set up and
  57. * "reset halt" will fail to halt.
  58. *
  59. */
  60. #define CLOCK_IDLE() 0
  61. /* The bitbang driver leaves the TCK 0 when in idle */
  62. static void bitbang_end_state(tap_state_t state)
  63. {
  64. if (tap_is_state_stable(state))
  65. tap_set_end_state(state);
  66. else {
  67. LOG_ERROR("BUG: %i is not a valid end state", state);
  68. exit(-1);
  69. }
  70. }
  71. static void bitbang_state_move(int skip)
  72. {
  73. int i = 0, tms = 0;
  74. uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  75. int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  76. for (i = skip; i < tms_count; i++) {
  77. tms = (tms_scan >> i) & 1;
  78. bitbang_interface->write(0, tms, 0);
  79. bitbang_interface->write(1, tms, 0);
  80. }
  81. bitbang_interface->write(CLOCK_IDLE(), tms, 0);
  82. tap_set_state(tap_get_end_state());
  83. }
  84. /**
  85. * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
  86. * (or SWD) state machine.
  87. */
  88. static int bitbang_execute_tms(struct jtag_command *cmd)
  89. {
  90. unsigned num_bits = cmd->cmd.tms->num_bits;
  91. const uint8_t *bits = cmd->cmd.tms->bits;
  92. DEBUG_JTAG_IO("TMS: %d bits", num_bits);
  93. int tms = 0;
  94. for (unsigned i = 0; i < num_bits; i++) {
  95. tms = ((bits[i/8] >> (i % 8)) & 1);
  96. bitbang_interface->write(0, tms, 0);
  97. bitbang_interface->write(1, tms, 0);
  98. }
  99. bitbang_interface->write(CLOCK_IDLE(), tms, 0);
  100. return ERROR_OK;
  101. }
  102. static void bitbang_path_move(struct pathmove_command *cmd)
  103. {
  104. int num_states = cmd->num_states;
  105. int state_count;
  106. int tms = 0;
  107. state_count = 0;
  108. while (num_states) {
  109. if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
  110. tms = 0;
  111. else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
  112. tms = 1;
  113. else {
  114. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
  115. tap_state_name(tap_get_state()),
  116. tap_state_name(cmd->path[state_count]));
  117. exit(-1);
  118. }
  119. bitbang_interface->write(0, tms, 0);
  120. bitbang_interface->write(1, tms, 0);
  121. tap_set_state(cmd->path[state_count]);
  122. state_count++;
  123. num_states--;
  124. }
  125. bitbang_interface->write(CLOCK_IDLE(), tms, 0);
  126. tap_set_end_state(tap_get_state());
  127. }
  128. static void bitbang_runtest(int num_cycles)
  129. {
  130. int i;
  131. tap_state_t saved_end_state = tap_get_end_state();
  132. /* only do a state_move when we're not already in IDLE */
  133. if (tap_get_state() != TAP_IDLE) {
  134. bitbang_end_state(TAP_IDLE);
  135. bitbang_state_move(0);
  136. }
  137. /* execute num_cycles */
  138. for (i = 0; i < num_cycles; i++) {
  139. bitbang_interface->write(0, 0, 0);
  140. bitbang_interface->write(1, 0, 0);
  141. }
  142. bitbang_interface->write(CLOCK_IDLE(), 0, 0);
  143. /* finish in end_state */
  144. bitbang_end_state(saved_end_state);
  145. if (tap_get_state() != tap_get_end_state())
  146. bitbang_state_move(0);
  147. }
  148. static void bitbang_stableclocks(int num_cycles)
  149. {
  150. int tms = (tap_get_state() == TAP_RESET ? 1 : 0);
  151. int i;
  152. /* send num_cycles clocks onto the cable */
  153. for (i = 0; i < num_cycles; i++) {
  154. bitbang_interface->write(1, tms, 0);
  155. bitbang_interface->write(0, tms, 0);
  156. }
  157. }
  158. static void bitbang_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
  159. {
  160. tap_state_t saved_end_state = tap_get_end_state();
  161. int bit_cnt;
  162. if (!((!ir_scan &&
  163. (tap_get_state() == TAP_DRSHIFT)) ||
  164. (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) {
  165. if (ir_scan)
  166. bitbang_end_state(TAP_IRSHIFT);
  167. else
  168. bitbang_end_state(TAP_DRSHIFT);
  169. bitbang_state_move(0);
  170. bitbang_end_state(saved_end_state);
  171. }
  172. for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++) {
  173. int val = 0;
  174. int tms = (bit_cnt == scan_size-1) ? 1 : 0;
  175. int tdi;
  176. int bytec = bit_cnt/8;
  177. int bcval = 1 << (bit_cnt % 8);
  178. /* if we're just reading the scan, but don't care about the output
  179. * default to outputting 'low', this also makes valgrind traces more readable,
  180. * as it removes the dependency on an uninitialised value
  181. */
  182. tdi = 0;
  183. if ((type != SCAN_IN) && (buffer[bytec] & bcval))
  184. tdi = 1;
  185. bitbang_interface->write(0, tms, tdi);
  186. if (type != SCAN_OUT)
  187. val = bitbang_interface->read();
  188. bitbang_interface->write(1, tms, tdi);
  189. if (type != SCAN_OUT) {
  190. if (val)
  191. buffer[bytec] |= bcval;
  192. else
  193. buffer[bytec] &= ~bcval;
  194. }
  195. }
  196. if (tap_get_state() != tap_get_end_state()) {
  197. /* we *KNOW* the above loop transitioned out of
  198. * the shift state, so we skip the first state
  199. * and move directly to the end state.
  200. */
  201. bitbang_state_move(1);
  202. }
  203. }
  204. int bitbang_execute_queue(void)
  205. {
  206. struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
  207. int scan_size;
  208. enum scan_type type;
  209. uint8_t *buffer;
  210. int retval;
  211. if (!bitbang_interface) {
  212. LOG_ERROR("BUG: Bitbang interface called, but not yet initialized");
  213. exit(-1);
  214. }
  215. /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
  216. * that wasn't handled by a caller-provided error handler
  217. */
  218. retval = ERROR_OK;
  219. if (bitbang_interface->blink)
  220. bitbang_interface->blink(1);
  221. while (cmd) {
  222. switch (cmd->type) {
  223. case JTAG_RESET:
  224. #ifdef _DEBUG_JTAG_IO_
  225. LOG_DEBUG("reset trst: %i srst %i",
  226. cmd->cmd.reset->trst,
  227. cmd->cmd.reset->srst);
  228. #endif
  229. if ((cmd->cmd.reset->trst == 1) ||
  230. (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
  231. tap_set_state(TAP_RESET);
  232. bitbang_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  233. break;
  234. case JTAG_RUNTEST:
  235. #ifdef _DEBUG_JTAG_IO_
  236. LOG_DEBUG("runtest %i cycles, end in %s",
  237. cmd->cmd.runtest->num_cycles,
  238. tap_state_name(cmd->cmd.runtest->end_state));
  239. #endif
  240. bitbang_end_state(cmd->cmd.runtest->end_state);
  241. bitbang_runtest(cmd->cmd.runtest->num_cycles);
  242. break;
  243. case JTAG_STABLECLOCKS:
  244. /* this is only allowed while in a stable state. A check for a stable
  245. * state was done in jtag_add_clocks()
  246. */
  247. bitbang_stableclocks(cmd->cmd.stableclocks->num_cycles);
  248. break;
  249. case JTAG_TLR_RESET:
  250. #ifdef _DEBUG_JTAG_IO_
  251. LOG_DEBUG("statemove end in %s",
  252. tap_state_name(cmd->cmd.statemove->end_state));
  253. #endif
  254. bitbang_end_state(cmd->cmd.statemove->end_state);
  255. bitbang_state_move(0);
  256. break;
  257. case JTAG_PATHMOVE:
  258. #ifdef _DEBUG_JTAG_IO_
  259. LOG_DEBUG("pathmove: %i states, end in %s",
  260. cmd->cmd.pathmove->num_states,
  261. tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
  262. #endif
  263. bitbang_path_move(cmd->cmd.pathmove);
  264. break;
  265. case JTAG_SCAN:
  266. #ifdef _DEBUG_JTAG_IO_
  267. LOG_DEBUG("%s scan end in %s",
  268. (cmd->cmd.scan->ir_scan) ? "IR" : "DR",
  269. tap_state_name(cmd->cmd.scan->end_state));
  270. #endif
  271. bitbang_end_state(cmd->cmd.scan->end_state);
  272. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  273. type = jtag_scan_type(cmd->cmd.scan);
  274. bitbang_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  275. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  276. retval = ERROR_JTAG_QUEUE_FAILED;
  277. if (buffer)
  278. free(buffer);
  279. break;
  280. case JTAG_SLEEP:
  281. #ifdef _DEBUG_JTAG_IO_
  282. LOG_DEBUG("sleep %" PRIi32, cmd->cmd.sleep->us);
  283. #endif
  284. jtag_sleep(cmd->cmd.sleep->us);
  285. break;
  286. case JTAG_TMS:
  287. retval = bitbang_execute_tms(cmd);
  288. break;
  289. default:
  290. LOG_ERROR("BUG: unknown JTAG command type encountered");
  291. exit(-1);
  292. }
  293. cmd = cmd->next;
  294. }
  295. if (bitbang_interface->blink)
  296. bitbang_interface->blink(0);
  297. return retval;
  298. }
  299. bool swd_mode;
  300. static int queued_retval;
  301. static int bitbang_swd_init(void)
  302. {
  303. LOG_DEBUG("bitbang_swd_init");
  304. swd_mode = true;
  305. return ERROR_OK;
  306. }
  307. static void bitbang_exchange(bool rnw, uint8_t buf[], unsigned int offset, unsigned int bit_cnt)
  308. {
  309. LOG_DEBUG("bitbang_exchange");
  310. int tdi;
  311. for (unsigned int i = offset; i < bit_cnt + offset; i++) {
  312. int bytec = i/8;
  313. int bcval = 1 << (i % 8);
  314. tdi = !rnw && (buf[bytec] & bcval);
  315. bitbang_interface->write(0, 0, tdi);
  316. if (rnw && buf) {
  317. if (bitbang_interface->swdio_read())
  318. buf[bytec] |= bcval;
  319. else
  320. buf[bytec] &= ~bcval;
  321. }
  322. bitbang_interface->write(1, 0, tdi);
  323. }
  324. }
  325. int bitbang_swd_switch_seq(enum swd_special_seq seq)
  326. {
  327. LOG_DEBUG("bitbang_swd_switch_seq");
  328. switch (seq) {
  329. case LINE_RESET:
  330. LOG_DEBUG("SWD line reset");
  331. bitbang_exchange(false, (uint8_t *)swd_seq_line_reset, 0, swd_seq_line_reset_len);
  332. break;
  333. case JTAG_TO_SWD:
  334. LOG_DEBUG("JTAG-to-SWD");
  335. bitbang_exchange(false, (uint8_t *)swd_seq_jtag_to_swd, 0, swd_seq_jtag_to_swd_len);
  336. break;
  337. case SWD_TO_JTAG:
  338. LOG_DEBUG("SWD-to-JTAG");
  339. bitbang_exchange(false, (uint8_t *)swd_seq_swd_to_jtag, 0, swd_seq_swd_to_jtag_len);
  340. break;
  341. default:
  342. LOG_ERROR("Sequence %d not supported", seq);
  343. return ERROR_FAIL;
  344. }
  345. return ERROR_OK;
  346. }
  347. void bitbang_switch_to_swd(void)
  348. {
  349. LOG_DEBUG("bitbang_switch_to_swd");
  350. bitbang_exchange(false, (uint8_t *)swd_seq_jtag_to_swd, 0, swd_seq_jtag_to_swd_len);
  351. }
  352. static void swd_clear_sticky_errors(void)
  353. {
  354. bitbang_swd_write_reg(swd_cmd(false, false, DP_ABORT),
  355. STKCMPCLR | STKERRCLR | WDERRCLR | ORUNERRCLR, 0);
  356. }
  357. static void bitbang_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
  358. {
  359. LOG_DEBUG("bitbang_swd_read_reg");
  360. assert(cmd & SWD_CMD_RnW);
  361. if (queued_retval != ERROR_OK) {
  362. LOG_DEBUG("Skip bitbang_swd_read_reg because queued_retval=%d", queued_retval);
  363. return;
  364. }
  365. for (;;) {
  366. uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)];
  367. cmd |= SWD_CMD_START | (1 << 7);
  368. bitbang_exchange(false, &cmd, 0, 8);
  369. bitbang_interface->swdio_drive(false);
  370. bitbang_exchange(true, trn_ack_data_parity_trn, 0, 1 + 3 + 32 + 1 + 1);
  371. bitbang_interface->swdio_drive(true);
  372. int ack = buf_get_u32(trn_ack_data_parity_trn, 1, 3);
  373. uint32_t data = buf_get_u32(trn_ack_data_parity_trn, 1 + 3, 32);
  374. int parity = buf_get_u32(trn_ack_data_parity_trn, 1 + 3 + 32, 1);
  375. LOG_DEBUG("%s %s %s reg %X = %08"PRIx32,
  376. ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
  377. cmd & SWD_CMD_APnDP ? "AP" : "DP",
  378. cmd & SWD_CMD_RnW ? "read" : "write",
  379. (cmd & SWD_CMD_A32) >> 1,
  380. data);
  381. switch (ack) {
  382. case SWD_ACK_OK:
  383. if (parity != parity_u32(data)) {
  384. LOG_DEBUG("Wrong parity detected");
  385. queued_retval = ERROR_FAIL;
  386. return;
  387. }
  388. if (value)
  389. *value = data;
  390. if (cmd & SWD_CMD_APnDP)
  391. bitbang_exchange(true, NULL, 0, ap_delay_clk);
  392. return;
  393. case SWD_ACK_WAIT:
  394. LOG_DEBUG("SWD_ACK_WAIT");
  395. swd_clear_sticky_errors();
  396. break;
  397. case SWD_ACK_FAULT:
  398. LOG_DEBUG("SWD_ACK_FAULT");
  399. queued_retval = ack;
  400. return;
  401. default:
  402. LOG_DEBUG("No valid acknowledge: ack=%d", ack);
  403. queued_retval = ack;
  404. return;
  405. }
  406. }
  407. }
  408. static void bitbang_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
  409. {
  410. LOG_DEBUG("bitbang_swd_write_reg");
  411. assert(!(cmd & SWD_CMD_RnW));
  412. if (queued_retval != ERROR_OK) {
  413. LOG_DEBUG("Skip bitbang_swd_write_reg because queued_retval=%d", queued_retval);
  414. return;
  415. }
  416. for (;;) {
  417. uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)];
  418. buf_set_u32(trn_ack_data_parity_trn, 1 + 3 + 1, 32, value);
  419. buf_set_u32(trn_ack_data_parity_trn, 1 + 3 + 1 + 32, 1, parity_u32(value));
  420. cmd |= SWD_CMD_START | (1 << 7);
  421. bitbang_exchange(false, &cmd, 0, 8);
  422. bitbang_interface->swdio_drive(false);
  423. bitbang_exchange(true, trn_ack_data_parity_trn, 0, 1 + 3 + 1);
  424. bitbang_interface->swdio_drive(true);
  425. bitbang_exchange(false, trn_ack_data_parity_trn, 1 + 3 + 1, 32 + 1);
  426. int ack = buf_get_u32(trn_ack_data_parity_trn, 1, 3);
  427. LOG_DEBUG("%s %s %s reg %X = %08"PRIx32,
  428. ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
  429. cmd & SWD_CMD_APnDP ? "AP" : "DP",
  430. cmd & SWD_CMD_RnW ? "read" : "write",
  431. (cmd & SWD_CMD_A32) >> 1,
  432. buf_get_u32(trn_ack_data_parity_trn, 1 + 3 + 1, 32));
  433. switch (ack) {
  434. case SWD_ACK_OK:
  435. if (cmd & SWD_CMD_APnDP)
  436. bitbang_exchange(true, NULL, 0, ap_delay_clk);
  437. return;
  438. case SWD_ACK_WAIT:
  439. LOG_DEBUG("SWD_ACK_WAIT");
  440. swd_clear_sticky_errors();
  441. break;
  442. case SWD_ACK_FAULT:
  443. LOG_DEBUG("SWD_ACK_FAULT");
  444. queued_retval = ack;
  445. return;
  446. default:
  447. LOG_DEBUG("No valid acknowledge: ack=%d", ack);
  448. queued_retval = ack;
  449. return;
  450. }
  451. }
  452. }
  453. static int bitbang_swd_run_queue(void)
  454. {
  455. LOG_DEBUG("bitbang_swd_run_queue");
  456. /* A transaction must be followed by another transaction or at least 8 idle cycles to
  457. * ensure that data is clocked through the AP. */
  458. bitbang_exchange(true, NULL, 0, 8);
  459. int retval = queued_retval;
  460. queued_retval = ERROR_OK;
  461. LOG_DEBUG("SWD queue return value: %02x", retval);
  462. return retval;
  463. }
  464. const struct swd_driver bitbang_swd = {
  465. .init = bitbang_swd_init,
  466. .switch_seq = bitbang_swd_switch_seq,
  467. .read_reg = bitbang_swd_read_reg,
  468. .write_reg = bitbang_swd_write_reg,
  469. .run = bitbang_swd_run_queue,
  470. };