1
2
3
10
11
12
13
14
15
16
17
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
45
46
47
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
79
80
81
82
83
90
91
92
93
94
95
96
97
98
99
100
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
134
135
136
140
141
142
143
144
145
149
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
199
200
201
202
203
204
205
206
209
210
211
215
216
217
218
219
220
221
222
223
224
225
226
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
293
294
295
296
297
298
299
300
301
302
303
304
305
306
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
331
332
333
334
335
336
337
338
339
340
341
342
343
348
349
350
351
355
356
357
358
359
360
361
362
363
364
365
366
367
372
373
374
375
379
380
381
382
383
384
385
386
387
388
389
390
391
396
397
398
399
403
404
405
406
407
408
409
410
411
412
413
414
415
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
524
525
526
527
532
533
534
535
536
537
538
539
540
541
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "arc.h"
/* ... */
static void arc_jtag_enque_write_ir(struct arc_jtag *jtag_info, uint32_t
new_instr)
{
uint32_t current_instr;
struct jtag_tap *tap;
uint8_t instr_buffer[sizeof(uint32_t)] = {0};
assert(jtag_info);
assert(jtag_info->tap);
tap = jtag_info->tap;
current_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
if (current_instr == new_instr)
return;
struct scan_field field = {
.num_bits = tap->ir_length,
.out_value = instr_buffer
...};
buf_set_u32(instr_buffer, 0, field.num_bits, new_instr);
/* ... */
jtag_add_ir_scan(tap, &field, TAP_IRPAUSE);
}{ ... }
/* ... */
static void arc_jtag_enque_read_dr(struct arc_jtag *jtag_info, uint8_t *data,
tap_state_t end_state)
{
assert(jtag_info);
assert(jtag_info->tap);
struct scan_field field = {
.num_bits = 32,
.in_value = data
...};
jtag_add_dr_scan(jtag_info->tap, 1, &field, end_state);
}{ ... }
/* ... */
static void arc_jtag_enque_write_dr(struct arc_jtag *jtag_info, uint32_t data,
tap_state_t end_state)
{
uint8_t out_value[sizeof(uint32_t)] = {0};
assert(jtag_info);
assert(jtag_info->tap);
buf_set_u32(out_value, 0, 32, data);
struct scan_field field = {
.num_bits = 32,
.out_value = out_value
...};
jtag_add_dr_scan(jtag_info->tap, 1, &field, end_state);
}{ ... }
/* ... */
static void arc_jtag_enque_set_transaction(struct arc_jtag *jtag_info,
uint32_t new_trans, tap_state_t end_state)
{
uint8_t out_value[sizeof(uint32_t)] = {0};
assert(jtag_info);
assert(jtag_info->tap);
if (jtag_info->cur_trans == new_trans)
return;
/* ... */
arc_jtag_enque_write_ir(jtag_info, ARC_TRANSACTION_CMD_REG);
buf_set_u32(out_value, 0, ARC_TRANSACTION_CMD_REG_LENGTH, new_trans);
struct scan_field field = {
.num_bits = ARC_TRANSACTION_CMD_REG_LENGTH,
.out_value = out_value
...};
jtag_add_dr_scan(jtag_info->tap, 1, &field, end_state);
jtag_info->cur_trans = new_trans;
}{ ... }
/* ... */
static void arc_jtag_enque_reset_transaction(struct arc_jtag *jtag_info)
{
arc_jtag_enque_set_transaction(jtag_info, ARC_JTAG_CMD_NOP, TAP_IDLE);
}{ ... }
static void arc_jtag_enque_status_read(struct arc_jtag * const jtag_info,
uint8_t * const buffer)
{
assert(jtag_info);
assert(jtag_info->tap);
assert(buffer);
arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_STATUS_REG);
arc_jtag_enque_read_dr(jtag_info, buffer, TAP_IDLE);
}{ ... }
int arc_jtag_startup(struct arc_jtag *jtag_info)
{
assert(jtag_info);
arc_jtag_enque_reset_transaction(jtag_info);
return jtag_execute_queue();
}{ ... }
int arc_jtag_status(struct arc_jtag * const jtag_info, uint32_t * const value)
{
uint8_t buffer[sizeof(uint32_t)];
assert(jtag_info);
assert(jtag_info->tap);
arc_jtag_enque_reset_transaction(jtag_info);
arc_jtag_enque_status_read(jtag_info, buffer);
arc_jtag_enque_reset_transaction(jtag_info);
CHECK_RETVAL(jtag_execute_queue());
*value = buf_get_u32(buffer, 0, 32);
return ERROR_OK;
}{ ... }
static void arc_jtag_enque_register_rw(struct arc_jtag *jtag_info, uint32_t *addr,
uint8_t *read_buffer, const uint32_t *write_buffer, uint32_t count)
{
uint32_t i;
for (i = 0; i < count; i++) {
/* ... */
if (i == 0 || (addr[i] != addr[i-1] + 1)) {
arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_ADDRESS_REG);
/* ... */
if (write_buffer)
arc_jtag_enque_write_dr(jtag_info, addr[i], TAP_DRPAUSE);
else
arc_jtag_enque_write_dr(jtag_info, addr[i], TAP_IDLE);
arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_DATA_REG);
}if (i == 0 || (addr[i] != addr[i-1] + 1)) { ... }
if (write_buffer)
arc_jtag_enque_write_dr(jtag_info, *(write_buffer + i), TAP_IDLE);
else
arc_jtag_enque_read_dr(jtag_info, read_buffer + i * 4, TAP_IDLE);
}for (i = 0; i < count; i++) { ... }
/* ... */
arc_jtag_enque_reset_transaction(jtag_info);
}{ ... }
/* ... */
static int arc_jtag_write_registers(struct arc_jtag *jtag_info, uint32_t type,
uint32_t *addr, uint32_t count, const uint32_t *buffer)
{
LOG_DEBUG("Writing to %s registers: addr[0]=0x%" PRIx32 ";count=%" PRIu32
";buffer[0]=0x%08" PRIx32,
(type == ARC_JTAG_CORE_REG ? "core" : "aux"), *addr, count, *buffer);
if (!count) {
LOG_ERROR("Trying to write 0 registers");
return ERROR_FAIL;
}if (!count) { ... }
arc_jtag_enque_reset_transaction(jtag_info);
const uint32_t transaction = (type == ARC_JTAG_CORE_REG ?
ARC_JTAG_WRITE_TO_CORE_REG : ARC_JTAG_WRITE_TO_AUX_REG);
arc_jtag_enque_set_transaction(jtag_info, transaction, TAP_DRPAUSE);
arc_jtag_enque_register_rw(jtag_info, addr, NULL, buffer, count);
return jtag_execute_queue();
}{ ... }
/* ... */
static int arc_jtag_read_registers(struct arc_jtag *jtag_info, uint32_t type,
uint32_t *addr, uint32_t count, uint32_t *buffer)
{
int retval;
uint32_t i;
assert(jtag_info);
assert(jtag_info->tap);
LOG_DEBUG("Reading %s registers: addr[0]=0x%" PRIx32 ";count=%" PRIu32,
(type == ARC_JTAG_CORE_REG ? "core" : "aux"), *addr, count);
if (!count) {
LOG_ERROR("Trying to read 0 registers");
return ERROR_FAIL;
}if (!count) { ... }
arc_jtag_enque_reset_transaction(jtag_info);
const uint32_t transaction = (type == ARC_JTAG_CORE_REG ?
ARC_JTAG_READ_FROM_CORE_REG : ARC_JTAG_READ_FROM_AUX_REG);
arc_jtag_enque_set_transaction(jtag_info, transaction, TAP_DRPAUSE);
uint8_t *data_buf = calloc(count * 4, sizeof(uint8_t));
arc_jtag_enque_register_rw(jtag_info, addr, data_buf, NULL, count);
retval = jtag_execute_queue();
if (retval != ERROR_OK) {
LOG_ERROR("Failed to execute jtag queue: %d", retval);
retval = ERROR_FAIL;
goto exit;
}if (retval != ERROR_OK) { ... }
for (i = 0; i < count; i++)
buffer[i] = buf_get_u32(data_buf + 4 * i, 0, 32);
LOG_DEBUG("Read from register: buf[0]=0x%" PRIx32, buffer[0]);
exit:
free(data_buf);
return retval;
}{ ... }
int arc_jtag_write_core_reg_one(struct arc_jtag *jtag_info, uint32_t addr,
uint32_t value)
{
return arc_jtag_write_core_reg(jtag_info, &addr, 1, &value);
}{ ... }
/* ... */
int arc_jtag_write_core_reg(struct arc_jtag *jtag_info, uint32_t *addr,
uint32_t count, const uint32_t *buffer)
{
return arc_jtag_write_registers(jtag_info, ARC_JTAG_CORE_REG, addr, count,
buffer);
}{ ... }
int arc_jtag_read_core_reg_one(struct arc_jtag *jtag_info, uint32_t addr,
uint32_t *value)
{
return arc_jtag_read_core_reg(jtag_info, &addr, 1, value);
}{ ... }
/* ... */
int arc_jtag_read_core_reg(struct arc_jtag *jtag_info, uint32_t *addr,
uint32_t count, uint32_t *buffer)
{
return arc_jtag_read_registers(jtag_info, ARC_JTAG_CORE_REG, addr, count,
buffer);
}{ ... }
int arc_jtag_write_aux_reg_one(struct arc_jtag *jtag_info, uint32_t addr,
uint32_t value)
{
return arc_jtag_write_aux_reg(jtag_info, &addr, 1, &value);
}{ ... }
/* ... */
int arc_jtag_write_aux_reg(struct arc_jtag *jtag_info, uint32_t *addr,
uint32_t count, const uint32_t *buffer)
{
return arc_jtag_write_registers(jtag_info, ARC_JTAG_AUX_REG, addr, count,
buffer);
}{ ... }
int arc_jtag_read_aux_reg_one(struct arc_jtag *jtag_info, uint32_t addr,
uint32_t *value)
{
return arc_jtag_read_aux_reg(jtag_info, &addr, 1, value);
}{ ... }
/* ... */
int arc_jtag_read_aux_reg(struct arc_jtag *jtag_info, uint32_t *addr,
uint32_t count, uint32_t *buffer)
{
return arc_jtag_read_registers(jtag_info, ARC_JTAG_AUX_REG, addr, count,
buffer);
}{ ... }
/* ... */
int arc_jtag_write_memory(struct arc_jtag *jtag_info, uint32_t addr,
uint32_t count, const uint32_t *buffer)
{
assert(jtag_info);
assert(buffer);
LOG_DEBUG("Writing to memory: addr=0x%08" PRIx32 ";count=%" PRIu32 ";buffer[0]=0x%08" PRIx32,
addr, count, *buffer);
if (!count)
return ERROR_OK;
arc_jtag_enque_reset_transaction(jtag_info);
arc_jtag_enque_set_transaction(jtag_info, ARC_JTAG_WRITE_TO_MEMORY, TAP_DRPAUSE);
arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_ADDRESS_REG);
arc_jtag_enque_write_dr(jtag_info, addr, TAP_DRPAUSE);
arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_DATA_REG);
uint32_t i;
for (i = 0; i < count; i++)
arc_jtag_enque_write_dr(jtag_info, *(buffer + i), TAP_IDLE);
return jtag_execute_queue();
}{ ... }
/* ... */
int arc_jtag_read_memory(struct arc_jtag *jtag_info, uint32_t addr,
uint32_t count, uint32_t *buffer, bool slow_memory)
{
uint8_t *data_buf;
uint32_t i;
int retval = ERROR_OK;
assert(jtag_info);
assert(jtag_info->tap);
LOG_DEBUG("Reading memory: addr=0x%" PRIx32 ";count=%" PRIu32 ";slow=%c",
addr, count, slow_memory ? 'Y' : 'N');
if (!count)
return ERROR_OK;
data_buf = calloc(count * 4, sizeof(uint8_t));
arc_jtag_enque_reset_transaction(jtag_info);
arc_jtag_enque_set_transaction(jtag_info, ARC_JTAG_READ_FROM_MEMORY, TAP_DRPAUSE);
for (i = 0; i < count; i++) {
/* ... */
if (slow_memory || i == 0) {
arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_ADDRESS_REG);
arc_jtag_enque_write_dr(jtag_info, addr + i * 4, TAP_IDLE);
arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_DATA_REG);
}if (slow_memory || i == 0) { ... }
arc_jtag_enque_read_dr(jtag_info, data_buf + i * 4, TAP_IDLE);
}for (i = 0; i < count; i++) { ... }
retval = jtag_execute_queue();
if (retval != ERROR_OK) {
LOG_ERROR("Failed to execute jtag queue: %d", retval);
retval = ERROR_FAIL;
goto exit;
}if (retval != ERROR_OK) { ... }
for (i = 0; i < count; i++)
buffer[i] = buf_get_u32(data_buf + 4*i, 0, 32);
exit:
free(data_buf);
return retval;
}{ ... }