rtc-cmos.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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_alarm_irq_enable(struct device *dev, unsigned int enabled)
  307. {
  308. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  309. unsigned long flags;
  310. if (!is_valid_irq(cmos->irq))
  311. return -EINVAL;
  312. spin_lock_irqsave(&rtc_lock, flags);
  313. if (enabled)
  314. cmos_irq_enable(cmos, RTC_AIE);
  315. else
  316. cmos_irq_disable(cmos, RTC_AIE);
  317. spin_unlock_irqrestore(&rtc_lock, flags);
  318. return 0;
  319. }
  320. #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
  321. static int cmos_procfs(struct device *dev, struct seq_file *seq)
  322. {
  323. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  324. unsigned char rtc_control, valid;
  325. spin_lock_irq(&rtc_lock);
  326. rtc_control = CMOS_READ(RTC_CONTROL);
  327. valid = CMOS_READ(RTC_VALID);
  328. spin_unlock_irq(&rtc_lock);
  329. /* NOTE: at least ICH6 reports battery status using a different
  330. * (non-RTC) bit; and SQWE is ignored on many current systems.
  331. */
  332. return seq_printf(seq,
  333. "periodic_IRQ\t: %s\n"
  334. "update_IRQ\t: %s\n"
  335. "HPET_emulated\t: %s\n"
  336. // "square_wave\t: %s\n"
  337. "BCD\t\t: %s\n"
  338. "DST_enable\t: %s\n"
  339. "periodic_freq\t: %d\n"
  340. "batt_status\t: %s\n",
  341. (rtc_control & RTC_PIE) ? "yes" : "no",
  342. (rtc_control & RTC_UIE) ? "yes" : "no",
  343. is_hpet_enabled() ? "yes" : "no",
  344. // (rtc_control & RTC_SQWE) ? "yes" : "no",
  345. (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
  346. (rtc_control & RTC_DST_EN) ? "yes" : "no",
  347. cmos->rtc->irq_freq,
  348. (valid & RTC_VRT) ? "okay" : "dead");
  349. }
  350. #else
  351. #define cmos_procfs NULL
  352. #endif
  353. static const struct rtc_class_ops cmos_rtc_ops = {
  354. .read_time = cmos_read_time,
  355. .set_time = cmos_set_time,
  356. .read_alarm = cmos_read_alarm,
  357. .set_alarm = cmos_set_alarm,
  358. .proc = cmos_procfs,
  359. .alarm_irq_enable = cmos_alarm_irq_enable,
  360. };
  361. /*----------------------------------------------------------------*/
  362. /*
  363. * All these chips have at least 64 bytes of address space, shared by
  364. * RTC registers and NVRAM. Most of those bytes of NVRAM are used
  365. * by boot firmware. Modern chips have 128 or 256 bytes.
  366. */
  367. #define NVRAM_OFFSET (RTC_REG_D + 1)
  368. static ssize_t
  369. cmos_nvram_read(struct file *filp, struct kobject *kobj,
  370. struct bin_attribute *attr,
  371. char *buf, loff_t off, size_t count)
  372. {
  373. int retval;
  374. if (unlikely(off >= attr->size))
  375. return 0;
  376. if (unlikely(off < 0))
  377. return -EINVAL;
  378. if ((off + count) > attr->size)
  379. count = attr->size - off;
  380. off += NVRAM_OFFSET;
  381. spin_lock_irq(&rtc_lock);
  382. for (retval = 0; count; count--, off++, retval++) {
  383. if (off < 128)
  384. *buf++ = CMOS_READ(off);
  385. else if (can_bank2)
  386. *buf++ = cmos_read_bank2(off);
  387. else
  388. break;
  389. }
  390. spin_unlock_irq(&rtc_lock);
  391. return retval;
  392. }
  393. static ssize_t
  394. cmos_nvram_write(struct file *filp, struct kobject *kobj,
  395. struct bin_attribute *attr,
  396. char *buf, loff_t off, size_t count)
  397. {
  398. struct cmos_rtc *cmos;
  399. int retval;
  400. cmos = dev_get_drvdata(container_of(kobj, struct device, kobj));
  401. if (unlikely(off >= attr->size))
  402. return -EFBIG;
  403. if (unlikely(off < 0))
  404. return -EINVAL;
  405. if ((off + count) > attr->size)
  406. count = attr->size - off;
  407. /* NOTE: on at least PCs and Ataris, the boot firmware uses a
  408. * checksum on part of the NVRAM data. That's currently ignored
  409. * here. If userspace is smart enough to know what fields of
  410. * NVRAM to update, updating checksums is also part of its job.
  411. */
  412. off += NVRAM_OFFSET;
  413. spin_lock_irq(&rtc_lock);
  414. for (retval = 0; count; count--, off++, retval++) {
  415. /* don't trash RTC registers */
  416. if (off == cmos->day_alrm
  417. || off == cmos->mon_alrm
  418. || off == cmos->century)
  419. buf++;
  420. else if (off < 128)
  421. CMOS_WRITE(*buf++, off);
  422. else if (can_bank2)
  423. cmos_write_bank2(*buf++, off);
  424. else
  425. break;
  426. }
  427. spin_unlock_irq(&rtc_lock);
  428. return retval;
  429. }
  430. static struct bin_attribute nvram = {
  431. .attr = {
  432. .name = "nvram",
  433. .mode = S_IRUGO | S_IWUSR,
  434. },
  435. .read = cmos_nvram_read,
  436. .write = cmos_nvram_write,
  437. /* size gets set up later */
  438. };
  439. /*----------------------------------------------------------------*/
  440. static struct cmos_rtc cmos_rtc;
  441. static irqreturn_t cmos_interrupt(int irq, void *p)
  442. {
  443. u8 irqstat;
  444. u8 rtc_control;
  445. spin_lock(&rtc_lock);
  446. /* When the HPET interrupt handler calls us, the interrupt
  447. * status is passed as arg1 instead of the irq number. But
  448. * always clear irq status, even when HPET is in the way.
  449. *
  450. * Note that HPET and RTC are almost certainly out of phase,
  451. * giving different IRQ status ...
  452. */
  453. irqstat = CMOS_READ(RTC_INTR_FLAGS);
  454. rtc_control = CMOS_READ(RTC_CONTROL);
  455. if (is_hpet_enabled())
  456. irqstat = (unsigned long)irq & 0xF0;
  457. irqstat &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  458. /* All Linux RTC alarms should be treated as if they were oneshot.
  459. * Similar code may be needed in system wakeup paths, in case the
  460. * alarm woke the system.
  461. */
  462. if (irqstat & RTC_AIE) {
  463. rtc_control &= ~RTC_AIE;
  464. CMOS_WRITE(rtc_control, RTC_CONTROL);
  465. hpet_mask_rtc_irq_bit(RTC_AIE);
  466. CMOS_READ(RTC_INTR_FLAGS);
  467. }
  468. spin_unlock(&rtc_lock);
  469. if (is_intr(irqstat)) {
  470. rtc_update_irq(p, 1, irqstat);
  471. return IRQ_HANDLED;
  472. } else
  473. return IRQ_NONE;
  474. }
  475. #ifdef CONFIG_PNP
  476. #define INITSECTION
  477. #else
  478. #define INITSECTION __init
  479. #endif
  480. static int INITSECTION
  481. cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
  482. {
  483. struct cmos_rtc_board_info *info = dev->platform_data;
  484. int retval = 0;
  485. unsigned char rtc_control;
  486. unsigned address_space;
  487. /* there can be only one ... */
  488. if (cmos_rtc.dev)
  489. return -EBUSY;
  490. if (!ports)
  491. return -ENODEV;
  492. /* Claim I/O ports ASAP, minimizing conflict with legacy driver.
  493. *
  494. * REVISIT non-x86 systems may instead use memory space resources
  495. * (needing ioremap etc), not i/o space resources like this ...
  496. */
  497. ports = request_region(ports->start,
  498. ports->end + 1 - ports->start,
  499. driver_name);
  500. if (!ports) {
  501. dev_dbg(dev, "i/o registers already in use\n");
  502. return -EBUSY;
  503. }
  504. cmos_rtc.irq = rtc_irq;
  505. cmos_rtc.iomem = ports;
  506. /* Heuristic to deduce NVRAM size ... do what the legacy NVRAM
  507. * driver did, but don't reject unknown configs. Old hardware
  508. * won't address 128 bytes. Newer chips have multiple banks,
  509. * though they may not be listed in one I/O resource.
  510. */
  511. #if defined(CONFIG_ATARI)
  512. address_space = 64;
  513. #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) \
  514. || defined(__sparc__) || defined(__mips__) \
  515. || defined(__powerpc__)
  516. address_space = 128;
  517. #else
  518. #warning Assuming 128 bytes of RTC+NVRAM address space, not 64 bytes.
  519. address_space = 128;
  520. #endif
  521. if (can_bank2 && ports->end > (ports->start + 1))
  522. address_space = 256;
  523. /* For ACPI systems extension info comes from the FADT. On others,
  524. * board specific setup provides it as appropriate. Systems where
  525. * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and
  526. * some almost-clones) can provide hooks to make that behave.
  527. *
  528. * Note that ACPI doesn't preclude putting these registers into
  529. * "extended" areas of the chip, including some that we won't yet
  530. * expect CMOS_READ and friends to handle.
  531. */
  532. if (info) {
  533. if (info->rtc_day_alarm && info->rtc_day_alarm < 128)
  534. cmos_rtc.day_alrm = info->rtc_day_alarm;
  535. if (info->rtc_mon_alarm && info->rtc_mon_alarm < 128)
  536. cmos_rtc.mon_alrm = info->rtc_mon_alarm;
  537. if (info->rtc_century && info->rtc_century < 128)
  538. cmos_rtc.century = info->rtc_century;
  539. if (info->wake_on && info->wake_off) {
  540. cmos_rtc.wake_on = info->wake_on;
  541. cmos_rtc.wake_off = info->wake_off;
  542. }
  543. }
  544. cmos_rtc.dev = dev;
  545. dev_set_drvdata(dev, &cmos_rtc);
  546. cmos_rtc.rtc = rtc_device_register(driver_name, dev,
  547. &cmos_rtc_ops, THIS_MODULE);
  548. if (IS_ERR(cmos_rtc.rtc)) {
  549. retval = PTR_ERR(cmos_rtc.rtc);
  550. goto cleanup0;
  551. }
  552. rename_region(ports, dev_name(&cmos_rtc.rtc->dev));
  553. spin_lock_irq(&rtc_lock);
  554. /* force periodic irq to CMOS reset default of 1024Hz;
  555. *
  556. * REVISIT it's been reported that at least one x86_64 ALI mobo
  557. * doesn't use 32KHz here ... for portability we might need to
  558. * do something about other clock frequencies.
  559. */
  560. cmos_rtc.rtc->irq_freq = 1024;
  561. hpet_set_periodic_freq(cmos_rtc.rtc->irq_freq);
  562. CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT);
  563. /* disable irqs */
  564. cmos_irq_disable(&cmos_rtc, RTC_PIE | RTC_AIE | RTC_UIE);
  565. rtc_control = CMOS_READ(RTC_CONTROL);
  566. spin_unlock_irq(&rtc_lock);
  567. /* FIXME:
  568. * <asm-generic/rtc.h> doesn't know 12-hour mode either.
  569. */
  570. if (is_valid_irq(rtc_irq) && !(rtc_control & RTC_24H)) {
  571. dev_warn(dev, "only 24-hr supported\n");
  572. retval = -ENXIO;
  573. goto cleanup1;
  574. }
  575. if (is_valid_irq(rtc_irq)) {
  576. irq_handler_t rtc_cmos_int_handler;
  577. if (is_hpet_enabled()) {
  578. int err;
  579. rtc_cmos_int_handler = hpet_rtc_interrupt;
  580. err = hpet_register_irq_handler(cmos_interrupt);
  581. if (err != 0) {
  582. printk(KERN_WARNING "hpet_register_irq_handler "
  583. " failed in rtc_init().");
  584. goto cleanup1;
  585. }
  586. } else
  587. rtc_cmos_int_handler = cmos_interrupt;
  588. retval = request_irq(rtc_irq, rtc_cmos_int_handler,
  589. IRQF_DISABLED, dev_name(&cmos_rtc.rtc->dev),
  590. cmos_rtc.rtc);
  591. if (retval < 0) {
  592. dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
  593. goto cleanup1;
  594. }
  595. }
  596. hpet_rtc_timer_init();
  597. /* export at least the first block of NVRAM */
  598. nvram.size = address_space - NVRAM_OFFSET;
  599. retval = sysfs_create_bin_file(&dev->kobj, &nvram);
  600. if (retval < 0) {
  601. dev_dbg(dev, "can't create nvram file? %d\n", retval);
  602. goto cleanup2;
  603. }
  604. pr_info("%s: %s%s, %zd bytes nvram%s\n",
  605. dev_name(&cmos_rtc.rtc->dev),
  606. !is_valid_irq(rtc_irq) ? "no alarms" :
  607. cmos_rtc.mon_alrm ? "alarms up to one year" :
  608. cmos_rtc.day_alrm ? "alarms up to one month" :
  609. "alarms up to one day",
  610. cmos_rtc.century ? ", y3k" : "",
  611. nvram.size,
  612. is_hpet_enabled() ? ", hpet irqs" : "");
  613. return 0;
  614. cleanup2:
  615. if (is_valid_irq(rtc_irq))
  616. free_irq(rtc_irq, cmos_rtc.rtc);
  617. cleanup1:
  618. cmos_rtc.dev = NULL;
  619. rtc_device_unregister(cmos_rtc.rtc);
  620. cleanup0:
  621. release_region(ports->start, ports->end + 1 - ports->start);
  622. return retval;
  623. }
  624. static void cmos_do_shutdown(void)
  625. {
  626. spin_lock_irq(&rtc_lock);
  627. cmos_irq_disable(&cmos_rtc, RTC_IRQMASK);
  628. spin_unlock_irq(&rtc_lock);
  629. }
  630. static void __exit cmos_do_remove(struct device *dev)
  631. {
  632. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  633. struct resource *ports;
  634. cmos_do_shutdown();
  635. sysfs_remove_bin_file(&dev->kobj, &nvram);
  636. if (is_valid_irq(cmos->irq)) {
  637. free_irq(cmos->irq, cmos->rtc);
  638. hpet_unregister_irq_handler(cmos_interrupt);
  639. }
  640. rtc_device_unregister(cmos->rtc);
  641. cmos->rtc = NULL;
  642. ports = cmos->iomem;
  643. release_region(ports->start, ports->end + 1 - ports->start);
  644. cmos->iomem = NULL;
  645. cmos->dev = NULL;
  646. dev_set_drvdata(dev, NULL);
  647. }
  648. #ifdef CONFIG_PM
  649. static int cmos_suspend(struct device *dev)
  650. {
  651. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  652. unsigned char tmp;
  653. /* only the alarm might be a wakeup event source */
  654. spin_lock_irq(&rtc_lock);
  655. cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL);
  656. if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
  657. unsigned char mask;
  658. if (device_may_wakeup(dev))
  659. mask = RTC_IRQMASK & ~RTC_AIE;
  660. else
  661. mask = RTC_IRQMASK;
  662. tmp &= ~mask;
  663. CMOS_WRITE(tmp, RTC_CONTROL);
  664. /* shut down hpet emulation - we don't need it for alarm */
  665. hpet_mask_rtc_irq_bit(RTC_PIE|RTC_AIE|RTC_UIE);
  666. cmos_checkintr(cmos, tmp);
  667. }
  668. spin_unlock_irq(&rtc_lock);
  669. if (tmp & RTC_AIE) {
  670. cmos->enabled_wake = 1;
  671. if (cmos->wake_on)
  672. cmos->wake_on(dev);
  673. else
  674. enable_irq_wake(cmos->irq);
  675. }
  676. pr_debug("%s: suspend%s, ctrl %02x\n",
  677. dev_name(&cmos_rtc.rtc->dev),
  678. (tmp & RTC_AIE) ? ", alarm may wake" : "",
  679. tmp);
  680. return 0;
  681. }
  682. /* We want RTC alarms to wake us from e.g. ACPI G2/S5 "soft off", even
  683. * after a detour through G3 "mechanical off", although the ACPI spec
  684. * says wakeup should only work from G1/S4 "hibernate". To most users,
  685. * distinctions between S4 and S5 are pointless. So when the hardware
  686. * allows, don't draw that distinction.
  687. */
  688. static inline int cmos_poweroff(struct device *dev)
  689. {
  690. return cmos_suspend(dev);
  691. }
  692. static int cmos_resume(struct device *dev)
  693. {
  694. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  695. unsigned char tmp = cmos->suspend_ctrl;
  696. /* re-enable any irqs previously active */
  697. if (tmp & RTC_IRQMASK) {
  698. unsigned char mask;
  699. if (cmos->enabled_wake) {
  700. if (cmos->wake_off)
  701. cmos->wake_off(dev);
  702. else
  703. disable_irq_wake(cmos->irq);
  704. cmos->enabled_wake = 0;
  705. }
  706. spin_lock_irq(&rtc_lock);
  707. do {
  708. CMOS_WRITE(tmp, RTC_CONTROL);
  709. hpet_set_rtc_irq_bit(tmp & RTC_IRQMASK);
  710. mask = CMOS_READ(RTC_INTR_FLAGS);
  711. mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
  712. if (!is_hpet_enabled() || !is_intr(mask))
  713. break;
  714. /* force one-shot behavior if HPET blocked
  715. * the wake alarm's irq
  716. */
  717. rtc_update_irq(cmos->rtc, 1, mask);
  718. tmp &= ~RTC_AIE;
  719. hpet_mask_rtc_irq_bit(RTC_AIE);
  720. } while (mask & RTC_AIE);
  721. spin_unlock_irq(&rtc_lock);
  722. }
  723. pr_debug("%s: resume, ctrl %02x\n",
  724. dev_name(&cmos_rtc.rtc->dev),
  725. tmp);
  726. return 0;
  727. }
  728. static SIMPLE_DEV_PM_OPS(cmos_pm_ops, cmos_suspend, cmos_resume);
  729. #else
  730. static inline int cmos_poweroff(struct device *dev)
  731. {
  732. return -ENOSYS;
  733. }
  734. #endif
  735. /*----------------------------------------------------------------*/
  736. /* On non-x86 systems, a "CMOS" RTC lives most naturally on platform_bus.
  737. * ACPI systems always list these as PNPACPI devices, and pre-ACPI PCs
  738. * probably list them in similar PNPBIOS tables; so PNP is more common.
  739. *
  740. * We don't use legacy "poke at the hardware" probing. Ancient PCs that
  741. * predate even PNPBIOS should set up platform_bus devices.
  742. */
  743. #ifdef CONFIG_ACPI
  744. #include <linux/acpi.h>
  745. static u32 rtc_handler(void *context)
  746. {
  747. acpi_clear_event(ACPI_EVENT_RTC);
  748. acpi_disable_event(ACPI_EVENT_RTC, 0);
  749. return ACPI_INTERRUPT_HANDLED;
  750. }
  751. static inline void rtc_wake_setup(void)
  752. {
  753. acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
  754. /*
  755. * After the RTC handler is installed, the Fixed_RTC event should
  756. * be disabled. Only when the RTC alarm is set will it be enabled.
  757. */
  758. acpi_clear_event(ACPI_EVENT_RTC);
  759. acpi_disable_event(ACPI_EVENT_RTC, 0);
  760. }
  761. static void rtc_wake_on(struct device *dev)
  762. {
  763. acpi_clear_event(ACPI_EVENT_RTC);
  764. acpi_enable_event(ACPI_EVENT_RTC, 0);
  765. }
  766. static void rtc_wake_off(struct device *dev)
  767. {
  768. acpi_disable_event(ACPI_EVENT_RTC, 0);
  769. }
  770. /* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find
  771. * its device node and pass extra config data. This helps its driver use
  772. * capabilities that the now-obsolete mc146818 didn't have, and informs it
  773. * that this board's RTC is wakeup-capable (per ACPI spec).
  774. */
  775. static struct cmos_rtc_board_info acpi_rtc_info;
  776. static void __devinit
  777. cmos_wake_setup(struct device *dev)
  778. {
  779. if (acpi_disabled)
  780. return;
  781. rtc_wake_setup();
  782. acpi_rtc_info.wake_on = rtc_wake_on;
  783. acpi_rtc_info.wake_off = rtc_wake_off;
  784. /* workaround bug in some ACPI tables */
  785. if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) {
  786. dev_dbg(dev, "bogus FADT month_alarm (%d)\n",
  787. acpi_gbl_FADT.month_alarm);
  788. acpi_gbl_FADT.month_alarm = 0;
  789. }
  790. acpi_rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
  791. acpi_rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
  792. acpi_rtc_info.rtc_century = acpi_gbl_FADT.century;
  793. /* NOTE: S4_RTC_WAKE is NOT currently useful to Linux */
  794. if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE)
  795. dev_info(dev, "RTC can wake from S4\n");
  796. dev->platform_data = &acpi_rtc_info;
  797. /* RTC always wakes from S1/S2/S3, and often S4/STD */
  798. device_init_wakeup(dev, 1);
  799. }
  800. #else
  801. static void __devinit
  802. cmos_wake_setup(struct device *dev)
  803. {
  804. }
  805. #endif
  806. #ifdef CONFIG_PNP
  807. #include <linux/pnp.h>
  808. static int __devinit
  809. cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
  810. {
  811. cmos_wake_setup(&pnp->dev);
  812. if (pnp_port_start(pnp,0) == 0x70 && !pnp_irq_valid(pnp,0))
  813. /* Some machines contain a PNP entry for the RTC, but
  814. * don't define the IRQ. It should always be safe to
  815. * hardcode it in these cases
  816. */
  817. return cmos_do_probe(&pnp->dev,
  818. pnp_get_resource(pnp, IORESOURCE_IO, 0), 8);
  819. else
  820. return cmos_do_probe(&pnp->dev,
  821. pnp_get_resource(pnp, IORESOURCE_IO, 0),
  822. pnp_irq(pnp, 0));
  823. }
  824. static void __exit cmos_pnp_remove(struct pnp_dev *pnp)
  825. {
  826. cmos_do_remove(&pnp->dev);
  827. }
  828. #ifdef CONFIG_PM
  829. static int cmos_pnp_suspend(struct pnp_dev *pnp, pm_message_t mesg)
  830. {
  831. return cmos_suspend(&pnp->dev);
  832. }
  833. static int cmos_pnp_resume(struct pnp_dev *pnp)
  834. {
  835. return cmos_resume(&pnp->dev);
  836. }
  837. #else
  838. #define cmos_pnp_suspend NULL
  839. #define cmos_pnp_resume NULL
  840. #endif
  841. static void cmos_pnp_shutdown(struct pnp_dev *pnp)
  842. {
  843. if (system_state == SYSTEM_POWER_OFF && !cmos_poweroff(&pnp->dev))
  844. return;
  845. cmos_do_shutdown();
  846. }
  847. static const struct pnp_device_id rtc_ids[] = {
  848. { .id = "PNP0b00", },
  849. { .id = "PNP0b01", },
  850. { .id = "PNP0b02", },
  851. { },
  852. };
  853. MODULE_DEVICE_TABLE(pnp, rtc_ids);
  854. static struct pnp_driver cmos_pnp_driver = {
  855. .name = (char *) driver_name,
  856. .id_table = rtc_ids,
  857. .probe = cmos_pnp_probe,
  858. .remove = __exit_p(cmos_pnp_remove),
  859. .shutdown = cmos_pnp_shutdown,
  860. /* flag ensures resume() gets called, and stops syslog spam */
  861. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  862. .suspend = cmos_pnp_suspend,
  863. .resume = cmos_pnp_resume,
  864. };
  865. #endif /* CONFIG_PNP */
  866. /*----------------------------------------------------------------*/
  867. /* Platform setup should have set up an RTC device, when PNP is
  868. * unavailable ... this could happen even on (older) PCs.
  869. */
  870. static int __init cmos_platform_probe(struct platform_device *pdev)
  871. {
  872. cmos_wake_setup(&pdev->dev);
  873. return cmos_do_probe(&pdev->dev,
  874. platform_get_resource(pdev, IORESOURCE_IO, 0),
  875. platform_get_irq(pdev, 0));
  876. }
  877. static int __exit cmos_platform_remove(struct platform_device *pdev)
  878. {
  879. cmos_do_remove(&pdev->dev);
  880. return 0;
  881. }
  882. static void cmos_platform_shutdown(struct platform_device *pdev)
  883. {
  884. if (system_state == SYSTEM_POWER_OFF && !cmos_poweroff(&pdev->dev))
  885. return;
  886. cmos_do_shutdown();
  887. }
  888. /* work with hotplug and coldplug */
  889. MODULE_ALIAS("platform:rtc_cmos");
  890. static struct platform_driver cmos_platform_driver = {
  891. .remove = __exit_p(cmos_platform_remove),
  892. .shutdown = cmos_platform_shutdown,
  893. .driver = {
  894. .name = (char *) driver_name,
  895. #ifdef CONFIG_PM
  896. .pm = &cmos_pm_ops,
  897. #endif
  898. }
  899. };
  900. #ifdef CONFIG_PNP
  901. static bool pnp_driver_registered;
  902. #endif
  903. static bool platform_driver_registered;
  904. static int __init cmos_init(void)
  905. {
  906. int retval = 0;
  907. #ifdef CONFIG_PNP
  908. retval = pnp_register_driver(&cmos_pnp_driver);
  909. if (retval == 0)
  910. pnp_driver_registered = true;
  911. #endif
  912. if (!cmos_rtc.dev) {
  913. retval = platform_driver_probe(&cmos_platform_driver,
  914. cmos_platform_probe);
  915. if (retval == 0)
  916. platform_driver_registered = true;
  917. }
  918. if (retval == 0)
  919. return 0;
  920. #ifdef CONFIG_PNP
  921. if (pnp_driver_registered)
  922. pnp_unregister_driver(&cmos_pnp_driver);
  923. #endif
  924. return retval;
  925. }
  926. module_init(cmos_init);
  927. static void __exit cmos_exit(void)
  928. {
  929. #ifdef CONFIG_PNP
  930. if (pnp_driver_registered)
  931. pnp_unregister_driver(&cmos_pnp_driver);
  932. #endif
  933. if (platform_driver_registered)
  934. platform_driver_unregister(&cmos_platform_driver);
  935. }
  936. module_exit(cmos_exit);
  937. MODULE_AUTHOR("David Brownell");
  938. MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
  939. MODULE_LICENSE("GPL");