rtc-cmos.c 29 KB

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