rtc-cmos alarm acts as oneshot
[safe/jmp/linux-2.6] / 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 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
35 #include <linux/spinlock.h>
36 #include <linux/platform_device.h>
37 #include <linux/mod_devicetable.h>
38
39 /* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */
40 #include <asm-generic/rtc.h>
41
42
43 struct cmos_rtc {
44         struct rtc_device       *rtc;
45         struct device           *dev;
46         int                     irq;
47         struct resource         *iomem;
48
49         void                    (*wake_on)(struct device *);
50         void                    (*wake_off)(struct device *);
51
52         u8                      enabled_wake;
53         u8                      suspend_ctrl;
54
55         /* newer hardware extends the original register set */
56         u8                      day_alrm;
57         u8                      mon_alrm;
58         u8                      century;
59 };
60
61 /* both platform and pnp busses use negative numbers for invalid irqs */
62 #define is_valid_irq(n)         ((n) >= 0)
63
64 static const char driver_name[] = "rtc_cmos";
65
66 /* The RTC_INTR register may have e.g. RTC_PF set even if RTC_PIE is clear;
67  * always mask it against the irq enable bits in RTC_CONTROL.  Bit values
68  * are the same: PF==PIE, AF=AIE, UF=UIE; so RTC_IRQMASK works with both.
69  */
70 #define RTC_IRQMASK     (RTC_PF | RTC_AF | RTC_UF)
71
72 static inline int is_intr(u8 rtc_intr)
73 {
74         if (!(rtc_intr & RTC_IRQF))
75                 return 0;
76         return rtc_intr & RTC_IRQMASK;
77 }
78
79 /*----------------------------------------------------------------*/
80
81 static int cmos_read_time(struct device *dev, struct rtc_time *t)
82 {
83         /* REVISIT:  if the clock has a "century" register, use
84          * that instead of the heuristic in get_rtc_time().
85          * That'll make Y3K compatility (year > 2070) easy!
86          */
87         get_rtc_time(t);
88         return 0;
89 }
90
91 static int cmos_set_time(struct device *dev, struct rtc_time *t)
92 {
93         /* REVISIT:  set the "century" register if available
94          *
95          * NOTE: this ignores the issue whereby updating the seconds
96          * takes effect exactly 500ms after we write the register.
97          * (Also queueing and other delays before we get this far.)
98          */
99         return set_rtc_time(t);
100 }
101
102 static int cmos_read_alarm(struct device *dev, struct rtc_wkalrm *t)
103 {
104         struct cmos_rtc *cmos = dev_get_drvdata(dev);
105         unsigned char   rtc_control;
106
107         if (!is_valid_irq(cmos->irq))
108                 return -EIO;
109
110         /* Basic alarms only support hour, minute, and seconds fields.
111          * Some also support day and month, for alarms up to a year in
112          * the future.
113          */
114         t->time.tm_mday = -1;
115         t->time.tm_mon = -1;
116
117         spin_lock_irq(&rtc_lock);
118         t->time.tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
119         t->time.tm_min = CMOS_READ(RTC_MINUTES_ALARM);
120         t->time.tm_hour = CMOS_READ(RTC_HOURS_ALARM);
121
122         if (cmos->day_alrm) {
123                 /* ignore upper bits on readback per ACPI spec */
124                 t->time.tm_mday = CMOS_READ(cmos->day_alrm) & 0x3f;
125                 if (!t->time.tm_mday)
126                         t->time.tm_mday = -1;
127
128                 if (cmos->mon_alrm) {
129                         t->time.tm_mon = CMOS_READ(cmos->mon_alrm);
130                         if (!t->time.tm_mon)
131                                 t->time.tm_mon = -1;
132                 }
133         }
134
135         rtc_control = CMOS_READ(RTC_CONTROL);
136         spin_unlock_irq(&rtc_lock);
137
138         /* REVISIT this assumes PC style usage:  always BCD */
139
140         if (((unsigned)t->time.tm_sec) < 0x60)
141                 t->time.tm_sec = BCD2BIN(t->time.tm_sec);
142         else
143                 t->time.tm_sec = -1;
144         if (((unsigned)t->time.tm_min) < 0x60)
145                 t->time.tm_min = BCD2BIN(t->time.tm_min);
146         else
147                 t->time.tm_min = -1;
148         if (((unsigned)t->time.tm_hour) < 0x24)
149                 t->time.tm_hour = BCD2BIN(t->time.tm_hour);
150         else
151                 t->time.tm_hour = -1;
152
153         if (cmos->day_alrm) {
154                 if (((unsigned)t->time.tm_mday) <= 0x31)
155                         t->time.tm_mday = BCD2BIN(t->time.tm_mday);
156                 else
157                         t->time.tm_mday = -1;
158                 if (cmos->mon_alrm) {
159                         if (((unsigned)t->time.tm_mon) <= 0x12)
160                                 t->time.tm_mon = BCD2BIN(t->time.tm_mon) - 1;
161                         else
162                                 t->time.tm_mon = -1;
163                 }
164         }
165         t->time.tm_year = -1;
166
167         t->enabled = !!(rtc_control & RTC_AIE);
168         t->pending = 0;
169
170         return 0;
171 }
172
173 static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
174 {
175         struct cmos_rtc *cmos = dev_get_drvdata(dev);
176         unsigned char   mon, mday, hrs, min, sec;
177         unsigned char   rtc_control, rtc_intr;
178
179         if (!is_valid_irq(cmos->irq))
180                 return -EIO;
181
182         /* REVISIT this assumes PC style usage:  always BCD */
183
184         /* Writing 0xff means "don't care" or "match all".  */
185
186         mon = t->time.tm_mon;
187         mon = (mon < 12) ? BIN2BCD(mon) : 0xff;
188         mon++;
189
190         mday = t->time.tm_mday;
191         mday = (mday >= 1 && mday <= 31) ? BIN2BCD(mday) : 0xff;
192
193         hrs = t->time.tm_hour;
194         hrs = (hrs < 24) ? BIN2BCD(hrs) : 0xff;
195
196         min = t->time.tm_min;
197         min = (min < 60) ? BIN2BCD(min) : 0xff;
198
199         sec = t->time.tm_sec;
200         sec = (sec < 60) ? BIN2BCD(sec) : 0xff;
201
202         spin_lock_irq(&rtc_lock);
203
204         /* next rtc irq must not be from previous alarm setting */
205         rtc_control = CMOS_READ(RTC_CONTROL);
206         rtc_control &= ~RTC_AIE;
207         CMOS_WRITE(rtc_control, RTC_CONTROL);
208         rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
209         rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
210         if (is_intr(rtc_intr))
211                 rtc_update_irq(cmos->rtc, 1, rtc_intr);
212
213         /* update alarm */
214         CMOS_WRITE(hrs, RTC_HOURS_ALARM);
215         CMOS_WRITE(min, RTC_MINUTES_ALARM);
216         CMOS_WRITE(sec, RTC_SECONDS_ALARM);
217
218         /* the system may support an "enhanced" alarm */
219         if (cmos->day_alrm) {
220                 CMOS_WRITE(mday, cmos->day_alrm);
221                 if (cmos->mon_alrm)
222                         CMOS_WRITE(mon, cmos->mon_alrm);
223         }
224
225         if (t->enabled) {
226                 rtc_control |= RTC_AIE;
227                 CMOS_WRITE(rtc_control, RTC_CONTROL);
228                 rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
229                 rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
230                 if (is_intr(rtc_intr))
231                         rtc_update_irq(cmos->rtc, 1, rtc_intr);
232         }
233
234         spin_unlock_irq(&rtc_lock);
235
236         return 0;
237 }
238
239 static int cmos_irq_set_freq(struct device *dev, int freq)
240 {
241         struct cmos_rtc *cmos = dev_get_drvdata(dev);
242         int             f;
243         unsigned long   flags;
244
245         if (!is_valid_irq(cmos->irq))
246                 return -ENXIO;
247
248         /* 0 = no irqs; 1 = 2^15 Hz ... 15 = 2^0 Hz */
249         f = ffs(freq);
250         if (f-- > 16)
251                 return -EINVAL;
252         f = 16 - f;
253
254         spin_lock_irqsave(&rtc_lock, flags);
255         CMOS_WRITE(RTC_REF_CLCK_32KHZ | f, RTC_FREQ_SELECT);
256         spin_unlock_irqrestore(&rtc_lock, flags);
257
258         return 0;
259 }
260
261 static int cmos_irq_set_state(struct device *dev, int enabled)
262 {
263         struct cmos_rtc *cmos = dev_get_drvdata(dev);
264         unsigned char   rtc_control, rtc_intr;
265         unsigned long   flags;
266
267         if (!is_valid_irq(cmos->irq))
268                 return -ENXIO;
269
270         spin_lock_irqsave(&rtc_lock, flags);
271         rtc_control = CMOS_READ(RTC_CONTROL);
272
273         if (enabled)
274                 rtc_control |= RTC_PIE;
275         else
276                 rtc_control &= ~RTC_PIE;
277
278         CMOS_WRITE(rtc_control, RTC_CONTROL);
279
280         rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
281         rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
282         if (is_intr(rtc_intr))
283                 rtc_update_irq(cmos->rtc, 1, rtc_intr);
284
285         spin_unlock_irqrestore(&rtc_lock, flags);
286         return 0;
287 }
288
289 #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
290
291 static int
292 cmos_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
293 {
294         struct cmos_rtc *cmos = dev_get_drvdata(dev);
295         unsigned char   rtc_control, rtc_intr;
296         unsigned long   flags;
297
298         switch (cmd) {
299         case RTC_AIE_OFF:
300         case RTC_AIE_ON:
301         case RTC_UIE_OFF:
302         case RTC_UIE_ON:
303         case RTC_PIE_OFF:
304         case RTC_PIE_ON:
305                 if (!is_valid_irq(cmos->irq))
306                         return -EINVAL;
307                 break;
308         default:
309                 return -ENOIOCTLCMD;
310         }
311
312         spin_lock_irqsave(&rtc_lock, flags);
313         rtc_control = CMOS_READ(RTC_CONTROL);
314         switch (cmd) {
315         case RTC_AIE_OFF:       /* alarm off */
316                 rtc_control &= ~RTC_AIE;
317                 break;
318         case RTC_AIE_ON:        /* alarm on */
319                 rtc_control |= RTC_AIE;
320                 break;
321         case RTC_UIE_OFF:       /* update off */
322                 rtc_control &= ~RTC_UIE;
323                 break;
324         case RTC_UIE_ON:        /* update on */
325                 rtc_control |= RTC_UIE;
326                 break;
327         case RTC_PIE_OFF:       /* periodic off */
328                 rtc_control &= ~RTC_PIE;
329                 break;
330         case RTC_PIE_ON:        /* periodic on */
331                 rtc_control |= RTC_PIE;
332                 break;
333         }
334         CMOS_WRITE(rtc_control, RTC_CONTROL);
335         rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
336         rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
337         if (is_intr(rtc_intr))
338                 rtc_update_irq(cmos->rtc, 1, rtc_intr);
339         spin_unlock_irqrestore(&rtc_lock, flags);
340         return 0;
341 }
342
343 #else
344 #define cmos_rtc_ioctl  NULL
345 #endif
346
347 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
348
349 static int cmos_procfs(struct device *dev, struct seq_file *seq)
350 {
351         struct cmos_rtc *cmos = dev_get_drvdata(dev);
352         unsigned char   rtc_control, valid;
353
354         spin_lock_irq(&rtc_lock);
355         rtc_control = CMOS_READ(RTC_CONTROL);
356         valid = CMOS_READ(RTC_VALID);
357         spin_unlock_irq(&rtc_lock);
358
359         /* NOTE:  at least ICH6 reports battery status using a different
360          * (non-RTC) bit; and SQWE is ignored on many current systems.
361          */
362         return seq_printf(seq,
363                         "periodic_IRQ\t: %s\n"
364                         "update_IRQ\t: %s\n"
365                         // "square_wave\t: %s\n"
366                         // "BCD\t\t: %s\n"
367                         "DST_enable\t: %s\n"
368                         "periodic_freq\t: %d\n"
369                         "batt_status\t: %s\n",
370                         (rtc_control & RTC_PIE) ? "yes" : "no",
371                         (rtc_control & RTC_UIE) ? "yes" : "no",
372                         // (rtc_control & RTC_SQWE) ? "yes" : "no",
373                         // (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
374                         (rtc_control & RTC_DST_EN) ? "yes" : "no",
375                         cmos->rtc->irq_freq,
376                         (valid & RTC_VRT) ? "okay" : "dead");
377 }
378
379 #else
380 #define cmos_procfs     NULL
381 #endif
382
383 static const struct rtc_class_ops cmos_rtc_ops = {
384         .ioctl          = cmos_rtc_ioctl,
385         .read_time      = cmos_read_time,
386         .set_time       = cmos_set_time,
387         .read_alarm     = cmos_read_alarm,
388         .set_alarm      = cmos_set_alarm,
389         .proc           = cmos_procfs,
390         .irq_set_freq   = cmos_irq_set_freq,
391         .irq_set_state  = cmos_irq_set_state,
392 };
393
394 /*----------------------------------------------------------------*/
395
396 /*
397  * All these chips have at least 64 bytes of address space, shared by
398  * RTC registers and NVRAM.  Most of those bytes of NVRAM are used
399  * by boot firmware.  Modern chips have 128 or 256 bytes.
400  */
401
402 #define NVRAM_OFFSET    (RTC_REG_D + 1)
403
404 static ssize_t
405 cmos_nvram_read(struct kobject *kobj, struct bin_attribute *attr,
406                 char *buf, loff_t off, size_t count)
407 {
408         int     retval;
409
410         if (unlikely(off >= attr->size))
411                 return 0;
412         if ((off + count) > attr->size)
413                 count = attr->size - off;
414
415         spin_lock_irq(&rtc_lock);
416         for (retval = 0, off += NVRAM_OFFSET; count--; retval++, off++)
417                 *buf++ = CMOS_READ(off);
418         spin_unlock_irq(&rtc_lock);
419
420         return retval;
421 }
422
423 static ssize_t
424 cmos_nvram_write(struct kobject *kobj, struct bin_attribute *attr,
425                 char *buf, loff_t off, size_t count)
426 {
427         struct cmos_rtc *cmos;
428         int             retval;
429
430         cmos = dev_get_drvdata(container_of(kobj, struct device, kobj));
431         if (unlikely(off >= attr->size))
432                 return -EFBIG;
433         if ((off + count) > attr->size)
434                 count = attr->size - off;
435
436         /* NOTE:  on at least PCs and Ataris, the boot firmware uses a
437          * checksum on part of the NVRAM data.  That's currently ignored
438          * here.  If userspace is smart enough to know what fields of
439          * NVRAM to update, updating checksums is also part of its job.
440          */
441         spin_lock_irq(&rtc_lock);
442         for (retval = 0, off += NVRAM_OFFSET; count--; retval++, off++) {
443                 /* don't trash RTC registers */
444                 if (off == cmos->day_alrm
445                                 || off == cmos->mon_alrm
446                                 || off == cmos->century)
447                         buf++;
448                 else
449                         CMOS_WRITE(*buf++, off);
450         }
451         spin_unlock_irq(&rtc_lock);
452
453         return retval;
454 }
455
456 static struct bin_attribute nvram = {
457         .attr = {
458                 .name   = "nvram",
459                 .mode   = S_IRUGO | S_IWUSR,
460                 .owner  = THIS_MODULE,
461         },
462
463         .read   = cmos_nvram_read,
464         .write  = cmos_nvram_write,
465         /* size gets set up later */
466 };
467
468 /*----------------------------------------------------------------*/
469
470 static struct cmos_rtc  cmos_rtc;
471
472 static irqreturn_t cmos_interrupt(int irq, void *p)
473 {
474         u8              irqstat;
475         u8              rtc_control;
476
477         spin_lock(&rtc_lock);
478         irqstat = CMOS_READ(RTC_INTR_FLAGS);
479         rtc_control = CMOS_READ(RTC_CONTROL);
480         irqstat &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
481
482         /* All Linux RTC alarms should be treated as if they were oneshot.
483          * Similar code may be needed in system wakeup paths, in case the
484          * alarm woke the system.
485          */
486         if (irqstat & RTC_AIE) {
487                 rtc_control &= ~RTC_AIE;
488                 CMOS_WRITE(rtc_control, RTC_CONTROL);
489                 CMOS_READ(RTC_INTR_FLAGS);
490         }
491         spin_unlock(&rtc_lock);
492
493         if (is_intr(irqstat)) {
494                 rtc_update_irq(p, 1, irqstat);
495                 return IRQ_HANDLED;
496         } else
497                 return IRQ_NONE;
498 }
499
500 #ifdef  CONFIG_PNP
501 #define INITSECTION
502
503 #else
504 #define INITSECTION     __init
505 #endif
506
507 static int INITSECTION
508 cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
509 {
510         struct cmos_rtc_board_info      *info = dev->platform_data;
511         int                             retval = 0;
512         unsigned char                   rtc_control;
513         unsigned                        address_space;
514
515         /* there can be only one ... */
516         if (cmos_rtc.dev)
517                 return -EBUSY;
518
519         if (!ports)
520                 return -ENODEV;
521
522         /* Claim I/O ports ASAP, minimizing conflict with legacy driver.
523          *
524          * REVISIT non-x86 systems may instead use memory space resources
525          * (needing ioremap etc), not i/o space resources like this ...
526          */
527         ports = request_region(ports->start,
528                         ports->end + 1 - ports->start,
529                         driver_name);
530         if (!ports) {
531                 dev_dbg(dev, "i/o registers already in use\n");
532                 return -EBUSY;
533         }
534
535         cmos_rtc.irq = rtc_irq;
536         cmos_rtc.iomem = ports;
537
538         /* Heuristic to deduce NVRAM size ... do what the legacy NVRAM
539          * driver did, but don't reject unknown configs.   Old hardware
540          * won't address 128 bytes, and for now we ignore the way newer
541          * chips can address 256 bytes (using two more i/o ports).
542          */
543 #if     defined(CONFIG_ATARI)
544         address_space = 64;
545 #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__)
546         address_space = 128;
547 #else
548 #warning Assuming 128 bytes of RTC+NVRAM address space, not 64 bytes.
549         address_space = 128;
550 #endif
551
552         /* For ACPI systems extension info comes from the FADT.  On others,
553          * board specific setup provides it as appropriate.  Systems where
554          * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and
555          * some almost-clones) can provide hooks to make that behave.
556          *
557          * Note that ACPI doesn't preclude putting these registers into
558          * "extended" areas of the chip, including some that we won't yet
559          * expect CMOS_READ and friends to handle.
560          */
561         if (info) {
562                 if (info->rtc_day_alarm && info->rtc_day_alarm < 128)
563                         cmos_rtc.day_alrm = info->rtc_day_alarm;
564                 if (info->rtc_mon_alarm && info->rtc_mon_alarm < 128)
565                         cmos_rtc.mon_alrm = info->rtc_mon_alarm;
566                 if (info->rtc_century && info->rtc_century < 128)
567                         cmos_rtc.century = info->rtc_century;
568
569                 if (info->wake_on && info->wake_off) {
570                         cmos_rtc.wake_on = info->wake_on;
571                         cmos_rtc.wake_off = info->wake_off;
572                 }
573         }
574
575         cmos_rtc.rtc = rtc_device_register(driver_name, dev,
576                                 &cmos_rtc_ops, THIS_MODULE);
577         if (IS_ERR(cmos_rtc.rtc)) {
578                 retval = PTR_ERR(cmos_rtc.rtc);
579                 goto cleanup0;
580         }
581
582         cmos_rtc.dev = dev;
583         dev_set_drvdata(dev, &cmos_rtc);
584         rename_region(ports, cmos_rtc.rtc->dev.bus_id);
585
586         spin_lock_irq(&rtc_lock);
587
588         /* force periodic irq to CMOS reset default of 1024Hz;
589          *
590          * REVISIT it's been reported that at least one x86_64 ALI mobo
591          * doesn't use 32KHz here ... for portability we might need to
592          * do something about other clock frequencies.
593          */
594         CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT);
595         cmos_rtc.rtc->irq_freq = 1024;
596
597         /* disable irqs.
598          *
599          * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
600          * allegedly some older rtcs need that to handle irqs properly
601          */
602         rtc_control = CMOS_READ(RTC_CONTROL);
603         rtc_control &= ~(RTC_PIE | RTC_AIE | RTC_UIE);
604         CMOS_WRITE(rtc_control, RTC_CONTROL);
605         CMOS_READ(RTC_INTR_FLAGS);
606
607         spin_unlock_irq(&rtc_lock);
608
609         /* FIXME teach the alarm code how to handle binary mode;
610          * <asm-generic/rtc.h> doesn't know 12-hour mode either.
611          */
612         if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY))) {
613                 dev_dbg(dev, "only 24-hr BCD mode supported\n");
614                 retval = -ENXIO;
615                 goto cleanup1;
616         }
617
618         if (is_valid_irq(rtc_irq))
619                 retval = request_irq(rtc_irq, cmos_interrupt, IRQF_DISABLED,
620                                 cmos_rtc.rtc->dev.bus_id,
621                                 cmos_rtc.rtc);
622         if (retval < 0) {
623                 dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
624                 goto cleanup1;
625         }
626
627         /* export at least the first block of NVRAM */
628         nvram.size = address_space - NVRAM_OFFSET;
629         retval = sysfs_create_bin_file(&dev->kobj, &nvram);
630         if (retval < 0) {
631                 dev_dbg(dev, "can't create nvram file? %d\n", retval);
632                 goto cleanup2;
633         }
634
635         pr_info("%s: alarms up to one %s%s\n",
636                         cmos_rtc.rtc->dev.bus_id,
637                         is_valid_irq(rtc_irq)
638                                 ?  (cmos_rtc.mon_alrm
639                                         ? "year"
640                                         : (cmos_rtc.day_alrm
641                                                 ? "month" : "day"))
642                                 : "no",
643                         cmos_rtc.century ? ", y3k" : ""
644                         );
645
646         return 0;
647
648 cleanup2:
649         if (is_valid_irq(rtc_irq))
650                 free_irq(rtc_irq, cmos_rtc.rtc);
651 cleanup1:
652         cmos_rtc.dev = NULL;
653         rtc_device_unregister(cmos_rtc.rtc);
654 cleanup0:
655         release_region(ports->start, ports->end + 1 - ports->start);
656         return retval;
657 }
658
659 static void cmos_do_shutdown(void)
660 {
661         unsigned char   rtc_control;
662
663         spin_lock_irq(&rtc_lock);
664         rtc_control = CMOS_READ(RTC_CONTROL);
665         rtc_control &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
666         CMOS_WRITE(rtc_control, RTC_CONTROL);
667         CMOS_READ(RTC_INTR_FLAGS);
668         spin_unlock_irq(&rtc_lock);
669 }
670
671 static void __exit cmos_do_remove(struct device *dev)
672 {
673         struct cmos_rtc *cmos = dev_get_drvdata(dev);
674         struct resource *ports;
675
676         cmos_do_shutdown();
677
678         sysfs_remove_bin_file(&dev->kobj, &nvram);
679
680         if (is_valid_irq(cmos->irq))
681                 free_irq(cmos->irq, cmos->rtc);
682
683         rtc_device_unregister(cmos->rtc);
684         cmos->rtc = NULL;
685
686         ports = cmos->iomem;
687         release_region(ports->start, ports->end + 1 - ports->start);
688         cmos->iomem = NULL;
689
690         cmos->dev = NULL;
691         dev_set_drvdata(dev, NULL);
692 }
693
694 #ifdef  CONFIG_PM
695
696 static int cmos_suspend(struct device *dev, pm_message_t mesg)
697 {
698         struct cmos_rtc *cmos = dev_get_drvdata(dev);
699         int             do_wake = device_may_wakeup(dev);
700         unsigned char   tmp;
701
702         /* only the alarm might be a wakeup event source */
703         spin_lock_irq(&rtc_lock);
704         cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL);
705         if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
706                 unsigned char   irqstat;
707
708                 if (do_wake)
709                         tmp &= ~(RTC_PIE|RTC_UIE);
710                 else
711                         tmp &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
712                 CMOS_WRITE(tmp, RTC_CONTROL);
713                 irqstat = CMOS_READ(RTC_INTR_FLAGS);
714                 irqstat &= (tmp & RTC_IRQMASK) | RTC_IRQF;
715                 if (is_intr(irqstat))
716                         rtc_update_irq(cmos->rtc, 1, irqstat);
717         }
718         spin_unlock_irq(&rtc_lock);
719
720         if (tmp & RTC_AIE) {
721                 cmos->enabled_wake = 1;
722                 if (cmos->wake_on)
723                         cmos->wake_on(dev);
724                 else
725                         enable_irq_wake(cmos->irq);
726         }
727
728         pr_debug("%s: suspend%s, ctrl %02x\n",
729                         cmos_rtc.rtc->dev.bus_id,
730                         (tmp & RTC_AIE) ? ", alarm may wake" : "",
731                         tmp);
732
733         return 0;
734 }
735
736 static int cmos_resume(struct device *dev)
737 {
738         struct cmos_rtc *cmos = dev_get_drvdata(dev);
739         unsigned char   tmp = cmos->suspend_ctrl;
740
741         /* re-enable any irqs previously active */
742         if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
743
744                 if (cmos->enabled_wake) {
745                         if (cmos->wake_off)
746                                 cmos->wake_off(dev);
747                         else
748                                 disable_irq_wake(cmos->irq);
749                         cmos->enabled_wake = 0;
750                 }
751
752                 spin_lock_irq(&rtc_lock);
753                 CMOS_WRITE(tmp, RTC_CONTROL);
754                 tmp = CMOS_READ(RTC_INTR_FLAGS);
755                 tmp &= (cmos->suspend_ctrl & RTC_IRQMASK) | RTC_IRQF;
756                 if (is_intr(tmp))
757                         rtc_update_irq(cmos->rtc, 1, tmp);
758                 spin_unlock_irq(&rtc_lock);
759         }
760
761         pr_debug("%s: resume, ctrl %02x\n",
762                         cmos_rtc.rtc->dev.bus_id,
763                         cmos->suspend_ctrl);
764
765
766         return 0;
767 }
768
769 #else
770 #define cmos_suspend    NULL
771 #define cmos_resume     NULL
772 #endif
773
774 /*----------------------------------------------------------------*/
775
776 /* On non-x86 systems, a "CMOS" RTC lives most naturally on platform_bus.
777  * ACPI systems always list these as PNPACPI devices, and pre-ACPI PCs
778  * probably list them in similar PNPBIOS tables; so PNP is more common.
779  *
780  * We don't use legacy "poke at the hardware" probing.  Ancient PCs that
781  * predate even PNPBIOS should set up platform_bus devices.
782  */
783
784 #ifdef  CONFIG_PNP
785
786 #include <linux/pnp.h>
787
788 static int __devinit
789 cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
790 {
791         /* REVISIT paranoia argues for a shutdown notifier, since PNP
792          * drivers can't provide shutdown() methods to disable IRQs.
793          * Or better yet, fix PNP to allow those methods...
794          */
795         if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0))
796                 /* Some machines contain a PNP entry for the RTC, but
797                  * don't define the IRQ. It should always be safe to
798                  * hardcode it in these cases
799                  */
800                 return cmos_do_probe(&pnp->dev, &pnp->res.port_resource[0], 8);
801         else
802                 return cmos_do_probe(&pnp->dev,
803                                      &pnp->res.port_resource[0],
804                                      pnp->res.irq_resource[0].start);
805 }
806
807 static void __exit cmos_pnp_remove(struct pnp_dev *pnp)
808 {
809         cmos_do_remove(&pnp->dev);
810 }
811
812 #ifdef  CONFIG_PM
813
814 static int cmos_pnp_suspend(struct pnp_dev *pnp, pm_message_t mesg)
815 {
816         return cmos_suspend(&pnp->dev, mesg);
817 }
818
819 static int cmos_pnp_resume(struct pnp_dev *pnp)
820 {
821         return cmos_resume(&pnp->dev);
822 }
823
824 #else
825 #define cmos_pnp_suspend        NULL
826 #define cmos_pnp_resume         NULL
827 #endif
828
829
830 static const struct pnp_device_id rtc_ids[] = {
831         { .id = "PNP0b00", },
832         { .id = "PNP0b01", },
833         { .id = "PNP0b02", },
834         { },
835 };
836 MODULE_DEVICE_TABLE(pnp, rtc_ids);
837
838 static struct pnp_driver cmos_pnp_driver = {
839         .name           = (char *) driver_name,
840         .id_table       = rtc_ids,
841         .probe          = cmos_pnp_probe,
842         .remove         = __exit_p(cmos_pnp_remove),
843
844         /* flag ensures resume() gets called, and stops syslog spam */
845         .flags          = PNP_DRIVER_RES_DO_NOT_CHANGE,
846         .suspend        = cmos_pnp_suspend,
847         .resume         = cmos_pnp_resume,
848 };
849
850 static int __init cmos_init(void)
851 {
852         return pnp_register_driver(&cmos_pnp_driver);
853 }
854 module_init(cmos_init);
855
856 static void __exit cmos_exit(void)
857 {
858         pnp_unregister_driver(&cmos_pnp_driver);
859 }
860 module_exit(cmos_exit);
861
862 #else   /* no PNP */
863
864 /*----------------------------------------------------------------*/
865
866 /* Platform setup should have set up an RTC device, when PNP is
867  * unavailable ... this could happen even on (older) PCs.
868  */
869
870 static int __init cmos_platform_probe(struct platform_device *pdev)
871 {
872         return cmos_do_probe(&pdev->dev,
873                         platform_get_resource(pdev, IORESOURCE_IO, 0),
874                         platform_get_irq(pdev, 0));
875 }
876
877 static int __exit cmos_platform_remove(struct platform_device *pdev)
878 {
879         cmos_do_remove(&pdev->dev);
880         return 0;
881 }
882
883 static void cmos_platform_shutdown(struct platform_device *pdev)
884 {
885         cmos_do_shutdown();
886 }
887
888 static struct platform_driver cmos_platform_driver = {
889         .remove         = __exit_p(cmos_platform_remove),
890         .shutdown       = cmos_platform_shutdown,
891         .driver = {
892                 .name           = (char *) driver_name,
893                 .suspend        = cmos_suspend,
894                 .resume         = cmos_resume,
895         }
896 };
897
898 static int __init cmos_init(void)
899 {
900         return platform_driver_probe(&cmos_platform_driver,
901                         cmos_platform_probe);
902 }
903 module_init(cmos_init);
904
905 static void __exit cmos_exit(void)
906 {
907         platform_driver_unregister(&cmos_platform_driver);
908 }
909 module_exit(cmos_exit);
910
911
912 #endif  /* !PNP */
913
914 MODULE_AUTHOR("David Brownell");
915 MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
916 MODULE_LICENSE("GPL");