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.
 
 
 
 
 
 

925 lines
30 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. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; either version 2 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with this program; if not, write to the *
  19. * Free Software Foundation, Inc., *
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  21. ***************************************************************************/
  22. /*
  23. This version has optimized assembly routines for 32 bit operations:
  24. - read word
  25. - write word
  26. - write array of words
  27. One thing to be aware of is that the MIPS32 cpu will execute the
  28. instruction after a branch instruction (one delay slot).
  29. For example:
  30. LW $2, ($5 +10)
  31. B foo
  32. LW $1, ($2 +100)
  33. The LW $1, ($2 +100) instruction is also executed. If this is
  34. not wanted a NOP can be inserted:
  35. LW $2, ($5 +10)
  36. B foo
  37. NOP
  38. LW $1, ($2 +100)
  39. or the code can be changed to:
  40. B foo
  41. LW $2, ($5 +10)
  42. LW $1, ($2 +100)
  43. The original code contained NOPs. I have removed these and moved
  44. the branches.
  45. I also moved the PRACC_STACK to 0xFF204000. This allows
  46. the use of 16 bits offsets to get pointers to the input
  47. and output area relative to the stack. Note that the stack
  48. isn't really a stack (the stack pointer is not 'moving')
  49. but a FIFO simulated in software.
  50. These changes result in a 35% speed increase when programming an
  51. external flash.
  52. More improvement could be gained if the registers do no need
  53. to be preserved but in that case the routines should be aware
  54. OpenOCD is used as a flash programmer or as a debug tool.
  55. Nico Coesel
  56. */
  57. #ifdef HAVE_CONFIG_H
  58. #include "config.h"
  59. #endif
  60. #include "mips32.h"
  61. #include "mips32_pracc.h"
  62. typedef struct {
  63. uint32_t *local_iparam;
  64. int num_iparam;
  65. uint32_t *local_oparam;
  66. int num_oparam;
  67. uint32_t *code;
  68. int code_len;
  69. uint32_t stack[32];
  70. int stack_offset;
  71. mips_ejtag_t *ejtag_info;
  72. } mips32_pracc_context;
  73. static int wait_for_pracc_rw(mips_ejtag_t *ejtag_info, uint32_t *ctrl)
  74. {
  75. uint32_t ejtag_ctrl;
  76. while (1)
  77. {
  78. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL, NULL);
  79. ejtag_ctrl = ejtag_info->ejtag_ctrl;
  80. mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  81. if (ejtag_ctrl & EJTAG_CTRL_PRACC)
  82. break;
  83. LOG_DEBUG("DEBUGMODULE: No memory access in progress!\n");
  84. return ERROR_JTAG_DEVICE_ERROR;
  85. }
  86. *ctrl = ejtag_ctrl;
  87. return ERROR_OK;
  88. }
  89. static int mips32_pracc_exec_read(mips32_pracc_context *ctx, uint32_t address)
  90. {
  91. mips_ejtag_t *ejtag_info = ctx->ejtag_info;
  92. int offset;
  93. uint32_t ejtag_ctrl, data;
  94. if ((address >= MIPS32_PRACC_PARAM_IN)
  95. && (address <= MIPS32_PRACC_PARAM_IN + ctx->num_iparam * 4))
  96. {
  97. offset = (address - MIPS32_PRACC_PARAM_IN) / 4;
  98. data = ctx->local_iparam[offset];
  99. }
  100. else if ((address >= MIPS32_PRACC_PARAM_OUT)
  101. && (address <= MIPS32_PRACC_PARAM_OUT + ctx->num_oparam * 4))
  102. {
  103. offset = (address - MIPS32_PRACC_PARAM_OUT) / 4;
  104. data = ctx->local_oparam[offset];
  105. }
  106. else if ((address >= MIPS32_PRACC_TEXT)
  107. && (address <= MIPS32_PRACC_TEXT + ctx->code_len*4))
  108. {
  109. offset = (address - MIPS32_PRACC_TEXT) / 4;
  110. data = ctx->code[offset];
  111. }
  112. else if (address == MIPS32_PRACC_STACK)
  113. {
  114. /* save to our debug stack */
  115. data = ctx->stack[--ctx->stack_offset];
  116. }
  117. else
  118. {
  119. /* TODO: send JMP 0xFF200000 instruction. Hopefully processor jump back
  120. * to start of debug vector */
  121. data = 0;
  122. LOG_ERROR("Error reading unexpected address %8.8" PRIx32 "", address);
  123. return ERROR_JTAG_DEVICE_ERROR;
  124. }
  125. /* Send the data out */
  126. mips_ejtag_set_instr(ctx->ejtag_info, EJTAG_INST_DATA, NULL);
  127. mips_ejtag_drscan_32(ctx->ejtag_info, &data);
  128. /* Clear the access pending bit (let the processor eat!) */
  129. ejtag_ctrl = ejtag_info->ejtag_ctrl & ~EJTAG_CTRL_PRACC;
  130. mips_ejtag_set_instr(ctx->ejtag_info, EJTAG_INST_CONTROL, NULL);
  131. mips_ejtag_drscan_32(ctx->ejtag_info, &ejtag_ctrl);
  132. jtag_add_clocks(5);
  133. jtag_execute_queue();
  134. return ERROR_OK;
  135. }
  136. static int mips32_pracc_exec_write(mips32_pracc_context *ctx, uint32_t address)
  137. {
  138. uint32_t ejtag_ctrl,data;
  139. int offset;
  140. mips_ejtag_t *ejtag_info = ctx->ejtag_info;
  141. mips_ejtag_set_instr(ctx->ejtag_info, EJTAG_INST_DATA, NULL);
  142. mips_ejtag_drscan_32(ctx->ejtag_info, &data);
  143. /* Clear access pending bit */
  144. ejtag_ctrl = ejtag_info->ejtag_ctrl & ~EJTAG_CTRL_PRACC;
  145. mips_ejtag_set_instr(ctx->ejtag_info, EJTAG_INST_CONTROL, NULL);
  146. mips_ejtag_drscan_32(ctx->ejtag_info, &ejtag_ctrl);
  147. jtag_add_clocks(5);
  148. jtag_execute_queue();
  149. if ((address >= MIPS32_PRACC_PARAM_IN)
  150. && (address <= MIPS32_PRACC_PARAM_IN + ctx->num_iparam * 4))
  151. {
  152. offset = (address - MIPS32_PRACC_PARAM_IN) / 4;
  153. ctx->local_iparam[offset] = data;
  154. }
  155. else if ((address >= MIPS32_PRACC_PARAM_OUT)
  156. && (address <= MIPS32_PRACC_PARAM_OUT + ctx->num_oparam * 4))
  157. {
  158. offset = (address - MIPS32_PRACC_PARAM_OUT) / 4;
  159. ctx->local_oparam[offset] = data;
  160. }
  161. else if (address == MIPS32_PRACC_STACK)
  162. {
  163. /* save data onto our stack */
  164. ctx->stack[ctx->stack_offset++] = data;
  165. }
  166. else
  167. {
  168. LOG_ERROR("Error writing unexpected address %8.8" PRIx32 "", address);
  169. return ERROR_JTAG_DEVICE_ERROR;
  170. }
  171. return ERROR_OK;
  172. }
  173. int mips32_pracc_exec(mips_ejtag_t *ejtag_info, int code_len, uint32_t *code, int num_param_in, uint32_t *param_in, int num_param_out, uint32_t *param_out, int cycle)
  174. {
  175. uint32_t ejtag_ctrl;
  176. uint32_t address, data;
  177. mips32_pracc_context ctx;
  178. int retval;
  179. int pass = 0;
  180. ctx.local_iparam = param_in;
  181. ctx.local_oparam = param_out;
  182. ctx.num_iparam = num_param_in;
  183. ctx.num_oparam = num_param_out;
  184. ctx.code = code;
  185. ctx.code_len = code_len;
  186. ctx.ejtag_info = ejtag_info;
  187. ctx.stack_offset = 0;
  188. while (1)
  189. {
  190. if ((retval = wait_for_pracc_rw(ejtag_info, &ejtag_ctrl)) != ERROR_OK)
  191. return retval;
  192. address = data = 0;
  193. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_ADDRESS, NULL);
  194. mips_ejtag_drscan_32(ejtag_info, &address);
  195. // printf("Adres: %.8x\n", address);
  196. /* Check for read or write */
  197. if (ejtag_ctrl & EJTAG_CTRL_PRNW)
  198. {
  199. if ((retval = mips32_pracc_exec_write(&ctx, address)) != ERROR_OK)
  200. return retval;
  201. }
  202. else
  203. {
  204. /* Check to see if its reading at the debug vector. The first pass through
  205. * the module is always read at the vector, so the first one we allow. When
  206. * the second read from the vector occurs we are done and just exit. */
  207. if ((address == MIPS32_PRACC_TEXT) && (pass++))
  208. {
  209. break;
  210. }
  211. if ((retval = mips32_pracc_exec_read(&ctx, address)) != ERROR_OK)
  212. return retval;
  213. }
  214. if (cycle == 0)
  215. break;
  216. }
  217. /* stack sanity check */
  218. if (ctx.stack_offset != 0)
  219. {
  220. LOG_DEBUG("Pracc Stack not zero");
  221. }
  222. return ERROR_OK;
  223. }
  224. int mips32_pracc_read_mem(mips_ejtag_t *ejtag_info, uint32_t addr, int size, int count, void *buf)
  225. {
  226. switch (size)
  227. {
  228. case 1:
  229. return mips32_pracc_read_mem8(ejtag_info, addr, count, (uint8_t*)buf);
  230. case 2:
  231. return mips32_pracc_read_mem16(ejtag_info, addr, count, (uint16_t*)buf);
  232. case 4:
  233. if (count == 1)
  234. return mips32_pracc_read_u32(ejtag_info, addr, (uint32_t*)buf);
  235. else
  236. return mips32_pracc_read_mem32(ejtag_info, addr, count, (uint32_t*)buf);
  237. }
  238. return ERROR_OK;
  239. }
  240. int mips32_pracc_read_mem32(mips_ejtag_t *ejtag_info, uint32_t addr, int count, uint32_t *buf)
  241. {
  242. uint32_t code[] = {
  243. /* start: */
  244. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  245. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  246. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  247. MIPS32_SW(8,0,15), /* sw $8,($15) */
  248. MIPS32_SW(9,0,15), /* sw $9,($15) */
  249. MIPS32_SW(10,0,15), /* sw $10,($15) */
  250. MIPS32_SW(11,0,15), /* sw $11,($15) */
  251. MIPS32_LUI(8,UPPER16(MIPS32_PRACC_PARAM_IN)), /* $8 = MIPS32_PRACC_PARAM_IN */
  252. MIPS32_ORI(8,8,LOWER16(MIPS32_PRACC_PARAM_IN)),
  253. MIPS32_LW(9,0,8), /* $9 = mem[$8]; read addr */
  254. MIPS32_LW(10,4,8), /* $10 = mem[$8 + 4]; read count */
  255. MIPS32_LUI(11,UPPER16(MIPS32_PRACC_PARAM_OUT)), /* $11 = MIPS32_PRACC_PARAM_OUT */
  256. MIPS32_ORI(11,11,LOWER16(MIPS32_PRACC_PARAM_OUT)),
  257. MIPS32_NOP,
  258. /* loop: */
  259. MIPS32_BEQ(0,10,9), /* beq 0, $10, end */
  260. MIPS32_NOP,
  261. MIPS32_LW(8,0,9), /* lw $8,0($9), Load $8 with the word @mem[$9] */
  262. MIPS32_SW(8,0,11), /* sw $8,0($11) */
  263. MIPS32_ADDI(10,10,NEG16(1)), /* $10-- */
  264. MIPS32_ADDI(9,9,4), /* $1 += 4 */
  265. MIPS32_ADDI(11,11,4), /* $11 += 4 */
  266. MIPS32_NOP,
  267. MIPS32_B(NEG16(9)), /* b loop */
  268. MIPS32_NOP,
  269. /* end: */
  270. MIPS32_LW(11,0,15), /* lw $11,($15) */
  271. MIPS32_LW(10,0,15), /* lw $10,($15) */
  272. MIPS32_LW(9,0,15), /* lw $9,($15) */
  273. MIPS32_LW(8,0,15), /* lw $8,($15) */
  274. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  275. MIPS32_NOP,
  276. MIPS32_B(NEG16(31)), /* b start */
  277. MIPS32_NOP,
  278. };
  279. int retval = ERROR_OK;
  280. int blocksize;
  281. int bytesread;
  282. uint32_t param_in[2];
  283. bytesread = 0;
  284. while (count > 0)
  285. {
  286. blocksize = count;
  287. if (count > 0x400)
  288. blocksize = 0x400;
  289. param_in[0] = addr;
  290. param_in[1] = blocksize;
  291. if ((retval = mips32_pracc_exec(ejtag_info, sizeof(code)/sizeof(code[0]), code,
  292. sizeof(param_in)/sizeof(param_in[0]), param_in, blocksize, &buf[bytesread], 1)) != ERROR_OK)
  293. {
  294. return retval;
  295. }
  296. count -= blocksize;
  297. addr += blocksize;
  298. bytesread += blocksize;
  299. }
  300. return retval;
  301. }
  302. int mips32_pracc_read_u32(mips_ejtag_t *ejtag_info, uint32_t addr, uint32_t *buf)
  303. {
  304. uint32_t code[] = {
  305. /* start: */
  306. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  307. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  308. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  309. MIPS32_SW(8,0,15), /* sw $8,($15) */
  310. MIPS32_LW(8,NEG16(MIPS32_PRACC_STACK-MIPS32_PRACC_PARAM_IN), 15), //load R8 @ param_in[0] = address
  311. MIPS32_LW(8,0,8), /* lw $8,0($8), Load $8 with the word @mem[$8] */
  312. MIPS32_SW(8,NEG16(MIPS32_PRACC_STACK-MIPS32_PRACC_PARAM_OUT),15), /* sw $8,0($9) */
  313. MIPS32_LW(8,0,15), /* lw $8,($15) */
  314. MIPS32_B(NEG16(9)), //was 17 /* b start */
  315. MIPS32_MFC0(15,31,0), //this instruction will be executed (MIPS executes instruction after jump) /* move COP0 DeSave to $15 */
  316. MIPS32_NOP,
  317. };
  318. int retval = ERROR_OK;
  319. uint32_t param_in[1];
  320. param_in[0] = addr;
  321. if ((retval = mips32_pracc_exec(ejtag_info, sizeof(code)/sizeof(code[0]), code,
  322. sizeof(param_in)/sizeof(param_in[0]), param_in, sizeof(uint32_t), buf, 1)) != ERROR_OK)
  323. {
  324. return retval;
  325. }
  326. return retval;
  327. }
  328. int mips32_pracc_read_mem16(mips_ejtag_t *ejtag_info, uint32_t addr, int count, uint16_t *buf)
  329. {
  330. uint32_t code[] = {
  331. /* start: */
  332. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  333. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  334. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  335. MIPS32_SW(8,0,15), /* sw $8,($15) */
  336. MIPS32_SW(9,0,15), /* sw $9,($15) */
  337. MIPS32_SW(10,0,15), /* sw $10,($15) */
  338. MIPS32_SW(11,0,15), /* sw $11,($15) */
  339. MIPS32_LUI(8,UPPER16(MIPS32_PRACC_PARAM_IN)), /* $8 = MIPS32_PRACC_PARAM_IN */
  340. MIPS32_ORI(8,8,LOWER16(MIPS32_PRACC_PARAM_IN)),
  341. MIPS32_LW(9,0,8), /* $9 = mem[$8]; read addr */
  342. MIPS32_LW(10,4,8), /* $10 = mem[$8 + 4]; read count */
  343. MIPS32_LUI(11,UPPER16(MIPS32_PRACC_PARAM_OUT)), /* $11 = MIPS32_PRACC_PARAM_OUT */
  344. MIPS32_ORI(11,11,LOWER16(MIPS32_PRACC_PARAM_OUT)),
  345. MIPS32_NOP,
  346. /* loop: */
  347. MIPS32_BEQ(0,10,9), /* beq 0, $10, end */
  348. MIPS32_NOP,
  349. MIPS32_LHU(8,0,9), /* lw $8,0($9), Load $8 with the halfword @mem[$9] */
  350. MIPS32_SW(8,0,11), /* sw $8,0($11) */
  351. MIPS32_ADDI(10,10,NEG16(1)), /* $10-- */
  352. MIPS32_ADDI(9,9,2), /* $9 += 2 */
  353. MIPS32_ADDI(11,11,4), /* $11 += 4 */
  354. MIPS32_NOP,
  355. MIPS32_B(NEG16(9)), /* b loop */
  356. MIPS32_NOP,
  357. MIPS32_LW(11,0,15), /* lw $11,($15) */
  358. MIPS32_LW(10,0,15), /* lw $10,($15) */
  359. MIPS32_LW(9,0,15), /* lw $9,($15) */
  360. MIPS32_LW(8,0,15), /* lw $8,($15) */
  361. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  362. MIPS32_NOP,
  363. MIPS32_B(NEG16(31)), /* b start */
  364. MIPS32_NOP,
  365. };
  366. // /* TODO remove array */
  367. uint32_t param_out[count];
  368. int i;
  369. // int retval;
  370. int blocksize;
  371. int bytesread;
  372. uint32_t param_in[2];
  373. bytesread = 0;
  374. //while (count > 0)
  375. {
  376. blocksize = count;
  377. if (count > 0x400)
  378. blocksize = 0x400;
  379. param_in[0] = addr;
  380. param_in[1] = blocksize;
  381. mips32_pracc_exec(ejtag_info, sizeof(code)/sizeof(code[0]), code, \
  382. sizeof(param_in)/sizeof(param_in[0]), param_in, count, param_out, 1);
  383. // count -= blocksize;
  384. // addr += blocksize;
  385. // bytesread += blocksize;
  386. }
  387. for (i = 0; i < count; i++)
  388. {
  389. buf[i] = param_out[i];
  390. }
  391. return ERROR_OK;
  392. }
  393. int mips32_pracc_read_mem8(mips_ejtag_t *ejtag_info, uint32_t addr, int count, uint8_t *buf)
  394. {
  395. uint32_t code[] = {
  396. /* start: */
  397. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  398. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  399. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  400. MIPS32_SW(8,0,15), /* sw $8,($15) */
  401. MIPS32_SW(9,0,15), /* sw $9,($15) */
  402. MIPS32_SW(10,0,15), /* sw $10,($15) */
  403. MIPS32_SW(11,0,15), /* sw $11,($15) */
  404. MIPS32_LUI(8,UPPER16(MIPS32_PRACC_PARAM_IN)), /* $8 = MIPS32_PRACC_PARAM_IN */
  405. MIPS32_ORI(8,8,LOWER16(MIPS32_PRACC_PARAM_IN)),
  406. MIPS32_LW(9,0,8), /* $9 = mem[$8]; read addr */
  407. MIPS32_LW(10,4,8), /* $10 = mem[$8 + 4]; read count */
  408. MIPS32_LUI(11,UPPER16(MIPS32_PRACC_PARAM_OUT)), /* $11 = MIPS32_PRACC_PARAM_OUT */
  409. MIPS32_ORI(11,11,LOWER16(MIPS32_PRACC_PARAM_OUT)),
  410. MIPS32_NOP,
  411. /* loop: */
  412. MIPS32_BEQ(0,10,9), /* beq 0, $10, end */
  413. MIPS32_NOP,
  414. MIPS32_LBU(8,0,9), /* lw $8,0($9), Load t4 with the byte @mem[t1] */
  415. MIPS32_SW(8,0,11), /* sw $8,0($11) */
  416. MIPS32_ADDI(10,10,NEG16(1)), /* $10-- */
  417. MIPS32_ADDI(9,9,1), /* $9 += 1 */
  418. MIPS32_ADDI(11,11,4), /* $11 += 4 */
  419. MIPS32_NOP,
  420. MIPS32_B(NEG16(9)), /* b loop */
  421. MIPS32_NOP,
  422. /* end: */
  423. MIPS32_LW(11,0,15), /* lw $11,($15) */
  424. MIPS32_LW(10,0,15), /* lw $10,($15) */
  425. MIPS32_LW(9,0,15), /* lw $9,($15) */
  426. MIPS32_LW(8,0,15), /* lw $8,($15) */
  427. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  428. MIPS32_NOP,
  429. MIPS32_B(NEG16(31)), /* b start */
  430. MIPS32_NOP,
  431. };
  432. // /* TODO remove array */
  433. uint32_t param_out[count];
  434. int i;
  435. // int retval;
  436. int blocksize;
  437. int bytesread;
  438. uint32_t param_in[2];
  439. bytesread = 0;
  440. // while (count > 0)
  441. {
  442. blocksize = count;
  443. if (count > 0x400)
  444. blocksize = 0x400;
  445. param_in[0] = addr;
  446. param_in[1] = blocksize;
  447. mips32_pracc_exec(ejtag_info, sizeof(code)/sizeof(code[0]), code, \
  448. sizeof(param_in)/sizeof(param_in[0]), param_in, count, param_out, 1);
  449. // count -= blocksize;
  450. // addr += blocksize;
  451. // bytesread += blocksize;
  452. }
  453. for (i = 0; i < count; i++)
  454. {
  455. buf[i] = param_out[i];
  456. }
  457. return ERROR_OK;
  458. }
  459. int mips32_pracc_write_mem(mips_ejtag_t *ejtag_info, uint32_t addr, int size, int count, void *buf)
  460. {
  461. switch (size)
  462. {
  463. case 1:
  464. return mips32_pracc_write_mem8(ejtag_info, addr, count, (uint8_t*)buf);
  465. case 2:
  466. return mips32_pracc_write_mem16(ejtag_info, addr, count,(uint16_t*)buf);
  467. case 4:
  468. if (count == 1)
  469. return mips32_pracc_write_u32(ejtag_info, addr, (uint32_t*)buf);
  470. else
  471. return mips32_pracc_write_mem32(ejtag_info, addr, count, (uint32_t*)buf);
  472. }
  473. return ERROR_OK;
  474. }
  475. int mips32_pracc_write_mem32(mips_ejtag_t *ejtag_info, uint32_t addr, int count, uint32_t *buf)
  476. {
  477. //NC: use destination pointer as loop counter (last address is in $10)
  478. uint32_t code[] = {
  479. /* start: */
  480. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  481. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  482. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  483. MIPS32_SW(8,0,15), /* sw $8,($15) */
  484. MIPS32_SW(9,0,15), /* sw $9,($15) */
  485. MIPS32_SW(10,0,15), /* sw $10,($15) */
  486. MIPS32_SW(11,0,15), /* sw $11,($15) */
  487. MIPS32_ADDI(8,15,NEG16(MIPS32_PRACC_STACK-MIPS32_PRACC_PARAM_IN)), //$8= MIPS32_PRACC_PARAM_IN
  488. MIPS32_LW(9,0,8), /* Load write addr to $9 */
  489. MIPS32_LW(10,4,8), //last address /* Load write count to $10 */
  490. MIPS32_ADDI(8,8,8), // $8 += 8 beginning of data
  491. //loop:
  492. MIPS32_LW(11,0,8), /* lw $11,0($8), Load $11 with the word @mem[$8] */
  493. MIPS32_SW(11,0,9), /* sw $11,0($9) */
  494. MIPS32_ADDI(9,9,4), /* $9 += 4 */
  495. MIPS32_BNE(10,9,NEG16(4)), //was 9 BNE $10, 9, loop /* b loop */
  496. MIPS32_ADDI(8,8,4), //this instruction is part of the loop (one delay slot)! /* $8 += 4 */
  497. /* end: */
  498. MIPS32_LW(11,0,15), /* lw $11,($15) */
  499. MIPS32_LW(10,0,15), /* lw $10,($15) */
  500. MIPS32_LW(9,0,15), /* lw $9,($15) */
  501. MIPS32_LW(8,0,15), /* lw $8,($15) */
  502. MIPS32_B(NEG16(21)), //was 30 /* b start */
  503. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  504. MIPS32_NOP, //this one will not be executed
  505. };
  506. /* TODO remove array */
  507. uint32_t param_in[count + 2];
  508. param_in[0] = addr;
  509. param_in[1] = addr + count * sizeof(uint32_t); //last address
  510. memcpy(&param_in[2], buf, count * sizeof(uint32_t));
  511. mips32_pracc_exec(ejtag_info, sizeof(code)/sizeof(code[0]), code, \
  512. sizeof(param_in)/sizeof(param_in[0]),param_in, 0, NULL, 1);
  513. return ERROR_OK;
  514. }
  515. int mips32_pracc_write_u32(mips_ejtag_t *ejtag_info, uint32_t addr, uint32_t *buf)
  516. {
  517. uint32_t code[] = {
  518. /* start: */
  519. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  520. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  521. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  522. MIPS32_SW(8,0,15), /* sw $8,($15) */
  523. MIPS32_SW(9,0,15), /* sw $9,($15) */
  524. MIPS32_LW(8,NEG16((MIPS32_PRACC_STACK-MIPS32_PRACC_PARAM_IN)-4), 15), //load R8 @ param_in[1] = data
  525. MIPS32_LW(9,NEG16(MIPS32_PRACC_STACK-MIPS32_PRACC_PARAM_IN), 15), //load R9 @ param_in[0] = address
  526. MIPS32_SW(8,0,9), /* sw $8,0($9) */
  527. MIPS32_LW(9,0,15), /* lw $9,($15) */
  528. MIPS32_LW(8,0,15), /* lw $8,($15) */
  529. MIPS32_B(NEG16(11)), /* b start */
  530. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  531. MIPS32_NOP,
  532. };
  533. /* TODO remove array */
  534. uint32_t param_in[1 + 1];
  535. param_in[0] = addr;
  536. param_in[1] = *buf;
  537. mips32_pracc_exec(ejtag_info, sizeof(code)/sizeof(code[0]), code, \
  538. sizeof(param_in)/sizeof(param_in[0]),param_in, 0, NULL, 1);
  539. return ERROR_OK;
  540. }
  541. int mips32_pracc_write_mem16(mips_ejtag_t *ejtag_info, uint32_t addr, int count, uint16_t *buf)
  542. {
  543. uint32_t code[] = {
  544. /* start: */
  545. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  546. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  547. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  548. MIPS32_SW(8,0,15), /* sw $8,($15) */
  549. MIPS32_SW(9,0,15), /* sw $9,($15) */
  550. MIPS32_SW(10,0,15), /* sw $10,($15) */
  551. MIPS32_SW(11,0,15), /* sw $11,($15) */
  552. MIPS32_LUI(8,UPPER16(MIPS32_PRACC_PARAM_IN)), /* $8 = MIPS32_PRACC_PARAM_IN */
  553. MIPS32_ORI(8,8,LOWER16(MIPS32_PRACC_PARAM_IN)),
  554. MIPS32_LW(9,0,8), /* Load write addr to $9 */
  555. MIPS32_LW(10,4,8), /* Load write count to $10 */
  556. MIPS32_ADDI(8,8,8), /* $8 += 8 */
  557. MIPS32_NOP,
  558. /* loop: */
  559. MIPS32_BEQ(0,10,9), /* beq $0, $10, end */
  560. MIPS32_NOP,
  561. MIPS32_LW(11,0,8), /* lw $11,0($8), Load $11 with the word @mem[$8] */
  562. MIPS32_SH(11,0,9), /* sh $11,0($9) */
  563. MIPS32_ADDI(10,10,NEG16(1)), /* $10-- */
  564. MIPS32_ADDI(9,9,2), /* $9 += 2 */
  565. MIPS32_ADDI(8,8,4), /* $8 += 4 */
  566. MIPS32_NOP,
  567. MIPS32_B(NEG16(9)), /* b loop */
  568. MIPS32_NOP,
  569. /* end: */
  570. MIPS32_LW(11,0,15), /* lw $11,($15) */
  571. MIPS32_LW(10,0,15), /* lw $10,($15) */
  572. MIPS32_LW(9,0,15), /* lw $9,($15) */
  573. MIPS32_LW(8,0,15), /* lw $8,($15) */
  574. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  575. MIPS32_NOP,
  576. MIPS32_B(NEG16(30)), /* b start */
  577. MIPS32_NOP,
  578. };
  579. /* TODO remove array */
  580. uint32_t param_in[count + 2];
  581. int i;
  582. param_in[0] = addr;
  583. param_in[1] = count;
  584. for (i = 0; i < count; i++)
  585. {
  586. param_in[i + 2] = buf[i];
  587. }
  588. mips32_pracc_exec(ejtag_info, sizeof(code)/sizeof(code[0]), code, \
  589. sizeof(param_in)/sizeof(param_in[0]), param_in, 0, NULL, 1);
  590. return ERROR_OK;
  591. }
  592. int mips32_pracc_write_mem8(mips_ejtag_t *ejtag_info, uint32_t addr, int count, uint8_t *buf)
  593. {
  594. uint32_t code[] = {
  595. /* start: */
  596. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  597. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  598. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  599. MIPS32_SW(8,0,15), /* sw $8,($15) */
  600. MIPS32_SW(9,0,15), /* sw $9,($15) */
  601. MIPS32_SW(10,0,15), /* sw $10,($15) */
  602. MIPS32_SW(11,0,15), /* sw $11,($15) */
  603. MIPS32_LUI(8,UPPER16(MIPS32_PRACC_PARAM_IN)), /* $8 = MIPS32_PRACC_PARAM_IN */
  604. MIPS32_ORI(8,8,LOWER16(MIPS32_PRACC_PARAM_IN)),
  605. MIPS32_LW(9,0,8), /* Load write addr to $9 */
  606. MIPS32_LW(10,4,8), /* Load write count to $10 */
  607. MIPS32_ADDI(8,8,8), /* $8 += 8 */
  608. MIPS32_NOP,
  609. /* loop: */
  610. MIPS32_BEQ(0,10,9), /* beq $0, $10, end */
  611. MIPS32_NOP,
  612. MIPS32_LW(11,0,8), /* lw $11,0($8), Load $11 with the word @mem[$8] */
  613. MIPS32_SB(11,0,9), /* sb $11,0($9) */
  614. MIPS32_ADDI(10,10,NEG16(1)), /* $10-- */
  615. MIPS32_ADDI(9,9,1), /* $9 += 1 */
  616. MIPS32_ADDI(8,8,4), /* $8 += 4 */
  617. MIPS32_NOP,
  618. MIPS32_B(NEG16(9)), /* b loop */
  619. MIPS32_NOP,
  620. /* end: */
  621. MIPS32_LW(11,0,15), /* lw $11,($15) */
  622. MIPS32_LW(10,0,15), /* lw $10,($15) */
  623. MIPS32_LW(9,0,15), /* lw $9,($15) */
  624. MIPS32_LW(8,0,15), /* lw $8,($15) */
  625. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  626. MIPS32_NOP,
  627. MIPS32_B(NEG16(30)), /* b start */
  628. MIPS32_NOP,
  629. };
  630. /* TODO remove array */
  631. uint32_t param_in[count + 2];
  632. int retval;
  633. int i;
  634. param_in[0] = addr;
  635. param_in[1] = count;
  636. for (i = 0; i < count; i++)
  637. {
  638. param_in[i + 2] = buf[i];
  639. }
  640. retval = mips32_pracc_exec(ejtag_info, sizeof(code)/sizeof(code[0]), code, \
  641. sizeof(param_in)/sizeof(param_in[0]), param_in, 0, NULL, 1);
  642. return retval;
  643. }
  644. int mips32_pracc_write_regs(mips_ejtag_t *ejtag_info, uint32_t *regs)
  645. {
  646. uint32_t code[] = {
  647. /* start: */
  648. MIPS32_LUI(2,UPPER16(MIPS32_PRACC_PARAM_IN)), /* $2 = MIPS32_PRACC_PARAM_IN */
  649. MIPS32_ORI(2,2,LOWER16(MIPS32_PRACC_PARAM_IN)),
  650. MIPS32_LW(1,1*4,2), /* lw $1,1*4($2) */
  651. MIPS32_LW(15,15*4,2), /* lw $15,15*4($2) */
  652. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  653. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  654. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  655. MIPS32_SW(1,0,15), /* sw $1,($15) */
  656. MIPS32_LUI(1,UPPER16(MIPS32_PRACC_PARAM_IN)), /* $1 = MIPS32_PRACC_PARAM_IN */
  657. MIPS32_ORI(1,1,LOWER16(MIPS32_PRACC_PARAM_IN)),
  658. MIPS32_LW(3,3*4,1), /* lw $3,3*4($1) */
  659. MIPS32_LW(4,4*4,1), /* lw $4,4*4($1) */
  660. MIPS32_LW(5,5*4,1), /* lw $5,5*4($1) */
  661. MIPS32_LW(6,6*4,1), /* lw $6,6*4($1) */
  662. MIPS32_LW(7,7*4,1), /* lw $7,7*4($1) */
  663. MIPS32_LW(8,8*4,1), /* lw $8,8*4($1) */
  664. MIPS32_LW(9,9*4,1), /* lw $9,9*4($1) */
  665. MIPS32_LW(10,10*4,1), /* lw $10,10*4($1) */
  666. MIPS32_LW(11,11*4,1), /* lw $11,11*4($1) */
  667. MIPS32_LW(12,12*4,1), /* lw $12,12*4($1) */
  668. MIPS32_LW(13,13*4,1), /* lw $13,13*4($1) */
  669. MIPS32_LW(14,14*4,1), /* lw $14,14*4($1) */
  670. MIPS32_LW(16,16*4,1), /* lw $16,16*4($1) */
  671. MIPS32_LW(17,17*4,1), /* lw $17,17*4($1) */
  672. MIPS32_LW(18,18*4,1), /* lw $18,18*4($1) */
  673. MIPS32_LW(19,19*4,1), /* lw $19,19*4($1) */
  674. MIPS32_LW(20,20*4,1), /* lw $20,20*4($1) */
  675. MIPS32_LW(21,21*4,1), /* lw $21,21*4($1) */
  676. MIPS32_LW(22,22*4,1), /* lw $22,22*4($1) */
  677. MIPS32_LW(23,23*4,1), /* lw $23,23*4($1) */
  678. MIPS32_LW(24,24*4,1), /* lw $24,24*4($1) */
  679. MIPS32_LW(25,25*4,1), /* lw $25,25*4($1) */
  680. MIPS32_LW(26,26*4,1), /* lw $26,26*4($1) */
  681. MIPS32_LW(27,27*4,1), /* lw $27,27*4($1) */
  682. MIPS32_LW(28,28*4,1), /* lw $28,28*4($1) */
  683. MIPS32_LW(29,29*4,1), /* lw $29,29*4($1) */
  684. MIPS32_LW(30,30*4,1), /* lw $30,30*4($1) */
  685. MIPS32_LW(31,31*4,1), /* lw $31,31*4($1) */
  686. MIPS32_LW(2,32*4,1), /* lw $2,32*4($1) */
  687. MIPS32_MTC0(2,12,0), /* move $2 to status */
  688. MIPS32_LW(2,33*4,1), /* lw $2,33*4($1) */
  689. MIPS32_MTLO(2), /* move $2 to lo */
  690. MIPS32_LW(2,34*4,1), /* lw $2,34*4($1) */
  691. MIPS32_MTHI(2), /* move $2 to hi */
  692. MIPS32_LW(2,35*4,1), /* lw $2,35*4($1) */
  693. MIPS32_MTC0(2,8,0), /* move $2 to badvaddr */
  694. MIPS32_LW(2,36*4,1), /* lw $2,36*4($1) */
  695. MIPS32_MTC0(2,13,0), /* move $2 to cause*/
  696. MIPS32_LW(2,37*4,1), /* lw $2,37*4($1) */
  697. MIPS32_MTC0(2,24,0), /* move $2 to pc */
  698. MIPS32_LW(2,2*4,1), /* lw $2,2*4($1) */
  699. MIPS32_LW(1,0,15), /* lw $1,($15) */
  700. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  701. MIPS32_NOP,
  702. MIPS32_B(NEG16(55)), /* b start */
  703. MIPS32_NOP,
  704. };
  705. int retval;
  706. retval = mips32_pracc_exec(ejtag_info, sizeof(code)/sizeof(code[0]), code, \
  707. 38, regs, 0, NULL, 1);
  708. return retval;
  709. }
  710. int mips32_pracc_read_regs(mips_ejtag_t *ejtag_info, uint32_t *regs)
  711. {
  712. uint32_t code[] = {
  713. /* start: */
  714. MIPS32_MTC0(2,31,0), /* move $2 to COP0 DeSave */
  715. MIPS32_LUI(2,UPPER16(MIPS32_PRACC_PARAM_OUT)), /* $2 = MIPS32_PRACC_PARAM_OUT */
  716. MIPS32_ORI(2,2,LOWER16(MIPS32_PRACC_PARAM_OUT)),
  717. MIPS32_SW(0,0*4,2), /* sw $0,0*4($2) */
  718. MIPS32_SW(1,1*4,2), /* sw $1,1*4($2) */
  719. MIPS32_SW(15,15*4,2), /* sw $15,15*4($2) */
  720. MIPS32_MFC0(2,31,0), /* move COP0 DeSave to $2 */
  721. MIPS32_MTC0(15,31,0), /* move $15 to COP0 DeSave */
  722. MIPS32_LUI(15,UPPER16(MIPS32_PRACC_STACK)), /* $15 = MIPS32_PRACC_STACK */
  723. MIPS32_ORI(15,15,LOWER16(MIPS32_PRACC_STACK)),
  724. MIPS32_SW(1,0,15), /* sw $1,($15) */
  725. MIPS32_SW(2,0,15), /* sw $2,($15) */
  726. MIPS32_LUI(1,UPPER16(MIPS32_PRACC_PARAM_OUT)), /* $1 = MIPS32_PRACC_PARAM_OUT */
  727. MIPS32_ORI(1,1,LOWER16(MIPS32_PRACC_PARAM_OUT)),
  728. MIPS32_SW(2,2*4,1), /* sw $2,2*4($1) */
  729. MIPS32_SW(3,3*4,1), /* sw $3,3*4($1) */
  730. MIPS32_SW(4,4*4,1), /* sw $4,4*4($1) */
  731. MIPS32_SW(5,5*4,1), /* sw $5,5*4($1) */
  732. MIPS32_SW(6,6*4,1), /* sw $6,6*4($1) */
  733. MIPS32_SW(7,7*4,1), /* sw $7,7*4($1) */
  734. MIPS32_SW(8,8*4,1), /* sw $8,8*4($1) */
  735. MIPS32_SW(9,9*4,1), /* sw $9,9*4($1) */
  736. MIPS32_SW(10,10*4,1), /* sw $10,10*4($1) */
  737. MIPS32_SW(11,11*4,1), /* sw $11,11*4($1) */
  738. MIPS32_SW(12,12*4,1), /* sw $12,12*4($1) */
  739. MIPS32_SW(13,13*4,1), /* sw $13,13*4($1) */
  740. MIPS32_SW(14,14*4,1), /* sw $14,14*4($1) */
  741. MIPS32_SW(16,16*4,1), /* sw $16,16*4($1) */
  742. MIPS32_SW(17,17*4,1), /* sw $17,17*4($1) */
  743. MIPS32_SW(18,18*4,1), /* sw $18,18*4($1) */
  744. MIPS32_SW(19,19*4,1), /* sw $19,19*4($1) */
  745. MIPS32_SW(20,20*4,1), /* sw $20,20*4($1) */
  746. MIPS32_SW(21,21*4,1), /* sw $21,21*4($1) */
  747. MIPS32_SW(22,22*4,1), /* sw $22,22*4($1) */
  748. MIPS32_SW(23,23*4,1), /* sw $23,23*4($1) */
  749. MIPS32_SW(24,24*4,1), /* sw $24,24*4($1) */
  750. MIPS32_SW(25,25*4,1), /* sw $25,25*4($1) */
  751. MIPS32_SW(26,26*4,1), /* sw $26,26*4($1) */
  752. MIPS32_SW(27,27*4,1), /* sw $27,27*4($1) */
  753. MIPS32_SW(28,28*4,1), /* sw $28,28*4($1) */
  754. MIPS32_SW(29,29*4,1), /* sw $29,29*4($1) */
  755. MIPS32_SW(30,30*4,1), /* sw $30,30*4($1) */
  756. MIPS32_SW(31,31*4,1), /* sw $31,31*4($1) */
  757. MIPS32_MFC0(2,12,0), /* move status to $2 */
  758. MIPS32_SW(2,32*4,1), /* sw $2,32*4($1) */
  759. MIPS32_MFLO(2), /* move lo to $2 */
  760. MIPS32_SW(2,33*4,1), /* sw $2,33*4($1) */
  761. MIPS32_MFHI(2), /* move hi to $2 */
  762. MIPS32_SW(2,34*4,1), /* sw $2,34*4($1) */
  763. MIPS32_MFC0(2,8,0), /* move badvaddr to $2 */
  764. MIPS32_SW(2,35*4,1), /* sw $2,35*4($1) */
  765. MIPS32_MFC0(2,13,0), /* move cause to $2 */
  766. MIPS32_SW(2,36*4,1), /* sw $2,36*4($1) */
  767. MIPS32_MFC0(2,24,0), /* move pc to $2 */
  768. MIPS32_SW(2,37*4,1), /* sw $2,37*4($1) */
  769. MIPS32_LW(2,0,15), /* lw $2,($15) */
  770. MIPS32_LW(1,0,15), /* lw $1,($15) */
  771. MIPS32_MFC0(15,31,0), /* move COP0 DeSave to $15 */
  772. MIPS32_NOP,
  773. MIPS32_B(NEG16(60)), /* b start */
  774. MIPS32_NOP,
  775. };
  776. int retval;
  777. retval = mips32_pracc_exec(ejtag_info, sizeof(code)/sizeof(code[0]), code, \
  778. 0, NULL, 38, regs, 1);
  779. return retval;
  780. }