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 /* The RTC_INTR register may have e.g. RTC_PF set even if RTC_PIE is clear;
63 * always mask it against the irq enable bits in RTC_CONTROL. Bit values
64 * are the same: PF==PIE, AF=AIE, UF=UIE; so RTC_IRQMASK works with both.
66 #define RTC_IRQMASK (RTC_PF | RTC_AF | RTC_UF)
68 static inline int is_intr(u8 rtc_intr
)
70 if (!(rtc_intr
& RTC_IRQF
))
72 return rtc_intr
& RTC_IRQMASK
;
75 /*----------------------------------------------------------------*/
77 static int cmos_read_time(struct device
*dev
, struct rtc_time
*t
)
79 /* REVISIT: if the clock has a "century" register, use
80 * that instead of the heuristic in get_rtc_time().
81 * That'll make Y3K compatility (year > 2070) easy!
87 static int cmos_set_time(struct device
*dev
, struct rtc_time
*t
)
89 /* REVISIT: set the "century" register if available
91 * NOTE: this ignores the issue whereby updating the seconds
92 * takes effect exactly 500ms after we write the register.
93 * (Also queueing and other delays before we get this far.)
95 return set_rtc_time(t
);
98 static int cmos_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
100 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
101 unsigned char rtc_control
;
103 if (!is_valid_irq(cmos
->irq
))
106 /* Basic alarms only support hour, minute, and seconds fields.
107 * Some also support day and month, for alarms up to a year in
110 t
->time
.tm_mday
= -1;
113 spin_lock_irq(&rtc_lock
);
114 t
->time
.tm_sec
= CMOS_READ(RTC_SECONDS_ALARM
);
115 t
->time
.tm_min
= CMOS_READ(RTC_MINUTES_ALARM
);
116 t
->time
.tm_hour
= CMOS_READ(RTC_HOURS_ALARM
);
118 if (cmos
->day_alrm
) {
119 t
->time
.tm_mday
= CMOS_READ(cmos
->day_alrm
);
120 if (!t
->time
.tm_mday
)
121 t
->time
.tm_mday
= -1;
123 if (cmos
->mon_alrm
) {
124 t
->time
.tm_mon
= CMOS_READ(cmos
->mon_alrm
);
130 rtc_control
= CMOS_READ(RTC_CONTROL
);
131 spin_unlock_irq(&rtc_lock
);
133 /* REVISIT this assumes PC style usage: always BCD */
135 if (((unsigned)t
->time
.tm_sec
) < 0x60)
136 t
->time
.tm_sec
= BCD2BIN(t
->time
.tm_sec
);
139 if (((unsigned)t
->time
.tm_min
) < 0x60)
140 t
->time
.tm_min
= BCD2BIN(t
->time
.tm_min
);
143 if (((unsigned)t
->time
.tm_hour
) < 0x24)
144 t
->time
.tm_hour
= BCD2BIN(t
->time
.tm_hour
);
146 t
->time
.tm_hour
= -1;
148 if (cmos
->day_alrm
) {
149 if (((unsigned)t
->time
.tm_mday
) <= 0x31)
150 t
->time
.tm_mday
= BCD2BIN(t
->time
.tm_mday
);
152 t
->time
.tm_mday
= -1;
153 if (cmos
->mon_alrm
) {
154 if (((unsigned)t
->time
.tm_mon
) <= 0x12)
155 t
->time
.tm_mon
= BCD2BIN(t
->time
.tm_mon
) - 1;
160 t
->time
.tm_year
= -1;
162 t
->enabled
= !!(rtc_control
& RTC_AIE
);
168 static int cmos_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
170 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
171 unsigned char mon
, mday
, hrs
, min
, sec
;
172 unsigned char rtc_control
, rtc_intr
;
174 if (!is_valid_irq(cmos
->irq
))
177 /* REVISIT this assumes PC style usage: always BCD */
179 /* Writing 0xff means "don't care" or "match all". */
181 mon
= t
->time
.tm_mon
;
182 mon
= (mon
< 12) ? BIN2BCD(mon
) : 0xff;
185 mday
= t
->time
.tm_mday
;
186 mday
= (mday
>= 1 && mday
<= 31) ? BIN2BCD(mday
) : 0xff;
188 hrs
= t
->time
.tm_hour
;
189 hrs
= (hrs
< 24) ? BIN2BCD(hrs
) : 0xff;
191 min
= t
->time
.tm_min
;
192 min
= (min
< 60) ? BIN2BCD(min
) : 0xff;
194 sec
= t
->time
.tm_sec
;
195 sec
= (sec
< 60) ? BIN2BCD(sec
) : 0xff;
197 spin_lock_irq(&rtc_lock
);
199 /* next rtc irq must not be from previous alarm setting */
200 rtc_control
= CMOS_READ(RTC_CONTROL
);
201 rtc_control
&= ~RTC_AIE
;
202 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
203 rtc_intr
= CMOS_READ(RTC_INTR_FLAGS
);
204 rtc_intr
&= (rtc_control
& RTC_IRQMASK
) | RTC_IRQF
;
205 if (is_intr(rtc_intr
))
206 rtc_update_irq(&cmos
->rtc
->class_dev
, 1, rtc_intr
);
209 CMOS_WRITE(hrs
, RTC_HOURS_ALARM
);
210 CMOS_WRITE(min
, RTC_MINUTES_ALARM
);
211 CMOS_WRITE(sec
, RTC_SECONDS_ALARM
);
213 /* the system may support an "enhanced" alarm */
214 if (cmos
->day_alrm
) {
215 CMOS_WRITE(mday
, cmos
->day_alrm
);
217 CMOS_WRITE(mon
, cmos
->mon_alrm
);
221 rtc_control
|= RTC_AIE
;
222 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
223 rtc_intr
= CMOS_READ(RTC_INTR_FLAGS
);
224 rtc_intr
&= (rtc_control
& RTC_IRQMASK
) | RTC_IRQF
;
225 if (is_intr(rtc_intr
))
226 rtc_update_irq(&cmos
->rtc
->class_dev
, 1, rtc_intr
);
229 spin_unlock_irq(&rtc_lock
);
234 static int cmos_set_freq(struct device
*dev
, int freq
)
236 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
240 if (!is_valid_irq(cmos
->irq
))
243 /* 0 = no irqs; 1 = 2^15 Hz ... 15 = 2^0 Hz */
246 if (f
-- > 16 || freq
!= (1 << f
))
251 spin_lock_irqsave(&rtc_lock
, flags
);
252 CMOS_WRITE(RTC_REF_CLCK_32KHZ
| f
, RTC_FREQ_SELECT
);
253 spin_unlock_irqrestore(&rtc_lock
, flags
);
258 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
261 cmos_rtc_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
263 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
264 unsigned char rtc_control
, rtc_intr
;
274 if (!is_valid_irq(cmos
->irq
))
281 spin_lock_irqsave(&rtc_lock
, flags
);
282 rtc_control
= CMOS_READ(RTC_CONTROL
);
284 case RTC_AIE_OFF
: /* alarm off */
285 rtc_control
&= ~RTC_AIE
;
287 case RTC_AIE_ON
: /* alarm on */
288 rtc_control
|= RTC_AIE
;
290 case RTC_UIE_OFF
: /* update off */
291 rtc_control
&= ~RTC_UIE
;
293 case RTC_UIE_ON
: /* update on */
294 rtc_control
|= RTC_UIE
;
296 case RTC_PIE_OFF
: /* periodic off */
297 rtc_control
&= ~RTC_PIE
;
299 case RTC_PIE_ON
: /* periodic on */
300 rtc_control
|= RTC_PIE
;
303 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
304 rtc_intr
= CMOS_READ(RTC_INTR_FLAGS
);
305 rtc_intr
&= (rtc_control
& RTC_IRQMASK
) | RTC_IRQF
;
306 if (is_intr(rtc_intr
))
307 rtc_update_irq(&cmos
->rtc
->class_dev
, 1, rtc_intr
);
308 spin_unlock_irqrestore(&rtc_lock
, flags
);
313 #define cmos_rtc_ioctl NULL
316 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
318 static int cmos_procfs(struct device
*dev
, struct seq_file
*seq
)
320 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
321 unsigned char rtc_control
, valid
;
323 spin_lock_irq(&rtc_lock
);
324 rtc_control
= CMOS_READ(RTC_CONTROL
);
325 valid
= CMOS_READ(RTC_VALID
);
326 spin_unlock_irq(&rtc_lock
);
328 /* NOTE: at least ICH6 reports battery status using a different
329 * (non-RTC) bit; and SQWE is ignored on many current systems.
331 return seq_printf(seq
,
332 "periodic_IRQ\t: %s\n"
334 // "square_wave\t: %s\n"
337 "periodic_freq\t: %d\n"
338 "batt_status\t: %s\n",
339 (rtc_control
& RTC_PIE
) ? "yes" : "no",
340 (rtc_control
& RTC_UIE
) ? "yes" : "no",
341 // (rtc_control & RTC_SQWE) ? "yes" : "no",
342 // (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
343 (rtc_control
& RTC_DST_EN
) ? "yes" : "no",
345 (valid
& RTC_VRT
) ? "okay" : "dead");
349 #define cmos_procfs NULL
352 static const struct rtc_class_ops cmos_rtc_ops
= {
353 .ioctl
= cmos_rtc_ioctl
,
354 .read_time
= cmos_read_time
,
355 .set_time
= cmos_set_time
,
356 .read_alarm
= cmos_read_alarm
,
357 .set_alarm
= cmos_set_alarm
,
359 .irq_set_freq
= cmos_set_freq
,
362 /*----------------------------------------------------------------*/
364 static struct cmos_rtc cmos_rtc
;
366 static irqreturn_t
cmos_interrupt(int irq
, void *p
)
370 spin_lock(&rtc_lock
);
371 irqstat
= CMOS_READ(RTC_INTR_FLAGS
);
372 irqstat
&= (CMOS_READ(RTC_CONTROL
) & RTC_IRQMASK
) | RTC_IRQF
;
373 spin_unlock(&rtc_lock
);
375 if (is_intr(irqstat
)) {
376 rtc_update_irq(p
, 1, irqstat
);
382 #ifdef CONFIG_PNPACPI
383 #define is_pnpacpi() 1
387 #define is_pnpacpi() 0
388 #define INITSECTION __init
391 static int INITSECTION
392 cmos_do_probe(struct device
*dev
, struct resource
*ports
, int rtc_irq
)
394 struct cmos_rtc_board_info
*info
= dev
->platform_data
;
396 unsigned char rtc_control
;
398 /* there can be only one ... */
405 cmos_rtc
.irq
= rtc_irq
;
406 cmos_rtc
.iomem
= ports
;
408 /* For ACPI systems the info comes from the FADT. On others,
409 * board specific setup provides it as appropriate.
412 cmos_rtc
.day_alrm
= info
->rtc_day_alarm
;
413 cmos_rtc
.mon_alrm
= info
->rtc_mon_alarm
;
414 cmos_rtc
.century
= info
->rtc_century
;
417 cmos_rtc
.rtc
= rtc_device_register(driver_name
, dev
,
418 &cmos_rtc_ops
, THIS_MODULE
);
419 if (IS_ERR(cmos_rtc
.rtc
))
420 return PTR_ERR(cmos_rtc
.rtc
);
423 dev_set_drvdata(dev
, &cmos_rtc
);
425 /* platform and pnp busses handle resources incompatibly.
427 * REVISIT for non-x86 systems we may need to handle io memory
428 * resources: ioremap them, and request_mem_region().
431 retval
= request_resource(&ioport_resource
, ports
);
433 dev_dbg(dev
, "i/o registers already in use\n");
437 rename_region(ports
, cmos_rtc
.rtc
->class_dev
.class_id
);
439 spin_lock_irq(&rtc_lock
);
441 /* force periodic irq to CMOS reset default of 1024Hz;
443 * REVISIT it's been reported that at least one x86_64 ALI mobo
444 * doesn't use 32KHz here ... for portability we might need to
445 * do something about other clock frequencies.
447 CMOS_WRITE(RTC_REF_CLCK_32KHZ
| 0x06, RTC_FREQ_SELECT
);
448 cmos_rtc
.rtc
->irq_freq
= 1024;
452 * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
453 * allegedly some older rtcs need that to handle irqs properly
455 rtc_control
= CMOS_READ(RTC_CONTROL
);
456 rtc_control
&= ~(RTC_PIE
| RTC_AIE
| RTC_UIE
);
457 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
458 CMOS_READ(RTC_INTR_FLAGS
);
460 spin_unlock_irq(&rtc_lock
);
462 /* FIXME teach the alarm code how to handle binary mode;
463 * <asm-generic/rtc.h> doesn't know 12-hour mode either.
465 if (!(rtc_control
& RTC_24H
) || (rtc_control
& (RTC_DM_BINARY
))) {
466 dev_dbg(dev
, "only 24-hr BCD mode supported\n");
471 if (is_valid_irq(rtc_irq
))
472 retval
= request_irq(rtc_irq
, cmos_interrupt
, IRQF_DISABLED
,
473 cmos_rtc
.rtc
->class_dev
.class_id
,
474 &cmos_rtc
.rtc
->class_dev
);
476 dev_dbg(dev
, "IRQ %d is already in use\n", rtc_irq
);
480 /* REVISIT optionally make 50 or 114 bytes NVRAM available,
481 * like rtc-ds1553, rtc-ds1742 ... this will often include
482 * registers for century, and day/month alarm.
485 pr_info("%s: alarms up to one %s%s\n",
486 cmos_rtc
.rtc
->class_dev
.class_id
,
487 is_valid_irq(rtc_irq
)
493 cmos_rtc
.century
? ", y3k" : ""
499 rename_region(ports
, NULL
);
501 rtc_device_unregister(cmos_rtc
.rtc
);
505 static void cmos_do_shutdown(void)
507 unsigned char rtc_control
;
509 spin_lock_irq(&rtc_lock
);
510 rtc_control
= CMOS_READ(RTC_CONTROL
);
511 rtc_control
&= ~(RTC_PIE
|RTC_AIE
|RTC_UIE
);
512 CMOS_WRITE(rtc_control
, RTC_CONTROL
);
513 CMOS_READ(RTC_INTR_FLAGS
);
514 spin_unlock_irq(&rtc_lock
);
517 static void __exit
cmos_do_remove(struct device
*dev
)
519 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
524 release_resource(cmos
->iomem
);
525 rename_region(cmos
->iomem
, NULL
);
527 if (is_valid_irq(cmos
->irq
))
528 free_irq(cmos
->irq
, &cmos_rtc
.rtc
->class_dev
);
530 rtc_device_unregister(cmos_rtc
.rtc
);
533 dev_set_drvdata(dev
, NULL
);
538 static int cmos_suspend(struct device
*dev
, pm_message_t mesg
)
540 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
541 int do_wake
= device_may_wakeup(dev
);
544 /* only the alarm might be a wakeup event source */
545 spin_lock_irq(&rtc_lock
);
546 cmos
->suspend_ctrl
= tmp
= CMOS_READ(RTC_CONTROL
);
547 if (tmp
& (RTC_PIE
|RTC_AIE
|RTC_UIE
)) {
548 unsigned char irqstat
;
551 tmp
&= ~(RTC_PIE
|RTC_UIE
);
553 tmp
&= ~(RTC_PIE
|RTC_AIE
|RTC_UIE
);
554 CMOS_WRITE(tmp
, RTC_CONTROL
);
555 irqstat
= CMOS_READ(RTC_INTR_FLAGS
);
556 irqstat
&= (tmp
& RTC_IRQMASK
) | RTC_IRQF
;
557 if (is_intr(irqstat
))
558 rtc_update_irq(&cmos
->rtc
->class_dev
, 1, irqstat
);
560 spin_unlock_irq(&rtc_lock
);
562 /* ACPI HOOK: enable ACPI_EVENT_RTC when (tmp & RTC_AIE)
563 * ... it'd be best if we could do that under rtc_lock.
566 pr_debug("%s: suspend%s, ctrl %02x\n",
567 cmos_rtc
.rtc
->class_dev
.class_id
,
568 (tmp
& RTC_AIE
) ? ", alarm may wake" : "",
574 static int cmos_resume(struct device
*dev
)
576 struct cmos_rtc
*cmos
= dev_get_drvdata(dev
);
577 unsigned char tmp
= cmos
->suspend_ctrl
;
579 /* REVISIT: a mechanism to resync the system clock (jiffies)
580 * on resume should be portable between platforms ...
583 /* re-enable any irqs previously active */
584 if (tmp
& (RTC_PIE
|RTC_AIE
|RTC_UIE
)) {
586 /* ACPI HOOK: disable ACPI_EVENT_RTC when (tmp & RTC_AIE) */
588 spin_lock_irq(&rtc_lock
);
589 CMOS_WRITE(tmp
, RTC_CONTROL
);
590 tmp
= CMOS_READ(RTC_INTR_FLAGS
);
591 tmp
&= (cmos
->suspend_ctrl
& RTC_IRQMASK
) | RTC_IRQF
;
593 rtc_update_irq(&cmos
->rtc
->class_dev
, 1, tmp
);
594 spin_unlock_irq(&rtc_lock
);
597 pr_debug("%s: resume, ctrl %02x\n",
598 cmos_rtc
.rtc
->class_dev
.class_id
,
606 #define cmos_suspend NULL
607 #define cmos_resume NULL
610 /*----------------------------------------------------------------*/
612 /* The "CMOS" RTC normally lives on the platform_bus. On ACPI systems,
613 * the device node will always be created as a PNPACPI device.
616 #ifdef CONFIG_PNPACPI
618 #include <linux/pnp.h>
621 cmos_pnp_probe(struct pnp_dev
*pnp
, const struct pnp_device_id
*id
)
623 /* REVISIT paranoia argues for a shutdown notifier, since PNP
624 * drivers can't provide shutdown() methods to disable IRQs.
625 * Or better yet, fix PNP to allow those methods...
627 return cmos_do_probe(&pnp
->dev
,
628 &pnp
->res
.port_resource
[0],
629 pnp
->res
.irq_resource
[0].start
);
632 static void __exit
cmos_pnp_remove(struct pnp_dev
*pnp
)
634 cmos_do_remove(&pnp
->dev
);
639 static int cmos_pnp_suspend(struct pnp_dev
*pnp
, pm_message_t mesg
)
641 return cmos_suspend(&pnp
->dev
, mesg
);
644 static int cmos_pnp_resume(struct pnp_dev
*pnp
)
646 return cmos_resume(&pnp
->dev
);
650 #define cmos_pnp_suspend NULL
651 #define cmos_pnp_resume NULL
655 static const struct pnp_device_id rtc_ids
[] = {
656 { .id
= "PNP0b00", },
657 { .id
= "PNP0b01", },
658 { .id
= "PNP0b02", },
661 MODULE_DEVICE_TABLE(pnp
, rtc_ids
);
663 static struct pnp_driver cmos_pnp_driver
= {
664 .name
= (char *) driver_name
,
666 .probe
= cmos_pnp_probe
,
667 .remove
= __exit_p(cmos_pnp_remove
),
669 /* flag ensures resume() gets called, and stops syslog spam */
670 .flags
= PNP_DRIVER_RES_DO_NOT_CHANGE
,
671 .suspend
= cmos_pnp_suspend
,
672 .resume
= cmos_pnp_resume
,
675 static int __init
cmos_init(void)
677 return pnp_register_driver(&cmos_pnp_driver
);
679 module_init(cmos_init
);
681 static void __exit
cmos_exit(void)
683 pnp_unregister_driver(&cmos_pnp_driver
);
685 module_exit(cmos_exit
);
687 #else /* no PNPACPI */
689 /*----------------------------------------------------------------*/
691 /* Platform setup should have set up an RTC device, when PNPACPI is
692 * unavailable ... this could happen even on (older) PCs.
695 static int __init
cmos_platform_probe(struct platform_device
*pdev
)
697 return cmos_do_probe(&pdev
->dev
,
698 platform_get_resource(pdev
, IORESOURCE_IO
, 0),
699 platform_get_irq(pdev
, 0));
702 static int __exit
cmos_platform_remove(struct platform_device
*pdev
)
704 cmos_do_remove(&pdev
->dev
);
708 static void cmos_platform_shutdown(struct platform_device
*pdev
)
713 static struct platform_driver cmos_platform_driver
= {
714 .remove
= __exit_p(cmos_platform_remove
),
715 .shutdown
= cmos_platform_shutdown
,
717 .name
= (char *) driver_name
,
718 .suspend
= cmos_suspend
,
719 .resume
= cmos_resume
,
723 static int __init
cmos_init(void)
725 return platform_driver_probe(&cmos_platform_driver
,
726 cmos_platform_probe
);
728 module_init(cmos_init
);
730 static void __exit
cmos_exit(void)
732 platform_driver_unregister(&cmos_platform_driver
);
734 module_exit(cmos_exit
);
737 #endif /* !PNPACPI */
739 MODULE_AUTHOR("David Brownell");
740 MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
741 MODULE_LICENSE("GPL");