rtc_common.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2019-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 <pmic_wrap_init.h>
  9. #include <rtc.h>
  10. /* RTC busy status polling interval and retry count */
  11. enum {
  12. RTC_WRTGR_POLLING_DELAY_MS = 10,
  13. RTC_WRTGR_POLLING_CNT = 100
  14. };
  15. uint16_t RTC_Read(uint32_t addr)
  16. {
  17. uint32_t rdata = 0;
  18. pwrap_read((uint32_t)addr, &rdata);
  19. return (uint16_t)rdata;
  20. }
  21. void RTC_Write(uint32_t addr, uint16_t data)
  22. {
  23. pwrap_write((uint32_t)addr, (uint32_t)data);
  24. }
  25. int32_t rtc_busy_wait(void)
  26. {
  27. uint64_t retry = RTC_WRTGR_POLLING_CNT;
  28. do {
  29. mdelay(RTC_WRTGR_POLLING_DELAY_MS);
  30. if (!(RTC_Read(RTC_BBPU) & RTC_BBPU_CBUSY))
  31. return 1;
  32. retry--;
  33. } while (retry);
  34. ERROR("[RTC] rtc cbusy time out!\n");
  35. return 0;
  36. }
  37. int32_t RTC_Write_Trigger(void)
  38. {
  39. RTC_Write(RTC_WRTGR, 1);
  40. return rtc_busy_wait();
  41. }
  42. int32_t Writeif_unlock(void)
  43. {
  44. RTC_Write(RTC_PROT, RTC_PROT_UNLOCK1);
  45. if (!RTC_Write_Trigger())
  46. return 0;
  47. RTC_Write(RTC_PROT, RTC_PROT_UNLOCK2);
  48. if (!RTC_Write_Trigger())
  49. return 0;
  50. return 1;
  51. }