2 * SiRFSoC Real Time Clock interface for Linux
4 * Copyright (c) 2013 Cambridge Silicon Radio Limited, a CSR plc group company.
6 * Licensed under GPLv2 or later.
9 #include <linux/module.h>
10 #include <linux/err.h>
11 #include <linux/rtc.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
16 #include <linux/rtc/sirfsoc_rtciobrg.h>
20 #define RTC_ALARM0 0x04
21 #define RTC_ALARM1 0x18
22 #define RTC_STATUS 0x08
23 #define RTC_SW_VALUE 0x40
24 #define SIRFSOC_RTC_AL1E (1<<6)
25 #define SIRFSOC_RTC_AL1 (1<<4)
26 #define SIRFSOC_RTC_HZE (1<<3)
27 #define SIRFSOC_RTC_AL0E (1<<2)
28 #define SIRFSOC_RTC_HZ (1<<1)
29 #define SIRFSOC_RTC_AL0 (1<<0)
31 #define RTC_DEEP_CTRL 0x14
32 #define RTC_CLOCK_SWITCH 0x1c
33 #define SIRFSOC_RTC_CLK 0x03 /* others are reserved */
35 /* Refer to RTC DIV switch */
38 /* This macro is also defined in arch/arm/plat-sirfsoc/cpu.c */
41 #define INTR_SYSRTC_CN 0x48
43 struct sirfsoc_rtc_drv
{
44 struct rtc_device
*rtc
;
48 /* Overflow for every 8 years extra time */
53 u32 saved_overflow_rtc
;
57 static int sirfsoc_rtc_read_alarm(struct device
*dev
,
58 struct rtc_wkalrm
*alrm
)
60 unsigned long rtc_alarm
, rtc_count
;
61 struct sirfsoc_rtc_drv
*rtcdrv
;
63 rtcdrv
= dev_get_drvdata(dev
);
65 spin_lock_irq(&rtcdrv
->lock
);
67 rtc_count
= sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_CN
);
69 rtc_alarm
= sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_ALARM0
);
70 memset(alrm
, 0, sizeof(struct rtc_wkalrm
));
73 * assume alarm interval not beyond one round counter overflow_rtc:
76 /* if alarm is in next overflow cycle */
77 if (rtc_count
> rtc_alarm
)
78 rtc_time_to_tm((rtcdrv
->overflow_rtc
+ 1)
79 << (BITS_PER_LONG
- RTC_SHIFT
)
80 | rtc_alarm
>> RTC_SHIFT
, &(alrm
->time
));
82 rtc_time_to_tm(rtcdrv
->overflow_rtc
83 << (BITS_PER_LONG
- RTC_SHIFT
)
84 | rtc_alarm
>> RTC_SHIFT
, &(alrm
->time
));
85 if (sirfsoc_rtc_iobrg_readl(
86 rtcdrv
->rtc_base
+ RTC_STATUS
) & SIRFSOC_RTC_AL0E
)
89 spin_unlock_irq(&rtcdrv
->lock
);
94 static int sirfsoc_rtc_set_alarm(struct device
*dev
,
95 struct rtc_wkalrm
*alrm
)
97 unsigned long rtc_status_reg
, rtc_alarm
;
98 struct sirfsoc_rtc_drv
*rtcdrv
;
99 rtcdrv
= dev_get_drvdata(dev
);
102 rtc_tm_to_time(&(alrm
->time
), &rtc_alarm
);
104 spin_lock_irq(&rtcdrv
->lock
);
106 rtc_status_reg
= sirfsoc_rtc_iobrg_readl(
107 rtcdrv
->rtc_base
+ RTC_STATUS
);
108 if (rtc_status_reg
& SIRFSOC_RTC_AL0E
) {
110 * An ongoing alarm in progress - ingore it and not
113 dev_info(dev
, "An old alarm was set, will be replaced by a new one\n");
116 sirfsoc_rtc_iobrg_writel(
117 rtc_alarm
<< RTC_SHIFT
, rtcdrv
->rtc_base
+ RTC_ALARM0
);
118 rtc_status_reg
&= ~0x07; /* mask out the lower status bits */
120 * This bit RTC_AL sets it as a wake-up source for Sleep Mode
121 * Writing 1 into this bit will clear it
123 rtc_status_reg
|= SIRFSOC_RTC_AL0
;
124 /* enable the RTC alarm interrupt */
125 rtc_status_reg
|= SIRFSOC_RTC_AL0E
;
126 sirfsoc_rtc_iobrg_writel(
127 rtc_status_reg
, rtcdrv
->rtc_base
+ RTC_STATUS
);
129 spin_unlock_irq(&rtcdrv
->lock
);
132 * if this function was called with enabled=0
133 * then it could mean that the application is
134 * trying to cancel an ongoing alarm
136 spin_lock_irq(&rtcdrv
->lock
);
138 rtc_status_reg
= sirfsoc_rtc_iobrg_readl(
139 rtcdrv
->rtc_base
+ RTC_STATUS
);
140 if (rtc_status_reg
& SIRFSOC_RTC_AL0E
) {
141 /* clear the RTC status register's alarm bit */
142 rtc_status_reg
&= ~0x07;
143 /* write 1 into SIRFSOC_RTC_AL0 to force a clear */
144 rtc_status_reg
|= (SIRFSOC_RTC_AL0
);
145 /* Clear the Alarm enable bit */
146 rtc_status_reg
&= ~(SIRFSOC_RTC_AL0E
);
148 sirfsoc_rtc_iobrg_writel(rtc_status_reg
,
149 rtcdrv
->rtc_base
+ RTC_STATUS
);
152 spin_unlock_irq(&rtcdrv
->lock
);
158 static int sirfsoc_rtc_read_time(struct device
*dev
,
161 unsigned long tmp_rtc
= 0;
162 struct sirfsoc_rtc_drv
*rtcdrv
;
163 rtcdrv
= dev_get_drvdata(dev
);
165 * This patch is taken from WinCE - Need to validate this for
166 * correctness. To work around sirfsoc RTC counter double sync logic
167 * fail, read several times to make sure get stable value.
170 tmp_rtc
= sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_CN
);
172 } while (tmp_rtc
!= sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_CN
));
174 rtc_time_to_tm(rtcdrv
->overflow_rtc
<< (BITS_PER_LONG
- RTC_SHIFT
) |
175 tmp_rtc
>> RTC_SHIFT
, tm
);
179 static int sirfsoc_rtc_set_time(struct device
*dev
,
182 unsigned long rtc_time
;
183 struct sirfsoc_rtc_drv
*rtcdrv
;
184 rtcdrv
= dev_get_drvdata(dev
);
186 rtc_tm_to_time(tm
, &rtc_time
);
188 rtcdrv
->overflow_rtc
= rtc_time
>> (BITS_PER_LONG
- RTC_SHIFT
);
190 sirfsoc_rtc_iobrg_writel(rtcdrv
->overflow_rtc
,
191 rtcdrv
->rtc_base
+ RTC_SW_VALUE
);
192 sirfsoc_rtc_iobrg_writel(
193 rtc_time
<< RTC_SHIFT
, rtcdrv
->rtc_base
+ RTC_CN
);
198 static int sirfsoc_rtc_ioctl(struct device
*dev
, unsigned int cmd
,
215 static int sirfsoc_rtc_alarm_irq_enable(struct device
*dev
,
216 unsigned int enabled
)
218 unsigned long rtc_status_reg
= 0x0;
219 struct sirfsoc_rtc_drv
*rtcdrv
;
221 rtcdrv
= dev_get_drvdata(dev
);
223 spin_lock_irq(&rtcdrv
->lock
);
225 rtc_status_reg
= sirfsoc_rtc_iobrg_readl(
226 rtcdrv
->rtc_base
+ RTC_STATUS
);
228 rtc_status_reg
|= SIRFSOC_RTC_AL0E
;
230 rtc_status_reg
&= ~SIRFSOC_RTC_AL0E
;
232 sirfsoc_rtc_iobrg_writel(rtc_status_reg
, rtcdrv
->rtc_base
+ RTC_STATUS
);
234 spin_unlock_irq(&rtcdrv
->lock
);
240 static const struct rtc_class_ops sirfsoc_rtc_ops
= {
241 .read_time
= sirfsoc_rtc_read_time
,
242 .set_time
= sirfsoc_rtc_set_time
,
243 .read_alarm
= sirfsoc_rtc_read_alarm
,
244 .set_alarm
= sirfsoc_rtc_set_alarm
,
245 .ioctl
= sirfsoc_rtc_ioctl
,
246 .alarm_irq_enable
= sirfsoc_rtc_alarm_irq_enable
249 static irqreturn_t
sirfsoc_rtc_irq_handler(int irq
, void *pdata
)
251 struct sirfsoc_rtc_drv
*rtcdrv
= pdata
;
252 unsigned long rtc_status_reg
= 0x0;
253 unsigned long events
= 0x0;
255 spin_lock(&rtcdrv
->lock
);
257 rtc_status_reg
= sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_STATUS
);
258 /* this bit will be set ONLY if an alarm was active
260 * So this is being used as an ASSERT
262 if (rtc_status_reg
& SIRFSOC_RTC_AL0
) {
264 * clear the RTC status register's alarm bit
265 * mask out the lower status bits
267 rtc_status_reg
&= ~0x07;
268 /* write 1 into SIRFSOC_RTC_AL0 to ACK the alarm interrupt */
269 rtc_status_reg
|= (SIRFSOC_RTC_AL0
);
270 /* Clear the Alarm enable bit */
271 rtc_status_reg
&= ~(SIRFSOC_RTC_AL0E
);
273 sirfsoc_rtc_iobrg_writel(rtc_status_reg
, rtcdrv
->rtc_base
+ RTC_STATUS
);
275 spin_unlock(&rtcdrv
->lock
);
277 /* this should wake up any apps polling/waiting on the read
278 * after setting the alarm
280 events
|= RTC_IRQF
| RTC_AF
;
281 rtc_update_irq(rtcdrv
->rtc
, 1, events
);
286 static const struct of_device_id sirfsoc_rtc_of_match
[] = {
287 { .compatible
= "sirf,prima2-sysrtc"},
290 MODULE_DEVICE_TABLE(of
, sirfsoc_rtc_of_match
);
292 static int sirfsoc_rtc_probe(struct platform_device
*pdev
)
295 unsigned long rtc_div
;
296 struct sirfsoc_rtc_drv
*rtcdrv
;
297 struct device_node
*np
= pdev
->dev
.of_node
;
299 rtcdrv
= devm_kzalloc(&pdev
->dev
,
300 sizeof(struct sirfsoc_rtc_drv
), GFP_KERNEL
);
304 spin_lock_init(&rtcdrv
->lock
);
306 err
= of_property_read_u32(np
, "reg", &rtcdrv
->rtc_base
);
308 dev_err(&pdev
->dev
, "unable to find base address of rtc node in dtb\n");
312 platform_set_drvdata(pdev
, rtcdrv
);
314 /* Register rtc alarm as a wakeup source */
315 device_init_wakeup(&pdev
->dev
, 1);
318 * Set SYS_RTC counter in RTC_HZ HZ Units
319 * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
320 * If 16HZ, therefore RTC_DIV = 1023;
322 rtc_div
= ((32768 / RTC_HZ
) / 2) - 1;
323 sirfsoc_rtc_iobrg_writel(rtc_div
, rtcdrv
->rtc_base
+ RTC_DIV
);
326 sirfsoc_rtc_iobrg_writel(SIRFSOC_RTC_CLK
,
327 rtcdrv
->rtc_base
+ RTC_CLOCK_SWITCH
);
329 /* reset SYS RTC ALARM0 */
330 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv
->rtc_base
+ RTC_ALARM0
);
332 /* reset SYS RTC ALARM1 */
333 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv
->rtc_base
+ RTC_ALARM1
);
335 /* Restore RTC Overflow From Register After Command Reboot */
336 rtcdrv
->overflow_rtc
=
337 sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_SW_VALUE
);
339 rtcdrv
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
340 &sirfsoc_rtc_ops
, THIS_MODULE
);
341 if (IS_ERR(rtcdrv
->rtc
)) {
342 err
= PTR_ERR(rtcdrv
->rtc
);
343 dev_err(&pdev
->dev
, "can't register RTC device\n");
347 rtcdrv
->irq
= platform_get_irq(pdev
, 0);
348 err
= devm_request_irq(
351 sirfsoc_rtc_irq_handler
,
356 dev_err(&pdev
->dev
, "Unable to register for the SiRF SOC RTC IRQ\n");
363 static int sirfsoc_rtc_remove(struct platform_device
*pdev
)
365 device_init_wakeup(&pdev
->dev
, 0);
370 #ifdef CONFIG_PM_SLEEP
371 static int sirfsoc_rtc_suspend(struct device
*dev
)
373 struct sirfsoc_rtc_drv
*rtcdrv
= dev_get_drvdata(dev
);
374 rtcdrv
->overflow_rtc
=
375 sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_SW_VALUE
);
377 rtcdrv
->saved_counter
=
378 sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_CN
);
379 rtcdrv
->saved_overflow_rtc
= rtcdrv
->overflow_rtc
;
380 if (device_may_wakeup(dev
) && !enable_irq_wake(rtcdrv
->irq
))
381 rtcdrv
->irq_wake
= 1;
386 static int sirfsoc_rtc_resume(struct device
*dev
)
389 struct sirfsoc_rtc_drv
*rtcdrv
= dev_get_drvdata(dev
);
392 * if resume from snapshot and the rtc power is lost,
393 * restroe the rtc settings
395 if (SIRFSOC_RTC_CLK
!= sirfsoc_rtc_iobrg_readl(
396 rtcdrv
->rtc_base
+ RTC_CLOCK_SWITCH
)) {
399 sirfsoc_rtc_iobrg_writel(SIRFSOC_RTC_CLK
,
400 rtcdrv
->rtc_base
+ RTC_CLOCK_SWITCH
);
402 * Set SYS_RTC counter in RTC_HZ HZ Units
403 * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
404 * If 16HZ, therefore RTC_DIV = 1023;
406 rtc_div
= ((32768 / RTC_HZ
) / 2) - 1;
408 sirfsoc_rtc_iobrg_writel(rtc_div
, rtcdrv
->rtc_base
+ RTC_DIV
);
410 /* reset SYS RTC ALARM0 */
411 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv
->rtc_base
+ RTC_ALARM0
);
413 /* reset SYS RTC ALARM1 */
414 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv
->rtc_base
+ RTC_ALARM1
);
416 rtcdrv
->overflow_rtc
= rtcdrv
->saved_overflow_rtc
;
419 * if current counter is small than previous,
420 * it means overflow in sleep
422 tmp
= sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_CN
);
423 if (tmp
<= rtcdrv
->saved_counter
)
424 rtcdrv
->overflow_rtc
++;
426 *PWRC Value Be Changed When Suspend, Restore Overflow
427 * In Memory To Register
429 sirfsoc_rtc_iobrg_writel(rtcdrv
->overflow_rtc
,
430 rtcdrv
->rtc_base
+ RTC_SW_VALUE
);
432 if (device_may_wakeup(dev
) && rtcdrv
->irq_wake
) {
433 disable_irq_wake(rtcdrv
->irq
);
434 rtcdrv
->irq_wake
= 0;
441 static SIMPLE_DEV_PM_OPS(sirfsoc_rtc_pm_ops
,
442 sirfsoc_rtc_suspend
, sirfsoc_rtc_resume
);
444 static struct platform_driver sirfsoc_rtc_driver
= {
446 .name
= "sirfsoc-rtc",
447 .pm
= &sirfsoc_rtc_pm_ops
,
448 .of_match_table
= sirfsoc_rtc_of_match
,
450 .probe
= sirfsoc_rtc_probe
,
451 .remove
= sirfsoc_rtc_remove
,
453 module_platform_driver(sirfsoc_rtc_driver
);
455 MODULE_DESCRIPTION("SiRF SoC rtc driver");
456 MODULE_AUTHOR("Xianglong Du <Xianglong.Du@csr.com>");
457 MODULE_LICENSE("GPL v2");
458 MODULE_ALIAS("platform:sirfsoc-rtc");