1 /*
2  * SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "esp_attr.h"
8 #include "esp_private/pm_trace.h"
9 #include "driver/gpio.h"
10 #include "soc/soc.h"
11 #include "soc/gpio_reg.h"
12 
13 /* GPIOs to use for tracing of esp_pm events.
14  * Two entries in the array for each type, one for each CPU.
15  * Feel free to change when debugging.
16  */
17 static const int DRAM_ATTR s_trace_io[] = {
18 #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
19     BIT(4),  BIT(5),  // ESP_PM_TRACE_IDLE
20     BIT(16), BIT(17), // ESP_PM_TRACE_TICK
21     BIT(18), BIT(18), // ESP_PM_TRACE_FREQ_SWITCH
22     BIT(19), BIT(19), // ESP_PM_TRACE_CCOMPARE_UPDATE
23     BIT(25), BIT(26), // ESP_PM_TRACE_ISR_HOOK
24     BIT(27), BIT(27), // ESP_PM_TRACE_SLEEP
25 #elif CONFIG_IDF_TARGET_ESP32S3
26     BIT(4),  BIT(5),  // ESP_PM_TRACE_IDLE
27     BIT(6),  BIT(7),  // ESP_PM_TRACE_TICK
28     BIT(14), BIT(14), // ESP_PM_TRACE_FREQ_SWITCH
29     BIT(15), BIT(15), // ESP_PM_TRACE_CCOMPARE_UPDATE
30     BIT(16), BIT(17), // ESP_PM_TRACE_ISR_HOOK
31     BIT(18), BIT(18), // ESP_PM_TRACE_SLEEP
32 #elif CONFIG_IDF_TARGET_ESP32H2
33     BIT(2),  BIT(3),  // ESP_PM_TRACE_IDLE
34     BIT(4),  BIT(5),  // ESP_PM_TRACE_TICK
35     BIT(6),  BIT(6),  // ESP_PM_TRACE_FREQ_SWITCH
36     BIT(7),  BIT(7),  // ESP_PM_TRACE_CCOMPARE_UPDATE
37     BIT(8),  BIT(9),  // ESP_PM_TRACE_ISR_HOOK
38     BIT(10), BIT(10), // ESP_PM_TRACE_SLEEP
39 #else
40     BIT(2),  BIT(3),  // ESP_PM_TRACE_IDLE
41     BIT(4),  BIT(5),  // ESP_PM_TRACE_TICK
42     BIT(6),  BIT(6),  // ESP_PM_TRACE_FREQ_SWITCH
43     BIT(7),  BIT(7),  // ESP_PM_TRACE_CCOMPARE_UPDATE
44     BIT(8),  BIT(9),  // ESP_PM_TRACE_ISR_HOOK
45     BIT(18), BIT(18), // ESP_PM_TRACE_SLEEP
46 #endif
47 };
48 
esp_pm_trace_init(void)49 void esp_pm_trace_init(void)
50 {
51     for (size_t i = 0; i < sizeof(s_trace_io) / sizeof(s_trace_io[0]); ++i) {
52         int io = __builtin_ffs(s_trace_io[i]);
53         if (io == 0) {
54             continue;
55         }
56         gpio_set_direction(io - 1, GPIO_MODE_OUTPUT);
57     }
58 }
59 
esp_pm_trace_enter(esp_pm_trace_event_t event,int core_id)60 void IRAM_ATTR esp_pm_trace_enter(esp_pm_trace_event_t event, int core_id)
61 {
62     REG_WRITE(GPIO_OUT_W1TS_REG, s_trace_io[2 * event + core_id]);
63 }
64 
esp_pm_trace_exit(esp_pm_trace_event_t event,int core_id)65 void IRAM_ATTR esp_pm_trace_exit(esp_pm_trace_event_t event, int core_id)
66 {
67     REG_WRITE(GPIO_OUT_W1TC_REG, s_trace_io[2 * event + core_id]);
68 }
69