2 * RTC class driver for "CMOS RTC": PCs, ACPI, etc
4 * Copyright (C) 1996 Paul Gortmaker (drivers/char/rtc.c)
5 * Copyright (C) 2006 David Brownell (convert to new framework)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
14 * The original "cmos clock" chip was an MC146818 chip, now obsolete.
15 * That defined the register interface now provided by all PCs, some
16 * non-PC systems, and incorporated into ACPI. Modern PC chipsets
17 * integrate an MC146818 clone in their southbridge, and boards use
18 * that instead of discrete clones like the DS12887 or M48T86. There
19 * are also clones that connect using the LPC bus.
21 * That register API is also used directly by various other drivers
22 * (notably for integrated NVRAM), infrastructure (x86 has code to
23 * bypass the RTC framework, directly reading the RTC during boot
24 * and updating minutes/seconds for systems using NTP synch) and
25 * utilities (like userspace 'hwclock', if no /dev node exists).
27 * So **ALL** calls to CMOS_READ and CMOS_WRITE must be done with
28 * interrupts disabled, holding the global rtc_lock, to exclude those
29 * other drivers and utilities on correctly configured systems.
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
35 #include <linux/spinlock.h>
36 #include <linux/platform_device.h>
37 #include <linux/mod_devicetable.h>
39 /* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */
40 #include <asm-generic/rtc.h>
44 struct rtc_device *rtc;
47 struct resource *iomem;
49 void (*wake_on)(struct device *);
50 void (*wake_off)(struct device *);
55 /* newer hardware extends the original register set */
61 /* both platform and pnp busses use negative numbers for invalid irqs */
62 #define is_valid_irq(n) ((n) >= 0)
64 static const char driver_name[] = "rtc_cmos";
66 /* The RTC_INTR register may have e.g. RTC_PF set even if RTC_PIE is clear;
67 * always mask it against the irq enable bits in RTC_CONTROL. Bit values
68 * are the same: PF==PIE, AF=AIE, UF=UIE; so RTC_IRQMASK works with both.
70 #define RTC_IRQMASK (RTC_PF | RTC_AF | RTC_UF)
72 static inline int is_intr(u8 rtc_intr)
74 if (!(rtc_intr & RTC_IRQF))
76 return rtc_intr & RTC_IRQMASK;
79 /*----------------------------------------------------------------*/
81 static int cmos_read_time(struct device *dev, struct rtc_time *t)
83 /* REVISIT: if the clock has a "century" register, use
84 * that instead of the heuristic in get_rtc_time().
85 * That'll make Y3K compatility (year > 2070) easy!
91 static int cmos_set_time(struct device *dev, struct rtc_time *t)
93 /* REVISIT: set the "century" register if available
95 * NOTE: this ignores the issue whereby updating the seconds
96 * takes effect exactly 500ms after we write the register.
97 * (Also queueing and other delays before we get this far.)
99 return set_rtc_time(t);
102 static int cmos_read_alarm(struct device *dev, struct rtc_wkalrm *t)
104 struct cmos_rtc *cmos = dev_get_drvdata(dev);
105 unsigned char rtc_control;
107 if (!is_valid_irq(cmos->irq))
110 /* Basic alarms only support hour, minute, and seconds fields.
111 * Some also support day and month, for alarms up to a year in
114 t->time.tm_mday = -1;
117 spin_lock_irq(&rtc_lock);
118 t->time.tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
119 t->time.tm_min = CMOS_READ(RTC_MINUTES_ALARM);
120 t->time.tm_hour = CMOS_READ(RTC_HOURS_ALARM);
122 if (cmos->day_alrm) {
123 /* ignore upper bits on readback per ACPI spec */
124 t->time.tm_mday = CMOS_READ(cmos->day_alrm) & 0x3f;
125 if (!t->time.tm_mday)
126 t->time.tm_mday = -1;
128 if (cmos->mon_alrm) {
129 t->time.tm_mon = CMOS_READ(cmos->mon_alrm);
135 rtc_control = CMOS_READ(RTC_CONTROL);
136 spin_unlock_irq(&rtc_lock);
138 /* REVISIT this assumes PC style usage: always BCD */
140 if (((unsigned)t->time.tm_sec) < 0x60)
141 t->time.tm_sec = BCD2BIN(t->time.tm_sec);
144 if (((unsigned)t->time.tm_min) < 0x60)
145 t->time.tm_min = BCD2BIN(t->time.tm_min);
148 if (((unsigned)t->time.tm_hour) < 0x24)
149 t->time.tm_hour = BCD2BIN(t->time.tm_hour);
151 t->time.tm_hour = -1;
153 if (cmos->day_alrm) {
154 if (((unsigned)t->time.tm_mday) <= 0x31)
155 t->time.tm_mday = BCD2BIN(t->time.tm_mday);
157 t->time.tm_mday = -1;
158 if (cmos->mon_alrm) {
159 if (((unsigned)t->time.tm_mon) <= 0x12)
160 t->time.tm_mon = BCD2BIN(t->time.tm_mon) - 1;
165 t->time.tm_year = -1;
167 t->enabled = !!(rtc_control & RTC_AIE);
173 static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
175 struct cmos_rtc *cmos = dev_get_drvdata(dev);
176 unsigned char mon, mday, hrs, min, sec;
177 unsigned char rtc_control, rtc_intr;
179 if (!is_valid_irq(cmos->irq))
182 /* REVISIT this assumes PC style usage: always BCD */
184 /* Writing 0xff means "don't care" or "match all". */
186 mon = t->time.tm_mon;
187 mon = (mon < 12) ? BIN2BCD(mon) : 0xff;
190 mday = t->time.tm_mday;
191 mday = (mday >= 1 && mday <= 31) ? BIN2BCD(mday) : 0xff;
193 hrs = t->time.tm_hour;
194 hrs = (hrs < 24) ? BIN2BCD(hrs) : 0xff;
196 min = t->time.tm_min;
197 min = (min < 60) ? BIN2BCD(min) : 0xff;
199 sec = t->time.tm_sec;
200 sec = (sec < 60) ? BIN2BCD(sec) : 0xff;
202 spin_lock_irq(&rtc_lock);
204 /* next rtc irq must not be from previous alarm setting */
205 rtc_control = CMOS_READ(RTC_CONTROL);
206 rtc_control &= ~RTC_AIE;
207 CMOS_WRITE(rtc_control, RTC_CONTROL);
208 rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
209 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
210 if (is_intr(rtc_intr))
211 rtc_update_irq(cmos->rtc, 1, rtc_intr);
214 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
215 CMOS_WRITE(min, RTC_MINUTES_ALARM);
216 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
218 /* the system may support an "enhanced" alarm */
219 if (cmos->day_alrm) {
220 CMOS_WRITE(mday, cmos->day_alrm);
222 CMOS_WRITE(mon, cmos->mon_alrm);
226 rtc_control |= RTC_AIE;
227 CMOS_WRITE(rtc_control, RTC_CONTROL);
228 rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
229 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
230 if (is_intr(rtc_intr))
231 rtc_update_irq(cmos->rtc, 1, rtc_intr);
234 spin_unlock_irq(&rtc_lock);
239 static int cmos_irq_set_freq(struct device *dev, int freq)
241 struct cmos_rtc *cmos = dev_get_drvdata(dev);
245 if (!is_valid_irq(cmos->irq))
248 /* 0 = no irqs; 1 = 2^15 Hz ... 15 = 2^0 Hz */
254 spin_lock_irqsave(&rtc_lock, flags);
255 CMOS_WRITE(RTC_REF_CLCK_32KHZ | f, RTC_FREQ_SELECT);
256 spin_unlock_irqrestore(&rtc_lock, flags);
261 static int cmos_irq_set_state(struct device *dev, int enabled)
263 struct cmos_rtc *cmos = dev_get_drvdata(dev);
264 unsigned char rtc_control, rtc_intr;
267 if (!is_valid_irq(cmos->irq))
270 spin_lock_irqsave(&rtc_lock, flags);
271 rtc_control = CMOS_READ(RTC_CONTROL);
274 rtc_control |= RTC_PIE;
276 rtc_control &= ~RTC_PIE;
278 CMOS_WRITE(rtc_control, RTC_CONTROL);
280 rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
281 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
282 if (is_intr(rtc_intr))
283 rtc_update_irq(cmos->rtc, 1, rtc_intr);
285 spin_unlock_irqrestore(&rtc_lock, flags);
289 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
292 cmos_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
294 struct cmos_rtc *cmos = dev_get_drvdata(dev);
295 unsigned char rtc_control, rtc_intr;
305 if (!is_valid_irq(cmos->irq))
312 spin_lock_irqsave(&rtc_lock, flags);
313 rtc_control = CMOS_READ(RTC_CONTROL);
315 case RTC_AIE_OFF: /* alarm off */
316 rtc_control &= ~RTC_AIE;
318 case RTC_AIE_ON: /* alarm on */
319 rtc_control |= RTC_AIE;
321 case RTC_UIE_OFF: /* update off */
322 rtc_control &= ~RTC_UIE;
324 case RTC_UIE_ON: /* update on */
325 rtc_control |= RTC_UIE;
327 case RTC_PIE_OFF: /* periodic off */
328 rtc_control &= ~RTC_PIE;
330 case RTC_PIE_ON: /* periodic on */
331 rtc_control |= RTC_PIE;
334 CMOS_WRITE(rtc_control, RTC_CONTROL);
335 rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
336 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
337 if (is_intr(rtc_intr))
338 rtc_update_irq(cmos->rtc, 1, rtc_intr);
339 spin_unlock_irqrestore(&rtc_lock, flags);
344 #define cmos_rtc_ioctl NULL
347 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
349 static int cmos_procfs(struct device *dev, struct seq_file *seq)
351 struct cmos_rtc *cmos = dev_get_drvdata(dev);
352 unsigned char rtc_control, valid;
354 spin_lock_irq(&rtc_lock);
355 rtc_control = CMOS_READ(RTC_CONTROL);
356 valid = CMOS_READ(RTC_VALID);
357 spin_unlock_irq(&rtc_lock);
359 /* NOTE: at least ICH6 reports battery status using a different
360 * (non-RTC) bit; and SQWE is ignored on many current systems.
362 return seq_printf(seq,
363 "periodic_IRQ\t: %s\n"
365 // "square_wave\t: %s\n"
368 "periodic_freq\t: %d\n"
369 "batt_status\t: %s\n",
370 (rtc_control & RTC_PIE) ? "yes" : "no",
371 (rtc_control & RTC_UIE) ? "yes" : "no",
372 // (rtc_control & RTC_SQWE) ? "yes" : "no",
373 // (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
374 (rtc_control & RTC_DST_EN) ? "yes" : "no",
376 (valid & RTC_VRT) ? "okay" : "dead");
380 #define cmos_procfs NULL
383 static const struct rtc_class_ops cmos_rtc_ops = {
384 .ioctl = cmos_rtc_ioctl,
385 .read_time = cmos_read_time,
386 .set_time = cmos_set_time,
387 .read_alarm = cmos_read_alarm,
388 .set_alarm = cmos_set_alarm,
390 .irq_set_freq = cmos_irq_set_freq,
391 .irq_set_state = cmos_irq_set_state,
394 /*----------------------------------------------------------------*/
396 static struct cmos_rtc cmos_rtc;
398 static irqreturn_t cmos_interrupt(int irq, void *p)
402 spin_lock(&rtc_lock);
403 irqstat = CMOS_READ(RTC_INTR_FLAGS);
404 irqstat &= (CMOS_READ(RTC_CONTROL) & RTC_IRQMASK) | RTC_IRQF;
405 spin_unlock(&rtc_lock);
407 if (is_intr(irqstat)) {
408 rtc_update_irq(p, 1, irqstat);
420 #define INITSECTION __init
423 static int INITSECTION
424 cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
426 struct cmos_rtc_board_info *info = dev->platform_data;
428 unsigned char rtc_control;
430 /* there can be only one ... */
437 /* Claim I/O ports ASAP, minimizing conflict with legacy driver.
439 * REVISIT non-x86 systems may instead use memory space resources
440 * (needing ioremap etc), not i/o space resources like this ...
442 ports = request_region(ports->start,
443 ports->end + 1 - ports->start,
446 dev_dbg(dev, "i/o registers already in use\n");
450 cmos_rtc.irq = rtc_irq;
451 cmos_rtc.iomem = ports;
453 /* For ACPI systems extension info comes from the FADT. On others,
454 * board specific setup provides it as appropriate. Systems where
455 * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and
456 * some almost-clones) can provide hooks to make that behave.
459 cmos_rtc.day_alrm = info->rtc_day_alarm;
460 cmos_rtc.mon_alrm = info->rtc_mon_alarm;
461 cmos_rtc.century = info->rtc_century;
463 if (info->wake_on && info->wake_off) {
464 cmos_rtc.wake_on = info->wake_on;
465 cmos_rtc.wake_off = info->wake_off;
469 cmos_rtc.rtc = rtc_device_register(driver_name, dev,
470 &cmos_rtc_ops, THIS_MODULE);
471 if (IS_ERR(cmos_rtc.rtc)) {
472 retval = PTR_ERR(cmos_rtc.rtc);
477 dev_set_drvdata(dev, &cmos_rtc);
478 rename_region(ports, cmos_rtc.rtc->dev.bus_id);
480 spin_lock_irq(&rtc_lock);
482 /* force periodic irq to CMOS reset default of 1024Hz;
484 * REVISIT it's been reported that at least one x86_64 ALI mobo
485 * doesn't use 32KHz here ... for portability we might need to
486 * do something about other clock frequencies.
488 CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT);
489 cmos_rtc.rtc->irq_freq = 1024;
493 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
494 * allegedly some older rtcs need that to handle irqs properly
496 rtc_control = CMOS_READ(RTC_CONTROL);
497 rtc_control &= ~(RTC_PIE | RTC_AIE | RTC_UIE);
498 CMOS_WRITE(rtc_control, RTC_CONTROL);
499 CMOS_READ(RTC_INTR_FLAGS);
501 spin_unlock_irq(&rtc_lock);
503 /* FIXME teach the alarm code how to handle binary mode;
504 * <asm-generic/rtc.h> doesn't know 12-hour mode either.
506 if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY))) {
507 dev_dbg(dev, "only 24-hr BCD mode supported\n");
512 if (is_valid_irq(rtc_irq))
513 retval = request_irq(rtc_irq, cmos_interrupt, IRQF_DISABLED,
514 cmos_rtc.rtc->dev.bus_id,
517 dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
521 /* REVISIT optionally make 50 or 114 bytes NVRAM available,
522 * like rtc-ds1553, rtc-ds1742 ... this will often include
523 * registers for century, and day/month alarm.
526 pr_info("%s: alarms up to one %s%s\n",
527 cmos_rtc.rtc->dev.bus_id,
528 is_valid_irq(rtc_irq)
534 cmos_rtc.century ? ", y3k" : ""
541 rtc_device_unregister(cmos_rtc.rtc);
543 release_region(ports->start, ports->end + 1 - ports->start);
547 static void cmos_do_shutdown(void)
549 unsigned char rtc_control;
551 spin_lock_irq(&rtc_lock);
552 rtc_control = CMOS_READ(RTC_CONTROL);
553 rtc_control &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
554 CMOS_WRITE(rtc_control, RTC_CONTROL);
555 CMOS_READ(RTC_INTR_FLAGS);
556 spin_unlock_irq(&rtc_lock);
559 static void __exit cmos_do_remove(struct device *dev)
561 struct cmos_rtc *cmos = dev_get_drvdata(dev);
562 struct resource *ports;
566 if (is_valid_irq(cmos->irq))
567 free_irq(cmos->irq, cmos->rtc);
569 rtc_device_unregister(cmos->rtc);
573 release_region(ports->start, ports->end + 1 - ports->start);
577 dev_set_drvdata(dev, NULL);
582 static int cmos_suspend(struct device *dev, pm_message_t mesg)
584 struct cmos_rtc *cmos = dev_get_drvdata(dev);
585 int do_wake = device_may_wakeup(dev);
588 /* only the alarm might be a wakeup event source */
589 spin_lock_irq(&rtc_lock);
590 cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL);
591 if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
592 unsigned char irqstat;
595 tmp &= ~(RTC_PIE|RTC_UIE);
597 tmp &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
598 CMOS_WRITE(tmp, RTC_CONTROL);
599 irqstat = CMOS_READ(RTC_INTR_FLAGS);
600 irqstat &= (tmp & RTC_IRQMASK) | RTC_IRQF;
601 if (is_intr(irqstat))
602 rtc_update_irq(cmos->rtc, 1, irqstat);
604 spin_unlock_irq(&rtc_lock);
607 cmos->enabled_wake = 1;
611 enable_irq_wake(cmos->irq);
614 pr_debug("%s: suspend%s, ctrl %02x\n",
615 cmos_rtc.rtc->dev.bus_id,
616 (tmp & RTC_AIE) ? ", alarm may wake" : "",
622 static int cmos_resume(struct device *dev)
624 struct cmos_rtc *cmos = dev_get_drvdata(dev);
625 unsigned char tmp = cmos->suspend_ctrl;
627 /* re-enable any irqs previously active */
628 if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
630 if (cmos->enabled_wake) {
634 disable_irq_wake(cmos->irq);
635 cmos->enabled_wake = 0;
638 spin_lock_irq(&rtc_lock);
639 CMOS_WRITE(tmp, RTC_CONTROL);
640 tmp = CMOS_READ(RTC_INTR_FLAGS);
641 tmp &= (cmos->suspend_ctrl & RTC_IRQMASK) | RTC_IRQF;
643 rtc_update_irq(cmos->rtc, 1, tmp);
644 spin_unlock_irq(&rtc_lock);
647 pr_debug("%s: resume, ctrl %02x\n",
648 cmos_rtc.rtc->dev.bus_id,
656 #define cmos_suspend NULL
657 #define cmos_resume NULL
660 /*----------------------------------------------------------------*/
662 /* The "CMOS" RTC normally lives on the platform_bus. On ACPI systems,
663 * the device node will always be created as a PNPACPI device. Plus
664 * pre-ACPI PCs probably list it in the PNPBIOS tables.
669 #include <linux/pnp.h>
672 cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
674 /* REVISIT paranoia argues for a shutdown notifier, since PNP
675 * drivers can't provide shutdown() methods to disable IRQs.
676 * Or better yet, fix PNP to allow those methods...
678 if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0))
679 /* Some machines contain a PNP entry for the RTC, but
680 * don't define the IRQ. It should always be safe to
681 * hardcode it in these cases
683 return cmos_do_probe(&pnp->dev, &pnp->res.port_resource[0], 8);
685 return cmos_do_probe(&pnp->dev,
686 &pnp->res.port_resource[0],
687 pnp->res.irq_resource[0].start);
690 static void __exit cmos_pnp_remove(struct pnp_dev *pnp)
692 cmos_do_remove(&pnp->dev);
697 static int cmos_pnp_suspend(struct pnp_dev *pnp, pm_message_t mesg)
699 return cmos_suspend(&pnp->dev, mesg);
702 static int cmos_pnp_resume(struct pnp_dev *pnp)
704 return cmos_resume(&pnp->dev);
708 #define cmos_pnp_suspend NULL
709 #define cmos_pnp_resume NULL
713 static const struct pnp_device_id rtc_ids[] = {
714 { .id = "PNP0b00", },
715 { .id = "PNP0b01", },
716 { .id = "PNP0b02", },
719 MODULE_DEVICE_TABLE(pnp, rtc_ids);
721 static struct pnp_driver cmos_pnp_driver = {
722 .name = (char *) driver_name,
724 .probe = cmos_pnp_probe,
725 .remove = __exit_p(cmos_pnp_remove),
727 /* flag ensures resume() gets called, and stops syslog spam */
728 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
729 .suspend = cmos_pnp_suspend,
730 .resume = cmos_pnp_resume,
733 static int __init cmos_init(void)
735 return pnp_register_driver(&cmos_pnp_driver);
737 module_init(cmos_init);
739 static void __exit cmos_exit(void)
741 pnp_unregister_driver(&cmos_pnp_driver);
743 module_exit(cmos_exit);
747 /*----------------------------------------------------------------*/
749 /* Platform setup should have set up an RTC device, when PNP is
750 * unavailable ... this could happen even on (older) PCs.
753 static int __init cmos_platform_probe(struct platform_device *pdev)
755 return cmos_do_probe(&pdev->dev,
756 platform_get_resource(pdev, IORESOURCE_IO, 0),
757 platform_get_irq(pdev, 0));
760 static int __exit cmos_platform_remove(struct platform_device *pdev)
762 cmos_do_remove(&pdev->dev);
766 static void cmos_platform_shutdown(struct platform_device *pdev)
771 static struct platform_driver cmos_platform_driver = {
772 .remove = __exit_p(cmos_platform_remove),
773 .shutdown = cmos_platform_shutdown,
775 .name = (char *) driver_name,
776 .suspend = cmos_suspend,
777 .resume = cmos_resume,
781 static int __init cmos_init(void)
783 return platform_driver_probe(&cmos_platform_driver,
784 cmos_platform_probe);
786 module_init(cmos_init);
788 static void __exit cmos_exit(void)
790 platform_driver_unregister(&cmos_platform_driver);
792 module_exit(cmos_exit);
797 MODULE_AUTHOR("David Brownell");
798 MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
799 MODULE_LICENSE("GPL");