mips_ejtag: remove memory protection bit before DM
[openocd.git] / src / target / mips_ejtag.c
blob85748146d0d7725aa6083981f066c7243de4ea06
1 /***************************************************************************
2 * Copyright (C) 2008 by Spencer Oliver *
3 * spen@spen-soft.co.uk *
4 * *
5 * Copyright (C) 2008 by David T.L. Wong *
6 * *
7 * Copyright (C) 2009 by David N. Claffey <dnclaffey@gmail.com> *
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, write to the *
21 * Free Software Foundation, Inc., *
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
23 ***************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include "mips32.h"
30 #include "mips_ejtag.h"
31 #include "mips32_dmaacc.h"
33 void mips_ejtag_set_instr(struct mips_ejtag *ejtag_info, int new_instr)
35 struct jtag_tap *tap;
37 tap = ejtag_info->tap;
38 assert(tap != NULL);
40 if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != (uint32_t)new_instr) {
41 struct scan_field field;
42 uint8_t t[4];
44 field.num_bits = tap->ir_length;
45 field.out_value = t;
46 buf_set_u32(t, 0, field.num_bits, new_instr);
47 field.in_value = NULL;
49 jtag_add_ir_scan(tap, &field, TAP_IDLE);
53 int mips_ejtag_get_idcode(struct mips_ejtag *ejtag_info, uint32_t *idcode)
55 struct scan_field field;
56 uint8_t r[4];
58 mips_ejtag_set_instr(ejtag_info, EJTAG_INST_IDCODE);
60 field.num_bits = 32;
61 field.out_value = NULL;
62 field.in_value = r;
64 jtag_add_dr_scan(ejtag_info->tap, 1, &field, TAP_IDLE);
66 int retval;
67 retval = jtag_execute_queue();
68 if (retval != ERROR_OK) {
69 LOG_ERROR("register read failed");
70 return retval;
73 *idcode = buf_get_u32(field.in_value, 0, 32);
75 return ERROR_OK;
78 static int mips_ejtag_get_impcode(struct mips_ejtag *ejtag_info, uint32_t *impcode)
80 struct scan_field field;
81 uint8_t r[4];
83 mips_ejtag_set_instr(ejtag_info, EJTAG_INST_IMPCODE);
85 field.num_bits = 32;
86 field.out_value = NULL;
87 field.in_value = r;
89 jtag_add_dr_scan(ejtag_info->tap, 1, &field, TAP_IDLE);
91 int retval;
92 retval = jtag_execute_queue();
93 if (retval != ERROR_OK) {
94 LOG_ERROR("register read failed");
95 return retval;
98 *impcode = buf_get_u32(field.in_value, 0, 32);
100 return ERROR_OK;
103 void mips_ejtag_add_scan_96(struct mips_ejtag *ejtag_info, uint32_t ctrl, uint32_t data, uint8_t *in_scan_buf)
105 assert(ejtag_info->tap != NULL);
106 struct jtag_tap *tap = ejtag_info->tap;
108 struct scan_field field;
109 uint8_t out_scan[12];
111 /* processor access "all" register 96 bit */
112 field.num_bits = 96;
114 field.out_value = out_scan;
115 buf_set_u32(out_scan, 0, 32, ctrl);
116 buf_set_u32(out_scan + 4, 0, 32, data);
117 buf_set_u32(out_scan + 8, 0, 32, 0);
119 field.in_value = in_scan_buf;
121 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
123 keep_alive();
126 int mips_ejtag_drscan_32(struct mips_ejtag *ejtag_info, uint32_t *data)
128 struct jtag_tap *tap;
129 tap = ejtag_info->tap;
130 assert(tap != NULL);
132 struct scan_field field;
133 uint8_t t[4], r[4];
134 int retval;
136 field.num_bits = 32;
137 field.out_value = t;
138 buf_set_u32(t, 0, field.num_bits, *data);
139 field.in_value = r;
141 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
143 retval = jtag_execute_queue();
144 if (retval != ERROR_OK) {
145 LOG_ERROR("register read failed");
146 return retval;
149 *data = buf_get_u32(field.in_value, 0, 32);
151 keep_alive();
153 return ERROR_OK;
156 void mips_ejtag_drscan_32_out(struct mips_ejtag *ejtag_info, uint32_t data)
158 uint8_t t[4];
159 struct jtag_tap *tap;
160 tap = ejtag_info->tap;
161 assert(tap != NULL);
163 struct scan_field field;
165 field.num_bits = 32;
166 field.out_value = t;
167 buf_set_u32(t, 0, field.num_bits, data);
169 field.in_value = NULL;
171 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
174 int mips_ejtag_drscan_8(struct mips_ejtag *ejtag_info, uint32_t *data)
176 struct jtag_tap *tap;
177 tap = ejtag_info->tap;
178 assert(tap != NULL);
180 struct scan_field field;
181 uint8_t t[4] = {0, 0, 0, 0}, r[4];
182 int retval;
184 field.num_bits = 8;
185 field.out_value = t;
186 buf_set_u32(t, 0, field.num_bits, *data);
187 field.in_value = r;
189 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
191 retval = jtag_execute_queue();
192 if (retval != ERROR_OK) {
193 LOG_ERROR("register read failed");
194 return retval;
197 *data = buf_get_u32(field.in_value, 0, 32);
199 return ERROR_OK;
202 void mips_ejtag_drscan_8_out(struct mips_ejtag *ejtag_info, uint8_t data)
204 struct jtag_tap *tap;
205 tap = ejtag_info->tap;
206 assert(tap != NULL);
208 struct scan_field field;
210 field.num_bits = 8;
211 field.out_value = &data;
212 field.in_value = NULL;
214 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
217 /* Set (to enable) or clear (to disable stepping) the SSt bit (bit 8) in Cp0 Debug reg (reg 23, sel 0) */
218 int mips_ejtag_config_step(struct mips_ejtag *ejtag_info, int enable_step)
220 struct pracc_queue_info ctx = {.max_code = 7};
221 pracc_queue_init(&ctx);
222 if (ctx.retval != ERROR_OK)
223 goto exit;
225 pracc_add(&ctx, 0, MIPS32_MFC0(8, 23, 0)); /* move COP0 Debug to $8 */
226 pracc_add(&ctx, 0, MIPS32_ORI(8, 8, 0x0100)); /* set SSt bit in debug reg */
227 if (!enable_step)
228 pracc_add(&ctx, 0, MIPS32_XORI(8, 8, 0x0100)); /* clear SSt bit in debug reg */
230 pracc_add(&ctx, 0, MIPS32_MTC0(8, 23, 0)); /* move $8 to COP0 Debug */
231 pracc_add(&ctx, 0, MIPS32_LUI(8, UPPER16(ejtag_info->reg8))); /* restore upper 16 bits of $8 */
232 pracc_add(&ctx, 0, MIPS32_B(NEG16((ctx.code_count + 1)))); /* jump to start */
233 pracc_add(&ctx, 0, MIPS32_ORI(8, 8, LOWER16(ejtag_info->reg8))); /* restore lower 16 bits of $8 */
235 ctx.retval = mips32_pracc_queue_exec(ejtag_info, &ctx, NULL);
236 exit:
237 pracc_queue_free(&ctx);
238 return ctx.retval;
242 * Disable memory protection for 0xFF20.0000–0xFF3F.FFFF
243 * It is needed by EJTAG 1.5-2.0, especially for BMIPS CPUs
244 * For example bcm7401 and others. At leas on some
245 * CPUs, DebugMode wont start if this bit is not removed.
247 static int disable_dcr_mp(struct mips_ejtag *ejtag_info)
249 uint32_t dcr;
250 int retval;
252 retval = mips32_dmaacc_read_mem(ejtag_info, EJTAG_DCR, 4, 1, &dcr);
253 if (retval != ERROR_OK)
254 goto error;
256 dcr &= ~EJTAG_DCR_MP;
257 retval = mips32_dmaacc_write_mem(ejtag_info, EJTAG_DCR, 4, 1, &dcr);
258 if (retval != ERROR_OK)
259 goto error;
260 return ERROR_OK;
261 error:
262 LOG_ERROR("Failed to remove DCR MPbit!");
263 return retval;
266 int mips_ejtag_enter_debug(struct mips_ejtag *ejtag_info)
268 uint32_t ejtag_ctrl;
269 mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL);
271 if (ejtag_info->ejtag_version == EJTAG_VERSION_20) {
272 if (disable_dcr_mp(ejtag_info) != ERROR_OK)
273 goto error;
276 /* set debug break bit */
277 ejtag_ctrl = ejtag_info->ejtag_ctrl | EJTAG_CTRL_JTAGBRK;
278 mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
280 /* break bit will be cleared by hardware */
281 ejtag_ctrl = ejtag_info->ejtag_ctrl;
282 mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
283 LOG_DEBUG("ejtag_ctrl: 0x%8.8" PRIx32 "", ejtag_ctrl);
284 if ((ejtag_ctrl & EJTAG_CTRL_BRKST) == 0)
285 goto error;
287 return ERROR_OK;
288 error:
289 LOG_ERROR("Failed to enter Debug Mode!");
290 return ERROR_FAIL;
293 int mips_ejtag_exit_debug(struct mips_ejtag *ejtag_info)
295 uint32_t instr = MIPS32_DRET;
296 struct pracc_queue_info ctx = {.max_code = 1, .pracc_list = &instr, .code_count = 1, .store_count = 0};
298 /* execute our dret instruction */
299 ctx.retval = mips32_pracc_queue_exec(ejtag_info, &ctx, NULL);
301 /* pic32mx workaround, false pending at low core clock */
302 jtag_add_sleep(1000);
303 return ctx.retval;
306 int mips_ejtag_init(struct mips_ejtag *ejtag_info)
308 int retval;
310 retval = mips_ejtag_get_impcode(ejtag_info, &ejtag_info->impcode);
311 if (retval != ERROR_OK)
312 return retval;
313 LOG_DEBUG("impcode: 0x%8.8" PRIx32 "", ejtag_info->impcode);
315 /* get ejtag version */
316 ejtag_info->ejtag_version = ((ejtag_info->impcode >> 29) & 0x07);
318 switch (ejtag_info->ejtag_version) {
319 case EJTAG_VERSION_20:
320 LOG_DEBUG("EJTAG: Version 1 or 2.0 Detected");
321 break;
322 case EJTAG_VERSION_25:
323 LOG_DEBUG("EJTAG: Version 2.5 Detected");
324 break;
325 case EJTAG_VERSION_26:
326 LOG_DEBUG("EJTAG: Version 2.6 Detected");
327 break;
328 case EJTAG_VERSION_31:
329 LOG_DEBUG("EJTAG: Version 3.1 Detected");
330 break;
331 case EJTAG_VERSION_41:
332 LOG_DEBUG("EJTAG: Version 4.1 Detected");
333 break;
334 case EJTAG_VERSION_51:
335 LOG_DEBUG("EJTAG: Version 5.1 Detected");
336 break;
337 default:
338 LOG_DEBUG("EJTAG: Unknown Version Detected");
339 break;
341 LOG_DEBUG("EJTAG: features:%s%s%s%s%s%s%s",
342 ejtag_info->impcode & EJTAG_IMP_R3K ? " R3k" : " R4k",
343 ejtag_info->impcode & EJTAG_IMP_DINT ? " DINT" : "",
344 ejtag_info->impcode & (1 << 22) ? " ASID_8" : "",
345 ejtag_info->impcode & (1 << 21) ? " ASID_6" : "",
346 ejtag_info->impcode & EJTAG_IMP_MIPS16 ? " MIPS16" : "",
347 ejtag_info->impcode & EJTAG_IMP_NODMA ? " noDMA" : " DMA",
348 ejtag_info->impcode & EJTAG_DCR_MIPS64 ? " MIPS64" : " MIPS32");
350 if ((ejtag_info->impcode & EJTAG_IMP_NODMA) == 0)
351 LOG_DEBUG("EJTAG: DMA Access Mode Support Enabled");
353 /* set initial state for ejtag control reg */
354 ejtag_info->ejtag_ctrl = EJTAG_CTRL_ROCC | EJTAG_CTRL_PRACC | EJTAG_CTRL_PROBEN | EJTAG_CTRL_SETDEV;
355 ejtag_info->fast_access_save = -1;
357 return ERROR_OK;
360 int mips_ejtag_fastdata_scan(struct mips_ejtag *ejtag_info, int write_t, uint32_t *data)
362 struct jtag_tap *tap;
364 tap = ejtag_info->tap;
365 assert(tap != NULL);
367 struct scan_field fields[2];
368 uint8_t spracc = 0;
369 uint8_t t[4] = {0, 0, 0, 0};
371 /* fastdata 1-bit register */
372 fields[0].num_bits = 1;
373 fields[0].out_value = &spracc;
374 fields[0].in_value = NULL;
376 /* processor access data register 32 bit */
377 fields[1].num_bits = 32;
378 fields[1].out_value = t;
380 if (write_t) {
381 fields[1].in_value = NULL;
382 buf_set_u32(t, 0, 32, *data);
383 } else
384 fields[1].in_value = (void *) data;
386 jtag_add_dr_scan(tap, 2, fields, TAP_IDLE);
388 if (!write_t && data)
389 jtag_add_callback(mips_le_to_h_u32,
390 (jtag_callback_data_t) data);
392 keep_alive();
394 return ERROR_OK;