1 # SPDX-License-Identifier: GPL-2.0-or-later
4 # GigaDevice GD32VF103 target
6 # https://www.gigadevice.com/products/microcontrollers/gd32/risc-v/
9 source [find mem_helper.tcl]
13 if { [info exists CHIPNAME] } {
14 set _CHIPNAME $CHIPNAME
16 set _CHIPNAME gd32vf103
19 # The smallest RAM size 6kB (GD32VF103C4/T4/R4)
20 if { [info exists WORKAREASIZE] } {
21 set _WORKAREASIZE $WORKAREASIZE
23 set _WORKAREASIZE 0x1800
26 jtag newtap $_CHIPNAME cpu -irlen 5 -expected-id 0x1000563d
28 set _TARGETNAME $_CHIPNAME.cpu
29 target create $_TARGETNAME riscv -chain-position $_TARGETNAME
31 proc default_mem_access {} {
32 riscv set_mem_access progbuf
37 $_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
39 set _FLASHNAME $_CHIPNAME.flash
40 flash bank $_FLASHNAME stm32f1x 0x08000000 0 0 0 $_TARGETNAME
42 # DBGMCU_CR register cannot be set in examine-end event as the running RISC-V CPU
43 # does not allow the debugger to access memory.
44 # Stop watchdogs at least before flash programming.
45 $_TARGETNAME configure -event reset-init {
46 # DBGMCU_CR |= DBG_WWDG_STOP | DBG_IWDG_STOP
47 mmw 0xE0042004 0x00000300 0
50 # On this chip, ndmreset (the debug module bit that triggers a software reset)
51 # doesn't work. So for JTAG connections without an SRST, we need to trigger a
52 # reset manually. This is an undocumented reset sequence that's used by the
53 # JTAG flashing script in the vendor-supplied GD32VF103 PlatformIO plugin:
55 # https://github.com/sipeed/platform-gd32v/commit/f9cbb44819bc05dd2010cc815c32be0486800cc2
57 $_TARGETNAME configure -event reset-assert {
59 set dmcontrol_dmactive [expr {1 << 0}]
60 set dmcontrol_ackhavereset [expr {1 << 28}]
61 set dmcontrol_haltreq [expr {1 << 31}]
65 # If hardware NRST signal is connected and configured (reset_config srst_only)
66 # the device has been recently reset in 'jtag arp_init-reset', therefore
67 # DM_DMSTATUS_ANYHAVERESET reads 1.
68 # The following 'halt' command checks this status bit
69 # and shows 'Hart 0 unexpectedly reset!' if set.
70 # Prevent this message by sending an acknowledge first.
71 set val [expr {$dmcontrol_dmactive | $dmcontrol_ackhavereset}]
72 riscv dmi_write $dmcontrol $val
74 # Halt the core so that we can write to memory. We do this first so
75 # that it doesn't clobber our dmcontrol configuration.
78 # Set haltreq appropriately for the type of reset we're doing. This
79 # replicates what the generic RISC-V reset_assert() function would
80 # do if we weren't overriding it. The $_RESETMODE hack sucks, but
81 # it's the least invasive way to determine whether we need to halt.
83 # If we didn't override the generic handler, we'd actually still have
84 # to do this: the default handler sets ndmreset, which prevents memory
85 # access even though it doesn't actually trigger a reset on this chip.
86 # So we'd need to unset it here, which involves a write to dmcontrol,
87 # Since haltreq is write-only and there's no way to leave it unchanged,
88 # we'd have to figure out its proper value anyway.
89 set val $dmcontrol_dmactive
90 if {$_RESETMODE ne "run"} {
91 set val [expr {$val | $dmcontrol_haltreq}]
93 riscv dmi_write $dmcontrol $val
95 # Unlock 0xe0042008 so that the next write triggers a reset
96 mww 0xe004200c 0x4b5a6978
98 # We need to trigger the reset using abstract memory access, since
99 # progbuf access tries to read a status code out of a core register
100 # after the write happens, which fails when the core is in reset.
101 riscv set_mem_access abstract
106 # Put the memory access mode back to what it was.
110 # Capture the mode of a given reset so that we can use it later in the
111 # reset-assert handler.
112 proc init_reset { mode } {