2 * TI OMAP1 Real Time Clock interface for Linux
4 * Copyright (C) 2003 MontaVista Software, Inc.
5 * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
7 * Copyright (C) 2006 David Brownell (new RTC framework)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/ioport.h>
19 #include <linux/delay.h>
20 #include <linux/rtc.h>
21 #include <linux/bcd.h>
22 #include <linux/platform_device.h>
25 #include <asm/mach/time.h>
28 /* The OMAP1 RTC is a year/month/day/hours/minutes/seconds BCD clock
29 * with century-range alarm matching, driven by the 32kHz clock.
31 * The main user-visible ways it differs from PC RTCs are by omitting
32 * "don't care" alarm fields and sub-second periodic IRQs, and having
33 * an autoadjust mechanism to calibrate to the true oscillator rate.
35 * Board-specific wiring options include using split power mode with
36 * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
37 * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
38 * low power modes). See the BOARD-SPECIFIC CUSTOMIZATION comment.
41 #define OMAP_RTC_BASE 0xfffb4800
44 #define OMAP_RTC_SECONDS_REG 0x00
45 #define OMAP_RTC_MINUTES_REG 0x04
46 #define OMAP_RTC_HOURS_REG 0x08
47 #define OMAP_RTC_DAYS_REG 0x0C
48 #define OMAP_RTC_MONTHS_REG 0x10
49 #define OMAP_RTC_YEARS_REG 0x14
50 #define OMAP_RTC_WEEKS_REG 0x18
52 #define OMAP_RTC_ALARM_SECONDS_REG 0x20
53 #define OMAP_RTC_ALARM_MINUTES_REG 0x24
54 #define OMAP_RTC_ALARM_HOURS_REG 0x28
55 #define OMAP_RTC_ALARM_DAYS_REG 0x2c
56 #define OMAP_RTC_ALARM_MONTHS_REG 0x30
57 #define OMAP_RTC_ALARM_YEARS_REG 0x34
59 #define OMAP_RTC_CTRL_REG 0x40
60 #define OMAP_RTC_STATUS_REG 0x44
61 #define OMAP_RTC_INTERRUPTS_REG 0x48
63 #define OMAP_RTC_COMP_LSB_REG 0x4c
64 #define OMAP_RTC_COMP_MSB_REG 0x50
65 #define OMAP_RTC_OSC_REG 0x54
67 /* OMAP_RTC_CTRL_REG bit fields: */
68 #define OMAP_RTC_CTRL_SPLIT (1<<7)
69 #define OMAP_RTC_CTRL_DISABLE (1<<6)
70 #define OMAP_RTC_CTRL_SET_32_COUNTER (1<<5)
71 #define OMAP_RTC_CTRL_TEST (1<<4)
72 #define OMAP_RTC_CTRL_MODE_12_24 (1<<3)
73 #define OMAP_RTC_CTRL_AUTO_COMP (1<<2)
74 #define OMAP_RTC_CTRL_ROUND_30S (1<<1)
75 #define OMAP_RTC_CTRL_STOP (1<<0)
77 /* OMAP_RTC_STATUS_REG bit fields: */
78 #define OMAP_RTC_STATUS_POWER_UP (1<<7)
79 #define OMAP_RTC_STATUS_ALARM (1<<6)
80 #define OMAP_RTC_STATUS_1D_EVENT (1<<5)
81 #define OMAP_RTC_STATUS_1H_EVENT (1<<4)
82 #define OMAP_RTC_STATUS_1M_EVENT (1<<3)
83 #define OMAP_RTC_STATUS_1S_EVENT (1<<2)
84 #define OMAP_RTC_STATUS_RUN (1<<1)
85 #define OMAP_RTC_STATUS_BUSY (1<<0)
87 /* OMAP_RTC_INTERRUPTS_REG bit fields: */
88 #define OMAP_RTC_INTERRUPTS_IT_ALARM (1<<3)
89 #define OMAP_RTC_INTERRUPTS_IT_TIMER (1<<2)
92 #define rtc_read(addr) omap_readb(OMAP_RTC_BASE + (addr))
93 #define rtc_write(val, addr) omap_writeb(val, OMAP_RTC_BASE + (addr))
96 /* platform_bus isn't hotpluggable, so for static linkage it'd be safe
97 * to get rid of probe() and remove() code ... too bad the driver struct
98 * remembers probe(), that's about 25% of the runtime footprint!!
103 #define __devexit __exit
104 #define __devexit_p __exit_p
108 /* we rely on the rtc framework to handle locking (rtc->ops_lock),
109 * so the only other requirement is that register accesses which
110 * require BUSY to be clear are made with IRQs locally disabled
112 static void rtc_wait_not_busy(void)
117 /* BUSY may stay active for 1/32768 second (~30 usec) */
118 for (count
= 0; count
< 50; count
++) {
119 status
= rtc_read(OMAP_RTC_STATUS_REG
);
120 if ((status
& (u8
)OMAP_RTC_STATUS_BUSY
) == 0)
124 /* now we have ~15 usec to read/write various registers */
127 static irqreturn_t
rtc_irq(int irq
, void *class_dev
)
129 unsigned long events
= 0;
132 irq_data
= rtc_read(OMAP_RTC_STATUS_REG
);
135 if (irq_data
& OMAP_RTC_STATUS_ALARM
) {
136 rtc_write(OMAP_RTC_STATUS_ALARM
, OMAP_RTC_STATUS_REG
);
137 events
|= RTC_IRQF
| RTC_AF
;
140 /* 1/sec periodic/update irq? */
141 if (irq_data
& OMAP_RTC_STATUS_1S_EVENT
)
142 events
|= RTC_IRQF
| RTC_UF
;
144 rtc_update_irq(class_dev
, 1, events
);
149 #ifdef CONFIG_RTC_INTF_DEV
152 omap_rtc_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
168 reg
= rtc_read(OMAP_RTC_INTERRUPTS_REG
);
170 /* AIE = Alarm Interrupt Enable */
172 reg
&= ~OMAP_RTC_INTERRUPTS_IT_ALARM
;
175 reg
|= OMAP_RTC_INTERRUPTS_IT_ALARM
;
177 /* UIE = Update Interrupt Enable (1/second) */
179 reg
&= ~OMAP_RTC_INTERRUPTS_IT_TIMER
;
182 reg
|= OMAP_RTC_INTERRUPTS_IT_TIMER
;
186 rtc_write(reg
, OMAP_RTC_INTERRUPTS_REG
);
193 #define omap_rtc_ioctl NULL
196 /* this hardware doesn't support "don't care" alarm fields */
197 static int tm2bcd(struct rtc_time
*tm
)
199 if (rtc_valid_tm(tm
) != 0)
202 tm
->tm_sec
= BIN2BCD(tm
->tm_sec
);
203 tm
->tm_min
= BIN2BCD(tm
->tm_min
);
204 tm
->tm_hour
= BIN2BCD(tm
->tm_hour
);
205 tm
->tm_mday
= BIN2BCD(tm
->tm_mday
);
207 tm
->tm_mon
= BIN2BCD(tm
->tm_mon
+ 1);
210 if (tm
->tm_year
< 100 || tm
->tm_year
> 199)
212 tm
->tm_year
= BIN2BCD(tm
->tm_year
- 100);
217 static void bcd2tm(struct rtc_time
*tm
)
219 tm
->tm_sec
= BCD2BIN(tm
->tm_sec
);
220 tm
->tm_min
= BCD2BIN(tm
->tm_min
);
221 tm
->tm_hour
= BCD2BIN(tm
->tm_hour
);
222 tm
->tm_mday
= BCD2BIN(tm
->tm_mday
);
223 tm
->tm_mon
= BCD2BIN(tm
->tm_mon
) - 1;
225 tm
->tm_year
= BCD2BIN(tm
->tm_year
) + 100;
229 static int omap_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
231 /* we don't report wday/yday/isdst ... */
235 tm
->tm_sec
= rtc_read(OMAP_RTC_SECONDS_REG
);
236 tm
->tm_min
= rtc_read(OMAP_RTC_MINUTES_REG
);
237 tm
->tm_hour
= rtc_read(OMAP_RTC_HOURS_REG
);
238 tm
->tm_mday
= rtc_read(OMAP_RTC_DAYS_REG
);
239 tm
->tm_mon
= rtc_read(OMAP_RTC_MONTHS_REG
);
240 tm
->tm_year
= rtc_read(OMAP_RTC_YEARS_REG
);
248 static int omap_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
255 rtc_write(tm
->tm_year
, OMAP_RTC_YEARS_REG
);
256 rtc_write(tm
->tm_mon
, OMAP_RTC_MONTHS_REG
);
257 rtc_write(tm
->tm_mday
, OMAP_RTC_DAYS_REG
);
258 rtc_write(tm
->tm_hour
, OMAP_RTC_HOURS_REG
);
259 rtc_write(tm
->tm_min
, OMAP_RTC_MINUTES_REG
);
260 rtc_write(tm
->tm_sec
, OMAP_RTC_SECONDS_REG
);
267 static int omap_rtc_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
272 alm
->time
.tm_sec
= rtc_read(OMAP_RTC_ALARM_SECONDS_REG
);
273 alm
->time
.tm_min
= rtc_read(OMAP_RTC_ALARM_MINUTES_REG
);
274 alm
->time
.tm_hour
= rtc_read(OMAP_RTC_ALARM_HOURS_REG
);
275 alm
->time
.tm_mday
= rtc_read(OMAP_RTC_ALARM_DAYS_REG
);
276 alm
->time
.tm_mon
= rtc_read(OMAP_RTC_ALARM_MONTHS_REG
);
277 alm
->time
.tm_year
= rtc_read(OMAP_RTC_ALARM_YEARS_REG
);
282 alm
->enabled
= !!(rtc_read(OMAP_RTC_INTERRUPTS_REG
)
283 & OMAP_RTC_INTERRUPTS_IT_ALARM
);
288 static int omap_rtc_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alm
)
292 /* Much userspace code uses RTC_ALM_SET, thus "don't care" for
293 * day/month/year specifies alarms up to 24 hours in the future.
294 * So we need to handle that ... but let's ignore the "don't care"
295 * values for hours/minutes/seconds.
297 if (alm
->time
.tm_mday
<= 0
298 && alm
->time
.tm_mon
< 0
299 && alm
->time
.tm_year
< 0) {
301 unsigned long now
, then
;
303 omap_rtc_read_time(dev
, &tm
);
304 rtc_tm_to_time(&tm
, &now
);
306 alm
->time
.tm_mday
= tm
.tm_mday
;
307 alm
->time
.tm_mon
= tm
.tm_mon
;
308 alm
->time
.tm_year
= tm
.tm_year
;
309 rtc_tm_to_time(&alm
->time
, &then
);
311 /* sometimes the alarm wraps into tomorrow */
313 rtc_time_to_tm(now
+ 24 * 60 * 60, &tm
);
314 alm
->time
.tm_mday
= tm
.tm_mday
;
315 alm
->time
.tm_mon
= tm
.tm_mon
;
316 alm
->time
.tm_year
= tm
.tm_year
;
320 if (tm2bcd(&alm
->time
) < 0)
326 rtc_write(alm
->time
.tm_year
, OMAP_RTC_ALARM_YEARS_REG
);
327 rtc_write(alm
->time
.tm_mon
, OMAP_RTC_ALARM_MONTHS_REG
);
328 rtc_write(alm
->time
.tm_mday
, OMAP_RTC_ALARM_DAYS_REG
);
329 rtc_write(alm
->time
.tm_hour
, OMAP_RTC_ALARM_HOURS_REG
);
330 rtc_write(alm
->time
.tm_min
, OMAP_RTC_ALARM_MINUTES_REG
);
331 rtc_write(alm
->time
.tm_sec
, OMAP_RTC_ALARM_SECONDS_REG
);
333 reg
= rtc_read(OMAP_RTC_INTERRUPTS_REG
);
335 reg
|= OMAP_RTC_INTERRUPTS_IT_ALARM
;
337 reg
&= ~OMAP_RTC_INTERRUPTS_IT_ALARM
;
338 rtc_write(reg
, OMAP_RTC_INTERRUPTS_REG
);
345 static struct rtc_class_ops omap_rtc_ops
= {
346 .ioctl
= omap_rtc_ioctl
,
347 .read_time
= omap_rtc_read_time
,
348 .set_time
= omap_rtc_set_time
,
349 .read_alarm
= omap_rtc_read_alarm
,
350 .set_alarm
= omap_rtc_set_alarm
,
353 static int omap_rtc_alarm
;
354 static int omap_rtc_timer
;
356 static int __devinit
omap_rtc_probe(struct platform_device
*pdev
)
358 struct resource
*res
, *mem
;
359 struct rtc_device
*rtc
;
362 omap_rtc_timer
= platform_get_irq(pdev
, 0);
363 if (omap_rtc_timer
<= 0) {
364 pr_debug("%s: no update irq?\n", pdev
->name
);
368 omap_rtc_alarm
= platform_get_irq(pdev
, 1);
369 if (omap_rtc_alarm
<= 0) {
370 pr_debug("%s: no alarm irq?\n", pdev
->name
);
374 /* NOTE: using static mapping for RTC registers */
375 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
376 if (res
&& res
->start
!= OMAP_RTC_BASE
) {
377 pr_debug("%s: RTC registers at %08x, expected %08x\n",
378 pdev
->name
, (unsigned) res
->start
, OMAP_RTC_BASE
);
383 mem
= request_mem_region(res
->start
,
384 res
->end
- res
->start
+ 1,
389 pr_debug("%s: RTC registers at %08x are not free\n",
390 pdev
->name
, OMAP_RTC_BASE
);
394 rtc
= rtc_device_register(pdev
->name
, &pdev
->dev
,
395 &omap_rtc_ops
, THIS_MODULE
);
397 pr_debug("%s: can't register RTC device, err %ld\n",
398 pdev
->name
, PTR_ERR(rtc
));
401 platform_set_drvdata(pdev
, rtc
);
402 class_set_devdata(&rtc
->class_dev
, mem
);
404 /* clear pending irqs, and set 1/second periodic,
405 * which we'll use instead of update irqs
407 rtc_write(0, OMAP_RTC_INTERRUPTS_REG
);
409 /* clear old status */
410 reg
= rtc_read(OMAP_RTC_STATUS_REG
);
411 if (reg
& (u8
) OMAP_RTC_STATUS_POWER_UP
) {
412 pr_info("%s: RTC power up reset detected\n",
414 rtc_write(OMAP_RTC_STATUS_POWER_UP
, OMAP_RTC_STATUS_REG
);
416 if (reg
& (u8
) OMAP_RTC_STATUS_ALARM
)
417 rtc_write(OMAP_RTC_STATUS_ALARM
, OMAP_RTC_STATUS_REG
);
419 /* handle periodic and alarm irqs */
420 if (request_irq(omap_rtc_timer
, rtc_irq
, IRQF_DISABLED
,
421 rtc
->class_dev
.class_id
, &rtc
->class_dev
)) {
422 pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n",
423 pdev
->name
, omap_rtc_timer
);
426 if (request_irq(omap_rtc_alarm
, rtc_irq
, IRQF_DISABLED
,
427 rtc
->class_dev
.class_id
, &rtc
->class_dev
)) {
428 pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n",
429 pdev
->name
, omap_rtc_alarm
);
433 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
434 reg
= rtc_read(OMAP_RTC_CTRL_REG
);
435 if (reg
& (u8
) OMAP_RTC_CTRL_STOP
)
436 pr_info("%s: already running\n", pdev
->name
);
438 /* force to 24 hour mode */
439 new_ctrl
= reg
& ~(OMAP_RTC_CTRL_SPLIT
|OMAP_RTC_CTRL_AUTO_COMP
);
440 new_ctrl
|= OMAP_RTC_CTRL_STOP
;
442 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
444 * - Boards wired so that RTC_WAKE_INT does something, and muxed
445 * right (W13_1610_RTC_WAKE_INT is the default after chip reset),
446 * should initialize the device wakeup flag appropriately.
448 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
449 * rather than nPWRON_RESET, should forcibly enable split
450 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
451 * is write-only, and always reads as zero...)
453 device_init_wakeup(&pdev
->dev
, 0);
455 if (new_ctrl
& (u8
) OMAP_RTC_CTRL_SPLIT
)
456 pr_info("%s: split power mode\n", pdev
->name
);
459 rtc_write(new_ctrl
, OMAP_RTC_CTRL_REG
);
464 free_irq(omap_rtc_timer
, NULL
);
466 rtc_device_unregister(rtc
);
468 release_resource(mem
);
472 static int __devexit
omap_rtc_remove(struct platform_device
*pdev
)
474 struct rtc_device
*rtc
= platform_get_drvdata(pdev
);;
476 device_init_wakeup(&pdev
->dev
, 0);
478 /* leave rtc running, but disable irqs */
479 rtc_write(0, OMAP_RTC_INTERRUPTS_REG
);
481 free_irq(omap_rtc_timer
, rtc
);
482 free_irq(omap_rtc_alarm
, rtc
);
484 release_resource(class_get_devdata(&rtc
->class_dev
));
485 rtc_device_unregister(rtc
);
491 static struct timespec rtc_delta
;
494 static int omap_rtc_suspend(struct platform_device
*pdev
, pm_message_t state
)
496 struct rtc_time rtc_tm
;
497 struct timespec time
;
500 omap_rtc_read_time(NULL
, &rtc_tm
);
501 rtc_tm_to_time(&rtc_tm
, &time
.tv_sec
);
503 save_time_delta(&rtc_delta
, &time
);
504 irqstat
= rtc_read(OMAP_RTC_INTERRUPTS_REG
);
506 /* FIXME the RTC alarm is not currently acting as a wakeup event
507 * source, and in fact this enable() call is just saving a flag
508 * that's never used...
510 if (device_may_wakeup(&pdev
->dev
))
511 enable_irq_wake(omap_rtc_alarm
);
513 rtc_write(0, OMAP_RTC_INTERRUPTS_REG
);
518 static int omap_rtc_resume(struct platform_device
*pdev
)
520 struct rtc_time rtc_tm
;
521 struct timespec time
;
524 omap_rtc_read_time(NULL
, &rtc_tm
);
525 rtc_tm_to_time(&rtc_tm
, &time
.tv_sec
);
527 restore_time_delta(&rtc_delta
, &time
);
528 if (device_may_wakeup(&pdev
->dev
))
529 disable_irq_wake(omap_rtc_alarm
);
531 rtc_write(irqstat
, OMAP_RTC_INTERRUPTS_REG
);
536 #define omap_rtc_suspend NULL
537 #define omap_rtc_resume NULL
540 static void omap_rtc_shutdown(struct platform_device
*pdev
)
542 rtc_write(0, OMAP_RTC_INTERRUPTS_REG
);
545 MODULE_ALIAS("omap_rtc");
546 static struct platform_driver omap_rtc_driver
= {
547 .probe
= omap_rtc_probe
,
548 .remove
= __devexit_p(omap_rtc_remove
),
549 .suspend
= omap_rtc_suspend
,
550 .resume
= omap_rtc_resume
,
551 .shutdown
= omap_rtc_shutdown
,
554 .owner
= THIS_MODULE
,
558 static int __init
rtc_init(void)
560 return platform_driver_register(&omap_rtc_driver
);
562 module_init(rtc_init
);
564 static void __exit
rtc_exit(void)
566 platform_driver_unregister(&omap_rtc_driver
);
568 module_exit(rtc_exit
);
570 MODULE_AUTHOR("George G. Davis (and others)");
571 MODULE_LICENSE("GPL");