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.
 
 
 
 
 
 

993 lines
30 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2008 by Spencer Oliver *
  6. * spen@spen-soft.co.uk *
  7. * *
  8. * Copyright (C) 2008 by Hongtao Zheng *
  9. * hontor@126.com *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "arm9tdmi.h"
  30. #include "target_type.h"
  31. /*
  32. * NOTE: this holds code that's used with multiple ARM9 processors:
  33. * - ARM9TDMI (ARMv4T) ... in ARM920, ARM922, and ARM940 cores
  34. * - ARM9E-S (ARMv5TE) ... in ARM946, ARM966, and ARM968 cores
  35. * - ARM9EJS (ARMv5TEJ) ... in ARM926 core
  36. *
  37. * In short, the file name is a misnomer ... it is NOT specific to
  38. * that first generation ARM9 processor, or cores using it.
  39. */
  40. #if 0
  41. #define _DEBUG_INSTRUCTION_EXECUTION_
  42. #endif
  43. static const arm9tdmi_vector_t arm9tdmi_vectors[] =
  44. {
  45. {"reset", ARM9TDMI_RESET_VECTOR},
  46. {"undef", ARM9TDMI_UNDEF_VECTOR},
  47. {"swi", ARM9TDMI_SWI_VECTOR},
  48. {"pabt", ARM9TDMI_PABT_VECTOR},
  49. {"dabt", ARM9TDMI_DABT_VECTOR},
  50. {"irq", ARM9TDMI_IRQ_VECTOR},
  51. {"fiq", ARM9TDMI_FIQ_VECTOR},
  52. {0, 0},
  53. };
  54. int arm9tdmi_examine_debug_reason(target_t *target)
  55. {
  56. int retval = ERROR_OK;
  57. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  58. /* only check the debug reason if we don't know it already */
  59. if ((target->debug_reason != DBG_REASON_DBGRQ)
  60. && (target->debug_reason != DBG_REASON_SINGLESTEP))
  61. {
  62. scan_field_t fields[3];
  63. uint8_t databus[4];
  64. uint8_t instructionbus[4];
  65. uint8_t debug_reason;
  66. jtag_set_end_state(TAP_DRPAUSE);
  67. fields[0].tap = arm7_9->jtag_info.tap;
  68. fields[0].num_bits = 32;
  69. fields[0].out_value = NULL;
  70. fields[0].in_value = databus;
  71. fields[1].tap = arm7_9->jtag_info.tap;
  72. fields[1].num_bits = 3;
  73. fields[1].out_value = NULL;
  74. fields[1].in_value = &debug_reason;
  75. fields[2].tap = arm7_9->jtag_info.tap;
  76. fields[2].num_bits = 32;
  77. fields[2].out_value = NULL;
  78. fields[2].in_value = instructionbus;
  79. if ((retval = arm_jtag_scann(&arm7_9->jtag_info, 0x1)) != ERROR_OK)
  80. {
  81. return retval;
  82. }
  83. arm_jtag_set_instr(&arm7_9->jtag_info, arm7_9->jtag_info.intest_instr, NULL);
  84. jtag_add_dr_scan(3, fields, jtag_set_end_state(TAP_DRPAUSE));
  85. if ((retval = jtag_execute_queue()) != ERROR_OK)
  86. {
  87. return retval;
  88. }
  89. fields[0].in_value = NULL;
  90. fields[0].out_value = databus;
  91. fields[1].in_value = NULL;
  92. fields[1].out_value = &debug_reason;
  93. fields[2].in_value = NULL;
  94. fields[2].out_value = instructionbus;
  95. jtag_add_dr_scan(3, fields, jtag_set_end_state(TAP_DRPAUSE));
  96. if (debug_reason & 0x4)
  97. if (debug_reason & 0x2)
  98. target->debug_reason = DBG_REASON_WPTANDBKPT;
  99. else
  100. target->debug_reason = DBG_REASON_WATCHPOINT;
  101. else
  102. target->debug_reason = DBG_REASON_BREAKPOINT;
  103. }
  104. return ERROR_OK;
  105. }
  106. /* put an instruction in the ARM9TDMI pipeline or write the data bus,
  107. * and optionally read data
  108. */
  109. int arm9tdmi_clock_out(arm_jtag_t *jtag_info, uint32_t instr,
  110. uint32_t out, uint32_t *in, int sysspeed)
  111. {
  112. int retval = ERROR_OK;
  113. scan_field_t fields[3];
  114. uint8_t out_buf[4];
  115. uint8_t instr_buf[4];
  116. uint8_t sysspeed_buf = 0x0;
  117. /* prepare buffer */
  118. buf_set_u32(out_buf, 0, 32, out);
  119. buf_set_u32(instr_buf, 0, 32, flip_u32(instr, 32));
  120. if (sysspeed)
  121. buf_set_u32(&sysspeed_buf, 2, 1, 1);
  122. jtag_set_end_state(TAP_DRPAUSE);
  123. if ((retval = arm_jtag_scann(jtag_info, 0x1)) != ERROR_OK)
  124. {
  125. return retval;
  126. }
  127. arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
  128. fields[0].tap = jtag_info->tap;
  129. fields[0].num_bits = 32;
  130. fields[0].out_value = out_buf;
  131. fields[0].in_value = NULL;
  132. fields[1].tap = jtag_info->tap;
  133. fields[1].num_bits = 3;
  134. fields[1].out_value = &sysspeed_buf;
  135. fields[1].in_value = NULL;
  136. fields[2].tap = jtag_info->tap;
  137. fields[2].num_bits = 32;
  138. fields[2].out_value = instr_buf;
  139. fields[2].in_value = NULL;
  140. if (in)
  141. {
  142. fields[0].in_value = (uint8_t *)in;
  143. jtag_add_dr_scan(3, fields, jtag_get_end_state());
  144. jtag_add_callback(arm_le_to_h_u32, (jtag_callback_data_t)in);
  145. }
  146. else
  147. {
  148. jtag_add_dr_scan(3, fields, jtag_get_end_state());
  149. }
  150. jtag_add_runtest(0, jtag_get_end_state());
  151. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  152. {
  153. if ((retval = jtag_execute_queue()) != ERROR_OK)
  154. {
  155. return retval;
  156. }
  157. if (in)
  158. {
  159. LOG_DEBUG("instr: 0x%8.8x, out: 0x%8.8x, in: 0x%8.8x", instr, out, *in);
  160. }
  161. else
  162. LOG_DEBUG("instr: 0x%8.8x, out: 0x%8.8x", instr, out);
  163. }
  164. #endif
  165. return ERROR_OK;
  166. }
  167. /* just read data (instruction and data-out = don't care) */
  168. int arm9tdmi_clock_data_in(arm_jtag_t *jtag_info, uint32_t *in)
  169. {
  170. int retval = ERROR_OK;;
  171. scan_field_t fields[3];
  172. jtag_set_end_state(TAP_DRPAUSE);
  173. if ((retval = arm_jtag_scann(jtag_info, 0x1)) != ERROR_OK)
  174. {
  175. return retval;
  176. }
  177. arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
  178. fields[0].tap = jtag_info->tap;
  179. fields[0].num_bits = 32;
  180. fields[0].out_value = NULL;
  181. fields[0].in_value = (uint8_t *)in;
  182. fields[1].tap = jtag_info->tap;
  183. fields[1].num_bits = 3;
  184. fields[1].out_value = NULL;
  185. fields[1].in_value = NULL;
  186. fields[2].tap = jtag_info->tap;
  187. fields[2].num_bits = 32;
  188. fields[2].out_value = NULL;
  189. fields[2].in_value = NULL;
  190. jtag_add_dr_scan(3, fields, jtag_get_end_state());
  191. jtag_add_callback(arm_le_to_h_u32, (jtag_callback_data_t)in);
  192. jtag_add_runtest(0, jtag_get_end_state());
  193. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  194. {
  195. if ((retval = jtag_execute_queue()) != ERROR_OK)
  196. {
  197. return retval;
  198. }
  199. if (in)
  200. {
  201. LOG_DEBUG("in: 0x%8.8x", *in);
  202. }
  203. else
  204. {
  205. LOG_ERROR("BUG: called with in == NULL");
  206. }
  207. }
  208. #endif
  209. return ERROR_OK;
  210. }
  211. extern void arm_endianness(uint8_t *tmp, void *in, int size, int be, int flip);
  212. static int arm9endianness(jtag_callback_data_t arg,
  213. jtag_callback_data_t size, jtag_callback_data_t be,
  214. jtag_callback_data_t captured)
  215. {
  216. uint8_t *in = (uint8_t *)arg;
  217. arm_endianness((uint8_t *)captured, in, (int)size, (int)be, 0);
  218. return ERROR_OK;
  219. }
  220. /* clock the target, and read the databus
  221. * the *in pointer points to a buffer where elements of 'size' bytes
  222. * are stored in big (be == 1) or little (be == 0) endianness
  223. */
  224. int arm9tdmi_clock_data_in_endianness(arm_jtag_t *jtag_info,
  225. void *in, int size, int be)
  226. {
  227. int retval = ERROR_OK;
  228. scan_field_t fields[3];
  229. jtag_set_end_state(TAP_DRPAUSE);
  230. if ((retval = arm_jtag_scann(jtag_info, 0x1)) != ERROR_OK)
  231. {
  232. return retval;
  233. }
  234. arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
  235. fields[0].tap = jtag_info->tap;
  236. fields[0].num_bits = 32;
  237. fields[0].out_value = NULL;
  238. jtag_alloc_in_value32(&fields[0]);
  239. fields[1].tap = jtag_info->tap;
  240. fields[1].num_bits = 3;
  241. fields[1].out_value = NULL;
  242. fields[1].in_value = NULL;
  243. fields[2].tap = jtag_info->tap;
  244. fields[2].num_bits = 32;
  245. fields[2].out_value = NULL;
  246. fields[2].in_value = NULL;
  247. jtag_add_dr_scan(3, fields, jtag_get_end_state());
  248. jtag_add_callback4(arm9endianness, (jtag_callback_data_t)in, (jtag_callback_data_t)size, (jtag_callback_data_t)be, (jtag_callback_data_t)fields[0].in_value);
  249. jtag_add_runtest(0, jtag_get_end_state());
  250. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  251. {
  252. if ((retval = jtag_execute_queue()) != ERROR_OK)
  253. {
  254. return retval;
  255. }
  256. if (in)
  257. {
  258. LOG_DEBUG("in: 0x%8.8x", *(uint32_t*)in);
  259. }
  260. else
  261. {
  262. LOG_ERROR("BUG: called with in == NULL");
  263. }
  264. }
  265. #endif
  266. return ERROR_OK;
  267. }
  268. static void arm9tdmi_change_to_arm(target_t *target,
  269. uint32_t *r0, uint32_t *pc)
  270. {
  271. int retval = ERROR_OK;
  272. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  273. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  274. /* save r0 before using it and put system in ARM state
  275. * to allow common handling of ARM and THUMB debugging */
  276. /* fetch STR r0, [r0] */
  277. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_STR(0, 0), 0, NULL, 0);
  278. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  279. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  280. /* STR r0, [r0] in Memory */
  281. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, r0, 0);
  282. /* MOV r0, r15 fetched, STR in Decode */
  283. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_MOV(0, 15), 0, NULL, 0);
  284. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  285. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_STR(0, 0), 0, NULL, 0);
  286. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  287. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  288. /* nothing fetched, STR r0, [r0] in Memory */
  289. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, pc, 0);
  290. /* use pc-relative LDR to clear r0[1:0] (for switch to ARM mode) */
  291. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_LDR_PCREL(0), 0, NULL, 0);
  292. /* LDR in Decode */
  293. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  294. /* LDR in Execute */
  295. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  296. /* LDR in Memory (to account for interlock) */
  297. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  298. /* fetch BX */
  299. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_BX(0), 0, NULL, 0);
  300. /* NOP fetched, BX in Decode, MOV in Execute */
  301. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  302. /* NOP fetched, BX in Execute (1) */
  303. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  304. if ((retval = jtag_execute_queue()) != ERROR_OK)
  305. {
  306. return;
  307. }
  308. /* fix program counter:
  309. * MOV r0, r15 was the 5th instruction (+8)
  310. * reading PC in Thumb state gives address of instruction + 4
  311. */
  312. *pc -= 0xc;
  313. }
  314. void arm9tdmi_read_core_regs(target_t *target,
  315. uint32_t mask, uint32_t* core_regs[16])
  316. {
  317. int i;
  318. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  319. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  320. /* STMIA r0-15, [r0] at debug speed
  321. * register values will start to appear on 4th DCLK
  322. */
  323. arm9tdmi_clock_out(jtag_info, ARMV4_5_STMIA(0, mask & 0xffff, 0, 0), 0, NULL, 0);
  324. /* fetch NOP, STM in DECODE stage */
  325. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  326. /* fetch NOP, STM in EXECUTE stage (1st cycle) */
  327. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  328. for (i = 0; i <= 15; i++)
  329. {
  330. if (mask & (1 << i))
  331. /* nothing fetched, STM in MEMORY (i'th cycle) */
  332. arm9tdmi_clock_data_in(jtag_info, core_regs[i]);
  333. }
  334. }
  335. static void arm9tdmi_read_core_regs_target_buffer(target_t *target,
  336. uint32_t mask, void* buffer, int size)
  337. {
  338. int i;
  339. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  340. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  341. int be = (target->endianness == TARGET_BIG_ENDIAN) ? 1 : 0;
  342. uint32_t *buf_u32 = buffer;
  343. uint16_t *buf_u16 = buffer;
  344. uint8_t *buf_u8 = buffer;
  345. /* STMIA r0-15, [r0] at debug speed
  346. * register values will start to appear on 4th DCLK
  347. */
  348. arm9tdmi_clock_out(jtag_info, ARMV4_5_STMIA(0, mask & 0xffff, 0, 0), 0, NULL, 0);
  349. /* fetch NOP, STM in DECODE stage */
  350. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  351. /* fetch NOP, STM in EXECUTE stage (1st cycle) */
  352. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  353. for (i = 0; i <= 15; i++)
  354. {
  355. if (mask & (1 << i))
  356. /* nothing fetched, STM in MEMORY (i'th cycle) */
  357. switch (size)
  358. {
  359. case 4:
  360. arm9tdmi_clock_data_in_endianness(jtag_info, buf_u32++, 4, be);
  361. break;
  362. case 2:
  363. arm9tdmi_clock_data_in_endianness(jtag_info, buf_u16++, 2, be);
  364. break;
  365. case 1:
  366. arm9tdmi_clock_data_in_endianness(jtag_info, buf_u8++, 1, be);
  367. break;
  368. }
  369. }
  370. }
  371. static void arm9tdmi_read_xpsr(target_t *target, uint32_t *xpsr, int spsr)
  372. {
  373. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  374. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  375. /* MRS r0, cpsr */
  376. arm9tdmi_clock_out(jtag_info, ARMV4_5_MRS(0, spsr & 1), 0, NULL, 0);
  377. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  378. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  379. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  380. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  381. /* STR r0, [r15] */
  382. arm9tdmi_clock_out(jtag_info, ARMV4_5_STR(0, 15), 0, NULL, 0);
  383. /* fetch NOP, STR in DECODE stage */
  384. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  385. /* fetch NOP, STR in EXECUTE stage (1st cycle) */
  386. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  387. /* nothing fetched, STR in MEMORY */
  388. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, xpsr, 0);
  389. }
  390. static void arm9tdmi_write_xpsr(target_t *target, uint32_t xpsr, int spsr)
  391. {
  392. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  393. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  394. LOG_DEBUG("xpsr: %8.8" PRIx32 ", spsr: %i", xpsr, spsr);
  395. /* MSR1 fetched */
  396. arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM(xpsr & 0xff, 0, 1, spsr), 0, NULL, 0);
  397. /* MSR2 fetched, MSR1 in DECODE */
  398. arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM((xpsr & 0xff00) >> 8, 0xc, 2, spsr), 0, NULL, 0);
  399. /* MSR3 fetched, MSR1 in EXECUTE (1), MSR2 in DECODE */
  400. arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM((xpsr & 0xff0000) >> 16, 0x8, 4, spsr), 0, NULL, 0);
  401. /* nothing fetched, MSR1 in EXECUTE (2) */
  402. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  403. /* nothing fetched, MSR1 in EXECUTE (3) */
  404. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  405. /* MSR4 fetched, MSR2 in EXECUTE (1), MSR3 in DECODE */
  406. arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM((xpsr & 0xff000000) >> 24, 0x4, 8, spsr), 0, NULL, 0);
  407. /* nothing fetched, MSR2 in EXECUTE (2) */
  408. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  409. /* nothing fetched, MSR2 in EXECUTE (3) */
  410. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  411. /* NOP fetched, MSR3 in EXECUTE (1), MSR4 in DECODE */
  412. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  413. /* nothing fetched, MSR3 in EXECUTE (2) */
  414. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  415. /* nothing fetched, MSR3 in EXECUTE (3) */
  416. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  417. /* NOP fetched, MSR4 in EXECUTE (1) */
  418. /* last MSR writes flags, which takes only one cycle */
  419. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  420. }
  421. static void arm9tdmi_write_xpsr_im8(target_t *target,
  422. uint8_t xpsr_im, int rot, int spsr)
  423. {
  424. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  425. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  426. LOG_DEBUG("xpsr_im: %2.2x, rot: %i, spsr: %i", xpsr_im, rot, spsr);
  427. /* MSR fetched */
  428. arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM(xpsr_im, rot, 1, spsr), 0, NULL, 0);
  429. /* NOP fetched, MSR in DECODE */
  430. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  431. /* NOP fetched, MSR in EXECUTE (1) */
  432. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  433. /* rot == 4 writes flags, which takes only one cycle */
  434. if (rot != 4)
  435. {
  436. /* nothing fetched, MSR in EXECUTE (2) */
  437. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  438. /* nothing fetched, MSR in EXECUTE (3) */
  439. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  440. }
  441. }
  442. void arm9tdmi_write_core_regs(target_t *target,
  443. uint32_t mask, uint32_t core_regs[16])
  444. {
  445. int i;
  446. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  447. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  448. /* LDMIA r0-15, [r0] at debug speed
  449. * register values will start to appear on 4th DCLK
  450. */
  451. arm9tdmi_clock_out(jtag_info, ARMV4_5_LDMIA(0, mask & 0xffff, 0, 0), 0, NULL, 0);
  452. /* fetch NOP, LDM in DECODE stage */
  453. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  454. /* fetch NOP, LDM in EXECUTE stage (1st cycle) */
  455. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  456. for (i = 0; i <= 15; i++)
  457. {
  458. if (mask & (1 << i))
  459. /* nothing fetched, LDM still in EXECUTE (1 + i cycle) */
  460. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, core_regs[i], NULL, 0);
  461. }
  462. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  463. }
  464. void arm9tdmi_load_word_regs(target_t *target, uint32_t mask)
  465. {
  466. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  467. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  468. /* put system-speed load-multiple into the pipeline */
  469. arm9tdmi_clock_out(jtag_info, ARMV4_5_LDMIA(0, mask & 0xffff, 0, 1), 0, NULL, 0);
  470. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
  471. }
  472. void arm9tdmi_load_hword_reg(target_t *target, int num)
  473. {
  474. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  475. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  476. /* put system-speed load half-word into the pipeline */
  477. arm9tdmi_clock_out(jtag_info, ARMV4_5_LDRH_IP(num, 0), 0, NULL, 0);
  478. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
  479. }
  480. void arm9tdmi_load_byte_reg(target_t *target, int num)
  481. {
  482. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  483. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  484. /* put system-speed load byte into the pipeline */
  485. arm9tdmi_clock_out(jtag_info, ARMV4_5_LDRB_IP(num, 0), 0, NULL, 0);
  486. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
  487. }
  488. void arm9tdmi_store_word_regs(target_t *target, uint32_t mask)
  489. {
  490. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  491. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  492. /* put system-speed store-multiple into the pipeline */
  493. arm9tdmi_clock_out(jtag_info, ARMV4_5_STMIA(0, mask, 0, 1), 0, NULL, 0);
  494. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
  495. }
  496. void arm9tdmi_store_hword_reg(target_t *target, int num)
  497. {
  498. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  499. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  500. /* put system-speed store half-word into the pipeline */
  501. arm9tdmi_clock_out(jtag_info, ARMV4_5_STRH_IP(num, 0), 0, NULL, 0);
  502. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
  503. }
  504. void arm9tdmi_store_byte_reg(target_t *target, int num)
  505. {
  506. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  507. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  508. /* put system-speed store byte into the pipeline */
  509. arm9tdmi_clock_out(jtag_info, ARMV4_5_STRB_IP(num, 0), 0, NULL, 0);
  510. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
  511. }
  512. static void arm9tdmi_write_pc(target_t *target, uint32_t pc)
  513. {
  514. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  515. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  516. /* LDMIA r0-15, [r0] at debug speed
  517. * register values will start to appear on 4th DCLK
  518. */
  519. arm9tdmi_clock_out(jtag_info, ARMV4_5_LDMIA(0, 0x8000, 0, 0), 0, NULL, 0);
  520. /* fetch NOP, LDM in DECODE stage */
  521. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  522. /* fetch NOP, LDM in EXECUTE stage (1st cycle) */
  523. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  524. /* nothing fetched, LDM in EXECUTE stage (2nd cycle) (output data) */
  525. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, pc, NULL, 0);
  526. /* nothing fetched, LDM in EXECUTE stage (3rd cycle) */
  527. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  528. /* fetch NOP, LDM in EXECUTE stage (4th cycle) */
  529. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  530. /* fetch NOP, LDM in EXECUTE stage (5th cycle) */
  531. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  532. }
  533. void arm9tdmi_branch_resume(target_t *target)
  534. {
  535. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  536. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  537. arm9tdmi_clock_out(jtag_info, ARMV4_5_B(0xfffffc, 0), 0, NULL, 0);
  538. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
  539. }
  540. static void arm9tdmi_branch_resume_thumb(target_t *target)
  541. {
  542. LOG_DEBUG("-");
  543. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  544. struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
  545. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  546. reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
  547. /* LDMIA r0-15, [r0] at debug speed
  548. * register values will start to appear on 4th DCLK
  549. */
  550. arm9tdmi_clock_out(jtag_info, ARMV4_5_LDMIA(0, 0x1, 0, 0), 0, NULL, 0);
  551. /* fetch NOP, LDM in DECODE stage */
  552. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  553. /* fetch NOP, LDM in EXECUTE stage (1st cycle) */
  554. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  555. /* nothing fetched, LDM in EXECUTE stage (2nd cycle) */
  556. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32) | 1, NULL, 0);
  557. /* nothing fetched, LDM in EXECUTE stage (3rd cycle) */
  558. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  559. /* Branch and eXchange */
  560. arm9tdmi_clock_out(jtag_info, ARMV4_5_BX(0), 0, NULL, 0);
  561. embeddedice_read_reg(dbg_stat);
  562. /* fetch NOP, BX in DECODE stage */
  563. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  564. embeddedice_read_reg(dbg_stat);
  565. /* fetch NOP, BX in EXECUTE stage (1st cycle) */
  566. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  567. /* target is now in Thumb state */
  568. embeddedice_read_reg(dbg_stat);
  569. /* load r0 value, MOV_IM in Decode*/
  570. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_LDR_PCREL(0), 0, NULL, 0);
  571. /* fetch NOP, LDR in Decode, MOV_IM in Execute */
  572. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  573. /* fetch NOP, LDR in Execute */
  574. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  575. /* nothing fetched, LDR in EXECUTE stage (2nd cycle) */
  576. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, buf_get_u32(armv4_5->core_cache->reg_list[0].value, 0, 32), NULL, 0);
  577. /* nothing fetched, LDR in EXECUTE stage (3rd cycle) */
  578. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  579. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  580. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  581. embeddedice_read_reg(dbg_stat);
  582. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_B(0x7f7), 0, NULL, 1);
  583. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  584. }
  585. void arm9tdmi_enable_single_step(target_t *target, uint32_t next_pc)
  586. {
  587. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  588. if (arm7_9->has_single_step)
  589. {
  590. buf_set_u32(arm7_9->eice_cache->reg_list[EICE_DBG_CTRL].value, 3, 1, 1);
  591. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_DBG_CTRL]);
  592. }
  593. else
  594. {
  595. arm7_9_enable_eice_step(target, next_pc);
  596. }
  597. }
  598. void arm9tdmi_disable_single_step(target_t *target)
  599. {
  600. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  601. if (arm7_9->has_single_step)
  602. {
  603. buf_set_u32(arm7_9->eice_cache->reg_list[EICE_DBG_CTRL].value, 3, 1, 0);
  604. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_DBG_CTRL]);
  605. }
  606. else
  607. {
  608. arm7_9_disable_eice_step(target);
  609. }
  610. }
  611. static void arm9tdmi_build_reg_cache(target_t *target)
  612. {
  613. reg_cache_t **cache_p = register_get_last_cache_p(&target->reg_cache);
  614. struct armv4_5_common_s *armv4_5 = target_to_armv4_5(target);
  615. (*cache_p) = armv4_5_build_reg_cache(target, armv4_5);
  616. armv4_5->core_cache = (*cache_p);
  617. }
  618. int arm9tdmi_examine(struct target_s *target)
  619. {
  620. int retval;
  621. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  622. if (!target_was_examined(target))
  623. {
  624. reg_cache_t **cache_p = register_get_last_cache_p(&target->reg_cache);
  625. reg_cache_t *t;
  626. /* one extra register (vector catch) */
  627. t = embeddedice_build_reg_cache(target, arm7_9);
  628. if (t == NULL)
  629. return ERROR_FAIL;
  630. (*cache_p) = t;
  631. arm7_9->eice_cache = (*cache_p);
  632. if (arm7_9->armv4_5_common.etm)
  633. {
  634. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  635. (*cache_p)->next = etm_build_reg_cache(target,
  636. jtag_info, arm7_9->armv4_5_common.etm);
  637. arm7_9->armv4_5_common.etm->reg_cache = (*cache_p)->next;
  638. }
  639. target_set_examined(target);
  640. }
  641. if ((retval = embeddedice_setup(target)) != ERROR_OK)
  642. return retval;
  643. if ((retval = arm7_9_setup(target)) != ERROR_OK)
  644. return retval;
  645. if (arm7_9->armv4_5_common.etm)
  646. {
  647. if ((retval = etm_setup(target)) != ERROR_OK)
  648. return retval;
  649. }
  650. return ERROR_OK;
  651. }
  652. int arm9tdmi_init_target(struct command_context_s *cmd_ctx,
  653. struct target_s *target)
  654. {
  655. arm9tdmi_build_reg_cache(target);
  656. return ERROR_OK;
  657. }
  658. int arm9tdmi_init_arch_info(target_t *target, arm9tdmi_common_t *arm9tdmi, struct jtag_tap *tap)
  659. {
  660. armv4_5_common_t *armv4_5;
  661. arm7_9_common_t *arm7_9;
  662. arm7_9 = &arm9tdmi->arm7_9_common;
  663. armv4_5 = &arm7_9->armv4_5_common;
  664. /* prepare JTAG information for the new target */
  665. arm7_9->jtag_info.tap = tap;
  666. arm7_9->jtag_info.scann_size = 5;
  667. /* register arch-specific functions */
  668. arm7_9->examine_debug_reason = arm9tdmi_examine_debug_reason;
  669. arm7_9->change_to_arm = arm9tdmi_change_to_arm;
  670. arm7_9->read_core_regs = arm9tdmi_read_core_regs;
  671. arm7_9->read_core_regs_target_buffer = arm9tdmi_read_core_regs_target_buffer;
  672. arm7_9->read_xpsr = arm9tdmi_read_xpsr;
  673. arm7_9->write_xpsr = arm9tdmi_write_xpsr;
  674. arm7_9->write_xpsr_im8 = arm9tdmi_write_xpsr_im8;
  675. arm7_9->write_core_regs = arm9tdmi_write_core_regs;
  676. arm7_9->load_word_regs = arm9tdmi_load_word_regs;
  677. arm7_9->load_hword_reg = arm9tdmi_load_hword_reg;
  678. arm7_9->load_byte_reg = arm9tdmi_load_byte_reg;
  679. arm7_9->store_word_regs = arm9tdmi_store_word_regs;
  680. arm7_9->store_hword_reg = arm9tdmi_store_hword_reg;
  681. arm7_9->store_byte_reg = arm9tdmi_store_byte_reg;
  682. arm7_9->write_pc = arm9tdmi_write_pc;
  683. arm7_9->branch_resume = arm9tdmi_branch_resume;
  684. arm7_9->branch_resume_thumb = arm9tdmi_branch_resume_thumb;
  685. arm7_9->enable_single_step = arm9tdmi_enable_single_step;
  686. arm7_9->disable_single_step = arm9tdmi_disable_single_step;
  687. arm7_9->post_debug_entry = NULL;
  688. arm7_9->pre_restore_context = NULL;
  689. arm7_9->post_restore_context = NULL;
  690. /* initialize arch-specific breakpoint handling */
  691. arm7_9->arm_bkpt = 0xdeeedeee;
  692. arm7_9->thumb_bkpt = 0xdeee;
  693. arm7_9->dbgreq_adjust_pc = 3;
  694. arm7_9_init_arch_info(target, arm7_9);
  695. /* override use of DBGRQ, this is safe on ARM9TDMI */
  696. arm7_9->use_dbgrq = 1;
  697. /* all ARM9s have the vector catch register */
  698. arm7_9->has_vector_catch = 1;
  699. return ERROR_OK;
  700. }
  701. static int arm9tdmi_target_create(struct target_s *target, Jim_Interp *interp)
  702. {
  703. arm9tdmi_common_t *arm9tdmi = calloc(1,sizeof(arm9tdmi_common_t));
  704. arm9tdmi_init_arch_info(target, arm9tdmi, target->tap);
  705. arm9tdmi->arm7_9_common.armv4_5_common.is_armv4 = true;
  706. return ERROR_OK;
  707. }
  708. COMMAND_HANDLER(handle_arm9tdmi_catch_vectors_command)
  709. {
  710. target_t *target = get_current_target(cmd_ctx);
  711. struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
  712. reg_t *vector_catch;
  713. uint32_t vector_catch_value;
  714. /* it's uncommon, but some ARM7 chips can support this */
  715. if (arm7_9->common_magic != ARM7_9_COMMON_MAGIC
  716. || !arm7_9->has_vector_catch) {
  717. command_print(cmd_ctx, "target doesn't have EmbeddedICE "
  718. "with vector_catch");
  719. return ERROR_TARGET_INVALID;
  720. }
  721. vector_catch = &arm7_9->eice_cache->reg_list[EICE_VEC_CATCH];
  722. /* read the vector catch register if necessary */
  723. if (!vector_catch->valid)
  724. embeddedice_read_reg(vector_catch);
  725. /* get the current setting */
  726. vector_catch_value = buf_get_u32(vector_catch->value, 0, 8);
  727. if (argc > 0)
  728. {
  729. vector_catch_value = 0x0;
  730. if (strcmp(args[0], "all") == 0)
  731. {
  732. vector_catch_value = 0xdf;
  733. }
  734. else if (strcmp(args[0], "none") == 0)
  735. {
  736. /* do nothing */
  737. }
  738. else
  739. {
  740. for (unsigned i = 0; i < argc; i++)
  741. {
  742. /* go through list of vectors */
  743. unsigned j;
  744. for (j = 0; arm9tdmi_vectors[j].name; j++)
  745. {
  746. if (strcmp(args[i], arm9tdmi_vectors[j].name) == 0)
  747. {
  748. vector_catch_value |= arm9tdmi_vectors[j].value;
  749. break;
  750. }
  751. }
  752. /* complain if vector wasn't found */
  753. if (!arm9tdmi_vectors[j].name)
  754. {
  755. command_print(cmd_ctx, "vector '%s' not found, leaving current setting unchanged", args[i]);
  756. /* reread current setting */
  757. vector_catch_value = buf_get_u32(
  758. vector_catch->value,
  759. 0, 8);
  760. break;
  761. }
  762. }
  763. }
  764. /* store new settings */
  765. buf_set_u32(vector_catch->value, 0, 8, vector_catch_value);
  766. embeddedice_store_reg(vector_catch);
  767. }
  768. /* output current settings */
  769. for (unsigned i = 0; arm9tdmi_vectors[i].name; i++) {
  770. command_print(cmd_ctx, "%s: %s", arm9tdmi_vectors[i].name,
  771. (vector_catch_value & arm9tdmi_vectors[i].value)
  772. ? "catch" : "don't catch");
  773. }
  774. return ERROR_OK;
  775. }
  776. int arm9tdmi_register_commands(struct command_context_s *cmd_ctx)
  777. {
  778. int retval;
  779. command_t *arm9tdmi_cmd;
  780. retval = arm7_9_register_commands(cmd_ctx);
  781. arm9tdmi_cmd = register_command(cmd_ctx, NULL, "arm9",
  782. NULL, COMMAND_ANY,
  783. "arm9 specific commands");
  784. register_command(cmd_ctx, arm9tdmi_cmd, "vector_catch",
  785. handle_arm9tdmi_catch_vectors_command, COMMAND_EXEC,
  786. "arm9 vector_catch [all|none|reset|undef|swi|pabt|dabt|irq|fiq] ...");
  787. return retval;
  788. }
  789. /** Holds methods for ARM9TDMI targets. */
  790. target_type_t arm9tdmi_target =
  791. {
  792. .name = "arm9tdmi",
  793. .poll = arm7_9_poll,
  794. .arch_state = armv4_5_arch_state,
  795. .target_request_data = arm7_9_target_request_data,
  796. .halt = arm7_9_halt,
  797. .resume = arm7_9_resume,
  798. .step = arm7_9_step,
  799. .assert_reset = arm7_9_assert_reset,
  800. .deassert_reset = arm7_9_deassert_reset,
  801. .soft_reset_halt = arm7_9_soft_reset_halt,
  802. .get_gdb_reg_list = armv4_5_get_gdb_reg_list,
  803. .read_memory = arm7_9_read_memory,
  804. .write_memory = arm7_9_write_memory,
  805. .bulk_write_memory = arm7_9_bulk_write_memory,
  806. .checksum_memory = arm7_9_checksum_memory,
  807. .blank_check_memory = arm7_9_blank_check_memory,
  808. .run_algorithm = armv4_5_run_algorithm,
  809. .add_breakpoint = arm7_9_add_breakpoint,
  810. .remove_breakpoint = arm7_9_remove_breakpoint,
  811. .add_watchpoint = arm7_9_add_watchpoint,
  812. .remove_watchpoint = arm7_9_remove_watchpoint,
  813. .register_commands = arm9tdmi_register_commands,
  814. .target_create = arm9tdmi_target_create,
  815. .init_target = arm9tdmi_init_target,
  816. .examine = arm9tdmi_examine,
  817. };