2 * Copyright (C) 2011-2012 Freescale Semiconductor, Inc.
4 * The code contained herein is licensed under the GNU General Public
5 * License. You may obtain a copy of the GNU General Public License
6 * Version 2 or later at the following locations:
8 * http://www.opensource.org/licenses/gpl-license.html
9 * http://www.gnu.org/copyleft/gpl.html
12 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/rtc.h>
20 #include <linux/clk.h>
22 /* These register offsets are relative to LP (Low Power) range */
23 #define SNVS_LPCR 0x04
24 #define SNVS_LPSR 0x18
25 #define SNVS_LPSRTCMR 0x1c
26 #define SNVS_LPSRTCLR 0x20
27 #define SNVS_LPTAR 0x24
28 #define SNVS_LPPGDR 0x30
30 #define SNVS_LPCR_SRTC_ENV (1 << 0)
31 #define SNVS_LPCR_LPTA_EN (1 << 1)
32 #define SNVS_LPCR_LPWUI_EN (1 << 3)
33 #define SNVS_LPSR_LPTA (1 << 0)
35 #define SNVS_LPPGDR_INIT 0x41736166
36 #define CNTR_TO_SECS_SH 15
38 struct snvs_rtc_data
{
39 struct rtc_device
*rtc
;
46 static u32
rtc_read_lp_counter(void __iomem
*ioaddr
)
51 read1
= readl(ioaddr
+ SNVS_LPSRTCMR
);
53 read1
|= readl(ioaddr
+ SNVS_LPSRTCLR
);
55 read2
= readl(ioaddr
+ SNVS_LPSRTCMR
);
57 read2
|= readl(ioaddr
+ SNVS_LPSRTCLR
);
58 } while (read1
!= read2
);
60 /* Convert 47-bit counter to 32-bit raw second count */
61 return (u32
) (read1
>> CNTR_TO_SECS_SH
);
64 static void rtc_write_sync_lp(void __iomem
*ioaddr
)
66 u32 count1
, count2
, count3
;
69 /* Wait for 3 CKIL cycles */
70 for (i
= 0; i
< 3; i
++) {
72 count1
= readl(ioaddr
+ SNVS_LPSRTCLR
);
73 count2
= readl(ioaddr
+ SNVS_LPSRTCLR
);
74 } while (count1
!= count2
);
76 /* Now wait until counter value changes */
79 count2
= readl(ioaddr
+ SNVS_LPSRTCLR
);
80 count3
= readl(ioaddr
+ SNVS_LPSRTCLR
);
81 } while (count2
!= count3
);
82 } while (count3
== count1
);
86 static int snvs_rtc_enable(struct snvs_rtc_data
*data
, bool enable
)
92 spin_lock_irqsave(&data
->lock
, flags
);
94 lpcr
= readl(data
->ioaddr
+ SNVS_LPCR
);
96 lpcr
|= SNVS_LPCR_SRTC_ENV
;
98 lpcr
&= ~SNVS_LPCR_SRTC_ENV
;
99 writel(lpcr
, data
->ioaddr
+ SNVS_LPCR
);
101 spin_unlock_irqrestore(&data
->lock
, flags
);
104 lpcr
= readl(data
->ioaddr
+ SNVS_LPCR
);
107 if (lpcr
& SNVS_LPCR_SRTC_ENV
)
110 if (!(lpcr
& SNVS_LPCR_SRTC_ENV
))
121 static int snvs_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
123 struct snvs_rtc_data
*data
= dev_get_drvdata(dev
);
124 unsigned long time
= rtc_read_lp_counter(data
->ioaddr
);
126 rtc_time_to_tm(time
, tm
);
131 static int snvs_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
133 struct snvs_rtc_data
*data
= dev_get_drvdata(dev
);
136 rtc_tm_to_time(tm
, &time
);
138 /* Disable RTC first */
139 snvs_rtc_enable(data
, false);
141 /* Write 32-bit time to 47-bit timer, leaving 15 LSBs blank */
142 writel(time
<< CNTR_TO_SECS_SH
, data
->ioaddr
+ SNVS_LPSRTCLR
);
143 writel(time
>> (32 - CNTR_TO_SECS_SH
), data
->ioaddr
+ SNVS_LPSRTCMR
);
145 /* Enable RTC again */
146 snvs_rtc_enable(data
, true);
151 static int snvs_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
153 struct snvs_rtc_data
*data
= dev_get_drvdata(dev
);
156 lptar
= readl(data
->ioaddr
+ SNVS_LPTAR
);
157 rtc_time_to_tm(lptar
, &alrm
->time
);
159 lpsr
= readl(data
->ioaddr
+ SNVS_LPSR
);
160 alrm
->pending
= (lpsr
& SNVS_LPSR_LPTA
) ? 1 : 0;
165 static int snvs_rtc_alarm_irq_enable(struct device
*dev
, unsigned int enable
)
167 struct snvs_rtc_data
*data
= dev_get_drvdata(dev
);
171 spin_lock_irqsave(&data
->lock
, flags
);
173 lpcr
= readl(data
->ioaddr
+ SNVS_LPCR
);
175 lpcr
|= (SNVS_LPCR_LPTA_EN
| SNVS_LPCR_LPWUI_EN
);
177 lpcr
&= ~(SNVS_LPCR_LPTA_EN
| SNVS_LPCR_LPWUI_EN
);
178 writel(lpcr
, data
->ioaddr
+ SNVS_LPCR
);
180 spin_unlock_irqrestore(&data
->lock
, flags
);
182 rtc_write_sync_lp(data
->ioaddr
);
187 static int snvs_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
189 struct snvs_rtc_data
*data
= dev_get_drvdata(dev
);
190 struct rtc_time
*alrm_tm
= &alrm
->time
;
195 rtc_tm_to_time(alrm_tm
, &time
);
197 spin_lock_irqsave(&data
->lock
, flags
);
199 /* Have to clear LPTA_EN before programming new alarm time in LPTAR */
200 lpcr
= readl(data
->ioaddr
+ SNVS_LPCR
);
201 lpcr
&= ~SNVS_LPCR_LPTA_EN
;
202 writel(lpcr
, data
->ioaddr
+ SNVS_LPCR
);
204 spin_unlock_irqrestore(&data
->lock
, flags
);
206 writel(time
, data
->ioaddr
+ SNVS_LPTAR
);
208 /* Clear alarm interrupt status bit */
209 writel(SNVS_LPSR_LPTA
, data
->ioaddr
+ SNVS_LPSR
);
211 return snvs_rtc_alarm_irq_enable(dev
, alrm
->enabled
);
214 static const struct rtc_class_ops snvs_rtc_ops
= {
215 .read_time
= snvs_rtc_read_time
,
216 .set_time
= snvs_rtc_set_time
,
217 .read_alarm
= snvs_rtc_read_alarm
,
218 .set_alarm
= snvs_rtc_set_alarm
,
219 .alarm_irq_enable
= snvs_rtc_alarm_irq_enable
,
222 static irqreturn_t
snvs_rtc_irq_handler(int irq
, void *dev_id
)
224 struct device
*dev
= dev_id
;
225 struct snvs_rtc_data
*data
= dev_get_drvdata(dev
);
229 lpsr
= readl(data
->ioaddr
+ SNVS_LPSR
);
231 if (lpsr
& SNVS_LPSR_LPTA
) {
232 events
|= (RTC_AF
| RTC_IRQF
);
234 /* RTC alarm should be one-shot */
235 snvs_rtc_alarm_irq_enable(dev
, 0);
237 rtc_update_irq(data
->rtc
, 1, events
);
240 /* clear interrupt status */
241 writel(lpsr
, data
->ioaddr
+ SNVS_LPSR
);
243 return events
? IRQ_HANDLED
: IRQ_NONE
;
246 static int snvs_rtc_probe(struct platform_device
*pdev
)
248 struct snvs_rtc_data
*data
;
249 struct resource
*res
;
252 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
256 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
257 data
->ioaddr
= devm_ioremap_resource(&pdev
->dev
, res
);
258 if (IS_ERR(data
->ioaddr
))
259 return PTR_ERR(data
->ioaddr
);
261 data
->irq
= platform_get_irq(pdev
, 0);
265 data
->clk
= devm_clk_get(&pdev
->dev
, "snvs-rtc");
266 if (IS_ERR(data
->clk
)) {
269 ret
= clk_prepare_enable(data
->clk
);
272 "Could not prepare or enable the snvs clock\n");
277 platform_set_drvdata(pdev
, data
);
279 spin_lock_init(&data
->lock
);
281 /* Initialize glitch detect */
282 writel(SNVS_LPPGDR_INIT
, data
->ioaddr
+ SNVS_LPPGDR
);
284 /* Clear interrupt status */
285 writel(0xffffffff, data
->ioaddr
+ SNVS_LPSR
);
288 snvs_rtc_enable(data
, true);
290 device_init_wakeup(&pdev
->dev
, true);
292 ret
= devm_request_irq(&pdev
->dev
, data
->irq
, snvs_rtc_irq_handler
,
293 IRQF_SHARED
, "rtc alarm", &pdev
->dev
);
295 dev_err(&pdev
->dev
, "failed to request irq %d: %d\n",
297 goto error_rtc_device_register
;
300 data
->rtc
= devm_rtc_device_register(&pdev
->dev
, pdev
->name
,
301 &snvs_rtc_ops
, THIS_MODULE
);
302 if (IS_ERR(data
->rtc
)) {
303 ret
= PTR_ERR(data
->rtc
);
304 dev_err(&pdev
->dev
, "failed to register rtc: %d\n", ret
);
305 goto error_rtc_device_register
;
310 error_rtc_device_register
:
312 clk_disable_unprepare(data
->clk
);
317 #ifdef CONFIG_PM_SLEEP
318 static int snvs_rtc_suspend(struct device
*dev
)
320 struct snvs_rtc_data
*data
= dev_get_drvdata(dev
);
322 if (device_may_wakeup(dev
))
323 enable_irq_wake(data
->irq
);
328 static int snvs_rtc_suspend_noirq(struct device
*dev
)
330 struct snvs_rtc_data
*data
= dev_get_drvdata(dev
);
333 clk_disable_unprepare(data
->clk
);
338 static int snvs_rtc_resume(struct device
*dev
)
340 struct snvs_rtc_data
*data
= dev_get_drvdata(dev
);
342 if (device_may_wakeup(dev
))
343 return disable_irq_wake(data
->irq
);
348 static int snvs_rtc_resume_noirq(struct device
*dev
)
350 struct snvs_rtc_data
*data
= dev_get_drvdata(dev
);
353 return clk_prepare_enable(data
->clk
);
358 static const struct dev_pm_ops snvs_rtc_pm_ops
= {
359 .suspend
= snvs_rtc_suspend
,
360 .suspend_noirq
= snvs_rtc_suspend_noirq
,
361 .resume
= snvs_rtc_resume
,
362 .resume_noirq
= snvs_rtc_resume_noirq
,
365 #define SNVS_RTC_PM_OPS (&snvs_rtc_pm_ops)
369 #define SNVS_RTC_PM_OPS NULL
373 static const struct of_device_id snvs_dt_ids
[] = {
374 { .compatible
= "fsl,sec-v4.0-mon-rtc-lp", },
377 MODULE_DEVICE_TABLE(of
, snvs_dt_ids
);
379 static struct platform_driver snvs_rtc_driver
= {
382 .pm
= SNVS_RTC_PM_OPS
,
383 .of_match_table
= snvs_dt_ids
,
385 .probe
= snvs_rtc_probe
,
387 module_platform_driver(snvs_rtc_driver
);
389 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
390 MODULE_DESCRIPTION("Freescale SNVS RTC Driver");
391 MODULE_LICENSE("GPL");