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 */
52 u32 saved_overflow_rtc
;
56 static int sirfsoc_rtc_read_alarm(struct device
*dev
,
57 struct rtc_wkalrm
*alrm
)
59 unsigned long rtc_alarm
, rtc_count
;
60 struct sirfsoc_rtc_drv
*rtcdrv
;
62 rtcdrv
= (struct sirfsoc_rtc_drv
*)dev_get_drvdata(dev
);
66 rtc_count
= sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_CN
);
68 rtc_alarm
= sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_ALARM0
);
69 memset(alrm
, 0, sizeof(struct rtc_wkalrm
));
72 * assume alarm interval not beyond one round counter overflow_rtc:
75 /* if alarm is in next overflow cycle */
76 if (rtc_count
> rtc_alarm
)
77 rtc_time_to_tm((rtcdrv
->overflow_rtc
+ 1)
78 << (BITS_PER_LONG
- RTC_SHIFT
)
79 | rtc_alarm
>> RTC_SHIFT
, &(alrm
->time
));
81 rtc_time_to_tm(rtcdrv
->overflow_rtc
82 << (BITS_PER_LONG
- RTC_SHIFT
)
83 | rtc_alarm
>> RTC_SHIFT
, &(alrm
->time
));
84 if (sirfsoc_rtc_iobrg_readl(
85 rtcdrv
->rtc_base
+ RTC_STATUS
) & SIRFSOC_RTC_AL0E
)
92 static int sirfsoc_rtc_set_alarm(struct device
*dev
,
93 struct rtc_wkalrm
*alrm
)
95 unsigned long rtc_status_reg
, rtc_alarm
;
96 struct sirfsoc_rtc_drv
*rtcdrv
;
97 rtcdrv
= (struct sirfsoc_rtc_drv
*)dev_get_drvdata(dev
);
100 rtc_tm_to_time(&(alrm
->time
), &rtc_alarm
);
104 rtc_status_reg
= sirfsoc_rtc_iobrg_readl(
105 rtcdrv
->rtc_base
+ RTC_STATUS
);
106 if (rtc_status_reg
& SIRFSOC_RTC_AL0E
) {
108 * An ongoing alarm in progress - ingore it and not
111 dev_info(dev
, "An old alarm was set, will be replaced by a new one\n");
114 sirfsoc_rtc_iobrg_writel(
115 rtc_alarm
<< RTC_SHIFT
, rtcdrv
->rtc_base
+ RTC_ALARM0
);
116 rtc_status_reg
&= ~0x07; /* mask out the lower status bits */
118 * This bit RTC_AL sets it as a wake-up source for Sleep Mode
119 * Writing 1 into this bit will clear it
121 rtc_status_reg
|= SIRFSOC_RTC_AL0
;
122 /* enable the RTC alarm interrupt */
123 rtc_status_reg
|= SIRFSOC_RTC_AL0E
;
124 sirfsoc_rtc_iobrg_writel(
125 rtc_status_reg
, rtcdrv
->rtc_base
+ RTC_STATUS
);
129 * if this function was called with enabled=0
130 * then it could mean that the application is
131 * trying to cancel an ongoing alarm
135 rtc_status_reg
= sirfsoc_rtc_iobrg_readl(
136 rtcdrv
->rtc_base
+ RTC_STATUS
);
137 if (rtc_status_reg
& SIRFSOC_RTC_AL0E
) {
138 /* clear the RTC status register's alarm bit */
139 rtc_status_reg
&= ~0x07;
140 /* write 1 into SIRFSOC_RTC_AL0 to force a clear */
141 rtc_status_reg
|= (SIRFSOC_RTC_AL0
);
142 /* Clear the Alarm enable bit */
143 rtc_status_reg
&= ~(SIRFSOC_RTC_AL0E
);
145 sirfsoc_rtc_iobrg_writel(rtc_status_reg
,
146 rtcdrv
->rtc_base
+ RTC_STATUS
);
155 static int sirfsoc_rtc_read_time(struct device
*dev
,
158 unsigned long tmp_rtc
= 0;
159 struct sirfsoc_rtc_drv
*rtcdrv
;
160 rtcdrv
= (struct sirfsoc_rtc_drv
*)dev_get_drvdata(dev
);
162 * This patch is taken from WinCE - Need to validate this for
163 * correctness. To work around sirfsoc RTC counter double sync logic
164 * fail, read several times to make sure get stable value.
167 tmp_rtc
= sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_CN
);
169 } while (tmp_rtc
!= sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_CN
));
171 rtc_time_to_tm(rtcdrv
->overflow_rtc
<< (BITS_PER_LONG
- RTC_SHIFT
) |
172 tmp_rtc
>> RTC_SHIFT
, tm
);
176 static int sirfsoc_rtc_set_time(struct device
*dev
,
179 unsigned long rtc_time
;
180 struct sirfsoc_rtc_drv
*rtcdrv
;
181 rtcdrv
= (struct sirfsoc_rtc_drv
*)dev_get_drvdata(dev
);
183 rtc_tm_to_time(tm
, &rtc_time
);
185 rtcdrv
->overflow_rtc
= rtc_time
>> (BITS_PER_LONG
- RTC_SHIFT
);
187 sirfsoc_rtc_iobrg_writel(rtcdrv
->overflow_rtc
,
188 rtcdrv
->rtc_base
+ RTC_SW_VALUE
);
189 sirfsoc_rtc_iobrg_writel(
190 rtc_time
<< RTC_SHIFT
, rtcdrv
->rtc_base
+ RTC_CN
);
195 static int sirfsoc_rtc_ioctl(struct device
*dev
, unsigned int cmd
,
212 static const struct rtc_class_ops sirfsoc_rtc_ops
= {
213 .read_time
= sirfsoc_rtc_read_time
,
214 .set_time
= sirfsoc_rtc_set_time
,
215 .read_alarm
= sirfsoc_rtc_read_alarm
,
216 .set_alarm
= sirfsoc_rtc_set_alarm
,
217 .ioctl
= sirfsoc_rtc_ioctl
220 static irqreturn_t
sirfsoc_rtc_irq_handler(int irq
, void *pdata
)
222 struct sirfsoc_rtc_drv
*rtcdrv
= pdata
;
223 unsigned long rtc_status_reg
= 0x0;
224 unsigned long events
= 0x0;
226 rtc_status_reg
= sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_STATUS
);
227 /* this bit will be set ONLY if an alarm was active
229 * So this is being used as an ASSERT
231 if (rtc_status_reg
& SIRFSOC_RTC_AL0
) {
233 * clear the RTC status register's alarm bit
234 * mask out the lower status bits
236 rtc_status_reg
&= ~0x07;
237 /* write 1 into SIRFSOC_RTC_AL0 to ACK the alarm interrupt */
238 rtc_status_reg
|= (SIRFSOC_RTC_AL0
);
239 /* Clear the Alarm enable bit */
240 rtc_status_reg
&= ~(SIRFSOC_RTC_AL0E
);
242 sirfsoc_rtc_iobrg_writel(rtc_status_reg
, rtcdrv
->rtc_base
+ RTC_STATUS
);
243 /* this should wake up any apps polling/waiting on the read
244 * after setting the alarm
246 events
|= RTC_IRQF
| RTC_AF
;
247 rtc_update_irq(rtcdrv
->rtc
, 1, events
);
252 static const struct of_device_id sirfsoc_rtc_of_match
[] = {
253 { .compatible
= "sirf,prima2-sysrtc"},
256 MODULE_DEVICE_TABLE(of
, sirfsoc_rtc_of_match
);
258 static int sirfsoc_rtc_probe(struct platform_device
*pdev
)
261 unsigned long rtc_div
;
262 struct sirfsoc_rtc_drv
*rtcdrv
;
263 struct device_node
*np
= pdev
->dev
.of_node
;
265 rtcdrv
= devm_kzalloc(&pdev
->dev
,
266 sizeof(struct sirfsoc_rtc_drv
), GFP_KERNEL
);
267 if (rtcdrv
== NULL
) {
269 "%s: can't alloc mem for drv struct\n",
274 err
= of_property_read_u32(np
, "reg", &rtcdrv
->rtc_base
);
276 dev_err(&pdev
->dev
, "unable to find base address of rtc node in dtb\n");
280 platform_set_drvdata(pdev
, rtcdrv
);
282 /* Register rtc alarm as a wakeup source */
283 device_init_wakeup(&pdev
->dev
, 1);
286 * Set SYS_RTC counter in RTC_HZ HZ Units
287 * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
288 * If 16HZ, therefore RTC_DIV = 1023;
290 rtc_div
= ((32768 / RTC_HZ
) / 2) - 1;
291 sirfsoc_rtc_iobrg_writel(rtc_div
, rtcdrv
->rtc_base
+ RTC_DIV
);
293 rtcdrv
->rtc
= rtc_device_register(pdev
->name
, &(pdev
->dev
),
294 &sirfsoc_rtc_ops
, THIS_MODULE
);
295 if (IS_ERR(rtcdrv
->rtc
)) {
296 err
= PTR_ERR(rtcdrv
->rtc
);
297 dev_err(&pdev
->dev
, "can't register RTC device\n");
302 sirfsoc_rtc_iobrg_writel(SIRFSOC_RTC_CLK
,
303 rtcdrv
->rtc_base
+ RTC_CLOCK_SWITCH
);
305 /* reset SYS RTC ALARM0 */
306 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv
->rtc_base
+ RTC_ALARM0
);
308 /* reset SYS RTC ALARM1 */
309 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv
->rtc_base
+ RTC_ALARM1
);
311 /* Restore RTC Overflow From Register After Command Reboot */
312 rtcdrv
->overflow_rtc
=
313 sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_SW_VALUE
);
315 rtcdrv
->irq
= platform_get_irq(pdev
, 0);
316 err
= devm_request_irq(
319 sirfsoc_rtc_irq_handler
,
324 dev_err(&pdev
->dev
, "Unable to register for the SiRF SOC RTC IRQ\n");
332 rtc_device_unregister(rtcdrv
->rtc
);
337 static int sirfsoc_rtc_remove(struct platform_device
*pdev
)
339 struct sirfsoc_rtc_drv
*rtcdrv
= platform_get_drvdata(pdev
);
341 device_init_wakeup(&pdev
->dev
, 0);
342 rtc_device_unregister(rtcdrv
->rtc
);
349 static int sirfsoc_rtc_suspend(struct device
*dev
)
351 struct platform_device
*pdev
= to_platform_device(dev
);
352 struct sirfsoc_rtc_drv
*rtcdrv
= platform_get_drvdata(pdev
);
353 rtcdrv
->overflow_rtc
=
354 sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_SW_VALUE
);
356 rtcdrv
->saved_counter
=
357 sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_CN
);
358 rtcdrv
->saved_overflow_rtc
= rtcdrv
->overflow_rtc
;
359 if (device_may_wakeup(&pdev
->dev
) && !enable_irq_wake(rtcdrv
->irq
))
360 rtcdrv
->irq_wake
= 1;
365 static int sirfsoc_rtc_freeze(struct device
*dev
)
367 sirfsoc_rtc_suspend(dev
);
372 static int sirfsoc_rtc_thaw(struct device
*dev
)
375 struct sirfsoc_rtc_drv
*rtcdrv
;
376 rtcdrv
= (struct sirfsoc_rtc_drv
*)dev_get_drvdata(dev
);
379 * if resume from snapshot and the rtc power is losed,
380 * restroe the rtc settings
382 if (SIRFSOC_RTC_CLK
!= sirfsoc_rtc_iobrg_readl(
383 rtcdrv
->rtc_base
+ RTC_CLOCK_SWITCH
)) {
386 sirfsoc_rtc_iobrg_writel(SIRFSOC_RTC_CLK
,
387 rtcdrv
->rtc_base
+ RTC_CLOCK_SWITCH
);
389 * Set SYS_RTC counter in RTC_HZ HZ Units
390 * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
391 * If 16HZ, therefore RTC_DIV = 1023;
393 rtc_div
= ((32768 / RTC_HZ
) / 2) - 1;
395 sirfsoc_rtc_iobrg_writel(rtc_div
, rtcdrv
->rtc_base
+ RTC_DIV
);
397 /* reset SYS RTC ALARM0 */
398 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv
->rtc_base
+ RTC_ALARM0
);
400 /* reset SYS RTC ALARM1 */
401 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv
->rtc_base
+ RTC_ALARM1
);
403 rtcdrv
->overflow_rtc
= rtcdrv
->saved_overflow_rtc
;
406 * if current counter is small than previous,
407 * it means overflow in sleep
409 tmp
= sirfsoc_rtc_iobrg_readl(rtcdrv
->rtc_base
+ RTC_CN
);
410 if (tmp
<= rtcdrv
->saved_counter
)
411 rtcdrv
->overflow_rtc
++;
413 *PWRC Value Be Changed When Suspend, Restore Overflow
414 * In Memory To Register
416 sirfsoc_rtc_iobrg_writel(rtcdrv
->overflow_rtc
,
417 rtcdrv
->rtc_base
+ RTC_SW_VALUE
);
422 static int sirfsoc_rtc_resume(struct device
*dev
)
424 struct platform_device
*pdev
= to_platform_device(dev
);
425 struct sirfsoc_rtc_drv
*rtcdrv
= platform_get_drvdata(pdev
);
426 sirfsoc_rtc_thaw(dev
);
427 if (device_may_wakeup(&pdev
->dev
) && rtcdrv
->irq_wake
) {
428 disable_irq_wake(rtcdrv
->irq
);
429 rtcdrv
->irq_wake
= 0;
435 static int sirfsoc_rtc_restore(struct device
*dev
)
437 struct platform_device
*pdev
= to_platform_device(dev
);
438 struct sirfsoc_rtc_drv
*rtcdrv
= platform_get_drvdata(pdev
);
440 if (device_may_wakeup(&pdev
->dev
) && rtcdrv
->irq_wake
) {
441 disable_irq_wake(rtcdrv
->irq
);
442 rtcdrv
->irq_wake
= 0;
448 #define sirfsoc_rtc_suspend NULL
449 #define sirfsoc_rtc_resume NULL
450 #define sirfsoc_rtc_freeze NULL
451 #define sirfsoc_rtc_thaw NULL
452 #define sirfsoc_rtc_restore NULL
455 static const struct dev_pm_ops sirfsoc_rtc_pm_ops
= {
456 .suspend
= sirfsoc_rtc_suspend
,
457 .resume
= sirfsoc_rtc_resume
,
458 .freeze
= sirfsoc_rtc_freeze
,
459 .thaw
= sirfsoc_rtc_thaw
,
460 .restore
= sirfsoc_rtc_restore
,
463 static struct platform_driver sirfsoc_rtc_driver
= {
465 .name
= "sirfsoc-rtc",
466 .owner
= THIS_MODULE
,
468 .pm
= &sirfsoc_rtc_pm_ops
,
470 .of_match_table
= of_match_ptr(sirfsoc_rtc_of_match
),
472 .probe
= sirfsoc_rtc_probe
,
473 .remove
= sirfsoc_rtc_remove
,
475 module_platform_driver(sirfsoc_rtc_driver
);
477 MODULE_DESCRIPTION("SiRF SoC rtc driver");
478 MODULE_AUTHOR("Xianglong Du <Xianglong.Du@csr.com>");
479 MODULE_LICENSE("GPL v2");
480 MODULE_ALIAS("platform:sirfsoc-rtc");