Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * RTC class driver for "CMOS RTC":  PCs, ACPI, etc
0004  *
0005  * Copyright (C) 1996 Paul Gortmaker (drivers/char/rtc.c)
0006  * Copyright (C) 2006 David Brownell (convert to new framework)
0007  */
0008 
0009 /*
0010  * The original "cmos clock" chip was an MC146818 chip, now obsolete.
0011  * That defined the register interface now provided by all PCs, some
0012  * non-PC systems, and incorporated into ACPI.  Modern PC chipsets
0013  * integrate an MC146818 clone in their southbridge, and boards use
0014  * that instead of discrete clones like the DS12887 or M48T86.  There
0015  * are also clones that connect using the LPC bus.
0016  *
0017  * That register API is also used directly by various other drivers
0018  * (notably for integrated NVRAM), infrastructure (x86 has code to
0019  * bypass the RTC framework, directly reading the RTC during boot
0020  * and updating minutes/seconds for systems using NTP synch) and
0021  * utilities (like userspace 'hwclock', if no /dev node exists).
0022  *
0023  * So **ALL** calls to CMOS_READ and CMOS_WRITE must be done with
0024  * interrupts disabled, holding the global rtc_lock, to exclude those
0025  * other drivers and utilities on correctly configured systems.
0026  */
0027 
0028 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0029 
0030 #include <linux/kernel.h>
0031 #include <linux/module.h>
0032 #include <linux/init.h>
0033 #include <linux/interrupt.h>
0034 #include <linux/spinlock.h>
0035 #include <linux/platform_device.h>
0036 #include <linux/log2.h>
0037 #include <linux/pm.h>
0038 #include <linux/of.h>
0039 #include <linux/of_platform.h>
0040 #ifdef CONFIG_X86
0041 #include <asm/i8259.h>
0042 #include <asm/processor.h>
0043 #include <linux/dmi.h>
0044 #endif
0045 
0046 /* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */
0047 #include <linux/mc146818rtc.h>
0048 
0049 #ifdef CONFIG_ACPI
0050 /*
0051  * Use ACPI SCI to replace HPET interrupt for RTC Alarm event
0052  *
0053  * If cleared, ACPI SCI is only used to wake up the system from suspend
0054  *
0055  * If set, ACPI SCI is used to handle UIE/AIE and system wakeup
0056  */
0057 
0058 static bool use_acpi_alarm;
0059 module_param(use_acpi_alarm, bool, 0444);
0060 
0061 static inline int cmos_use_acpi_alarm(void)
0062 {
0063     return use_acpi_alarm;
0064 }
0065 #else /* !CONFIG_ACPI */
0066 
0067 static inline int cmos_use_acpi_alarm(void)
0068 {
0069     return 0;
0070 }
0071 #endif
0072 
0073 struct cmos_rtc {
0074     struct rtc_device   *rtc;
0075     struct device       *dev;
0076     int         irq;
0077     struct resource     *iomem;
0078     time64_t        alarm_expires;
0079 
0080     void            (*wake_on)(struct device *);
0081     void            (*wake_off)(struct device *);
0082 
0083     u8          enabled_wake;
0084     u8          suspend_ctrl;
0085 
0086     /* newer hardware extends the original register set */
0087     u8          day_alrm;
0088     u8          mon_alrm;
0089     u8          century;
0090 
0091     struct rtc_wkalrm   saved_wkalrm;
0092 };
0093 
0094 /* both platform and pnp busses use negative numbers for invalid irqs */
0095 #define is_valid_irq(n)     ((n) > 0)
0096 
0097 static const char driver_name[] = "rtc_cmos";
0098 
0099 /* The RTC_INTR register may have e.g. RTC_PF set even if RTC_PIE is clear;
0100  * always mask it against the irq enable bits in RTC_CONTROL.  Bit values
0101  * are the same: PF==PIE, AF=AIE, UF=UIE; so RTC_IRQMASK works with both.
0102  */
0103 #define RTC_IRQMASK (RTC_PF | RTC_AF | RTC_UF)
0104 
0105 static inline int is_intr(u8 rtc_intr)
0106 {
0107     if (!(rtc_intr & RTC_IRQF))
0108         return 0;
0109     return rtc_intr & RTC_IRQMASK;
0110 }
0111 
0112 /*----------------------------------------------------------------*/
0113 
0114 /* Much modern x86 hardware has HPETs (10+ MHz timers) which, because
0115  * many BIOS programmers don't set up "sane mode" IRQ routing, are mostly
0116  * used in a broken "legacy replacement" mode.  The breakage includes
0117  * HPET #1 hijacking the IRQ for this RTC, and being unavailable for
0118  * other (better) use.
0119  *
0120  * When that broken mode is in use, platform glue provides a partial
0121  * emulation of hardware RTC IRQ facilities using HPET #1.  We don't
0122  * want to use HPET for anything except those IRQs though...
0123  */
0124 #ifdef CONFIG_HPET_EMULATE_RTC
0125 #include <asm/hpet.h>
0126 #else
0127 
0128 static inline int is_hpet_enabled(void)
0129 {
0130     return 0;
0131 }
0132 
0133 static inline int hpet_mask_rtc_irq_bit(unsigned long mask)
0134 {
0135     return 0;
0136 }
0137 
0138 static inline int hpet_set_rtc_irq_bit(unsigned long mask)
0139 {
0140     return 0;
0141 }
0142 
0143 static inline int
0144 hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
0145 {
0146     return 0;
0147 }
0148 
0149 static inline int hpet_set_periodic_freq(unsigned long freq)
0150 {
0151     return 0;
0152 }
0153 
0154 static inline int hpet_rtc_dropped_irq(void)
0155 {
0156     return 0;
0157 }
0158 
0159 static inline int hpet_rtc_timer_init(void)
0160 {
0161     return 0;
0162 }
0163 
0164 extern irq_handler_t hpet_rtc_interrupt;
0165 
0166 static inline int hpet_register_irq_handler(irq_handler_t handler)
0167 {
0168     return 0;
0169 }
0170 
0171 static inline int hpet_unregister_irq_handler(irq_handler_t handler)
0172 {
0173     return 0;
0174 }
0175 
0176 #endif
0177 
0178 /* Don't use HPET for RTC Alarm event if ACPI Fixed event is used */
0179 static inline int use_hpet_alarm(void)
0180 {
0181     return is_hpet_enabled() && !cmos_use_acpi_alarm();
0182 }
0183 
0184 /*----------------------------------------------------------------*/
0185 
0186 #ifdef RTC_PORT
0187 
0188 /* Most newer x86 systems have two register banks, the first used
0189  * for RTC and NVRAM and the second only for NVRAM.  Caller must
0190  * own rtc_lock ... and we won't worry about access during NMI.
0191  */
0192 #define can_bank2   true
0193 
0194 static inline unsigned char cmos_read_bank2(unsigned char addr)
0195 {
0196     outb(addr, RTC_PORT(2));
0197     return inb(RTC_PORT(3));
0198 }
0199 
0200 static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
0201 {
0202     outb(addr, RTC_PORT(2));
0203     outb(val, RTC_PORT(3));
0204 }
0205 
0206 #else
0207 
0208 #define can_bank2   false
0209 
0210 static inline unsigned char cmos_read_bank2(unsigned char addr)
0211 {
0212     return 0;
0213 }
0214 
0215 static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
0216 {
0217 }
0218 
0219 #endif
0220 
0221 /*----------------------------------------------------------------*/
0222 
0223 static int cmos_read_time(struct device *dev, struct rtc_time *t)
0224 {
0225     int ret;
0226 
0227     /*
0228      * If pm_trace abused the RTC for storage, set the timespec to 0,
0229      * which tells the caller that this RTC value is unusable.
0230      */
0231     if (!pm_trace_rtc_valid())
0232         return -EIO;
0233 
0234     ret = mc146818_get_time(t);
0235     if (ret < 0) {
0236         dev_err_ratelimited(dev, "unable to read current time\n");
0237         return ret;
0238     }
0239 
0240     return 0;
0241 }
0242 
0243 static int cmos_set_time(struct device *dev, struct rtc_time *t)
0244 {
0245     /* NOTE: this ignores the issue whereby updating the seconds
0246      * takes effect exactly 500ms after we write the register.
0247      * (Also queueing and other delays before we get this far.)
0248      */
0249     return mc146818_set_time(t);
0250 }
0251 
0252 struct cmos_read_alarm_callback_param {
0253     struct cmos_rtc *cmos;
0254     struct rtc_time *time;
0255     unsigned char   rtc_control;
0256 };
0257 
0258 static void cmos_read_alarm_callback(unsigned char __always_unused seconds,
0259                      void *param_in)
0260 {
0261     struct cmos_read_alarm_callback_param *p =
0262         (struct cmos_read_alarm_callback_param *)param_in;
0263     struct rtc_time *time = p->time;
0264 
0265     time->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
0266     time->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
0267     time->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
0268 
0269     if (p->cmos->day_alrm) {
0270         /* ignore upper bits on readback per ACPI spec */
0271         time->tm_mday = CMOS_READ(p->cmos->day_alrm) & 0x3f;
0272         if (!time->tm_mday)
0273             time->tm_mday = -1;
0274 
0275         if (p->cmos->mon_alrm) {
0276             time->tm_mon = CMOS_READ(p->cmos->mon_alrm);
0277             if (!time->tm_mon)
0278                 time->tm_mon = -1;
0279         }
0280     }
0281 
0282     p->rtc_control = CMOS_READ(RTC_CONTROL);
0283 }
0284 
0285 static int cmos_read_alarm(struct device *dev, struct rtc_wkalrm *t)
0286 {
0287     struct cmos_rtc *cmos = dev_get_drvdata(dev);
0288     struct cmos_read_alarm_callback_param p = {
0289         .cmos = cmos,
0290         .time = &t->time,
0291     };
0292 
0293     /* This not only a rtc_op, but also called directly */
0294     if (!is_valid_irq(cmos->irq))
0295         return -EIO;
0296 
0297     /* Basic alarms only support hour, minute, and seconds fields.
0298      * Some also support day and month, for alarms up to a year in
0299      * the future.
0300      */
0301 
0302     /* Some Intel chipsets disconnect the alarm registers when the clock
0303      * update is in progress - during this time reads return bogus values
0304      * and writes may fail silently. See for example "7th Generation Intel®
0305      * Processor Family I/O for U/Y Platforms [...] Datasheet", section
0306      * 27.7.1
0307      *
0308      * Use the mc146818_avoid_UIP() function to avoid this.
0309      */
0310     if (!mc146818_avoid_UIP(cmos_read_alarm_callback, &p))
0311         return -EIO;
0312 
0313     if (!(p.rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
0314         if (((unsigned)t->time.tm_sec) < 0x60)
0315             t->time.tm_sec = bcd2bin(t->time.tm_sec);
0316         else
0317             t->time.tm_sec = -1;
0318         if (((unsigned)t->time.tm_min) < 0x60)
0319             t->time.tm_min = bcd2bin(t->time.tm_min);
0320         else
0321             t->time.tm_min = -1;
0322         if (((unsigned)t->time.tm_hour) < 0x24)
0323             t->time.tm_hour = bcd2bin(t->time.tm_hour);
0324         else
0325             t->time.tm_hour = -1;
0326 
0327         if (cmos->day_alrm) {
0328             if (((unsigned)t->time.tm_mday) <= 0x31)
0329                 t->time.tm_mday = bcd2bin(t->time.tm_mday);
0330             else
0331                 t->time.tm_mday = -1;
0332 
0333             if (cmos->mon_alrm) {
0334                 if (((unsigned)t->time.tm_mon) <= 0x12)
0335                     t->time.tm_mon = bcd2bin(t->time.tm_mon)-1;
0336                 else
0337                     t->time.tm_mon = -1;
0338             }
0339         }
0340     }
0341 
0342     t->enabled = !!(p.rtc_control & RTC_AIE);
0343     t->pending = 0;
0344 
0345     return 0;
0346 }
0347 
0348 static void cmos_checkintr(struct cmos_rtc *cmos, unsigned char rtc_control)
0349 {
0350     unsigned char   rtc_intr;
0351 
0352     /* NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
0353      * allegedly some older rtcs need that to handle irqs properly
0354      */
0355     rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
0356 
0357     if (use_hpet_alarm())
0358         return;
0359 
0360     rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
0361     if (is_intr(rtc_intr))
0362         rtc_update_irq(cmos->rtc, 1, rtc_intr);
0363 }
0364 
0365 static void cmos_irq_enable(struct cmos_rtc *cmos, unsigned char mask)
0366 {
0367     unsigned char   rtc_control;
0368 
0369     /* flush any pending IRQ status, notably for update irqs,
0370      * before we enable new IRQs
0371      */
0372     rtc_control = CMOS_READ(RTC_CONTROL);
0373     cmos_checkintr(cmos, rtc_control);
0374 
0375     rtc_control |= mask;
0376     CMOS_WRITE(rtc_control, RTC_CONTROL);
0377     if (use_hpet_alarm())
0378         hpet_set_rtc_irq_bit(mask);
0379 
0380     if ((mask & RTC_AIE) && cmos_use_acpi_alarm()) {
0381         if (cmos->wake_on)
0382             cmos->wake_on(cmos->dev);
0383     }
0384 
0385     cmos_checkintr(cmos, rtc_control);
0386 }
0387 
0388 static void cmos_irq_disable(struct cmos_rtc *cmos, unsigned char mask)
0389 {
0390     unsigned char   rtc_control;
0391 
0392     rtc_control = CMOS_READ(RTC_CONTROL);
0393     rtc_control &= ~mask;
0394     CMOS_WRITE(rtc_control, RTC_CONTROL);
0395     if (use_hpet_alarm())
0396         hpet_mask_rtc_irq_bit(mask);
0397 
0398     if ((mask & RTC_AIE) && cmos_use_acpi_alarm()) {
0399         if (cmos->wake_off)
0400             cmos->wake_off(cmos->dev);
0401     }
0402 
0403     cmos_checkintr(cmos, rtc_control);
0404 }
0405 
0406 static int cmos_validate_alarm(struct device *dev, struct rtc_wkalrm *t)
0407 {
0408     struct cmos_rtc *cmos = dev_get_drvdata(dev);
0409     struct rtc_time now;
0410 
0411     cmos_read_time(dev, &now);
0412 
0413     if (!cmos->day_alrm) {
0414         time64_t t_max_date;
0415         time64_t t_alrm;
0416 
0417         t_max_date = rtc_tm_to_time64(&now);
0418         t_max_date += 24 * 60 * 60 - 1;
0419         t_alrm = rtc_tm_to_time64(&t->time);
0420         if (t_alrm > t_max_date) {
0421             dev_err(dev,
0422                 "Alarms can be up to one day in the future\n");
0423             return -EINVAL;
0424         }
0425     } else if (!cmos->mon_alrm) {
0426         struct rtc_time max_date = now;
0427         time64_t t_max_date;
0428         time64_t t_alrm;
0429         int max_mday;
0430 
0431         if (max_date.tm_mon == 11) {
0432             max_date.tm_mon = 0;
0433             max_date.tm_year += 1;
0434         } else {
0435             max_date.tm_mon += 1;
0436         }
0437         max_mday = rtc_month_days(max_date.tm_mon, max_date.tm_year);
0438         if (max_date.tm_mday > max_mday)
0439             max_date.tm_mday = max_mday;
0440 
0441         t_max_date = rtc_tm_to_time64(&max_date);
0442         t_max_date -= 1;
0443         t_alrm = rtc_tm_to_time64(&t->time);
0444         if (t_alrm > t_max_date) {
0445             dev_err(dev,
0446                 "Alarms can be up to one month in the future\n");
0447             return -EINVAL;
0448         }
0449     } else {
0450         struct rtc_time max_date = now;
0451         time64_t t_max_date;
0452         time64_t t_alrm;
0453         int max_mday;
0454 
0455         max_date.tm_year += 1;
0456         max_mday = rtc_month_days(max_date.tm_mon, max_date.tm_year);
0457         if (max_date.tm_mday > max_mday)
0458             max_date.tm_mday = max_mday;
0459 
0460         t_max_date = rtc_tm_to_time64(&max_date);
0461         t_max_date -= 1;
0462         t_alrm = rtc_tm_to_time64(&t->time);
0463         if (t_alrm > t_max_date) {
0464             dev_err(dev,
0465                 "Alarms can be up to one year in the future\n");
0466             return -EINVAL;
0467         }
0468     }
0469 
0470     return 0;
0471 }
0472 
0473 struct cmos_set_alarm_callback_param {
0474     struct cmos_rtc *cmos;
0475     unsigned char mon, mday, hrs, min, sec;
0476     struct rtc_wkalrm *t;
0477 };
0478 
0479 /* Note: this function may be executed by mc146818_avoid_UIP() more then
0480  *   once
0481  */
0482 static void cmos_set_alarm_callback(unsigned char __always_unused seconds,
0483                     void *param_in)
0484 {
0485     struct cmos_set_alarm_callback_param *p =
0486         (struct cmos_set_alarm_callback_param *)param_in;
0487 
0488     /* next rtc irq must not be from previous alarm setting */
0489     cmos_irq_disable(p->cmos, RTC_AIE);
0490 
0491     /* update alarm */
0492     CMOS_WRITE(p->hrs, RTC_HOURS_ALARM);
0493     CMOS_WRITE(p->min, RTC_MINUTES_ALARM);
0494     CMOS_WRITE(p->sec, RTC_SECONDS_ALARM);
0495 
0496     /* the system may support an "enhanced" alarm */
0497     if (p->cmos->day_alrm) {
0498         CMOS_WRITE(p->mday, p->cmos->day_alrm);
0499         if (p->cmos->mon_alrm)
0500             CMOS_WRITE(p->mon, p->cmos->mon_alrm);
0501     }
0502 
0503     if (use_hpet_alarm()) {
0504         /*
0505          * FIXME the HPET alarm glue currently ignores day_alrm
0506          * and mon_alrm ...
0507          */
0508         hpet_set_alarm_time(p->t->time.tm_hour, p->t->time.tm_min,
0509                     p->t->time.tm_sec);
0510     }
0511 
0512     if (p->t->enabled)
0513         cmos_irq_enable(p->cmos, RTC_AIE);
0514 }
0515 
0516 static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
0517 {
0518     struct cmos_rtc *cmos = dev_get_drvdata(dev);
0519     struct cmos_set_alarm_callback_param p = {
0520         .cmos = cmos,
0521         .t = t
0522     };
0523     unsigned char rtc_control;
0524     int ret;
0525 
0526     /* This not only a rtc_op, but also called directly */
0527     if (!is_valid_irq(cmos->irq))
0528         return -EIO;
0529 
0530     ret = cmos_validate_alarm(dev, t);
0531     if (ret < 0)
0532         return ret;
0533 
0534     p.mon = t->time.tm_mon + 1;
0535     p.mday = t->time.tm_mday;
0536     p.hrs = t->time.tm_hour;
0537     p.min = t->time.tm_min;
0538     p.sec = t->time.tm_sec;
0539 
0540     spin_lock_irq(&rtc_lock);
0541     rtc_control = CMOS_READ(RTC_CONTROL);
0542     spin_unlock_irq(&rtc_lock);
0543 
0544     if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
0545         /* Writing 0xff means "don't care" or "match all".  */
0546         p.mon = (p.mon <= 12) ? bin2bcd(p.mon) : 0xff;
0547         p.mday = (p.mday >= 1 && p.mday <= 31) ? bin2bcd(p.mday) : 0xff;
0548         p.hrs = (p.hrs < 24) ? bin2bcd(p.hrs) : 0xff;
0549         p.min = (p.min < 60) ? bin2bcd(p.min) : 0xff;
0550         p.sec = (p.sec < 60) ? bin2bcd(p.sec) : 0xff;
0551     }
0552 
0553     /*
0554      * Some Intel chipsets disconnect the alarm registers when the clock
0555      * update is in progress - during this time writes fail silently.
0556      *
0557      * Use mc146818_avoid_UIP() to avoid this.
0558      */
0559     if (!mc146818_avoid_UIP(cmos_set_alarm_callback, &p))
0560         return -EIO;
0561 
0562     cmos->alarm_expires = rtc_tm_to_time64(&t->time);
0563 
0564     return 0;
0565 }
0566 
0567 static int cmos_alarm_irq_enable(struct device *dev, unsigned int enabled)
0568 {
0569     struct cmos_rtc *cmos = dev_get_drvdata(dev);
0570     unsigned long   flags;
0571 
0572     spin_lock_irqsave(&rtc_lock, flags);
0573 
0574     if (enabled)
0575         cmos_irq_enable(cmos, RTC_AIE);
0576     else
0577         cmos_irq_disable(cmos, RTC_AIE);
0578 
0579     spin_unlock_irqrestore(&rtc_lock, flags);
0580     return 0;
0581 }
0582 
0583 #if IS_ENABLED(CONFIG_RTC_INTF_PROC)
0584 
0585 static int cmos_procfs(struct device *dev, struct seq_file *seq)
0586 {
0587     struct cmos_rtc *cmos = dev_get_drvdata(dev);
0588     unsigned char   rtc_control, valid;
0589 
0590     spin_lock_irq(&rtc_lock);
0591     rtc_control = CMOS_READ(RTC_CONTROL);
0592     valid = CMOS_READ(RTC_VALID);
0593     spin_unlock_irq(&rtc_lock);
0594 
0595     /* NOTE:  at least ICH6 reports battery status using a different
0596      * (non-RTC) bit; and SQWE is ignored on many current systems.
0597      */
0598     seq_printf(seq,
0599            "periodic_IRQ\t: %s\n"
0600            "update_IRQ\t: %s\n"
0601            "HPET_emulated\t: %s\n"
0602            // "square_wave\t: %s\n"
0603            "BCD\t\t: %s\n"
0604            "DST_enable\t: %s\n"
0605            "periodic_freq\t: %d\n"
0606            "batt_status\t: %s\n",
0607            (rtc_control & RTC_PIE) ? "yes" : "no",
0608            (rtc_control & RTC_UIE) ? "yes" : "no",
0609            use_hpet_alarm() ? "yes" : "no",
0610            // (rtc_control & RTC_SQWE) ? "yes" : "no",
0611            (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
0612            (rtc_control & RTC_DST_EN) ? "yes" : "no",
0613            cmos->rtc->irq_freq,
0614            (valid & RTC_VRT) ? "okay" : "dead");
0615 
0616     return 0;
0617 }
0618 
0619 #else
0620 #define cmos_procfs NULL
0621 #endif
0622 
0623 static const struct rtc_class_ops cmos_rtc_ops = {
0624     .read_time      = cmos_read_time,
0625     .set_time       = cmos_set_time,
0626     .read_alarm     = cmos_read_alarm,
0627     .set_alarm      = cmos_set_alarm,
0628     .proc           = cmos_procfs,
0629     .alarm_irq_enable   = cmos_alarm_irq_enable,
0630 };
0631 
0632 /*----------------------------------------------------------------*/
0633 
0634 /*
0635  * All these chips have at least 64 bytes of address space, shared by
0636  * RTC registers and NVRAM.  Most of those bytes of NVRAM are used
0637  * by boot firmware.  Modern chips have 128 or 256 bytes.
0638  */
0639 
0640 #define NVRAM_OFFSET    (RTC_REG_D + 1)
0641 
0642 static int cmos_nvram_read(void *priv, unsigned int off, void *val,
0643                size_t count)
0644 {
0645     unsigned char *buf = val;
0646     int retval;
0647 
0648     off += NVRAM_OFFSET;
0649     spin_lock_irq(&rtc_lock);
0650     for (retval = 0; count; count--, off++, retval++) {
0651         if (off < 128)
0652             *buf++ = CMOS_READ(off);
0653         else if (can_bank2)
0654             *buf++ = cmos_read_bank2(off);
0655         else
0656             break;
0657     }
0658     spin_unlock_irq(&rtc_lock);
0659 
0660     return retval;
0661 }
0662 
0663 static int cmos_nvram_write(void *priv, unsigned int off, void *val,
0664                 size_t count)
0665 {
0666     struct cmos_rtc *cmos = priv;
0667     unsigned char   *buf = val;
0668     int     retval;
0669 
0670     /* NOTE:  on at least PCs and Ataris, the boot firmware uses a
0671      * checksum on part of the NVRAM data.  That's currently ignored
0672      * here.  If userspace is smart enough to know what fields of
0673      * NVRAM to update, updating checksums is also part of its job.
0674      */
0675     off += NVRAM_OFFSET;
0676     spin_lock_irq(&rtc_lock);
0677     for (retval = 0; count; count--, off++, retval++) {
0678         /* don't trash RTC registers */
0679         if (off == cmos->day_alrm
0680                 || off == cmos->mon_alrm
0681                 || off == cmos->century)
0682             buf++;
0683         else if (off < 128)
0684             CMOS_WRITE(*buf++, off);
0685         else if (can_bank2)
0686             cmos_write_bank2(*buf++, off);
0687         else
0688             break;
0689     }
0690     spin_unlock_irq(&rtc_lock);
0691 
0692     return retval;
0693 }
0694 
0695 /*----------------------------------------------------------------*/
0696 
0697 static struct cmos_rtc  cmos_rtc;
0698 
0699 static irqreturn_t cmos_interrupt(int irq, void *p)
0700 {
0701     u8      irqstat;
0702     u8      rtc_control;
0703 
0704     spin_lock(&rtc_lock);
0705 
0706     /* When the HPET interrupt handler calls us, the interrupt
0707      * status is passed as arg1 instead of the irq number.  But
0708      * always clear irq status, even when HPET is in the way.
0709      *
0710      * Note that HPET and RTC are almost certainly out of phase,
0711      * giving different IRQ status ...
0712      */
0713     irqstat = CMOS_READ(RTC_INTR_FLAGS);
0714     rtc_control = CMOS_READ(RTC_CONTROL);
0715     if (use_hpet_alarm())
0716         irqstat = (unsigned long)irq & 0xF0;
0717 
0718     /* If we were suspended, RTC_CONTROL may not be accurate since the
0719      * bios may have cleared it.
0720      */
0721     if (!cmos_rtc.suspend_ctrl)
0722         irqstat &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
0723     else
0724         irqstat &= (cmos_rtc.suspend_ctrl & RTC_IRQMASK) | RTC_IRQF;
0725 
0726     /* All Linux RTC alarms should be treated as if they were oneshot.
0727      * Similar code may be needed in system wakeup paths, in case the
0728      * alarm woke the system.
0729      */
0730     if (irqstat & RTC_AIE) {
0731         cmos_rtc.suspend_ctrl &= ~RTC_AIE;
0732         rtc_control &= ~RTC_AIE;
0733         CMOS_WRITE(rtc_control, RTC_CONTROL);
0734         if (use_hpet_alarm())
0735             hpet_mask_rtc_irq_bit(RTC_AIE);
0736         CMOS_READ(RTC_INTR_FLAGS);
0737     }
0738     spin_unlock(&rtc_lock);
0739 
0740     if (is_intr(irqstat)) {
0741         rtc_update_irq(p, 1, irqstat);
0742         return IRQ_HANDLED;
0743     } else
0744         return IRQ_NONE;
0745 }
0746 
0747 #ifdef  CONFIG_PNP
0748 #define INITSECTION
0749 
0750 #else
0751 #define INITSECTION __init
0752 #endif
0753 
0754 static int INITSECTION
0755 cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
0756 {
0757     struct cmos_rtc_board_info  *info = dev_get_platdata(dev);
0758     int             retval = 0;
0759     unsigned char           rtc_control;
0760     unsigned            address_space;
0761     u32             flags = 0;
0762     struct nvmem_config nvmem_cfg = {
0763         .name = "cmos_nvram",
0764         .word_size = 1,
0765         .stride = 1,
0766         .reg_read = cmos_nvram_read,
0767         .reg_write = cmos_nvram_write,
0768         .priv = &cmos_rtc,
0769     };
0770 
0771     /* there can be only one ... */
0772     if (cmos_rtc.dev)
0773         return -EBUSY;
0774 
0775     if (!ports)
0776         return -ENODEV;
0777 
0778     /* Claim I/O ports ASAP, minimizing conflict with legacy driver.
0779      *
0780      * REVISIT non-x86 systems may instead use memory space resources
0781      * (needing ioremap etc), not i/o space resources like this ...
0782      */
0783     if (RTC_IOMAPPED)
0784         ports = request_region(ports->start, resource_size(ports),
0785                        driver_name);
0786     else
0787         ports = request_mem_region(ports->start, resource_size(ports),
0788                        driver_name);
0789     if (!ports) {
0790         dev_dbg(dev, "i/o registers already in use\n");
0791         return -EBUSY;
0792     }
0793 
0794     cmos_rtc.irq = rtc_irq;
0795     cmos_rtc.iomem = ports;
0796 
0797     /* Heuristic to deduce NVRAM size ... do what the legacy NVRAM
0798      * driver did, but don't reject unknown configs.   Old hardware
0799      * won't address 128 bytes.  Newer chips have multiple banks,
0800      * though they may not be listed in one I/O resource.
0801      */
0802 #if defined(CONFIG_ATARI)
0803     address_space = 64;
0804 #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) \
0805             || defined(__sparc__) || defined(__mips__) \
0806             || defined(__powerpc__)
0807     address_space = 128;
0808 #else
0809 #warning Assuming 128 bytes of RTC+NVRAM address space, not 64 bytes.
0810     address_space = 128;
0811 #endif
0812     if (can_bank2 && ports->end > (ports->start + 1))
0813         address_space = 256;
0814 
0815     /* For ACPI systems extension info comes from the FADT.  On others,
0816      * board specific setup provides it as appropriate.  Systems where
0817      * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and
0818      * some almost-clones) can provide hooks to make that behave.
0819      *
0820      * Note that ACPI doesn't preclude putting these registers into
0821      * "extended" areas of the chip, including some that we won't yet
0822      * expect CMOS_READ and friends to handle.
0823      */
0824     if (info) {
0825         if (info->flags)
0826             flags = info->flags;
0827         if (info->address_space)
0828             address_space = info->address_space;
0829 
0830         if (info->rtc_day_alarm && info->rtc_day_alarm < 128)
0831             cmos_rtc.day_alrm = info->rtc_day_alarm;
0832         if (info->rtc_mon_alarm && info->rtc_mon_alarm < 128)
0833             cmos_rtc.mon_alrm = info->rtc_mon_alarm;
0834         if (info->rtc_century && info->rtc_century < 128)
0835             cmos_rtc.century = info->rtc_century;
0836 
0837         if (info->wake_on && info->wake_off) {
0838             cmos_rtc.wake_on = info->wake_on;
0839             cmos_rtc.wake_off = info->wake_off;
0840         }
0841     }
0842 
0843     cmos_rtc.dev = dev;
0844     dev_set_drvdata(dev, &cmos_rtc);
0845 
0846     cmos_rtc.rtc = devm_rtc_allocate_device(dev);
0847     if (IS_ERR(cmos_rtc.rtc)) {
0848         retval = PTR_ERR(cmos_rtc.rtc);
0849         goto cleanup0;
0850     }
0851 
0852     rename_region(ports, dev_name(&cmos_rtc.rtc->dev));
0853 
0854     if (!mc146818_does_rtc_work()) {
0855         dev_warn(dev, "broken or not accessible\n");
0856         retval = -ENXIO;
0857         goto cleanup1;
0858     }
0859 
0860     spin_lock_irq(&rtc_lock);
0861 
0862     if (!(flags & CMOS_RTC_FLAGS_NOFREQ)) {
0863         /* force periodic irq to CMOS reset default of 1024Hz;
0864          *
0865          * REVISIT it's been reported that at least one x86_64 ALI
0866          * mobo doesn't use 32KHz here ... for portability we might
0867          * need to do something about other clock frequencies.
0868          */
0869         cmos_rtc.rtc->irq_freq = 1024;
0870         if (use_hpet_alarm())
0871             hpet_set_periodic_freq(cmos_rtc.rtc->irq_freq);
0872         CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT);
0873     }
0874 
0875     /* disable irqs */
0876     if (is_valid_irq(rtc_irq))
0877         cmos_irq_disable(&cmos_rtc, RTC_PIE | RTC_AIE | RTC_UIE);
0878 
0879     rtc_control = CMOS_READ(RTC_CONTROL);
0880 
0881     spin_unlock_irq(&rtc_lock);
0882 
0883     if (is_valid_irq(rtc_irq) && !(rtc_control & RTC_24H)) {
0884         dev_warn(dev, "only 24-hr supported\n");
0885         retval = -ENXIO;
0886         goto cleanup1;
0887     }
0888 
0889     if (use_hpet_alarm())
0890         hpet_rtc_timer_init();
0891 
0892     if (is_valid_irq(rtc_irq)) {
0893         irq_handler_t rtc_cmos_int_handler;
0894 
0895         if (use_hpet_alarm()) {
0896             rtc_cmos_int_handler = hpet_rtc_interrupt;
0897             retval = hpet_register_irq_handler(cmos_interrupt);
0898             if (retval) {
0899                 hpet_mask_rtc_irq_bit(RTC_IRQMASK);
0900                 dev_warn(dev, "hpet_register_irq_handler "
0901                         " failed in rtc_init().");
0902                 goto cleanup1;
0903             }
0904         } else
0905             rtc_cmos_int_handler = cmos_interrupt;
0906 
0907         retval = request_irq(rtc_irq, rtc_cmos_int_handler,
0908                 0, dev_name(&cmos_rtc.rtc->dev),
0909                 cmos_rtc.rtc);
0910         if (retval < 0) {
0911             dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
0912             goto cleanup1;
0913         }
0914     } else {
0915         clear_bit(RTC_FEATURE_ALARM, cmos_rtc.rtc->features);
0916     }
0917 
0918     cmos_rtc.rtc->ops = &cmos_rtc_ops;
0919 
0920     retval = devm_rtc_register_device(cmos_rtc.rtc);
0921     if (retval)
0922         goto cleanup2;
0923 
0924     /* Set the sync offset for the periodic 11min update correct */
0925     cmos_rtc.rtc->set_offset_nsec = NSEC_PER_SEC / 2;
0926 
0927     /* export at least the first block of NVRAM */
0928     nvmem_cfg.size = address_space - NVRAM_OFFSET;
0929     devm_rtc_nvmem_register(cmos_rtc.rtc, &nvmem_cfg);
0930 
0931     dev_info(dev, "%s%s, %d bytes nvram%s\n",
0932          !is_valid_irq(rtc_irq) ? "no alarms" :
0933          cmos_rtc.mon_alrm ? "alarms up to one year" :
0934          cmos_rtc.day_alrm ? "alarms up to one month" :
0935          "alarms up to one day",
0936          cmos_rtc.century ? ", y3k" : "",
0937          nvmem_cfg.size,
0938          use_hpet_alarm() ? ", hpet irqs" : "");
0939 
0940     return 0;
0941 
0942 cleanup2:
0943     if (is_valid_irq(rtc_irq))
0944         free_irq(rtc_irq, cmos_rtc.rtc);
0945 cleanup1:
0946     cmos_rtc.dev = NULL;
0947 cleanup0:
0948     if (RTC_IOMAPPED)
0949         release_region(ports->start, resource_size(ports));
0950     else
0951         release_mem_region(ports->start, resource_size(ports));
0952     return retval;
0953 }
0954 
0955 static void cmos_do_shutdown(int rtc_irq)
0956 {
0957     spin_lock_irq(&rtc_lock);
0958     if (is_valid_irq(rtc_irq))
0959         cmos_irq_disable(&cmos_rtc, RTC_IRQMASK);
0960     spin_unlock_irq(&rtc_lock);
0961 }
0962 
0963 static void cmos_do_remove(struct device *dev)
0964 {
0965     struct cmos_rtc *cmos = dev_get_drvdata(dev);
0966     struct resource *ports;
0967 
0968     cmos_do_shutdown(cmos->irq);
0969 
0970     if (is_valid_irq(cmos->irq)) {
0971         free_irq(cmos->irq, cmos->rtc);
0972         if (use_hpet_alarm())
0973             hpet_unregister_irq_handler(cmos_interrupt);
0974     }
0975 
0976     cmos->rtc = NULL;
0977 
0978     ports = cmos->iomem;
0979     if (RTC_IOMAPPED)
0980         release_region(ports->start, resource_size(ports));
0981     else
0982         release_mem_region(ports->start, resource_size(ports));
0983     cmos->iomem = NULL;
0984 
0985     cmos->dev = NULL;
0986 }
0987 
0988 static int cmos_aie_poweroff(struct device *dev)
0989 {
0990     struct cmos_rtc *cmos = dev_get_drvdata(dev);
0991     struct rtc_time now;
0992     time64_t t_now;
0993     int retval = 0;
0994     unsigned char rtc_control;
0995 
0996     if (!cmos->alarm_expires)
0997         return -EINVAL;
0998 
0999     spin_lock_irq(&rtc_lock);
1000     rtc_control = CMOS_READ(RTC_CONTROL);
1001     spin_unlock_irq(&rtc_lock);
1002 
1003     /* We only care about the situation where AIE is disabled. */
1004     if (rtc_control & RTC_AIE)
1005         return -EBUSY;
1006 
1007     cmos_read_time(dev, &now);
1008     t_now = rtc_tm_to_time64(&now);
1009 
1010     /*
1011      * When enabling "RTC wake-up" in BIOS setup, the machine reboots
1012      * automatically right after shutdown on some buggy boxes.
1013      * This automatic rebooting issue won't happen when the alarm
1014      * time is larger than now+1 seconds.
1015      *
1016      * If the alarm time is equal to now+1 seconds, the issue can be
1017      * prevented by cancelling the alarm.
1018      */
1019     if (cmos->alarm_expires == t_now + 1) {
1020         struct rtc_wkalrm alarm;
1021 
1022         /* Cancel the AIE timer by configuring the past time. */
1023         rtc_time64_to_tm(t_now - 1, &alarm.time);
1024         alarm.enabled = 0;
1025         retval = cmos_set_alarm(dev, &alarm);
1026     } else if (cmos->alarm_expires > t_now + 1) {
1027         retval = -EBUSY;
1028     }
1029 
1030     return retval;
1031 }
1032 
1033 static int cmos_suspend(struct device *dev)
1034 {
1035     struct cmos_rtc *cmos = dev_get_drvdata(dev);
1036     unsigned char   tmp;
1037 
1038     /* only the alarm might be a wakeup event source */
1039     spin_lock_irq(&rtc_lock);
1040     cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL);
1041     if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
1042         unsigned char   mask;
1043 
1044         if (device_may_wakeup(dev))
1045             mask = RTC_IRQMASK & ~RTC_AIE;
1046         else
1047             mask = RTC_IRQMASK;
1048         tmp &= ~mask;
1049         CMOS_WRITE(tmp, RTC_CONTROL);
1050         if (use_hpet_alarm())
1051             hpet_mask_rtc_irq_bit(mask);
1052         cmos_checkintr(cmos, tmp);
1053     }
1054     spin_unlock_irq(&rtc_lock);
1055 
1056     if ((tmp & RTC_AIE) && !cmos_use_acpi_alarm()) {
1057         cmos->enabled_wake = 1;
1058         if (cmos->wake_on)
1059             cmos->wake_on(dev);
1060         else
1061             enable_irq_wake(cmos->irq);
1062     }
1063 
1064     memset(&cmos->saved_wkalrm, 0, sizeof(struct rtc_wkalrm));
1065     cmos_read_alarm(dev, &cmos->saved_wkalrm);
1066 
1067     dev_dbg(dev, "suspend%s, ctrl %02x\n",
1068             (tmp & RTC_AIE) ? ", alarm may wake" : "",
1069             tmp);
1070 
1071     return 0;
1072 }
1073 
1074 /* We want RTC alarms to wake us from e.g. ACPI G2/S5 "soft off", even
1075  * after a detour through G3 "mechanical off", although the ACPI spec
1076  * says wakeup should only work from G1/S4 "hibernate".  To most users,
1077  * distinctions between S4 and S5 are pointless.  So when the hardware
1078  * allows, don't draw that distinction.
1079  */
1080 static inline int cmos_poweroff(struct device *dev)
1081 {
1082     if (!IS_ENABLED(CONFIG_PM))
1083         return -ENOSYS;
1084 
1085     return cmos_suspend(dev);
1086 }
1087 
1088 static void cmos_check_wkalrm(struct device *dev)
1089 {
1090     struct cmos_rtc *cmos = dev_get_drvdata(dev);
1091     struct rtc_wkalrm current_alarm;
1092     time64_t t_now;
1093     time64_t t_current_expires;
1094     time64_t t_saved_expires;
1095     struct rtc_time now;
1096 
1097     /* Check if we have RTC Alarm armed */
1098     if (!(cmos->suspend_ctrl & RTC_AIE))
1099         return;
1100 
1101     cmos_read_time(dev, &now);
1102     t_now = rtc_tm_to_time64(&now);
1103 
1104     /*
1105      * ACPI RTC wake event is cleared after resume from STR,
1106      * ACK the rtc irq here
1107      */
1108     if (t_now >= cmos->alarm_expires && cmos_use_acpi_alarm()) {
1109         local_irq_disable();
1110         cmos_interrupt(0, (void *)cmos->rtc);
1111         local_irq_enable();
1112         return;
1113     }
1114 
1115     memset(&current_alarm, 0, sizeof(struct rtc_wkalrm));
1116     cmos_read_alarm(dev, &current_alarm);
1117     t_current_expires = rtc_tm_to_time64(&current_alarm.time);
1118     t_saved_expires = rtc_tm_to_time64(&cmos->saved_wkalrm.time);
1119     if (t_current_expires != t_saved_expires ||
1120         cmos->saved_wkalrm.enabled != current_alarm.enabled) {
1121         cmos_set_alarm(dev, &cmos->saved_wkalrm);
1122     }
1123 }
1124 
1125 static void cmos_check_acpi_rtc_status(struct device *dev,
1126                        unsigned char *rtc_control);
1127 
1128 static int __maybe_unused cmos_resume(struct device *dev)
1129 {
1130     struct cmos_rtc *cmos = dev_get_drvdata(dev);
1131     unsigned char tmp;
1132 
1133     if (cmos->enabled_wake && !cmos_use_acpi_alarm()) {
1134         if (cmos->wake_off)
1135             cmos->wake_off(dev);
1136         else
1137             disable_irq_wake(cmos->irq);
1138         cmos->enabled_wake = 0;
1139     }
1140 
1141     /* The BIOS might have changed the alarm, restore it */
1142     cmos_check_wkalrm(dev);
1143 
1144     spin_lock_irq(&rtc_lock);
1145     tmp = cmos->suspend_ctrl;
1146     cmos->suspend_ctrl = 0;
1147     /* re-enable any irqs previously active */
1148     if (tmp & RTC_IRQMASK) {
1149         unsigned char   mask;
1150 
1151         if (device_may_wakeup(dev) && use_hpet_alarm())
1152             hpet_rtc_timer_init();
1153 
1154         do {
1155             CMOS_WRITE(tmp, RTC_CONTROL);
1156             if (use_hpet_alarm())
1157                 hpet_set_rtc_irq_bit(tmp & RTC_IRQMASK);
1158 
1159             mask = CMOS_READ(RTC_INTR_FLAGS);
1160             mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
1161             if (!use_hpet_alarm() || !is_intr(mask))
1162                 break;
1163 
1164             /* force one-shot behavior if HPET blocked
1165              * the wake alarm's irq
1166              */
1167             rtc_update_irq(cmos->rtc, 1, mask);
1168             tmp &= ~RTC_AIE;
1169             hpet_mask_rtc_irq_bit(RTC_AIE);
1170         } while (mask & RTC_AIE);
1171 
1172         if (tmp & RTC_AIE)
1173             cmos_check_acpi_rtc_status(dev, &tmp);
1174     }
1175     spin_unlock_irq(&rtc_lock);
1176 
1177     dev_dbg(dev, "resume, ctrl %02x\n", tmp);
1178 
1179     return 0;
1180 }
1181 
1182 static SIMPLE_DEV_PM_OPS(cmos_pm_ops, cmos_suspend, cmos_resume);
1183 
1184 /*----------------------------------------------------------------*/
1185 
1186 /* On non-x86 systems, a "CMOS" RTC lives most naturally on platform_bus.
1187  * ACPI systems always list these as PNPACPI devices, and pre-ACPI PCs
1188  * probably list them in similar PNPBIOS tables; so PNP is more common.
1189  *
1190  * We don't use legacy "poke at the hardware" probing.  Ancient PCs that
1191  * predate even PNPBIOS should set up platform_bus devices.
1192  */
1193 
1194 #ifdef  CONFIG_ACPI
1195 
1196 #include <linux/acpi.h>
1197 
1198 static u32 rtc_handler(void *context)
1199 {
1200     struct device *dev = context;
1201     struct cmos_rtc *cmos = dev_get_drvdata(dev);
1202     unsigned char rtc_control = 0;
1203     unsigned char rtc_intr;
1204     unsigned long flags;
1205 
1206 
1207     /*
1208      * Always update rtc irq when ACPI is used as RTC Alarm.
1209      * Or else, ACPI SCI is enabled during suspend/resume only,
1210      * update rtc irq in that case.
1211      */
1212     if (cmos_use_acpi_alarm())
1213         cmos_interrupt(0, (void *)cmos->rtc);
1214     else {
1215         /* Fix me: can we use cmos_interrupt() here as well? */
1216         spin_lock_irqsave(&rtc_lock, flags);
1217         if (cmos_rtc.suspend_ctrl)
1218             rtc_control = CMOS_READ(RTC_CONTROL);
1219         if (rtc_control & RTC_AIE) {
1220             cmos_rtc.suspend_ctrl &= ~RTC_AIE;
1221             CMOS_WRITE(rtc_control, RTC_CONTROL);
1222             rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
1223             rtc_update_irq(cmos->rtc, 1, rtc_intr);
1224         }
1225         spin_unlock_irqrestore(&rtc_lock, flags);
1226     }
1227 
1228     pm_wakeup_hard_event(dev);
1229     acpi_clear_event(ACPI_EVENT_RTC);
1230     acpi_disable_event(ACPI_EVENT_RTC, 0);
1231     return ACPI_INTERRUPT_HANDLED;
1232 }
1233 
1234 static inline void rtc_wake_setup(struct device *dev)
1235 {
1236     acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, dev);
1237     /*
1238      * After the RTC handler is installed, the Fixed_RTC event should
1239      * be disabled. Only when the RTC alarm is set will it be enabled.
1240      */
1241     acpi_clear_event(ACPI_EVENT_RTC);
1242     acpi_disable_event(ACPI_EVENT_RTC, 0);
1243 }
1244 
1245 static void rtc_wake_on(struct device *dev)
1246 {
1247     acpi_clear_event(ACPI_EVENT_RTC);
1248     acpi_enable_event(ACPI_EVENT_RTC, 0);
1249 }
1250 
1251 static void rtc_wake_off(struct device *dev)
1252 {
1253     acpi_disable_event(ACPI_EVENT_RTC, 0);
1254 }
1255 
1256 #ifdef CONFIG_X86
1257 /* Enable use_acpi_alarm mode for Intel platforms no earlier than 2015 */
1258 static void use_acpi_alarm_quirks(void)
1259 {
1260     if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
1261         return;
1262 
1263     if (!is_hpet_enabled())
1264         return;
1265 
1266     if (dmi_get_bios_year() < 2015)
1267         return;
1268 
1269     use_acpi_alarm = true;
1270 }
1271 #else
1272 static inline void use_acpi_alarm_quirks(void) { }
1273 #endif
1274 
1275 /* Every ACPI platform has a mc146818 compatible "cmos rtc".  Here we find
1276  * its device node and pass extra config data.  This helps its driver use
1277  * capabilities that the now-obsolete mc146818 didn't have, and informs it
1278  * that this board's RTC is wakeup-capable (per ACPI spec).
1279  */
1280 static struct cmos_rtc_board_info acpi_rtc_info;
1281 
1282 static void cmos_wake_setup(struct device *dev)
1283 {
1284     if (acpi_disabled)
1285         return;
1286 
1287     use_acpi_alarm_quirks();
1288 
1289     rtc_wake_setup(dev);
1290     acpi_rtc_info.wake_on = rtc_wake_on;
1291     acpi_rtc_info.wake_off = rtc_wake_off;
1292 
1293     /* workaround bug in some ACPI tables */
1294     if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) {
1295         dev_dbg(dev, "bogus FADT month_alarm (%d)\n",
1296             acpi_gbl_FADT.month_alarm);
1297         acpi_gbl_FADT.month_alarm = 0;
1298     }
1299 
1300     acpi_rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
1301     acpi_rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
1302     acpi_rtc_info.rtc_century = acpi_gbl_FADT.century;
1303 
1304     /* NOTE:  S4_RTC_WAKE is NOT currently useful to Linux */
1305     if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE)
1306         dev_info(dev, "RTC can wake from S4\n");
1307 
1308     dev->platform_data = &acpi_rtc_info;
1309 
1310     /* RTC always wakes from S1/S2/S3, and often S4/STD */
1311     device_init_wakeup(dev, 1);
1312 }
1313 
1314 static void cmos_check_acpi_rtc_status(struct device *dev,
1315                        unsigned char *rtc_control)
1316 {
1317     struct cmos_rtc *cmos = dev_get_drvdata(dev);
1318     acpi_event_status rtc_status;
1319     acpi_status status;
1320 
1321     if (acpi_gbl_FADT.flags & ACPI_FADT_FIXED_RTC)
1322         return;
1323 
1324     status = acpi_get_event_status(ACPI_EVENT_RTC, &rtc_status);
1325     if (ACPI_FAILURE(status)) {
1326         dev_err(dev, "Could not get RTC status\n");
1327     } else if (rtc_status & ACPI_EVENT_FLAG_SET) {
1328         unsigned char mask;
1329         *rtc_control &= ~RTC_AIE;
1330         CMOS_WRITE(*rtc_control, RTC_CONTROL);
1331         mask = CMOS_READ(RTC_INTR_FLAGS);
1332         rtc_update_irq(cmos->rtc, 1, mask);
1333     }
1334 }
1335 
1336 #else
1337 
1338 static void cmos_wake_setup(struct device *dev)
1339 {
1340 }
1341 
1342 static void cmos_check_acpi_rtc_status(struct device *dev,
1343                        unsigned char *rtc_control)
1344 {
1345 }
1346 
1347 #endif
1348 
1349 #ifdef  CONFIG_PNP
1350 
1351 #include <linux/pnp.h>
1352 
1353 static int cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
1354 {
1355     cmos_wake_setup(&pnp->dev);
1356 
1357     if (pnp_port_start(pnp, 0) == 0x70 && !pnp_irq_valid(pnp, 0)) {
1358         unsigned int irq = 0;
1359 #ifdef CONFIG_X86
1360         /* Some machines contain a PNP entry for the RTC, but
1361          * don't define the IRQ. It should always be safe to
1362          * hardcode it on systems with a legacy PIC.
1363          */
1364         if (nr_legacy_irqs())
1365             irq = RTC_IRQ;
1366 #endif
1367         return cmos_do_probe(&pnp->dev,
1368                 pnp_get_resource(pnp, IORESOURCE_IO, 0), irq);
1369     } else {
1370         return cmos_do_probe(&pnp->dev,
1371                 pnp_get_resource(pnp, IORESOURCE_IO, 0),
1372                 pnp_irq(pnp, 0));
1373     }
1374 }
1375 
1376 static void cmos_pnp_remove(struct pnp_dev *pnp)
1377 {
1378     cmos_do_remove(&pnp->dev);
1379 }
1380 
1381 static void cmos_pnp_shutdown(struct pnp_dev *pnp)
1382 {
1383     struct device *dev = &pnp->dev;
1384     struct cmos_rtc *cmos = dev_get_drvdata(dev);
1385 
1386     if (system_state == SYSTEM_POWER_OFF) {
1387         int retval = cmos_poweroff(dev);
1388 
1389         if (cmos_aie_poweroff(dev) < 0 && !retval)
1390             return;
1391     }
1392 
1393     cmos_do_shutdown(cmos->irq);
1394 }
1395 
1396 static const struct pnp_device_id rtc_ids[] = {
1397     { .id = "PNP0b00", },
1398     { .id = "PNP0b01", },
1399     { .id = "PNP0b02", },
1400     { },
1401 };
1402 MODULE_DEVICE_TABLE(pnp, rtc_ids);
1403 
1404 static struct pnp_driver cmos_pnp_driver = {
1405     .name       = driver_name,
1406     .id_table   = rtc_ids,
1407     .probe      = cmos_pnp_probe,
1408     .remove     = cmos_pnp_remove,
1409     .shutdown   = cmos_pnp_shutdown,
1410 
1411     /* flag ensures resume() gets called, and stops syslog spam */
1412     .flags      = PNP_DRIVER_RES_DO_NOT_CHANGE,
1413     .driver     = {
1414             .pm = &cmos_pm_ops,
1415     },
1416 };
1417 
1418 #endif  /* CONFIG_PNP */
1419 
1420 #ifdef CONFIG_OF
1421 static const struct of_device_id of_cmos_match[] = {
1422     {
1423         .compatible = "motorola,mc146818",
1424     },
1425     { },
1426 };
1427 MODULE_DEVICE_TABLE(of, of_cmos_match);
1428 
1429 static __init void cmos_of_init(struct platform_device *pdev)
1430 {
1431     struct device_node *node = pdev->dev.of_node;
1432     const __be32 *val;
1433 
1434     if (!node)
1435         return;
1436 
1437     val = of_get_property(node, "ctrl-reg", NULL);
1438     if (val)
1439         CMOS_WRITE(be32_to_cpup(val), RTC_CONTROL);
1440 
1441     val = of_get_property(node, "freq-reg", NULL);
1442     if (val)
1443         CMOS_WRITE(be32_to_cpup(val), RTC_FREQ_SELECT);
1444 }
1445 #else
1446 static inline void cmos_of_init(struct platform_device *pdev) {}
1447 #endif
1448 /*----------------------------------------------------------------*/
1449 
1450 /* Platform setup should have set up an RTC device, when PNP is
1451  * unavailable ... this could happen even on (older) PCs.
1452  */
1453 
1454 static int __init cmos_platform_probe(struct platform_device *pdev)
1455 {
1456     struct resource *resource;
1457     int irq;
1458 
1459     cmos_of_init(pdev);
1460     cmos_wake_setup(&pdev->dev);
1461 
1462     if (RTC_IOMAPPED)
1463         resource = platform_get_resource(pdev, IORESOURCE_IO, 0);
1464     else
1465         resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1466     irq = platform_get_irq(pdev, 0);
1467     if (irq < 0)
1468         irq = -1;
1469 
1470     return cmos_do_probe(&pdev->dev, resource, irq);
1471 }
1472 
1473 static int cmos_platform_remove(struct platform_device *pdev)
1474 {
1475     cmos_do_remove(&pdev->dev);
1476     return 0;
1477 }
1478 
1479 static void cmos_platform_shutdown(struct platform_device *pdev)
1480 {
1481     struct device *dev = &pdev->dev;
1482     struct cmos_rtc *cmos = dev_get_drvdata(dev);
1483 
1484     if (system_state == SYSTEM_POWER_OFF) {
1485         int retval = cmos_poweroff(dev);
1486 
1487         if (cmos_aie_poweroff(dev) < 0 && !retval)
1488             return;
1489     }
1490 
1491     cmos_do_shutdown(cmos->irq);
1492 }
1493 
1494 /* work with hotplug and coldplug */
1495 MODULE_ALIAS("platform:rtc_cmos");
1496 
1497 static struct platform_driver cmos_platform_driver = {
1498     .remove     = cmos_platform_remove,
1499     .shutdown   = cmos_platform_shutdown,
1500     .driver = {
1501         .name       = driver_name,
1502         .pm     = &cmos_pm_ops,
1503         .of_match_table = of_match_ptr(of_cmos_match),
1504     }
1505 };
1506 
1507 #ifdef CONFIG_PNP
1508 static bool pnp_driver_registered;
1509 #endif
1510 static bool platform_driver_registered;
1511 
1512 static int __init cmos_init(void)
1513 {
1514     int retval = 0;
1515 
1516 #ifdef  CONFIG_PNP
1517     retval = pnp_register_driver(&cmos_pnp_driver);
1518     if (retval == 0)
1519         pnp_driver_registered = true;
1520 #endif
1521 
1522     if (!cmos_rtc.dev) {
1523         retval = platform_driver_probe(&cmos_platform_driver,
1524                            cmos_platform_probe);
1525         if (retval == 0)
1526             platform_driver_registered = true;
1527     }
1528 
1529     if (retval == 0)
1530         return 0;
1531 
1532 #ifdef  CONFIG_PNP
1533     if (pnp_driver_registered)
1534         pnp_unregister_driver(&cmos_pnp_driver);
1535 #endif
1536     return retval;
1537 }
1538 module_init(cmos_init);
1539 
1540 static void __exit cmos_exit(void)
1541 {
1542 #ifdef  CONFIG_PNP
1543     if (pnp_driver_registered)
1544         pnp_unregister_driver(&cmos_pnp_driver);
1545 #endif
1546     if (platform_driver_registered)
1547         platform_driver_unregister(&cmos_platform_driver);
1548 }
1549 module_exit(cmos_exit);
1550 
1551 
1552 MODULE_AUTHOR("David Brownell");
1553 MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
1554 MODULE_LICENSE("GPL");