gd32vf103.cfg 3.8 KB

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