rtc-cmos.c 37 KB

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