1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * ESP32-S3 target API for OpenOCD *
5 * Copyright (C) 2020 Espressif Systems Ltd. *
6 ***************************************************************************/
12 #include <helper/time_support.h>
13 #include <target/target.h>
14 #include <target/target_type.h>
15 #include <target/smp.h>
17 #include "esp_xtensa_smp.h"
20 This is a JTAG driver for the ESP32_S3, the are two Tensilica cores inside
21 the ESP32_S3 chip. For more information please have a look into ESP32_S3 target
25 /* ESP32_S3 memory map */
26 #define ESP32_S3_IRAM_LOW 0x40370000
27 #define ESP32_S3_IRAM_HIGH 0x403E0000
28 #define ESP32_S3_IROM_MASK_LOW 0x40000000
29 #define ESP32_S3_IROM_MASK_HIGH 0x40060000
30 #define ESP32_S3_DRAM_LOW 0x3FC88000
31 #define ESP32_S3_DRAM_HIGH 0x3FD00000
32 #define ESP32_S3_RTC_IRAM_LOW 0x600FE000
33 #define ESP32_S3_RTC_IRAM_HIGH 0x60100000
34 #define ESP32_S3_RTC_DRAM_LOW 0x600FE000
35 #define ESP32_S3_RTC_DRAM_HIGH 0x60100000
36 #define ESP32_S3_RTC_DATA_LOW 0x50000000
37 #define ESP32_S3_RTC_DATA_HIGH 0x50002000
38 #define ESP32_S3_EXTRAM_DATA_LOW 0x3D000000
39 #define ESP32_S3_EXTRAM_DATA_HIGH 0x3E000000
40 #define ESP32_S3_SYS_RAM_LOW 0x60000000UL
41 #define ESP32_S3_SYS_RAM_HIGH (ESP32_S3_SYS_RAM_LOW + 0x10000000UL)
42 #define ESP32_S3_RTC_SLOW_MEM_BASE ESP32_S3_RTC_DATA_LOW
45 #define ESP32_S3_WDT_WKEY_VALUE 0x50D83AA1
46 #define ESP32_S3_TIMG0_BASE 0x6001F000
47 #define ESP32_S3_TIMG1_BASE 0x60020000
48 #define ESP32_S3_TIMGWDT_CFG0_OFF 0x48
49 #define ESP32_S3_TIMGWDT_PROTECT_OFF 0x64
50 #define ESP32_S3_TIMG0WDT_CFG0 (ESP32_S3_TIMG0_BASE + ESP32_S3_TIMGWDT_CFG0_OFF)
51 #define ESP32_S3_TIMG1WDT_CFG0 (ESP32_S3_TIMG1_BASE + ESP32_S3_TIMGWDT_CFG0_OFF)
52 #define ESP32_S3_TIMG0WDT_PROTECT (ESP32_S3_TIMG0_BASE + ESP32_S3_TIMGWDT_PROTECT_OFF)
53 #define ESP32_S3_TIMG1WDT_PROTECT (ESP32_S3_TIMG1_BASE + ESP32_S3_TIMGWDT_PROTECT_OFF)
54 #define ESP32_S3_RTCCNTL_BASE 0x60008000
55 #define ESP32_S3_RTCWDT_CFG_OFF 0x98
56 #define ESP32_S3_RTCWDT_PROTECT_OFF 0xB0
57 #define ESP32_S3_SWD_CONF_OFF 0xB0
58 #define ESP32_S3_SWD_WPROTECT_OFF 0xB4
59 #define ESP32_S3_RTCWDT_CFG (ESP32_S3_RTCCNTL_BASE + ESP32_S3_RTCWDT_CFG_OFF)
60 #define ESP32_S3_RTCWDT_PROTECT (ESP32_S3_RTCCNTL_BASE + ESP32_S3_RTCWDT_PROTECT_OFF)
61 #define ESP32_S3_SWD_CONF_REG (ESP32_S3_RTCCNTL_BASE + ESP32_S3_SWD_CONF_OFF)
62 #define ESP32_S3_SWD_WPROTECT_REG (ESP32_S3_RTCCNTL_BASE + ESP32_S3_SWD_WPROTECT_OFF)
63 #define ESP32_S3_SWD_AUTO_FEED_EN_M BIT(31)
64 #define ESP32_S3_SWD_WKEY_VALUE 0x8F1D312AU
66 #define ESP32_S3_TRACEMEM_BLOCK_SZ 0x4000
68 /* ESP32_S3 dport regs */
69 #define ESP32_S3_DR_REG_SYSTEM_BASE 0x600c0000
70 #define ESP32_S3_SYSTEM_CORE_1_CONTROL_0_REG (ESP32_S3_DR_REG_SYSTEM_BASE + 0x014)
71 #define ESP32_S3_SYSTEM_CONTROL_CORE_1_CLKGATE_EN BIT(1)
73 /* ESP32_S3 RTC regs */
74 #define ESP32_S3_RTC_CNTL_SW_CPU_STALL_REG (ESP32_S3_RTCCNTL_BASE + 0xBC)
75 #define ESP32_S3_RTC_CNTL_SW_CPU_STALL_DEF 0x0
77 struct esp32s3_common {
78 struct esp_xtensa_smp_common esp_xtensa_smp;
81 /* Reset ESP32-S3's peripherals.
82 * 1. OpenOCD makes sure the target is halted; if not, tries to halt it.
83 * If that fails, tries to reset it (via OCD) and then halt.
84 * 2. OpenOCD loads the stub code into RTC_SLOW_MEM.
85 * 3. Executes the stub code from address 0x50000004.
86 * 4. The stub code changes the reset vector to 0x50000000, and triggers
87 * a system reset using RTC_CNTL_SW_SYS_RST bit.
88 * 5. Once the PRO CPU is out of reset, it executes the stub code from address 0x50000000.
89 * The stub code disables the watchdog, re-enables JTAG and the APP CPU,
90 * restores the reset vector, and enters an infinite loop.
91 * 6. OpenOCD waits until it can talk to the OCD module again, then halts the target.
92 * 7. OpenOCD restores the contents of RTC_SLOW_MEM.
94 * End result: all the peripherals except RTC_CNTL are reset, CPU's PC is undefined,
95 * PRO CPU is halted, APP CPU is in reset.
98 const uint8_t esp32s3_reset_stub_code[] = {
99 #include "../../../contrib/loaders/reset/espressif/esp32s3/cpu_reset_handler_code.inc"
102 static int esp32s3_soc_reset(struct target *target)
105 struct target_list *head;
106 struct xtensa *xtensa;
109 /* In order to write to peripheral registers, target must be halted first */
110 if (target->state != TARGET_HALTED) {
111 LOG_DEBUG("Target not halted before SoC reset, trying to halt it first");
113 res = target_wait_state(target, TARGET_HALTED, 1000);
114 if (res != ERROR_OK) {
115 LOG_DEBUG("Couldn't halt target before SoC reset, trying to do reset-halt");
116 res = xtensa_assert_reset(target);
117 if (res != ERROR_OK) {
119 "Couldn't halt target before SoC reset! (xtensa_assert_reset returned %d)",
125 bool reset_halt_save = target->reset_halt;
126 target->reset_halt = true;
127 res = xtensa_deassert_reset(target);
128 target->reset_halt = reset_halt_save;
129 if (res != ERROR_OK) {
131 "Couldn't halt target before SoC reset! (xtensa_deassert_reset returned %d)",
138 res = target_wait_state(target, TARGET_HALTED, 1000);
139 if (res != ERROR_OK) {
140 LOG_ERROR("Couldn't halt target before SoC reset");
147 foreach_smp_target(head, target->smp_targets) {
148 xtensa = target_to_xtensa(head->target);
149 /* if any of the cores is stalled unstall them */
150 if (xtensa_dm_core_is_stalled(&xtensa->dbg_mod)) {
151 LOG_TARGET_DEBUG(head->target, "Unstall CPUs before SW reset!");
152 res = target_write_u32(target,
153 ESP32_S3_RTC_CNTL_SW_CPU_STALL_REG,
154 ESP32_S3_RTC_CNTL_SW_CPU_STALL_DEF);
155 if (res != ERROR_OK) {
156 LOG_TARGET_ERROR(head->target, "Failed to unstall CPUs before SW reset!");
159 break; /* both cores are unstalled now, so exit the loop */
164 LOG_DEBUG("Loading stub code into RTC RAM");
165 uint8_t slow_mem_save[sizeof(esp32s3_reset_stub_code)];
167 /* Save contents of RTC_SLOW_MEM which we are about to overwrite */
168 res = target_read_buffer(target, ESP32_S3_RTC_SLOW_MEM_BASE, sizeof(slow_mem_save), slow_mem_save);
169 if (res != ERROR_OK) {
170 LOG_ERROR("Failed to save contents of RTC_SLOW_MEM (%d)!", res);
174 /* Write stub code into RTC_SLOW_MEM */
175 res = target_write_buffer(target,
176 ESP32_S3_RTC_SLOW_MEM_BASE,
177 sizeof(esp32s3_reset_stub_code),
178 esp32s3_reset_stub_code);
179 if (res != ERROR_OK) {
180 LOG_ERROR("Failed to write stub (%d)!", res);
184 LOG_DEBUG("Resuming the target");
185 xtensa = target_to_xtensa(target);
186 xtensa->suppress_dsr_errors = true;
187 res = xtensa_resume(target, 0, ESP32_S3_RTC_SLOW_MEM_BASE + 4, 0, 0);
188 xtensa->suppress_dsr_errors = false;
189 if (res != ERROR_OK) {
190 LOG_ERROR("Failed to run stub (%d)!", res);
193 LOG_DEBUG("resume done, waiting for the target to come alive");
195 /* Wait for SoC to reset */
197 int64_t timeout = timeval_ms() + 100;
198 bool get_timeout = false;
199 while (target->state != TARGET_RESET && target->state != TARGET_RUNNING) {
202 if (timeval_ms() >= timeout) {
203 LOG_TARGET_ERROR(target,
204 "Timed out waiting for CPU to be reset, target state=%d",
211 /* Halt the CPU again */
212 LOG_DEBUG("halting the target");
214 res = target_wait_state(target, TARGET_HALTED, 1000);
215 if (res == ERROR_OK) {
216 LOG_DEBUG("restoring RTC_SLOW_MEM");
217 res = target_write_buffer(target, ESP32_S3_RTC_SLOW_MEM_BASE, sizeof(slow_mem_save), slow_mem_save);
219 LOG_TARGET_ERROR(target, "Failed to restore contents of RTC_SLOW_MEM (%d)!", res);
221 LOG_TARGET_ERROR(target, "Timed out waiting for CPU to be halted after SoC reset");
224 return get_timeout ? ERROR_TARGET_TIMEOUT : res;
227 static int esp32s3_disable_wdts(struct target *target)
230 int res = target_write_u32(target, ESP32_S3_TIMG0WDT_PROTECT, ESP32_S3_WDT_WKEY_VALUE);
231 if (res != ERROR_OK) {
232 LOG_ERROR("Failed to write ESP32_S3_TIMG0WDT_PROTECT (%d)!", res);
235 res = target_write_u32(target, ESP32_S3_TIMG0WDT_CFG0, 0);
236 if (res != ERROR_OK) {
237 LOG_ERROR("Failed to write ESP32_S3_TIMG0WDT_CFG0 (%d)!", res);
241 res = target_write_u32(target, ESP32_S3_TIMG1WDT_PROTECT, ESP32_S3_WDT_WKEY_VALUE);
242 if (res != ERROR_OK) {
243 LOG_ERROR("Failed to write ESP32_S3_TIMG1WDT_PROTECT (%d)!", res);
246 res = target_write_u32(target, ESP32_S3_TIMG1WDT_CFG0, 0);
247 if (res != ERROR_OK) {
248 LOG_ERROR("Failed to write ESP32_S3_TIMG1WDT_CFG0 (%d)!", res);
252 res = target_write_u32(target, ESP32_S3_RTCWDT_PROTECT, ESP32_S3_WDT_WKEY_VALUE);
253 if (res != ERROR_OK) {
254 LOG_ERROR("Failed to write ESP32_S3_RTCWDT_PROTECT (%d)!", res);
257 res = target_write_u32(target, ESP32_S3_RTCWDT_CFG, 0);
258 if (res != ERROR_OK) {
259 LOG_ERROR("Failed to write ESP32_S3_RTCWDT_CFG (%d)!", res);
262 /* Enable SWD auto-feed */
263 res = target_write_u32(target, ESP32_S3_SWD_WPROTECT_REG, ESP32_S3_SWD_WKEY_VALUE);
264 if (res != ERROR_OK) {
265 LOG_ERROR("Failed to write ESP32_S3_SWD_WPROTECT_REG (%d)!", res);
268 uint32_t swd_conf_reg = 0;
269 res = target_read_u32(target, ESP32_S3_SWD_CONF_REG, &swd_conf_reg);
270 if (res != ERROR_OK) {
271 LOG_ERROR("Failed to read ESP32_S3_SWD_CONF_REG (%d)!", res);
274 swd_conf_reg |= ESP32_S3_SWD_AUTO_FEED_EN_M;
275 res = target_write_u32(target, ESP32_S3_SWD_CONF_REG, swd_conf_reg);
276 if (res != ERROR_OK) {
277 LOG_ERROR("Failed to write ESP32_S3_SWD_CONF_REG (%d)!", res);
283 static int esp32s3_on_halt(struct target *target)
285 return esp32s3_disable_wdts(target);
288 static int esp32s3_arch_state(struct target *target)
293 static int esp32s3_virt2phys(struct target *target,
294 target_addr_t virtual, target_addr_t *physical)
303 static int esp32s3_target_init(struct command_context *cmd_ctx, struct target *target)
305 return esp_xtensa_target_init(cmd_ctx, target);
308 static const struct xtensa_debug_ops esp32s3_dbg_ops = {
309 .queue_enable = xtensa_dm_queue_enable,
310 .queue_reg_read = xtensa_dm_queue_reg_read,
311 .queue_reg_write = xtensa_dm_queue_reg_write
314 static const struct xtensa_power_ops esp32s3_pwr_ops = {
315 .queue_reg_read = xtensa_dm_queue_pwr_reg_read,
316 .queue_reg_write = xtensa_dm_queue_pwr_reg_write
319 static const struct esp_xtensa_smp_chip_ops esp32s3_chip_ops = {
320 .reset = esp32s3_soc_reset,
321 .on_halt = esp32s3_on_halt
324 static int esp32s3_target_create(struct target *target, Jim_Interp *interp)
326 struct xtensa_debug_module_config esp32s3_dm_cfg = {
327 .dbg_ops = &esp32s3_dbg_ops,
328 .pwr_ops = &esp32s3_pwr_ops,
330 .queue_tdi_idle = NULL,
331 .queue_tdi_idle_arg = NULL
334 struct esp32s3_common *esp32s3 = calloc(1, sizeof(struct esp32s3_common));
336 LOG_ERROR("Failed to alloc memory for arch info!");
340 int ret = esp_xtensa_smp_init_arch_info(target,
341 &esp32s3->esp_xtensa_smp,
344 if (ret != ERROR_OK) {
345 LOG_ERROR("Failed to init arch info!");
350 /* Assume running target. If different, the first poll will fix this. */
351 target->state = TARGET_RUNNING;
352 target->debug_reason = DBG_REASON_NOTHALTED;
356 static const struct command_registration esp32s3_command_handlers[] = {
359 .chain = esp_xtensa_smp_command_handlers,
364 .chain = smp_command_handlers,
366 COMMAND_REGISTRATION_DONE
369 /** Holds methods for Xtensa targets. */
370 struct target_type esp32s3_target = {
373 .poll = esp_xtensa_smp_poll,
374 .arch_state = esp32s3_arch_state,
377 .resume = esp_xtensa_smp_resume,
378 .step = esp_xtensa_smp_step,
380 .assert_reset = esp_xtensa_smp_assert_reset,
381 .deassert_reset = esp_xtensa_smp_deassert_reset,
382 .soft_reset_halt = esp_xtensa_smp_soft_reset_halt,
384 .virt2phys = esp32s3_virt2phys,
385 .mmu = xtensa_mmu_is_enabled,
386 .read_memory = xtensa_read_memory,
387 .write_memory = xtensa_write_memory,
389 .read_buffer = xtensa_read_buffer,
390 .write_buffer = xtensa_write_buffer,
392 .checksum_memory = xtensa_checksum_memory,
394 .get_gdb_arch = xtensa_get_gdb_arch,
395 .get_gdb_reg_list = xtensa_get_gdb_reg_list,
397 .add_breakpoint = esp_xtensa_breakpoint_add,
398 .remove_breakpoint = esp_xtensa_breakpoint_remove,
400 .add_watchpoint = esp_xtensa_smp_watchpoint_add,
401 .remove_watchpoint = esp_xtensa_smp_watchpoint_remove,
403 .target_create = esp32s3_target_create,
404 .init_target = esp32s3_target_init,
405 .examine = xtensa_examine,
406 .deinit_target = esp_xtensa_target_deinit,
408 .commands = esp32s3_command_handlers,