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
;
51 /* newer hardware extends the original register set */
57 /* both platform and pnp busses use negative numbers for invalid irqs */
58 #define is_valid_irq(n) ((n) >= 0)
60 static const char driver_name
[] = "rtc_cmos";
62 /*----------------------------------------------------------------*/
64 static int cmos_read_time(struct device
*dev
, struct rtc_time
*t
)
66 /* REVISIT: if the clock has a "century" register, use
67 * that instead of the heuristic in get_rtc_time().
68 * That'll make Y3K compatility (year > 2070) easy!
74 static int cmos_set_time(struct device
*dev
, struct rtc_time
*t
)
76 /* REVISIT: set the "century" register if available
78 * NOTE: this ignores the issue whereby updating the seconds
79 * takes effect exactly 500ms after we write the register.
80 * (Also queueing and other delays before we get this far.)
82 return set_rtc_time(t
);
85 static int cmos_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
87 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
88 unsigned char rtc_control
;
90 if (!is_valid_irq(cmos
->irq
))
93 /* Basic alarms only support hour, minute, and seconds fields.
94 * Some also support day and month, for alarms up to a year in
100 spin_lock_irq(&rtc_lock
);
101 t
->time
.tm_sec
= CMOS_READ(RTC_SECONDS_ALARM
);
102 t
->time
.tm_min
= CMOS_READ(RTC_MINUTES_ALARM
);
103 t
->time
.tm_hour
= CMOS_READ(RTC_HOURS_ALARM
);
105 if (cmos
->day_alrm
) {
106 t
->time
.tm_mday
= CMOS_READ(cmos
->day_alrm
);
107 if (!t
->time
.tm_mday
)
108 t
->time
.tm_mday
= -1;
110 if (cmos
->mon_alrm
) {
111 t
->time
.tm_mon
= CMOS_READ(cmos
->mon_alrm
);
117 rtc_control
= CMOS_READ(RTC_CONTROL
);
118 spin_unlock_irq(&rtc_lock
);
120 /* REVISIT this assumes PC style usage: always BCD */
122 if (((unsigned)t
->time
.tm_sec
) < 0x60)
123 t
->time
.tm_sec
= BCD2BIN(t
->time
.tm_sec
);
126 if (((unsigned)t
->time
.tm_min
) < 0x60)
127 t
->time
.tm_min
= BCD2BIN(t
->time
.tm_min
);
130 if (((unsigned)t
->time
.tm_hour
) < 0x24)
131 t
->time
.tm_hour
= BCD2BIN(t
->time
.tm_hour
);
133 t
->time
.tm_hour
= -1;
135 if (cmos
->day_alrm
) {
136 if (((unsigned)t
->time
.tm_mday
) <= 0x31)
137 t
->time
.tm_mday
= BCD2BIN(t
->time
.tm_mday
);
139 t
->time
.tm_mday
= -1;
140 if (cmos
->mon_alrm
) {
141 if (((unsigned)t
->time
.tm_mon
) <= 0x12)
142 t
->time
.tm_mon
= BCD2BIN(t
->time
.tm_mon
) - 1;
147 t
->time
.tm_year
= -1;
149 t
->enabled
= !!(rtc_control
& RTC_AIE
);
155 static int cmos_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
157 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
158 unsigned char mon
, mday
, hrs
, min
, sec
;
159 unsigned char rtc_control
, rtc_intr
;
161 if (!is_valid_irq(cmos
->irq
))
164 /* REVISIT this assumes PC style usage: always BCD */
166 /* Writing 0xff means "don't care" or "match all". */
168 mon
= t
->time
.tm_mon
;
169 mon
= (mon
< 12) ? BIN2BCD(mon
) : 0xff;
172 mday
= t
->time
.tm_mday
;
173 mday
= (mday
>= 1 && mday
<= 31) ? BIN2BCD(mday
) : 0xff;
175 hrs
= t
->time
.tm_hour
;
176 hrs
= (hrs
< 24) ? BIN2BCD(hrs
) : 0xff;
178 min
= t
->time
.tm_min
;
179 min
= (min
< 60) ? BIN2BCD(min
) : 0xff;
181 sec
= t
->time
.tm_sec
;
182 sec
= (sec
< 60) ? BIN2BCD(sec
) : 0xff;
184 spin_lock_irq(&rtc_lock
);
186 /* next rtc irq must not be from previous alarm setting */
187 rtc_control
= CMOS_READ(RTC_CONTROL
);
188 rtc_control
&= ~RTC_AIE
;
189 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
190 rtc_intr
= CMOS_READ(RTC_INTR_FLAGS
);
192 rtc_update_irq(&cmos
->rtc
->class_dev
, 1, rtc_intr
);
195 CMOS_WRITE(hrs
, RTC_HOURS_ALARM
);
196 CMOS_WRITE(min
, RTC_MINUTES_ALARM
);
197 CMOS_WRITE(sec
, RTC_SECONDS_ALARM
);
199 /* the system may support an "enhanced" alarm */
200 if (cmos
->day_alrm
) {
201 CMOS_WRITE(mday
, cmos
->day_alrm
);
203 CMOS_WRITE(mon
, cmos
->mon_alrm
);
207 rtc_control
|= RTC_AIE
;
208 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
209 rtc_intr
= CMOS_READ(RTC_INTR_FLAGS
);
211 rtc_update_irq(&cmos
->rtc
->class_dev
, 1, rtc_intr
);
214 spin_unlock_irq(&rtc_lock
);
219 static int cmos_set_freq(struct device
*dev
, int freq
)
221 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
225 if (!is_valid_irq(cmos
->irq
))
228 /* 0 = no irqs; 1 = 2^15 Hz ... 15 = 2^0 Hz */
231 if (f
-- > 16 || freq
!= (1 << f
))
236 spin_lock_irqsave(&rtc_lock
, flags
);
237 CMOS_WRITE(RTC_REF_CLCK_32KHZ
| f
, RTC_FREQ_SELECT
);
238 spin_unlock_irqrestore(&rtc_lock
, flags
);
243 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
246 cmos_rtc_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
248 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
249 unsigned char rtc_control
, rtc_intr
;
259 if (!is_valid_irq(cmos
->irq
))
266 spin_lock_irqsave(&rtc_lock
, flags
);
267 rtc_control
= CMOS_READ(RTC_CONTROL
);
269 case RTC_AIE_OFF
: /* alarm off */
270 rtc_control
&= ~RTC_AIE
;
272 case RTC_AIE_ON
: /* alarm on */
273 rtc_control
|= RTC_AIE
;
275 case RTC_UIE_OFF
: /* update off */
276 rtc_control
&= ~RTC_UIE
;
278 case RTC_UIE_ON
: /* update on */
279 rtc_control
|= RTC_UIE
;
281 case RTC_PIE_OFF
: /* periodic off */
282 rtc_control
&= ~RTC_PIE
;
284 case RTC_PIE_ON
: /* periodic on */
285 rtc_control
|= RTC_PIE
;
288 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
289 rtc_intr
= CMOS_READ(RTC_INTR_FLAGS
);
291 rtc_update_irq(&cmos
->rtc
->class_dev
, 1, rtc_intr
);
292 spin_unlock_irqrestore(&rtc_lock
, flags
);
297 #define cmos_rtc_ioctl NULL
300 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
302 static int cmos_procfs(struct device
*dev
, struct seq_file
*seq
)
304 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
305 unsigned char rtc_control
, valid
;
307 spin_lock_irq(&rtc_lock
);
308 rtc_control
= CMOS_READ(RTC_CONTROL
);
309 valid
= CMOS_READ(RTC_VALID
);
310 spin_unlock_irq(&rtc_lock
);
312 /* NOTE: at least ICH6 reports battery status using a different
313 * (non-RTC) bit; and SQWE is ignored on many current systems.
315 return seq_printf(seq
,
316 "periodic_IRQ\t: %s\n"
318 // "square_wave\t: %s\n"
321 "periodic_freq\t: %d\n"
322 "batt_status\t: %s\n",
323 (rtc_control
& RTC_PIE
) ? "yes" : "no",
324 (rtc_control
& RTC_UIE
) ? "yes" : "no",
325 // (rtc_control & RTC_SQWE) ? "yes" : "no",
326 // (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
327 (rtc_control
& RTC_DST_EN
) ? "yes" : "no",
329 (valid
& RTC_VRT
) ? "okay" : "dead");
333 #define cmos_procfs NULL
336 static const struct rtc_class_ops cmos_rtc_ops
= {
337 .ioctl
= cmos_rtc_ioctl
,
338 .read_time
= cmos_read_time
,
339 .set_time
= cmos_set_time
,
340 .read_alarm
= cmos_read_alarm
,
341 .set_alarm
= cmos_set_alarm
,
343 .irq_set_freq
= cmos_set_freq
,
346 /*----------------------------------------------------------------*/
348 static struct cmos_rtc cmos_rtc
;
350 static irqreturn_t
cmos_interrupt(int irq
, void *p
)
354 spin_lock(&rtc_lock
);
355 irqstat
= CMOS_READ(RTC_INTR_FLAGS
);
356 spin_unlock(&rtc_lock
);
359 /* NOTE: irqstat may have e.g. RTC_PF set
360 * even when RTC_PIE is clear...
362 rtc_update_irq(p
, 1, irqstat
);
368 #ifdef CONFIG_PNPACPI
369 #define is_pnpacpi() 1
373 #define is_pnpacpi() 0
374 #define INITSECTION __init
377 static int INITSECTION
378 cmos_do_probe(struct device
*dev
, struct resource
*ports
, int rtc_irq
)
380 struct cmos_rtc_board_info
*info
= dev
->platform_data
;
382 unsigned char rtc_control
;
384 /* there can be only one ... */
391 cmos_rtc
.irq
= rtc_irq
;
392 cmos_rtc
.iomem
= ports
;
394 /* For ACPI systems the info comes from the FADT. On others,
395 * board specific setup provides it as appropriate.
398 cmos_rtc
.day_alrm
= info
->rtc_day_alarm
;
399 cmos_rtc
.mon_alrm
= info
->rtc_mon_alarm
;
400 cmos_rtc
.century
= info
->rtc_century
;
403 cmos_rtc
.rtc
= rtc_device_register(driver_name
, dev
,
404 &cmos_rtc_ops
, THIS_MODULE
);
405 if (IS_ERR(cmos_rtc
.rtc
))
406 return PTR_ERR(cmos_rtc
.rtc
);
409 dev_set_drvdata(dev
, &cmos_rtc
);
411 /* platform and pnp busses handle resources incompatibly.
413 * REVISIT for non-x86 systems we may need to handle io memory
414 * resources: ioremap them, and request_mem_region().
417 retval
= request_resource(&ioport_resource
, ports
);
419 dev_dbg(dev
, "i/o registers already in use\n");
423 rename_region(ports
, cmos_rtc
.rtc
->class_dev
.class_id
);
425 spin_lock_irq(&rtc_lock
);
427 /* force periodic irq to CMOS reset default of 1024Hz;
429 * REVISIT it's been reported that at least one x86_64 ALI mobo
430 * doesn't use 32KHz here ... for portability we might need to
431 * do something about other clock frequencies.
433 CMOS_WRITE(RTC_REF_CLCK_32KHZ
| 0x06, RTC_FREQ_SELECT
);
434 cmos_rtc
.rtc
->irq_freq
= 1024;
438 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
439 * allegedly some older rtcs need that to handle irqs properly
441 rtc_control
= CMOS_READ(RTC_CONTROL
);
442 rtc_control
&= ~(RTC_PIE
| RTC_AIE
| RTC_UIE
);
443 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
444 CMOS_READ(RTC_INTR_FLAGS
);
446 spin_unlock_irq(&rtc_lock
);
448 /* FIXME teach the alarm code how to handle binary mode;
449 * <asm-generic/rtc.h> doesn't know 12-hour mode either.
451 if (!(rtc_control
& RTC_24H
) || (rtc_control
& (RTC_DM_BINARY
))) {
452 dev_dbg(dev
, "only 24-hr BCD mode supported\n");
457 if (is_valid_irq(rtc_irq
))
458 retval
= request_irq(rtc_irq
, cmos_interrupt
, IRQF_DISABLED
,
459 cmos_rtc
.rtc
->class_dev
.class_id
,
460 &cmos_rtc
.rtc
->class_dev
);
462 dev_dbg(dev
, "IRQ %d is already in use\n", rtc_irq
);
466 /* REVISIT optionally make 50 or 114 bytes NVRAM available,
467 * like rtc-ds1553, rtc-ds1742 ... this will often include
468 * registers for century, and day/month alarm.
471 pr_info("%s: alarms up to one %s%s\n",
472 cmos_rtc
.rtc
->class_dev
.class_id
,
473 is_valid_irq(rtc_irq
)
479 cmos_rtc
.century
? ", y3k" : ""
485 rename_region(ports
, NULL
);
487 rtc_device_unregister(cmos_rtc
.rtc
);
491 static void cmos_do_shutdown(void)
493 unsigned char rtc_control
;
495 spin_lock_irq(&rtc_lock
);
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
);
500 spin_unlock_irq(&rtc_lock
);
503 static void __exit
cmos_do_remove(struct device
*dev
)
505 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
510 release_resource(cmos
->iomem
);
511 rename_region(cmos
->iomem
, NULL
);
513 if (is_valid_irq(cmos
->irq
))
514 free_irq(cmos
->irq
, &cmos_rtc
.rtc
->class_dev
);
516 rtc_device_unregister(cmos_rtc
.rtc
);
519 dev_set_drvdata(dev
, NULL
);
524 static int cmos_suspend(struct device
*dev
, pm_message_t mesg
)
526 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
527 int do_wake
= device_may_wakeup(dev
);
528 unsigned char tmp
, irqstat
;
530 /* only the alarm might be a wakeup event source */
531 spin_lock_irq(&rtc_lock
);
532 cmos
->suspend_ctrl
= tmp
= CMOS_READ(RTC_CONTROL
);
533 if (tmp
& (RTC_PIE
|RTC_AIE
|RTC_UIE
)) {
535 tmp
&= ~(RTC_PIE
|RTC_UIE
);
537 tmp
&= ~(RTC_PIE
|RTC_AIE
|RTC_UIE
);
538 CMOS_WRITE(tmp
, RTC_CONTROL
);
539 irqstat
= CMOS_READ(RTC_INTR_FLAGS
);
542 spin_unlock_irq(&rtc_lock
);
545 rtc_update_irq(&cmos
->rtc
->class_dev
, 1, irqstat
);
547 /* ACPI HOOK: enable ACPI_EVENT_RTC when (tmp & RTC_AIE)
548 * ... it'd be best if we could do that under rtc_lock.
551 pr_debug("%s: suspend%s, ctrl %02x\n",
552 cmos_rtc
.rtc
->class_dev
.class_id
,
553 (tmp
& RTC_AIE
) ? ", alarm may wake" : "",
559 static int cmos_resume(struct device
*dev
)
561 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
562 unsigned char tmp
= cmos
->suspend_ctrl
;
564 /* REVISIT: a mechanism to resync the system clock (jiffies)
565 * on resume should be portable between platforms ...
568 /* re-enable any irqs previously active */
569 if (tmp
& (RTC_PIE
|RTC_AIE
|RTC_UIE
)) {
571 /* ACPI HOOK: disable ACPI_EVENT_RTC when (tmp & RTC_AIE) */
573 spin_lock_irq(&rtc_lock
);
574 CMOS_WRITE(tmp
, RTC_CONTROL
);
575 tmp
= CMOS_READ(RTC_INTR_FLAGS
);
576 spin_unlock_irq(&rtc_lock
);
578 rtc_update_irq(&cmos
->rtc
->class_dev
, 1, tmp
);
581 pr_debug("%s: resume, ctrl %02x\n",
582 cmos_rtc
.rtc
->class_dev
.class_id
,
590 #define cmos_suspend NULL
591 #define cmos_resume NULL
594 /*----------------------------------------------------------------*/
596 /* The "CMOS" RTC normally lives on the platform_bus. On ACPI systems,
597 * the device node may alternatively be created as a PNP device.
600 #ifdef CONFIG_PNPACPI
602 #include <linux/pnp.h>
605 cmos_pnp_probe(struct pnp_dev
*pnp
, const struct pnp_device_id
*id
)
607 /* REVISIT paranoia argues for a shutdown notifier, since PNP
608 * drivers can't provide shutdown() methods to disable IRQs.
609 * Or better yet, fix PNP to allow those methods...
611 return cmos_do_probe(&pnp
->dev
,
612 &pnp
->res
.port_resource
[0],
613 pnp
->res
.irq_resource
[0].start
);
616 static void __exit
cmos_pnp_remove(struct pnp_dev
*pnp
)
618 cmos_do_remove(&pnp
->dev
);
623 static int cmos_pnp_suspend(struct pnp_dev
*pnp
, pm_message_t mesg
)
625 return cmos_suspend(&pnp
->dev
, mesg
);
628 static int cmos_pnp_resume(struct pnp_dev
*pnp
)
630 return cmos_resume(&pnp
->dev
);
634 #define cmos_pnp_suspend NULL
635 #define cmos_pnp_resume NULL
639 static const struct pnp_device_id rtc_ids
[] = {
640 { .id
= "PNP0b00", },
641 { .id
= "PNP0b01", },
642 { .id
= "PNP0b02", },
645 MODULE_DEVICE_TABLE(pnp
, rtc_ids
);
647 static struct pnp_driver cmos_pnp_driver
= {
648 .name
= (char *) driver_name
,
650 .probe
= cmos_pnp_probe
,
651 .remove
= __exit_p(cmos_pnp_remove
),
653 /* flag ensures resume() gets called, and stops syslog spam */
654 .flags
= PNP_DRIVER_RES_DO_NOT_CHANGE
,
655 .suspend
= cmos_pnp_suspend
,
656 .resume
= cmos_pnp_resume
,
659 static int __init
cmos_init(void)
661 return pnp_register_driver(&cmos_pnp_driver
);
663 module_init(cmos_init
);
665 static void __exit
cmos_exit(void)
667 pnp_unregister_driver(&cmos_pnp_driver
);
669 module_exit(cmos_exit
);
671 #else /* no PNPACPI */
673 /*----------------------------------------------------------------*/
675 /* Platform setup should have set up an RTC device, when PNPACPI is
676 * unavailable ... this is the normal case, common even on PCs.
679 static int __init
cmos_platform_probe(struct platform_device
*pdev
)
681 return cmos_do_probe(&pdev
->dev
,
682 platform_get_resource(pdev
, IORESOURCE_IO
, 0),
683 platform_get_irq(pdev
, 0));
686 static int __exit
cmos_platform_remove(struct platform_device
*pdev
)
688 cmos_do_remove(&pdev
->dev
);
692 static void cmos_platform_shutdown(struct platform_device
*pdev
)
697 static struct platform_driver cmos_platform_driver
= {
698 .remove
= __exit_p(cmos_platform_remove
),
699 .shutdown
= cmos_platform_shutdown
,
701 .name
= (char *) driver_name
,
702 .suspend
= cmos_suspend
,
703 .resume
= cmos_resume
,
707 static int __init
cmos_init(void)
709 return platform_driver_probe(&cmos_platform_driver
,
710 cmos_platform_probe
);
712 module_init(cmos_init
);
714 static void __exit
cmos_exit(void)
716 platform_driver_unregister(&cmos_platform_driver
);
718 module_exit(cmos_exit
);
721 #endif /* !PNPACPI */
723 MODULE_AUTHOR("David Brownell");
724 MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
725 MODULE_LICENSE("GPL");