rtc-cmos.c 30 KB

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