rtc-cmos.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/log2.h>
  38. #include <linux/pm.h>
  39. #include <linux/of.h>
  40. #include <linux/of_platform.h>
  41. #ifdef CONFIG_X86
  42. #include <asm/i8259.h>
  43. #include <asm/processor.h>
  44. #include <linux/dmi.h>
  45. #endif
  46. /* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */
  47. #include <linux/mc146818rtc.h>
  48. #ifdef CONFIG_ACPI
  49. /*
  50. * Use ACPI SCI to replace HPET interrupt for RTC Alarm event
  51. *
  52. * If cleared, ACPI SCI is only used to wake up the system from suspend
  53. *
  54. * If set, ACPI SCI is used to handle UIE/AIE and system wakeup
  55. */
  56. static bool use_acpi_alarm;
  57. module_param(use_acpi_alarm, bool, 0444);
  58. static inline int cmos_use_acpi_alarm(void)
  59. {
  60. return use_acpi_alarm;
  61. }
  62. #else /* !CONFIG_ACPI */
  63. static inline int cmos_use_acpi_alarm(void)
  64. {
  65. return 0;
  66. }
  67. #endif
  68. struct cmos_rtc {
  69. struct rtc_device *rtc;
  70. struct device *dev;
  71. int irq;
  72. struct resource *iomem;
  73. time64_t alarm_expires;
  74. void (*wake_on)(struct device *);
  75. void (*wake_off)(struct device *);
  76. u8 enabled_wake;
  77. u8 suspend_ctrl;
  78. /* newer hardware extends the original register set */
  79. u8 day_alrm;
  80. u8 mon_alrm;
  81. u8 century;
  82. struct rtc_wkalrm saved_wkalrm;
  83. };
  84. /* both platform and pnp busses use negative numbers for invalid irqs */
  85. #define is_valid_irq(n) ((n) > 0)
  86. static const char driver_name[] = "rtc_cmos";
  87. /* The RTC_INTR register may have e.g. RTC_PF set even if RTC_PIE is clear;
  88. * always mask it against the irq enable bits in RTC_CONTROL. Bit values
  89. * are the same: PF==PIE, AF=AIE, UF=UIE; so RTC_IRQMASK works with both.
  90. */
  91. #define RTC_IRQMASK (RTC_PF | RTC_AF | RTC_UF)
  92. static inline int is_intr(u8 rtc_intr)
  93. {
  94. if (!(rtc_intr & RTC_IRQF))
  95. return 0;
  96. return rtc_intr & RTC_IRQMASK;
  97. }
  98. /*----------------------------------------------------------------*/
  99. /* Much modern x86 hardware has HPETs (10+ MHz timers) which, because
  100. * many BIOS programmers don't set up "sane mode" IRQ routing, are mostly
  101. * used in a broken "legacy replacement" mode. The breakage includes
  102. * HPET #1 hijacking the IRQ for this RTC, and being unavailable for
  103. * other (better) use.
  104. *
  105. * When that broken mode is in use, platform glue provides a partial
  106. * emulation of hardware RTC IRQ facilities using HPET #1. We don't
  107. * want to use HPET for anything except those IRQs though...
  108. */
  109. #ifdef CONFIG_HPET_EMULATE_RTC
  110. #include <asm/hpet.h>
  111. #else
  112. static inline int is_hpet_enabled(void)
  113. {
  114. return 0;
  115. }
  116. static inline int hpet_mask_rtc_irq_bit(unsigned long mask)
  117. {
  118. return 0;
  119. }
  120. static inline int hpet_set_rtc_irq_bit(unsigned long mask)
  121. {
  122. return 0;
  123. }
  124. static inline int
  125. hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
  126. {
  127. return 0;
  128. }
  129. static inline int hpet_set_periodic_freq(unsigned long freq)
  130. {
  131. return 0;
  132. }
  133. static inline int hpet_rtc_dropped_irq(void)
  134. {
  135. return 0;
  136. }
  137. static inline int hpet_rtc_timer_init(void)
  138. {
  139. return 0;
  140. }
  141. extern irq_handler_t hpet_rtc_interrupt;
  142. static inline int hpet_register_irq_handler(irq_handler_t handler)
  143. {
  144. return 0;
  145. }
  146. static inline int hpet_unregister_irq_handler(irq_handler_t handler)
  147. {
  148. return 0;
  149. }
  150. #endif
  151. /* Don't use HPET for RTC Alarm event if ACPI Fixed event is used */
  152. static inline int use_hpet_alarm(void)
  153. {
  154. return is_hpet_enabled() && !cmos_use_acpi_alarm();
  155. }
  156. /*----------------------------------------------------------------*/
  157. #ifdef RTC_PORT
  158. /* Most newer x86 systems have two register banks, the first used
  159. * for RTC and NVRAM and the second only for NVRAM. Caller must
  160. * own rtc_lock ... and we won't worry about access during NMI.
  161. */
  162. #define can_bank2 true
  163. static inline unsigned char cmos_read_bank2(unsigned char addr)
  164. {
  165. outb(addr, RTC_PORT(2));
  166. return inb(RTC_PORT(3));
  167. }
  168. static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
  169. {
  170. outb(addr, RTC_PORT(2));
  171. outb(val, RTC_PORT(3));
  172. }
  173. #else
  174. #define can_bank2 false
  175. static inline unsigned char cmos_read_bank2(unsigned char addr)
  176. {
  177. return 0;
  178. }
  179. static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
  180. {
  181. }
  182. #endif
  183. /*----------------------------------------------------------------*/
  184. static int cmos_read_time(struct device *dev, struct rtc_time *t)
  185. {
  186. /*
  187. * If pm_trace abused the RTC for storage, set the timespec to 0,
  188. * which tells the caller that this RTC value is unusable.
  189. */
  190. if (!pm_trace_rtc_valid())
  191. return -EIO;
  192. /* REVISIT: if the clock has a "century" register, use
  193. * that instead of the heuristic in mc146818_get_time().
  194. * That'll make Y3K compatility (year > 2070) easy!
  195. */
  196. mc146818_get_time(t);
  197. return 0;
  198. }
  199. static int cmos_set_time(struct device *dev, struct rtc_time *t)
  200. {
  201. /* REVISIT: set the "century" register if available
  202. *
  203. * NOTE: this ignores the issue whereby updating the seconds
  204. * takes effect exactly 500ms after we write the register.
  205. * (Also queueing and other delays before we get this far.)
  206. */
  207. return mc146818_set_time(t);
  208. }
  209. static int cmos_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  210. {
  211. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  212. unsigned char rtc_control;
  213. if (!is_valid_irq(cmos->irq))
  214. return -EIO;
  215. /* Basic alarms only support hour, minute, and seconds fields.
  216. * Some also support day and month, for alarms up to a year in
  217. * the future.
  218. */
  219. spin_lock_irq(&rtc_lock);
  220. t->time.tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
  221. t->time.tm_min = CMOS_READ(RTC_MINUTES_ALARM);
  222. t->time.tm_hour = CMOS_READ(RTC_HOURS_ALARM);
  223. if (cmos->day_alrm) {
  224. /* ignore upper bits on readback per ACPI spec */
  225. t->time.tm_mday = CMOS_READ(cmos->day_alrm) & 0x3f;
  226. if (!t->time.tm_mday)
  227. t->time.tm_mday = -1;
  228. if (cmos->mon_alrm) {
  229. t->time.tm_mon = CMOS_READ(cmos->mon_alrm);
  230. if (!t->time.tm_mon)
  231. t->time.tm_mon = -1;
  232. }
  233. }
  234. rtc_control = CMOS_READ(RTC_CONTROL);
  235. spin_unlock_irq(&rtc_lock);
  236. if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  237. if (((unsigned)t->time.tm_sec) < 0x60)
  238. t->time.tm_sec = bcd2bin(t->time.tm_sec);
  239. else
  240. t->time.tm_sec = -1;
  241. if (((unsigned)t->time.tm_min) < 0x60)
  242. t->time.tm_min = bcd2bin(t->time.tm_min);
  243. else
  244. t->time.tm_min = -1;
  245. if (((unsigned)t->time.tm_hour) < 0x24)
  246. t->time.tm_hour = bcd2bin(t->time.tm_hour);
  247. else
  248. t->time.tm_hour = -1;
  249. if (cmos->day_alrm) {
  250. if (((unsigned)t->time.tm_mday) <= 0x31)
  251. t->time.tm_mday = bcd2bin(t->time.tm_mday);
  252. else
  253. t->time.tm_mday = -1;
  254. if (cmos->mon_alrm) {
  255. if (((unsigned)t->time.tm_mon) <= 0x12)
  256. t->time.tm_mon = bcd2bin(t->time.tm_mon)-1;
  257. else
  258. t->time.tm_mon = -1;
  259. }
  260. }
  261. }
  262. t->enabled = !!(rtc_control & RTC_AIE);
  263. t->pending = 0;
  264. return 0;
  265. }
  266. static void cmos_checkintr(struct cmos_rtc *cmos, unsigned char rtc_control)
  267. {
  268. unsigned char rtc_intr;
  269. /* NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
  270. * allegedly some older rtcs need that to handle irqs properly
  271. */
  272. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  273. if (use_hpet_alarm())
  274. return;
  275. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  276. if (is_intr(rtc_intr))
  277. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  278. }
  279. static void cmos_irq_enable(struct cmos_rtc *cmos, unsigned char mask)
  280. {
  281. unsigned char rtc_control;
  282. /* flush any pending IRQ status, notably for update irqs,
  283. * before we enable new IRQs
  284. */
  285. rtc_control = CMOS_READ(RTC_CONTROL);
  286. cmos_checkintr(cmos, rtc_control);
  287. rtc_control |= mask;
  288. CMOS_WRITE(rtc_control, RTC_CONTROL);
  289. if (use_hpet_alarm())
  290. hpet_set_rtc_irq_bit(mask);
  291. if ((mask & RTC_AIE) && cmos_use_acpi_alarm()) {
  292. if (cmos->wake_on)
  293. cmos->wake_on(cmos->dev);
  294. }
  295. cmos_checkintr(cmos, rtc_control);
  296. }
  297. static void cmos_irq_disable(struct cmos_rtc *cmos, unsigned char mask)
  298. {
  299. unsigned char rtc_control;
  300. rtc_control = CMOS_READ(RTC_CONTROL);
  301. rtc_control &= ~mask;
  302. CMOS_WRITE(rtc_control, RTC_CONTROL);
  303. if (use_hpet_alarm())
  304. hpet_mask_rtc_irq_bit(mask);
  305. if ((mask & RTC_AIE) && cmos_use_acpi_alarm()) {
  306. if (cmos->wake_off)
  307. cmos->wake_off(cmos->dev);
  308. }
  309. cmos_checkintr(cmos, rtc_control);
  310. }
  311. static int cmos_validate_alarm(struct device *dev, struct rtc_wkalrm *t)
  312. {
  313. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  314. struct rtc_time now;
  315. cmos_read_time(dev, &now);
  316. if (!cmos->day_alrm) {
  317. time64_t t_max_date;
  318. time64_t t_alrm;
  319. t_max_date = rtc_tm_to_time64(&now);
  320. t_max_date += 24 * 60 * 60 - 1;
  321. t_alrm = rtc_tm_to_time64(&t->time);
  322. if (t_alrm > t_max_date) {
  323. dev_err(dev,
  324. "Alarms can be up to one day in the future\n");
  325. return -EINVAL;
  326. }
  327. } else if (!cmos->mon_alrm) {
  328. struct rtc_time max_date = now;
  329. time64_t t_max_date;
  330. time64_t t_alrm;
  331. int max_mday;
  332. if (max_date.tm_mon == 11) {
  333. max_date.tm_mon = 0;
  334. max_date.tm_year += 1;
  335. } else {
  336. max_date.tm_mon += 1;
  337. }
  338. max_mday = rtc_month_days(max_date.tm_mon, max_date.tm_year);
  339. if (max_date.tm_mday > max_mday)
  340. max_date.tm_mday = max_mday;
  341. t_max_date = rtc_tm_to_time64(&max_date);
  342. t_max_date -= 1;
  343. t_alrm = rtc_tm_to_time64(&t->time);
  344. if (t_alrm > t_max_date) {
  345. dev_err(dev,
  346. "Alarms can be up to one month in the future\n");
  347. return -EINVAL;
  348. }
  349. } else {
  350. struct rtc_time max_date = now;
  351. time64_t t_max_date;
  352. time64_t t_alrm;
  353. int max_mday;
  354. max_date.tm_year += 1;
  355. max_mday = rtc_month_days(max_date.tm_mon, max_date.tm_year);
  356. if (max_date.tm_mday > max_mday)
  357. max_date.tm_mday = max_mday;
  358. t_max_date = rtc_tm_to_time64(&max_date);
  359. t_max_date -= 1;
  360. t_alrm = rtc_tm_to_time64(&t->time);
  361. if (t_alrm > t_max_date) {
  362. dev_err(dev,
  363. "Alarms can be up to one year in the future\n");
  364. return -EINVAL;
  365. }
  366. }
  367. return 0;
  368. }
  369. static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  370. {
  371. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  372. unsigned char mon, mday, hrs, min, sec, rtc_control;
  373. int ret;
  374. if (!is_valid_irq(cmos->irq))
  375. return -EIO;
  376. ret = cmos_validate_alarm(dev, t);
  377. if (ret < 0)
  378. return ret;
  379. mon = t->time.tm_mon + 1;
  380. mday = t->time.tm_mday;
  381. hrs = t->time.tm_hour;
  382. min = t->time.tm_min;
  383. sec = t->time.tm_sec;
  384. rtc_control = CMOS_READ(RTC_CONTROL);
  385. if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  386. /* Writing 0xff means "don't care" or "match all". */
  387. mon = (mon <= 12) ? bin2bcd(mon) : 0xff;
  388. mday = (mday >= 1 && mday <= 31) ? bin2bcd(mday) : 0xff;
  389. hrs = (hrs < 24) ? bin2bcd(hrs) : 0xff;
  390. min = (min < 60) ? bin2bcd(min) : 0xff;
  391. sec = (sec < 60) ? bin2bcd(sec) : 0xff;
  392. }
  393. spin_lock_irq(&rtc_lock);
  394. /* next rtc irq must not be from previous alarm setting */
  395. cmos_irq_disable(cmos, RTC_AIE);
  396. /* update alarm */
  397. CMOS_WRITE(hrs, RTC_HOURS_ALARM);
  398. CMOS_WRITE(min, RTC_MINUTES_ALARM);
  399. CMOS_WRITE(sec, RTC_SECONDS_ALARM);
  400. /* the system may support an "enhanced" alarm */
  401. if (cmos->day_alrm) {
  402. CMOS_WRITE(mday, cmos->day_alrm);
  403. if (cmos->mon_alrm)
  404. CMOS_WRITE(mon, cmos->mon_alrm);
  405. }
  406. if (use_hpet_alarm()) {
  407. /*
  408. * FIXME the HPET alarm glue currently ignores day_alrm
  409. * and mon_alrm ...
  410. */
  411. hpet_set_alarm_time(t->time.tm_hour, t->time.tm_min,
  412. t->time.tm_sec);
  413. }
  414. if (t->enabled)
  415. cmos_irq_enable(cmos, RTC_AIE);
  416. spin_unlock_irq(&rtc_lock);
  417. cmos->alarm_expires = rtc_tm_to_time64(&t->time);
  418. return 0;
  419. }
  420. static int cmos_alarm_irq_enable(struct device *dev, unsigned int enabled)
  421. {
  422. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  423. unsigned long flags;
  424. if (!is_valid_irq(cmos->irq))
  425. return -EINVAL;
  426. spin_lock_irqsave(&rtc_lock, flags);
  427. if (enabled)
  428. cmos_irq_enable(cmos, RTC_AIE);
  429. else
  430. cmos_irq_disable(cmos, RTC_AIE);
  431. spin_unlock_irqrestore(&rtc_lock, flags);
  432. return 0;
  433. }
  434. #if IS_ENABLED(CONFIG_RTC_INTF_PROC)
  435. static int cmos_procfs(struct device *dev, struct seq_file *seq)
  436. {
  437. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  438. unsigned char rtc_control, valid;
  439. spin_lock_irq(&rtc_lock);
  440. rtc_control = CMOS_READ(RTC_CONTROL);
  441. valid = CMOS_READ(RTC_VALID);
  442. spin_unlock_irq(&rtc_lock);
  443. /* NOTE: at least ICH6 reports battery status using a different
  444. * (non-RTC) bit; and SQWE is ignored on many current systems.
  445. */
  446. seq_printf(seq,
  447. "periodic_IRQ\t: %s\n"
  448. "update_IRQ\t: %s\n"
  449. "HPET_emulated\t: %s\n"
  450. // "square_wave\t: %s\n"
  451. "BCD\t\t: %s\n"
  452. "DST_enable\t: %s\n"
  453. "periodic_freq\t: %d\n"
  454. "batt_status\t: %s\n",
  455. (rtc_control & RTC_PIE) ? "yes" : "no",
  456. (rtc_control & RTC_UIE) ? "yes" : "no",
  457. use_hpet_alarm() ? "yes" : "no",
  458. // (rtc_control & RTC_SQWE) ? "yes" : "no",
  459. (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
  460. (rtc_control & RTC_DST_EN) ? "yes" : "no",
  461. cmos->rtc->irq_freq,
  462. (valid & RTC_VRT) ? "okay" : "dead");
  463. return 0;
  464. }
  465. #else
  466. #define cmos_procfs NULL
  467. #endif
  468. static const struct rtc_class_ops cmos_rtc_ops = {
  469. .read_time = cmos_read_time,
  470. .set_time = cmos_set_time,
  471. .read_alarm = cmos_read_alarm,
  472. .set_alarm = cmos_set_alarm,
  473. .proc = cmos_procfs,
  474. .alarm_irq_enable = cmos_alarm_irq_enable,
  475. };
  476. /*----------------------------------------------------------------*/
  477. /*
  478. * All these chips have at least 64 bytes of address space, shared by
  479. * RTC registers and NVRAM. Most of those bytes of NVRAM are used
  480. * by boot firmware. Modern chips have 128 or 256 bytes.
  481. */
  482. #define NVRAM_OFFSET (RTC_REG_D + 1)
  483. static int cmos_nvram_read(void *priv, unsigned int off, void *val,
  484. size_t count)
  485. {
  486. unsigned char *buf = val;
  487. int retval;
  488. off += NVRAM_OFFSET;
  489. spin_lock_irq(&rtc_lock);
  490. for (retval = 0; count; count--, off++, retval++) {
  491. if (off < 128)
  492. *buf++ = CMOS_READ(off);
  493. else if (can_bank2)
  494. *buf++ = cmos_read_bank2(off);
  495. else
  496. break;
  497. }
  498. spin_unlock_irq(&rtc_lock);
  499. return retval;
  500. }
  501. static int cmos_nvram_write(void *priv, unsigned int off, void *val,
  502. size_t count)
  503. {
  504. struct cmos_rtc *cmos = priv;
  505. unsigned char *buf = val;
  506. int retval;
  507. /* NOTE: on at least PCs and Ataris, the boot firmware uses a
  508. * checksum on part of the NVRAM data. That's currently ignored
  509. * here. If userspace is smart enough to know what fields of
  510. * NVRAM to update, updating checksums is also part of its job.
  511. */
  512. off += NVRAM_OFFSET;
  513. spin_lock_irq(&rtc_lock);
  514. for (retval = 0; count; count--, off++, retval++) {
  515. /* don't trash RTC registers */
  516. if (off == cmos->day_alrm
  517. || off == cmos->mon_alrm
  518. || off == cmos->century)
  519. buf++;
  520. else if (off < 128)
  521. CMOS_WRITE(*buf++, off);
  522. else if (can_bank2)
  523. cmos_write_bank2(*buf++, off);
  524. else
  525. break;
  526. }
  527. spin_unlock_irq(&rtc_lock);
  528. return retval;
  529. }
  530. /*----------------------------------------------------------------*/
  531. static struct cmos_rtc cmos_rtc;
  532. static irqreturn_t cmos_interrupt(int irq, void *p)
  533. {
  534. u8 irqstat;
  535. u8 rtc_control;
  536. spin_lock(&rtc_lock);
  537. /* When the HPET interrupt handler calls us, the interrupt
  538. * status is passed as arg1 instead of the irq number. But
  539. * always clear irq status, even when HPET is in the way.
  540. *
  541. * Note that HPET and RTC are almost certainly out of phase,
  542. * giving different IRQ status ...
  543. */
  544. irqstat = CMOS_READ(RTC_INTR_FLAGS);
  545. rtc_control = CMOS_READ(RTC_CONTROL);
  546. if (use_hpet_alarm())
  547. irqstat = (unsigned long)irq & 0xF0;
  548. /* If we were suspended, RTC_CONTROL may not be accurate since the
  549. * bios may have cleared it.
  550. */
  551. if (!cmos_rtc.suspend_ctrl)
  552. irqstat &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  553. else
  554. irqstat &= (cmos_rtc.suspend_ctrl & RTC_IRQMASK) | RTC_IRQF;
  555. /* All Linux RTC alarms should be treated as if they were oneshot.
  556. * Similar code may be needed in system wakeup paths, in case the
  557. * alarm woke the system.
  558. */
  559. if (irqstat & RTC_AIE) {
  560. cmos_rtc.suspend_ctrl &= ~RTC_AIE;
  561. rtc_control &= ~RTC_AIE;
  562. CMOS_WRITE(rtc_control, RTC_CONTROL);
  563. if (use_hpet_alarm())
  564. hpet_mask_rtc_irq_bit(RTC_AIE);
  565. CMOS_READ(RTC_INTR_FLAGS);
  566. }
  567. spin_unlock(&rtc_lock);
  568. if (is_intr(irqstat)) {
  569. rtc_update_irq(p, 1, irqstat);
  570. return IRQ_HANDLED;
  571. } else
  572. return IRQ_NONE;
  573. }
  574. #ifdef CONFIG_PNP
  575. #define INITSECTION
  576. #else
  577. #define INITSECTION __init
  578. #endif
  579. static int INITSECTION
  580. cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
  581. {
  582. struct cmos_rtc_board_info *info = dev_get_platdata(dev);
  583. int retval = 0;
  584. unsigned char rtc_control;
  585. unsigned address_space;
  586. u32 flags = 0;
  587. struct nvmem_config nvmem_cfg = {
  588. .name = "cmos_nvram",
  589. .word_size = 1,
  590. .stride = 1,
  591. .reg_read = cmos_nvram_read,
  592. .reg_write = cmos_nvram_write,
  593. .priv = &cmos_rtc,
  594. };
  595. /* there can be only one ... */
  596. if (cmos_rtc.dev)
  597. return -EBUSY;
  598. if (!ports)
  599. return -ENODEV;
  600. /* Claim I/O ports ASAP, minimizing conflict with legacy driver.
  601. *
  602. * REVISIT non-x86 systems may instead use memory space resources
  603. * (needing ioremap etc), not i/o space resources like this ...
  604. */
  605. if (RTC_IOMAPPED)
  606. ports = request_region(ports->start, resource_size(ports),
  607. driver_name);
  608. else
  609. ports = request_mem_region(ports->start, resource_size(ports),
  610. driver_name);
  611. if (!ports) {
  612. dev_dbg(dev, "i/o registers already in use\n");
  613. return -EBUSY;
  614. }
  615. cmos_rtc.irq = rtc_irq;
  616. cmos_rtc.iomem = ports;
  617. /* Heuristic to deduce NVRAM size ... do what the legacy NVRAM
  618. * driver did, but don't reject unknown configs. Old hardware
  619. * won't address 128 bytes. Newer chips have multiple banks,
  620. * though they may not be listed in one I/O resource.
  621. */
  622. #if defined(CONFIG_ATARI)
  623. address_space = 64;
  624. #elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) \
  625. || defined(__sparc__) || defined(__mips__) \
  626. || defined(__powerpc__)
  627. address_space = 128;
  628. #else
  629. #warning Assuming 128 bytes of RTC+NVRAM address space, not 64 bytes.
  630. address_space = 128;
  631. #endif
  632. if (can_bank2 && ports->end > (ports->start + 1))
  633. address_space = 256;
  634. /* For ACPI systems extension info comes from the FADT. On others,
  635. * board specific setup provides it as appropriate. Systems where
  636. * the alarm IRQ isn't automatically a wakeup IRQ (like ACPI, and
  637. * some almost-clones) can provide hooks to make that behave.
  638. *
  639. * Note that ACPI doesn't preclude putting these registers into
  640. * "extended" areas of the chip, including some that we won't yet
  641. * expect CMOS_READ and friends to handle.
  642. */
  643. if (info) {
  644. if (info->flags)
  645. flags = info->flags;
  646. if (info->address_space)
  647. address_space = info->address_space;
  648. if (info->rtc_day_alarm && info->rtc_day_alarm < 128)
  649. cmos_rtc.day_alrm = info->rtc_day_alarm;
  650. if (info->rtc_mon_alarm && info->rtc_mon_alarm < 128)
  651. cmos_rtc.mon_alrm = info->rtc_mon_alarm;
  652. if (info->rtc_century && info->rtc_century < 128)
  653. cmos_rtc.century = info->rtc_century;
  654. if (info->wake_on && info->wake_off) {
  655. cmos_rtc.wake_on = info->wake_on;
  656. cmos_rtc.wake_off = info->wake_off;
  657. }
  658. }
  659. cmos_rtc.dev = dev;
  660. dev_set_drvdata(dev, &cmos_rtc);
  661. cmos_rtc.rtc = devm_rtc_allocate_device(dev);
  662. if (IS_ERR(cmos_rtc.rtc)) {
  663. retval = PTR_ERR(cmos_rtc.rtc);
  664. goto cleanup0;
  665. }
  666. rename_region(ports, dev_name(&cmos_rtc.rtc->dev));
  667. spin_lock_irq(&rtc_lock);
  668. if (!(flags & CMOS_RTC_FLAGS_NOFREQ)) {
  669. /* force periodic irq to CMOS reset default of 1024Hz;
  670. *
  671. * REVISIT it's been reported that at least one x86_64 ALI
  672. * mobo doesn't use 32KHz here ... for portability we might
  673. * need to do something about other clock frequencies.
  674. */
  675. cmos_rtc.rtc->irq_freq = 1024;
  676. if (use_hpet_alarm())
  677. hpet_set_periodic_freq(cmos_rtc.rtc->irq_freq);
  678. CMOS_WRITE(RTC_REF_CLCK_32KHZ | 0x06, RTC_FREQ_SELECT);
  679. }
  680. /* disable irqs */
  681. if (is_valid_irq(rtc_irq))
  682. cmos_irq_disable(&cmos_rtc, RTC_PIE | RTC_AIE | RTC_UIE);
  683. rtc_control = CMOS_READ(RTC_CONTROL);
  684. spin_unlock_irq(&rtc_lock);
  685. if (is_valid_irq(rtc_irq) && !(rtc_control & RTC_24H)) {
  686. dev_warn(dev, "only 24-hr supported\n");
  687. retval = -ENXIO;
  688. goto cleanup1;
  689. }
  690. if (use_hpet_alarm())
  691. hpet_rtc_timer_init();
  692. if (is_valid_irq(rtc_irq)) {
  693. irq_handler_t rtc_cmos_int_handler;
  694. if (use_hpet_alarm()) {
  695. rtc_cmos_int_handler = hpet_rtc_interrupt;
  696. retval = hpet_register_irq_handler(cmos_interrupt);
  697. if (retval) {
  698. hpet_mask_rtc_irq_bit(RTC_IRQMASK);
  699. dev_warn(dev, "hpet_register_irq_handler "
  700. " failed in rtc_init().");
  701. goto cleanup1;
  702. }
  703. } else
  704. rtc_cmos_int_handler = cmos_interrupt;
  705. retval = request_irq(rtc_irq, rtc_cmos_int_handler,
  706. IRQF_SHARED, dev_name(&cmos_rtc.rtc->dev),
  707. cmos_rtc.rtc);
  708. if (retval < 0) {
  709. dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
  710. goto cleanup1;
  711. }
  712. }
  713. cmos_rtc.rtc->ops = &cmos_rtc_ops;
  714. cmos_rtc.rtc->nvram_old_abi = true;
  715. retval = rtc_register_device(cmos_rtc.rtc);
  716. if (retval)
  717. goto cleanup2;
  718. /* export at least the first block of NVRAM */
  719. nvmem_cfg.size = address_space - NVRAM_OFFSET;
  720. if (rtc_nvmem_register(cmos_rtc.rtc, &nvmem_cfg))
  721. dev_err(dev, "nvmem registration failed\n");
  722. dev_info(dev, "%s%s, %d bytes nvram%s\n",
  723. !is_valid_irq(rtc_irq) ? "no alarms" :
  724. cmos_rtc.mon_alrm ? "alarms up to one year" :
  725. cmos_rtc.day_alrm ? "alarms up to one month" :
  726. "alarms up to one day",
  727. cmos_rtc.century ? ", y3k" : "",
  728. nvmem_cfg.size,
  729. use_hpet_alarm() ? ", hpet irqs" : "");
  730. return 0;
  731. cleanup2:
  732. if (is_valid_irq(rtc_irq))
  733. free_irq(rtc_irq, cmos_rtc.rtc);
  734. cleanup1:
  735. cmos_rtc.dev = NULL;
  736. cleanup0:
  737. if (RTC_IOMAPPED)
  738. release_region(ports->start, resource_size(ports));
  739. else
  740. release_mem_region(ports->start, resource_size(ports));
  741. return retval;
  742. }
  743. static void cmos_do_shutdown(int rtc_irq)
  744. {
  745. spin_lock_irq(&rtc_lock);
  746. if (is_valid_irq(rtc_irq))
  747. cmos_irq_disable(&cmos_rtc, RTC_IRQMASK);
  748. spin_unlock_irq(&rtc_lock);
  749. }
  750. static void cmos_do_remove(struct device *dev)
  751. {
  752. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  753. struct resource *ports;
  754. cmos_do_shutdown(cmos->irq);
  755. if (is_valid_irq(cmos->irq)) {
  756. free_irq(cmos->irq, cmos->rtc);
  757. if (use_hpet_alarm())
  758. hpet_unregister_irq_handler(cmos_interrupt);
  759. }
  760. cmos->rtc = NULL;
  761. ports = cmos->iomem;
  762. if (RTC_IOMAPPED)
  763. release_region(ports->start, resource_size(ports));
  764. else
  765. release_mem_region(ports->start, resource_size(ports));
  766. cmos->iomem = NULL;
  767. cmos->dev = NULL;
  768. }
  769. static int cmos_aie_poweroff(struct device *dev)
  770. {
  771. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  772. struct rtc_time now;
  773. time64_t t_now;
  774. int retval = 0;
  775. unsigned char rtc_control;
  776. if (!cmos->alarm_expires)
  777. return -EINVAL;
  778. spin_lock_irq(&rtc_lock);
  779. rtc_control = CMOS_READ(RTC_CONTROL);
  780. spin_unlock_irq(&rtc_lock);
  781. /* We only care about the situation where AIE is disabled. */
  782. if (rtc_control & RTC_AIE)
  783. return -EBUSY;
  784. cmos_read_time(dev, &now);
  785. t_now = rtc_tm_to_time64(&now);
  786. /*
  787. * When enabling "RTC wake-up" in BIOS setup, the machine reboots
  788. * automatically right after shutdown on some buggy boxes.
  789. * This automatic rebooting issue won't happen when the alarm
  790. * time is larger than now+1 seconds.
  791. *
  792. * If the alarm time is equal to now+1 seconds, the issue can be
  793. * prevented by cancelling the alarm.
  794. */
  795. if (cmos->alarm_expires == t_now + 1) {
  796. struct rtc_wkalrm alarm;
  797. /* Cancel the AIE timer by configuring the past time. */
  798. rtc_time64_to_tm(t_now - 1, &alarm.time);
  799. alarm.enabled = 0;
  800. retval = cmos_set_alarm(dev, &alarm);
  801. } else if (cmos->alarm_expires > t_now + 1) {
  802. retval = -EBUSY;
  803. }
  804. return retval;
  805. }
  806. static int cmos_suspend(struct device *dev)
  807. {
  808. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  809. unsigned char tmp;
  810. /* only the alarm might be a wakeup event source */
  811. spin_lock_irq(&rtc_lock);
  812. cmos->suspend_ctrl = tmp = CMOS_READ(RTC_CONTROL);
  813. if (tmp & (RTC_PIE|RTC_AIE|RTC_UIE)) {
  814. unsigned char mask;
  815. if (device_may_wakeup(dev))
  816. mask = RTC_IRQMASK & ~RTC_AIE;
  817. else
  818. mask = RTC_IRQMASK;
  819. tmp &= ~mask;
  820. CMOS_WRITE(tmp, RTC_CONTROL);
  821. if (use_hpet_alarm())
  822. hpet_mask_rtc_irq_bit(mask);
  823. cmos_checkintr(cmos, tmp);
  824. }
  825. spin_unlock_irq(&rtc_lock);
  826. if ((tmp & RTC_AIE) && !cmos_use_acpi_alarm()) {
  827. cmos->enabled_wake = 1;
  828. if (cmos->wake_on)
  829. cmos->wake_on(dev);
  830. else
  831. enable_irq_wake(cmos->irq);
  832. }
  833. cmos_read_alarm(dev, &cmos->saved_wkalrm);
  834. dev_dbg(dev, "suspend%s, ctrl %02x\n",
  835. (tmp & RTC_AIE) ? ", alarm may wake" : "",
  836. tmp);
  837. return 0;
  838. }
  839. /* We want RTC alarms to wake us from e.g. ACPI G2/S5 "soft off", even
  840. * after a detour through G3 "mechanical off", although the ACPI spec
  841. * says wakeup should only work from G1/S4 "hibernate". To most users,
  842. * distinctions between S4 and S5 are pointless. So when the hardware
  843. * allows, don't draw that distinction.
  844. */
  845. static inline int cmos_poweroff(struct device *dev)
  846. {
  847. if (!IS_ENABLED(CONFIG_PM))
  848. return -ENOSYS;
  849. return cmos_suspend(dev);
  850. }
  851. static void cmos_check_wkalrm(struct device *dev)
  852. {
  853. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  854. struct rtc_wkalrm current_alarm;
  855. time64_t t_now;
  856. time64_t t_current_expires;
  857. time64_t t_saved_expires;
  858. struct rtc_time now;
  859. /* Check if we have RTC Alarm armed */
  860. if (!(cmos->suspend_ctrl & RTC_AIE))
  861. return;
  862. cmos_read_time(dev, &now);
  863. t_now = rtc_tm_to_time64(&now);
  864. /*
  865. * ACPI RTC wake event is cleared after resume from STR,
  866. * ACK the rtc irq here
  867. */
  868. if (t_now >= cmos->alarm_expires && cmos_use_acpi_alarm()) {
  869. cmos_interrupt(0, (void *)cmos->rtc);
  870. return;
  871. }
  872. cmos_read_alarm(dev, &current_alarm);
  873. t_current_expires = rtc_tm_to_time64(&current_alarm.time);
  874. t_saved_expires = rtc_tm_to_time64(&cmos->saved_wkalrm.time);
  875. if (t_current_expires != t_saved_expires ||
  876. cmos->saved_wkalrm.enabled != current_alarm.enabled) {
  877. cmos_set_alarm(dev, &cmos->saved_wkalrm);
  878. }
  879. }
  880. static void cmos_check_acpi_rtc_status(struct device *dev,
  881. unsigned char *rtc_control);
  882. static int __maybe_unused cmos_resume(struct device *dev)
  883. {
  884. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  885. unsigned char tmp;
  886. if (cmos->enabled_wake && !cmos_use_acpi_alarm()) {
  887. if (cmos->wake_off)
  888. cmos->wake_off(dev);
  889. else
  890. disable_irq_wake(cmos->irq);
  891. cmos->enabled_wake = 0;
  892. }
  893. /* The BIOS might have changed the alarm, restore it */
  894. cmos_check_wkalrm(dev);
  895. spin_lock_irq(&rtc_lock);
  896. tmp = cmos->suspend_ctrl;
  897. cmos->suspend_ctrl = 0;
  898. /* re-enable any irqs previously active */
  899. if (tmp & RTC_IRQMASK) {
  900. unsigned char mask;
  901. if (device_may_wakeup(dev) && use_hpet_alarm())
  902. hpet_rtc_timer_init();
  903. do {
  904. CMOS_WRITE(tmp, RTC_CONTROL);
  905. if (use_hpet_alarm())
  906. hpet_set_rtc_irq_bit(tmp & RTC_IRQMASK);
  907. mask = CMOS_READ(RTC_INTR_FLAGS);
  908. mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
  909. if (!use_hpet_alarm() || !is_intr(mask))
  910. break;
  911. /* force one-shot behavior if HPET blocked
  912. * the wake alarm's irq
  913. */
  914. rtc_update_irq(cmos->rtc, 1, mask);
  915. tmp &= ~RTC_AIE;
  916. hpet_mask_rtc_irq_bit(RTC_AIE);
  917. } while (mask & RTC_AIE);
  918. if (tmp & RTC_AIE)
  919. cmos_check_acpi_rtc_status(dev, &tmp);
  920. }
  921. spin_unlock_irq(&rtc_lock);
  922. dev_dbg(dev, "resume, ctrl %02x\n", tmp);
  923. return 0;
  924. }
  925. static SIMPLE_DEV_PM_OPS(cmos_pm_ops, cmos_suspend, cmos_resume);
  926. /*----------------------------------------------------------------*/
  927. /* On non-x86 systems, a "CMOS" RTC lives most naturally on platform_bus.
  928. * ACPI systems always list these as PNPACPI devices, and pre-ACPI PCs
  929. * probably list them in similar PNPBIOS tables; so PNP is more common.
  930. *
  931. * We don't use legacy "poke at the hardware" probing. Ancient PCs that
  932. * predate even PNPBIOS should set up platform_bus devices.
  933. */
  934. #ifdef CONFIG_ACPI
  935. #include <linux/acpi.h>
  936. static u32 rtc_handler(void *context)
  937. {
  938. struct device *dev = context;
  939. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  940. unsigned char rtc_control = 0;
  941. unsigned char rtc_intr;
  942. unsigned long flags;
  943. /*
  944. * Always update rtc irq when ACPI is used as RTC Alarm.
  945. * Or else, ACPI SCI is enabled during suspend/resume only,
  946. * update rtc irq in that case.
  947. */
  948. if (cmos_use_acpi_alarm())
  949. cmos_interrupt(0, (void *)cmos->rtc);
  950. else {
  951. /* Fix me: can we use cmos_interrupt() here as well? */
  952. spin_lock_irqsave(&rtc_lock, flags);
  953. if (cmos_rtc.suspend_ctrl)
  954. rtc_control = CMOS_READ(RTC_CONTROL);
  955. if (rtc_control & RTC_AIE) {
  956. cmos_rtc.suspend_ctrl &= ~RTC_AIE;
  957. CMOS_WRITE(rtc_control, RTC_CONTROL);
  958. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  959. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  960. }
  961. spin_unlock_irqrestore(&rtc_lock, flags);
  962. }
  963. pm_wakeup_hard_event(dev);
  964. acpi_clear_event(ACPI_EVENT_RTC);
  965. acpi_disable_event(ACPI_EVENT_RTC, 0);
  966. return ACPI_INTERRUPT_HANDLED;
  967. }
  968. static inline void rtc_wake_setup(struct device *dev)
  969. {
  970. acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, dev);
  971. /*
  972. * After the RTC handler is installed, the Fixed_RTC event should
  973. * be disabled. Only when the RTC alarm is set will it be enabled.
  974. */
  975. acpi_clear_event(ACPI_EVENT_RTC);
  976. acpi_disable_event(ACPI_EVENT_RTC, 0);
  977. }
  978. static void rtc_wake_on(struct device *dev)
  979. {
  980. acpi_clear_event(ACPI_EVENT_RTC);
  981. acpi_enable_event(ACPI_EVENT_RTC, 0);
  982. }
  983. static void rtc_wake_off(struct device *dev)
  984. {
  985. acpi_disable_event(ACPI_EVENT_RTC, 0);
  986. }
  987. #ifdef CONFIG_X86
  988. /* Enable use_acpi_alarm mode for Intel platforms no earlier than 2015 */
  989. static void use_acpi_alarm_quirks(void)
  990. {
  991. int year;
  992. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  993. return;
  994. if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0))
  995. return;
  996. if (!is_hpet_enabled())
  997. return;
  998. if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year >= 2015)
  999. use_acpi_alarm = true;
  1000. }
  1001. #else
  1002. static inline void use_acpi_alarm_quirks(void) { }
  1003. #endif
  1004. /* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find
  1005. * its device node and pass extra config data. This helps its driver use
  1006. * capabilities that the now-obsolete mc146818 didn't have, and informs it
  1007. * that this board's RTC is wakeup-capable (per ACPI spec).
  1008. */
  1009. static struct cmos_rtc_board_info acpi_rtc_info;
  1010. static void cmos_wake_setup(struct device *dev)
  1011. {
  1012. if (acpi_disabled)
  1013. return;
  1014. use_acpi_alarm_quirks();
  1015. rtc_wake_setup(dev);
  1016. acpi_rtc_info.wake_on = rtc_wake_on;
  1017. acpi_rtc_info.wake_off = rtc_wake_off;
  1018. /* workaround bug in some ACPI tables */
  1019. if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) {
  1020. dev_dbg(dev, "bogus FADT month_alarm (%d)\n",
  1021. acpi_gbl_FADT.month_alarm);
  1022. acpi_gbl_FADT.month_alarm = 0;
  1023. }
  1024. acpi_rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
  1025. acpi_rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
  1026. acpi_rtc_info.rtc_century = acpi_gbl_FADT.century;
  1027. /* NOTE: S4_RTC_WAKE is NOT currently useful to Linux */
  1028. if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE)
  1029. dev_info(dev, "RTC can wake from S4\n");
  1030. dev->platform_data = &acpi_rtc_info;
  1031. /* RTC always wakes from S1/S2/S3, and often S4/STD */
  1032. device_init_wakeup(dev, 1);
  1033. }
  1034. static void cmos_check_acpi_rtc_status(struct device *dev,
  1035. unsigned char *rtc_control)
  1036. {
  1037. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  1038. acpi_event_status rtc_status;
  1039. acpi_status status;
  1040. if (acpi_gbl_FADT.flags & ACPI_FADT_FIXED_RTC)
  1041. return;
  1042. status = acpi_get_event_status(ACPI_EVENT_RTC, &rtc_status);
  1043. if (ACPI_FAILURE(status)) {
  1044. dev_err(dev, "Could not get RTC status\n");
  1045. } else if (rtc_status & ACPI_EVENT_FLAG_SET) {
  1046. unsigned char mask;
  1047. *rtc_control &= ~RTC_AIE;
  1048. CMOS_WRITE(*rtc_control, RTC_CONTROL);
  1049. mask = CMOS_READ(RTC_INTR_FLAGS);
  1050. rtc_update_irq(cmos->rtc, 1, mask);
  1051. }
  1052. }
  1053. #else
  1054. static void cmos_wake_setup(struct device *dev)
  1055. {
  1056. }
  1057. static void cmos_check_acpi_rtc_status(struct device *dev,
  1058. unsigned char *rtc_control)
  1059. {
  1060. }
  1061. #endif
  1062. #ifdef CONFIG_PNP
  1063. #include <linux/pnp.h>
  1064. static int cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
  1065. {
  1066. cmos_wake_setup(&pnp->dev);
  1067. if (pnp_port_start(pnp, 0) == 0x70 && !pnp_irq_valid(pnp, 0)) {
  1068. unsigned int irq = 0;
  1069. #ifdef CONFIG_X86
  1070. /* Some machines contain a PNP entry for the RTC, but
  1071. * don't define the IRQ. It should always be safe to
  1072. * hardcode it on systems with a legacy PIC.
  1073. */
  1074. if (nr_legacy_irqs())
  1075. irq = 8;
  1076. #endif
  1077. return cmos_do_probe(&pnp->dev,
  1078. pnp_get_resource(pnp, IORESOURCE_IO, 0), irq);
  1079. } else {
  1080. return cmos_do_probe(&pnp->dev,
  1081. pnp_get_resource(pnp, IORESOURCE_IO, 0),
  1082. pnp_irq(pnp, 0));
  1083. }
  1084. }
  1085. static void cmos_pnp_remove(struct pnp_dev *pnp)
  1086. {
  1087. cmos_do_remove(&pnp->dev);
  1088. }
  1089. static void cmos_pnp_shutdown(struct pnp_dev *pnp)
  1090. {
  1091. struct device *dev = &pnp->dev;
  1092. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  1093. if (system_state == SYSTEM_POWER_OFF) {
  1094. int retval = cmos_poweroff(dev);
  1095. if (cmos_aie_poweroff(dev) < 0 && !retval)
  1096. return;
  1097. }
  1098. cmos_do_shutdown(cmos->irq);
  1099. }
  1100. static const struct pnp_device_id rtc_ids[] = {
  1101. { .id = "PNP0b00", },
  1102. { .id = "PNP0b01", },
  1103. { .id = "PNP0b02", },
  1104. { },
  1105. };
  1106. MODULE_DEVICE_TABLE(pnp, rtc_ids);
  1107. static struct pnp_driver cmos_pnp_driver = {
  1108. .name = (char *) driver_name,
  1109. .id_table = rtc_ids,
  1110. .probe = cmos_pnp_probe,
  1111. .remove = cmos_pnp_remove,
  1112. .shutdown = cmos_pnp_shutdown,
  1113. /* flag ensures resume() gets called, and stops syslog spam */
  1114. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  1115. .driver = {
  1116. .pm = &cmos_pm_ops,
  1117. },
  1118. };
  1119. #endif /* CONFIG_PNP */
  1120. #ifdef CONFIG_OF
  1121. static const struct of_device_id of_cmos_match[] = {
  1122. {
  1123. .compatible = "motorola,mc146818",
  1124. },
  1125. { },
  1126. };
  1127. MODULE_DEVICE_TABLE(of, of_cmos_match);
  1128. static __init void cmos_of_init(struct platform_device *pdev)
  1129. {
  1130. struct device_node *node = pdev->dev.of_node;
  1131. const __be32 *val;
  1132. if (!node)
  1133. return;
  1134. val = of_get_property(node, "ctrl-reg", NULL);
  1135. if (val)
  1136. CMOS_WRITE(be32_to_cpup(val), RTC_CONTROL);
  1137. val = of_get_property(node, "freq-reg", NULL);
  1138. if (val)
  1139. CMOS_WRITE(be32_to_cpup(val), RTC_FREQ_SELECT);
  1140. }
  1141. #else
  1142. static inline void cmos_of_init(struct platform_device *pdev) {}
  1143. #endif
  1144. /*----------------------------------------------------------------*/
  1145. /* Platform setup should have set up an RTC device, when PNP is
  1146. * unavailable ... this could happen even on (older) PCs.
  1147. */
  1148. static int __init cmos_platform_probe(struct platform_device *pdev)
  1149. {
  1150. struct resource *resource;
  1151. int irq;
  1152. cmos_of_init(pdev);
  1153. cmos_wake_setup(&pdev->dev);
  1154. if (RTC_IOMAPPED)
  1155. resource = platform_get_resource(pdev, IORESOURCE_IO, 0);
  1156. else
  1157. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1158. irq = platform_get_irq(pdev, 0);
  1159. if (irq < 0)
  1160. irq = -1;
  1161. return cmos_do_probe(&pdev->dev, resource, irq);
  1162. }
  1163. static int cmos_platform_remove(struct platform_device *pdev)
  1164. {
  1165. cmos_do_remove(&pdev->dev);
  1166. return 0;
  1167. }
  1168. static void cmos_platform_shutdown(struct platform_device *pdev)
  1169. {
  1170. struct device *dev = &pdev->dev;
  1171. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  1172. if (system_state == SYSTEM_POWER_OFF) {
  1173. int retval = cmos_poweroff(dev);
  1174. if (cmos_aie_poweroff(dev) < 0 && !retval)
  1175. return;
  1176. }
  1177. cmos_do_shutdown(cmos->irq);
  1178. }
  1179. /* work with hotplug and coldplug */
  1180. MODULE_ALIAS("platform:rtc_cmos");
  1181. static struct platform_driver cmos_platform_driver = {
  1182. .remove = cmos_platform_remove,
  1183. .shutdown = cmos_platform_shutdown,
  1184. .driver = {
  1185. .name = driver_name,
  1186. .pm = &cmos_pm_ops,
  1187. .of_match_table = of_match_ptr(of_cmos_match),
  1188. }
  1189. };
  1190. #ifdef CONFIG_PNP
  1191. static bool pnp_driver_registered;
  1192. #endif
  1193. static bool platform_driver_registered;
  1194. static int __init cmos_init(void)
  1195. {
  1196. int retval = 0;
  1197. #ifdef CONFIG_PNP
  1198. retval = pnp_register_driver(&cmos_pnp_driver);
  1199. if (retval == 0)
  1200. pnp_driver_registered = true;
  1201. #endif
  1202. if (!cmos_rtc.dev) {
  1203. retval = platform_driver_probe(&cmos_platform_driver,
  1204. cmos_platform_probe);
  1205. if (retval == 0)
  1206. platform_driver_registered = true;
  1207. }
  1208. if (retval == 0)
  1209. return 0;
  1210. #ifdef CONFIG_PNP
  1211. if (pnp_driver_registered)
  1212. pnp_unregister_driver(&cmos_pnp_driver);
  1213. #endif
  1214. return retval;
  1215. }
  1216. module_init(cmos_init);
  1217. static void __exit cmos_exit(void)
  1218. {
  1219. #ifdef CONFIG_PNP
  1220. if (pnp_driver_registered)
  1221. pnp_unregister_driver(&cmos_pnp_driver);
  1222. #endif
  1223. if (platform_driver_registered)
  1224. platform_driver_unregister(&cmos_platform_driver);
  1225. }
  1226. module_exit(cmos_exit);
  1227. MODULE_AUTHOR("David Brownell");
  1228. MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
  1229. MODULE_LICENSE("GPL");