|
| 1 | +# SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | + |
| 3 | +# Script for Bouffalo chips with similar architecture used in BL602 |
| 4 | +# based on SiFive E21 core |
| 5 | + |
| 6 | +source [find mem_helper.tcl] |
| 7 | + |
| 8 | +transport select jtag |
| 9 | + |
| 10 | +if { [info exists CPUTAPID ] } { |
| 11 | + set _CPUTAPID $CPUTAPID |
| 12 | +} else { |
| 13 | + error "you must specify a tap id" |
| 14 | +} |
| 15 | + |
| 16 | +if { [info exists BL602_CHIPNAME] } { |
| 17 | + set _CHIPNAME $BL602_CHIPNAME |
| 18 | +} else { |
| 19 | + error "you must specify a chip name" |
| 20 | +} |
| 21 | + |
| 22 | +if { [info exists WORKAREAADDR] } { |
| 23 | + set _WORKAREAADDR $WORKAREAADDR |
| 24 | +} else { |
| 25 | + error "you must specify a work area address" |
| 26 | +} |
| 27 | + |
| 28 | +if { [info exists WORKAREASIZE] } { |
| 29 | + set _WORKAREASIZE $WORKAREASIZE |
| 30 | +} else { |
| 31 | + error "you must specify a work area size" |
| 32 | +} |
| 33 | + |
| 34 | +jtag newtap $_CHIPNAME cpu -irlen 5 -expected-id $_CPUTAPID |
| 35 | + |
| 36 | +set _TARGETNAME $_CHIPNAME.cpu |
| 37 | +target create $_TARGETNAME riscv -chain-position $_TARGETNAME |
| 38 | + |
| 39 | +riscv set_mem_access sysbus |
| 40 | +riscv set_enable_virt2phys off |
| 41 | + |
| 42 | +$_TARGETNAME configure -work-area-phys $_WORKAREAADDR -work-area-size $_WORKAREASIZE -work-area-backup 1 |
| 43 | + |
| 44 | +# Internal RC ticks on 32 MHz, so this speed should be safe to use. |
| 45 | +adapter speed 8000 |
| 46 | + |
| 47 | +# Useful functions |
| 48 | + |
| 49 | +set dmcontrol 0x10 |
| 50 | +set dmcontrol_dmactive [expr {1 << 0}] |
| 51 | +set dmcontrol_ndmreset [expr {1 << 1}] |
| 52 | +set dmcontrol_resumereq [expr {1 << 30}] |
| 53 | +set dmcontrol_haltreq [expr {1 << 31}] |
| 54 | + |
| 55 | +proc bl602_restore_clock_defaults { } { |
| 56 | + # Switch clock to internal RC32M |
| 57 | + # In HBN_GLB, set ROOT_CLK_SEL = 0 |
| 58 | + mmw 0x4000f030 0x0 0x00000003 |
| 59 | + # Wait for clock switch |
| 60 | + sleep 10 |
| 61 | + |
| 62 | + # GLB_REG_BCLK_DIS_FALSE |
| 63 | + mww 0x40000ffc 0x0 |
| 64 | + |
| 65 | + # HCLK is RC32M, so BCLK/HCLK doesn't need divider |
| 66 | + # In GLB_CLK_CFG0, set BCLK_DIV = 0 and HCLK_DIV = 0 |
| 67 | + mmw 0x40000000 0x0 0x00FFFF00 |
| 68 | + # Wait for clock to stabilize |
| 69 | + sleep 10 |
| 70 | +} |
| 71 | + |
| 72 | +# By spec, ndmreset should reset whole chip. This implementation resets only few parts of the chip. |
| 73 | +# CTRL_PWRON_RESET register in GLB core triggers full "power-on like" reset, so we use it instead |
| 74 | +# for full software reset. |
| 75 | +proc bl602_sw_reset { } { |
| 76 | + # In GLB_SWRST_CFG2, clear CTRL_SYS_RESET, CTRL_CPU_RESET and CTRL_PWRON_RESET |
| 77 | + mmw 0x40000018 0x0 0x00000007 |
| 78 | + |
| 79 | + # This Software reset method resets everything, so CPU as well. |
| 80 | + # It does that in not much good way, resulting in Debug Module being reset as well. |
| 81 | + # This also means, that right after CPU and Debug Module are turned on, we need to |
| 82 | + # enable Debug Module and halt CPU if needed. Additionally, we trigger this SW reset |
| 83 | + # through system bus access directly with DMI commands, to avoid errors printed by |
| 84 | + # OpenOCD about unsuccessful register write. |
| 85 | + |
| 86 | + # In GLB_SWRST_CFG2, set CTRL_SYS_RESET, CTRL_CPU_RESET and CTRL_PWRON_RESET to 1 |
| 87 | + riscv dmi_write 0x39 0x40000018 |
| 88 | + riscv dmi_write 0x3c 0x7 |
| 89 | + |
| 90 | + # We need to wait for chip to finish reset and execute BootROM |
| 91 | + sleep 1 |
| 92 | + |
| 93 | + # JTAG Debug Transport Module is reset as well, so we need to get into RUN/IDLE state |
| 94 | + runtest 10 |
| 95 | + |
| 96 | + # We need to enable Debug Module and halt the CPU, so we can reset Program Counter |
| 97 | + # and to do additional clean-ups. If reset was called without halt, resume is handled |
| 98 | + # by reset-deassert-post event handler. |
| 99 | + |
| 100 | + # In Debug Module Control (dmcontrol), set dmactive to 1 and then haltreq to 1 |
| 101 | + riscv dmi_write $::dmcontrol $::dmcontrol_dmactive |
| 102 | + riscv dmi_write $::dmcontrol [ expr {$::dmcontrol_dmactive | $::dmcontrol_haltreq} ] |
| 103 | + |
| 104 | + # Set Program Counter to start of BootROM |
| 105 | + set_reg {pc 0x21000000} |
| 106 | +} |
| 107 | + |
| 108 | +# On BL602 and BL702, the only way to force chip stay in BootROM (until JTAG attaches) |
| 109 | +# is by putting infinity loop into HBN RAM (which is not reset by sw reset), and then |
| 110 | +# configure HBN registers, which will cause BootROM to jump into our code early in BootROM. |
| 111 | +proc bl602_sw_reset_hbn_wait {} { |
| 112 | + # Restore clocks to defaults |
| 113 | + bl602_restore_clock_defaults |
| 114 | + |
| 115 | + # In HBN RAM, write infinity loop instruction |
| 116 | + # beq zero, zero, 0 |
| 117 | + mww 0x40010000 0x00000063 |
| 118 | + # In HNB, set HBN_RSV0 (Status Flag) to "EHBN" (as uint32_t) |
| 119 | + mww 0x4000f100 0x4e424845 |
| 120 | + # In HBN, set HBN_RSV1 (WakeUp Address) to HBN RAM address |
| 121 | + mww 0x4000f104 0x40010000 |
| 122 | + |
| 123 | + # Perform software reset |
| 124 | + bl602_sw_reset |
| 125 | + |
| 126 | + # Clear HBN RAM, HBN_RSV0 and HBN_RSV1 |
| 127 | + mww 0x40010000 0x00000000 |
| 128 | + mww 0x4000f100 0x00000000 |
| 129 | + mww 0x4000f104 0x00000000 |
| 130 | + |
| 131 | + # This early jump method locks up BootROM through Trust Zone Controller. |
| 132 | + # That means any read of BootROM returns 0xDEADBEEF. |
| 133 | + # Only way to reset it, is through JTAG Reset, thus toggling ndmreset in dmcontrol. |
| 134 | + riscv dmi_write $::dmcontrol [ expr {$::dmcontrol_dmactive | $::dmcontrol_ndmreset} ] |
| 135 | + riscv dmi_write $::dmcontrol [ expr {$::dmcontrol_dmactive} ] |
| 136 | +} |
| 137 | + |
| 138 | +$_TARGETNAME configure -event reset-deassert-post { |
| 139 | + # Resume the processor if reset was triggered without halt request |
| 140 | + if {$halt == 0} { |
| 141 | + riscv dmi_write $::dmcontrol [ expr {$::dmcontrol_dmactive | $::dmcontrol_resumereq} ] |
| 142 | + } |
| 143 | +} |
0 commit comments