1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # common script for stm32l5x and stm32u5x families
5 # Work-area is a space in RAM used for flash programming
7 if { [info exists WORKAREASIZE] } {
8 set _WORKAREASIZE $WORKAREASIZE
10 set _WORKAREASIZE 0x10000
14 if { [info exists CPUTAPID] } {
15 set _CPUTAPID $CPUTAPID
18 # STM32L5x: RM0438 Rev5, Section 52.2.8 JTAG debug port - Table 425. JTAG-DP data registers
19 # STM32U5x: RM0456 Rev1, Section 65.2.8 JTAG debug port - Table 661. JTAG-DP data registers
20 # Corresponds to Cortex®-M33 JTAG debug port ID code
21 set _CPUTAPID 0x0ba04477
23 # SWD IDCODE (single drop, arm)
24 set _CPUTAPID 0x0be12477
28 swj_newdap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID
29 dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
32 jtag newtap $_CHIPNAME bs -irlen 5
35 set _TARGETNAME $_CHIPNAME.cpu
36 target create $_TARGETNAME cortex_m -endian little -dap $_CHIPNAME.dap
38 # use non-secure RAM by default
39 $_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
41 # create sec/ns flash and otp memories (sizes will be probed)
42 flash bank $_CHIPNAME.flash_ns stm32l4x 0x08000000 0 0 0 $_TARGETNAME
43 flash bank $_CHIPNAME.flash_alias_s stm32l4x 0x0C000000 0 0 0 $_TARGETNAME
44 flash bank $_CHIPNAME.otp stm32l4x 0x0BFA0000 0 0 0 $_TARGETNAME
46 # Common knowledge tells JTAG speed should be <= F_CPU/6.
47 # F_CPU after reset is MSI 4MHz, so use F_JTAG = 500 kHz to stay on
50 # Note that there is a pretty wide band where things are
51 # more or less stable, see http://review.openocd.org/3366
54 adapter srst delay 100
59 reset_config srst_nogate
62 # if srst is not fitted use SYSRESETREQ to
63 # perform a soft reset
64 cortex_m reset_config sysresetreq
67 proc stm32x5x_is_secure {} {
68 # read Debug Security Control and Status Register (DSCSR) and check CDS (bit 16)
69 set DSCSR [mrw 0xE000EE08]
70 return [expr {($DSCSR & (1 << 16)) != 0}]
73 proc stm32x5x_ahb_ap_non_secure_access {} {
74 # SPROT=1=Non Secure access, Priv=1
75 [[target current] cget -dap] apcsw 0x4B000000 0x4F000000
78 proc stm32x5x_ahb_ap_secure_access {} {
79 # SPROT=0=Secure access, Priv=1
80 [[target current] cget -dap] apcsw 0x0B000000 0x4F000000
83 $_TARGETNAME configure -event reset-start {
84 # Reset clock is MSI (4 MHz)
88 $_TARGETNAME configure -event examine-end {
89 # DBGMCU_CR |= DBG_STANDBY | DBG_STOP
90 mmw 0xE0044004 0x00000006 0
92 # Stop watchdog counters during halt
93 # DBGMCU_APB1_FZ |= DBG_IWDG_STOP | DBG_WWDG_STOP
94 mmw 0xE0044008 0x00001800 0
97 $_TARGETNAME configure -event halted {
98 set secure [stm32x5x_is_secure]
101 set secure_str "Secure"
102 stm32x5x_ahb_ap_secure_access
104 set secure_str "Non-Secure"
105 stm32x5x_ahb_ap_non_secure_access
108 # print the secure state only when it changes
109 set _TARGETNAME [target current]
110 global $_TARGETNAME.secure
112 if {![info exists $_TARGETNAME.secure] || $secure != [set $_TARGETNAME.secure]} {
113 echo "CPU in $secure_str state"
114 # update saved security state
115 set $_TARGETNAME.secure $secure
119 $_TARGETNAME configure -event gdb-flash-erase-start {
120 set use_secure_workarea 0
121 # check if FLASH_OPTR.TZEN is enabled
122 set FLASH_OPTR [mrw 0x40022040]
123 if {[expr {$FLASH_OPTR & 0x80000000}] == 0} {
124 echo "TZEN option bit disabled"
125 stm32x5x_ahb_ap_non_secure_access
127 stm32x5x_ahb_ap_secure_access
128 echo "TZEN option bit enabled"
130 # check if FLASH_OPTR.RDP is not Level 0.5
131 if {[expr {$FLASH_OPTR & 0xFF}] != 0x55} {
132 set use_secure_workarea 1
136 set _TARGETNAME [target current]
137 set workarea_addr [$_TARGETNAME cget -work-area-phys]
138 echo "workarea_addr $workarea_addr"
140 if {$use_secure_workarea} {
141 set workarea_addr [expr {$workarea_addr | 0x10000000}]
143 set workarea_addr [expr {$workarea_addr & ~0x10000000}]
146 $_TARGETNAME configure -work-area-phys $workarea_addr
149 $_TARGETNAME configure -event trace-config {
150 # Set TRACE_IOEN; TRACE_MODE is set to async; when using sync
151 # change this value accordingly to configure trace pins
153 mmw 0xE0044004 0x00000020 0