rtc-cmos.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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_timer_init(void)
  130. {
  131. return 0;
  132. }
  133. extern irq_handler_t hpet_rtc_interrupt;
  134. static inline int hpet_register_irq_handler(irq_handler_t handler)
  135. {
  136. return 0;
  137. }
  138. static inline int hpet_unregister_irq_handler(irq_handler_t handler)
  139. {
  140. return 0;
  141. }
  142. #endif
  143. /* Don't use HPET for RTC Alarm event if ACPI Fixed event is used */
  144. static inline int use_hpet_alarm(void)
  145. {
  146. return is_hpet_enabled() && !cmos_use_acpi_alarm();
  147. }
  148. /*----------------------------------------------------------------*/
  149. #ifdef RTC_PORT
  150. /* Most newer x86 systems have two register banks, the first used
  151. * for RTC and NVRAM and the second only for NVRAM. Caller must
  152. * own rtc_lock ... and we won't worry about access during NMI.
  153. */
  154. #define can_bank2 true
  155. static inline unsigned char cmos_read_bank2(unsigned char addr)
  156. {
  157. outb(addr, RTC_PORT(2));
  158. return inb(RTC_PORT(3));
  159. }
  160. static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
  161. {
  162. outb(addr, RTC_PORT(2));
  163. outb(val, RTC_PORT(3));
  164. }
  165. #else
  166. #define can_bank2 false
  167. static inline unsigned char cmos_read_bank2(unsigned char addr)
  168. {
  169. return 0;
  170. }
  171. static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
  172. {
  173. }
  174. #endif
  175. /*----------------------------------------------------------------*/
  176. static int cmos_read_time(struct device *dev, struct rtc_time *t)
  177. {
  178. int ret;
  179. /*
  180. * If pm_trace abused the RTC for storage, set the timespec to 0,
  181. * which tells the caller that this RTC value is unusable.
  182. */
  183. if (!pm_trace_rtc_valid())
  184. return -EIO;
  185. ret = mc146818_get_time(t, 1000);
  186. if (ret < 0) {
  187. dev_err_ratelimited(dev, "unable to read current time\n");
  188. return ret;
  189. }
  190. return 0;
  191. }
  192. static int cmos_set_time(struct device *dev, struct rtc_time *t)
  193. {
  194. /* NOTE: this ignores the issue whereby updating the seconds
  195. * takes effect exactly 500ms after we write the register.
  196. * (Also queueing and other delays before we get this far.)
  197. */
  198. return mc146818_set_time(t);
  199. }
  200. struct cmos_read_alarm_callback_param {
  201. struct cmos_rtc *cmos;
  202. struct rtc_time *time;
  203. unsigned char rtc_control;
  204. };
  205. static void cmos_read_alarm_callback(unsigned char __always_unused seconds,
  206. void *param_in)
  207. {
  208. struct cmos_read_alarm_callback_param *p =
  209. (struct cmos_read_alarm_callback_param *)param_in;
  210. struct rtc_time *time = p->time;
  211. time->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
  212. time->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
  213. time->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
  214. if (p->cmos->day_alrm) {
  215. /* ignore upper bits on readback per ACPI spec */
  216. time->tm_mday = CMOS_READ(p->cmos->day_alrm) & 0x3f;
  217. if (!time->tm_mday)
  218. time->tm_mday = -1;
  219. if (p->cmos->mon_alrm) {
  220. time->tm_mon = CMOS_READ(p->cmos->mon_alrm);
  221. if (!time->tm_mon)
  222. time->tm_mon = -1;
  223. }
  224. }
  225. p->rtc_control = CMOS_READ(RTC_CONTROL);
  226. }
  227. static int cmos_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  228. {
  229. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  230. struct cmos_read_alarm_callback_param p = {
  231. .cmos = cmos,
  232. .time = &t->time,
  233. };
  234. /* This not only a rtc_op, but also called directly */
  235. if (!is_valid_irq(cmos->irq))
  236. return -ETIMEDOUT;
  237. /* Basic alarms only support hour, minute, and seconds fields.
  238. * Some also support day and month, for alarms up to a year in
  239. * the future.
  240. */
  241. /* Some Intel chipsets disconnect the alarm registers when the clock
  242. * update is in progress - during this time reads return bogus values
  243. * and writes may fail silently. See for example "7th Generation Intel®
  244. * Processor Family I/O for U/Y Platforms [...] Datasheet", section
  245. * 27.7.1
  246. *
  247. * Use the mc146818_avoid_UIP() function to avoid this.
  248. */
  249. if (!mc146818_avoid_UIP(cmos_read_alarm_callback, 10, &p))
  250. return -EIO;
  251. if (!(p.rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  252. if (((unsigned)t->time.tm_sec) < 0x60)
  253. t->time.tm_sec = bcd2bin(t->time.tm_sec);
  254. else
  255. t->time.tm_sec = -1;
  256. if (((unsigned)t->time.tm_min) < 0x60)
  257. t->time.tm_min = bcd2bin(t->time.tm_min);
  258. else
  259. t->time.tm_min = -1;
  260. if (((unsigned)t->time.tm_hour) < 0x24)
  261. t->time.tm_hour = bcd2bin(t->time.tm_hour);
  262. else
  263. t->time.tm_hour = -1;
  264. if (cmos->day_alrm) {
  265. if (((unsigned)t->time.tm_mday) <= 0x31)
  266. t->time.tm_mday = bcd2bin(t->time.tm_mday);
  267. else
  268. t->time.tm_mday = -1;
  269. if (cmos->mon_alrm) {
  270. if (((unsigned)t->time.tm_mon) <= 0x12)
  271. t->time.tm_mon = bcd2bin(t->time.tm_mon)-1;
  272. else
  273. t->time.tm_mon = -1;
  274. }
  275. }
  276. }
  277. t->enabled = !!(p.rtc_control & RTC_AIE);
  278. t->pending = 0;
  279. return 0;
  280. }
  281. static void cmos_checkintr(struct cmos_rtc *cmos, unsigned char rtc_control)
  282. {
  283. unsigned char rtc_intr;
  284. /* NOTE after changing RTC_xIE bits we always read INTR_FLAGS;
  285. * allegedly some older rtcs need that to handle irqs properly
  286. */
  287. rtc_intr = CMOS_READ(RTC_INTR_FLAGS);
  288. if (use_hpet_alarm())
  289. return;
  290. rtc_intr &= (rtc_control & RTC_IRQMASK) | RTC_IRQF;
  291. if (is_intr(rtc_intr))
  292. rtc_update_irq(cmos->rtc, 1, rtc_intr);
  293. }
  294. static void cmos_irq_enable(struct cmos_rtc *cmos, unsigned char mask)
  295. {
  296. unsigned char rtc_control;
  297. /* flush any pending IRQ status, notably for update irqs,
  298. * before we enable new IRQs
  299. */
  300. rtc_control = CMOS_READ(RTC_CONTROL);
  301. cmos_checkintr(cmos, rtc_control);
  302. rtc_control |= mask;
  303. CMOS_WRITE(rtc_control, RTC_CONTROL);
  304. if (use_hpet_alarm())
  305. hpet_set_rtc_irq_bit(mask);
  306. if ((mask & RTC_AIE) && cmos_use_acpi_alarm()) {
  307. if (cmos->wake_on)
  308. cmos->wake_on(cmos->dev);
  309. }
  310. cmos_checkintr(cmos, rtc_control);
  311. }
  312. static void cmos_irq_disable(struct cmos_rtc *cmos, unsigned char mask)
  313. {
  314. unsigned char rtc_control;
  315. rtc_control = CMOS_READ(RTC_CONTROL);
  316. rtc_control &= ~mask;
  317. CMOS_WRITE(rtc_control, RTC_CONTROL);
  318. if (use_hpet_alarm())
  319. hpet_mask_rtc_irq_bit(mask);
  320. if ((mask & RTC_AIE) && cmos_use_acpi_alarm()) {
  321. if (cmos->wake_off)
  322. cmos->wake_off(cmos->dev);
  323. }
  324. cmos_checkintr(cmos, rtc_control);
  325. }
  326. static int cmos_validate_alarm(struct device *dev, struct rtc_wkalrm *t)
  327. {
  328. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  329. struct rtc_time now;
  330. cmos_read_time(dev, &now);
  331. if (!cmos->day_alrm) {
  332. time64_t t_max_date;
  333. time64_t t_alrm;
  334. t_max_date = rtc_tm_to_time64(&now);
  335. t_max_date += 24 * 60 * 60 - 1;
  336. t_alrm = rtc_tm_to_time64(&t->time);
  337. if (t_alrm > t_max_date) {
  338. dev_err(dev,
  339. "Alarms can be up to one day in the future\n");
  340. return -EINVAL;
  341. }
  342. } else if (!cmos->mon_alrm) {
  343. struct rtc_time max_date = now;
  344. time64_t t_max_date;
  345. time64_t t_alrm;
  346. int max_mday;
  347. if (max_date.tm_mon == 11) {
  348. max_date.tm_mon = 0;
  349. max_date.tm_year += 1;
  350. } else {
  351. max_date.tm_mon += 1;
  352. }
  353. max_mday = rtc_month_days(max_date.tm_mon, max_date.tm_year);
  354. if (max_date.tm_mday > max_mday)
  355. max_date.tm_mday = max_mday;
  356. t_max_date = rtc_tm_to_time64(&max_date);
  357. t_max_date -= 1;
  358. t_alrm = rtc_tm_to_time64(&t->time);
  359. if (t_alrm > t_max_date) {
  360. dev_err(dev,
  361. "Alarms can be up to one month in the future\n");
  362. return -EINVAL;
  363. }
  364. } else {
  365. struct rtc_time max_date = now;
  366. time64_t t_max_date;
  367. time64_t t_alrm;
  368. int max_mday;
  369. max_date.tm_year += 1;
  370. max_mday = rtc_month_days(max_date.tm_mon, max_date.tm_year);
  371. if (max_date.tm_mday > max_mday)
  372. max_date.tm_mday = max_mday;
  373. t_max_date = rtc_tm_to_time64(&max_date);
  374. t_max_date -= 1;
  375. t_alrm = rtc_tm_to_time64(&t->time);
  376. if (t_alrm > t_max_date) {
  377. dev_err(dev,
  378. "Alarms can be up to one year in the future\n");
  379. return -EINVAL;
  380. }
  381. }
  382. return 0;
  383. }
  384. struct cmos_set_alarm_callback_param {
  385. struct cmos_rtc *cmos;
  386. unsigned char mon, mday, hrs, min, sec;
  387. struct rtc_wkalrm *t;
  388. };
  389. /* Note: this function may be executed by mc146818_avoid_UIP() more then
  390. * once
  391. */
  392. static void cmos_set_alarm_callback(unsigned char __always_unused seconds,
  393. void *param_in)
  394. {
  395. struct cmos_set_alarm_callback_param *p =
  396. (struct cmos_set_alarm_callback_param *)param_in;
  397. /* next rtc irq must not be from previous alarm setting */
  398. cmos_irq_disable(p->cmos, RTC_AIE);
  399. /* update alarm */
  400. CMOS_WRITE(p->hrs, RTC_HOURS_ALARM);
  401. CMOS_WRITE(p->min, RTC_MINUTES_ALARM);
  402. CMOS_WRITE(p->sec, RTC_SECONDS_ALARM);
  403. /* the system may support an "enhanced" alarm */
  404. if (p->cmos->day_alrm) {
  405. CMOS_WRITE(p->mday, p->cmos->day_alrm);
  406. if (p->cmos->mon_alrm)
  407. CMOS_WRITE(p->mon, p->cmos->mon_alrm);
  408. }
  409. if (use_hpet_alarm()) {
  410. /*
  411. * FIXME the HPET alarm glue currently ignores day_alrm
  412. * and mon_alrm ...
  413. */
  414. hpet_set_alarm_time(p->t->time.tm_hour, p->t->time.tm_min,
  415. p->t->time.tm_sec);
  416. }
  417. if (p->t->enabled)
  418. cmos_irq_enable(p->cmos, RTC_AIE);
  419. }
  420. static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  421. {
  422. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  423. struct cmos_set_alarm_callback_param p = {
  424. .cmos = cmos,
  425. .t = t
  426. };
  427. unsigned char rtc_control;
  428. int ret;
  429. /* This not only a rtc_op, but also called directly */
  430. if (!is_valid_irq(cmos->irq))
  431. return -EIO;
  432. ret = cmos_validate_alarm(dev, t);
  433. if (ret < 0)
  434. return ret;
  435. p.mon = t->time.tm_mon + 1;
  436. p.mday = t->time.tm_mday;
  437. p.hrs = t->time.tm_hour;
  438. p.min = t->time.tm_min;
  439. p.sec = t->time.tm_sec;
  440. spin_lock_irq(&rtc_lock);
  441. rtc_control = CMOS_READ(RTC_CONTROL);
  442. spin_unlock_irq(&rtc_lock);
  443. if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  444. /* Writing 0xff means "don't care" or "match all". */
  445. p.mon = (p.mon <= 12) ? bin2bcd(p.mon) : 0xff;
  446. p.mday = (p.mday >= 1 && p.mday <= 31) ? bin2bcd(p.mday) : 0xff;
  447. p.hrs = (p.hrs < 24) ? bin2bcd(p.hrs) : 0xff;
  448. p.min = (p.min < 60) ? bin2bcd(p.min) : 0xff;
  449. p.sec = (p.sec < 60) ? bin2bcd(p.sec) : 0xff;
  450. }
  451. /*
  452. * Some Intel chipsets disconnect the alarm registers when the clock
  453. * update is in progress - during this time writes fail silently.
  454. *
  455. * Use mc146818_avoid_UIP() to avoid this.
  456. */
  457. if (!mc146818_avoid_UIP(cmos_set_alarm_callback, 10, &p))
  458. return -ETIMEDOUT;
  459. cmos->alarm_expires = rtc_tm_to_time64(&t->time);
  460. return 0;
  461. }
  462. static int cmos_alarm_irq_enable(struct device *dev, unsigned int enabled)
  463. {
  464. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  465. unsigned long flags;
  466. spin_lock_irqsave(&rtc_lock, flags);
  467. if (enabled)
  468. cmos_irq_enable(cmos, RTC_AIE);
  469. else
  470. cmos_irq_disable(cmos, RTC_AIE);
  471. spin_unlock_irqrestore(&rtc_lock, flags);
  472. return 0;
  473. }
  474. #if IS_ENABLED(CONFIG_RTC_INTF_PROC)
  475. static int cmos_procfs(struct device *dev, struct seq_file *seq)
  476. {
  477. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  478. unsigned char rtc_control, valid;
  479. spin_lock_irq(&rtc_lock);
  480. rtc_control = CMOS_READ(RTC_CONTROL);
  481. valid = CMOS_READ(RTC_VALID);
  482. spin_unlock_irq(&rtc_lock);
  483. /* NOTE: at least ICH6 reports battery status using a different
  484. * (non-RTC) bit; and SQWE is ignored on many current systems.
  485. */
  486. seq_printf(seq,
  487. "periodic_IRQ\t: %s\n"
  488. "update_IRQ\t: %s\n"
  489. "HPET_emulated\t: %s\n"
  490. // "square_wave\t: %s\n"
  491. "BCD\t\t: %s\n"
  492. "DST_enable\t: %s\n"
  493. "periodic_freq\t: %d\n"
  494. "batt_status\t: %s\n",
  495. (rtc_control & RTC_PIE) ? "yes" : "no",
  496. (rtc_control & RTC_UIE) ? "yes" : "no",
  497. use_hpet_alarm() ? "yes" : "no",
  498. // (rtc_control & RTC_SQWE) ? "yes" : "no",
  499. (rtc_control & RTC_DM_BINARY) ? "no" : "yes",
  500. (rtc_control & RTC_DST_EN) ? "yes" : "no",
  501. cmos->rtc->irq_freq,
  502. (valid & RTC_VRT) ? "okay" : "dead");
  503. return 0;
  504. }
  505. #else
  506. #define cmos_procfs NULL
  507. #endif
  508. static const struct rtc_class_ops cmos_rtc_ops = {
  509. .read_time = cmos_read_time,
  510. .set_time = cmos_set_time,
  511. .read_alarm = cmos_read_alarm,
  512. .set_alarm = cmos_set_alarm,
  513. .proc = cmos_procfs,
  514. .alarm_irq_enable = cmos_alarm_irq_enable,
  515. };
  516. /*----------------------------------------------------------------*/
  517. /*
  518. * All these chips have at least 64 bytes of address space, shared by
  519. * RTC registers and NVRAM. Most of those bytes of NVRAM are used
  520. * by boot firmware. Modern chips have 128 or 256 bytes.
  521. */
  522. #define NVRAM_OFFSET (RTC_REG_D + 1)
  523. static int cmos_nvram_read(void *priv, unsigned int off, void *val,
  524. size_t count)
  525. {
  526. unsigned char *buf = val;
  527. off += NVRAM_OFFSET;
  528. for (; count; count--, off++, buf++) {
  529. guard(spinlock_irq)(&rtc_lock);
  530. if (off < 128)
  531. *buf = CMOS_READ(off);
  532. else if (can_bank2)
  533. *buf = cmos_read_bank2(off);
  534. else
  535. return -EIO;
  536. }
  537. return 0;
  538. }
  539. static int cmos_nvram_write(void *priv, unsigned int off, void *val,
  540. size_t count)
  541. {
  542. struct cmos_rtc *cmos = priv;
  543. unsigned char *buf = val;
  544. /* NOTE: on at least PCs and Ataris, the boot firmware uses a
  545. * checksum on part of the NVRAM data. That's currently ignored
  546. * here. If userspace is smart enough to know what fields of
  547. * NVRAM to update, updating checksums is also part of its job.
  548. */
  549. off += NVRAM_OFFSET;
  550. for (; count; count--, off++, buf++) {
  551. /* don't trash RTC registers */
  552. if (off == cmos->day_alrm
  553. || off == cmos->mon_alrm
  554. || off == cmos->century)
  555. continue;
  556. guard(spinlock_irq)(&rtc_lock);
  557. if (off < 128)
  558. CMOS_WRITE(*buf, off);
  559. else if (can_bank2)
  560. cmos_write_bank2(*buf, off);
  561. else
  562. return -EIO;
  563. }
  564. return 0;
  565. }
  566. /*----------------------------------------------------------------*/
  567. static struct cmos_rtc cmos_rtc;
  568. static irqreturn_t cmos_interrupt(int irq, void *p)
  569. {
  570. u8 irqstat;
  571. u8 rtc_control;
  572. unsigned long flags;
  573. /* We cannot use spin_lock() here, as cmos_interrupt() is also called
  574. * in a non-irq context.
  575. */
  576. spin_lock_irqsave(&rtc_lock, flags);
  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_irqrestore(&rtc_lock, flags);
  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, true);
  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. cmos_interrupt(0, (void *)cmos->rtc);
  1079. return;
  1080. }
  1081. memset(&current_alarm, 0, sizeof(struct rtc_wkalrm));
  1082. cmos_read_alarm(dev, &current_alarm);
  1083. t_current_expires = rtc_tm_to_time64(&current_alarm.time);
  1084. t_saved_expires = rtc_tm_to_time64(&cmos->saved_wkalrm.time);
  1085. if (t_current_expires != t_saved_expires ||
  1086. cmos->saved_wkalrm.enabled != current_alarm.enabled) {
  1087. cmos_set_alarm(dev, &cmos->saved_wkalrm);
  1088. }
  1089. }
  1090. static int __maybe_unused cmos_resume(struct device *dev)
  1091. {
  1092. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  1093. unsigned char tmp;
  1094. if (cmos->enabled_wake && !cmos_use_acpi_alarm()) {
  1095. if (cmos->wake_off)
  1096. cmos->wake_off(dev);
  1097. else
  1098. disable_irq_wake(cmos->irq);
  1099. cmos->enabled_wake = 0;
  1100. }
  1101. /* The BIOS might have changed the alarm, restore it */
  1102. cmos_check_wkalrm(dev);
  1103. spin_lock_irq(&rtc_lock);
  1104. tmp = cmos->suspend_ctrl;
  1105. cmos->suspend_ctrl = 0;
  1106. /* re-enable any irqs previously active */
  1107. if (tmp & RTC_IRQMASK) {
  1108. unsigned char mask;
  1109. if (device_may_wakeup(dev) && use_hpet_alarm())
  1110. hpet_rtc_timer_init();
  1111. do {
  1112. CMOS_WRITE(tmp, RTC_CONTROL);
  1113. if (use_hpet_alarm())
  1114. hpet_set_rtc_irq_bit(tmp & RTC_IRQMASK);
  1115. mask = CMOS_READ(RTC_INTR_FLAGS);
  1116. mask &= (tmp & RTC_IRQMASK) | RTC_IRQF;
  1117. if (!use_hpet_alarm() || !is_intr(mask))
  1118. break;
  1119. /* force one-shot behavior if HPET blocked
  1120. * the wake alarm's irq
  1121. */
  1122. rtc_update_irq(cmos->rtc, 1, mask);
  1123. tmp &= ~RTC_AIE;
  1124. hpet_mask_rtc_irq_bit(RTC_AIE);
  1125. } while (mask & RTC_AIE);
  1126. if (tmp & RTC_AIE)
  1127. cmos_check_acpi_rtc_status(dev, &tmp);
  1128. }
  1129. spin_unlock_irq(&rtc_lock);
  1130. dev_dbg(dev, "resume, ctrl %02x\n", tmp);
  1131. return 0;
  1132. }
  1133. static SIMPLE_DEV_PM_OPS(cmos_pm_ops, cmos_suspend, cmos_resume);
  1134. /*----------------------------------------------------------------*/
  1135. /* On non-x86 systems, a "CMOS" RTC lives most naturally on platform_bus.
  1136. * ACPI systems always list these as PNPACPI devices, and pre-ACPI PCs
  1137. * probably list them in similar PNPBIOS tables; so PNP is more common.
  1138. *
  1139. * We don't use legacy "poke at the hardware" probing. Ancient PCs that
  1140. * predate even PNPBIOS should set up platform_bus devices.
  1141. */
  1142. #ifdef CONFIG_PNP
  1143. #include <linux/pnp.h>
  1144. static int cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
  1145. {
  1146. int irq;
  1147. if (pnp_port_start(pnp, 0) == 0x70 && !pnp_irq_valid(pnp, 0)) {
  1148. irq = 0;
  1149. #ifdef CONFIG_X86
  1150. /* Some machines contain a PNP entry for the RTC, but
  1151. * don't define the IRQ. It should always be safe to
  1152. * hardcode it on systems with a legacy PIC.
  1153. */
  1154. if (nr_legacy_irqs())
  1155. irq = RTC_IRQ;
  1156. #endif
  1157. } else {
  1158. irq = pnp_irq(pnp, 0);
  1159. }
  1160. return cmos_do_probe(&pnp->dev, pnp_get_resource(pnp, IORESOURCE_IO, 0), irq);
  1161. }
  1162. static void cmos_pnp_remove(struct pnp_dev *pnp)
  1163. {
  1164. cmos_do_remove(&pnp->dev);
  1165. }
  1166. static void cmos_pnp_shutdown(struct pnp_dev *pnp)
  1167. {
  1168. struct device *dev = &pnp->dev;
  1169. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  1170. if (system_state == SYSTEM_POWER_OFF) {
  1171. int retval = cmos_poweroff(dev);
  1172. if (cmos_aie_poweroff(dev) < 0 && !retval)
  1173. return;
  1174. }
  1175. cmos_do_shutdown(cmos->irq);
  1176. }
  1177. static const struct pnp_device_id rtc_ids[] = {
  1178. { .id = "PNP0b00", },
  1179. { .id = "PNP0b01", },
  1180. { .id = "PNP0b02", },
  1181. { },
  1182. };
  1183. MODULE_DEVICE_TABLE(pnp, rtc_ids);
  1184. static struct pnp_driver cmos_pnp_driver = {
  1185. .name = driver_name,
  1186. .id_table = rtc_ids,
  1187. .probe = cmos_pnp_probe,
  1188. .remove = cmos_pnp_remove,
  1189. .shutdown = cmos_pnp_shutdown,
  1190. /* flag ensures resume() gets called, and stops syslog spam */
  1191. .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
  1192. .driver = {
  1193. .pm = &cmos_pm_ops,
  1194. },
  1195. };
  1196. #endif /* CONFIG_PNP */
  1197. #ifdef CONFIG_OF
  1198. static const struct of_device_id of_cmos_match[] = {
  1199. {
  1200. .compatible = "motorola,mc146818",
  1201. },
  1202. { },
  1203. };
  1204. MODULE_DEVICE_TABLE(of, of_cmos_match);
  1205. static __init void cmos_of_init(struct platform_device *pdev)
  1206. {
  1207. struct device_node *node = pdev->dev.of_node;
  1208. const __be32 *val;
  1209. if (!node)
  1210. return;
  1211. val = of_get_property(node, "ctrl-reg", NULL);
  1212. if (val)
  1213. CMOS_WRITE(be32_to_cpup(val), RTC_CONTROL);
  1214. val = of_get_property(node, "freq-reg", NULL);
  1215. if (val)
  1216. CMOS_WRITE(be32_to_cpup(val), RTC_FREQ_SELECT);
  1217. }
  1218. #else
  1219. static inline void cmos_of_init(struct platform_device *pdev) {}
  1220. #endif
  1221. /*----------------------------------------------------------------*/
  1222. /* Platform setup should have set up an RTC device, when PNP is
  1223. * unavailable ... this could happen even on (older) PCs.
  1224. */
  1225. static int __init cmos_platform_probe(struct platform_device *pdev)
  1226. {
  1227. struct resource *resource;
  1228. int irq;
  1229. cmos_of_init(pdev);
  1230. if (RTC_IOMAPPED)
  1231. resource = platform_get_resource(pdev, IORESOURCE_IO, 0);
  1232. else
  1233. resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1234. irq = platform_get_irq(pdev, 0);
  1235. if (irq < 0)
  1236. irq = -1;
  1237. return cmos_do_probe(&pdev->dev, resource, irq);
  1238. }
  1239. static void cmos_platform_remove(struct platform_device *pdev)
  1240. {
  1241. cmos_do_remove(&pdev->dev);
  1242. }
  1243. static void cmos_platform_shutdown(struct platform_device *pdev)
  1244. {
  1245. struct device *dev = &pdev->dev;
  1246. struct cmos_rtc *cmos = dev_get_drvdata(dev);
  1247. if (system_state == SYSTEM_POWER_OFF) {
  1248. int retval = cmos_poweroff(dev);
  1249. if (cmos_aie_poweroff(dev) < 0 && !retval)
  1250. return;
  1251. }
  1252. cmos_do_shutdown(cmos->irq);
  1253. }
  1254. /* work with hotplug and coldplug */
  1255. MODULE_ALIAS("platform:rtc_cmos");
  1256. static struct platform_driver cmos_platform_driver = {
  1257. .remove = cmos_platform_remove,
  1258. .shutdown = cmos_platform_shutdown,
  1259. .driver = {
  1260. .name = driver_name,
  1261. .pm = &cmos_pm_ops,
  1262. .of_match_table = of_match_ptr(of_cmos_match),
  1263. }
  1264. };
  1265. #ifdef CONFIG_PNP
  1266. static bool pnp_driver_registered;
  1267. #endif
  1268. static bool platform_driver_registered;
  1269. static int __init cmos_init(void)
  1270. {
  1271. int retval = 0;
  1272. #ifdef CONFIG_PNP
  1273. retval = pnp_register_driver(&cmos_pnp_driver);
  1274. if (retval == 0)
  1275. pnp_driver_registered = true;
  1276. #endif
  1277. if (!cmos_rtc.dev) {
  1278. retval = platform_driver_probe(&cmos_platform_driver,
  1279. cmos_platform_probe);
  1280. if (retval == 0)
  1281. platform_driver_registered = true;
  1282. }
  1283. if (retval == 0)
  1284. return 0;
  1285. #ifdef CONFIG_PNP
  1286. if (pnp_driver_registered)
  1287. pnp_unregister_driver(&cmos_pnp_driver);
  1288. #endif
  1289. return retval;
  1290. }
  1291. module_init(cmos_init);
  1292. static void __exit cmos_exit(void)
  1293. {
  1294. #ifdef CONFIG_PNP
  1295. if (pnp_driver_registered)
  1296. pnp_unregister_driver(&cmos_pnp_driver);
  1297. #endif
  1298. if (platform_driver_registered)
  1299. platform_driver_unregister(&cmos_platform_driver);
  1300. }
  1301. module_exit(cmos_exit);
  1302. MODULE_AUTHOR("David Brownell");
  1303. MODULE_DESCRIPTION("Driver for PC-style 'CMOS' RTCs");
  1304. MODULE_LICENSE("GPL");