1 /***************************************************************************
2 * Copyright (C) 2009 by Mathias Kuester *
3 * mkdorg@users.sourceforge.net *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
27 #include "target_type.h"
30 #include "dsp563xx_once.h"
32 #define JTAG_STATUS_NORMAL 0x01
33 #define JTAG_STATUS_STOPWAIT 0x05
34 #define JTAG_STATUS_BUSY 0x09
35 #define JTAG_STATUS_DEBUG 0x0d
37 #define JTAG_INSTR_EXTEST 0x00
38 #define JTAG_INSTR_SAMPLE_PRELOAD 0x01
39 #define JTAG_INSTR_IDCODE 0x02
40 #define JTAG_INSTR_HIZ 0x04
41 #define JTAG_INSTR_CLAMP 0x05
42 #define JTAG_INSTR_ENABLE_ONCE 0x06
43 #define JTAG_INSTR_DEBUG_REQUEST 0x07
44 #define JTAG_INSTR_BYPASS 0x0F
46 static inline int dsp563xx_write_dr(struct jtag_tap
*tap
, uint8_t * dr_in
, uint8_t * dr_out
, int dr_len
, int rti
)
50 LOG_ERROR("invalid tap");
54 jtag_add_plain_dr_scan(dr_len
, dr_out
, dr_in
, TAP_IDLE
);
59 static inline int dsp563xx_write_dr_u8(struct jtag_tap
*tap
, uint8_t * dr_in
, uint8_t dr_out
, int dr_len
, int rti
)
63 LOG_ERROR("dr_len overflow, maxium is 8");
67 return dsp563xx_write_dr(tap
, dr_in
, &dr_out
, dr_len
, rti
);
70 static inline int dsp563xx_write_dr_u32(struct jtag_tap
*tap
, uint32_t * dr_in
, uint32_t dr_out
, int dr_len
, int rti
)
74 LOG_ERROR("dr_len overflow, maxium is 32");
78 return dsp563xx_write_dr(tap
, (uint8_t *) dr_in
, (uint8_t *) & dr_out
, dr_len
, rti
);
81 /** single word instruction */
82 static inline int dsp563xx_once_ir_exec(struct jtag_tap
*tap
, uint8_t instr
, uint8_t rw
, uint8_t go
, uint8_t ex
)
86 if ((err
= dsp563xx_write_dr_u8(tap
, 0, instr
| (ex
<< 5) | (go
<< 6) | (rw
<< 7), 8, 0)) != ERROR_OK
)
89 return jtag_execute_queue();
92 /** single word instruction */
93 static inline int dsp563xx_once_ir_exec_nq(struct jtag_tap
*tap
, uint8_t instr
, uint8_t rw
, uint8_t go
, uint8_t ex
)
95 return dsp563xx_write_dr_u8(tap
, 0, instr
| (ex
<< 5) | (go
<< 6) | (rw
<< 7), 8, 0);
98 /* IR and DR functions */
99 static inline int dsp563xx_write_ir(struct jtag_tap
*tap
, uint8_t * ir_in
, uint8_t * ir_out
, int ir_len
, int rti
)
103 LOG_ERROR("invalid tap");
106 if (ir_len
!= tap
->ir_length
)
108 LOG_ERROR("invalid ir_len");
112 jtag_add_plain_ir_scan(tap
->ir_length
, ir_out
, ir_in
, TAP_IDLE
);
117 static inline int dsp563xx_write_ir_u8(struct jtag_tap
*tap
, uint8_t * ir_in
, uint8_t ir_out
, int ir_len
, int rti
)
121 LOG_ERROR("ir_len overflow, maxium is 8");
125 return dsp563xx_write_ir(tap
, ir_in
, &ir_out
, ir_len
, rti
);
128 static inline int dsp563xx_jtag_sendinstr(struct jtag_tap
*tap
, uint8_t * ir_in
, uint8_t ir_out
)
130 return dsp563xx_write_ir_u8(tap
, ir_in
, ir_out
, tap
->ir_length
, 1);
134 int dsp563xx_once_target_status(struct jtag_tap
*tap
)
139 if ((err
= dsp563xx_jtag_sendinstr(tap
, &jtag_status
, JTAG_INSTR_ENABLE_ONCE
)) != ERROR_OK
)
141 if ((err
= jtag_execute_queue()) != ERROR_OK
)
144 if ((jtag_status
& 1) != 1)
146 return TARGET_UNKNOWN
;
149 if (jtag_status
!= JTAG_STATUS_DEBUG
)
151 return TARGET_RUNNING
;
154 return TARGET_HALTED
;
158 int dsp563xx_once_request_debug(struct jtag_tap
*tap
, int reset_state
)
161 uint8_t ir_in
= 0, pattern
= 0;
164 /* in reset state we only get a ACK
165 * from the interface */
172 pattern
= JTAG_STATUS_DEBUG
;
175 /* wait until we get the ack */
176 while (ir_in
!= pattern
)
178 if ((err
= dsp563xx_jtag_sendinstr(tap
, &ir_in
, JTAG_INSTR_DEBUG_REQUEST
)) != ERROR_OK
)
180 if ((err
= jtag_execute_queue()) != ERROR_OK
)
183 LOG_DEBUG("debug request: %02X", ir_in
);
187 return ERROR_TARGET_FAILURE
;
191 /* we cant enable the once in reset state */
197 /* try to enable once */
200 while (ir_in
!= pattern
)
202 if ((err
= dsp563xx_jtag_sendinstr(tap
, &ir_in
, JTAG_INSTR_ENABLE_ONCE
)) != ERROR_OK
)
204 if ((err
= jtag_execute_queue()) != ERROR_OK
)
207 LOG_DEBUG("enable once: %02X", ir_in
);
212 return ERROR_TARGET_FAILURE
;
216 if (ir_in
!= JTAG_STATUS_DEBUG
)
218 return ERROR_TARGET_FAILURE
;
224 /** once read registers */
225 int dsp563xx_once_read_register(struct jtag_tap
*tap
, struct once_reg
*regs
, int len
)
230 for (i
= 0; i
< len
; i
++)
232 if ((err
= dsp563xx_once_reg_read_ex_nq(tap
, regs
[i
].addr
, regs
[i
].len
, ®s
[i
].reg
)) != ERROR_OK
)
236 return jtag_execute_queue();
240 printf("%08X\n",regs[i].reg);
245 /** once read register */
246 int dsp563xx_once_reg_read_ex_nq(struct jtag_tap
*tap
, uint8_t reg
, uint8_t len
, uint32_t * data
)
250 if ((err
= dsp563xx_once_ir_exec(tap
, reg
, 1, 0, 0)) != ERROR_OK
)
252 return dsp563xx_write_dr_u32(tap
, data
, 0x00, len
, 0);
255 /** once read register */
256 int dsp563xx_once_reg_read_ex(struct jtag_tap
*tap
, uint8_t reg
, uint8_t len
, uint32_t * data
)
260 if ((err
= dsp563xx_once_ir_exec(tap
, reg
, 1, 0, 0)) != ERROR_OK
)
262 if ((err
= dsp563xx_write_dr_u32(tap
, data
, 0x00, len
, 0)) != ERROR_OK
)
264 return jtag_execute_queue();
267 /** once read register */
268 int dsp563xx_once_reg_read(struct jtag_tap
*tap
, uint8_t reg
, uint32_t * data
)
272 if ((err
= dsp563xx_once_ir_exec(tap
, reg
, 1, 0, 0)) != ERROR_OK
)
274 if ((err
= dsp563xx_write_dr_u32(tap
, data
, 0x00, 24, 0)) != ERROR_OK
)
276 return jtag_execute_queue();
279 /** once write register */
280 int dsp563xx_once_reg_write(struct jtag_tap
*tap
, uint8_t reg
, uint32_t data
)
284 if ((err
= dsp563xx_once_ir_exec(tap
, reg
, 0, 0, 0)) != ERROR_OK
)
286 if ((err
= dsp563xx_write_dr_u32(tap
, 0x00, data
, 24, 0)) != ERROR_OK
)
288 return jtag_execute_queue();
291 /** single word instruction */
292 int dsp563xx_once_execute_sw_ir(struct jtag_tap
*tap
, uint32_t opcode
)
296 if ((err
= dsp563xx_once_ir_exec(tap
, DSP563XX_ONCE_OPDBR
, 0, 1, 0)) != ERROR_OK
)
298 if ((err
= dsp563xx_write_dr_u32(tap
, 0, opcode
, 24, 0)) != ERROR_OK
)
300 return jtag_execute_queue();
303 /** double word instruction */
304 int dsp563xx_once_execute_dw_ir(struct jtag_tap
*tap
, uint32_t opcode
, uint32_t operand
)
308 if ((err
= dsp563xx_once_ir_exec(tap
, DSP563XX_ONCE_OPDBR
, 0, 0, 0)) != ERROR_OK
)
310 if ((err
= dsp563xx_write_dr_u32(tap
, 0, opcode
, 24, 0)) != ERROR_OK
)
312 if ((err
= jtag_execute_queue()) != ERROR_OK
)
315 if ((err
= dsp563xx_once_ir_exec(tap
, DSP563XX_ONCE_OPDBR
, 0, 1, 0)) != ERROR_OK
)
317 if ((err
= dsp563xx_write_dr_u32(tap
, 0, operand
, 24, 0)) != ERROR_OK
)
319 if ((err
= jtag_execute_queue()) != ERROR_OK
)
325 /** single word instruction */
326 int dsp563xx_once_execute_sw_ir_nq(struct jtag_tap
*tap
, uint32_t opcode
)
330 if ((err
= dsp563xx_once_ir_exec_nq(tap
, DSP563XX_ONCE_OPDBR
, 0, 1, 0)) != ERROR_OK
)
332 if ((err
= dsp563xx_write_dr_u32(tap
, 0, opcode
, 24, 0)) != ERROR_OK
)
338 /** double word instruction */
339 int dsp563xx_once_execute_dw_ir_nq(struct jtag_tap
*tap
, uint32_t opcode
, uint32_t operand
)
343 if ((err
= dsp563xx_once_ir_exec_nq(tap
, DSP563XX_ONCE_OPDBR
, 0, 0, 0)) != ERROR_OK
)
345 if ((err
= dsp563xx_write_dr_u32(tap
, 0, opcode
, 24, 0)) != ERROR_OK
)
347 if ((err
= dsp563xx_once_ir_exec_nq(tap
, DSP563XX_ONCE_OPDBR
, 0, 1, 0)) != ERROR_OK
)
349 if ((err
= dsp563xx_write_dr_u32(tap
, 0, operand
, 24, 0)) != ERROR_OK
)