1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SiRFSoC Real Time Clock interface for Linux
5 * Copyright (c) 2013 Cambridge Silicon Radio Limited, a CSR plc group company.
8 #include <linux/module.h>
10 #include <linux/rtc.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
15 #include <linux/regmap.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 */
51 struct regmap
*regmap
;
54 u32 saved_overflow_rtc
;
58 static u32
sirfsoc_rtc_readl(struct sirfsoc_rtc_drv
*rtcdrv
, u32 offset
)
62 regmap_read(rtcdrv
->regmap
, rtcdrv
->rtc_base
+ offset
, &val
);
66 static void sirfsoc_rtc_writel(struct sirfsoc_rtc_drv
*rtcdrv
,
69 regmap_write(rtcdrv
->regmap
, rtcdrv
->rtc_base
+ offset
, val
);
72 static int sirfsoc_rtc_read_alarm(struct device
*dev
,
73 struct rtc_wkalrm
*alrm
)
75 unsigned long rtc_alarm
, rtc_count
;
76 struct sirfsoc_rtc_drv
*rtcdrv
;
78 rtcdrv
= dev_get_drvdata(dev
);
80 spin_lock_irq(&rtcdrv
->lock
);
82 rtc_count
= sirfsoc_rtc_readl(rtcdrv
, RTC_CN
);
84 rtc_alarm
= sirfsoc_rtc_readl(rtcdrv
, RTC_ALARM0
);
85 memset(alrm
, 0, sizeof(struct rtc_wkalrm
));
88 * assume alarm interval not beyond one round counter overflow_rtc:
91 /* if alarm is in next overflow cycle */
92 if (rtc_count
> rtc_alarm
)
93 rtc_time_to_tm((rtcdrv
->overflow_rtc
+ 1)
94 << (BITS_PER_LONG
- RTC_SHIFT
)
95 | rtc_alarm
>> RTC_SHIFT
, &(alrm
->time
));
97 rtc_time_to_tm(rtcdrv
->overflow_rtc
98 << (BITS_PER_LONG
- RTC_SHIFT
)
99 | rtc_alarm
>> RTC_SHIFT
, &(alrm
->time
));
100 if (sirfsoc_rtc_readl(rtcdrv
, RTC_STATUS
) & SIRFSOC_RTC_AL0E
)
103 spin_unlock_irq(&rtcdrv
->lock
);
108 static int sirfsoc_rtc_set_alarm(struct device
*dev
,
109 struct rtc_wkalrm
*alrm
)
111 unsigned long rtc_status_reg
, rtc_alarm
;
112 struct sirfsoc_rtc_drv
*rtcdrv
;
113 rtcdrv
= dev_get_drvdata(dev
);
116 rtc_tm_to_time(&(alrm
->time
), &rtc_alarm
);
118 spin_lock_irq(&rtcdrv
->lock
);
120 rtc_status_reg
= sirfsoc_rtc_readl(rtcdrv
, RTC_STATUS
);
121 if (rtc_status_reg
& SIRFSOC_RTC_AL0E
) {
123 * An ongoing alarm in progress - ingore it and not
126 dev_info(dev
, "An old alarm was set, will be replaced by a new one\n");
129 sirfsoc_rtc_writel(rtcdrv
, RTC_ALARM0
, rtc_alarm
<< RTC_SHIFT
);
130 rtc_status_reg
&= ~0x07; /* mask out the lower status bits */
132 * This bit RTC_AL sets it as a wake-up source for Sleep Mode
133 * Writing 1 into this bit will clear it
135 rtc_status_reg
|= SIRFSOC_RTC_AL0
;
136 /* enable the RTC alarm interrupt */
137 rtc_status_reg
|= SIRFSOC_RTC_AL0E
;
138 sirfsoc_rtc_writel(rtcdrv
, RTC_STATUS
, rtc_status_reg
);
140 spin_unlock_irq(&rtcdrv
->lock
);
143 * if this function was called with enabled=0
144 * then it could mean that the application is
145 * trying to cancel an ongoing alarm
147 spin_lock_irq(&rtcdrv
->lock
);
149 rtc_status_reg
= sirfsoc_rtc_readl(rtcdrv
, RTC_STATUS
);
150 if (rtc_status_reg
& SIRFSOC_RTC_AL0E
) {
151 /* clear the RTC status register's alarm bit */
152 rtc_status_reg
&= ~0x07;
153 /* write 1 into SIRFSOC_RTC_AL0 to force a clear */
154 rtc_status_reg
|= (SIRFSOC_RTC_AL0
);
155 /* Clear the Alarm enable bit */
156 rtc_status_reg
&= ~(SIRFSOC_RTC_AL0E
);
158 sirfsoc_rtc_writel(rtcdrv
, RTC_STATUS
,
162 spin_unlock_irq(&rtcdrv
->lock
);
168 static int sirfsoc_rtc_read_time(struct device
*dev
,
171 unsigned long tmp_rtc
= 0;
172 struct sirfsoc_rtc_drv
*rtcdrv
;
173 rtcdrv
= dev_get_drvdata(dev
);
175 * This patch is taken from WinCE - Need to validate this for
176 * correctness. To work around sirfsoc RTC counter double sync logic
177 * fail, read several times to make sure get stable value.
180 tmp_rtc
= sirfsoc_rtc_readl(rtcdrv
, RTC_CN
);
182 } while (tmp_rtc
!= sirfsoc_rtc_readl(rtcdrv
, RTC_CN
));
184 rtc_time_to_tm(rtcdrv
->overflow_rtc
<< (BITS_PER_LONG
- RTC_SHIFT
) |
185 tmp_rtc
>> RTC_SHIFT
, tm
);
189 static int sirfsoc_rtc_set_time(struct device
*dev
,
192 unsigned long rtc_time
;
193 struct sirfsoc_rtc_drv
*rtcdrv
;
194 rtcdrv
= dev_get_drvdata(dev
);
196 rtc_tm_to_time(tm
, &rtc_time
);
198 rtcdrv
->overflow_rtc
= rtc_time
>> (BITS_PER_LONG
- RTC_SHIFT
);
200 sirfsoc_rtc_writel(rtcdrv
, RTC_SW_VALUE
, rtcdrv
->overflow_rtc
);
201 sirfsoc_rtc_writel(rtcdrv
, RTC_CN
, rtc_time
<< RTC_SHIFT
);
206 static int sirfsoc_rtc_alarm_irq_enable(struct device
*dev
,
207 unsigned int enabled
)
209 unsigned long rtc_status_reg
= 0x0;
210 struct sirfsoc_rtc_drv
*rtcdrv
;
212 rtcdrv
= dev_get_drvdata(dev
);
214 spin_lock_irq(&rtcdrv
->lock
);
216 rtc_status_reg
= sirfsoc_rtc_readl(rtcdrv
, RTC_STATUS
);
218 rtc_status_reg
|= SIRFSOC_RTC_AL0E
;
220 rtc_status_reg
&= ~SIRFSOC_RTC_AL0E
;
222 sirfsoc_rtc_writel(rtcdrv
, RTC_STATUS
, rtc_status_reg
);
224 spin_unlock_irq(&rtcdrv
->lock
);
230 static const struct rtc_class_ops sirfsoc_rtc_ops
= {
231 .read_time
= sirfsoc_rtc_read_time
,
232 .set_time
= sirfsoc_rtc_set_time
,
233 .read_alarm
= sirfsoc_rtc_read_alarm
,
234 .set_alarm
= sirfsoc_rtc_set_alarm
,
235 .alarm_irq_enable
= sirfsoc_rtc_alarm_irq_enable
238 static irqreturn_t
sirfsoc_rtc_irq_handler(int irq
, void *pdata
)
240 struct sirfsoc_rtc_drv
*rtcdrv
= pdata
;
241 unsigned long rtc_status_reg
= 0x0;
242 unsigned long events
= 0x0;
244 spin_lock(&rtcdrv
->lock
);
246 rtc_status_reg
= sirfsoc_rtc_readl(rtcdrv
, RTC_STATUS
);
247 /* this bit will be set ONLY if an alarm was active
249 * So this is being used as an ASSERT
251 if (rtc_status_reg
& SIRFSOC_RTC_AL0
) {
253 * clear the RTC status register's alarm bit
254 * mask out the lower status bits
256 rtc_status_reg
&= ~0x07;
257 /* write 1 into SIRFSOC_RTC_AL0 to ACK the alarm interrupt */
258 rtc_status_reg
|= (SIRFSOC_RTC_AL0
);
259 /* Clear the Alarm enable bit */
260 rtc_status_reg
&= ~(SIRFSOC_RTC_AL0E
);
263 sirfsoc_rtc_writel(rtcdrv
, RTC_STATUS
, rtc_status_reg
);
265 spin_unlock(&rtcdrv
->lock
);
267 /* this should wake up any apps polling/waiting on the read
268 * after setting the alarm
270 events
|= RTC_IRQF
| RTC_AF
;
271 rtc_update_irq(rtcdrv
->rtc
, 1, events
);
276 static const struct of_device_id sirfsoc_rtc_of_match
[] = {
277 { .compatible
= "sirf,prima2-sysrtc"},
281 static const struct regmap_config sysrtc_regmap_config
= {
287 MODULE_DEVICE_TABLE(of
, sirfsoc_rtc_of_match
);
289 static int sirfsoc_rtc_probe(struct platform_device
*pdev
)
292 unsigned long rtc_div
;
293 struct sirfsoc_rtc_drv
*rtcdrv
;
294 struct device_node
*np
= pdev
->dev
.of_node
;
296 rtcdrv
= devm_kzalloc(&pdev
->dev
,
297 sizeof(struct sirfsoc_rtc_drv
), GFP_KERNEL
);
301 spin_lock_init(&rtcdrv
->lock
);
303 err
= of_property_read_u32(np
, "reg", &rtcdrv
->rtc_base
);
305 dev_err(&pdev
->dev
, "unable to find base address of rtc node in dtb\n");
309 platform_set_drvdata(pdev
, rtcdrv
);
311 /* Register rtc alarm as a wakeup source */
312 device_init_wakeup(&pdev
->dev
, 1);
314 rtcdrv
->regmap
= devm_regmap_init_iobg(&pdev
->dev
,
315 &sysrtc_regmap_config
);
316 if (IS_ERR(rtcdrv
->regmap
)) {
317 err
= PTR_ERR(rtcdrv
->regmap
);
318 dev_err(&pdev
->dev
, "Failed to allocate register map: %d\n",
324 * Set SYS_RTC counter in RTC_HZ HZ Units
325 * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
326 * If 16HZ, therefore RTC_DIV = 1023;
328 rtc_div
= ((32768 / RTC_HZ
) / 2) - 1;
329 sirfsoc_rtc_writel(rtcdrv
, RTC_DIV
, rtc_div
);
332 sirfsoc_rtc_writel(rtcdrv
, RTC_CLOCK_SWITCH
, SIRFSOC_RTC_CLK
);
334 /* reset SYS RTC ALARM0 */
335 sirfsoc_rtc_writel(rtcdrv
, RTC_ALARM0
, 0x0);
337 /* reset SYS RTC ALARM1 */
338 sirfsoc_rtc_writel(rtcdrv
, RTC_ALARM1
, 0x0);
340 /* Restore RTC Overflow From Register After Command Reboot */
341 rtcdrv
->overflow_rtc
=
342 sirfsoc_rtc_readl(rtcdrv
, RTC_SW_VALUE
);
344 rtcdrv
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
345 &sirfsoc_rtc_ops
, THIS_MODULE
);
346 if (IS_ERR(rtcdrv
->rtc
)) {
347 err
= PTR_ERR(rtcdrv
->rtc
);
348 dev_err(&pdev
->dev
, "can't register RTC device\n");
352 rtcdrv
->irq
= platform_get_irq(pdev
, 0);
353 err
= devm_request_irq(
356 sirfsoc_rtc_irq_handler
,
361 dev_err(&pdev
->dev
, "Unable to register for the SiRF SOC RTC IRQ\n");
368 #ifdef CONFIG_PM_SLEEP
369 static int sirfsoc_rtc_suspend(struct device
*dev
)
371 struct sirfsoc_rtc_drv
*rtcdrv
= dev_get_drvdata(dev
);
372 rtcdrv
->overflow_rtc
=
373 sirfsoc_rtc_readl(rtcdrv
, RTC_SW_VALUE
);
375 rtcdrv
->saved_counter
=
376 sirfsoc_rtc_readl(rtcdrv
, RTC_CN
);
377 rtcdrv
->saved_overflow_rtc
= rtcdrv
->overflow_rtc
;
378 if (device_may_wakeup(dev
) && !enable_irq_wake(rtcdrv
->irq
))
379 rtcdrv
->irq_wake
= 1;
384 static int sirfsoc_rtc_resume(struct device
*dev
)
387 struct sirfsoc_rtc_drv
*rtcdrv
= dev_get_drvdata(dev
);
390 * if resume from snapshot and the rtc power is lost,
391 * restroe the rtc settings
393 if (SIRFSOC_RTC_CLK
!= sirfsoc_rtc_readl(rtcdrv
, RTC_CLOCK_SWITCH
)) {
396 sirfsoc_rtc_writel(rtcdrv
, RTC_CLOCK_SWITCH
, SIRFSOC_RTC_CLK
);
398 * Set SYS_RTC counter in RTC_HZ HZ Units
399 * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
400 * If 16HZ, therefore RTC_DIV = 1023;
402 rtc_div
= ((32768 / RTC_HZ
) / 2) - 1;
404 sirfsoc_rtc_writel(rtcdrv
, RTC_DIV
, rtc_div
);
406 /* reset SYS RTC ALARM0 */
407 sirfsoc_rtc_writel(rtcdrv
, RTC_ALARM0
, 0x0);
409 /* reset SYS RTC ALARM1 */
410 sirfsoc_rtc_writel(rtcdrv
, RTC_ALARM1
, 0x0);
412 rtcdrv
->overflow_rtc
= rtcdrv
->saved_overflow_rtc
;
415 * if current counter is small than previous,
416 * it means overflow in sleep
418 tmp
= sirfsoc_rtc_readl(rtcdrv
, RTC_CN
);
419 if (tmp
<= rtcdrv
->saved_counter
)
420 rtcdrv
->overflow_rtc
++;
422 *PWRC Value Be Changed When Suspend, Restore Overflow
423 * In Memory To Register
425 sirfsoc_rtc_writel(rtcdrv
, RTC_SW_VALUE
, rtcdrv
->overflow_rtc
);
427 if (device_may_wakeup(dev
) && rtcdrv
->irq_wake
) {
428 disable_irq_wake(rtcdrv
->irq
);
429 rtcdrv
->irq_wake
= 0;
436 static SIMPLE_DEV_PM_OPS(sirfsoc_rtc_pm_ops
,
437 sirfsoc_rtc_suspend
, sirfsoc_rtc_resume
);
439 static struct platform_driver sirfsoc_rtc_driver
= {
441 .name
= "sirfsoc-rtc",
442 .pm
= &sirfsoc_rtc_pm_ops
,
443 .of_match_table
= sirfsoc_rtc_of_match
,
445 .probe
= sirfsoc_rtc_probe
,
447 module_platform_driver(sirfsoc_rtc_driver
);
449 MODULE_DESCRIPTION("SiRF SoC rtc driver");
450 MODULE_AUTHOR("Xianglong Du <Xianglong.Du@csr.com>");
451 MODULE_LICENSE("GPL v2");
452 MODULE_ALIAS("platform:sirfsoc-rtc");