target/adiv5: Large Physical Address Extension
[openocd.git] / src / target / dsp563xx_once.c
blob624474d1b657de2748e197269e39f2d9ab063271
1 /***************************************************************************
2 * Copyright (C) 2009 by Mathias Kuester *
3 * mkdorg@users.sourceforge.net *
4 * *
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. *
9 * *
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. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
23 #include <jim.h>
25 #include "target.h"
26 #include "target_type.h"
27 #include "register.h"
28 #include "dsp563xx.h"
29 #include "dsp563xx_once.h"
31 #define JTAG_STATUS_STATIC_MASK 0x03
32 #define JTAG_STATUS_STATIC_VALUE 0x01
34 #define JTAG_STATUS_NORMAL 0x01
35 #define JTAG_STATUS_STOPWAIT 0x05
36 #define JTAG_STATUS_BUSY 0x09
37 #define JTAG_STATUS_DEBUG 0x0d
39 #define JTAG_INSTR_EXTEST 0x00
40 #define JTAG_INSTR_SAMPLE_PRELOAD 0x01
41 #define JTAG_INSTR_IDCODE 0x02
42 #define JTAG_INSTR_HIZ 0x04
43 #define JTAG_INSTR_CLAMP 0x05
44 #define JTAG_INSTR_ENABLE_ONCE 0x06
45 #define JTAG_INSTR_DEBUG_REQUEST 0x07
46 #define JTAG_INSTR_BYPASS 0x0F
48 /** */
49 static inline int dsp563xx_write_dr(struct jtag_tap *tap, uint8_t *dr_in, uint8_t *dr_out, int dr_len, int rti)
51 jtag_add_plain_dr_scan(dr_len, dr_out, dr_in, TAP_IDLE);
53 return ERROR_OK;
56 /** */
57 static inline int dsp563xx_write_dr_u8(struct jtag_tap *tap, uint8_t *dr_in, uint8_t dr_out, int dr_len, int rti)
59 return dsp563xx_write_dr(tap, dr_in, &dr_out, dr_len, rti);
62 /** */
63 static inline int dsp563xx_write_dr_u32(struct jtag_tap *tap, uint32_t *dr_in, uint32_t dr_out, int dr_len, int rti)
65 return dsp563xx_write_dr(tap, (uint8_t *) dr_in, (uint8_t *) &dr_out, dr_len, rti);
68 /** single word instruction */
69 static inline int dsp563xx_once_ir_exec(struct jtag_tap *tap, int flush, uint8_t instr,
70 uint8_t rw, uint8_t go, uint8_t ex)
72 int err;
74 err = dsp563xx_write_dr_u8(tap, 0, instr | (ex << 5) | (go << 6) | (rw << 7), 8, 0);
75 if (err != ERROR_OK)
76 return err;
77 if (flush)
78 err = jtag_execute_queue();
79 return err;
82 /* IR and DR functions */
83 static inline int dsp563xx_write_ir(struct jtag_tap *tap, uint8_t *ir_in, uint8_t *ir_out, int ir_len, int rti)
85 jtag_add_plain_ir_scan(tap->ir_length, ir_out, ir_in, TAP_IDLE);
87 return ERROR_OK;
90 static inline int dsp563xx_write_ir_u8(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out, int ir_len, int rti)
92 return dsp563xx_write_ir(tap, ir_in, &ir_out, ir_len, rti);
95 static inline int dsp563xx_jtag_sendinstr(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out)
97 return dsp563xx_write_ir_u8(tap, ir_in, ir_out, tap->ir_length, 1);
100 /** */
101 int dsp563xx_once_target_status(struct jtag_tap *tap)
103 int err;
104 uint8_t jtag_status;
106 err = dsp563xx_jtag_sendinstr(tap, &jtag_status, JTAG_INSTR_ENABLE_ONCE);
107 if (err != ERROR_OK)
108 return TARGET_UNKNOWN;
109 err = jtag_execute_queue();
110 if (err != ERROR_OK)
111 return TARGET_UNKNOWN;
113 /* verify correct static status pattern */
114 if ((jtag_status & JTAG_STATUS_STATIC_MASK) != JTAG_STATUS_STATIC_VALUE)
115 return TARGET_UNKNOWN;
117 if (jtag_status != JTAG_STATUS_DEBUG)
118 return TARGET_RUNNING;
120 return TARGET_HALTED;
123 /** */
124 int dsp563xx_once_request_debug(struct jtag_tap *tap, int reset_state)
126 int err;
127 uint8_t ir_in = 0, pattern = 0;
128 uint32_t retry = 0;
130 /* in reset state we only get a ACK
131 * from the interface */
132 if (reset_state)
133 pattern = 1;
134 else
135 pattern = JTAG_STATUS_DEBUG;
137 /* wait until we get the ack */
138 while (ir_in != pattern) {
139 err = dsp563xx_jtag_sendinstr(tap, &ir_in, JTAG_INSTR_DEBUG_REQUEST);
140 if (err != ERROR_OK)
141 return err;
142 err = jtag_execute_queue();
143 if (err != ERROR_OK)
144 return err;
146 LOG_DEBUG("debug request: %02X", ir_in);
148 if (retry++ == 100)
149 return ERROR_TARGET_FAILURE;
152 /* we cant enable the once in reset state */
153 if (pattern == 1)
154 return ERROR_OK;
156 /* try to enable once */
157 retry = 0;
158 ir_in = 0;
159 while (ir_in != pattern) {
160 err = dsp563xx_jtag_sendinstr(tap, &ir_in, JTAG_INSTR_ENABLE_ONCE);
161 if (err != ERROR_OK)
162 return err;
163 err = jtag_execute_queue();
164 if (err != ERROR_OK)
165 return err;
167 LOG_DEBUG("enable once: %02X", ir_in);
169 if (retry++ == 100) {
170 LOG_DEBUG("error");
171 return ERROR_TARGET_FAILURE;
175 if (ir_in != JTAG_STATUS_DEBUG)
176 return ERROR_TARGET_FAILURE;
178 return ERROR_OK;
181 /** once read registers */
182 int dsp563xx_once_read_register(struct jtag_tap *tap, int flush, struct once_reg *regs, int len)
184 int i;
185 int err = ERROR_OK;
187 for (i = 0; i < len; i++) {
188 err = dsp563xx_once_reg_read_ex(tap, flush, regs[i].addr, regs[i].len, &regs[i].reg);
189 if (err != ERROR_OK)
190 return err;
193 if (flush)
194 err = jtag_execute_queue();
195 return err;
198 /** once read register with register len */
199 int dsp563xx_once_reg_read_ex(struct jtag_tap *tap, int flush, uint8_t reg, uint8_t len, uint32_t *data)
201 int err;
203 err = dsp563xx_once_ir_exec(tap, 1, reg, 1, 0, 0);
204 if (err != ERROR_OK)
205 return err;
206 err = dsp563xx_write_dr_u32(tap, data, 0x00, len, 0);
207 if (err != ERROR_OK)
208 return err;
209 if (flush)
210 err = jtag_execute_queue();
212 return err;
215 /** once read register */
216 int dsp563xx_once_reg_read(struct jtag_tap *tap, int flush, uint8_t reg, uint32_t *data)
218 int err;
220 err = dsp563xx_once_ir_exec(tap, flush, reg, 1, 0, 0);
221 if (err != ERROR_OK)
222 return err;
223 err = dsp563xx_write_dr_u32(tap, data, 0x00, 24, 0);
224 if (err != ERROR_OK)
225 return err;
226 if (flush)
227 err = jtag_execute_queue();
229 return err;
232 /** once write register */
233 int dsp563xx_once_reg_write(struct jtag_tap *tap, int flush, uint8_t reg, uint32_t data)
235 int err;
237 err = dsp563xx_once_ir_exec(tap, flush, reg, 0, 0, 0);
238 if (err != ERROR_OK)
239 return err;
240 err = dsp563xx_write_dr_u32(tap, 0x00, data, 24, 0);
241 if (err != ERROR_OK)
242 return err;
243 if (flush)
244 err = jtag_execute_queue();
245 return err;
248 /** single word instruction */
249 int dsp563xx_once_execute_sw_ir(struct jtag_tap *tap, int flush, uint32_t opcode)
251 int err;
253 err = dsp563xx_once_ir_exec(tap, flush, DSP563XX_ONCE_OPDBR, 0, 1, 0);
254 if (err != ERROR_OK)
255 return err;
256 err = dsp563xx_write_dr_u32(tap, 0, opcode, 24, 0);
257 if (err != ERROR_OK)
258 return err;
259 if (flush)
260 err = jtag_execute_queue();
261 return err;
264 /** double word instruction */
265 int dsp563xx_once_execute_dw_ir(struct jtag_tap *tap, int flush, uint32_t opcode, uint32_t operand)
267 int err;
269 err = dsp563xx_once_ir_exec(tap, flush, DSP563XX_ONCE_OPDBR, 0, 0, 0);
270 if (err != ERROR_OK)
271 return err;
272 err = dsp563xx_write_dr_u32(tap, 0, opcode, 24, 0);
273 if (err != ERROR_OK)
274 return err;
275 if (flush) {
276 err = jtag_execute_queue();
277 if (err != ERROR_OK)
278 return err;
281 err = dsp563xx_once_ir_exec(tap, flush, DSP563XX_ONCE_OPDBR, 0, 1, 0);
282 if (err != ERROR_OK)
283 return err;
284 err = dsp563xx_write_dr_u32(tap, 0, operand, 24, 0);
285 if (err != ERROR_OK)
286 return err;
287 if (flush) {
288 err = jtag_execute_queue();
289 if (err != ERROR_OK)
290 return err;
293 return ERROR_OK;