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