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.
 
 
 
 
 
 

806 lines
24 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) 2007,2008 Øyvind Harboe *
  8. * oyvind.harboe@zylin.com *
  9. * *
  10. * Copyright (C) 2011 by Drasko DRASKOVIC *
  11. * drasko.draskovic@gmail.com *
  12. * *
  13. * This program is free software; you can redistribute it and/or modify *
  14. * it under the terms of the GNU General Public License as published by *
  15. * the Free Software Foundation; either version 2 of the License, or *
  16. * (at your option) any later version. *
  17. * *
  18. * This program is distributed in the hope that it will be useful, *
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  21. * GNU General Public License for more details. *
  22. * *
  23. * You should have received a copy of the GNU General Public License *
  24. * along with this program; if not, write to the *
  25. * Free Software Foundation, Inc., *
  26. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  27. ***************************************************************************/
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. #include "mips32.h"
  32. #include "breakpoints.h"
  33. #include "algorithm.h"
  34. #include "register.h"
  35. static char *mips32_core_reg_list[] = {
  36. "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
  37. "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
  38. "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
  39. "t8", "t9", "k0", "k1", "gp", "sp", "fp", "ra",
  40. "status", "lo", "hi", "badvaddr", "cause", "pc"
  41. };
  42. static const char *mips_isa_strings[] = {
  43. "MIPS32", "MIPS16e"
  44. };
  45. static struct mips32_core_reg mips32_core_reg_list_arch_info[MIPS32NUMCOREREGS] = {
  46. {0, NULL, NULL},
  47. {1, NULL, NULL},
  48. {2, NULL, NULL},
  49. {3, NULL, NULL},
  50. {4, NULL, NULL},
  51. {5, NULL, NULL},
  52. {6, NULL, NULL},
  53. {7, NULL, NULL},
  54. {8, NULL, NULL},
  55. {9, NULL, NULL},
  56. {10, NULL, NULL},
  57. {11, NULL, NULL},
  58. {12, NULL, NULL},
  59. {13, NULL, NULL},
  60. {14, NULL, NULL},
  61. {15, NULL, NULL},
  62. {16, NULL, NULL},
  63. {17, NULL, NULL},
  64. {18, NULL, NULL},
  65. {19, NULL, NULL},
  66. {20, NULL, NULL},
  67. {21, NULL, NULL},
  68. {22, NULL, NULL},
  69. {23, NULL, NULL},
  70. {24, NULL, NULL},
  71. {25, NULL, NULL},
  72. {26, NULL, NULL},
  73. {27, NULL, NULL},
  74. {28, NULL, NULL},
  75. {29, NULL, NULL},
  76. {30, NULL, NULL},
  77. {31, NULL, NULL},
  78. {32, NULL, NULL},
  79. {33, NULL, NULL},
  80. {34, NULL, NULL},
  81. {35, NULL, NULL},
  82. {36, NULL, NULL},
  83. {37, NULL, NULL},
  84. };
  85. /* number of mips dummy fp regs fp0 - fp31 + fsr and fir
  86. * we also add 18 unknown registers to handle gdb requests */
  87. #define MIPS32NUMFPREGS (34 + 18)
  88. static uint8_t mips32_gdb_dummy_fp_value[] = {0, 0, 0, 0};
  89. static struct reg mips32_gdb_dummy_fp_reg = {
  90. .name = "GDB dummy floating-point register",
  91. .value = mips32_gdb_dummy_fp_value,
  92. .dirty = 0,
  93. .valid = 1,
  94. .size = 32,
  95. .arch_info = NULL,
  96. };
  97. static int mips32_get_core_reg(struct reg *reg)
  98. {
  99. int retval;
  100. struct mips32_core_reg *mips32_reg = reg->arch_info;
  101. struct target *target = mips32_reg->target;
  102. struct mips32_common *mips32_target = target_to_mips32(target);
  103. if (target->state != TARGET_HALTED)
  104. return ERROR_TARGET_NOT_HALTED;
  105. retval = mips32_target->read_core_reg(target, mips32_reg->num);
  106. return retval;
  107. }
  108. static int mips32_set_core_reg(struct reg *reg, uint8_t *buf)
  109. {
  110. struct mips32_core_reg *mips32_reg = reg->arch_info;
  111. struct target *target = mips32_reg->target;
  112. uint32_t value = buf_get_u32(buf, 0, 32);
  113. if (target->state != TARGET_HALTED)
  114. return ERROR_TARGET_NOT_HALTED;
  115. buf_set_u32(reg->value, 0, 32, value);
  116. reg->dirty = 1;
  117. reg->valid = 1;
  118. return ERROR_OK;
  119. }
  120. static int mips32_read_core_reg(struct target *target, int num)
  121. {
  122. uint32_t reg_value;
  123. /* get pointers to arch-specific information */
  124. struct mips32_common *mips32 = target_to_mips32(target);
  125. if ((num < 0) || (num >= MIPS32NUMCOREREGS))
  126. return ERROR_COMMAND_SYNTAX_ERROR;
  127. reg_value = mips32->core_regs[num];
  128. buf_set_u32(mips32->core_cache->reg_list[num].value, 0, 32, reg_value);
  129. mips32->core_cache->reg_list[num].valid = 1;
  130. mips32->core_cache->reg_list[num].dirty = 0;
  131. return ERROR_OK;
  132. }
  133. static int mips32_write_core_reg(struct target *target, int num)
  134. {
  135. uint32_t reg_value;
  136. /* get pointers to arch-specific information */
  137. struct mips32_common *mips32 = target_to_mips32(target);
  138. if ((num < 0) || (num >= MIPS32NUMCOREREGS))
  139. return ERROR_COMMAND_SYNTAX_ERROR;
  140. reg_value = buf_get_u32(mips32->core_cache->reg_list[num].value, 0, 32);
  141. mips32->core_regs[num] = reg_value;
  142. LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", num , reg_value);
  143. mips32->core_cache->reg_list[num].valid = 1;
  144. mips32->core_cache->reg_list[num].dirty = 0;
  145. return ERROR_OK;
  146. }
  147. int mips32_get_gdb_reg_list(struct target *target, struct reg **reg_list[], int *reg_list_size)
  148. {
  149. /* get pointers to arch-specific information */
  150. struct mips32_common *mips32 = target_to_mips32(target);
  151. int i;
  152. /* include floating point registers */
  153. *reg_list_size = MIPS32NUMCOREREGS + MIPS32NUMFPREGS;
  154. *reg_list = malloc(sizeof(struct reg *) * (*reg_list_size));
  155. for (i = 0; i < MIPS32NUMCOREREGS; i++)
  156. (*reg_list)[i] = &mips32->core_cache->reg_list[i];
  157. /* add dummy floating points regs */
  158. for (i = MIPS32NUMCOREREGS; i < (MIPS32NUMCOREREGS + MIPS32NUMFPREGS); i++)
  159. (*reg_list)[i] = &mips32_gdb_dummy_fp_reg;
  160. return ERROR_OK;
  161. }
  162. int mips32_save_context(struct target *target)
  163. {
  164. int i;
  165. /* get pointers to arch-specific information */
  166. struct mips32_common *mips32 = target_to_mips32(target);
  167. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  168. /* read core registers */
  169. mips32_pracc_read_regs(ejtag_info, mips32->core_regs);
  170. for (i = 0; i < MIPS32NUMCOREREGS; i++) {
  171. if (!mips32->core_cache->reg_list[i].valid)
  172. mips32->read_core_reg(target, i);
  173. }
  174. return ERROR_OK;
  175. }
  176. int mips32_restore_context(struct target *target)
  177. {
  178. int i;
  179. /* get pointers to arch-specific information */
  180. struct mips32_common *mips32 = target_to_mips32(target);
  181. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  182. for (i = 0; i < MIPS32NUMCOREREGS; i++) {
  183. if (mips32->core_cache->reg_list[i].dirty)
  184. mips32->write_core_reg(target, i);
  185. }
  186. /* write core regs */
  187. mips32_pracc_write_regs(ejtag_info, mips32->core_regs);
  188. return ERROR_OK;
  189. }
  190. int mips32_arch_state(struct target *target)
  191. {
  192. struct mips32_common *mips32 = target_to_mips32(target);
  193. LOG_USER("target halted in %s mode due to %s, pc: 0x%8.8" PRIx32 "",
  194. mips_isa_strings[mips32->isa_mode],
  195. debug_reason_name(target),
  196. buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32));
  197. return ERROR_OK;
  198. }
  199. static const struct reg_arch_type mips32_reg_type = {
  200. .get = mips32_get_core_reg,
  201. .set = mips32_set_core_reg,
  202. };
  203. struct reg_cache *mips32_build_reg_cache(struct target *target)
  204. {
  205. /* get pointers to arch-specific information */
  206. struct mips32_common *mips32 = target_to_mips32(target);
  207. int num_regs = MIPS32NUMCOREREGS;
  208. struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
  209. struct reg_cache *cache = malloc(sizeof(struct reg_cache));
  210. struct reg *reg_list = malloc(sizeof(struct reg) * num_regs);
  211. struct mips32_core_reg *arch_info = malloc(sizeof(struct mips32_core_reg) * num_regs);
  212. int i;
  213. register_init_dummy(&mips32_gdb_dummy_fp_reg);
  214. /* Build the process context cache */
  215. cache->name = "mips32 registers";
  216. cache->next = NULL;
  217. cache->reg_list = reg_list;
  218. cache->num_regs = num_regs;
  219. (*cache_p) = cache;
  220. mips32->core_cache = cache;
  221. for (i = 0; i < num_regs; i++) {
  222. arch_info[i] = mips32_core_reg_list_arch_info[i];
  223. arch_info[i].target = target;
  224. arch_info[i].mips32_common = mips32;
  225. reg_list[i].name = mips32_core_reg_list[i];
  226. reg_list[i].size = 32;
  227. reg_list[i].value = calloc(1, 4);
  228. reg_list[i].dirty = 0;
  229. reg_list[i].valid = 0;
  230. reg_list[i].type = &mips32_reg_type;
  231. reg_list[i].arch_info = &arch_info[i];
  232. }
  233. return cache;
  234. }
  235. int mips32_init_arch_info(struct target *target, struct mips32_common *mips32, struct jtag_tap *tap)
  236. {
  237. target->arch_info = mips32;
  238. mips32->common_magic = MIPS32_COMMON_MAGIC;
  239. mips32->fast_data_area = NULL;
  240. /* has breakpoint/watchpint unit been scanned */
  241. mips32->bp_scanned = 0;
  242. mips32->data_break_list = NULL;
  243. mips32->ejtag_info.tap = tap;
  244. mips32->read_core_reg = mips32_read_core_reg;
  245. mips32->write_core_reg = mips32_write_core_reg;
  246. return ERROR_OK;
  247. }
  248. /* run to exit point. return error if exit point was not reached. */
  249. static int mips32_run_and_wait(struct target *target, uint32_t entry_point,
  250. int timeout_ms, uint32_t exit_point, struct mips32_common *mips32)
  251. {
  252. uint32_t pc;
  253. int retval;
  254. /* This code relies on the target specific resume() and poll()->debug_entry()
  255. * sequence to write register values to the processor and the read them back */
  256. retval = target_resume(target, 0, entry_point, 0, 1);
  257. if (retval != ERROR_OK)
  258. return retval;
  259. retval = target_wait_state(target, TARGET_HALTED, timeout_ms);
  260. /* If the target fails to halt due to the breakpoint, force a halt */
  261. if (retval != ERROR_OK || target->state != TARGET_HALTED) {
  262. retval = target_halt(target);
  263. if (retval != ERROR_OK)
  264. return retval;
  265. retval = target_wait_state(target, TARGET_HALTED, 500);
  266. if (retval != ERROR_OK)
  267. return retval;
  268. return ERROR_TARGET_TIMEOUT;
  269. }
  270. pc = buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32);
  271. if (exit_point && (pc != exit_point)) {
  272. LOG_DEBUG("failed algorithm halted at 0x%" PRIx32 " ", pc);
  273. return ERROR_TARGET_TIMEOUT;
  274. }
  275. return ERROR_OK;
  276. }
  277. int mips32_run_algorithm(struct target *target, int num_mem_params,
  278. struct mem_param *mem_params, int num_reg_params,
  279. struct reg_param *reg_params, uint32_t entry_point,
  280. uint32_t exit_point, int timeout_ms, void *arch_info)
  281. {
  282. struct mips32_common *mips32 = target_to_mips32(target);
  283. struct mips32_algorithm *mips32_algorithm_info = arch_info;
  284. enum mips32_isa_mode isa_mode = mips32->isa_mode;
  285. uint32_t context[MIPS32NUMCOREREGS];
  286. int i;
  287. int retval = ERROR_OK;
  288. LOG_DEBUG("Running algorithm");
  289. /* NOTE: mips32_run_algorithm requires that each algorithm uses a software breakpoint
  290. * at the exit point */
  291. if (mips32->common_magic != MIPS32_COMMON_MAGIC) {
  292. LOG_ERROR("current target isn't a MIPS32 target");
  293. return ERROR_TARGET_INVALID;
  294. }
  295. if (target->state != TARGET_HALTED) {
  296. LOG_WARNING("target not halted");
  297. return ERROR_TARGET_NOT_HALTED;
  298. }
  299. /* refresh core register cache */
  300. for (i = 0; i < MIPS32NUMCOREREGS; i++) {
  301. if (!mips32->core_cache->reg_list[i].valid)
  302. mips32->read_core_reg(target, i);
  303. context[i] = buf_get_u32(mips32->core_cache->reg_list[i].value, 0, 32);
  304. }
  305. for (i = 0; i < num_mem_params; i++) {
  306. retval = target_write_buffer(target, mem_params[i].address,
  307. mem_params[i].size, mem_params[i].value);
  308. if (retval != ERROR_OK)
  309. return retval;
  310. }
  311. for (i = 0; i < num_reg_params; i++) {
  312. struct reg *reg = register_get_by_name(mips32->core_cache, reg_params[i].reg_name, 0);
  313. if (!reg) {
  314. LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
  315. return ERROR_COMMAND_SYNTAX_ERROR;
  316. }
  317. if (reg->size != reg_params[i].size) {
  318. LOG_ERROR("BUG: register '%s' size doesn't match reg_params[i].size",
  319. reg_params[i].reg_name);
  320. return ERROR_COMMAND_SYNTAX_ERROR;
  321. }
  322. mips32_set_core_reg(reg, reg_params[i].value);
  323. }
  324. mips32->isa_mode = mips32_algorithm_info->isa_mode;
  325. retval = mips32_run_and_wait(target, entry_point, timeout_ms, exit_point, mips32);
  326. if (retval != ERROR_OK)
  327. return retval;
  328. for (i = 0; i < num_mem_params; i++) {
  329. if (mem_params[i].direction != PARAM_OUT) {
  330. retval = target_read_buffer(target, mem_params[i].address, mem_params[i].size,
  331. mem_params[i].value);
  332. if (retval != ERROR_OK)
  333. return retval;
  334. }
  335. }
  336. for (i = 0; i < num_reg_params; i++) {
  337. if (reg_params[i].direction != PARAM_OUT) {
  338. struct reg *reg = register_get_by_name(mips32->core_cache, reg_params[i].reg_name, 0);
  339. if (!reg) {
  340. LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
  341. return ERROR_COMMAND_SYNTAX_ERROR;
  342. }
  343. if (reg->size != reg_params[i].size) {
  344. LOG_ERROR("BUG: register '%s' size doesn't match reg_params[i].size",
  345. reg_params[i].reg_name);
  346. return ERROR_COMMAND_SYNTAX_ERROR;
  347. }
  348. buf_set_u32(reg_params[i].value, 0, 32, buf_get_u32(reg->value, 0, 32));
  349. }
  350. }
  351. /* restore everything we saved before */
  352. for (i = 0; i < MIPS32NUMCOREREGS; i++) {
  353. uint32_t regvalue;
  354. regvalue = buf_get_u32(mips32->core_cache->reg_list[i].value, 0, 32);
  355. if (regvalue != context[i]) {
  356. LOG_DEBUG("restoring register %s with value 0x%8.8" PRIx32,
  357. mips32->core_cache->reg_list[i].name, context[i]);
  358. buf_set_u32(mips32->core_cache->reg_list[i].value,
  359. 0, 32, context[i]);
  360. mips32->core_cache->reg_list[i].valid = 1;
  361. mips32->core_cache->reg_list[i].dirty = 1;
  362. }
  363. }
  364. mips32->isa_mode = isa_mode;
  365. return ERROR_OK;
  366. }
  367. int mips32_examine(struct target *target)
  368. {
  369. struct mips32_common *mips32 = target_to_mips32(target);
  370. if (!target_was_examined(target)) {
  371. target_set_examined(target);
  372. /* we will configure later */
  373. mips32->bp_scanned = 0;
  374. mips32->num_inst_bpoints = 0;
  375. mips32->num_data_bpoints = 0;
  376. mips32->num_inst_bpoints_avail = 0;
  377. mips32->num_data_bpoints_avail = 0;
  378. }
  379. return ERROR_OK;
  380. }
  381. int mips32_configure_break_unit(struct target *target)
  382. {
  383. /* get pointers to arch-specific information */
  384. struct mips32_common *mips32 = target_to_mips32(target);
  385. int retval;
  386. uint32_t dcr, bpinfo;
  387. int i;
  388. if (mips32->bp_scanned)
  389. return ERROR_OK;
  390. /* get info about breakpoint support */
  391. retval = target_read_u32(target, EJTAG_DCR, &dcr);
  392. if (retval != ERROR_OK)
  393. return retval;
  394. if (dcr & EJTAG_DCR_IB) {
  395. /* get number of inst breakpoints */
  396. retval = target_read_u32(target, EJTAG_IBS, &bpinfo);
  397. if (retval != ERROR_OK)
  398. return retval;
  399. mips32->num_inst_bpoints = (bpinfo >> 24) & 0x0F;
  400. mips32->num_inst_bpoints_avail = mips32->num_inst_bpoints;
  401. mips32->inst_break_list = calloc(mips32->num_inst_bpoints, sizeof(struct mips32_comparator));
  402. for (i = 0; i < mips32->num_inst_bpoints; i++)
  403. mips32->inst_break_list[i].reg_address = EJTAG_IBA1 + (0x100 * i);
  404. /* clear IBIS reg */
  405. retval = target_write_u32(target, EJTAG_IBS, 0);
  406. if (retval != ERROR_OK)
  407. return retval;
  408. }
  409. if (dcr & EJTAG_DCR_DB) {
  410. /* get number of data breakpoints */
  411. retval = target_read_u32(target, EJTAG_DBS, &bpinfo);
  412. if (retval != ERROR_OK)
  413. return retval;
  414. mips32->num_data_bpoints = (bpinfo >> 24) & 0x0F;
  415. mips32->num_data_bpoints_avail = mips32->num_data_bpoints;
  416. mips32->data_break_list = calloc(mips32->num_data_bpoints, sizeof(struct mips32_comparator));
  417. for (i = 0; i < mips32->num_data_bpoints; i++)
  418. mips32->data_break_list[i].reg_address = EJTAG_DBA1 + (0x100 * i);
  419. /* clear DBIS reg */
  420. retval = target_write_u32(target, EJTAG_DBS, 0);
  421. if (retval != ERROR_OK)
  422. return retval;
  423. }
  424. /* check if target endianness settings matches debug control register */
  425. if (((dcr & EJTAG_DCR_ENM) && (target->endianness == TARGET_LITTLE_ENDIAN)) ||
  426. (!(dcr & EJTAG_DCR_ENM) && (target->endianness == TARGET_BIG_ENDIAN)))
  427. LOG_WARNING("DCR endianness settings does not match target settings");
  428. LOG_DEBUG("DCR 0x%" PRIx32 " numinst %i numdata %i", dcr, mips32->num_inst_bpoints,
  429. mips32->num_data_bpoints);
  430. mips32->bp_scanned = 1;
  431. return ERROR_OK;
  432. }
  433. int mips32_enable_interrupts(struct target *target, int enable)
  434. {
  435. int retval;
  436. int update = 0;
  437. uint32_t dcr;
  438. /* read debug control register */
  439. retval = target_read_u32(target, EJTAG_DCR, &dcr);
  440. if (retval != ERROR_OK)
  441. return retval;
  442. if (enable) {
  443. if (!(dcr & EJTAG_DCR_INTE)) {
  444. /* enable interrupts */
  445. dcr |= EJTAG_DCR_INTE;
  446. update = 1;
  447. }
  448. } else {
  449. if (dcr & EJTAG_DCR_INTE) {
  450. /* disable interrupts */
  451. dcr &= ~EJTAG_DCR_INTE;
  452. update = 1;
  453. }
  454. }
  455. if (update) {
  456. retval = target_write_u32(target, EJTAG_DCR, dcr);
  457. if (retval != ERROR_OK)
  458. return retval;
  459. }
  460. return ERROR_OK;
  461. }
  462. int mips32_checksum_memory(struct target *target, uint32_t address,
  463. uint32_t count, uint32_t *checksum)
  464. {
  465. struct working_area *crc_algorithm;
  466. struct reg_param reg_params[2];
  467. struct mips32_algorithm mips32_info;
  468. int retval;
  469. uint32_t i;
  470. /* see contib/loaders/checksum/mips32.s for src */
  471. static const uint32_t mips_crc_code[] = {
  472. 0x248C0000, /* addiu $t4, $a0, 0 */
  473. 0x24AA0000, /* addiu $t2, $a1, 0 */
  474. 0x2404FFFF, /* addiu $a0, $zero, 0xffffffff */
  475. 0x10000010, /* beq $zero, $zero, ncomp */
  476. 0x240B0000, /* addiu $t3, $zero, 0 */
  477. /* nbyte: */
  478. 0x81850000, /* lb $a1, ($t4) */
  479. 0x218C0001, /* addi $t4, $t4, 1 */
  480. 0x00052E00, /* sll $a1, $a1, 24 */
  481. 0x3C0204C1, /* lui $v0, 0x04c1 */
  482. 0x00852026, /* xor $a0, $a0, $a1 */
  483. 0x34471DB7, /* ori $a3, $v0, 0x1db7 */
  484. 0x00003021, /* addu $a2, $zero, $zero */
  485. /* loop: */
  486. 0x00044040, /* sll $t0, $a0, 1 */
  487. 0x24C60001, /* addiu $a2, $a2, 1 */
  488. 0x28840000, /* slti $a0, $a0, 0 */
  489. 0x01074826, /* xor $t1, $t0, $a3 */
  490. 0x0124400B, /* movn $t0, $t1, $a0 */
  491. 0x28C30008, /* slti $v1, $a2, 8 */
  492. 0x1460FFF9, /* bne $v1, $zero, loop */
  493. 0x01002021, /* addu $a0, $t0, $zero */
  494. /* ncomp: */
  495. 0x154BFFF0, /* bne $t2, $t3, nbyte */
  496. 0x256B0001, /* addiu $t3, $t3, 1 */
  497. 0x7000003F, /* sdbbp */
  498. };
  499. /* make sure we have a working area */
  500. if (target_alloc_working_area(target, sizeof(mips_crc_code), &crc_algorithm) != ERROR_OK)
  501. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  502. /* convert flash writing code into a buffer in target endianness */
  503. for (i = 0; i < ARRAY_SIZE(mips_crc_code); i++)
  504. target_write_u32(target, crc_algorithm->address + i*sizeof(uint32_t), mips_crc_code[i]);
  505. mips32_info.common_magic = MIPS32_COMMON_MAGIC;
  506. mips32_info.isa_mode = MIPS32_ISA_MIPS32;
  507. init_reg_param(&reg_params[0], "a0", 32, PARAM_IN_OUT);
  508. buf_set_u32(reg_params[0].value, 0, 32, address);
  509. init_reg_param(&reg_params[1], "a1", 32, PARAM_OUT);
  510. buf_set_u32(reg_params[1].value, 0, 32, count);
  511. int timeout = 20000 * (1 + (count / (1024 * 1024)));
  512. retval = target_run_algorithm(target, 0, NULL, 2, reg_params,
  513. crc_algorithm->address, crc_algorithm->address + (sizeof(mips_crc_code)-4), timeout,
  514. &mips32_info);
  515. if (retval != ERROR_OK) {
  516. destroy_reg_param(&reg_params[0]);
  517. destroy_reg_param(&reg_params[1]);
  518. target_free_working_area(target, crc_algorithm);
  519. return retval;
  520. }
  521. *checksum = buf_get_u32(reg_params[0].value, 0, 32);
  522. destroy_reg_param(&reg_params[0]);
  523. destroy_reg_param(&reg_params[1]);
  524. target_free_working_area(target, crc_algorithm);
  525. return ERROR_OK;
  526. }
  527. /** Checks whether a memory region is zeroed. */
  528. int mips32_blank_check_memory(struct target *target,
  529. uint32_t address, uint32_t count, uint32_t *blank)
  530. {
  531. struct working_area *erase_check_algorithm;
  532. struct reg_param reg_params[3];
  533. struct mips32_algorithm mips32_info;
  534. int retval;
  535. uint32_t i;
  536. static const uint32_t erase_check_code[] = {
  537. /* nbyte: */
  538. 0x80880000, /* lb $t0, ($a0) */
  539. 0x00C83024, /* and $a2, $a2, $t0 */
  540. 0x24A5FFFF, /* addiu $a1, $a1, -1 */
  541. 0x14A0FFFC, /* bne $a1, $zero, nbyte */
  542. 0x24840001, /* addiu $a0, $a0, 1 */
  543. 0x7000003F /* sdbbp */
  544. };
  545. /* make sure we have a working area */
  546. if (target_alloc_working_area(target, sizeof(erase_check_code), &erase_check_algorithm) != ERROR_OK)
  547. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  548. /* convert flash writing code into a buffer in target endianness */
  549. for (i = 0; i < ARRAY_SIZE(erase_check_code); i++) {
  550. target_write_u32(target, erase_check_algorithm->address + i*sizeof(uint32_t),
  551. erase_check_code[i]);
  552. }
  553. mips32_info.common_magic = MIPS32_COMMON_MAGIC;
  554. mips32_info.isa_mode = MIPS32_ISA_MIPS32;
  555. init_reg_param(&reg_params[0], "a0", 32, PARAM_OUT);
  556. buf_set_u32(reg_params[0].value, 0, 32, address);
  557. init_reg_param(&reg_params[1], "a1", 32, PARAM_OUT);
  558. buf_set_u32(reg_params[1].value, 0, 32, count);
  559. init_reg_param(&reg_params[2], "a2", 32, PARAM_IN_OUT);
  560. buf_set_u32(reg_params[2].value, 0, 32, 0xff);
  561. retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
  562. erase_check_algorithm->address,
  563. erase_check_algorithm->address + (sizeof(erase_check_code)-4),
  564. 10000, &mips32_info);
  565. if (retval != ERROR_OK) {
  566. destroy_reg_param(&reg_params[0]);
  567. destroy_reg_param(&reg_params[1]);
  568. destroy_reg_param(&reg_params[2]);
  569. target_free_working_area(target, erase_check_algorithm);
  570. return retval;
  571. }
  572. *blank = buf_get_u32(reg_params[2].value, 0, 32);
  573. destroy_reg_param(&reg_params[0]);
  574. destroy_reg_param(&reg_params[1]);
  575. destroy_reg_param(&reg_params[2]);
  576. target_free_working_area(target, erase_check_algorithm);
  577. return ERROR_OK;
  578. }
  579. static int mips32_verify_pointer(struct command_context *cmd_ctx,
  580. struct mips32_common *mips32)
  581. {
  582. if (mips32->common_magic != MIPS32_COMMON_MAGIC) {
  583. command_print(cmd_ctx, "target is not an MIPS32");
  584. return ERROR_TARGET_INVALID;
  585. }
  586. return ERROR_OK;
  587. }
  588. /**
  589. * MIPS32 targets expose command interface
  590. * to manipulate CP0 registers
  591. */
  592. COMMAND_HANDLER(mips32_handle_cp0_command)
  593. {
  594. int retval;
  595. struct target *target = get_current_target(CMD_CTX);
  596. struct mips32_common *mips32 = target_to_mips32(target);
  597. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  598. retval = mips32_verify_pointer(CMD_CTX, mips32);
  599. if (retval != ERROR_OK)
  600. return retval;
  601. if (target->state != TARGET_HALTED) {
  602. command_print(CMD_CTX, "target must be stopped for \"%s\" command", CMD_NAME);
  603. return ERROR_OK;
  604. }
  605. /* two or more argument, access a single register/select (write if third argument is given) */
  606. if (CMD_ARGC < 2)
  607. return ERROR_COMMAND_SYNTAX_ERROR;
  608. else {
  609. uint32_t cp0_reg, cp0_sel;
  610. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], cp0_reg);
  611. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], cp0_sel);
  612. if (CMD_ARGC == 2) {
  613. uint32_t value;
  614. retval = mips32_cp0_read(ejtag_info, &value, cp0_reg, cp0_sel);
  615. if (retval != ERROR_OK) {
  616. command_print(CMD_CTX,
  617. "couldn't access reg %" PRIi32,
  618. cp0_reg);
  619. return ERROR_OK;
  620. }
  621. retval = jtag_execute_queue();
  622. if (retval != ERROR_OK)
  623. return retval;
  624. command_print(CMD_CTX, "cp0 reg %" PRIi32 ", select %" PRIi32 ": %8.8" PRIx32,
  625. cp0_reg, cp0_sel, value);
  626. } else if (CMD_ARGC == 3) {
  627. uint32_t value;
  628. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], value);
  629. retval = mips32_cp0_write(ejtag_info, value, cp0_reg, cp0_sel);
  630. if (retval != ERROR_OK) {
  631. command_print(CMD_CTX,
  632. "couldn't access cp0 reg %" PRIi32 ", select %" PRIi32,
  633. cp0_reg, cp0_sel);
  634. return ERROR_OK;
  635. }
  636. command_print(CMD_CTX, "cp0 reg %" PRIi32 ", select %" PRIi32 ": %8.8" PRIx32,
  637. cp0_reg, cp0_sel, value);
  638. }
  639. }
  640. return ERROR_OK;
  641. }
  642. static const struct command_registration mips32_exec_command_handlers[] = {
  643. {
  644. .name = "cp0",
  645. .handler = mips32_handle_cp0_command,
  646. .mode = COMMAND_EXEC,
  647. .usage = "regnum select [value]",
  648. .help = "display/modify cp0 register",
  649. },
  650. COMMAND_REGISTRATION_DONE
  651. };
  652. const struct command_registration mips32_command_handlers[] = {
  653. {
  654. .name = "mips32",
  655. .mode = COMMAND_ANY,
  656. .help = "mips32 command group",
  657. .usage = "",
  658. .chain = mips32_exec_command_handlers,
  659. },
  660. COMMAND_REGISTRATION_DONE
  661. };