763a7857a08cb6cffc104658d93805a66b7dc9f7
[fw/openocd] / tcl / target / stm32h7x.cfg
1 # script for stm32h7x family
2
3 #
4 # stm32h7 devices support both JTAG and SWD transports.
5 #
6 source [find target/swj-dp.tcl]
7 source [find mem_helper.tcl]
8
9 if { [info exists CHIPNAME] } {
10    set _CHIPNAME $CHIPNAME
11 } else {
12    set _CHIPNAME stm32h7x
13 }
14
15 if { [info exists DUAL_BANK] } {
16         set $_CHIPNAME.DUAL_BANK $DUAL_BANK
17         unset DUAL_BANK
18 } else {
19         set $_CHIPNAME.DUAL_BANK 0
20 }
21
22 if { [info exists DUAL_CORE] } {
23         set $_CHIPNAME.DUAL_CORE $DUAL_CORE
24         unset DUAL_CORE
25 } else {
26         set $_CHIPNAME.DUAL_CORE 0
27 }
28
29 # Issue a warning when hla is used, and fallback to single core configuration
30 if { [set $_CHIPNAME.DUAL_CORE] && [using_hla] } {
31         echo "Warning : hla does not support multicore debugging"
32         set $_CHIPNAME.DUAL_CORE 0
33 }
34
35 if { [info exists USE_CTI] } {
36         set $_CHIPNAME.USE_CTI $USE_CTI
37         unset USE_CTI
38 } else {
39         set $_CHIPNAME.USE_CTI 0
40 }
41
42 # Issue a warning when DUAL_CORE=0 and USE_CTI=1, and fallback to USE_CTI=0
43 if { ![set $_CHIPNAME.DUAL_CORE] && [set $_CHIPNAME.USE_CTI] } {
44         echo "Warning : could not use CTI with a single core device, CTI is disabled"
45         set $_CHIPNAME.USE_CTI 0
46 }
47
48 set _ENDIAN little
49
50 # Work-area is a space in RAM used for flash programming
51 # By default use 64kB
52 if { [info exists WORKAREASIZE] } {
53    set _WORKAREASIZE $WORKAREASIZE
54 } else {
55    set _WORKAREASIZE 0x10000
56 }
57
58 #jtag scan chain
59 if { [info exists CPUTAPID] } {
60    set _CPUTAPID $CPUTAPID
61 } else {
62    if { [using_jtag] } {
63           set _CPUTAPID 0x6ba00477
64    } {
65       set _CPUTAPID 0x6ba02477
66    }
67 }
68
69 swj_newdap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID
70 dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
71
72 if {[using_jtag]} {
73    jtag newtap $_CHIPNAME bs -irlen 5
74 }
75
76 if {![using_hla]} {
77         # STM32H7 provides an APB-AP at access port 2, which allows the access to
78         # the debug and trace features on the system APB System Debug Bus (APB-D).
79         target create $_CHIPNAME.ap2 mem_ap -dap $_CHIPNAME.dap -ap-num 2
80 }
81
82 target create $_CHIPNAME.cpu0 cortex_m -endian $_ENDIAN -dap $_CHIPNAME.dap -ap-num 0
83
84 $_CHIPNAME.cpu0 configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
85
86 flash bank $_CHIPNAME.bank1.cpu0 stm32h7x 0x08000000 0 0 0 $_CHIPNAME.cpu0
87
88 if {[set $_CHIPNAME.DUAL_BANK]} {
89         flash bank $_CHIPNAME.bank2.cpu0 stm32h7x 0x08100000 0 0 0 $_CHIPNAME.cpu0
90 }
91
92 if {[set $_CHIPNAME.DUAL_CORE]} {
93         target create $_CHIPNAME.cpu1 cortex_m -endian $_ENDIAN -dap $_CHIPNAME.dap -ap-num 3
94
95         $_CHIPNAME.cpu1 configure -work-area-phys 0x38000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
96
97         flash bank $_CHIPNAME.bank1.cpu1 stm32h7x 0x08000000 0 0 0 $_CHIPNAME.cpu1
98
99         if {[set $_CHIPNAME.DUAL_BANK]} {
100                 flash bank $_CHIPNAME.bank2.cpu1 stm32h7x 0x08100000 0 0 0 $_CHIPNAME.cpu1
101         }
102 }
103
104 # Make sure that cpu0 is selected
105 targets $_CHIPNAME.cpu0
106
107 # Clock after reset is HSI at 64 MHz, no need of PLL
108 adapter speed 1800
109
110 adapter srst delay 100
111 if {[using_jtag]} {
112  jtag_ntrst_delay 100
113 }
114
115 # use hardware reset
116 #
117 # The STM32H7 does not support connect_assert_srst mode because the AXI is
118 # unavailable while SRST is asserted, and that is used to access the DBGMCU
119 # component at 0x5C001000 in the examine-end event handler.
120 #
121 # It is possible to access the DBGMCU component at 0xE00E1000 via AP2 instead
122 # of the default AP0, and that works with SRST asserted; however, nonzero AP
123 # usage does not work with HLA, so is not done by default. That change could be
124 # made in a local configuration file if connect_assert_srst mode is needed for
125 # a specific application and a non-HLA adapter is in use.
126 reset_config srst_only srst_nogate
127
128 if {![using_hla]} {
129    # if srst is not fitted use SYSRESETREQ to
130    # perform a soft reset
131         $_CHIPNAME.cpu0 cortex_m reset_config sysresetreq
132
133         if {[set $_CHIPNAME.DUAL_CORE]} {
134                 $_CHIPNAME.cpu1 cortex_m reset_config sysresetreq
135         }
136
137    # Set CSW[27], which according to ARM ADI v5 appendix E1.4 maps to AHB signal
138    # HPROT[3], which according to AMBA AHB/ASB/APB specification chapter 3.7.3
139    # makes the data access cacheable. This allows reading and writing data in the
140    # CPU cache from the debugger, which is far more useful than going straight to
141    # RAM when operating on typical variables, and is generally no worse when
142    # operating on special memory locations.
143    $_CHIPNAME.dap apcsw 0x08000000 0x08000000
144 }
145
146 $_CHIPNAME.cpu0 configure -event examine-end {
147         # Enable D3 and D1 DBG clocks
148         # DBGMCU_CR |= D3DBGCKEN | D1DBGCKEN
149         stm32h7x_dbgmcu_mmw 0x004 0x00600000 0
150
151         # Enable debug during low power modes (uses more power)
152         # DBGMCU_CR |= DBG_STANDBY | DBG_STOP | DBG_SLEEP D1 Domain
153         stm32h7x_dbgmcu_mmw 0x004 0x00000007 0
154         # DBGMCU_CR |= DBG_STANDBY | DBG_STOP | DBG_SLEEP D2 Domain
155         stm32h7x_dbgmcu_mmw 0x004 0x00000038 0
156
157         # Stop watchdog counters during halt
158         # DBGMCU_APB3FZ1 |= WWDG1
159         stm32h7x_dbgmcu_mmw 0x034 0x00000040 0
160         # DBGMCU_APB1LFZ1 |= WWDG2
161         stm32h7x_dbgmcu_mmw 0x03C 0x00000800 0
162         # DBGMCU_APB4FZ1 |= WDGLSD1 | WDGLSD2
163         stm32h7x_dbgmcu_mmw 0x054 0x000C0000 0
164 }
165
166 $_CHIPNAME.cpu0 configure -event trace-config {
167         # Set TRACECLKEN; TRACE_MODE is set to async; when using sync
168         # change this value accordingly to configure trace pins
169         # assignment
170         stm32h7x_dbgmcu_mmw 0x004 0x00100000 0
171 }
172
173 $_CHIPNAME.cpu0 configure -event reset-init {
174         # Clock after reset is HSI at 64 MHz, no need of PLL
175         adapter speed 4000
176 }
177
178 # get _CHIPNAME from current target
179 proc stm32h7x_get_chipname {} {
180         set t [target current]
181         set sep [string last "." $t]
182         if {$sep == -1} {
183                 return $t
184         }
185         return [string range $t 0 [expr $sep - 1]]
186 }
187
188 if {[set $_CHIPNAME.DUAL_CORE]} {
189         $_CHIPNAME.cpu1 configure -event examine-end {
190                 set _CHIPNAME [stm32h7x_get_chipname]
191                 global $_CHIPNAME.USE_CTI
192
193                 # Stop watchdog counters during halt
194                 # DBGMCU_APB3FZ2 |= WWDG1
195                 stm32h7x_dbgmcu_mmw 0x038 0x00000040 0
196                 # DBGMCU_APB1LFZ2 |= WWDG2
197                 stm32h7x_dbgmcu_mmw 0x040 0x00000800 0
198                 # DBGMCU_APB4FZ2 |= WDGLSD1 | WDGLSD2
199                 stm32h7x_dbgmcu_mmw 0x058 0x000C0000 0
200
201                 if {[set $_CHIPNAME.USE_CTI]} {
202                         stm32h7x_cti_start
203                 }
204         }
205 }
206
207 # like mrw, but with target selection
208 proc stm32h7x_mrw {used_target reg} {
209         set value ""
210         $used_target mem2array value 32 $reg 1
211         return $value(0)
212 }
213
214 # like mmw, but with target selection
215 proc stm32h7x_mmw {used_target reg setbits clearbits} {
216         set old [stm32h7x_mrw $used_target $reg]
217         set new [expr ($old & ~$clearbits) | $setbits]
218         $used_target mww $reg $new
219 }
220
221 # mmw for dbgmcu component registers, it accepts the register offset from dbgmcu base
222 # this procedure will use the mem_ap on AP2 whenever possible
223 proc stm32h7x_dbgmcu_mmw {reg_offset setbits clearbits} {
224         # use $_CHIPNAME.ap2 if possible, and use the proper dbgmcu base address
225         if {![using_hla]} {
226                 set _CHIPNAME [stm32h7x_get_chipname]
227                 set used_target $_CHIPNAME.ap2
228                 set reg_addr [expr 0xE00E1000 + $reg_offset]
229         } {
230                 set used_target [target current]
231                 set reg_addr [expr 0x5C001000 + $reg_offset]
232         }
233
234         stm32h7x_mmw $used_target $reg_addr $setbits $clearbits
235 }
236
237 if {[set $_CHIPNAME.USE_CTI]} {
238         # create CTI instances for both cores
239         cti create $_CHIPNAME.cti0 -dap $_CHIPNAME.dap -ap-num 0 -baseaddr 0xE0043000
240         cti create $_CHIPNAME.cti1 -dap $_CHIPNAME.dap -ap-num 3 -baseaddr 0xE0043000
241
242         $_CHIPNAME.cpu0 configure -event halted { stm32h7x_cti_prepare_restart_all }
243         $_CHIPNAME.cpu1 configure -event halted { stm32h7x_cti_prepare_restart_all }
244
245         $_CHIPNAME.cpu0 configure -event debug-halted { stm32h7x_cti_prepare_restart_all }
246         $_CHIPNAME.cpu1 configure -event debug-halted { stm32h7x_cti_prepare_restart_all }
247
248         proc stm32h7x_cti_start {} {
249                 set _CHIPNAME [stm32h7x_get_chipname]
250
251                 # Configure Cores' CTIs to halt each other
252                 # TRIGIN0 (DBGTRIGGER) and TRIGOUT0 (EDBGRQ) at CTM_CHANNEL_0
253                 $_CHIPNAME.cti0 write INEN0 0x1
254                 $_CHIPNAME.cti0 write OUTEN0 0x1
255                 $_CHIPNAME.cti1 write INEN0 0x1
256                 $_CHIPNAME.cti1 write OUTEN0 0x1
257
258                 # enable CTIs
259                 $_CHIPNAME.cti0 enable on
260                 $_CHIPNAME.cti1 enable on
261         }
262
263         proc stm32h7x_cti_stop {} {
264                 set _CHIPNAME [stm32h7x_get_chipname]
265
266                 $_CHIPNAME.cti0 enable off
267                 $_CHIPNAME.cti1 enable off
268         }
269
270         proc stm32h7x_cti_prepare_restart_all {} {
271                 stm32h7x_cti_prepare_restart cti0
272                 stm32h7x_cti_prepare_restart cti1
273         }
274
275         proc stm32h7x_cti_prepare_restart {cti} {
276                 set _CHIPNAME [stm32h7x_get_chipname]
277
278                 # Acknowlodge EDBGRQ at TRIGOUT0
279                 $_CHIPNAME.$cti write INACK 0x01
280                 $_CHIPNAME.$cti write INACK 0x00
281         }
282 }