rtc-cmos.c 29 KB

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