rtc-cmos.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. * The original "cmos clock" chip was an MC146818 chip, now obsolete.
  14. * That defined the register interface now provided by all PCs, some
  15. * non-PC systems, and incorporated into ACPI. Modern PC chipsets
  16. * integrate an MC146818 clone in their southbridge, and boards use
  17. * that instead of discrete clones like the DS12887 or M48T86. There
  18. * are also clones that connect using the LPC bus.
  19. *
  20. * That register API is also used directly by various other drivers
  21. * (notably for integrated NVRAM), infrastructure (x86 has code to
  22. * bypass the RTC framework, directly reading the RTC during boot
  23. * and updating minutes/seconds for systems using NTP synch) and
  24. * utilities (like userspace 'hwclock', if no /dev node exists).
  25. *
  26. * So **ALL** calls to CMOS_READ and CMOS_WRITE must be done with
  27. * interrupts disabled, holding the global rtc_lock, to exclude those
  28. * other drivers and utilities on correctly configured systems.
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/mod_devicetable.h>
  37. /* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */
  38. #include <asm-generic/rtc.h>
  39. struct cmos_rtc {
  40. struct rtc_device *rtc;
  41. struct device *dev;
  42. int irq;
  43. struct resource *iomem;
  44. void (*wake_on)(struct device *);
  45. void (*wake_off)(struct device *);
  46. u8 enabled_wake;
  47. u8 suspend_ctrl;
  48. /* newer hardware extends the original register set */
  49. u8 day_alrm;
  50. u8 mon_alrm;
  51. u8 century;
  52. };
  53. /* both platform and pnp busses use negative numbers for invalid irqs */
  54. #define is_valid_irq(n) ((n) >= 0)
  55. static const char driver_name[] = "rtc_cmos";
  56. /* The RTC_INTR register may have e.g. RTC_PF set even if RTC_PIE is clear;
  57. * always mask it against the irq enable bits in RTC_CONTROL. Bit values
  58. * are the same: PF==PIE, AF=AIE, UF=UIE; so RTC_IRQMASK works with both.
  59. */
  60. #define RTC_IRQMASK (RTC_PF | RTC_AF | RTC_UF)
  61. static inline int is_intr(u8 rtc_intr)
  62. {
  63. if (!(rtc_intr & RTC_IRQF))
  64. return 0;
  65. return rtc_intr & RTC_IRQMASK;
  66. }
  67. /*----------------------------------------------------------------*/
  68. static int cmos_read_time(struct device *dev, struct rtc_time *t)
  69. {
  70. /* REVISIT: if the clock has a "century" register, use
  71. * that instead of the heuristic in get_rtc_time().
  72. * That'll make Y3K compatility (year > 2070) easy!
  73. */
  74. get_rtc_time(t);
  75. return 0;
  76. }
  77. static int cmos_set_time(struct device *dev, struct rtc_time *t)
  78. {
  79. /* REVISIT: set the "century" register if available
  80. *
  81. * NOTE: this ignores the issue whereby updating the seconds
  82. * takes effect exactly 500ms after we write the register.
  83. * (Also queueing and other delays before we get this far.)
  84. */
  85. return set_rtc_time(t);
  86. }
  87. static int cmos_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  88. {
  89. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  90. unsigned char rtc_control;
  91. if (!is_valid_irq(cmos->irq))
  92. return -EIO;
  93. /* Basic alarms only support hour, minute, and seconds fields.
  94. * Some also support day and month, for alarms up to a year in
  95. * the future.
  96. */
  97. t->time.tm_mday = -1;
  98. t->time.tm_mon = -1;
  99. spin_lock_irq(&rtc_lock);
  100. t->time.tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
  101. t->time.tm_min = CMOS_READ(RTC_MINUTES_ALARM);
  102. t->time.tm_hour = CMOS_READ(RTC_HOURS_ALARM);
  103. if (cmos->day_alrm) {
  104. t->time.tm_mday = CMOS_READ(cmos->day_alrm);
  105. if (!t->time.tm_mday)
  106. t->time.tm_mday = -1;
  107. if (cmos->mon_alrm) {
  108. t->time.tm_mon = CMOS_READ(cmos->mon_alrm);
  109. if (!t->time.tm_mon)
  110. t->time.tm_mon = -1;
  111. }
  112. }
  113. rtc_control = CMOS_READ(RTC_CONTROL);
  114. spin_unlock_irq(&rtc_lock);
  115. /* REVISIT this assumes PC style usage: always BCD */
  116. if (((unsigned)t->time.tm_sec) < 0x60)
  117. t->time.tm_sec = BCD2BIN(t->time.tm_sec);
  118. else
  119. t->time.tm_sec = -1;
  120. if (((unsigned)t->time.tm_min) < 0x60)
  121. t->time.tm_min = BCD2BIN(t->time.tm_min);
  122. else
  123. t->time.tm_min = -1;
  124. if (((unsigned)t->time.tm_hour) < 0x24)
  125. t->time.tm_hour = BCD2BIN(t->time.tm_hour);
  126. else
  127. t->time.tm_hour = -1;
  128. if (cmos->day_alrm) {
  129. if (((unsigned)t->time.tm_mday) <= 0x31)
  130. t->time.tm_mday = BCD2BIN(t->time.tm_mday);
  131. else
  132. t->time.tm_mday = -1;
  133. if (cmos->mon_alrm) {
  134. if (((unsigned)t->time.tm_mon) <= 0x12)
  135. t->time.tm_mon = BCD2BIN(t->time.tm_mon) - 1;
  136. else
  137. t->time.tm_mon = -1;
  138. }
  139. }
  140. t->time.tm_year = -1;
  141. t->enabled = !!(rtc_control & RTC_AIE);
  142. t->pending = 0;
  143. return 0;
  144. }
  145. static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  146. {
  147. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  148. unsigned char mon, mday, hrs, min, sec;
  149. unsigned char rtc_control, rtc_intr;
  150. if (!is_valid_irq(cmos->irq))
  151. return -EIO;
  152. /* REVISIT this assumes PC style usage: always BCD */
  153. /* Writing 0xff means "don't care" or "match all". */
  154. mon = t->time.tm_mon;
  155. mon = (mon < 12) ? BIN2BCD(mon) : 0xff;
  156. mon++;
  157. mday = t->time.tm_mday;
  158. mday = (mday >= 1 && mday <= 31) ? BIN2BCD(mday) : 0xff;
  159. hrs = t->time.tm_hour;
  160. hrs = (hrs < 24) ? BIN2BCD(hrs) : 0xff;
  161. min = t->time.tm_min;
  162. min = (min < 60) ? BIN2BCD(min) : 0xff;
  163. sec = t->time.tm_sec;
  164. sec = (sec < 60) ? BIN2BCD(sec) : 0xff;
  165. spin_lock_irq(&rtc_lock);
  166. /* next rtc irq must not be from previous alarm setting */
  167. rtc_control = CMOS_READ(RTC_CONTROL);
  168. rtc_control &= ~RTC_AIE;
  169. CMOS_WRITE(rtc_control, RTC_CONTROL);
  170. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  171. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  172. if (is_intr(rtc_intr))
  173. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  174. /* update alarm */
  175. CMOS_WRITE(hrs, RTC_HOURS_ALARM);
  176. CMOS_WRITE(min, RTC_MINUTES_ALARM);
  177. CMOS_WRITE(sec, RTC_SECONDS_ALARM);
  178. /* the system may support an "enhanced" alarm */
  179. if (cmos->day_alrm) {
  180. CMOS_WRITE(mday, cmos->day_alrm);
  181. if (cmos->mon_alrm)
  182. CMOS_WRITE(mon, cmos->mon_alrm);
  183. }
  184. if (t->enabled) {
  185. rtc_control |= RTC_AIE;
  186. CMOS_WRITE(rtc_control, RTC_CONTROL);
  187. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  188. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  189. if (is_intr(rtc_intr))
  190. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  191. }
  192. spin_unlock_irq(&rtc_lock);
  193. return 0;
  194. }
  195. static int cmos_irq_set_freq(struct device *dev, int freq)
  196. {
  197. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  198. int f;
  199. unsigned long flags;
  200. if (!is_valid_irq(cmos->irq))
  201. return -ENXIO;
  202. /* 0 = no irqs; 1 = 2^15 Hz ... 15 = 2^0 Hz */
  203. f = ffs(freq);
  204. if (f != 0) {
  205. if (f-- > 16 || freq != (1 << f))
  206. return -EINVAL;
  207. f = 16 - f;
  208. }
  209. spin_lock_irqsave(&rtc_lock, flags);
  210. CMOS_WRITE(RTC_REF_CLCK_32KHZ | f, RTC_FREQ_SELECT);
  211. spin_unlock_irqrestore(&rtc_lock, flags);
  212. return 0;
  213. }
  214. static int cmos_irq_set_state(struct device *dev, int enabled)
  215. {
  216. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  217. unsigned char rtc_control, rtc_intr;
  218. unsigned long flags;
  219. if (!is_valid_irq(cmos->irq))
  220. return -ENXIO;
  221. spin_lock_irqsave(&rtc_lock, flags);
  222. rtc_control = CMOS_READ(RTC_CONTROL);
  223. if (enabled)
  224. rtc_control |= RTC_PIE;
  225. else
  226. rtc_control &= ~RTC_PIE;
  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. spin_unlock_irqrestore(&rtc_lock, flags);
  233. return 0;
  234. }
  235. #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
  236. static int
  237. cmos_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  238. {
  239. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  240. unsigned char rtc_control, rtc_intr;
  241. unsigned long flags;
  242. switch (cmd) {
  243. case RTC_AIE_OFF:
  244. case RTC_AIE_ON:
  245. case RTC_UIE_OFF:
  246. case RTC_UIE_ON:
  247. case RTC_PIE_OFF:
  248. case RTC_PIE_ON:
  249. if (!is_valid_irq(cmos->irq))
  250. return -EINVAL;
  251. break;
  252. default:
  253. return -ENOIOCTLCMD;
  254. }
  255. spin_lock_irqsave(&rtc_lock, flags);
  256. rtc_control = CMOS_READ(RTC_CONTROL);
  257. switch (cmd) {
  258. case RTC_AIE_OFF: /* alarm off */
  259. rtc_control &= ~RTC_AIE;
  260. break;
  261. case RTC_AIE_ON: /* alarm on */
  262. rtc_control |= RTC_AIE;
  263. break;
  264. case RTC_UIE_OFF: /* update off */
  265. rtc_control &= ~RTC_UIE;
  266. break;
  267. case RTC_UIE_ON: /* update on */
  268. rtc_control |= RTC_UIE;
  269. break;
  270. case RTC_PIE_OFF: /* periodic off */
  271. rtc_control &= ~RTC_PIE;
  272. break;
  273. case RTC_PIE_ON: /* periodic on */
  274. rtc_control |= RTC_PIE;
  275. break;
  276. }
  277. CMOS_WRITE(rtc_control, RTC_CONTROL);
  278. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  279. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  280. if (is_intr(rtc_intr))
  281. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  282. spin_unlock_irqrestore(&rtc_lock, flags);
  283. return 0;
  284. }
  285. #else
  286. #define cmos_rtc_ioctl NULL
  287. #endif
  288. #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
  289. static int cmos_procfs(struct device *dev, struct seq_file *seq)
  290. {
  291. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  292. unsigned char rtc_control, valid;
  293. spin_lock_irq(&rtc_lock);
  294. rtc_control = CMOS_READ(RTC_CONTROL);
  295. valid = CMOS_READ(RTC_VALID);
  296. spin_unlock_irq(&rtc_lock);
  297. /* NOTE: at least ICH6 reports battery status using a different
  298. * (non-RTC) bit; and SQWE is ignored on many current systems.
  299. */
  300. return seq_printf(seq,
  301. "periodic_IRQ\t: %s\n"
  302. "update_IRQ\t: %s\n"
  303. // "square_wave\t: %s\n"
  304. // "BCD\t\t: %s\n"
  305. "DST_enable\t: %s\n"
  306. "periodic_freq\t: %d\n"
  307. "batt_status\t: %s\n",
  308. (rtc_control & RTC_PIE) ? "yes" : "no",
  309. (rtc_control & RTC_UIE) ? "yes" : "no",
  310. // (rtc_control & RTC_SQWE) ? "yes" : "no",
  311. // (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
  312. (rtc_control & RTC_DST_EN) ? "yes" : "no",
  313. cmos->rtc->irq_freq,
  314. (valid & RTC_VRT) ? "okay" : "dead");
  315. }
  316. #else
  317. #define cmos_procfs NULL
  318. #endif
  319. static const struct rtc_class_ops cmos_rtc_ops = {
  320. .ioctl = cmos_rtc_ioctl,
  321. .read_time = cmos_read_time,
  322. .set_time = cmos_set_time,
  323. .read_alarm = cmos_read_alarm,
  324. .set_alarm = cmos_set_alarm,
  325. .proc = cmos_procfs,
  326. .irq_set_freq = cmos_irq_set_freq,
  327. .irq_set_state = cmos_irq_set_state,
  328. };
  329. /*----------------------------------------------------------------*/
  330. static struct cmos_rtc cmos_rtc;
  331. static irqreturn_t cmos_interrupt(int irq, void *p)
  332. {
  333. u8 irqstat;
  334. spin_lock(&rtc_lock);
  335. irqstat = CMOS_READ(RTC_INTR_FLAGS);
  336. irqstat &= (CMOS_READ(RTC_CONTROL) & RTC_IRQMASK) | RTC_IRQF;
  337. spin_unlock(&rtc_lock);
  338. if (is_intr(irqstat)) {
  339. rtc_update_irq(p, 1, irqstat);
  340. return IRQ_HANDLED;
  341. } else
  342. return IRQ_NONE;
  343. }
  344. #ifdef CONFIG_PNP
  345. #define is_pnp() 1
  346. #define INITSECTION
  347. #else
  348. #define is_pnp() 0
  349. #define INITSECTION __init
  350. #endif
  351. static int INITSECTION
  352. cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
  353. {
  354. struct cmos_rtc_board_info *info = dev->platform_data;
  355. int retval = 0;
  356. unsigned char rtc_control;
  357. /* there can be only one ... */
  358. if (cmos_rtc.dev)
  359. return -EBUSY;
  360. if (!ports)
  361. return -ENODEV;
  362. cmos_rtc.irq = rtc_irq;
  363. cmos_rtc.iomem = ports;
  364. /* For ACPI systems extension info comes from the FADT. On others,
  365. * board specific setup provides it as appropriate. Systems where
  366. * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and
  367. * some almost-clones) can provide hooks to make that behave.
  368. */
  369. if (info) {
  370. cmos_rtc.day_alrm = info->rtc_day_alarm;
  371. cmos_rtc.mon_alrm = info->rtc_mon_alarm;
  372. cmos_rtc.century = info->rtc_century;
  373. if (info->wake_on && info->wake_off) {
  374. cmos_rtc.wake_on = info->wake_on;
  375. cmos_rtc.wake_off = info->wake_off;
  376. }
  377. }
  378. cmos_rtc.rtc = rtc_device_register(driver_name, dev,
  379. &cmos_rtc_ops, THIS_MODULE);
  380. if (IS_ERR(cmos_rtc.rtc))
  381. return PTR_ERR(cmos_rtc.rtc);
  382. cmos_rtc.dev = dev;
  383. dev_set_drvdata(dev, &cmos_rtc);
  384. /* platform and pnp busses handle resources incompatibly.
  385. *
  386. * REVISIT for non-x86 systems we may need to handle io memory
  387. * resources: ioremap them, and request_mem_region().
  388. */
  389. if (is_pnp()) {
  390. retval = request_resource(&ioport_resource, ports);
  391. if (retval < 0) {
  392. dev_dbg(dev, "i/o registers already in use\n");
  393. goto cleanup0;
  394. }
  395. }
  396. rename_region(ports, cmos_rtc.rtc->dev.bus_id);
  397. spin_lock_irq(&rtc_lock);
  398. /* force periodic irq to CMOS reset default of 1024Hz;
  399. *
  400. * REVISIT it's been reported that at least one x86_64 ALI mobo
  401. * doesn't use 32KHz here ... for portability we might need to
  402. * do something about other clock frequencies.
  403. */
  404. CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT);
  405. cmos_rtc.rtc->irq_freq = 1024;
  406. /* disable irqs.
  407. *
  408. * NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
  409. * allegedly some older rtcs need that to handle irqs properly
  410. */
  411. rtc_control = CMOS_READ(RTC_CONTROL);
  412. rtc_control &= ~(RTC_PIE | RTC_AIE | RTC_UIE);
  413. CMOS_WRITE(rtc_control, RTC_CONTROL);
  414. CMOS_READ(RTC_INTR_FLAGS);
  415. spin_unlock_irq(&rtc_lock);
  416. /* FIXME teach the alarm code how to handle binary mode;
  417. * <asm-generic/rtc.h> doesn't know 12-hour mode either.
  418. */
  419. if (!(rtc_control & RTC_24H) || (rtc_control & (RTC_DM_BINARY))) {
  420. dev_dbg(dev, "only 24-hr BCD mode supported\n");
  421. retval = -ENXIO;
  422. goto cleanup1;
  423. }
  424. if (is_valid_irq(rtc_irq))
  425. retval = request_irq(rtc_irq, cmos_interrupt, IRQF_DISABLED,
  426. cmos_rtc.rtc->dev.bus_id,
  427. cmos_rtc.rtc);
  428. if (retval < 0) {
  429. dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
  430. goto cleanup1;
  431. }
  432. /* REVISIT optionally make 50 or 114 bytes NVRAM available,
  433. * like rtc-ds1553, rtc-ds1742 ... this will often include
  434. * registers for century, and day/month alarm.
  435. */
  436. pr_info("%s: alarms up to one %s%s\n",
  437. cmos_rtc.rtc->dev.bus_id,
  438. is_valid_irq(rtc_irq)
  439. ? (cmos_rtc.mon_alrm
  440. ? "year"
  441. : (cmos_rtc.day_alrm
  442. ? "month" : "day"))
  443. : "no",
  444. cmos_rtc.century ? ", y3k" : ""
  445. );
  446. return 0;
  447. cleanup1:
  448. rename_region(ports, NULL);
  449. cleanup0:
  450. rtc_device_unregister(cmos_rtc.rtc);
  451. return retval;
  452. }
  453. static void cmos_do_shutdown(void)
  454. {
  455. unsigned char rtc_control;
  456. spin_lock_irq(&rtc_lock);
  457. rtc_control = CMOS_READ(RTC_CONTROL);
  458. rtc_control &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
  459. CMOS_WRITE(rtc_control, RTC_CONTROL);
  460. CMOS_READ(RTC_INTR_FLAGS);
  461. spin_unlock_irq(&rtc_lock);
  462. }
  463. static void __exit cmos_do_remove(struct device *dev)
  464. {
  465. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  466. cmos_do_shutdown();
  467. if (is_pnp())
  468. release_resource(cmos->iomem);
  469. rename_region(cmos->iomem, NULL);
  470. if (is_valid_irq(cmos->irq))
  471. free_irq(cmos->irq, cmos_rtc.rtc);
  472. rtc_device_unregister(cmos_rtc.rtc);
  473. cmos_rtc.dev = NULL;
  474. dev_set_drvdata(dev, NULL);
  475. }
  476. #ifdef CONFIG_PM
  477. static int cmos_suspend(struct device *dev, pm_message_t mesg)
  478. {
  479. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  480. int do_wake = device_may_wakeup(dev);
  481. unsigned char tmp;
  482. /* only the alarm might be a wakeup event source */
  483. spin_lock_irq(&rtc_lock);
  484. cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL);
  485. if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
  486. unsigned char irqstat;
  487. if (do_wake)
  488. tmp &= ~(RTC_PIE|RTC_UIE);
  489. else
  490. tmp &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
  491. CMOS_WRITE(tmp, RTC_CONTROL);
  492. irqstat = CMOS_READ(RTC_INTR_FLAGS);
  493. irqstat &= (tmp & RTC_IRQMASK) | RTC_IRQF;
  494. if (is_intr(irqstat))
  495. rtc_update_irq(cmos->rtc, 1, irqstat);
  496. }
  497. spin_unlock_irq(&rtc_lock);
  498. if (tmp & RTC_AIE) {
  499. cmos->enabled_wake = 1;
  500. if (cmos->wake_on)
  501. cmos->wake_on(dev);
  502. else
  503. enable_irq_wake(cmos->irq);
  504. }
  505. pr_debug("%s: suspend%s, ctrl %02x\n",
  506. cmos_rtc.rtc->dev.bus_id,
  507. (tmp & RTC_AIE) ? ", alarm may wake" : "",
  508. tmp);
  509. return 0;
  510. }
  511. static int cmos_resume(struct device *dev)
  512. {
  513. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  514. unsigned char tmp = cmos->suspend_ctrl;
  515. /* re-enable any irqs previously active */
  516. if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
  517. if (cmos->enabled_wake) {
  518. if (cmos->wake_off)
  519. cmos->wake_off(dev);
  520. else
  521. disable_irq_wake(cmos->irq);
  522. cmos->enabled_wake = 0;
  523. }
  524. spin_lock_irq(&rtc_lock);
  525. CMOS_WRITE(tmp, RTC_CONTROL);
  526. tmp = CMOS_READ(RTC_INTR_FLAGS);
  527. tmp &= (cmos->suspend_ctrl & RTC_IRQMASK) | RTC_IRQF;
  528. if (is_intr(tmp))
  529. rtc_update_irq(cmos->rtc, 1, tmp);
  530. spin_unlock_irq(&rtc_lock);
  531. }
  532. pr_debug("%s: resume, ctrl %02x\n",
  533. cmos_rtc.rtc->dev.bus_id,
  534. cmos->suspend_ctrl);
  535. return 0;
  536. }
  537. #else
  538. #define cmos_suspend NULL
  539. #define cmos_resume NULL
  540. #endif
  541. /*----------------------------------------------------------------*/
  542. /* The "CMOS" RTC normally lives on the platform_bus. On ACPI systems,
  543. * the device node will always be created as a PNPACPI device.
  544. */
  545. #ifdef CONFIG_PNP
  546. #include <linux/pnp.h>
  547. static int __devinit
  548. cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
  549. {
  550. /* REVISIT paranoia argues for a shutdown notifier, since PNP
  551. * drivers can't provide shutdown() methods to disable IRQs.
  552. * Or better yet, fix PNP to allow those methods...
  553. */
  554. if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0))
  555. /* Some machines contain a PNP entry for the RTC, but
  556. * don't define the IRQ. It should always be safe to
  557. * hardcode it in these cases
  558. */
  559. return cmos_do_probe(&pnp->dev, &pnp->res.port_resource[0], 8);
  560. else
  561. return cmos_do_probe(&pnp->dev,
  562. &pnp->res.port_resource[0],
  563. pnp->res.irq_resource[0].start);
  564. }
  565. static void __exit cmos_pnp_remove(struct pnp_dev *pnp)
  566. {
  567. cmos_do_remove(&pnp->dev);
  568. }
  569. #ifdef CONFIG_PM
  570. static int cmos_pnp_suspend(struct pnp_dev *pnp, pm_message_t mesg)
  571. {
  572. return cmos_suspend(&pnp->dev, mesg);
  573. }
  574. static int cmos_pnp_resume(struct pnp_dev *pnp)
  575. {
  576. return cmos_resume(&pnp->dev);
  577. }
  578. #else
  579. #define cmos_pnp_suspend NULL
  580. #define cmos_pnp_resume NULL
  581. #endif
  582. static const struct pnp_device_id rtc_ids[] = {
  583. { .id = "PNP0b00", },
  584. { .id = "PNP0b01", },
  585. { .id = "PNP0b02", },
  586. { },
  587. };
  588. MODULE_DEVICE_TABLE(pnp, rtc_ids);
  589. static struct pnp_driver cmos_pnp_driver = {
  590. .name = (char *) driver_name,
  591. .id_table = rtc_ids,
  592. .probe = cmos_pnp_probe,
  593. .remove = __exit_p(cmos_pnp_remove),
  594. /* flag ensures resume() gets called, and stops syslog spam */
  595. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  596. .suspend = cmos_pnp_suspend,
  597. .resume = cmos_pnp_resume,
  598. };
  599. static int __init cmos_init(void)
  600. {
  601. return pnp_register_driver(&cmos_pnp_driver);
  602. }
  603. module_init(cmos_init);
  604. static void __exit cmos_exit(void)
  605. {
  606. pnp_unregister_driver(&cmos_pnp_driver);
  607. }
  608. module_exit(cmos_exit);
  609. #else /* no PNP */
  610. /*----------------------------------------------------------------*/
  611. /* Platform setup should have set up an RTC device, when PNP is
  612. * unavailable ... this could happen even on (older) PCs.
  613. */
  614. static int __init cmos_platform_probe(struct platform_device *pdev)
  615. {
  616. return cmos_do_probe(&pdev->dev,
  617. platform_get_resource(pdev, IORESOURCE_IO, 0),
  618. platform_get_irq(pdev, 0));
  619. }
  620. static int __exit cmos_platform_remove(struct platform_device *pdev)
  621. {
  622. cmos_do_remove(&pdev->dev);
  623. return 0;
  624. }
  625. static void cmos_platform_shutdown(struct platform_device *pdev)
  626. {
  627. cmos_do_shutdown();
  628. }
  629. static struct platform_driver cmos_platform_driver = {
  630. .remove = __exit_p(cmos_platform_remove),
  631. .shutdown = cmos_platform_shutdown,
  632. .driver = {
  633. .name = (char *) driver_name,
  634. .suspend = cmos_suspend,
  635. .resume = cmos_resume,
  636. }
  637. };
  638. static int __init cmos_init(void)
  639. {
  640. return platform_driver_probe(&cmos_platform_driver,
  641. cmos_platform_probe);
  642. }
  643. module_init(cmos_init);
  644. static void __exit cmos_exit(void)
  645. {
  646. platform_driver_unregister(&cmos_platform_driver);
  647. }
  648. module_exit(cmos_exit);
  649. #endif /* !PNP */
  650. MODULE_AUTHOR("David Brownell");
  651. MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
  652. MODULE_LICENSE("GPL");