blob: 5ac9a5da85228d8005d817497a995676e9632119 [file] [log] [blame]
David Brownell7be2c7c2007-02-10 01:46:02 -08001/*
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 */
Joe Perchesa737e832015-04-16 12:46:14 -070031
32#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
David Brownell7be2c7c2007-02-10 01:46:02 -080034#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>
Jonathan Cameron5d2a5032009-01-06 14:42:12 -080040#include <linux/log2.h>
Paul Fox2fb08e62011-01-12 17:00:07 -080041#include <linux/pm.h>
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +010042#include <linux/of.h>
43#include <linux/of_platform.h>
Borislav Petkovd5a1c7e2013-07-20 19:00:23 +020044#include <linux/dmi.h>
David Brownell7be2c7c2007-02-10 01:46:02 -080045
46/* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */
47#include <asm-generic/rtc.h>
48
David Brownell7be2c7c2007-02-10 01:46:02 -080049struct cmos_rtc {
50 struct rtc_device *rtc;
51 struct device *dev;
52 int irq;
53 struct resource *iomem;
Adrian Huang88b8d332015-07-06 12:19:12 +080054 time64_t alarm_expires;
David Brownell7be2c7c2007-02-10 01:46:02 -080055
David Brownell87ac84f2007-05-08 00:34:00 -070056 void (*wake_on)(struct device *);
57 void (*wake_off)(struct device *);
58
59 u8 enabled_wake;
David Brownell7be2c7c2007-02-10 01:46:02 -080060 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 */
Anton Vorontsov2fac6672009-01-06 14:42:11 -080069#define is_valid_irq(n) ((n) > 0)
David Brownell7be2c7c2007-02-10 01:46:02 -080070
71static const char driver_name[] = "rtc_cmos";
72
David Brownellbcd9b892007-04-01 23:49:47 -070073/* 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
79static 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
David Brownell7be2c7c2007-02-10 01:46:02 -080086/*----------------------------------------------------------------*/
87
David Brownell35d3fdd2008-07-23 21:30:43 -070088/* 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
102static inline int is_hpet_enabled(void)
103{
104 return 0;
105}
106
107static inline int hpet_mask_rtc_irq_bit(unsigned long mask)
108{
109 return 0;
110}
111
112static inline int hpet_set_rtc_irq_bit(unsigned long mask)
113{
114 return 0;
115}
116
117static inline int
118hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
119{
120 return 0;
121}
122
123static inline int hpet_set_periodic_freq(unsigned long freq)
124{
125 return 0;
126}
127
128static inline int hpet_rtc_dropped_irq(void)
129{
130 return 0;
131}
132
133static inline int hpet_rtc_timer_init(void)
134{
135 return 0;
136}
137
138extern irq_handler_t hpet_rtc_interrupt;
139
140static inline int hpet_register_irq_handler(irq_handler_t handler)
141{
142 return 0;
143}
144
145static inline int hpet_unregister_irq_handler(irq_handler_t handler)
146{
147 return 0;
148}
149
150#endif
151
152/*----------------------------------------------------------------*/
153
David Brownellc8fc40c2008-10-18 20:27:47 -0700154#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
162static inline unsigned char cmos_read_bank2(unsigned char addr)
163{
164 outb(addr, RTC_PORT(2));
165 return inb(RTC_PORT(3));
166}
167
168static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
169{
170 outb(addr, RTC_PORT(2));
Ondrej Zaryb43c1ea2012-01-10 15:10:26 -0800171 outb(val, RTC_PORT(3));
David Brownellc8fc40c2008-10-18 20:27:47 -0700172}
173
174#else
175
176#define can_bank2 false
177
178static inline unsigned char cmos_read_bank2(unsigned char addr)
179{
180 return 0;
181}
182
183static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
184{
185}
186
187#endif
188
189/*----------------------------------------------------------------*/
190
David Brownell7be2c7c2007-02-10 01:46:02 -0800191static 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
201static 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
212static 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) {
Mark Lord615bb292007-11-03 22:04:03 -0400233 /* ignore upper bits on readback per ACPI spec */
234 t->time.tm_mday = CMOS_READ(cmos->day_alrm) & 0x3f;
David Brownell7be2c7c2007-02-10 01:46:02 -0800235 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
Arnaud Patard3804a892010-04-29 11:58:44 +0200248 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);
David Brownell7be2c7c2007-02-10 01:46:02 -0800251 else
Arnaud Patard3804a892010-04-29 11:58:44 +0200252 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);
David Brownell7be2c7c2007-02-10 01:46:02 -0800265 else
Arnaud Patard3804a892010-04-29 11:58:44 +0200266 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 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800274 }
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
David Brownell7e2a31d2008-07-23 21:30:47 -0700284static 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
301static 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
318static 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
David Brownell7be2c7c2007-02-10 01:46:02 -0800330static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
331{
332 struct cmos_rtc *cmos = dev_get_drvdata(dev);
Sachin Kamat5e8599d2013-07-03 15:05:45 -0700333 unsigned char mon, mday, hrs, min, sec, rtc_control;
David Brownell7be2c7c2007-02-10 01:46:02 -0800334
335 if (!is_valid_irq(cmos->irq))
336 return -EIO;
337
Zhao Yakui2b653e02008-04-15 14:34:29 -0700338 mon = t->time.tm_mon + 1;
David Brownell7be2c7c2007-02-10 01:46:02 -0800339 mday = t->time.tm_mday;
David Brownell7be2c7c2007-02-10 01:46:02 -0800340 hrs = t->time.tm_hour;
David Brownell7be2c7c2007-02-10 01:46:02 -0800341 min = t->time.tm_min;
David Brownell7be2c7c2007-02-10 01:46:02 -0800342 sec = t->time.tm_sec;
Arnaud Patard3804a892010-04-29 11:58:44 +0200343
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 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800353
354 spin_lock_irq(&rtc_lock);
355
356 /* next rtc irq must not be from previous alarm setting */
David Brownell7e2a31d2008-07-23 21:30:47 -0700357 cmos_irq_disable(cmos, RTC_AIE);
David Brownell7be2c7c2007-02-10 01:46:02 -0800358
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
David Brownell35d3fdd2008-07-23 21:30:43 -0700371 /* 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
David Brownell7e2a31d2008-07-23 21:30:47 -0700376 if (t->enabled)
377 cmos_irq_enable(cmos, RTC_AIE);
David Brownell7be2c7c2007-02-10 01:46:02 -0800378
379 spin_unlock_irq(&rtc_lock);
380
Adrian Huang88b8d332015-07-06 12:19:12 +0800381 cmos->alarm_expires = rtc_tm_to_time64(&t->time);
382
David Brownell7be2c7c2007-02-10 01:46:02 -0800383 return 0;
384}
385
Borislav Petkovd5a1c7e2013-07-20 19:00:23 +0200386/*
387 * Do not disable RTC alarm on shutdown - workaround for b0rked BIOSes.
388 */
389static bool alarm_disable_quirk;
390
391static int __init set_alarm_disable_quirk(const struct dmi_system_id *id)
392{
393 alarm_disable_quirk = true;
Joe Perchesa737e832015-04-16 12:46:14 -0700394 pr_info("BIOS has alarm-disable quirk - RTC alarms disabled\n");
Borislav Petkovd5a1c7e2013-07-20 19:00:23 +0200395 return 0;
396}
397
398static 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
Herton Ronaldo Krzesinskia8462ef2009-12-15 16:45:56 -0800430static int cmos_alarm_irq_enable(struct device *dev, unsigned int enabled)
David Brownell7be2c7c2007-02-10 01:46:02 -0800431{
432 struct cmos_rtc *cmos = dev_get_drvdata(dev);
David Brownell7be2c7c2007-02-10 01:46:02 -0800433 unsigned long flags;
434
Herton Ronaldo Krzesinskia8462ef2009-12-15 16:45:56 -0800435 if (!is_valid_irq(cmos->irq))
436 return -EINVAL;
David Brownell7be2c7c2007-02-10 01:46:02 -0800437
Borislav Petkovd5a1c7e2013-07-20 19:00:23 +0200438 if (alarm_disable_quirk)
439 return 0;
440
David Brownell7be2c7c2007-02-10 01:46:02 -0800441 spin_lock_irqsave(&rtc_lock, flags);
Herton Ronaldo Krzesinskia8462ef2009-12-15 16:45:56 -0800442
443 if (enabled)
David Brownell7e2a31d2008-07-23 21:30:47 -0700444 cmos_irq_enable(cmos, RTC_AIE);
Herton Ronaldo Krzesinskia8462ef2009-12-15 16:45:56 -0800445 else
446 cmos_irq_disable(cmos, RTC_AIE);
447
David Brownell7be2c7c2007-02-10 01:46:02 -0800448 spin_unlock_irqrestore(&rtc_lock, flags);
449 return 0;
450}
451
David Brownell7be2c7c2007-02-10 01:46:02 -0800452#if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
453
454static 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 */
Joe Perches4395eb12015-04-15 16:17:51 -0700467 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;
David Brownell7be2c7c2007-02-10 01:46:02 -0800486}
487
488#else
489#define cmos_procfs NULL
490#endif
491
492static const struct rtc_class_ops cmos_rtc_ops = {
Herton Ronaldo Krzesinskia8462ef2009-12-15 16:45:56 -0800493 .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,
Herton Ronaldo Krzesinskia8462ef2009-12-15 16:45:56 -0800498 .alarm_irq_enable = cmos_alarm_irq_enable,
David Brownell7be2c7c2007-02-10 01:46:02 -0800499};
500
501/*----------------------------------------------------------------*/
502
David Brownelle07e2322008-02-06 01:38:43 -0800503/*
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
511static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700512cmos_nvram_read(struct file *filp, struct kobject *kobj,
513 struct bin_attribute *attr,
David Brownelle07e2322008-02-06 01:38:43 -0800514 char *buf, loff_t off, size_t count)
515{
516 int retval;
517
518 if (unlikely(off >= attr->size))
519 return 0;
David Brownellc8fc40c2008-10-18 20:27:47 -0700520 if (unlikely(off < 0))
521 return -EINVAL;
David Brownelle07e2322008-02-06 01:38:43 -0800522 if ((off + count) > attr->size)
523 count = attr->size - off;
524
David Brownellc8fc40c2008-10-18 20:27:47 -0700525 off += NVRAM_OFFSET;
David Brownelle07e2322008-02-06 01:38:43 -0800526 spin_lock_irq(&rtc_lock);
David Brownellc8fc40c2008-10-18 20:27:47 -0700527 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 }
David Brownelle07e2322008-02-06 01:38:43 -0800535 spin_unlock_irq(&rtc_lock);
536
537 return retval;
538}
539
540static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700541cmos_nvram_write(struct file *filp, struct kobject *kobj,
542 struct bin_attribute *attr,
David Brownelle07e2322008-02-06 01:38:43 -0800543 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;
David Brownellc8fc40c2008-10-18 20:27:47 -0700551 if (unlikely(off < 0))
552 return -EINVAL;
David Brownelle07e2322008-02-06 01:38:43 -0800553 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 */
David Brownellc8fc40c2008-10-18 20:27:47 -0700561 off += NVRAM_OFFSET;
David Brownelle07e2322008-02-06 01:38:43 -0800562 spin_lock_irq(&rtc_lock);
David Brownellc8fc40c2008-10-18 20:27:47 -0700563 for (retval = 0; count; count--, off++, retval++) {
David Brownelle07e2322008-02-06 01:38:43 -0800564 /* don't trash RTC registers */
565 if (off == cmos->day_alrm
566 || off == cmos->mon_alrm
567 || off == cmos->century)
568 buf++;
David Brownellc8fc40c2008-10-18 20:27:47 -0700569 else if (off < 128)
David Brownelle07e2322008-02-06 01:38:43 -0800570 CMOS_WRITE(*buf++, off);
David Brownellc8fc40c2008-10-18 20:27:47 -0700571 else if (can_bank2)
572 cmos_write_bank2(*buf++, off);
573 else
574 break;
David Brownelle07e2322008-02-06 01:38:43 -0800575 }
576 spin_unlock_irq(&rtc_lock);
577
578 return retval;
579}
580
581static struct bin_attribute nvram = {
582 .attr = {
583 .name = "nvram",
584 .mode = S_IRUGO | S_IWUSR,
David Brownelle07e2322008-02-06 01:38:43 -0800585 },
586
587 .read = cmos_nvram_read,
588 .write = cmos_nvram_write,
589 /* size gets set up later */
590};
591
592/*----------------------------------------------------------------*/
593
David Brownell7be2c7c2007-02-10 01:46:02 -0800594static struct cmos_rtc cmos_rtc;
595
596static irqreturn_t cmos_interrupt(int irq, void *p)
597{
598 u8 irqstat;
David Brownell8a0bdfd2008-02-06 01:38:45 -0800599 u8 rtc_control;
David Brownell7be2c7c2007-02-10 01:46:02 -0800600
601 spin_lock(&rtc_lock);
David Brownell35d3fdd2008-07-23 21:30:43 -0700602
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 ...
Bernhard Walle9d8af782008-02-06 01:38:52 -0800609 */
David Brownell35d3fdd2008-07-23 21:30:43 -0700610 irqstat = CMOS_READ(RTC_INTR_FLAGS);
611 rtc_control = CMOS_READ(RTC_CONTROL);
Bernhard Walle9d8af782008-02-06 01:38:52 -0800612 if (is_hpet_enabled())
613 irqstat = (unsigned long)irq & 0xF0;
Derek Basehore998a0602013-07-03 15:07:54 -0700614
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;
David Brownell8a0bdfd2008-02-06 01:38:45 -0800622
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) {
Derek Basehore998a0602013-07-03 15:07:54 -0700628 cmos_rtc.suspend_ctrl &= ~RTC_AIE;
David Brownell8a0bdfd2008-02-06 01:38:45 -0800629 rtc_control &= ~RTC_AIE;
630 CMOS_WRITE(rtc_control, RTC_CONTROL);
David Brownell35d3fdd2008-07-23 21:30:43 -0700631 hpet_mask_rtc_irq_bit(RTC_AIE);
David Brownell8a0bdfd2008-02-06 01:38:45 -0800632 CMOS_READ(RTC_INTR_FLAGS);
633 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800634 spin_unlock(&rtc_lock);
635
David Brownellbcd9b892007-04-01 23:49:47 -0700636 if (is_intr(irqstat)) {
David Brownell7be2c7c2007-02-10 01:46:02 -0800637 rtc_update_irq(p, 1, irqstat);
638 return IRQ_HANDLED;
639 } else
640 return IRQ_NONE;
641}
642
Marko Vrh41ac8df2007-05-08 00:34:09 -0700643#ifdef CONFIG_PNP
David Brownell7be2c7c2007-02-10 01:46:02 -0800644#define INITSECTION
645
646#else
David Brownell7be2c7c2007-02-10 01:46:02 -0800647#define INITSECTION __init
648#endif
649
650static int INITSECTION
651cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
652{
Jingoo Han97a92e72013-11-12 15:10:38 -0800653 struct cmos_rtc_board_info *info = dev_get_platdata(dev);
David Brownell7be2c7c2007-02-10 01:46:02 -0800654 int retval = 0;
655 unsigned char rtc_control;
David Brownelle07e2322008-02-06 01:38:43 -0800656 unsigned address_space;
Maciej W. Rozycki31632db2014-06-06 14:35:49 -0700657 u32 flags = 0;
David Brownell7be2c7c2007-02-10 01:46:02 -0800658
659 /* there can be only one ... */
660 if (cmos_rtc.dev)
661 return -EBUSY;
662
663 if (!ports)
664 return -ENODEV;
665
David Brownell05440df2007-10-16 01:28:21 -0700666 /* 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 */
Maciej W. Rozycki31632db2014-06-06 14:35:49 -0700671 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);
David Brownell05440df2007-10-16 01:28:21 -0700677 if (!ports) {
678 dev_dbg(dev, "i/o registers already in use\n");
679 return -EBUSY;
680 }
681
David Brownell7be2c7c2007-02-10 01:46:02 -0800682 cmos_rtc.irq = rtc_irq;
683 cmos_rtc.iomem = ports;
684
David Brownelle07e2322008-02-06 01:38:43 -0800685 /* Heuristic to deduce NVRAM size ... do what the legacy NVRAM
686 * driver did, but don't reject unknown configs. Old hardware
David Brownellc8fc40c2008-10-18 20:27:47 -0700687 * won't address 128 bytes. Newer chips have multiple banks,
688 * though they may not be listed in one I/O resource.
David Brownelle07e2322008-02-06 01:38:43 -0800689 */
690#if defined(CONFIG_ATARI)
691 address_space = 64;
Wu Zhangjin95abd0d2009-11-05 09:21:54 +0800692#elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) \
Srikanth Krishnakar8cb7c712010-10-14 04:03:35 +0000693 || defined(__sparc__) || defined(__mips__) \
694 || defined(__powerpc__)
David Brownelle07e2322008-02-06 01:38:43 -0800695 address_space = 128;
696#else
697#warning Assuming 128 bytes of RTC+NVRAM address space, not 64 bytes.
698 address_space = 128;
699#endif
David Brownellc8fc40c2008-10-18 20:27:47 -0700700 if (can_bank2 && ports->end > (ports->start + 1))
701 address_space = 256;
David Brownelle07e2322008-02-06 01:38:43 -0800702
David Brownell87ac84f2007-05-08 00:34:00 -0700703 /* 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.
David Brownelle07e2322008-02-06 01:38:43 -0800707 *
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.
David Brownell7be2c7c2007-02-10 01:46:02 -0800711 */
712 if (info) {
Maciej W. Rozycki31632db2014-06-06 14:35:49 -0700713 if (info->flags)
714 flags = info->flags;
715 if (info->address_space)
716 address_space = info->address_space;
717
David Brownelle07e2322008-02-06 01:38:43 -0800718 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;
David Brownell87ac84f2007-05-08 00:34:00 -0700724
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 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800729 }
730
Dan Carpenter6ba8bcd2010-05-24 14:33:49 -0700731 cmos_rtc.dev = dev;
732 dev_set_drvdata(dev, &cmos_rtc);
733
David Brownell7be2c7c2007-02-10 01:46:02 -0800734 cmos_rtc.rtc = rtc_device_register(driver_name, dev,
735 &cmos_rtc_ops, THIS_MODULE);
David Brownell05440df2007-10-16 01:28:21 -0700736 if (IS_ERR(cmos_rtc.rtc)) {
737 retval = PTR_ERR(cmos_rtc.rtc);
738 goto cleanup0;
739 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800740
Kay Sieversd4afc762009-01-06 14:42:11 -0800741 rename_region(ports, dev_name(&cmos_rtc.rtc->dev));
David Brownell7be2c7c2007-02-10 01:46:02 -0800742
743 spin_lock_irq(&rtc_lock);
744
Maciej W. Rozycki31632db2014-06-06 14:35:49 -0700745 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 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800756
David Brownell7e2a31d2008-07-23 21:30:47 -0700757 /* disable irqs */
Maciej W. Rozycki31632db2014-06-06 14:35:49 -0700758 if (is_valid_irq(rtc_irq))
759 cmos_irq_disable(&cmos_rtc, RTC_PIE | RTC_AIE | RTC_UIE);
David Brownell35d3fdd2008-07-23 21:30:43 -0700760
David Brownell7e2a31d2008-07-23 21:30:47 -0700761 rtc_control = CMOS_READ(RTC_CONTROL);
David Brownell7be2c7c2007-02-10 01:46:02 -0800762
763 spin_unlock_irq(&rtc_lock);
764
Arnaud Patard3804a892010-04-29 11:58:44 +0200765 /* FIXME:
David Brownell7be2c7c2007-02-10 01:46:02 -0800766 * <asm-generic/rtc.h> doesn't know 12-hour mode either.
767 */
Sachin Kamat5e8599d2013-07-03 15:05:45 -0700768 if (is_valid_irq(rtc_irq) && !(rtc_control & RTC_24H)) {
Arnaud Patard3804a892010-04-29 11:58:44 +0200769 dev_warn(dev, "only 24-hr supported\n");
David Brownell7be2c7c2007-02-10 01:46:02 -0800770 retval = -ENXIO;
771 goto cleanup1;
772 }
773
Bernhard Walle9d8af782008-02-06 01:38:52 -0800774 if (is_valid_irq(rtc_irq)) {
775 irq_handler_t rtc_cmos_int_handler;
776
777 if (is_hpet_enabled()) {
Bernhard Walle9d8af782008-02-06 01:38:52 -0800778 rtc_cmos_int_handler = hpet_rtc_interrupt;
Andrew Morton24b34472014-01-23 15:55:15 -0800779 retval = hpet_register_irq_handler(cmos_interrupt);
780 if (retval) {
Jingoo Hanee443352013-02-21 16:45:34 -0800781 dev_warn(dev, "hpet_register_irq_handler "
Bernhard Walle9d8af782008-02-06 01:38:52 -0800782 " 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,
Yong Zhang2f6e5f92012-03-23 15:02:34 -0700789 0, dev_name(&cmos_rtc.rtc->dev),
David Brownellab6a2d72007-05-08 00:33:30 -0700790 cmos_rtc.rtc);
Bernhard Walle9d8af782008-02-06 01:38:52 -0800791 if (retval < 0) {
792 dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
793 goto cleanup1;
794 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800795 }
Bernhard Walle9d8af782008-02-06 01:38:52 -0800796 hpet_rtc_timer_init();
David Brownell7be2c7c2007-02-10 01:46:02 -0800797
David Brownelle07e2322008-02-06 01:38:43 -0800798 /* 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 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800805
Jingoo Hanee443352013-02-21 16:45:34 -0800806 dev_info(dev, "%s%s, %zd bytes nvram%s\n",
Krzysztof Halasa6d029b62009-04-21 12:24:49 -0700807 !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" : "");
David Brownell7be2c7c2007-02-10 01:46:02 -0800814
815 return 0;
816
David Brownelle07e2322008-02-06 01:38:43 -0800817cleanup2:
818 if (is_valid_irq(rtc_irq))
819 free_irq(rtc_irq, cmos_rtc.rtc);
David Brownell7be2c7c2007-02-10 01:46:02 -0800820cleanup1:
David Brownell05440df2007-10-16 01:28:21 -0700821 cmos_rtc.dev = NULL;
David Brownell7be2c7c2007-02-10 01:46:02 -0800822 rtc_device_unregister(cmos_rtc.rtc);
David Brownell05440df2007-10-16 01:28:21 -0700823cleanup0:
Maciej W. Rozycki31632db2014-06-06 14:35:49 -0700824 if (RTC_IOMAPPED)
825 release_region(ports->start, resource_size(ports));
826 else
827 release_mem_region(ports->start, resource_size(ports));
David Brownell7be2c7c2007-02-10 01:46:02 -0800828 return retval;
829}
830
Maciej W. Rozycki31632db2014-06-06 14:35:49 -0700831static void cmos_do_shutdown(int rtc_irq)
David Brownell7be2c7c2007-02-10 01:46:02 -0800832{
David Brownell7be2c7c2007-02-10 01:46:02 -0800833 spin_lock_irq(&rtc_lock);
Maciej W. Rozycki31632db2014-06-06 14:35:49 -0700834 if (is_valid_irq(rtc_irq))
835 cmos_irq_disable(&cmos_rtc, RTC_IRQMASK);
David Brownell7be2c7c2007-02-10 01:46:02 -0800836 spin_unlock_irq(&rtc_lock);
837}
838
839static void __exit cmos_do_remove(struct device *dev)
840{
841 struct cmos_rtc *cmos = dev_get_drvdata(dev);
David Brownell05440df2007-10-16 01:28:21 -0700842 struct resource *ports;
David Brownell7be2c7c2007-02-10 01:46:02 -0800843
Maciej W. Rozycki31632db2014-06-06 14:35:49 -0700844 cmos_do_shutdown(cmos->irq);
David Brownell7be2c7c2007-02-10 01:46:02 -0800845
David Brownelle07e2322008-02-06 01:38:43 -0800846 sysfs_remove_bin_file(&dev->kobj, &nvram);
847
Bernhard Walle9d8af782008-02-06 01:38:52 -0800848 if (is_valid_irq(cmos->irq)) {
David Brownell05440df2007-10-16 01:28:21 -0700849 free_irq(cmos->irq, cmos->rtc);
Bernhard Walle9d8af782008-02-06 01:38:52 -0800850 hpet_unregister_irq_handler(cmos_interrupt);
851 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800852
David Brownell05440df2007-10-16 01:28:21 -0700853 rtc_device_unregister(cmos->rtc);
854 cmos->rtc = NULL;
David Brownell7be2c7c2007-02-10 01:46:02 -0800855
David Brownell05440df2007-10-16 01:28:21 -0700856 ports = cmos->iomem;
Maciej W. Rozycki31632db2014-06-06 14:35:49 -0700857 if (RTC_IOMAPPED)
858 release_region(ports->start, resource_size(ports));
859 else
860 release_mem_region(ports->start, resource_size(ports));
David Brownell05440df2007-10-16 01:28:21 -0700861 cmos->iomem = NULL;
862
863 cmos->dev = NULL;
David Brownell7be2c7c2007-02-10 01:46:02 -0800864}
865
Adrian Huang88b8d332015-07-06 12:19:12 +0800866static 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
Daniel Glöcknera882b142014-10-13 15:53:16 -0700911#ifdef CONFIG_PM
David Brownell7be2c7c2007-02-10 01:46:02 -0800912
Paul Fox2fb08e62011-01-12 17:00:07 -0800913static int cmos_suspend(struct device *dev)
David Brownell7be2c7c2007-02-10 01:46:02 -0800914{
915 struct cmos_rtc *cmos = dev_get_drvdata(dev);
David Brownellbcd9b892007-04-01 23:49:47 -0700916 unsigned char tmp;
David Brownell7be2c7c2007-02-10 01:46:02 -0800917
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)) {
David Brownell35d3fdd2008-07-23 21:30:43 -0700922 unsigned char mask;
David Brownellbcd9b892007-04-01 23:49:47 -0700923
Rafael J. Wysocki74c46332008-09-02 14:36:11 -0700924 if (device_may_wakeup(dev))
David Brownell35d3fdd2008-07-23 21:30:43 -0700925 mask = RTC_IRQMASK & ~RTC_AIE;
David Brownell7be2c7c2007-02-10 01:46:02 -0800926 else
David Brownell35d3fdd2008-07-23 21:30:43 -0700927 mask = RTC_IRQMASK;
928 tmp &= ~mask;
David Brownell7be2c7c2007-02-10 01:46:02 -0800929 CMOS_WRITE(tmp, RTC_CONTROL);
Derek Basehoree0057152013-04-29 16:20:23 -0700930 hpet_mask_rtc_irq_bit(mask);
David Brownell35d3fdd2008-07-23 21:30:43 -0700931
David Brownell7e2a31d2008-07-23 21:30:47 -0700932 cmos_checkintr(cmos, tmp);
David Brownellbcd9b892007-04-01 23:49:47 -0700933 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800934 spin_unlock_irq(&rtc_lock);
935
David Brownell87ac84f2007-05-08 00:34:00 -0700936 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 }
David Brownell7be2c7c2007-02-10 01:46:02 -0800943
Jingoo Hanee443352013-02-21 16:45:34 -0800944 dev_dbg(dev, "suspend%s, ctrl %02x\n",
David Brownell7be2c7c2007-02-10 01:46:02 -0800945 (tmp & RTC_AIE) ? ", alarm may wake" : "",
946 tmp);
947
948 return 0;
949}
950
Rafael J. Wysocki74c46332008-09-02 14:36:11 -0700951/* 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 */
957static inline int cmos_poweroff(struct device *dev)
958{
Paul Fox2fb08e62011-01-12 17:00:07 -0800959 return cmos_suspend(dev);
Rafael J. Wysocki74c46332008-09-02 14:36:11 -0700960}
961
Daniel Glöcknera882b142014-10-13 15:53:16 -0700962#ifdef CONFIG_PM_SLEEP
963
David Brownell7be2c7c2007-02-10 01:46:02 -0800964static int cmos_resume(struct device *dev)
965{
966 struct cmos_rtc *cmos = dev_get_drvdata(dev);
Derek Basehore998a0602013-07-03 15:07:54 -0700967 unsigned char tmp;
David Brownell7be2c7c2007-02-10 01:46:02 -0800968
Derek Basehore998a0602013-07-03 15:07:54 -0700969 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;
David Brownell7be2c7c2007-02-10 01:46:02 -0800980 /* re-enable any irqs previously active */
David Brownell35d3fdd2008-07-23 21:30:43 -0700981 if (tmp & RTC_IRQMASK) {
982 unsigned char mask;
David Brownell7be2c7c2007-02-10 01:46:02 -0800983
Derek Basehoreebf8d6c2013-06-12 14:04:45 -0700984 if (device_may_wakeup(dev))
985 hpet_rtc_timer_init();
986
David Brownell35d3fdd2008-07-23 21:30:43 -0700987 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;
David Brownell7e2a31d2008-07-23 21:30:47 -0700993 if (!is_hpet_enabled() || !is_intr(mask))
David Brownell35d3fdd2008-07-23 21:30:43 -0700994 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);
David Brownell7be2c7c2007-02-10 01:46:02 -08001003 }
Derek Basehore998a0602013-07-03 15:07:54 -07001004 spin_unlock_irq(&rtc_lock);
David Brownell7be2c7c2007-02-10 01:46:02 -08001005
Jingoo Hanee443352013-02-21 16:45:34 -08001006 dev_dbg(dev, "resume, ctrl %02x\n", tmp);
David Brownell7be2c7c2007-02-10 01:46:02 -08001007
1008 return 0;
1009}
1010
Daniel Glöcknera882b142014-10-13 15:53:16 -07001011#endif
David Brownell7be2c7c2007-02-10 01:46:02 -08001012#else
Rafael J. Wysocki74c46332008-09-02 14:36:11 -07001013
1014static inline int cmos_poweroff(struct device *dev)
1015{
1016 return -ENOSYS;
1017}
1018
David Brownell7be2c7c2007-02-10 01:46:02 -08001019#endif
1020
Mika Westerbergb5ada462014-04-03 14:50:05 -07001021static SIMPLE_DEV_PM_OPS(cmos_pm_ops, cmos_suspend, cmos_resume);
1022
David Brownell7be2c7c2007-02-10 01:46:02 -08001023/*----------------------------------------------------------------*/
1024
David Brownelle07e2322008-02-06 01:38:43 -08001025/* 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.
David Brownell7be2c7c2007-02-10 01:46:02 -08001031 */
1032
Bjorn Helgaasa474aae2008-10-14 13:50:21 -06001033#ifdef CONFIG_ACPI
1034
1035#include <linux/acpi.h>
1036
Bjorn Helgaasa474aae2008-10-14 13:50:21 -06001037static u32 rtc_handler(void *context)
1038{
Daniel Drakeb2201e52012-05-18 22:59:41 +02001039 struct device *dev = context;
1040
1041 pm_wakeup_event(dev, 0);
Bjorn Helgaasa474aae2008-10-14 13:50:21 -06001042 acpi_clear_event(ACPI_EVENT_RTC);
1043 acpi_disable_event(ACPI_EVENT_RTC, 0);
1044 return ACPI_INTERRUPT_HANDLED;
1045}
1046
Daniel Drakeb2201e52012-05-18 22:59:41 +02001047static inline void rtc_wake_setup(struct device *dev)
Bjorn Helgaasa474aae2008-10-14 13:50:21 -06001048{
Daniel Drakeb2201e52012-05-18 22:59:41 +02001049 acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, dev);
Bjorn Helgaasa474aae2008-10-14 13:50:21 -06001050 /*
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
1058static 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
1064static void rtc_wake_off(struct device *dev)
1065{
1066 acpi_disable_event(ACPI_EVENT_RTC, 0);
1067}
Bjorn Helgaasa474aae2008-10-14 13:50:21 -06001068
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 */
1074static struct cmos_rtc_board_info acpi_rtc_info;
1075
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001076static void cmos_wake_setup(struct device *dev)
Bjorn Helgaasa474aae2008-10-14 13:50:21 -06001077{
1078 if (acpi_disabled)
1079 return;
1080
Daniel Drakeb2201e52012-05-18 22:59:41 +02001081 rtc_wake_setup(dev);
Bjorn Helgaasa474aae2008-10-14 13:50:21 -06001082 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
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001108static void cmos_wake_setup(struct device *dev)
Bjorn Helgaasa474aae2008-10-14 13:50:21 -06001109{
1110}
1111
1112#endif
1113
Marko Vrh41ac8df2007-05-08 00:34:09 -07001114#ifdef CONFIG_PNP
David Brownell7be2c7c2007-02-10 01:46:02 -08001115
1116#include <linux/pnp.h>
1117
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -08001118static int cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
David Brownell7be2c7c2007-02-10 01:46:02 -08001119{
Bjorn Helgaasa474aae2008-10-14 13:50:21 -06001120 cmos_wake_setup(&pnp->dev);
1121
Sachin Kamat5e8599d2013-07-03 15:05:45 -07001122 if (pnp_port_start(pnp, 0) == 0x70 && !pnp_irq_valid(pnp, 0))
Matthew Garrett6cd8fa82007-06-01 00:46:51 -07001123 /* 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 */
Bjorn Helgaas8766ad02008-04-28 16:34:27 -06001127 return cmos_do_probe(&pnp->dev,
1128 pnp_get_resource(pnp, IORESOURCE_IO, 0), 8);
Matthew Garrett6cd8fa82007-06-01 00:46:51 -07001129 else
1130 return cmos_do_probe(&pnp->dev,
Bjorn Helgaas8766ad02008-04-28 16:34:27 -06001131 pnp_get_resource(pnp, IORESOURCE_IO, 0),
1132 pnp_irq(pnp, 0));
David Brownell7be2c7c2007-02-10 01:46:02 -08001133}
1134
1135static void __exit cmos_pnp_remove(struct pnp_dev *pnp)
1136{
1137 cmos_do_remove(&pnp->dev);
1138}
1139
OGAWA Hirofumi004731b2010-01-08 14:43:11 -08001140static void cmos_pnp_shutdown(struct pnp_dev *pnp)
Rafael J. Wysocki74c46332008-09-02 14:36:11 -07001141{
Maciej W. Rozycki31632db2014-06-06 14:35:49 -07001142 struct device *dev = &pnp->dev;
1143 struct cmos_rtc *cmos = dev_get_drvdata(dev);
1144
Adrian Huang88b8d332015-07-06 12:19:12 +08001145 if (system_state == SYSTEM_POWER_OFF) {
1146 int retval = cmos_poweroff(dev);
1147
1148 if (cmos_aie_poweroff(dev) < 0 && !retval)
1149 return;
1150 }
Rafael J. Wysocki74c46332008-09-02 14:36:11 -07001151
Maciej W. Rozycki31632db2014-06-06 14:35:49 -07001152 cmos_do_shutdown(cmos->irq);
Rafael J. Wysocki74c46332008-09-02 14:36:11 -07001153}
David Brownell7be2c7c2007-02-10 01:46:02 -08001154
1155static const struct pnp_device_id rtc_ids[] = {
1156 { .id = "PNP0b00", },
1157 { .id = "PNP0b01", },
1158 { .id = "PNP0b02", },
1159 { },
1160};
1161MODULE_DEVICE_TABLE(pnp, rtc_ids);
1162
1163static 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),
OGAWA Hirofumi004731b2010-01-08 14:43:11 -08001168 .shutdown = cmos_pnp_shutdown,
David Brownell7be2c7c2007-02-10 01:46:02 -08001169
1170 /* flag ensures resume() gets called, and stops syslog spam */
1171 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
Shuah Khana8a38082013-09-11 14:23:11 -07001172 .driver = {
1173 .pm = &cmos_pm_ops,
1174 },
David Brownell7be2c7c2007-02-10 01:46:02 -08001175};
1176
Stas Sergeev1da2e3d2008-06-12 15:21:54 -07001177#endif /* CONFIG_PNP */
David Brownell7be2c7c2007-02-10 01:46:02 -08001178
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +01001179#ifdef CONFIG_OF
1180static const struct of_device_id of_cmos_match[] = {
1181 {
1182 .compatible = "motorola,mc146818",
1183 },
1184 { },
1185};
1186MODULE_DEVICE_TABLE(of, of_cmos_match);
1187
1188static __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
1217static inline void cmos_of_init(struct platform_device *pdev) {}
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +01001218#endif
David Brownell7be2c7c2007-02-10 01:46:02 -08001219/*----------------------------------------------------------------*/
1220
Marko Vrh41ac8df2007-05-08 00:34:09 -07001221/* Platform setup should have set up an RTC device, when PNP is
David Brownellbcd9b892007-04-01 23:49:47 -07001222 * unavailable ... this could happen even on (older) PCs.
David Brownell7be2c7c2007-02-10 01:46:02 -08001223 */
1224
1225static int __init cmos_platform_probe(struct platform_device *pdev)
1226{
Maciej W. Rozycki31632db2014-06-06 14:35:49 -07001227 struct resource *resource;
1228 int irq;
1229
Sebastian Andrzej Siewior3bcbaf62011-02-22 21:07:46 +01001230 cmos_of_init(pdev);
Bjorn Helgaasa474aae2008-10-14 13:50:21 -06001231 cmos_wake_setup(&pdev->dev);
Maciej W. Rozycki31632db2014-06-06 14:35:49 -07001232
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);
David Brownell7be2c7c2007-02-10 01:46:02 -08001242}
1243
1244static int __exit cmos_platform_remove(struct platform_device *pdev)
1245{
1246 cmos_do_remove(&pdev->dev);
1247 return 0;
1248}
1249
1250static void cmos_platform_shutdown(struct platform_device *pdev)
1251{
Maciej W. Rozycki31632db2014-06-06 14:35:49 -07001252 struct device *dev = &pdev->dev;
1253 struct cmos_rtc *cmos = dev_get_drvdata(dev);
1254
Adrian Huang88b8d332015-07-06 12:19:12 +08001255 if (system_state == SYSTEM_POWER_OFF) {
1256 int retval = cmos_poweroff(dev);
1257
1258 if (cmos_aie_poweroff(dev) < 0 && !retval)
1259 return;
1260 }
Rafael J. Wysocki74c46332008-09-02 14:36:11 -07001261
Maciej W. Rozycki31632db2014-06-06 14:35:49 -07001262 cmos_do_shutdown(cmos->irq);
David Brownell7be2c7c2007-02-10 01:46:02 -08001263}
1264
Kay Sieversad28a072008-04-10 21:29:25 -07001265/* work with hotplug and coldplug */
1266MODULE_ALIAS("platform:rtc_cmos");
1267
David Brownell7be2c7c2007-02-10 01:46:02 -08001268static struct platform_driver cmos_platform_driver = {
1269 .remove = __exit_p(cmos_platform_remove),
1270 .shutdown = cmos_platform_shutdown,
1271 .driver = {
Geert Uytterhoevenc823a202014-01-23 15:55:11 -08001272 .name = driver_name,
Paul Fox2fb08e62011-01-12 17:00:07 -08001273#ifdef CONFIG_PM
1274 .pm = &cmos_pm_ops,
1275#endif
Sachin Kamatc8a60462013-02-21 16:44:28 -08001276 .of_match_table = of_match_ptr(of_cmos_match),
David Brownell7be2c7c2007-02-10 01:46:02 -08001277 }
1278};
1279
Thadeu Lima de Souza Cascardo65909812009-07-29 15:02:13 -07001280#ifdef CONFIG_PNP
1281static bool pnp_driver_registered;
1282#endif
1283static bool platform_driver_registered;
1284
David Brownell7be2c7c2007-02-10 01:46:02 -08001285static int __init cmos_init(void)
1286{
Bjorn Helgaas72f22b12008-10-14 17:01:59 -06001287 int retval = 0;
1288
Stas Sergeev1da2e3d2008-06-12 15:21:54 -07001289#ifdef CONFIG_PNP
Thadeu Lima de Souza Cascardo65909812009-07-29 15:02:13 -07001290 retval = pnp_register_driver(&cmos_pnp_driver);
1291 if (retval == 0)
1292 pnp_driver_registered = true;
Bjorn Helgaas72f22b12008-10-14 17:01:59 -06001293#endif
1294
Thadeu Lima de Souza Cascardo65909812009-07-29 15:02:13 -07001295 if (!cmos_rtc.dev) {
Bjorn Helgaas72f22b12008-10-14 17:01:59 -06001296 retval = platform_driver_probe(&cmos_platform_driver,
1297 cmos_platform_probe);
Thadeu Lima de Souza Cascardo65909812009-07-29 15:02:13 -07001298 if (retval == 0)
1299 platform_driver_registered = true;
1300 }
Bjorn Helgaas72f22b12008-10-14 17:01:59 -06001301
Borislav Petkovd5a1c7e2013-07-20 19:00:23 +02001302 dmi_check_system(rtc_quirks);
1303
Bjorn Helgaas72f22b12008-10-14 17:01:59 -06001304 if (retval == 0)
1305 return 0;
1306
1307#ifdef CONFIG_PNP
Thadeu Lima de Souza Cascardo65909812009-07-29 15:02:13 -07001308 if (pnp_driver_registered)
1309 pnp_unregister_driver(&cmos_pnp_driver);
Bjorn Helgaas72f22b12008-10-14 17:01:59 -06001310#endif
1311 return retval;
David Brownell7be2c7c2007-02-10 01:46:02 -08001312}
1313module_init(cmos_init);
1314
1315static void __exit cmos_exit(void)
1316{
Stas Sergeev1da2e3d2008-06-12 15:21:54 -07001317#ifdef CONFIG_PNP
Thadeu Lima de Souza Cascardo65909812009-07-29 15:02:13 -07001318 if (pnp_driver_registered)
1319 pnp_unregister_driver(&cmos_pnp_driver);
Bjorn Helgaas72f22b12008-10-14 17:01:59 -06001320#endif
Thadeu Lima de Souza Cascardo65909812009-07-29 15:02:13 -07001321 if (platform_driver_registered)
1322 platform_driver_unregister(&cmos_platform_driver);
David Brownell7be2c7c2007-02-10 01:46:02 -08001323}
1324module_exit(cmos_exit);
1325
1326
David Brownell7be2c7c2007-02-10 01:46:02 -08001327MODULE_AUTHOR("David Brownell");
1328MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
1329MODULE_LICENSE("GPL");