rtc-cmos.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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-- > 16)
  205. return -EINVAL;
  206. f = 16 - f;
  207. spin_lock_irqsave(&rtc_lock, flags);
  208. CMOS_WRITE(RTC_REF_CLCK_32KHZ | f, RTC_FREQ_SELECT);
  209. spin_unlock_irqrestore(&rtc_lock, flags);
  210. return 0;
  211. }
  212. static int cmos_irq_set_state(struct device *dev, int enabled)
  213. {
  214. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  215. unsigned char rtc_control, rtc_intr;
  216. unsigned long flags;
  217. if (!is_valid_irq(cmos->irq))
  218. return -ENXIO;
  219. spin_lock_irqsave(&rtc_lock, flags);
  220. rtc_control = CMOS_READ(RTC_CONTROL);
  221. if (enabled)
  222. rtc_control |= RTC_PIE;
  223. else
  224. rtc_control &= ~RTC_PIE;
  225. CMOS_WRITE(rtc_control, RTC_CONTROL);
  226. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  227. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  228. if (is_intr(rtc_intr))
  229. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  230. spin_unlock_irqrestore(&rtc_lock, flags);
  231. return 0;
  232. }
  233. #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
  234. static int
  235. cmos_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  236. {
  237. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  238. unsigned char rtc_control, rtc_intr;
  239. unsigned long flags;
  240. switch (cmd) {
  241. case RTC_AIE_OFF:
  242. case RTC_AIE_ON:
  243. case RTC_UIE_OFF:
  244. case RTC_UIE_ON:
  245. case RTC_PIE_OFF:
  246. case RTC_PIE_ON:
  247. if (!is_valid_irq(cmos->irq))
  248. return -EINVAL;
  249. break;
  250. default:
  251. return -ENOIOCTLCMD;
  252. }
  253. spin_lock_irqsave(&rtc_lock, flags);
  254. rtc_control = CMOS_READ(RTC_CONTROL);
  255. switch (cmd) {
  256. case RTC_AIE_OFF: /* alarm off */
  257. rtc_control &= ~RTC_AIE;
  258. break;
  259. case RTC_AIE_ON: /* alarm on */
  260. rtc_control |= RTC_AIE;
  261. break;
  262. case RTC_UIE_OFF: /* update off */
  263. rtc_control &= ~RTC_UIE;
  264. break;
  265. case RTC_UIE_ON: /* update on */
  266. rtc_control |= RTC_UIE;
  267. break;
  268. case RTC_PIE_OFF: /* periodic off */
  269. rtc_control &= ~RTC_PIE;
  270. break;
  271. case RTC_PIE_ON: /* periodic on */
  272. rtc_control |= RTC_PIE;
  273. break;
  274. }
  275. CMOS_WRITE(rtc_control, RTC_CONTROL);
  276. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  277. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  278. if (is_intr(rtc_intr))
  279. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  280. spin_unlock_irqrestore(&rtc_lock, flags);
  281. return 0;
  282. }
  283. #else
  284. #define cmos_rtc_ioctl NULL
  285. #endif
  286. #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
  287. static int cmos_procfs(struct device *dev, struct seq_file *seq)
  288. {
  289. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  290. unsigned char rtc_control, valid;
  291. spin_lock_irq(&rtc_lock);
  292. rtc_control = CMOS_READ(RTC_CONTROL);
  293. valid = CMOS_READ(RTC_VALID);
  294. spin_unlock_irq(&rtc_lock);
  295. /* NOTE: at least ICH6 reports battery status using a different
  296. * (non-RTC) bit; and SQWE is ignored on many current systems.
  297. */
  298. return seq_printf(seq,
  299. "periodic_IRQ\t: %s\n"
  300. "update_IRQ\t: %s\n"
  301. // "square_wave\t: %s\n"
  302. // "BCD\t\t: %s\n"
  303. "DST_enable\t: %s\n"
  304. "periodic_freq\t: %d\n"
  305. "batt_status\t: %s\n",
  306. (rtc_control & RTC_PIE) ? "yes" : "no",
  307. (rtc_control & RTC_UIE) ? "yes" : "no",
  308. // (rtc_control & RTC_SQWE) ? "yes" : "no",
  309. // (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
  310. (rtc_control & RTC_DST_EN) ? "yes" : "no",
  311. cmos->rtc->irq_freq,
  312. (valid & RTC_VRT) ? "okay" : "dead");
  313. }
  314. #else
  315. #define cmos_procfs NULL
  316. #endif
  317. static const struct rtc_class_ops cmos_rtc_ops = {
  318. .ioctl = cmos_rtc_ioctl,
  319. .read_time = cmos_read_time,
  320. .set_time = cmos_set_time,
  321. .read_alarm = cmos_read_alarm,
  322. .set_alarm = cmos_set_alarm,
  323. .proc = cmos_procfs,
  324. .irq_set_freq = cmos_irq_set_freq,
  325. .irq_set_state = cmos_irq_set_state,
  326. };
  327. /*----------------------------------------------------------------*/
  328. static struct cmos_rtc cmos_rtc;
  329. static irqreturn_t cmos_interrupt(int irq, void *p)
  330. {
  331. u8 irqstat;
  332. spin_lock(&rtc_lock);
  333. irqstat = CMOS_READ(RTC_INTR_FLAGS);
  334. irqstat &= (CMOS_READ(RTC_CONTROL) & RTC_IRQMASK) | RTC_IRQF;
  335. spin_unlock(&rtc_lock);
  336. if (is_intr(irqstat)) {
  337. rtc_update_irq(p, 1, irqstat);
  338. return IRQ_HANDLED;
  339. } else
  340. return IRQ_NONE;
  341. }
  342. #ifdef CONFIG_PNP
  343. #define is_pnp() 1
  344. #define INITSECTION
  345. #else
  346. #define is_pnp() 0
  347. #define INITSECTION __init
  348. #endif
  349. static int INITSECTION
  350. cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
  351. {
  352. struct cmos_rtc_board_info *info = dev->platform_data;
  353. int retval = 0;
  354. unsigned char rtc_control;
  355. /* there can be only one ... */
  356. if (cmos_rtc.dev)
  357. return -EBUSY;
  358. if (!ports)
  359. return -ENODEV;
  360. /* Claim I/O ports ASAP, minimizing conflict with legacy driver.
  361. *
  362. * REVISIT non-x86 systems may instead use memory space resources
  363. * (needing ioremap etc), not i/o space resources like this ...
  364. */
  365. ports = request_region(ports->start,
  366. ports->end + 1 - ports->start,
  367. driver_name);
  368. if (!ports) {
  369. dev_dbg(dev, "i/o registers already in use\n");
  370. return -EBUSY;
  371. }
  372. cmos_rtc.irq = rtc_irq;
  373. cmos_rtc.iomem = ports;
  374. /* For ACPI systems extension info comes from the FADT. On others,
  375. * board specific setup provides it as appropriate. Systems where
  376. * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and
  377. * some almost-clones) can provide hooks to make that behave.
  378. */
  379. if (info) {
  380. cmos_rtc.day_alrm = info->rtc_day_alarm;
  381. cmos_rtc.mon_alrm = info->rtc_mon_alarm;
  382. cmos_rtc.century = info->rtc_century;
  383. if (info->wake_on && info->wake_off) {
  384. cmos_rtc.wake_on = info->wake_on;
  385. cmos_rtc.wake_off = info->wake_off;
  386. }
  387. }
  388. cmos_rtc.rtc = rtc_device_register(driver_name, dev,
  389. &cmos_rtc_ops, THIS_MODULE);
  390. if (IS_ERR(cmos_rtc.rtc)) {
  391. retval = PTR_ERR(cmos_rtc.rtc);
  392. goto cleanup0;
  393. }
  394. cmos_rtc.dev = dev;
  395. dev_set_drvdata(dev, &cmos_rtc);
  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. cmos_rtc.dev = NULL;
  449. rtc_device_unregister(cmos_rtc.rtc);
  450. cleanup0:
  451. release_region(ports->start, ports->end + 1 - ports->start);
  452. return retval;
  453. }
  454. static void cmos_do_shutdown(void)
  455. {
  456. unsigned char rtc_control;
  457. spin_lock_irq(&rtc_lock);
  458. rtc_control = CMOS_READ(RTC_CONTROL);
  459. rtc_control &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
  460. CMOS_WRITE(rtc_control, RTC_CONTROL);
  461. CMOS_READ(RTC_INTR_FLAGS);
  462. spin_unlock_irq(&rtc_lock);
  463. }
  464. static void __exit cmos_do_remove(struct device *dev)
  465. {
  466. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  467. struct resource *ports;
  468. cmos_do_shutdown();
  469. if (is_valid_irq(cmos->irq))
  470. free_irq(cmos->irq, cmos->rtc);
  471. rtc_device_unregister(cmos->rtc);
  472. cmos->rtc = NULL;
  473. ports = cmos->iomem;
  474. release_region(ports->start, ports->end + 1 - ports->start);
  475. cmos->iomem = NULL;
  476. cmos->dev = NULL;
  477. dev_set_drvdata(dev, NULL);
  478. }
  479. #ifdef CONFIG_PM
  480. static int cmos_suspend(struct device *dev, pm_message_t mesg)
  481. {
  482. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  483. int do_wake = device_may_wakeup(dev);
  484. unsigned char tmp;
  485. /* only the alarm might be a wakeup event source */
  486. spin_lock_irq(&rtc_lock);
  487. cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL);
  488. if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
  489. unsigned char irqstat;
  490. if (do_wake)
  491. tmp &= ~(RTC_PIE|RTC_UIE);
  492. else
  493. tmp &= ~(RTC_PIE|RTC_AIE|RTC_UIE);
  494. CMOS_WRITE(tmp, RTC_CONTROL);
  495. irqstat = CMOS_READ(RTC_INTR_FLAGS);
  496. irqstat &= (tmp & RTC_IRQMASK) | RTC_IRQF;
  497. if (is_intr(irqstat))
  498. rtc_update_irq(cmos->rtc, 1, irqstat);
  499. }
  500. spin_unlock_irq(&rtc_lock);
  501. if (tmp & RTC_AIE) {
  502. cmos->enabled_wake = 1;
  503. if (cmos->wake_on)
  504. cmos->wake_on(dev);
  505. else
  506. enable_irq_wake(cmos->irq);
  507. }
  508. pr_debug("%s: suspend%s, ctrl %02x\n",
  509. cmos_rtc.rtc->dev.bus_id,
  510. (tmp & RTC_AIE) ? ", alarm may wake" : "",
  511. tmp);
  512. return 0;
  513. }
  514. static int cmos_resume(struct device *dev)
  515. {
  516. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  517. unsigned char tmp = cmos->suspend_ctrl;
  518. /* re-enable any irqs previously active */
  519. if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
  520. if (cmos->enabled_wake) {
  521. if (cmos->wake_off)
  522. cmos->wake_off(dev);
  523. else
  524. disable_irq_wake(cmos->irq);
  525. cmos->enabled_wake = 0;
  526. }
  527. spin_lock_irq(&rtc_lock);
  528. CMOS_WRITE(tmp, RTC_CONTROL);
  529. tmp = CMOS_READ(RTC_INTR_FLAGS);
  530. tmp &= (cmos->suspend_ctrl & RTC_IRQMASK) | RTC_IRQF;
  531. if (is_intr(tmp))
  532. rtc_update_irq(cmos->rtc, 1, tmp);
  533. spin_unlock_irq(&rtc_lock);
  534. }
  535. pr_debug("%s: resume, ctrl %02x\n",
  536. cmos_rtc.rtc->dev.bus_id,
  537. cmos->suspend_ctrl);
  538. return 0;
  539. }
  540. #else
  541. #define cmos_suspend NULL
  542. #define cmos_resume NULL
  543. #endif
  544. /*----------------------------------------------------------------*/
  545. /* The "CMOS" RTC normally lives on the platform_bus. On ACPI systems,
  546. * the device node will always be created as a PNPACPI device. Plus
  547. * pre-ACPI PCs probably list it in the PNPBIOS tables.
  548. */
  549. #ifdef CONFIG_PNP
  550. #include <linux/pnp.h>
  551. static int __devinit
  552. cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
  553. {
  554. /* REVISIT paranoia argues for a shutdown notifier, since PNP
  555. * drivers can't provide shutdown() methods to disable IRQs.
  556. * Or better yet, fix PNP to allow those methods...
  557. */
  558. if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0))
  559. /* Some machines contain a PNP entry for the RTC, but
  560. * don't define the IRQ. It should always be safe to
  561. * hardcode it in these cases
  562. */
  563. return cmos_do_probe(&pnp->dev, &pnp->res.port_resource[0], 8);
  564. else
  565. return cmos_do_probe(&pnp->dev,
  566. &pnp->res.port_resource[0],
  567. pnp->res.irq_resource[0].start);
  568. }
  569. static void __exit cmos_pnp_remove(struct pnp_dev *pnp)
  570. {
  571. cmos_do_remove(&pnp->dev);
  572. }
  573. #ifdef CONFIG_PM
  574. static int cmos_pnp_suspend(struct pnp_dev *pnp, pm_message_t mesg)
  575. {
  576. return cmos_suspend(&pnp->dev, mesg);
  577. }
  578. static int cmos_pnp_resume(struct pnp_dev *pnp)
  579. {
  580. return cmos_resume(&pnp->dev);
  581. }
  582. #else
  583. #define cmos_pnp_suspend NULL
  584. #define cmos_pnp_resume NULL
  585. #endif
  586. static const struct pnp_device_id rtc_ids[] = {
  587. { .id = "PNP0b00", },
  588. { .id = "PNP0b01", },
  589. { .id = "PNP0b02", },
  590. { },
  591. };
  592. MODULE_DEVICE_TABLE(pnp, rtc_ids);
  593. static struct pnp_driver cmos_pnp_driver = {
  594. .name = (char *) driver_name,
  595. .id_table = rtc_ids,
  596. .probe = cmos_pnp_probe,
  597. .remove = __exit_p(cmos_pnp_remove),
  598. /* flag ensures resume() gets called, and stops syslog spam */
  599. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  600. .suspend = cmos_pnp_suspend,
  601. .resume = cmos_pnp_resume,
  602. };
  603. static int __init cmos_init(void)
  604. {
  605. return pnp_register_driver(&cmos_pnp_driver);
  606. }
  607. module_init(cmos_init);
  608. static void __exit cmos_exit(void)
  609. {
  610. pnp_unregister_driver(&cmos_pnp_driver);
  611. }
  612. module_exit(cmos_exit);
  613. #else /* no PNP */
  614. /*----------------------------------------------------------------*/
  615. /* Platform setup should have set up an RTC device, when PNP is
  616. * unavailable ... this could happen even on (older) PCs.
  617. */
  618. static int __init cmos_platform_probe(struct platform_device *pdev)
  619. {
  620. return cmos_do_probe(&pdev->dev,
  621. platform_get_resource(pdev, IORESOURCE_IO, 0),
  622. platform_get_irq(pdev, 0));
  623. }
  624. static int __exit cmos_platform_remove(struct platform_device *pdev)
  625. {
  626. cmos_do_remove(&pdev->dev);
  627. return 0;
  628. }
  629. static void cmos_platform_shutdown(struct platform_device *pdev)
  630. {
  631. cmos_do_shutdown();
  632. }
  633. static struct platform_driver cmos_platform_driver = {
  634. .remove = __exit_p(cmos_platform_remove),
  635. .shutdown = cmos_platform_shutdown,
  636. .driver = {
  637. .name = (char *) driver_name,
  638. .suspend = cmos_suspend,
  639. .resume = cmos_resume,
  640. }
  641. };
  642. static int __init cmos_init(void)
  643. {
  644. return platform_driver_probe(&cmos_platform_driver,
  645. cmos_platform_probe);
  646. }
  647. module_init(cmos_init);
  648. static void __exit cmos_exit(void)
  649. {
  650. platform_driver_unregister(&cmos_platform_driver);
  651. }
  652. module_exit(cmos_exit);
  653. #endif /* !PNP */
  654. MODULE_AUTHOR("David Brownell");
  655. MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
  656. MODULE_LICENSE("GPL");