@@ -56,6 +56,24 @@ $_TARGETNAME configure -event reset-init {
5656 mmw 0xE0042004 0x00000300 0
5757}
5858
59+ set dmcontrol 0x10
60+ set dmcontrol_dmactive [expr {1 << 0}]
61+ set dmcontrol_clrresethaltreq [expr {1 << 2}]
62+ set dmcontrol_setresethaltreq [expr {1 << 3}]
63+ set dmcontrol_ackhavereset [expr {1 << 28}]
64+ set dmstatus 0x11
65+ set dmstatus_allunavail [expr {1 << 12}]
66+ set dmstatus_allhavereset [expr {1 << 19}]
67+
68+ $_TARGETNAME configure -event reset-start {
69+ if {$halt} {
70+ set ctrl [expr {$::dmcontrol_dmactive | $::dmcontrol_setresethaltreq}]
71+ } else {
72+ set ctrl [expr {$::dmcontrol_dmactive | $::dmcontrol_clrresethaltreq}]
73+ }
74+ riscv dmi_write $::dmcontrol $ctrl
75+ }
76+
5977# On this chip, ndmreset (the debug module bit that triggers a software reset)
6078# doesn't work. So for JTAG connections without an SRST, we need to trigger a
6179# reset manually. This is an undocumented reset sequence that's used by the
@@ -64,42 +82,19 @@ $_TARGETNAME configure -event reset-init {
6482# https://github.com/sipeed/platform-gd32v/commit/f9cbb44819bc05dd2010cc815c32be0486800cc2
6583#
6684$_TARGETNAME configure -event reset-assert {
67- set dmcontrol 0x10
68- set dmcontrol_dmactive [expr {1 << 0}]
69- set dmcontrol_ackhavereset [expr {1 << 28}]
70- set dmcontrol_haltreq [expr {1 << 31}]
71-
72- global _RESETMODE
73-
74- # If hardware NRST signal is connected and configured (reset_config srst_only)
75- # the device has been recently reset in 'jtag arp_init-reset', therefore
76- # DM_DMSTATUS_ANYHAVERESET reads 1.
77- # The following 'halt' command checks this status bit
78- # and shows 'Hart 0 unexpectedly reset!' if set.
79- # Prevent this message by sending an acknowledge first.
80- set val [expr {$dmcontrol_dmactive | $dmcontrol_ackhavereset}]
81- riscv dmi_write $dmcontrol $val
85+ set reset_config_options [reset_config]
86+ # If hardware NRST signal is connected and configured, reset has been
87+ # triggered. Avoid second reset and return early
88+ if {[string match {srst_only *} $reset_config_options]
89+ || [string match {srst_and_trst *} $reset_config_options]} {
90+ return
91+ }
8292
8393 # Halt the core so that we can write to memory. We do this first so
8494 # that it doesn't clobber our dmcontrol configuration.
8595 halt
8696
87- # Set haltreq appropriately for the type of reset we're doing. This
88- # replicates what the generic RISC-V reset_assert() function would
89- # do if we weren't overriding it. The $_RESETMODE hack sucks, but
90- # it's the least invasive way to determine whether we need to halt.
91- #
92- # If we didn't override the generic handler, we'd actually still have
93- # to do this: the default handler sets ndmreset, which prevents memory
94- # access even though it doesn't actually trigger a reset on this chip.
95- # So we'd need to unset it here, which involves a write to dmcontrol,
96- # Since haltreq is write-only and there's no way to leave it unchanged,
97- # we'd have to figure out its proper value anyway.
98- set val $dmcontrol_dmactive
99- if {$halt} {
100- set val [expr {$val | $dmcontrol_haltreq}]
101- }
102- riscv dmi_write $dmcontrol $val
97+ echo " gd32vf103 reset workaround halt=$halt"
10398
10499 # Unlock 0xe0042008 so that the next write triggers a reset
105100 mww 0xe004200c 0x4b5a6978
@@ -122,12 +117,21 @@ $_TARGETNAME configure -event reset-assert {
122117# lowered in the main deassert_reset procedure, we wait for the absence of the
123118# unavailable state.
124119$_TARGETNAME configure -event reset-deassert-post {
125- set timeout_s 2
126- set start [clock seconds]
127- # dmstatus address is 0x11, allunavail is the 12th bit
128- while {[riscv dmi_read 0x11] & 1 << 12} {
129- if {[clock seconds] - $start > $timeout_s} {
120+ set timeout_ms 100
121+ set start [clock milliseconds]
122+ while {1} {
123+ set status [riscv dmi_read $::dmstatus]
124+ if {!($status & $::dmstatus_allunavail)} {
125+ break
126+ }
127+ if {[clock milliseconds] - $start > $timeout_ms} {
130128 error {Timed out waiting for the hart to become available after a reset}
131129 }
132130 }
131+
132+ set ctrl [expr {$::dmcontrol_dmactive | $::dmcontrol_clrresethaltreq}]
133+ if {$status & $::dmstatus_allhavereset} {
134+ set ctrl [expr {$ctrl | $::dmcontrol_ackhavereset}]
135+ }
136+ riscv dmi_write $::dmcontrol $ctrl
133137}
0 commit comments