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.
 
 
 
 
 
 

530 lines
15 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2018 by Square, Inc. *
  3. * Steven Stallion <stallion@squareup.com> *
  4. * James Zhao <hjz@squareup.com> *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <helper/binarybuffer.h>
  23. #include <helper/log.h>
  24. #include <helper/types.h>
  25. #include <jtag/jtag.h>
  26. #include <jtag/commands.h>
  27. #include <jtag/interface.h>
  28. #include "esirisc_jtag.h"
  29. static void esirisc_jtag_set_instr(struct esirisc_jtag *jtag_info, uint32_t new_instr)
  30. {
  31. struct jtag_tap *tap = jtag_info->tap;
  32. if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr) {
  33. struct scan_field field;
  34. uint8_t t[4] = { 0 };
  35. field.num_bits = tap->ir_length;
  36. field.out_value = t;
  37. buf_set_u32(t, 0, field.num_bits, new_instr);
  38. field.in_value = NULL;
  39. jtag_add_ir_scan(tap, &field, TAP_IDLE);
  40. }
  41. }
  42. /*
  43. * The data register is latched every 8 bits while in the Shift-DR state
  44. * (Update-DR is not supported). This necessitates prepending padding
  45. * bits to ensure data is aligned when multiple TAPs are present.
  46. */
  47. static int esirisc_jtag_get_padding(void)
  48. {
  49. int padding = 0;
  50. int bypass_devices = 0;
  51. for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap;
  52. tap = jtag_tap_next_enabled(tap))
  53. if (tap->bypass)
  54. bypass_devices++;
  55. int num_bits = bypass_devices % 8;
  56. if (num_bits > 0)
  57. padding = 8 - num_bits;
  58. return padding;
  59. }
  60. static int esirisc_jtag_count_bits(int num_fields, struct scan_field *fields)
  61. {
  62. int bit_count = 0;
  63. for (int i = 0; i < num_fields; ++i)
  64. bit_count += fields[i].num_bits;
  65. return bit_count;
  66. }
  67. /*
  68. * Data received from the target will be byte-stuffed if it contains
  69. * either the pad byte (0xAA) or stuffing marker (0x55). Buffers should
  70. * be sized twice the expected length to account for stuffing overhead.
  71. */
  72. static void esirisc_jtag_unstuff(uint8_t *data, size_t len)
  73. {
  74. uint8_t *r, *w;
  75. uint8_t *end;
  76. r = w = data;
  77. end = data + len;
  78. while (r < end) {
  79. if (*r == STUFF_MARKER) {
  80. r++; /* skip stuffing marker */
  81. assert(r < end);
  82. *w++ = *r++ ^ STUFF_MARKER;
  83. } else
  84. *w++ = *r++;
  85. }
  86. }
  87. /*
  88. * The eSi-Debug protocol defines a byte-oriented command/response
  89. * channel that operates over serial or JTAG. While not strictly
  90. * required, separate DR scans are used for sending and receiving data.
  91. * This allows the TAP to recover gracefully if the byte stream is
  92. * corrupted at the expense of sending additional padding bits.
  93. */
  94. static int esirisc_jtag_send(struct esirisc_jtag *jtag_info, uint8_t command,
  95. int num_out_fields, struct scan_field *out_fields)
  96. {
  97. int num_fields = 2 + num_out_fields;
  98. struct scan_field *fields = cmd_queue_alloc(num_fields * sizeof(struct scan_field));
  99. esirisc_jtag_set_instr(jtag_info, INSTR_DEBUG);
  100. fields[0].num_bits = esirisc_jtag_get_padding();
  101. fields[0].out_value = NULL;
  102. fields[0].in_value = NULL;
  103. fields[1].num_bits = 8;
  104. fields[1].out_value = &command;
  105. fields[1].in_value = NULL;
  106. /* append command data */
  107. for (int i = 0; i < num_out_fields; ++i)
  108. jtag_scan_field_clone(&fields[2+i], &out_fields[i]);
  109. jtag_add_dr_scan(jtag_info->tap, num_fields, fields, TAP_IDLE);
  110. return jtag_execute_queue();
  111. }
  112. static int esirisc_jtag_recv(struct esirisc_jtag *jtag_info,
  113. int num_in_fields, struct scan_field *in_fields)
  114. {
  115. int num_in_bits = esirisc_jtag_count_bits(num_in_fields, in_fields);
  116. int num_in_bytes = DIV_ROUND_UP(num_in_bits, 8);
  117. struct scan_field fields[3];
  118. uint8_t r[num_in_bytes * 2];
  119. esirisc_jtag_set_instr(jtag_info, INSTR_DEBUG);
  120. fields[0].num_bits = esirisc_jtag_get_padding() + 1;
  121. fields[0].out_value = NULL;
  122. fields[0].in_value = NULL;
  123. fields[1].num_bits = 8;
  124. fields[1].out_value = NULL;
  125. fields[1].in_value = &jtag_info->status;
  126. fields[2].num_bits = num_in_bits * 2;
  127. fields[2].out_value = NULL;
  128. fields[2].in_value = r;
  129. jtag_add_dr_scan(jtag_info->tap, ARRAY_SIZE(fields), fields, TAP_IDLE);
  130. int retval = jtag_execute_queue();
  131. if (retval != ERROR_OK)
  132. return retval;
  133. /* unstuff response data and write back to caller */
  134. if (num_in_fields > 0) {
  135. esirisc_jtag_unstuff(r, ARRAY_SIZE(r));
  136. int bit_count = 0;
  137. for (int i = 0; i < num_in_fields; ++i) {
  138. buf_set_buf(r, bit_count, in_fields[i].in_value, 0, in_fields[i].num_bits);
  139. bit_count += in_fields[i].num_bits;
  140. }
  141. }
  142. return ERROR_OK;
  143. }
  144. static int esirisc_jtag_check_status(struct esirisc_jtag *jtag_info)
  145. {
  146. uint8_t eid = esirisc_jtag_get_eid(jtag_info);
  147. if (eid != EID_NONE) {
  148. LOG_ERROR("esirisc_jtag: bad status: 0x%02" PRIx8 " (DA: %" PRId32 ", "
  149. "S: %" PRId32 ", EID: 0x%02" PRIx8 ")",
  150. jtag_info->status, esirisc_jtag_is_debug_active(jtag_info),
  151. esirisc_jtag_is_stopped(jtag_info), eid);
  152. return ERROR_FAIL;
  153. }
  154. return ERROR_OK;
  155. }
  156. static int esirisc_jtag_send_and_recv(struct esirisc_jtag *jtag_info, uint8_t command,
  157. int num_out_fields, struct scan_field *out_fields,
  158. int num_in_fields, struct scan_field *in_fields)
  159. {
  160. int retval;
  161. jtag_info->status = 0; /* clear status */
  162. retval = esirisc_jtag_send(jtag_info, command, num_out_fields, out_fields);
  163. if (retval != ERROR_OK) {
  164. LOG_ERROR("esirisc_jtag: send failed (command: 0x%02" PRIx8 ")", command);
  165. return ERROR_FAIL;
  166. }
  167. retval = esirisc_jtag_recv(jtag_info, num_in_fields, in_fields);
  168. if (retval != ERROR_OK) {
  169. LOG_ERROR("esirisc_jtag: recv failed (command: 0x%02" PRIx8 ")", command);
  170. return ERROR_FAIL;
  171. }
  172. return esirisc_jtag_check_status(jtag_info);
  173. }
  174. /*
  175. * Status is automatically updated after each command completes;
  176. * these functions make each field available to the caller.
  177. */
  178. bool esirisc_jtag_is_debug_active(struct esirisc_jtag *jtag_info)
  179. {
  180. return !!(jtag_info->status & 1<<7); /* DA */
  181. }
  182. bool esirisc_jtag_is_stopped(struct esirisc_jtag *jtag_info)
  183. {
  184. return !!(jtag_info->status & 1<<6); /* S */
  185. }
  186. uint8_t esirisc_jtag_get_eid(struct esirisc_jtag *jtag_info)
  187. {
  188. return jtag_info->status & 0x3f; /* EID */
  189. }
  190. /*
  191. * Most commands manipulate target data (eg. memory and registers); each
  192. * command returns a status byte that indicates success. Commands must
  193. * transmit multibyte values in big-endian order, however response
  194. * values are in little-endian order. Target endianness does not have an
  195. * effect on this ordering.
  196. */
  197. int esirisc_jtag_read_byte(struct esirisc_jtag *jtag_info, uint32_t address, uint8_t *data)
  198. {
  199. struct scan_field out_fields[1];
  200. uint8_t a[4];
  201. out_fields[0].num_bits = 32;
  202. out_fields[0].out_value = a;
  203. h_u32_to_be(a, address);
  204. out_fields[0].in_value = NULL;
  205. struct scan_field in_fields[1];
  206. uint8_t d[1];
  207. in_fields[0].num_bits = 8;
  208. in_fields[0].out_value = NULL;
  209. in_fields[0].in_value = d;
  210. int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_BYTE,
  211. ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
  212. if (retval != ERROR_OK)
  213. return retval;
  214. *data = *d;
  215. LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx8, address, *data);
  216. return ERROR_OK;
  217. }
  218. int esirisc_jtag_read_hword(struct esirisc_jtag *jtag_info, uint32_t address, uint16_t *data)
  219. {
  220. struct scan_field out_fields[1];
  221. uint8_t a[4];
  222. out_fields[0].num_bits = 32;
  223. out_fields[0].out_value = a;
  224. h_u32_to_be(a, address);
  225. out_fields[0].in_value = NULL;
  226. struct scan_field in_fields[1];
  227. uint8_t d[2];
  228. in_fields[0].num_bits = 16;
  229. in_fields[0].out_value = NULL;
  230. in_fields[0].in_value = d;
  231. int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_HWORD,
  232. ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
  233. if (retval != ERROR_OK)
  234. return retval;
  235. *data = le_to_h_u16(d);
  236. LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx16, address, *data);
  237. return ERROR_OK;
  238. }
  239. int esirisc_jtag_read_word(struct esirisc_jtag *jtag_info, uint32_t address, uint32_t *data)
  240. {
  241. struct scan_field out_fields[1];
  242. uint8_t a[4];
  243. out_fields[0].num_bits = 32;
  244. out_fields[0].out_value = a;
  245. h_u32_to_be(a, address);
  246. out_fields[0].in_value = NULL;
  247. struct scan_field in_fields[1];
  248. uint8_t d[4];
  249. in_fields[0].num_bits = 32;
  250. in_fields[0].out_value = NULL;
  251. in_fields[0].in_value = d;
  252. int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_WORD,
  253. ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
  254. if (retval != ERROR_OK)
  255. return retval;
  256. *data = le_to_h_u32(d);
  257. LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx32, address, *data);
  258. return ERROR_OK;
  259. }
  260. int esirisc_jtag_write_byte(struct esirisc_jtag *jtag_info, uint32_t address, uint8_t data)
  261. {
  262. struct scan_field out_fields[2];
  263. uint8_t a[4];
  264. LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx8, address, data);
  265. out_fields[0].num_bits = 32;
  266. out_fields[0].out_value = a;
  267. h_u32_to_be(a, address);
  268. out_fields[0].in_value = NULL;
  269. out_fields[1].num_bits = 8;
  270. out_fields[1].out_value = &data;
  271. out_fields[1].in_value = NULL;
  272. return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_BYTE,
  273. ARRAY_SIZE(out_fields), out_fields, 0, NULL);
  274. }
  275. int esirisc_jtag_write_hword(struct esirisc_jtag *jtag_info, uint32_t address, uint16_t data)
  276. {
  277. struct scan_field out_fields[2];
  278. uint8_t a[4], d[2];
  279. LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx16, address, data);
  280. out_fields[0].num_bits = 32;
  281. out_fields[0].out_value = a;
  282. h_u32_to_be(a, address);
  283. out_fields[0].in_value = NULL;
  284. out_fields[1].num_bits = 16;
  285. out_fields[1].out_value = d;
  286. h_u16_to_be(d, data);
  287. out_fields[1].in_value = NULL;
  288. return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_HWORD,
  289. ARRAY_SIZE(out_fields), out_fields, 0, NULL);
  290. }
  291. int esirisc_jtag_write_word(struct esirisc_jtag *jtag_info, uint32_t address, uint32_t data)
  292. {
  293. struct scan_field out_fields[2];
  294. uint8_t a[4], d[4];
  295. LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx32, address, data);
  296. out_fields[0].num_bits = 32;
  297. out_fields[0].out_value = a;
  298. h_u32_to_be(a, address);
  299. out_fields[0].in_value = NULL;
  300. out_fields[1].num_bits = 32;
  301. out_fields[1].out_value = d;
  302. h_u32_to_be(d, data);
  303. out_fields[1].in_value = NULL;
  304. return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_WORD,
  305. ARRAY_SIZE(out_fields), out_fields, 0, NULL);
  306. }
  307. int esirisc_jtag_read_reg(struct esirisc_jtag *jtag_info, uint8_t reg, uint32_t *data)
  308. {
  309. struct scan_field out_fields[1];
  310. out_fields[0].num_bits = 8;
  311. out_fields[0].out_value = &reg;
  312. out_fields[0].in_value = NULL;
  313. struct scan_field in_fields[1];
  314. uint8_t d[4];
  315. in_fields[0].num_bits = 32;
  316. in_fields[0].out_value = NULL;
  317. in_fields[0].in_value = d;
  318. int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_REG,
  319. ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
  320. if (retval != ERROR_OK)
  321. return retval;
  322. *data = le_to_h_u32(d);
  323. LOG_DEBUG("register: 0x%" PRIx8 ", data: 0x%" PRIx32, reg, *data);
  324. return ERROR_OK;
  325. }
  326. int esirisc_jtag_write_reg(struct esirisc_jtag *jtag_info, uint8_t reg, uint32_t data)
  327. {
  328. struct scan_field out_fields[2];
  329. uint8_t d[4];
  330. LOG_DEBUG("register: 0x%" PRIx8 ", data: 0x%" PRIx32, reg, data);
  331. out_fields[0].num_bits = 8;
  332. out_fields[0].out_value = &reg;
  333. out_fields[0].in_value = NULL;
  334. out_fields[1].num_bits = 32;
  335. out_fields[1].out_value = d;
  336. h_u32_to_be(d, data);
  337. out_fields[1].in_value = NULL;
  338. return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_REG,
  339. ARRAY_SIZE(out_fields), out_fields, 0, NULL);
  340. }
  341. int esirisc_jtag_read_csr(struct esirisc_jtag *jtag_info, uint8_t bank, uint8_t csr, uint32_t *data)
  342. {
  343. struct scan_field out_fields[1];
  344. uint8_t c[2];
  345. out_fields[0].num_bits = 16;
  346. out_fields[0].out_value = c;
  347. h_u16_to_be(c, (csr << 5) | bank);
  348. out_fields[0].in_value = NULL;
  349. struct scan_field in_fields[1];
  350. uint8_t d[4];
  351. in_fields[0].num_bits = 32;
  352. in_fields[0].out_value = NULL;
  353. in_fields[0].in_value = d;
  354. int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_CSR,
  355. ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
  356. if (retval != ERROR_OK)
  357. return retval;
  358. *data = le_to_h_u32(d);
  359. LOG_DEBUG("bank: 0x%" PRIx8 ", csr: 0x%" PRIx8 ", data: 0x%" PRIx32, bank, csr, *data);
  360. return ERROR_OK;
  361. }
  362. int esirisc_jtag_write_csr(struct esirisc_jtag *jtag_info, uint8_t bank, uint8_t csr, uint32_t data)
  363. {
  364. struct scan_field out_fields[2];
  365. uint8_t c[2], d[4];
  366. LOG_DEBUG("bank: 0x%" PRIx8 ", csr: 0x%" PRIx8 ", data: 0x%" PRIx32, bank, csr, data);
  367. out_fields[0].num_bits = 16;
  368. out_fields[0].out_value = c;
  369. h_u16_to_be(c, (csr << 5) | bank);
  370. out_fields[0].in_value = NULL;
  371. out_fields[1].num_bits = 32;
  372. out_fields[1].out_value = d;
  373. h_u32_to_be(d, data);
  374. out_fields[1].in_value = NULL;
  375. return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_CSR,
  376. ARRAY_SIZE(out_fields), out_fields, 0, NULL);
  377. }
  378. /*
  379. * Control commands affect CPU operation; these commands send no data
  380. * and return a status byte.
  381. */
  382. static inline int esirisc_jtag_send_ctrl(struct esirisc_jtag *jtag_info, uint8_t command)
  383. {
  384. return esirisc_jtag_send_and_recv(jtag_info, command, 0, NULL, 0, NULL);
  385. }
  386. int esirisc_jtag_enable_debug(struct esirisc_jtag *jtag_info)
  387. {
  388. return esirisc_jtag_send_ctrl(jtag_info, DEBUG_ENABLE_DEBUG);
  389. }
  390. int esirisc_jtag_disable_debug(struct esirisc_jtag *jtag_info)
  391. {
  392. return esirisc_jtag_send_ctrl(jtag_info, DEBUG_DISABLE_DEBUG);
  393. }
  394. int esirisc_jtag_assert_reset(struct esirisc_jtag *jtag_info)
  395. {
  396. return esirisc_jtag_send_ctrl(jtag_info, DEBUG_ASSERT_RESET);
  397. }
  398. int esirisc_jtag_deassert_reset(struct esirisc_jtag *jtag_info)
  399. {
  400. return esirisc_jtag_send_ctrl(jtag_info, DEBUG_DEASSERT_RESET);
  401. }
  402. int esirisc_jtag_break(struct esirisc_jtag *jtag_info)
  403. {
  404. return esirisc_jtag_send_ctrl(jtag_info, DEBUG_BREAK);
  405. }
  406. int esirisc_jtag_continue(struct esirisc_jtag *jtag_info)
  407. {
  408. return esirisc_jtag_send_ctrl(jtag_info, DEBUG_CONTINUE);
  409. }
  410. int esirisc_jtag_flush_caches(struct esirisc_jtag *jtag_info)
  411. {
  412. return esirisc_jtag_send_ctrl(jtag_info, DEBUG_FLUSH_CACHES);
  413. }