embeddedice.c 13.8 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
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 *   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 "embeddedice.h"


33
34
#if 0
static bitfield_desc_t embeddedice_comms_ctrl_bitfield_desc[] =
35
36
37
38
39
40
{
	{"R", 1},
	{"W", 1},
	{"reserved", 26},
	{"version", 4}
};
41
#endif
42

43
static int embeddedice_reg_arch_info[] =
44
45
46
47
48
49
50
{
	0x0, 0x1, 0x4, 0x5,
	0x8, 0x9, 0xa, 0xb, 0xc, 0xd,
	0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
	0x2
};

51
static char* embeddedice_reg_list[] =
52
53
54
{
	"debug_ctrl",
	"debug_status",
55

56
57
	"comms_ctrl",
	"comms_data",
58

59
60
61
62
63
64
	"watch 0 addr value",
	"watch 0 addr mask",
	"watch 0 data value",
	"watch 0 data mask",
	"watch 0 control value",
	"watch 0 control mask",
65

66
67
68
69
70
71
	"watch 1 addr value",
	"watch 1 addr mask",
	"watch 1 data value",
	"watch 1 data mask",
	"watch 1 control value",
	"watch 1 control mask",
72

73
74
75
	"vector catch"
};

76
static int embeddedice_reg_arch_type = -1;
77

78
static int embeddedice_get_reg(reg_t *reg);
79
80
81

reg_cache_t* embeddedice_build_reg_cache(target_t *target, arm7_9_common_t *arm7_9)
{
oharboe's avatar
oharboe committed
82
	int retval;
83
84
85
86
87
88
89
	reg_cache_t *reg_cache = malloc(sizeof(reg_cache_t));
	reg_t *reg_list = NULL;
	embeddedice_reg_t *arch_info = NULL;
	arm_jtag_t *jtag_info = &arm7_9->jtag_info;
	int num_regs;
	int i;
	int eice_version = 0;
90

91
92
93
	/* register a register arch-type for EmbeddedICE registers only once */
	if (embeddedice_reg_arch_type == -1)
		embeddedice_reg_arch_type = register_reg_arch_type(embeddedice_get_reg, embeddedice_set_reg_w_exec);
94

95
96
97
98
	if (arm7_9->has_vector_catch)
		num_regs = 17;
	else
		num_regs = 16;
99

100
101
102
	/* the actual registers are kept in two arrays */
	reg_list = calloc(num_regs, sizeof(reg_t));
	arch_info = calloc(num_regs, sizeof(embeddedice_reg_t));
103

104
105
106
107
108
	/* fill in values for the reg cache */
	reg_cache->name = "EmbeddedICE registers";
	reg_cache->next = NULL;
	reg_cache->reg_list = reg_list;
	reg_cache->num_regs = num_regs;
109

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	/* set up registers */
	for (i = 0; i < num_regs; i++)
	{
		reg_list[i].name = embeddedice_reg_list[i];
		reg_list[i].size = 32;
		reg_list[i].dirty = 0;
		reg_list[i].valid = 0;
		reg_list[i].bitfield_desc = NULL;
		reg_list[i].num_bitfields = 0;
		reg_list[i].value = calloc(1, 4);
		reg_list[i].arch_info = &arch_info[i];
		reg_list[i].arch_type = embeddedice_reg_arch_type;
		arch_info[i].addr = embeddedice_reg_arch_info[i];
		arch_info[i].jtag_info = jtag_info;
	}
125

126
127
	/* identify EmbeddedICE version by reading DCC control register */
	embeddedice_read_reg(&reg_list[EICE_COMMS_CTRL]);
oharboe's avatar
oharboe committed
128
129
130
131
132
133
134
135
136
137
	if ((retval=jtag_execute_queue())!=ERROR_OK)
	{
		for (i = 0; i < num_regs; i++)
		{
			free(reg_list[i].value);
		}
		free(reg_list);
		free(arch_info);
		return NULL;
	}
138

139
	eice_version = buf_get_u32(reg_list[EICE_COMMS_CTRL].value, 28, 4);
140

141
142
143
144
145
146
147
148
149
150
151
152
	switch (eice_version)
	{
		case 1:
			reg_list[EICE_DBG_CTRL].size = 3;
			reg_list[EICE_DBG_STAT].size = 5;
			break;
		case 2:
			reg_list[EICE_DBG_CTRL].size = 4;
			reg_list[EICE_DBG_STAT].size = 5;
			arm7_9->has_single_step = 1;
			break;
		case 3:
153
			LOG_ERROR("EmbeddedICE version 3 detected, EmbeddedICE handling might be broken");
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
			reg_list[EICE_DBG_CTRL].size = 6;
			reg_list[EICE_DBG_STAT].size = 5;
			arm7_9->has_single_step = 1;
			arm7_9->has_monitor_mode = 1;
			break;
		case 4:
			reg_list[EICE_DBG_CTRL].size = 6;
			reg_list[EICE_DBG_STAT].size = 5;
			arm7_9->has_monitor_mode = 1;
			break;
		case 5:
			reg_list[EICE_DBG_CTRL].size = 6;
			reg_list[EICE_DBG_STAT].size = 5;
			arm7_9->has_single_step = 1;
			arm7_9->has_monitor_mode = 1;
			break;
		case 6:
			reg_list[EICE_DBG_CTRL].size = 6;
			reg_list[EICE_DBG_STAT].size = 10;
			arm7_9->has_monitor_mode = 1;
			break;
		case 7:
176
			LOG_WARNING("EmbeddedICE version 7 detected, EmbeddedICE handling might be broken");
177
178
179
180
181
			reg_list[EICE_DBG_CTRL].size = 6;
			reg_list[EICE_DBG_STAT].size = 5;
			arm7_9->has_monitor_mode = 1;
			break;
		default:
kc8apf's avatar
kc8apf committed
182
183
184
185
186
			/*
			 * The Feroceon implementation has the version number
			 * in some unusual bits.  Let feroceon.c validate it
			 * and do the appropriate setup itself.
			 */
zwelch's avatar
zwelch committed
187
			if (strcmp(target_get_name(target), "feroceon") == 0)
kc8apf's avatar
kc8apf committed
188
				break;
189
			LOG_ERROR("unknown EmbeddedICE version (comms ctrl: 0x%8.8x)", buf_get_u32(reg_list[EICE_COMMS_CTRL].value, 0, 32));
190
	}
191

oharboe's avatar
oharboe committed
192
193
194
195
196
197
198
199
	return reg_cache;
}

int embeddedice_setup(target_t *target)
{
	int retval;
	armv4_5_common_t *armv4_5 = target->arch_info;
	arm7_9_common_t *arm7_9 = armv4_5->arch_info;
200

201
202
203
	/* explicitly disable monitor mode */
	if (arm7_9->has_monitor_mode)
	{
oharboe's avatar
oharboe committed
204
		reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
205

oharboe's avatar
oharboe committed
206
207
208
209
210
		embeddedice_read_reg(dbg_ctrl);
		if ((retval=jtag_execute_queue())!=ERROR_OK)
			return retval;
		buf_set_u32(dbg_ctrl->value, 4, 1, 0);
		embeddedice_set_reg_w_exec(dbg_ctrl, dbg_ctrl->value);
211
	}
oharboe's avatar
oharboe committed
212
	return jtag_execute_queue();
213
214
}

215
static int embeddedice_get_reg(reg_t *reg)
216
{
217
218
	int retval;
	if ((retval = embeddedice_read_reg(reg)) != ERROR_OK)
219
	{
220
		LOG_ERROR("BUG: error scheduling EmbeddedICE register read");
221
		return retval;
222
	}
223

224
	if ((retval = jtag_execute_queue()) != ERROR_OK)
225
	{
226
		LOG_ERROR("register read failed");
227
		return retval;
228
	}
229

230
231
232
233
234
235
236
237
238
239
240
	return ERROR_OK;
}

int embeddedice_read_reg_w_check(reg_t *reg, u8* check_value, u8* check_mask)
{
	embeddedice_reg_t *ice_reg = reg->arch_info;
	u8 reg_addr = ice_reg->addr & 0x1f;
	scan_field_t fields[3];
	u8 field1_out[1];
	u8 field2_out[1];

241
	jtag_add_end_state(TAP_IDLE);
242
	arm_jtag_scann(ice_reg->jtag_info, 0x2);
243

244
	arm_jtag_set_instr(ice_reg->jtag_info, ice_reg->jtag_info->intest_instr, NULL);
245

246
	fields[0].tap = ice_reg->jtag_info->tap;
247
248
249
	fields[0].num_bits = 32;
	fields[0].out_value = reg->value;
	fields[0].in_value = NULL;
250
251
	fields[0].check_value = NULL;
	fields[0].check_mask = NULL;
oharboe's avatar
oharboe committed
252

253
	fields[1].tap = ice_reg->jtag_info->tap;
254
255
256
257
	fields[1].num_bits = 5;
	fields[1].out_value = field1_out;
	buf_set_u32(fields[1].out_value, 0, 5, reg_addr);
	fields[1].in_value = NULL;
258
259
	fields[1].check_value = NULL;
	fields[1].check_mask = NULL;
oharboe's avatar
oharboe committed
260

261
	fields[2].tap = ice_reg->jtag_info->tap;
262
263
264
265
	fields[2].num_bits = 1;
	fields[2].out_value = field2_out;
	buf_set_u32(fields[2].out_value, 0, 1, 0);
	fields[2].in_value = NULL;
266
267
	fields[2].check_value = NULL;
	fields[2].check_mask = NULL;
oharboe's avatar
oharboe committed
268

269
	jtag_add_dr_scan(3, fields, jtag_add_end_state(TAP_INVALID));
270

271
	fields[0].in_value = reg->value;
272
273
	fields[0].check_value = check_value;
	fields[0].check_mask = check_mask;
274

275
276
277
278
279
	/* when reading the DCC data register, leaving the address field set to
	 * EICE_COMMS_DATA would read the register twice
	 * reading the control register is safe
	 */
	buf_set_u32(fields[1].out_value, 0, 5, embeddedice_reg_arch_info[EICE_COMMS_CTRL]);
280

281
	jtag_add_dr_scan_check(3, fields, jtag_add_end_state(TAP_INVALID));
282

283
284
285
286
287
288
289
290
291
292
293
294
295
	return ERROR_OK;
}

/* receive <size> words of 32 bit from the DCC
 * we pretend the target is always going to be fast enough
 * (relative to the JTAG clock), so we don't need to handshake
 */
int embeddedice_receive(arm_jtag_t *jtag_info, u32 *data, u32 size)
{
	scan_field_t fields[3];
	u8 field1_out[1];
	u8 field2_out[1];

296
	jtag_add_end_state(TAP_IDLE);
297
298
	arm_jtag_scann(jtag_info, 0x2);
	arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
299

300
	fields[0].tap = jtag_info->tap;
301
302
	fields[0].num_bits = 32;
	fields[0].out_value = NULL;
303
	fields[0].in_value = NULL;
304

305
	fields[1].tap = jtag_info->tap;
306
307
308
309
310
	fields[1].num_bits = 5;
	fields[1].out_value = field1_out;
	buf_set_u32(fields[1].out_value, 0, 5, embeddedice_reg_arch_info[EICE_COMMS_DATA]);
	fields[1].in_value = NULL;

311
	fields[2].tap = jtag_info->tap;
312
313
314
315
	fields[2].num_bits = 1;
	fields[2].out_value = field2_out;
	buf_set_u32(fields[2].out_value, 0, 1, 0);
	fields[2].in_value = NULL;
316

317
	jtag_add_dr_scan(3, fields, jtag_add_end_state(TAP_INVALID));
318

319
320
321
322
323
324
325
	while (size > 0)
	{
		/* when reading the last item, set the register address to the DCC control reg,
		 * to avoid reading additional data from the DCC data reg
		 */
		if (size == 1)
			buf_set_u32(fields[1].out_value, 0, 5, embeddedice_reg_arch_info[EICE_COMMS_CTRL]);
326

327
		fields[0].in_value = (u8 *)data;
328
		jtag_add_dr_scan(3, fields, jtag_add_end_state(TAP_INVALID));
329
		jtag_add_callback(arm_le_to_h_u32, (u8 *)data);
330

331
332
333
		data++;
		size--;
	}
334

335
336
337
338
339
	return jtag_execute_queue();
}

int embeddedice_read_reg(reg_t *reg)
{
340
	return embeddedice_read_reg_w_check(reg, NULL, NULL);
341
342
}

343
void embeddedice_set_reg(reg_t *reg, u32 value)
344
{
345
	embeddedice_write_reg(reg, value);
346

347
348
349
	buf_set_u32(reg->value, 0, reg->size, value);
	reg->valid = 1;
	reg->dirty = 0;
350

351
352
353
354
}

int embeddedice_set_reg_w_exec(reg_t *reg, u8 *buf)
{
355
	int retval;
356
	embeddedice_set_reg(reg, buf_get_u32(buf, 0, reg->size));
357

358
	if ((retval = jtag_execute_queue()) != ERROR_OK)
359
	{
360
		LOG_ERROR("register write failed");
361
		return retval;
362
363
364
365
	}
	return ERROR_OK;
}

366
void embeddedice_write_reg(reg_t *reg, u32 value)
367
368
369
{
	embeddedice_reg_t *ice_reg = reg->arch_info;

370
	LOG_DEBUG("%i: 0x%8.8x", ice_reg->addr, value);
371

372
	jtag_add_end_state(TAP_IDLE);
373
	arm_jtag_scann(ice_reg->jtag_info, 0x2);
374

375
376
	arm_jtag_set_instr(ice_reg->jtag_info, ice_reg->jtag_info->intest_instr, NULL);

377
	u8 reg_addr = ice_reg->addr & 0x1f;
378
	embeddedice_write_reg_inner(ice_reg->jtag_info->tap, reg_addr, value);
379

380
381
}

382
void embeddedice_store_reg(reg_t *reg)
383
{
384
	embeddedice_write_reg(reg, buf_get_u32(reg->value, 0, reg->size));
385
386
387
388
389
390
391
392
393
394
395
396
397
}

/* send <size> words of 32 bit to the DCC
 * we pretend the target is always going to be fast enough
 * (relative to the JTAG clock), so we don't need to handshake
 */
int embeddedice_send(arm_jtag_t *jtag_info, u32 *data, u32 size)
{
	scan_field_t fields[3];
	u8 field0_out[4];
	u8 field1_out[1];
	u8 field2_out[1];

398
	jtag_add_end_state(TAP_IDLE);
399
400
401
	arm_jtag_scann(jtag_info, 0x2);
	arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);

402
	fields[0].tap = jtag_info->tap;
403
404
405
	fields[0].num_bits = 32;
	fields[0].out_value = field0_out;
	fields[0].in_value = NULL;
oharboe's avatar
oharboe committed
406

407
	fields[1].tap = jtag_info->tap;
408
409
410
411
	fields[1].num_bits = 5;
	fields[1].out_value = field1_out;
	buf_set_u32(fields[1].out_value, 0, 5, embeddedice_reg_arch_info[EICE_COMMS_DATA]);
	fields[1].in_value = NULL;
oharboe's avatar
oharboe committed
412

413
	fields[2].tap = jtag_info->tap;
414
415
416
	fields[2].num_bits = 1;
	fields[2].out_value = field2_out;
	buf_set_u32(fields[2].out_value, 0, 1, 1);
oharboe's avatar
oharboe committed
417

418
	fields[2].in_value = NULL;
oharboe's avatar
oharboe committed
419

420
421
422
	while (size > 0)
	{
		buf_set_u32(fields[0].out_value, 0, 32, *data);
423
		jtag_add_dr_scan(3, fields, jtag_add_end_state(TAP_INVALID));
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

		data++;
		size--;
	}

	/* call to jtag_execute_queue() intentionally omitted */
	return ERROR_OK;
}

/* wait for DCC control register R/W handshake bit to become active
 */
int embeddedice_handshake(arm_jtag_t *jtag_info, int hsbit, u32 timeout)
{
	scan_field_t fields[3];
	u8 field0_in[4];
	u8 field1_out[1];
	u8 field2_out[1];
	int retval;
442
	u32 hsact;
443
444
445
446
447
448
449
450
451
452
	struct timeval lap;
	struct timeval now;

	if (hsbit == EICE_COMM_CTRL_WBIT)
		hsact = 1;
	else if (hsbit == EICE_COMM_CTRL_RBIT)
		hsact = 0;
	else
		return ERROR_INVALID_ARGUMENTS;

453
	jtag_add_end_state(TAP_IDLE);
454
455
456
	arm_jtag_scann(jtag_info, 0x2);
	arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);

457
	fields[0].tap = jtag_info->tap;
458
459
460
	fields[0].num_bits = 32;
	fields[0].out_value = NULL;
	fields[0].in_value = field0_in;
oharboe's avatar
oharboe committed
461

462
	fields[1].tap = jtag_info->tap;
463
464
465
466
	fields[1].num_bits = 5;
	fields[1].out_value = field1_out;
	buf_set_u32(fields[1].out_value, 0, 5, embeddedice_reg_arch_info[EICE_COMMS_CTRL]);
	fields[1].in_value = NULL;
oharboe's avatar
oharboe committed
467

468
	fields[2].tap = jtag_info->tap;
469
470
471
472
	fields[2].num_bits = 1;
	fields[2].out_value = field2_out;
	buf_set_u32(fields[2].out_value, 0, 1, 0);
	fields[2].in_value = NULL;
oharboe's avatar
oharboe committed
473

474
	jtag_add_dr_scan(3, fields, jtag_add_end_state(TAP_INVALID));
475
476
477
	gettimeofday(&lap, NULL);
	do
	{
478
		jtag_add_dr_scan(3, fields, jtag_add_end_state(TAP_INVALID));
479
480
481
482
483
484
485
486
		if ((retval = jtag_execute_queue()) != ERROR_OK)
			return retval;

		if (buf_get_u32(field0_in, hsbit, 1) == hsact)
			return ERROR_OK;

		gettimeofday(&now, NULL);
	}
487
	while ((u32)((now.tv_sec-lap.tv_sec)*1000 + (now.tv_usec-lap.tv_usec)/1000) <= timeout);
488
489
490

	return ERROR_TARGET_TIMEOUT;
}
491

oharboe's avatar
oharboe committed
492
#ifndef HAVE_JTAG_MINIDRIVER_H
493
/* this is the inner loop of the open loop DCC write of data to target */
oharboe's avatar
oharboe committed
494
void embeddedice_write_dcc(jtag_tap_t *tap, int reg_addr, u8 *buffer, int little, int count)
495
496
497
498
{
	int i;
	for (i = 0; i < count; i++)
	{
499
		embeddedice_write_reg_inner(tap, reg_addr, fast_target_buffer_get_u32(buffer, little));
500
501
502
		buffer += 4;
	}
}
oharboe's avatar
oharboe committed
503
504
505
#else
/* provided by minidriver */
#endif