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.
 
 
 
 
 
 

717 lines
24 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2008 by Marvell Semiconductors, Inc. *
  3. * Written by Nicolas Pitre <nico@marvell.com> *
  4. * *
  5. * Copyright (C) 2008 by Hongtao Zheng *
  6. * hontor@126.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. /*
  24. * Marvell Feroceon support, including Orion and Kirkwood SOCs.
  25. *
  26. * The Feroceon core mimics the ARM926 ICE interface with the following
  27. * differences:
  28. *
  29. * - the MOE (method of entry) reporting is not implemented
  30. *
  31. * - breakpoint/watchpoint comparator #1 is seemingly not implemented
  32. *
  33. * - due to a different pipeline implementation, some injected debug
  34. * instruction sequences have to be somewhat different
  35. *
  36. * Other issues:
  37. *
  38. * - asserting DBGRQ doesn't work if target is looping on the undef vector
  39. *
  40. * - the EICE version signature in the COMMS_CTL reg is next to the flow bits
  41. * not at the top, and rather meaningless due to existing discrepencies
  42. *
  43. * - the DCC channel is half duplex (only one FIFO for both directions) with
  44. * seemingly no proper flow control.
  45. */
  46. #ifdef HAVE_CONFIG_H
  47. #include "config.h"
  48. #endif
  49. #include "arm926ejs.h"
  50. #include "target_type.h"
  51. int feroceon_examine(struct target_s *target);
  52. int feroceon_target_create(struct target_s *target, Jim_Interp *interp);
  53. int feroceon_bulk_write_memory(target_t *target, u32 address, u32 count, u8 *buffer);
  54. int feroceon_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
  55. int feroceon_quit(void);
  56. int feroceon_assert_reset(target_t *target)
  57. {
  58. armv4_5_common_t *armv4_5 = target->arch_info;
  59. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  60. int ud = arm7_9->use_dbgrq;
  61. arm7_9->use_dbgrq = 0;
  62. if (target->reset_halt)
  63. arm7_9_halt(target);
  64. arm7_9->use_dbgrq = ud;
  65. return arm7_9_assert_reset(target);
  66. }
  67. target_type_t feroceon_target =
  68. {
  69. .name = "feroceon",
  70. .poll = arm7_9_poll,
  71. .arch_state = arm926ejs_arch_state,
  72. .target_request_data = arm7_9_target_request_data,
  73. .halt = arm7_9_halt,
  74. .resume = arm7_9_resume,
  75. .step = arm7_9_step,
  76. .assert_reset = feroceon_assert_reset,
  77. .deassert_reset = arm7_9_deassert_reset,
  78. .soft_reset_halt = arm926ejs_soft_reset_halt,
  79. .get_gdb_reg_list = armv4_5_get_gdb_reg_list,
  80. .read_memory = arm7_9_read_memory,
  81. .write_memory = arm926ejs_write_memory,
  82. .bulk_write_memory = feroceon_bulk_write_memory,
  83. .checksum_memory = arm7_9_checksum_memory,
  84. .blank_check_memory = arm7_9_blank_check_memory,
  85. .run_algorithm = armv4_5_run_algorithm,
  86. .add_breakpoint = arm7_9_add_breakpoint,
  87. .remove_breakpoint = arm7_9_remove_breakpoint,
  88. .add_watchpoint = arm7_9_add_watchpoint,
  89. .remove_watchpoint = arm7_9_remove_watchpoint,
  90. .register_commands = arm926ejs_register_commands,
  91. .target_create = feroceon_target_create,
  92. .init_target = feroceon_init_target,
  93. .examine = feroceon_examine,
  94. .quit = feroceon_quit
  95. };
  96. int feroceon_dummy_clock_out(arm_jtag_t *jtag_info, u32 instr)
  97. {
  98. scan_field_t fields[3];
  99. u8 out_buf[4];
  100. u8 instr_buf[4];
  101. u8 sysspeed_buf = 0x0;
  102. /* prepare buffer */
  103. buf_set_u32(out_buf, 0, 32, 0);
  104. buf_set_u32(instr_buf, 0, 32, flip_u32(instr, 32));
  105. jtag_set_end_state(TAP_DRPAUSE);
  106. arm_jtag_scann(jtag_info, 0x1);
  107. arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
  108. fields[0].tap = jtag_info->tap;
  109. fields[0].num_bits = 32;
  110. fields[0].out_value = out_buf;
  111. fields[0].in_value = NULL;
  112. fields[1].tap = jtag_info->tap;
  113. fields[1].num_bits = 3;
  114. fields[1].out_value = &sysspeed_buf;
  115. fields[1].in_value = NULL;
  116. fields[2].tap = jtag_info->tap;
  117. fields[2].num_bits = 32;
  118. fields[2].out_value = instr_buf;
  119. fields[2].in_value = NULL;
  120. jtag_add_dr_scan(3, fields, jtag_get_end_state());
  121. /* no jtag_add_runtest(0, jtag_get_end_state()) here */
  122. return ERROR_OK;
  123. }
  124. void feroceon_change_to_arm(target_t *target, u32 *r0, u32 *pc)
  125. {
  126. armv4_5_common_t *armv4_5 = target->arch_info;
  127. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  128. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  129. /*
  130. * save r0 before using it and put system in ARM state
  131. * to allow common handling of ARM and THUMB debugging
  132. */
  133. feroceon_dummy_clock_out(jtag_info, ARMV4_5_T_NOP);
  134. feroceon_dummy_clock_out(jtag_info, ARMV4_5_T_NOP);
  135. feroceon_dummy_clock_out(jtag_info, ARMV4_5_T_NOP);
  136. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_STR(0, 0), 0, NULL, 0);
  137. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  138. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  139. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, r0, 0);
  140. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  141. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  142. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_MOV(0, 15), 0, NULL, 0);
  143. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_STR(0, 0), 0, NULL, 0);
  144. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  145. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  146. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, pc, 0);
  147. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  148. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  149. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_BX(15), 0, NULL, 0);
  150. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  151. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  152. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  153. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  154. jtag_execute_queue();
  155. /*
  156. * fix program counter:
  157. * MOV R0, PC was the 7th instruction (+12)
  158. * reading PC in Thumb state gives address of instruction + 4
  159. */
  160. *pc -= (12 + 4);
  161. }
  162. void feroceon_read_core_regs(target_t *target, u32 mask, u32* core_regs[16])
  163. {
  164. int i;
  165. armv4_5_common_t *armv4_5 = target->arch_info;
  166. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  167. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  168. arm9tdmi_clock_out(jtag_info, ARMV4_5_STMIA(0, mask & 0xffff, 0, 0), 0, NULL, 0);
  169. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  170. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  171. for (i = 0; i <= 15; i++)
  172. if (mask & (1 << i))
  173. arm9tdmi_clock_data_in(jtag_info, core_regs[i]);
  174. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  175. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  176. }
  177. void feroceon_read_core_regs_target_buffer(target_t *target, u32 mask, void* buffer, int size)
  178. {
  179. int i;
  180. armv4_5_common_t *armv4_5 = target->arch_info;
  181. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  182. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  183. int be = (target->endianness == TARGET_BIG_ENDIAN) ? 1 : 0;
  184. u32 *buf_u32 = buffer;
  185. u16 *buf_u16 = buffer;
  186. u8 *buf_u8 = buffer;
  187. arm9tdmi_clock_out(jtag_info, ARMV4_5_STMIA(0, mask & 0xffff, 0, 0), 0, NULL, 0);
  188. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  189. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  190. for (i = 0; i <= 15; i++)
  191. {
  192. if (mask & (1 << i)) {
  193. switch (size)
  194. {
  195. case 4:
  196. arm9tdmi_clock_data_in_endianness(jtag_info, buf_u32++, 4, be);
  197. break;
  198. case 2:
  199. arm9tdmi_clock_data_in_endianness(jtag_info, buf_u16++, 2, be);
  200. break;
  201. case 1:
  202. arm9tdmi_clock_data_in_endianness(jtag_info, buf_u8++, 1, be);
  203. break;
  204. }
  205. }
  206. }
  207. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  208. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  209. }
  210. void feroceon_read_xpsr(target_t *target, u32 *xpsr, int spsr)
  211. {
  212. armv4_5_common_t *armv4_5 = target->arch_info;
  213. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  214. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  215. arm9tdmi_clock_out(jtag_info, ARMV4_5_MRS(0, spsr & 1), 0, NULL, 0);
  216. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  217. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  218. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  219. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  220. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  221. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  222. arm9tdmi_clock_out(jtag_info, ARMV4_5_STMIA(0, 1, 0, 0), 0, NULL, 0);
  223. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  224. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  225. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, xpsr, 0);
  226. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  227. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  228. }
  229. void feroceon_write_xpsr(target_t *target, u32 xpsr, int spsr)
  230. {
  231. armv4_5_common_t *armv4_5 = target->arch_info;
  232. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  233. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  234. LOG_DEBUG("xpsr: %8.8x, spsr: %i", xpsr, spsr);
  235. arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM(xpsr & 0xff, 0, 1, spsr), 0, NULL, 0);
  236. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  237. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  238. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  239. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  240. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  241. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  242. arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM((xpsr & 0xff00) >> 8, 0xc, 2, spsr), 0, NULL, 0);
  243. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  244. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  245. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  246. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  247. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  248. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  249. arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM((xpsr & 0xff0000) >> 16, 0x8, 4, spsr), 0, NULL, 0);
  250. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  251. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  252. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  253. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  254. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  255. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  256. arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM((xpsr & 0xff000000) >> 24, 0x4, 8, spsr), 0, NULL, 0);
  257. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  258. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  259. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  260. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  261. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  262. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  263. }
  264. void feroceon_write_xpsr_im8(target_t *target, u8 xpsr_im, int rot, int spsr)
  265. {
  266. armv4_5_common_t *armv4_5 = target->arch_info;
  267. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  268. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  269. LOG_DEBUG("xpsr_im: %2.2x, rot: %i, spsr: %i", xpsr_im, rot, spsr);
  270. arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM(xpsr_im, rot, 1, spsr), 0, NULL, 0);
  271. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  272. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  273. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  274. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  275. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  276. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  277. }
  278. void feroceon_write_core_regs(target_t *target, u32 mask, u32 core_regs[16])
  279. {
  280. int i;
  281. armv4_5_common_t *armv4_5 = target->arch_info;
  282. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  283. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  284. arm9tdmi_clock_out(jtag_info, ARMV4_5_LDMIA(0, mask & 0xffff, 0, 0), 0, NULL, 0);
  285. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  286. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  287. for (i = 0; i <= 15; i++)
  288. if (mask & (1 << i))
  289. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, core_regs[i], NULL, 0);
  290. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  291. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  292. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  293. }
  294. void feroceon_branch_resume(target_t *target)
  295. {
  296. armv4_5_common_t *armv4_5 = target->arch_info;
  297. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  298. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  299. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  300. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  301. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  302. arm9tdmi_clock_out(jtag_info, ARMV4_5_B(0xfffff9, 0), 0, NULL, 0);
  303. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
  304. arm7_9->need_bypass_before_restart = 1;
  305. }
  306. void feroceon_branch_resume_thumb(target_t *target)
  307. {
  308. LOG_DEBUG("-");
  309. armv4_5_common_t *armv4_5 = target->arch_info;
  310. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  311. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  312. u32 r0 = buf_get_u32(armv4_5->core_cache->reg_list[0].value, 0, 32);
  313. u32 pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
  314. (void)(r0); // use R0...
  315. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  316. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  317. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  318. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  319. arm9tdmi_clock_out(jtag_info, 0xE28F0001, 0, NULL, 0); // add r0,pc,#1
  320. arm9tdmi_clock_out(jtag_info, ARMV4_5_BX(0), 0, NULL, 0);
  321. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  322. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  323. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_LDMIA(0, 0x1), 0, NULL, 0);
  324. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  325. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  326. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, pc, NULL, 0);
  327. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  328. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
  329. pc = (pc & 2) >> 1;
  330. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_B(0x7e9 + pc), 0, NULL, 0);
  331. arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 1);
  332. arm7_9->need_bypass_before_restart = 1;
  333. }
  334. int feroceon_read_cp15(target_t *target, u32 op1, u32 op2, u32 CRn, u32 CRm, u32 *value)
  335. {
  336. armv4_5_common_t *armv4_5 = target->arch_info;
  337. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  338. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  339. int err;
  340. arm9tdmi_clock_out(jtag_info, ARMV4_5_MRC(15, op1, 0, CRn, CRm, op2), 0, NULL, 0);
  341. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
  342. err = arm7_9_execute_sys_speed(target);
  343. if (err != ERROR_OK)
  344. return err;
  345. arm9tdmi_clock_out(jtag_info, ARMV4_5_STMIA(0, 1, 0, 0), 0, NULL, 0);
  346. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  347. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  348. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, value, 0);
  349. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  350. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  351. return jtag_execute_queue();
  352. }
  353. int feroceon_write_cp15(target_t *target, u32 op1, u32 op2, u32 CRn, u32 CRm, u32 value)
  354. {
  355. armv4_5_common_t *armv4_5 = target->arch_info;
  356. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  357. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  358. arm9tdmi_clock_out(jtag_info, ARMV4_5_LDMIA(0, 1, 0, 0), 0, NULL, 0);
  359. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  360. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  361. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, value, NULL, 0);
  362. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  363. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  364. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
  365. arm9tdmi_clock_out(jtag_info, ARMV4_5_MCR(15, op1, 0, CRn, CRm, op2), 0, NULL, 0);
  366. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
  367. return arm7_9_execute_sys_speed(target);
  368. }
  369. void feroceon_set_dbgrq(target_t *target)
  370. {
  371. armv4_5_common_t *armv4_5 = target->arch_info;
  372. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  373. reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
  374. buf_set_u32(dbg_ctrl->value, 0, 8, 2);
  375. embeddedice_store_reg(dbg_ctrl);
  376. }
  377. void feroceon_enable_single_step(target_t *target, u32 next_pc)
  378. {
  379. armv4_5_common_t *armv4_5 = target->arch_info;
  380. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  381. /* set a breakpoint there */
  382. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], next_pc);
  383. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0);
  384. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
  385. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x100);
  386. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xf7);
  387. }
  388. void feroceon_disable_single_step(target_t *target)
  389. {
  390. armv4_5_common_t *armv4_5 = target->arch_info;
  391. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  392. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE]);
  393. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK]);
  394. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK]);
  395. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK]);
  396. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
  397. }
  398. int feroceon_examine_debug_reason(target_t *target)
  399. {
  400. /* the MOE is not implemented */
  401. if (target->debug_reason != DBG_REASON_SINGLESTEP)
  402. {
  403. target->debug_reason = DBG_REASON_DBGRQ;
  404. }
  405. return ERROR_OK;
  406. }
  407. int feroceon_bulk_write_memory(target_t *target, u32 address, u32 count, u8 *buffer)
  408. {
  409. int retval;
  410. armv4_5_common_t *armv4_5 = target->arch_info;
  411. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  412. enum armv4_5_state core_state = armv4_5->core_state;
  413. u32 x, flip, shift, save[7];
  414. u32 i;
  415. /*
  416. * We can't use the dcc flow control bits, so let's transfer data
  417. * with 31 bits and flip the MSB each time a new data word is sent.
  418. */
  419. static u32 dcc_code[] =
  420. {
  421. 0xee115e10, /* 3: mrc p14, 0, r5, c1, c0, 0 */
  422. 0xe3a0301e, /* 1: mov r3, #30 */
  423. 0xe3a04002, /* mov r4, #2 */
  424. 0xee111e10, /* 2: mrc p14, 0, r1, c1, c0, 0 */
  425. 0xe1310005, /* teq r1, r5 */
  426. 0x0afffffc, /* beq 1b */
  427. 0xe1a05001, /* mov r5, r1 */
  428. 0xe1a01081, /* mov r1, r1, lsl #1 */
  429. 0xee112e10, /* 3: mrc p14, 0, r2, c1, c0, 0 */
  430. 0xe1320005, /* teq r2, r5 */
  431. 0x0afffffc, /* beq 3b */
  432. 0xe1a05002, /* mov r5, r2 */
  433. 0xe3c22102, /* bic r2, r2, #0x80000000 */
  434. 0xe1811332, /* orr r1, r1, r2, lsr r3 */
  435. 0xe2533001, /* subs r3, r3, #1 */
  436. 0xe4801004, /* str r1, [r0], #4 */
  437. 0xe1a01412, /* mov r1, r2, lsl r4 */
  438. 0xe2844001, /* add r4, r4, #1 */
  439. 0x4affffed, /* bmi 1b */
  440. 0xeafffff3, /* b 3b */
  441. };
  442. u32 dcc_size = sizeof(dcc_code);
  443. if (!arm7_9->dcc_downloads)
  444. return target_write_memory(target, address, 4, count, buffer);
  445. /* regrab previously allocated working_area, or allocate a new one */
  446. if (!arm7_9->dcc_working_area)
  447. {
  448. u8 dcc_code_buf[dcc_size];
  449. /* make sure we have a working area */
  450. if (target_alloc_working_area(target, dcc_size, &arm7_9->dcc_working_area) != ERROR_OK)
  451. {
  452. LOG_INFO("no working area available, falling back to memory writes");
  453. return target_write_memory(target, address, 4, count, buffer);
  454. }
  455. /* copy target instructions to target endianness */
  456. for (i = 0; i < dcc_size/4; i++)
  457. target_buffer_set_u32(target, dcc_code_buf + i*4, dcc_code[i]);
  458. /* write DCC code to working area */
  459. if((retval = target_write_memory(target, arm7_9->dcc_working_area->address, 4, dcc_size/4, dcc_code_buf)) != ERROR_OK)
  460. {
  461. return retval;
  462. }
  463. }
  464. /* backup clobbered processor state */
  465. for (i = 0; i <= 5; i++)
  466. save[i] = buf_get_u32(armv4_5->core_cache->reg_list[i].value, 0, 32);
  467. save[i] = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
  468. /* set up target address in r0 */
  469. buf_set_u32(armv4_5->core_cache->reg_list[0].value, 0, 32, address);
  470. armv4_5->core_cache->reg_list[0].valid = 1;
  471. armv4_5->core_cache->reg_list[0].dirty = 1;
  472. armv4_5->core_state = ARMV4_5_STATE_ARM;
  473. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], 0);
  474. arm7_9_resume(target, 0, arm7_9->dcc_working_area->address, 1, 1);
  475. /* send data over */
  476. x = 0;
  477. flip = 0;
  478. shift = 1;
  479. for (i = 0; i < count; i++)
  480. {
  481. u32 y = target_buffer_get_u32(target, buffer);
  482. u32 z = (x >> 1) | (y >> shift) | (flip ^= 0x80000000);
  483. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], z);
  484. x = y << (32 - shift);
  485. if (++shift >= 32 || i + 1 >= count)
  486. {
  487. z = (x >> 1) | (flip ^= 0x80000000);
  488. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], z);
  489. x = 0;
  490. shift = 1;
  491. }
  492. buffer += 4;
  493. }
  494. target_halt(target);
  495. while (target->state != TARGET_HALTED)
  496. target_poll(target);
  497. /* restore target state */
  498. for (i = 0; i <= 5; i++)
  499. {
  500. buf_set_u32(armv4_5->core_cache->reg_list[i].value, 0, 32, save[i]);
  501. armv4_5->core_cache->reg_list[i].valid = 1;
  502. armv4_5->core_cache->reg_list[i].dirty = 1;
  503. }
  504. buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, save[i]);
  505. armv4_5->core_cache->reg_list[15].valid = 1;
  506. armv4_5->core_cache->reg_list[15].dirty = 1;
  507. armv4_5->core_state = core_state;
  508. return ERROR_OK;
  509. }
  510. int feroceon_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
  511. {
  512. arm9tdmi_init_target(cmd_ctx, target);
  513. return ERROR_OK;
  514. }
  515. int feroceon_quit(void)
  516. {
  517. return ERROR_OK;
  518. }
  519. int feroceon_target_create(struct target_s *target, Jim_Interp *interp)
  520. {
  521. armv4_5_common_t *armv4_5;
  522. arm7_9_common_t *arm7_9;
  523. arm926ejs_common_t *arm926ejs = calloc(1,sizeof(arm926ejs_common_t));
  524. arm926ejs_init_arch_info(target, arm926ejs, target->tap);
  525. armv4_5 = target->arch_info;
  526. arm7_9 = armv4_5->arch_info;
  527. /* override some insn sequence functions */
  528. arm7_9->change_to_arm = feroceon_change_to_arm;
  529. arm7_9->read_core_regs = feroceon_read_core_regs;
  530. arm7_9->read_core_regs_target_buffer = feroceon_read_core_regs_target_buffer;
  531. arm7_9->read_xpsr = feroceon_read_xpsr;
  532. arm7_9->write_xpsr = feroceon_write_xpsr;
  533. arm7_9->write_xpsr_im8 = feroceon_write_xpsr_im8;
  534. arm7_9->write_core_regs = feroceon_write_core_regs;
  535. arm7_9->branch_resume = feroceon_branch_resume;
  536. arm7_9->branch_resume_thumb = feroceon_branch_resume_thumb;
  537. /* must be implemented with only one comparator */
  538. arm7_9->enable_single_step = feroceon_enable_single_step;
  539. arm7_9->disable_single_step = feroceon_disable_single_step;
  540. /* MOE is not implemented */
  541. arm7_9->examine_debug_reason = feroceon_examine_debug_reason;
  542. /* the standard ARM926 methods don't always work (don't ask...) */
  543. arm926ejs->read_cp15 = feroceon_read_cp15;
  544. arm926ejs->write_cp15 = feroceon_write_cp15;
  545. /* Note: asserting DBGRQ might not win over the undef exception.
  546. If that happens then just use "arm7_9 dbgrq disable". */
  547. arm7_9->use_dbgrq = 1;
  548. arm7_9->set_special_dbgrq = feroceon_set_dbgrq;
  549. /* only one working comparator */
  550. arm7_9->wp_available_max = 1;
  551. arm7_9->wp1_used_default = -1;
  552. return ERROR_OK;
  553. }
  554. int feroceon_examine(struct target_s *target)
  555. {
  556. armv4_5_common_t *armv4_5;
  557. arm7_9_common_t *arm7_9;
  558. int retval;
  559. retval = arm9tdmi_examine(target);
  560. if (retval!=ERROR_OK)
  561. return retval;
  562. armv4_5 = target->arch_info;
  563. arm7_9 = armv4_5->arch_info;
  564. /* the COMMS_CTRL bits are all contiguous */
  565. if (buf_get_u32(arm7_9->eice_cache->reg_list[EICE_COMMS_CTRL].value, 2, 4) != 6)
  566. LOG_ERROR("unexpected Feroceon EICE version signature");
  567. arm7_9->eice_cache->reg_list[EICE_DBG_CTRL].size = 6;
  568. arm7_9->eice_cache->reg_list[EICE_DBG_STAT].size = 5;
  569. arm7_9->has_monitor_mode = 1;
  570. /* vector catch reg is not initialized on reset */
  571. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_VEC_CATCH], 0);
  572. /* clear monitor mode, enable comparators */
  573. embeddedice_read_reg(&arm7_9->eice_cache->reg_list[EICE_DBG_CTRL]);
  574. jtag_execute_queue();
  575. buf_set_u32(arm7_9->eice_cache->reg_list[EICE_DBG_CTRL].value, 4, 1, 0);
  576. buf_set_u32(arm7_9->eice_cache->reg_list[EICE_DBG_CTRL].value, 5, 1, 0);
  577. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_DBG_CTRL]);
  578. return ERROR_OK;
  579. }