rtc-cmos.c 25 KB

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