rtc-cmos.c 28 KB

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