31 #include <linux/kernel.h>
32 #include <linux/module.h>
65 #define is_valid_irq(n) ((n) > 0)
73 #define RTC_IRQMASK (RTC_PF | RTC_AF | RTC_UF)
75 static inline int is_intr(
u8 rtc_intr)
94 #ifdef CONFIG_HPET_EMULATE_RTC
156 #define can_bank2 true
158 static inline unsigned char cmos_read_bank2(
unsigned char addr)
164 static inline void cmos_write_bank2(
unsigned char val,
unsigned char addr)
172 #define can_bank2 false
174 static inline unsigned char cmos_read_bank2(
unsigned char addr)
179 static inline void cmos_write_bank2(
unsigned char val,
unsigned char addr)
220 t->
time.tm_mday = -1;
223 spin_lock_irq(&rtc_lock);
231 if (!t->
time.tm_mday)
232 t->
time.tm_mday = -1;
242 spin_unlock_irq(&rtc_lock);
245 if (((
unsigned)t->
time.tm_sec) < 0x60)
249 if (((
unsigned)t->
time.tm_min) < 0x60)
253 if (((
unsigned)t->
time.tm_hour) < 0x24)
256 t->
time.tm_hour = -1;
259 if (((
unsigned)t->
time.tm_mday) <= 0x31)
262 t->
time.tm_mday = -1;
265 if (((
unsigned)t->
time.tm_mon) <= 0x12)
272 t->
time.tm_year = -1;
280 static void cmos_checkintr(
struct cmos_rtc *cmos,
unsigned char rtc_control)
282 unsigned char rtc_intr;
293 if (is_intr(rtc_intr))
297 static void cmos_irq_enable(
struct cmos_rtc *cmos,
unsigned char mask)
305 cmos_checkintr(cmos, rtc_control);
311 cmos_checkintr(cmos, rtc_control);
314 static void cmos_irq_disable(
struct cmos_rtc *cmos,
unsigned char mask)
319 rtc_control &= ~mask;
323 cmos_checkintr(cmos, rtc_control);
334 mon = t->
time.tm_mon + 1;
335 mday = t->
time.tm_mday;
336 hrs = t->
time.tm_hour;
337 min = t->
time.tm_min;
338 sec = t->
time.tm_sec;
343 mon = (mon <= 12) ?
bin2bcd(mon) : 0xff;
344 mday = (mday >= 1 && mday <= 31) ?
bin2bcd(mday) : 0xff;
345 hrs = (hrs < 24) ?
bin2bcd(hrs) : 0xff;
346 min = (min < 60) ?
bin2bcd(min) : 0xff;
347 sec = (sec < 60) ?
bin2bcd(sec) : 0xff;
350 spin_lock_irq(&rtc_lock);
353 cmos_irq_disable(cmos,
RTC_AIE);
373 cmos_irq_enable(cmos,
RTC_AIE);
375 spin_unlock_irq(&rtc_lock);
380 static int cmos_alarm_irq_enable(
struct device *dev,
unsigned int enabled)
391 cmos_irq_enable(cmos,
RTC_AIE);
393 cmos_irq_disable(cmos,
RTC_AIE);
395 spin_unlock_irqrestore(&rtc_lock, flags);
399 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
406 spin_lock_irq(&rtc_lock);
409 spin_unlock_irq(&rtc_lock);
415 "periodic_IRQ\t: %s\n"
417 "HPET_emulated\t: %s\n"
421 "periodic_freq\t: %d\n"
422 "batt_status\t: %s\n",
423 (rtc_control &
RTC_PIE) ?
"yes" :
"no",
424 (rtc_control &
RTC_UIE) ?
"yes" :
"no",
427 (rtc_control & RTC_DM_BINARY) ?
"no" :
"yes",
430 (valid &
RTC_VRT) ?
"okay" :
"dead");
434 #define cmos_procfs NULL
438 .read_time = cmos_read_time,
439 .set_time = cmos_set_time,
440 .read_alarm = cmos_read_alarm,
441 .set_alarm = cmos_set_alarm,
443 .alarm_irq_enable = cmos_alarm_irq_enable,
454 #define NVRAM_OFFSET (RTC_REG_D + 1)
457 cmos_nvram_read(
struct file *filp,
struct kobject *kobj,
459 char *
buf, loff_t off,
size_t count)
467 if ((off + count) > attr->
size)
468 count = attr->
size - off;
472 for (retval = 0;
count; count--, off++, retval++) {
476 *buf++ = cmos_read_bank2(off);
486 cmos_nvram_write(
struct file *filp,
struct kobject *kobj,
488 char *
buf, loff_t off,
size_t count)
498 if ((off + count) > attr->
size)
499 count = attr->
size - off;
507 spin_lock_irq(&rtc_lock);
508 for (retval = 0;
count; count--, off++, retval++) {
517 cmos_write_bank2(*buf++, off);
521 spin_unlock_irq(&rtc_lock);
532 .read = cmos_nvram_read,
533 .write = cmos_nvram_write,
546 spin_lock(&rtc_lock);
558 irqstat = (
unsigned long)irq & 0xF0;
566 rtc_control &= ~RTC_AIE;
572 spin_unlock(&rtc_lock);
574 if (is_intr(irqstat)) {
585 #define INITSECTION __init
589 cmos_do_probe(
struct device *dev,
struct resource *ports,
int rtc_irq)
609 resource_size(ports),
612 dev_dbg(dev,
"i/o registers already in use\n");
624 #if defined(CONFIG_ATARI)
626 #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) \
627 || defined(__sparc__) || defined(__mips__) \
628 || defined(__powerpc__)
631 #warning Assuming 128 bytes of RTC+NVRAM address space, not 64 bytes.
647 if (info->rtc_day_alarm && info->rtc_day_alarm < 128)
649 if (info->rtc_mon_alarm && info->rtc_mon_alarm < 128)
651 if (info->rtc_century && info->rtc_century < 128)
654 if (info->wake_on && info->wake_off) {
685 cmos_irq_disable(&
cmos_rtc, RTC_PIE | RTC_AIE | RTC_UIE);
695 dev_warn(dev,
"only 24-hr supported\n");
710 " failed in rtc_init().");
714 rtc_cmos_int_handler = cmos_interrupt;
716 retval =
request_irq(rtc_irq, rtc_cmos_int_handler,
720 dev_dbg(dev,
"IRQ %d is already in use\n", rtc_irq);
730 dev_dbg(dev,
"can't create nvram file? %d\n", retval);
734 pr_info(
"%s: %s%s, %zd bytes nvram%s\n",
739 "alarms up to one day",
757 static void cmos_do_shutdown(
void)
759 spin_lock_irq(&rtc_lock);
761 spin_unlock_irq(&rtc_lock);
791 static int cmos_suspend(
struct device *dev)
797 spin_lock_irq(&rtc_lock);
799 if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
802 if (device_may_wakeup(dev))
811 cmos_checkintr(cmos, tmp);
813 spin_unlock_irq(&rtc_lock);
820 enable_irq_wake(cmos->
irq);
823 pr_debug(
"%s: suspend%s, ctrl %02x\n",
825 (tmp & RTC_AIE) ?
", alarm may wake" :
"",
837 static inline int cmos_poweroff(
struct device *dev)
839 return cmos_suspend(dev);
842 static int cmos_resume(
struct device *dev)
855 disable_irq_wake(cmos->
irq);
859 spin_lock_irq(&rtc_lock);
875 }
while (mask & RTC_AIE);
876 spin_unlock_irq(&rtc_lock);
890 static inline int cmos_poweroff(
struct device *dev)
921 static inline void rtc_wake_setup(
struct device *dev)
932 static void rtc_wake_on(
struct device *dev)
938 static void rtc_wake_off(
struct device *dev)
948 static struct cmos_rtc_board_info acpi_rtc_info;
951 cmos_wake_setup(
struct device *dev)
957 acpi_rtc_info.wake_on = rtc_wake_on;
958 acpi_rtc_info.wake_off = rtc_wake_off;
962 dev_dbg(dev,
"bogus FADT month_alarm (%d)\n",
973 dev_info(dev,
"RTC can wake from S4\n");
984 cmos_wake_setup(
struct device *dev)
997 cmos_wake_setup(&pnp->
dev);
999 if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0))
1004 return cmos_do_probe(&pnp->
dev,
1007 return cmos_do_probe(&pnp->
dev,
1014 cmos_do_remove(&pnp->
dev);
1021 return cmos_suspend(&pnp->
dev);
1024 static int cmos_pnp_resume(
struct pnp_dev *pnp)
1026 return cmos_resume(&pnp->
dev);
1030 #define cmos_pnp_suspend NULL
1031 #define cmos_pnp_resume NULL
1034 static void cmos_pnp_shutdown(
struct pnp_dev *pnp)
1043 { .
id =
"PNP0b00", },
1044 { .id =
"PNP0b01", },
1045 { .id =
"PNP0b02", },
1053 .
probe = cmos_pnp_probe,
1054 .
remove =
__exit_p(cmos_pnp_remove),
1055 .shutdown = cmos_pnp_shutdown,
1059 .suspend = cmos_pnp_suspend,
1060 .resume = cmos_pnp_resume,
1068 .compatible =
"motorola,mc146818",
1104 #define of_cmos_match NULL
1115 cmos_wake_setup(&pdev->
dev);
1116 return cmos_do_probe(&pdev->
dev,
1123 cmos_do_remove(&pdev->
dev);
1139 .remove =
__exit_p(cmos_platform_remove),
1140 .shutdown = cmos_platform_shutdown,
1146 .of_match_table = of_cmos_match,
1151 static bool pnp_driver_registered;
1153 static bool platform_driver_registered;
1155 static int __init cmos_init(
void)
1162 pnp_driver_registered =
true;
1167 cmos_platform_probe);
1169 platform_driver_registered =
true;
1176 if (pnp_driver_registered)
1183 static void __exit cmos_exit(
void)
1186 if (pnp_driver_registered)
1189 if (platform_driver_registered)