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.
 
 
 
 
 
 

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