1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * RTC class driver for "CMOS RTC": PCs, ACPI, etc 4 * 5 * Copyright (C) 1996 Paul Gortmaker (drivers/char/rtc.c) 6 * Copyright (C) 2006 David Brownell (convert to new framework) 7 */ 8 9 /* 10 * The original "cmos clock" chip was an MC146818 chip, now obsolete. 11 * That defined the register interface now provided by all PCs, some 12 * non-PC systems, and incorporated into ACPI. Modern PC chipsets 13 * integrate an MC146818 clone in their southbridge, and boards use 14 * that instead of discrete clones like the DS12887 or M48T86. There 15 * are also clones that connect using the LPC bus. 16 * 17 * That register API is also used directly by various other drivers 18 * (notably for integrated NVRAM), infrastructure (x86 has code to 19 * bypass the RTC framework, directly reading the RTC during boot 20 * and updating minutes/seconds for systems using NTP synch) and 21 * utilities (like userspace 'hwclock', if no /dev node exists). 22 * 23 * So **ALL** calls to CMOS_READ and CMOS_WRITE must be done with 24 * interrupts disabled, holding the global rtc_lock, to exclude those 25 * other drivers and utilities on correctly configured systems. 26 */ 27 28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 29 30 #include <linux/acpi.h> 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/log2.h> 38 #include <linux/pm.h> 39 #include <linux/of.h> 40 #include <linux/of_platform.h> 41 #ifdef CONFIG_X86 42 #include <asm/i8259.h> 43 #include <asm/processor.h> 44 #include <linux/dmi.h> 45 #endif 46 47 /* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */ 48 #include <linux/mc146818rtc.h> 49 50 #ifdef CONFIG_ACPI 51 /* 52 * Use ACPI SCI to replace HPET interrupt for RTC Alarm event 53 * 54 * If cleared, ACPI SCI is only used to wake up the system from suspend 55 * 56 * If set, ACPI SCI is used to handle UIE/AIE and system wakeup 57 */ 58 59 static bool use_acpi_alarm; 60 module_param(use_acpi_alarm, bool, 0444); 61 62 static inline int cmos_use_acpi_alarm(void) 63 { 64 return use_acpi_alarm; 65 } 66 #else /* !CONFIG_ACPI */ 67 68 static inline int cmos_use_acpi_alarm(void) 69 { 70 return 0; 71 } 72 #endif 73 74 struct cmos_rtc { 75 struct rtc_device *rtc; 76 struct device *dev; 77 int irq; 78 struct resource *iomem; 79 time64_t alarm_expires; 80 81 void (*wake_on)(struct device *); 82 void (*wake_off)(struct device *); 83 84 u8 enabled_wake; 85 u8 suspend_ctrl; 86 87 /* newer hardware extends the original register set */ 88 u8 day_alrm; 89 u8 mon_alrm; 90 u8 century; 91 92 struct rtc_wkalrm saved_wkalrm; 93 }; 94 95 /* both platform and pnp busses use negative numbers for invalid irqs */ 96 #define is_valid_irq(n) ((n) > 0) 97 98 static const char driver_name[] = "rtc_cmos"; 99 100 /* The RTC_INTR register may have e.g. RTC_PF set even if RTC_PIE is clear; 101 * always mask it against the irq enable bits in RTC_CONTROL. Bit values 102 * are the same: PF==PIE, AF=AIE, UF=UIE; so RTC_IRQMASK works with both. 103 */ 104 #define RTC_IRQMASK (RTC_PF | RTC_AF | RTC_UF) 105 106 static inline int is_intr(u8 rtc_intr) 107 { 108 if (!(rtc_intr & RTC_IRQF)) 109 return 0; 110 return rtc_intr & RTC_IRQMASK; 111 } 112 113 /*----------------------------------------------------------------*/ 114 115 /* Much modern x86 hardware has HPETs (10+ MHz timers) which, because 116 * many BIOS programmers don't set up "sane mode" IRQ routing, are mostly 117 * used in a broken "legacy replacement" mode. The breakage includes 118 * HPET #1 hijacking the IRQ for this RTC, and being unavailable for 119 * other (better) use. 120 * 121 * When that broken mode is in use, platform glue provides a partial 122 * emulation of hardware RTC IRQ facilities using HPET #1. We don't 123 * want to use HPET for anything except those IRQs though... 124 */ 125 #ifdef CONFIG_HPET_EMULATE_RTC 126 #include <asm/hpet.h> 127 #else 128 129 static inline int is_hpet_enabled(void) 130 { 131 return 0; 132 } 133 134 static inline int hpet_mask_rtc_irq_bit(unsigned long mask) 135 { 136 return 0; 137 } 138 139 static inline int hpet_set_rtc_irq_bit(unsigned long mask) 140 { 141 return 0; 142 } 143 144 static inline int 145 hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec) 146 { 147 return 0; 148 } 149 150 static inline int hpet_set_periodic_freq(unsigned long freq) 151 { 152 return 0; 153 } 154 155 static inline int hpet_rtc_timer_init(void) 156 { 157 return 0; 158 } 159 160 extern irq_handler_t hpet_rtc_interrupt; 161 162 static inline int hpet_register_irq_handler(irq_handler_t handler) 163 { 164 return 0; 165 } 166 167 static inline int hpet_unregister_irq_handler(irq_handler_t handler) 168 { 169 return 0; 170 } 171 172 #endif 173 174 /* Don't use HPET for RTC Alarm event if ACPI Fixed event is used */ 175 static inline int use_hpet_alarm(void) 176 { 177 return is_hpet_enabled() && !cmos_use_acpi_alarm(); 178 } 179 180 /*----------------------------------------------------------------*/ 181 182 #ifdef RTC_PORT 183 184 /* Most newer x86 systems have two register banks, the first used 185 * for RTC and NVRAM and the second only for NVRAM. Caller must 186 * own rtc_lock ... and we won't worry about access during NMI. 187 */ 188 #define can_bank2 true 189 190 static inline unsigned char cmos_read_bank2(unsigned char addr) 191 { 192 outb(addr, RTC_PORT(2)); 193 return inb(RTC_PORT(3)); 194 } 195 196 static inline void cmos_write_bank2(unsigned char val, unsigned char addr) 197 { 198 outb(addr, RTC_PORT(2)); 199 outb(val, RTC_PORT(3)); 200 } 201 202 #else 203 204 #define can_bank2 false 205 206 static inline unsigned char cmos_read_bank2(unsigned char addr) 207 { 208 return 0; 209 } 210 211 static inline void cmos_write_bank2(unsigned char val, unsigned char addr) 212 { 213 } 214 215 #endif 216 217 /*----------------------------------------------------------------*/ 218 219 static bool cmos_no_alarm(struct cmos_rtc *cmos) 220 { 221 return !is_valid_irq(cmos->irq) && !cmos_use_acpi_alarm(); 222 } 223 224 static int cmos_read_time(struct device *dev, struct rtc_time *t) 225 { 226 int ret; 227 228 /* 229 * If pm_trace abused the RTC for storage, set the timespec to 0, 230 * which tells the caller that this RTC value is unusable. 231 */ 232 if (!pm_trace_rtc_valid()) 233 return -EIO; 234 235 ret = mc146818_get_time(t, 1000); 236 if (ret < 0) { 237 dev_err_ratelimited(dev, "unable to read current time\n"); 238 return ret; 239 } 240 241 return 0; 242 } 243 244 static int cmos_set_time(struct device *dev, struct rtc_time *t) 245 { 246 /* NOTE: this ignores the issue whereby updating the seconds 247 * takes effect exactly 500ms after we write the register. 248 * (Also queueing and other delays before we get this far.) 249 */ 250 return mc146818_set_time(t); 251 } 252 253 struct cmos_read_alarm_callback_param { 254 struct cmos_rtc *cmos; 255 struct rtc_time *time; 256 unsigned char rtc_control; 257 }; 258 259 static void cmos_read_alarm_callback(unsigned char __always_unused seconds, 260 void *param_in) 261 { 262 struct cmos_read_alarm_callback_param *p = 263 (struct cmos_read_alarm_callback_param *)param_in; 264 struct rtc_time *time = p->time; 265 266 time->tm_sec = CMOS_READ(RTC_SECONDS_ALARM); 267 time->tm_min = CMOS_READ(RTC_MINUTES_ALARM); 268 time->tm_hour = CMOS_READ(RTC_HOURS_ALARM); 269 270 if (p->cmos->day_alrm) { 271 /* ignore upper bits on readback per ACPI spec */ 272 time->tm_mday = CMOS_READ(p->cmos->day_alrm) & 0x3f; 273 if (!time->tm_mday) 274 time->tm_mday = -1; 275 276 if (p->cmos->mon_alrm) { 277 time->tm_mon = CMOS_READ(p->cmos->mon_alrm); 278 if (!time->tm_mon) 279 time->tm_mon = -1; 280 } 281 } 282 283 p->rtc_control = CMOS_READ(RTC_CONTROL); 284 } 285 286 static int cmos_read_alarm(struct device *dev, struct rtc_wkalrm *t) 287 { 288 struct cmos_rtc *cmos = dev_get_drvdata(dev); 289 struct cmos_read_alarm_callback_param p = { 290 .cmos = cmos, 291 .time = &t->time, 292 }; 293 294 /* This not only a rtc_op, but also called directly */ 295 if (cmos_no_alarm(cmos)) 296 return -ETIMEDOUT; 297 298 /* Basic alarms only support hour, minute, and seconds fields. 299 * Some also support day and month, for alarms up to a year in 300 * the future. 301 */ 302 303 /* Some Intel chipsets disconnect the alarm registers when the clock 304 * update is in progress - during this time reads return bogus values 305 * and writes may fail silently. See for example "7th Generation Intel® 306 * Processor Family I/O for U/Y Platforms [...] Datasheet", section 307 * 27.7.1 308 * 309 * Use the mc146818_avoid_UIP() function to avoid this. 310 */ 311 if (!mc146818_avoid_UIP(cmos_read_alarm_callback, 10, &p)) 312 return -EIO; 313 314 if (!(p.rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { 315 if (((unsigned)t->time.tm_sec) < 0x60) 316 t->time.tm_sec = bcd2bin(t->time.tm_sec); 317 else 318 t->time.tm_sec = -1; 319 if (((unsigned)t->time.tm_min) < 0x60) 320 t->time.tm_min = bcd2bin(t->time.tm_min); 321 else 322 t->time.tm_min = -1; 323 if (((unsigned)t->time.tm_hour) < 0x24) 324 t->time.tm_hour = bcd2bin(t->time.tm_hour); 325 else 326 t->time.tm_hour = -1; 327 328 if (cmos->day_alrm) { 329 if (((unsigned)t->time.tm_mday) <= 0x31) 330 t->time.tm_mday = bcd2bin(t->time.tm_mday); 331 else 332 t->time.tm_mday = -1; 333 334 if (cmos->mon_alrm) { 335 if (((unsigned)t->time.tm_mon) <= 0x12) 336 t->time.tm_mon = bcd2bin(t->time.tm_mon)-1; 337 else 338 t->time.tm_mon = -1; 339 } 340 } 341 } 342 343 t->enabled = !!(p.rtc_control & RTC_AIE); 344 t->pending = 0; 345 346 return 0; 347 } 348 349 static void cmos_checkintr(struct cmos_rtc *cmos, unsigned char rtc_control) 350 { 351 unsigned char rtc_intr; 352 353 /* NOTE after changing RTC_xIE bits we always read INTR_FLAGS; 354 * allegedly some older rtcs need that to handle irqs properly 355 */ 356 rtc_intr = CMOS_READ(RTC_INTR_FLAGS); 357 358 if (use_hpet_alarm()) 359 return; 360 361 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF; 362 if (is_intr(rtc_intr)) 363 rtc_update_irq(cmos->rtc, 1, rtc_intr); 364 } 365 366 static void cmos_irq_enable(struct cmos_rtc *cmos, unsigned char mask) 367 { 368 unsigned char rtc_control; 369 370 /* flush any pending IRQ status, notably for update irqs, 371 * before we enable new IRQs 372 */ 373 rtc_control = CMOS_READ(RTC_CONTROL); 374 cmos_checkintr(cmos, rtc_control); 375 376 rtc_control |= mask; 377 CMOS_WRITE(rtc_control, RTC_CONTROL); 378 if (use_hpet_alarm()) 379 hpet_set_rtc_irq_bit(mask); 380 381 if ((mask & RTC_AIE) && cmos_use_acpi_alarm()) { 382 if (cmos->wake_on) 383 cmos->wake_on(cmos->dev); 384 } 385 386 cmos_checkintr(cmos, rtc_control); 387 } 388 389 static void cmos_irq_disable(struct cmos_rtc *cmos, unsigned char mask) 390 { 391 unsigned char rtc_control; 392 393 rtc_control = CMOS_READ(RTC_CONTROL); 394 rtc_control &= ~mask; 395 CMOS_WRITE(rtc_control, RTC_CONTROL); 396 if (use_hpet_alarm()) 397 hpet_mask_rtc_irq_bit(mask); 398 399 if ((mask & RTC_AIE) && cmos_use_acpi_alarm()) { 400 if (cmos->wake_off) 401 cmos->wake_off(cmos->dev); 402 } 403 404 cmos_checkintr(cmos, rtc_control); 405 } 406 407 static int cmos_validate_alarm(struct device *dev, struct rtc_wkalrm *t) 408 { 409 struct cmos_rtc *cmos = dev_get_drvdata(dev); 410 struct rtc_time now; 411 412 cmos_read_time(dev, &now); 413 414 if (!cmos->day_alrm) { 415 time64_t t_max_date; 416 time64_t t_alrm; 417 418 t_max_date = rtc_tm_to_time64(&now); 419 t_max_date += 24 * 60 * 60 - 1; 420 t_alrm = rtc_tm_to_time64(&t->time); 421 if (t_alrm > t_max_date) { 422 dev_err(dev, 423 "Alarms can be up to one day in the future\n"); 424 return -EINVAL; 425 } 426 } else if (!cmos->mon_alrm) { 427 struct rtc_time max_date = now; 428 time64_t t_max_date; 429 time64_t t_alrm; 430 int max_mday; 431 432 if (max_date.tm_mon == 11) { 433 max_date.tm_mon = 0; 434 max_date.tm_year += 1; 435 } else { 436 max_date.tm_mon += 1; 437 } 438 max_mday = rtc_month_days(max_date.tm_mon, max_date.tm_year); 439 if (max_date.tm_mday > max_mday) 440 max_date.tm_mday = max_mday; 441 442 t_max_date = rtc_tm_to_time64(&max_date); 443 t_max_date -= 1; 444 t_alrm = rtc_tm_to_time64(&t->time); 445 if (t_alrm > t_max_date) { 446 dev_err(dev, 447 "Alarms can be up to one month in the future\n"); 448 return -EINVAL; 449 } 450 } else { 451 struct rtc_time max_date = now; 452 time64_t t_max_date; 453 time64_t t_alrm; 454 int max_mday; 455 456 max_date.tm_year += 1; 457 max_mday = rtc_month_days(max_date.tm_mon, max_date.tm_year); 458 if (max_date.tm_mday > max_mday) 459 max_date.tm_mday = max_mday; 460 461 t_max_date = rtc_tm_to_time64(&max_date); 462 t_max_date -= 1; 463 t_alrm = rtc_tm_to_time64(&t->time); 464 if (t_alrm > t_max_date) { 465 dev_err(dev, 466 "Alarms can be up to one year in the future\n"); 467 return -EINVAL; 468 } 469 } 470 471 return 0; 472 } 473 474 struct cmos_set_alarm_callback_param { 475 struct cmos_rtc *cmos; 476 unsigned char mon, mday, hrs, min, sec; 477 struct rtc_wkalrm *t; 478 }; 479 480 /* Note: this function may be executed by mc146818_avoid_UIP() more then 481 * once 482 */ 483 static void cmos_set_alarm_callback(unsigned char __always_unused seconds, 484 void *param_in) 485 { 486 struct cmos_set_alarm_callback_param *p = 487 (struct cmos_set_alarm_callback_param *)param_in; 488 489 /* next rtc irq must not be from previous alarm setting */ 490 cmos_irq_disable(p->cmos, RTC_AIE); 491 492 /* update alarm */ 493 CMOS_WRITE(p->hrs, RTC_HOURS_ALARM); 494 CMOS_WRITE(p->min, RTC_MINUTES_ALARM); 495 CMOS_WRITE(p->sec, RTC_SECONDS_ALARM); 496 497 /* the system may support an "enhanced" alarm */ 498 if (p->cmos->day_alrm) { 499 CMOS_WRITE(p->mday, p->cmos->day_alrm); 500 if (p->cmos->mon_alrm) 501 CMOS_WRITE(p->mon, p->cmos->mon_alrm); 502 } 503 504 if (use_hpet_alarm()) { 505 /* 506 * FIXME the HPET alarm glue currently ignores day_alrm 507 * and mon_alrm ... 508 */ 509 hpet_set_alarm_time(p->t->time.tm_hour, p->t->time.tm_min, 510 p->t->time.tm_sec); 511 } 512 513 if (p->t->enabled) 514 cmos_irq_enable(p->cmos, RTC_AIE); 515 } 516 517 static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t) 518 { 519 struct cmos_rtc *cmos = dev_get_drvdata(dev); 520 struct cmos_set_alarm_callback_param p = { 521 .cmos = cmos, 522 .t = t 523 }; 524 unsigned char rtc_control; 525 int ret; 526 527 /* This not only a rtc_op, but also called directly */ 528 if (cmos_no_alarm(cmos)) 529 return -EIO; 530 531 ret = cmos_validate_alarm(dev, t); 532 if (ret < 0) 533 return ret; 534 535 p.mon = t->time.tm_mon + 1; 536 p.mday = t->time.tm_mday; 537 p.hrs = t->time.tm_hour; 538 p.min = t->time.tm_min; 539 p.sec = t->time.tm_sec; 540 541 spin_lock_irq(&rtc_lock); 542 rtc_control = CMOS_READ(RTC_CONTROL); 543 spin_unlock_irq(&rtc_lock); 544 545 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { 546 /* Writing 0xff means "don't care" or "match all". */ 547 p.mon = (p.mon <= 12) ? bin2bcd(p.mon) : 0xff; 548 p.mday = (p.mday >= 1 && p.mday <= 31) ? bin2bcd(p.mday) : 0xff; 549 p.hrs = (p.hrs < 24) ? bin2bcd(p.hrs) : 0xff; 550 p.min = (p.min < 60) ? bin2bcd(p.min) : 0xff; 551 p.sec = (p.sec < 60) ? bin2bcd(p.sec) : 0xff; 552 } 553 554 /* 555 * Some Intel chipsets disconnect the alarm registers when the clock 556 * update is in progress - during this time writes fail silently. 557 * 558 * Use mc146818_avoid_UIP() to avoid this. 559 */ 560 if (!mc146818_avoid_UIP(cmos_set_alarm_callback, 10, &p)) 561 return -ETIMEDOUT; 562 563 cmos->alarm_expires = rtc_tm_to_time64(&t->time); 564 565 return 0; 566 } 567 568 static int cmos_alarm_irq_enable(struct device *dev, unsigned int enabled) 569 { 570 struct cmos_rtc *cmos = dev_get_drvdata(dev); 571 unsigned long flags; 572 573 spin_lock_irqsave(&rtc_lock, flags); 574 575 if (enabled) 576 cmos_irq_enable(cmos, RTC_AIE); 577 else 578 cmos_irq_disable(cmos, RTC_AIE); 579 580 spin_unlock_irqrestore(&rtc_lock, flags); 581 return 0; 582 } 583 584 #if IS_ENABLED(CONFIG_RTC_INTF_PROC) 585 586 static int cmos_procfs(struct device *dev, struct seq_file *seq) 587 { 588 struct cmos_rtc *cmos = dev_get_drvdata(dev); 589 unsigned char rtc_control, valid; 590 591 spin_lock_irq(&rtc_lock); 592 rtc_control = CMOS_READ(RTC_CONTROL); 593 valid = CMOS_READ(RTC_VALID); 594 spin_unlock_irq(&rtc_lock); 595 596 /* NOTE: at least ICH6 reports battery status using a different 597 * (non-RTC) bit; and SQWE is ignored on many current systems. 598 */ 599 seq_printf(seq, 600 "periodic_IRQ\t: %s\n" 601 "update_IRQ\t: %s\n" 602 "HPET_emulated\t: %s\n" 603 // "square_wave\t: %s\n" 604 "BCD\t\t: %s\n" 605 "DST_enable\t: %s\n" 606 "periodic_freq\t: %d\n" 607 "batt_status\t: %s\n", 608 (rtc_control & RTC_PIE) ? "yes" : "no", 609 (rtc_control & RTC_UIE) ? "yes" : "no", 610 use_hpet_alarm() ? "yes" : "no", 611 // (rtc_control & RTC_SQWE) ? "yes" : "no", 612 (rtc_control & RTC_DM_BINARY) ? "no" : "yes", 613 (rtc_control & RTC_DST_EN) ? "yes" : "no", 614 cmos->rtc->irq_freq, 615 (valid & RTC_VRT) ? "okay" : "dead"); 616 617 return 0; 618 } 619 620 #else 621 #define cmos_procfs NULL 622 #endif 623 624 static const struct rtc_class_ops cmos_rtc_ops = { 625 .read_time = cmos_read_time, 626 .set_time = cmos_set_time, 627 .read_alarm = cmos_read_alarm, 628 .set_alarm = cmos_set_alarm, 629 .proc = cmos_procfs, 630 .alarm_irq_enable = cmos_alarm_irq_enable, 631 }; 632 633 /*----------------------------------------------------------------*/ 634 635 /* 636 * All these chips have at least 64 bytes of address space, shared by 637 * RTC registers and NVRAM. Most of those bytes of NVRAM are used 638 * by boot firmware. Modern chips have 128 or 256 bytes. 639 */ 640 641 #define NVRAM_OFFSET (RTC_REG_D + 1) 642 643 static int cmos_nvram_read(void *priv, unsigned int off, void *val, 644 size_t count) 645 { 646 unsigned char *buf = val; 647 648 off += NVRAM_OFFSET; 649 for (; count; count--, off++, buf++) { 650 guard(spinlock_irq)(&rtc_lock); 651 if (off < 128) 652 *buf = CMOS_READ(off); 653 else if (can_bank2) 654 *buf = cmos_read_bank2(off); 655 else 656 return -EIO; 657 } 658 659 return 0; 660 } 661 662 static int cmos_nvram_write(void *priv, unsigned int off, void *val, 663 size_t count) 664 { 665 struct cmos_rtc *cmos = priv; 666 unsigned char *buf = val; 667 668 /* NOTE: on at least PCs and Ataris, the boot firmware uses a 669 * checksum on part of the NVRAM data. That's currently ignored 670 * here. If userspace is smart enough to know what fields of 671 * NVRAM to update, updating checksums is also part of its job. 672 */ 673 off += NVRAM_OFFSET; 674 for (; count; count--, off++, buf++) { 675 /* don't trash RTC registers */ 676 if (off == cmos->day_alrm 677 || off == cmos->mon_alrm 678 || off == cmos->century) 679 continue; 680 681 guard(spinlock_irq)(&rtc_lock); 682 if (off < 128) 683 CMOS_WRITE(*buf, off); 684 else if (can_bank2) 685 cmos_write_bank2(*buf, off); 686 else 687 return -EIO; 688 } 689 690 return 0; 691 } 692 693 /*----------------------------------------------------------------*/ 694 695 static struct cmos_rtc cmos_rtc; 696 697 static irqreturn_t cmos_interrupt(int irq, void *p) 698 { 699 u8 irqstat; 700 u8 rtc_control; 701 unsigned long flags; 702 703 /* We cannot use spin_lock() here, as cmos_interrupt() is also called 704 * in a non-irq context. 705 */ 706 spin_lock_irqsave(&rtc_lock, flags); 707 708 /* When the HPET interrupt handler calls us, the interrupt 709 * status is passed as arg1 instead of the irq number. But 710 * always clear irq status, even when HPET is in the way. 711 * 712 * Note that HPET and RTC are almost certainly out of phase, 713 * giving different IRQ status ... 714 */ 715 irqstat = CMOS_READ(RTC_INTR_FLAGS); 716 rtc_control = CMOS_READ(RTC_CONTROL); 717 if (use_hpet_alarm()) 718 irqstat = (unsigned long)irq & 0xF0; 719 720 /* If we were suspended, RTC_CONTROL may not be accurate since the 721 * bios may have cleared it. 722 */ 723 if (!cmos_rtc.suspend_ctrl) 724 irqstat &= (rtc_control & RTC_IRQMASK) | RTC_IRQF; 725 else 726 irqstat &= (cmos_rtc.suspend_ctrl & RTC_IRQMASK) | RTC_IRQF; 727 728 /* All Linux RTC alarms should be treated as if they were oneshot. 729 * Similar code may be needed in system wakeup paths, in case the 730 * alarm woke the system. 731 */ 732 if (irqstat & RTC_AIE) { 733 cmos_rtc.suspend_ctrl &= ~RTC_AIE; 734 rtc_control &= ~RTC_AIE; 735 CMOS_WRITE(rtc_control, RTC_CONTROL); 736 if (use_hpet_alarm()) 737 hpet_mask_rtc_irq_bit(RTC_AIE); 738 CMOS_READ(RTC_INTR_FLAGS); 739 } 740 spin_unlock_irqrestore(&rtc_lock, flags); 741 742 if (is_intr(irqstat)) { 743 rtc_update_irq(p, 1, irqstat); 744 return IRQ_HANDLED; 745 } else 746 return IRQ_NONE; 747 } 748 749 #ifdef CONFIG_ACPI 750 751 #include <linux/acpi.h> 752 753 static u32 rtc_handler(void *context) 754 { 755 struct device *dev = context; 756 struct cmos_rtc *cmos = dev_get_drvdata(dev); 757 unsigned char rtc_control = 0; 758 unsigned char rtc_intr; 759 unsigned long flags; 760 761 762 /* 763 * Always update rtc irq when ACPI is used as RTC Alarm. 764 * Or else, ACPI SCI is enabled during suspend/resume only, 765 * update rtc irq in that case. 766 */ 767 if (cmos_use_acpi_alarm()) 768 cmos_interrupt(0, (void *)cmos->rtc); 769 else { 770 /* Fix me: can we use cmos_interrupt() here as well? */ 771 spin_lock_irqsave(&rtc_lock, flags); 772 if (cmos_rtc.suspend_ctrl) 773 rtc_control = CMOS_READ(RTC_CONTROL); 774 if (rtc_control & RTC_AIE) { 775 cmos_rtc.suspend_ctrl &= ~RTC_AIE; 776 CMOS_WRITE(rtc_control, RTC_CONTROL); 777 rtc_intr = CMOS_READ(RTC_INTR_FLAGS); 778 rtc_update_irq(cmos->rtc, 1, rtc_intr); 779 } 780 spin_unlock_irqrestore(&rtc_lock, flags); 781 } 782 783 pm_wakeup_hard_event(dev); 784 acpi_clear_event(ACPI_EVENT_RTC); 785 acpi_disable_event(ACPI_EVENT_RTC, 0); 786 return ACPI_INTERRUPT_HANDLED; 787 } 788 789 static void acpi_rtc_event_setup(struct device *dev) 790 { 791 if (acpi_disabled) 792 return; 793 794 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, dev); 795 /* 796 * After the RTC handler is installed, the Fixed_RTC event should 797 * be disabled. Only when the RTC alarm is set will it be enabled. 798 */ 799 acpi_clear_event(ACPI_EVENT_RTC); 800 acpi_disable_event(ACPI_EVENT_RTC, 0); 801 } 802 803 static void acpi_rtc_event_cleanup(void) 804 { 805 if (acpi_disabled) 806 return; 807 808 acpi_remove_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler); 809 } 810 811 static void rtc_wake_on(struct device *dev) 812 { 813 acpi_clear_event(ACPI_EVENT_RTC); 814 acpi_enable_event(ACPI_EVENT_RTC, 0); 815 } 816 817 static void rtc_wake_off(struct device *dev) 818 { 819 acpi_disable_event(ACPI_EVENT_RTC, 0); 820 } 821 822 #ifdef CONFIG_X86 823 static void use_acpi_alarm_quirks(void) 824 { 825 if (acpi_gbl_FADT.flags & ACPI_FADT_FIXED_RTC) 826 return; 827 828 switch (boot_cpu_data.x86_vendor) { 829 case X86_VENDOR_INTEL: 830 if (dmi_get_bios_year() < 2015) 831 return; 832 break; 833 case X86_VENDOR_AMD: 834 case X86_VENDOR_HYGON: 835 if (dmi_get_bios_year() < 2021) 836 return; 837 break; 838 default: 839 return; 840 } 841 842 use_acpi_alarm = true; 843 } 844 #else 845 static inline void use_acpi_alarm_quirks(void) { } 846 #endif 847 848 static void acpi_cmos_wake_setup(struct device *dev) 849 { 850 if (acpi_disabled) 851 return; 852 853 use_acpi_alarm_quirks(); 854 855 cmos_rtc.wake_on = rtc_wake_on; 856 cmos_rtc.wake_off = rtc_wake_off; 857 858 /* ACPI tables bug workaround. */ 859 if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) { 860 dev_dbg(dev, "bogus FADT month_alarm (%d)\n", 861 acpi_gbl_FADT.month_alarm); 862 acpi_gbl_FADT.month_alarm = 0; 863 } 864 865 cmos_rtc.day_alrm = acpi_gbl_FADT.day_alarm; 866 cmos_rtc.mon_alrm = acpi_gbl_FADT.month_alarm; 867 cmos_rtc.century = acpi_gbl_FADT.century; 868 869 if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE) 870 dev_info(dev, "RTC can wake from S4\n"); 871 872 /* RTC always wakes from S1/S2/S3, and often S4/STD */ 873 device_init_wakeup(dev, true); 874 } 875 876 static void cmos_check_acpi_rtc_status(struct device *dev, 877 unsigned char *rtc_control) 878 { 879 struct cmos_rtc *cmos = dev_get_drvdata(dev); 880 acpi_event_status rtc_status; 881 acpi_status status; 882 883 if (acpi_gbl_FADT.flags & ACPI_FADT_FIXED_RTC) 884 return; 885 886 status = acpi_get_event_status(ACPI_EVENT_RTC, &rtc_status); 887 if (ACPI_FAILURE(status)) { 888 dev_err(dev, "Could not get RTC status\n"); 889 } else if (rtc_status & ACPI_EVENT_FLAG_SET) { 890 unsigned char mask; 891 *rtc_control &= ~RTC_AIE; 892 CMOS_WRITE(*rtc_control, RTC_CONTROL); 893 mask = CMOS_READ(RTC_INTR_FLAGS); 894 rtc_update_irq(cmos->rtc, 1, mask); 895 } 896 } 897 898 #else /* !CONFIG_ACPI */ 899 900 static inline void acpi_rtc_event_setup(struct device *dev) 901 { 902 } 903 904 static inline void acpi_rtc_event_cleanup(void) 905 { 906 } 907 908 static inline void acpi_cmos_wake_setup(struct device *dev) 909 { 910 } 911 912 static inline void cmos_check_acpi_rtc_status(struct device *dev, 913 unsigned char *rtc_control) 914 { 915 } 916 #endif /* CONFIG_ACPI */ 917 918 #ifdef CONFIG_PNP 919 #define INITSECTION 920 921 #else 922 #define INITSECTION __init 923 #endif 924 925 #define SECS_PER_DAY (24 * 60 * 60) 926 #define SECS_PER_MONTH (28 * SECS_PER_DAY) 927 #define SECS_PER_YEAR (365 * SECS_PER_DAY) 928 929 static int INITSECTION 930 cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq) 931 { 932 struct cmos_rtc_board_info *info = dev_get_platdata(dev); 933 int retval = 0; 934 unsigned char rtc_control; 935 unsigned address_space; 936 u32 flags = 0; 937 struct nvmem_config nvmem_cfg = { 938 .name = "cmos_nvram", 939 .word_size = 1, 940 .stride = 1, 941 .reg_read = cmos_nvram_read, 942 .reg_write = cmos_nvram_write, 943 .priv = &cmos_rtc, 944 }; 945 946 /* there can be only one ... */ 947 if (cmos_rtc.dev) 948 return -EBUSY; 949 950 if (!ports) 951 return -ENODEV; 952 953 /* Claim I/O ports ASAP, minimizing conflict with legacy driver. 954 * 955 * REVISIT non-x86 systems may instead use memory space resources 956 * (needing ioremap etc), not i/o space resources like this ... 957 */ 958 if (RTC_IOMAPPED) 959 ports = request_region(ports->start, resource_size(ports), 960 driver_name); 961 else 962 ports = request_mem_region(ports->start, resource_size(ports), 963 driver_name); 964 if (!ports) { 965 dev_dbg(dev, "i/o registers already in use\n"); 966 return -EBUSY; 967 } 968 969 cmos_rtc.irq = rtc_irq; 970 cmos_rtc.iomem = ports; 971 972 /* Heuristic to deduce NVRAM size ... do what the legacy NVRAM 973 * driver did, but don't reject unknown configs. Old hardware 974 * won't address 128 bytes. Newer chips have multiple banks, 975 * though they may not be listed in one I/O resource. 976 */ 977 #if defined(CONFIG_ATARI) 978 address_space = 64; 979 #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) \ 980 || defined(__sparc__) || defined(__mips__) \ 981 || defined(__powerpc__) 982 address_space = 128; 983 #else 984 #warning Assuming 128 bytes of RTC+NVRAM address space, not 64 bytes. 985 address_space = 128; 986 #endif 987 if (can_bank2 && ports->end > (ports->start + 1)) 988 address_space = 256; 989 990 /* For ACPI systems extension info comes from the FADT. On others, 991 * board specific setup provides it as appropriate. Systems where 992 * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and 993 * some almost-clones) can provide hooks to make that behave. 994 * 995 * Note that ACPI doesn't preclude putting these registers into 996 * "extended" areas of the chip, including some that we won't yet 997 * expect CMOS_READ and friends to handle. 998 */ 999 if (info) { 1000 if (info->flags) 1001 flags = info->flags; 1002 if (info->address_space) 1003 address_space = info->address_space; 1004 1005 cmos_rtc.day_alrm = info->rtc_day_alarm; 1006 cmos_rtc.mon_alrm = info->rtc_mon_alarm; 1007 cmos_rtc.century = info->rtc_century; 1008 1009 if (info->wake_on && info->wake_off) { 1010 cmos_rtc.wake_on = info->wake_on; 1011 cmos_rtc.wake_off = info->wake_off; 1012 } 1013 } else { 1014 acpi_cmos_wake_setup(dev); 1015 } 1016 1017 if (cmos_rtc.day_alrm >= 128) 1018 cmos_rtc.day_alrm = 0; 1019 1020 if (cmos_rtc.mon_alrm >= 128) 1021 cmos_rtc.mon_alrm = 0; 1022 1023 if (cmos_rtc.century >= 128) 1024 cmos_rtc.century = 0; 1025 1026 cmos_rtc.dev = dev; 1027 dev_set_drvdata(dev, &cmos_rtc); 1028 1029 cmos_rtc.rtc = devm_rtc_allocate_device(dev); 1030 if (IS_ERR(cmos_rtc.rtc)) { 1031 retval = PTR_ERR(cmos_rtc.rtc); 1032 goto cleanup0; 1033 } 1034 1035 if (cmos_rtc.mon_alrm) 1036 cmos_rtc.rtc->alarm_offset_max = SECS_PER_YEAR - 1; 1037 else if (cmos_rtc.day_alrm) 1038 cmos_rtc.rtc->alarm_offset_max = SECS_PER_MONTH - 1; 1039 else 1040 cmos_rtc.rtc->alarm_offset_max = SECS_PER_DAY - 1; 1041 1042 rename_region(ports, dev_name(&cmos_rtc.rtc->dev)); 1043 1044 if (!mc146818_does_rtc_work()) { 1045 dev_warn(dev, "broken or not accessible\n"); 1046 retval = -ENXIO; 1047 goto cleanup1; 1048 } 1049 1050 spin_lock_irq(&rtc_lock); 1051 1052 if (!(flags & CMOS_RTC_FLAGS_NOFREQ)) { 1053 /* force periodic irq to CMOS reset default of 1024Hz; 1054 * 1055 * REVISIT it's been reported that at least one x86_64 ALI 1056 * mobo doesn't use 32KHz here ... for portability we might 1057 * need to do something about other clock frequencies. 1058 */ 1059 cmos_rtc.rtc->irq_freq = 1024; 1060 if (use_hpet_alarm()) 1061 hpet_set_periodic_freq(cmos_rtc.rtc->irq_freq); 1062 CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT); 1063 } 1064 1065 /* disable irqs */ 1066 if (is_valid_irq(rtc_irq)) 1067 cmos_irq_disable(&cmos_rtc, RTC_PIE | RTC_AIE | RTC_UIE); 1068 1069 rtc_control = CMOS_READ(RTC_CONTROL); 1070 1071 spin_unlock_irq(&rtc_lock); 1072 1073 if (is_valid_irq(rtc_irq) && !(rtc_control & RTC_24H)) { 1074 dev_warn(dev, "only 24-hr supported\n"); 1075 retval = -ENXIO; 1076 goto cleanup1; 1077 } 1078 1079 if (use_hpet_alarm()) 1080 hpet_rtc_timer_init(); 1081 1082 if (is_valid_irq(rtc_irq)) { 1083 irq_handler_t rtc_cmos_int_handler; 1084 1085 if (use_hpet_alarm()) { 1086 rtc_cmos_int_handler = hpet_rtc_interrupt; 1087 retval = hpet_register_irq_handler(cmos_interrupt); 1088 if (retval) { 1089 hpet_mask_rtc_irq_bit(RTC_IRQMASK); 1090 dev_warn(dev, "hpet_register_irq_handler " 1091 " failed in rtc_init()."); 1092 goto cleanup1; 1093 } 1094 } else 1095 rtc_cmos_int_handler = cmos_interrupt; 1096 1097 retval = request_irq(rtc_irq, rtc_cmos_int_handler, 1098 0, dev_name(&cmos_rtc.rtc->dev), 1099 cmos_rtc.rtc); 1100 if (retval < 0) { 1101 dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq); 1102 goto cleanup1; 1103 } 1104 } else if (!cmos_use_acpi_alarm()) { 1105 clear_bit(RTC_FEATURE_ALARM, cmos_rtc.rtc->features); 1106 } 1107 1108 cmos_rtc.rtc->ops = &cmos_rtc_ops; 1109 1110 retval = devm_rtc_register_device(cmos_rtc.rtc); 1111 if (retval) 1112 goto cleanup2; 1113 1114 /* Set the sync offset for the periodic 11min update correct */ 1115 cmos_rtc.rtc->set_offset_nsec = NSEC_PER_SEC / 2; 1116 1117 /* export at least the first block of NVRAM */ 1118 nvmem_cfg.size = address_space - NVRAM_OFFSET; 1119 devm_rtc_nvmem_register(cmos_rtc.rtc, &nvmem_cfg); 1120 1121 /* 1122 * Everything has gone well so far, so by default register a handler for 1123 * the ACPI RTC fixed event. 1124 */ 1125 if (!info) 1126 acpi_rtc_event_setup(dev); 1127 1128 dev_info(dev, "%s%s, %d bytes nvram%s\n", 1129 cmos_no_alarm(&cmos_rtc) ? "no alarms" : 1130 cmos_rtc.mon_alrm ? "alarms up to one year" : 1131 cmos_rtc.day_alrm ? "alarms up to one month" : 1132 "alarms up to one day", 1133 cmos_rtc.century ? ", y3k" : "", 1134 nvmem_cfg.size, 1135 use_hpet_alarm() ? ", hpet irqs" : ""); 1136 1137 return 0; 1138 1139 cleanup2: 1140 if (is_valid_irq(rtc_irq)) 1141 free_irq(rtc_irq, cmos_rtc.rtc); 1142 cleanup1: 1143 cmos_rtc.dev = NULL; 1144 cleanup0: 1145 if (RTC_IOMAPPED) 1146 release_region(ports->start, resource_size(ports)); 1147 else 1148 release_mem_region(ports->start, resource_size(ports)); 1149 return retval; 1150 } 1151 1152 static void cmos_do_shutdown(int rtc_irq) 1153 { 1154 spin_lock_irq(&rtc_lock); 1155 if (!cmos_no_alarm(&cmos_rtc)) 1156 cmos_irq_disable(&cmos_rtc, RTC_IRQMASK); 1157 spin_unlock_irq(&rtc_lock); 1158 } 1159 1160 static void cmos_do_remove(struct device *dev) 1161 { 1162 struct cmos_rtc *cmos = dev_get_drvdata(dev); 1163 struct resource *ports; 1164 1165 cmos_do_shutdown(cmos->irq); 1166 1167 if (is_valid_irq(cmos->irq)) { 1168 free_irq(cmos->irq, cmos->rtc); 1169 if (use_hpet_alarm()) 1170 hpet_unregister_irq_handler(cmos_interrupt); 1171 } 1172 1173 if (!dev_get_platdata(dev)) 1174 acpi_rtc_event_cleanup(); 1175 1176 cmos->rtc = NULL; 1177 1178 ports = cmos->iomem; 1179 if (RTC_IOMAPPED) 1180 release_region(ports->start, resource_size(ports)); 1181 else 1182 release_mem_region(ports->start, resource_size(ports)); 1183 cmos->iomem = NULL; 1184 1185 cmos->dev = NULL; 1186 } 1187 1188 static int cmos_aie_poweroff(struct device *dev) 1189 { 1190 struct cmos_rtc *cmos = dev_get_drvdata(dev); 1191 struct rtc_time now; 1192 time64_t t_now; 1193 int retval = 0; 1194 unsigned char rtc_control; 1195 1196 if (!cmos->alarm_expires) 1197 return -EINVAL; 1198 1199 spin_lock_irq(&rtc_lock); 1200 rtc_control = CMOS_READ(RTC_CONTROL); 1201 spin_unlock_irq(&rtc_lock); 1202 1203 /* We only care about the situation where AIE is disabled. */ 1204 if (rtc_control & RTC_AIE) 1205 return -EBUSY; 1206 1207 cmos_read_time(dev, &now); 1208 t_now = rtc_tm_to_time64(&now); 1209 1210 /* 1211 * When enabling "RTC wake-up" in BIOS setup, the machine reboots 1212 * automatically right after shutdown on some buggy boxes. 1213 * This automatic rebooting issue won't happen when the alarm 1214 * time is larger than now+1 seconds. 1215 * 1216 * If the alarm time is equal to now+1 seconds, the issue can be 1217 * prevented by cancelling the alarm. 1218 */ 1219 if (cmos->alarm_expires == t_now + 1) { 1220 struct rtc_wkalrm alarm; 1221 1222 /* Cancel the AIE timer by configuring the past time. */ 1223 rtc_time64_to_tm(t_now - 1, &alarm.time); 1224 alarm.enabled = 0; 1225 retval = cmos_set_alarm(dev, &alarm); 1226 } else if (cmos->alarm_expires > t_now + 1) { 1227 retval = -EBUSY; 1228 } 1229 1230 return retval; 1231 } 1232 1233 static int cmos_suspend(struct device *dev) 1234 { 1235 struct cmos_rtc *cmos = dev_get_drvdata(dev); 1236 unsigned char tmp; 1237 1238 /* only the alarm might be a wakeup event source */ 1239 spin_lock_irq(&rtc_lock); 1240 cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL); 1241 if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) { 1242 unsigned char mask; 1243 1244 if (device_may_wakeup(dev)) 1245 mask = RTC_IRQMASK & ~RTC_AIE; 1246 else 1247 mask = RTC_IRQMASK; 1248 tmp &= ~mask; 1249 CMOS_WRITE(tmp, RTC_CONTROL); 1250 if (use_hpet_alarm()) 1251 hpet_mask_rtc_irq_bit(mask); 1252 cmos_checkintr(cmos, tmp); 1253 } 1254 spin_unlock_irq(&rtc_lock); 1255 1256 if ((tmp & RTC_AIE) && !cmos_use_acpi_alarm()) { 1257 cmos->enabled_wake = 1; 1258 if (cmos->wake_on) 1259 cmos->wake_on(dev); 1260 else 1261 enable_irq_wake(cmos->irq); 1262 } 1263 1264 memset(&cmos->saved_wkalrm, 0, sizeof(struct rtc_wkalrm)); 1265 cmos_read_alarm(dev, &cmos->saved_wkalrm); 1266 1267 dev_dbg(dev, "suspend%s, ctrl %02x\n", 1268 (tmp & RTC_AIE) ? ", alarm may wake" : "", 1269 tmp); 1270 1271 return 0; 1272 } 1273 1274 /* We want RTC alarms to wake us from e.g. ACPI G2/S5 "soft off", even 1275 * after a detour through G3 "mechanical off", although the ACPI spec 1276 * says wakeup should only work from G1/S4 "hibernate". To most users, 1277 * distinctions between S4 and S5 are pointless. So when the hardware 1278 * allows, don't draw that distinction. 1279 */ 1280 static inline int cmos_poweroff(struct device *dev) 1281 { 1282 if (!IS_ENABLED(CONFIG_PM)) 1283 return -ENOSYS; 1284 1285 return cmos_suspend(dev); 1286 } 1287 1288 static void cmos_check_wkalrm(struct device *dev) 1289 { 1290 struct cmos_rtc *cmos = dev_get_drvdata(dev); 1291 struct rtc_wkalrm current_alarm; 1292 time64_t t_now; 1293 time64_t t_current_expires; 1294 time64_t t_saved_expires; 1295 struct rtc_time now; 1296 1297 /* Check if we have RTC Alarm armed */ 1298 if (!(cmos->suspend_ctrl & RTC_AIE)) 1299 return; 1300 1301 cmos_read_time(dev, &now); 1302 t_now = rtc_tm_to_time64(&now); 1303 1304 /* 1305 * ACPI RTC wake event is cleared after resume from STR, 1306 * ACK the rtc irq here 1307 */ 1308 if (t_now >= cmos->alarm_expires && cmos_use_acpi_alarm()) { 1309 cmos_interrupt(0, (void *)cmos->rtc); 1310 return; 1311 } 1312 1313 memset(¤t_alarm, 0, sizeof(struct rtc_wkalrm)); 1314 cmos_read_alarm(dev, ¤t_alarm); 1315 t_current_expires = rtc_tm_to_time64(¤t_alarm.time); 1316 t_saved_expires = rtc_tm_to_time64(&cmos->saved_wkalrm.time); 1317 if (t_current_expires != t_saved_expires || 1318 cmos->saved_wkalrm.enabled != current_alarm.enabled) { 1319 cmos_set_alarm(dev, &cmos->saved_wkalrm); 1320 } 1321 } 1322 1323 static int __maybe_unused cmos_resume(struct device *dev) 1324 { 1325 struct cmos_rtc *cmos = dev_get_drvdata(dev); 1326 unsigned char tmp; 1327 1328 if (cmos->enabled_wake && !cmos_use_acpi_alarm()) { 1329 if (cmos->wake_off) 1330 cmos->wake_off(dev); 1331 else 1332 disable_irq_wake(cmos->irq); 1333 cmos->enabled_wake = 0; 1334 } 1335 1336 /* The BIOS might have changed the alarm, restore it */ 1337 cmos_check_wkalrm(dev); 1338 1339 spin_lock_irq(&rtc_lock); 1340 tmp = cmos->suspend_ctrl; 1341 cmos->suspend_ctrl = 0; 1342 /* re-enable any irqs previously active */ 1343 if (tmp & RTC_IRQMASK) { 1344 unsigned char mask; 1345 1346 if (device_may_wakeup(dev) && use_hpet_alarm()) 1347 hpet_rtc_timer_init(); 1348 1349 do { 1350 CMOS_WRITE(tmp, RTC_CONTROL); 1351 if (use_hpet_alarm()) 1352 hpet_set_rtc_irq_bit(tmp & RTC_IRQMASK); 1353 1354 mask = CMOS_READ(RTC_INTR_FLAGS); 1355 mask &= (tmp & RTC_IRQMASK) | RTC_IRQF; 1356 if (!use_hpet_alarm() || !is_intr(mask)) 1357 break; 1358 1359 /* force one-shot behavior if HPET blocked 1360 * the wake alarm's irq 1361 */ 1362 rtc_update_irq(cmos->rtc, 1, mask); 1363 tmp &= ~RTC_AIE; 1364 hpet_mask_rtc_irq_bit(RTC_AIE); 1365 } while (mask & RTC_AIE); 1366 1367 if (tmp & RTC_AIE) 1368 cmos_check_acpi_rtc_status(dev, &tmp); 1369 } 1370 spin_unlock_irq(&rtc_lock); 1371 1372 dev_dbg(dev, "resume, ctrl %02x\n", tmp); 1373 1374 return 0; 1375 } 1376 1377 static SIMPLE_DEV_PM_OPS(cmos_pm_ops, cmos_suspend, cmos_resume); 1378 1379 #ifdef CONFIG_OF 1380 static const struct of_device_id of_cmos_match[] = { 1381 { 1382 .compatible = "motorola,mc146818", 1383 }, 1384 { }, 1385 }; 1386 MODULE_DEVICE_TABLE(of, of_cmos_match); 1387 1388 static __init void cmos_of_init(struct platform_device *pdev) 1389 { 1390 struct device_node *node = pdev->dev.of_node; 1391 const __be32 *val; 1392 1393 if (!node) 1394 return; 1395 1396 val = of_get_property(node, "ctrl-reg", NULL); 1397 if (val) 1398 CMOS_WRITE(be32_to_cpup(val), RTC_CONTROL); 1399 1400 val = of_get_property(node, "freq-reg", NULL); 1401 if (val) 1402 CMOS_WRITE(be32_to_cpup(val), RTC_FREQ_SELECT); 1403 } 1404 #else 1405 static inline void cmos_of_init(struct platform_device *pdev) {} 1406 #endif 1407 1408 #ifdef CONFIG_ACPI 1409 static const struct acpi_device_id acpi_cmos_rtc_ids[] = { 1410 ACPI_CMOS_RTC_IDS 1411 }; 1412 MODULE_DEVICE_TABLE(acpi, acpi_cmos_rtc_ids); 1413 #endif 1414 1415 /*----------------------------------------------------------------*/ 1416 1417 /* Platform setup should have set up an RTC device, when PNP is 1418 * unavailable ... this could happen even on (older) PCs. 1419 */ 1420 1421 static int __init cmos_platform_probe(struct platform_device *pdev) 1422 { 1423 struct resource *resource; 1424 int irq; 1425 1426 cmos_of_init(pdev); 1427 1428 if (RTC_IOMAPPED) 1429 resource = platform_get_resource(pdev, IORESOURCE_IO, 0); 1430 else 1431 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1432 irq = platform_get_irq(pdev, 0); 1433 if (irq < 0) 1434 irq = -1; 1435 1436 return cmos_do_probe(&pdev->dev, resource, irq); 1437 } 1438 1439 static void cmos_platform_remove(struct platform_device *pdev) 1440 { 1441 cmos_do_remove(&pdev->dev); 1442 } 1443 1444 static void cmos_platform_shutdown(struct platform_device *pdev) 1445 { 1446 struct device *dev = &pdev->dev; 1447 struct cmos_rtc *cmos = dev_get_drvdata(dev); 1448 1449 if (system_state == SYSTEM_POWER_OFF) { 1450 int retval = cmos_poweroff(dev); 1451 1452 if (cmos_aie_poweroff(dev) < 0 && !retval) 1453 return; 1454 } 1455 1456 cmos_do_shutdown(cmos->irq); 1457 } 1458 1459 /* work with hotplug and coldplug */ 1460 MODULE_ALIAS("platform:rtc_cmos"); 1461 1462 static struct platform_driver cmos_platform_driver = { 1463 .remove = cmos_platform_remove, 1464 .shutdown = cmos_platform_shutdown, 1465 .driver = { 1466 .name = driver_name, 1467 .pm = &cmos_pm_ops, 1468 .of_match_table = of_match_ptr(of_cmos_match), 1469 .acpi_match_table = ACPI_PTR(acpi_cmos_rtc_ids), 1470 } 1471 }; 1472 1473 static bool platform_driver_registered; 1474 1475 static int __init cmos_init(void) 1476 { 1477 int retval; 1478 1479 if (cmos_rtc.dev) 1480 return 0; 1481 1482 retval = platform_driver_probe(&cmos_platform_driver, cmos_platform_probe); 1483 if (retval) 1484 return retval; 1485 1486 platform_driver_registered = true; 1487 1488 return 0; 1489 } 1490 module_init(cmos_init); 1491 1492 static void __exit cmos_exit(void) 1493 { 1494 if (platform_driver_registered) 1495 platform_driver_unregister(&cmos_platform_driver); 1496 } 1497 module_exit(cmos_exit); 1498 1499 1500 MODULE_AUTHOR("David Brownell"); 1501 MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs"); 1502 MODULE_LICENSE("GPL"); 1503