1 /***************************************************************************
2 * Copyright (C) 2011 by Rodrigo L. Rosa *
3 * rodrigorosa.LG@gmail.com *
5 * Based on dsp563xx_once.h written by Mathias Kuester *
6 * mkdorg@users.sourceforge.net *
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. *
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. *
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 ***************************************************************************/
28 #include "target_type.h"
29 #include "dsp5680xx.h"
31 struct dsp5680xx_common dsp5680xx_context;
34 #define err_check(retval,err_msg) if(retval != ERROR_OK){LOG_ERROR("%s: %d %s.",__FUNCTION__,__LINE__,err_msg);return retval;}
35 #define err_check_propagate(retval) if(retval!=ERROR_OK){return retval;}
37 int dsp5680xx_execute_queue(void){
39 retval = jtag_execute_queue();
40 err_check_propagate(retval);
44 static int dsp5680xx_drscan(struct target * target, uint8_t * data_to_shift_into_dr, uint8_t * data_shifted_out_of_dr, int len){
45 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
48 // - data_to_shift_into_dr: This is the data that will be shifted into the JTAG DR reg.
49 // - data_shifted_out_of_dr: The data that will be shifted out of the JTAG DR reg will stored here
50 // - len: Length of the data to be shifted to JTAG DR.
52 // Note: If data_shifted_out_of_dr == NULL, discard incoming bits.
54 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
55 int retval = ERROR_OK;
56 if (NULL == target->tap){
58 err_check(retval,"Invalid tap");
62 err_check(retval,"dr_len overflow, maxium is 32");
64 //TODO what values of len are valid for jtag_add_plain_dr_scan?
65 //can i send as many bits as i want?
66 //is the casting necessary?
67 jtag_add_plain_dr_scan(len,data_to_shift_into_dr,data_shifted_out_of_dr, TAP_IDLE);
68 if(dsp5680xx_context.flush){
69 retval = dsp5680xx_execute_queue();
70 err_check_propagate(retval);
72 if(data_shifted_out_of_dr!=NULL){
73 LOG_DEBUG("Data read (%d bits): 0x%04X",len,*data_shifted_out_of_dr);
75 LOG_DEBUG("Data read was discarded.");
79 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){
80 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
82 // - data_to_shift_into_ir: This is the data that will be shifted into the JTAG IR reg.
83 // - data_shifted_out_of_ir: The data that will be shifted out of the JTAG IR reg will stored here
84 // - len: Length of the data to be shifted to JTAG IR.
86 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
87 int retval = ERROR_OK;
88 if (NULL == target->tap){
90 err_check(retval,"Invalid tap");
92 if (ir_len != target->tap->ir_length){
93 LOG_WARNING("%s: Invalid ir_len of core tap. If you are removing protection on flash then do not worry about this warninig.",__FUNCTION__);
94 //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.
96 //TODO what values of len are valid for jtag_add_plain_ir_scan?
97 //can i send as many bits as i want?
98 //is the casting necessary?
99 jtag_add_plain_ir_scan(ir_len,(uint8_t *)data_to_shift_into_ir,(uint8_t *)data_shifted_out_of_ir, TAP_IDLE);
100 if(dsp5680xx_context.flush){
101 retval = dsp5680xx_execute_queue();
102 err_check_propagate(retval);
107 static int dsp5680xx_jtag_status(struct target *target, uint8_t * status){
108 uint32_t read_from_ir;
111 instr = JTAG_INSTR_ENABLE_ONCE;
112 retval = dsp5680xx_irscan(target,& instr, & read_from_ir,DSP5680XX_JTAG_CORE_TAP_IRLEN);
113 err_check_propagate(retval);
115 *status = (uint8_t)read_from_ir;
119 static int jtag_data_read(struct target * target, uint8_t * data_read, int num_bits){
120 uint32_t bogus_instr = 0;
121 int retval = dsp5680xx_drscan(target,(uint8_t *) & bogus_instr,data_read,num_bits);
122 LOG_DEBUG("Data read (%d bits): 0x%04X",num_bits,*data_read);//TODO remove this or move to jtagio?
126 #define jtag_data_read8(target,data_read) jtag_data_read(target,data_read,8)
127 #define jtag_data_read16(target,data_read) jtag_data_read(target,data_read,16)
128 #define jtag_data_read32(target,data_read) jtag_data_read(target,data_read,32)
130 static uint32_t data_read_dummy;
131 static int jtag_data_write(struct target * target, uint32_t instr,int num_bits, uint32_t * data_read){
133 retval = dsp5680xx_drscan(target,(uint8_t *) & instr,(uint8_t *) & data_read_dummy,num_bits);
134 err_check_propagate(retval);
135 if(data_read != NULL)
136 *data_read = data_read_dummy;
140 #define jtag_data_write8(target,instr,data_read) jtag_data_write(target,instr,8,data_read)
141 #define jtag_data_write16(target,instr,data_read) jtag_data_write(target,instr,16,data_read)
142 #define jtag_data_write24(target,instr,data_read) jtag_data_write(target,instr,24,data_read)
143 #define jtag_data_write32(target,instr,data_read) jtag_data_write(target,instr,32,data_read)
146 * Executes EOnCE instruction.
149 * @param instr Instruction to execute.
153 * @param eonce_status Value read from the EOnCE status register.
157 static int eonce_instruction_exec_single(struct target * target, uint8_t instr, uint8_t rw, uint8_t go, uint8_t ex,uint8_t * eonce_status){
160 uint8_t instr_with_flags = instr|(rw<<7)|(go<<6)|(ex<<5);
161 retval = jtag_data_write(target,instr_with_flags,8,&dr_out_tmp);
162 err_check_propagate(retval);
163 if(eonce_status != NULL)
164 *eonce_status = (uint8_t) dr_out_tmp;
168 ///wrappers for multi opcode instructions
169 #define dsp5680xx_exe_1(target,opcode1,opcode2,opcode3) dsp5680xx_exe1(target,opcode1)
170 #define dsp5680xx_exe_2(target,opcode1,opcode2,opcode3) dsp5680xx_exe2(target,opcode1,opcode2)
171 #define dsp5680xx_exe_3(target,opcode1,opcode2,opcode3) dsp5680xx_exe3(target,opcode1,opcode2,opcode3)
172 #define dsp5680xx_exe_generic(target,words,opcode1,opcode2,opcode3) dsp5680xx_exe_##words(target,opcode1,opcode2,opcode3)
174 /// Executes one word DSP instruction
175 static int dsp5680xx_exe1(struct target * target, uint16_t opcode){
177 retval = eonce_instruction_exec_single(target,0x04,0,1,0,NULL);
178 err_check_propagate(retval);
179 retval = jtag_data_write16(target,opcode,NULL);
180 err_check_propagate(retval);
184 /// Executes two word DSP instruction
185 static int dsp5680xx_exe2(struct target * target,uint16_t opcode1, uint16_t opcode2){
187 retval = eonce_instruction_exec_single(target,0x04,0,0,0,NULL);
188 err_check_propagate(retval);
189 retval = jtag_data_write16(target,opcode1,NULL);
190 err_check_propagate(retval);
191 retval = eonce_instruction_exec_single(target,0x04,0,1,0,NULL);
192 err_check_propagate(retval);
193 retval = jtag_data_write16(target,opcode2,NULL);
194 err_check_propagate(retval);
198 /// Executes three word DSP instruction
199 static int dsp5680xx_exe3(struct target * target, uint16_t opcode1,uint16_t opcode2,uint16_t opcode3){
201 retval = eonce_instruction_exec_single(target,0x04,0,0,0,NULL);
202 err_check_propagate(retval);
203 retval = jtag_data_write16(target,opcode1,NULL);
204 err_check_propagate(retval);
205 retval = eonce_instruction_exec_single(target,0x04,0,0,0,NULL);
206 err_check_propagate(retval);
207 retval = jtag_data_write16(target,opcode2,NULL);
208 err_check_propagate(retval);
209 retval = eonce_instruction_exec_single(target,0x04,0,1,0,NULL);
210 err_check_propagate(retval);
211 retval = jtag_data_write16(target,opcode3,NULL);
212 err_check_propagate(retval);
217 * --------------- Real-time data exchange ---------------
218 * The EOnCE Transmit (OTX) and Receive (ORX) registers are data memory mapped, each with an upper and lower 16 bit word.
219 * Transmit and receive directions are defined from the core’s perspective.
220 * 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.
221 * Both registers have a combined data memory mapped OTXRXSR which provides indication when each may be accessed.
222 *ref: eonce_rev.1.0_0208081.pdf@36
225 /// writes data into upper ORx register of the target
226 static int core_tx_upper_data(struct target * target, uint16_t data, uint32_t * eonce_status_low){
228 retval = eonce_instruction_exec_single(target,DSP5680XX_ONCE_ORX1,0,0,0,NULL);
229 err_check_propagate(retval);
230 retval = jtag_data_write16(target,data,eonce_status_low);
231 err_check_propagate(retval);
235 /// writes data into lower ORx register of the target
236 #define core_tx_lower_data(target,data) eonce_instruction_exec_single(target,DSP5680XX_ONCE_ORX,0,0,0,NULL);\
237 jtag_data_write16(target,data)
242 * @param data_read: Returns the data read from the upper OTX register via JTAG.
243 * @return: Returns an error code (see error code documentation)
245 static int core_rx_upper_data(struct target * target, uint8_t * data_read)
248 retval = eonce_instruction_exec_single(target,DSP5680XX_ONCE_OTX1,1,0,0,NULL);
249 err_check_propagate(retval);
250 retval = jtag_data_read16(target,data_read);
251 err_check_propagate(retval);
258 * @param data_read: Returns the data read from the lower OTX register via JTAG.
259 * @return: Returns an error code (see error code documentation)
261 static int core_rx_lower_data(struct target * target,uint8_t * data_read)
264 retval = eonce_instruction_exec_single(target,DSP5680XX_ONCE_OTX,1,0,0,NULL);
265 err_check_propagate(retval);
266 retval = jtag_data_read16(target,data_read);
267 err_check_propagate(retval);
272 * -- -- -- -- --- -- -- -- --- -- -- -- --- -- -- -- --- -- -- -- --- --
273 * -- -- -- -- --- -- -- -Core Instructions- -- -- -- --- -- -- -- --- --
274 * -- -- -- -- --- -- -- -- --- -- -- -- --- -- -- -- --- -- -- -- --- --
278 #define core_move_long_to_r0(target,value) dsp5680xx_exe_generic(target,3,0xe418,value&0xffff,value>>16)
281 #define core_move_long_to_n(target,value) dsp5680xx_exe_generic(target,3,0xe41e,value&0xffff,value>>16)
284 #define core_move_at_r0_to_y0(target) dsp5680xx_exe_generic(target,1,0xF514,0,0)
287 #define core_move_at_r0_to_y1(target) dsp5680xx_exe_generic(target,1,0xF714,0,0)
290 #define core_move_long_at_r0_y(target) dsp5680xx_exe_generic(target,1,0xF734,0,0)
293 #define core_move_y0_at_r0(target) dsp5680xx_exe_generic(target,1,0xd514,0,0)
295 /// bfclr #value,x:(r0)
296 #define eonce_bfclr_at_r0(target,value) dsp5680xx_exe_generic(target,2,0x8040,value,0)
299 #define core_move_value_to_y0(target,value) dsp5680xx_exe_generic(target,2,0x8745,value,0)
301 /// move.w y0,x:(r0)+
302 #define core_move_y0_at_r0_inc(target) dsp5680xx_exe_generic(target,1,0xd500,0,0)
304 /// move.w y0,p:(r0)+
305 #define core_move_y0_at_pr0_inc(target) dsp5680xx_exe_generic(target,1,0x8560,0,0)
307 /// move.w p:(r0)+,y0
308 #define core_move_at_pr0_inc_to_y0(target) dsp5680xx_exe_generic(target,1,0x8568,0,0)
310 /// move.w p:(r0)+,y1
311 #define core_move_at_pr0_inc_to_y1(target) dsp5680xx_exe_generic(target,1,0x8768,0,0)
314 #define core_move_long_to_r2(target,value) dsp5680xx_exe_generic(target,3,0xe41A,value&0xffff,value>>16)
317 #define core_move_y0_at_r2(target) dsp5680xx_exe_generic(target,1,0xd516,0,0)
319 /// move.w #<value>,x:(r2)
320 #define core_move_value_at_r2(target,value) dsp5680xx_exe_generic(target,2,0x8642,value,0)
322 /// move.w #<value>,x:(r0)
323 #define core_move_value_at_r0(target,value) dsp5680xx_exe_generic(target,2,0x8640,value,0)
325 /// move.w #<value>,x:(R2+<disp>)
326 #define core_move_value_at_r2_disp(target,value,disp) dsp5680xx_exe_generic(target,3,0x8646,value,disp)
329 #define core_move_at_r2_to_y0(target) dsp5680xx_exe_generic(target,1,0xF516,0,0)
331 /// move.w p:(r2)+,y0
332 #define core_move_at_pr2_inc_to_y0(target) dsp5680xx_exe_generic(target,1,0x856A,0,0)
335 #define core_move_long_to_r1(target,value) dsp5680xx_exe_generic(target,3,0xE419,value&0xffff,value>>16)
338 #define core_move_long_to_r3(target,value) dsp5680xx_exe_generic(target,3,0xE41B,value&0xffff,value>>16)
340 /// move.w y0,p:(r3)+
341 #define core_move_y0_at_pr3_inc(target) dsp5680xx_exe_generic(target,1,0x8563,0,0)
344 #define core_move_y0_at_r3(target) dsp5680xx_exe_generic(target,1,0xD503,0,0)
347 #define core_move_long_to_r4(target,value) dsp5680xx_exe_generic(target,3,0xE41C,value&0xffff,value>>16)
350 #define core_move_pc_to_r4(target) dsp5680xx_exe_generic(target,1,0xE716,0,0)
353 #define core_move_r4_to_y(target) dsp5680xx_exe_generic(target,1,0xe764,0,0)
355 /// move.w p:(r0)+,y0
356 #define core_move_at_pr0_inc_to_y0(target) dsp5680xx_exe_generic(target,1,0x8568,0,0)
358 /// move.w x:(r0)+,y0
359 #define core_move_at_r0_inc_to_y0(target) dsp5680xx_exe_generic(target,1,0xf500,0,0)
362 #define core_move_at_r0_y0(target) dsp5680xx_exe_generic(target,1,0xF514,0,0)
365 #define eonce_nop(target) dsp5680xx_exe_generic(target,1,0xe700,0,0)
367 /// move.w x:(R2+<disp>),Y0
368 #define core_move_at_r2_disp_to_y0(target,disp) dsp5680xx_exe_generic(target,2,0xF542,disp,0)
371 #define core_move_y1_at_r2(target) dsp5680xx_exe_generic(target,1,0xd716,0,0)
374 #define core_move_y1_at_r0(target) dsp5680xx_exe_generic(target,1,0xd714,0,0)
376 /// move.bp y0,x:(r0)+
377 #define core_move_byte_y0_at_r0(target) dsp5680xx_exe_generic(target,1,0xd5a0,0,0)
379 /// move.w y1,p:(r0)+
380 #define core_move_y1_at_pr0_inc(target) dsp5680xx_exe_generic(target,1,0x8760,0,0)
382 /// move.w y1,x:(r0)+
383 #define core_move_y1_at_r0_inc(target) dsp5680xx_exe_generic(target,1,0xD700,0,0)
386 #define core_move_long_to_y(target,value) dsp5680xx_exe_generic(target,3,0xe417,value&0xffff,value>>16)
388 static int core_move_value_to_pc(struct target * target, uint32_t value){
389 if (!(target->state == TARGET_HALTED)){
390 LOG_ERROR("Target must be halted to move PC. Target state = %d.",target->state);
391 return ERROR_TARGET_NOT_HALTED;
394 retval = dsp5680xx_exe_generic(target,3,0xE71E,value&0xffff,value>>16);
395 err_check_propagate(retval);
399 static int eonce_load_TX_RX_to_r0(struct target * target)
402 retval = core_move_long_to_r0(target,((MC568013_EONCE_TX_RX_ADDR)+(MC568013_EONCE_OBASE_ADDR<<16)));
406 static int core_load_TX_RX_high_addr_to_r0(struct target * target)
409 retval = core_move_long_to_r0(target,((MC568013_EONCE_TX1_RX1_HIGH_ADDR)+(MC568013_EONCE_OBASE_ADDR<<16)));
413 static int dsp5680xx_read_core_reg(struct target * target, uint8_t reg_addr, uint16_t * data_read)
415 //TODO implement a general version of this which matches what openocd uses.
417 uint32_t dummy_data_to_shift_into_dr;
418 retval = eonce_instruction_exec_single(target,reg_addr,1,0,0,NULL);
419 err_check_propagate(retval);
420 retval = dsp5680xx_drscan(target,(uint8_t *)& dummy_data_to_shift_into_dr,(uint8_t *) data_read, 8);
421 err_check_propagate(retval);
422 LOG_DEBUG("Reg. data: 0x%02X.",*data_read);
426 static int eonce_read_status_reg(struct target * target, uint16_t * data){
428 retval = dsp5680xx_read_core_reg(target,DSP5680XX_ONCE_OSR,data);
429 err_check_propagate(retval);
434 * Takes the core out of debug mode.
437 * @param eonce_status Data read from the EOnCE status register.
441 static int eonce_exit_debug_mode(struct target * target,uint8_t * eonce_status){
443 retval = eonce_instruction_exec_single(target,0x1F,0,0,1,eonce_status);
444 err_check_propagate(retval);
449 * Puts the core into debug mode, enabling the EOnCE module.
452 * @param eonce_status Data read from the EOnCE status register.
456 static int eonce_enter_debug_mode(struct target * target, uint16_t * eonce_status){
458 uint32_t instr = JTAG_INSTR_DEBUG_REQUEST;
459 uint32_t ir_out;//not used, just to make jtag happy.
461 retval = dsp5680xx_irscan(target,& instr,& ir_out,DSP5680XX_JTAG_CORE_TAP_IRLEN);
462 err_check_propagate(retval);
464 // Enable EOnCE module
465 instr = JTAG_INSTR_ENABLE_ONCE;
466 //Two rounds of jtag 0x6 (enable eonce) to enable EOnCE.
467 retval = dsp5680xx_irscan(target, & instr, & ir_out,DSP5680XX_JTAG_CORE_TAP_IRLEN);
468 err_check_propagate(retval);
469 retval = dsp5680xx_irscan(target, & instr, & ir_out,DSP5680XX_JTAG_CORE_TAP_IRLEN);
470 err_check_propagate(retval);
471 // Verify that debug mode is enabled
472 uint16_t data_read_from_dr;
473 retval = eonce_read_status_reg(target,&data_read_from_dr);
474 err_check_propagate(retval);
475 if((data_read_from_dr&0x30) == 0x30){
476 LOG_DEBUG("EOnCE successfully entered debug mode.");
477 target->state = TARGET_HALTED;
480 retval = ERROR_TARGET_FAILURE;
481 err_check(retval,"Failed to set EOnCE module to debug mode.");
483 if(eonce_status!=NULL)
484 *eonce_status = data_read_from_dr;
489 * Reads the current value of the program counter and stores it.
495 static int eonce_pc_store(struct target * target){
498 retval = core_move_pc_to_r4(target);
499 err_check_propagate(retval);
500 retval = core_move_r4_to_y(target);
501 err_check_propagate(retval);
502 retval = eonce_load_TX_RX_to_r0(target);
503 err_check_propagate(retval);
504 retval = core_move_y0_at_r0(target);
505 err_check_propagate(retval);
506 retval = core_rx_lower_data(target,tmp);
507 err_check_propagate(retval);
508 LOG_USER("PC value: 0x%X%X\n",tmp[1],tmp[0]);
509 dsp5680xx_context.stored_pc = (tmp[0]|(tmp[1]<<8));
513 static int dsp5680xx_target_create(struct target *target, Jim_Interp * interp){
514 struct dsp5680xx_common *dsp5680xx = calloc(1, sizeof(struct dsp5680xx_common));
515 target->arch_info = dsp5680xx;
519 static int dsp5680xx_init_target(struct command_context *cmd_ctx, struct target *target){
520 dsp5680xx_context.stored_pc = 0;
521 dsp5680xx_context.flush = 1;
522 LOG_DEBUG("target initiated!");
523 //TODO core tap must be enabled before running these commands, currently this is done in the .cfg tcl script.
527 static int dsp5680xx_arch_state(struct target *target){
528 LOG_USER("%s not implemented yet.",__FUNCTION__);
532 int dsp5680xx_target_status(struct target * target, uint8_t * jtag_st, uint16_t * eonce_st){
533 return target->state;
536 static int dsp5680xx_assert_reset(struct target *target){
537 target->state = TARGET_RESET;
541 static int dsp5680xx_deassert_reset(struct target *target){
542 target->state = TARGET_RUNNING;
546 static int dsp5680xx_halt(struct target *target){
548 uint16_t eonce_status = 0xbeef;
549 if(target->state == TARGET_HALTED){
550 LOG_USER("Target already halted.");
553 retval = eonce_enter_debug_mode(target,&eonce_status);
554 err_check_propagate(retval);
555 retval = eonce_pc_store(target);
556 err_check_propagate(retval);
557 //TODO is it useful to store the pc?
561 static int dsp5680xx_poll(struct target *target){
564 uint8_t eonce_status;
566 retval = dsp5680xx_jtag_status(target,&jtag_status);
567 err_check_propagate(retval);
568 if (jtag_status == JTAG_STATUS_DEBUG)
569 if (target->state != TARGET_HALTED){
570 retval = eonce_enter_debug_mode(target,&read_tmp);
571 err_check_propagate(retval);
572 eonce_status = (uint8_t) read_tmp;
573 if((eonce_status&EONCE_STAT_MASK) != DSP5680XX_ONCE_OSCR_DEBUG_M){
574 LOG_WARNING("%s: Failed to put EOnCE in debug mode. Is flash locked?...",__FUNCTION__);
575 return ERROR_TARGET_FAILURE;
577 target->state = TARGET_HALTED;
581 if (jtag_status == JTAG_STATUS_NORMAL){
582 if(target->state == TARGET_RESET){
583 retval = dsp5680xx_halt(target);
584 err_check_propagate(retval);
585 retval = eonce_exit_debug_mode(target,&eonce_status);
586 err_check_propagate(retval);
587 if((eonce_status&EONCE_STAT_MASK) != DSP5680XX_ONCE_OSCR_NORMAL_M){
588 LOG_WARNING("%s: JTAG running, but cannot make EOnCE run. Try resetting...",__FUNCTION__);
589 return ERROR_TARGET_FAILURE;
591 target->state = TARGET_RUNNING;
595 if(target->state != TARGET_RUNNING){
596 retval = eonce_read_status_reg(target,&read_tmp);
597 err_check_propagate(retval);
598 eonce_status = (uint8_t) read_tmp;
599 if((eonce_status&EONCE_STAT_MASK) != DSP5680XX_ONCE_OSCR_NORMAL_M){
600 LOG_WARNING("Inconsistent target status. Restart!");
601 return ERROR_TARGET_FAILURE;
604 target->state = TARGET_RUNNING;
607 if(jtag_status == JTAG_STATUS_DEAD){
608 LOG_ERROR("%s: Cannot communicate with JTAG. Check connection...",__FUNCTION__);
609 target->state = TARGET_UNKNOWN;
610 return ERROR_TARGET_FAILURE;
612 if (target->state == TARGET_UNKNOWN){
613 LOG_ERROR("%s: Target status invalid - communication failure",__FUNCTION__);
614 return ERROR_TARGET_FAILURE;
619 static int dsp5680xx_resume(struct target *target, int current, uint32_t address,int handle_breakpoints, int debug_execution){
620 if(target->state == TARGET_RUNNING){
621 LOG_USER("Target already running.");
625 uint8_t eonce_status;
627 retval = core_move_value_to_pc(target,address);
628 err_check_propagate(retval);
633 retval = eonce_exit_debug_mode(target,&eonce_status );
634 err_check_propagate(retval);
635 if(eonce_status == DSP5680XX_ONCE_OSCR_NORMAL_M)
639 retval = ERROR_TARGET_FAILURE;
640 err_check(retval,"Failed to resume...");
642 target->state = TARGET_RUNNING;
644 LOG_DEBUG("EOnCE status: 0x%02X.",eonce_status);
654 * 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.
655 * The special case of 0xFFXXXX is not modified, since it allows to read out the memory mapped EOnCE registers.
662 static int dsp5680xx_convert_address(uint32_t * address, int * pmem){
663 // Distinguish data memory (x:) from program memory (p:) by the address.
664 // Addresses over S_FILE_DATA_OFFSET are considered (x:) memory.
665 if(*address >= S_FILE_DATA_OFFSET){
667 if(((*address)&0xff0000)!=0xff0000)
668 *address -= S_FILE_DATA_OFFSET;
673 static int dsp5680xx_read_16_single(struct target * target, uint32_t address, uint8_t * data_read, int r_pmem){
675 retval = core_move_long_to_r0(target,address);
676 err_check_propagate(retval);
678 retval = core_move_at_pr0_inc_to_y0(target);
680 retval = core_move_at_r0_to_y0(target);
681 err_check_propagate(retval);
682 retval = eonce_load_TX_RX_to_r0(target);
683 err_check_propagate(retval);
684 retval = core_move_y0_at_r0(target);
685 err_check_propagate(retval);
686 // at this point the data i want is at the reg eonce can read
687 retval = core_rx_lower_data(target,data_read);
688 err_check_propagate(retval);
689 LOG_DEBUG("%s: Data read from 0x%06X: 0x%02X%02X",__FUNCTION__, address,data_read[1],data_read[0]);
693 static int dsp5680xx_read_32_single(struct target * target, uint32_t address, uint8_t * data_read, int r_pmem){
695 address = (address & 0xFFFFFE);
696 // Get data to an intermediate register
697 retval = core_move_long_to_r0(target,address);
698 err_check_propagate(retval);
700 retval = core_move_at_pr0_inc_to_y0(target);
701 err_check_propagate(retval);
702 retval = core_move_at_pr0_inc_to_y1(target);
703 err_check_propagate(retval);
705 retval = core_move_at_r0_inc_to_y0(target);
706 err_check_propagate(retval);
707 retval = core_move_at_r0_to_y1(target);
708 err_check_propagate(retval);
710 // Get lower part of data to TX/RX
711 retval = eonce_load_TX_RX_to_r0(target);
712 err_check_propagate(retval);
713 retval = core_move_y0_at_r0_inc(target); // This also load TX/RX high to r0
714 err_check_propagate(retval);
715 // Get upper part of data to TX/RX
716 retval = core_move_y1_at_r0(target);
717 err_check_propagate(retval);
718 // at this point the data i want is at the reg eonce can read
719 retval = core_rx_lower_data(target,data_read);
720 err_check_propagate(retval);
721 retval = core_rx_upper_data(target,data_read+2);
722 err_check_propagate(retval);
726 static int dsp5680xx_read(struct target * target, uint32_t address, unsigned size, unsigned count, uint8_t * buffer){
727 if(target->state != TARGET_HALTED){
728 LOG_USER("Target must be halted.");
731 int retval = ERROR_OK;
734 retval = dsp5680xx_convert_address(&address, &pmem);
735 err_check_propagate(retval);
737 dsp5680xx_context.flush = 0;
738 int counter = FLUSH_COUNT_READ_WRITE;
740 for (unsigned i=0; i<count; i++){
742 dsp5680xx_context.flush = 1;
743 counter = FLUSH_COUNT_READ_WRITE;
748 retval = dsp5680xx_read_16_single(target, address + i/2, buffer + i, pmem);
752 retval = dsp5680xx_read_16_single(target, address + i, buffer+2*i, pmem);
755 retval = dsp5680xx_read_32_single(target, address + 2*i, buffer + 4*i, pmem);
758 LOG_USER("%s: Invalid read size.",__FUNCTION__);
761 err_check_propagate(retval);
762 dsp5680xx_context.flush = 0;
765 dsp5680xx_context.flush = 1;
766 retval = dsp5680xx_execute_queue();
767 err_check_propagate(retval);
772 static int dsp5680xx_write_16_single(struct target *target, uint32_t address, uint16_t data, uint8_t w_pmem){
774 retval = core_move_long_to_r0(target,address);
775 err_check_propagate(retval);
777 retval = core_move_value_to_y0(target,data);
778 err_check_propagate(retval);
779 retval = core_move_y0_at_pr0_inc(target);
780 err_check_propagate(retval);
782 retval = core_move_value_at_r0(target,data);
783 err_check_propagate(retval);
788 static int dsp5680xx_write_32_single(struct target *target, uint32_t address, uint32_t data, int w_pmem){
790 retval = core_move_long_to_r0(target,address);
791 err_check_propagate(retval);
792 retval = core_move_long_to_y(target,data);
793 err_check_propagate(retval);
795 retval = core_move_y0_at_pr0_inc(target);
797 retval = core_move_y0_at_r0_inc(target);
798 err_check_propagate(retval);
800 retval = core_move_y1_at_pr0_inc(target);
802 retval = core_move_y1_at_r0_inc(target);
803 err_check_propagate(retval);
807 static int dsp5680xx_write_8(struct target * target, uint32_t address, uint32_t count, const uint8_t * data, int pmem){
808 if(target->state != TARGET_HALTED){
809 LOG_ERROR("%s: Target must be halted.",__FUNCTION__);
816 int counter = FLUSH_COUNT_READ_WRITE;
817 for(iter = 0; iter<count/2; iter++){
819 dsp5680xx_context.flush = 1;
820 counter = FLUSH_COUNT_READ_WRITE;
822 data_16=(data[2*iter]|(data[2*iter+1]<<8));
823 retval = dsp5680xx_write_16_single(target,address+iter,data_16, pmem);
824 if(retval != ERROR_OK){
825 LOG_ERROR("%s: Could not write to p:0x%04X",__FUNCTION__,address);
826 dsp5680xx_context.flush = 1;
829 dsp5680xx_context.flush = 0;
831 dsp5680xx_context.flush = 1;
833 // Only one byte left, let's not overwrite the other byte (mem is 16bit)
834 // Need to retrieve the part we do not want to overwrite.
836 if((count==1)||(count%2)){
837 retval = dsp5680xx_read(target,address+iter,1,1,(uint8_t *)&data_old);
838 err_check_propagate(retval);
840 data_old=(((data_old&0xff)<<8)|data[0]);// preserve upper byte
842 data_old=(((data_old&0xff)<<8)|data[2*iter+1]);
843 retval = dsp5680xx_write_16_single(target,address+iter,data_old, pmem);
844 err_check_propagate(retval);
849 static int dsp5680xx_write_16(struct target * target, uint32_t address, uint32_t count, const uint8_t * data, int pmem){
850 int retval = ERROR_OK;
851 if(target->state != TARGET_HALTED){
852 retval = ERROR_TARGET_NOT_HALTED;
853 err_check(retval,"Target must be halted.");
856 int counter = FLUSH_COUNT_READ_WRITE;
858 for(iter = 0; iter<count; iter++){
860 dsp5680xx_context.flush = 1;
861 counter = FLUSH_COUNT_READ_WRITE;
863 retval = dsp5680xx_write_16_single(target,address+iter,data[iter], pmem);
864 if(retval != ERROR_OK){
865 LOG_ERROR("%s: Could not write to p:0x%04X",__FUNCTION__,address);
866 dsp5680xx_context.flush = 1;
869 dsp5680xx_context.flush = 0;
871 dsp5680xx_context.flush = 1;
875 static int dsp5680xx_write_32(struct target * target, uint32_t address, uint32_t count, const uint8_t * data, int pmem){
876 int retval = ERROR_OK;
877 if(target->state != TARGET_HALTED){
878 retval = ERROR_TARGET_NOT_HALTED;
879 err_check(retval,"Target must be halted.");
882 int counter = FLUSH_COUNT_READ_WRITE;
884 for(iter = 0; iter<count; iter++){
886 dsp5680xx_context.flush = 1;
887 counter = FLUSH_COUNT_READ_WRITE;
889 retval = dsp5680xx_write_32_single(target,address+(iter<<1),data[iter], pmem);
890 if(retval != ERROR_OK){
891 LOG_ERROR("%s: Could not write to p:0x%04X",__FUNCTION__,address);
892 dsp5680xx_context.flush = 1;
895 dsp5680xx_context.flush = 0;
897 dsp5680xx_context.flush = 1;
902 * Writes @buffer to memory.
903 * The parameter @address determines whether @buffer should be written to P: (program) memory or X: (data) memory.
907 * @param size Bytes (1), Half words (2), Words (4).
908 * @param count In bytes.
913 static int dsp5680xx_write(struct target *target, uint32_t address, uint32_t size, uint32_t count, const uint8_t * buffer){
914 //TODO Cannot write 32bit to odd address, will write 0x12345678 as 0x5678 0x0012
915 if(target->state != TARGET_HALTED){
916 LOG_USER("Target must be halted.");
921 retval = dsp5680xx_convert_address(&address, &p_mem);
922 err_check_propagate(retval);
926 retval = dsp5680xx_write_8(target, address, count, buffer, p_mem);
929 retval = dsp5680xx_write_16(target, address, count, buffer, p_mem);
932 retval = dsp5680xx_write_32(target, address, count, buffer, p_mem);
935 retval = ERROR_TARGET_DATA_ABORT;
936 err_check(retval,"Invalid data size.");
942 static int dsp5680xx_bulk_write_memory(struct target * target,uint32_t address, uint32_t aligned, const uint8_t * buffer){
943 LOG_ERROR("Not implemented yet.");
947 static int dsp5680xx_write_buffer(struct target * target, uint32_t address, uint32_t size, const uint8_t * buffer){
948 if(target->state != TARGET_HALTED){
949 LOG_USER("Target must be halted.");
952 return dsp5680xx_write(target, address, 1, size, buffer);
956 * This function is called by verify_image, it is used to read data from memory.
959 * @param address Word addressing.
960 * @param size In bytes.
965 static int dsp5680xx_read_buffer(struct target * target, uint32_t address, uint32_t size, uint8_t * buffer){
966 if(target->state != TARGET_HALTED){
967 LOG_USER("Target must be halted.");
970 // The "/2" solves the byte/word addressing issue.
971 return dsp5680xx_read(target,address,2,size/2,buffer);
975 * This function is not implemented.
976 * It returns an error in order to get OpenOCD to do read out the data and calculate the CRC, or try a binary comparison.
979 * @param address Start address of the image.
980 * @param size In bytes.
985 static int dsp5680xx_checksum_memory(struct target * target, uint32_t address, uint32_t size, uint32_t * checksum){
990 * 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.
991 * This algorithm is based on the perl script available from the Freescale website at FAQ 25630.
998 static int perl_crc(uint8_t * buff8,uint32_t word_count){
999 uint16_t checksum = 0xffff;
1000 uint16_t data,fbmisr;
1002 for(i=0;i<word_count;i++){
1003 data = (buff8[2*i]|(buff8[2*i+1]<<8));
1004 fbmisr = (checksum & 2)>>1 ^ (checksum & 4)>>2 ^ (checksum & 16)>>4 ^ (checksum & 0x8000)>>15;
1005 checksum = (data ^ ((checksum << 1) | fbmisr));
1008 for(;!(i&0x80000000);i--){
1009 data = (buff8[2*i]|(buff8[2*i+1]<<8));
1010 fbmisr = (checksum & 2)>>1 ^ (checksum & 4)>>2 ^ (checksum & 16)>>4 ^ (checksum & 0x8000)>>15;
1011 checksum = (data ^ ((checksum << 1) | fbmisr));
1017 * Resets the SIM. (System Integration Module).
1023 int dsp5680xx_f_SIM_reset(struct target * target){
1024 int retval = ERROR_OK;
1025 uint16_t sim_cmd = SIM_CMD_RESET;
1027 if(strcmp(target->tap->chip,"dsp568013")==0){
1028 sim_addr = MC568013_SIM_BASE_ADDR+S_FILE_DATA_OFFSET;
1029 retval = dsp5680xx_write(target,sim_addr,1,2,(const uint8_t *)&sim_cmd);
1030 err_check_propagate(retval);
1036 * Halts the core and resets the SIM. (System Integration Module).
1042 static int dsp5680xx_soft_reset_halt(struct target *target){
1043 //TODO is this what this function is expected to do...?
1045 retval = dsp5680xx_halt(target);
1046 err_check_propagate(retval);
1047 retval = dsp5680xx_f_SIM_reset(target);
1048 err_check_propagate(retval);
1052 int dsp5680xx_f_protect_check(struct target * target, uint16_t * protected) {
1054 if (dsp5680xx_target_status(target,NULL,NULL) != TARGET_HALTED){
1055 retval = dsp5680xx_halt(target);
1056 err_check_propagate(retval);
1058 if(protected == NULL){
1059 err_check(ERROR_FAIL,"NULL pointer not valid.");
1061 retval = dsp5680xx_read_16_single(target,HFM_BASE_ADDR|HFM_PROT,(uint8_t *)protected,0);
1062 err_check_propagate(retval);
1067 * Executes a command on the FM module. Some commands use the parameters @address and @data, others ignore them.
1070 * @param command Command to execute.
1071 * @param address Command parameter.
1072 * @param data Command parameter.
1073 * @param hfm_ustat FM status register.
1074 * @param pmem Address is P: (program) memory (@pmem==1) or X: (data) memory (@pmem==0)
1078 static int dsp5680xx_f_execute_command(struct target * target, uint16_t command, uint32_t address, uint32_t data, uint16_t * hfm_ustat, int pmem){
1080 retval = core_load_TX_RX_high_addr_to_r0(target);
1081 err_check_propagate(retval);
1082 retval = core_move_long_to_r2(target,HFM_BASE_ADDR);
1083 err_check_propagate(retval);
1087 retval = core_move_at_r2_disp_to_y0(target,HFM_USTAT); // read HMF_USTAT
1088 err_check_propagate(retval);
1089 retval = core_move_y0_at_r0(target);
1090 err_check_propagate(retval);
1091 retval = core_rx_upper_data(target,i);
1092 err_check_propagate(retval);
1093 if((watchdog--)==1){
1094 retval = ERROR_TARGET_FAILURE;
1095 err_check(retval,"FM execute command failed.");
1097 }while (!(i[0]&0x40)); // wait until current command is complete
1099 dsp5680xx_context.flush = 0;
1101 retval = core_move_value_at_r2_disp(target,0x00,HFM_CNFG); // write to HFM_CNFG (lock=0, select bank) -- flash_desc.bank&0x03,0x01 == 0x00,0x01 ???
1102 err_check_propagate(retval);
1103 retval = core_move_value_at_r2_disp(target,0x04,HFM_USTAT); // write to HMF_USTAT, clear PVIOL, ACCERR & BLANK bits
1104 err_check_propagate(retval);
1105 retval = core_move_value_at_r2_disp(target,0x10,HFM_USTAT); // clear only one bit at a time
1106 err_check_propagate(retval);
1107 retval = core_move_value_at_r2_disp(target,0x20,HFM_USTAT);
1108 err_check_propagate(retval);
1109 retval = core_move_value_at_r2_disp(target,0x00,HFM_PROT); // write to HMF_PROT, clear protection
1110 err_check_propagate(retval);
1111 retval = core_move_value_at_r2_disp(target,0x00,HFM_PROTB); // write to HMF_PROTB, clear protection
1112 err_check_propagate(retval);
1113 retval = core_move_value_to_y0(target,data);
1114 err_check_propagate(retval);
1115 retval = core_move_long_to_r3(target,address); // write to the flash block
1116 err_check_propagate(retval);
1118 retval = core_move_y0_at_pr3_inc(target);
1119 err_check_propagate(retval);
1121 retval = core_move_y0_at_r3(target);
1122 err_check_propagate(retval);
1124 retval = core_move_value_at_r2_disp(target,command,HFM_CMD); // write command to the HFM_CMD reg
1125 err_check_propagate(retval);
1126 retval = core_move_value_at_r2_disp(target,0x80,HFM_USTAT); // start the command
1127 err_check_propagate(retval);
1129 dsp5680xx_context.flush = 1;
1130 retval = dsp5680xx_execute_queue();
1131 err_check_propagate(retval);
1135 retval = core_move_at_r2_disp_to_y0(target,HFM_USTAT); // read HMF_USTAT
1136 err_check_propagate(retval);
1137 retval = core_move_y0_at_r0(target);
1138 err_check_propagate(retval);
1139 retval = core_rx_upper_data(target,i);
1140 err_check_propagate(retval);
1141 if((watchdog--)==1){
1142 retval = ERROR_TARGET_FAILURE;
1143 err_check(retval,"FM execution did not finish.");
1145 }while (!(i[0]&0x40)); // wait until the command is complete
1146 *hfm_ustat = ((i[0]<<8)|(i[1]));
1147 if (i[0]&HFM_USTAT_MASK_PVIOL_ACCER){
1148 retval = ERROR_TARGET_FAILURE;
1149 err_check(retval,"pviol and/or accer bits set. HFM command execution error");
1155 * 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.)
1161 static int set_fm_ck_div(struct target * target){
1164 retval = core_move_long_to_r2(target,HFM_BASE_ADDR);
1165 err_check_propagate(retval);
1166 retval = core_load_TX_RX_high_addr_to_r0(target);
1167 err_check_propagate(retval);
1168 retval = core_move_at_r2_to_y0(target);// read HFM_CLKD
1169 err_check_propagate(retval);
1170 retval = core_move_y0_at_r0(target);
1171 err_check_propagate(retval);
1172 retval = core_rx_upper_data(target,i);
1173 err_check_propagate(retval);
1174 unsigned int hfm_at_wrong_value = 0;
1175 if ((i[0]&0x7f)!=HFM_CLK_DEFAULT) {
1176 LOG_DEBUG("HFM CLK divisor contained incorrect value (0x%02X).",i[0]&0x7f);
1177 hfm_at_wrong_value = 1;
1179 LOG_DEBUG("HFM CLK divisor was already set to correct value (0x%02X).",i[0]&0x7f);
1182 retval = core_move_value_at_r2(target,HFM_CLK_DEFAULT); // write HFM_CLKD
1183 err_check_propagate(retval);
1184 retval = core_move_at_r2_to_y0(target); // verify HFM_CLKD
1185 err_check_propagate(retval);
1186 retval = core_move_y0_at_r0(target);
1187 err_check_propagate(retval);
1188 retval = core_rx_upper_data(target,i);
1189 err_check_propagate(retval);
1190 if (i[0]!=(0x80|(HFM_CLK_DEFAULT&0x7f))) {
1191 retval = ERROR_TARGET_FAILURE;
1192 err_check(retval,"Unable to set HFM CLK divisor.");
1194 if(hfm_at_wrong_value)
1195 LOG_DEBUG("HFM CLK divisor set to 0x%02x.",i[0]&0x7f);
1200 * 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.
1203 * @param address Start of flash array where the signature should be calculated.
1204 * @param words Number of words over which the signature should be calculated.
1205 * @param signature Value calculated by the FM.
1209 static int dsp5680xx_f_signature(struct target * target, uint32_t address, uint32_t words, uint16_t * signature){
1212 if (dsp5680xx_target_status(target,NULL,NULL) != TARGET_HALTED){
1213 retval = eonce_enter_debug_mode(target,NULL);
1214 err_check_propagate(retval);
1216 retval = dsp5680xx_f_execute_command(target,HFM_CALCULATE_DATA_SIGNATURE,address,words,&hfm_ustat,1);
1217 err_check_propagate(retval);
1218 retval = dsp5680xx_read_16_single(target, HFM_BASE_ADDR|HFM_DATA, (uint8_t *)signature, 0);
1222 int dsp5680xx_f_erase_check(struct target * target, uint8_t * erased,uint32_t sector){
1225 if (dsp5680xx_target_status(target,NULL,NULL) != TARGET_HALTED){
1226 retval = dsp5680xx_halt(target);
1227 err_check_propagate(retval);
1229 retval = set_fm_ck_div(target);
1230 err_check_propagate(retval);
1231 // Check if chip is already erased.
1232 retval = dsp5680xx_f_execute_command(target,HFM_ERASE_VERIFY,HFM_FLASH_BASE_ADDR+sector*HFM_SECTOR_SIZE/2,0,&hfm_ustat,1); // blank check
1233 err_check_propagate(retval);
1235 *erased = (uint8_t)(hfm_ustat&HFM_USTAT_MASK_BLANK);
1240 * Executes the FM page erase command.
1243 * @param sector Page to erase.
1244 * @param hfm_ustat FM module status register.
1248 static int erase_sector(struct target * target, int sector, uint16_t * hfm_ustat){
1250 retval = dsp5680xx_f_execute_command(target,HFM_PAGE_ERASE,HFM_FLASH_BASE_ADDR+sector*HFM_SECTOR_SIZE/2,0,hfm_ustat,1);
1251 err_check_propagate(retval);
1256 * Executes the FM mass erase command. Erases the flash array completely.
1259 * @param hfm_ustat FM module status register.
1263 static int mass_erase(struct target * target, uint16_t * hfm_ustat){
1265 retval = dsp5680xx_f_execute_command(target,HFM_MASS_ERASE,0,0,hfm_ustat,1);
1269 int dsp5680xx_f_erase(struct target * target, int first, int last){
1271 if (dsp5680xx_target_status(target,NULL,NULL) != TARGET_HALTED){
1272 retval = dsp5680xx_halt(target);
1273 err_check_propagate(retval);
1275 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1277 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1278 retval = dsp5680xx_f_SIM_reset(target);
1279 err_check_propagate(retval);
1280 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1282 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1283 retval = set_fm_ck_div(target);
1284 err_check_propagate(retval);
1287 int do_mass_erase = ((!(first|last)) || ((first==0)&&(last == (HFM_SECTOR_COUNT-1))));
1290 retval = mass_erase(target,&hfm_ustat);
1291 err_check_propagate(retval);
1292 last = HFM_SECTOR_COUNT-1;
1294 for(int i = first;i<=last;i++){
1295 retval = erase_sector(target,i,&hfm_ustat);
1296 err_check_propagate(retval);
1303 * Algorithm for programming normal p: flash
1304 * Follow state machine from "56F801x Peripheral Reference Manual"@163.
1305 * Registers to set up before calling:
1306 * r0: TX/RX high address.
1307 * r2: FM module base address.
1308 * r3: Destination address in flash.
1310 * hfm_wait: // wait for command to finish
1311 * brclr #0x40,x:(r2+0x13),hfm_wait
1312 * rx_check: // wait for input buffer full
1313 * brclr #0x01,x:(r0-2),rx_check
1314 * move.w x:(r0),y0 // read from Rx buffer
1316 * move.w #0x20,x:(r2+0x14) // write PGM command
1317 * move.w #0x80,x:(r2+0x13) // start the command
1318 * brclr #0x20,X:(R2+0x13),accerr_check // protection violation check
1319 * bfset #0x20,X:(R2+0x13) // clear pviol
1322 * brclr #0x10,X:(R2+0x13),hfm_wait // access error check
1323 * bfset #0x10,X:(R2+0x13) // clear accerr
1324 * bra hfm_wait // loop
1325 *0x00000073 0x8A460013407D brclr #0x40,X:(R2+0x13),*+0
1326 *0x00000076 0xE700 nop
1327 *0x00000077 0xE700 nop
1328 *0x00000078 0x8A44FFFE017B brclr #1,X:(R0-2),*-2
1329 *0x0000007B 0xE700 nop
1330 *0x0000007C 0xF514 move.w X:(R0),Y0
1331 *0x0000007D 0x8563 move.w Y0,P:(R3)+
1332 *0x0000007E 0x864600200014 move.w #0x20,X:(R2+0x14)
1333 *0x00000081 0x864600800013 move.w #0x80,X:(R2+0x13)
1334 *0x00000084 0x8A4600132004 brclr #0x20,X:(R2+0x13),*+7
1335 *0x00000087 0x824600130020 bfset #0x20,X:(R2+0x13)
1336 *0x0000008A 0xA968 bra *-23
1337 *0x0000008B 0x8A4600131065 brclr #0x10,X:(R2+0x13),*-24
1338 *0x0000008E 0x824600130010 bfset #0x10,X:(R2+0x13)
1339 *0x00000091 0xA961 bra *-30
1341 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};
1342 const uint32_t pgm_write_pflash_length = 31;
1344 int dsp5680xx_f_wr(struct target * target, uint8_t *buffer, uint32_t address, uint32_t count, int is_flash_lock){
1345 int retval = ERROR_OK;
1346 if (dsp5680xx_target_status(target,NULL,NULL) != TARGET_HALTED){
1347 retval = eonce_enter_debug_mode(target,NULL);
1348 err_check_propagate(retval);
1350 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1351 // Download the pgm that flashes.
1352 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1353 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
1355 retval = dsp5680xx_write(target, my_favourite_ram_address, 1, pgm_write_pflash_length*2,(uint8_t *) pgm_write_pflash);
1356 err_check_propagate(retval);
1357 retval = dsp5680xx_execute_queue();
1358 err_check_propagate(retval);
1360 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1362 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1363 retval = set_fm_ck_div(target);
1364 err_check_propagate(retval);
1365 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1366 // Setup registers needed by pgm_write_pflash
1367 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1369 dsp5680xx_context.flush = 0;
1371 retval = core_move_long_to_r3(target,address); // Destination address to r3
1372 err_check_propagate(retval);
1373 core_load_TX_RX_high_addr_to_r0(target); // TX/RX reg address to r0
1374 err_check_propagate(retval);
1375 retval = core_move_long_to_r2(target,HFM_BASE_ADDR);// FM base address to r2
1376 err_check_propagate(retval);
1377 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1378 // Run flashing program.
1379 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1380 retval = core_move_value_at_r2_disp(target,0x00,HFM_CNFG); // write to HFM_CNFG (lock=0, select bank)
1381 err_check_propagate(retval);
1382 retval = core_move_value_at_r2_disp(target,0x04,HFM_USTAT);// write to HMF_USTAT, clear PVIOL, ACCERR & BLANK bits
1383 err_check_propagate(retval);
1384 retval = core_move_value_at_r2_disp(target,0x10,HFM_USTAT);// clear only one bit at a time
1385 err_check_propagate(retval);
1386 retval = core_move_value_at_r2_disp(target,0x20,HFM_USTAT);
1387 err_check_propagate(retval);
1388 retval = core_move_value_at_r2_disp(target,0x00,HFM_PROT);// write to HMF_PROT, clear protection
1389 err_check_propagate(retval);
1390 retval = core_move_value_at_r2_disp(target,0x00,HFM_PROTB);// write to HMF_PROTB, clear protection
1391 err_check_propagate(retval);
1393 //TODO implement handling of odd number of words.
1394 retval = ERROR_FAIL;
1395 err_check(retval,"Cannot handle odd number of words.");
1398 dsp5680xx_context.flush = 1;
1399 retval = dsp5680xx_execute_queue();
1400 err_check_propagate(retval);
1402 uint32_t drscan_data;
1403 uint16_t tmp = (buffer[0]|(buffer[1]<<8));
1404 retval = core_tx_upper_data(target,tmp,&drscan_data);
1405 err_check_propagate(retval);
1407 retval = dsp5680xx_resume(target,0,my_favourite_ram_address,0,0);
1408 err_check_propagate(retval);
1410 int counter = FLUSH_COUNT_FLASH;
1411 dsp5680xx_context.flush = 0;
1413 for(i=1; (i<count/2)&&(i<HFM_SIZE_WORDS); i++){
1415 dsp5680xx_context.flush = 1;
1416 counter = FLUSH_COUNT_FLASH;
1418 tmp = (buffer[2*i]|(buffer[2*i+1]<<8));
1419 retval = core_tx_upper_data(target,tmp,&drscan_data);
1420 if(retval!=ERROR_OK){
1421 dsp5680xx_context.flush = 1;
1422 err_check_propagate(retval);
1424 dsp5680xx_context.flush = 0;
1426 dsp5680xx_context.flush = 1;
1428 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1429 // Verify flash (skip when exec lock sequence)
1430 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1433 retval = dsp5680xx_f_signature(target,address,i,&signature);
1434 err_check_propagate(retval);
1435 pc_crc = perl_crc(buffer,i);
1436 if(pc_crc != signature){
1437 retval = ERROR_FAIL;
1438 err_check(retval,"Flashed data failed CRC check, flash again!");
1444 // Reset state machine
1445 int reset_jtag(void){
1447 tap_state_t states[2];
1448 const char *cp = "RESET";
1449 states[0] = tap_state_by_name(cp);
1450 retval = jtag_add_statemove(states[0]);
1451 err_check_propagate(retval);
1452 retval = jtag_execute_queue();
1453 err_check_propagate(retval);
1454 jtag_add_pathmove(0, states + 1);
1455 retval = jtag_execute_queue();
1459 int dsp5680xx_f_unlock(struct target * target){
1461 if(target->tap->enabled){
1462 //TODO find a way to switch to the master tap here.
1463 LOG_ERROR("Master tap must be enabled to unlock flash.");
1464 return ERROR_TARGET_FAILURE;
1466 uint32_t data_to_shift_in = MASTER_TAP_CMD_FLASH_ERASE;
1467 uint32_t data_shifted_out;
1468 retval = dsp5680xx_irscan(target,&data_to_shift_in,&data_shifted_out,8);
1469 err_check_propagate(retval);
1470 data_to_shift_in = HFM_CLK_DEFAULT;
1471 retval = dsp5680xx_drscan(target,((uint8_t *) & data_to_shift_in),((uint8_t *)&data_shifted_out),8);
1472 err_check_propagate(retval);
1476 int dsp5680xx_f_lock(struct target * target){
1478 uint16_t lock_word[] = {HFM_LOCK_FLASH,HFM_LOCK_FLASH};
1479 retval = dsp5680xx_f_wr(target,(uint8_t *)(lock_word),HFM_LOCK_ADDR_L,4,1);
1480 err_check_propagate(retval);
1484 static int dsp5680xx_step(struct target * target,int current, uint32_t address, int handle_breakpoints){
1485 err_check(ERROR_FAIL,"Not implemented yet.");
1488 /** Holds methods for dsp5680xx targets. */
1489 struct target_type dsp5680xx_target = {
1490 .name = "dsp5680xx",
1492 .poll = dsp5680xx_poll,
1493 .arch_state = dsp5680xx_arch_state,
1495 .target_request_data = NULL,
1497 .halt = dsp5680xx_halt,
1498 .resume = dsp5680xx_resume,
1499 .step = dsp5680xx_step,
1501 .write_buffer = dsp5680xx_write_buffer,
1502 .read_buffer = dsp5680xx_read_buffer,
1504 .assert_reset = dsp5680xx_assert_reset,
1505 .deassert_reset = dsp5680xx_deassert_reset,
1506 .soft_reset_halt = dsp5680xx_soft_reset_halt,
1508 .read_memory = dsp5680xx_read,
1509 .write_memory = dsp5680xx_write,
1510 .bulk_write_memory = dsp5680xx_bulk_write_memory,
1512 .checksum_memory = dsp5680xx_checksum_memory,
1514 .target_create = dsp5680xx_target_create,
1515 .init_target = dsp5680xx_init_target,