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.
 
 
 
 
 
 

1498 lines
52 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2011 by Rodrigo L. Rosa *
  3. * rodrigorosa.LG@gmail.com *
  4. * *
  5. * Based on dsp563xx_once.h written by Mathias Kuester *
  6. * mkdorg@users.sourceforge.net *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "target.h"
  27. #include "target_type.h"
  28. #include "dsp5680xx.h"
  29. struct dsp5680xx_common dsp5680xx_context;
  30. #define err_check(retval,err_msg) if(retval != ERROR_OK){LOG_ERROR("%s: %d %s.",__FUNCTION__,__LINE__,err_msg);return retval;}
  31. #define err_check_propagate(retval) if(retval!=ERROR_OK){return retval;}
  32. int dsp5680xx_execute_queue(void){
  33. int retval;
  34. retval = jtag_execute_queue();
  35. err_check_propagate(retval);
  36. return retval;
  37. }
  38. static int dsp5680xx_drscan(struct target * target, uint8_t * data_to_shift_into_dr, uint8_t * data_shifted_out_of_dr, int len){
  39. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  40. //
  41. // Inputs:
  42. // - data_to_shift_into_dr: This is the data that will be shifted into the JTAG DR reg.
  43. // - data_shifted_out_of_dr: The data that will be shifted out of the JTAG DR reg will stored here
  44. // - len: Length of the data to be shifted to JTAG DR.
  45. //
  46. // Note: If data_shifted_out_of_dr == NULL, discard incoming bits.
  47. //
  48. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  49. int retval = ERROR_OK;
  50. if (NULL == target->tap){
  51. retval = ERROR_FAIL;
  52. err_check(retval,"Invalid tap");
  53. }
  54. if (len > 32){
  55. retval = ERROR_FAIL;
  56. err_check(retval,"dr_len overflow, maxium is 32");
  57. }
  58. //TODO what values of len are valid for jtag_add_plain_dr_scan?
  59. //can i send as many bits as i want?
  60. //is the casting necessary?
  61. jtag_add_plain_dr_scan(len,data_to_shift_into_dr,data_shifted_out_of_dr, TAP_IDLE);
  62. if(dsp5680xx_context.flush){
  63. retval = dsp5680xx_execute_queue();
  64. err_check_propagate(retval);
  65. }
  66. if(data_shifted_out_of_dr!=NULL){
  67. LOG_DEBUG("Data read (%d bits): 0x%04X",len,*data_shifted_out_of_dr);
  68. }else
  69. LOG_DEBUG("Data read was discarded.");
  70. return retval;
  71. }
  72. static int dsp5680xx_irscan(struct target * target, uint32_t * data_to_shift_into_ir, uint32_t * data_shifted_out_of_ir, uint8_t ir_len){
  73. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  74. // Inputs:
  75. // - data_to_shift_into_ir: This is the data that will be shifted into the JTAG IR reg.
  76. // - data_shifted_out_of_ir: The data that will be shifted out of the JTAG IR reg will stored here
  77. // - len: Length of the data to be shifted to JTAG IR.
  78. //
  79. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  80. int retval = ERROR_OK;
  81. if (NULL == target->tap){
  82. retval = ERROR_FAIL;
  83. err_check(retval,"Invalid tap");
  84. }
  85. if (ir_len != target->tap->ir_length){
  86. LOG_WARNING("%s: Invalid ir_len of core tap. If you are removing protection on flash then do not worry about this warninig.",__FUNCTION__);
  87. //return ERROR_FAIL;//TODO this was commented out to enable unlocking using the master tap. did not find a way to enable the master tap without using tcl.
  88. }
  89. //TODO what values of len are valid for jtag_add_plain_ir_scan?
  90. //can i send as many bits as i want?
  91. //is the casting necessary?
  92. jtag_add_plain_ir_scan(ir_len,(uint8_t *)data_to_shift_into_ir,(uint8_t *)data_shifted_out_of_ir, TAP_IDLE);
  93. if(dsp5680xx_context.flush){
  94. retval = dsp5680xx_execute_queue();
  95. err_check_propagate(retval);
  96. }
  97. return retval;
  98. }
  99. static int dsp5680xx_jtag_status(struct target *target, uint8_t * status){
  100. uint32_t read_from_ir;
  101. uint32_t instr;
  102. int retval;
  103. instr = JTAG_INSTR_ENABLE_ONCE;
  104. retval = dsp5680xx_irscan(target,& instr, & read_from_ir,DSP5680XX_JTAG_CORE_TAP_IRLEN);
  105. err_check_propagate(retval);
  106. if(status!=NULL)
  107. *status = (uint8_t)read_from_ir;
  108. return ERROR_OK;
  109. }
  110. static int jtag_data_read(struct target * target, uint8_t * data_read, int num_bits){
  111. uint32_t bogus_instr = 0;
  112. int retval = dsp5680xx_drscan(target,(uint8_t *) & bogus_instr,data_read,num_bits);
  113. LOG_DEBUG("Data read (%d bits): 0x%04X",num_bits,*data_read);//TODO remove this or move to jtagio?
  114. return retval;
  115. }
  116. #define jtag_data_read8(target,data_read) jtag_data_read(target,data_read,8)
  117. #define jtag_data_read16(target,data_read) jtag_data_read(target,data_read,16)
  118. #define jtag_data_read32(target,data_read) jtag_data_read(target,data_read,32)
  119. static uint32_t data_read_dummy;
  120. static int jtag_data_write(struct target * target, uint32_t instr,int num_bits, uint32_t * data_read){
  121. int retval;
  122. retval = dsp5680xx_drscan(target,(uint8_t *) & instr,(uint8_t *) & data_read_dummy,num_bits);
  123. err_check_propagate(retval);
  124. if(data_read != NULL)
  125. *data_read = data_read_dummy;
  126. return retval;
  127. }
  128. #define jtag_data_write8(target,instr,data_read) jtag_data_write(target,instr,8,data_read)
  129. #define jtag_data_write16(target,instr,data_read) jtag_data_write(target,instr,16,data_read)
  130. #define jtag_data_write24(target,instr,data_read) jtag_data_write(target,instr,24,data_read)
  131. #define jtag_data_write32(target,instr,data_read) jtag_data_write(target,instr,32,data_read)
  132. /**
  133. * Executes DSP instruction.
  134. *
  135. * @param target
  136. * @param instr Instruction to execute.
  137. * @param rw
  138. * @param go
  139. * @param ex
  140. * @param eonce_status Value read from the EOnCE status register.
  141. *
  142. * @return
  143. */
  144. static int eonce_instruction_exec(struct target * target, uint8_t instr, uint8_t rw, uint8_t go, uint8_t ex,uint8_t * eonce_status){
  145. int retval;
  146. uint32_t dr_out_tmp;
  147. uint8_t instr_with_flags = instr|(rw<<7)|(go<<6)|(ex<<5);
  148. retval = jtag_data_write(target,instr_with_flags,8,&dr_out_tmp);
  149. err_check_propagate(retval);
  150. if(eonce_status != NULL)
  151. *eonce_status = (uint8_t) dr_out_tmp;
  152. return retval;
  153. }
  154. ///wrappers for parameter conversion between eonce_execute_instruction and eonce_execute_instructionX
  155. #define eonce_execute_instruction_1(target,opcode1,opcode2,opcode3) eonce_execute_instruction1(target,opcode1)
  156. #define eonce_execute_instruction_2(target,opcode1,opcode2,opcode3) eonce_execute_instruction2(target,opcode1,opcode2)
  157. #define eonce_execute_instruction_3(target,opcode1,opcode2,opcode3) eonce_execute_instruction3(target,opcode1,opcode2,opcode3)
  158. #define eonce_execute_instruction(target,words,opcode1,opcode2,opcode3) eonce_execute_instruction_##words(target,opcode1,opcode2,opcode3)
  159. /// Executes one word DSP instruction
  160. static int eonce_execute_instruction1(struct target * target, uint16_t opcode){
  161. int retval;
  162. retval = eonce_instruction_exec(target,0x04,0,1,0,NULL);
  163. err_check_propagate(retval);
  164. retval = jtag_data_write16(target,opcode,NULL);
  165. err_check_propagate(retval);
  166. return retval;
  167. }
  168. /// Executes two word DSP instruction
  169. static int eonce_execute_instruction2(struct target * target,uint16_t opcode1, uint16_t opcode2){
  170. int retval;
  171. retval = eonce_instruction_exec(target,0x04,0,0,0,NULL);
  172. err_check_propagate(retval);
  173. retval = jtag_data_write16(target,opcode1,NULL);
  174. err_check_propagate(retval);
  175. retval = eonce_instruction_exec(target,0x04,0,1,0,NULL);
  176. err_check_propagate(retval);
  177. retval = jtag_data_write16(target,opcode2,NULL);
  178. err_check_propagate(retval);
  179. return retval;
  180. }
  181. /// Executes three word DSP instruction
  182. static int eonce_execute_instruction3(struct target * target, uint16_t opcode1,uint16_t opcode2,uint16_t opcode3){
  183. int retval;
  184. retval = eonce_instruction_exec(target,0x04,0,0,0,NULL);
  185. err_check_propagate(retval);
  186. retval = jtag_data_write16(target,opcode1,NULL);
  187. err_check_propagate(retval);
  188. retval = eonce_instruction_exec(target,0x04,0,0,0,NULL);
  189. err_check_propagate(retval);
  190. retval = jtag_data_write16(target,opcode2,NULL);
  191. err_check_propagate(retval);
  192. retval = eonce_instruction_exec(target,0x04,0,1,0,NULL);
  193. err_check_propagate(retval);
  194. retval = jtag_data_write16(target,opcode3,NULL);
  195. err_check_propagate(retval);
  196. return retval;
  197. }
  198. /**
  199. * --------------- Real-time data exchange ---------------
  200. * The EOnCE Transmit (OTX) and Receive (ORX) registers are data memory mapped, each with an upper and lower 16 bit word.
  201. * Transmit and receive directions are defined from the core’s perspective.
  202. * The core writes to the Transmit register and reads the Receive register, and the host through JTAG writes to the Receive register and reads the Transmit register.
  203. * Both registers have a combined data memory mapped OTXRXSR which provides indication when each may be accessed.
  204. *ref: eonce_rev.1.0_0208081.pdf@36
  205. */
  206. /// writes data into upper ORx register of the target
  207. static int eonce_tx_upper_data(struct target * target, uint16_t data, uint32_t * eonce_status_low){
  208. int retval;
  209. retval = eonce_instruction_exec(target,DSP5680XX_ONCE_ORX1,0,0,0,NULL);
  210. err_check_propagate(retval);
  211. retval = jtag_data_write16(target,data,eonce_status_low);
  212. err_check_propagate(retval);
  213. return retval;
  214. }
  215. /// writes data into lower ORx register of the target
  216. #define eonce_tx_lower_data(target,data) eonce_instruction_exec(target,DSP5680XX_ONCE_ORX,0,0,0,NULL);\
  217. jtag_data_write16(target,data)
  218. /**
  219. *
  220. * @param target
  221. * @param data_read: Returns the data read from the upper OTX register via JTAG.
  222. * @return: Returns an error code (see error code documentation)
  223. */
  224. static int eonce_rx_upper_data(struct target * target, uint8_t * data_read)
  225. {
  226. int retval;
  227. retval = eonce_instruction_exec(target,DSP5680XX_ONCE_OTX1,1,0,0,NULL);
  228. err_check_propagate(retval);
  229. retval = jtag_data_read16(target,data_read);
  230. err_check_propagate(retval);
  231. return retval;
  232. }
  233. /**
  234. *
  235. * @param target
  236. * @param data_read: Returns the data read from the lower OTX register via JTAG.
  237. * @return: Returns an error code (see error code documentation)
  238. */
  239. static int eonce_rx_lower_data(struct target * target,uint8_t * data_read)
  240. {
  241. int retval;
  242. retval = eonce_instruction_exec(target,DSP5680XX_ONCE_OTX,1,0,0,NULL);
  243. err_check_propagate(retval);
  244. retval = jtag_data_read16(target,data_read);
  245. err_check_propagate(retval);
  246. return retval;
  247. }
  248. /**
  249. * -- -- -- -- --- -- -- -- --- -- -- -- --- -- -- -- --- -- -- -- --- --
  250. * -- -- -- -- --- -- -- -Core Instructions- -- -- -- --- -- -- -- --- --
  251. * -- -- -- -- --- -- -- -- --- -- -- -- --- -- -- -- --- -- -- -- --- --
  252. */
  253. /// move.l #value,r0
  254. #define eonce_move_long_to_r0(target,value) eonce_execute_instruction(target,3,0xe418,value&0xffff,value>>16)
  255. /// move.l #value,n
  256. #define eonce_move_long_to_n(target,value) eonce_execute_instruction(target,3,0xe41e,value&0xffff,value>>16)
  257. /// move x:(r0),y0
  258. #define eonce_move_at_r0_to_y0(target) eonce_execute_instruction(target,1,0xF514,0,0)
  259. /// move x:(r0),y1
  260. #define eonce_move_at_r0_to_y1(target) eonce_execute_instruction(target,1,0xF714,0,0)
  261. /// move.l x:(r0),y
  262. #define eonce_move_long_at_r0_y(target) eonce_execute_instruction(target,1,0xF734,0,0)
  263. /// move y0,x:(r0)
  264. #define eonce_move_y0_at_r0(target) eonce_execute_instruction(target,1,0xd514,0,0)
  265. /// bfclr #value,x:(r0)
  266. #define eonce_bfclr_at_r0(target,value) eonce_execute_instruction(target,2,0x8040,value,0)
  267. /// move #value,y0
  268. #define eonce_move_value_to_y0(target,value) eonce_execute_instruction(target,2,0x8745,value,0)
  269. /// move.w y0,x:(r0)+
  270. #define eonce_move_y0_at_r0_inc(target) eonce_execute_instruction(target,1,0xd500,0,0)
  271. /// move.w y0,p:(r0)+
  272. #define eonce_move_y0_at_pr0_inc(target) eonce_execute_instruction(target,1,0x8560,0,0)
  273. /// move.w p:(r0)+,y0
  274. #define eonce_move_at_pr0_inc_to_y0(target) eonce_execute_instruction(target,1,0x8568,0,0)
  275. /// move.w p:(r0)+,y1
  276. #define eonce_move_at_pr0_inc_to_y1(target) eonce_execute_instruction(target,1,0x8768,0,0)
  277. /// move.l #value,r2
  278. #define eonce_move_long_to_r2(target,value) eonce_execute_instruction(target,3,0xe41A,value&0xffff,value>>16)
  279. /// move y0,x:(r2)
  280. #define eonce_move_y0_at_r2(target) eonce_execute_instruction(target,1,0xd516,0,0)
  281. /// move.w #<value>,x:(r2)
  282. #define eonce_move_value_at_r2(target,value) eonce_execute_instruction(target,2,0x8642,value,0)
  283. /// move.w #<value>,x:(r0)
  284. #define eonce_move_value_at_r0(target,value) eonce_execute_instruction(target,2,0x8640,value,0)
  285. /// move.w #<value>,x:(R2+<disp>)
  286. #define eonce_move_value_at_r2_disp(target,value,disp) eonce_execute_instruction(target,3,0x8646,value,disp)
  287. /// move.w x:(r2),Y0
  288. #define eonce_move_at_r2_to_y0(target) eonce_execute_instruction(target,1,0xF516,0,0)
  289. /// move.w p:(r2)+,y0
  290. #define eonce_move_at_pr2_inc_to_y0(target) eonce_execute_instruction(target,1,0x856A,0,0)
  291. /// move.l #value,r3
  292. #define eonce_move_long_to_r1(target,value) eonce_execute_instruction(target,3,0xE419,value&0xffff,value>>16)
  293. /// move.l #value,r3
  294. #define eonce_move_long_to_r3(target,value) eonce_execute_instruction(target,3,0xE41B,value&0xffff,value>>16)
  295. /// move.w y0,p:(r3)+
  296. #define eonce_move_y0_at_pr3_inc(target) eonce_execute_instruction(target,1,0x8563,0,0)
  297. /// move.w y0,x:(r3)
  298. #define eonce_move_y0_at_r3(target) eonce_execute_instruction(target,1,0xD503,0,0)
  299. /// move.l #value,r4
  300. #define eonce_move_long_to_r4(target,value) eonce_execute_instruction(target,3,0xE41C,value&0xffff,value>>16)
  301. /// move pc,r4
  302. #define eonce_move_pc_to_r4(target) eonce_execute_instruction(target,1,0xE716,0,0)
  303. /// move.l r4,y
  304. #define eonce_move_r4_to_y(target) eonce_execute_instruction(target,1,0xe764,0,0)
  305. /// move.w p:(r0)+,y0
  306. #define eonce_move_at_pr0_inc_to_y0(target) eonce_execute_instruction(target,1,0x8568,0,0)
  307. /// move.w x:(r0)+,y0
  308. #define eonce_move_at_r0_inc_to_y0(target) eonce_execute_instruction(target,1,0xf500,0,0)
  309. /// move x:(r0),y0
  310. #define eonce_move_at_r0_y0(target) eonce_execute_instruction(target,1,0xF514,0,0)
  311. /// nop
  312. #define eonce_nop(target) eonce_execute_instruction(target,1,0xe700,0,0)
  313. /// move.w x:(R2+<disp>),Y0
  314. #define eonce_move_at_r2_disp_to_y0(target,disp) eonce_execute_instruction(target,2,0xF542,disp,0)
  315. /// move.w y1,x:(r2)
  316. #define eonce_move_y1_at_r2(target) eonce_execute_instruction(target,1,0xd716,0,0)
  317. /// move.w y1,x:(r0)
  318. #define eonce_move_y1_at_r0(target) eonce_execute_instruction(target,1,0xd714,0,0)
  319. /// move.bp y0,x:(r0)+
  320. #define eonce_move_byte_y0_at_r0(target) eonce_execute_instruction(target,1,0xd5a0,0,0)
  321. /// move.w y1,p:(r0)+
  322. #define eonce_move_y1_at_pr0_inc(target) eonce_execute_instruction(target,1,0x8760,0,0)
  323. /// move.w y1,x:(r0)+
  324. #define eonce_move_y1_at_r0_inc(target) eonce_execute_instruction(target,1,0xD700,0,0)
  325. /// move.l #value,y
  326. #define eonce_move_long_to_y(target,value) eonce_execute_instruction(target,3,0xe417,value&0xffff,value>>16)
  327. static int eonce_move_value_to_pc(struct target * target, uint32_t value){
  328. if (!(target->state == TARGET_HALTED)){
  329. LOG_ERROR("Target must be halted to move PC. Target state = %d.",target->state);
  330. return ERROR_TARGET_NOT_HALTED;
  331. };
  332. int retval;
  333. retval = eonce_execute_instruction(target,3,0xE71E,value&0xffff,value>>16);
  334. err_check_propagate(retval);
  335. return retval;
  336. }
  337. static int eonce_load_TX_RX_to_r0(struct target * target)
  338. {
  339. int retval;
  340. retval = eonce_move_long_to_r0(target,((MC568013_EONCE_TX_RX_ADDR)+(MC568013_EONCE_OBASE_ADDR<<16)));
  341. return retval;
  342. }
  343. static int eonce_load_TX_RX_high_to_r0(struct target * target)
  344. {
  345. int retval = 0;
  346. retval = eonce_move_long_to_r0(target,((MC568013_EONCE_TX1_RX1_HIGH_ADDR)+(MC568013_EONCE_OBASE_ADDR<<16)));
  347. return retval;
  348. }
  349. static int dsp5680xx_read_core_reg(struct target * target, uint8_t reg_addr, uint16_t * data_read)
  350. {
  351. //TODO implement a general version of this which matches what openocd uses.
  352. int retval;
  353. uint32_t dummy_data_to_shift_into_dr;
  354. retval = eonce_instruction_exec(target,reg_addr,1,0,0,NULL);
  355. err_check_propagate(retval);
  356. retval = dsp5680xx_drscan(target,(uint8_t *)& dummy_data_to_shift_into_dr,(uint8_t *) data_read, 8);
  357. err_check_propagate(retval);
  358. LOG_DEBUG("Reg. data: 0x%02X.",*data_read);
  359. return retval;
  360. }
  361. static int eonce_read_status_reg(struct target * target, uint16_t * data){
  362. int retval;
  363. retval = dsp5680xx_read_core_reg(target,DSP5680XX_ONCE_OSR,data);
  364. err_check_propagate(retval);
  365. return retval;
  366. }
  367. /**
  368. * Takes the core out of debug mode.
  369. *
  370. * @param target
  371. * @param eonce_status Data read from the EOnCE status register.
  372. *
  373. * @return
  374. */
  375. static int eonce_exit_debug_mode(struct target * target,uint8_t * eonce_status){
  376. int retval;
  377. retval = eonce_instruction_exec(target,0x1F,0,0,1,eonce_status);
  378. err_check_propagate(retval);
  379. return retval;
  380. }
  381. /**
  382. * Puts the core into debug mode, enabling the EOnCE module.
  383. *
  384. * @param target
  385. * @param eonce_status Data read from the EOnCE status register.
  386. *
  387. * @return
  388. */
  389. static int eonce_enter_debug_mode(struct target * target, uint16_t * eonce_status){
  390. int retval;
  391. uint32_t instr = JTAG_INSTR_DEBUG_REQUEST;
  392. uint32_t ir_out;//not used, just to make jtag happy.
  393. // Debug request #1
  394. retval = dsp5680xx_irscan(target,& instr,& ir_out,DSP5680XX_JTAG_CORE_TAP_IRLEN);
  395. err_check_propagate(retval);
  396. // Enable EOnCE module
  397. instr = JTAG_INSTR_ENABLE_ONCE;
  398. //Two rounds of jtag 0x6 (enable eonce) to enable EOnCE.
  399. retval = dsp5680xx_irscan(target, & instr, & ir_out,DSP5680XX_JTAG_CORE_TAP_IRLEN);
  400. err_check_propagate(retval);
  401. retval = dsp5680xx_irscan(target, & instr, & ir_out,DSP5680XX_JTAG_CORE_TAP_IRLEN);
  402. err_check_propagate(retval);
  403. // Verify that debug mode is enabled
  404. uint16_t data_read_from_dr;
  405. retval = eonce_read_status_reg(target,&data_read_from_dr);
  406. err_check_propagate(retval);
  407. if((data_read_from_dr&0x30) == 0x30){
  408. LOG_DEBUG("EOnCE successfully entered debug mode.");
  409. target->state = TARGET_HALTED;
  410. return ERROR_OK;
  411. }else{
  412. retval = ERROR_TARGET_FAILURE;
  413. err_check(retval,"Failed to set EOnCE module to debug mode.");
  414. }
  415. if(eonce_status!=NULL)
  416. *eonce_status = data_read_from_dr;
  417. return ERROR_OK;
  418. }
  419. /**
  420. * Reads the current value of the program counter and stores it.
  421. *
  422. * @param target
  423. *
  424. * @return
  425. */
  426. static int eonce_pc_store(struct target * target){
  427. uint8_t tmp[2];
  428. int retval;
  429. retval = eonce_move_pc_to_r4(target);
  430. err_check_propagate(retval);
  431. retval = eonce_move_r4_to_y(target);
  432. err_check_propagate(retval);
  433. retval = eonce_load_TX_RX_to_r0(target);
  434. err_check_propagate(retval);
  435. retval = eonce_move_y0_at_r0(target);
  436. err_check_propagate(retval);
  437. retval = eonce_rx_lower_data(target,tmp);
  438. err_check_propagate(retval);
  439. LOG_USER("PC value: 0x%X%X\n",tmp[1],tmp[0]);
  440. dsp5680xx_context.stored_pc = (tmp[0]|(tmp[1]<<8));
  441. return ERROR_OK;
  442. }
  443. static int dsp5680xx_target_create(struct target *target, Jim_Interp * interp){
  444. struct dsp5680xx_common *dsp5680xx = calloc(1, sizeof(struct dsp5680xx_common));
  445. target->arch_info = dsp5680xx;
  446. return ERROR_OK;
  447. }
  448. static int dsp5680xx_init_target(struct command_context *cmd_ctx, struct target *target){
  449. dsp5680xx_context.stored_pc = 0;
  450. dsp5680xx_context.flush = 1;
  451. LOG_DEBUG("target initiated!");
  452. //TODO core tap must be enabled before running these commands, currently this is done in the .cfg tcl script.
  453. return ERROR_OK;
  454. }
  455. static int dsp5680xx_arch_state(struct target *target){
  456. LOG_USER("%s not implemented yet.",__FUNCTION__);
  457. return ERROR_OK;
  458. }
  459. int dsp5680xx_target_status(struct target * target, uint8_t * jtag_st, uint16_t * eonce_st){
  460. return target->state;
  461. }
  462. static int dsp5680xx_assert_reset(struct target *target){
  463. target->state = TARGET_RESET;
  464. return ERROR_OK;
  465. }
  466. static int dsp5680xx_deassert_reset(struct target *target){
  467. target->state = TARGET_RUNNING;
  468. return ERROR_OK;
  469. }
  470. static int dsp5680xx_halt(struct target *target){
  471. int retval;
  472. uint16_t eonce_status = 0xbeef;
  473. if(target->state == TARGET_HALTED){
  474. LOG_USER("Target already halted.");
  475. return ERROR_OK;
  476. }
  477. retval = eonce_enter_debug_mode(target,&eonce_status);
  478. err_check_propagate(retval);
  479. retval = eonce_pc_store(target);
  480. err_check_propagate(retval);
  481. //TODO is it useful to store the pc?
  482. return retval;
  483. }
  484. static int dsp5680xx_poll(struct target *target){
  485. int retval;
  486. uint8_t jtag_status;
  487. uint8_t eonce_status;
  488. uint16_t read_tmp;
  489. retval = dsp5680xx_jtag_status(target,&jtag_status);
  490. err_check_propagate(retval);
  491. if (jtag_status == JTAG_STATUS_DEBUG)
  492. if (target->state != TARGET_HALTED){
  493. retval = eonce_enter_debug_mode(target,&read_tmp);
  494. err_check_propagate(retval);
  495. eonce_status = (uint8_t) read_tmp;
  496. if((eonce_status&EONCE_STAT_MASK) != DSP5680XX_ONCE_OSCR_DEBUG_M){
  497. LOG_WARNING("%s: Failed to put EOnCE in debug mode. Is flash locked?...",__FUNCTION__);
  498. return ERROR_TARGET_FAILURE;
  499. }else{
  500. target->state = TARGET_HALTED;
  501. return ERROR_OK;
  502. }
  503. }
  504. if (jtag_status == JTAG_STATUS_NORMAL){
  505. if(target->state == TARGET_RESET){
  506. retval = dsp5680xx_halt(target);
  507. err_check_propagate(retval);
  508. retval = eonce_exit_debug_mode(target,&eonce_status);
  509. err_check_propagate(retval);
  510. if((eonce_status&EONCE_STAT_MASK) != DSP5680XX_ONCE_OSCR_NORMAL_M){
  511. LOG_WARNING("%s: JTAG running, but cannot make EOnCE run. Try resetting...",__FUNCTION__);
  512. return ERROR_TARGET_FAILURE;
  513. }else{
  514. target->state = TARGET_RUNNING;
  515. return ERROR_OK;
  516. }
  517. }
  518. if(target->state != TARGET_RUNNING){
  519. retval = eonce_read_status_reg(target,&read_tmp);
  520. err_check_propagate(retval);
  521. eonce_status = (uint8_t) read_tmp;
  522. if((eonce_status&EONCE_STAT_MASK) != DSP5680XX_ONCE_OSCR_NORMAL_M){
  523. LOG_WARNING("Inconsistent target status. Restart!");
  524. return ERROR_TARGET_FAILURE;
  525. }
  526. }
  527. target->state = TARGET_RUNNING;
  528. return ERROR_OK;
  529. }
  530. if(jtag_status == JTAG_STATUS_DEAD){
  531. LOG_ERROR("%s: Cannot communicate with JTAG. Check connection...",__FUNCTION__);
  532. target->state = TARGET_UNKNOWN;
  533. return ERROR_TARGET_FAILURE;
  534. };
  535. if (target->state == TARGET_UNKNOWN){
  536. LOG_ERROR("%s: Target status invalid - communication failure",__FUNCTION__);
  537. return ERROR_TARGET_FAILURE;
  538. };
  539. return ERROR_OK;
  540. }
  541. static int dsp5680xx_resume(struct target *target, int current, uint32_t address,int handle_breakpoints, int debug_execution){
  542. if(target->state == TARGET_RUNNING){
  543. LOG_USER("Target already running.");
  544. return ERROR_OK;
  545. }
  546. int retval;
  547. uint8_t eonce_status;
  548. if(!current){
  549. retval = eonce_move_value_to_pc(target,address);
  550. err_check_propagate(retval);
  551. }
  552. int retry = 20;
  553. while(retry-- > 1){
  554. retval = eonce_exit_debug_mode(target,&eonce_status );
  555. err_check_propagate(retval);
  556. if(eonce_status == DSP5680XX_ONCE_OSCR_NORMAL_M)
  557. break;
  558. }
  559. if(retry == 0){
  560. retval = ERROR_TARGET_FAILURE;
  561. err_check(retval,"Failed to resume...");
  562. }else{
  563. target->state = TARGET_RUNNING;
  564. }
  565. LOG_DEBUG("EOnCE status: 0x%02X.",eonce_status);
  566. return ERROR_OK;
  567. }
  568. /**
  569. * The value of @address determines if it corresponds to P: (program) or X: (data) memory. If the address is over 0x200000 then it is considered X: memory, and @pmem = 0.
  570. * The special case of 0xFFXXXX is not modified, since it allows to read out the memory mapped EOnCE registers.
  571. *
  572. * @param address
  573. * @param pmem
  574. *
  575. * @return
  576. */
  577. static int dsp5680xx_convert_address(uint32_t * address, int * pmem){
  578. // Distinguish data memory (x:) from program memory (p:) by the address.
  579. // Addresses over S_FILE_DATA_OFFSET are considered (x:) memory.
  580. if(*address >= S_FILE_DATA_OFFSET){
  581. *pmem = 0;
  582. if(((*address)&0xff0000)!=0xff0000)
  583. *address -= S_FILE_DATA_OFFSET;
  584. }
  585. return ERROR_OK;
  586. }
  587. static int dsp5680xx_read_16_single(struct target * target, uint32_t address, uint8_t * data_read, int r_pmem){
  588. int retval;
  589. retval = eonce_move_long_to_r0(target,address);
  590. err_check_propagate(retval);
  591. if(r_pmem)
  592. retval = eonce_move_at_pr0_inc_to_y0(target);
  593. else
  594. retval = eonce_move_at_r0_to_y0(target);
  595. err_check_propagate(retval);
  596. retval = eonce_load_TX_RX_to_r0(target);
  597. err_check_propagate(retval);
  598. retval = eonce_move_y0_at_r0(target);
  599. err_check_propagate(retval);
  600. // at this point the data i want is at the reg eonce can read
  601. retval = eonce_rx_lower_data(target,data_read);
  602. err_check_propagate(retval);
  603. LOG_DEBUG("%s: Data read from 0x%06X: 0x%02X%02X",__FUNCTION__, address,data_read[1],data_read[0]);
  604. return retval;
  605. }
  606. static int dsp5680xx_read_32_single(struct target * target, uint32_t address, uint8_t * data_read, int r_pmem){
  607. int retval;
  608. address = (address & 0xFFFFFE);
  609. // Get data to an intermediate register
  610. retval = eonce_move_long_to_r0(target,address);
  611. err_check_propagate(retval);
  612. if(r_pmem){
  613. retval = eonce_move_at_pr0_inc_to_y0(target);
  614. err_check_propagate(retval);
  615. retval = eonce_move_at_pr0_inc_to_y1(target);
  616. err_check_propagate(retval);
  617. }else{
  618. retval = eonce_move_at_r0_inc_to_y0(target);
  619. err_check_propagate(retval);
  620. retval = eonce_move_at_r0_to_y1(target);
  621. err_check_propagate(retval);
  622. }
  623. // Get lower part of data to TX/RX
  624. retval = eonce_load_TX_RX_to_r0(target);
  625. err_check_propagate(retval);
  626. retval = eonce_move_y0_at_r0_inc(target); // This also load TX/RX high to r0
  627. err_check_propagate(retval);
  628. // Get upper part of data to TX/RX
  629. retval = eonce_move_y1_at_r0(target);
  630. err_check_propagate(retval);
  631. // at this point the data i want is at the reg eonce can read
  632. retval = eonce_rx_lower_data(target,data_read);
  633. err_check_propagate(retval);
  634. retval = eonce_rx_upper_data(target,data_read+2);
  635. err_check_propagate(retval);
  636. return retval;
  637. }
  638. static int dsp5680xx_read(struct target * target, uint32_t address, unsigned size, unsigned count, uint8_t * buffer){
  639. if(target->state != TARGET_HALTED){
  640. LOG_USER("Target must be halted.");
  641. return ERROR_OK;
  642. }
  643. int retval = ERROR_OK;
  644. int pmem = 1;
  645. retval = dsp5680xx_convert_address(&address, &pmem);
  646. err_check_propagate(retval);
  647. dsp5680xx_context.flush = 0;
  648. int counter = FLUSH_COUNT_READ_WRITE;
  649. for (unsigned i=0; i<count; i++){
  650. if(--counter==0){
  651. dsp5680xx_context.flush = 1;
  652. counter = FLUSH_COUNT_FLASH;
  653. }
  654. switch (size){
  655. case 1:
  656. if(!(i%2)){
  657. retval = dsp5680xx_read_16_single(target, address + i/2, buffer + i, pmem);
  658. }
  659. break;
  660. case 2:
  661. retval = dsp5680xx_read_16_single(target, address + i, buffer+2*i, pmem);
  662. break;
  663. case 4:
  664. retval = dsp5680xx_read_32_single(target, address + 2*i, buffer + 4*i, pmem);
  665. break;
  666. default:
  667. LOG_USER("%s: Invalid read size.",__FUNCTION__);
  668. break;
  669. }
  670. err_check_propagate(retval);
  671. dsp5680xx_context.flush = 0;
  672. }
  673. dsp5680xx_context.flush = 1;
  674. retval = dsp5680xx_execute_queue();
  675. err_check_propagate(retval);
  676. return retval;
  677. }
  678. static int dsp5680xx_write_16_single(struct target *target, uint32_t address, uint16_t data, uint8_t w_pmem){
  679. int retval = 0;
  680. retval = eonce_move_long_to_r0(target,address);
  681. err_check_propagate(retval);
  682. if(w_pmem){
  683. retval = eonce_move_value_to_y0(target,data);
  684. err_check_propagate(retval);
  685. retval = eonce_move_y0_at_pr0_inc(target);
  686. err_check_propagate(retval);
  687. }else{
  688. retval = eonce_move_value_at_r0(target,data);
  689. err_check_propagate(retval);
  690. }
  691. return retval;
  692. }
  693. static int dsp5680xx_write_32_single(struct target *target, uint32_t address, uint32_t data, int w_pmem){
  694. int retval = 0;
  695. retval = eonce_move_long_to_r0(target,address);
  696. err_check_propagate(retval);
  697. retval = eonce_move_long_to_y(target,data);
  698. err_check_propagate(retval);
  699. if(w_pmem)
  700. retval = eonce_move_y0_at_pr0_inc(target);
  701. else
  702. retval = eonce_move_y0_at_r0_inc(target);
  703. err_check_propagate(retval);
  704. if(w_pmem)
  705. retval = eonce_move_y1_at_pr0_inc(target);
  706. else
  707. retval = eonce_move_y1_at_r0_inc(target);
  708. err_check_propagate(retval);
  709. return retval;
  710. }
  711. static int dsp5680xx_write_8(struct target * target, uint32_t address, uint32_t count, const uint8_t * data, int pmem){
  712. if(target->state != TARGET_HALTED){
  713. LOG_ERROR("%s: Target must be halted.",__FUNCTION__);
  714. return ERROR_OK;
  715. };
  716. int retval = 0;
  717. uint16_t data_16;
  718. uint32_t iter;
  719. int counter = FLUSH_COUNT_READ_WRITE;
  720. for(iter = 0; iter<count/2; iter++){
  721. if(--counter==0){
  722. dsp5680xx_context.flush = 1;
  723. counter = FLUSH_COUNT_READ_WRITE;
  724. }
  725. data_16=(data[2*iter]|(data[2*iter+1]<<8));
  726. retval = dsp5680xx_write_16_single(target,address+iter,data_16, pmem);
  727. if(retval != ERROR_OK){
  728. LOG_ERROR("%s: Could not write to p:0x%04X",__FUNCTION__,address);
  729. dsp5680xx_context.flush = 1;
  730. return retval;
  731. }
  732. dsp5680xx_context.flush = 0;
  733. }
  734. dsp5680xx_context.flush = 1;
  735. // Only one byte left, let's not overwrite the other byte (mem is 16bit)
  736. // Need to retrieve the part we do not want to overwrite.
  737. uint16_t data_old;
  738. if((count==1)||(count%2)){
  739. retval = dsp5680xx_read(target,address+iter,1,1,(uint8_t *)&data_old);
  740. err_check_propagate(retval);
  741. if(count==1)
  742. data_old=(((data_old&0xff)<<8)|data[0]);// preserve upper byte
  743. else
  744. data_old=(((data_old&0xff)<<8)|data[2*iter+1]);
  745. retval = dsp5680xx_write_16_single(target,address+iter,data_old, pmem);
  746. err_check_propagate(retval);
  747. }
  748. return retval;
  749. }
  750. static int dsp5680xx_write_16(struct target * target, uint32_t address, uint32_t count, const uint8_t * data, int pmem){
  751. int retval = ERROR_OK;
  752. if(target->state != TARGET_HALTED){
  753. retval = ERROR_TARGET_NOT_HALTED;
  754. err_check(retval,"Target must be halted.");
  755. };
  756. uint32_t iter;
  757. int counter = FLUSH_COUNT_READ_WRITE;
  758. for(iter = 0; iter<count; iter++){
  759. if(--counter==0){
  760. dsp5680xx_context.flush = 1;
  761. counter = FLUSH_COUNT_READ_WRITE;
  762. }
  763. retval = dsp5680xx_write_16_single(target,address+iter,data[iter], pmem);
  764. if(retval != ERROR_OK){
  765. LOG_ERROR("%s: Could not write to p:0x%04X",__FUNCTION__,address);
  766. dsp5680xx_context.flush = 1;
  767. return retval;
  768. }
  769. dsp5680xx_context.flush = 0;
  770. }
  771. dsp5680xx_context.flush = 1;
  772. return retval;
  773. }
  774. static int dsp5680xx_write_32(struct target * target, uint32_t address, uint32_t count, const uint8_t * data, int pmem){
  775. int retval = ERROR_OK;
  776. if(target->state != TARGET_HALTED){
  777. retval = ERROR_TARGET_NOT_HALTED;
  778. err_check(retval,"Target must be halted.");
  779. };
  780. uint32_t iter;
  781. int counter = FLUSH_COUNT_READ_WRITE;
  782. for(iter = 0; iter<count; iter++){
  783. if(--counter==0){
  784. dsp5680xx_context.flush = 1;
  785. counter = FLUSH_COUNT_READ_WRITE;
  786. }
  787. retval = dsp5680xx_write_32_single(target,address+(iter<<1),data[iter], pmem);
  788. if(retval != ERROR_OK){
  789. LOG_ERROR("%s: Could not write to p:0x%04X",__FUNCTION__,address);
  790. dsp5680xx_context.flush = 1;
  791. return retval;
  792. }
  793. dsp5680xx_context.flush = 0;
  794. }
  795. dsp5680xx_context.flush = 1;
  796. return retval;
  797. }
  798. /**
  799. * Writes @buffer to memory.
  800. * The parameter @address determines whether @buffer should be written to P: (program) memory or X: (data) memory.
  801. *
  802. * @param target
  803. * @param address
  804. * @param size Bytes (1), Half words (2), Words (4).
  805. * @param count In bytes.
  806. * @param buffer
  807. *
  808. * @return
  809. */
  810. static int dsp5680xx_write(struct target *target, uint32_t address, uint32_t size, uint32_t count, const uint8_t * buffer){
  811. //TODO Cannot write 32bit to odd address, will write 0x12345678 as 0x5678 0x0012
  812. if(target->state != TARGET_HALTED){
  813. LOG_USER("Target must be halted.");
  814. return ERROR_OK;
  815. }
  816. int retval = 0;
  817. int p_mem = 1;
  818. retval = dsp5680xx_convert_address(&address, &p_mem);
  819. err_check_propagate(retval);
  820. switch (size){
  821. case 1:
  822. retval = dsp5680xx_write_8(target, address, count, buffer, p_mem);
  823. break;
  824. case 2:
  825. retval = dsp5680xx_write_16(target, address, count, buffer, p_mem);
  826. break;
  827. case 4:
  828. retval = dsp5680xx_write_32(target, address, count, buffer, p_mem);
  829. break;
  830. default:
  831. retval = ERROR_TARGET_DATA_ABORT;
  832. err_check(retval,"Invalid data size.");
  833. break;
  834. }
  835. return retval;
  836. }
  837. static int dsp5680xx_bulk_write_memory(struct target * target,uint32_t address, uint32_t aligned, const uint8_t * buffer){
  838. LOG_ERROR("Not implemented yet.");
  839. return ERROR_FAIL;
  840. }
  841. static int dsp5680xx_write_buffer(struct target * target, uint32_t address, uint32_t size, const uint8_t * buffer){
  842. if(target->state != TARGET_HALTED){
  843. LOG_USER("Target must be halted.");
  844. return ERROR_OK;
  845. }
  846. return dsp5680xx_write(target, address, 1, size, buffer);
  847. }
  848. /**
  849. * This function is called by verify_image, it is used to read data from memory.
  850. *
  851. * @param target
  852. * @param address Word addressing.
  853. * @param size In bytes.
  854. * @param buffer
  855. *
  856. * @return
  857. */
  858. static int dsp5680xx_read_buffer(struct target * target, uint32_t address, uint32_t size, uint8_t * buffer){
  859. if(target->state != TARGET_HALTED){
  860. LOG_USER("Target must be halted.");
  861. return ERROR_OK;
  862. }
  863. // The "/2" solves the byte/word addressing issue.
  864. return dsp5680xx_read(target,address,2,size/2,buffer);
  865. }
  866. /**
  867. * This function is not implemented.
  868. * It returns an error in order to get OpenOCD to do read out the data and calculate the CRC, or try a binary comparison.
  869. *
  870. * @param target
  871. * @param address Start address of the image.
  872. * @param size In bytes.
  873. * @param checksum
  874. *
  875. * @return
  876. */
  877. static int dsp5680xx_checksum_memory(struct target * target, uint32_t address, uint32_t size, uint32_t * checksum){
  878. return ERROR_FAIL;
  879. }
  880. /**
  881. * Calculates a signature over @word_count words in the data from @buff16. The algorithm used is the same the FM uses, so the @return may be used to compare with the one generated by the FM module, and check if flashing was successful.
  882. * This algorithm is based on the perl script available from the Freescale website at FAQ 25630.
  883. *
  884. * @param buff16
  885. * @param word_count
  886. *
  887. * @return
  888. */
  889. static int perl_crc(uint8_t * buff8,uint32_t word_count){
  890. uint16_t checksum = 0xffff;
  891. uint16_t data,fbmisr;
  892. uint32_t i;
  893. for(i=0;i<word_count;i++){
  894. data = (buff8[2*i]|(buff8[2*i+1]<<8));
  895. fbmisr = (checksum & 2)>>1 ^ (checksum & 4)>>2 ^ (checksum & 16)>>4 ^ (checksum & 0x8000)>>15;
  896. checksum = (data ^ ((checksum << 1) | fbmisr));
  897. }
  898. i--;
  899. for(;!(i&0x80000000);i--){
  900. data = (buff8[2*i]|(buff8[2*i+1]<<8));
  901. fbmisr = (checksum & 2)>>1 ^ (checksum & 4)>>2 ^ (checksum & 16)>>4 ^ (checksum & 0x8000)>>15;
  902. checksum = (data ^ ((checksum << 1) | fbmisr));
  903. }
  904. return checksum;
  905. }
  906. /**
  907. * Resets the SIM. (System Integration Module).
  908. *
  909. * @param target
  910. *
  911. * @return
  912. */
  913. int dsp5680xx_f_SIM_reset(struct target * target){
  914. int retval = ERROR_OK;
  915. uint16_t sim_cmd = SIM_CMD_RESET;
  916. uint32_t sim_addr;
  917. if(strcmp(target->tap->chip,"dsp568013")==0){
  918. sim_addr = MC568013_SIM_BASE_ADDR+S_FILE_DATA_OFFSET;
  919. retval = dsp5680xx_write(target,sim_addr,1,2,(const uint8_t *)&sim_cmd);
  920. err_check_propagate(retval);
  921. }
  922. return retval;
  923. }
  924. /**
  925. * Halts the core and resets the SIM. (System Integration Module).
  926. *
  927. * @param target
  928. *
  929. * @return
  930. */
  931. static int dsp5680xx_soft_reset_halt(struct target *target){
  932. //TODO is this what this function is expected to do...?
  933. int retval;
  934. retval = dsp5680xx_halt(target);
  935. err_check_propagate(retval);
  936. retval = dsp5680xx_f_SIM_reset(target);
  937. err_check_propagate(retval);
  938. return retval;
  939. }
  940. int dsp5680xx_f_protect_check(struct target * target, uint16_t * protected) {
  941. int retval;
  942. if (dsp5680xx_target_status(target,NULL,NULL) != TARGET_HALTED){
  943. retval = dsp5680xx_halt(target);
  944. err_check_propagate(retval);
  945. }
  946. if(protected == NULL){
  947. err_check(ERROR_FAIL,"NULL pointer not valid.");
  948. }
  949. retval = dsp5680xx_read_16_single(target,HFM_BASE_ADDR|HFM_PROT,(uint8_t *)protected,0);
  950. err_check_propagate(retval);
  951. return retval;
  952. }
  953. /**
  954. * Executes a command on the FM module. Some commands use the parameters @address and @data, others ignore them.
  955. *
  956. * @param target
  957. * @param command Command to execute.
  958. * @param address Command parameter.
  959. * @param data Command parameter.
  960. * @param hfm_ustat FM status register.
  961. * @param pmem Address is P: (program) memory (@pmem==1) or X: (data) memory (@pmem==0)
  962. *
  963. * @return
  964. */
  965. static int dsp5680xx_f_execute_command(struct target * target, uint16_t command, uint32_t address, uint32_t data, uint16_t * hfm_ustat, int pmem){
  966. int retval;
  967. retval = eonce_load_TX_RX_high_to_r0(target);
  968. err_check_propagate(retval);
  969. retval = eonce_move_long_to_r2(target,HFM_BASE_ADDR);
  970. err_check_propagate(retval);
  971. uint8_t i[2];
  972. int watchdog = 100;
  973. do{
  974. retval = eonce_move_at_r2_disp_to_y0(target,HFM_USTAT); // read HMF_USTAT
  975. err_check_propagate(retval);
  976. retval = eonce_move_y0_at_r0(target);
  977. err_check_propagate(retval);
  978. retval = eonce_rx_upper_data(target,i);
  979. err_check_propagate(retval);
  980. if((watchdog--)==1){
  981. retval = ERROR_TARGET_FAILURE;
  982. err_check(retval,"FM execute command failed.");
  983. }
  984. }while (!(i[0]&0x40)); // wait until current command is complete
  985. dsp5680xx_context.flush = 0;
  986. retval = eonce_move_value_at_r2_disp(target,0x00,HFM_CNFG); // write to HFM_CNFG (lock=0, select bank) -- flash_desc.bank&0x03,0x01 == 0x00,0x01 ???
  987. err_check_propagate(retval);
  988. retval = eonce_move_value_at_r2_disp(target,0x04,HFM_USTAT); // write to HMF_USTAT, clear PVIOL, ACCERR & BLANK bits
  989. err_check_propagate(retval);
  990. retval = eonce_move_value_at_r2_disp(target,0x10,HFM_USTAT); // clear only one bit at a time
  991. err_check_propagate(retval);
  992. retval = eonce_move_value_at_r2_disp(target,0x20,HFM_USTAT);
  993. err_check_propagate(retval);
  994. retval = eonce_move_value_at_r2_disp(target,0x00,HFM_PROT); // write to HMF_PROT, clear protection
  995. err_check_propagate(retval);
  996. retval = eonce_move_value_at_r2_disp(target,0x00,HFM_PROTB); // write to HMF_PROTB, clear protection
  997. err_check_propagate(retval);
  998. retval = eonce_move_value_to_y0(target,data);
  999. err_check_propagate(retval);
  1000. retval = eonce_move_long_to_r3(target,address); // write to the flash block
  1001. err_check_propagate(retval);
  1002. if (pmem){
  1003. retval = eonce_move_y0_at_pr3_inc(target);
  1004. err_check_propagate(retval);
  1005. }else{
  1006. retval = eonce_move_y0_at_r3(target);
  1007. err_check_propagate(retval);
  1008. }
  1009. retval = eonce_move_value_at_r2_disp(target,command,HFM_CMD); // write command to the HFM_CMD reg
  1010. err_check_propagate(retval);
  1011. retval = eonce_move_value_at_r2_disp(target,0x80,HFM_USTAT); // start the command
  1012. err_check_propagate(retval);
  1013. dsp5680xx_context.flush = 1;
  1014. retval = dsp5680xx_execute_queue();
  1015. err_check_propagate(retval);
  1016. watchdog = 100;
  1017. do{
  1018. retval = eonce_move_at_r2_disp_to_y0(target,HFM_USTAT); // read HMF_USTAT
  1019. err_check_propagate(retval);
  1020. retval = eonce_move_y0_at_r0(target);
  1021. err_check_propagate(retval);
  1022. retval = eonce_rx_upper_data(target,i);
  1023. err_check_propagate(retval);
  1024. if((watchdog--)==1){
  1025. retval = ERROR_TARGET_FAILURE;
  1026. err_check(retval,"FM execution did not finish.");
  1027. }
  1028. }while (!(i[0]&0x40)); // wait until the command is complete
  1029. *hfm_ustat = ((i[0]<<8)|(i[1]));
  1030. if (i[0]&HFM_USTAT_MASK_PVIOL_ACCER){
  1031. retval = ERROR_TARGET_FAILURE;
  1032. err_check(retval,"pviol and/or accer bits set. HFM command execution error");
  1033. }
  1034. return ERROR_OK;
  1035. }
  1036. /**
  1037. * Prior to the execution of any Flash module command, the Flash module Clock Divider (CLKDIV) register must be initialized. The values of this register determine the speed of the internal Flash Clock (FCLK). FCLK must be in the range of 150kHz ≤ FCLK ≤ 200kHz for proper operation of the Flash module. (Running FCLK too slowly wears out the module, while running it too fast under programs Flash leading to bit errors.)
  1038. *
  1039. * @param target
  1040. *
  1041. * @return
  1042. */
  1043. static int eonce_set_hfmdiv(struct target * target){
  1044. uint8_t i[2];
  1045. int retval;
  1046. retval = eonce_move_long_to_r2(target,HFM_BASE_ADDR);
  1047. err_check_propagate(retval);
  1048. retval = eonce_load_TX_RX_high_to_r0(target);
  1049. err_check_propagate(retval);
  1050. retval = eonce_move_at_r2_to_y0(target);// read HFM_CLKD
  1051. err_check_propagate(retval);
  1052. retval = eonce_move_y0_at_r0(target);
  1053. err_check_propagate(retval);
  1054. retval = eonce_rx_upper_data(target,i);
  1055. err_check_propagate(retval);
  1056. unsigned int hfm_at_wrong_value = 0;
  1057. if ((i[0]&0x7f)!=HFM_CLK_DEFAULT) {
  1058. LOG_DEBUG("HFM CLK divisor contained incorrect value (0x%02X).",i[0]&0x7f);
  1059. hfm_at_wrong_value = 1;
  1060. }else{
  1061. LOG_DEBUG("HFM CLK divisor was already set to correct value (0x%02X).",i[0]&0x7f);
  1062. return ERROR_OK;
  1063. }
  1064. retval = eonce_move_value_at_r2(target,HFM_CLK_DEFAULT); // write HFM_CLKD
  1065. err_check_propagate(retval);
  1066. retval = eonce_move_at_r2_to_y0(target); // verify HFM_CLKD
  1067. err_check_propagate(retval);
  1068. retval = eonce_move_y0_at_r0(target);
  1069. err_check_propagate(retval);
  1070. retval = eonce_rx_upper_data(target,i);
  1071. err_check_propagate(retval);
  1072. if (i[0]!=(0x80|(HFM_CLK_DEFAULT&0x7f))) {
  1073. retval = ERROR_TARGET_FAILURE;
  1074. err_check(retval,"Unable to set HFM CLK divisor.");
  1075. }
  1076. if(hfm_at_wrong_value)
  1077. LOG_DEBUG("HFM CLK divisor set to 0x%02x.",i[0]&0x7f);
  1078. return ERROR_OK;
  1079. }
  1080. /**
  1081. * Executes the FM calculate signature command. The FM will calculate over the data from @address to @address + @words -1. The result is written to a register, then read out by this function and returned in @signature. The value @signature may be compared to the the one returned by perl_crc to verify the flash was written correctly.
  1082. *
  1083. * @param target
  1084. * @param address Start of flash array where the signature should be calculated.
  1085. * @param words Number of words over which the signature should be calculated.
  1086. * @param signature Value calculated by the FM.
  1087. *
  1088. * @return
  1089. */
  1090. static int dsp5680xx_f_signature(struct target * target, uint32_t address, uint32_t words, uint16_t * signature){
  1091. int retval;
  1092. uint16_t hfm_ustat;
  1093. if (dsp5680xx_target_status(target,NULL,NULL) != TARGET_HALTED){
  1094. retval = eonce_enter_debug_mode(target,NULL);
  1095. err_check_propagate(retval);
  1096. }
  1097. retval = dsp5680xx_f_execute_command(target,HFM_CALCULATE_DATA_SIGNATURE,address,words,&hfm_ustat,1);
  1098. err_check_propagate(retval);
  1099. retval = dsp5680xx_read_16_single(target, HFM_BASE_ADDR|HFM_DATA, (uint8_t *)signature, 0);
  1100. return retval;
  1101. }
  1102. int dsp5680xx_f_erase_check(struct target * target, uint8_t * erased,uint32_t sector){
  1103. int retval;
  1104. uint16_t hfm_ustat;
  1105. if (dsp5680xx_target_status(target,NULL,NULL) != TARGET_HALTED){
  1106. retval = dsp5680xx_halt(target);
  1107. err_check_propagate(retval);
  1108. }
  1109. // Check if chip is already erased.
  1110. retval = dsp5680xx_f_execute_command(target,HFM_ERASE_VERIFY,HFM_FLASH_BASE_ADDR+sector*HFM_SECTOR_SIZE/2,0,&hfm_ustat,1); // blank check
  1111. err_check_propagate(retval);
  1112. if(erased!=NULL)
  1113. *erased = (uint8_t)(hfm_ustat&HFM_USTAT_MASK_BLANK);
  1114. return retval;
  1115. }
  1116. /**
  1117. * Executes the FM page erase command.
  1118. *
  1119. * @param target
  1120. * @param sector Page to erase.
  1121. * @param hfm_ustat FM module status register.
  1122. *
  1123. * @return
  1124. */
  1125. static int erase_sector(struct target * target, int sector, uint16_t * hfm_ustat){
  1126. int retval;
  1127. retval = dsp5680xx_f_execute_command(target,HFM_PAGE_ERASE,HFM_FLASH_BASE_ADDR+sector*HFM_SECTOR_SIZE/2,0,hfm_ustat,1);
  1128. err_check_propagate(retval);
  1129. return retval;
  1130. }
  1131. /**
  1132. * Executes the FM mass erase command. Erases the flash array completely.
  1133. *
  1134. * @param target
  1135. * @param hfm_ustat FM module status register.
  1136. *
  1137. * @return
  1138. */
  1139. static int mass_erase(struct target * target, uint16_t * hfm_ustat){
  1140. int retval;
  1141. retval = dsp5680xx_f_execute_command(target,HFM_MASS_ERASE,0,0,hfm_ustat,1);
  1142. return retval;
  1143. }
  1144. int dsp5680xx_f_erase(struct target * target, int first, int last){
  1145. int retval;
  1146. if (dsp5680xx_target_status(target,NULL,NULL) != TARGET_HALTED){
  1147. retval = dsp5680xx_halt(target);
  1148. err_check_propagate(retval);
  1149. }
  1150. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1151. // Reset SIM
  1152. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1153. retval = dsp5680xx_f_SIM_reset(target);
  1154. err_check_propagate(retval);
  1155. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1156. // Set hfmdiv
  1157. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1158. retval = eonce_set_hfmdiv(target);
  1159. err_check_propagate(retval);
  1160. uint16_t hfm_ustat;
  1161. int do_mass_erase = ((!(first|last)) || ((first==0)&&(last == (HFM_SECTOR_COUNT-1))));
  1162. if(do_mass_erase){
  1163. //Mass erase
  1164. retval = mass_erase(target,&hfm_ustat);
  1165. err_check_propagate(retval);
  1166. last = HFM_SECTOR_COUNT-1;
  1167. }else{
  1168. for(int i = first;i<=last;i++){
  1169. retval = erase_sector(target,i,&hfm_ustat);
  1170. err_check_propagate(retval);
  1171. }
  1172. }
  1173. return ERROR_OK;
  1174. }
  1175. /**
  1176. * Algorithm for programming normal p: flash
  1177. * Follow state machine from "56F801x Peripheral Reference Manual"@163.
  1178. * Registers to set up before calling:
  1179. * r0: TX/RX high address.
  1180. * r2: FM module base address.
  1181. * r3: Destination address in flash.
  1182. *
  1183. * hfm_wait: // wait for command to finish
  1184. * brclr #0x40,x:(r2+0x13),hfm_wait
  1185. * rx_check: // wait for input buffer full
  1186. * brclr #0x01,x:(r0-2),rx_check
  1187. * move.w x:(r0),y0 // read from Rx buffer
  1188. * move.w y0,p:(r3)+
  1189. * move.w #0x20,x:(r2+0x14) // write PGM command
  1190. * move.w #0x80,x:(r2+0x13) // start the command
  1191. * brclr #0x20,X:(R2+0x13),accerr_check // protection violation check
  1192. * bfset #0x20,X:(R2+0x13) // clear pviol
  1193. * bra hfm_wait
  1194. * accerr_check:
  1195. * brclr #0x10,X:(R2+0x13),hfm_wait // access error check
  1196. * bfset #0x10,X:(R2+0x13) // clear accerr
  1197. * bra hfm_wait // loop
  1198. *0x00000073 0x8A460013407D brclr #0x40,X:(R2+0x13),*+0
  1199. *0x00000076 0xE700 nop
  1200. *0x00000077 0xE700 nop
  1201. *0x00000078 0x8A44FFFE017B brclr #1,X:(R0-2),*-2
  1202. *0x0000007B 0xE700 nop
  1203. *0x0000007C 0xF514 move.w X:(R0),Y0
  1204. *0x0000007D 0x8563 move.w Y0,P:(R3)+
  1205. *0x0000007E 0x864600200014 move.w #0x20,X:(R2+0x14)
  1206. *0x00000081 0x864600800013 move.w #0x80,X:(R2+0x13)
  1207. *0x00000084 0x8A4600132004 brclr #0x20,X:(R2+0x13),*+7
  1208. *0x00000087 0x824600130020 bfset #0x20,X:(R2+0x13)
  1209. *0x0000008A 0xA968 bra *-23
  1210. *0x0000008B 0x8A4600131065 brclr #0x10,X:(R2+0x13),*-24
  1211. *0x0000008E 0x824600130010 bfset #0x10,X:(R2+0x13)
  1212. *0x00000091 0xA961 bra *-30
  1213. */
  1214. const uint16_t pgm_write_pflash[] = {0x8A46,0x0013,0x407D,0xE700,0xE700,0x8A44,0xFFFE,0x017B,0xE700,0xF514,0x8563,0x8646,0x0020,0x0014,0x8646,0x0080,0x0013,0x8A46,0x0013,0x2004,0x8246,0x0013,0x0020,0xA968,0x8A46,0x0013,0x1065,0x8246,0x0013,0x0010,0xA961};
  1215. const uint32_t pgm_write_pflash_length = 31;
  1216. int dsp5680xx_f_wr(struct target * target, uint8_t *buffer, uint32_t address, uint32_t count){
  1217. int retval = ERROR_OK;
  1218. if (dsp5680xx_target_status(target,NULL,NULL) != TARGET_HALTED){
  1219. retval = eonce_enter_debug_mode(target,NULL);
  1220. err_check_propagate(retval);
  1221. }
  1222. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1223. // Download the pgm that flashes.
  1224. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1225. uint32_t my_favourite_ram_address = 0x8700; // This seems to be a safe address. This one is the one used by codewarrior in 56801x_flash.cfg
  1226. retval = dsp5680xx_write(target, my_favourite_ram_address, 1, pgm_write_pflash_length*2,(uint8_t *) pgm_write_pflash);
  1227. err_check_propagate(retval);
  1228. retval = dsp5680xx_execute_queue();
  1229. err_check_propagate(retval);
  1230. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1231. // Set hfmdiv
  1232. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1233. retval = eonce_set_hfmdiv(target);
  1234. err_check_propagate(retval);
  1235. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1236. // Setup registers needed by pgm_write_pflash
  1237. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1238. dsp5680xx_context.flush = 0;
  1239. retval = eonce_move_long_to_r3(target,address); // Destination address to r3
  1240. err_check_propagate(retval);
  1241. eonce_load_TX_RX_high_to_r0(target); // TX/RX reg address to r0
  1242. err_check_propagate(retval);
  1243. retval = eonce_move_long_to_r2(target,HFM_BASE_ADDR);// FM base address to r2
  1244. err_check_propagate(retval);
  1245. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1246. // Run flashing program.
  1247. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1248. retval = eonce_move_value_at_r2_disp(target,0x00,HFM_CNFG); // write to HFM_CNFG (lock=0, select bank)
  1249. err_check_propagate(retval);
  1250. retval = eonce_move_value_at_r2_disp(target,0x04,HFM_USTAT);// write to HMF_USTAT, clear PVIOL, ACCERR & BLANK bits
  1251. err_check_propagate(retval);
  1252. retval = eonce_move_value_at_r2_disp(target,0x10,HFM_USTAT);// clear only one bit at a time
  1253. err_check_propagate(retval);
  1254. retval = eonce_move_value_at_r2_disp(target,0x20,HFM_USTAT);
  1255. err_check_propagate(retval);
  1256. retval = eonce_move_value_at_r2_disp(target,0x00,HFM_PROT);// write to HMF_PROT, clear protection
  1257. err_check_propagate(retval);
  1258. retval = eonce_move_value_at_r2_disp(target,0x00,HFM_PROTB);// write to HMF_PROTB, clear protection
  1259. err_check_propagate(retval);
  1260. if(count%2){
  1261. //TODO implement handling of odd number of words.
  1262. retval = ERROR_FAIL;
  1263. err_check(retval,"Cannot handle odd number of words.");
  1264. }
  1265. dsp5680xx_context.flush = 1;
  1266. retval = dsp5680xx_execute_queue();
  1267. err_check_propagate(retval);
  1268. uint32_t drscan_data;
  1269. uint16_t tmp = (buffer[0]|(buffer[1]<<8));
  1270. retval = eonce_tx_upper_data(target,tmp,&drscan_data);
  1271. err_check_propagate(retval);
  1272. retval = dsp5680xx_resume(target,0,my_favourite_ram_address,0,0);
  1273. err_check_propagate(retval);
  1274. int counter = FLUSH_COUNT_FLASH;
  1275. dsp5680xx_context.flush = 0;
  1276. uint32_t i;
  1277. for(i=1; (i<count/2)&&(i<HFM_SIZE_WORDS); i++){
  1278. if(--counter==0){
  1279. dsp5680xx_context.flush = 1;
  1280. counter = FLUSH_COUNT_FLASH;
  1281. }
  1282. tmp = (buffer[2*i]|(buffer[2*i+1]<<8));
  1283. retval = eonce_tx_upper_data(target,tmp,&drscan_data);
  1284. if(retval!=ERROR_OK){
  1285. dsp5680xx_context.flush = 1;
  1286. err_check_propagate(retval);
  1287. }
  1288. dsp5680xx_context.flush = 0;
  1289. }
  1290. dsp5680xx_context.flush = 1;
  1291. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1292. // Verify flash
  1293. // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  1294. uint16_t signature;
  1295. uint16_t pc_crc;
  1296. retval = dsp5680xx_f_signature(target,address,i,&signature);
  1297. err_check_propagate(retval);
  1298. pc_crc = perl_crc(buffer,i);
  1299. if(pc_crc != signature){
  1300. retval = ERROR_FAIL;
  1301. err_check(retval,"Flashed data failed CRC check, flash again!");
  1302. }
  1303. return retval;
  1304. }
  1305. int dsp5680xx_f_unlock(struct target * target){
  1306. int retval;
  1307. if(target->tap->enabled){
  1308. //TODO find a way to switch to the master tap here.
  1309. LOG_ERROR("Master tap must be enabled to unlock flash.");
  1310. return ERROR_TARGET_FAILURE;
  1311. }
  1312. uint32_t data_to_shift_in = MASTER_TAP_CMD_FLASH_ERASE;
  1313. uint32_t data_shifted_out;
  1314. retval = dsp5680xx_irscan(target,&data_to_shift_in,&data_shifted_out,8);
  1315. err_check_propagate(retval);
  1316. data_to_shift_in = HFM_CLK_DEFAULT;
  1317. retval = dsp5680xx_drscan(target,((uint8_t *) & data_to_shift_in),((uint8_t *)&data_shifted_out),8);
  1318. err_check_propagate(retval);
  1319. return retval;
  1320. }
  1321. int dsp5680xx_f_lock(struct target * target){
  1322. int retval;
  1323. uint16_t lock_word[] = {HFM_LOCK_FLASH,HFM_LOCK_FLASH};
  1324. retval = dsp5680xx_f_wr(target,(uint8_t *)(lock_word),HFM_LOCK_ADDR_L,4);
  1325. err_check_propagate(retval);
  1326. return retval;
  1327. }
  1328. static int dsp5680xx_step(struct target * target,int current, uint32_t address, int handle_breakpoints){
  1329. err_check(ERROR_FAIL,"Not implemented yet.");
  1330. }
  1331. /** Holds methods for dsp5680xx targets. */
  1332. struct target_type dsp5680xx_target = {
  1333. .name = "dsp5680xx",
  1334. .poll = dsp5680xx_poll,
  1335. .arch_state = dsp5680xx_arch_state,
  1336. .target_request_data = NULL,
  1337. .halt = dsp5680xx_halt,
  1338. .resume = dsp5680xx_resume,
  1339. .step = dsp5680xx_step,
  1340. .write_buffer = dsp5680xx_write_buffer,
  1341. .read_buffer = dsp5680xx_read_buffer,
  1342. .assert_reset = dsp5680xx_assert_reset,
  1343. .deassert_reset = dsp5680xx_deassert_reset,
  1344. .soft_reset_halt = dsp5680xx_soft_reset_halt,
  1345. .read_memory = dsp5680xx_read,
  1346. .write_memory = dsp5680xx_write,
  1347. .bulk_write_memory = dsp5680xx_bulk_write_memory,
  1348. .checksum_memory = dsp5680xx_checksum_memory,
  1349. .target_create = dsp5680xx_target_create,
  1350. .init_target = dsp5680xx_init_target,
  1351. };