loaders/reset/espressif: replace the GPL-2.0-or-later license tag
[fw/openocd] / contrib / loaders / reset / espressif / esp32 / esp32_cpu_reset_handler.S
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /***************************************************************************
4  *   Reset stub used by esp32 target                                       *
5  *   Copyright (C) 2017 Espressif Systems Ltd.                             *
6  ***************************************************************************/
7
8 #define RTC_CNTL_RESET_STATE_REG    0x3ff48034
9 #define RTC_CNTL_RESET_STATE_DEF    0x3000
10 #define RTC_CNTL_CLK_CONF_REG       0x3ff48070
11 #define RTC_CNTL_CLK_CONF_DEF       0x2210
12 #define RTC_CNTL_STORE4_REG         0x3ff480b0
13 #define RTC_CNTL_STORE5_REG         0x3ff480b4
14 #define WDT_WKEY_VALUE              0x50D83AA1
15 #define TIMG0_WDTWPROTECT_REG       0x3ff5f064
16 #define TIMG0_WDTCONFIG0_REG        0x3ff5f048
17 #define TIMG1_WDTWPROTECT_REG       0x3FF60064
18 #define TIMG1_WDTCONFIG0_REG        0x3ff60048
19 #define RTC_CNTL_WDTCONFIG0_REG     0x3ff4808c
20 #define RTC_CNTL_WDTWPROTECT_REG    0x3ff480a4
21 #define JTAG_ENABLE_REG             0x3ff5a1fc
22 #define RTC_CNTL_OPTIONS0_REG       0x3ff48000
23 #define RTC_CNTL_OPTIONS0_DEF       0x1c492000
24 #define RTC_CNTL_SW_SYS_RST         0x80000000
25 #define DPORT_APPCPU_CTRL_A_REG     0x3ff0002c
26 #define DPORT_APPCPU_RST_EN         0x1
27 #define DPORT_APPCPU_CTRL_B_REG     0x3ff00030
28 #define DPORT_APPCPU_CLKGATE_EN     0x1
29 #define DPORT_APPCPU_CTRL_C_REG     0x3ff00034
30 #define DPORT_APPCPU_CTRL_D_REG     0x3ff00038
31
32
33 /* This stub is copied to RTC_SLOW_MEM by OpenOCD, and the CPU starts executing
34  * it instead of the ROM code (0x40000400). This stub disables watchdogs and
35  * goes into a loop.
36  * OpenOCD will then halt the target and perform CPU reset using OCD.
37  */
38
39
40 /* Has to be at offset 0. This is the entry point of the CPU, once
41  * RTC_CNTL_PROCPU_STAT_VECTOR_SEL is cleared.
42  * CPU will come here after the system reset, triggered by RTC_CNTL_SW_SYS_RST.
43  */
44     .global     cpu_at_start_handler
45     .type       cpu_at_start_handler,@function
46     .align      4
47 cpu_at_start_handler:
48     j start
49
50
51 /* Has to be at offset 4. Once the stub code has been uploaded into RTC Slow
52  * memory, OpenOCD will set the PC to this address, and resume execution.
53  * The stub will then jump to 'reset' label and perform the reset.
54  */
55     .global     cpu_reset_handler
56     .type       cpu_reset_handler,@function
57     .align      4
58 cpu_reset_handler:
59     j reset
60
61     .align 4
62     .literal_position
63
64     .align 4
65 reset:
66     /* Use a5 as a zero register */
67     xor a5, a5, a5
68     /* Select static reset vector 0 (XCHAL_RESET_VECTOR0_VADDR, 0x50000000) */
69     movi a4, RTC_CNTL_RESET_STATE_REG
70     s32i a5, a4, 0
71     /* Set some clock-related RTC registers to the default values */
72     movi a4, RTC_CNTL_STORE4_REG
73     s32i a5, a4, 0
74     movi a4, RTC_CNTL_STORE5_REG
75     s32i a5, a4, 0
76     movi a4, RTC_CNTL_CLK_CONF_REG
77     movi a3, RTC_CNTL_CLK_CONF_DEF
78     s32i a3, a4, 0
79     /* Reset the digital part of the chip (RTC controller doesn't get reset) */
80     movi a3, (RTC_CNTL_OPTIONS0_DEF | RTC_CNTL_SW_SYS_RST)
81     movi a4, RTC_CNTL_OPTIONS0_REG
82     s32i a3, a4, 0
83     /* Doesn't reach beyond this instruction */
84
85     .align 4
86 start:
87     /* If running on the APP CPU, skip directly to the parking loop */
88     rsr.prid a6
89     extui a6, a6, 1, 1
90     bnez a6, parking_loop
91
92     /* Use a5 as a zero register */
93     xor a5, a5, a5
94     /* Disable the watchdogs */
95     movi a3, WDT_WKEY_VALUE
96     movi a4, RTC_CNTL_WDTWPROTECT_REG
97     s32i.n a3, a4, 0
98     movi a4, TIMG0_WDTWPROTECT_REG
99     s32i.n a3, a4, 0
100     movi a4, TIMG1_WDTWPROTECT_REG
101     s32i.n a3, a4, 0
102     movi a4, RTC_CNTL_WDTCONFIG0_REG
103     s32i.n a5, a4, 0
104     movi a4, TIMG0_WDTCONFIG0_REG
105     s32i.n a5, a4, 0
106     movi a4, TIMG1_WDTCONFIG0_REG
107     s32i.n a5, a4, 0
108     /* Enable JTAG (needed since rev. 3) */
109     movi a4, JTAG_ENABLE_REG
110     s32i.n a5, a4, 0
111     /* Clear APP_CPU boot address */
112     movi a4, DPORT_APPCPU_CTRL_D_REG
113     s32i.n a5, a4, 0
114     /* Clear APP_CPU clock gating */
115     movi a4, DPORT_APPCPU_CTRL_B_REG
116     movi a3, DPORT_APPCPU_CLKGATE_EN
117     s32i.n a3, a4, 0
118     /* Set and clear APP_CPU reset */
119     movi a4, DPORT_APPCPU_CTRL_A_REG
120     movi a3, DPORT_APPCPU_RST_EN
121     s32i.n a3, a4, 0
122     s32i.n a5, a4, 0
123     /* Restore the reset vector to ROM */
124     movi a4, RTC_CNTL_RESET_STATE_REG
125     movi a3, RTC_CNTL_RESET_STATE_DEF
126     s32i.n a3, a4, 0
127
128
129 parking_loop:
130     /* PRO and APP CPU will be in this loop, until OpenOCD
131      * finds the JTAG taps and puts the CPUs into debug mode.
132      */
133     waiti 0
134     j parking_loop