David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 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 | |
David Brownell | 87ac84f | 2007-05-08 00:34:00 -0700 | [diff] [blame] | 49 | void (*wake_on)(struct device *); |
| 50 | void (*wake_off)(struct device *); |
| 51 | |
| 52 | u8 enabled_wake; |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 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 | |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 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 | |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 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) { |
Mark Lord | 615bb29 | 2007-11-03 22:04:03 -0400 | [diff] [blame^] | 123 | /* ignore upper bits on readback per ACPI spec */ |
| 124 | t->time.tm_mday = CMOS_READ(cmos->day_alrm) & 0x3f; |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 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); |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 209 | rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF; |
| 210 | if (is_intr(rtc_intr)) |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 211 | rtc_update_irq(cmos->rtc, 1, rtc_intr); |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 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); |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 229 | rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF; |
| 230 | if (is_intr(rtc_intr)) |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 231 | rtc_update_irq(cmos->rtc, 1, rtc_intr); |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | spin_unlock_irq(&rtc_lock); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Alessandro Zummo | 57deb52 | 2007-07-19 01:49:21 -0700 | [diff] [blame] | 239 | static int cmos_irq_set_freq(struct device *dev, int freq) |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 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); |
David Brownell | 97144c6 | 2007-10-16 01:28:16 -0700 | [diff] [blame] | 250 | if (f-- > 16) |
| 251 | return -EINVAL; |
| 252 | f = 16 - f; |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 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 | |
Alessandro Zummo | 57deb52 | 2007-07-19 01:49:21 -0700 | [diff] [blame] | 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 | |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 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); |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 336 | rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF; |
| 337 | if (is_intr(rtc_intr)) |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 338 | rtc_update_irq(cmos->rtc, 1, rtc_intr); |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 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, |
Alessandro Zummo | 57deb52 | 2007-07-19 01:49:21 -0700 | [diff] [blame] | 390 | .irq_set_freq = cmos_irq_set_freq, |
| 391 | .irq_set_state = cmos_irq_set_state, |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 392 | }; |
| 393 | |
| 394 | /*----------------------------------------------------------------*/ |
| 395 | |
| 396 | static struct cmos_rtc cmos_rtc; |
| 397 | |
| 398 | static irqreturn_t cmos_interrupt(int irq, void *p) |
| 399 | { |
| 400 | u8 irqstat; |
| 401 | |
| 402 | spin_lock(&rtc_lock); |
| 403 | irqstat = CMOS_READ(RTC_INTR_FLAGS); |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 404 | irqstat &= (CMOS_READ(RTC_CONTROL) & RTC_IRQMASK) | RTC_IRQF; |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 405 | spin_unlock(&rtc_lock); |
| 406 | |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 407 | if (is_intr(irqstat)) { |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 408 | rtc_update_irq(p, 1, irqstat); |
| 409 | return IRQ_HANDLED; |
| 410 | } else |
| 411 | return IRQ_NONE; |
| 412 | } |
| 413 | |
Marko Vrh | 41ac8df | 2007-05-08 00:34:09 -0700 | [diff] [blame] | 414 | #ifdef CONFIG_PNP |
| 415 | #define is_pnp() 1 |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 416 | #define INITSECTION |
| 417 | |
| 418 | #else |
Marko Vrh | 41ac8df | 2007-05-08 00:34:09 -0700 | [diff] [blame] | 419 | #define is_pnp() 0 |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 420 | #define INITSECTION __init |
| 421 | #endif |
| 422 | |
| 423 | static int INITSECTION |
| 424 | cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq) |
| 425 | { |
| 426 | struct cmos_rtc_board_info *info = dev->platform_data; |
| 427 | int retval = 0; |
| 428 | unsigned char rtc_control; |
| 429 | |
| 430 | /* there can be only one ... */ |
| 431 | if (cmos_rtc.dev) |
| 432 | return -EBUSY; |
| 433 | |
| 434 | if (!ports) |
| 435 | return -ENODEV; |
| 436 | |
David Brownell | 05440df | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 437 | /* Claim I/O ports ASAP, minimizing conflict with legacy driver. |
| 438 | * |
| 439 | * REVISIT non-x86 systems may instead use memory space resources |
| 440 | * (needing ioremap etc), not i/o space resources like this ... |
| 441 | */ |
| 442 | ports = request_region(ports->start, |
| 443 | ports->end + 1 - ports->start, |
| 444 | driver_name); |
| 445 | if (!ports) { |
| 446 | dev_dbg(dev, "i/o registers already in use\n"); |
| 447 | return -EBUSY; |
| 448 | } |
| 449 | |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 450 | cmos_rtc.irq = rtc_irq; |
| 451 | cmos_rtc.iomem = ports; |
| 452 | |
David Brownell | 87ac84f | 2007-05-08 00:34:00 -0700 | [diff] [blame] | 453 | /* For ACPI systems extension info comes from the FADT. On others, |
| 454 | * board specific setup provides it as appropriate. Systems where |
| 455 | * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and |
| 456 | * some almost-clones) can provide hooks to make that behave. |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 457 | */ |
| 458 | if (info) { |
| 459 | cmos_rtc.day_alrm = info->rtc_day_alarm; |
| 460 | cmos_rtc.mon_alrm = info->rtc_mon_alarm; |
| 461 | cmos_rtc.century = info->rtc_century; |
David Brownell | 87ac84f | 2007-05-08 00:34:00 -0700 | [diff] [blame] | 462 | |
| 463 | if (info->wake_on && info->wake_off) { |
| 464 | cmos_rtc.wake_on = info->wake_on; |
| 465 | cmos_rtc.wake_off = info->wake_off; |
| 466 | } |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | cmos_rtc.rtc = rtc_device_register(driver_name, dev, |
| 470 | &cmos_rtc_ops, THIS_MODULE); |
David Brownell | 05440df | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 471 | if (IS_ERR(cmos_rtc.rtc)) { |
| 472 | retval = PTR_ERR(cmos_rtc.rtc); |
| 473 | goto cleanup0; |
| 474 | } |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 475 | |
| 476 | cmos_rtc.dev = dev; |
| 477 | dev_set_drvdata(dev, &cmos_rtc); |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 478 | rename_region(ports, cmos_rtc.rtc->dev.bus_id); |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 479 | |
| 480 | spin_lock_irq(&rtc_lock); |
| 481 | |
| 482 | /* force periodic irq to CMOS reset default of 1024Hz; |
| 483 | * |
| 484 | * REVISIT it's been reported that at least one x86_64 ALI mobo |
| 485 | * doesn't use 32KHz here ... for portability we might need to |
| 486 | * do something about other clock frequencies. |
| 487 | */ |
| 488 | CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT); |
| 489 | cmos_rtc.rtc->irq_freq = 1024; |
| 490 | |
| 491 | /* disable irqs. |
| 492 | * |
| 493 | * NOTE after changing RTC_xIE bits we always read INTR_FLAGS; |
| 494 | * allegedly some older rtcs need that to handle irqs properly |
| 495 | */ |
| 496 | rtc_control = CMOS_READ(RTC_CONTROL); |
| 497 | rtc_control &= ~(RTC_PIE | RTC_AIE | RTC_UIE); |
| 498 | CMOS_WRITE(rtc_control, RTC_CONTROL); |
| 499 | CMOS_READ(RTC_INTR_FLAGS); |
| 500 | |
| 501 | spin_unlock_irq(&rtc_lock); |
| 502 | |
| 503 | /* FIXME teach the alarm code how to handle binary mode; |
| 504 | * <asm-generic/rtc.h> doesn't know 12-hour mode either. |
| 505 | */ |
| 506 | if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY))) { |
| 507 | dev_dbg(dev, "only 24-hr BCD mode supported\n"); |
| 508 | retval = -ENXIO; |
| 509 | goto cleanup1; |
| 510 | } |
| 511 | |
| 512 | if (is_valid_irq(rtc_irq)) |
| 513 | retval = request_irq(rtc_irq, cmos_interrupt, IRQF_DISABLED, |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 514 | cmos_rtc.rtc->dev.bus_id, |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 515 | cmos_rtc.rtc); |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 516 | if (retval < 0) { |
| 517 | dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq); |
| 518 | goto cleanup1; |
| 519 | } |
| 520 | |
| 521 | /* REVISIT optionally make 50 or 114 bytes NVRAM available, |
| 522 | * like rtc-ds1553, rtc-ds1742 ... this will often include |
| 523 | * registers for century, and day/month alarm. |
| 524 | */ |
| 525 | |
| 526 | pr_info("%s: alarms up to one %s%s\n", |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 527 | cmos_rtc.rtc->dev.bus_id, |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 528 | is_valid_irq(rtc_irq) |
| 529 | ? (cmos_rtc.mon_alrm |
| 530 | ? "year" |
| 531 | : (cmos_rtc.day_alrm |
| 532 | ? "month" : "day")) |
| 533 | : "no", |
| 534 | cmos_rtc.century ? ", y3k" : "" |
| 535 | ); |
| 536 | |
| 537 | return 0; |
| 538 | |
| 539 | cleanup1: |
David Brownell | 05440df | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 540 | cmos_rtc.dev = NULL; |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 541 | rtc_device_unregister(cmos_rtc.rtc); |
David Brownell | 05440df | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 542 | cleanup0: |
| 543 | release_region(ports->start, ports->end + 1 - ports->start); |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 544 | return retval; |
| 545 | } |
| 546 | |
| 547 | static void cmos_do_shutdown(void) |
| 548 | { |
| 549 | unsigned char rtc_control; |
| 550 | |
| 551 | spin_lock_irq(&rtc_lock); |
| 552 | rtc_control = CMOS_READ(RTC_CONTROL); |
| 553 | rtc_control &= ~(RTC_PIE|RTC_AIE|RTC_UIE); |
| 554 | CMOS_WRITE(rtc_control, RTC_CONTROL); |
| 555 | CMOS_READ(RTC_INTR_FLAGS); |
| 556 | spin_unlock_irq(&rtc_lock); |
| 557 | } |
| 558 | |
| 559 | static void __exit cmos_do_remove(struct device *dev) |
| 560 | { |
| 561 | struct cmos_rtc *cmos = dev_get_drvdata(dev); |
David Brownell | 05440df | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 562 | struct resource *ports; |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 563 | |
| 564 | cmos_do_shutdown(); |
| 565 | |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 566 | if (is_valid_irq(cmos->irq)) |
David Brownell | 05440df | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 567 | free_irq(cmos->irq, cmos->rtc); |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 568 | |
David Brownell | 05440df | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 569 | rtc_device_unregister(cmos->rtc); |
| 570 | cmos->rtc = NULL; |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 571 | |
David Brownell | 05440df | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 572 | ports = cmos->iomem; |
| 573 | release_region(ports->start, ports->end + 1 - ports->start); |
| 574 | cmos->iomem = NULL; |
| 575 | |
| 576 | cmos->dev = NULL; |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 577 | dev_set_drvdata(dev, NULL); |
| 578 | } |
| 579 | |
| 580 | #ifdef CONFIG_PM |
| 581 | |
| 582 | static int cmos_suspend(struct device *dev, pm_message_t mesg) |
| 583 | { |
| 584 | struct cmos_rtc *cmos = dev_get_drvdata(dev); |
| 585 | int do_wake = device_may_wakeup(dev); |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 586 | unsigned char tmp; |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 587 | |
| 588 | /* only the alarm might be a wakeup event source */ |
| 589 | spin_lock_irq(&rtc_lock); |
| 590 | cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL); |
| 591 | if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) { |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 592 | unsigned char irqstat; |
| 593 | |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 594 | if (do_wake) |
| 595 | tmp &= ~(RTC_PIE|RTC_UIE); |
| 596 | else |
| 597 | tmp &= ~(RTC_PIE|RTC_AIE|RTC_UIE); |
| 598 | CMOS_WRITE(tmp, RTC_CONTROL); |
| 599 | irqstat = CMOS_READ(RTC_INTR_FLAGS); |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 600 | irqstat &= (tmp & RTC_IRQMASK) | RTC_IRQF; |
| 601 | if (is_intr(irqstat)) |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 602 | rtc_update_irq(cmos->rtc, 1, irqstat); |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 603 | } |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 604 | spin_unlock_irq(&rtc_lock); |
| 605 | |
David Brownell | 87ac84f | 2007-05-08 00:34:00 -0700 | [diff] [blame] | 606 | if (tmp & RTC_AIE) { |
| 607 | cmos->enabled_wake = 1; |
| 608 | if (cmos->wake_on) |
| 609 | cmos->wake_on(dev); |
| 610 | else |
| 611 | enable_irq_wake(cmos->irq); |
| 612 | } |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 613 | |
| 614 | pr_debug("%s: suspend%s, ctrl %02x\n", |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 615 | cmos_rtc.rtc->dev.bus_id, |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 616 | (tmp & RTC_AIE) ? ", alarm may wake" : "", |
| 617 | tmp); |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static int cmos_resume(struct device *dev) |
| 623 | { |
| 624 | struct cmos_rtc *cmos = dev_get_drvdata(dev); |
| 625 | unsigned char tmp = cmos->suspend_ctrl; |
| 626 | |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 627 | /* re-enable any irqs previously active */ |
| 628 | if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) { |
| 629 | |
David Brownell | 87ac84f | 2007-05-08 00:34:00 -0700 | [diff] [blame] | 630 | if (cmos->enabled_wake) { |
| 631 | if (cmos->wake_off) |
| 632 | cmos->wake_off(dev); |
| 633 | else |
| 634 | disable_irq_wake(cmos->irq); |
| 635 | cmos->enabled_wake = 0; |
| 636 | } |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 637 | |
| 638 | spin_lock_irq(&rtc_lock); |
| 639 | CMOS_WRITE(tmp, RTC_CONTROL); |
| 640 | tmp = CMOS_READ(RTC_INTR_FLAGS); |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 641 | tmp &= (cmos->suspend_ctrl & RTC_IRQMASK) | RTC_IRQF; |
| 642 | if (is_intr(tmp)) |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 643 | rtc_update_irq(cmos->rtc, 1, tmp); |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 644 | spin_unlock_irq(&rtc_lock); |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | pr_debug("%s: resume, ctrl %02x\n", |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 648 | cmos_rtc.rtc->dev.bus_id, |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 649 | cmos->suspend_ctrl); |
| 650 | |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | #else |
| 656 | #define cmos_suspend NULL |
| 657 | #define cmos_resume NULL |
| 658 | #endif |
| 659 | |
| 660 | /*----------------------------------------------------------------*/ |
| 661 | |
| 662 | /* The "CMOS" RTC normally lives on the platform_bus. On ACPI systems, |
David Brownell | 05440df | 2007-10-16 01:28:21 -0700 | [diff] [blame] | 663 | * the device node will always be created as a PNPACPI device. Plus |
| 664 | * pre-ACPI PCs probably list it in the PNPBIOS tables. |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 665 | */ |
| 666 | |
Marko Vrh | 41ac8df | 2007-05-08 00:34:09 -0700 | [diff] [blame] | 667 | #ifdef CONFIG_PNP |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 668 | |
| 669 | #include <linux/pnp.h> |
| 670 | |
| 671 | static int __devinit |
| 672 | cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) |
| 673 | { |
| 674 | /* REVISIT paranoia argues for a shutdown notifier, since PNP |
| 675 | * drivers can't provide shutdown() methods to disable IRQs. |
| 676 | * Or better yet, fix PNP to allow those methods... |
| 677 | */ |
Matthew Garrett | 6cd8fa8 | 2007-06-01 00:46:51 -0700 | [diff] [blame] | 678 | if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0)) |
| 679 | /* Some machines contain a PNP entry for the RTC, but |
| 680 | * don't define the IRQ. It should always be safe to |
| 681 | * hardcode it in these cases |
| 682 | */ |
| 683 | return cmos_do_probe(&pnp->dev, &pnp->res.port_resource[0], 8); |
| 684 | else |
| 685 | return cmos_do_probe(&pnp->dev, |
| 686 | &pnp->res.port_resource[0], |
| 687 | pnp->res.irq_resource[0].start); |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static void __exit cmos_pnp_remove(struct pnp_dev *pnp) |
| 691 | { |
| 692 | cmos_do_remove(&pnp->dev); |
| 693 | } |
| 694 | |
| 695 | #ifdef CONFIG_PM |
| 696 | |
| 697 | static int cmos_pnp_suspend(struct pnp_dev *pnp, pm_message_t mesg) |
| 698 | { |
| 699 | return cmos_suspend(&pnp->dev, mesg); |
| 700 | } |
| 701 | |
| 702 | static int cmos_pnp_resume(struct pnp_dev *pnp) |
| 703 | { |
| 704 | return cmos_resume(&pnp->dev); |
| 705 | } |
| 706 | |
| 707 | #else |
| 708 | #define cmos_pnp_suspend NULL |
| 709 | #define cmos_pnp_resume NULL |
| 710 | #endif |
| 711 | |
| 712 | |
| 713 | static const struct pnp_device_id rtc_ids[] = { |
| 714 | { .id = "PNP0b00", }, |
| 715 | { .id = "PNP0b01", }, |
| 716 | { .id = "PNP0b02", }, |
| 717 | { }, |
| 718 | }; |
| 719 | MODULE_DEVICE_TABLE(pnp, rtc_ids); |
| 720 | |
| 721 | static struct pnp_driver cmos_pnp_driver = { |
| 722 | .name = (char *) driver_name, |
| 723 | .id_table = rtc_ids, |
| 724 | .probe = cmos_pnp_probe, |
| 725 | .remove = __exit_p(cmos_pnp_remove), |
| 726 | |
| 727 | /* flag ensures resume() gets called, and stops syslog spam */ |
| 728 | .flags = PNP_DRIVER_RES_DO_NOT_CHANGE, |
| 729 | .suspend = cmos_pnp_suspend, |
| 730 | .resume = cmos_pnp_resume, |
| 731 | }; |
| 732 | |
| 733 | static int __init cmos_init(void) |
| 734 | { |
| 735 | return pnp_register_driver(&cmos_pnp_driver); |
| 736 | } |
| 737 | module_init(cmos_init); |
| 738 | |
| 739 | static void __exit cmos_exit(void) |
| 740 | { |
| 741 | pnp_unregister_driver(&cmos_pnp_driver); |
| 742 | } |
| 743 | module_exit(cmos_exit); |
| 744 | |
Marko Vrh | 41ac8df | 2007-05-08 00:34:09 -0700 | [diff] [blame] | 745 | #else /* no PNP */ |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 746 | |
| 747 | /*----------------------------------------------------------------*/ |
| 748 | |
Marko Vrh | 41ac8df | 2007-05-08 00:34:09 -0700 | [diff] [blame] | 749 | /* Platform setup should have set up an RTC device, when PNP is |
David Brownell | bcd9b89 | 2007-04-01 23:49:47 -0700 | [diff] [blame] | 750 | * unavailable ... this could happen even on (older) PCs. |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 751 | */ |
| 752 | |
| 753 | static int __init cmos_platform_probe(struct platform_device *pdev) |
| 754 | { |
| 755 | return cmos_do_probe(&pdev->dev, |
| 756 | platform_get_resource(pdev, IORESOURCE_IO, 0), |
| 757 | platform_get_irq(pdev, 0)); |
| 758 | } |
| 759 | |
| 760 | static int __exit cmos_platform_remove(struct platform_device *pdev) |
| 761 | { |
| 762 | cmos_do_remove(&pdev->dev); |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | static void cmos_platform_shutdown(struct platform_device *pdev) |
| 767 | { |
| 768 | cmos_do_shutdown(); |
| 769 | } |
| 770 | |
| 771 | static struct platform_driver cmos_platform_driver = { |
| 772 | .remove = __exit_p(cmos_platform_remove), |
| 773 | .shutdown = cmos_platform_shutdown, |
| 774 | .driver = { |
| 775 | .name = (char *) driver_name, |
| 776 | .suspend = cmos_suspend, |
| 777 | .resume = cmos_resume, |
| 778 | } |
| 779 | }; |
| 780 | |
| 781 | static int __init cmos_init(void) |
| 782 | { |
| 783 | return platform_driver_probe(&cmos_platform_driver, |
| 784 | cmos_platform_probe); |
| 785 | } |
| 786 | module_init(cmos_init); |
| 787 | |
| 788 | static void __exit cmos_exit(void) |
| 789 | { |
| 790 | platform_driver_unregister(&cmos_platform_driver); |
| 791 | } |
| 792 | module_exit(cmos_exit); |
| 793 | |
| 794 | |
Marko Vrh | 41ac8df | 2007-05-08 00:34:09 -0700 | [diff] [blame] | 795 | #endif /* !PNP */ |
David Brownell | 7be2c7c | 2007-02-10 01:46:02 -0800 | [diff] [blame] | 796 | |
| 797 | MODULE_AUTHOR("David Brownell"); |
| 798 | MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs"); |
| 799 | MODULE_LICENSE("GPL"); |