mips_m4k.c 24.4 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
26
27
/***************************************************************************
 *   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

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


/* cli handling */

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

ntfreak's avatar
ntfreak committed
47
int mips_m4k_examine(struct target_s *target);
ntfreak's avatar
ntfreak committed
48
49
int mips_m4k_assert_reset(target_t *target);
int mips_m4k_deassert_reset(target_t *target);
50
int mips_m4k_checksum_memory(target_t *target, uint32_t address, uint32_t size, uint32_t *checksum);
ntfreak's avatar
ntfreak committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

target_type_t mips_m4k_target =
{
	.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,
68

ntfreak's avatar
ntfreak committed
69
70
71
72
73
	.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,
74
	.checksum_memory = mips_m4k_checksum_memory,
ntfreak's avatar
ntfreak committed
75
	.blank_check_memory = NULL,
76

ntfreak's avatar
ntfreak committed
77
78
79
80
81
82
83
84
	.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,
85
	.target_create = mips_m4k_target_create,
ntfreak's avatar
ntfreak committed
86
87
88
89
90
	.init_target = mips_m4k_init_target,
	.examine = mips_m4k_examine,
	.quit = mips_m4k_quit
};

91
int mips_m4k_examine_debug_reason(target_t *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;
}

ntfreak's avatar
ntfreak committed
125
126
127
128
int mips_m4k_debug_entry(target_t *target)
{
	mips32_common_t *mips32 = target->arch_info;
	mips_ejtag_t *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
		  Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name);
152

ntfreak's avatar
ntfreak committed
153
154
155
156
157
158
159
160
	return ERROR_OK;
}

int mips_m4k_poll(target_t *target)
{
	int retval;
	mips32_common_t *mips32 = target->arch_info;
	mips_ejtag_t *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

212
//	LOG_DEBUG("ctrl=0x%08X", ejtag_ctrl);
213

ntfreak's avatar
ntfreak committed
214
215
216
217
218
219
220
	return ERROR_OK;
}

int mips_m4k_halt(struct target_s *target)
{
	mips32_common_t *mips32 = target->arch_info;
	mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
221
222

	LOG_DEBUG("target->state: %s",
223
		  Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name);
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
262
263
264
265
	return ERROR_OK;
}

int mips_m4k_assert_reset(target_t *target)
{
	mips32_common_t *mips32 = target->arch_info;
	mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
266
267

	LOG_DEBUG("target->state: %s",
ntfreak's avatar
ntfreak committed
268
		Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name);
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;
317
318
		if ((retval = target_halt(target))!=ERROR_OK)
			return retval;
ntfreak's avatar
ntfreak committed
319
	}
320

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

int mips_m4k_deassert_reset(target_t *target)
{
326
	LOG_DEBUG("target->state: %s",
ntfreak's avatar
ntfreak committed
327
		Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name);
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
335
336
337
338
339
340
	return ERROR_OK;
}

int mips_m4k_soft_reset_halt(struct target_s *target)
{
	/* TODO */
	return ERROR_OK;
}

341
342
343
344
int mips_m4k_single_step_core(target_t *target)
{
	mips32_common_t *mips32 = target->arch_info;
	mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
345

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

349
350
351
	/* disable interrupts while stepping */
	mips32_enable_interrupts(target, 0);
	
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;
}

360
int mips_m4k_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
ntfreak's avatar
ntfreak committed
361
362
363
364
{
	mips32_common_t *mips32 = target->arch_info;
	mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
	breakpoint_t *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
407
408
409
	/* enable interrupts if we are running */
	mips32_enable_interrupts(target, !debug_execution);
	
	/* 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;
}

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

	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
465
	/* disable interrupts while stepping */
	mips32_enable_interrupts(target, 0);
		
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
483
484
485
	return ERROR_OK;
}

void mips_m4k_enable_breakpoints(struct target_s *target)
{
	breakpoint_t *breakpoint = target->breakpoints;
486

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

int mips_m4k_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
{
498
499
	mips32_common_t *mips32 = target->arch_info;
	mips32_comparator_t * comparator_list = mips32->inst_break_list;
500
501
	int retval;
	
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
516
517
518
519
520
521
522
523
524
525
			bp_num++;
		if (bp_num >= mips32->num_inst_bpoints)
		{
			LOG_DEBUG("ERROR Can not find free FP Comparator");
			LOG_WARNING("ERROR Can not find free FP Comparator");
			exit(-1);
		}
		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);
duane's avatar
duane committed
526
		LOG_DEBUG("bp_num %i bp_value 0x%" PRIx32 "", bp_num, comparator_list[bp_num].bp_value);
527
528
529
	}
	else if (breakpoint->type == BKPT_SOFT)
	{
530
531
		if (breakpoint->length == 4)
		{
532
			uint32_t verify = 0xffffffff;
533
			
zwelch's avatar
zwelch committed
534
			if ((retval = target_read_memory(target, breakpoint->address, breakpoint->length, 1, breakpoint->orig_instr)) != ERROR_OK)
535
536
537
538
539
540
541
542
543
544
545
546
547
548
			{
				return retval;
			}
			if ((retval = target_write_u32(target, breakpoint->address, MIPS32_SDBBP)) != ERROR_OK)
			{
				return retval;
			}
			
			if ((retval = target_read_u32(target, breakpoint->address, &verify)) != ERROR_OK)
			{
				return retval;
			}
			if (verify != MIPS32_SDBBP)
			{
duane's avatar
duane committed
549
				LOG_ERROR("Unable to set 32bit breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
550
551
552
553
554
				return ERROR_OK;
			}
		}
		else
		{
zwelch's avatar
zwelch committed
555
			uint16_t verify = 0xffff;
556
			
zwelch's avatar
zwelch committed
557
			if ((retval = target_read_memory(target, breakpoint->address, breakpoint->length, 1, breakpoint->orig_instr)) != ERROR_OK)
558
559
560
561
562
563
564
565
566
567
568
569
570
571
			{
				return retval;
			}
			if ((retval = target_write_u16(target, breakpoint->address, MIPS16_SDBBP)) != ERROR_OK)
			{
				return retval;
			}
			
			if ((retval = target_read_u16(target, breakpoint->address, &verify)) != ERROR_OK)
			{
				return retval;
			}
			if (verify != MIPS16_SDBBP)
			{
duane's avatar
duane committed
572
				LOG_ERROR("Unable to set 16bit breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
573
574
575
576
577
				return ERROR_OK;
			}
		}
		
		breakpoint->set = 20; /* Any nice value but 0 */
578
	}
579

ntfreak's avatar
ntfreak committed
580
581
582
583
584
	return ERROR_OK;
}

int mips_m4k_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
{
585
586
587
	/* get pointers to arch-specific information */
	mips32_common_t *mips32 = target->arch_info;
	mips32_comparator_t * comparator_list = mips32->inst_break_list;
588
589
	int retval;
	
590
591
592
593
594
	if (!breakpoint->set)
	{
		LOG_WARNING("breakpoint not set");
		return ERROR_OK;
	}
595

596
597
598
599
600
601
602
603
604
605
606
607
608
609
	if (breakpoint->type == BKPT_HARD)
	{
		int bp_num = breakpoint->set - 1;
		if ((bp_num < 0) || (bp_num >= mips32->num_inst_bpoints))
		{
			LOG_DEBUG("Invalid FP Comparator number in breakpoint");
			return ERROR_OK;
		}
		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);
	}
	else
	{
610
611
612
		/* restore original instruction (kept in target endianness) */
		if (breakpoint->length == 4)
		{
613
			uint32_t current_instr;
614
615
			
			/* check that user program has not modified breakpoint instruction */
616
			if ((retval = target_read_memory(target, breakpoint->address, 4, 1, (uint8_t*)&current_instr)) != ERROR_OK)
617
618
619
620
621
			{
				return retval;
			}
			if (current_instr == MIPS32_SDBBP)
			{
zwelch's avatar
zwelch committed
622
				if ((retval = target_write_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
623
624
625
626
627
628
629
				{
					return retval;
				}
			}
		}
		else
		{
zwelch's avatar
zwelch committed
630
			uint16_t current_instr;
631
632
			
			/* check that user program has not modified breakpoint instruction */
633
			if ((retval = target_read_memory(target, breakpoint->address, 2, 1, (uint8_t*)&current_instr)) != ERROR_OK)
634
635
636
637
638
639
			{
				return retval;
			}
			
			if (current_instr == MIPS16_SDBBP)
			{
zwelch's avatar
zwelch committed
640
				if ((retval = target_write_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
641
642
643
644
645
				{
					return retval;
				}
			}
		}
646
647
	}
	breakpoint->set = 0;
648

ntfreak's avatar
ntfreak committed
649
650
651
652
653
	return ERROR_OK;
}

int mips_m4k_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
{
654
	mips32_common_t *mips32 = target->arch_info;
655

656
	if (breakpoint->type == BKPT_HARD)
657
	{
658
659
660
661
662
663
664
665
		if (mips32->num_inst_bpoints_avail < 1)
		{
			LOG_INFO("no hardware breakpoint available");
			return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
		}
		
		mips32->num_inst_bpoints_avail--;
	}	
666

667
	mips_m4k_set_breakpoint(target, breakpoint);
668

ntfreak's avatar
ntfreak committed
669
670
671
672
673
	return ERROR_OK;
}

int mips_m4k_remove_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
{
674
675
	/* get pointers to arch-specific information */
	mips32_common_t *mips32 = target->arch_info;
676

677
678
679
680
681
	if (target->state != TARGET_HALTED)
	{
		LOG_WARNING("target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}
682

683
684
685
686
	if (breakpoint->set)
	{
		mips_m4k_unset_breakpoint(target, breakpoint);
	}
687

688
689
	if (breakpoint->type == BKPT_HARD)
		mips32->num_inst_bpoints_avail++;
690

ntfreak's avatar
ntfreak committed
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
	return ERROR_OK;
}

int mips_m4k_set_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
{
	/* TODO */
	return ERROR_OK;
}

int mips_m4k_unset_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
{
	/* TODO */
	return ERROR_OK;
}

int mips_m4k_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
{
	/* TODO */
	return ERROR_OK;
}

int mips_m4k_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
{
	/* TODO */
	return ERROR_OK;
}

void mips_m4k_enable_watchpoints(struct target_s *target)
{
	watchpoint_t *watchpoint = target->watchpoints;
721

ntfreak's avatar
ntfreak committed
722
723
724
725
726
727
728
729
730
	/* set any pending watchpoints */
	while (watchpoint)
	{
		if (watchpoint->set == 0)
			mips_m4k_set_watchpoint(target, watchpoint);
		watchpoint = watchpoint->next;
	}
}

731
int mips_m4k_read_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
ntfreak's avatar
ntfreak committed
732
733
734
{
	mips32_common_t *mips32 = target->arch_info;
	mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
735

duane's avatar
duane committed
736
	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
737
738
739
740
741
742
743
744
745
746
747
748
749

	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;
750

751
752
	/* if noDMA off, use DMAACC mode for memory read */
	int retval;
zwelch's avatar
zwelch committed
753
	if (ejtag_info->impcode & EJTAG_IMP_NODMA)
754
755
756
757
758
		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
759

zwelch's avatar
zwelch committed
760
761
762
	/* TAP data register is loaded LSB first (little endian) */
	if (target->endianness == TARGET_BIG_ENDIAN) 
	{
763
		uint32_t i, t32;
zwelch's avatar
zwelch committed
764
		uint16_t t16;
zwelch's avatar
zwelch committed
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780

		for(i = 0; i < (count*size); i += size)
		{
			switch(size)
			{
				case 4:
					t32 = le_to_h_u32(&buffer[i]);
					h_u32_to_be(&buffer[i], t32);
					break;
				case 2:
					t16 = le_to_h_u16(&buffer[i]);
					h_u16_to_be(&buffer[i], t16);
					break;
			}
		}
	}
781
782

	return ERROR_OK;
ntfreak's avatar
ntfreak committed
783
784
}

785
int mips_m4k_write_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
ntfreak's avatar
ntfreak committed
786
787
788
{
	mips32_common_t *mips32 = target->arch_info;
	mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
789

duane's avatar
duane committed
790
	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
791
792
793
794
795
796
797
798
799
800
801
802
803

	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;
804

zwelch's avatar
zwelch committed
805
806
807
	/* TAP data register is loaded LSB first (little endian) */
	if (target->endianness == TARGET_BIG_ENDIAN)
	{
808
		uint32_t i, t32;
zwelch's avatar
zwelch committed
809
		uint16_t t16;
zwelch's avatar
zwelch committed
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826

		for(i = 0; i < (count*size); i += size)
		{
			switch(size) 
			{
				case 4:
					t32 = be_to_h_u32(&buffer[i]);
					h_u32_to_le(&buffer[i], t32);
					break;
				case 2:
					t16 = be_to_h_u16(&buffer[i]);
					h_u16_to_le(&buffer[i], t16);
					break;
			}
		}
	}	   

827
	/* if noDMA off, use DMAACC mode for memory write */
zwelch's avatar
zwelch committed
828
	if (ejtag_info->impcode & EJTAG_IMP_NODMA)
829
		return mips32_pracc_write_mem(ejtag_info, address, size, count, (void *)buffer);
zwelch's avatar
zwelch committed
830
	else
831
		return mips32_dmaacc_write_mem(ejtag_info, address, size, count, (void *)buffer);
ntfreak's avatar
ntfreak committed
832
833
834
835
836
}

int mips_m4k_register_commands(struct command_context_s *cmd_ctx)
{
	int retval;
837

ntfreak's avatar
ntfreak committed
838
839
840
841
842
843
844
	retval = mips32_register_commands(cmd_ctx);
	return retval;
}

int mips_m4k_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
{
	mips32_build_reg_cache(target);
845

ntfreak's avatar
ntfreak committed
846
847
848
	return ERROR_OK;
}

oharboe's avatar
oharboe committed
849
int mips_m4k_quit(void)
ntfreak's avatar
ntfreak committed
850
851
852
853
{
	return ERROR_OK;
}

854
int mips_m4k_init_arch_info(target_t *target, mips_m4k_common_t *mips_m4k, jtag_tap_t *tap)
ntfreak's avatar
ntfreak committed
855
856
{
	mips32_common_t *mips32 = &mips_m4k->mips32_common;
857

ntfreak's avatar
ntfreak committed
858
	mips_m4k->common_magic = MIPSM4K_COMMON_MAGIC;
859

ntfreak's avatar
ntfreak committed
860
	/* initialize mips4k specific info */
861
	mips32_init_arch_info(target, mips32, tap);
ntfreak's avatar
ntfreak committed
862
	mips32->arch_info = mips_m4k;
863

ntfreak's avatar
ntfreak committed
864
865
866
	return ERROR_OK;
}

867
int mips_m4k_target_create(struct target_s *target, Jim_Interp *interp)
ntfreak's avatar
ntfreak committed
868
{
869
	mips_m4k_common_t *mips_m4k = calloc(1,sizeof(mips_m4k_common_t));
870

871
	mips_m4k_init_arch_info(target, mips_m4k, target->tap);
872

ntfreak's avatar
ntfreak committed
873
874
875
	return ERROR_OK;
}

ntfreak's avatar
ntfreak committed
876
int mips_m4k_examine(struct target_s *target)
ntfreak's avatar
ntfreak committed
877
878
879
880
{
	int retval;
	mips32_common_t *mips32 = target->arch_info;
	mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
881
	uint32_t idcode = 0;
882

883
	if (!target_was_examined(target))
ntfreak's avatar
ntfreak committed
884
	{
885
		mips_ejtag_get_idcode(ejtag_info, &idcode);
886
887
		ejtag_info->idcode = idcode;
		
888
889
890
891
892
893
894
		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
895
	}
896

ntfreak's avatar
ntfreak committed
897
898
899
	/* init rest of ejtag interface */
	if ((retval = mips_ejtag_init(ejtag_info)) != ERROR_OK)
		return retval;
900

901
902
	if ((retval = mips32_examine(target)) != ERROR_OK)
		return retval;
903

ntfreak's avatar
ntfreak committed
904
905
906
	return ERROR_OK;
}

907
int mips_m4k_bulk_write_memory(target_t *target, uint32_t address, uint32_t count, uint8_t *buffer)
ntfreak's avatar
ntfreak committed
908
909
910
{
	return mips_m4k_write_memory(target, address, 4, count, buffer);
}
911

912
int mips_m4k_checksum_memory(target_t *target, uint32_t address, uint32_t size, uint32_t *checksum)
913
914
915
{
	return ERROR_FAIL; /* use bulk read method */
}