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.
 
 
 
 
 
 

323 lines
9.0 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2008 by Spencer Oliver *
  3. * spen@spen-soft.co.uk *
  4. * *
  5. * Copyright (C) 2008 by David T.L. Wong *
  6. * *
  7. * Copyright (C) 2009 by David N. Claffey <dnclaffey@gmail.com> *
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. * This program is distributed in the hope that it will be useful, *
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  17. * GNU General Public License for more details. *
  18. * *
  19. * You should have received a copy of the GNU General Public License *
  20. * along with this program; if not, write to the *
  21. * Free Software Foundation, Inc., *
  22. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  23. ***************************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "mips32.h"
  28. #include "mips_ejtag.h"
  29. int mips_ejtag_set_instr(struct mips_ejtag *ejtag_info, int new_instr, void *delete_me_and_submit_patch)
  30. {
  31. struct jtag_tap *tap;
  32. tap = ejtag_info->tap;
  33. if (tap == NULL)
  34. return ERROR_FAIL;
  35. if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != (uint32_t)new_instr)
  36. {
  37. struct scan_field field;
  38. uint8_t t[4];
  39. field.tap = tap;
  40. field.num_bits = tap->ir_length;
  41. field.out_value = t;
  42. buf_set_u32(field.out_value, 0, field.num_bits, new_instr);
  43. field.in_value = NULL;
  44. jtag_add_ir_scan(1, &field, jtag_get_end_state());
  45. }
  46. return ERROR_OK;
  47. }
  48. int mips_ejtag_get_idcode(struct mips_ejtag *ejtag_info, uint32_t *idcode)
  49. {
  50. struct scan_field field;
  51. jtag_set_end_state(TAP_IDLE);
  52. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_IDCODE, NULL);
  53. field.tap = ejtag_info->tap;
  54. field.num_bits = 32;
  55. field.out_value = NULL;
  56. field.in_value = (void*)idcode;
  57. jtag_add_dr_scan(1, &field, jtag_get_end_state());
  58. if (jtag_execute_queue() != ERROR_OK)
  59. {
  60. LOG_ERROR("register read failed");
  61. }
  62. return ERROR_OK;
  63. }
  64. int mips_ejtag_get_impcode(struct mips_ejtag *ejtag_info, uint32_t *impcode)
  65. {
  66. struct scan_field field;
  67. jtag_set_end_state(TAP_IDLE);
  68. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_IMPCODE, NULL);
  69. field.tap = ejtag_info->tap;
  70. field.num_bits = 32;
  71. field.out_value = NULL;
  72. field.in_value = (void*)impcode;
  73. jtag_add_dr_scan(1, &field, jtag_get_end_state());
  74. if (jtag_execute_queue() != ERROR_OK)
  75. {
  76. LOG_ERROR("register read failed");
  77. }
  78. return ERROR_OK;
  79. }
  80. int mips_ejtag_drscan_32(struct mips_ejtag *ejtag_info, uint32_t *data)
  81. {
  82. struct jtag_tap *tap;
  83. tap = ejtag_info->tap;
  84. if (tap == NULL)
  85. return ERROR_FAIL;
  86. struct scan_field field;
  87. uint8_t t[4], r[4];
  88. int retval;
  89. field.tap = tap;
  90. field.num_bits = 32;
  91. field.out_value = t;
  92. buf_set_u32(field.out_value, 0, field.num_bits, *data);
  93. field.in_value = r;
  94. jtag_add_dr_scan(1, &field, jtag_get_end_state());
  95. if ((retval = jtag_execute_queue()) != ERROR_OK)
  96. {
  97. LOG_ERROR("register read failed");
  98. return retval;
  99. }
  100. *data = buf_get_u32(field.in_value, 0, 32);
  101. keep_alive();
  102. return ERROR_OK;
  103. }
  104. int mips_ejtag_step_enable(struct mips_ejtag *ejtag_info)
  105. {
  106. uint32_t code[] = {
  107. MIPS32_MTC0(1,31,0), /* move $1 to COP0 DeSave */
  108. MIPS32_MFC0(1,23,0), /* move COP0 Debug to $1 */
  109. MIPS32_ORI(1,1,0x0100), /* set SSt bit in debug reg */
  110. MIPS32_MTC0(1,23,0), /* move $1 to COP0 Debug */
  111. MIPS32_B(NEG16(5)),
  112. MIPS32_MFC0(1,31,0), /* move COP0 DeSave to $1 */
  113. };
  114. mips32_pracc_exec(ejtag_info, ARRAY_SIZE(code), code, \
  115. 0, NULL, 0, NULL, 1);
  116. return ERROR_OK;
  117. }
  118. int mips_ejtag_step_disable(struct mips_ejtag *ejtag_info)
  119. {
  120. uint32_t code[] = {
  121. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  122. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  123. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  124. MIPS32_SW(1,0,15), /* sw $1,($15) */
  125. MIPS32_SW(2,0,15), /* sw $2,($15) */
  126. MIPS32_MFC0(1,23,0), /* move COP0 Debug to $1 */
  127. MIPS32_LUI(2,0xFFFF), /* $2 = 0xfffffeff */
  128. MIPS32_ORI(2,2,0xFEFF),
  129. MIPS32_AND(1,1,2),
  130. MIPS32_MTC0(1,23,0), /* move $1 to COP0 Debug */
  131. MIPS32_LW(2,0,15),
  132. MIPS32_LW(1,0,15),
  133. MIPS32_B(NEG16(13)),
  134. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  135. };
  136. mips32_pracc_exec(ejtag_info, ARRAY_SIZE(code), code, \
  137. 0, NULL, 0, NULL, 1);
  138. return ERROR_OK;
  139. }
  140. int mips_ejtag_config_step(struct mips_ejtag *ejtag_info, int enable_step)
  141. {
  142. if (enable_step)
  143. return mips_ejtag_step_enable(ejtag_info);
  144. return mips_ejtag_step_disable(ejtag_info);
  145. }
  146. int mips_ejtag_enter_debug(struct mips_ejtag *ejtag_info)
  147. {
  148. uint32_t ejtag_ctrl;
  149. jtag_set_end_state(TAP_IDLE);
  150. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL, NULL);
  151. /* set debug break bit */
  152. ejtag_ctrl = ejtag_info->ejtag_ctrl | EJTAG_CTRL_JTAGBRK;
  153. mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  154. /* break bit will be cleared by hardware */
  155. ejtag_ctrl = ejtag_info->ejtag_ctrl;
  156. mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  157. LOG_DEBUG("ejtag_ctrl: 0x%8.8" PRIx32 "", ejtag_ctrl);
  158. if ((ejtag_ctrl & EJTAG_CTRL_BRKST) == 0)
  159. LOG_DEBUG("Failed to enter Debug Mode!");
  160. return ERROR_OK;
  161. }
  162. int mips_ejtag_exit_debug(struct mips_ejtag *ejtag_info)
  163. {
  164. uint32_t inst;
  165. inst = MIPS32_DRET;
  166. /* execute our dret instruction */
  167. mips32_pracc_exec(ejtag_info, 1, &inst, 0, NULL, 0, NULL, 0);
  168. return ERROR_OK;
  169. }
  170. int mips_ejtag_read_debug(struct mips_ejtag *ejtag_info, uint32_t* debug_reg)
  171. {
  172. /* read ejtag ECR */
  173. uint32_t code[] = {
  174. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  175. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  176. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  177. MIPS32_SW(1,0,15), /* sw $1,($15) */
  178. MIPS32_SW(2,0,15), /* sw $2,($15) */
  179. MIPS32_LUI(1,UPPER16(MIPS32_PRACC_PARAM_OUT)), /* $1 = MIPS32_PRACC_PARAM_OUT */
  180. MIPS32_ORI(1,1,LOWER16(MIPS32_PRACC_PARAM_OUT)),
  181. MIPS32_MFC0(2,23,0), /* move COP0 Debug to $2 */
  182. MIPS32_SW(2,0,1),
  183. MIPS32_LW(2,0,15),
  184. MIPS32_LW(1,0,15),
  185. MIPS32_B(NEG16(12)),
  186. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  187. };
  188. mips32_pracc_exec(ejtag_info, ARRAY_SIZE(code), code, \
  189. 0, NULL, 1, debug_reg, 1);
  190. return ERROR_OK;
  191. }
  192. int mips_ejtag_init(struct mips_ejtag *ejtag_info)
  193. {
  194. uint32_t ejtag_version;
  195. mips_ejtag_get_impcode(ejtag_info, &ejtag_info->impcode);
  196. LOG_DEBUG("impcode: 0x%8.8" PRIx32 "", ejtag_info->impcode);
  197. /* get ejtag version */
  198. ejtag_version = ((ejtag_info->impcode >> 29) & 0x07);
  199. switch (ejtag_version)
  200. {
  201. case 0:
  202. LOG_DEBUG("EJTAG: Version 1 or 2.0 Detected");
  203. break;
  204. case 1:
  205. LOG_DEBUG("EJTAG: Version 2.5 Detected");
  206. break;
  207. case 2:
  208. LOG_DEBUG("EJTAG: Version 2.6 Detected");
  209. break;
  210. case 3:
  211. LOG_DEBUG("EJTAG: Version 3.1 Detected");
  212. break;
  213. default:
  214. LOG_DEBUG("EJTAG: Unknown Version Detected");
  215. break;
  216. }
  217. LOG_DEBUG("EJTAG: features:%s%s%s%s%s%s%s",
  218. ejtag_info->impcode & (1 << 28) ? " R3k": " R4k",
  219. ejtag_info->impcode & (1 << 24) ? " DINT": "",
  220. ejtag_info->impcode & (1 << 22) ? " ASID_8": "",
  221. ejtag_info->impcode & (1 << 21) ? " ASID_6": "",
  222. ejtag_info->impcode & (1 << 16) ? " MIPS16": "",
  223. ejtag_info->impcode & (1 << 14) ? " noDMA": " DMA",
  224. ejtag_info->impcode & (1 << 0) ? " MIPS64": " MIPS32"
  225. );
  226. if ((ejtag_info->impcode & (1 << 14)) == 0)
  227. LOG_DEBUG("EJTAG: DMA Access Mode Support Enabled");
  228. /* set initial state for ejtag control reg */
  229. ejtag_info->ejtag_ctrl = EJTAG_CTRL_ROCC | EJTAG_CTRL_PRACC | EJTAG_CTRL_PROBEN | EJTAG_CTRL_SETDEV;
  230. return ERROR_OK;
  231. }
  232. int mips_ejtag_fastdata_scan(struct mips_ejtag *ejtag_info, int write, uint32_t *data)
  233. {
  234. struct jtag_tap *tap;
  235. tap = ejtag_info->tap;
  236. if (tap == NULL)
  237. return ERROR_FAIL;
  238. struct scan_field fields[2];
  239. uint8_t spracc = 0;
  240. uint8_t t[4] = {0, 0, 0, 0};
  241. /* fastdata 1-bit register */
  242. fields[0].tap = tap;
  243. fields[0].num_bits = 1;
  244. fields[0].out_value = &spracc;
  245. fields[0].in_value = NULL;
  246. /* processor access data register 32 bit */
  247. fields[1].tap = tap;
  248. fields[1].num_bits = 32;
  249. fields[1].out_value = t;
  250. if (write)
  251. {
  252. fields[1].in_value = NULL;
  253. buf_set_u32(t, 0, 32, *data);
  254. }
  255. else
  256. {
  257. fields[1].in_value = (uint8_t *) data;
  258. }
  259. jtag_add_dr_scan(2, fields, jtag_get_end_state());
  260. keep_alive();
  261. return ERROR_OK;
  262. }