rtc-cmos.c 20 KB

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