rtc-cmos.c 38 KB

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