mips_m4k.c 26.9 KB
Newer Older
ntfreak's avatar
ntfreak committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/***************************************************************************
 *   Copyright (C) 2008 by Spencer Oliver                                  *
 *   spen@spen-soft.co.uk                                                  *
 *                                                                         *
 *   Copyright (C) 2008 by David T.L. Wong                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

26
#include "breakpoints.h"
ntfreak's avatar
ntfreak committed
27
28
#include "mips32.h"
#include "mips_m4k.h"
29
#include "mips32_dmaacc.h"
30
#include "target_type.h"
31
#include "register.h"
ntfreak's avatar
ntfreak committed
32
33
34
35
36


/* cli handling */

/* forward declarations */
Zachary T Welch's avatar
Zachary T Welch committed
37
38
39
40
41
42
43
int mips_m4k_poll(struct target *target);
int mips_m4k_halt(struct target *target);
int mips_m4k_soft_reset_halt(struct target *target);
int mips_m4k_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution);
int mips_m4k_step(struct target *target, int current, uint32_t address, int handle_breakpoints);
int mips_m4k_read_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
int mips_m4k_write_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
44
45
int mips_m4k_register_commands(struct command_context *cmd_ctx);
int mips_m4k_init_target(struct command_context *cmd_ctx, struct target *target);
Zachary T Welch's avatar
Zachary T Welch committed
46
int mips_m4k_target_create(struct target *target, Jim_Interp *interp);
ntfreak's avatar
ntfreak committed
47

Zachary T Welch's avatar
Zachary T Welch committed
48
49
50
51
int mips_m4k_examine(struct target *target);
int mips_m4k_assert_reset(struct target *target);
int mips_m4k_deassert_reset(struct target *target);
int mips_m4k_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t *checksum);
ntfreak's avatar
ntfreak committed
52

53
struct target_type mips_m4k_target =
ntfreak's avatar
ntfreak committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{
	.name = "mips_m4k",

	.poll = mips_m4k_poll,
	.arch_state = mips32_arch_state,

	.target_request_data = NULL,

	.halt = mips_m4k_halt,
	.resume = mips_m4k_resume,
	.step = mips_m4k_step,

	.assert_reset = mips_m4k_assert_reset,
	.deassert_reset = mips_m4k_deassert_reset,
	.soft_reset_halt = mips_m4k_soft_reset_halt,
69

ntfreak's avatar
ntfreak committed
70
71
72
73
74
	.get_gdb_reg_list = mips32_get_gdb_reg_list,

	.read_memory = mips_m4k_read_memory,
	.write_memory = mips_m4k_write_memory,
	.bulk_write_memory = mips_m4k_bulk_write_memory,
75
	.checksum_memory = mips_m4k_checksum_memory,
ntfreak's avatar
ntfreak committed
76
	.blank_check_memory = NULL,
77

ntfreak's avatar
ntfreak committed
78
79
80
81
82
83
84
85
	.run_algorithm = mips32_run_algorithm,

	.add_breakpoint = mips_m4k_add_breakpoint,
	.remove_breakpoint = mips_m4k_remove_breakpoint,
	.add_watchpoint = mips_m4k_add_watchpoint,
	.remove_watchpoint = mips_m4k_remove_watchpoint,

	.register_commands = mips_m4k_register_commands,
86
	.target_create = mips_m4k_target_create,
ntfreak's avatar
ntfreak committed
87
88
89
90
	.init_target = mips_m4k_init_target,
	.examine = mips_m4k_examine,
};

Zachary T Welch's avatar
Zachary T Welch committed
91
int mips_m4k_examine_debug_reason(struct target *target)
92
{
93
	uint32_t break_status;
94
	int retval;
95

96
97
98
99
100
101
102
103
104
105
106
107
108
	if ((target->debug_reason != DBG_REASON_DBGRQ)
		&& (target->debug_reason != DBG_REASON_SINGLESTEP))
	{
		/* get info about inst breakpoint support */
		if ((retval = target_read_u32(target, EJTAG_IBS, &break_status)) != ERROR_OK)
			return retval;
		if (break_status & 0x1f)
		{
			/* we have halted on a  breakpoint */
			if ((retval = target_write_u32(target, EJTAG_IBS, 0)) != ERROR_OK)
				return retval;
			target->debug_reason = DBG_REASON_BREAKPOINT;
		}
109

110
111
112
113
114
115
116
117
118
119
120
		/* get info about data breakpoint support */
		if ((retval = target_read_u32(target, 0xFF302000, &break_status)) != ERROR_OK)
			return retval;
		if (break_status & 0x1f)
		{
			/* we have halted on a  breakpoint */
			if ((retval = target_write_u32(target, 0xFF302000, 0)) != ERROR_OK)
				return retval;
			target->debug_reason = DBG_REASON_WATCHPOINT;
		}
	}
121

122
123
124
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
125
int mips_m4k_debug_entry(struct target *target)
ntfreak's avatar
ntfreak committed
126
{
127
	struct mips32_common *mips32 = target->arch_info;
128
	struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
129
	uint32_t debug_reg;
130

ntfreak's avatar
ntfreak committed
131
132
	/* read debug register */
	mips_ejtag_read_debug(ejtag_info, &debug_reg);
133

134
135
	/* make sure break uit configured */
	mips32_configure_break_unit(target);
136

137
138
	/* attempt to find halt reason */
	mips_m4k_examine_debug_reason(target);
139

140
	/* clear single step if active */
ntfreak's avatar
ntfreak committed
141
142
143
144
145
	if (debug_reg & EJTAG_DEBUG_DSS)
	{
		/* stopped due to single step - clear step bit */
		mips_ejtag_config_step(ejtag_info, 0);
	}
146

ntfreak's avatar
ntfreak committed
147
	mips32_save_context(target);
148

duane's avatar
duane committed
149
	LOG_DEBUG("entered debug state at PC 0x%" PRIx32 ", target->state: %s",
150
		*(uint32_t*)(mips32->core_cache->reg_list[MIPS32_PC].value),
151
		  target_state_name(target));
152

ntfreak's avatar
ntfreak committed
153
154
155
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
156
int mips_m4k_poll(struct target *target)
ntfreak's avatar
ntfreak committed
157
158
{
	int retval;
159
	struct mips32_common *mips32 = target->arch_info;
160
	struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
161
	uint32_t ejtag_ctrl = ejtag_info->ejtag_ctrl;
162

ntfreak's avatar
ntfreak committed
163
	/* read ejtag control reg */
164
	jtag_set_end_state(TAP_IDLE);
ntfreak's avatar
ntfreak committed
165
	mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL, NULL);
166
	mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
167

168
169
170
171
172
173
	/* clear this bit before handling polling
	 * as after reset registers will read zero */
	if (ejtag_ctrl & EJTAG_CTRL_ROCC)
	{
		/* we have detected a reset, clear flag
		 * otherwise ejtag will not work */
174
		jtag_set_end_state(TAP_IDLE);
175
		ejtag_ctrl = ejtag_info->ejtag_ctrl & ~EJTAG_CTRL_ROCC;
176

177
178
179
180
		mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL, NULL);
		mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
		LOG_DEBUG("Reset Detected");
	}
181

182
	/* check for processor halted */
183
	if (ejtag_ctrl & EJTAG_CTRL_BRKST)
ntfreak's avatar
ntfreak committed
184
185
186
	{
		if ((target->state == TARGET_RUNNING) || (target->state == TARGET_RESET))
		{
187
			jtag_set_end_state(TAP_IDLE);
ntfreak's avatar
ntfreak committed
188
			mips_ejtag_set_instr(ejtag_info, EJTAG_INST_NORMALBOOT, NULL);
189

ntfreak's avatar
ntfreak committed
190
			target->state = TARGET_HALTED;
191

ntfreak's avatar
ntfreak committed
192
193
			if ((retval = mips_m4k_debug_entry(target)) != ERROR_OK)
				return retval;
194

ntfreak's avatar
ntfreak committed
195
196
197
198
199
			target_call_event_callbacks(target, TARGET_EVENT_HALTED);
		}
		else if (target->state == TARGET_DEBUG_RUNNING)
		{
			target->state = TARGET_HALTED;
200

ntfreak's avatar
ntfreak committed
201
202
			if ((retval = mips_m4k_debug_entry(target)) != ERROR_OK)
				return retval;
203

ntfreak's avatar
ntfreak committed
204
205
206
207
208
209
210
			target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
		}
	}
	else
	{
		target->state = TARGET_RUNNING;
	}
211

zwelch's avatar
zwelch committed
212
//	LOG_DEBUG("ctrl = 0x%08X", ejtag_ctrl);
213

ntfreak's avatar
ntfreak committed
214
215
216
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
217
int mips_m4k_halt(struct target *target)
ntfreak's avatar
ntfreak committed
218
{
219
	struct mips32_common *mips32 = target->arch_info;
220
	struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
221
222

	LOG_DEBUG("target->state: %s",
223
		  target_state_name(target));
224

ntfreak's avatar
ntfreak committed
225
226
227
228
229
	if (target->state == TARGET_HALTED)
	{
		LOG_DEBUG("target was already halted");
		return ERROR_OK;
	}
230

ntfreak's avatar
ntfreak committed
231
232
233
234
	if (target->state == TARGET_UNKNOWN)
	{
		LOG_WARNING("target was in unknown state when halt was requested");
	}
235
236

	if (target->state == TARGET_RESET)
ntfreak's avatar
ntfreak committed
237
	{
238
		if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) && jtag_get_srst())
ntfreak's avatar
ntfreak committed
239
240
241
242
243
244
245
246
247
248
		{
			LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
			return ERROR_TARGET_FAILURE;
		}
		else
		{
			/* we came here in a reset_halt or reset_init sequence
			 * debug entry was already prepared in mips32_prepare_reset_halt()
			 */
			target->debug_reason = DBG_REASON_DBGRQ;
249

ntfreak's avatar
ntfreak committed
250
251
252
			return ERROR_OK;
		}
	}
253

ntfreak's avatar
ntfreak committed
254
255
	/* break processor */
	mips_ejtag_enter_debug(ejtag_info);
256

ntfreak's avatar
ntfreak committed
257
	target->debug_reason = DBG_REASON_DBGRQ;
258

ntfreak's avatar
ntfreak committed
259
260
261
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
262
int mips_m4k_assert_reset(struct target *target)
ntfreak's avatar
ntfreak committed
263
{
264
	struct mips32_common *mips32 = target->arch_info;
265
	struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
266
267

	LOG_DEBUG("target->state: %s",
268
		target_state_name(target));
269

270
	enum reset_types jtag_reset_config = jtag_get_reset_config();
ntfreak's avatar
ntfreak committed
271
272
273
274
275
	if (!(jtag_reset_config & RESET_HAS_SRST))
	{
		LOG_ERROR("Can't assert SRST");
		return ERROR_FAIL;
	}
276

ntfreak's avatar
ntfreak committed
277
278
279
	if (target->reset_halt)
	{
		/* use hardware to catch reset */
280
		jtag_set_end_state(TAP_IDLE);
ntfreak's avatar
ntfreak committed
281
282
283
284
		mips_ejtag_set_instr(ejtag_info, EJTAG_INST_EJTAGBOOT, NULL);
	}
	else
	{
285
		jtag_set_end_state(TAP_IDLE);
ntfreak's avatar
ntfreak committed
286
287
		mips_ejtag_set_instr(ejtag_info, EJTAG_INST_NORMALBOOT, NULL);
	}
288

289
	if (strcmp(target->variant, "ejtag_srst") == 0)
ntfreak's avatar
ntfreak committed
290
	{
291
		uint32_t ejtag_ctrl = ejtag_info->ejtag_ctrl | EJTAG_CTRL_PRRST | EJTAG_CTRL_PERRST;
292
293
294
		LOG_DEBUG("Using EJTAG reset (PRRST) to reset processor...");
		mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL, NULL);
		mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
ntfreak's avatar
ntfreak committed
295
296
297
	}
	else
	{
298
299
300
301
302
303
304
305
306
		/* here we should issue a srst only, but we may have to assert trst as well */
		if (jtag_reset_config & RESET_SRST_PULLS_TRST)
		{
			jtag_add_reset(1, 1);
		}
		else
		{
			jtag_add_reset(0, 1);
		}
ntfreak's avatar
ntfreak committed
307
	}
308

ntfreak's avatar
ntfreak committed
309
310
311
312
313
	target->state = TARGET_RESET;
	jtag_add_sleep(50000);

	mips32_invalidate_core_regs(target);

ntfreak's avatar
ntfreak committed
314
315
316
	if (target->reset_halt)
	{
		int retval;
zwelch's avatar
zwelch committed
317
		if ((retval = target_halt(target)) != ERROR_OK)
318
			return retval;
ntfreak's avatar
ntfreak committed
319
	}
320

ntfreak's avatar
ntfreak committed
321
322
323
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
324
int mips_m4k_deassert_reset(struct target *target)
ntfreak's avatar
ntfreak committed
325
{
326
	LOG_DEBUG("target->state: %s",
327
		target_state_name(target));
328

ntfreak's avatar
ntfreak committed
329
330
	/* deassert reset lines */
	jtag_add_reset(0, 0);
331

ntfreak's avatar
ntfreak committed
332
333
334
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
335
int mips_m4k_soft_reset_halt(struct target *target)
ntfreak's avatar
ntfreak committed
336
337
338
339
340
{
	/* TODO */
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
341
int mips_m4k_single_step_core(struct target *target)
342
{
343
	struct mips32_common *mips32 = target->arch_info;
344
	struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
345

346
347
	/* configure single step mode */
	mips_ejtag_config_step(ejtag_info, 1);
348

349
350
	/* disable interrupts while stepping */
	mips32_enable_interrupts(target, 0);
351

352
	/* exit debug mode */
353
	mips_ejtag_exit_debug(ejtag_info);
354

355
	mips_m4k_debug_entry(target);
356

357
358
359
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
360
int mips_m4k_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
ntfreak's avatar
ntfreak committed
361
{
362
	struct mips32_common *mips32 = target->arch_info;
363
	struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
364
	struct breakpoint *breakpoint = NULL;
365
	uint32_t resume_pc;
366

ntfreak's avatar
ntfreak committed
367
368
369
370
371
	if (target->state != TARGET_HALTED)
	{
		LOG_WARNING("target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}
372

ntfreak's avatar
ntfreak committed
373
374
375
376
377
378
	if (!debug_execution)
	{
		target_free_all_working_areas(target);
		mips_m4k_enable_breakpoints(target);
		mips_m4k_enable_watchpoints(target);
	}
379

ntfreak's avatar
ntfreak committed
380
	/* current = 1: continue on current pc, otherwise continue at <address> */
ntfreak's avatar
ntfreak committed
381
	if (!current)
ntfreak's avatar
ntfreak committed
382
383
384
385
386
	{
		buf_set_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32, address);
		mips32->core_cache->reg_list[MIPS32_PC].dirty = 1;
		mips32->core_cache->reg_list[MIPS32_PC].valid = 1;
	}
387

ntfreak's avatar
ntfreak committed
388
	resume_pc = buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32);
389

ntfreak's avatar
ntfreak committed
390
	mips32_restore_context(target);
391

ntfreak's avatar
ntfreak committed
392
393
394
395
396
397
	/* the front-end may request us not to handle breakpoints */
	if (handle_breakpoints)
	{
		/* Single step past breakpoint at current address */
		if ((breakpoint = breakpoint_find(target, resume_pc)))
		{
duane's avatar
duane committed
398
			LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 "", breakpoint->address);
ntfreak's avatar
ntfreak committed
399
			mips_m4k_unset_breakpoint(target, breakpoint);
400
			mips_m4k_single_step_core(target);
ntfreak's avatar
ntfreak committed
401
402
403
			mips_m4k_set_breakpoint(target, breakpoint);
		}
	}
404

405
406
	/* enable interrupts if we are running */
	mips32_enable_interrupts(target, !debug_execution);
407

408
409
	/* exit debug mode */
	mips_ejtag_exit_debug(ejtag_info);
410
	target->debug_reason = DBG_REASON_NOTHALTED;
411

ntfreak's avatar
ntfreak committed
412
413
	/* registers are now invalid */
	mips32_invalidate_core_regs(target);
414

ntfreak's avatar
ntfreak committed
415
416
417
418
	if (!debug_execution)
	{
		target->state = TARGET_RUNNING;
		target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
duane's avatar
duane committed
419
		LOG_DEBUG("target resumed at 0x%" PRIx32 "", resume_pc);
ntfreak's avatar
ntfreak committed
420
421
422
423
424
	}
	else
	{
		target->state = TARGET_DEBUG_RUNNING;
		target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
duane's avatar
duane committed
425
		LOG_DEBUG("target debug resumed at 0x%" PRIx32 "", resume_pc);
ntfreak's avatar
ntfreak committed
426
	}
427

ntfreak's avatar
ntfreak committed
428
429
430
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
431
int mips_m4k_step(struct target *target, int current, uint32_t address, int handle_breakpoints)
ntfreak's avatar
ntfreak committed
432
433
{
	/* get pointers to arch-specific information */
434
	struct mips32_common *mips32 = target->arch_info;
435
	struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
436
	struct breakpoint *breakpoint = NULL;
ntfreak's avatar
ntfreak committed
437
438
439
440
441
442
443
444
445
446

	if (target->state != TARGET_HALTED)
	{
		LOG_WARNING("target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	/* current = 1: continue on current pc, otherwise continue at <address> */
	if (!current)
		buf_set_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32, address);
447

ntfreak's avatar
ntfreak committed
448
449
450
451
	/* the front-end may request us not to handle breakpoints */
	if (handle_breakpoints)
		if ((breakpoint = breakpoint_find(target, buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32))))
			mips_m4k_unset_breakpoint(target, breakpoint);
452

ntfreak's avatar
ntfreak committed
453
454
	/* restore context */
	mips32_restore_context(target);
455

ntfreak's avatar
ntfreak committed
456
457
	/* configure single step mode */
	mips_ejtag_config_step(ejtag_info, 1);
458

ntfreak's avatar
ntfreak committed
459
	target->debug_reason = DBG_REASON_SINGLESTEP;
460

ntfreak's avatar
ntfreak committed
461
	target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
462

463
464
	/* disable interrupts while stepping */
	mips32_enable_interrupts(target, 0);
465

ntfreak's avatar
ntfreak committed
466
	/* exit debug mode */
467
	mips_ejtag_exit_debug(ejtag_info);
468

ntfreak's avatar
ntfreak committed
469
470
	/* registers are now invalid */
	mips32_invalidate_core_regs(target);
471

ntfreak's avatar
ntfreak committed
472
473
474
475
	if (breakpoint)
		mips_m4k_set_breakpoint(target, breakpoint);

	LOG_DEBUG("target stepped ");
476

ntfreak's avatar
ntfreak committed
477
478
	mips_m4k_debug_entry(target);
	target_call_event_callbacks(target, TARGET_EVENT_HALTED);
479

ntfreak's avatar
ntfreak committed
480
481
482
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
483
void mips_m4k_enable_breakpoints(struct target *target)
ntfreak's avatar
ntfreak committed
484
{
485
	struct breakpoint *breakpoint = target->breakpoints;
486

ntfreak's avatar
ntfreak committed
487
488
489
490
491
492
493
494
495
	/* set any pending breakpoints */
	while (breakpoint)
	{
		if (breakpoint->set == 0)
			mips_m4k_set_breakpoint(target, breakpoint);
		breakpoint = breakpoint->next;
	}
}

Zachary T Welch's avatar
Zachary T Welch committed
496
int mips_m4k_set_breakpoint(struct target *target, struct breakpoint *breakpoint)
ntfreak's avatar
ntfreak committed
497
{
498
	struct mips32_common *mips32 = target->arch_info;
499
	struct mips32_comparator * comparator_list = mips32->inst_break_list;
500
	int retval;
501

502
503
504
505
506
	if (breakpoint->set)
	{
		LOG_WARNING("breakpoint already set");
		return ERROR_OK;
	}
507

508
509
510
	if (breakpoint->type == BKPT_HARD)
	{
		int bp_num = 0;
511

zwelch's avatar
zwelch committed
512
		while (comparator_list[bp_num].used && (bp_num < mips32->num_inst_bpoints))
513
514
515
			bp_num++;
		if (bp_num >= mips32->num_inst_bpoints)
		{
David Brownell's avatar
David Brownell committed
516
			LOG_ERROR("Can not find free FP Comparator(bpid: %d)",
517
					  breakpoint->unique_id );
David Brownell's avatar
David Brownell committed
518
			return ERROR_FAIL;
519
520
521
522
523
524
525
		}
		breakpoint->set = bp_num + 1;
		comparator_list[bp_num].used = 1;
		comparator_list[bp_num].bp_value = breakpoint->address;
		target_write_u32(target, comparator_list[bp_num].reg_address, comparator_list[bp_num].bp_value);
		target_write_u32(target, comparator_list[bp_num].reg_address + 0x08, 0x00000000);
		target_write_u32(target, comparator_list[bp_num].reg_address + 0x18, 1);
526
		LOG_DEBUG("bpid: %d, bp_num %i bp_value 0x%" PRIx32 "",
527
528
				  breakpoint->unique_id,
				  bp_num, comparator_list[bp_num].bp_value);
529
530
531
	}
	else if (breakpoint->type == BKPT_SOFT)
	{
532
		LOG_DEBUG("bpid: %d", breakpoint->unique_id );
533
534
		if (breakpoint->length == 4)
		{
535
			uint32_t verify = 0xffffffff;
536

zwelch's avatar
zwelch committed
537
			if ((retval = target_read_memory(target, breakpoint->address, breakpoint->length, 1, breakpoint->orig_instr)) != ERROR_OK)
538
539
540
541
542
543
544
			{
				return retval;
			}
			if ((retval = target_write_u32(target, breakpoint->address, MIPS32_SDBBP)) != ERROR_OK)
			{
				return retval;
			}
545

546
547
548
549
550
551
			if ((retval = target_read_u32(target, breakpoint->address, &verify)) != ERROR_OK)
			{
				return retval;
			}
			if (verify != MIPS32_SDBBP)
			{
duane's avatar
duane committed
552
				LOG_ERROR("Unable to set 32bit breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
553
554
555
556
557
				return ERROR_OK;
			}
		}
		else
		{
zwelch's avatar
zwelch committed
558
			uint16_t verify = 0xffff;
559

zwelch's avatar
zwelch committed
560
			if ((retval = target_read_memory(target, breakpoint->address, breakpoint->length, 1, breakpoint->orig_instr)) != ERROR_OK)
561
562
563
564
565
566
567
			{
				return retval;
			}
			if ((retval = target_write_u16(target, breakpoint->address, MIPS16_SDBBP)) != ERROR_OK)
			{
				return retval;
			}
568

569
570
571
572
573
574
			if ((retval = target_read_u16(target, breakpoint->address, &verify)) != ERROR_OK)
			{
				return retval;
			}
			if (verify != MIPS16_SDBBP)
			{
duane's avatar
duane committed
575
				LOG_ERROR("Unable to set 16bit breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
576
577
578
				return ERROR_OK;
			}
		}
579

580
		breakpoint->set = 20; /* Any nice value but 0 */
581
	}
582

ntfreak's avatar
ntfreak committed
583
584
585
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
586
int mips_m4k_unset_breakpoint(struct target *target, struct breakpoint *breakpoint)
ntfreak's avatar
ntfreak committed
587
{
588
	/* get pointers to arch-specific information */
589
	struct mips32_common *mips32 = target->arch_info;
590
	struct mips32_comparator * comparator_list = mips32->inst_break_list;
591
	int retval;
592

593
594
595
596
597
	if (!breakpoint->set)
	{
		LOG_WARNING("breakpoint not set");
		return ERROR_OK;
	}
598

599
600
601
602
603
	if (breakpoint->type == BKPT_HARD)
	{
		int bp_num = breakpoint->set - 1;
		if ((bp_num < 0) || (bp_num >= mips32->num_inst_bpoints))
		{
604
605
			LOG_DEBUG("Invalid FP Comparator number in breakpoint (bpid: %d)",
					  breakpoint->unique_id);
606
607
			return ERROR_OK;
		}
608
609
610
		LOG_DEBUG("bpid: %d - releasing hw: %d",
				  breakpoint->unique_id,
				  bp_num );
611
612
613
		comparator_list[bp_num].used = 0;
		comparator_list[bp_num].bp_value = 0;
		target_write_u32(target, comparator_list[bp_num].reg_address + 0x18, 0);
614

615
616
617
	}
	else
	{
618
		/* restore original instruction (kept in target endianness) */
619
		LOG_DEBUG("bpid: %d", breakpoint->unique_id);
620
621
		if (breakpoint->length == 4)
		{
622
			uint32_t current_instr;
623

624
			/* check that user program has not modified breakpoint instruction */
625
			if ((retval = target_read_memory(target, breakpoint->address, 4, 1, (uint8_t*)&current_instr)) != ERROR_OK)
626
627
628
629
630
			{
				return retval;
			}
			if (current_instr == MIPS32_SDBBP)
			{
zwelch's avatar
zwelch committed
631
				if ((retval = target_write_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
632
633
634
635
636
637
638
				{
					return retval;
				}
			}
		}
		else
		{
zwelch's avatar
zwelch committed
639
			uint16_t current_instr;
640

641
			/* check that user program has not modified breakpoint instruction */
642
			if ((retval = target_read_memory(target, breakpoint->address, 2, 1, (uint8_t*)&current_instr)) != ERROR_OK)
643
644
645
			{
				return retval;
			}
646

647
648
			if (current_instr == MIPS16_SDBBP)
			{
zwelch's avatar
zwelch committed
649
				if ((retval = target_write_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
650
651
652
653
654
				{
					return retval;
				}
			}
		}
655
656
	}
	breakpoint->set = 0;
657

ntfreak's avatar
ntfreak committed
658
659
660
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
661
int mips_m4k_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
ntfreak's avatar
ntfreak committed
662
{
663
	struct mips32_common *mips32 = target->arch_info;
664

665
	if (breakpoint->type == BKPT_HARD)
666
	{
667
668
669
670
671
		if (mips32->num_inst_bpoints_avail < 1)
		{
			LOG_INFO("no hardware breakpoint available");
			return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
		}
672

673
		mips32->num_inst_bpoints_avail--;
674
	}
675

676
	mips_m4k_set_breakpoint(target, breakpoint);
677

ntfreak's avatar
ntfreak committed
678
679
680
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
681
int mips_m4k_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
ntfreak's avatar
ntfreak committed
682
{
683
	/* get pointers to arch-specific information */
684
	struct mips32_common *mips32 = target->arch_info;
685

686
687
688
689
690
	if (target->state != TARGET_HALTED)
	{
		LOG_WARNING("target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}
691

692
693
694
695
	if (breakpoint->set)
	{
		mips_m4k_unset_breakpoint(target, breakpoint);
	}
696

697
698
	if (breakpoint->type == BKPT_HARD)
		mips32->num_inst_bpoints_avail++;
699

ntfreak's avatar
ntfreak committed
700
701
702
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
703
int mips_m4k_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
ntfreak's avatar
ntfreak committed
704
{
705
	struct mips32_common *mips32 = target->arch_info;
706
	struct mips32_comparator * comparator_list = mips32->data_break_list;
707
708
709
710
711
712
	int wp_num = 0;
	/*
	 * watchpoint enabled, ignore all byte lanes in value register
	 * and exclude both load and store accesses from  watchpoint
	 * condition evaluation
	*/
713
	int enable = EJTAG_DBCn_NOSB | EJTAG_DBCn_NOLB | EJTAG_DBCn_BE |
714
                (0xff << EJTAG_DBCn_BLM_SHIFT);
715

716
717
718
719
720
721
722
723
724
725
	if (watchpoint->set)
	{
		LOG_WARNING("watchpoint already set");
		return ERROR_OK;
	}

	while(comparator_list[wp_num].used && (wp_num < mips32->num_data_bpoints))
		wp_num++;
	if (wp_num >= mips32->num_data_bpoints)
	{
David Brownell's avatar
David Brownell committed
726
727
		LOG_ERROR("Can not find free FP Comparator");
		return ERROR_FAIL;
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
	}

	if (watchpoint->length != 4)
	{
		LOG_ERROR("Only watchpoints of length 4 are supported");
		return ERROR_TARGET_UNALIGNED_ACCESS;
	}

	if (watchpoint->address % 4)
	{
		LOG_ERROR("Watchpoints address should be word aligned");
		return ERROR_TARGET_UNALIGNED_ACCESS;
	}

	switch (watchpoint->rw)
	{
		case WPT_READ:
			enable &= ~EJTAG_DBCn_NOLB;
			break;
		case WPT_WRITE:
			enable &= ~EJTAG_DBCn_NOSB;
			break;
		case WPT_ACCESS:
			enable &= ~(EJTAG_DBCn_NOLB | EJTAG_DBCn_NOSB);
			break;
		default:
			LOG_ERROR("BUG: watchpoint->rw neither read, write nor access");
	}

	watchpoint->set = wp_num + 1;
	comparator_list[wp_num].used = 1;
	comparator_list[wp_num].bp_value = watchpoint->address;
	target_write_u32(target, comparator_list[wp_num].reg_address, comparator_list[wp_num].bp_value);
	target_write_u32(target, comparator_list[wp_num].reg_address + 0x08, 0x00000000);
	target_write_u32(target, comparator_list[wp_num].reg_address + 0x10, 0x00000000);
	target_write_u32(target, comparator_list[wp_num].reg_address + 0x18, enable);
	target_write_u32(target, comparator_list[wp_num].reg_address + 0x20, 0);
	LOG_DEBUG("wp_num %i bp_value 0x%" PRIx32 "", wp_num, comparator_list[wp_num].bp_value);
766

ntfreak's avatar
ntfreak committed
767
768
769
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
770
int mips_m4k_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
ntfreak's avatar
ntfreak committed
771
{
772
	/* get pointers to arch-specific information */
773
	struct mips32_common *mips32 = target->arch_info;
774
	struct mips32_comparator * comparator_list = mips32->data_break_list;
775

776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
	if (!watchpoint->set)
	{
		LOG_WARNING("watchpoint not set");
		return ERROR_OK;
	}

	int wp_num = watchpoint->set - 1;
	if ((wp_num < 0) || (wp_num >= mips32->num_data_bpoints))
	{
		LOG_DEBUG("Invalid FP Comparator number in watchpoint");
		return ERROR_OK;
	}
	comparator_list[wp_num].used = 0;
	comparator_list[wp_num].bp_value = 0;
	target_write_u32(target, comparator_list[wp_num].reg_address + 0x18, 0);
	watchpoint->set = 0;

ntfreak's avatar
ntfreak committed
793
794
795
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
796
int mips_m4k_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
ntfreak's avatar
ntfreak committed
797
{
798
	struct mips32_common *mips32 = target->arch_info;
799
800
801
802
803
804

	if (mips32->num_data_bpoints_avail < 1)
	{
		LOG_INFO("no hardware watchpoints available");
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
	}
805

806
807
808
	mips32->num_data_bpoints_avail--;

	mips_m4k_set_watchpoint(target, watchpoint);
ntfreak's avatar
ntfreak committed
809
810
811
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
812
int mips_m4k_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
ntfreak's avatar
ntfreak committed
813
{
814
	/* get pointers to arch-specific information */
815
	struct mips32_common *mips32 = target->arch_info;
816
817
818
819
820
821
822
823
824
825
826
827
828
829

	if (target->state != TARGET_HALTED)
	{
		LOG_WARNING("target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	if (watchpoint->set)
	{
		mips_m4k_unset_watchpoint(target, watchpoint);
	}

	mips32->num_data_bpoints_avail++;

ntfreak's avatar
ntfreak committed
830
831
832
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
833
void mips_m4k_enable_watchpoints(struct target *target)
ntfreak's avatar
ntfreak committed
834
{
835
	struct watchpoint *watchpoint = target->watchpoints;
836

ntfreak's avatar
ntfreak committed
837
838
839
840
841
842
843
844
845
	/* set any pending watchpoints */
	while (watchpoint)
	{
		if (watchpoint->set == 0)
			mips_m4k_set_watchpoint(target, watchpoint);
		watchpoint = watchpoint->next;
	}
}

Zachary T Welch's avatar
Zachary T Welch committed
846
int mips_m4k_read_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
ntfreak's avatar
ntfreak committed
847
{
848
	struct mips32_common *mips32 = target->arch_info;
849
	struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
850

duane's avatar
duane committed
851
	LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", address, size, count);
ntfreak's avatar
ntfreak committed
852
853
854
855
856
857
858
859
860
861
862
863
864

	if (target->state != TARGET_HALTED)
	{
		LOG_WARNING("target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	/* sanitize arguments */
	if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
		return ERROR_INVALID_ARGUMENTS;

	if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
		return ERROR_TARGET_UNALIGNED_ACCESS;
865

866
867
	/* if noDMA off, use DMAACC mode for memory read */
	int retval;
zwelch's avatar
zwelch committed
868
	if (ejtag_info->impcode & EJTAG_IMP_NODMA)
869
870
871
872
873
		retval = mips32_pracc_read_mem(ejtag_info, address, size, count, (void *)buffer);
	else
		retval = mips32_dmaacc_read_mem(ejtag_info, address, size, count, (void *)buffer);
	if (ERROR_OK != retval)
		return retval;
ntfreak's avatar
ntfreak committed
874

875
	return ERROR_OK;
ntfreak's avatar
ntfreak committed
876
877
}

Zachary T Welch's avatar
Zachary T Welch committed
878
int mips_m4k_write_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
ntfreak's avatar
ntfreak committed
879
{
880
	struct mips32_common *mips32 = target->arch_info;
881
	struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
882

duane's avatar
duane committed
883
	LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", address, size, count);
ntfreak's avatar
ntfreak committed
884
885
886
887
888
889
890
891
892
893
894
895
896

	if (target->state != TARGET_HALTED)
	{
		LOG_WARNING("target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	/* sanitize arguments */
	if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
		return ERROR_INVALID_ARGUMENTS;

	if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
		return ERROR_TARGET_UNALIGNED_ACCESS;
897

898
	/* if noDMA off, use DMAACC mode for memory write */
zwelch's avatar
zwelch committed
899
	if (ejtag_info->impcode & EJTAG_IMP_NODMA)
900
		return mips32_pracc_write_mem(ejtag_info, address, size, count, (void *)buffer);
zwelch's avatar
zwelch committed
901
	else
902
		return mips32_dmaacc_write_mem(ejtag_info, address, size, count, (void *)buffer);
ntfreak's avatar
ntfreak committed
903
904
}

905
int mips_m4k_register_commands(struct command_context *cmd_ctx)
ntfreak's avatar
ntfreak committed
906
907
{
	int retval;
908

ntfreak's avatar
ntfreak committed
909
910
911
912
	retval = mips32_register_commands(cmd_ctx);
	return retval;
}

913
int mips_m4k_init_target(struct command_context *cmd_ctx, struct target *target)
ntfreak's avatar
ntfreak committed
914
915
{
	mips32_build_reg_cache(target);
916

ntfreak's avatar
ntfreak committed
917
918
919
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
920
int mips_m4k_init_arch_info(struct target *target, struct mips_m4k_common *mips_m4k, struct jtag_tap *tap)
ntfreak's avatar
ntfreak committed
921
{
922
	struct mips32_common *mips32 = &mips_m4k->mips32_common;
923

ntfreak's avatar
ntfreak committed
924
	mips_m4k->common_magic = MIPSM4K_COMMON_MAGIC;
925

ntfreak's avatar
ntfreak committed
926
	/* initialize mips4k specific info */
927
	mips32_init_arch_info(target, mips32, tap);
ntfreak's avatar
ntfreak committed
928
	mips32->arch_info = mips_m4k;
929

ntfreak's avatar
ntfreak committed
930
931
932
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
933
int mips_m4k_target_create(struct target *target, Jim_Interp *interp)
ntfreak's avatar
ntfreak committed
934
{
935
	struct mips_m4k_common *mips_m4k = calloc(1,sizeof(struct mips_m4k_common));
936

937
	mips_m4k_init_arch_info(target, mips_m4k, target->tap);
938

ntfreak's avatar
ntfreak committed
939
940
941
	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
942
int mips_m4k_examine(struct target *target)
ntfreak's avatar
ntfreak committed
943
944
{
	int retval;
945
	struct mips32_common *mips32 = target->arch_info;
946
	struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
947
	uint32_t idcode = 0;
948

949
	if (!target_was_examined(target))
ntfreak's avatar
ntfreak committed
950
	{
951
		mips_ejtag_get_idcode(ejtag_info, &idcode);
952
		ejtag_info->idcode = idcode;
953

954
955
956
957
958
959
960
		if (((idcode >> 1) & 0x7FF) == 0x29)
		{
			/* we are using a pic32mx so select ejtag port
			 * as it is not selected by default */
			mips_ejtag_set_instr(ejtag_info, 0x05, NULL);
			LOG_DEBUG("PIC32MX Detected - using EJTAG Interface");
		}
ntfreak's avatar
ntfreak committed
961
	}
962

ntfreak's avatar