arm7_9_common.c 82.2 KB
Newer Older
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2005 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
5
 *   Copyright (C) 2007,2008 yvind Harboe                                 *
6
7
 *   oyvind.harboe@zylin.com                                               *
 *                                                                         *
8
9
10
 *   Copyright (C) 2008 by Spencer Oliver                                  *
 *   spen@spen-soft.co.uk                                                  *
 *                                                                         *
11
12
13
 *   Copyright (C) 2008 by Hongtao Zheng                                   *
 *   hontor@126.com                                                        *
 *                                                                         *
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *   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.             *
 ***************************************************************************/
29
#ifdef HAVE_CONFIG_H
30
#include "config.h"
31
32
33
#endif

#include "replacements.h"
34
35
36

#include "embeddedice.h"
#include "target.h"
37
#include "target_request.h"
38
39
40
41
42
43
#include "armv4_5.h"
#include "arm_jtag.h"
#include "jtag.h"
#include "log.h"
#include "arm7_9_common.h"
#include "breakpoints.h"
oharboe's avatar
oharboe committed
44
#include "time_support.h"
45
#include "arm_simulator.h"
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>

int arm7_9_debug_entry(target_t *target);
int arm7_9_enable_sw_bkpts(struct target_s *target);

/* command handler forward declarations */
int handle_arm7_9_write_xpsr_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int handle_arm7_9_write_xpsr_im8_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int handle_arm7_9_read_core_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int handle_arm7_9_write_core_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int handle_arm7_9_dbgrq_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
65
int handle_arm7_9_fast_memory_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
66
int handle_arm7_9_dcc_downloads_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
67
int handle_arm7_9_etm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68

69

70
71
72
73
74
static int arm7_9_clear_watchpoints(arm7_9_common_t *arm7_9)
{
	embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
	embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
	arm7_9->sw_breakpoints_added = 0;
75
	arm7_9->wp0_used = 0;
76
77
	arm7_9->wp1_used = arm7_9->wp1_used_default;
	arm7_9->wp_available = arm7_9->wp_available_max;
78

79
80
81
82
83
84
85
	return jtag_execute_queue();
}

/* set up embedded ice registers */
static int arm7_9_set_software_breakpoints(arm7_9_common_t *arm7_9)
{
	if (arm7_9->sw_breakpoints_added)
86
	{
87
88
89
90
91
92
93
94
		return ERROR_OK;
	}
	if (arm7_9->wp_available < 1)
	{
		LOG_WARNING("can't enable sw breakpoints with no watchpoint unit available");
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
	}
	arm7_9->wp_available--;
oharboe's avatar
oharboe committed
95

96
97
98
99
100
101
102
103
104
105
106
107
108
109
	/* pick a breakpoint unit */
	if (!arm7_9->wp0_used)
	{
		arm7_9->sw_breakpoints_added=1;
		arm7_9->wp0_used = 3;
	} else if (!arm7_9->wp1_used)
	{
		arm7_9->sw_breakpoints_added=2;
		arm7_9->wp1_used = 3;
	}
	else
	{
		LOG_ERROR("BUG: both watchpoints used, but wp_available >= 1");
		return ERROR_FAIL;
110
	}
111

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
	if (arm7_9->sw_breakpoints_added==1)
	{
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_VALUE], arm7_9->arm_bkpt);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0x0);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffffu);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
	}
	else if (arm7_9->sw_breakpoints_added==2)
	{
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_VALUE], arm7_9->arm_bkpt);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0x0);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0xffffffffu);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
	}
	else
129
	{
130
131
		LOG_ERROR("BUG: both watchpoints used, but wp_available >= 1");
		return ERROR_FAIL;
132
	}
133

134
	return jtag_execute_queue();
135
136
}

137
138
/* set things up after a reset / on startup */
int arm7_9_setup(target_t *target)
139
{
140
141
142
143
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;

	return arm7_9_clear_watchpoints(arm7_9);
144
145
}

146

147
148
149
150
int arm7_9_get_arch_pointers(target_t *target, armv4_5_common_t **armv4_5_p, arm7_9_common_t **arm7_9_p)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
151

152
153
154
155
	if (armv4_5->common_magic != ARMV4_5_COMMON_MAGIC)
	{
		return -1;
	}
156

157
158
159
160
	if (arm7_9->common_magic != ARM7_9_COMMON_MAGIC)
	{
		return -1;
	}
161

162
163
	*armv4_5_p = armv4_5;
	*arm7_9_p = arm7_9;
164

165
166
167
	return ERROR_OK;
}

168
169
170
/* we set up the breakpoint even if it is already set. Some action, e.g. reset
 * might have erased the values in embedded ice
 */
171
172
173
174
int arm7_9_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
175
	int retval=ERROR_OK;
176

177
178
	if (target->state != TARGET_HALTED)
	{
179
		LOG_WARNING("target not halted");
180
181
		return ERROR_TARGET_NOT_HALTED;
	}
182

183
184
185
186
	if (breakpoint->type == BKPT_HARD)
	{
		/* either an ARM (4 byte) or Thumb (2 byte) breakpoint */
		u32 mask = (breakpoint->length == 4) ? 0x3u : 0x1u;
187
		if (breakpoint->set==1)
188
189
190
191
192
193
194
		{
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], breakpoint->address);
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], mask);
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffffu);
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
		}
195
		else if (breakpoint->set==2)
196
197
198
199
200
201
202
203
204
		{
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], breakpoint->address);
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], mask);
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffffu);
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
		}
		else
		{
205
			LOG_ERROR("BUG: no hardware comparator available");
206
207
			return ERROR_OK;
		}
208
209

		retval=jtag_execute_queue();
210
211
212
	}
	else if (breakpoint->type == BKPT_SOFT)
	{
213
214
		if ((retval=arm7_9_set_software_breakpoints(arm7_9))!=ERROR_OK)
			return retval;
oharboe's avatar
oharboe committed
215

216
217
218
		/* did we already set this breakpoint? */
		if (breakpoint->set)
			return ERROR_OK;
oharboe's avatar
oharboe committed
219

220
221
		if (breakpoint->length == 4)
		{
ntfreak's avatar
ntfreak committed
222
			u32 verify = 0xffffffff;
223
			/* keep the original instruction in target endianness */
224
225
226
227
			if ((retval = target->type->read_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
			{
				return retval;
			}
drath's avatar
drath committed
228
			/* write the breakpoint instruction in target endianness (arm7_9->arm_bkpt is host endian) */
229
230
231
232
			if ((retval = target_write_u32(target, breakpoint->address, arm7_9->arm_bkpt)) != ERROR_OK)
			{
				return retval;
			}
233

234
			if ((retval = target_read_u32(target, breakpoint->address, &verify)) != ERROR_OK)
235
236
237
			{
				return retval;
			}
ntfreak's avatar
ntfreak committed
238
239
			if (verify != arm7_9->arm_bkpt)
			{
240
				LOG_ERROR("Unable to set 32 bit software breakpoint at address %08x - check that memory is read/writable", breakpoint->address);
ntfreak's avatar
ntfreak committed
241
242
				return ERROR_OK;
			}
243
244
245
		}
		else
		{
ntfreak's avatar
ntfreak committed
246
			u16 verify = 0xffff;
247
			/* keep the original instruction in target endianness */
248
249
250
251
			if ((retval = target->type->read_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
			{
				return retval;
			}
drath's avatar
drath committed
252
			/* write the breakpoint instruction in target endianness (arm7_9->thumb_bkpt is host endian) */
253
254
255
256
			if ((retval = target_write_u16(target, breakpoint->address, arm7_9->thumb_bkpt)) != ERROR_OK)
			{
				return retval;
			}
257

258
			if ((retval = target_read_u16(target, breakpoint->address, &verify)) != ERROR_OK)
259
260
261
			{
				return retval;
			}
ntfreak's avatar
ntfreak committed
262
263
			if (verify != arm7_9->thumb_bkpt)
			{
264
				LOG_ERROR("Unable to set thumb software breakpoint at address %08x - check that memory is read/writable", breakpoint->address);
ntfreak's avatar
ntfreak committed
265
266
				return ERROR_OK;
			}
267
268
269
270
		}
		breakpoint->set = 1;
	}

271
	return retval;
272
273
274
275
276

}

int arm7_9_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
{
277
278
	int retval = ERROR_OK;

279
280
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
281

282
283
	if (!breakpoint->set)
	{
284
		LOG_WARNING("breakpoint not set");
285
286
		return ERROR_OK;
	}
287

288
289
290
291
292
293
294
295
296
297
298
299
	if (breakpoint->type == BKPT_HARD)
	{
		if (breakpoint->set == 1)
		{
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
			arm7_9->wp0_used = 0;
		}
		else if (breakpoint->set == 2)
		{
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
			arm7_9->wp1_used = 0;
		}
300
		retval = jtag_execute_queue();
301
302
303
304
		breakpoint->set = 0;
	}
	else
	{
305
		/* restore original instruction (kept in target endianness) */
306
307
		if (breakpoint->length == 4)
		{
308
309
			u32 current_instr;
			/* check that user program as not modified breakpoint instruction */
310
311
312
313
			if ((retval = target->type->read_memory(target, breakpoint->address, 4, 1, (u8*)&current_instr)) != ERROR_OK)
			{
				return retval;
			}
314
			if (current_instr==arm7_9->arm_bkpt)
315
316
317
318
				if ((retval = target->type->write_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
				{
					return retval;
				}
319
320
321
		}
		else
		{
322
323
			u16 current_instr;
			/* check that user program as not modified breakpoint instruction */
324
325
326
327
			if ((retval = target->type->read_memory(target, breakpoint->address, 2, 1, (u8*)&current_instr)) != ERROR_OK)
			{
				return retval;
			}
328
			if (current_instr==arm7_9->thumb_bkpt)
329
330
331
332
				if ((retval = target->type->write_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
				{
					return retval;
				}
333
334
335
336
		}
		breakpoint->set = 0;
	}

337
	return retval;
338
339
}

340
int arm7_9_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
341
342
343
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
344

345
346
	if (target->state != TARGET_HALTED)
	{
347
		LOG_WARNING("target not halted");
348
349
		return ERROR_TARGET_NOT_HALTED;
	}
oharboe's avatar
oharboe committed
350

351
	if (arm7_9->breakpoint_count==0)
352
	{
oharboe's avatar
oharboe committed
353
354
		/* make sure we don't have any dangling breakpoints. This is vital upon
		 * GDB connect/disconnect
355
		 */
oharboe's avatar
oharboe committed
356
		arm7_9_clear_watchpoints(arm7_9);
357
	}
358

359
	if ((breakpoint->type == BKPT_HARD) && (arm7_9->wp_available < 1))
360
	{
361
		LOG_INFO("no watchpoint unit available for hardware breakpoint");
362
363
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
	}
364

365
	if ((breakpoint->length != 2) && (breakpoint->length != 4))
366
	{
367
		LOG_INFO("only breakpoints of two (Thumb) or four (ARM) bytes length supported");
368
369
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
	}
370

371
	if (breakpoint->type == BKPT_HARD)
372
	{
373
		arm7_9->wp_available--;
oharboe's avatar
oharboe committed
374

375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
		if (!arm7_9->wp0_used)
		{
			arm7_9->wp0_used = 1;
			breakpoint->set = 1;
		}
		else if (!arm7_9->wp1_used)
		{
			arm7_9->wp1_used = 1;
			breakpoint->set = 2;
		}
		else
		{
			LOG_ERROR("BUG: no hardware comparator available");
		}
	}
oharboe's avatar
oharboe committed
390

391

392
	arm7_9->breakpoint_count++;
oharboe's avatar
oharboe committed
393

394
	return arm7_9_set_breakpoint(target, breakpoint);
395
396
397
398
}

int arm7_9_remove_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
{
399
	int retval = ERROR_OK;
400
401
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
402

403
404
405
406
	if((retval = arm7_9_unset_breakpoint(target, breakpoint)) != ERROR_OK)
	{
		return retval;
	}
407

408
409
	if (breakpoint->type == BKPT_HARD)
		arm7_9->wp_available++;
oharboe's avatar
oharboe committed
410

411
412
413
414
	arm7_9->breakpoint_count--;
	if (arm7_9->breakpoint_count==0)
	{
		/* make sure we don't have any dangling breakpoints */
415
416
417
418
		if((retval = arm7_9_clear_watchpoints(arm7_9)) != ERROR_OK)
		{
			return retval;
		}
419
	}
420

421
422
423
424
425
	return ERROR_OK;
}

int arm7_9_set_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
{
426
	int retval = ERROR_OK;
427
428
429
430
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
	int rw_mask = 1;
	u32 mask;
431

432
	mask = watchpoint->length - 1;
433

434
435
	if (target->state != TARGET_HALTED)
	{
436
		LOG_WARNING("target not halted");
437
438
		return ERROR_TARGET_NOT_HALTED;
	}
439

440
441
442
443
	if (watchpoint->rw == WPT_ACCESS)
		rw_mask = 0;
	else
		rw_mask = 1;
444

445
446
447
448
449
450
451
452
453
454
	if (!arm7_9->wp0_used)
	{
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], watchpoint->address);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], mask);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], watchpoint->mask);
		if( watchpoint->mask != 0xffffffffu )
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_VALUE], watchpoint->value);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xff & ~EICE_W_CTRL_nOPC & ~rw_mask);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE | EICE_W_CTRL_nOPC | (watchpoint->rw & 1));

455
456
457
458
		if((retval = jtag_execute_queue()) != ERROR_OK)
		{
			return retval;
		}
459
460
461
462
463
464
465
466
467
468
469
470
471
		watchpoint->set = 1;
		arm7_9->wp0_used = 2;
	}
	else if (!arm7_9->wp1_used)
	{
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], watchpoint->address);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], mask);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], watchpoint->mask);
		if( watchpoint->mask != 0xffffffffu )
			embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_VALUE], watchpoint->value);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], 0xff & ~EICE_W_CTRL_nOPC & ~rw_mask);
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE | EICE_W_CTRL_nOPC | (watchpoint->rw & 1));

472
473
474
475
		if((retval = jtag_execute_queue()) != ERROR_OK)
		{
			return retval;
		}
476
477
		watchpoint->set = 2;
		arm7_9->wp1_used = 2;
478
	}
479
480
	else
	{
481
		LOG_ERROR("BUG: no hardware comparator available");
482
483
		return ERROR_OK;
	}
484

485
486
487
488
489
	return ERROR_OK;
}

int arm7_9_unset_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
{
490
	int retval = ERROR_OK;
491
492
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
493

494
495
	if (target->state != TARGET_HALTED)
	{
496
		LOG_WARNING("target not halted");
497
498
		return ERROR_TARGET_NOT_HALTED;
	}
499

500
501
	if (!watchpoint->set)
	{
502
		LOG_WARNING("breakpoint not set");
503
504
		return ERROR_OK;
	}
505

506
507
508
	if (watchpoint->set == 1)
	{
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
509
510
511
512
		if((retval = jtag_execute_queue()) != ERROR_OK)
		{
			return retval;
		}
513
514
515
516
517
		arm7_9->wp0_used = 0;
	}
	else if (watchpoint->set == 2)
	{
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
518
519
520
521
		if((retval = jtag_execute_queue()) != ERROR_OK)
		{
			return retval;
		}
522
523
524
525
526
527
528
		arm7_9->wp1_used = 0;
	}
	watchpoint->set = 0;

	return ERROR_OK;
}

529
int arm7_9_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
530
531
532
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
533

534
535
	if (target->state != TARGET_HALTED)
	{
536
		LOG_WARNING("target not halted");
537
538
		return ERROR_TARGET_NOT_HALTED;
	}
539

540
541
542
543
	if (arm7_9->wp_available < 1)
	{
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
	}
544

545
	if ((watchpoint->length != 1) && (watchpoint->length != 2) && (watchpoint->length != 4))
546
547
548
	{
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
	}
549

550
	arm7_9->wp_available--;
551

552
553
554
555
556
	return ERROR_OK;
}

int arm7_9_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
{
557
	int retval = ERROR_OK;
558
559
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
560

561
562
	if (watchpoint->set)
	{
563
564
565
566
		if((retval = arm7_9_unset_watchpoint(target, watchpoint)) != ERROR_OK)
		{
			return retval;
		}
567
	}
568

569
	arm7_9->wp_available++;
570

571
572
573
	return ERROR_OK;
}

574
575


576
577
578
579

int arm7_9_execute_sys_speed(struct target_s *target)
{
	int retval;
580

581
582
583
584
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
	arm_jtag_t *jtag_info = &arm7_9->jtag_info;
	reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
585

586
587
	/* set RESTART instruction */
	jtag_add_end_state(TAP_RTI);
oharboe's avatar
oharboe committed
588
589
590
591
	if (arm7_9->need_bypass_before_restart) {
		arm7_9->need_bypass_before_restart = 0;
		arm_jtag_set_instr(jtag_info, 0xf, NULL);
	}
592
	arm_jtag_set_instr(jtag_info, 0x4, NULL);
593

oharboe's avatar
oharboe committed
594
595
596
	long long then=timeval_ms();
	int timeout;
	while (!(timeout=((timeval_ms()-then)>1000)))
597
598
599
600
601
602
603
604
	{
		/* read debug status register */
		embeddedice_read_reg(dbg_stat);
		if ((retval = jtag_execute_queue()) != ERROR_OK)
			return retval;
		if ((buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
				   && (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_SYSCOMP, 1)))
			break;
oharboe's avatar
oharboe committed
605
606
607
608
609
610
611
		if (debug_level>=3)
		{
			alive_sleep(100);
		} else
		{
			keep_alive();
		}
612
	}
oharboe's avatar
oharboe committed
613
	if (timeout)
614
	{
615
		LOG_ERROR("timeout waiting for SYSCOMP & DBGACK, last DBG_STATUS: %x", buf_get_u32(dbg_stat->value, 0, dbg_stat->size));
616
617
		return ERROR_TARGET_TIMEOUT;
	}
618

619
620
621
622
623
	return ERROR_OK;
}

int arm7_9_execute_fast_sys_speed(struct target_s *target)
{
mifi's avatar
mifi committed
624
625
	static int set=0;
	static u8 check_value[4], check_mask[4];
626

627
628
629
630
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
	arm_jtag_t *jtag_info = &arm7_9->jtag_info;
	reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
631

632
633
	/* set RESTART instruction */
	jtag_add_end_state(TAP_RTI);
oharboe's avatar
oharboe committed
634
635
636
637
	if (arm7_9->need_bypass_before_restart) {
		arm7_9->need_bypass_before_restart = 0;
		arm_jtag_set_instr(jtag_info, 0xf, NULL);
	}
638
	arm_jtag_set_instr(jtag_info, 0x4, NULL);
639

mifi's avatar
mifi committed
640
641
642
	if (!set)
	{
		/* check for DBGACK and SYSCOMP set (others don't care) */
643

mifi's avatar
mifi committed
644
645
646
647
648
649
650
651
		/* NB! These are constants that must be available until after next jtag_execute() and
		   we evaluate the values upon first execution in lieu of setting up these constants
		   during early setup.
		*/
		buf_set_u32(check_value, 0, 32, 0x9);
		buf_set_u32(check_mask, 0, 32, 0x9);
		set=1;
	}
652

653
654
655
656
657
658
	/* read debug status register */
	embeddedice_read_reg_w_check(dbg_stat, check_value, check_value);

	return ERROR_OK;
}

659
660
661
662
663
664
int arm7_9_target_request_data(target_t *target, u32 size, u8 *buffer)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
	arm_jtag_t *jtag_info = &arm7_9->jtag_info;
	u32 *data;
665
	int i, retval = ERROR_OK;
666

667
	data = malloc(size * (sizeof(u32)));
668

669
	retval = embeddedice_receive(jtag_info, data, size);
670

671
672
673
674
	for (i = 0; i < size; i++)
	{
		h_u32_to_le(buffer + (i * 4), data[i]);
	}
675

676
	free(data);
677

678
	return retval;
679
680
681
682
}

int arm7_9_handle_target_request(void *priv)
{
683
	int retval = ERROR_OK;
684
	target_t *target = priv;
685
686
	if (!target->type->examined)
		return ERROR_OK;
687
688
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
689
	arm_jtag_t *jtag_info = &arm7_9->jtag_info;
690
	reg_t *dcc_control = &arm7_9->eice_cache->reg_list[EICE_COMMS_CTRL];
691
692


693
694
	if (!target->dbg_msg_enabled)
		return ERROR_OK;
695

696
697
698
699
	if (target->state == TARGET_RUNNING)
	{
		/* read DCC control register */
		embeddedice_read_reg(dcc_control);
700
701
702
703
		if ((retval = jtag_execute_queue()) != ERROR_OK)
		{
			return retval;
		}
704

705
706
707
708
		/* check W bit */
		if (buf_get_u32(dcc_control->value, 1, 1) == 1)
		{
			u32 request;
709

710
711
712
713
714
715
716
717
			if ((retval = embeddedice_receive(jtag_info, &request, 1)) != ERROR_OK)
			{
				return retval;
			}
			if ((retval = target_request(target, request)) != ERROR_OK)
			{
				return retval;
			}
718
719
		}
	}
720

721
722
723
	return ERROR_OK;
}

724
int arm7_9_poll(target_t *target)
725
726
727
728
729
730
731
732
733
734
{
	int retval;
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
	reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];

	/* read debug status register */
	embeddedice_read_reg(dbg_stat);
	if ((retval = jtag_execute_queue()) != ERROR_OK)
	{
735
		return retval;
736
	}
737

738
739
	if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
	{
740
/*		LOG_DEBUG("DBGACK set, dbg_state->value: 0x%x", buf_get_u32(dbg_stat->value, 0, 32));*/
741
		if (target->state == TARGET_UNKNOWN)
742
		{
743
			target->state = TARGET_RUNNING;
744
			LOG_WARNING("DBGACK set while target was in unknown state. Reset or initialize target.");
745
746
747
		}
		if ((target->state == TARGET_RUNNING) || (target->state == TARGET_RESET))
		{
748
749
750
			int check_pc=0;
			if (target->state == TARGET_RESET)
			{
oharboe's avatar
   
oharboe committed
751
				if (target->reset_halt)
752
753
754
755
756
757
758
				{
					if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
					{
						check_pc = 1;
					}
				}
			}
759

760
			target->state = TARGET_HALTED;
761

762
763
			if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
				return retval;
764

765
766
767
768
769
770
			if (check_pc)
			{
				reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
				u32 t=*((u32 *)reg->value);
				if (t!=0)
				{
771
					LOG_ERROR("PC was not 0. Does this target need srst_pulls_trst?");
772
773
				}
			}
774

775
776
777
778
			if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
			{
				return retval;
			}
779
780
781
782
783
784
		}
		if (target->state == TARGET_DEBUG_RUNNING)
		{
			target->state = TARGET_HALTED;
			if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
				return retval;
785

786
787
788
789
			if ((retval = target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED)) != ERROR_OK)
			{
				return retval;
			}
790
		}
791
792
		if (target->state != TARGET_HALTED)
		{
793
			LOG_WARNING("DBGACK set, but the target did not end up in the halted stated %d", target->state);
794
		}
795
	}
796
797
798
799
800
	else
	{
		if (target->state != TARGET_DEBUG_RUNNING)
			target->state = TARGET_RUNNING;
	}
801

802
	return ERROR_OK;
803
804
}

805
806
807
/*
  Some -S targets (ARM966E-S in the STR912 isn't affected, ARM926EJ-S
  in the LPC3180 and AT91SAM9260 is affected) completely stop the JTAG clock
oharboe's avatar
   
oharboe committed
808
  while the core is held in reset(SRST). It isn't possible to program the halt
809
810
811
812
  condition once reset was asserted, hence a hook that allows the target to set
  up its reset-halt condition prior to asserting reset.
*/

813
814
int arm7_9_assert_reset(target_t *target)
{
815
816
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
oharboe's avatar
oharboe committed
817
	LOG_DEBUG("target->state: %s",
818
		  Jim_Nvp_value2name_simple( nvp_target_state,target->state)->name);
819

820
821
822
823
824
	if (!(jtag_reset_config & RESET_HAS_SRST))
	{
		LOG_ERROR("Can't assert SRST");
		return ERROR_FAIL;
	}
oharboe's avatar
   
oharboe committed
825

oharboe's avatar
   
oharboe committed
826
	if (target->reset_halt)
827
	{
oharboe's avatar
   
oharboe committed
828
829
830
		/*
		 * Some targets do not support communication while SRST is asserted. We need to
		 * set up the reset vector catch here.
831
		 *
oharboe's avatar
   
oharboe committed
832
		 * If TRST is asserted, then these settings will be reset anyway, so setting them
833
		 * here is harmless.
oharboe's avatar
   
oharboe committed
834
835
836
837
838
839
840
841
842
		 */
		if (arm7_9->has_vector_catch)
		{
			/* program vector catch register to catch reset vector */
			embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_VEC_CATCH], 0x1);
		}
		else
		{
			/* program watchpoint unit to match on reset vector address */
843
			embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], 0x0);
oharboe's avatar
   
oharboe committed
844
			embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0x3);
845
846
847
			embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
			embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
			embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
oharboe's avatar
   
oharboe committed
848
		}
849
850
	}

oharboe's avatar
   
oharboe committed
851
	/* here we should issue a srst only, but we may have to assert trst as well */
852
	if (jtag_reset_config & RESET_SRST_PULLS_TRST)
853
	{
854
855
856
857
		jtag_add_reset(1, 1);
	} else
	{
		jtag_add_reset(0, 1);
858
	}
859

oharboe's avatar
   
oharboe committed
860

861
862
	target->state = TARGET_RESET;
	jtag_add_sleep(50000);
oharboe's avatar
   
oharboe committed
863

864
865
	armv4_5_invalidate_core_regs(target);

866
867
868
869
870
    if ((target->reset_halt)&&((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
	{
		/* debug entry was already prepared in arm7_9_assert_reset() */
		target->debug_reason = DBG_REASON_DBGRQ;
	}
oharboe's avatar
oharboe committed
871

872
873
874
875
876
877
	return ERROR_OK;

}

int arm7_9_deassert_reset(target_t *target)
{
878
	int retval=ERROR_OK;
oharboe's avatar
oharboe committed
879
	LOG_DEBUG("target->state: %s",
880
881
		  Jim_Nvp_value2name_simple( nvp_target_state,target->state)->name);

882

883
884
	/* deassert reset lines */
	jtag_add_reset(0, 0);
885

886
	if (target->reset_halt&&(jtag_reset_config & RESET_SRST_PULLS_TRST)!=0)
887
	{
888
		LOG_WARNING("srst pulls trst - can not reset into halted mode. Issuing halt after reset.");
889
890
891
		/* set up embedded ice registers again */
		if ((retval=target->type->examine(target))!=ERROR_OK)
			return retval;
892
893
894
895
896

		if ((retval=target_poll(target))!=ERROR_OK)
		{
			return retval;
		}
oharboe's avatar
oharboe committed
897

898
899
900
901
		if ((retval=target_halt(target))!=ERROR_OK)
		{
			return retval;
		}
oharboe's avatar
oharboe committed
902

903
904
	}
	return retval;
905
906
}

907
908
909
910
911
int arm7_9_clear_halt(target_t *target)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
	reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
912

913
914
	/* we used DBGRQ only if we didn't come out of reset */
	if (!arm7_9->debug_entry_from_reset && arm7_9->use_dbgrq)
915
916
917
	{
		/* program EmbeddedICE Debug Control Register to deassert DBGRQ
		 */
918
		buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
919
920
921
922
		embeddedice_store_reg(dbg_ctrl);
	}
	else
	{
923
		if (arm7_9->debug_entry_from_reset && arm7_9->has_vector_catch)
924
		{
925
926
927
928
929
930
931
932
933
934
935
936
			/* if we came out of reset, and vector catch is supported, we used
			 * vector catch to enter debug state
			 * restore the register in that case
			 */
			embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_VEC_CATCH]);
		}
		else
		{
			/* restore registers if watchpoint unit 0 was in use
			 */
			if (arm7_9->wp0_used)
			{
937
938
939
940
				if (arm7_9->debug_entry_from_reset)
				{
					embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE]);
				}
941
942
943
944
				embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK]);
				embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK]);
				embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK]);
			}
945
			/* control value always has to be restored, as it was either disabled,
946
947
948
			 * or enabled with possibly different bits
			 */
			embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
949
950
		}
	}
951

952
953
954
	return ERROR_OK;
}

955
956
957
958
959
int arm7_9_soft_reset_halt(struct target_s *target)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
	reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
960
	reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
961
	int i;
oharboe's avatar
   
oharboe committed
962
	int retval;
963

964
	if ((retval=target_halt(target))!=ERROR_OK)
oharboe's avatar
   
oharboe committed
965
		return retval;
966

oharboe's avatar
oharboe committed
967
968
969
	long long then=timeval_ms();
	int timeout;
	while (!(timeout=((timeval_ms()-then)>1000)))
970
	{
oharboe's avatar
   
oharboe committed
971
972
		if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) != 0)
			break;
973
		embeddedice_read_reg(dbg_stat);
oharboe's avatar
   
oharboe committed
974
975
		if ((retval=jtag_execute_queue())!=ERROR_OK)
			return retval;
oharboe's avatar
oharboe committed
976
977
978
979
980
981
982
		if (debug_level>=3)
		{
			alive_sleep(100);
		} else
		{
			keep_alive();
		}
oharboe's avatar
   
oharboe committed
983
	}
oharboe's avatar
oharboe committed
984
	if (timeout)
oharboe's avatar
   
oharboe committed
985
	{
986
		LOG_ERROR("Failed to halt CPU after 1 sec");
oharboe's avatar
   
oharboe committed
987
		return ERROR_TARGET_TIMEOUT;
988
989
	}
	target->state = TARGET_HALTED;
990

991
992
993
994
995
996
997
	/* program EmbeddedICE Debug Control Register to assert DBGACK and INTDIS
	 * ensure that DBGRQ is cleared
	 */
	buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
	buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
	buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 1);
	embeddedice_store_reg(dbg_ctrl);
998

999
1000
	if ((retval = arm7_9_clear_halt(target)) != ERROR_OK)
	{
For faster browsing, not all history is shown. View entire blame