rtc-cmos.c 29 KB

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