rtc-cmos.c 30 KB

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