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>
24 #include <linux/of_device.h>
29 /* The OMAP1 RTC is a year/month/day/hours/minutes/seconds BCD clock
30 * with century-range alarm matching, driven by the 32kHz clock.
32 * The main user-visible ways it differs from PC RTCs are by omitting
33 * "don't care" alarm fields and sub-second periodic IRQs, and having
34 * an autoadjust mechanism to calibrate to the true oscillator rate.
36 * Board-specific wiring options include using split power mode with
37 * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
38 * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
39 * low power modes) for OMAP1 boards (OMAP-L138 has this built into
40 * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
43 #define DRIVER_NAME "omap_rtc"
45 #define OMAP_RTC_BASE 0xfffb4800
48 #define OMAP_RTC_SECONDS_REG 0x00
49 #define OMAP_RTC_MINUTES_REG 0x04
50 #define OMAP_RTC_HOURS_REG 0x08
51 #define OMAP_RTC_DAYS_REG 0x0C
52 #define OMAP_RTC_MONTHS_REG 0x10
53 #define OMAP_RTC_YEARS_REG 0x14
54 #define OMAP_RTC_WEEKS_REG 0x18
56 #define OMAP_RTC_ALARM_SECONDS_REG 0x20
57 #define OMAP_RTC_ALARM_MINUTES_REG 0x24
58 #define OMAP_RTC_ALARM_HOURS_REG 0x28
59 #define OMAP_RTC_ALARM_DAYS_REG 0x2c
60 #define OMAP_RTC_ALARM_MONTHS_REG 0x30
61 #define OMAP_RTC_ALARM_YEARS_REG 0x34
63 #define OMAP_RTC_CTRL_REG 0x40
64 #define OMAP_RTC_STATUS_REG 0x44
65 #define OMAP_RTC_INTERRUPTS_REG 0x48
67 #define OMAP_RTC_COMP_LSB_REG 0x4c
68 #define OMAP_RTC_COMP_MSB_REG 0x50
69 #define OMAP_RTC_OSC_REG 0x54
71 #define OMAP_RTC_KICK0_REG 0x6c
72 #define OMAP_RTC_KICK1_REG 0x70
74 /* OMAP_RTC_CTRL_REG bit fields: */
75 #define OMAP_RTC_CTRL_SPLIT (1<<7)
76 #define OMAP_RTC_CTRL_DISABLE (1<<6)
77 #define OMAP_RTC_CTRL_SET_32_COUNTER (1<<5)
78 #define OMAP_RTC_CTRL_TEST (1<<4)
79 #define OMAP_RTC_CTRL_MODE_12_24 (1<<3)
80 #define OMAP_RTC_CTRL_AUTO_COMP (1<<2)
81 #define OMAP_RTC_CTRL_ROUND_30S (1<<1)
82 #define OMAP_RTC_CTRL_STOP (1<<0)
84 /* OMAP_RTC_STATUS_REG bit fields: */
85 #define OMAP_RTC_STATUS_POWER_UP (1<<7)
86 #define OMAP_RTC_STATUS_ALARM (1<<6)
87 #define OMAP_RTC_STATUS_1D_EVENT (1<<5)
88 #define OMAP_RTC_STATUS_1H_EVENT (1<<4)
89 #define OMAP_RTC_STATUS_1M_EVENT (1<<3)
90 #define OMAP_RTC_STATUS_1S_EVENT (1<<2)
91 #define OMAP_RTC_STATUS_RUN (1<<1)
92 #define OMAP_RTC_STATUS_BUSY (1<<0)
94 /* OMAP_RTC_INTERRUPTS_REG bit fields: */
95 #define OMAP_RTC_INTERRUPTS_IT_ALARM (1<<3)
96 #define OMAP_RTC_INTERRUPTS_IT_TIMER (1<<2)
98 /* OMAP_RTC_KICKER values */
99 #define KICK0_VALUE 0x83e70b13
100 #define KICK1_VALUE 0x95a4f1e0
102 #define OMAP_RTC_HAS_KICKER 0x1
104 static void __iomem *rtc_base;
106 #define rtc_read(addr) readb(rtc_base + (addr))
107 #define rtc_write(val, addr) writeb(val, rtc_base + (addr))
109 #define rtc_writel(val, addr) writel(val, rtc_base + (addr))
112 /* we rely on the rtc framework to handle locking (rtc->ops_lock),
113 * so the only other requirement is that register accesses which
114 * require BUSY to be clear are made with IRQs locally disabled
116 static void rtc_wait_not_busy(void)
121 /* BUSY may stay active for 1/32768 second (~30 usec) */
122 for (count = 0; count < 50; count++) {
123 status = rtc_read(OMAP_RTC_STATUS_REG);
124 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0)
128 /* now we have ~15 usec to read/write various registers */
131 static irqreturn_t rtc_irq(int irq, void *rtc)
133 unsigned long events = 0;
136 irq_data = rtc_read(OMAP_RTC_STATUS_REG);
139 if (irq_data & OMAP_RTC_STATUS_ALARM) {
140 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
141 events |= RTC_IRQF | RTC_AF;
144 /* 1/sec periodic/update irq? */
145 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
146 events |= RTC_IRQF | RTC_UF;
148 rtc_update_irq(rtc, 1, events);
153 static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
159 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
161 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
163 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
165 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
171 /* this hardware doesn't support "don't care" alarm fields */
172 static int tm2bcd(struct rtc_time *tm)
174 if (rtc_valid_tm(tm) != 0)
177 tm->tm_sec = bin2bcd(tm->tm_sec);
178 tm->tm_min = bin2bcd(tm->tm_min);
179 tm->tm_hour = bin2bcd(tm->tm_hour);
180 tm->tm_mday = bin2bcd(tm->tm_mday);
182 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
185 if (tm->tm_year < 100 || tm->tm_year > 199)
187 tm->tm_year = bin2bcd(tm->tm_year - 100);
192 static void bcd2tm(struct rtc_time *tm)
194 tm->tm_sec = bcd2bin(tm->tm_sec);
195 tm->tm_min = bcd2bin(tm->tm_min);
196 tm->tm_hour = bcd2bin(tm->tm_hour);
197 tm->tm_mday = bcd2bin(tm->tm_mday);
198 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
200 tm->tm_year = bcd2bin(tm->tm_year) + 100;
204 static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
206 /* we don't report wday/yday/isdst ... */
210 tm->tm_sec = rtc_read(OMAP_RTC_SECONDS_REG);
211 tm->tm_min = rtc_read(OMAP_RTC_MINUTES_REG);
212 tm->tm_hour = rtc_read(OMAP_RTC_HOURS_REG);
213 tm->tm_mday = rtc_read(OMAP_RTC_DAYS_REG);
214 tm->tm_mon = rtc_read(OMAP_RTC_MONTHS_REG);
215 tm->tm_year = rtc_read(OMAP_RTC_YEARS_REG);
223 static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
230 rtc_write(tm->tm_year, OMAP_RTC_YEARS_REG);
231 rtc_write(tm->tm_mon, OMAP_RTC_MONTHS_REG);
232 rtc_write(tm->tm_mday, OMAP_RTC_DAYS_REG);
233 rtc_write(tm->tm_hour, OMAP_RTC_HOURS_REG);
234 rtc_write(tm->tm_min, OMAP_RTC_MINUTES_REG);
235 rtc_write(tm->tm_sec, OMAP_RTC_SECONDS_REG);
242 static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
247 alm->time.tm_sec = rtc_read(OMAP_RTC_ALARM_SECONDS_REG);
248 alm->time.tm_min = rtc_read(OMAP_RTC_ALARM_MINUTES_REG);
249 alm->time.tm_hour = rtc_read(OMAP_RTC_ALARM_HOURS_REG);
250 alm->time.tm_mday = rtc_read(OMAP_RTC_ALARM_DAYS_REG);
251 alm->time.tm_mon = rtc_read(OMAP_RTC_ALARM_MONTHS_REG);
252 alm->time.tm_year = rtc_read(OMAP_RTC_ALARM_YEARS_REG);
257 alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG)
258 & OMAP_RTC_INTERRUPTS_IT_ALARM);
263 static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
267 if (tm2bcd(&alm->time) < 0)
273 rtc_write(alm->time.tm_year, OMAP_RTC_ALARM_YEARS_REG);
274 rtc_write(alm->time.tm_mon, OMAP_RTC_ALARM_MONTHS_REG);
275 rtc_write(alm->time.tm_mday, OMAP_RTC_ALARM_DAYS_REG);
276 rtc_write(alm->time.tm_hour, OMAP_RTC_ALARM_HOURS_REG);
277 rtc_write(alm->time.tm_min, OMAP_RTC_ALARM_MINUTES_REG);
278 rtc_write(alm->time.tm_sec, OMAP_RTC_ALARM_SECONDS_REG);
280 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
282 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
284 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
285 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
292 static struct rtc_class_ops omap_rtc_ops = {
293 .read_time = omap_rtc_read_time,
294 .set_time = omap_rtc_set_time,
295 .read_alarm = omap_rtc_read_alarm,
296 .set_alarm = omap_rtc_set_alarm,
297 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
300 static int omap_rtc_alarm;
301 static int omap_rtc_timer;
303 #define OMAP_RTC_DATA_DA830_IDX 1
305 static struct platform_device_id omap_rtc_devtype[] = {
310 .driver_data = OMAP_RTC_HAS_KICKER,
314 MODULE_DEVICE_TABLE(platform, omap_rtc_devtype);
316 static const struct of_device_id omap_rtc_of_match[] = {
317 { .compatible = "ti,da830-rtc",
318 .data = &omap_rtc_devtype[OMAP_RTC_DATA_DA830_IDX],
322 MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
324 static int __init omap_rtc_probe(struct platform_device *pdev)
326 struct resource *res, *mem;
327 struct rtc_device *rtc;
329 const struct platform_device_id *id_entry;
330 const struct of_device_id *of_id;
332 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
334 pdev->id_entry = of_id->data;
336 omap_rtc_timer = platform_get_irq(pdev, 0);
337 if (omap_rtc_timer <= 0) {
338 pr_debug("%s: no update irq?\n", pdev->name);
342 omap_rtc_alarm = platform_get_irq(pdev, 1);
343 if (omap_rtc_alarm <= 0) {
344 pr_debug("%s: no alarm irq?\n", pdev->name);
348 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
350 pr_debug("%s: RTC resource data missing\n", pdev->name);
354 mem = request_mem_region(res->start, resource_size(res), pdev->name);
356 pr_debug("%s: RTC registers at %08x are not free\n",
357 pdev->name, res->start);
361 rtc_base = ioremap(res->start, resource_size(res));
363 pr_debug("%s: RTC registers can't be mapped\n", pdev->name);
367 id_entry = platform_get_device_id(pdev);
368 if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER)) {
369 rtc_writel(KICK0_VALUE, OMAP_RTC_KICK0_REG);
370 rtc_writel(KICK1_VALUE, OMAP_RTC_KICK1_REG);
373 rtc = rtc_device_register(pdev->name, &pdev->dev,
374 &omap_rtc_ops, THIS_MODULE);
376 pr_debug("%s: can't register RTC device, err %ld\n",
377 pdev->name, PTR_ERR(rtc));
380 platform_set_drvdata(pdev, rtc);
381 dev_set_drvdata(&rtc->dev, mem);
383 /* clear pending irqs, and set 1/second periodic,
384 * which we'll use instead of update irqs
386 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
388 /* clear old status */
389 reg = rtc_read(OMAP_RTC_STATUS_REG);
390 if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) {
391 pr_info("%s: RTC power up reset detected\n",
393 rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
395 if (reg & (u8) OMAP_RTC_STATUS_ALARM)
396 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
398 /* handle periodic and alarm irqs */
399 if (request_irq(omap_rtc_timer, rtc_irq, 0,
400 dev_name(&rtc->dev), rtc)) {
401 pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n",
402 pdev->name, omap_rtc_timer);
405 if ((omap_rtc_timer != omap_rtc_alarm) &&
406 (request_irq(omap_rtc_alarm, rtc_irq, 0,
407 dev_name(&rtc->dev), rtc))) {
408 pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n",
409 pdev->name, omap_rtc_alarm);
413 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
414 reg = rtc_read(OMAP_RTC_CTRL_REG);
415 if (reg & (u8) OMAP_RTC_CTRL_STOP)
416 pr_info("%s: already running\n", pdev->name);
418 /* force to 24 hour mode */
419 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP);
420 new_ctrl |= OMAP_RTC_CTRL_STOP;
422 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
424 * - Device wake-up capability setting should come through chip
425 * init logic. OMAP1 boards should initialize the "wakeup capable"
426 * flag in the platform device if the board is wired right for
427 * being woken up by RTC alarm. For OMAP-L138, this capability
428 * is built into the SoC by the "Deep Sleep" capability.
430 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
431 * rather than nPWRON_RESET, should forcibly enable split
432 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
433 * is write-only, and always reads as zero...)
436 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
437 pr_info("%s: split power mode\n", pdev->name);
440 rtc_write(new_ctrl, OMAP_RTC_CTRL_REG);
445 free_irq(omap_rtc_timer, rtc);
447 rtc_device_unregister(rtc);
449 if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER))
450 rtc_writel(0, OMAP_RTC_KICK0_REG);
453 release_mem_region(mem->start, resource_size(mem));
457 static int __exit omap_rtc_remove(struct platform_device *pdev)
459 struct rtc_device *rtc = platform_get_drvdata(pdev);
460 struct resource *mem = dev_get_drvdata(&rtc->dev);
461 const struct platform_device_id *id_entry =
462 platform_get_device_id(pdev);
464 device_init_wakeup(&pdev->dev, 0);
466 /* leave rtc running, but disable irqs */
467 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
469 free_irq(omap_rtc_timer, rtc);
471 if (omap_rtc_timer != omap_rtc_alarm)
472 free_irq(omap_rtc_alarm, rtc);
474 rtc_device_unregister(rtc);
475 if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER))
476 rtc_writel(0, OMAP_RTC_KICK0_REG);
478 release_mem_region(mem->start, resource_size(mem));
486 static int omap_rtc_suspend(struct platform_device *pdev, pm_message_t state)
488 irqstat = rtc_read(OMAP_RTC_INTERRUPTS_REG);
490 /* FIXME the RTC alarm is not currently acting as a wakeup event
491 * source, and in fact this enable() call is just saving a flag
492 * that's never used...
494 if (device_may_wakeup(&pdev->dev))
495 enable_irq_wake(omap_rtc_alarm);
497 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
502 static int omap_rtc_resume(struct platform_device *pdev)
504 if (device_may_wakeup(&pdev->dev))
505 disable_irq_wake(omap_rtc_alarm);
507 rtc_write(irqstat, OMAP_RTC_INTERRUPTS_REG);
512 #define omap_rtc_suspend NULL
513 #define omap_rtc_resume NULL
516 static void omap_rtc_shutdown(struct platform_device *pdev)
518 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
521 MODULE_ALIAS("platform:omap_rtc");
522 static struct platform_driver omap_rtc_driver = {
523 .remove = __exit_p(omap_rtc_remove),
524 .suspend = omap_rtc_suspend,
525 .resume = omap_rtc_resume,
526 .shutdown = omap_rtc_shutdown,
529 .owner = THIS_MODULE,
530 .of_match_table = of_match_ptr(omap_rtc_of_match),
532 .id_table = omap_rtc_devtype,
535 static int __init rtc_init(void)
537 return platform_driver_probe(&omap_rtc_driver, omap_rtc_probe);
539 module_init(rtc_init);
541 static void __exit rtc_exit(void)
543 platform_driver_unregister(&omap_rtc_driver);
545 module_exit(rtc_exit);
547 MODULE_AUTHOR("George G. Davis (and others)");
548 MODULE_LICENSE("GPL");