1 /***************************************************************************
2 * Copyright (C) 2006 by Magnus Lundin *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
23 /***************************************************************************
25 * CoreSight (Light?) SerialWireJtagDebugPort *
27 * CoreSightâ„¢ DAP-Lite TRM, ARM DDI 0316A *
28 * Cortex-M3â„¢ TRM, ARM DDI 0337C *
30 ***************************************************************************/
35 #include "replacements.h"
37 #include "cortex_m3.h"
38 #include "cortex_swjdp.h"
41 #include "time_support.h"
46 * swjdp->trans_mode = TRANS_MODE_COMPOSITE;
47 * Uses Overrun checking mode and does not do actual JTAG send/receive or transaction
48 * result checking until swjdp_end_transaction()
49 * This must be done before using or deallocating any return variables.
50 * swjdp->trans_mode == TRANS_MODE_ATOMIC
51 * All reads and writes to the AHB bus are checked for valid completion, and return values
52 * are immediatley available.
55 /***************************************************************************
57 * DPACC and APACC scanchain access through JTAG-DR *
59 ***************************************************************************/
61 /* Scan out and in from target ordered u8 buffers */
62 int swjdp_scan(arm_jtag_t *jtag_info, u8 instr, u8 reg_addr, u8 RnW, u8 *outvalue, u8 *invalue, u8 *ack)
64 scan_field_t fields[2];
67 jtag_add_end_state(TAP_RTI);
68 arm_jtag_set_instr(jtag_info, instr, NULL);
70 fields[0].device = jtag_info->chain_pos;
71 fields[0].num_bits = 3;
72 buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
73 fields[0].out_value = &out_addr_buf;
74 fields[0].out_mask = NULL;
75 fields[0].in_value = ack;
76 fields[0].in_check_value = NULL;
77 fields[0].in_check_mask = NULL;
78 fields[0].in_handler = NULL;
79 fields[0].in_handler_priv = NULL;
81 fields[1].device = jtag_info->chain_pos;
82 fields[1].num_bits = 32;
83 fields[1].out_value = outvalue;
84 fields[1].out_mask = NULL;
85 fields[1].in_value = invalue;
86 fields[1].in_handler = NULL;
87 fields[1].in_handler_priv = NULL;
88 fields[1].in_check_value = NULL;
89 fields[1].in_check_mask = NULL;
91 jtag_add_dr_scan(2, fields, -1);
96 /* Scan out and in from host ordered u32 variables */
97 int swjdp_scan_u32(arm_jtag_t *jtag_info, u8 instr, u8 reg_addr, u8 RnW, u32 outvalue, u32 *invalue, u8 *ack)
99 scan_field_t fields[2];
103 jtag_add_end_state(TAP_RTI);
104 arm_jtag_set_instr(jtag_info, instr, NULL);
106 fields[0].device = jtag_info->chain_pos;
107 fields[0].num_bits = 3;
108 buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
109 fields[0].out_value = &out_addr_buf;
110 fields[0].out_mask = NULL;
111 fields[0].in_value = ack;
112 fields[0].in_check_value = NULL;
113 fields[0].in_check_mask = NULL;
114 fields[0].in_handler = NULL;
115 fields[0].in_handler_priv = NULL;
117 fields[1].device = jtag_info->chain_pos;
118 fields[1].num_bits = 32;
119 buf_set_u32(out_value_buf, 0, 32, outvalue);
120 fields[1].out_value = out_value_buf;
121 fields[1].out_mask = NULL;
122 fields[1].in_value = NULL;
125 fields[1].in_handler = arm_jtag_buf_to_u32;
126 fields[1].in_handler_priv = invalue;
130 fields[1].in_handler = NULL;
131 fields[1].in_handler_priv = NULL;
133 fields[1].in_check_value = NULL;
134 fields[1].in_check_mask = NULL;
136 jtag_add_dr_scan(2, fields, -1);
141 /* scan_inout_check adds one extra inscan for DPAP_READ commands to read variables */
142 int scan_inout_check(swjdp_common_t *swjdp, u8 instr, u8 reg_addr, u8 RnW, u8 *outvalue, u8 *invalue)
144 swjdp_scan(swjdp->jtag_info, instr, reg_addr, RnW, outvalue, NULL, NULL);
145 if ((RnW == DPAP_READ) && (invalue != NULL))
147 swjdp_scan(swjdp->jtag_info, SWJDP_IR_DPACC, DP_RDBUFF, DPAP_READ, 0, invalue, &swjdp->ack);
150 /* In TRANS_MODE_ATOMIC all SWJDP_IR_APACC transactions wait for ack=OK/FAULT and the check CTRL_STAT */
151 if ((instr == SWJDP_IR_APACC) && (swjdp->trans_mode == TRANS_MODE_ATOMIC))
153 return swjdp_transaction_endcheck(swjdp);
159 int scan_inout_check_u32(swjdp_common_t *swjdp, u8 instr, u8 reg_addr, u8 RnW, u32 outvalue, u32 *invalue)
161 swjdp_scan_u32(swjdp->jtag_info, instr, reg_addr, RnW, outvalue, NULL, NULL);
162 if ((RnW==DPAP_READ) && (invalue != NULL))
164 swjdp_scan_u32(swjdp->jtag_info, SWJDP_IR_DPACC, DP_RDBUFF, DPAP_READ, 0, invalue, &swjdp->ack);
167 /* In TRANS_MODE_ATOMIC all SWJDP_IR_APACC transactions wait for ack=OK/FAULT and then check CTRL_STAT */
168 if ((instr == SWJDP_IR_APACC) && (swjdp->trans_mode == TRANS_MODE_ATOMIC))
170 return swjdp_transaction_endcheck(swjdp);
176 int swjdp_transaction_endcheck(swjdp_common_t *swjdp)
183 /* Danger!!!! BROKEN!!!! */
184 scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
185 /* Danger!!!! BROKEN!!!! Why will jtag_execute_queue() fail here????
186 R956 introduced the check on return value here and now Michael Schwingen reports
187 that this code no longer works....
189 https://lists.berlios.de/pipermail/openocd-development/2008-September/003107.html
191 if ((retval=jtag_execute_queue())!=ERROR_OK)
193 LOG_ERROR("BUG: Why does this fail the first time????");
195 /* Why??? second time it works??? */
196 scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
197 if ((retval=jtag_execute_queue())!=ERROR_OK)
200 swjdp->ack = swjdp->ack & 0x7;
202 long long then=timeval_ms();
203 while (swjdp->ack != 2)
207 if ((timeval_ms()-then) > 1000)
209 LOG_WARNING("Timeout (1000ms) waiting for ACK = OK/FAULT in SWJDP transaction");
210 return ERROR_JTAG_DEVICE_ERROR;
215 LOG_WARNING("Invalid ACK in SWJDP transaction");
216 return ERROR_JTAG_DEVICE_ERROR;
219 scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
220 if ((retval=jtag_execute_queue())!=ERROR_OK)
222 swjdp->ack = swjdp->ack & 0x7;
225 /* Check for STICKYERR and STICKYORUN */
226 if (ctrlstat & (SSTICKYORUN | SSTICKYERR))
228 LOG_DEBUG("swjdp: CTRL/STAT error 0x%x", ctrlstat);
229 /* Check power to debug regions */
230 if ((ctrlstat & 0xf0000000) != 0xf0000000)
232 ahbap_debugport_init(swjdp);
236 u32 dcb_dhcsr,nvic_shcsr, nvic_bfar, nvic_cfsr;
238 if (ctrlstat & SSTICKYORUN)
239 LOG_ERROR("SWJ-DP OVERRUN - check clock or reduce jtag speed");
241 if (ctrlstat & SSTICKYERR)
242 LOG_ERROR("SWJ-DP STICKY ERROR");
244 /* Clear Sticky Error Bits */
245 scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, DP_CTRL_STAT, DPAP_WRITE, swjdp->dp_ctrl_stat | SSTICKYORUN | SSTICKYERR, NULL);
246 scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
247 if ((retval=jtag_execute_queue())!=ERROR_OK)
250 LOG_DEBUG("swjdp: status 0x%x", ctrlstat);
252 /* Can we find out the reason for the error ?? */
253 ahbap_read_system_atomic_u32(swjdp, DCB_DHCSR, &dcb_dhcsr);
254 ahbap_read_system_atomic_u32(swjdp, NVIC_SHCSR, &nvic_shcsr);
255 ahbap_read_system_atomic_u32(swjdp, NVIC_CFSR, &nvic_cfsr);
256 ahbap_read_system_atomic_u32(swjdp, NVIC_BFAR, &nvic_bfar);
257 LOG_ERROR("dcb_dhcsr 0x%x, nvic_shcsr 0x%x, nvic_cfsr 0x%x, nvic_bfar 0x%x", dcb_dhcsr, nvic_shcsr, nvic_cfsr, nvic_bfar);
259 if ((retval=jtag_execute_queue())!=ERROR_OK)
261 return ERROR_JTAG_DEVICE_ERROR;
267 /***************************************************************************
269 * DP and AHB-AP register access through APACC and DPACC *
271 ***************************************************************************/
273 int swjdp_write_dpacc(swjdp_common_t *swjdp, u32 value, u8 reg_addr)
275 return scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, reg_addr, DPAP_WRITE, value, NULL);
278 int swjdp_read_dpacc(swjdp_common_t *swjdp, u32 *value, u8 reg_addr)
280 return scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, reg_addr, DPAP_READ, 0, value);
283 int swjdp_bankselect_apacc(swjdp_common_t *swjdp,u32 reg_addr)
286 select = (reg_addr & 0xFF0000F0);
288 if (select != swjdp->dp_select_value)
290 swjdp_write_dpacc(swjdp, select, DP_SELECT);
291 swjdp->dp_select_value = select;
297 int ahbap_write_reg(swjdp_common_t *swjdp, u32 reg_addr, u8* out_value_buf)
299 swjdp_bankselect_apacc(swjdp, reg_addr);
300 scan_inout_check(swjdp, SWJDP_IR_APACC, reg_addr, DPAP_WRITE, out_value_buf, NULL);
305 int ahbap_read_reg(swjdp_common_t *swjdp, u32 reg_addr, u8 *in_value_buf)
307 swjdp_bankselect_apacc(swjdp, reg_addr);
308 scan_inout_check(swjdp, SWJDP_IR_APACC, reg_addr, DPAP_READ, 0, in_value_buf);
312 int ahbap_write_reg_u32(swjdp_common_t *swjdp, u32 reg_addr, u32 value)
316 buf_set_u32(out_value_buf, 0, 32, value);
317 swjdp_bankselect_apacc(swjdp, reg_addr);
318 scan_inout_check(swjdp, SWJDP_IR_APACC, reg_addr, DPAP_WRITE, out_value_buf, NULL);
323 int ahbap_read_reg_u32(swjdp_common_t *swjdp, u32 reg_addr, u32 *value)
325 swjdp_bankselect_apacc(swjdp, reg_addr);
326 scan_inout_check_u32(swjdp, SWJDP_IR_APACC, reg_addr, DPAP_READ, 0, value);
331 /***************************************************************************
333 * AHB-AP access to memory and system registers on AHB bus *
335 ***************************************************************************/
337 int ahbap_setup_accessport(swjdp_common_t *swjdp, u32 csw, u32 tar)
339 csw = csw | CSW_DBGSWENABLE | CSW_MASTER_DEBUG | CSW_HPROT;
340 if (csw != swjdp->ap_csw_value)
342 /* LOG_DEBUG("swjdp : Set CSW %x",csw); */
343 ahbap_write_reg_u32(swjdp, AHBAP_CSW, csw );
344 swjdp->ap_csw_value = csw;
346 if (tar != swjdp->ap_tar_value)
348 /* LOG_DEBUG("swjdp : Set TAR %x",tar); */
349 ahbap_write_reg_u32(swjdp, AHBAP_TAR, tar );
350 swjdp->ap_tar_value = tar;
352 if (csw & CSW_ADDRINC_MASK)
354 /* Do not cache TAR value when autoincrementing */
355 swjdp->ap_tar_value = -1;
360 /*****************************************************************************
362 * ahbap_read_system_u32(swjdp_common_t *swjdp, u32 address, u32 *value) *
364 * Read a u32 value from memory or system register *
365 * Functionally equivalent to target_read_u32(target, address, u32 *value), *
366 * but with less overhead *
367 *****************************************************************************/
368 int ahbap_read_system_u32(swjdp_common_t *swjdp, u32 address, u32 *value)
370 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
372 ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, address & 0xFFFFFFF0);
373 ahbap_read_reg_u32(swjdp, AHBAP_BD0 | (address & 0xC), value );
378 int ahbap_read_system_atomic_u32(swjdp_common_t *swjdp, u32 address, u32 *value)
380 ahbap_read_system_u32(swjdp, address, value);
382 return swjdp_transaction_endcheck(swjdp);
385 /*****************************************************************************
387 * ahbap_write_system_u32(swjdp_common_t *swjdp, u32 address, u32 value) *
389 * Write a u32 value to memory or system register *
391 *****************************************************************************/
392 int ahbap_write_system_u32(swjdp_common_t *swjdp, u32 address, u32 value)
394 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
396 ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, address & 0xFFFFFFF0);
397 ahbap_write_reg_u32(swjdp, AHBAP_BD0 | (address & 0xC), value );
402 int ahbap_write_system_atomic_u32(swjdp_common_t *swjdp, u32 address, u32 value)
404 ahbap_write_system_u32(swjdp, address, value);
406 return swjdp_transaction_endcheck(swjdp);
409 /*****************************************************************************
411 * ahbap_write_buf(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address) *
413 * Write a buffer in target order (little endian) *
415 *****************************************************************************/
416 int ahbap_write_buf_u32(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
419 int wcount, blocksize, writecount, errorcount = 0, retval = ERROR_OK;
421 u8* pBuffer = buffer;
423 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
428 /* if we have an unaligned access - reorder data */
431 for (writecount = 0; writecount < count; writecount++)
434 outvalue = *((u32*)pBuffer);
436 for (i = 0; i < 4; i++ )
438 *((u8*)pBuffer + (adr & 0x3)) = outvalue;
448 /* Adjust to write blocks within 4K aligned boundaries */
449 blocksize = (0x1000 - (0xFFF & address)) >> 2;
450 if (wcount < blocksize)
453 /* handle unaligned data at 4k boundary */
457 ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
459 for (writecount = 0; writecount < blocksize; writecount++)
461 ahbap_write_reg(swjdp, AHBAP_DRW, buffer + 4 * writecount );
464 if (swjdp_transaction_endcheck(swjdp) == ERROR_OK)
466 wcount = wcount - blocksize;
467 address = address + 4 * blocksize;
468 buffer = buffer + 4 * blocksize;
477 LOG_WARNING("Block write error address 0x%x, wcount 0x%x", address, wcount);
478 return ERROR_JTAG_DEVICE_ERROR;
485 int ahbap_write_buf_packed_u16(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
488 int retval = ERROR_OK;
489 int wcount, blocksize, writecount, i;
491 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
499 /* Adjust to read within 4K block boundaries */
500 blocksize = (0x1000 - (0xFFF & address)) >> 1;
502 if (wcount < blocksize)
505 /* handle unaligned data at 4k boundary */
509 ahbap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_PACKED, address);
510 writecount = blocksize;
514 nbytes = MIN((writecount << 1), 4);
518 if (ahbap_write_buf_u16(swjdp, buffer, nbytes, address) != ERROR_OK)
520 LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
521 return ERROR_JTAG_DEVICE_ERROR;
524 address += nbytes >> 1;
528 outvalue = *((u32*)buffer);
530 for (i = 0; i < nbytes; i++ )
532 *((u8*)buffer + (address & 0x3)) = outvalue;
537 outvalue = *((u32*)buffer);
538 ahbap_write_reg_u32(swjdp, AHBAP_DRW, outvalue);
539 if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
541 LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
542 return ERROR_JTAG_DEVICE_ERROR;
546 buffer += nbytes >> 1;
547 writecount -= nbytes >> 1;
549 } while (writecount);
556 int ahbap_write_buf_u16(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
559 int retval = ERROR_OK;
562 return ahbap_write_buf_packed_u16(swjdp, buffer, count, address);
564 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
568 ahbap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
569 outvalue = *((u16*)buffer) << 8 * (address & 0x3);
570 ahbap_write_reg_u32(swjdp, AHBAP_DRW, outvalue );
571 retval = swjdp_transaction_endcheck(swjdp);
580 int ahbap_write_buf_packed_u8(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
583 int retval = ERROR_OK;
584 int wcount, blocksize, writecount, i;
586 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
594 /* Adjust to read within 4K block boundaries */
595 blocksize = (0x1000 - (0xFFF & address));
597 if (wcount < blocksize)
600 ahbap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_PACKED, address);
601 writecount = blocksize;
605 nbytes = MIN(writecount, 4);
609 if (ahbap_write_buf_u8(swjdp, buffer, nbytes, address) != ERROR_OK)
611 LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
612 return ERROR_JTAG_DEVICE_ERROR;
619 outvalue = *((u32*)buffer);
621 for (i = 0; i < nbytes; i++ )
623 *((u8*)buffer + (address & 0x3)) = outvalue;
628 outvalue = *((u32*)buffer);
629 ahbap_write_reg_u32(swjdp, AHBAP_DRW, outvalue);
630 if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
632 LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
633 return ERROR_JTAG_DEVICE_ERROR;
638 writecount -= nbytes;
640 } while (writecount);
647 int ahbap_write_buf_u8(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
650 int retval = ERROR_OK;
653 return ahbap_write_buf_packed_u8(swjdp, buffer, count, address);
655 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
659 ahbap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
660 outvalue = *((u8*)buffer) << 8 * (address & 0x3);
661 ahbap_write_reg_u32(swjdp, AHBAP_DRW, outvalue );
662 retval = swjdp_transaction_endcheck(swjdp);
671 /*********************************************************************************
673 * ahbap_read_buf_u32(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address) *
675 * Read block fast in target order (little endian) into a buffer *
677 **********************************************************************************/
678 int ahbap_read_buf_u32(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
680 int wcount, blocksize, readcount, errorcount = 0, retval = ERROR_OK;
682 u8* pBuffer = buffer;
684 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
691 /* Adjust to read within 4K block boundaries */
692 blocksize = (0x1000 - (0xFFF & address)) >> 2;
693 if (wcount < blocksize)
696 /* handle unaligned data at 4k boundary */
700 ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
702 /* Scan out first read */
703 swjdp_scan(swjdp->jtag_info, SWJDP_IR_APACC, AHBAP_DRW, DPAP_READ, 0, NULL, NULL);
704 for (readcount = 0; readcount < blocksize - 1; readcount++)
706 /* Scan out read instruction and scan in previous value */
707 swjdp_scan(swjdp->jtag_info, SWJDP_IR_APACC, AHBAP_DRW, DPAP_READ, 0, buffer + 4 * readcount, &swjdp->ack);
710 /* Scan in last value */
711 swjdp_scan(swjdp->jtag_info, SWJDP_IR_DPACC, DP_RDBUFF, DPAP_READ, 0, buffer + 4 * readcount, &swjdp->ack);
712 if (swjdp_transaction_endcheck(swjdp) == ERROR_OK)
714 wcount = wcount - blocksize;
715 address += 4 * blocksize;
716 buffer += 4 * blocksize;
725 LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
726 return ERROR_JTAG_DEVICE_ERROR;
730 /* if we have an unaligned access - reorder data */
733 for (readcount = 0; readcount < count; readcount++)
736 u32 data = *((u32*)pBuffer);
738 for (i = 0; i < 4; i++ )
740 *((u8*)pBuffer) = (data >> 8 * (adr & 0x3));
750 int ahbap_read_buf_packed_u16(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
753 int retval = ERROR_OK;
754 int wcount, blocksize, readcount, i;
756 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
764 /* Adjust to read within 4K block boundaries */
765 blocksize = (0x1000 - (0xFFF & address)) >> 1;
766 if (wcount < blocksize)
769 ahbap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_PACKED, address);
771 /* handle unaligned data at 4k boundary */
774 readcount = blocksize;
778 ahbap_read_reg_u32(swjdp, AHBAP_DRW, &invalue );
779 if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
781 LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
782 return ERROR_JTAG_DEVICE_ERROR;
785 nbytes = MIN((readcount << 1), 4);
787 for (i = 0; i < nbytes; i++ )
789 *((u8*)buffer) = (invalue >> 8 * (address & 0x3));
794 readcount -= (nbytes >> 1);
802 int ahbap_read_buf_u16(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
805 int retval = ERROR_OK;
808 return ahbap_read_buf_packed_u16(swjdp, buffer, count, address);
810 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
814 ahbap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
815 ahbap_read_reg_u32(swjdp, AHBAP_DRW, &invalue );
816 retval = swjdp_transaction_endcheck(swjdp);
819 for (i = 0; i < 2; i++ )
821 *((u8*)buffer) = (invalue >> 8 * (address & 0x3));
828 *((u16*)buffer) = (invalue >> 8 * (address & 0x3));
838 int ahbap_read_buf_packed_u8(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
841 int retval = ERROR_OK;
842 int wcount, blocksize, readcount, i;
844 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
852 /* Adjust to read within 4K block boundaries */
853 blocksize = (0x1000 - (0xFFF & address));
855 if (wcount < blocksize)
858 ahbap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_PACKED, address);
859 readcount = blocksize;
863 ahbap_read_reg_u32(swjdp, AHBAP_DRW, &invalue );
864 if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
866 LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
867 return ERROR_JTAG_DEVICE_ERROR;
870 nbytes = MIN(readcount, 4);
872 for (i = 0; i < nbytes; i++ )
874 *((u8*)buffer) = (invalue >> 8 * (address & 0x3));
887 int ahbap_read_buf_u8(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
890 int retval = ERROR_OK;
893 return ahbap_read_buf_packed_u8(swjdp, buffer, count, address);
895 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
899 ahbap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
900 ahbap_read_reg_u32(swjdp, AHBAP_DRW, &invalue );
901 retval = swjdp_transaction_endcheck(swjdp);
902 *((u8*)buffer) = (invalue >> 8 * (address & 0x3));
911 int ahbap_read_coreregister_u32(swjdp_common_t *swjdp, u32 *value, int regnum)
916 /* because the DCB_DCRDR is used for the emulated dcc channel
917 * we gave to save/restore the DCB_DCRDR when used */
919 ahbap_read_system_atomic_u32(swjdp, DCB_DCRDR, &dcrdr);
921 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
923 /* ahbap_write_system_u32(swjdp, DCB_DCRSR, regnum); */
924 ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
925 ahbap_write_reg_u32(swjdp, AHBAP_BD0 | (DCB_DCRSR & 0xC), regnum );
927 /* ahbap_read_system_u32(swjdp, DCB_DCRDR, value); */
928 ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
929 ahbap_read_reg_u32(swjdp, AHBAP_BD0 | (DCB_DCRDR & 0xC), value );
931 retval = swjdp_transaction_endcheck(swjdp);
932 ahbap_write_system_atomic_u32(swjdp, DCB_DCRDR, dcrdr);
936 int ahbap_write_coreregister_u32(swjdp_common_t *swjdp, u32 value, int regnum)
941 /* because the DCB_DCRDR is used for the emulated dcc channel
942 * we gave to save/restore the DCB_DCRDR when used */
944 ahbap_read_system_atomic_u32(swjdp, DCB_DCRDR, &dcrdr);
946 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
948 /* ahbap_write_system_u32(swjdp, DCB_DCRDR, core_regs[i]); */
949 ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
950 ahbap_write_reg_u32(swjdp, AHBAP_BD0 | (DCB_DCRDR & 0xC), value );
952 /* ahbap_write_system_u32(swjdp, DCB_DCRSR, i | DCRSR_WnR ); */
953 ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
954 ahbap_write_reg_u32(swjdp, AHBAP_BD0 | (DCB_DCRSR & 0xC), regnum | DCRSR_WnR );
956 retval = swjdp_transaction_endcheck(swjdp);
957 ahbap_write_system_atomic_u32(swjdp, DCB_DCRDR, dcrdr);
961 int ahbap_debugport_init(swjdp_common_t *swjdp)
963 u32 idreg, romaddr, dummy;
970 swjdp->ap_csw_value = -1;
971 swjdp->ap_tar_value = -1;
972 swjdp->trans_mode = TRANS_MODE_ATOMIC;
973 swjdp_read_dpacc(swjdp, &dummy, DP_CTRL_STAT);
974 swjdp_write_dpacc(swjdp, SSTICKYERR, DP_CTRL_STAT);
975 swjdp_read_dpacc(swjdp, &dummy, DP_CTRL_STAT);
977 swjdp->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
979 swjdp_write_dpacc(swjdp, swjdp->dp_ctrl_stat, DP_CTRL_STAT);
980 swjdp_read_dpacc(swjdp, &ctrlstat, DP_CTRL_STAT);
981 if ((retval=jtag_execute_queue())!=ERROR_OK)
984 /* Check that we have debug power domains activated */
985 while (!(ctrlstat & CDBGPWRUPACK) && (cnt++ < 10))
987 LOG_DEBUG("swjdp: wait CDBGPWRUPACK");
988 swjdp_read_dpacc(swjdp, &ctrlstat, DP_CTRL_STAT);
989 if ((retval=jtag_execute_queue())!=ERROR_OK)
994 while (!(ctrlstat & CSYSPWRUPACK) && (cnt++ < 10))
996 LOG_DEBUG("swjdp: wait CSYSPWRUPACK");
997 swjdp_read_dpacc(swjdp, &ctrlstat, DP_CTRL_STAT);
998 if ((retval=jtag_execute_queue())!=ERROR_OK)
1003 swjdp_read_dpacc(swjdp, &dummy, DP_CTRL_STAT);
1004 /* With debug power on we can activate OVERRUN checking */
1005 swjdp->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ | CORUNDETECT;
1006 swjdp_write_dpacc(swjdp, swjdp->dp_ctrl_stat, DP_CTRL_STAT);
1007 swjdp_read_dpacc(swjdp, &dummy, DP_CTRL_STAT);
1009 ahbap_read_reg_u32(swjdp, 0xFC, &idreg);
1010 ahbap_read_reg_u32(swjdp, 0xF8, &romaddr);
1012 LOG_DEBUG("AHB-AP ID Register 0x%x, Debug ROM Address 0x%x", idreg, romaddr);