1 /***************************************************************************
2 * Copyright (C) 2013 by Franck Jullien *
5 * Inspired from adv_jtag_bridge which is: *
6 * Copyright (C) 2008-2010 Nathan Yawn *
7 * nyawn@opencores.net *
9 * And the Mohor interface version of this file which is: *
10 * Copyright (C) 2011 by Julius Baxter *
11 * julius@opencores.org *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
18 * This program is distributed in the hope that it will be useful, *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21 * GNU General Public License for more details. *
23 * You should have received a copy of the GNU General Public License *
24 * along with this program; if not, write to the *
25 * Free Software Foundation, Inc., *
26 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
27 ***************************************************************************/
37 #include <target/target.h>
38 #include <jtag/jtag.h>
40 /* This an option to the adv debug unit.
41 * If this is defined, status bits will be skipped on burst
42 * reads and writes to improve download speeds.
43 * This option must match the RTL configured option.
45 #define ADBG_USE_HISPEED 1
47 /* Definitions for the top-level debug unit. This really just consists
48 * of a single register, used to select the active debug module ("chain").
50 #define DBG_MODULE_SELECT_REG_SIZE 2
51 #define DBG_MAX_MODULES 4
59 /* CPU control register bits mask */
60 #define DBG_CPU_CR_STALL 0x01
61 #define DBG_CPU_CR_RESET 0x02
63 /* Polynomial for the CRC calculation
64 * Yes, it's backwards. Yes, this is on purpose.
65 * The hardware is designed this way to save on logic and routing,
66 * and it's really all the same to us here.
68 #define ADBG_CRC_POLY 0xedb88320
70 /* These are for the internal registers in the Wishbone module
71 * The first is the length of the index register,
72 * the indexes of the various registers are defined after that.
74 #define DBG_WB_REG_SEL_LEN 1
75 #define DBG_WB_REG_ERROR 0
77 /* Opcode definitions for the Wishbone module. */
78 #define DBG_WB_OPCODE_LEN 4
79 #define DBG_WB_CMD_NOP 0x0
80 #define DBG_WB_CMD_BWRITE8 0x1
81 #define DBG_WB_CMD_BWRITE16 0x2
82 #define DBG_WB_CMD_BWRITE32 0x3
83 #define DBG_WB_CMD_BREAD8 0x5
84 #define DBG_WB_CMD_BREAD16 0x6
85 #define DBG_WB_CMD_BREAD32 0x7
86 #define DBG_WB_CMD_IREG_WR 0x9
87 #define DBG_WB_CMD_IREG_SEL 0xd
89 /* Internal register definitions for the CPU0 module. */
90 #define DBG_CPU0_REG_SEL_LEN 1
91 #define DBG_CPU0_REG_STATUS 0
93 /* Opcode definitions for the first CPU module. */
94 #define DBG_CPU0_OPCODE_LEN 4
95 #define DBG_CPU0_CMD_NOP 0x0
96 #define DBG_CPU0_CMD_BWRITE32 0x3
97 #define DBG_CPU0_CMD_BREAD32 0x7
98 #define DBG_CPU0_CMD_IREG_WR 0x9
99 #define DBG_CPU0_CMD_IREG_SEL 0xd
101 /* Internal register definitions for the CPU1 module. */
102 #define DBG_CPU1_REG_SEL_LEN 1
103 #define DBG_CPU1_REG_STATUS 0
105 /* Opcode definitions for the second CPU module. */
106 #define DBG_CPU1_OPCODE_LEN 4
107 #define DBG_CPU1_CMD_NOP 0x0
108 #define DBG_CPU1_CMD_BWRITE32 0x3
109 #define DBG_CPU1_CMD_BREAD32 0x7
110 #define DBG_CPU1_CMD_IREG_WR 0x9
111 #define DBG_CPU1_CMD_IREG_SEL 0xd
113 #define MAX_READ_STATUS_WAIT 10
114 #define MAX_READ_BUSY_RETRY 2
115 #define MAX_READ_CRC_RETRY 2
116 #define MAX_WRITE_CRC_RETRY 2
117 #define BURST_READ_READY 1
118 #define MAX_BUS_ERRORS 2
120 #define MAX_BURST_SIZE (4 * 1024)
122 #define STATUS_BYTES 1
125 static struct or1k_du or1k_du_adv;
127 static const char * const chain_name[] = {"WISHBONE", "CPU0", "CPU1", "JSP"};
129 static uint32_t adbg_compute_crc(uint32_t crc, uint32_t data_in,
132 for (int i = 0; i < length_bits; i++) {
134 d = ((data_in >> i) & 0x1) ? 0xffffffff : 0;
135 c = (crc & 0x1) ? 0xffffffff : 0;
137 crc = crc ^ ((d ^ c) & ADBG_CRC_POLY);
143 static int find_status_bit(void *_buf, int len)
150 while (!(buf[i] & (1 << count++)) && (i < len)) {
158 ret = (i * 8) + count;
163 static int or1k_adv_jtag_init(struct or1k_jtag *jtag_info)
165 struct or1k_tap_ip *tap_ip = jtag_info->tap_ip;
167 int retval = tap_ip->init(jtag_info);
168 if (retval != ERROR_OK) {
169 LOG_ERROR("TAP initialization failed");
173 /* TAP is now configured to communicate with debug interface */
174 jtag_info->or1k_jtag_inited = 1;
176 /* TAP reset - not sure what state debug module chain is in now */
177 jtag_info->or1k_jtag_module_selected = DC_NONE;
179 jtag_info->current_reg_idx = malloc(DBG_MAX_MODULES * sizeof(uint8_t));
180 memset(jtag_info->current_reg_idx, 0, DBG_MAX_MODULES * sizeof(uint8_t));
182 if (or1k_du_adv.options & ADBG_USE_HISPEED)
183 LOG_INFO("adv debug unit is configured with option ADBG_USE_HISPEED");
185 LOG_DEBUG("Init done");
191 /* Selects one of the modules in the debug unit
192 * (e.g. wishbone unit, CPU0, etc.)
194 static int adbg_select_module(struct or1k_jtag *jtag_info, int chain)
196 if (jtag_info->or1k_jtag_module_selected == chain)
199 /* MSB of the data out must be set to 1, indicating a module
202 uint8_t data = chain | (1 << DBG_MODULE_SELECT_REG_SIZE);
204 LOG_DEBUG("Select module: %s", chain_name[chain]);
206 struct scan_field field;
208 field.num_bits = (DBG_MODULE_SELECT_REG_SIZE + 1);
209 field.out_value = &data;
210 field.in_value = NULL;
211 jtag_add_dr_scan(jtag_info->tap, 1, &field, TAP_IDLE);
213 int retval = jtag_execute_queue();
214 if (retval != ERROR_OK)
217 jtag_info->or1k_jtag_module_selected = chain;
222 /* Set the index of the desired register in the currently selected module
223 * 1 bit module select command
227 static int adbg_select_ctrl_reg(struct or1k_jtag *jtag_info, uint8_t regidx)
233 /* If this reg is already selected, don't do a JTAG transaction */
234 if (jtag_info->current_reg_idx[jtag_info->or1k_jtag_module_selected] == regidx)
237 switch (jtag_info->or1k_jtag_module_selected) {
239 index_len = DBG_WB_REG_SEL_LEN;
240 opcode = DBG_WB_CMD_IREG_SEL;
241 opcode_len = DBG_WB_OPCODE_LEN;
244 index_len = DBG_CPU0_REG_SEL_LEN;
245 opcode = DBG_CPU0_CMD_IREG_SEL;
246 opcode_len = DBG_CPU0_OPCODE_LEN;
249 index_len = DBG_CPU1_REG_SEL_LEN;
250 opcode = DBG_CPU1_CMD_IREG_SEL;
251 opcode_len = DBG_CPU1_OPCODE_LEN;
254 LOG_ERROR("Illegal debug chain selected (%i) while selecting control register",
255 jtag_info->or1k_jtag_module_selected);
259 /* MSB must be 0 to access modules */
260 uint32_t data = (opcode & ~(1 << opcode_len)) << index_len;
263 struct scan_field field;
265 field.num_bits = (opcode_len + 1) + index_len;
266 field.out_value = (uint8_t *)&data;
267 field.in_value = NULL;
268 jtag_add_dr_scan(jtag_info->tap, 1, &field, TAP_IDLE);
270 int retval = jtag_execute_queue();
271 if (retval != ERROR_OK)
274 jtag_info->current_reg_idx[jtag_info->or1k_jtag_module_selected] = regidx;
279 /* Write control register (internal to the debug unit) */
280 static int adbg_ctrl_write(struct or1k_jtag *jtag_info, uint8_t regidx,
281 uint32_t *cmd_data, int length_bits)
287 LOG_DEBUG("Write control register %" PRId8 ": 0x%08" PRIx32, regidx, cmd_data[0]);
289 int retval = adbg_select_ctrl_reg(jtag_info, regidx);
290 if (retval != ERROR_OK) {
291 LOG_ERROR("Error while calling adbg_select_ctrl_reg");
295 switch (jtag_info->or1k_jtag_module_selected) {
297 index_len = DBG_WB_REG_SEL_LEN;
298 opcode = DBG_WB_CMD_IREG_WR;
299 opcode_len = DBG_WB_OPCODE_LEN;
302 index_len = DBG_CPU0_REG_SEL_LEN;
303 opcode = DBG_CPU0_CMD_IREG_WR;
304 opcode_len = DBG_CPU0_OPCODE_LEN;
307 index_len = DBG_CPU1_REG_SEL_LEN;
308 opcode = DBG_CPU1_CMD_IREG_WR;
309 opcode_len = DBG_CPU1_OPCODE_LEN;
312 LOG_ERROR("Illegal debug chain selected (%i) while doing control write",
313 jtag_info->or1k_jtag_module_selected);
317 struct scan_field field[2];
319 /* MSB must be 0 to access modules */
320 uint32_t data = (opcode & ~(1 << opcode_len)) << index_len;
323 field[0].num_bits = length_bits;
324 field[0].out_value = (uint8_t *)cmd_data;
325 field[0].in_value = NULL;
327 field[1].num_bits = (opcode_len + 1) + index_len;
328 field[1].out_value = (uint8_t *)&data;
329 field[1].in_value = NULL;
331 jtag_add_dr_scan(jtag_info->tap, 2, field, TAP_IDLE);
333 return jtag_execute_queue();
336 /* Reads control register (internal to the debug unit) */
337 static int adbg_ctrl_read(struct or1k_jtag *jtag_info, uint32_t regidx,
338 uint32_t *data, int length_bits)
341 int retval = adbg_select_ctrl_reg(jtag_info, regidx);
342 if (retval != ERROR_OK) {
343 LOG_ERROR("Error while calling adbg_select_ctrl_reg");
350 /* There is no 'read' command, We write a NOP to read */
351 switch (jtag_info->or1k_jtag_module_selected) {
353 opcode = DBG_WB_CMD_NOP;
354 opcode_len = DBG_WB_OPCODE_LEN;
357 opcode = DBG_CPU0_CMD_NOP;
358 opcode_len = DBG_CPU0_OPCODE_LEN;
361 opcode = DBG_CPU1_CMD_NOP;
362 opcode_len = DBG_CPU1_OPCODE_LEN;
365 LOG_ERROR("Illegal debug chain selected (%i) while doing control read",
366 jtag_info->or1k_jtag_module_selected);
370 /* Zero MSB = op for module, not top-level debug unit */
371 uint32_t outdata = opcode & ~(0x1 << opcode_len);
373 struct scan_field field[2];
375 field[0].num_bits = length_bits;
376 field[0].out_value = NULL;
377 field[0].in_value = (uint8_t *)data;
379 field[1].num_bits = opcode_len + 1;
380 field[1].out_value = (uint8_t *)&outdata;
381 field[1].in_value = NULL;
383 jtag_add_dr_scan(jtag_info->tap, 2, field, TAP_IDLE);
385 return jtag_execute_queue();
388 /* sends out a burst command to the selected module in the debug unit (MSB to LSB):
389 * 1-bit module command
392 * 16-bit length (of the burst, in words)
394 static int adbg_burst_command(struct or1k_jtag *jtag_info, uint32_t opcode,
395 uint32_t address, uint16_t length_words)
399 /* Set up the data */
400 data[0] = length_words | (address << 16);
401 /* MSB must be 0 to access modules */
402 data[1] = ((address >> 16) | ((opcode & 0xf) << 16)) & ~(0x1 << 20);
404 struct scan_field field;
407 field.out_value = (uint8_t *)&data[0];
408 field.in_value = NULL;
410 jtag_add_dr_scan(jtag_info->tap, 1, &field, TAP_IDLE);
412 return jtag_execute_queue();
415 static int adbg_wb_burst_read(struct or1k_jtag *jtag_info, int size,
416 int count, uint32_t start_address, uint8_t *data)
418 int retry_full_crc = 0;
419 int retry_full_busy = 0;
423 LOG_DEBUG("Doing burst read, word size %d, word count %d, start address 0x%08" PRIx32,
424 size, count, start_address);
426 /* Select the appropriate opcode */
427 switch (jtag_info->or1k_jtag_module_selected) {
430 opcode = DBG_WB_CMD_BREAD8;
432 opcode = DBG_WB_CMD_BREAD16;
434 opcode = DBG_WB_CMD_BREAD32;
436 LOG_WARNING("Tried burst read with invalid word size (%d),"
437 "defaulting to 4-byte words", size);
438 opcode = DBG_WB_CMD_BREAD32;
443 opcode = DBG_CPU0_CMD_BREAD32;
445 LOG_WARNING("Tried burst read with invalid word size (%d),"
446 "defaulting to 4-byte words", size);
447 opcode = DBG_CPU0_CMD_BREAD32;
452 opcode = DBG_CPU1_CMD_BREAD32;
454 LOG_WARNING("Tried burst read with invalid word size (%d),"
455 "defaulting to 4-byte words", size);
456 opcode = DBG_CPU0_CMD_BREAD32;
460 LOG_ERROR("Illegal debug chain selected (%i) while doing burst read",
461 jtag_info->or1k_jtag_module_selected);
465 int total_size_bytes = count * size;
466 struct scan_field field;
467 uint8_t *in_buffer = malloc(total_size_bytes + CRC_LEN + STATUS_BYTES);
471 /* Send the BURST READ command, returns TAP to idle state */
472 retval = adbg_burst_command(jtag_info, opcode, start_address, count);
473 if (retval != ERROR_OK)
476 field.num_bits = (total_size_bytes + CRC_LEN + STATUS_BYTES) * 8;
477 field.out_value = NULL;
478 field.in_value = in_buffer;
480 jtag_add_dr_scan(jtag_info->tap, 1, &field, TAP_IDLE);
482 retval = jtag_execute_queue();
483 if (retval != ERROR_OK)
486 /* Look for the start bit in the first (STATUS_BYTES * 8) bits */
487 int shift = find_status_bit(in_buffer, STATUS_BYTES);
489 /* We expect the status bit to be in the first byte */
491 if (retry_full_busy++ < MAX_READ_BUSY_RETRY) {
492 LOG_WARNING("Burst read timed out");
493 goto retry_read_full;
495 LOG_ERROR("Burst read failed");
501 buffer_shr(in_buffer, total_size_bytes + CRC_LEN + STATUS_BYTES, shift);
504 memcpy(data, in_buffer, total_size_bytes);
505 memcpy(&crc_read, &in_buffer[total_size_bytes], 4);
507 uint32_t crc_calc = 0xffffffff;
508 for (int i = 0; i < total_size_bytes; i++)
509 crc_calc = adbg_compute_crc(crc_calc, data[i], 8);
511 if (crc_calc != crc_read) {
512 LOG_WARNING("CRC ERROR! Computed 0x%08" PRIx32 ", read CRC 0x%08" PRIx32, crc_calc, crc_read);
513 if (retry_full_crc++ < MAX_READ_CRC_RETRY)
514 goto retry_read_full;
516 LOG_ERROR("Burst read failed");
521 LOG_DEBUG("CRC OK!");
523 /* Now, read the error register, and retry/recompute as necessary */
524 if (jtag_info->or1k_jtag_module_selected == DC_WISHBONE &&
525 !(or1k_du_adv.options & ADBG_USE_HISPEED)) {
527 uint32_t err_data[2] = {0, 0};
529 int bus_error_retries = 0;
531 /* First, just get 1 bit...read address only if necessary */
532 retval = adbg_ctrl_read(jtag_info, DBG_WB_REG_ERROR, err_data, 1);
533 if (retval != ERROR_OK)
536 /* Then we have a problem */
537 if (err_data[0] & 0x1) {
539 retval = adbg_ctrl_read(jtag_info, DBG_WB_REG_ERROR, err_data, 33);
540 if (retval != ERROR_OK)
543 addr = (err_data[0] >> 1) | (err_data[1] << 31);
544 LOG_WARNING("WB bus error during burst read, address 0x%08" PRIx32 ", retrying!", addr);
547 if (bus_error_retries > MAX_BUS_ERRORS) {
548 LOG_ERROR("Max WB bus errors reached during burst read");
553 /* Don't call retry_do(), a JTAG reset won't help a WB bus error */
554 /* Write 1 bit, to reset the error register */
556 retval = adbg_ctrl_write(jtag_info, DBG_WB_REG_ERROR, err_data, 1);
557 if (retval != ERROR_OK)
560 goto retry_read_full;
570 /* Set up and execute a burst write to a contiguous set of addresses */
571 static int adbg_wb_burst_write(struct or1k_jtag *jtag_info, const uint8_t *data, int size,
572 int count, unsigned long start_address)
574 int retry_full_crc = 0;
578 LOG_DEBUG("Doing burst write, word size %d, word count %d,"
579 "start address 0x%08lx", size, count, start_address);
581 /* Select the appropriate opcode */
582 switch (jtag_info->or1k_jtag_module_selected) {
585 opcode = DBG_WB_CMD_BWRITE8;
587 opcode = DBG_WB_CMD_BWRITE16;
589 opcode = DBG_WB_CMD_BWRITE32;
591 LOG_DEBUG("Tried WB burst write with invalid word size (%d),"
592 "defaulting to 4-byte words", size);
593 opcode = DBG_WB_CMD_BWRITE32;
598 opcode = DBG_CPU0_CMD_BWRITE32;
600 LOG_DEBUG("Tried CPU0 burst write with invalid word size (%d),"
601 "defaulting to 4-byte words", size);
602 opcode = DBG_CPU0_CMD_BWRITE32;
607 opcode = DBG_CPU1_CMD_BWRITE32;
609 LOG_DEBUG("Tried CPU1 burst write with invalid word size (%d),"
610 "defaulting to 4-byte words", size);
611 opcode = DBG_CPU0_CMD_BWRITE32;
615 LOG_ERROR("Illegal debug chain selected (%i) while doing burst write",
616 jtag_info->or1k_jtag_module_selected);
622 /* Send the BURST WRITE command, returns TAP to idle state */
623 retval = adbg_burst_command(jtag_info, opcode, start_address, count);
624 if (retval != ERROR_OK)
627 struct scan_field field[3];
629 /* Write a start bit so it knows when to start counting */
631 field[0].num_bits = 1;
632 field[0].out_value = &value;
633 field[0].in_value = NULL;
635 uint32_t crc_calc = 0xffffffff;
636 for (int i = 0; i < (count * size); i++)
637 crc_calc = adbg_compute_crc(crc_calc, data[i], 8);
639 field[1].num_bits = count * size * 8;
640 field[1].out_value = data;
641 field[1].in_value = NULL;
643 field[2].num_bits = 32;
644 field[2].out_value = (uint8_t *)&crc_calc;
645 field[2].in_value = NULL;
647 jtag_add_dr_scan(jtag_info->tap, 3, field, TAP_DRSHIFT);
649 /* Read the 'CRC match' bit, and go to idle */
650 field[0].num_bits = 1;
651 field[0].out_value = NULL;
652 field[0].in_value = &value;
653 jtag_add_dr_scan(jtag_info->tap, 1, field, TAP_IDLE);
655 retval = jtag_execute_queue();
656 if (retval != ERROR_OK)
660 LOG_WARNING("CRC ERROR! match bit after write is %" PRIi8 " (computed CRC 0x%08" PRIx32 ")", value, crc_calc);
661 if (retry_full_crc++ < MAX_WRITE_CRC_RETRY)
662 goto retry_full_write;
666 LOG_DEBUG("CRC OK!\n");
668 /* Now, read the error register, and retry/recompute as necessary */
669 if (jtag_info->or1k_jtag_module_selected == DC_WISHBONE &&
670 !(or1k_du_adv.options & ADBG_USE_HISPEED)) {
672 int bus_error_retries = 0;
673 uint32_t err_data[2] = {0, 0};
675 /* First, just get 1 bit...read address only if necessary */
676 retval = adbg_ctrl_read(jtag_info, DBG_WB_REG_ERROR, err_data, 1);
677 if (retval != ERROR_OK)
680 /* Then we have a problem */
681 if (err_data[0] & 0x1) {
683 retval = adbg_ctrl_read(jtag_info, DBG_WB_REG_ERROR, err_data, 33);
684 if (retval != ERROR_OK)
687 addr = (err_data[0] >> 1) | (err_data[1] << 31);
688 LOG_WARNING("WB bus error during burst write, address 0x%08" PRIx32 ", retrying!", addr);
691 if (bus_error_retries > MAX_BUS_ERRORS) {
692 LOG_ERROR("Max WB bus errors reached during burst read");
697 /* Don't call retry_do(), a JTAG reset won't help a WB bus error */
698 /* Write 1 bit, to reset the error register */
700 retval = adbg_ctrl_write(jtag_info, DBG_WB_REG_ERROR, err_data, 1);
701 if (retval != ERROR_OK)
704 goto retry_full_write;
711 /* Currently hard set in functions to 32-bits */
712 static int or1k_adv_jtag_read_cpu(struct or1k_jtag *jtag_info,
713 uint32_t addr, int count, uint32_t *value)
716 if (!jtag_info->or1k_jtag_inited) {
717 retval = or1k_adv_jtag_init(jtag_info);
718 if (retval != ERROR_OK)
722 retval = adbg_select_module(jtag_info, DC_CPU0);
723 if (retval != ERROR_OK)
726 return adbg_wb_burst_read(jtag_info, 4, count, addr, (uint8_t *)value);
729 static int or1k_adv_jtag_write_cpu(struct or1k_jtag *jtag_info,
730 uint32_t addr, int count, const uint32_t *value)
733 if (!jtag_info->or1k_jtag_inited) {
734 retval = or1k_adv_jtag_init(jtag_info);
735 if (retval != ERROR_OK)
739 retval = adbg_select_module(jtag_info, DC_CPU0);
740 if (retval != ERROR_OK)
743 return adbg_wb_burst_write(jtag_info, (uint8_t *)value, 4, count, addr);
746 static int or1k_adv_cpu_stall(struct or1k_jtag *jtag_info, int action)
749 if (!jtag_info->or1k_jtag_inited) {
750 retval = or1k_adv_jtag_init(jtag_info);
751 if (retval != ERROR_OK)
755 retval = adbg_select_module(jtag_info, DC_CPU0);
756 if (retval != ERROR_OK)
760 retval = adbg_ctrl_read(jtag_info, DBG_CPU0_REG_STATUS, &cpu_cr, 2);
761 if (retval != ERROR_OK)
764 if (action == CPU_STALL)
765 cpu_cr |= DBG_CPU_CR_STALL;
767 cpu_cr &= ~DBG_CPU_CR_STALL;
769 retval = adbg_select_module(jtag_info, DC_CPU0);
770 if (retval != ERROR_OK)
773 return adbg_ctrl_write(jtag_info, DBG_CPU0_REG_STATUS, &cpu_cr, 2);
776 static int or1k_adv_is_cpu_running(struct or1k_jtag *jtag_info, int *running)
779 if (!jtag_info->or1k_jtag_inited) {
780 retval = or1k_adv_jtag_init(jtag_info);
781 if (retval != ERROR_OK)
785 int current = jtag_info->or1k_jtag_module_selected;
787 retval = adbg_select_module(jtag_info, DC_CPU0);
788 if (retval != ERROR_OK)
792 retval = adbg_ctrl_read(jtag_info, DBG_CPU0_REG_STATUS, &cpu_cr, 2);
793 if (retval != ERROR_OK)
796 if (cpu_cr & DBG_CPU_CR_STALL)
801 if (current != DC_NONE) {
802 retval = adbg_select_module(jtag_info, current);
803 if (retval != ERROR_OK)
810 static int or1k_adv_cpu_reset(struct or1k_jtag *jtag_info, int action)
813 if (!jtag_info->or1k_jtag_inited) {
814 retval = or1k_adv_jtag_init(jtag_info);
815 if (retval != ERROR_OK)
819 retval = adbg_select_module(jtag_info, DC_CPU0);
820 if (retval != ERROR_OK)
824 retval = adbg_ctrl_read(jtag_info, DBG_CPU0_REG_STATUS, &cpu_cr, 2);
825 if (retval != ERROR_OK)
828 if (action == CPU_RESET)
829 cpu_cr |= DBG_CPU_CR_RESET;
831 cpu_cr &= ~DBG_CPU_CR_RESET;
833 retval = adbg_select_module(jtag_info, DC_CPU0);
834 if (retval != ERROR_OK)
837 return adbg_ctrl_write(jtag_info, DBG_CPU0_REG_STATUS, &cpu_cr, 2);
840 static int or1k_adv_jtag_read_memory(struct or1k_jtag *jtag_info,
841 uint32_t addr, uint32_t size, int count, uint8_t *buffer)
843 LOG_DEBUG("Reading WB%" PRId32 " at 0x%08" PRIx32, size * 8, addr);
846 if (!jtag_info->or1k_jtag_inited) {
847 retval = or1k_adv_jtag_init(jtag_info);
848 if (retval != ERROR_OK)
852 retval = adbg_select_module(jtag_info, DC_WISHBONE);
853 if (retval != ERROR_OK)
856 int block_count_left = count;
857 uint32_t block_count_address = addr;
858 uint8_t *block_count_buffer = buffer;
860 while (block_count_left) {
862 int blocks_this_round = (block_count_left > MAX_BURST_SIZE) ?
863 MAX_BURST_SIZE : block_count_left;
865 retval = adbg_wb_burst_read(jtag_info, size, blocks_this_round,
866 block_count_address, block_count_buffer);
867 if (retval != ERROR_OK)
870 block_count_left -= blocks_this_round;
871 block_count_address += size * MAX_BURST_SIZE;
872 block_count_buffer += size * MAX_BURST_SIZE;
875 /* The adv_debug_if always return words and half words in
876 * little-endian order no matter what the target endian is.
877 * So if the target endian is big, change the order.
880 struct target *target = jtag_info->target;
881 if ((target->endianness == TARGET_BIG_ENDIAN) && (size != 1)) {
884 buf_bswap32(buffer, buffer, size * count);
887 buf_bswap16(buffer, buffer, size * count);
895 static int or1k_adv_jtag_write_memory(struct or1k_jtag *jtag_info,
896 uint32_t addr, uint32_t size, int count, const uint8_t *buffer)
898 LOG_DEBUG("Writing WB%" PRId32 " at 0x%08" PRIx32, size * 8, addr);
901 if (!jtag_info->or1k_jtag_inited) {
902 retval = or1k_adv_jtag_init(jtag_info);
903 if (retval != ERROR_OK)
907 retval = adbg_select_module(jtag_info, DC_WISHBONE);
908 if (retval != ERROR_OK)
911 /* The adv_debug_if wants words and half words in little-endian
912 * order no matter what the target endian is. So if the target
913 * endian is big, change the order.
917 struct target *target = jtag_info->target;
918 if ((target->endianness == TARGET_BIG_ENDIAN) && (size != 1)) {
919 t = malloc(count * size * sizeof(uint8_t));
921 LOG_ERROR("Out of memory");
927 buf_bswap32(t, buffer, size * count);
930 buf_bswap16(t, buffer, size * count);
936 int block_count_left = count;
937 uint32_t block_count_address = addr;
938 uint8_t *block_count_buffer = (uint8_t *)buffer;
940 while (block_count_left) {
942 int blocks_this_round = (block_count_left > MAX_BURST_SIZE) ?
943 MAX_BURST_SIZE : block_count_left;
945 retval = adbg_wb_burst_write(jtag_info, block_count_buffer,
946 size, blocks_this_round,
947 block_count_address);
948 if (retval != ERROR_OK) {
954 block_count_left -= blocks_this_round;
955 block_count_address += size * MAX_BURST_SIZE;
956 block_count_buffer += size * MAX_BURST_SIZE;
965 static struct or1k_du or1k_du_adv = {
967 .options = ADBG_USE_HISPEED,
968 .or1k_jtag_init = or1k_adv_jtag_init,
970 .or1k_is_cpu_running = or1k_adv_is_cpu_running,
971 .or1k_cpu_stall = or1k_adv_cpu_stall,
972 .or1k_cpu_reset = or1k_adv_cpu_reset,
974 .or1k_jtag_read_cpu = or1k_adv_jtag_read_cpu,
975 .or1k_jtag_write_cpu = or1k_adv_jtag_write_cpu,
977 .or1k_jtag_read_memory = or1k_adv_jtag_read_memory,
978 .or1k_jtag_write_memory = or1k_adv_jtag_write_memory
981 int or1k_du_adv_register(void)
983 list_add_tail(&or1k_du_adv.list, &du_list);