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.
 
 
 
 
 
 

347 lines
9.5 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)
  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.num_bits = tap->ir_length;
  40. field.out_value = t;
  41. buf_set_u32(field.out_value, 0, field.num_bits, new_instr);
  42. field.in_value = NULL;
  43. jtag_add_ir_scan(tap, &field, jtag_get_end_state());
  44. }
  45. return ERROR_OK;
  46. }
  47. int mips_ejtag_get_idcode(struct mips_ejtag *ejtag_info, uint32_t *idcode)
  48. {
  49. struct scan_field field;
  50. jtag_set_end_state(TAP_IDLE);
  51. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_IDCODE);
  52. field.num_bits = 32;
  53. field.out_value = NULL;
  54. field.in_value = (void*)idcode;
  55. jtag_add_dr_scan(ejtag_info->tap, 1, &field, TAP_IDLE);
  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(struct mips_ejtag *ejtag_info, uint32_t *impcode)
  63. {
  64. struct scan_field field;
  65. jtag_set_end_state(TAP_IDLE);
  66. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_IMPCODE);
  67. field.num_bits = 32;
  68. field.out_value = NULL;
  69. field.in_value = (void*)impcode;
  70. jtag_add_dr_scan(ejtag_info->tap, 1, &field, TAP_IDLE);
  71. if (jtag_execute_queue() != ERROR_OK)
  72. {
  73. LOG_ERROR("register read failed");
  74. }
  75. return ERROR_OK;
  76. }
  77. int mips_ejtag_drscan_32(struct mips_ejtag *ejtag_info, uint32_t *data)
  78. {
  79. struct jtag_tap *tap;
  80. tap = ejtag_info->tap;
  81. if (tap == NULL)
  82. return ERROR_FAIL;
  83. struct scan_field field;
  84. uint8_t t[4], r[4];
  85. int retval;
  86. field.num_bits = 32;
  87. field.out_value = t;
  88. buf_set_u32(field.out_value, 0, field.num_bits, *data);
  89. field.in_value = r;
  90. jtag_add_dr_scan(tap, 1, &field, jtag_get_end_state());
  91. if ((retval = jtag_execute_queue()) != ERROR_OK)
  92. {
  93. LOG_ERROR("register read failed");
  94. return retval;
  95. }
  96. *data = buf_get_u32(field.in_value, 0, 32);
  97. keep_alive();
  98. return ERROR_OK;
  99. }
  100. int mips_ejtag_drscan_8(struct mips_ejtag *ejtag_info, uint32_t *data)
  101. {
  102. struct jtag_tap *tap;
  103. tap = ejtag_info->tap;
  104. if (tap == NULL)
  105. return ERROR_FAIL;
  106. struct scan_field field;
  107. uint8_t t[4] = {0, 0, 0, 0}, r[4];
  108. int retval;
  109. field.num_bits = 8;
  110. field.out_value = t;
  111. buf_set_u32(field.out_value, 0, field.num_bits, *data);
  112. field.in_value = r;
  113. jtag_add_dr_scan(tap, 1, &field, jtag_get_end_state());
  114. if ((retval = jtag_execute_queue()) != ERROR_OK)
  115. {
  116. LOG_ERROR("register read failed");
  117. return retval;
  118. }
  119. *data = buf_get_u32(field.in_value, 0, 32);
  120. keep_alive();
  121. return ERROR_OK;
  122. }
  123. int mips_ejtag_step_enable(struct mips_ejtag *ejtag_info)
  124. {
  125. static const uint32_t code[] = {
  126. MIPS32_MTC0(1,31,0), /* move $1 to COP0 DeSave */
  127. MIPS32_MFC0(1,23,0), /* move COP0 Debug to $1 */
  128. MIPS32_ORI(1,1,0x0100), /* set SSt bit in debug reg */
  129. MIPS32_MTC0(1,23,0), /* move $1 to COP0 Debug */
  130. MIPS32_B(NEG16(5)),
  131. MIPS32_MFC0(1,31,0), /* move COP0 DeSave to $1 */
  132. };
  133. mips32_pracc_exec(ejtag_info, ARRAY_SIZE(code), code, \
  134. 0, NULL, 0, NULL, 1);
  135. return ERROR_OK;
  136. }
  137. int mips_ejtag_step_disable(struct mips_ejtag *ejtag_info)
  138. {
  139. static const uint32_t code[] = {
  140. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  141. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  142. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  143. MIPS32_SW(1,0,15), /* sw $1,($15) */
  144. MIPS32_SW(2,0,15), /* sw $2,($15) */
  145. MIPS32_MFC0(1,23,0), /* move COP0 Debug to $1 */
  146. MIPS32_LUI(2,0xFFFF), /* $2 = 0xfffffeff */
  147. MIPS32_ORI(2,2,0xFEFF),
  148. MIPS32_AND(1,1,2),
  149. MIPS32_MTC0(1,23,0), /* move $1 to COP0 Debug */
  150. MIPS32_LW(2,0,15),
  151. MIPS32_LW(1,0,15),
  152. MIPS32_B(NEG16(13)),
  153. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  154. };
  155. mips32_pracc_exec(ejtag_info, ARRAY_SIZE(code), code, \
  156. 0, NULL, 0, NULL, 1);
  157. return ERROR_OK;
  158. }
  159. int mips_ejtag_config_step(struct mips_ejtag *ejtag_info, int enable_step)
  160. {
  161. if (enable_step)
  162. return mips_ejtag_step_enable(ejtag_info);
  163. return mips_ejtag_step_disable(ejtag_info);
  164. }
  165. int mips_ejtag_enter_debug(struct mips_ejtag *ejtag_info)
  166. {
  167. uint32_t ejtag_ctrl;
  168. jtag_set_end_state(TAP_IDLE);
  169. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL);
  170. /* set debug break bit */
  171. ejtag_ctrl = ejtag_info->ejtag_ctrl | EJTAG_CTRL_JTAGBRK;
  172. mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  173. /* break bit will be cleared by hardware */
  174. ejtag_ctrl = ejtag_info->ejtag_ctrl;
  175. mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  176. LOG_DEBUG("ejtag_ctrl: 0x%8.8" PRIx32 "", ejtag_ctrl);
  177. if ((ejtag_ctrl & EJTAG_CTRL_BRKST) == 0)
  178. LOG_DEBUG("Failed to enter Debug Mode!");
  179. return ERROR_OK;
  180. }
  181. int mips_ejtag_exit_debug(struct mips_ejtag *ejtag_info)
  182. {
  183. uint32_t inst;
  184. inst = MIPS32_DRET;
  185. /* execute our dret instruction */
  186. mips32_pracc_exec(ejtag_info, 1, &inst, 0, NULL, 0, NULL, 0);
  187. return ERROR_OK;
  188. }
  189. int mips_ejtag_read_debug(struct mips_ejtag *ejtag_info, uint32_t* debug_reg)
  190. {
  191. /* read ejtag ECR */
  192. static const uint32_t code[] = {
  193. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  194. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  195. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  196. MIPS32_SW(1,0,15), /* sw $1,($15) */
  197. MIPS32_SW(2,0,15), /* sw $2,($15) */
  198. MIPS32_LUI(1,UPPER16(MIPS32_PRACC_PARAM_OUT)), /* $1 = MIPS32_PRACC_PARAM_OUT */
  199. MIPS32_ORI(1,1,LOWER16(MIPS32_PRACC_PARAM_OUT)),
  200. MIPS32_MFC0(2,23,0), /* move COP0 Debug to $2 */
  201. MIPS32_SW(2,0,1),
  202. MIPS32_LW(2,0,15),
  203. MIPS32_LW(1,0,15),
  204. MIPS32_B(NEG16(12)),
  205. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  206. };
  207. mips32_pracc_exec(ejtag_info, ARRAY_SIZE(code), code, \
  208. 0, NULL, 1, debug_reg, 1);
  209. return ERROR_OK;
  210. }
  211. int mips_ejtag_init(struct mips_ejtag *ejtag_info)
  212. {
  213. uint32_t ejtag_version;
  214. mips_ejtag_get_impcode(ejtag_info, &ejtag_info->impcode);
  215. LOG_DEBUG("impcode: 0x%8.8" PRIx32 "", ejtag_info->impcode);
  216. /* get ejtag version */
  217. ejtag_version = ((ejtag_info->impcode >> 29) & 0x07);
  218. switch (ejtag_version)
  219. {
  220. case 0:
  221. LOG_DEBUG("EJTAG: Version 1 or 2.0 Detected");
  222. break;
  223. case 1:
  224. LOG_DEBUG("EJTAG: Version 2.5 Detected");
  225. break;
  226. case 2:
  227. LOG_DEBUG("EJTAG: Version 2.6 Detected");
  228. break;
  229. case 3:
  230. LOG_DEBUG("EJTAG: Version 3.1 Detected");
  231. break;
  232. default:
  233. LOG_DEBUG("EJTAG: Unknown Version Detected");
  234. break;
  235. }
  236. LOG_DEBUG("EJTAG: features:%s%s%s%s%s%s%s",
  237. ejtag_info->impcode & EJTAG_IMP_R3K ? " R3k" : " R4k",
  238. ejtag_info->impcode & EJTAG_IMP_DINT ? " DINT" : "",
  239. ejtag_info->impcode & (1 << 22) ? " ASID_8" : "",
  240. ejtag_info->impcode & (1 << 21) ? " ASID_6" : "",
  241. ejtag_info->impcode & EJTAG_IMP_MIPS16 ? " MIPS16" : "",
  242. ejtag_info->impcode & EJTAG_IMP_NODMA ? " noDMA" : " DMA",
  243. ejtag_info->impcode & EJTAG_DCR_MIPS64 ? " MIPS64" : " MIPS32");
  244. if ((ejtag_info->impcode & EJTAG_IMP_NODMA) == 0)
  245. LOG_DEBUG("EJTAG: DMA Access Mode Support Enabled");
  246. /* set initial state for ejtag control reg */
  247. ejtag_info->ejtag_ctrl = EJTAG_CTRL_ROCC | EJTAG_CTRL_PRACC | EJTAG_CTRL_PROBEN | EJTAG_CTRL_SETDEV;
  248. return ERROR_OK;
  249. }
  250. int mips_ejtag_fastdata_scan(struct mips_ejtag *ejtag_info, int write, uint32_t *data)
  251. {
  252. struct jtag_tap *tap;
  253. tap = ejtag_info->tap;
  254. if (tap == NULL)
  255. return ERROR_FAIL;
  256. struct scan_field fields[2];
  257. uint8_t spracc = 0;
  258. uint8_t t[4] = {0, 0, 0, 0};
  259. /* fastdata 1-bit register */
  260. fields[0].num_bits = 1;
  261. fields[0].out_value = &spracc;
  262. fields[0].in_value = NULL;
  263. /* processor access data register 32 bit */
  264. fields[1].num_bits = 32;
  265. fields[1].out_value = t;
  266. if (write)
  267. {
  268. fields[1].in_value = NULL;
  269. buf_set_u32(t, 0, 32, *data);
  270. }
  271. else
  272. {
  273. fields[1].in_value = (uint8_t *) data;
  274. }
  275. jtag_add_dr_scan(tap, 2, fields, jtag_get_end_state());
  276. keep_alive();
  277. return ERROR_OK;
  278. }