1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2013-2014 by Franck Jullien *
7 * Inspired from adv_jtag_bridge which is: *
8 * Copyright (C) 2008-2010 Nathan Yawn *
9 * nyawn@opencores.net *
11 * And the Mohor interface version of this file which is: *
12 * Copyright (C) 2011 by Julius Baxter *
13 * julius@opencores.org *
14 ***************************************************************************/
23 #include "jsp_server.h"
25 #include <helper/crc32.h>
26 #include <jtag/jtag.h>
27 #include <target/target.h>
29 #define JSP_BANNER "\n\r" \
30 "******************************\n\r" \
31 "** JTAG Serial Port **\n\r" \
32 "******************************\n\r" \
37 /* This an option to the adv debug unit.
38 * If this is defined, status bits will be skipped on burst
39 * reads and writes to improve download speeds.
40 * This option must match the RTL configured option.
42 #define ADBG_USE_HISPEED 1
44 /* This an option to the adv debug unit.
45 * If this is defined, the JTAG Serial Port Server is started.
46 * This option must match the RTL configured option.
48 #define ENABLE_JSP_SERVER 2
50 /* Define this if you intend to use the JSP in a system with multiple
51 * devices on the JTAG chain
53 #define ENABLE_JSP_MULTI 4
55 /* Definitions for the top-level debug unit. This really just consists
56 * of a single register, used to select the active debug module ("chain").
58 #define DBG_MODULE_SELECT_REG_SIZE 2
59 #define DBG_MAX_MODULES 4
67 /* CPU control register bits mask */
68 #define DBG_CPU_CR_STALL 0x01
69 #define DBG_CPU_CR_RESET 0x02
71 /* These are for the internal registers in the Wishbone module
72 * The first is the length of the index register,
73 * the indexes of the various registers are defined after that.
75 #define DBG_WB_REG_SEL_LEN 1
76 #define DBG_WB_REG_ERROR 0
78 /* Opcode definitions for the Wishbone module. */
79 #define DBG_WB_OPCODE_LEN 4
80 #define DBG_WB_CMD_NOP 0x0
81 #define DBG_WB_CMD_BWRITE8 0x1
82 #define DBG_WB_CMD_BWRITE16 0x2
83 #define DBG_WB_CMD_BWRITE32 0x3
84 #define DBG_WB_CMD_BREAD8 0x5
85 #define DBG_WB_CMD_BREAD16 0x6
86 #define DBG_WB_CMD_BREAD32 0x7
87 #define DBG_WB_CMD_IREG_WR 0x9
88 #define DBG_WB_CMD_IREG_SEL 0xd
90 /* Internal register definitions for the CPU0 module. */
91 #define DBG_CPU0_REG_SEL_LEN 1
92 #define DBG_CPU0_REG_STATUS 0
94 /* Opcode definitions for the first CPU module. */
95 #define DBG_CPU0_OPCODE_LEN 4
96 #define DBG_CPU0_CMD_NOP 0x0
97 #define DBG_CPU0_CMD_BWRITE32 0x3
98 #define DBG_CPU0_CMD_BREAD32 0x7
99 #define DBG_CPU0_CMD_IREG_WR 0x9
100 #define DBG_CPU0_CMD_IREG_SEL 0xd
102 /* Internal register definitions for the CPU1 module. */
103 #define DBG_CPU1_REG_SEL_LEN 1
104 #define DBG_CPU1_REG_STATUS 0
106 /* Opcode definitions for the second CPU module. */
107 #define DBG_CPU1_OPCODE_LEN 4
108 #define DBG_CPU1_CMD_NOP 0x0
109 #define DBG_CPU1_CMD_BWRITE32 0x3
110 #define DBG_CPU1_CMD_BREAD32 0x7
111 #define DBG_CPU1_CMD_IREG_WR 0x9
112 #define DBG_CPU1_CMD_IREG_SEL 0xd
114 #define MAX_READ_STATUS_WAIT 10
115 #define MAX_READ_BUSY_RETRY 2
116 #define MAX_READ_CRC_RETRY 2
117 #define MAX_WRITE_CRC_RETRY 2
118 #define BURST_READ_READY 1
119 #define MAX_BUS_ERRORS 2
121 #define MAX_BURST_SIZE (4 * 1024)
123 #define STATUS_BYTES 1
126 static struct or1k_du or1k_du_adv
;
128 static const char * const chain_name
[] = {"WISHBONE", "CPU0", "CPU1", "JSP"};
130 static int find_status_bit(void *_buf
, int len
)
137 while (!(buf
[i
] & (1 << count
++)) && (i
< len
)) {
145 ret
= (i
* 8) + count
;
150 static int or1k_adv_jtag_init(struct or1k_jtag
*jtag_info
)
152 struct or1k_tap_ip
*tap_ip
= jtag_info
->tap_ip
;
154 int retval
= tap_ip
->init(jtag_info
);
155 if (retval
!= ERROR_OK
) {
156 LOG_ERROR("TAP initialization failed");
160 /* TAP is now configured to communicate with debug interface */
161 jtag_info
->or1k_jtag_inited
= 1;
163 /* TAP reset - not sure what state debug module chain is in now */
164 jtag_info
->or1k_jtag_module_selected
= DC_NONE
;
166 jtag_info
->current_reg_idx
= malloc(DBG_MAX_MODULES
* sizeof(uint8_t));
167 memset(jtag_info
->current_reg_idx
, 0, DBG_MAX_MODULES
* sizeof(uint8_t));
169 if (or1k_du_adv
.options
& ADBG_USE_HISPEED
)
170 LOG_INFO("adv debug unit is configured with option ADBG_USE_HISPEED");
172 if (or1k_du_adv
.options
& ENABLE_JSP_SERVER
) {
173 if (or1k_du_adv
.options
& ENABLE_JSP_MULTI
)
174 LOG_INFO("adv debug unit is configured with option ENABLE_JSP_MULTI");
175 LOG_INFO("adv debug unit is configured with option ENABLE_JSP_SERVER");
176 retval
= jsp_init(jtag_info
, JSP_BANNER
);
177 if (retval
!= ERROR_OK
) {
178 LOG_ERROR("Couldn't start the JSP server");
183 LOG_DEBUG("Init done");
189 /* Selects one of the modules in the debug unit
190 * (e.g. wishbone unit, CPU0, etc.)
192 static int adbg_select_module(struct or1k_jtag
*jtag_info
, int chain
)
194 if (jtag_info
->or1k_jtag_module_selected
== chain
)
197 /* MSB of the data out must be set to 1, indicating a module
200 uint8_t data
= chain
| (1 << DBG_MODULE_SELECT_REG_SIZE
);
202 LOG_DEBUG("Select module: %s", chain_name
[chain
]);
204 struct scan_field field
;
206 field
.num_bits
= (DBG_MODULE_SELECT_REG_SIZE
+ 1);
207 field
.out_value
= &data
;
208 field
.in_value
= NULL
;
209 jtag_add_dr_scan(jtag_info
->tap
, 1, &field
, TAP_IDLE
);
211 int retval
= jtag_execute_queue();
212 if (retval
!= ERROR_OK
)
215 jtag_info
->or1k_jtag_module_selected
= chain
;
220 /* Set the index of the desired register in the currently selected module
221 * 1 bit module select command
225 static int adbg_select_ctrl_reg(struct or1k_jtag
*jtag_info
, uint8_t regidx
)
231 /* If this reg is already selected, don't do a JTAG transaction */
232 if (jtag_info
->current_reg_idx
[jtag_info
->or1k_jtag_module_selected
] == regidx
)
235 switch (jtag_info
->or1k_jtag_module_selected
) {
237 index_len
= DBG_WB_REG_SEL_LEN
;
238 opcode
= DBG_WB_CMD_IREG_SEL
;
239 opcode_len
= DBG_WB_OPCODE_LEN
;
242 index_len
= DBG_CPU0_REG_SEL_LEN
;
243 opcode
= DBG_CPU0_CMD_IREG_SEL
;
244 opcode_len
= DBG_CPU0_OPCODE_LEN
;
247 index_len
= DBG_CPU1_REG_SEL_LEN
;
248 opcode
= DBG_CPU1_CMD_IREG_SEL
;
249 opcode_len
= DBG_CPU1_OPCODE_LEN
;
252 LOG_ERROR("Illegal debug chain selected (%i) while selecting control register",
253 jtag_info
->or1k_jtag_module_selected
);
257 /* MSB must be 0 to access modules */
258 uint32_t data
= (opcode
& ~(1 << opcode_len
)) << index_len
;
261 struct scan_field field
;
263 field
.num_bits
= (opcode_len
+ 1) + index_len
;
264 field
.out_value
= (uint8_t *)&data
;
265 field
.in_value
= NULL
;
266 jtag_add_dr_scan(jtag_info
->tap
, 1, &field
, TAP_IDLE
);
268 int retval
= jtag_execute_queue();
269 if (retval
!= ERROR_OK
)
272 jtag_info
->current_reg_idx
[jtag_info
->or1k_jtag_module_selected
] = regidx
;
277 /* Write control register (internal to the debug unit) */
278 static int adbg_ctrl_write(struct or1k_jtag
*jtag_info
, uint8_t regidx
,
279 uint32_t *cmd_data
, int length_bits
)
285 LOG_DEBUG("Write control register %" PRId8
": 0x%08" PRIx32
, regidx
, cmd_data
[0]);
287 int retval
= adbg_select_ctrl_reg(jtag_info
, regidx
);
288 if (retval
!= ERROR_OK
) {
289 LOG_ERROR("Error while calling adbg_select_ctrl_reg");
293 switch (jtag_info
->or1k_jtag_module_selected
) {
295 index_len
= DBG_WB_REG_SEL_LEN
;
296 opcode
= DBG_WB_CMD_IREG_WR
;
297 opcode_len
= DBG_WB_OPCODE_LEN
;
300 index_len
= DBG_CPU0_REG_SEL_LEN
;
301 opcode
= DBG_CPU0_CMD_IREG_WR
;
302 opcode_len
= DBG_CPU0_OPCODE_LEN
;
305 index_len
= DBG_CPU1_REG_SEL_LEN
;
306 opcode
= DBG_CPU1_CMD_IREG_WR
;
307 opcode_len
= DBG_CPU1_OPCODE_LEN
;
310 LOG_ERROR("Illegal debug chain selected (%i) while doing control write",
311 jtag_info
->or1k_jtag_module_selected
);
315 struct scan_field field
[2];
317 /* MSB must be 0 to access modules */
318 uint32_t data
= (opcode
& ~(1 << opcode_len
)) << index_len
;
321 field
[0].num_bits
= length_bits
;
322 field
[0].out_value
= (uint8_t *)cmd_data
;
323 field
[0].in_value
= NULL
;
325 field
[1].num_bits
= (opcode_len
+ 1) + index_len
;
326 field
[1].out_value
= (uint8_t *)&data
;
327 field
[1].in_value
= NULL
;
329 jtag_add_dr_scan(jtag_info
->tap
, 2, field
, TAP_IDLE
);
331 return jtag_execute_queue();
334 /* Reads control register (internal to the debug unit) */
335 static int adbg_ctrl_read(struct or1k_jtag
*jtag_info
, uint32_t regidx
,
336 uint32_t *data
, int length_bits
)
339 int retval
= adbg_select_ctrl_reg(jtag_info
, regidx
);
340 if (retval
!= ERROR_OK
) {
341 LOG_ERROR("Error while calling adbg_select_ctrl_reg");
348 /* There is no 'read' command, We write a NOP to read */
349 switch (jtag_info
->or1k_jtag_module_selected
) {
351 opcode
= DBG_WB_CMD_NOP
;
352 opcode_len
= DBG_WB_OPCODE_LEN
;
355 opcode
= DBG_CPU0_CMD_NOP
;
356 opcode_len
= DBG_CPU0_OPCODE_LEN
;
359 opcode
= DBG_CPU1_CMD_NOP
;
360 opcode_len
= DBG_CPU1_OPCODE_LEN
;
363 LOG_ERROR("Illegal debug chain selected (%i) while doing control read",
364 jtag_info
->or1k_jtag_module_selected
);
368 /* Zero MSB = op for module, not top-level debug unit */
369 uint32_t outdata
= opcode
& ~(0x1 << opcode_len
);
371 struct scan_field field
[2];
373 field
[0].num_bits
= length_bits
;
374 field
[0].out_value
= NULL
;
375 field
[0].in_value
= (uint8_t *)data
;
377 field
[1].num_bits
= opcode_len
+ 1;
378 field
[1].out_value
= (uint8_t *)&outdata
;
379 field
[1].in_value
= NULL
;
381 jtag_add_dr_scan(jtag_info
->tap
, 2, field
, TAP_IDLE
);
383 return jtag_execute_queue();
386 /* sends out a burst command to the selected module in the debug unit (MSB to LSB):
387 * 1-bit module command
390 * 16-bit length (of the burst, in words)
392 static int adbg_burst_command(struct or1k_jtag
*jtag_info
, uint32_t opcode
,
393 uint32_t address
, uint16_t length_words
)
397 /* Set up the data */
398 data
[0] = length_words
| (address
<< 16);
399 /* MSB must be 0 to access modules */
400 data
[1] = ((address
>> 16) | ((opcode
& 0xf) << 16)) & ~(0x1 << 20);
402 struct scan_field field
;
405 field
.out_value
= (uint8_t *)&data
[0];
406 field
.in_value
= NULL
;
408 jtag_add_dr_scan(jtag_info
->tap
, 1, &field
, TAP_IDLE
);
410 return jtag_execute_queue();
413 static int adbg_wb_burst_read(struct or1k_jtag
*jtag_info
, int size
,
414 int count
, uint32_t start_address
, uint8_t *data
)
416 int retry_full_crc
= 0;
417 int retry_full_busy
= 0;
421 LOG_DEBUG("Doing burst read, word size %d, word count %d, start address 0x%08" PRIx32
,
422 size
, count
, start_address
);
424 /* Select the appropriate opcode */
425 switch (jtag_info
->or1k_jtag_module_selected
) {
428 opcode
= DBG_WB_CMD_BREAD8
;
430 opcode
= DBG_WB_CMD_BREAD16
;
432 opcode
= DBG_WB_CMD_BREAD32
;
434 LOG_WARNING("Tried burst read with invalid word size (%d),"
435 "defaulting to 4-byte words", size
);
436 opcode
= DBG_WB_CMD_BREAD32
;
441 opcode
= DBG_CPU0_CMD_BREAD32
;
443 LOG_WARNING("Tried burst read with invalid word size (%d),"
444 "defaulting to 4-byte words", size
);
445 opcode
= DBG_CPU0_CMD_BREAD32
;
450 opcode
= DBG_CPU1_CMD_BREAD32
;
452 LOG_WARNING("Tried burst read with invalid word size (%d),"
453 "defaulting to 4-byte words", size
);
454 opcode
= DBG_CPU0_CMD_BREAD32
;
458 LOG_ERROR("Illegal debug chain selected (%i) while doing burst read",
459 jtag_info
->or1k_jtag_module_selected
);
463 int total_size_bytes
= count
* size
;
464 struct scan_field field
;
465 uint8_t *in_buffer
= malloc(total_size_bytes
+ CRC_LEN
+ STATUS_BYTES
);
469 /* Send the BURST READ command, returns TAP to idle state */
470 retval
= adbg_burst_command(jtag_info
, opcode
, start_address
, count
);
471 if (retval
!= ERROR_OK
)
474 field
.num_bits
= (total_size_bytes
+ CRC_LEN
+ STATUS_BYTES
) * 8;
475 field
.out_value
= NULL
;
476 field
.in_value
= in_buffer
;
478 jtag_add_dr_scan(jtag_info
->tap
, 1, &field
, TAP_IDLE
);
480 retval
= jtag_execute_queue();
481 if (retval
!= ERROR_OK
)
484 /* Look for the start bit in the first (STATUS_BYTES * 8) bits */
485 int shift
= find_status_bit(in_buffer
, STATUS_BYTES
);
487 /* We expect the status bit to be in the first byte */
489 if (retry_full_busy
++ < MAX_READ_BUSY_RETRY
) {
490 LOG_WARNING("Burst read timed out");
491 goto retry_read_full
;
493 LOG_ERROR("Burst read failed");
499 buffer_shr(in_buffer
, total_size_bytes
+ CRC_LEN
+ STATUS_BYTES
, shift
);
502 memcpy(data
, in_buffer
, total_size_bytes
);
503 memcpy(&crc_read
, &in_buffer
[total_size_bytes
], 4);
505 uint32_t crc_calc
= crc32_le(CRC32_POLY_LE
, 0xffffffff, data
,
508 if (crc_calc
!= crc_read
) {
509 LOG_WARNING("CRC ERROR! Computed 0x%08" PRIx32
", read CRC 0x%08" PRIx32
, crc_calc
, crc_read
);
510 if (retry_full_crc
++ < MAX_READ_CRC_RETRY
)
511 goto retry_read_full
;
513 LOG_ERROR("Burst read failed");
518 LOG_DEBUG("CRC OK!");
520 /* Now, read the error register, and retry/recompute as necessary */
521 if (jtag_info
->or1k_jtag_module_selected
== DC_WISHBONE
&&
522 !(or1k_du_adv
.options
& ADBG_USE_HISPEED
)) {
524 uint32_t err_data
[2] = {0, 0};
526 int bus_error_retries
= 0;
528 /* First, just get 1 bit...read address only if necessary */
529 retval
= adbg_ctrl_read(jtag_info
, DBG_WB_REG_ERROR
, err_data
, 1);
530 if (retval
!= ERROR_OK
)
533 /* Then we have a problem */
534 if (err_data
[0] & 0x1) {
536 retval
= adbg_ctrl_read(jtag_info
, DBG_WB_REG_ERROR
, err_data
, 33);
537 if (retval
!= ERROR_OK
)
540 addr
= (err_data
[0] >> 1) | (err_data
[1] << 31);
541 LOG_WARNING("WB bus error during burst read, address 0x%08" PRIx32
", retrying!", addr
);
544 if (bus_error_retries
> MAX_BUS_ERRORS
) {
545 LOG_ERROR("Max WB bus errors reached during burst read");
550 /* Don't call retry_do(), a JTAG reset won't help a WB bus error */
551 /* Write 1 bit, to reset the error register */
553 retval
= adbg_ctrl_write(jtag_info
, DBG_WB_REG_ERROR
, err_data
, 1);
554 if (retval
!= ERROR_OK
)
557 goto retry_read_full
;
567 /* Set up and execute a burst write to a contiguous set of addresses */
568 static int adbg_wb_burst_write(struct or1k_jtag
*jtag_info
, const uint8_t *data
, int size
,
569 int count
, unsigned long start_address
)
571 int retry_full_crc
= 0;
575 LOG_DEBUG("Doing burst write, word size %d, word count %d,"
576 "start address 0x%08lx", size
, count
, start_address
);
578 /* Select the appropriate opcode */
579 switch (jtag_info
->or1k_jtag_module_selected
) {
582 opcode
= DBG_WB_CMD_BWRITE8
;
584 opcode
= DBG_WB_CMD_BWRITE16
;
586 opcode
= DBG_WB_CMD_BWRITE32
;
588 LOG_DEBUG("Tried WB burst write with invalid word size (%d),"
589 "defaulting to 4-byte words", size
);
590 opcode
= DBG_WB_CMD_BWRITE32
;
595 opcode
= DBG_CPU0_CMD_BWRITE32
;
597 LOG_DEBUG("Tried CPU0 burst write with invalid word size (%d),"
598 "defaulting to 4-byte words", size
);
599 opcode
= DBG_CPU0_CMD_BWRITE32
;
604 opcode
= DBG_CPU1_CMD_BWRITE32
;
606 LOG_DEBUG("Tried CPU1 burst write with invalid word size (%d),"
607 "defaulting to 4-byte words", size
);
608 opcode
= DBG_CPU0_CMD_BWRITE32
;
612 LOG_ERROR("Illegal debug chain selected (%i) while doing burst write",
613 jtag_info
->or1k_jtag_module_selected
);
619 /* Send the BURST WRITE command, returns TAP to idle state */
620 retval
= adbg_burst_command(jtag_info
, opcode
, start_address
, count
);
621 if (retval
!= ERROR_OK
)
624 struct scan_field field
[3];
626 /* Write a start bit so it knows when to start counting */
628 field
[0].num_bits
= 1;
629 field
[0].out_value
= &value
;
630 field
[0].in_value
= NULL
;
632 uint32_t crc_calc
= crc32_le(CRC32_POLY_LE
, 0xffffffff, data
,
635 field
[1].num_bits
= count
* size
* 8;
636 field
[1].out_value
= data
;
637 field
[1].in_value
= NULL
;
639 field
[2].num_bits
= 32;
640 field
[2].out_value
= (uint8_t *)&crc_calc
;
641 field
[2].in_value
= NULL
;
643 jtag_add_dr_scan(jtag_info
->tap
, 3, field
, TAP_DRSHIFT
);
645 /* Read the 'CRC match' bit, and go to idle */
646 field
[0].num_bits
= 1;
647 field
[0].out_value
= NULL
;
648 field
[0].in_value
= &value
;
649 jtag_add_dr_scan(jtag_info
->tap
, 1, field
, TAP_IDLE
);
651 retval
= jtag_execute_queue();
652 if (retval
!= ERROR_OK
)
656 LOG_WARNING("CRC ERROR! match bit after write is %" PRIi8
" (computed CRC 0x%08" PRIx32
")", value
, crc_calc
);
657 if (retry_full_crc
++ < MAX_WRITE_CRC_RETRY
)
658 goto retry_full_write
;
662 LOG_DEBUG("CRC OK!\n");
664 /* Now, read the error register, and retry/recompute as necessary */
665 if (jtag_info
->or1k_jtag_module_selected
== DC_WISHBONE
&&
666 !(or1k_du_adv
.options
& ADBG_USE_HISPEED
)) {
668 int bus_error_retries
= 0;
669 uint32_t err_data
[2] = {0, 0};
671 /* First, just get 1 bit...read address only if necessary */
672 retval
= adbg_ctrl_read(jtag_info
, DBG_WB_REG_ERROR
, err_data
, 1);
673 if (retval
!= ERROR_OK
)
676 /* Then we have a problem */
677 if (err_data
[0] & 0x1) {
679 retval
= adbg_ctrl_read(jtag_info
, DBG_WB_REG_ERROR
, err_data
, 33);
680 if (retval
!= ERROR_OK
)
683 addr
= (err_data
[0] >> 1) | (err_data
[1] << 31);
684 LOG_WARNING("WB bus error during burst write, address 0x%08" PRIx32
", retrying!", addr
);
687 if (bus_error_retries
> MAX_BUS_ERRORS
) {
688 LOG_ERROR("Max WB bus errors reached during burst read");
693 /* Don't call retry_do(), a JTAG reset won't help a WB bus error */
694 /* Write 1 bit, to reset the error register */
696 retval
= adbg_ctrl_write(jtag_info
, DBG_WB_REG_ERROR
, err_data
, 1);
697 if (retval
!= ERROR_OK
)
700 goto retry_full_write
;
707 /* Currently hard set in functions to 32-bits */
708 static int or1k_adv_jtag_read_cpu(struct or1k_jtag
*jtag_info
,
709 uint32_t addr
, int count
, uint32_t *value
)
712 if (!jtag_info
->or1k_jtag_inited
) {
713 retval
= or1k_adv_jtag_init(jtag_info
);
714 if (retval
!= ERROR_OK
)
718 retval
= adbg_select_module(jtag_info
, DC_CPU0
);
719 if (retval
!= ERROR_OK
)
722 return adbg_wb_burst_read(jtag_info
, 4, count
, addr
, (uint8_t *)value
);
725 static int or1k_adv_jtag_write_cpu(struct or1k_jtag
*jtag_info
,
726 uint32_t addr
, int count
, const uint32_t *value
)
729 if (!jtag_info
->or1k_jtag_inited
) {
730 retval
= or1k_adv_jtag_init(jtag_info
);
731 if (retval
!= ERROR_OK
)
735 retval
= adbg_select_module(jtag_info
, DC_CPU0
);
736 if (retval
!= ERROR_OK
)
739 return adbg_wb_burst_write(jtag_info
, (uint8_t *)value
, 4, count
, addr
);
742 static int or1k_adv_cpu_stall(struct or1k_jtag
*jtag_info
, int action
)
745 if (!jtag_info
->or1k_jtag_inited
) {
746 retval
= or1k_adv_jtag_init(jtag_info
);
747 if (retval
!= ERROR_OK
)
751 retval
= adbg_select_module(jtag_info
, DC_CPU0
);
752 if (retval
!= ERROR_OK
)
756 retval
= adbg_ctrl_read(jtag_info
, DBG_CPU0_REG_STATUS
, &cpu_cr
, 2);
757 if (retval
!= ERROR_OK
)
760 if (action
== CPU_STALL
)
761 cpu_cr
|= DBG_CPU_CR_STALL
;
763 cpu_cr
&= ~DBG_CPU_CR_STALL
;
765 retval
= adbg_select_module(jtag_info
, DC_CPU0
);
766 if (retval
!= ERROR_OK
)
769 return adbg_ctrl_write(jtag_info
, DBG_CPU0_REG_STATUS
, &cpu_cr
, 2);
772 static int or1k_adv_is_cpu_running(struct or1k_jtag
*jtag_info
, int *running
)
775 if (!jtag_info
->or1k_jtag_inited
) {
776 retval
= or1k_adv_jtag_init(jtag_info
);
777 if (retval
!= ERROR_OK
)
781 int current
= jtag_info
->or1k_jtag_module_selected
;
783 retval
= adbg_select_module(jtag_info
, DC_CPU0
);
784 if (retval
!= ERROR_OK
)
788 retval
= adbg_ctrl_read(jtag_info
, DBG_CPU0_REG_STATUS
, &cpu_cr
, 2);
789 if (retval
!= ERROR_OK
)
792 if (cpu_cr
& DBG_CPU_CR_STALL
)
797 if (current
!= DC_NONE
) {
798 retval
= adbg_select_module(jtag_info
, current
);
799 if (retval
!= ERROR_OK
)
806 static int or1k_adv_cpu_reset(struct or1k_jtag
*jtag_info
, int action
)
809 if (!jtag_info
->or1k_jtag_inited
) {
810 retval
= or1k_adv_jtag_init(jtag_info
);
811 if (retval
!= ERROR_OK
)
815 retval
= adbg_select_module(jtag_info
, DC_CPU0
);
816 if (retval
!= ERROR_OK
)
820 retval
= adbg_ctrl_read(jtag_info
, DBG_CPU0_REG_STATUS
, &cpu_cr
, 2);
821 if (retval
!= ERROR_OK
)
824 if (action
== CPU_RESET
)
825 cpu_cr
|= DBG_CPU_CR_RESET
;
827 cpu_cr
&= ~DBG_CPU_CR_RESET
;
829 retval
= adbg_select_module(jtag_info
, DC_CPU0
);
830 if (retval
!= ERROR_OK
)
833 return adbg_ctrl_write(jtag_info
, DBG_CPU0_REG_STATUS
, &cpu_cr
, 2);
836 static int or1k_adv_jtag_read_memory(struct or1k_jtag
*jtag_info
,
837 uint32_t addr
, uint32_t size
, int count
, uint8_t *buffer
)
839 LOG_DEBUG("Reading WB%" PRIu32
" at 0x%08" PRIx32
, size
* 8, addr
);
842 if (!jtag_info
->or1k_jtag_inited
) {
843 retval
= or1k_adv_jtag_init(jtag_info
);
844 if (retval
!= ERROR_OK
)
848 retval
= adbg_select_module(jtag_info
, DC_WISHBONE
);
849 if (retval
!= ERROR_OK
)
852 int block_count_left
= count
;
853 uint32_t block_count_address
= addr
;
854 uint8_t *block_count_buffer
= buffer
;
856 while (block_count_left
) {
858 int blocks_this_round
= (block_count_left
> MAX_BURST_SIZE
) ?
859 MAX_BURST_SIZE
: block_count_left
;
861 retval
= adbg_wb_burst_read(jtag_info
, size
, blocks_this_round
,
862 block_count_address
, block_count_buffer
);
863 if (retval
!= ERROR_OK
)
866 block_count_left
-= blocks_this_round
;
867 block_count_address
+= size
* MAX_BURST_SIZE
;
868 block_count_buffer
+= size
* MAX_BURST_SIZE
;
871 /* The adv_debug_if always return words and half words in
872 * little-endian order no matter what the target endian is.
873 * So if the target endian is big, change the order.
876 struct target
*target
= jtag_info
->target
;
877 if ((target
->endianness
== TARGET_BIG_ENDIAN
) && (size
!= 1)) {
880 buf_bswap32(buffer
, buffer
, size
* count
);
883 buf_bswap16(buffer
, buffer
, size
* count
);
891 static int or1k_adv_jtag_write_memory(struct or1k_jtag
*jtag_info
,
892 uint32_t addr
, uint32_t size
, int count
, const uint8_t *buffer
)
894 LOG_DEBUG("Writing WB%" PRIu32
" at 0x%08" PRIx32
, size
* 8, addr
);
897 if (!jtag_info
->or1k_jtag_inited
) {
898 retval
= or1k_adv_jtag_init(jtag_info
);
899 if (retval
!= ERROR_OK
)
903 retval
= adbg_select_module(jtag_info
, DC_WISHBONE
);
904 if (retval
!= ERROR_OK
)
907 /* The adv_debug_if wants words and half words in little-endian
908 * order no matter what the target endian is. So if the target
909 * endian is big, change the order.
913 struct target
*target
= jtag_info
->target
;
914 if ((target
->endianness
== TARGET_BIG_ENDIAN
) && (size
!= 1)) {
915 t
= calloc(count
* size
, sizeof(uint8_t));
917 LOG_ERROR("Out of memory");
923 buf_bswap32(t
, buffer
, size
* count
);
926 buf_bswap16(t
, buffer
, size
* count
);
930 return ERROR_TARGET_FAILURE
;
935 int block_count_left
= count
;
936 uint32_t block_count_address
= addr
;
937 uint8_t *block_count_buffer
= (uint8_t *)buffer
;
939 while (block_count_left
) {
941 int blocks_this_round
= (block_count_left
> MAX_BURST_SIZE
) ?
942 MAX_BURST_SIZE
: block_count_left
;
944 retval
= adbg_wb_burst_write(jtag_info
, block_count_buffer
,
945 size
, blocks_this_round
,
946 block_count_address
);
947 if (retval
!= ERROR_OK
) {
952 block_count_left
-= blocks_this_round
;
953 block_count_address
+= size
* MAX_BURST_SIZE
;
954 block_count_buffer
+= size
* MAX_BURST_SIZE
;
961 int or1k_adv_jtag_jsp_xfer(struct or1k_jtag
*jtag_info
,
962 int *out_len
, unsigned char *out_buffer
,
963 int *in_len
, unsigned char *in_buffer
)
965 LOG_DEBUG("JSP transfer");
968 if (!jtag_info
->or1k_jtag_inited
)
971 retval
= adbg_select_module(jtag_info
, DC_JSP
);
972 if (retval
!= ERROR_OK
)
975 /* return nb char xmit */
982 uint8_t out_data
[10];
984 struct scan_field field
;
985 int startbit
, stopbit
, wrapbit
;
987 memset(out_data
, 0, 10);
989 if (or1k_du_adv
.options
& ENABLE_JSP_MULTI
) {
992 wrapbit
= (xmitsize
>> 3) & 0x1;
993 out_data
[0] = (xmitsize
<< 5) | 0x1; /* set the start bit */
996 /* don't copy off the end of the input array */
997 for (i
= 0; i
< xmitsize
; i
++) {
998 out_data
[i
+ 1] = (out_buffer
[i
] << 1) | wrapbit
;
999 wrapbit
= (out_buffer
[i
] >> 7) & 0x1;
1003 out_data
[i
+ 1] = wrapbit
;
1005 out_data
[9] = wrapbit
;
1007 /* If the last data bit is a '1', then we need to append a '0' so the top-level module
1008 * won't treat the burst as a 'module select' command.
1010 stopbit
= !!(out_data
[9] & 0x01);
1014 /* First byte out has write count in upper nibble */
1015 out_data
[0] = 0x0 | (xmitsize
<< 4);
1017 memcpy(&out_data
[1], out_buffer
, xmitsize
);
1019 /* If the last data bit is a '1', then we need to append a '0' so the top-level module
1020 * won't treat the burst as a 'module select' command.
1022 stopbit
= !!(out_data
[8] & 0x80);
1025 field
.num_bits
= 72 + startbit
+ stopbit
;
1026 field
.out_value
= out_data
;
1027 field
.in_value
= in_data
;
1029 jtag_add_dr_scan(jtag_info
->tap
, 1, &field
, TAP_IDLE
);
1031 retval
= jtag_execute_queue();
1032 if (retval
!= ERROR_OK
)
1035 /* bytes available is in the upper nibble */
1036 *in_len
= (in_data
[0] >> 4) & 0xF;
1037 memcpy(in_buffer
, &in_data
[1], *in_len
);
1039 int bytes_free
= in_data
[0] & 0x0F;
1040 *out_len
= (bytes_free
< xmitsize
) ? bytes_free
: xmitsize
;
1045 static struct or1k_du or1k_du_adv
= {
1047 .options
= NO_OPTION
,
1048 .or1k_jtag_init
= or1k_adv_jtag_init
,
1050 .or1k_is_cpu_running
= or1k_adv_is_cpu_running
,
1051 .or1k_cpu_stall
= or1k_adv_cpu_stall
,
1052 .or1k_cpu_reset
= or1k_adv_cpu_reset
,
1054 .or1k_jtag_read_cpu
= or1k_adv_jtag_read_cpu
,
1055 .or1k_jtag_write_cpu
= or1k_adv_jtag_write_cpu
,
1057 .or1k_jtag_read_memory
= or1k_adv_jtag_read_memory
,
1058 .or1k_jtag_write_memory
= or1k_adv_jtag_write_memory
1061 int or1k_du_adv_register(void)
1063 list_add_tail(&or1k_du_adv
.list
, &du_list
);