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