rtc_mt6359p.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2020-2022, 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;
  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 int32_t 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. return RTC_Write_Trigger();
  25. }
  26. static int32_t rtc_enable_k_eosc(void)
  27. {
  28. uint16_t alm_dow, alm_sec;
  29. int16_t ret;
  30. /* Turning on eosc cali mode clock */
  31. RTC_Config_Interface(PMIC_RG_SCK_TOP_CKPDN_CON0_CLR, 1,
  32. PMIC_RG_RTC_EOSC32_CK_PDN_MASK,
  33. PMIC_RG_RTC_EOSC32_CK_PDN_SHIFT);
  34. alm_sec = RTC_Read(RTC_AL_SEC) & (~RTC_LPD_OPT_MASK);
  35. RTC_Write(RTC_AL_SEC, alm_sec);
  36. ret = RTC_Write_Trigger();
  37. if (ret == 0) {
  38. return 0;
  39. }
  40. RTC_Write(RTC_CON, RTC_LPD_EN);
  41. ret = RTC_Write_Trigger();
  42. if (ret == 0) {
  43. return 0;
  44. }
  45. RTC_Write(RTC_CON, RTC_LPD_RST);
  46. ret = RTC_Write_Trigger();
  47. if (ret == 0) {
  48. return 0;
  49. }
  50. RTC_Write(RTC_CON, RTC_LPD_EN);
  51. ret = RTC_Write_Trigger();
  52. if (ret == 0) {
  53. return 0;
  54. }
  55. RTC_Write(RTC_POWERKEY1, RTC_POWERKEY1_KEY);
  56. RTC_Write(RTC_POWERKEY2, RTC_POWERKEY2_KEY);
  57. ret = RTC_Write_Trigger();
  58. if (ret == 0) {
  59. return 0;
  60. }
  61. /* set RTC EOSC calibration period = 8sec */
  62. alm_dow = (RTC_Read(RTC_AL_DOW) & (~RTC_RG_EOSC_CALI_TD_MASK)) |
  63. RTC_RG_EOSC_CALI_TD_8SEC;
  64. RTC_Write(RTC_AL_DOW, alm_dow);
  65. ret = RTC_Write_Trigger();
  66. if (ret == 0) {
  67. return 0;
  68. }
  69. RTC_Write(RTC_BBPU,
  70. RTC_Read(RTC_BBPU) | RTC_BBPU_KEY | RTC_BBPU_RELOAD);
  71. ret = RTC_Write_Trigger();
  72. if (ret == 0) {
  73. return 0;
  74. }
  75. /* Enable K EOSC mode :use solution1 of eosc cali to fix mt6359p 32K*/
  76. RTC_Write(RTC_AL_YEA, (((RTC_Read(RTC_AL_YEA) | RTC_K_EOSC_RSV_0)
  77. & (~RTC_K_EOSC_RSV_1)) | (RTC_K_EOSC_RSV_2)));
  78. ret = RTC_Write_Trigger();
  79. if (ret == 0) {
  80. return 0;
  81. }
  82. INFO("[RTC] RTC_enable_k_eosc\n");
  83. return 1;
  84. }
  85. void rtc_power_off_sequence(void)
  86. {
  87. uint16_t bbpu;
  88. int16_t ret;
  89. ret = rtc_disable_2sec_reboot();
  90. if (ret == 0) {
  91. return;
  92. }
  93. ret = rtc_enable_k_eosc();
  94. if (ret == 0) {
  95. return;
  96. }
  97. bbpu = RTC_BBPU_KEY | RTC_BBPU_PWREN;
  98. if (Writeif_unlock() != 0) {
  99. RTC_Write(RTC_BBPU,
  100. bbpu | RTC_BBPU_RESET_ALARM | RTC_BBPU_RESET_SPAR);
  101. RTC_Write(RTC_AL_MASK, RTC_AL_MASK_DOW);
  102. ret = RTC_Write_Trigger();
  103. if (ret == 0) {
  104. return;
  105. }
  106. mdelay(1);
  107. bbpu = RTC_Read(RTC_BBPU);
  108. if (((bbpu & RTC_BBPU_RESET_ALARM) > 0) ||
  109. ((bbpu & RTC_BBPU_RESET_SPAR) > 0)) {
  110. INFO("[RTC] timeout\n");
  111. }
  112. bbpu = RTC_Read(RTC_BBPU) | RTC_BBPU_KEY | RTC_BBPU_RELOAD;
  113. RTC_Write(RTC_BBPU, bbpu);
  114. ret = RTC_Write_Trigger();
  115. if (ret == 0) {
  116. return;
  117. }
  118. }
  119. }