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 t->time.tm_mday = CMOS_READ(cmos->day_alrm);
124 if (!t->time.tm_mday)
125 t->time.tm_mday = -1;
127 if (cmos->mon_alrm) {
128 t->time.tm_mon = CMOS_READ(cmos->mon_alrm);
134 rtc_control = CMOS_READ(RTC_CONTROL);
135 spin_unlock_irq(&rtc_lock);
137 /* REVISIT this assumes PC style usage: always BCD */
139 if (((unsigned)t->time.tm_sec) < 0x60)
140 t->time.tm_sec = BCD2BIN(t->time.tm_sec);
143 if (((unsigned)t->time.tm_min) < 0x60)
144 t->time.tm_min = BCD2BIN(t->time.tm_min);
147 if (((unsigned)t->time.tm_hour) < 0x24)
148 t->time.tm_hour = BCD2BIN(t->time.tm_hour);
150 t->time.tm_hour = -1;
152 if (cmos->day_alrm) {
153 if (((unsigned)t->time.tm_mday) <= 0x31)
154 t->time.tm_mday = BCD2BIN(t->time.tm_mday);
156 t->time.tm_mday = -1;
157 if (cmos->mon_alrm) {
158 if (((unsigned)t->time.tm_mon) <= 0x12)
159 t->time.tm_mon = BCD2BIN(t->time.tm_mon) - 1;
164 t->time.tm_year = -1;
166 t->enabled = !!(rtc_control & RTC_AIE);
172 static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
174 struct cmos_rtc *cmos = dev_get_drvdata(dev);
175 unsigned char mon, mday, hrs, min, sec;
176 unsigned char rtc_control, rtc_intr;
178 if (!is_valid_irq(cmos->irq))
181 /* REVISIT this assumes PC style usage: always BCD */
183 /* Writing 0xff means "don't care" or "match all". */
185 mon = t->time.tm_mon;
186 mon = (mon < 12) ? BIN2BCD(mon) : 0xff;
189 mday = t->time.tm_mday;
190 mday = (mday >= 1 && mday <= 31) ? BIN2BCD(mday) : 0xff;
192 hrs = t->time.tm_hour;
193 hrs = (hrs < 24) ? BIN2BCD(hrs) : 0xff;
195 min = t->time.tm_min;
196 min = (min < 60) ? BIN2BCD(min) : 0xff;
198 sec = t->time.tm_sec;
199 sec = (sec < 60) ? BIN2BCD(sec) : 0xff;
201 spin_lock_irq(&rtc_lock);
203 /* next rtc irq must not be from previous alarm setting */
204 rtc_control = CMOS_READ(RTC_CONTROL);
205 rtc_control &= ~RTC_AIE;
206 CMOS_WRITE(rtc_control, RTC_CONTROL);
207 rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
208 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
209 if (is_intr(rtc_intr))
210 rtc_update_irq(cmos->rtc, 1, rtc_intr);
213 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
214 CMOS_WRITE(min, RTC_MINUTES_ALARM);
215 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
217 /* the system may support an "enhanced" alarm */
218 if (cmos->day_alrm) {
219 CMOS_WRITE(mday, cmos->day_alrm);
221 CMOS_WRITE(mon, cmos->mon_alrm);
225 rtc_control |= RTC_AIE;
226 CMOS_WRITE(rtc_control, RTC_CONTROL);
227 rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
228 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
229 if (is_intr(rtc_intr))
230 rtc_update_irq(cmos->rtc, 1, rtc_intr);
233 spin_unlock_irq(&rtc_lock);
238 static int cmos_irq_set_freq(struct device *dev, int freq)
240 struct cmos_rtc *cmos = dev_get_drvdata(dev);
244 if (!is_valid_irq(cmos->irq))
247 /* 0 = no irqs; 1 = 2^15 Hz ... 15 = 2^0 Hz */
250 if (f-- > 16 || freq != (1 << f))
255 spin_lock_irqsave(&rtc_lock, flags);
256 CMOS_WRITE(RTC_REF_CLCK_32KHZ | f, RTC_FREQ_SELECT);
257 spin_unlock_irqrestore(&rtc_lock, flags);
262 static int cmos_irq_set_state(struct device *dev, int enabled)
264 struct cmos_rtc *cmos = dev_get_drvdata(dev);
265 unsigned char rtc_control, rtc_intr;
268 if (!is_valid_irq(cmos->irq))
271 spin_lock_irqsave(&rtc_lock, flags);
272 rtc_control = CMOS_READ(RTC_CONTROL);
275 rtc_control |= RTC_PIE;
277 rtc_control &= ~RTC_PIE;
279 CMOS_WRITE(rtc_control, RTC_CONTROL);
281 rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
282 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
283 if (is_intr(rtc_intr))
284 rtc_update_irq(cmos->rtc, 1, rtc_intr);
286 spin_unlock_irqrestore(&rtc_lock, flags);
290 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
293 cmos_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
295 struct cmos_rtc *cmos = dev_get_drvdata(dev);
296 unsigned char rtc_control, rtc_intr;
306 if (!is_valid_irq(cmos->irq))
313 spin_lock_irqsave(&rtc_lock, flags);
314 rtc_control = CMOS_READ(RTC_CONTROL);
316 case RTC_AIE_OFF: /* alarm off */
317 rtc_control &= ~RTC_AIE;
319 case RTC_AIE_ON: /* alarm on */
320 rtc_control |= RTC_AIE;
322 case RTC_UIE_OFF: /* update off */
323 rtc_control &= ~RTC_UIE;
325 case RTC_UIE_ON: /* update on */
326 rtc_control |= RTC_UIE;
328 case RTC_PIE_OFF: /* periodic off */
329 rtc_control &= ~RTC_PIE;
331 case RTC_PIE_ON: /* periodic on */
332 rtc_control |= RTC_PIE;
335 CMOS_WRITE(rtc_control, RTC_CONTROL);
336 rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
337 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
338 if (is_intr(rtc_intr))
339 rtc_update_irq(cmos->rtc, 1, rtc_intr);
340 spin_unlock_irqrestore(&rtc_lock, flags);
345 #define cmos_rtc_ioctl NULL
348 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
350 static int cmos_procfs(struct device *dev, struct seq_file *seq)
352 struct cmos_rtc *cmos = dev_get_drvdata(dev);
353 unsigned char rtc_control, valid;
355 spin_lock_irq(&rtc_lock);
356 rtc_control = CMOS_READ(RTC_CONTROL);
357 valid = CMOS_READ(RTC_VALID);
358 spin_unlock_irq(&rtc_lock);
360 /* NOTE: at least ICH6 reports battery status using a different
361 * (non-RTC) bit; and SQWE is ignored on many current systems.
363 return seq_printf(seq,
364 "periodic_IRQ\t: %s\n"
366 // "square_wave\t: %s\n"
369 "periodic_freq\t: %d\n"
370 "batt_status\t: %s\n",
371 (rtc_control & RTC_PIE) ? "yes" : "no",
372 (rtc_control & RTC_UIE) ? "yes" : "no",
373 // (rtc_control & RTC_SQWE) ? "yes" : "no",
374 // (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
375 (rtc_control & RTC_DST_EN) ? "yes" : "no",
377 (valid & RTC_VRT) ? "okay" : "dead");
381 #define cmos_procfs NULL
384 static const struct rtc_class_ops cmos_rtc_ops = {
385 .ioctl = cmos_rtc_ioctl,
386 .read_time = cmos_read_time,
387 .set_time = cmos_set_time,
388 .read_alarm = cmos_read_alarm,
389 .set_alarm = cmos_set_alarm,
391 .irq_set_freq = cmos_irq_set_freq,
392 .irq_set_state = cmos_irq_set_state,
395 /*----------------------------------------------------------------*/
397 static struct cmos_rtc cmos_rtc;
399 static irqreturn_t cmos_interrupt(int irq, void *p)
403 spin_lock(&rtc_lock);
404 irqstat = CMOS_READ(RTC_INTR_FLAGS);
405 irqstat &= (CMOS_READ(RTC_CONTROL) & RTC_IRQMASK) | RTC_IRQF;
406 spin_unlock(&rtc_lock);
408 if (is_intr(irqstat)) {
409 rtc_update_irq(p, 1, irqstat);
421 #define INITSECTION __init
424 static int INITSECTION
425 cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
427 struct cmos_rtc_board_info *info = dev->platform_data;
429 unsigned char rtc_control;
431 /* there can be only one ... */
438 cmos_rtc.irq = rtc_irq;
439 cmos_rtc.iomem = ports;
441 /* For ACPI systems extension info comes from the FADT. On others,
442 * board specific setup provides it as appropriate. Systems where
443 * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and
444 * some almost-clones) can provide hooks to make that behave.
447 cmos_rtc.day_alrm = info->rtc_day_alarm;
448 cmos_rtc.mon_alrm = info->rtc_mon_alarm;
449 cmos_rtc.century = info->rtc_century;
451 if (info->wake_on && info->wake_off) {
452 cmos_rtc.wake_on = info->wake_on;
453 cmos_rtc.wake_off = info->wake_off;
457 cmos_rtc.rtc = rtc_device_register(driver_name, dev,
458 &cmos_rtc_ops, THIS_MODULE);
459 if (IS_ERR(cmos_rtc.rtc))
460 return PTR_ERR(cmos_rtc.rtc);
463 dev_set_drvdata(dev, &cmos_rtc);
465 /* platform and pnp busses handle resources incompatibly.
467 * REVISIT for non-x86 systems we may need to handle io memory
468 * resources: ioremap them, and request_mem_region().
471 retval = request_resource(&ioport_resource, ports);
473 dev_dbg(dev, "i/o registers already in use\n");
477 rename_region(ports, cmos_rtc.rtc->dev.bus_id);
479 spin_lock_irq(&rtc_lock);
481 /* force periodic irq to CMOS reset default of 1024Hz;
483 * REVISIT it's been reported that at least one x86_64 ALI mobo
484 * doesn't use 32KHz here ... for portability we might need to
485 * do something about other clock frequencies.
487 CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT);
488 cmos_rtc.rtc->irq_freq = 1024;
492 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
493 * allegedly some older rtcs need that to handle irqs properly
495 rtc_control = CMOS_READ(RTC_CONTROL);
496 rtc_control &= ~(RTC_PIE | RTC_AIE | RTC_UIE);
497 CMOS_WRITE(rtc_control, RTC_CONTROL);
498 CMOS_READ(RTC_INTR_FLAGS);
500 spin_unlock_irq(&rtc_lock);
502 /* FIXME teach the alarm code how to handle binary mode;
503 * <asm-generic/rtc.h> doesn't know 12-hour mode either.
505 if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY))) {
506 dev_dbg(dev, "only 24-hr BCD mode supported\n");
511 if (is_valid_irq(rtc_irq))
512 retval = request_irq(rtc_irq, cmos_interrupt, IRQF_DISABLED,
513 cmos_rtc.rtc->dev.bus_id,
516 dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
520 /* REVISIT optionally make 50 or 114 bytes NVRAM available,
521 * like rtc-ds1553, rtc-ds1742 ... this will often include
522 * registers for century, and day/month alarm.
525 pr_info("%s: alarms up to one %s%s\n",
526 cmos_rtc.rtc->dev.bus_id,
527 is_valid_irq(rtc_irq)
533 cmos_rtc.century ? ", y3k" : ""
539 rename_region(ports, NULL);
541 rtc_device_unregister(cmos_rtc.rtc);
545 static void cmos_do_shutdown(void)
547 unsigned char rtc_control;
549 spin_lock_irq(&rtc_lock);
550 rtc_control = CMOS_READ(RTC_CONTROL);
551 rtc_control &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
552 CMOS_WRITE(rtc_control, RTC_CONTROL);
553 CMOS_READ(RTC_INTR_FLAGS);
554 spin_unlock_irq(&rtc_lock);
557 static void __exit cmos_do_remove(struct device *dev)
559 struct cmos_rtc *cmos = dev_get_drvdata(dev);
564 release_resource(cmos->iomem);
565 rename_region(cmos->iomem, NULL);
567 if (is_valid_irq(cmos->irq))
568 free_irq(cmos->irq, cmos_rtc.rtc);
570 rtc_device_unregister(cmos_rtc.rtc);
573 dev_set_drvdata(dev, NULL);
578 static int cmos_suspend(struct device *dev, pm_message_t mesg)
580 struct cmos_rtc *cmos = dev_get_drvdata(dev);
581 int do_wake = device_may_wakeup(dev);
584 /* only the alarm might be a wakeup event source */
585 spin_lock_irq(&rtc_lock);
586 cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL);
587 if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
588 unsigned char irqstat;
591 tmp &= ~(RTC_PIE|RTC_UIE);
593 tmp &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
594 CMOS_WRITE(tmp, RTC_CONTROL);
595 irqstat = CMOS_READ(RTC_INTR_FLAGS);
596 irqstat &= (tmp & RTC_IRQMASK) | RTC_IRQF;
597 if (is_intr(irqstat))
598 rtc_update_irq(cmos->rtc, 1, irqstat);
600 spin_unlock_irq(&rtc_lock);
603 cmos->enabled_wake = 1;
607 enable_irq_wake(cmos->irq);
610 pr_debug("%s: suspend%s, ctrl %02x\n",
611 cmos_rtc.rtc->dev.bus_id,
612 (tmp & RTC_AIE) ? ", alarm may wake" : "",
618 static int cmos_resume(struct device *dev)
620 struct cmos_rtc *cmos = dev_get_drvdata(dev);
621 unsigned char tmp = cmos->suspend_ctrl;
623 /* re-enable any irqs previously active */
624 if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
626 if (cmos->enabled_wake) {
630 disable_irq_wake(cmos->irq);
631 cmos->enabled_wake = 0;
634 spin_lock_irq(&rtc_lock);
635 CMOS_WRITE(tmp, RTC_CONTROL);
636 tmp = CMOS_READ(RTC_INTR_FLAGS);
637 tmp &= (cmos->suspend_ctrl & RTC_IRQMASK) | RTC_IRQF;
639 rtc_update_irq(cmos->rtc, 1, tmp);
640 spin_unlock_irq(&rtc_lock);
643 pr_debug("%s: resume, ctrl %02x\n",
644 cmos_rtc.rtc->dev.bus_id,
652 #define cmos_suspend NULL
653 #define cmos_resume NULL
656 /*----------------------------------------------------------------*/
658 /* The "CMOS" RTC normally lives on the platform_bus. On ACPI systems,
659 * the device node will always be created as a PNPACPI device.
664 #include <linux/pnp.h>
667 cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
669 /* REVISIT paranoia argues for a shutdown notifier, since PNP
670 * drivers can't provide shutdown() methods to disable IRQs.
671 * Or better yet, fix PNP to allow those methods...
673 if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0))
674 /* Some machines contain a PNP entry for the RTC, but
675 * don't define the IRQ. It should always be safe to
676 * hardcode it in these cases
678 return cmos_do_probe(&pnp->dev, &pnp->res.port_resource[0], 8);
680 return cmos_do_probe(&pnp->dev,
681 &pnp->res.port_resource[0],
682 pnp->res.irq_resource[0].start);
685 static void __exit cmos_pnp_remove(struct pnp_dev *pnp)
687 cmos_do_remove(&pnp->dev);
692 static int cmos_pnp_suspend(struct pnp_dev *pnp, pm_message_t mesg)
694 return cmos_suspend(&pnp->dev, mesg);
697 static int cmos_pnp_resume(struct pnp_dev *pnp)
699 return cmos_resume(&pnp->dev);
703 #define cmos_pnp_suspend NULL
704 #define cmos_pnp_resume NULL
708 static const struct pnp_device_id rtc_ids[] = {
709 { .id = "PNP0b00", },
710 { .id = "PNP0b01", },
711 { .id = "PNP0b02", },
714 MODULE_DEVICE_TABLE(pnp, rtc_ids);
716 static struct pnp_driver cmos_pnp_driver = {
717 .name = (char *) driver_name,
719 .probe = cmos_pnp_probe,
720 .remove = __exit_p(cmos_pnp_remove),
722 /* flag ensures resume() gets called, and stops syslog spam */
723 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
724 .suspend = cmos_pnp_suspend,
725 .resume = cmos_pnp_resume,
728 static int __init cmos_init(void)
730 return pnp_register_driver(&cmos_pnp_driver);
732 module_init(cmos_init);
734 static void __exit cmos_exit(void)
736 pnp_unregister_driver(&cmos_pnp_driver);
738 module_exit(cmos_exit);
742 /*----------------------------------------------------------------*/
744 /* Platform setup should have set up an RTC device, when PNP is
745 * unavailable ... this could happen even on (older) PCs.
748 static int __init cmos_platform_probe(struct platform_device *pdev)
750 return cmos_do_probe(&pdev->dev,
751 platform_get_resource(pdev, IORESOURCE_IO, 0),
752 platform_get_irq(pdev, 0));
755 static int __exit cmos_platform_remove(struct platform_device *pdev)
757 cmos_do_remove(&pdev->dev);
761 static void cmos_platform_shutdown(struct platform_device *pdev)
766 static struct platform_driver cmos_platform_driver = {
767 .remove = __exit_p(cmos_platform_remove),
768 .shutdown = cmos_platform_shutdown,
770 .name = (char *) driver_name,
771 .suspend = cmos_suspend,
772 .resume = cmos_resume,
776 static int __init cmos_init(void)
778 return platform_driver_probe(&cmos_platform_driver,
779 cmos_platform_probe);
781 module_init(cmos_init);
783 static void __exit cmos_exit(void)
785 platform_driver_unregister(&cmos_platform_driver);
787 module_exit(cmos_exit);
792 MODULE_AUTHOR("David Brownell");
793 MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
794 MODULE_LICENSE("GPL");