rtc_cntl_hal.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. // The HAL layer for RTC CNTL (common part)
  7. #include "hal/rtc_hal.h"
  8. #include "soc/soc_caps.h"
  9. #include "esp32s3/rom/lldesc.h"
  10. #include "esp32s3/rom/cache.h"
  11. #include "hal/dma_types.h"
  12. #include "hal/assert.h"
  13. #include "esp_attr.h"
  14. #define RTC_CNTL_HAL_LINK_BUF_SIZE_MIN (SOC_RTC_CNTL_CPU_PD_DMA_BLOCK_SIZE) /* The minimum size of dma link buffer */
  15. typedef struct rtc_cntl_link_buf_conf {
  16. uint32_t cfg[4]; /* 4 word for dma link buffer configuration */
  17. } rtc_cntl_link_buf_conf_t;
  18. void * rtc_cntl_hal_dma_link_init(void *elem, void *buff, int size, void *next)
  19. {
  20. HAL_ASSERT(elem != NULL);
  21. HAL_ASSERT(buff != NULL);
  22. HAL_ASSERT(size >= RTC_CNTL_HAL_LINK_BUF_SIZE_MIN);
  23. lldesc_t *plink = (lldesc_t *)elem;
  24. plink->eof = next ? 0 : 1;
  25. plink->owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  26. plink->size = size >> 4; /* in unit of 16 bytes */
  27. plink->length = size >> 4;
  28. plink->buf = buff;
  29. plink->offset = 0;
  30. plink->sosf = 0;
  31. STAILQ_NEXT(plink, qe) = next;
  32. return (void *)plink;
  33. }
  34. #if SOC_PM_SUPPORT_CPU_PD
  35. #define DEFAULT_RETENTION_WAIT_CYCLES (0x7f)
  36. #define DEFAULT_RETENTION_CLKOFF_WAIT_CYCLES (0xf)
  37. #define DEFAULT_RETENTION_DONE_WAIT_CYCLES (0x7)
  38. void rtc_cntl_hal_enable_cpu_retention(void *addr)
  39. {
  40. rtc_cntl_sleep_retent_t *retent = (rtc_cntl_sleep_retent_t *)addr;
  41. if (addr) {
  42. if (retent->cpu_pd_mem) {
  43. lldesc_t *plink = (lldesc_t *)retent->cpu_pd_mem;
  44. /* dma link buffer configure */
  45. rtc_cntl_link_buf_conf_t *pbuf = (rtc_cntl_link_buf_conf_t *)plink->buf;
  46. pbuf->cfg[0] = 0;
  47. pbuf->cfg[1] = 0;
  48. pbuf->cfg[2] = 0;
  49. pbuf->cfg[3] = 0xfffe0000;
  50. rtc_cntl_ll_set_cpu_retention_link_addr((uint32_t)plink);
  51. rtc_cntl_ll_config_cpu_retention_timing(
  52. DEFAULT_RETENTION_WAIT_CYCLES,
  53. DEFAULT_RETENTION_CLKOFF_WAIT_CYCLES,
  54. DEFAULT_RETENTION_DONE_WAIT_CYCLES
  55. );
  56. rtc_cntl_ll_enable_cpu_retention_clock();
  57. rtc_cntl_ll_enable_cpu_retention();
  58. #if SOC_PM_SUPPORT_TAGMEM_PD
  59. if (!retent->tagmem.dcache.enable) {
  60. // Here we only need to care for the safety of the PSRAM data in the DCache.
  61. // Since only rodata, bss, heap data may be placed in PSRAM, and these data won't be
  62. // modified in the sleep process code after now, so it is safe to writeback here.
  63. Cache_WriteBack_All();
  64. }
  65. #endif
  66. }
  67. }
  68. }
  69. void IRAM_ATTR rtc_cntl_hal_disable_cpu_retention(void *addr)
  70. {
  71. rtc_cntl_sleep_retent_t *retent = (rtc_cntl_sleep_retent_t *)addr;
  72. if (addr) {
  73. if (retent->cpu_pd_mem) {
  74. /* I/d-cache tagmem retention has not been included or not
  75. * been enabled, after the system wakes up, all the contents
  76. * of i/d-cache need to be invalidated. */
  77. #if SOC_PM_SUPPORT_TAGMEM_PD
  78. if (!retent->tagmem.icache.enable) {
  79. Cache_Invalidate_ICache_All();
  80. }
  81. if (!retent->tagmem.dcache.enable) {
  82. Cache_Invalidate_DCache_All();
  83. }
  84. #else
  85. Cache_Invalidate_ICache_All();
  86. Cache_Invalidate_DCache_All();
  87. #endif // SOC_PM_SUPPORT_TAGMEM_PD
  88. rtc_cntl_ll_disable_cpu_retention();
  89. }
  90. }
  91. }
  92. #endif // SOC_PM_SUPPORT_CPU_PD
  93. #if SOC_PM_SUPPORT_TAGMEM_PD
  94. void rtc_cntl_hal_enable_tagmem_retention(void *addr)
  95. {
  96. rtc_cntl_sleep_retent_t *retent = (rtc_cntl_sleep_retent_t *)addr;
  97. if (addr) {
  98. if (retent->tagmem.link_addr) {
  99. rtc_cntl_ll_set_tagmem_retention_link_addr((uint32_t)(retent->tagmem.link_addr));
  100. rtc_cntl_ll_enable_tagmem_retention();
  101. if (retent->tagmem.icache.enable) {
  102. rtc_cntl_ll_enable_icache_tagmem_retention(
  103. retent->tagmem.icache.start_point,
  104. retent->tagmem.icache.vld_size,
  105. retent->tagmem.icache.size
  106. );
  107. }
  108. if (retent->tagmem.dcache.enable) {
  109. rtc_cntl_ll_enable_dcache_tagmem_retention(
  110. retent->tagmem.dcache.start_point,
  111. retent->tagmem.dcache.vld_size,
  112. retent->tagmem.dcache.size
  113. );
  114. }
  115. }
  116. }
  117. }
  118. void IRAM_ATTR rtc_cntl_hal_disable_tagmem_retention(void *addr)
  119. {
  120. rtc_cntl_sleep_retent_t *retent = (rtc_cntl_sleep_retent_t *)addr;
  121. if (addr) {
  122. if (retent->tagmem.link_addr) {
  123. rtc_cntl_ll_disable_tagmem_retention();
  124. if (retent->tagmem.icache.enable) {
  125. rtc_cntl_ll_disable_icache_tagmem_retention();
  126. }
  127. if (retent->tagmem.dcache.enable) {
  128. rtc_cntl_ll_disable_dcache_tagmem_retention();
  129. }
  130. }
  131. }
  132. }
  133. #endif // SOC_PM_SUPPORT_TAGMEM_PD