1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2009 by Mathias Kuester *
5 * mkdorg@users.sourceforge.net *
6 ***************************************************************************/
15 #include "target_type.h"
18 #include "dsp563xx_once.h"
20 #define JTAG_STATUS_STATIC_MASK 0x03
21 #define JTAG_STATUS_STATIC_VALUE 0x01
23 #define JTAG_STATUS_NORMAL 0x01
24 #define JTAG_STATUS_STOPWAIT 0x05
25 #define JTAG_STATUS_BUSY 0x09
26 #define JTAG_STATUS_DEBUG 0x0d
28 #define JTAG_INSTR_EXTEST 0x00
29 #define JTAG_INSTR_SAMPLE_PRELOAD 0x01
30 #define JTAG_INSTR_IDCODE 0x02
31 #define JTAG_INSTR_HIZ 0x04
32 #define JTAG_INSTR_CLAMP 0x05
33 #define JTAG_INSTR_ENABLE_ONCE 0x06
34 #define JTAG_INSTR_DEBUG_REQUEST 0x07
35 #define JTAG_INSTR_BYPASS 0x0F
38 static inline int dsp563xx_write_dr(struct jtag_tap
*tap
, uint8_t *dr_in
, uint8_t *dr_out
, int dr_len
, int rti
)
40 jtag_add_plain_dr_scan(dr_len
, dr_out
, dr_in
, TAP_IDLE
);
46 static inline int dsp563xx_write_dr_u8(struct jtag_tap
*tap
, uint8_t *dr_in
, uint8_t dr_out
, int dr_len
, int rti
)
48 return dsp563xx_write_dr(tap
, dr_in
, &dr_out
, dr_len
, rti
);
52 static inline int dsp563xx_write_dr_u32(struct jtag_tap
*tap
, uint32_t *dr_in
, uint32_t dr_out
, int dr_len
, int rti
)
54 return dsp563xx_write_dr(tap
, (uint8_t *) dr_in
, (uint8_t *) &dr_out
, dr_len
, rti
);
57 /** single word instruction */
58 static inline int dsp563xx_once_ir_exec(struct jtag_tap
*tap
, int flush
, uint8_t instr
,
59 uint8_t rw
, uint8_t go
, uint8_t ex
)
63 err
= dsp563xx_write_dr_u8(tap
, NULL
, instr
| (ex
<< 5) | (go
<< 6) | (rw
<< 7), 8, 0);
67 err
= jtag_execute_queue();
71 /* IR and DR functions */
72 static inline int dsp563xx_write_ir(struct jtag_tap
*tap
, uint8_t *ir_in
, uint8_t *ir_out
, int ir_len
, int rti
)
74 jtag_add_plain_ir_scan(tap
->ir_length
, ir_out
, ir_in
, TAP_IDLE
);
79 static inline int dsp563xx_write_ir_u8(struct jtag_tap
*tap
, uint8_t *ir_in
, uint8_t ir_out
, int ir_len
, int rti
)
81 return dsp563xx_write_ir(tap
, ir_in
, &ir_out
, ir_len
, rti
);
84 static inline int dsp563xx_jtag_sendinstr(struct jtag_tap
*tap
, uint8_t *ir_in
, uint8_t ir_out
)
86 return dsp563xx_write_ir_u8(tap
, ir_in
, ir_out
, tap
->ir_length
, 1);
90 int dsp563xx_once_target_status(struct jtag_tap
*tap
)
95 err
= dsp563xx_jtag_sendinstr(tap
, &jtag_status
, JTAG_INSTR_ENABLE_ONCE
);
97 return TARGET_UNKNOWN
;
98 err
= jtag_execute_queue();
100 return TARGET_UNKNOWN
;
102 /* verify correct static status pattern */
103 if ((jtag_status
& JTAG_STATUS_STATIC_MASK
) != JTAG_STATUS_STATIC_VALUE
)
104 return TARGET_UNKNOWN
;
106 if (jtag_status
!= JTAG_STATUS_DEBUG
)
107 return TARGET_RUNNING
;
109 return TARGET_HALTED
;
113 int dsp563xx_once_request_debug(struct jtag_tap
*tap
, int reset_state
)
116 uint8_t ir_in
= 0, pattern
= 0;
119 /* in reset state we only get a ACK
120 * from the interface */
124 pattern
= JTAG_STATUS_DEBUG
;
126 /* wait until we get the ack */
127 while (ir_in
!= pattern
) {
128 err
= dsp563xx_jtag_sendinstr(tap
, &ir_in
, JTAG_INSTR_DEBUG_REQUEST
);
131 err
= jtag_execute_queue();
135 LOG_DEBUG("debug request: %02X", ir_in
);
138 return ERROR_TARGET_FAILURE
;
141 /* we cant enable the once in reset state */
145 /* try to enable once */
148 while (ir_in
!= pattern
) {
149 err
= dsp563xx_jtag_sendinstr(tap
, &ir_in
, JTAG_INSTR_ENABLE_ONCE
);
152 err
= jtag_execute_queue();
156 LOG_DEBUG("enable once: %02X", ir_in
);
158 if (retry
++ == 100) {
160 return ERROR_TARGET_FAILURE
;
164 if (ir_in
!= JTAG_STATUS_DEBUG
)
165 return ERROR_TARGET_FAILURE
;
170 /** once read registers */
171 int dsp563xx_once_read_register(struct jtag_tap
*tap
, int flush
, struct once_reg
*regs
, int len
)
176 for (i
= 0; i
< len
; i
++) {
177 err
= dsp563xx_once_reg_read_ex(tap
, flush
, regs
[i
].addr
, regs
[i
].len
, ®s
[i
].reg
);
183 err
= jtag_execute_queue();
187 /** once read register with register len */
188 int dsp563xx_once_reg_read_ex(struct jtag_tap
*tap
, int flush
, uint8_t reg
, uint8_t len
, uint32_t *data
)
192 err
= dsp563xx_once_ir_exec(tap
, 1, reg
, 1, 0, 0);
195 err
= dsp563xx_write_dr_u32(tap
, data
, 0x00, len
, 0);
199 err
= jtag_execute_queue();
204 /** once read register */
205 int dsp563xx_once_reg_read(struct jtag_tap
*tap
, int flush
, uint8_t reg
, uint32_t *data
)
209 err
= dsp563xx_once_ir_exec(tap
, flush
, reg
, 1, 0, 0);
212 err
= dsp563xx_write_dr_u32(tap
, data
, 0x00, 24, 0);
216 err
= jtag_execute_queue();
221 /** once write register */
222 int dsp563xx_once_reg_write(struct jtag_tap
*tap
, int flush
, uint8_t reg
, uint32_t data
)
226 err
= dsp563xx_once_ir_exec(tap
, flush
, reg
, 0, 0, 0);
229 err
= dsp563xx_write_dr_u32(tap
, NULL
, data
, 24, 0);
233 err
= jtag_execute_queue();
237 /** single word instruction */
238 int dsp563xx_once_execute_sw_ir(struct jtag_tap
*tap
, int flush
, uint32_t opcode
)
242 err
= dsp563xx_once_ir_exec(tap
, flush
, DSP563XX_ONCE_OPDBR
, 0, 1, 0);
245 err
= dsp563xx_write_dr_u32(tap
, NULL
, opcode
, 24, 0);
249 err
= jtag_execute_queue();
253 /** double word instruction */
254 int dsp563xx_once_execute_dw_ir(struct jtag_tap
*tap
, int flush
, uint32_t opcode
, uint32_t operand
)
258 err
= dsp563xx_once_ir_exec(tap
, flush
, DSP563XX_ONCE_OPDBR
, 0, 0, 0);
261 err
= dsp563xx_write_dr_u32(tap
, NULL
, opcode
, 24, 0);
265 err
= jtag_execute_queue();
270 err
= dsp563xx_once_ir_exec(tap
, flush
, DSP563XX_ONCE_OPDBR
, 0, 1, 0);
273 err
= dsp563xx_write_dr_u32(tap
, NULL
, operand
, 24, 0);
277 err
= jtag_execute_queue();