stm32x5x_common.cfg 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # SPDX-License-Identifier: GPL-2.0-or-later
  2. # common script for stm32l5x and stm32u5x families
  3. # Work-area is a space in RAM used for flash programming
  4. # By default use 64kB
  5. if { [info exists WORKAREASIZE] } {
  6. set _WORKAREASIZE $WORKAREASIZE
  7. } else {
  8. set _WORKAREASIZE 0x10000
  9. }
  10. #jtag scan chain
  11. if { [info exists CPUTAPID] } {
  12. set _CPUTAPID $CPUTAPID
  13. } else {
  14. if { [using_jtag] } {
  15. # STM32L5x: RM0438 Rev5, Section 52.2.8 JTAG debug port - Table 425. JTAG-DP data registers
  16. # STM32U5x: RM0456 Rev1, Section 65.2.8 JTAG debug port - Table 661. JTAG-DP data registers
  17. # Corresponds to Cortex®-M33 JTAG debug port ID code
  18. set _CPUTAPID 0x0ba04477
  19. } {
  20. # SWD IDCODE (single drop, arm)
  21. set _CPUTAPID 0x0be12477
  22. }
  23. }
  24. swj_newdap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID
  25. dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
  26. if {[using_jtag]} {
  27. jtag newtap $_CHIPNAME bs -irlen 5
  28. }
  29. set _TARGETNAME $_CHIPNAME.cpu
  30. target create $_TARGETNAME cortex_m -endian little -dap $_CHIPNAME.dap
  31. # use non-secure RAM by default
  32. $_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
  33. # create sec/ns flash and otp memories (sizes will be probed)
  34. flash bank $_CHIPNAME.flash_ns stm32l4x 0x08000000 0 0 0 $_TARGETNAME
  35. flash bank $_CHIPNAME.flash_alias_s stm32l4x 0x0C000000 0 0 0 $_TARGETNAME
  36. flash bank $_CHIPNAME.otp stm32l4x 0x0BFA0000 0 0 0 $_TARGETNAME
  37. # Common knowledge tells JTAG speed should be <= F_CPU/6.
  38. # F_CPU after reset is MSI 4MHz, so use F_JTAG = 500 kHz to stay on
  39. # the safe side.
  40. #
  41. # Note that there is a pretty wide band where things are
  42. # more or less stable, see http://review.openocd.org/3366
  43. adapter speed 500
  44. adapter srst delay 100
  45. if {[using_jtag]} {
  46. jtag_ntrst_delay 100
  47. }
  48. reset_config srst_nogate
  49. if {[using_hla]} {
  50. echo "Warn : The selected adapter does not support debugging this device in secure mode"
  51. } else {
  52. # if srst is not fitted use SYSRESETREQ to
  53. # perform a soft reset
  54. cortex_m reset_config sysresetreq
  55. }
  56. proc stm32x5x_is_secure {} {
  57. # read Debug Security Control and Status Register (DSCSR) and check CDS (bit 16)
  58. set DSCSR [mrw 0xE000EE08]
  59. return [expr {($DSCSR & (1 << 16)) != 0}]
  60. }
  61. proc stm32x5x_ahb_ap_non_secure_access {} {
  62. # in HLA mode, non-secure debugging is possible without changing the AP CSW
  63. if {![using_hla]} {
  64. # SPROT=1=Non Secure access, Priv=1
  65. [[target current] cget -dap] apcsw 0x4B000000 0x4F000000
  66. }
  67. }
  68. proc stm32x5x_ahb_ap_secure_access {} {
  69. if {![using_hla]} {
  70. # SPROT=0=Secure access, Priv=1
  71. [[target current] cget -dap] apcsw 0x0B000000 0x4F000000
  72. }
  73. }
  74. $_TARGETNAME configure -event reset-start {
  75. # Reset clock is MSI (4 MHz)
  76. adapter speed 480
  77. }
  78. $_TARGETNAME configure -event examine-end {
  79. # DBGMCU_CR |= DBG_STANDBY | DBG_STOP
  80. mmw 0xE0044004 0x00000006 0
  81. # Stop watchdog counters during halt
  82. # DBGMCU_APB1_FZ |= DBG_IWDG_STOP | DBG_WWDG_STOP
  83. mmw 0xE0044008 0x00001800 0
  84. }
  85. $_TARGETNAME configure -event halted {
  86. set secure [stm32x5x_is_secure]
  87. if {$secure} {
  88. set secure_str "Secure"
  89. stm32x5x_ahb_ap_secure_access
  90. } else {
  91. set secure_str "Non-Secure"
  92. stm32x5x_ahb_ap_non_secure_access
  93. }
  94. # print the secure state only when it changes
  95. set _TARGETNAME [target current]
  96. global $_TARGETNAME.secure
  97. if {![info exists $_TARGETNAME.secure] || $secure != [set $_TARGETNAME.secure]} {
  98. echo "CPU in $secure_str state"
  99. # update saved security state
  100. set $_TARGETNAME.secure $secure
  101. }
  102. }
  103. $_TARGETNAME configure -event gdb-flash-erase-start {
  104. set use_secure_workarea 0
  105. # check if FLASH_OPTR.TZEN is enabled
  106. set FLASH_OPTR [mrw 0x40022040]
  107. if {[expr {$FLASH_OPTR & 0x80000000}] == 0} {
  108. echo "TZEN option bit disabled"
  109. stm32x5x_ahb_ap_non_secure_access
  110. } else {
  111. stm32x5x_ahb_ap_secure_access
  112. echo "TZEN option bit enabled"
  113. # check if FLASH_OPTR.RDP is not Level 0.5
  114. if {[expr {$FLASH_OPTR & 0xFF}] != 0x55} {
  115. set use_secure_workarea 1
  116. }
  117. }
  118. set _TARGETNAME [target current]
  119. set workarea_addr [$_TARGETNAME cget -work-area-phys]
  120. echo "workarea_addr $workarea_addr"
  121. if {$use_secure_workarea} {
  122. set workarea_addr [expr {$workarea_addr | 0x10000000}]
  123. } else {
  124. set workarea_addr [expr {$workarea_addr & ~0x10000000}]
  125. }
  126. $_TARGETNAME configure -work-area-phys $workarea_addr
  127. }
  128. tpiu create $_CHIPNAME.tpiu -dap $_CHIPNAME.dap -ap-num 0 -baseaddr 0xE0040000
  129. lappend _telnet_autocomplete_skip _proc_pre_enable_$_CHIPNAME.tpiu
  130. proc _proc_pre_enable_$_CHIPNAME.tpiu {_targetname} {
  131. targets $_targetname
  132. # Set TRACE_EN and TRACE_IOEN in DBGMCU_CR
  133. # Leave TRACE_MODE untouched (defaults to async).
  134. # When using sync change this value accordingly to configure trace pins
  135. # assignment
  136. mmw 0xE0044004 0x00000030 0
  137. }
  138. $_CHIPNAME.tpiu configure -event pre-enable "_proc_pre_enable_$_CHIPNAME.tpiu $_TARGETNAME"