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.
 
 
 
 
 
 

537 lines
14 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  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 "embeddedice.h"
  30. #include "armv4_5.h"
  31. #include "arm7_9_common.h"
  32. #include "log.h"
  33. #include "arm_jtag.h"
  34. #include "types.h"
  35. #include "binarybuffer.h"
  36. #include "target.h"
  37. #include "register.h"
  38. #include "jtag.h"
  39. #include <stdlib.h>
  40. #if 0
  41. static bitfield_desc_t embeddedice_comms_ctrl_bitfield_desc[] =
  42. {
  43. {"R", 1},
  44. {"W", 1},
  45. {"reserved", 26},
  46. {"version", 4}
  47. };
  48. #endif
  49. static int embeddedice_reg_arch_info[] =
  50. {
  51. 0x0, 0x1, 0x4, 0x5,
  52. 0x8, 0x9, 0xa, 0xb, 0xc, 0xd,
  53. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
  54. 0x2
  55. };
  56. static char* embeddedice_reg_list[] =
  57. {
  58. "debug_ctrl",
  59. "debug_status",
  60. "comms_ctrl",
  61. "comms_data",
  62. "watch 0 addr value",
  63. "watch 0 addr mask",
  64. "watch 0 data value",
  65. "watch 0 data mask",
  66. "watch 0 control value",
  67. "watch 0 control mask",
  68. "watch 1 addr value",
  69. "watch 1 addr mask",
  70. "watch 1 data value",
  71. "watch 1 data mask",
  72. "watch 1 control value",
  73. "watch 1 control mask",
  74. "vector catch"
  75. };
  76. static int embeddedice_reg_arch_type = -1;
  77. static int embeddedice_get_reg(reg_t *reg);
  78. reg_cache_t* embeddedice_build_reg_cache(target_t *target, arm7_9_common_t *arm7_9)
  79. {
  80. int retval;
  81. reg_cache_t *reg_cache = malloc(sizeof(reg_cache_t));
  82. reg_t *reg_list = NULL;
  83. embeddedice_reg_t *arch_info = NULL;
  84. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  85. int num_regs;
  86. int i;
  87. int eice_version = 0;
  88. /* register a register arch-type for EmbeddedICE registers only once */
  89. if (embeddedice_reg_arch_type == -1)
  90. embeddedice_reg_arch_type = register_reg_arch_type(embeddedice_get_reg, embeddedice_set_reg_w_exec);
  91. if (arm7_9->has_vector_catch)
  92. num_regs = 17;
  93. else
  94. num_regs = 16;
  95. /* the actual registers are kept in two arrays */
  96. reg_list = calloc(num_regs, sizeof(reg_t));
  97. arch_info = calloc(num_regs, sizeof(embeddedice_reg_t));
  98. /* fill in values for the reg cache */
  99. reg_cache->name = "EmbeddedICE registers";
  100. reg_cache->next = NULL;
  101. reg_cache->reg_list = reg_list;
  102. reg_cache->num_regs = num_regs;
  103. /* set up registers */
  104. for (i = 0; i < num_regs; i++)
  105. {
  106. reg_list[i].name = embeddedice_reg_list[i];
  107. reg_list[i].size = 32;
  108. reg_list[i].dirty = 0;
  109. reg_list[i].valid = 0;
  110. reg_list[i].bitfield_desc = NULL;
  111. reg_list[i].num_bitfields = 0;
  112. reg_list[i].value = calloc(1, 4);
  113. reg_list[i].arch_info = &arch_info[i];
  114. reg_list[i].arch_type = embeddedice_reg_arch_type;
  115. arch_info[i].addr = embeddedice_reg_arch_info[i];
  116. arch_info[i].jtag_info = jtag_info;
  117. }
  118. /* identify EmbeddedICE version by reading DCC control register */
  119. embeddedice_read_reg(&reg_list[EICE_COMMS_CTRL]);
  120. if ((retval=jtag_execute_queue())!=ERROR_OK)
  121. {
  122. for (i = 0; i < num_regs; i++)
  123. {
  124. free(reg_list[i].value);
  125. }
  126. free(reg_list);
  127. free(arch_info);
  128. return NULL;
  129. }
  130. eice_version = buf_get_u32(reg_list[EICE_COMMS_CTRL].value, 28, 4);
  131. switch (eice_version)
  132. {
  133. case 1:
  134. reg_list[EICE_DBG_CTRL].size = 3;
  135. reg_list[EICE_DBG_STAT].size = 5;
  136. break;
  137. case 2:
  138. reg_list[EICE_DBG_CTRL].size = 4;
  139. reg_list[EICE_DBG_STAT].size = 5;
  140. arm7_9->has_single_step = 1;
  141. break;
  142. case 3:
  143. LOG_ERROR("EmbeddedICE version 3 detected, EmbeddedICE handling might be broken");
  144. reg_list[EICE_DBG_CTRL].size = 6;
  145. reg_list[EICE_DBG_STAT].size = 5;
  146. arm7_9->has_single_step = 1;
  147. arm7_9->has_monitor_mode = 1;
  148. break;
  149. case 4:
  150. reg_list[EICE_DBG_CTRL].size = 6;
  151. reg_list[EICE_DBG_STAT].size = 5;
  152. arm7_9->has_monitor_mode = 1;
  153. break;
  154. case 5:
  155. reg_list[EICE_DBG_CTRL].size = 6;
  156. reg_list[EICE_DBG_STAT].size = 5;
  157. arm7_9->has_single_step = 1;
  158. arm7_9->has_monitor_mode = 1;
  159. break;
  160. case 6:
  161. reg_list[EICE_DBG_CTRL].size = 6;
  162. reg_list[EICE_DBG_STAT].size = 10;
  163. arm7_9->has_monitor_mode = 1;
  164. break;
  165. case 7:
  166. LOG_WARNING("EmbeddedICE version 7 detected, EmbeddedICE handling might be broken");
  167. reg_list[EICE_DBG_CTRL].size = 6;
  168. reg_list[EICE_DBG_STAT].size = 5;
  169. arm7_9->has_monitor_mode = 1;
  170. break;
  171. default:
  172. LOG_ERROR("unknown EmbeddedICE version (comms ctrl: 0x%8.8x)", buf_get_u32(reg_list[EICE_COMMS_CTRL].value, 0, 32));
  173. }
  174. return reg_cache;
  175. }
  176. int embeddedice_setup(target_t *target)
  177. {
  178. int retval;
  179. armv4_5_common_t *armv4_5 = target->arch_info;
  180. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  181. /* explicitly disable monitor mode */
  182. if (arm7_9->has_monitor_mode)
  183. {
  184. reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
  185. embeddedice_read_reg(dbg_ctrl);
  186. if ((retval=jtag_execute_queue())!=ERROR_OK)
  187. return retval;
  188. buf_set_u32(dbg_ctrl->value, 4, 1, 0);
  189. embeddedice_set_reg_w_exec(dbg_ctrl, dbg_ctrl->value);
  190. }
  191. return jtag_execute_queue();
  192. }
  193. static int embeddedice_get_reg(reg_t *reg)
  194. {
  195. int retval;
  196. if ((retval = embeddedice_read_reg(reg)) != ERROR_OK)
  197. {
  198. LOG_ERROR("BUG: error scheduling EmbeddedICE register read");
  199. return retval;
  200. }
  201. if ((retval = jtag_execute_queue()) != ERROR_OK)
  202. {
  203. LOG_ERROR("register read failed");
  204. return retval;
  205. }
  206. return ERROR_OK;
  207. }
  208. int embeddedice_read_reg_w_check(reg_t *reg, u8* check_value, u8* check_mask)
  209. {
  210. embeddedice_reg_t *ice_reg = reg->arch_info;
  211. u8 reg_addr = ice_reg->addr & 0x1f;
  212. scan_field_t fields[3];
  213. u8 field1_out[1];
  214. u8 field2_out[1];
  215. jtag_add_end_state(TAP_IDLE);
  216. arm_jtag_scann(ice_reg->jtag_info, 0x2);
  217. arm_jtag_set_instr(ice_reg->jtag_info, ice_reg->jtag_info->intest_instr, NULL);
  218. fields[0].tap = ice_reg->jtag_info->tap;
  219. fields[0].num_bits = 32;
  220. fields[0].out_value = reg->value;
  221. fields[0].in_value = NULL;
  222. fields[0].in_handler = NULL;
  223. fields[1].tap = ice_reg->jtag_info->tap;
  224. fields[1].num_bits = 5;
  225. fields[1].out_value = field1_out;
  226. buf_set_u32(fields[1].out_value, 0, 5, reg_addr);
  227. fields[1].in_value = NULL;
  228. fields[1].in_handler = NULL;
  229. fields[2].tap = ice_reg->jtag_info->tap;
  230. fields[2].num_bits = 1;
  231. fields[2].out_value = field2_out;
  232. buf_set_u32(fields[2].out_value, 0, 1, 0);
  233. fields[2].in_value = NULL;
  234. fields[2].in_handler = NULL;
  235. jtag_add_dr_scan(3, fields, TAP_INVALID);
  236. fields[0].in_value = reg->value;
  237. /* when reading the DCC data register, leaving the address field set to
  238. * EICE_COMMS_DATA would read the register twice
  239. * reading the control register is safe
  240. */
  241. buf_set_u32(fields[1].out_value, 0, 5, embeddedice_reg_arch_info[EICE_COMMS_CTRL]);
  242. jtag_add_dr_scan(3, fields, TAP_INVALID);
  243. jtag_check_value_mask(fields+0, check_value, check_mask);
  244. return ERROR_OK;
  245. }
  246. /* receive <size> words of 32 bit from the DCC
  247. * we pretend the target is always going to be fast enough
  248. * (relative to the JTAG clock), so we don't need to handshake
  249. */
  250. int embeddedice_receive(arm_jtag_t *jtag_info, u32 *data, u32 size)
  251. {
  252. scan_field_t fields[3];
  253. u8 field1_out[1];
  254. u8 field2_out[1];
  255. jtag_add_end_state(TAP_IDLE);
  256. arm_jtag_scann(jtag_info, 0x2);
  257. arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
  258. fields[0].tap = jtag_info->tap;
  259. fields[0].num_bits = 32;
  260. fields[0].out_value = NULL;
  261. u8 tmp[4];
  262. fields[0].in_value = tmp;
  263. fields[0].in_handler = NULL;
  264. fields[1].tap = jtag_info->tap;
  265. fields[1].num_bits = 5;
  266. fields[1].out_value = field1_out;
  267. buf_set_u32(fields[1].out_value, 0, 5, embeddedice_reg_arch_info[EICE_COMMS_DATA]);
  268. fields[1].in_value = NULL;
  269. fields[1].in_handler = NULL;
  270. fields[2].tap = jtag_info->tap;
  271. fields[2].num_bits = 1;
  272. fields[2].out_value = field2_out;
  273. buf_set_u32(fields[2].out_value, 0, 1, 0);
  274. fields[2].in_value = NULL;
  275. fields[2].in_handler = NULL;
  276. jtag_add_dr_scan(3, fields, TAP_INVALID);
  277. while (size > 0)
  278. {
  279. /* when reading the last item, set the register address to the DCC control reg,
  280. * to avoid reading additional data from the DCC data reg
  281. */
  282. if (size == 1)
  283. buf_set_u32(fields[1].out_value, 0, 5, embeddedice_reg_arch_info[EICE_COMMS_CTRL]);
  284. jtag_add_dr_scan_now(3, fields, TAP_INVALID);
  285. *data = le_to_h_u32(tmp);
  286. data++;
  287. size--;
  288. }
  289. return jtag_execute_queue();
  290. }
  291. int embeddedice_read_reg(reg_t *reg)
  292. {
  293. return embeddedice_read_reg_w_check(reg, NULL, NULL);
  294. }
  295. void embeddedice_set_reg(reg_t *reg, u32 value)
  296. {
  297. embeddedice_write_reg(reg, value);
  298. buf_set_u32(reg->value, 0, reg->size, value);
  299. reg->valid = 1;
  300. reg->dirty = 0;
  301. }
  302. int embeddedice_set_reg_w_exec(reg_t *reg, u8 *buf)
  303. {
  304. int retval;
  305. embeddedice_set_reg(reg, buf_get_u32(buf, 0, reg->size));
  306. if ((retval = jtag_execute_queue()) != ERROR_OK)
  307. {
  308. LOG_ERROR("register write failed");
  309. return retval;
  310. }
  311. return ERROR_OK;
  312. }
  313. void embeddedice_write_reg(reg_t *reg, u32 value)
  314. {
  315. embeddedice_reg_t *ice_reg = reg->arch_info;
  316. LOG_DEBUG("%i: 0x%8.8x", ice_reg->addr, value);
  317. jtag_add_end_state(TAP_IDLE);
  318. arm_jtag_scann(ice_reg->jtag_info, 0x2);
  319. arm_jtag_set_instr(ice_reg->jtag_info, ice_reg->jtag_info->intest_instr, NULL);
  320. u8 reg_addr = ice_reg->addr & 0x1f;
  321. embeddedice_write_reg_inner(ice_reg->jtag_info->tap, reg_addr, value);
  322. }
  323. void embeddedice_store_reg(reg_t *reg)
  324. {
  325. embeddedice_write_reg(reg, buf_get_u32(reg->value, 0, reg->size));
  326. }
  327. /* send <size> words of 32 bit to the DCC
  328. * we pretend the target is always going to be fast enough
  329. * (relative to the JTAG clock), so we don't need to handshake
  330. */
  331. int embeddedice_send(arm_jtag_t *jtag_info, u32 *data, u32 size)
  332. {
  333. scan_field_t fields[3];
  334. u8 field0_out[4];
  335. u8 field1_out[1];
  336. u8 field2_out[1];
  337. jtag_add_end_state(TAP_IDLE);
  338. arm_jtag_scann(jtag_info, 0x2);
  339. arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
  340. fields[0].tap = jtag_info->tap;
  341. fields[0].num_bits = 32;
  342. fields[0].out_value = field0_out;
  343. fields[0].in_value = NULL;
  344. fields[0].in_handler = NULL;
  345. fields[1].tap = jtag_info->tap;
  346. fields[1].num_bits = 5;
  347. fields[1].out_value = field1_out;
  348. buf_set_u32(fields[1].out_value, 0, 5, embeddedice_reg_arch_info[EICE_COMMS_DATA]);
  349. fields[1].in_value = NULL;
  350. fields[1].in_handler = NULL;
  351. fields[2].tap = jtag_info->tap;
  352. fields[2].num_bits = 1;
  353. fields[2].out_value = field2_out;
  354. buf_set_u32(fields[2].out_value, 0, 1, 1);
  355. fields[2].in_value = NULL;
  356. fields[2].in_handler = NULL;
  357. while (size > 0)
  358. {
  359. buf_set_u32(fields[0].out_value, 0, 32, *data);
  360. jtag_add_dr_scan(3, fields, TAP_INVALID);
  361. data++;
  362. size--;
  363. }
  364. /* call to jtag_execute_queue() intentionally omitted */
  365. return ERROR_OK;
  366. }
  367. /* wait for DCC control register R/W handshake bit to become active
  368. */
  369. int embeddedice_handshake(arm_jtag_t *jtag_info, int hsbit, u32 timeout)
  370. {
  371. scan_field_t fields[3];
  372. u8 field0_in[4];
  373. u8 field1_out[1];
  374. u8 field2_out[1];
  375. int retval;
  376. u32 hsact;
  377. struct timeval lap;
  378. struct timeval now;
  379. if (hsbit == EICE_COMM_CTRL_WBIT)
  380. hsact = 1;
  381. else if (hsbit == EICE_COMM_CTRL_RBIT)
  382. hsact = 0;
  383. else
  384. return ERROR_INVALID_ARGUMENTS;
  385. jtag_add_end_state(TAP_IDLE);
  386. arm_jtag_scann(jtag_info, 0x2);
  387. arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
  388. fields[0].tap = jtag_info->tap;
  389. fields[0].num_bits = 32;
  390. fields[0].out_value = NULL;
  391. fields[0].in_value = field0_in;
  392. fields[0].in_handler = NULL;
  393. fields[1].tap = jtag_info->tap;
  394. fields[1].num_bits = 5;
  395. fields[1].out_value = field1_out;
  396. buf_set_u32(fields[1].out_value, 0, 5, embeddedice_reg_arch_info[EICE_COMMS_CTRL]);
  397. fields[1].in_value = NULL;
  398. fields[1].in_handler = NULL;
  399. fields[2].tap = jtag_info->tap;
  400. fields[2].num_bits = 1;
  401. fields[2].out_value = field2_out;
  402. buf_set_u32(fields[2].out_value, 0, 1, 0);
  403. fields[2].in_value = NULL;
  404. fields[2].in_handler = NULL;
  405. jtag_add_dr_scan(3, fields, TAP_INVALID);
  406. gettimeofday(&lap, NULL);
  407. do
  408. {
  409. jtag_add_dr_scan(3, fields, TAP_INVALID);
  410. if ((retval = jtag_execute_queue()) != ERROR_OK)
  411. return retval;
  412. if (buf_get_u32(field0_in, hsbit, 1) == hsact)
  413. return ERROR_OK;
  414. gettimeofday(&now, NULL);
  415. }
  416. while ((u32)((now.tv_sec-lap.tv_sec)*1000 + (now.tv_usec-lap.tv_usec)/1000) <= timeout);
  417. return ERROR_TARGET_TIMEOUT;
  418. }
  419. /* this is the inner loop of the open loop DCC write of data to target */
  420. void MINIDRIVER(embeddedice_write_dcc)(jtag_tap_t *tap, int reg_addr, u8 *buffer, int little, int count)
  421. {
  422. int i;
  423. for (i = 0; i < count; i++)
  424. {
  425. embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little));
  426. buffer += 4;
  427. }
  428. }