1
2
3
8
9
10
11
12
13
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
71
72
77
93
94
101
102
103
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
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
168
169
170
171
172
185
186
187
188
189
190
191
192
193
194
195
199
200
201
205
206
207
208
209
213
214
218
219
223
224
228
229
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
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
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
482
483
487
488
492
493
497
498
502
503
507
508
512
513
517
518
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <helper/binarybuffer.h>
#include <helper/log.h>
#include <helper/types.h>
#include <jtag/jtag.h>
#include <jtag/commands.h>
#include <jtag/interface.h>
#include "esirisc_jtag.h"
7 includes
static void esirisc_jtag_set_instr(struct esirisc_jtag *jtag_info, uint32_t new_instr)
{
struct jtag_tap *tap = jtag_info->tap;
if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr) {
struct scan_field field;
uint8_t t[4] = { 0 };
field.num_bits = tap->ir_length;
field.out_value = t;
buf_set_u32(t, 0, field.num_bits, new_instr);
field.in_value = NULL;
jtag_add_ir_scan(tap, &field, TAP_IDLE);
}if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr) { ... }
}{ ... }
/* ... */
static int esirisc_jtag_get_padding(void)
{
int padding = 0;
int bypass_devices = 0;
for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap;
tap = jtag_tap_next_enabled(tap))
if (tap->bypass)
bypass_devices++;
int num_bits = bypass_devices % 8;
if (num_bits > 0)
padding = 8 - num_bits;
return padding;
}{ ... }
static int esirisc_jtag_count_bits(unsigned int num_fields,
struct scan_field *fields)
{
int bit_count = 0;
for (unsigned int i = 0; i < num_fields; ++i)
bit_count += fields[i].num_bits;
return bit_count;
}{ ... }
/* ... */
static void esirisc_jtag_unstuff(uint8_t *data, size_t len)
{
uint8_t *r, *w;
uint8_t *end;
r = w = data;
end = data + len;
while (r < end) {
if (*r == STUFF_MARKER) {
r++;
assert(r < end);
*w++ = *r++ ^ STUFF_MARKER;
}if (*r == STUFF_MARKER) { ... } else
*w++ = *r++;
}while (r < end) { ... }
}{ ... }
/* ... */
static int esirisc_jtag_send(struct esirisc_jtag *jtag_info, uint8_t command,
int num_out_fields, struct scan_field *out_fields)
{
int num_fields = 2 + num_out_fields;
struct scan_field *fields = cmd_queue_alloc(num_fields * sizeof(struct scan_field));
esirisc_jtag_set_instr(jtag_info, INSTR_DEBUG);
fields[0].num_bits = esirisc_jtag_get_padding();
fields[0].out_value = NULL;
fields[0].in_value = NULL;
fields[1].num_bits = 8;
fields[1].out_value = &command;
fields[1].in_value = NULL;
for (int i = 0; i < num_out_fields; ++i)
jtag_scan_field_clone(&fields[2+i], &out_fields[i]);
jtag_add_dr_scan(jtag_info->tap, num_fields, fields, TAP_IDLE);
return jtag_execute_queue();
}{ ... }
static int esirisc_jtag_recv(struct esirisc_jtag *jtag_info,
int num_in_fields, struct scan_field *in_fields)
{
int num_in_bits = esirisc_jtag_count_bits(num_in_fields, in_fields);
int num_in_bytes = DIV_ROUND_UP(num_in_bits, 8);
struct scan_field fields[3];
int r_size = num_in_bytes ? num_in_bytes * 2 : 1;
uint8_t r[r_size];
esirisc_jtag_set_instr(jtag_info, INSTR_DEBUG);
fields[0].num_bits = esirisc_jtag_get_padding() + 1;
fields[0].out_value = NULL;
fields[0].in_value = NULL;
fields[1].num_bits = 8;
fields[1].out_value = NULL;
fields[1].in_value = &jtag_info->status;
fields[2].num_bits = num_in_bits * 2;
fields[2].out_value = NULL;
fields[2].in_value = r;
jtag_add_dr_scan(jtag_info->tap, ARRAY_SIZE(fields), fields, TAP_IDLE);
int retval = jtag_execute_queue();
if (retval != ERROR_OK)
return retval;
if (num_in_fields > 0) {
esirisc_jtag_unstuff(r, ARRAY_SIZE(r));
int bit_count = 0;
for (int i = 0; i < num_in_fields; ++i) {
buf_set_buf(r, bit_count, in_fields[i].in_value, 0, in_fields[i].num_bits);
bit_count += in_fields[i].num_bits;
}for (int i = 0; i < num_in_fields; ++i) { ... }
}if (num_in_fields > 0) { ... }
return ERROR_OK;
}{ ... }
static int esirisc_jtag_check_status(struct esirisc_jtag *jtag_info)
{
uint8_t eid = esirisc_jtag_get_eid(jtag_info);
if (eid != EID_NONE) {
LOG_ERROR("esirisc_jtag: bad status: 0x%02" PRIx8 " (DA: %" PRId32 ", "
"S: %" PRId32 ", EID: 0x%02" PRIx8 ")",
jtag_info->status, esirisc_jtag_is_debug_active(jtag_info),
esirisc_jtag_is_stopped(jtag_info), eid);
return ERROR_FAIL;
}if (eid != EID_NONE) { ... }
return ERROR_OK;
}{ ... }
static int esirisc_jtag_send_and_recv(struct esirisc_jtag *jtag_info, uint8_t command,
int num_out_fields, struct scan_field *out_fields,
int num_in_fields, struct scan_field *in_fields)
{
int retval;
jtag_info->status = 0;
retval = esirisc_jtag_send(jtag_info, command, num_out_fields, out_fields);
if (retval != ERROR_OK) {
LOG_ERROR("esirisc_jtag: send failed (command: 0x%02" PRIx8 ")", command);
return ERROR_FAIL;
}if (retval != ERROR_OK) { ... }
retval = esirisc_jtag_recv(jtag_info, num_in_fields, in_fields);
if (retval != ERROR_OK) {
LOG_ERROR("esirisc_jtag: recv failed (command: 0x%02" PRIx8 ")", command);
return ERROR_FAIL;
}if (retval != ERROR_OK) { ... }
return esirisc_jtag_check_status(jtag_info);
}{ ... }
/* ... */
bool esirisc_jtag_is_debug_active(struct esirisc_jtag *jtag_info)
{
return !!(jtag_info->status & 1<<7);
}{ ... }
bool esirisc_jtag_is_stopped(struct esirisc_jtag *jtag_info)
{
return !!(jtag_info->status & 1<<6);
}{ ... }
uint8_t esirisc_jtag_get_eid(struct esirisc_jtag *jtag_info)
{
return jtag_info->status & 0x3f;
}{ ... }
/* ... */
int esirisc_jtag_read_byte(struct esirisc_jtag *jtag_info, uint32_t address, uint8_t *data)
{
struct scan_field out_fields[1];
uint8_t a[4];
out_fields[0].num_bits = 32;
out_fields[0].out_value = a;
h_u32_to_be(a, address);
out_fields[0].in_value = NULL;
struct scan_field in_fields[1];
uint8_t d[1];
in_fields[0].num_bits = 8;
in_fields[0].out_value = NULL;
in_fields[0].in_value = d;
int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_BYTE,
ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
if (retval != ERROR_OK)
return retval;
*data = *d;
LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx8, address, *data);
return ERROR_OK;
}{ ... }
int esirisc_jtag_read_hword(struct esirisc_jtag *jtag_info, uint32_t address, uint16_t *data)
{
struct scan_field out_fields[1];
uint8_t a[4];
out_fields[0].num_bits = 32;
out_fields[0].out_value = a;
h_u32_to_be(a, address);
out_fields[0].in_value = NULL;
struct scan_field in_fields[1];
uint8_t d[2];
in_fields[0].num_bits = 16;
in_fields[0].out_value = NULL;
in_fields[0].in_value = d;
int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_HWORD,
ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
if (retval != ERROR_OK)
return retval;
*data = le_to_h_u16(d);
LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx16, address, *data);
return ERROR_OK;
}{ ... }
int esirisc_jtag_read_word(struct esirisc_jtag *jtag_info, uint32_t address, uint32_t *data)
{
struct scan_field out_fields[1];
uint8_t a[4];
out_fields[0].num_bits = 32;
out_fields[0].out_value = a;
h_u32_to_be(a, address);
out_fields[0].in_value = NULL;
struct scan_field in_fields[1];
uint8_t d[4];
in_fields[0].num_bits = 32;
in_fields[0].out_value = NULL;
in_fields[0].in_value = d;
int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_WORD,
ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
if (retval != ERROR_OK)
return retval;
*data = le_to_h_u32(d);
LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx32, address, *data);
return ERROR_OK;
}{ ... }
int esirisc_jtag_write_byte(struct esirisc_jtag *jtag_info, uint32_t address, uint8_t data)
{
struct scan_field out_fields[2];
uint8_t a[4];
LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx8, address, data);
out_fields[0].num_bits = 32;
out_fields[0].out_value = a;
h_u32_to_be(a, address);
out_fields[0].in_value = NULL;
out_fields[1].num_bits = 8;
out_fields[1].out_value = &data;
out_fields[1].in_value = NULL;
return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_BYTE,
ARRAY_SIZE(out_fields), out_fields, 0, NULL);
}{ ... }
int esirisc_jtag_write_hword(struct esirisc_jtag *jtag_info, uint32_t address, uint16_t data)
{
struct scan_field out_fields[2];
uint8_t a[4], d[2];
LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx16, address, data);
out_fields[0].num_bits = 32;
out_fields[0].out_value = a;
h_u32_to_be(a, address);
out_fields[0].in_value = NULL;
out_fields[1].num_bits = 16;
out_fields[1].out_value = d;
h_u16_to_be(d, data);
out_fields[1].in_value = NULL;
return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_HWORD,
ARRAY_SIZE(out_fields), out_fields, 0, NULL);
}{ ... }
int esirisc_jtag_write_word(struct esirisc_jtag *jtag_info, uint32_t address, uint32_t data)
{
struct scan_field out_fields[2];
uint8_t a[4], d[4];
LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx32, address, data);
out_fields[0].num_bits = 32;
out_fields[0].out_value = a;
h_u32_to_be(a, address);
out_fields[0].in_value = NULL;
out_fields[1].num_bits = 32;
out_fields[1].out_value = d;
h_u32_to_be(d, data);
out_fields[1].in_value = NULL;
return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_WORD,
ARRAY_SIZE(out_fields), out_fields, 0, NULL);
}{ ... }
int esirisc_jtag_read_reg(struct esirisc_jtag *jtag_info, uint8_t reg, uint32_t *data)
{
struct scan_field out_fields[1];
out_fields[0].num_bits = 8;
out_fields[0].out_value = ®
out_fields[0].in_value = NULL;
struct scan_field in_fields[1];
uint8_t d[4];
in_fields[0].num_bits = 32;
in_fields[0].out_value = NULL;
in_fields[0].in_value = d;
int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_REG,
ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
if (retval != ERROR_OK)
return retval;
*data = le_to_h_u32(d);
LOG_DEBUG("register: 0x%" PRIx8 ", data: 0x%" PRIx32, reg, *data);
return ERROR_OK;
}{ ... }
int esirisc_jtag_write_reg(struct esirisc_jtag *jtag_info, uint8_t reg, uint32_t data)
{
struct scan_field out_fields[2];
uint8_t d[4];
LOG_DEBUG("register: 0x%" PRIx8 ", data: 0x%" PRIx32, reg, data);
out_fields[0].num_bits = 8;
out_fields[0].out_value = ®
out_fields[0].in_value = NULL;
out_fields[1].num_bits = 32;
out_fields[1].out_value = d;
h_u32_to_be(d, data);
out_fields[1].in_value = NULL;
return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_REG,
ARRAY_SIZE(out_fields), out_fields, 0, NULL);
}{ ... }
int esirisc_jtag_read_csr(struct esirisc_jtag *jtag_info, uint8_t bank, uint8_t csr, uint32_t *data)
{
struct scan_field out_fields[1];
uint8_t c[2];
out_fields[0].num_bits = 16;
out_fields[0].out_value = c;
h_u16_to_be(c, (csr << 5) | bank);
out_fields[0].in_value = NULL;
struct scan_field in_fields[1];
uint8_t d[4];
in_fields[0].num_bits = 32;
in_fields[0].out_value = NULL;
in_fields[0].in_value = d;
int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_CSR,
ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
if (retval != ERROR_OK)
return retval;
*data = le_to_h_u32(d);
LOG_DEBUG("bank: 0x%" PRIx8 ", csr: 0x%" PRIx8 ", data: 0x%" PRIx32, bank, csr, *data);
return ERROR_OK;
}{ ... }
int esirisc_jtag_write_csr(struct esirisc_jtag *jtag_info, uint8_t bank, uint8_t csr, uint32_t data)
{
struct scan_field out_fields[2];
uint8_t c[2], d[4];
LOG_DEBUG("bank: 0x%" PRIx8 ", csr: 0x%" PRIx8 ", data: 0x%" PRIx32, bank, csr, data);
out_fields[0].num_bits = 16;
out_fields[0].out_value = c;
h_u16_to_be(c, (csr << 5) | bank);
out_fields[0].in_value = NULL;
out_fields[1].num_bits = 32;
out_fields[1].out_value = d;
h_u32_to_be(d, data);
out_fields[1].in_value = NULL;
return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_CSR,
ARRAY_SIZE(out_fields), out_fields, 0, NULL);
}{ ... }
/* ... */
static inline int esirisc_jtag_send_ctrl(struct esirisc_jtag *jtag_info, uint8_t command)
{
return esirisc_jtag_send_and_recv(jtag_info, command, 0, NULL, 0, NULL);
}{ ... }
int esirisc_jtag_enable_debug(struct esirisc_jtag *jtag_info)
{
return esirisc_jtag_send_ctrl(jtag_info, DEBUG_ENABLE_DEBUG);
}{ ... }
int esirisc_jtag_disable_debug(struct esirisc_jtag *jtag_info)
{
return esirisc_jtag_send_ctrl(jtag_info, DEBUG_DISABLE_DEBUG);
}{ ... }
int esirisc_jtag_assert_reset(struct esirisc_jtag *jtag_info)
{
return esirisc_jtag_send_ctrl(jtag_info, DEBUG_ASSERT_RESET);
}{ ... }
int esirisc_jtag_deassert_reset(struct esirisc_jtag *jtag_info)
{
return esirisc_jtag_send_ctrl(jtag_info, DEBUG_DEASSERT_RESET);
}{ ... }
int esirisc_jtag_break(struct esirisc_jtag *jtag_info)
{
return esirisc_jtag_send_ctrl(jtag_info, DEBUG_BREAK);
}{ ... }
int esirisc_jtag_continue(struct esirisc_jtag *jtag_info)
{
return esirisc_jtag_send_ctrl(jtag_info, DEBUG_CONTINUE);
}{ ... }
int esirisc_jtag_flush_caches(struct esirisc_jtag *jtag_info)
{
return esirisc_jtag_send_ctrl(jtag_info, DEBUG_FLUSH_CACHES);
}{ ... }