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.
 
 
 
 
 
 

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