flash/stm32l4x: STM32L55/L56xx basic support (non-secure mode)
[fw/openocd] / tcl / target / stm32l5x.cfg
1 # SPDX-License-Identifier: GPL-2.0-or-later
2
3 # script for stm32l5x family
4
5 #
6 # stm32l5 devices support both JTAG and SWD transports.
7 #
8 source [find target/swj-dp.tcl]
9 source [find mem_helper.tcl]
10
11 if { [info exists CHIPNAME] } {
12         set _CHIPNAME $CHIPNAME
13 } else {
14         set _CHIPNAME stm32l5x
15 }
16
17 set _ENDIAN little
18
19 # Work-area is a space in RAM used for flash programming
20 # By default use 64kB
21 if { [info exists WORKAREASIZE] } {
22         set _WORKAREASIZE $WORKAREASIZE
23 } else {
24         set _WORKAREASIZE 0x10000
25 }
26
27 #jtag scan chain
28 if { [info exists CPUTAPID] } {
29         set _CPUTAPID $CPUTAPID
30 } else {
31         if { [using_jtag] } {
32                 # See STM Document RM0438
33                 # RM0438 Rev5, Section 52.2.8 JTAG debug port - Table 425. JTAG-DP data registers
34                 # Corresponds to Cortex®-M33 JTAG debug port ID code
35                 set _CPUTAPID 0x0ba04477
36         } {
37                 # SWD IDCODE (single drop, arm)
38                 set _CPUTAPID 0x0be12477
39         }
40 }
41
42 swj_newdap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID
43 dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
44
45 if {[using_jtag]} {
46         jtag newtap $_CHIPNAME bs -irlen 5
47 }
48
49 set _TARGETNAME $_CHIPNAME.cpu
50 target create $_TARGETNAME cortex_m -endian $_ENDIAN -dap $_CHIPNAME.dap
51
52 # use non-secure RAM by default
53 $_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
54
55 # declare non-secure flash
56 flash bank $_CHIPNAME.flash_ns stm32l4x 0 0 0 0 $_TARGETNAME
57
58 # Common knowledges tells JTAG speed should be <= F_CPU/6.
59 # F_CPU after reset is MSI 4MHz, so use F_JTAG = 500 kHz to stay on
60 # the safe side.
61 #
62 # Note that there is a pretty wide band where things are
63 # more or less stable, see http://openocd.zylin.com/#/c/3366/
64 adapter speed 500
65
66 adapter srst delay 100
67 if {[using_jtag]} {
68         jtag_ntrst_delay 100
69 }
70
71 reset_config srst_nogate
72
73 if {![using_hla]} {
74         # if srst is not fitted use SYSRESETREQ to
75         # perform a soft reset
76         cortex_m reset_config sysresetreq
77 }
78
79 proc clock_config_110_mhz {} {
80         # MCU clock is MSI (4MHz) after reset, set MCU freq at 110 MHz with PLL
81         # RCC_APB1ENR1 = PWREN
82         mww 0x40021058 0x10000000
83         # delay for register clock enable (read back reg)
84         mrw 0x40021058
85         # PWR_CR1 : VOS Range 0
86         mww 0x40007000 0
87         # while (PWR_SR2 & VOSF)
88         while {([mrw 0x40007014] & 0x0400)} {}
89         # FLASH_ACR : 5 WS for 110 MHz HCLK
90         mww 0x40022000 0x00000005
91         # RCC_PLLCFGR = PLLP=PLLQ=0, PLLR=00=2, PLLREN=1, PLLN=55, PLLM=0000=1, PLLSRC=MSI 4MHz
92         # fVCO = 4 x 55 /1 = 220
93         # SYSCLOCK = fVCO/PLLR = 220/2 = 110 MHz
94         mww 0x4002100C 0x01003711
95         # RCC_CR |= PLLON
96         mmw 0x40021000 0x01000000 0
97         # while !(RCC_CR & PLLRDY)
98         while {!([mrw 0x40021000] & 0x02000000)} {}
99         # RCC_CFGR |= SW_PLL
100         mmw 0x40021008 0x00000003 0
101         # while ((RCC_CFGR & SWS) != PLL)
102         while {([mrw 0x40021008] & 0x0C) != 0x0C} {}
103 }
104
105 $_TARGETNAME configure -event reset-init {
106         clock_config_110_mhz
107         # Boost JTAG frequency
108         adapter speed 4000
109 }
110
111 $_TARGETNAME configure -event reset-start {
112         # Reset clock is MSI (4 MHz)
113         adapter speed 480
114 }
115
116 $_TARGETNAME configure -event examine-end {
117         # DBGMCU_CR |= DBG_STANDBY | DBG_STOP
118         mmw 0xE0044004 0x00000006 0
119
120         # Stop watchdog counters during halt
121         # DBGMCU_APB1_FZ |= DBG_IWDG_STOP | DBG_WWDG_STOP
122         mmw 0xE0044008 0x00001800 0
123 }
124
125 $_TARGETNAME configure -event trace-config {
126         # Set TRACE_IOEN; TRACE_MODE is set to async; when using sync
127         # change this value accordingly to configure trace pins
128         # assignment
129         mmw 0xE0044004 0x00000020 0
130 }