arm7_9_common.c 76.4 KB
Newer Older
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2005 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
5
6
7
 *   Copyright (C) 2007,2008 yvind Harboe                                      *
 *   oyvind.harboe@zylin.com                                               *
 *                                                                         *
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *   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.             *
 ***************************************************************************/
23
#ifdef HAVE_CONFIG_H
24
#include "config.h"
25
26
27
#endif

#include "replacements.h"
28
29
30

#include "embeddedice.h"
#include "target.h"
31
#include "target_request.h"
32
33
34
35
36
37
#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
38
#include "time_support.h"
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#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);
58
int handle_arm7_9_fast_memory_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
59
int handle_arm7_9_dcc_downloads_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
60
int handle_arm7_9_etm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
61

62

63
64
65
66
67
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;
68
	arm7_9->wp0_used = 0;
69
70
	arm7_9->wp1_used = arm7_9->wp1_used_default;
	arm7_9->wp_available = arm7_9->wp_available_max;
71

72
73
74
75
76
77
78
	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)
79
	{
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
		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--;
	
	/* 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;
103
	}
104

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	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
122
	{
123
124
		LOG_ERROR("BUG: both watchpoints used, but wp_available >= 1");
		return ERROR_FAIL;
125
	}
126

127
	return jtag_execute_queue();
128
129
}

130
131
/* set things up after a reset / on startup */
int arm7_9_setup(target_t *target)
132
{
133
134
135
136
	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);
137
138
}

139

140
141
142
143
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;
144

145
146
147
148
	if (armv4_5->common_magic != ARMV4_5_COMMON_MAGIC)
	{
		return -1;
	}
149

150
151
152
153
	if (arm7_9->common_magic != ARM7_9_COMMON_MAGIC)
	{
		return -1;
	}
154

155
156
	*armv4_5_p = armv4_5;
	*arm7_9_p = arm7_9;
157

158
159
160
	return ERROR_OK;
}

161
162
163
/* we set up the breakpoint even if it is already set. Some action, e.g. reset
 * might have erased the values in embedded ice
 */
164
165
166
167
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;
168
	int retval=ERROR_OK;
169

170
171
	if (target->state != TARGET_HALTED)
	{
172
		LOG_WARNING("target not halted");
173
174
		return ERROR_TARGET_NOT_HALTED;
	}
175

176
177
178
179
	if (breakpoint->type == BKPT_HARD)
	{
		/* either an ARM (4 byte) or Thumb (2 byte) breakpoint */
		u32 mask = (breakpoint->length == 4) ? 0x3u : 0x1u;
180
		if (breakpoint->set==1)
181
182
183
184
185
186
187
		{
			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);
		}
188
		else if (breakpoint->set==2)
189
190
191
192
193
194
195
196
197
		{
			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
		{
198
			LOG_ERROR("BUG: no hardware comparator available");
199
200
			return ERROR_OK;
		}
201
202

		retval=jtag_execute_queue();
203
204
205
	}
	else if (breakpoint->type == BKPT_SOFT)
	{
206
207
208
209
210
211
212
		if ((retval=arm7_9_set_software_breakpoints(arm7_9))!=ERROR_OK)
			return retval;
		
		/* did we already set this breakpoint? */
		if (breakpoint->set)
			return ERROR_OK;
		
213
214
		if (breakpoint->length == 4)
		{
ntfreak's avatar
ntfreak committed
215
			u32 verify = 0xffffffff;
216
			/* keep the original instruction in target endianness */
217
			target->type->read_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr);
drath's avatar
drath committed
218
			/* write the breakpoint instruction in target endianness (arm7_9->arm_bkpt is host endian) */
219
			target_write_u32(target, breakpoint->address, arm7_9->arm_bkpt);
220

ntfreak's avatar
ntfreak committed
221
222
223
			target->type->read_memory(target, breakpoint->address, 4, 1, (u8 *)&verify);
			if (verify != arm7_9->arm_bkpt)
			{
224
				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
225
226
				return ERROR_OK;
			}
227
228
229
		}
		else
		{
ntfreak's avatar
ntfreak committed
230
			u16 verify = 0xffff;
231
			/* keep the original instruction in target endianness */
232
			target->type->read_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr);
drath's avatar
drath committed
233
234
			/* write the breakpoint instruction in target endianness (arm7_9->thumb_bkpt is host endian) */
			target_write_u16(target, breakpoint->address, arm7_9->thumb_bkpt);
235

ntfreak's avatar
ntfreak committed
236
237
238
			target->type->read_memory(target, breakpoint->address, 2, 1, (u8 *)&verify);
			if (verify != arm7_9->thumb_bkpt)
			{
239
				LOG_ERROR("Unable to set thumb software breakpoint at address %08x - check that memory is read/writable", breakpoint->address);
ntfreak's avatar
ntfreak committed
240
241
				return ERROR_OK;
			}
242
243
244
245
		}
		breakpoint->set = 1;
	}

246
	return retval;
247
248
249
250
251
252
253

}

int arm7_9_unset_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;
254

255
256
	if (!breakpoint->set)
	{
257
		LOG_WARNING("breakpoint not set");
258
259
		return ERROR_OK;
	}
260

261
262
263
264
265
266
267
268
269
270
271
272
	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;
		}
273
		jtag_execute_queue();
274
275
276
277
		breakpoint->set = 0;
	}
	else
	{
278
		/* restore original instruction (kept in target endianness) */
279
280
		if (breakpoint->length == 4)
		{
281
282
283
284
285
			u32 current_instr;
			/* check that user program as not modified breakpoint instruction */
			target->type->read_memory(target, breakpoint->address, 4, 1, (u8*)&current_instr);
			if (current_instr==arm7_9->arm_bkpt)
				target->type->write_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr);
286
287
288
		}
		else
		{
289
290
291
292
293
			u16 current_instr;
			/* check that user program as not modified breakpoint instruction */
			target->type->read_memory(target, breakpoint->address, 2, 1, (u8*)&current_instr);
			if (current_instr==arm7_9->thumb_bkpt)
				target->type->write_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr);
294
295
296
297
298
299
300
		}
		breakpoint->set = 0;
	}

	return ERROR_OK;
}

301
int arm7_9_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
302
303
304
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
305

306
307
	if (target->state != TARGET_HALTED)
	{
308
		LOG_WARNING("target not halted");
309
310
		return ERROR_TARGET_NOT_HALTED;
	}
311
312
	
	if (arm7_9->breakpoint_count==0)
313
	{
314
315
316
317
		/* make sure we don't have any dangling breakpoints. This is vital upon 
		 * GDB connect/disconnect 
		 */
		arm7_9_clear_watchpoints(arm7_9);	
318
	}
319

320
	if ((breakpoint->type == BKPT_HARD) && (arm7_9->wp_available < 1))
321
	{
322
		LOG_INFO("no watchpoint unit available for hardware breakpoint");
323
324
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
	}
325

326
	if ((breakpoint->length != 2) && (breakpoint->length != 4))
327
	{
328
		LOG_INFO("only breakpoints of two (Thumb) or four (ARM) bytes length supported");
329
330
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
	}
331

332
	if (breakpoint->type == BKPT_HARD)
333
	{
334
		arm7_9->wp_available--;
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
		
		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");
		}
	}
	
352

353
354
355
	arm7_9->breakpoint_count++;
	
	return arm7_9_set_breakpoint(target, breakpoint);
356
357
358
359
360
361
}

int arm7_9_remove_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;
362

363
	arm7_9_unset_breakpoint(target, breakpoint);
364

365
366
	if (breakpoint->type == BKPT_HARD)
		arm7_9->wp_available++;
367
368
369
370
371
372
373
	
	arm7_9->breakpoint_count--;
	if (arm7_9->breakpoint_count==0)
	{
		/* make sure we don't have any dangling breakpoints */
		arm7_9_clear_watchpoints(arm7_9);	
	}
374

375
376
377
378
379
380
381
382
383
	return ERROR_OK;
}

int arm7_9_set_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
{
	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;
384

385
	mask = watchpoint->length - 1;
386

387
388
	if (target->state != TARGET_HALTED)
	{
389
		LOG_WARNING("target not halted");
390
391
		return ERROR_TARGET_NOT_HALTED;
	}
392

393
394
395
396
	if (watchpoint->rw == WPT_ACCESS)
		rw_mask = 0;
	else
		rw_mask = 1;
397

398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
	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));

		jtag_execute_queue();
		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));

		jtag_execute_queue();
		watchpoint->set = 2;
		arm7_9->wp1_used = 2;
425
	}
426
427
	else
	{
428
		LOG_ERROR("BUG: no hardware comparator available");
429
430
		return ERROR_OK;
	}
431

432
433
434
435
436
437
438
	return ERROR_OK;
}

int arm7_9_unset_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
439

440
441
	if (target->state != TARGET_HALTED)
	{
442
		LOG_WARNING("target not halted");
443
444
		return ERROR_TARGET_NOT_HALTED;
	}
445

446
447
	if (!watchpoint->set)
	{
448
		LOG_WARNING("breakpoint not set");
449
450
		return ERROR_OK;
	}
451

452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
	if (watchpoint->set == 1)
	{
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
		jtag_execute_queue();
		arm7_9->wp0_used = 0;
	}
	else if (watchpoint->set == 2)
	{
		embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
		jtag_execute_queue();
		arm7_9->wp1_used = 0;
	}
	watchpoint->set = 0;

	return ERROR_OK;
}

469
int arm7_9_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
470
471
472
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
473

474
475
	if (target->state != TARGET_HALTED)
	{
476
		LOG_WARNING("target not halted");
477
478
		return ERROR_TARGET_NOT_HALTED;
	}
479

480
481
482
483
	if (arm7_9->wp_available < 1)
	{
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
	}
484

485
	if ((watchpoint->length != 1) && (watchpoint->length != 2) && (watchpoint->length != 4))
486
487
488
	{
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
	}
489

490
	arm7_9->wp_available--;
491

492
493
494
495
496
497
498
	return ERROR_OK;
}

int arm7_9_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
499

500
501
502
503
	if (watchpoint->set)
	{
		arm7_9_unset_watchpoint(target, watchpoint);
	}
504

505
	arm7_9->wp_available++;
506

507
508
509
	return ERROR_OK;
}

510
511


512
513
514
515

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

517
518
519
520
	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];
521

522
523
	/* set RESTART instruction */
	jtag_add_end_state(TAP_RTI);
oharboe's avatar
oharboe committed
524
525
526
527
	if (arm7_9->need_bypass_before_restart) {
		arm7_9->need_bypass_before_restart = 0;
		arm_jtag_set_instr(jtag_info, 0xf, NULL);
	}
528
	arm_jtag_set_instr(jtag_info, 0x4, NULL);
529

oharboe's avatar
oharboe committed
530
531
532
	long long then=timeval_ms();
	int timeout;
	while (!(timeout=((timeval_ms()-then)>1000)))
533
534
535
536
537
538
539
540
	{
		/* 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
541
542
543
544
545
546
547
		if (debug_level>=3)
		{
			alive_sleep(100);
		} else
		{
			keep_alive();
		}
548
	}
oharboe's avatar
oharboe committed
549
	if (timeout)
550
	{
551
		LOG_ERROR("timeout waiting for SYSCOMP & DBGACK, last DBG_STATUS: %x", buf_get_u32(dbg_stat->value, 0, dbg_stat->size));
552
553
		return ERROR_TARGET_TIMEOUT;
	}
554

555
556
557
558
559
	return ERROR_OK;
}

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

563
564
565
566
	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];
567

568
569
	/* set RESTART instruction */
	jtag_add_end_state(TAP_RTI);
oharboe's avatar
oharboe committed
570
571
572
573
	if (arm7_9->need_bypass_before_restart) {
		arm7_9->need_bypass_before_restart = 0;
		arm_jtag_set_instr(jtag_info, 0xf, NULL);
	}
574
	arm_jtag_set_instr(jtag_info, 0x4, NULL);
575

mifi's avatar
mifi committed
576
577
578
	if (!set)
	{
		/* check for DBGACK and SYSCOMP set (others don't care) */
579

mifi's avatar
mifi committed
580
581
582
583
584
585
586
587
		/* 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;
	}
588

589
590
591
592
593
594
	/* read debug status register */
	embeddedice_read_reg_w_check(dbg_stat, check_value, check_value);

	return ERROR_OK;
}

595
596
597
598
599
600
601
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;
	int i;
602

603
	data = malloc(size * (sizeof(u32)));
604

605
	embeddedice_receive(jtag_info, data, size);
606

607
608
609
610
	for (i = 0; i < size; i++)
	{
		h_u32_to_le(buffer + (i * 4), data[i]);
	}
611

612
	free(data);
613

614
615
616
617
618
619
	return ERROR_OK;
}

int arm7_9_handle_target_request(void *priv)
{
	target_t *target = priv;
620
621
	if (!target->type->examined)
		return ERROR_OK;
622
623
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
624
	arm_jtag_t *jtag_info = &arm7_9->jtag_info;
625
	reg_t *dcc_control = &arm7_9->eice_cache->reg_list[EICE_COMMS_CTRL];
626
627


628
629
	if (!target->dbg_msg_enabled)
		return ERROR_OK;
630

631
632
633
634
635
	if (target->state == TARGET_RUNNING)
	{
		/* read DCC control register */
		embeddedice_read_reg(dcc_control);
		jtag_execute_queue();
636

637
638
639
640
		/* check W bit */
		if (buf_get_u32(dcc_control->value, 1, 1) == 1)
		{
			u32 request;
641

642
643
644
645
			embeddedice_receive(jtag_info, &request, 1);
			target_request(target, request);
		}
	}
646

647
648
649
	return ERROR_OK;
}

650
int arm7_9_poll(target_t *target)
651
652
653
654
655
656
657
658
659
660
{
	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)
	{
661
		return retval;
662
	}
663

664
665
	if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
	{
666
/*		LOG_DEBUG("DBGACK set, dbg_state->value: 0x%x", buf_get_u32(dbg_stat->value, 0, 32));*/
667
		if (target->state == TARGET_UNKNOWN)
668
		{
669
			target->state = TARGET_RUNNING;
670
			LOG_WARNING("DBGACK set while target was in unknown state. Reset or initialize target.");
671
672
673
		}
		if ((target->state == TARGET_RUNNING) || (target->state == TARGET_RESET))
		{
674
675
676
			int check_pc=0;
			if (target->state == TARGET_RESET)
			{
oharboe's avatar
   
oharboe committed
677
				if (target->reset_halt)
678
679
680
681
682
683
684
				{
					if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
					{
						check_pc = 1;
					}
				}
			}
685

686
			target->state = TARGET_HALTED;
687

688
689
			if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
				return retval;
690

691
692
693
694
695
696
			if (check_pc)
			{
				reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
				u32 t=*((u32 *)reg->value);
				if (t!=0)
				{
697
					LOG_ERROR("PC was not 0. Does this target need srst_pulls_trst?");
698
699
				}
			}
700

701
702
703
704
705
706
707
			target_call_event_callbacks(target, TARGET_EVENT_HALTED);
		}
		if (target->state == TARGET_DEBUG_RUNNING)
		{
			target->state = TARGET_HALTED;
			if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
				return retval;
708

709
710
			target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
		}
711
712
		if (target->state != TARGET_HALTED)
		{
713
			LOG_WARNING("DBGACK set, but the target did not end up in the halted stated %d", target->state);
714
		}
715
	}
716
717
718
719
720
	else
	{
		if (target->state != TARGET_DEBUG_RUNNING)
			target->state = TARGET_RUNNING;
	}
721

722
	return ERROR_OK;
723
724
}

725
726
727
/*
  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
728
  while the core is held in reset(SRST). It isn't possible to program the halt
729
730
731
732
  condition once reset was asserted, hence a hook that allows the target to set
  up its reset-halt condition prior to asserting reset.
*/

733
734
int arm7_9_assert_reset(target_t *target)
{
735
736
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
737
738
	LOG_DEBUG("target->state: %s", 
		  Jim_Nvp_value2name_simple( nvp_target_state,target->state)->name);
739

740
741
742
743
744
	if (!(jtag_reset_config & RESET_HAS_SRST))
	{
		LOG_ERROR("Can't assert SRST");
		return ERROR_FAIL;
	}
oharboe's avatar
   
oharboe committed
745

oharboe's avatar
   
oharboe committed
746
	if (target->reset_halt)
747
	{
oharboe's avatar
   
oharboe committed
748
749
750
		/*
		 * Some targets do not support communication while SRST is asserted. We need to
		 * set up the reset vector catch here.
751
		 *
oharboe's avatar
   
oharboe committed
752
		 * If TRST is asserted, then these settings will be reset anyway, so setting them
753
		 * here is harmless.
oharboe's avatar
   
oharboe committed
754
755
756
757
758
759
760
761
762
		 */
		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 */
763
			embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], 0x0);
oharboe's avatar
   
oharboe committed
764
			embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0x3);
765
766
767
			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
768
		}
769
770
	}

oharboe's avatar
   
oharboe committed
771
	/* here we should issue a srst only, but we may have to assert trst as well */
772
	if (jtag_reset_config & RESET_SRST_PULLS_TRST)
773
	{
774
775
776
777
		jtag_add_reset(1, 1);
	} else
	{
		jtag_add_reset(0, 1);
778
	}
779

oharboe's avatar
   
oharboe committed
780

781
782
	target->state = TARGET_RESET;
	jtag_add_sleep(50000);
oharboe's avatar
   
oharboe committed
783

784
785
	armv4_5_invalidate_core_regs(target);

786
787
788
789
790
791
    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;
	}
	
792
793
794
795
796
797
	return ERROR_OK;

}

int arm7_9_deassert_reset(target_t *target)
{
798
	int retval=ERROR_OK;
799
800
801
	LOG_DEBUG("target->state: %s", 
		  Jim_Nvp_value2name_simple( nvp_target_state,target->state)->name);

802

803
804
	/* deassert reset lines */
	jtag_add_reset(0, 0);
805

806
	if (target->reset_halt&&(jtag_reset_config & RESET_SRST_PULLS_TRST)!=0)
807
	{
808
		LOG_WARNING("srst pulls trst - can not reset into halted mode. Issuing halt after reset.");
809
810
811
		/* set up embedded ice registers again */
		if ((retval=target->type->examine(target))!=ERROR_OK)
			return retval;
812
813
814
815
816
817
818
819
820
821
822

		if ((retval=target_poll(target))!=ERROR_OK)
		{
			return retval;
		}
		
		if ((retval=target_halt(target))!=ERROR_OK)
		{
			return retval;
		}
		
823
824
	}
	return retval;
825
826
}

827
828
829
830
831
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];
832

833
834
	/* we used DBGRQ only if we didn't come out of reset */
	if (!arm7_9->debug_entry_from_reset && arm7_9->use_dbgrq)
835
836
837
	{
		/* program EmbeddedICE Debug Control Register to deassert DBGRQ
		 */
838
		buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
839
840
841
842
		embeddedice_store_reg(dbg_ctrl);
	}
	else
	{
843
		if (arm7_9->debug_entry_from_reset && arm7_9->has_vector_catch)
844
		{
845
846
847
848
849
850
851
852
853
854
855
856
			/* 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)
			{
857
858
859
860
				if (arm7_9->debug_entry_from_reset)
				{
					embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE]);
				}
861
862
863
864
				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]);
			}
865
			/* control value always has to be restored, as it was either disabled,
866
867
868
			 * or enabled with possibly different bits
			 */
			embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
869
870
		}
	}
871

872
873
874
	return ERROR_OK;
}

875
876
877
878
879
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];
880
	reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
881
	int i;
oharboe's avatar
   
oharboe committed
882
	int retval;
883

884
	if ((retval=target_halt(target))!=ERROR_OK)
oharboe's avatar
   
oharboe committed
885
		return retval;
886

oharboe's avatar
oharboe committed
887
888
889
	long long then=timeval_ms();
	int timeout;
	while (!(timeout=((timeval_ms()-then)>1000)))
890
	{
oharboe's avatar
   
oharboe committed
891
892
		if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) != 0)
			break;
893
		embeddedice_read_reg(dbg_stat);
oharboe's avatar
   
oharboe committed
894
895
		if ((retval=jtag_execute_queue())!=ERROR_OK)
			return retval;
oharboe's avatar
oharboe committed
896
897
898
899
900
901
902
		if (debug_level>=3)
		{
			alive_sleep(100);
		} else
		{
			keep_alive();
		}
oharboe's avatar
   
oharboe committed
903
	}
oharboe's avatar
oharboe committed
904
	if (timeout)
oharboe's avatar
   
oharboe committed
905
	{
906
		LOG_ERROR("Failed to halt CPU after 1 sec");
oharboe's avatar
   
oharboe committed
907
		return ERROR_TARGET_TIMEOUT;
908
909
	}
	target->state = TARGET_HALTED;
910

911
912
913
914
915
916
917
	/* 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);
918

919
	arm7_9_clear_halt(target);
920

921
922
923
924
	/* if the target is in Thumb state, change to ARM state */
	if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_ITBIT, 1))
	{
		u32 r0_thumb, pc_thumb;
925
		LOG_DEBUG("target entered debug from Thumb state, changing to ARM");
926
927
928
929
		/* Entered debug from Thumb mode */
		armv4_5->core_state = ARMV4_5_STATE_THUMB;
		arm7_9->change_to_arm(target, &r0_thumb, &pc_thumb);
	}
930

931
932
	/* all register content is now invalid */
	armv4_5_invalidate_core_regs(target);
933

934
935
936
937
	/* SVC, ARM state, IRQ and FIQ disabled */
	buf_set_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8, 0xd3);
	armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 1;
	armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
938

939
940
941
942
	/* start fetching from 0x0 */
	buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, 0x0);
	armv4_5->core_cache->reg_list[15].dirty = 1;
	armv4_5->core_cache->reg_list[15].valid = 1;
943

944
945
	armv4_5->core_mode = ARMV4_5_MODE_SVC;
	armv4_5->core_state = ARMV4_5_STATE_ARM;
946
947
948

	if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
		return ERROR_FAIL;
949

950
951
	/* reset registers */
	for (i = 0; i <= 14; i++)
952
	{
953
954
955
956
		buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).value, 0, 32, 0xffffffff);
		ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = 1;
		ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid = 1;
	}
957

958
	target_call_event_callbacks(target, TARGET_EVENT_HALTED);
959

960
961
962
963
964
	return ERROR_OK;
}

int arm7_9_halt(target_t *target)
{
965
	if (target->state==TARGET_RESET)
966
	{
967
		LOG_ERROR("BUG: arm7/9 does not support halt during reset. This is handled in arm7_9_assert_reset()");
968
969
970
		return ERROR_OK;
	}

971
972
973
	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];
974

975
976
	LOG_DEBUG("target->state: %s", 
		  Jim_Nvp_value2name_simple( nvp_target_state,target->state)->name);
977

978
979
	if (target->state == TARGET_HALTED)
	{
980
		LOG_DEBUG("target was already halted");
oharboe's avatar
   
oharboe committed
981
		return ERROR_OK;
982
	}
983

984
985
	if (target->state == TARGET_UNKNOWN)
	{
986
		LOG_WARNING("target was in unknown state when halt was requested");
987
	}
988

989
990
991
992
	if (arm7_9->use_dbgrq)
	{
		/* program EmbeddedICE Debug Control Register to assert DBGRQ
		 */
oharboe's avatar
oharboe committed
993
994
995
		if (arm7_9->set_special_dbgrq) {
			arm7_9->set_special_dbgrq(target);
		} else {
996
			buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 1);
oharboe's avatar
oharboe committed
997
998
			embeddedice_store_reg(dbg_ctrl);
		}
999
1000
	}
	else
For faster browsing, not all history is shown. View entire blame