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.
 
 
 
 
 
 

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