xscale.c 97.4 KB
Newer Older
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2006, 2007 by Dominic Rath                              *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
5
 *   Copyright (C) 2007,2008 yvind Harboe                                 *
6
7
 *   oyvind.harboe@zylin.com                                               *
 *                                                                         *
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *   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 "replacements.h"

#include "xscale.h"

31
#include "arm7_9_common.h"
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "register.h"
#include "target.h"
#include "armv4_5.h"
#include "arm_simulator.h"
#include "arm_disassembler.h"
#include "log.h"
#include "jtag.h"
#include "binarybuffer.h"
#include "time_support.h"
#include "breakpoints.h"
#include "fileio.h"

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

#include <sys/types.h>
#include <unistd.h>
#include <errno.h>


/* cli handling */
int xscale_register_commands(struct command_context_s *cmd_ctx);

/* forward declarations */
56
int xscale_target_create(struct target_s *target, Jim_Interp *interp);
57
int xscale_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
oharboe's avatar
oharboe committed
58
int xscale_quit(void);
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

int xscale_arch_state(struct target_s *target);
int xscale_poll(target_t *target);
int xscale_halt(target_t *target);
int xscale_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution);
int xscale_step(struct target_s *target, int current, u32 address, int handle_breakpoints);
int xscale_debug_entry(target_t *target);
int xscale_restore_context(target_t *target);

int xscale_assert_reset(target_t *target);
int xscale_deassert_reset(target_t *target);
int xscale_soft_reset_halt(struct target_s *target);

int xscale_set_reg_u32(reg_t *reg, u32 value);

int xscale_read_core_reg(struct target_s *target, int num, enum armv4_5_mode mode);
int xscale_write_core_reg(struct target_s *target, int num, enum armv4_5_mode mode, u32 value);

int xscale_read_memory(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer);
int xscale_write_memory(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer);
int xscale_bulk_write_memory(target_t *target, u32 address, u32 count, u8 *buffer);

int xscale_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint);
int xscale_remove_breakpoint(struct target_s *target, breakpoint_t *breakpoint);
int xscale_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint);
int xscale_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint);
int xscale_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint);
int xscale_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint);
void xscale_enable_watchpoints(struct target_s *target);
void xscale_enable_breakpoints(struct target_s *target);
static int xscale_virt2phys(struct target_s *target, u32 virtual, u32 *physical);
static int xscale_mmu(struct target_s *target, int *enabled);

int xscale_read_trace(target_t *target);

target_type_t xscale_target =
{
	.name = "xscale",

	.poll = xscale_poll,
	.arch_state = xscale_arch_state,

	.target_request_data = NULL,

	.halt = xscale_halt,
	.resume = xscale_resume,
	.step = xscale_step,

	.assert_reset = xscale_assert_reset,
	.deassert_reset = xscale_deassert_reset,
	.soft_reset_halt = xscale_soft_reset_halt,

	.get_gdb_reg_list = armv4_5_get_gdb_reg_list,

	.read_memory = xscale_read_memory,
	.write_memory = xscale_write_memory,
	.bulk_write_memory = xscale_bulk_write_memory,
116
117
	.checksum_memory = arm7_9_checksum_memory,
	.blank_check_memory = arm7_9_blank_check_memory,
118

119
120
121
122
123
124
125
126
	.run_algorithm = armv4_5_run_algorithm,

	.add_breakpoint = xscale_add_breakpoint,
	.remove_breakpoint = xscale_remove_breakpoint,
	.add_watchpoint = xscale_add_watchpoint,
	.remove_watchpoint = xscale_remove_watchpoint,

	.register_commands = xscale_register_commands,
127
	.target_create = xscale_target_create,
128
129
	.init_target = xscale_init_target,
	.quit = xscale_quit,
130

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
	.virt2phys = xscale_virt2phys,
	.mmu = xscale_mmu
};

char* xscale_reg_list[] =
{
	"XSCALE_MAINID",		/* 0 */
	"XSCALE_CACHETYPE",
	"XSCALE_CTRL",
	"XSCALE_AUXCTRL",
	"XSCALE_TTB",
	"XSCALE_DAC",
	"XSCALE_FSR",
	"XSCALE_FAR",
	"XSCALE_PID",
	"XSCALE_CPACCESS",
	"XSCALE_IBCR0",			/* 10 */
	"XSCALE_IBCR1",
	"XSCALE_DBR0",
	"XSCALE_DBR1",
	"XSCALE_DBCON",
	"XSCALE_TBREG",
	"XSCALE_CHKPT0",
	"XSCALE_CHKPT1",
	"XSCALE_DCSR",
	"XSCALE_TX",
	"XSCALE_RX",			/* 20 */
	"XSCALE_TXRXCTRL",
};

xscale_reg_t xscale_reg_arch_info[] =
{
	{XSCALE_MAINID, NULL},
	{XSCALE_CACHETYPE, NULL},
	{XSCALE_CTRL, NULL},
	{XSCALE_AUXCTRL, NULL},
	{XSCALE_TTB, NULL},
	{XSCALE_DAC, NULL},
	{XSCALE_FSR, NULL},
	{XSCALE_FAR, NULL},
	{XSCALE_PID, NULL},
	{XSCALE_CPACCESS, NULL},
	{XSCALE_IBCR0, NULL},
	{XSCALE_IBCR1, NULL},
	{XSCALE_DBR0, NULL},
	{XSCALE_DBR1, NULL},
	{XSCALE_DBCON, NULL},
	{XSCALE_TBREG, NULL},
	{XSCALE_CHKPT0, NULL},
	{XSCALE_CHKPT1, NULL},
	{XSCALE_DCSR, NULL}, /* DCSR accessed via JTAG or SW */
	{-1, NULL}, /* TX accessed via JTAG */
	{-1, NULL}, /* RX accessed via JTAG */
	{-1, NULL}, /* TXRXCTRL implicit access via JTAG */
};

int xscale_reg_arch_type = -1;

int xscale_get_reg(reg_t *reg);
int xscale_set_reg(reg_t *reg, u8 *buf);

int xscale_get_arch_pointers(target_t *target, armv4_5_common_t **armv4_5_p, xscale_common_t **xscale_p)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;

	if (armv4_5->common_magic != ARMV4_5_COMMON_MAGIC)
	{
199
		LOG_ERROR("target isn't an XScale target");
200
201
202
203
204
		return -1;
	}

	if (xscale->common_magic != XSCALE_COMMON_MAGIC)
	{
205
		LOG_ERROR("target isn't an XScale target");
206
207
208
209
210
211
212
213
214
		return -1;
	}

	*armv4_5_p = armv4_5;
	*xscale_p = xscale;

	return ERROR_OK;
}

215
int xscale_jtag_set_instr(jtag_tap_t *tap, u32 new_instr)
216
{
217
	if (tap==NULL)
218
		return ERROR_FAIL;
219

220
	if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr)
221
222
223
	{
		scan_field_t field;

224
225
		field.tap = tap;
		field.num_bits = tap->ir_length;
226
227
228
229
		field.out_value = calloc(CEIL(field.num_bits, 8), 1);
		buf_set_u32(field.out_value, 0, field.num_bits, new_instr);
		field.out_mask = NULL;
		field.in_value = NULL;
230
		jtag_set_check_value(&field, tap->expected, tap->expected_mask, NULL);
231

mifi's avatar
mifi committed
232
		jtag_add_ir_scan(1, &field, TAP_INVALID);
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

		free(field.out_value);
	}

	return ERROR_OK;
}

int xscale_read_dcsr(target_t *target)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;

	int retval;

	scan_field_t fields[3];
	u8 field0 = 0x0;
	u8 field0_check_value = 0x2;
	u8 field0_check_mask = 0x7;
	u8 field2 = 0x0;
	u8 field2_check_value = 0x0;
	u8 field2_check_mask = 0x1;

255
	jtag_add_end_state(TAP_DRPAUSE);
256
	xscale_jtag_set_instr(xscale->jtag_info.tap, xscale->jtag_info.dcsr);
257
258
259
260

	buf_set_u32(&field0, 1, 1, xscale->hold_rst);
	buf_set_u32(&field0, 2, 1, xscale->external_debug_break);

261
	fields[0].tap = xscale->jtag_info.tap;
262
263
264
265
266
267
	fields[0].num_bits = 3;
	fields[0].out_value = &field0;
	fields[0].out_mask = NULL;
	fields[0].in_value = NULL;
	jtag_set_check_value(fields+0, &field0_check_value, &field0_check_mask, NULL);

268
	fields[1].tap = xscale->jtag_info.tap;
269
270
271
272
273
274
275
276
277
	fields[1].num_bits = 32;
	fields[1].out_value = NULL;
	fields[1].out_mask = NULL;
	fields[1].in_value = xscale->reg_cache->reg_list[XSCALE_DCSR].value;
	fields[1].in_handler = NULL;
	fields[1].in_handler_priv = NULL;
	fields[1].in_check_value = NULL;
	fields[1].in_check_mask = NULL;

278
	fields[2].tap = xscale->jtag_info.tap;
279
280
281
282
283
284
	fields[2].num_bits = 1;
	fields[2].out_value = &field2;
	fields[2].out_mask = NULL;
	fields[2].in_value = NULL;
	jtag_set_check_value(fields+2, &field2_check_value, &field2_check_mask, NULL);

mifi's avatar
mifi committed
285
	jtag_add_dr_scan(3, fields, TAP_INVALID);
286
287
288

	if ((retval = jtag_execute_queue()) != ERROR_OK)
	{
289
		LOG_ERROR("JTAG error while reading DCSR");
290
291
292
293
294
295
296
297
298
299
300
301
302
		return retval;
	}

	xscale->reg_cache->reg_list[XSCALE_DCSR].dirty = 0;
	xscale->reg_cache->reg_list[XSCALE_DCSR].valid = 1;

	/* write the register with the value we just read
	 * on this second pass, only the first bit of field0 is guaranteed to be 0)
	 */
	field0_check_mask = 0x1;
	fields[1].out_value = xscale->reg_cache->reg_list[XSCALE_DCSR].value;
	fields[1].in_value = NULL;

303
	jtag_add_end_state(TAP_IDLE);
304

mifi's avatar
mifi committed
305
	jtag_add_dr_scan(3, fields, TAP_INVALID);
306

307
308
309
	/* DANGER!!! this must be here. It will make sure that the arguments
	 * to jtag_set_check_value() does not go out of scope! */
	return jtag_execute_queue();
310
311
312
313
}

int xscale_receive(target_t *target, u32 *buffer, int num_words)
{
314
315
	if (num_words==0)
		return ERROR_INVALID_ARGUMENTS;
316

317
	int retval=ERROR_OK;
318
319
320
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;

321
	tap_state_t path[3];
322
323
324
325
326
327
328
329
330
331
332
333
334
	scan_field_t fields[3];

	u8 *field0 = malloc(num_words * 1);
	u8 field0_check_value = 0x2;
	u8 field0_check_mask = 0x6;
	u32 *field1 = malloc(num_words * 4);
	u8 field2_check_value = 0x0;
	u8 field2_check_mask = 0x1;
	int words_done = 0;
	int words_scheduled = 0;

	int i;

335
336
337
	path[0] = TAP_DRSELECT;
	path[1] = TAP_DRCAPTURE;
	path[2] = TAP_DRSHIFT;
338

339
	fields[0].tap = xscale->jtag_info.tap;
340
341
342
	fields[0].num_bits = 3;
	fields[0].out_value = NULL;
	fields[0].out_mask = NULL;
343
	fields[0].in_value = NULL;
344
345
	jtag_set_check_value(fields+0, &field0_check_value, &field0_check_mask, NULL);

346
	fields[1].tap = xscale->jtag_info.tap;
347
348
349
350
351
352
353
354
355
	fields[1].num_bits = 32;
	fields[1].out_value = NULL;
	fields[1].out_mask = NULL;
	fields[1].in_value = NULL;
	fields[1].in_handler = NULL;
	fields[1].in_handler_priv = NULL;
	fields[1].in_check_value = NULL;
	fields[1].in_check_mask = NULL;

356
	fields[2].tap = xscale->jtag_info.tap;
357
358
359
360
361
362
	fields[2].num_bits = 1;
	fields[2].out_value = NULL;
	fields[2].out_mask = NULL;
	fields[2].in_value = NULL;
	jtag_set_check_value(fields+2, &field2_check_value, &field2_check_mask, NULL);

363
	jtag_add_end_state(TAP_IDLE);
364
	xscale_jtag_set_instr(xscale->jtag_info.tap, xscale->jtag_info.dbgtx);
mifi's avatar
mifi committed
365
	jtag_add_runtest(1, TAP_INVALID); /* ensures that we're in the TAP_IDLE state as the above could be a no-op */
366
367

	/* repeat until all words have been collected */
368
	int attempts=0;
369
370
371
372
373
374
375
376
377
378
379
	while (words_done < num_words)
	{
		/* schedule reads */
		words_scheduled = 0;
		for (i = words_done; i < num_words; i++)
		{
			fields[0].in_value = &field0[i];
			fields[1].in_handler = buf_to_u32_handler;
			fields[1].in_handler_priv = (u8*)&field1[i];

			jtag_add_pathmove(3, path);
380
			jtag_add_dr_scan(3, fields, TAP_IDLE);
381
382
383
384
385
			words_scheduled++;
		}

		if ((retval = jtag_execute_queue()) != ERROR_OK)
		{
386
			LOG_ERROR("JTAG error while receiving data from debug handler");
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
			break;
		}

		/* examine results */
		for (i = words_done; i < num_words; i++)
		{
			if (!(field0[0] & 1))
			{
				/* move backwards if necessary */
				int j;
				for (j = i; j < num_words - 1; j++)
				{
					field0[j] = field0[j+1];
					field1[j] = field1[j+1];
				}
				words_scheduled--;
			}
		}
405
		if (words_scheduled==0)
406
		{
407
			if (attempts++==1000)
408
			{
409
				LOG_ERROR("Failed to receiving data from debug handler after 1000 attempts");
410
				retval=ERROR_TARGET_TIMEOUT;
411
412
413
				break;
			}
		}
414

415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
		words_done += words_scheduled;
	}

	for (i = 0; i < num_words; i++)
		*(buffer++) = buf_get_u32((u8*)&field1[i], 0, 32);

	free(field1);

	return retval;
}

int xscale_read_tx(target_t *target, int consume)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;
430
431
	tap_state_t path[3];
	tap_state_t noconsume_path[6];
432
433
434
435
436
437
438
439
440
441
442

	int retval;
	struct timeval timeout, now;

	scan_field_t fields[3];
	u8 field0_in = 0x0;
	u8 field0_check_value = 0x2;
	u8 field0_check_mask = 0x6;
	u8 field2_check_value = 0x0;
	u8 field2_check_mask = 0x1;

443
	jtag_add_end_state(TAP_IDLE);
444

445
	xscale_jtag_set_instr(xscale->jtag_info.tap, xscale->jtag_info.dbgtx);
446

447
448
449
	path[0] = TAP_DRSELECT;
	path[1] = TAP_DRCAPTURE;
	path[2] = TAP_DRSHIFT;
450

451
452
453
454
455
456
	noconsume_path[0] = TAP_DRSELECT;
	noconsume_path[1] = TAP_DRCAPTURE;
	noconsume_path[2] = TAP_DREXIT1;
	noconsume_path[3] = TAP_DRPAUSE;
	noconsume_path[4] = TAP_DREXIT2;
	noconsume_path[5] = TAP_DRSHIFT;
457

458
	fields[0].tap = xscale->jtag_info.tap;
459
460
461
462
463
464
	fields[0].num_bits = 3;
	fields[0].out_value = NULL;
	fields[0].out_mask = NULL;
	fields[0].in_value = &field0_in;
	jtag_set_check_value(fields+0, &field0_check_value, &field0_check_mask, NULL);

465
	fields[1].tap = xscale->jtag_info.tap;
466
467
468
469
470
471
472
473
474
	fields[1].num_bits = 32;
	fields[1].out_value = NULL;
	fields[1].out_mask = NULL;
	fields[1].in_value = xscale->reg_cache->reg_list[XSCALE_TX].value;
	fields[1].in_handler = NULL;
	fields[1].in_handler_priv = NULL;
	fields[1].in_check_value = NULL;
	fields[1].in_check_mask = NULL;

475
	fields[2].tap = xscale->jtag_info.tap;
476
477
478
479
480
481
482
	fields[2].num_bits = 1;
	fields[2].out_value = NULL;
	fields[2].out_mask = NULL;
	fields[2].in_value = NULL;
	jtag_set_check_value(fields+2, &field2_check_value, &field2_check_mask, NULL);

	gettimeofday(&timeout, NULL);
oharboe's avatar
oharboe committed
483
	timeval_add_time(&timeout, 1, 0);
484

485
	for (;;)
486
	{
487
488
489
490
491
492
493
		/* if we want to consume the register content (i.e. clear TX_READY),
		 * we have to go straight from Capture-DR to Shift-DR
		 * otherwise, we go from Capture-DR to Exit1-DR to Pause-DR
		*/
		if (consume)
			jtag_add_pathmove(3, path);
		else
494
		{
495
496
497
			jtag_add_pathmove(sizeof(noconsume_path)/sizeof(*noconsume_path), noconsume_path);
		}

498
		jtag_add_dr_scan(3, fields, TAP_IDLE);
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517

		if ((retval = jtag_execute_queue()) != ERROR_OK)
		{
			LOG_ERROR("JTAG error while reading TX");
			return ERROR_TARGET_TIMEOUT;
		}

		gettimeofday(&now, NULL);
		if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec)&& (now.tv_usec > timeout.tv_usec)))
		{
			LOG_ERROR("time out reading TX register");
			return ERROR_TARGET_TIMEOUT;
		}
		if (!((!(field0_in & 1)) && consume))
		{
			goto done;
		}
		if (debug_level>=3)
		{
518
519
520
521
522
			LOG_DEBUG("waiting 100ms");
			alive_sleep(100); /* avoid flooding the logs */
		} else
		{
			keep_alive();
523
		}
524
	}
oharboe's avatar
oharboe committed
525
	done:
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549

	if (!(field0_in & 1))
		return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;

	return ERROR_OK;
}

int xscale_write_rx(target_t *target)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;

	int retval;
	struct timeval timeout, now;

	scan_field_t fields[3];
	u8 field0_out = 0x0;
	u8 field0_in = 0x0;
	u8 field0_check_value = 0x2;
	u8 field0_check_mask = 0x6;
	u8 field2 = 0x0;
	u8 field2_check_value = 0x0;
	u8 field2_check_mask = 0x1;

550
	jtag_add_end_state(TAP_IDLE);
551

552
	xscale_jtag_set_instr(xscale->jtag_info.tap, xscale->jtag_info.dbgrx);
553

554
	fields[0].tap = xscale->jtag_info.tap;
555
556
557
558
559
560
	fields[0].num_bits = 3;
	fields[0].out_value = &field0_out;
	fields[0].out_mask = NULL;
	fields[0].in_value = &field0_in;
	jtag_set_check_value(fields+0, &field0_check_value, &field0_check_mask, NULL);

561
	fields[1].tap = xscale->jtag_info.tap;
562
563
564
565
566
567
568
569
570
	fields[1].num_bits = 32;
	fields[1].out_value = xscale->reg_cache->reg_list[XSCALE_RX].value;
	fields[1].out_mask = NULL;
	fields[1].in_value = NULL;
	fields[1].in_handler = NULL;
	fields[1].in_handler_priv = NULL;
	fields[1].in_check_value = NULL;
	fields[1].in_check_mask = NULL;

571
	fields[2].tap = xscale->jtag_info.tap;
572
573
574
575
576
577
578
	fields[2].num_bits = 1;
	fields[2].out_value = &field2;
	fields[2].out_mask = NULL;
	fields[2].in_value = NULL;
	jtag_set_check_value(fields+2, &field2_check_value, &field2_check_mask, NULL);

	gettimeofday(&timeout, NULL);
oharboe's avatar
oharboe committed
579
	timeval_add_time(&timeout, 1, 0);
580
581

	/* poll until rx_read is low */
582
	LOG_DEBUG("polling RX");
583
	for (;;)
584
	{
585
		jtag_add_dr_scan(3, fields, TAP_IDLE);
586
587

		if ((retval = jtag_execute_queue()) != ERROR_OK)
588
		{
589
590
591
592
593
594
595
596
597
598
599
600
601
602
			LOG_ERROR("JTAG error while writing RX");
			return retval;
		}

		gettimeofday(&now, NULL);
		if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec)&& (now.tv_usec > timeout.tv_usec)))
		{
			LOG_ERROR("time out writing RX register");
			return ERROR_TARGET_TIMEOUT;
		}
		if (!(field0_in & 1))
			goto done;
		if (debug_level>=3)
		{
603
604
605
606
607
			LOG_DEBUG("waiting 100ms");
			alive_sleep(100); /* avoid flooding the logs */
		} else
		{
			keep_alive();
608
		}
609
	}
oharboe's avatar
oharboe committed
610
	done:
611

612
613
	/* set rx_valid */
	field2 = 0x1;
614
	jtag_add_dr_scan(3, fields, TAP_IDLE);
615
616
617

	if ((retval = jtag_execute_queue()) != ERROR_OK)
	{
618
		LOG_ERROR("JTAG error while writing RX");
619
620
621
622
623
624
625
626
627
628
629
		return retval;
	}

	return ERROR_OK;
}

/* send count elements of size byte to the debug handler */
int xscale_send(target_t *target, u8 *buffer, int count, int size)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;
630
631
	u32 t[3];
	int bits[3];
632
633
634
635

	int retval;

	int done_count = 0;
636

637
	jtag_add_end_state(TAP_IDLE);
638

639
	xscale_jtag_set_instr(xscale->jtag_info.tap, xscale->jtag_info.dbgrx);
640

641
642
643
644
645
646
647
	bits[0]=3;
	t[0]=0;
	bits[1]=32;
	t[2]=1;
	bits[2]=1;
	int endianness = target->endianness;
	while (done_count++ < count)
648
	{
649
		switch (size)
650
		{
651
652
		case 4:
			if (endianness == TARGET_LITTLE_ENDIAN)
653
			{
654
655
656
657
				t[1]=le_to_h_u32(buffer);
			} else
			{
				t[1]=be_to_h_u32(buffer);
658
			}
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
			break;
		case 2:
			if (endianness == TARGET_LITTLE_ENDIAN)
			{
				t[1]=le_to_h_u16(buffer);
			} else
			{
				t[1]=be_to_h_u16(buffer);
			}
			break;
		case 1:
			t[1]=buffer[0];
			break;
		default:
			LOG_ERROR("BUG: size neither 4, 2 nor 1");
			exit(-1);
675
		}
676
		jtag_add_dr_out(xscale->jtag_info.tap,
677
678
679
				3,
				bits,
				t,
680
				TAP_IDLE);
681
		buffer += size;
682
683
684
685
	}

	if ((retval = jtag_execute_queue()) != ERROR_OK)
	{
686
		LOG_ERROR("JTAG error while sending data to debug handler");
687
688
689
690
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
721
722
		return retval;
	}

	return ERROR_OK;
}

int xscale_send_u32(target_t *target, u32 value)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;

	buf_set_u32(xscale->reg_cache->reg_list[XSCALE_RX].value, 0, 32, value);
	return xscale_write_rx(target);
}

int xscale_write_dcsr(target_t *target, int hold_rst, int ext_dbg_brk)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;

	int retval;

	scan_field_t fields[3];
	u8 field0 = 0x0;
	u8 field0_check_value = 0x2;
	u8 field0_check_mask = 0x7;
	u8 field2 = 0x0;
	u8 field2_check_value = 0x0;
	u8 field2_check_mask = 0x1;

	if (hold_rst != -1)
		xscale->hold_rst = hold_rst;

	if (ext_dbg_brk != -1)
		xscale->external_debug_break = ext_dbg_brk;

723
	jtag_add_end_state(TAP_IDLE);
724
	xscale_jtag_set_instr(xscale->jtag_info.tap, xscale->jtag_info.dcsr);
725
726
727
728

	buf_set_u32(&field0, 1, 1, xscale->hold_rst);
	buf_set_u32(&field0, 2, 1, xscale->external_debug_break);

729
	fields[0].tap = xscale->jtag_info.tap;
730
731
732
733
734
735
	fields[0].num_bits = 3;
	fields[0].out_value = &field0;
	fields[0].out_mask = NULL;
	fields[0].in_value = NULL;
	jtag_set_check_value(fields+0, &field0_check_value, &field0_check_mask, NULL);

736
	fields[1].tap = xscale->jtag_info.tap;
737
738
739
740
741
742
743
744
745
	fields[1].num_bits = 32;
	fields[1].out_value = xscale->reg_cache->reg_list[XSCALE_DCSR].value;
	fields[1].out_mask = NULL;
	fields[1].in_value = NULL;
	fields[1].in_handler = NULL;
	fields[1].in_handler_priv = NULL;
	fields[1].in_check_value = NULL;
	fields[1].in_check_mask = NULL;

746
	fields[2].tap = xscale->jtag_info.tap;
747
748
749
750
751
752
	fields[2].num_bits = 1;
	fields[2].out_value = &field2;
	fields[2].out_mask = NULL;
	fields[2].in_value = NULL;
	jtag_set_check_value(fields+2, &field2_check_value, &field2_check_mask, NULL);

mifi's avatar
mifi committed
753
	jtag_add_dr_scan(3, fields, TAP_INVALID);
754
755
756

	if ((retval = jtag_execute_queue()) != ERROR_OK)
	{
757
		LOG_ERROR("JTAG error while writing DCSR");
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
		return retval;
	}

	xscale->reg_cache->reg_list[XSCALE_DCSR].dirty = 0;
	xscale->reg_cache->reg_list[XSCALE_DCSR].valid = 1;

	return ERROR_OK;
}

/* parity of the number of bits 0 if even; 1 if odd. for 32 bit words */
unsigned int parity (unsigned int v)
{
	unsigned int ov = v;
	v ^= v >> 16;
	v ^= v >> 8;
	v ^= v >> 4;
	v &= 0xf;
775
	LOG_DEBUG("parity of 0x%x is %i", ov, (0x6996 >> v) & 1);
776
777
778
779
780
781
782
783
784
785
786
787
788
	return (0x6996 >> v) & 1;
}

int xscale_load_ic(target_t *target, int mini, u32 va, u32 buffer[8])
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;
	u8 packet[4];
	u8 cmd;
	int word;

	scan_field_t fields[2];

789
	LOG_DEBUG("loading miniIC at 0x%8.8x", va);
790

791
	jtag_add_end_state(TAP_IDLE);
792
	xscale_jtag_set_instr(xscale->jtag_info.tap, xscale->jtag_info.ldic); /* LDIC */
793
794
795
796
797
798
799
800
801
802
803
804

	/* CMD is b010 for Main IC and b011 for Mini IC */
	if (mini)
		buf_set_u32(&cmd, 0, 3, 0x3);
	else
		buf_set_u32(&cmd, 0, 3, 0x2);

	buf_set_u32(&cmd, 3, 3, 0x0);

	/* virtual address of desired cache line */
	buf_set_u32(packet, 0, 27, va >> 5);

805
	fields[0].tap = xscale->jtag_info.tap;
806
807
808
809
810
811
812
813
814
	fields[0].num_bits = 6;
	fields[0].out_value = &cmd;
	fields[0].out_mask = NULL;
	fields[0].in_value = NULL;
	fields[0].in_check_value = NULL;
	fields[0].in_check_mask = NULL;
	fields[0].in_handler = NULL;
	fields[0].in_handler_priv = NULL;

815
	fields[1].tap = xscale->jtag_info.tap;
816
817
818
819
820
821
822
823
824
	fields[1].num_bits = 27;
	fields[1].out_value = packet;
	fields[1].out_mask = NULL;
	fields[1].in_value = NULL;
	fields[1].in_check_value = NULL;
	fields[1].in_check_mask = NULL;
	fields[1].in_handler = NULL;
	fields[1].in_handler_priv = NULL;

mifi's avatar
mifi committed
825
	jtag_add_dr_scan(2, fields, TAP_INVALID);
826
827
828
829
830
831
832
833
834
835
836

	fields[0].num_bits = 32;
	fields[0].out_value = packet;

	fields[1].num_bits = 1;
	fields[1].out_value = &cmd;

	for (word = 0; word < 8; word++)
	{
		buf_set_u32(packet, 0, 32, buffer[word]);
		cmd = parity(*((u32*)packet));
mifi's avatar
mifi committed
837
		jtag_add_dr_scan(2, fields, TAP_INVALID);
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
	}

	jtag_execute_queue();

	return ERROR_OK;
}

int xscale_invalidate_ic_line(target_t *target, u32 va)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;
	u8 packet[4];
	u8 cmd;

	scan_field_t fields[2];

854
	jtag_add_end_state(TAP_IDLE);
855
	xscale_jtag_set_instr(xscale->jtag_info.tap, xscale->jtag_info.ldic); /* LDIC */
856
857
858
859
860
861
862

	/* CMD for invalidate IC line b000, bits [6:4] b000 */
	buf_set_u32(&cmd, 0, 6, 0x0);

	/* virtual address of desired cache line */
	buf_set_u32(packet, 0, 27, va >> 5);

863
	fields[0].tap = xscale->jtag_info.tap;
864
865
866
867
868
869
870
871
872
	fields[0].num_bits = 6;
	fields[0].out_value = &cmd;
	fields[0].out_mask = NULL;
	fields[0].in_value = NULL;
	fields[0].in_check_value = NULL;
	fields[0].in_check_mask = NULL;
	fields[0].in_handler = NULL;
	fields[0].in_handler_priv = NULL;

873
	fields[1].tap = xscale->jtag_info.tap;
874
875
876
877
878
879
880
881
882
	fields[1].num_bits = 27;
	fields[1].out_value = packet;
	fields[1].out_mask = NULL;
	fields[1].in_value = NULL;
	fields[1].in_check_value = NULL;
	fields[1].in_check_mask = NULL;
	fields[1].in_handler = NULL;
	fields[1].in_handler_priv = NULL;

mifi's avatar
mifi committed
883
	jtag_add_dr_scan(2, fields, TAP_INVALID);
884
885
886
887
888
889
890
891
892

	return ERROR_OK;
}

int xscale_update_vectors(target_t *target)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;
	int i;
893
	int retval;
894
895
896
897
898
899
900
901
902
903
904
905

	u32 low_reset_branch, high_reset_branch;

	for (i = 1; i < 8; i++)
	{
		/* if there's a static vector specified for this exception, override */
		if (xscale->static_high_vectors_set & (1 << i))
		{
			xscale->high_vectors[i] = xscale->static_high_vectors[i];
		}
		else
		{
906
907
908
909
			retval=target_read_u32(target, 0xffff0000 + 4*i, &xscale->high_vectors[i]);
			if (retval == ERROR_TARGET_TIMEOUT)
				return retval;
			if (retval!=ERROR_OK)
910
			{
911
				/* Some of these reads will fail as part of normal execution */
912
913
914
915
916
917
918
919
920
921
922
923
924
				xscale->high_vectors[i] = ARMV4_5_B(0xfffffe, 0);
			}
		}
	}

	for (i = 1; i < 8; i++)
	{
		if (xscale->static_low_vectors_set & (1 << i))
		{
			xscale->low_vectors[i] = xscale->static_low_vectors[i];
		}
		else
		{
925
926
927
928
			retval=target_read_u32(target, 0x0 + 4*i, &xscale->low_vectors[i]);
			if (retval == ERROR_TARGET_TIMEOUT)
				return retval;
			if (retval!=ERROR_OK)
929
			{
930
				/* Some of these reads will fail as part of normal execution */
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
				xscale->low_vectors[i] = ARMV4_5_B(0xfffffe, 0);
			}
		}
	}

	/* calculate branches to debug handler */
	low_reset_branch = (xscale->handler_address + 0x20 - 0x0 - 0x8) >> 2;
	high_reset_branch = (xscale->handler_address + 0x20 - 0xffff0000 - 0x8) >> 2;

	xscale->low_vectors[0] = ARMV4_5_B((low_reset_branch & 0xffffff), 0);
	xscale->high_vectors[0] = ARMV4_5_B((high_reset_branch & 0xffffff), 0);

	/* invalidate and load exception vectors in mini i-cache */
	xscale_invalidate_ic_line(target, 0x0);
	xscale_invalidate_ic_line(target, 0xffff0000);

	xscale_load_ic(target, 1, 0x0, xscale->low_vectors);
	xscale_load_ic(target, 1, 0xffff0000, xscale->high_vectors);

	return ERROR_OK;
}

int xscale_arch_state(struct target_s *target)
{
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;

	char *state[] =
	{
		"disabled", "enabled"
	};

	char *arch_dbg_reason[] =
	{
		"", "\n(processor reset)", "\n(trace buffer full)"
	};

	if (armv4_5->common_magic != ARMV4_5_COMMON_MAGIC)
	{
970
		LOG_ERROR("BUG: called for a non-ARMv4/5 target");
971
972
973
		exit(-1);
	}

974
	LOG_USER("target halted in %s state due to %s, current mode: %s\n"
975
976
977
978
			"cpsr: 0x%8.8x pc: 0x%8.8x\n"
			"MMU: %s, D-Cache: %s, I-Cache: %s"
			"%s",
			 armv4_5_state_strings[armv4_5->core_state],
979
			 Jim_Nvp_value2name_simple( nvp_target_debug_reason, target->debug_reason )->name ,
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
			 armv4_5_mode_strings[armv4_5_mode_to_number(armv4_5->core_mode)],
			 buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32),
			 buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32),
			 state[xscale->armv4_5_mmu.mmu_enabled],
			 state[xscale->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled],
			 state[xscale->armv4_5_mmu.armv4_5_cache.i_cache_enabled],
			 arch_dbg_reason[xscale->arch_debug_reason]);

	return ERROR_OK;
}

int xscale_poll(target_t *target)
{
	int retval=ERROR_OK;
	armv4_5_common_t *armv4_5 = target->arch_info;
	xscale_common_t *xscale = armv4_5->arch_info;

	if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
	{
		enum target_state previous_state = target->state;
		if ((retval = xscale_read_tx(target, 0)) == ERROR_OK)
For faster browsing, not all history is shown. View entire blame