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