drv_rtc.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * drv_rtc.h
  3. *
  4. * Created on: 2016Äê12ÔÂ9ÈÕ
  5. * Author: Urey
  6. */
  7. #ifndef _DRV_RTC_H_
  8. #define _DRV_RTC_H_
  9. #include <stdint.h>
  10. #include "x1000.h"
  11. #ifndef RTC_BASE
  12. #define RTC_BASE 0xB0003000
  13. #endif
  14. /*************************************************************************
  15. * RTC
  16. *************************************************************************/
  17. #define REG_RTC_RTCCR REG32(RTC_BASE + RTC_RTCCR)
  18. #define REG_RTC_RTCSR REG32(RTC_BASE + RTC_RTCSR)
  19. #define REG_RTC_RTCSAR REG32(RTC_BASE + RTC_RTCSAR)
  20. #define REG_RTC_RTCGR REG32(RTC_BASE + RTC_RTCGR)
  21. #define REG_RTC_HCR REG32(RTC_BASE + RTC_HCR)
  22. #define REG_RTC_HWFCR REG32(RTC_BASE + RTC_HWFCR)
  23. #define REG_RTC_HRCR REG32(RTC_BASE + RTC_HRCR)
  24. #define REG_RTC_HWCR REG32(RTC_BASE + RTC_HWCR)
  25. #define REG_RTC_HWRSR REG32(RTC_BASE + RTC_HWRSR)
  26. #define REG_RTC_HSPR REG32(RTC_BASE + RTC_HSPR)
  27. #define REG_RTC_WENR REG32(RTC_BASE + RTC_WENR)
  28. #define REG_RTC_CKPCR REG32(RTC_BASE + RTC_CKPCR)
  29. #define REG_RTC_OWIPCR REG32(RTC_BASE + RTC_OWIPCR)
  30. #define REG_RTC_PWRONCR REG32(RTC_BASE + RTC_PWRONCR)
  31. #define COLD_BOOT_SIG 0x12345678
  32. /* RTC Control Register */
  33. #define RTC_RTCCR_WRDY_BIT 7
  34. #define RTC_RTCCR_WRDY (1 << RTC_RTCCR_WRDY_BIT) /* Write Ready Flag */
  35. #define RTC_RTCCR_1HZ_BIT 6
  36. #define RTC_RTCCR_1HZ (1 << RTC_RTCCR_1HZ_BIT) /* 1Hz Flag */
  37. #define RTC_RTCCR_1HZIE_BIT 5
  38. #define RTC_RTCCR_1HZIE (1 << RTC_RTCCR_1HZIE_BIT) /* 1Hz Interrupt Enable */
  39. #define RTC_RTCCR_AF_BIT 4
  40. #define RTC_RTCCR_AF (1 << RTC_RTCCR_AF_BIT) /* Alarm Flag */
  41. #define RTC_RTCCR_AIE_BIT 3
  42. #define RTC_RTCCR_AIE (1 << RTC_RTCCR_AIE_BIT) /* Alarm Interrupt Enable */
  43. #define RTC_RTCCR_AE_BIT 2
  44. #define RTC_RTCCR_AE (1 << RTC_RTCCR_AE_BIT) /* Alarm Enable */
  45. #define RTC_RTCCR_SELEXC_BIT 1
  46. #define RTC_RTCCR_SELEXC (1 << RTC_RTCCR_SELEXC_BIT)
  47. #define RTC_RTCCR_RTCE_BIT 0
  48. #define RTC_RTCCR_RTCE (1 << RTC_RTCCR_RTCE_BIT) /* RTC Enable */
  49. /* RTC Regulator Register */
  50. #define RTC_RTCGR_LOCK (1 << 31) /* Lock Bit */
  51. #define RTC_RTCGR_ADJC_BIT 16
  52. #define RTC_RTCGR_ADJC_MASK (0x3ff << RTC_RTCGR_ADJC_BIT)
  53. #define RTC_RTCGR_NC1HZ_BIT 0
  54. #define RTC_RTCGR_NC1HZ_MASK (0xffff << RTC_RTCGR_NC1HZ_BIT)
  55. #define RTCGR_DIV_1HZ ((32767 << RTC_RTCGR_NC1HZ_BIT) & RTC_RTCGR_NC1HZ_MASK )
  56. /* Hibernate Control Register */
  57. #define RTC_HCR_PD (1 << 0) /* Power Down */
  58. /* Hibernate Wakeup Filter Counter Register */
  59. #define RTC_HWFCR_BIT 5
  60. #define RTC_HWFCR_MASK (0x7ff << RTC_HWFCR_BIT)
  61. #define HWFCR_WAIT_TIME(ms) (((ms) << RTC_HWFCR_BIT) > RTC_HWFCR_MASK ? RTC_HWFCR_MASK : ((ms) << RTC_HWFCR_BIT))
  62. /* Hibernate Reset Counter Register */
  63. #define RTC_HRCR_BIT 5
  64. #define RTC_HRCR_MASK (0x7f << RTC_HRCR_BIT)
  65. #define HRCR_WAIT_TIME(ms) (((ms) << RTC_HRCR_BIT) > RTC_HRCR_MASK ? RTC_HRCR_MASK : ((ms) << RTC_HRCR_BIT))
  66. /* Hibernate Wakeup Control Register */
  67. #define RTC_HWCR_EALM (1 << 0) /* RTC alarm wakeup enable */
  68. /* Hibernate Wakeup Status Register */
  69. #define RTC_HWRSR_HR (1 << 5) /* Hibernate reset */
  70. #define RTC_HWRSR_PPR (1 << 4) /* PPR reset */
  71. #define RTC_HWRSR_PIN (1 << 1) /* Wakeup pin status bit */
  72. #define RTC_HWRSR_ALM (1 << 0) /* RTC alarm status bit */
  73. /* Write Enable Pattern Register */
  74. #define RTC_WENR_WEN (1 << 31) /* The write enable flag */
  75. #define RTC_WENR_WENPAT_BIT 0
  76. #define RTC_WENR_WENPAT_MASK (0xffff << RTC_WENR_WENPAT_BIT)
  77. #define WENR_WENPAT_WRITABLE (0xa55a)
  78. /* CLK32K Pin Control Register */
  79. #define RTC_CKPCR_CK32RD (1 << 5) /* Read this bit will return CLK32K pin status. */
  80. #define RTC_CKPCR_CK32PULL (1 << 4) /* Pull up configures. */
  81. #define RTC_CKPCR_CK32CTL_BIT 1
  82. #define RTC_CKPCR_CK32CTL_MASK (0x3 << RTC_CKPCR_CK32CTL_BIT)
  83. #define RTC_CKPCR_CK32D (1 << 0)
  84. /* Power Monitor Register */
  85. #define RTC_PMCR_NBF (1 << 0) /* No RTC battery flag */
  86. /* Hibernate scratch pattern register(HSPR) */
  87. #define HSPR_RTCV 0x52544356 /* The value is 'RTCV', means rtc is valid */
  88. #ifndef __ASSEMBLER__
  89. /***************************************************************************
  90. * RTC ops
  91. ***************************************************************************/
  92. #define __rtc_write_ready() ( (REG_RTC_RTCCR & RTC_RTCCR_WRDY) >> RTC_RTCCR_WRDY_BIT )
  93. #define __rtc_enabled() ( REG_RTC_RTCCR |= RTC_RTCCR_RTCE )
  94. #define __rtc_disabled() ( REG_RTC_RTCCR &= ~RTC_RTCCR_RTCE )
  95. #define __rtc_enable_alarm() ( REG_RTC_RTCCR |= RTC_RTCCR_AE )
  96. #define __rtc_disable_alarm() ( REG_RTC_RTCCR &= ~RTC_RTCCR_AE )
  97. #define __rtc_enable_alarm_irq() ( REG_RTC_RTCCR |= RTC_RTCCR_AIE )
  98. #define __rtc_disable_alarm_irq() ( REG_RTC_RTCCR &= ~RTC_RTCCR_AIE )
  99. #define __rtc_enable_1Hz_irq() ( REG_RTC_RTCCR |= RTC_RTCCR_1HZIE )
  100. #define __rtc_disable_1Hz_irq() ( REG_RTC_RTCCR &= ~RTC_RTCCR_1HZIE )
  101. #define __rtc_get_1Hz_flag() ( (REG_RTC_RTCCR >> RTC_RTCCR_1HZIE_BIT) & 0x1 )
  102. #define __rtc_clear_1Hz_flag() ( REG_RTC_RTCCR &= ~RTC_RTCCR_1HZ )
  103. #define __rtc_get_alarm_flag() ( (REG_RTC_RTCCR >> RTC_RTCCR_AF_BIT) & 0x1 )
  104. #define __rtc_clear_alarm_flag() ( REG_RTC_RTCCR &= ~RTC_RTCCR_AF )
  105. #define __rtc_get_second() ( REG_RTC_RTCSR )
  106. #define __rtc_set_second(v) ( REG_RTC_RTCSR = v )
  107. #define __rtc_get_alarm_second() ( REG_RTC_RTCSAR )
  108. #define __rtc_set_alarm_second(v) ( REG_RTC_RTCSAR = v )
  109. #define __rtc_RGR_is_locked() ( (REG_RTC_RTCGR >> RTC_RTCGR_LOCK) )
  110. #define __rtc_lock_RGR() ( REG_RTC_RTCGR |= RTC_RTCGR_LOCK )
  111. #define __rtc_unlock_RGR() ( REG_RTC_RTCGR &= ~RTC_RTCGR_LOCK )
  112. #define __rtc_get_adjc_val() ( (REG_RTC_RTCGR & RTC_RTCGR_ADJC_MASK) >> RTC_RTCGR_ADJC_BIT )
  113. #define __rtc_set_adjc_val(v) \
  114. ( REG_RTC_RTCGR = ( (REG_RTC_RTCGR & ~RTC_RTCGR_ADJC_MASK) | (v << RTC_RTCGR_ADJC_BIT) ))
  115. #define __rtc_get_nc1Hz_val() ( (REG_RTC_RTCGR & RTC_RTCGR_NC1HZ_MASK) >> RTC_RTCGR_NC1HZ_BIT )
  116. #define __rtc_set_nc1Hz_val(v) \
  117. ( REG_RTC_RTCGR = ( (REG_RTC_RTCGR & ~RTC_RTCGR_NC1HZ_MASK) | (v << RTC_RTCGR_NC1HZ_BIT) ))
  118. #define __rtc_power_down() ( REG_RTC_HCR |= RTC_HCR_PD )
  119. #define __rtc_get_hwfcr_val() ( REG_RTC_HWFCR & RTC_HWFCR_MASK )
  120. #define __rtc_set_hwfcr_val(v) ( REG_RTC_HWFCR = (v) & RTC_HWFCR_MASK )
  121. #define __rtc_get_hrcr_val() ( REG_RTC_HRCR & RTC_HRCR_MASK )
  122. #define __rtc_set_hrcr_val(v) ( REG_RTC_HRCR = (v) & RTC_HRCR_MASK )
  123. #define __rtc_enable_alarm_wakeup() ( REG_RTC_HWCR |= RTC_HWCR_EALM )
  124. #define __rtc_disable_alarm_wakeup() ( REG_RTC_HWCR &= ~RTC_HWCR_EALM )
  125. #define __rtc_status_hib_reset_occur() ( (REG_RTC_HWRSR >> RTC_HWRSR_HR) & 0x1 )
  126. #define __rtc_status_ppr_reset_occur() ( (REG_RTC_HWRSR >> RTC_HWRSR_PPR) & 0x1 )
  127. #define __rtc_status_wakeup_pin_waken_up() ( (REG_RTC_HWRSR >> RTC_HWRSR_PIN) & 0x1 )
  128. #define __rtc_status_alarm_waken_up() ( (REG_RTC_HWRSR >> RTC_HWRSR_ALM) & 0x1 )
  129. #define __rtc_clear_hib_stat_all() ( REG_RTC_HWRSR = 0 )
  130. #define __rtc_get_scratch_pattern() (REG_RTC_HSPR)
  131. #define __rtc_set_scratch_pattern(n) (REG_RTC_HSPR = n )
  132. /* Waiting for the RTC register writing finish */
  133. #define __wait_write_ready() \
  134. do { \
  135. unsigned int timeout = 1; \
  136. while (!(rtc_read_reg(RTC_BASE + RTC_RTCCR) & RTC_RTCCR_WRDY) && timeout++); \
  137. }while(0);
  138. /* Waiting for the RTC register writable */
  139. #define __wait_writable() \
  140. do { \
  141. unsigned int timeout = 1; \
  142. __wait_write_ready(); \
  143. jz_writel((RTC_BASE + RTC_WENR), WENR_WENPAT_WRITABLE); \
  144. __wait_write_ready(); \
  145. while (!(rtc_read_reg(RTC_BASE + RTC_WENR) & RTC_WENR_WEN) && timeout++); \
  146. }while(0);
  147. /* Basic RTC ops */
  148. #define rtc_read_reg(reg) \
  149. ({ \
  150. unsigned int data; \
  151. do { \
  152. data = jz_readl(reg); \
  153. } while (jz_readl(reg) != data); \
  154. data; \
  155. })
  156. #define rtc_write_reg(reg, data) \
  157. do { \
  158. __wait_writable(); \
  159. jz_writel(reg, data); \
  160. __wait_write_ready(); \
  161. }while(0);
  162. #define rtc_set_reg(reg, data) rtc_write_reg(reg, rtc_read_reg(reg) | (data))
  163. #define rtc_clr_reg(reg, data) rtc_write_reg(reg, rtc_read_reg(reg) & ~(data))
  164. #endif /* __ASSEMBLER__ */
  165. void rtc32k_enable(void);
  166. void rtc32k_disable(void);
  167. #endif /* _DRV_RTC_H_ */