rtc_init.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <sys/param.h>
  8. #include "soc/soc.h"
  9. #include "soc/rtc.h"
  10. #include "soc/rtc_cntl_reg.h"
  11. #include "soc/dport_reg.h"
  12. #include "soc/gpio_reg.h"
  13. #include "soc/syscon_reg.h"
  14. #include "soc/spi_mem_reg.h"
  15. #include "soc/extmem_reg.h"
  16. #include "soc/syscon_reg.h"
  17. #include "regi2c_ctrl.h"
  18. #include "soc/regi2c_lp_bias.h"
  19. #include "soc/regi2c_ulp.h"
  20. #include "soc/regi2c_dig_reg.h"
  21. #include "esp_hw_log.h"
  22. #include "esp_err.h"
  23. #include "esp_attr.h"
  24. #include "esp_private/mspi_timing_tuning.h"
  25. #include "hal/efuse_hal.h"
  26. #include "hal/efuse_ll.h"
  27. #ifndef BOOTLOADER_BUILD
  28. #include "esp_private/sar_periph_ctrl.h"
  29. #endif
  30. #define RTC_CNTL_MEM_FORCE_NOISO (RTC_CNTL_SLOWMEM_FORCE_NOISO | RTC_CNTL_FASTMEM_FORCE_NOISO)
  31. static const char *TAG = "rtcinit";
  32. static void set_ocode_by_efuse(int calib_version);
  33. static void calibrate_ocode(void);
  34. static void rtc_set_stored_dbias(void);
  35. // Initial values are used for bootloader, and these variables will be re-assigned based on efuse values during application startup
  36. uint32_t g_dig_dbias_pvt_240m = 28;
  37. uint32_t g_rtc_dbias_pvt_240m = 28;
  38. uint32_t g_dig_dbias_pvt_non_240m = 27;
  39. uint32_t g_rtc_dbias_pvt_non_240m = 27;
  40. void rtc_init(rtc_config_t cfg)
  41. {
  42. /**
  43. * When run rtc_init, it maybe deep sleep reset. Since we power down modem in deep sleep, after wakeup
  44. * from deep sleep, these fields are changed and not reset. We will access two BB regs(BBPD_CTRL and
  45. * NRXPD_CTRL) in rtc_sleep_pu. If PD modem and no iso, CPU will stuck when access these two BB regs
  46. * and finally triggle RTC WDT. So need to clear modem Force PD.
  47. *
  48. * No worry about the power consumption, Because modem Force PD will be set at the end of this function.
  49. */
  50. CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_WIFI_FORCE_PD);
  51. REGI2C_WRITE_MASK(I2C_DIG_REG, I2C_DIG_REG_XPD_RTC_REG, 0);
  52. REGI2C_WRITE_MASK(I2C_DIG_REG, I2C_DIG_REG_XPD_DIG_REG, 0);
  53. CLEAR_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_PVTMON_PU);
  54. REG_SET_FIELD(RTC_CNTL_TIMER1_REG, RTC_CNTL_PLL_BUF_WAIT, cfg.pll_wait);
  55. REG_SET_FIELD(RTC_CNTL_TIMER1_REG, RTC_CNTL_CK8M_WAIT, cfg.ck8m_wait);
  56. /* Moved from rtc sleep to rtc init to save sleep function running time */
  57. // set shortest possible sleep time limit
  58. REG_SET_FIELD(RTC_CNTL_TIMER5_REG, RTC_CNTL_MIN_SLP_VAL, RTC_CNTL_MIN_SLP_VAL_MIN);
  59. // set wifi timer
  60. rtc_init_config_t rtc_init_cfg = RTC_INIT_CONFIG_DEFAULT();
  61. REG_SET_FIELD(RTC_CNTL_TIMER3_REG, RTC_CNTL_WIFI_POWERUP_TIMER, rtc_init_cfg.wifi_powerup_cycles);
  62. REG_SET_FIELD(RTC_CNTL_TIMER3_REG, RTC_CNTL_WIFI_WAIT_TIMER, rtc_init_cfg.wifi_wait_cycles);
  63. // set bt timer
  64. REG_SET_FIELD(RTC_CNTL_TIMER3_REG, RTC_CNTL_BT_POWERUP_TIMER, rtc_init_cfg.bt_powerup_cycles);
  65. REG_SET_FIELD(RTC_CNTL_TIMER3_REG, RTC_CNTL_BT_WAIT_TIMER, rtc_init_cfg.bt_wait_cycles);
  66. REG_SET_FIELD(RTC_CNTL_TIMER6_REG, RTC_CNTL_CPU_TOP_POWERUP_TIMER, rtc_init_cfg.cpu_top_powerup_cycles);
  67. REG_SET_FIELD(RTC_CNTL_TIMER6_REG, RTC_CNTL_CPU_TOP_WAIT_TIMER, rtc_init_cfg.cpu_top_wait_cycles);
  68. // set rtc peri timer
  69. REG_SET_FIELD(RTC_CNTL_TIMER4_REG, RTC_CNTL_POWERUP_TIMER, rtc_init_cfg.rtc_powerup_cycles);
  70. REG_SET_FIELD(RTC_CNTL_TIMER4_REG, RTC_CNTL_WAIT_TIMER, rtc_init_cfg.rtc_wait_cycles);
  71. // set digital wrap timer
  72. REG_SET_FIELD(RTC_CNTL_TIMER4_REG, RTC_CNTL_DG_WRAP_POWERUP_TIMER, rtc_init_cfg.dg_wrap_powerup_cycles);
  73. REG_SET_FIELD(RTC_CNTL_TIMER4_REG, RTC_CNTL_DG_WRAP_WAIT_TIMER, rtc_init_cfg.dg_wrap_wait_cycles);
  74. REG_SET_FIELD(RTC_CNTL_TIMER6_REG, RTC_CNTL_DG_PERI_POWERUP_TIMER, rtc_init_cfg.dg_peri_powerup_cycles);
  75. REG_SET_FIELD(RTC_CNTL_TIMER6_REG, RTC_CNTL_DG_PERI_WAIT_TIMER, rtc_init_cfg.dg_peri_wait_cycles);
  76. /* Reset RTC bias to default value (needed if waking up from deep sleep) */
  77. REGI2C_WRITE_MASK(I2C_DIG_REG, I2C_DIG_REG_EXT_RTC_DREG_SLEEP, RTC_CNTL_DBIAS_1V10);
  78. REGI2C_WRITE_MASK(I2C_DIG_REG, I2C_DIG_REG_EXT_RTC_DREG, RTC_CNTL_DBIAS_1V10);
  79. /* Set the wait time to the default value. */
  80. REG_SET_FIELD(RTC_CNTL_TIMER2_REG, RTC_CNTL_ULPCP_TOUCH_START_WAIT, RTC_CNTL_ULPCP_TOUCH_START_WAIT_DEFAULT);
  81. if (cfg.cali_ocode) {
  82. uint32_t blk_ver_major = efuse_ll_get_blk_version_major(); // IDF-5366
  83. //default blk_ver_major will fallback to using the self-calibration way for OCode
  84. bool ocode_efuse_cali = (blk_ver_major == 1);
  85. if (ocode_efuse_cali) {
  86. set_ocode_by_efuse(blk_ver_major);
  87. } else {
  88. calibrate_ocode();
  89. }
  90. }
  91. //LDO dbias initialization
  92. rtc_set_stored_dbias();
  93. REGI2C_WRITE_MASK(I2C_DIG_REG, I2C_DIG_REG_EXT_RTC_DREG, g_rtc_dbias_pvt_non_240m);
  94. REGI2C_WRITE_MASK(I2C_DIG_REG, I2C_DIG_REG_EXT_DIG_DREG, g_dig_dbias_pvt_non_240m);
  95. if (cfg.clkctl_init) {
  96. //clear CMMU clock force on
  97. CLEAR_PERI_REG_MASK(EXTMEM_CACHE_MMU_POWER_CTRL_REG, EXTMEM_CACHE_MMU_MEM_FORCE_ON);
  98. //clear clkgate force on
  99. REG_WRITE(SYSCON_CLKGATE_FORCE_ON_REG, 0);
  100. //clear tag clock force on
  101. CLEAR_PERI_REG_MASK(EXTMEM_DCACHE_TAG_POWER_CTRL_REG, EXTMEM_DCACHE_TAG_MEM_FORCE_ON);
  102. CLEAR_PERI_REG_MASK(EXTMEM_ICACHE_TAG_POWER_CTRL_REG, EXTMEM_ICACHE_TAG_MEM_FORCE_ON);
  103. //clear register clock force on
  104. CLEAR_PERI_REG_MASK(SPI_MEM_CLOCK_GATE_REG(0), SPI_MEM_CLK_EN);
  105. CLEAR_PERI_REG_MASK(SPI_MEM_CLOCK_GATE_REG(1), SPI_MEM_CLK_EN);
  106. }
  107. if (cfg.pwrctl_init) {
  108. CLEAR_PERI_REG_MASK(RTC_CNTL_CLK_CONF_REG, RTC_CNTL_CK8M_FORCE_PU);
  109. //cancel xtal force pu if no need to force power up
  110. //cannot cancel xtal force pu if pll is force power on
  111. if (!(cfg.xtal_fpu | cfg.bbpll_fpu)) {
  112. CLEAR_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_XTL_FORCE_PU);
  113. } else {
  114. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_XTL_FORCE_PU);
  115. }
  116. //open sar_i2c protect function to avoid sar_i2c reset when rtc_ldo is low.
  117. CLEAR_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_I2C_RESET_POR_FORCE_PD);
  118. //cancel bbpll force pu if setting no force power up
  119. if (!cfg.bbpll_fpu) {
  120. CLEAR_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BBPLL_FORCE_PU);
  121. CLEAR_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BBPLL_I2C_FORCE_PU);
  122. CLEAR_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BB_I2C_FORCE_PU);
  123. } else {
  124. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BBPLL_FORCE_PU);
  125. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BBPLL_I2C_FORCE_PU);
  126. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, RTC_CNTL_BB_I2C_FORCE_PU);
  127. }
  128. //cancel RTC REG force PU
  129. CLEAR_PERI_REG_MASK(RTC_CNTL_PWC_REG, RTC_CNTL_FORCE_PU);
  130. CLEAR_PERI_REG_MASK(RTC_CNTL_REG, RTC_CNTL_REGULATOR_FORCE_PU);
  131. CLEAR_PERI_REG_MASK(RTC_CNTL_REG, RTC_CNTL_DBOOST_FORCE_PU);
  132. CLEAR_PERI_REG_MASK(RTC_CNTL_PWC_REG, RTC_CNTL_SLOWMEM_FORCE_NOISO | RTC_CNTL_FASTMEM_FORCE_NOISO);
  133. if (cfg.rtc_dboost_fpd) {
  134. SET_PERI_REG_MASK(RTC_CNTL_REG, RTC_CNTL_DBOOST_FORCE_PD);
  135. } else {
  136. CLEAR_PERI_REG_MASK(RTC_CNTL_REG, RTC_CNTL_DBOOST_FORCE_PD);
  137. }
  138. //clear i2c_reset_protect pd force, need tested in low temperature.
  139. CLEAR_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_I2C_RESET_POR_FORCE_PD);
  140. /* If this mask is enabled, all soc memories cannot enter power down mode */
  141. /* We should control soc memory power down mode from RTC, so we will not touch this register any more */
  142. CLEAR_PERI_REG_MASK(SYSTEM_MEM_PD_MASK_REG, SYSTEM_LSLP_MEM_PD_MASK);
  143. /* If this pd_cfg is set to 1, all memory won't enter low power mode during light sleep */
  144. /* If this pd_cfg is set to 0, all memory will enter low power mode during light sleep */
  145. rtc_sleep_pu_config_t pu_cfg = RTC_SLEEP_PU_CONFIG_ALL(0);
  146. rtc_sleep_pu(pu_cfg);
  147. REG_CLR_BIT(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_DG_WRAP_FORCE_PU);
  148. REG_CLR_BIT(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_DG_WRAP_FORCE_NOISO | RTC_CNTL_DG_WRAP_FORCE_ISO);
  149. REG_CLR_BIT(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_WIFI_FORCE_NOISO | RTC_CNTL_WIFI_FORCE_ISO);
  150. REG_CLR_BIT(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_WIFI_FORCE_PU);
  151. REG_CLR_BIT(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_BT_FORCE_NOISO | RTC_CNTL_BT_FORCE_ISO);
  152. REG_CLR_BIT(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_BT_FORCE_PU);
  153. REG_CLR_BIT(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_CPU_TOP_FORCE_NOISO | RTC_CNTL_CPU_TOP_FORCE_ISO);
  154. REG_CLR_BIT(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_CPU_TOP_FORCE_PU);
  155. REG_CLR_BIT(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_DG_PERI_FORCE_NOISO | RTC_CNTL_DG_PERI_FORCE_ISO);
  156. REG_CLR_BIT(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_DG_PERI_FORCE_PU);
  157. REG_CLR_BIT(RTC_CNTL_PWC_REG, RTC_CNTL_FORCE_NOISO);
  158. REG_CLR_BIT(RTC_CNTL_PWC_REG, RTC_CNTL_FORCE_ISO);
  159. REG_CLR_BIT(RTC_CNTL_PWC_REG, RTC_CNTL_FORCE_PU);
  160. //cancel digital PADS force no iso
  161. if (cfg.cpu_waiti_clk_gate) {
  162. CLEAR_PERI_REG_MASK(SYSTEM_CPU_PER_CONF_REG, SYSTEM_CPU_WAIT_MODE_FORCE_ON);
  163. } else {
  164. SET_PERI_REG_MASK(SYSTEM_CPU_PER_CONF_REG, SYSTEM_CPU_WAIT_MODE_FORCE_ON);
  165. }
  166. /*if SYSTEM_CPU_WAIT_MODE_FORCE_ON == 0 , the cpu clk will be closed when cpu enter WAITI mode*/
  167. CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_DG_PAD_FORCE_UNHOLD);
  168. CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_DG_PAD_FORCE_NOISO);
  169. }
  170. /* force power down modem(wifi and ble) power domain */
  171. SET_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_WIFI_FORCE_ISO);
  172. SET_PERI_REG_MASK(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_WIFI_FORCE_PD);
  173. REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
  174. REG_WRITE(RTC_CNTL_INT_CLR_REG, UINT32_MAX);
  175. #ifndef BOOTLOADER_BUILD
  176. //initialise SAR related peripheral register settings
  177. sar_periph_ctrl_init();
  178. #endif
  179. }
  180. rtc_vddsdio_config_t rtc_vddsdio_get_config(void)
  181. {
  182. rtc_vddsdio_config_t result;
  183. uint32_t sdio_conf_reg = REG_READ(RTC_CNTL_SDIO_CONF_REG);
  184. result.drefh = (sdio_conf_reg & RTC_CNTL_DREFH_SDIO_M) >> RTC_CNTL_DREFH_SDIO_S;
  185. result.drefm = (sdio_conf_reg & RTC_CNTL_DREFM_SDIO_M) >> RTC_CNTL_DREFM_SDIO_S;
  186. result.drefl = (sdio_conf_reg & RTC_CNTL_DREFL_SDIO_M) >> RTC_CNTL_DREFL_SDIO_S;
  187. if (sdio_conf_reg & RTC_CNTL_SDIO_FORCE) {
  188. // Get configuration from RTC
  189. result.force = 1;
  190. result.enable = (sdio_conf_reg & RTC_CNTL_XPD_SDIO_REG_M) >> RTC_CNTL_XPD_SDIO_REG_S;
  191. result.tieh = (sdio_conf_reg & RTC_CNTL_SDIO_TIEH_M) >> RTC_CNTL_SDIO_TIEH_S;
  192. return result;
  193. } else {
  194. result.force = 0;
  195. }
  196. // Otherwise, VDD_SDIO is controlled by bootstrapping pin
  197. uint32_t strap_reg = REG_READ(GPIO_STRAP_REG);
  198. result.tieh = (strap_reg & BIT(5)) ? RTC_VDDSDIO_TIEH_1_8V : RTC_VDDSDIO_TIEH_3_3V;
  199. result.enable = 1;
  200. return result;
  201. }
  202. void rtc_vddsdio_set_config(rtc_vddsdio_config_t config)
  203. {
  204. uint32_t val = 0;
  205. val |= (config.force << RTC_CNTL_SDIO_FORCE_S);
  206. val |= (config.enable << RTC_CNTL_XPD_SDIO_REG_S);
  207. val |= (config.drefh << RTC_CNTL_DREFH_SDIO_S);
  208. val |= (config.drefm << RTC_CNTL_DREFM_SDIO_S);
  209. val |= (config.drefl << RTC_CNTL_DREFL_SDIO_S);
  210. val |= (config.tieh << RTC_CNTL_SDIO_TIEH_S);
  211. val |= RTC_CNTL_SDIO_PD_EN;
  212. REG_WRITE(RTC_CNTL_SDIO_CONF_REG, val);
  213. }
  214. static void set_ocode_by_efuse(int calib_version)
  215. {
  216. assert(calib_version == 1);
  217. // use efuse ocode.
  218. uint32_t ocode = efuse_ll_get_ocode();
  219. REGI2C_WRITE_MASK(I2C_ULP, I2C_ULP_EXT_CODE, ocode);
  220. REGI2C_WRITE_MASK(I2C_ULP, I2C_ULP_IR_FORCE_CODE, 1);
  221. }
  222. /**
  223. * TODO: IDF-4141
  224. * 1. This function will change the system clock source to XTAL. Under lower frequency (e.g. XTAL), MSPI timing tuning configures should be modified accordingly.
  225. * 2. RTC related should be done before SPI0 initialisation
  226. */
  227. static void calibrate_ocode(void)
  228. {
  229. #if !defined(BOOTLOADER_BUILD) && !defined(CONFIG_APP_BUILD_TYPE_PURE_RAM_APP)
  230. /**
  231. * Background:
  232. * 1. Following code will switch the system clock to XTAL first, to self-calibrate the OCode.
  233. * 2. For some of the MSPI high frequency setting (e.g. 80M DDR mode Flash or PSRAM), timing tuning is required.
  234. * Certain delay will be added to the MSPI RX direction.
  235. *
  236. * When CPU clock switches down, the delay should be cleared. Therefore here we call this function to remove the delays.
  237. */
  238. mspi_timing_change_speed_mode_cache_safe(true);
  239. #endif // #if !defined(BOOTLOADER_BUILD) && !defined(CONFIG_APP_BUILD_TYPE_PURE_RAM_APP)
  240. /*
  241. Bandgap output voltage is not precise when calibrate o-code by hardware sometimes, so need software o-code calibration (must turn off PLL).
  242. Method:
  243. 1. read current cpu config, save in old_config;
  244. 2. switch cpu to xtal because PLL will be closed when o-code calibration;
  245. 3. begin o-code calibration;
  246. 4. wait o-code calibration done flag(odone_flag & bg_odone_flag) or timeout;
  247. 5. set cpu to old-config.
  248. */
  249. soc_rtc_slow_clk_src_t slow_clk_src = rtc_clk_slow_src_get();
  250. rtc_cal_sel_t cal_clk = RTC_CAL_RTC_MUX;
  251. if (slow_clk_src == SOC_RTC_SLOW_CLK_SRC_XTAL32K) {
  252. cal_clk = RTC_CAL_32K_XTAL;
  253. } else if (slow_clk_src == SOC_RTC_SLOW_CLK_SRC_RC_FAST_D256) {
  254. cal_clk = RTC_CAL_8MD256;
  255. }
  256. uint64_t max_delay_time_us = 10000;
  257. uint32_t slow_clk_period = rtc_clk_cal(cal_clk, 100);
  258. uint64_t max_delay_cycle = rtc_time_us_to_slowclk(max_delay_time_us, slow_clk_period);
  259. uint64_t cycle0 = rtc_time_get();
  260. uint64_t timeout_cycle = cycle0 + max_delay_cycle;
  261. uint64_t cycle1 = 0;
  262. rtc_cpu_freq_config_t old_config;
  263. rtc_clk_cpu_freq_get_config(&old_config);
  264. rtc_clk_cpu_freq_set_xtal();
  265. REGI2C_WRITE_MASK(I2C_ULP, I2C_ULP_IR_RESETB, 0);
  266. REGI2C_WRITE_MASK(I2C_ULP, I2C_ULP_IR_RESETB, 1);
  267. bool odone_flag = 0;
  268. bool bg_odone_flag = 0;
  269. while (1) {
  270. odone_flag = REGI2C_READ_MASK(I2C_ULP, I2C_ULP_O_DONE_FLAG);
  271. bg_odone_flag = REGI2C_READ_MASK(I2C_ULP, I2C_ULP_BG_O_DONE_FLAG);
  272. cycle1 = rtc_time_get();
  273. if (odone_flag && bg_odone_flag) {
  274. break;
  275. }
  276. if (cycle1 >= timeout_cycle) {
  277. ESP_HW_LOGW(TAG, "o_code calibration fail\n");
  278. break;
  279. }
  280. }
  281. rtc_clk_cpu_freq_set_config(&old_config);
  282. #if !defined(BOOTLOADER_BUILD) && !defined(CONFIG_APP_BUILD_TYPE_PURE_RAM_APP)
  283. //System clock is switched back to PLL. Here we switch to the MSPI high speed mode, add the delays back
  284. mspi_timing_change_speed_mode_cache_safe(false);
  285. #endif // #if !defined(BOOTLOADER_BUILD) && !defined(CONFIG_APP_BUILD_TYPE_PURE_RAM_APP)
  286. }
  287. static uint32_t get_dig_dbias_by_efuse(uint8_t pvt_scheme_ver)
  288. {
  289. assert(pvt_scheme_ver == 1);
  290. return efuse_ll_get_dig_dbias_hvt();
  291. }
  292. static uint32_t get_rtc_dbias_by_efuse(uint8_t pvt_scheme_ver, uint32_t dig_dbias)
  293. {
  294. assert(pvt_scheme_ver == 1);
  295. uint32_t rtc_dbias = 0;
  296. signed int k_rtc_ldo = efuse_ll_get_k_rtc_ldo();
  297. signed int k_dig_ldo = efuse_ll_get_k_dig_ldo();
  298. signed int v_rtc_bias20 = efuse_ll_get_v_rtc_dbias20();
  299. signed int v_dig_bias20 = efuse_ll_get_v_dig_dbias20();
  300. k_rtc_ldo = ((k_rtc_ldo & BIT(6)) != 0)? -(k_rtc_ldo & 0x3f): (uint8_t)k_rtc_ldo;
  301. k_dig_ldo = ((k_dig_ldo & BIT(6)) != 0)? -(k_dig_ldo & 0x3f): (uint8_t)k_dig_ldo;
  302. v_rtc_bias20 = ((v_rtc_bias20 & BIT(7)) != 0)? -(v_rtc_bias20 & 0x7f): (uint8_t)v_rtc_bias20;
  303. v_dig_bias20 = ((v_dig_bias20 & BIT(7)) != 0)? -(v_dig_bias20 & 0x7f): (uint8_t)v_dig_bias20;
  304. uint32_t v_rtc_dbias20_real_mul10000 = V_RTC_MID_MUL10000 + v_rtc_bias20 * 10000 / 500;
  305. uint32_t v_dig_dbias20_real_mul10000 = V_DIG_MID_MUL10000 + v_dig_bias20 * 10000 / 500;
  306. signed int k_rtc_ldo_real_mul10000 = K_RTC_MID_MUL10000 + k_rtc_ldo;
  307. signed int k_dig_ldo_real_mul10000 = K_DIG_MID_MUL10000 + k_dig_ldo;
  308. uint32_t v_dig_nearest_1v15_mul10000 = v_dig_dbias20_real_mul10000 + k_dig_ldo_real_mul10000 * (dig_dbias - 20);
  309. for (rtc_dbias = 15; rtc_dbias < 31; rtc_dbias++) {
  310. uint32_t v_rtc_nearest_1v15_mul10000 = 0;
  311. v_rtc_nearest_1v15_mul10000 = v_rtc_dbias20_real_mul10000 + k_rtc_ldo_real_mul10000 * (rtc_dbias - 20);
  312. if (v_rtc_nearest_1v15_mul10000 >= v_dig_nearest_1v15_mul10000 - 250) {
  313. break;
  314. }
  315. }
  316. return rtc_dbias;
  317. }
  318. static uint32_t get_dig1v3_dbias_by_efuse(uint8_t pvt_scheme_ver)
  319. {
  320. assert(pvt_scheme_ver == 1);
  321. signed int k_dig_ldo = efuse_ll_get_k_dig_ldo();
  322. signed int v_dig_bias20 = efuse_ll_get_v_dig_dbias20();
  323. k_dig_ldo = ((k_dig_ldo & BIT(6)) != 0)? -(k_dig_ldo & 0x3f): (uint8_t)k_dig_ldo;
  324. v_dig_bias20 = ((v_dig_bias20 & BIT(7)) != 0)? -(v_dig_bias20 & 0x7f): (uint8_t)v_dig_bias20;
  325. uint32_t v_dig_dbias20_real_mul10000 = V_DIG_MID_MUL10000 + v_dig_bias20 * 10000 / 500;
  326. signed int k_dig_ldo_real_mul10000 = K_DIG_MID_MUL10000 + k_dig_ldo;
  327. uint32_t dig_dbias =15;
  328. for (dig_dbias = 15; dig_dbias < 31; dig_dbias++) {
  329. uint32_t v_dig_nearest_1v3_mul10000 = 0;
  330. v_dig_nearest_1v3_mul10000 = v_dig_dbias20_real_mul10000 + k_dig_ldo_real_mul10000 * (dig_dbias - 20);
  331. if (v_dig_nearest_1v3_mul10000 >= 13000) {
  332. break;
  333. }
  334. }
  335. return dig_dbias;
  336. }
  337. static void rtc_set_stored_dbias(void)
  338. {
  339. /*
  340. 1. a reasonable dig_dbias which by scaning pvt to make 240 CPU run successful stored in efuse;
  341. 2. also we store some value in efuse, include:
  342. k_rtc_ldo (slope of rtc voltage & rtc_dbias);
  343. k_dig_ldo (slope of digital voltage & digital_dbias);
  344. v_rtc_bias20 (rtc voltage when rtc dbais is 20);
  345. v_dig_bias20 (digital voltage when digital dbais is 20).
  346. 3. a reasonable rtc_dbias can be calculated by a certion formula.
  347. 4. save these values for reuse
  348. */
  349. uint8_t blk_minor = efuse_ll_get_blk_version_minor();
  350. uint8_t blk_major = efuse_ll_get_blk_version_major();
  351. uint8_t pvt_scheme_ver = 0;
  352. if ( (blk_major <= 1 && blk_minor == 1) || blk_major > 1 || (blk_major == 1 && blk_minor >= 2) ) {
  353. /* PVT supported after blk_ver 1.2 */
  354. pvt_scheme_ver = 1;
  355. }
  356. if (pvt_scheme_ver == 1) {
  357. uint32_t dig1v3_dbias = get_dig1v3_dbias_by_efuse(pvt_scheme_ver);
  358. uint32_t dig_dbias = get_dig_dbias_by_efuse(pvt_scheme_ver);
  359. if (dig_dbias != 0) {
  360. g_dig_dbias_pvt_240m = MIN(dig1v3_dbias, dig_dbias + 3);
  361. g_dig_dbias_pvt_non_240m = MIN(dig1v3_dbias, dig_dbias + 2);
  362. g_rtc_dbias_pvt_240m = get_rtc_dbias_by_efuse(pvt_scheme_ver, g_dig_dbias_pvt_240m);
  363. g_rtc_dbias_pvt_non_240m = get_rtc_dbias_by_efuse(pvt_scheme_ver, g_dig_dbias_pvt_non_240m);
  364. } else {
  365. ESP_HW_LOGD(TAG, "not burn core voltage in efuse or burn wrong voltage value in blk version: 0%d\n", pvt_scheme_ver);
  366. }
  367. } else {
  368. ESP_HW_LOGD(TAG, "core voltage not decided in efuse, use default value.");
  369. }
  370. }