blob: a9ea7d181380dfdf2efdc556912c496fbf54cf7e [file] [log] [blame]
Xianglong Due88b8152013-07-03 15:08:04 -07001/*
2 * SiRFSoC Real Time Clock interface for Linux
3 *
4 * Copyright (c) 2013 Cambridge Silicon Radio Limited, a CSR plc group company.
5 *
6 * Licensed under GPLv2 or later.
7 */
8
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>
14#include <linux/io.h>
15#include <linux/of.h>
16#include <linux/rtc/sirfsoc_rtciobrg.h>
17
18
19#define RTC_CN 0x00
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)
30#define RTC_DIV 0x0c
31#define RTC_DEEP_CTRL 0x14
32#define RTC_CLOCK_SWITCH 0x1c
33#define SIRFSOC_RTC_CLK 0x03 /* others are reserved */
34
35/* Refer to RTC DIV switch */
36#define RTC_HZ 16
37
38/* This macro is also defined in arch/arm/plat-sirfsoc/cpu.c */
39#define RTC_SHIFT 4
40
41#define INTR_SYSRTC_CN 0x48
42
43struct sirfsoc_rtc_drv {
44 struct rtc_device *rtc;
45 u32 rtc_base;
46 u32 irq;
Xianglong Du28984c7d2013-09-11 14:24:23 -070047 unsigned irq_wake;
Xianglong Due88b8152013-07-03 15:08:04 -070048 /* Overflow for every 8 years extra time */
49 u32 overflow_rtc;
50#ifdef CONFIG_PM
51 u32 saved_counter;
52 u32 saved_overflow_rtc;
53#endif
54};
55
56static int sirfsoc_rtc_read_alarm(struct device *dev,
57 struct rtc_wkalrm *alrm)
58{
59 unsigned long rtc_alarm, rtc_count;
60 struct sirfsoc_rtc_drv *rtcdrv;
61
Jingoo Hanb7efdf32013-11-12 15:10:35 -080062 rtcdrv = dev_get_drvdata(dev);
Xianglong Due88b8152013-07-03 15:08:04 -070063
64 local_irq_disable();
65
66 rtc_count = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
67
68 rtc_alarm = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_ALARM0);
69 memset(alrm, 0, sizeof(struct rtc_wkalrm));
70
71 /*
72 * assume alarm interval not beyond one round counter overflow_rtc:
73 * 0->0xffffffff
74 */
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));
80 else
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)
86 alrm->enabled = 1;
87 local_irq_enable();
88
89 return 0;
90}
91
92static int sirfsoc_rtc_set_alarm(struct device *dev,
93 struct rtc_wkalrm *alrm)
94{
95 unsigned long rtc_status_reg, rtc_alarm;
96 struct sirfsoc_rtc_drv *rtcdrv;
Jingoo Hanb7efdf32013-11-12 15:10:35 -080097 rtcdrv = dev_get_drvdata(dev);
Xianglong Due88b8152013-07-03 15:08:04 -070098
99 if (alrm->enabled) {
100 rtc_tm_to_time(&(alrm->time), &rtc_alarm);
101
102 local_irq_disable();
103
104 rtc_status_reg = sirfsoc_rtc_iobrg_readl(
105 rtcdrv->rtc_base + RTC_STATUS);
106 if (rtc_status_reg & SIRFSOC_RTC_AL0E) {
107 /*
108 * An ongoing alarm in progress - ingore it and not
109 * to return EBUSY
110 */
111 dev_info(dev, "An old alarm was set, will be replaced by a new one\n");
112 }
113
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 */
117 /*
118 * This bit RTC_AL sets it as a wake-up source for Sleep Mode
119 * Writing 1 into this bit will clear it
120 */
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);
126 local_irq_enable();
127 } else {
128 /*
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
132 */
133 local_irq_disable();
134
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);
144
145 sirfsoc_rtc_iobrg_writel(rtc_status_reg,
146 rtcdrv->rtc_base + RTC_STATUS);
147 }
148
149 local_irq_enable();
150 }
151
152 return 0;
153}
154
155static int sirfsoc_rtc_read_time(struct device *dev,
156 struct rtc_time *tm)
157{
158 unsigned long tmp_rtc = 0;
159 struct sirfsoc_rtc_drv *rtcdrv;
Jingoo Hanb7efdf32013-11-12 15:10:35 -0800160 rtcdrv = dev_get_drvdata(dev);
Xianglong Due88b8152013-07-03 15:08:04 -0700161 /*
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.
165 */
166 do {
167 tmp_rtc = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
168 cpu_relax();
169 } while (tmp_rtc != sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN));
170
171 rtc_time_to_tm(rtcdrv->overflow_rtc << (BITS_PER_LONG - RTC_SHIFT) |
172 tmp_rtc >> RTC_SHIFT, tm);
173 return 0;
174}
175
176static int sirfsoc_rtc_set_time(struct device *dev,
177 struct rtc_time *tm)
178{
179 unsigned long rtc_time;
180 struct sirfsoc_rtc_drv *rtcdrv;
Jingoo Hanb7efdf32013-11-12 15:10:35 -0800181 rtcdrv = dev_get_drvdata(dev);
Xianglong Due88b8152013-07-03 15:08:04 -0700182
183 rtc_tm_to_time(tm, &rtc_time);
184
185 rtcdrv->overflow_rtc = rtc_time >> (BITS_PER_LONG - RTC_SHIFT);
186
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);
191
192 return 0;
193}
194
195static int sirfsoc_rtc_ioctl(struct device *dev, unsigned int cmd,
196 unsigned long arg)
197{
198 switch (cmd) {
199 case RTC_PIE_ON:
200 case RTC_PIE_OFF:
201 case RTC_UIE_ON:
202 case RTC_UIE_OFF:
203 case RTC_AIE_ON:
204 case RTC_AIE_OFF:
205 return 0;
206
207 default:
208 return -ENOIOCTLCMD;
209 }
210}
211
hao liu09e427f2014-12-10 15:53:49 -0800212static int sirfsoc_rtc_alarm_irq_enable(struct device *dev,
213 unsigned int enabled)
214{
215 unsigned long rtc_status_reg = 0x0;
216 struct sirfsoc_rtc_drv *rtcdrv;
217
218 rtcdrv = dev_get_drvdata(dev);
219
220 local_irq_disable();
221
222 rtc_status_reg = sirfsoc_rtc_iobrg_readl(
223 rtcdrv->rtc_base + RTC_STATUS);
224 if (enabled)
225 rtc_status_reg |= SIRFSOC_RTC_AL0E;
226 else
227 rtc_status_reg &= ~SIRFSOC_RTC_AL0E;
228
229 sirfsoc_rtc_iobrg_writel(rtc_status_reg, rtcdrv->rtc_base + RTC_STATUS);
230 local_irq_enable();
231
232 return 0;
233
234}
235
Xianglong Due88b8152013-07-03 15:08:04 -0700236static const struct rtc_class_ops sirfsoc_rtc_ops = {
237 .read_time = sirfsoc_rtc_read_time,
238 .set_time = sirfsoc_rtc_set_time,
239 .read_alarm = sirfsoc_rtc_read_alarm,
240 .set_alarm = sirfsoc_rtc_set_alarm,
hao liu09e427f2014-12-10 15:53:49 -0800241 .ioctl = sirfsoc_rtc_ioctl,
242 .alarm_irq_enable = sirfsoc_rtc_alarm_irq_enable
Xianglong Due88b8152013-07-03 15:08:04 -0700243};
244
245static irqreturn_t sirfsoc_rtc_irq_handler(int irq, void *pdata)
246{
247 struct sirfsoc_rtc_drv *rtcdrv = pdata;
248 unsigned long rtc_status_reg = 0x0;
249 unsigned long events = 0x0;
250
251 rtc_status_reg = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_STATUS);
252 /* this bit will be set ONLY if an alarm was active
253 * and it expired NOW
254 * So this is being used as an ASSERT
255 */
256 if (rtc_status_reg & SIRFSOC_RTC_AL0) {
257 /*
258 * clear the RTC status register's alarm bit
259 * mask out the lower status bits
260 */
261 rtc_status_reg &= ~0x07;
262 /* write 1 into SIRFSOC_RTC_AL0 to ACK the alarm interrupt */
263 rtc_status_reg |= (SIRFSOC_RTC_AL0);
264 /* Clear the Alarm enable bit */
265 rtc_status_reg &= ~(SIRFSOC_RTC_AL0E);
266 }
267 sirfsoc_rtc_iobrg_writel(rtc_status_reg, rtcdrv->rtc_base + RTC_STATUS);
268 /* this should wake up any apps polling/waiting on the read
269 * after setting the alarm
270 */
271 events |= RTC_IRQF | RTC_AF;
272 rtc_update_irq(rtcdrv->rtc, 1, events);
273
274 return IRQ_HANDLED;
275}
276
277static const struct of_device_id sirfsoc_rtc_of_match[] = {
278 { .compatible = "sirf,prima2-sysrtc"},
279 {},
280};
281MODULE_DEVICE_TABLE(of, sirfsoc_rtc_of_match);
282
283static int sirfsoc_rtc_probe(struct platform_device *pdev)
284{
285 int err;
286 unsigned long rtc_div;
287 struct sirfsoc_rtc_drv *rtcdrv;
288 struct device_node *np = pdev->dev.of_node;
289
290 rtcdrv = devm_kzalloc(&pdev->dev,
291 sizeof(struct sirfsoc_rtc_drv), GFP_KERNEL);
Jingoo Han98e2d212014-04-03 14:49:45 -0700292 if (rtcdrv == NULL)
Xianglong Due88b8152013-07-03 15:08:04 -0700293 return -ENOMEM;
Xianglong Due88b8152013-07-03 15:08:04 -0700294
295 err = of_property_read_u32(np, "reg", &rtcdrv->rtc_base);
296 if (err) {
297 dev_err(&pdev->dev, "unable to find base address of rtc node in dtb\n");
Sachin Kamat3d091622013-11-12 15:10:31 -0800298 return err;
Xianglong Due88b8152013-07-03 15:08:04 -0700299 }
300
301 platform_set_drvdata(pdev, rtcdrv);
302
303 /* Register rtc alarm as a wakeup source */
304 device_init_wakeup(&pdev->dev, 1);
305
306 /*
307 * Set SYS_RTC counter in RTC_HZ HZ Units
308 * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
309 * If 16HZ, therefore RTC_DIV = 1023;
310 */
311 rtc_div = ((32768 / RTC_HZ) / 2) - 1;
312 sirfsoc_rtc_iobrg_writel(rtc_div, rtcdrv->rtc_base + RTC_DIV);
313
Xianglong Due88b8152013-07-03 15:08:04 -0700314 /* 0x3 -> RTC_CLK */
315 sirfsoc_rtc_iobrg_writel(SIRFSOC_RTC_CLK,
316 rtcdrv->rtc_base + RTC_CLOCK_SWITCH);
317
318 /* reset SYS RTC ALARM0 */
319 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv->rtc_base + RTC_ALARM0);
320
321 /* reset SYS RTC ALARM1 */
322 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv->rtc_base + RTC_ALARM1);
323
324 /* Restore RTC Overflow From Register After Command Reboot */
325 rtcdrv->overflow_rtc =
326 sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_SW_VALUE);
327
Guo Zeng0e953252014-12-10 15:52:24 -0800328 rtcdrv->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
329 &sirfsoc_rtc_ops, THIS_MODULE);
330 if (IS_ERR(rtcdrv->rtc)) {
331 err = PTR_ERR(rtcdrv->rtc);
332 dev_err(&pdev->dev, "can't register RTC device\n");
333 return err;
334 }
335
Xianglong Due88b8152013-07-03 15:08:04 -0700336 rtcdrv->irq = platform_get_irq(pdev, 0);
337 err = devm_request_irq(
338 &pdev->dev,
339 rtcdrv->irq,
340 sirfsoc_rtc_irq_handler,
341 IRQF_SHARED,
342 pdev->name,
343 rtcdrv);
344 if (err) {
345 dev_err(&pdev->dev, "Unable to register for the SiRF SOC RTC IRQ\n");
Sachin Kamat3d091622013-11-12 15:10:31 -0800346 return err;
Xianglong Due88b8152013-07-03 15:08:04 -0700347 }
348
349 return 0;
Xianglong Due88b8152013-07-03 15:08:04 -0700350}
351
352static int sirfsoc_rtc_remove(struct platform_device *pdev)
353{
Xianglong Due88b8152013-07-03 15:08:04 -0700354 device_init_wakeup(&pdev->dev, 0);
Xianglong Due88b8152013-07-03 15:08:04 -0700355
356 return 0;
357}
358
Xianglong Du3916b092014-04-03 14:49:53 -0700359#ifdef CONFIG_PM_SLEEP
Xianglong Due88b8152013-07-03 15:08:04 -0700360static int sirfsoc_rtc_suspend(struct device *dev)
361{
Xianglong Du3916b092014-04-03 14:49:53 -0700362 struct sirfsoc_rtc_drv *rtcdrv = dev_get_drvdata(dev);
Xianglong Due88b8152013-07-03 15:08:04 -0700363 rtcdrv->overflow_rtc =
364 sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_SW_VALUE);
365
366 rtcdrv->saved_counter =
367 sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
368 rtcdrv->saved_overflow_rtc = rtcdrv->overflow_rtc;
Xianglong Du3916b092014-04-03 14:49:53 -0700369 if (device_may_wakeup(dev) && !enable_irq_wake(rtcdrv->irq))
Xianglong Du28984c7d2013-09-11 14:24:23 -0700370 rtcdrv->irq_wake = 1;
Xianglong Due88b8152013-07-03 15:08:04 -0700371
372 return 0;
373}
374
Xianglong Du3916b092014-04-03 14:49:53 -0700375static int sirfsoc_rtc_resume(struct device *dev)
Xianglong Due88b8152013-07-03 15:08:04 -0700376{
377 u32 tmp;
Xianglong Du3916b092014-04-03 14:49:53 -0700378 struct sirfsoc_rtc_drv *rtcdrv = dev_get_drvdata(dev);
Xianglong Due88b8152013-07-03 15:08:04 -0700379
380 /*
Xianglong Du3916b092014-04-03 14:49:53 -0700381 * if resume from snapshot and the rtc power is lost,
Xianglong Due88b8152013-07-03 15:08:04 -0700382 * restroe the rtc settings
383 */
384 if (SIRFSOC_RTC_CLK != sirfsoc_rtc_iobrg_readl(
385 rtcdrv->rtc_base + RTC_CLOCK_SWITCH)) {
386 u32 rtc_div;
387 /* 0x3 -> RTC_CLK */
388 sirfsoc_rtc_iobrg_writel(SIRFSOC_RTC_CLK,
389 rtcdrv->rtc_base + RTC_CLOCK_SWITCH);
390 /*
391 * Set SYS_RTC counter in RTC_HZ HZ Units
392 * We are using 32K RTC crystal (32768 / RTC_HZ / 2) -1
393 * If 16HZ, therefore RTC_DIV = 1023;
394 */
395 rtc_div = ((32768 / RTC_HZ) / 2) - 1;
396
397 sirfsoc_rtc_iobrg_writel(rtc_div, rtcdrv->rtc_base + RTC_DIV);
398
399 /* reset SYS RTC ALARM0 */
400 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv->rtc_base + RTC_ALARM0);
401
402 /* reset SYS RTC ALARM1 */
403 sirfsoc_rtc_iobrg_writel(0x0, rtcdrv->rtc_base + RTC_ALARM1);
404 }
405 rtcdrv->overflow_rtc = rtcdrv->saved_overflow_rtc;
406
407 /*
408 * if current counter is small than previous,
409 * it means overflow in sleep
410 */
411 tmp = sirfsoc_rtc_iobrg_readl(rtcdrv->rtc_base + RTC_CN);
412 if (tmp <= rtcdrv->saved_counter)
413 rtcdrv->overflow_rtc++;
414 /*
415 *PWRC Value Be Changed When Suspend, Restore Overflow
416 * In Memory To Register
417 */
418 sirfsoc_rtc_iobrg_writel(rtcdrv->overflow_rtc,
419 rtcdrv->rtc_base + RTC_SW_VALUE);
420
Xianglong Du3916b092014-04-03 14:49:53 -0700421 if (device_may_wakeup(dev) && rtcdrv->irq_wake) {
Xianglong Due88b8152013-07-03 15:08:04 -0700422 disable_irq_wake(rtcdrv->irq);
Xianglong Du28984c7d2013-09-11 14:24:23 -0700423 rtcdrv->irq_wake = 0;
424 }
Xianglong Due88b8152013-07-03 15:08:04 -0700425
426 return 0;
427}
Xianglong Due88b8152013-07-03 15:08:04 -0700428#endif
429
Xianglong Du3916b092014-04-03 14:49:53 -0700430static SIMPLE_DEV_PM_OPS(sirfsoc_rtc_pm_ops,
431 sirfsoc_rtc_suspend, sirfsoc_rtc_resume);
Xianglong Due88b8152013-07-03 15:08:04 -0700432
433static struct platform_driver sirfsoc_rtc_driver = {
434 .driver = {
435 .name = "sirfsoc-rtc",
436 .owner = THIS_MODULE,
Xianglong Due88b8152013-07-03 15:08:04 -0700437 .pm = &sirfsoc_rtc_pm_ops,
Sachin Kamatd1496322013-11-12 15:10:56 -0800438 .of_match_table = sirfsoc_rtc_of_match,
Xianglong Due88b8152013-07-03 15:08:04 -0700439 },
440 .probe = sirfsoc_rtc_probe,
441 .remove = sirfsoc_rtc_remove,
442};
443module_platform_driver(sirfsoc_rtc_driver);
444
445MODULE_DESCRIPTION("SiRF SoC rtc driver");
446MODULE_AUTHOR("Xianglong Du <Xianglong.Du@csr.com>");
447MODULE_LICENSE("GPL v2");
448MODULE_ALIAS("platform:sirfsoc-rtc");