1 /***************************************************************************
2 * Reset stub used by esp32 target *
3 * Copyright (C) 2017 Espressif Systems Ltd. *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
19 #define RTC_CNTL_RESET_STATE_REG 0x3ff48034
20 #define RTC_CNTL_RESET_STATE_DEF 0x3000
21 #define RTC_CNTL_CLK_CONF_REG 0x3ff48070
22 #define RTC_CNTL_CLK_CONF_DEF 0x2210
23 #define RTC_CNTL_STORE4_REG 0x3ff480b0
24 #define RTC_CNTL_STORE5_REG 0x3ff480b4
25 #define WDT_WKEY_VALUE 0x50D83AA1
26 #define TIMG0_WDTWPROTECT_REG 0x3ff5f064
27 #define TIMG0_WDTCONFIG0_REG 0x3ff5f048
28 #define TIMG1_WDTWPROTECT_REG 0x3FF60064
29 #define TIMG1_WDTCONFIG0_REG 0x3ff60048
30 #define RTC_CNTL_WDTCONFIG0_REG 0x3ff4808c
31 #define RTC_CNTL_WDTWPROTECT_REG 0x3ff480a4
32 #define JTAG_ENABLE_REG 0x3ff5a1fc
33 #define RTC_CNTL_OPTIONS0_REG 0x3ff48000
34 #define RTC_CNTL_OPTIONS0_DEF 0x1c492000
35 #define RTC_CNTL_SW_SYS_RST 0x80000000
36 #define DPORT_APPCPU_CTRL_A_REG 0x3ff0002c
37 #define DPORT_APPCPU_RST_EN 0x1
38 #define DPORT_APPCPU_CTRL_B_REG 0x3ff00030
39 #define DPORT_APPCPU_CLKGATE_EN 0x1
40 #define DPORT_APPCPU_CTRL_C_REG 0x3ff00034
41 #define DPORT_APPCPU_CTRL_D_REG 0x3ff00038
44 /* This stub is copied to RTC_SLOW_MEM by OpenOCD, and the CPU starts executing
45 * it instead of the ROM code (0x40000400). This stub disables watchdogs and
47 * OpenOCD will then halt the target and perform CPU reset using OCD.
51 /* Has to be at offset 0. This is the entry point of the CPU, once
52 * RTC_CNTL_PROCPU_STAT_VECTOR_SEL is cleared.
53 * CPU will come here after the system reset, triggered by RTC_CNTL_SW_SYS_RST.
55 .global cpu_at_start_handler
56 .type cpu_at_start_handler,@function
62 /* Has to be at offset 4. Once the stub code has been uploaded into RTC Slow
63 * memory, OpenOCD will set the PC to this address, and resume execution.
64 * The stub will then jump to 'reset' label and perform the reset.
66 .global cpu_reset_handler
67 .type cpu_reset_handler,@function
77 /* Use a5 as a zero register */
79 /* Select static reset vector 0 (XCHAL_RESET_VECTOR0_VADDR, 0x50000000) */
80 movi a4, RTC_CNTL_RESET_STATE_REG
82 /* Set some clock-related RTC registers to the default values */
83 movi a4, RTC_CNTL_STORE4_REG
85 movi a4, RTC_CNTL_STORE5_REG
87 movi a4, RTC_CNTL_CLK_CONF_REG
88 movi a3, RTC_CNTL_CLK_CONF_DEF
90 /* Reset the digital part of the chip (RTC controller doesn't get reset) */
91 movi a3, (RTC_CNTL_OPTIONS0_DEF | RTC_CNTL_SW_SYS_RST)
92 movi a4, RTC_CNTL_OPTIONS0_REG
94 /* Doesn't reach beyond this instruction */
98 /* If running on the APP CPU, skip directly to the parking loop */
101 bnez a6, parking_loop
103 /* Use a5 as a zero register */
105 /* Disable the watchdogs */
106 movi a3, WDT_WKEY_VALUE
107 movi a4, RTC_CNTL_WDTWPROTECT_REG
109 movi a4, TIMG0_WDTWPROTECT_REG
111 movi a4, TIMG1_WDTWPROTECT_REG
113 movi a4, RTC_CNTL_WDTCONFIG0_REG
115 movi a4, TIMG0_WDTCONFIG0_REG
117 movi a4, TIMG1_WDTCONFIG0_REG
119 /* Enable JTAG (needed since rev. 3) */
120 movi a4, JTAG_ENABLE_REG
122 /* Clear APP_CPU boot address */
123 movi a4, DPORT_APPCPU_CTRL_D_REG
125 /* Clear APP_CPU clock gating */
126 movi a4, DPORT_APPCPU_CTRL_B_REG
127 movi a3, DPORT_APPCPU_CLKGATE_EN
129 /* Set and clear APP_CPU reset */
130 movi a4, DPORT_APPCPU_CTRL_A_REG
131 movi a3, DPORT_APPCPU_RST_EN
134 /* Restore the reset vector to ROM */
135 movi a4, RTC_CNTL_RESET_STATE_REG
136 movi a3, RTC_CNTL_RESET_STATE_DEF
141 /* PRO and APP CPU will be in this loop, until OpenOCD
142 * finds the JTAG taps and puts the CPUs into debug mode.