2 * Real Time Clock interface for StrongARM SA1x00 and XScale PXA2xx
4 * Copyright (c) 2000 Nils Faerber
6 * Based on rtc.c by Paul Gortmaker
8 * Original Driver by Nils Faerber <nils@kernelconcepts.de>
11 * CIH <cih@coventive.com>
12 * Nicolas Pitre <nico@fluxnic.net>
13 * Andrew Christian <andrew.christian@hp.com>
15 * Converted to the RTC subsystem and Driver Model
16 * by Richard Purdie <rpurdie@rpsys.net>
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
24 #include <linux/platform_device.h>
25 #include <linux/module.h>
26 #include <linux/rtc.h>
27 #include <linux/init.h>
29 #include <linux/interrupt.h>
30 #include <linux/string.h>
32 #include <linux/bitops.h>
34 #include <mach/hardware.h>
37 #ifdef CONFIG_ARCH_PXA
38 #include <mach/regs-rtc.h>
39 #include <mach/regs-ost.h>
42 #define RTC_DEF_DIVIDER (32768 - 1)
43 #define RTC_DEF_TRIM 0
45 static const unsigned long RTC_FREQ = 1024;
46 static unsigned long timer_freq;
47 static struct rtc_time rtc_alarm;
48 static DEFINE_SPINLOCK(sa1100_rtc_lock);
50 static inline int rtc_periodic_alarm(struct rtc_time *tm)
52 return (tm->tm_year == -1) ||
53 ((unsigned)tm->tm_mon >= 12) ||
54 ((unsigned)(tm->tm_mday - 1) >= 31) ||
55 ((unsigned)tm->tm_hour > 23) ||
56 ((unsigned)tm->tm_min > 59) ||
57 ((unsigned)tm->tm_sec > 59);
61 * Calculate the next alarm time given the requested alarm time mask
62 * and the current time.
64 static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now,
65 struct rtc_time *alrm)
67 unsigned long next_time;
68 unsigned long now_time;
70 next->tm_year = now->tm_year;
71 next->tm_mon = now->tm_mon;
72 next->tm_mday = now->tm_mday;
73 next->tm_hour = alrm->tm_hour;
74 next->tm_min = alrm->tm_min;
75 next->tm_sec = alrm->tm_sec;
77 rtc_tm_to_time(now, &now_time);
78 rtc_tm_to_time(next, &next_time);
80 if (next_time < now_time) {
82 next_time += 60 * 60 * 24;
83 rtc_time_to_tm(next_time, next);
87 static int rtc_update_alarm(struct rtc_time *alrm)
89 struct rtc_time alarm_tm, now_tm;
90 unsigned long now, time;
95 rtc_time_to_tm(now, &now_tm);
96 rtc_next_alarm_time(&alarm_tm, &now_tm, alrm);
97 ret = rtc_tm_to_time(&alarm_tm, &time);
101 RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
103 } while (now != RCNR);
108 static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
110 struct platform_device *pdev = to_platform_device(dev_id);
111 struct rtc_device *rtc = platform_get_drvdata(pdev);
113 unsigned long events = 0;
115 spin_lock(&sa1100_rtc_lock);
118 /* clear interrupt sources */
120 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
121 * See also the comments in sa1100_rtc_probe(). */
122 if (rtsr & (RTSR_ALE | RTSR_HZE)) {
123 /* This is the original code, before there was the if test
124 * above. This code does not clear interrupts that were not
126 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
128 /* For some reason, it is possible to enter this routine
129 * without interruptions enabled, it has been tested with
130 * several units (Bug in SA11xx chip?).
132 * This situation leads to an infinite "loop" of interrupt
133 * routine calling and as a result the processor seems to
134 * lock on its first call to open(). */
135 RTSR = RTSR_AL | RTSR_HZ;
138 /* clear alarm interrupt if it has occurred */
141 RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
143 /* update irq data & counter */
145 events |= RTC_AF | RTC_IRQF;
147 events |= RTC_UF | RTC_IRQF;
149 rtc_update_irq(rtc, 1, events);
151 if (rtsr & RTSR_AL && rtc_periodic_alarm(&rtc_alarm))
152 rtc_update_alarm(&rtc_alarm);
154 spin_unlock(&sa1100_rtc_lock);
159 static int sa1100_irq_set_freq(struct device *dev, int freq)
161 if (freq < 1 || freq > timer_freq) {
164 struct rtc_device *rtc = (struct rtc_device *)dev;
166 rtc->irq_freq = freq;
172 static int rtc_timer1_count;
174 static inline int sa1100_timer1_retrigger(struct rtc_device *rtc)
177 unsigned long period = timer_freq / rtc->irq_freq;
179 spin_lock_irq(&sa1100_rtc_lock);
184 /* If OSCR > OSMR1, diff is a very large number (unsigned
185 * math). This means we have a lost interrupt. */
186 } while (diff > period);
189 spin_unlock_irq(&sa1100_rtc_lock);
194 static irqreturn_t timer1_interrupt(int irq, void *dev_id)
196 struct platform_device *pdev = to_platform_device(dev_id);
197 struct rtc_device *rtc = platform_get_drvdata(pdev);
200 * If we match for the first time, rtc_timer1_count will be 1.
201 * Otherwise, we wrapped around (very unlikely but
202 * still possible) so compute the amount of missed periods.
203 * The match reg is updated only when the data is actually retrieved
204 * to avoid unnecessary interrupts.
206 OSSR = OSSR_M1; /* clear match on timer1 */
208 rtc_update_irq(rtc, rtc_timer1_count, RTC_PF | RTC_IRQF);
210 if (rtc_timer1_count == 1)
212 (rtc->irq_freq * ((1 << 30) / (timer_freq >> 2)));
215 sa1100_timer1_retrigger(rtc);
220 static int sa1100_rtc_read_callback(struct device *dev, int data)
223 struct rtc_device *rtc = (struct rtc_device *)dev;
225 /* interpolate missed periods and set match for the next */
226 unsigned long period = timer_freq / rtc->irq_freq;
227 unsigned long oscr = OSCR;
228 unsigned long osmr1 = OSMR1;
229 unsigned long missed = (oscr - osmr1)/period;
231 OSSR = OSSR_M1; /* clear match on timer 1 */
232 OSMR1 = osmr1 + (missed + 1)*period;
233 /* Ensure we didn't miss another match in the mean time.
234 * Here we compare (match - OSCR) 8 instead of 0 --
235 * see comment in pxa_timer_interrupt() for explanation.
237 while ((signed long)((osmr1 = OSMR1) - OSCR) <= 8) {
239 OSSR = OSSR_M1; /* clear match on timer 1 */
240 OSMR1 = osmr1 + period;
246 static int sa1100_rtc_open(struct device *dev)
249 struct rtc_device *rtc = (struct rtc_device *)dev;
251 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
254 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
257 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
260 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
263 ret = request_irq(IRQ_OST1, timer1_interrupt, IRQF_DISABLED,
266 dev_err(dev, "IRQ %d already in use.\n", IRQ_OST1);
269 rtc->max_user_freq = RTC_FREQ;
270 sa1100_irq_set_freq(dev, RTC_FREQ);
275 free_irq(IRQ_RTCAlrm, dev);
277 free_irq(IRQ_RTC1Hz, dev);
282 static void sa1100_rtc_release(struct device *dev)
284 spin_lock_irq(&sa1100_rtc_lock);
288 spin_unlock_irq(&sa1100_rtc_lock);
290 free_irq(IRQ_OST1, dev);
291 free_irq(IRQ_RTCAlrm, dev);
292 free_irq(IRQ_RTC1Hz, dev);
296 static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd,
301 spin_lock_irq(&sa1100_rtc_lock);
303 spin_unlock_irq(&sa1100_rtc_lock);
306 spin_lock_irq(&sa1100_rtc_lock);
308 spin_unlock_irq(&sa1100_rtc_lock);
314 static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
316 spin_lock_irq(&sa1100_rtc_lock);
321 spin_unlock_irq(&sa1100_rtc_lock);
325 static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
327 rtc_time_to_tm(RCNR, tm);
331 static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
336 ret = rtc_tm_to_time(tm, &time);
342 static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
346 memcpy(&alrm->time, &rtc_alarm, sizeof(struct rtc_time));
348 alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
349 alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
353 static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
357 spin_lock_irq(&sa1100_rtc_lock);
358 ret = rtc_update_alarm(&alrm->time);
365 spin_unlock_irq(&sa1100_rtc_lock);
370 static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
372 struct rtc_device *rtc = (struct rtc_device *)dev;
374 seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR);
375 seq_printf(seq, "update_IRQ\t: %s\n",
376 (RTSR & RTSR_HZE) ? "yes" : "no");
377 seq_printf(seq, "periodic_IRQ\t: %s\n",
378 (OIER & OIER_E1) ? "yes" : "no");
379 seq_printf(seq, "periodic_freq\t: %d\n", rtc->irq_freq);
380 seq_printf(seq, "RTSR\t\t: 0x%08x\n", (u32)RTSR);
385 static const struct rtc_class_ops sa1100_rtc_ops = {
386 .open = sa1100_rtc_open,
387 .read_callback = sa1100_rtc_read_callback,
388 .release = sa1100_rtc_release,
389 .ioctl = sa1100_rtc_ioctl,
390 .read_time = sa1100_rtc_read_time,
391 .set_time = sa1100_rtc_set_time,
392 .read_alarm = sa1100_rtc_read_alarm,
393 .set_alarm = sa1100_rtc_set_alarm,
394 .proc = sa1100_rtc_proc,
395 .alarm_irq_enable = sa1100_rtc_alarm_irq_enable,
398 static int sa1100_rtc_probe(struct platform_device *pdev)
400 struct rtc_device *rtc;
402 timer_freq = get_clock_tick_rate();
405 * According to the manual we should be able to let RTTR be zero
406 * and then a default diviser for a 32.768KHz clock is used.
407 * Apparently this doesn't work, at least for my SA1110 rev 5.
408 * If the clock divider is uninitialized then reset it to the
409 * default value to get the 1Hz clock.
412 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
413 dev_warn(&pdev->dev, "warning: "
414 "initializing default clock divider/trim value\n");
415 /* The current RTC value probably doesn't make sense either */
419 device_init_wakeup(&pdev->dev, 1);
421 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
427 platform_set_drvdata(pdev, rtc);
429 /* Set the irq_freq */
430 /*TODO: Find out who is messing with this value after we initialize
432 rtc->irq_freq = RTC_FREQ;
434 /* Fix for a nasty initialization problem the in SA11xx RTSR register.
435 * See also the comments in sa1100_rtc_interrupt().
437 * Sometimes bit 1 of the RTSR (RTSR_HZ) will wake up 1, which means an
438 * interrupt pending, even though interrupts were never enabled.
439 * In this case, this bit it must be reset before enabling
440 * interruptions to avoid a nonexistent interrupt to occur.
442 * In principle, the same problem would apply to bit 0, although it has
443 * never been observed to happen.
445 * This issue is addressed both here and in sa1100_rtc_interrupt().
446 * If the issue is not addressed here, in the times when the processor
447 * wakes up with the bit set there will be one spurious interrupt.
449 * The issue is also dealt with in sa1100_rtc_interrupt() to be on the
450 * safe side, once the condition that lead to this strange
451 * initialization is unknown and could in principle happen during
454 * Notice that clearing bit 1 and 0 is accomplished by writting ONES to
455 * the corresponding bits in RTSR. */
456 RTSR = RTSR_AL | RTSR_HZ;
461 static int sa1100_rtc_remove(struct platform_device *pdev)
463 struct rtc_device *rtc = platform_get_drvdata(pdev);
466 rtc_device_unregister(rtc);
472 static int sa1100_rtc_suspend(struct device *dev)
474 if (device_may_wakeup(dev))
475 enable_irq_wake(IRQ_RTCAlrm);
479 static int sa1100_rtc_resume(struct device *dev)
481 if (device_may_wakeup(dev))
482 disable_irq_wake(IRQ_RTCAlrm);
486 static const struct dev_pm_ops sa1100_rtc_pm_ops = {
487 .suspend = sa1100_rtc_suspend,
488 .resume = sa1100_rtc_resume,
492 static struct platform_driver sa1100_rtc_driver = {
493 .probe = sa1100_rtc_probe,
494 .remove = sa1100_rtc_remove,
496 .name = "sa1100-rtc",
498 .pm = &sa1100_rtc_pm_ops,
503 static int __init sa1100_rtc_init(void)
505 return platform_driver_register(&sa1100_rtc_driver);
508 static void __exit sa1100_rtc_exit(void)
510 platform_driver_unregister(&sa1100_rtc_driver);
513 module_init(sa1100_rtc_init);
514 module_exit(sa1100_rtc_exit);
516 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
517 MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
518 MODULE_LICENSE("GPL");
519 MODULE_ALIAS("platform:sa1100-rtc");