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.
 
 
 
 
 
 

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