blob: 66a9bb85bbe8698e2b7234b4d26f4b13ef3d52d0 [file] [log] [blame]
Richard Purdiee842f1c2006-03-27 01:16:46 -08001/*
2 * Real Time Clock interface for StrongARM SA1x00 and XScale PXA2xx
3 *
4 * Copyright (c) 2000 Nils Faerber
5 *
6 * Based on rtc.c by Paul Gortmaker
7 *
8 * Original Driver by Nils Faerber <nils@kernelconcepts.de>
9 *
10 * Modifications from:
11 * CIH <cih@coventive.com>
12 * Nicolas Pitre <nico@cam.org>
13 * Andrew Christian <andrew.christian@hp.com>
14 *
15 * Converted to the RTC subsystem and Driver Model
16 * by Richard Purdie <rpurdie@rpsys.net>
17 *
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.
22 */
23
24#include <linux/platform_device.h>
25#include <linux/module.h>
26#include <linux/rtc.h>
27#include <linux/init.h>
28#include <linux/fs.h>
29#include <linux/interrupt.h>
30#include <linux/string.h>
31#include <linux/pm.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070032#include <linux/bitops.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080033
Russell Kinga09e64f2008-08-05 16:14:15 +010034#include <mach/hardware.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080035#include <asm/irq.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080036
37#ifdef CONFIG_ARCH_PXA
Russell Kinga09e64f2008-08-05 16:14:15 +010038#include <mach/pxa-regs.h>
Richard Purdiee842f1c2006-03-27 01:16:46 -080039#endif
40
41#define TIMER_FREQ CLOCK_TICK_RATE
42#define RTC_DEF_DIVIDER 32768 - 1
43#define RTC_DEF_TRIM 0
44
45static unsigned long rtc_freq = 1024;
46static struct rtc_time rtc_alarm;
Ingo Molnar34af9462006-06-27 02:53:55 -070047static DEFINE_SPINLOCK(sa1100_rtc_lock);
Richard Purdiee842f1c2006-03-27 01:16:46 -080048
Russell King797276e2008-04-20 12:30:41 +010049static inline int rtc_periodic_alarm(struct rtc_time *tm)
50{
51 return (tm->tm_year == -1) ||
52 ((unsigned)tm->tm_mon >= 12) ||
53 ((unsigned)(tm->tm_mday - 1) >= 31) ||
54 ((unsigned)tm->tm_hour > 23) ||
55 ((unsigned)tm->tm_min > 59) ||
56 ((unsigned)tm->tm_sec > 59);
57}
58
59/*
60 * Calculate the next alarm time given the requested alarm time mask
61 * and the current time.
62 */
63static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm)
64{
65 unsigned long next_time;
66 unsigned long now_time;
67
68 next->tm_year = now->tm_year;
69 next->tm_mon = now->tm_mon;
70 next->tm_mday = now->tm_mday;
71 next->tm_hour = alrm->tm_hour;
72 next->tm_min = alrm->tm_min;
73 next->tm_sec = alrm->tm_sec;
74
75 rtc_tm_to_time(now, &now_time);
76 rtc_tm_to_time(next, &next_time);
77
78 if (next_time < now_time) {
79 /* Advance one day */
80 next_time += 60 * 60 * 24;
81 rtc_time_to_tm(next_time, next);
82 }
83}
84
Richard Purdiee842f1c2006-03-27 01:16:46 -080085static int rtc_update_alarm(struct rtc_time *alrm)
86{
87 struct rtc_time alarm_tm, now_tm;
88 unsigned long now, time;
89 int ret;
90
91 do {
92 now = RCNR;
93 rtc_time_to_tm(now, &now_tm);
94 rtc_next_alarm_time(&alarm_tm, &now_tm, alrm);
95 ret = rtc_tm_to_time(&alarm_tm, &time);
96 if (ret != 0)
97 break;
98
99 RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
100 RTAR = time;
101 } while (now != RCNR);
102
103 return ret;
104}
105
David Howells7d12e782006-10-05 14:55:46 +0100106static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
Richard Purdiee842f1c2006-03-27 01:16:46 -0800107{
108 struct platform_device *pdev = to_platform_device(dev_id);
109 struct rtc_device *rtc = platform_get_drvdata(pdev);
110 unsigned int rtsr;
111 unsigned long events = 0;
112
113 spin_lock(&sa1100_rtc_lock);
114
115 rtsr = RTSR;
116 /* clear interrupt sources */
117 RTSR = 0;
118 RTSR = (RTSR_AL | RTSR_HZ) & (rtsr >> 2);
119
120 /* clear alarm interrupt if it has occurred */
121 if (rtsr & RTSR_AL)
122 rtsr &= ~RTSR_ALE;
123 RTSR = rtsr & (RTSR_ALE | RTSR_HZE);
124
125 /* update irq data & counter */
126 if (rtsr & RTSR_AL)
127 events |= RTC_AF | RTC_IRQF;
128 if (rtsr & RTSR_HZ)
129 events |= RTC_UF | RTC_IRQF;
130
David Brownellab6a2d72007-05-08 00:33:30 -0700131 rtc_update_irq(rtc, 1, events);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800132
133 if (rtsr & RTSR_AL && rtc_periodic_alarm(&rtc_alarm))
134 rtc_update_alarm(&rtc_alarm);
135
136 spin_unlock(&sa1100_rtc_lock);
137
138 return IRQ_HANDLED;
139}
140
141static int rtc_timer1_count;
142
David Howells7d12e782006-10-05 14:55:46 +0100143static irqreturn_t timer1_interrupt(int irq, void *dev_id)
Richard Purdiee842f1c2006-03-27 01:16:46 -0800144{
145 struct platform_device *pdev = to_platform_device(dev_id);
146 struct rtc_device *rtc = platform_get_drvdata(pdev);
147
148 /*
149 * If we match for the first time, rtc_timer1_count will be 1.
150 * Otherwise, we wrapped around (very unlikely but
151 * still possible) so compute the amount of missed periods.
152 * The match reg is updated only when the data is actually retrieved
153 * to avoid unnecessary interrupts.
154 */
155 OSSR = OSSR_M1; /* clear match on timer1 */
156
David Brownellab6a2d72007-05-08 00:33:30 -0700157 rtc_update_irq(rtc, rtc_timer1_count, RTC_PF | RTC_IRQF);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800158
159 if (rtc_timer1_count == 1)
160 rtc_timer1_count = (rtc_freq * ((1<<30)/(TIMER_FREQ>>2)));
161
162 return IRQ_HANDLED;
163}
164
165static int sa1100_rtc_read_callback(struct device *dev, int data)
166{
167 if (data & RTC_PF) {
168 /* interpolate missed periods and set match for the next */
169 unsigned long period = TIMER_FREQ/rtc_freq;
170 unsigned long oscr = OSCR;
171 unsigned long osmr1 = OSMR1;
172 unsigned long missed = (oscr - osmr1)/period;
173 data += missed << 8;
174 OSSR = OSSR_M1; /* clear match on timer 1 */
175 OSMR1 = osmr1 + (missed + 1)*period;
176 /* Ensure we didn't miss another match in the mean time.
177 * Here we compare (match - OSCR) 8 instead of 0 --
178 * see comment in pxa_timer_interrupt() for explanation.
179 */
180 while( (signed long)((osmr1 = OSMR1) - OSCR) <= 8 ) {
181 data += 0x100;
182 OSSR = OSSR_M1; /* clear match on timer 1 */
183 OSMR1 = osmr1 + period;
184 }
185 }
186 return data;
187}
188
189static int sa1100_rtc_open(struct device *dev)
190{
191 int ret;
192
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700193 ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800194 "rtc 1Hz", dev);
195 if (ret) {
Alessandro Zummo2260a252006-04-10 22:54:46 -0700196 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800197 goto fail_ui;
198 }
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700199 ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800200 "rtc Alrm", dev);
201 if (ret) {
Alessandro Zummo2260a252006-04-10 22:54:46 -0700202 dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800203 goto fail_ai;
204 }
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700205 ret = request_irq(IRQ_OST1, timer1_interrupt, IRQF_DISABLED,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800206 "rtc timer", dev);
207 if (ret) {
Alessandro Zummo2260a252006-04-10 22:54:46 -0700208 dev_err(dev, "IRQ %d already in use.\n", IRQ_OST1);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800209 goto fail_pi;
210 }
211 return 0;
212
213 fail_pi:
Russell Kingf1226702006-05-06 11:29:21 +0100214 free_irq(IRQ_RTCAlrm, dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800215 fail_ai:
Russell Kingf1226702006-05-06 11:29:21 +0100216 free_irq(IRQ_RTC1Hz, dev);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800217 fail_ui:
218 return ret;
219}
220
221static void sa1100_rtc_release(struct device *dev)
222{
223 spin_lock_irq(&sa1100_rtc_lock);
224 RTSR = 0;
225 OIER &= ~OIER_E1;
226 OSSR = OSSR_M1;
227 spin_unlock_irq(&sa1100_rtc_lock);
228
229 free_irq(IRQ_OST1, dev);
230 free_irq(IRQ_RTCAlrm, dev);
231 free_irq(IRQ_RTC1Hz, dev);
232}
233
234
235static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd,
236 unsigned long arg)
237{
238 switch(cmd) {
239 case RTC_AIE_OFF:
240 spin_lock_irq(&sa1100_rtc_lock);
241 RTSR &= ~RTSR_ALE;
242 spin_unlock_irq(&sa1100_rtc_lock);
243 return 0;
244 case RTC_AIE_ON:
245 spin_lock_irq(&sa1100_rtc_lock);
246 RTSR |= RTSR_ALE;
247 spin_unlock_irq(&sa1100_rtc_lock);
248 return 0;
249 case RTC_UIE_OFF:
250 spin_lock_irq(&sa1100_rtc_lock);
251 RTSR &= ~RTSR_HZE;
252 spin_unlock_irq(&sa1100_rtc_lock);
253 return 0;
254 case RTC_UIE_ON:
255 spin_lock_irq(&sa1100_rtc_lock);
256 RTSR |= RTSR_HZE;
257 spin_unlock_irq(&sa1100_rtc_lock);
258 return 0;
259 case RTC_PIE_OFF:
260 spin_lock_irq(&sa1100_rtc_lock);
261 OIER &= ~OIER_E1;
262 spin_unlock_irq(&sa1100_rtc_lock);
263 return 0;
264 case RTC_PIE_ON:
Richard Purdiee842f1c2006-03-27 01:16:46 -0800265 spin_lock_irq(&sa1100_rtc_lock);
266 OSMR1 = TIMER_FREQ/rtc_freq + OSCR;
267 OIER |= OIER_E1;
268 rtc_timer1_count = 1;
269 spin_unlock_irq(&sa1100_rtc_lock);
270 return 0;
271 case RTC_IRQP_READ:
272 return put_user(rtc_freq, (unsigned long *)arg);
273 case RTC_IRQP_SET:
274 if (arg < 1 || arg > TIMER_FREQ)
275 return -EINVAL;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800276 rtc_freq = arg;
277 return 0;
278 }
Alessandro Zummob3969e52006-05-20 15:00:29 -0700279 return -ENOIOCTLCMD;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800280}
281
282static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
283{
284 rtc_time_to_tm(RCNR, tm);
285 return 0;
286}
287
288static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
289{
290 unsigned long time;
291 int ret;
292
293 ret = rtc_tm_to_time(tm, &time);
294 if (ret == 0)
295 RCNR = time;
296 return ret;
297}
298
299static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
300{
David Brownell32b49da2007-02-20 13:58:13 -0800301 u32 rtsr;
302
Richard Purdiee842f1c2006-03-27 01:16:46 -0800303 memcpy(&alrm->time, &rtc_alarm, sizeof(struct rtc_time));
David Brownell32b49da2007-02-20 13:58:13 -0800304 rtsr = RTSR;
305 alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
306 alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800307 return 0;
308}
309
310static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
311{
312 int ret;
313
314 spin_lock_irq(&sa1100_rtc_lock);
315 ret = rtc_update_alarm(&alrm->time);
316 if (ret == 0) {
Richard Purdiee842f1c2006-03-27 01:16:46 -0800317 if (alrm->enabled)
David Brownell32b49da2007-02-20 13:58:13 -0800318 RTSR |= RTSR_ALE;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800319 else
David Brownell32b49da2007-02-20 13:58:13 -0800320 RTSR &= ~RTSR_ALE;
Richard Purdiee842f1c2006-03-27 01:16:46 -0800321 }
322 spin_unlock_irq(&sa1100_rtc_lock);
323
324 return ret;
325}
326
327static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
328{
David Brownella2db8df2006-12-13 00:35:08 -0800329 seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800330 seq_printf(seq, "update_IRQ\t: %s\n",
331 (RTSR & RTSR_HZE) ? "yes" : "no");
332 seq_printf(seq, "periodic_IRQ\t: %s\n",
333 (OIER & OIER_E1) ? "yes" : "no");
334 seq_printf(seq, "periodic_freq\t: %ld\n", rtc_freq);
335
336 return 0;
337}
338
David Brownellff8371a2006-09-30 23:28:17 -0700339static const struct rtc_class_ops sa1100_rtc_ops = {
Richard Purdiee842f1c2006-03-27 01:16:46 -0800340 .open = sa1100_rtc_open,
341 .read_callback = sa1100_rtc_read_callback,
342 .release = sa1100_rtc_release,
343 .ioctl = sa1100_rtc_ioctl,
344 .read_time = sa1100_rtc_read_time,
345 .set_time = sa1100_rtc_set_time,
346 .read_alarm = sa1100_rtc_read_alarm,
347 .set_alarm = sa1100_rtc_set_alarm,
348 .proc = sa1100_rtc_proc,
349};
350
351static int sa1100_rtc_probe(struct platform_device *pdev)
352{
353 struct rtc_device *rtc;
354
355 /*
356 * According to the manual we should be able to let RTTR be zero
357 * and then a default diviser for a 32.768KHz clock is used.
358 * Apparently this doesn't work, at least for my SA1110 rev 5.
359 * If the clock divider is uninitialized then reset it to the
360 * default value to get the 1Hz clock.
361 */
362 if (RTTR == 0) {
363 RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
Alessandro Zummo2260a252006-04-10 22:54:46 -0700364 dev_warn(&pdev->dev, "warning: initializing default clock divider/trim value\n");
Richard Purdiee842f1c2006-03-27 01:16:46 -0800365 /* The current RTC value probably doesn't make sense either */
366 RCNR = 0;
367 }
368
Uli Luckase5a2c9c2008-06-18 09:54:03 +0100369 device_init_wakeup(&pdev->dev, 1);
370
Richard Purdiee842f1c2006-03-27 01:16:46 -0800371 rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
372 THIS_MODULE);
373
Alessandro Zummo2260a252006-04-10 22:54:46 -0700374 if (IS_ERR(rtc))
Richard Purdiee842f1c2006-03-27 01:16:46 -0800375 return PTR_ERR(rtc);
Richard Purdiee842f1c2006-03-27 01:16:46 -0800376
377 platform_set_drvdata(pdev, rtc);
378
Richard Purdiee842f1c2006-03-27 01:16:46 -0800379 return 0;
380}
381
382static int sa1100_rtc_remove(struct platform_device *pdev)
383{
384 struct rtc_device *rtc = platform_get_drvdata(pdev);
385
386 if (rtc)
387 rtc_device_unregister(rtc);
388
389 return 0;
390}
391
Russell King6bc54e62007-11-12 22:49:58 +0000392#ifdef CONFIG_PM
393static int sa1100_rtc_suspend(struct platform_device *pdev, pm_message_t state)
394{
David Brownellf6182582008-02-06 01:38:57 -0800395 if (device_may_wakeup(&pdev->dev))
396 enable_irq_wake(IRQ_RTCAlrm);
Russell King6bc54e62007-11-12 22:49:58 +0000397 return 0;
398}
399
400static int sa1100_rtc_resume(struct platform_device *pdev)
401{
David Brownellf6182582008-02-06 01:38:57 -0800402 if (device_may_wakeup(&pdev->dev))
403 disable_irq_wake(IRQ_RTCAlrm);
Russell King6bc54e62007-11-12 22:49:58 +0000404 return 0;
405}
406#else
407#define sa1100_rtc_suspend NULL
408#define sa1100_rtc_resume NULL
409#endif
410
Richard Purdiee842f1c2006-03-27 01:16:46 -0800411static struct platform_driver sa1100_rtc_driver = {
412 .probe = sa1100_rtc_probe,
413 .remove = sa1100_rtc_remove,
Russell King6bc54e62007-11-12 22:49:58 +0000414 .suspend = sa1100_rtc_suspend,
415 .resume = sa1100_rtc_resume,
Richard Purdiee842f1c2006-03-27 01:16:46 -0800416 .driver = {
417 .name = "sa1100-rtc",
418 },
419};
420
421static int __init sa1100_rtc_init(void)
422{
423 return platform_driver_register(&sa1100_rtc_driver);
424}
425
426static void __exit sa1100_rtc_exit(void)
427{
428 platform_driver_unregister(&sa1100_rtc_driver);
429}
430
431module_init(sa1100_rtc_init);
432module_exit(sa1100_rtc_exit);
433
434MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
435MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
436MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700437MODULE_ALIAS("platform:sa1100-rtc");