dd5cd5a0ee792b1f17bffc000e845e787c33c96e
[fw/openocd] / src / target / esirisc_jtag.c
1 /***************************************************************************
2  *   Copyright (C) 2018 by Square, Inc.                                    *
3  *   Steven Stallion <stallion@squareup.com>                               *
4  *   James Zhao <hjz@squareup.com>                                         *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
18  ***************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <helper/binarybuffer.h>
25 #include <helper/log.h>
26 #include <helper/types.h>
27 #include <jtag/jtag.h>
28 #include <jtag/commands.h>
29 #include <jtag/interface.h>
30
31 #include "esirisc_jtag.h"
32
33 static void esirisc_jtag_set_instr(struct esirisc_jtag *jtag_info, uint32_t new_instr)
34 {
35         struct jtag_tap *tap = jtag_info->tap;
36
37         if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr) {
38                 struct scan_field field;
39                 uint8_t t[4] = { 0 };
40
41                 field.num_bits = tap->ir_length;
42                 field.out_value = t;
43                 buf_set_u32(t, 0, field.num_bits, new_instr);
44                 field.in_value = NULL;
45
46                 jtag_add_ir_scan(tap, &field, TAP_IDLE);
47         }
48 }
49
50 /*
51  * The data register is latched every 8 bits while in the Shift-DR state
52  * (Update-DR is not supported). This necessitates prepending padding
53  * bits to ensure data is aligned when multiple TAPs are present.
54  */
55 static int esirisc_jtag_get_padding(void)
56 {
57         int padding = 0;
58         int bypass_devices = 0;
59
60         for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap;
61                         tap = jtag_tap_next_enabled(tap))
62                 if (tap->bypass)
63                         bypass_devices++;
64
65         int num_bits = bypass_devices % 8;
66         if (num_bits > 0)
67                 padding = 8 - num_bits;
68
69         return padding;
70 }
71
72 static int esirisc_jtag_count_bits(int num_fields, struct scan_field *fields)
73 {
74         int bit_count = 0;
75
76         for (int i = 0; i < num_fields; ++i)
77                 bit_count += fields[i].num_bits;
78
79         return bit_count;
80 }
81
82 /*
83  * Data received from the target will be byte-stuffed if it contains
84  * either the pad byte (0xAA) or stuffing marker (0x55). Buffers should
85  * be sized twice the expected length to account for stuffing overhead.
86  */
87 static void esirisc_jtag_unstuff(uint8_t *data, size_t len)
88 {
89         uint8_t *r, *w;
90         uint8_t *end;
91
92         r = w = data;
93         end = data + len;
94         while (r < end) {
95                 if (*r == STUFF_MARKER) {
96                         r++; /* skip stuffing marker */
97                         assert(r < end);
98                         *w++ = *r++ ^ STUFF_MARKER;
99                 } else
100                         *w++ = *r++;
101         }
102 }
103
104 /*
105  * The eSi-Debug protocol defines a byte-oriented command/response
106  * channel that operates over serial or JTAG. While not strictly
107  * required, separate DR scans are used for sending and receiving data.
108  * This allows the TAP to recover gracefully if the byte stream is
109  * corrupted at the expense of sending additional padding bits.
110  */
111
112 static int esirisc_jtag_send(struct esirisc_jtag *jtag_info, uint8_t command,
113                 int num_out_fields, struct scan_field *out_fields)
114 {
115         int num_fields = 2 + num_out_fields;
116         struct scan_field *fields = cmd_queue_alloc(num_fields * sizeof(struct scan_field));
117
118         esirisc_jtag_set_instr(jtag_info, INSTR_DEBUG);
119
120         fields[0].num_bits = esirisc_jtag_get_padding();
121         fields[0].out_value = NULL;
122         fields[0].in_value = NULL;
123
124         fields[1].num_bits = 8;
125         fields[1].out_value = &command;
126         fields[1].in_value = NULL;
127
128         /* append command data */
129         for (int i = 0; i < num_out_fields; ++i)
130                 jtag_scan_field_clone(&fields[2+i], &out_fields[i]);
131
132         jtag_add_dr_scan(jtag_info->tap, num_fields, fields, TAP_IDLE);
133
134         return jtag_execute_queue();
135 }
136
137 static int esirisc_jtag_recv(struct esirisc_jtag *jtag_info,
138                 int num_in_fields, struct scan_field *in_fields)
139 {
140         int num_in_bits = esirisc_jtag_count_bits(num_in_fields, in_fields);
141         int num_in_bytes = DIV_ROUND_UP(num_in_bits, 8);
142
143         struct scan_field fields[3];
144         uint8_t r[num_in_bytes * 2];
145
146         esirisc_jtag_set_instr(jtag_info, INSTR_DEBUG);
147
148         fields[0].num_bits = esirisc_jtag_get_padding() + 1;
149         fields[0].out_value = NULL;
150         fields[0].in_value = NULL;
151
152         fields[1].num_bits = 8;
153         fields[1].out_value = NULL;
154         fields[1].in_value = &jtag_info->status;
155
156         fields[2].num_bits = num_in_bits * 2;
157         fields[2].out_value = NULL;
158         fields[2].in_value = r;
159
160         jtag_add_dr_scan(jtag_info->tap, ARRAY_SIZE(fields), fields, TAP_IDLE);
161
162         int retval = jtag_execute_queue();
163         if (retval != ERROR_OK)
164                 return retval;
165
166         /* unstuff response data and write back to caller */
167         if (num_in_fields > 0) {
168                 esirisc_jtag_unstuff(r, ARRAY_SIZE(r));
169
170                 int bit_count = 0;
171                 for (int i = 0; i < num_in_fields; ++i) {
172                         buf_set_buf(r, bit_count, in_fields[i].in_value, 0, in_fields[i].num_bits);
173                         bit_count += in_fields[i].num_bits;
174                 }
175         }
176
177         return ERROR_OK;
178 }
179
180 static int esirisc_jtag_check_status(struct esirisc_jtag *jtag_info)
181 {
182         uint8_t eid = esirisc_jtag_get_eid(jtag_info);
183         if (eid != EID_NONE) {
184                 LOG_ERROR("esirisc_jtag: bad status: 0x%02" PRIx8 " (DA: %" PRId32 ", "
185                                 "S: %" PRId32 ", EID: 0x%02" PRIx8 ")",
186                                 jtag_info->status, esirisc_jtag_is_debug_active(jtag_info),
187                                 esirisc_jtag_is_stopped(jtag_info), eid);
188                 return ERROR_FAIL;
189         }
190
191         return ERROR_OK;
192 }
193
194 static int esirisc_jtag_send_and_recv(struct esirisc_jtag *jtag_info, uint8_t command,
195                 int num_out_fields, struct scan_field *out_fields,
196                 int num_in_fields, struct scan_field *in_fields)
197 {
198         int retval;
199
200         jtag_info->status = 0;  /* clear status */
201
202         retval = esirisc_jtag_send(jtag_info, command, num_out_fields, out_fields);
203         if (retval != ERROR_OK) {
204                 LOG_ERROR("esirisc_jtag: send failed (command: 0x%02" PRIx8 ")", command);
205                 return ERROR_FAIL;
206         }
207
208         retval = esirisc_jtag_recv(jtag_info, num_in_fields, in_fields);
209         if (retval != ERROR_OK) {
210                 LOG_ERROR("esirisc_jtag: recv failed (command: 0x%02" PRIx8 ")", command);
211                 return ERROR_FAIL;
212         }
213
214         return esirisc_jtag_check_status(jtag_info);
215 }
216
217 /*
218  * Status is automatically updated after each command completes;
219  * these functions make each field available to the caller.
220  */
221
222 bool esirisc_jtag_is_debug_active(struct esirisc_jtag *jtag_info)
223 {
224         return !!(jtag_info->status & 1<<7);    /* DA */
225 }
226
227 bool esirisc_jtag_is_stopped(struct esirisc_jtag *jtag_info)
228 {
229         return !!(jtag_info->status & 1<<6);    /* S */
230 }
231
232 uint8_t esirisc_jtag_get_eid(struct esirisc_jtag *jtag_info)
233 {
234         return jtag_info->status & 0x3f;                /* EID */
235 }
236
237 /*
238  * Most commands manipulate target data (eg. memory and registers); each
239  * command returns a status byte that indicates success. Commands must
240  * transmit multibyte values in big-endian order, however response
241  * values are in little-endian order. Target endianness does not have an
242  * effect on this ordering.
243  */
244
245 int esirisc_jtag_read_byte(struct esirisc_jtag *jtag_info, uint32_t address, uint8_t *data)
246 {
247         struct scan_field out_fields[1];
248         uint8_t a[4];
249
250         out_fields[0].num_bits = 32;
251         out_fields[0].out_value = a;
252         h_u32_to_be(a, address);
253         out_fields[0].in_value = NULL;
254
255         struct scan_field in_fields[1];
256         uint8_t d[1];
257
258         in_fields[0].num_bits = 8;
259         in_fields[0].out_value = NULL;
260         in_fields[0].in_value = d;
261
262         int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_BYTE,
263                         ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
264         if (retval != ERROR_OK)
265                 return retval;
266
267         *data = *d;
268         LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx8, address, *data);
269
270         return ERROR_OK;
271 }
272
273 int esirisc_jtag_read_hword(struct esirisc_jtag *jtag_info, uint32_t address, uint16_t *data)
274 {
275         struct scan_field out_fields[1];
276         uint8_t a[4];
277
278         out_fields[0].num_bits = 32;
279         out_fields[0].out_value = a;
280         h_u32_to_be(a, address);
281         out_fields[0].in_value = NULL;
282
283         struct scan_field in_fields[1];
284         uint8_t d[2];
285
286         in_fields[0].num_bits = 16;
287         in_fields[0].out_value = NULL;
288         in_fields[0].in_value = d;
289
290         int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_HWORD,
291                         ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
292         if (retval != ERROR_OK)
293                 return retval;
294
295         *data = le_to_h_u16(d);
296         LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx16, address, *data);
297
298         return ERROR_OK;
299 }
300
301 int esirisc_jtag_read_word(struct esirisc_jtag *jtag_info, uint32_t address, uint32_t *data)
302 {
303         struct scan_field out_fields[1];
304         uint8_t a[4];
305
306         out_fields[0].num_bits = 32;
307         out_fields[0].out_value = a;
308         h_u32_to_be(a, address);
309         out_fields[0].in_value = NULL;
310
311         struct scan_field in_fields[1];
312         uint8_t d[4];
313
314         in_fields[0].num_bits = 32;
315         in_fields[0].out_value = NULL;
316         in_fields[0].in_value = d;
317
318         int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_WORD,
319                         ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
320         if (retval != ERROR_OK)
321                 return retval;
322
323         *data = le_to_h_u32(d);
324         LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx32, address, *data);
325
326         return ERROR_OK;
327 }
328
329 int esirisc_jtag_write_byte(struct esirisc_jtag *jtag_info, uint32_t address, uint8_t data)
330 {
331         struct scan_field out_fields[2];
332         uint8_t a[4];
333
334         LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx8, address, data);
335
336         out_fields[0].num_bits = 32;
337         out_fields[0].out_value = a;
338         h_u32_to_be(a, address);
339         out_fields[0].in_value = NULL;
340
341         out_fields[1].num_bits = 8;
342         out_fields[1].out_value = &data;
343         out_fields[1].in_value = NULL;
344
345         return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_BYTE,
346                         ARRAY_SIZE(out_fields), out_fields, 0, NULL);
347 }
348
349 int esirisc_jtag_write_hword(struct esirisc_jtag *jtag_info, uint32_t address, uint16_t data)
350 {
351         struct scan_field out_fields[2];
352         uint8_t a[4], d[2];
353
354         LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx16, address, data);
355
356         out_fields[0].num_bits = 32;
357         out_fields[0].out_value = a;
358         h_u32_to_be(a, address);
359         out_fields[0].in_value = NULL;
360
361         out_fields[1].num_bits = 16;
362         out_fields[1].out_value = d;
363         h_u16_to_be(d, data);
364         out_fields[1].in_value = NULL;
365
366         return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_HWORD,
367                         ARRAY_SIZE(out_fields), out_fields, 0, NULL);
368 }
369
370 int esirisc_jtag_write_word(struct esirisc_jtag *jtag_info, uint32_t address, uint32_t data)
371 {
372         struct scan_field out_fields[2];
373         uint8_t a[4], d[4];
374
375         LOG_DEBUG("address: 0x%" PRIx32 ", data: 0x%" PRIx32, address, data);
376
377         out_fields[0].num_bits = 32;
378         out_fields[0].out_value = a;
379         h_u32_to_be(a, address);
380         out_fields[0].in_value = NULL;
381
382         out_fields[1].num_bits = 32;
383         out_fields[1].out_value = d;
384         h_u32_to_be(d, data);
385         out_fields[1].in_value = NULL;
386
387         return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_WORD,
388                         ARRAY_SIZE(out_fields), out_fields, 0, NULL);
389 }
390
391 int esirisc_jtag_read_reg(struct esirisc_jtag *jtag_info, uint8_t reg, uint32_t *data)
392 {
393         struct scan_field out_fields[1];
394
395         out_fields[0].num_bits = 8;
396         out_fields[0].out_value = &reg;
397         out_fields[0].in_value = NULL;
398
399         struct scan_field in_fields[1];
400         uint8_t d[4];
401
402         in_fields[0].num_bits = 32;
403         in_fields[0].out_value = NULL;
404         in_fields[0].in_value = d;
405
406         int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_REG,
407                         ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
408         if (retval != ERROR_OK)
409                 return retval;
410
411         *data = le_to_h_u32(d);
412         LOG_DEBUG("register: 0x%" PRIx8 ", data: 0x%" PRIx32, reg, *data);
413
414         return ERROR_OK;
415 }
416
417 int esirisc_jtag_write_reg(struct esirisc_jtag *jtag_info, uint8_t reg, uint32_t data)
418 {
419         struct scan_field out_fields[2];
420         uint8_t d[4];
421
422         LOG_DEBUG("register: 0x%" PRIx8 ", data: 0x%" PRIx32, reg, data);
423
424         out_fields[0].num_bits = 8;
425         out_fields[0].out_value = &reg;
426         out_fields[0].in_value = NULL;
427
428         out_fields[1].num_bits = 32;
429         out_fields[1].out_value = d;
430         h_u32_to_be(d, data);
431         out_fields[1].in_value = NULL;
432
433         return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_REG,
434                         ARRAY_SIZE(out_fields), out_fields, 0, NULL);
435 }
436
437 int esirisc_jtag_read_csr(struct esirisc_jtag *jtag_info, uint8_t bank, uint8_t csr, uint32_t *data)
438 {
439         struct scan_field out_fields[1];
440         uint8_t c[2];
441
442         out_fields[0].num_bits = 16;
443         out_fields[0].out_value = c;
444         h_u16_to_be(c, (csr << 5) | bank);
445         out_fields[0].in_value = NULL;
446
447         struct scan_field in_fields[1];
448         uint8_t d[4];
449
450         in_fields[0].num_bits = 32;
451         in_fields[0].out_value = NULL;
452         in_fields[0].in_value = d;
453
454         int retval = esirisc_jtag_send_and_recv(jtag_info, DEBUG_READ_CSR,
455                         ARRAY_SIZE(out_fields), out_fields, ARRAY_SIZE(in_fields), in_fields);
456         if (retval != ERROR_OK)
457                 return retval;
458
459         *data = le_to_h_u32(d);
460         LOG_DEBUG("bank: 0x%" PRIx8 ", csr: 0x%" PRIx8 ", data: 0x%" PRIx32, bank, csr, *data);
461
462         return ERROR_OK;
463 }
464
465 int esirisc_jtag_write_csr(struct esirisc_jtag *jtag_info, uint8_t bank, uint8_t csr, uint32_t data)
466 {
467         struct scan_field out_fields[2];
468         uint8_t c[2], d[4];
469
470         LOG_DEBUG("bank: 0x%" PRIx8 ", csr: 0x%" PRIx8 ", data: 0x%" PRIx32, bank, csr, data);
471
472         out_fields[0].num_bits = 16;
473         out_fields[0].out_value = c;
474         h_u16_to_be(c, (csr << 5) | bank);
475         out_fields[0].in_value = NULL;
476
477         out_fields[1].num_bits = 32;
478         out_fields[1].out_value = d;
479         h_u32_to_be(d, data);
480         out_fields[1].in_value = NULL;
481
482         return esirisc_jtag_send_and_recv(jtag_info, DEBUG_WRITE_CSR,
483                         ARRAY_SIZE(out_fields), out_fields, 0, NULL);
484 }
485
486 /*
487  * Control commands affect CPU operation; these commands send no data
488  * and return a status byte.
489  */
490
491 static inline int esirisc_jtag_send_ctrl(struct esirisc_jtag *jtag_info, uint8_t command)
492 {
493         return esirisc_jtag_send_and_recv(jtag_info, command, 0, NULL, 0, NULL);
494 }
495
496 int esirisc_jtag_enable_debug(struct esirisc_jtag *jtag_info)
497 {
498         return esirisc_jtag_send_ctrl(jtag_info, DEBUG_ENABLE_DEBUG);
499 }
500
501 int esirisc_jtag_disable_debug(struct esirisc_jtag *jtag_info)
502 {
503         return esirisc_jtag_send_ctrl(jtag_info, DEBUG_DISABLE_DEBUG);
504 }
505
506 int esirisc_jtag_assert_reset(struct esirisc_jtag *jtag_info)
507 {
508         return esirisc_jtag_send_ctrl(jtag_info, DEBUG_ASSERT_RESET);
509 }
510
511 int esirisc_jtag_deassert_reset(struct esirisc_jtag *jtag_info)
512 {
513         return esirisc_jtag_send_ctrl(jtag_info, DEBUG_DEASSERT_RESET);
514 }
515
516 int esirisc_jtag_break(struct esirisc_jtag *jtag_info)
517 {
518         return esirisc_jtag_send_ctrl(jtag_info, DEBUG_BREAK);
519 }
520
521 int esirisc_jtag_continue(struct esirisc_jtag *jtag_info)
522 {
523         return esirisc_jtag_send_ctrl(jtag_info, DEBUG_CONTINUE);
524 }
525
526 int esirisc_jtag_flush_caches(struct esirisc_jtag *jtag_info)
527 {
528         return esirisc_jtag_send_ctrl(jtag_info, DEBUG_FLUSH_CACHES);
529 }