sunxi_hal_rtc.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #ifndef _SUNXI_HAL_RTC_H
  2. #define _SUNXI_HAL_RTC_H
  3. #include <sunxi_hal_common.h>
  4. #include <rtc/rtc.h>
  5. #include <hal_clk.h>
  6. #include <hal_reset.h>
  7. #include <rtc/platform_rtc.h>
  8. /*
  9. * Time unit conversions
  10. */
  11. #define SEC_IN_MIN 60
  12. #define SEC_IN_HOUR (60 * SEC_IN_MIN)
  13. #define SEC_IN_DAY (24 * SEC_IN_HOUR)
  14. /*
  15. * The year parameter passed to the driver is usually an offset relative to
  16. * the year 1900. This macro is used to convert this offset to another one
  17. * relative to the minimum year allowed by the hardware.
  18. */
  19. #define SUNXI_YEAR_OFF(x) ((x)->min - 1900)
  20. #define EFEX_FLAG (0x5AA5A55A)
  21. #define RTC_FEL_INDEX 2
  22. #define RTC_BOOT_INDEX 6
  23. #define RTC_LOG_LEVEL_INDEX 5
  24. /* debug */
  25. #define SUNXI_DEBUG_MODE_FLAG (0x59)
  26. /* efex */
  27. #define SUNXI_EFEX_CMD_FLAG (0x5A)
  28. /* boot-resignature */
  29. #define SUNXI_BOOT_RESIGNATURE_FLAG (0x5B)
  30. /* recovery or boot-recovery */
  31. #define SUNXI_BOOT_RECOVERY_FLAG (0x5C)
  32. /* sysrecovery */
  33. #define SUNXI_SYS_RECOVERY_FLAG (0x5D)
  34. /* usb-recovery*/
  35. #define SUNXI_USB_RECOVERY_FLAG (0x5E)
  36. /* bootloader */
  37. #define SUNXI_FASTBOOT_FLAG (0x5F)
  38. /* uboot */
  39. #define SUNXI_UBOOT_FLAG (0x60)
  40. #define SUNXI_MASK_DH 0x0000001f
  41. #define SUNXI_MASK_SM 0x0000003f
  42. #define SUNXI_MASK_M 0x0000000f
  43. #define SUNXI_MASK_LY 0x00000001
  44. #define SUNXI_MASK_D 0x00000ffe
  45. #define SUNXI_GET(x, mask, shift) (((x) & ((mask) << (shift))) \
  46. >> (shift))
  47. #define SUNXI_SET(x, mask, shift) (((x) & (mask)) << (shift))
  48. /*
  49. * Get date values
  50. */
  51. #define SUNXI_DATE_GET_DAY_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 0)
  52. #define SUNXI_DATE_GET_MON_VALUE(x) SUNXI_GET(x, SUNXI_MASK_M, 8)
  53. #define SUNXI_DATE_GET_YEAR_VALUE(x, d) SUNXI_GET(x, (d)->mask, (d)->yshift)
  54. /*
  55. * Get time values
  56. */
  57. #define SUNXI_TIME_GET_SEC_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 0)
  58. #define SUNXI_TIME_GET_MIN_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 8)
  59. #define SUNXI_TIME_GET_HOUR_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 16)
  60. /*
  61. * Get alarm values
  62. */
  63. #define SUNXI_ALRM_GET_SEC_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 0)
  64. #define SUNXI_ALRM_GET_MIN_VALUE(x) SUNXI_GET(x, SUNXI_MASK_SM, 8)
  65. #define SUNXI_ALRM_GET_HOUR_VALUE(x) SUNXI_GET(x, SUNXI_MASK_DH, 16)
  66. /*
  67. * Set date values
  68. */
  69. #define SUNXI_DATE_SET_DAY_VALUE(x) SUNXI_DATE_GET_DAY_VALUE(x)
  70. #define SUNXI_DATE_SET_MON_VALUE(x) SUNXI_SET(x, SUNXI_MASK_M, 8)
  71. #define SUNXI_DATE_SET_YEAR_VALUE(x, d) SUNXI_SET(x, (d)->mask, (d)->yshift)
  72. #define SUNXI_LEAP_SET_VALUE(x, shift) SUNXI_SET(x, SUNXI_MASK_LY, shift)
  73. /*
  74. * Set time values
  75. */
  76. #define SUNXI_TIME_SET_SEC_VALUE(x) SUNXI_TIME_GET_SEC_VALUE(x)
  77. #define SUNXI_TIME_SET_MIN_VALUE(x) SUNXI_SET(x, SUNXI_MASK_SM, 8)
  78. #define SUNXI_TIME_SET_HOUR_VALUE(x) SUNXI_SET(x, SUNXI_MASK_DH, 16)
  79. /*
  80. * Set alarm values
  81. */
  82. #define SUNXI_ALRM_SET_SEC_VALUE(x) SUNXI_ALRM_GET_SEC_VALUE(x)
  83. #define SUNXI_ALRM_SET_MIN_VALUE(x) SUNXI_SET(x, SUNXI_MASK_SM, 8)
  84. #define SUNXI_ALRM_SET_HOUR_VALUE(x) SUNXI_SET(x, SUNXI_MASK_DH, 16)
  85. #define SUNXI_ALRM_SET_DAY_VALUE(x) SUNXI_SET(x, SUNXI_MASK_D, 21)
  86. typedef int (*rtc_callback_t)(void);
  87. /*
  88. * min and max year are arbitrary set considering the limited range of the
  89. * hardware register field
  90. */
  91. struct hal_rtc_data_year
  92. {
  93. unsigned int min; /* min year allowed */
  94. unsigned int max; /* max year allowed */
  95. unsigned int mask; /* mask for the year field */
  96. unsigned int yshift; /* bit shift to get the year */
  97. unsigned char leap_shift; /* bit shift to get the leap year */
  98. };
  99. struct hal_rtc_dev
  100. {
  101. struct hal_rtc_data_year *data_year;
  102. rtc_callback_t user_callback;
  103. unsigned long base;
  104. int irq;
  105. hal_clk_t bus_clk;
  106. hal_clk_t rtc1k_clk;
  107. hal_clk_t rtcspi_clk;
  108. struct reset_control *reset;
  109. };
  110. typedef enum
  111. {
  112. RTC_GET_TIME = 0,
  113. RTC_SET_TIME = 1,
  114. RTC_GET_ALARM = 2,
  115. RTC_SET_ALARM = 3,
  116. RTC_CALLBACK = 4,
  117. RTC_IRQENABLE = 5
  118. } hal_rtc_transfer_cmd_t;
  119. void hal_rtc_set_fel_flag(void);
  120. u32 hal_rtc_probe_fel_flag(void);
  121. void hal_rtc_clear_fel_flag(void);
  122. int hal_rtc_get_bootmode_flag(void);
  123. int hal_rtc_set_bootmode_flag(u8 flag);
  124. void hal_rtc_write_data(int index, u32 val);
  125. u32 hal_rtc_read_data(int index);
  126. int hal_rtc_gettime(struct rtc_time *rtc_tm);
  127. int hal_rtc_settime(struct rtc_time *rtc_tm);
  128. int hal_rtc_getalarm(struct rtc_wkalrm *wkalrm);
  129. int hal_rtc_setalarm(struct rtc_wkalrm *wkalrm);
  130. int hal_rtc_alarm_irq_enable(unsigned int enabled);
  131. void hal_rtc_min_year_show(unsigned int *min);
  132. void hal_rtc_max_year_show(unsigned int *max);
  133. int hal_rtc_register_callback(rtc_callback_t user_callback);
  134. int hal_rtc_init(void);
  135. int hal_rtc_deinit(void);
  136. #endif /* _SUNXI_HAL_RTC_H */