2 * "RTT as Real Time Clock" driver for AT91SAM9 SoC family
4 * (C) 2007 Michel Benoit
6 * Based on rtc-at91rm9200.c by Rick Bronson
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 #include <linux/time.h>
18 #include <linux/rtc.h>
19 #include <linux/interrupt.h>
20 #include <linux/ioctl.h>
22 #include <mach/board.h>
23 #include <mach/at91_rtt.h>
27 * This driver uses two configurable hardware resources that live in the
28 * AT91SAM9 backup power domain (intended to be powered at all times)
29 * to implement the Real Time Clock interfaces
31 * - A "Real-time Timer" (RTT) counts up in seconds from a base time.
32 * We can't assign the counter value (CRTV) ... but we can reset it.
34 * - One of the "General Purpose Backup Registers" (GPBRs) holds the
35 * base time, normally an offset from the beginning of the POSIX
36 * epoch (1970-Jan-1 00:00:00 UTC). Some systems also include the
37 * local timezone's offset.
39 * The RTC's value is the RTT counter plus that offset. The RTC's alarm
40 * is likewise a base (ALMV) plus that offset.
42 * Not all RTTs will be used as RTCs; some systems have multiple RTTs to
43 * choose from, or a "real" RTC module. All systems have multiple GPBR
44 * registers available, likewise usable for more than "RTC" support.
48 * We store ALARM_DISABLED in ALMV to record that no alarm is set.
49 * It's also the reset value for that field.
51 #define ALARM_DISABLED ((u32)~0)
56 struct rtc_device
*rtcdev
;
60 #define rtt_readl(rtc, field) \
61 __raw_readl((rtc)->rtt + AT91_RTT_ ## field)
62 #define rtt_writel(rtc, field, val) \
63 __raw_writel((val), (rtc)->rtt + AT91_RTT_ ## field)
65 #define gpbr_readl(rtc) \
66 at91_sys_read(AT91_GPBR + 4 * CONFIG_RTC_DRV_AT91SAM9_GPBR)
67 #define gpbr_writel(rtc, val) \
68 at91_sys_write(AT91_GPBR + 4 * CONFIG_RTC_DRV_AT91SAM9_GPBR, (val))
71 * Read current time and date in RTC
73 static int at91_rtc_readtime(struct device
*dev
, struct rtc_time
*tm
)
75 struct sam9_rtc
*rtc
= dev_get_drvdata(dev
);
79 /* read current time offset */
80 offset
= gpbr_readl(rtc
);
84 /* reread the counter to help sync the two clock domains */
85 secs
= rtt_readl(rtc
, VR
);
86 secs2
= rtt_readl(rtc
, VR
);
88 secs
= rtt_readl(rtc
, VR
);
90 rtc_time_to_tm(offset
+ secs
, tm
);
92 dev_dbg(dev
, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "readtime",
93 1900 + tm
->tm_year
, tm
->tm_mon
, tm
->tm_mday
,
94 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
100 * Set current time and date in RTC
102 static int at91_rtc_settime(struct device
*dev
, struct rtc_time
*tm
)
104 struct sam9_rtc
*rtc
= dev_get_drvdata(dev
);
106 u32 offset
, alarm
, mr
;
109 dev_dbg(dev
, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "settime",
110 1900 + tm
->tm_year
, tm
->tm_mon
, tm
->tm_mday
,
111 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
113 err
= rtc_tm_to_time(tm
, &secs
);
117 mr
= rtt_readl(rtc
, MR
);
119 /* disable interrupts */
120 rtt_writel(rtc
, MR
, mr
& ~(AT91_RTT_ALMIEN
| AT91_RTT_RTTINCIEN
));
122 /* read current time offset */
123 offset
= gpbr_readl(rtc
);
125 /* store the new base time in a battery backup register */
127 gpbr_writel(rtc
, secs
);
129 /* adjust the alarm time for the new base */
130 alarm
= rtt_readl(rtc
, AR
);
131 if (alarm
!= ALARM_DISABLED
) {
133 /* time jumped backwards, increase time until alarm */
134 alarm
+= (offset
- secs
);
135 } else if ((alarm
+ offset
) > secs
) {
136 /* time jumped forwards, decrease time until alarm */
137 alarm
-= (secs
- offset
);
139 /* time jumped past the alarm, disable alarm */
140 alarm
= ALARM_DISABLED
;
141 mr
&= ~AT91_RTT_ALMIEN
;
143 rtt_writel(rtc
, AR
, alarm
);
146 /* reset the timer, and re-enable interrupts */
147 rtt_writel(rtc
, MR
, mr
| AT91_RTT_RTTRST
);
152 static int at91_rtc_readalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
154 struct sam9_rtc
*rtc
= dev_get_drvdata(dev
);
155 struct rtc_time
*tm
= &alrm
->time
;
156 u32 alarm
= rtt_readl(rtc
, AR
);
159 offset
= gpbr_readl(rtc
);
163 memset(alrm
, 0, sizeof(alrm
));
164 if (alarm
!= ALARM_DISABLED
&& offset
!= 0) {
165 rtc_time_to_tm(offset
+ alarm
, tm
);
167 dev_dbg(dev
, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "readalarm",
168 1900 + tm
->tm_year
, tm
->tm_mon
, tm
->tm_mday
,
169 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
171 if (rtt_readl(rtc
, MR
) & AT91_RTT_ALMIEN
)
178 static int at91_rtc_setalarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
180 struct sam9_rtc
*rtc
= dev_get_drvdata(dev
);
181 struct rtc_time
*tm
= &alrm
->time
;
187 err
= rtc_tm_to_time(tm
, &secs
);
191 offset
= gpbr_readl(rtc
);
193 /* time is not set */
196 mr
= rtt_readl(rtc
, MR
);
197 rtt_writel(rtc
, MR
, mr
& ~AT91_RTT_ALMIEN
);
199 /* alarm in the past? finish and leave disabled */
200 if (secs
<= offset
) {
201 rtt_writel(rtc
, AR
, ALARM_DISABLED
);
205 /* else set alarm and maybe enable it */
206 rtt_writel(rtc
, AR
, secs
- offset
);
208 rtt_writel(rtc
, MR
, mr
| AT91_RTT_ALMIEN
);
210 dev_dbg(dev
, "%s: %4d-%02d-%02d %02d:%02d:%02d\n", "setalarm",
211 tm
->tm_year
, tm
->tm_mon
, tm
->tm_mday
, tm
->tm_hour
,
212 tm
->tm_min
, tm
->tm_sec
);
218 * Handle commands from user-space
220 static int at91_rtc_ioctl(struct device
*dev
, unsigned int cmd
,
223 struct sam9_rtc
*rtc
= dev_get_drvdata(dev
);
225 u32 mr
= rtt_readl(rtc
, MR
);
227 dev_dbg(dev
, "ioctl: cmd=%08x, arg=%08lx, mr %08x\n", cmd
, arg
, mr
);
230 case RTC_AIE_OFF
: /* alarm off */
231 rtt_writel(rtc
, MR
, mr
& ~AT91_RTT_ALMIEN
);
233 case RTC_AIE_ON
: /* alarm on */
234 rtt_writel(rtc
, MR
, mr
| AT91_RTT_ALMIEN
);
236 case RTC_UIE_OFF
: /* update off */
237 rtt_writel(rtc
, MR
, mr
& ~AT91_RTT_RTTINCIEN
);
239 case RTC_UIE_ON
: /* update on */
240 rtt_writel(rtc
, MR
, mr
| AT91_RTT_RTTINCIEN
);
251 * Provide additional RTC information in /proc/driver/rtc
253 static int at91_rtc_proc(struct device
*dev
, struct seq_file
*seq
)
255 struct sam9_rtc
*rtc
= dev_get_drvdata(dev
);
256 u32 mr
= mr
= rtt_readl(rtc
, MR
);
258 seq_printf(seq
, "update_IRQ\t: %s\n",
259 (mr
& AT91_RTT_RTTINCIEN
) ? "yes" : "no");
264 * IRQ handler for the RTC
266 static irqreturn_t
at91_rtc_interrupt(int irq
, void *_rtc
)
268 struct sam9_rtc
*rtc
= _rtc
;
270 unsigned long events
= 0;
272 /* Shared interrupt may be for another device. Note: reading
273 * SR clears it, so we must only read it in this irq handler!
275 mr
= rtt_readl(rtc
, MR
) & (AT91_RTT_ALMIEN
| AT91_RTT_RTTINCIEN
);
276 sr
= rtt_readl(rtc
, SR
) & (mr
>> 16);
281 if (sr
& AT91_RTT_ALMS
)
282 events
|= (RTC_AF
| RTC_IRQF
);
284 /* timer update/increment */
285 if (sr
& AT91_RTT_RTTINC
)
286 events
|= (RTC_UF
| RTC_IRQF
);
288 rtc_update_irq(rtc
->rtcdev
, 1, events
);
290 pr_debug("%s: num=%ld, events=0x%02lx\n", __func__
,
291 events
>> 8, events
& 0x000000FF);
296 static const struct rtc_class_ops at91_rtc_ops
= {
297 .ioctl
= at91_rtc_ioctl
,
298 .read_time
= at91_rtc_readtime
,
299 .set_time
= at91_rtc_settime
,
300 .read_alarm
= at91_rtc_readalarm
,
301 .set_alarm
= at91_rtc_setalarm
,
302 .proc
= at91_rtc_proc
,
306 * Initialize and install RTC driver
308 static int __init
at91_rtc_probe(struct platform_device
*pdev
)
311 struct sam9_rtc
*rtc
;
315 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
319 rtc
= kzalloc(sizeof *rtc
, GFP_KERNEL
);
323 /* platform setup code should have handled this; sigh */
324 if (!device_can_wakeup(&pdev
->dev
))
325 device_init_wakeup(&pdev
->dev
, 1);
327 platform_set_drvdata(pdev
, rtc
);
328 rtc
->rtt
= (void __force __iomem
*) (AT91_VA_BASE_SYS
- AT91_BASE_SYS
);
329 rtc
->rtt
+= r
->start
;
331 mr
= rtt_readl(rtc
, MR
);
333 /* unless RTT is counting at 1 Hz, re-initialize it */
334 if ((mr
& AT91_RTT_RTPRES
) != AT91_SLOW_CLOCK
) {
335 mr
= AT91_RTT_RTTRST
| (AT91_SLOW_CLOCK
& AT91_RTT_RTPRES
);
339 /* disable all interrupts (same as on shutdown path) */
340 mr
&= ~(AT91_RTT_ALMIEN
| AT91_RTT_RTTINCIEN
);
341 rtt_writel(rtc
, MR
, mr
);
343 rtc
->rtcdev
= rtc_device_register(pdev
->name
, &pdev
->dev
,
344 &at91_rtc_ops
, THIS_MODULE
);
345 if (IS_ERR(rtc
->rtcdev
)) {
346 ret
= PTR_ERR(rtc
->rtcdev
);
350 /* register irq handler after we know what name we'll use */
351 ret
= request_irq(AT91_ID_SYS
, at91_rtc_interrupt
,
352 IRQF_DISABLED
| IRQF_SHARED
,
353 rtc
->rtcdev
->dev
.bus_id
, rtc
);
355 dev_dbg(&pdev
->dev
, "can't share IRQ %d?\n", AT91_ID_SYS
);
356 rtc_device_unregister(rtc
->rtcdev
);
360 /* NOTE: sam9260 rev A silicon has a ROM bug which resets the
361 * RTT on at least some reboots. If you have that chip, you must
362 * initialize the time from some external source like a GPS, wall
363 * clock, discrete RTC, etc
366 if (gpbr_readl(rtc
) == 0)
367 dev_warn(&pdev
->dev
, "%s: SET TIME!\n",
368 rtc
->rtcdev
->dev
.bus_id
);
373 platform_set_drvdata(pdev
, NULL
);
379 * Disable and remove the RTC driver
381 static int __exit
at91_rtc_remove(struct platform_device
*pdev
)
383 struct sam9_rtc
*rtc
= platform_get_drvdata(pdev
);
384 u32 mr
= rtt_readl(rtc
, MR
);
386 /* disable all interrupts */
387 rtt_writel(rtc
, MR
, mr
& ~(AT91_RTT_ALMIEN
| AT91_RTT_RTTINCIEN
));
388 free_irq(AT91_ID_SYS
, rtc
);
390 rtc_device_unregister(rtc
->rtcdev
);
392 platform_set_drvdata(pdev
, NULL
);
397 static void at91_rtc_shutdown(struct platform_device
*pdev
)
399 struct sam9_rtc
*rtc
= platform_get_drvdata(pdev
);
400 u32 mr
= rtt_readl(rtc
, MR
);
402 rtc
->imr
= mr
& (AT91_RTT_ALMIEN
| AT91_RTT_RTTINCIEN
);
403 rtt_writel(rtc
, MR
, mr
& ~rtc
->imr
);
408 /* AT91SAM9 RTC Power management control */
410 static int at91_rtc_suspend(struct platform_device
*pdev
,
413 struct sam9_rtc
*rtc
= platform_get_drvdata(pdev
);
414 u32 mr
= rtt_readl(rtc
, MR
);
417 * This IRQ is shared with DBGU and other hardware which isn't
418 * necessarily a wakeup event source.
420 rtc
->imr
= mr
& (AT91_RTT_ALMIEN
| AT91_RTT_RTTINCIEN
);
422 if (device_may_wakeup(&pdev
->dev
) && (mr
& AT91_RTT_ALMIEN
)) {
423 enable_irq_wake(AT91_ID_SYS
);
424 /* don't let RTTINC cause wakeups */
425 if (mr
& AT91_RTT_RTTINCIEN
)
426 rtt_writel(rtc
, MR
, mr
& ~AT91_RTT_RTTINCIEN
);
428 rtt_writel(rtc
, MR
, mr
& ~rtc
->imr
);
434 static int at91_rtc_resume(struct platform_device
*pdev
)
436 struct sam9_rtc
*rtc
= platform_get_drvdata(pdev
);
440 if (device_may_wakeup(&pdev
->dev
))
441 disable_irq_wake(AT91_ID_SYS
);
442 mr
= rtt_readl(rtc
, MR
);
443 rtt_writel(rtc
, MR
, mr
| rtc
->imr
);
449 #define at91_rtc_suspend NULL
450 #define at91_rtc_resume NULL
453 static struct platform_driver at91_rtc_driver
= {
454 .driver
.name
= "rtc-at91sam9",
455 .driver
.owner
= THIS_MODULE
,
456 .remove
= __exit_p(at91_rtc_remove
),
457 .shutdown
= at91_rtc_shutdown
,
458 .suspend
= at91_rtc_suspend
,
459 .resume
= at91_rtc_resume
,
462 /* Chips can have more than one RTT module, and they can be used for more
463 * than just RTCs. So we can't just register as "the" RTT driver.
465 * A normal approach in such cases is to create a library to allocate and
466 * free the modules. Here we just use bus_find_device() as like such a
467 * library, binding directly ... no runtime "library" footprint is needed.
469 static int __init
at91_rtc_match(struct device
*dev
, void *v
)
471 struct platform_device
*pdev
= to_platform_device(dev
);
474 /* continue searching if this isn't the RTT we need */
475 if (strcmp("at91_rtt", pdev
->name
) != 0
476 || pdev
->id
!= CONFIG_RTC_DRV_AT91SAM9_RTT
)
479 /* else we found it ... but fail unless we can bind to the RTC driver */
481 dev_dbg(dev
, "busy, can't use as RTC!\n");
484 dev
->driver
= &at91_rtc_driver
.driver
;
485 if (device_attach(dev
) == 0) {
486 dev_dbg(dev
, "can't attach RTC!\n");
489 ret
= at91_rtc_probe(pdev
);
493 dev_dbg(dev
, "RTC probe err %d!\n", ret
);
498 static int __init
at91_rtc_init(void)
503 status
= platform_driver_register(&at91_rtc_driver
);
506 rtc
= bus_find_device(&platform_bus_type
, NULL
,
507 NULL
, at91_rtc_match
);
509 platform_driver_unregister(&at91_rtc_driver
);
510 return rtc
? 0 : -ENODEV
;
512 module_init(at91_rtc_init
);
514 static void __exit
at91_rtc_exit(void)
516 platform_driver_unregister(&at91_rtc_driver
);
518 module_exit(at91_rtc_exit
);
521 MODULE_AUTHOR("Michel Benoit");
522 MODULE_DESCRIPTION("RTC driver for Atmel AT91SAM9x");
523 MODULE_LICENSE("GPL");