rtc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2021, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <common/debug.h>
  7. #include <drivers/delay_timer.h>
  8. #include <rtc.h>
  9. static void RTC_Config_Interface(uint32_t addr, uint16_t data,
  10. uint16_t MASK, uint16_t SHIFT)
  11. {
  12. uint16_t pmic_reg = 0;
  13. pmic_reg = RTC_Read(addr);
  14. pmic_reg &= ~(MASK << SHIFT);
  15. pmic_reg |= (data << SHIFT);
  16. RTC_Write(addr, pmic_reg);
  17. }
  18. static void rtc_disable_2sec_reboot(void)
  19. {
  20. uint16_t reboot;
  21. reboot = (RTC_Read(RTC_AL_SEC) & ~RTC_BBPU_2SEC_EN) &
  22. ~RTC_BBPU_AUTO_PDN_SEL;
  23. RTC_Write(RTC_AL_SEC, reboot);
  24. RTC_Write_Trigger();
  25. }
  26. static void rtc_xosc_write(uint16_t val, bool reload)
  27. {
  28. uint16_t bbpu;
  29. RTC_Write(RTC_OSC32CON, RTC_OSC32CON_UNLOCK1);
  30. rtc_busy_wait();
  31. RTC_Write(RTC_OSC32CON, RTC_OSC32CON_UNLOCK2);
  32. rtc_busy_wait();
  33. RTC_Write(RTC_OSC32CON, val);
  34. rtc_busy_wait();
  35. if (reload) {
  36. bbpu = RTC_Read(RTC_BBPU) | RTC_BBPU_KEY | RTC_BBPU_RELOAD;
  37. RTC_Write(RTC_BBPU, bbpu);
  38. RTC_Write_Trigger();
  39. }
  40. }
  41. static void rtc_enable_k_eosc(void)
  42. {
  43. uint16_t osc32;
  44. uint16_t rtc_eosc_cali_td = 8; /* eosc cali period time */
  45. /* Truning on eosc cali mode clock */
  46. RTC_Config_Interface(PMIC_RG_TOP_CON, 1,
  47. PMIC_RG_SRCLKEN_IN0_HW_MODE_MASK,
  48. PMIC_RG_SRCLKEN_IN0_HW_MODE_SHIFT);
  49. RTC_Config_Interface(PMIC_RG_TOP_CON, 1,
  50. PMIC_RG_SRCLKEN_IN1_HW_MODE_MASK,
  51. PMIC_RG_SRCLKEN_IN1_HW_MODE_SHIFT);
  52. RTC_Config_Interface(PMIC_RG_SCK_TOP_CKPDN_CON0, 0,
  53. PMIC_RG_RTC_EOSC32_CK_PDN_MASK,
  54. PMIC_RG_RTC_EOSC32_CK_PDN_SHIFT);
  55. switch (rtc_eosc_cali_td) {
  56. case 1:
  57. RTC_Config_Interface(PMIC_RG_EOSC_CALI_CON0, 0x3,
  58. PMIC_RG_EOSC_CALI_TD_MASK, PMIC_RG_EOSC_CALI_TD_SHIFT);
  59. break;
  60. case 2:
  61. RTC_Config_Interface(PMIC_RG_EOSC_CALI_CON0, 0x4,
  62. PMIC_RG_EOSC_CALI_TD_MASK, PMIC_RG_EOSC_CALI_TD_SHIFT);
  63. break;
  64. case 4:
  65. RTC_Config_Interface(PMIC_RG_EOSC_CALI_CON0, 0x5,
  66. PMIC_RG_EOSC_CALI_TD_MASK, PMIC_RG_EOSC_CALI_TD_SHIFT);
  67. break;
  68. case 16:
  69. RTC_Config_Interface(PMIC_RG_EOSC_CALI_CON0, 0x7,
  70. PMIC_RG_EOSC_CALI_TD_MASK, PMIC_RG_EOSC_CALI_TD_SHIFT);
  71. break;
  72. default:
  73. RTC_Config_Interface(PMIC_RG_EOSC_CALI_CON0, 0x6,
  74. PMIC_RG_EOSC_CALI_TD_MASK, PMIC_RG_EOSC_CALI_TD_SHIFT);
  75. break;
  76. }
  77. /* Switch the DCXO from 32k-less mode to RTC mode,
  78. * otherwise, EOSC cali will fail
  79. */
  80. /* RTC mode will have only OFF mode and FPM */
  81. RTC_Config_Interface(PMIC_RG_DCXO_CW02, 0, PMIC_RG_XO_EN32K_MAN_MASK,
  82. PMIC_RG_XO_EN32K_MAN_SHIFT);
  83. RTC_Write(RTC_BBPU,
  84. RTC_Read(RTC_BBPU) | RTC_BBPU_KEY | RTC_BBPU_RELOAD);
  85. RTC_Write_Trigger();
  86. /* Enable K EOSC mode for normal power off and then plug out battery */
  87. RTC_Write(RTC_AL_YEA, ((RTC_Read(RTC_AL_YEA) | RTC_K_EOSC_RSV_0)
  88. & (~RTC_K_EOSC_RSV_1)) | RTC_K_EOSC_RSV_2);
  89. RTC_Write_Trigger();
  90. osc32 = RTC_Read(RTC_OSC32CON);
  91. rtc_xosc_write(osc32 | RTC_EMBCK_SRC_SEL, true);
  92. INFO("[RTC] RTC_enable_k_eosc\n");
  93. }
  94. void rtc_power_off_sequence(void)
  95. {
  96. uint16_t bbpu;
  97. rtc_disable_2sec_reboot();
  98. rtc_enable_k_eosc();
  99. /* clear alarm */
  100. bbpu = RTC_BBPU_KEY | RTC_BBPU_CLR | RTC_BBPU_PWREN;
  101. if (Writeif_unlock()) {
  102. RTC_Write(RTC_BBPU, bbpu);
  103. RTC_Write(RTC_AL_MASK, RTC_AL_MASK_DOW);
  104. RTC_Write_Trigger();
  105. mdelay(1);
  106. bbpu = RTC_Read(RTC_BBPU) | RTC_BBPU_KEY | RTC_BBPU_RELOAD;
  107. RTC_Write(RTC_BBPU, bbpu);
  108. RTC_Write_Trigger();
  109. INFO("[RTC] BBPU=0x%x, IRQ_EN=0x%x, AL_MSK=0x%x, AL_SEC=0x%x\n",
  110. RTC_Read(RTC_BBPU), RTC_Read(RTC_IRQ_EN),
  111. RTC_Read(RTC_AL_MASK), RTC_Read(RTC_AL_SEC));
  112. }
  113. }