rtc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * RTC related functions
  4. */
  5. #include <linux/platform_device.h>
  6. #include <linux/mc146818rtc.h>
  7. #include <linux/acpi.h>
  8. #include <linux/bcd.h>
  9. #include <linux/export.h>
  10. #include <linux/pnp.h>
  11. #include <linux/of.h>
  12. #include <asm/vsyscall.h>
  13. #include <asm/x86_init.h>
  14. #include <asm/time.h>
  15. #include <asm/intel-mid.h>
  16. #include <asm/setup.h>
  17. #ifdef CONFIG_X86_32
  18. /*
  19. * This is a special lock that is owned by the CPU and holds the index
  20. * register we are working with. It is required for NMI access to the
  21. * CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details.
  22. */
  23. volatile unsigned long cmos_lock;
  24. EXPORT_SYMBOL(cmos_lock);
  25. #endif /* CONFIG_X86_32 */
  26. /* For two digit years assume time is always after that */
  27. #define CMOS_YEARS_OFFS 2000
  28. DEFINE_SPINLOCK(rtc_lock);
  29. EXPORT_SYMBOL(rtc_lock);
  30. /*
  31. * In order to set the CMOS clock precisely, set_rtc_mmss has to be
  32. * called 500 ms after the second nowtime has started, because when
  33. * nowtime is written into the registers of the CMOS clock, it will
  34. * jump to the next second precisely 500 ms later. Check the Motorola
  35. * MC146818A or Dallas DS12887 data sheet for details.
  36. */
  37. int mach_set_rtc_mmss(const struct timespec64 *now)
  38. {
  39. unsigned long long nowtime = now->tv_sec;
  40. struct rtc_time tm;
  41. int retval = 0;
  42. rtc_time64_to_tm(nowtime, &tm);
  43. if (!rtc_valid_tm(&tm)) {
  44. retval = mc146818_set_time(&tm);
  45. if (retval)
  46. printk(KERN_ERR "%s: RTC write failed with error %d\n",
  47. __func__, retval);
  48. } else {
  49. printk(KERN_ERR
  50. "%s: Invalid RTC value: write of %llx to RTC failed\n",
  51. __func__, nowtime);
  52. retval = -EINVAL;
  53. }
  54. return retval;
  55. }
  56. void mach_get_cmos_time(struct timespec64 *now)
  57. {
  58. unsigned int status, year, mon, day, hour, min, sec, century = 0;
  59. unsigned long flags;
  60. /*
  61. * If pm_trace abused the RTC as storage, set the timespec to 0,
  62. * which tells the caller that this RTC value is unusable.
  63. */
  64. if (!pm_trace_rtc_valid()) {
  65. now->tv_sec = now->tv_nsec = 0;
  66. return;
  67. }
  68. spin_lock_irqsave(&rtc_lock, flags);
  69. /*
  70. * If UIP is clear, then we have >= 244 microseconds before
  71. * RTC registers will be updated. Spec sheet says that this
  72. * is the reliable way to read RTC - registers. If UIP is set
  73. * then the register access might be invalid.
  74. */
  75. while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  76. cpu_relax();
  77. sec = CMOS_READ(RTC_SECONDS);
  78. min = CMOS_READ(RTC_MINUTES);
  79. hour = CMOS_READ(RTC_HOURS);
  80. day = CMOS_READ(RTC_DAY_OF_MONTH);
  81. mon = CMOS_READ(RTC_MONTH);
  82. year = CMOS_READ(RTC_YEAR);
  83. #ifdef CONFIG_ACPI
  84. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  85. acpi_gbl_FADT.century)
  86. century = CMOS_READ(acpi_gbl_FADT.century);
  87. #endif
  88. status = CMOS_READ(RTC_CONTROL);
  89. WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
  90. spin_unlock_irqrestore(&rtc_lock, flags);
  91. if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
  92. sec = bcd2bin(sec);
  93. min = bcd2bin(min);
  94. hour = bcd2bin(hour);
  95. day = bcd2bin(day);
  96. mon = bcd2bin(mon);
  97. year = bcd2bin(year);
  98. }
  99. if (century) {
  100. century = bcd2bin(century);
  101. year += century * 100;
  102. } else
  103. year += CMOS_YEARS_OFFS;
  104. now->tv_sec = mktime64(year, mon, day, hour, min, sec);
  105. now->tv_nsec = 0;
  106. }
  107. /* Routines for accessing the CMOS RAM/RTC. */
  108. unsigned char rtc_cmos_read(unsigned char addr)
  109. {
  110. unsigned char val;
  111. lock_cmos_prefix(addr);
  112. outb(addr, RTC_PORT(0));
  113. val = inb(RTC_PORT(1));
  114. lock_cmos_suffix(addr);
  115. return val;
  116. }
  117. EXPORT_SYMBOL(rtc_cmos_read);
  118. void rtc_cmos_write(unsigned char val, unsigned char addr)
  119. {
  120. lock_cmos_prefix(addr);
  121. outb(addr, RTC_PORT(0));
  122. outb(val, RTC_PORT(1));
  123. lock_cmos_suffix(addr);
  124. }
  125. EXPORT_SYMBOL(rtc_cmos_write);
  126. int update_persistent_clock64(struct timespec64 now)
  127. {
  128. return x86_platform.set_wallclock(&now);
  129. }
  130. /* not static: needed by APM */
  131. void read_persistent_clock64(struct timespec64 *ts)
  132. {
  133. x86_platform.get_wallclock(ts);
  134. }
  135. static struct resource rtc_resources[] = {
  136. [0] = {
  137. .start = RTC_PORT(0),
  138. .end = RTC_PORT(1),
  139. .flags = IORESOURCE_IO,
  140. },
  141. [1] = {
  142. .start = RTC_IRQ,
  143. .end = RTC_IRQ,
  144. .flags = IORESOURCE_IRQ,
  145. }
  146. };
  147. static struct platform_device rtc_device = {
  148. .name = "rtc_cmos",
  149. .id = -1,
  150. .resource = rtc_resources,
  151. .num_resources = ARRAY_SIZE(rtc_resources),
  152. };
  153. static __init int add_rtc_cmos(void)
  154. {
  155. #ifdef CONFIG_PNP
  156. static const char * const ids[] __initconst =
  157. { "PNP0b00", "PNP0b01", "PNP0b02", };
  158. struct pnp_dev *dev;
  159. struct pnp_id *id;
  160. int i;
  161. pnp_for_each_dev(dev) {
  162. for (id = dev->id; id; id = id->next) {
  163. for (i = 0; i < ARRAY_SIZE(ids); i++) {
  164. if (compare_pnp_id(id, ids[i]) != 0)
  165. return 0;
  166. }
  167. }
  168. }
  169. #endif
  170. if (!x86_platform.legacy.rtc)
  171. return -ENODEV;
  172. platform_device_register(&rtc_device);
  173. dev_info(&rtc_device.dev,
  174. "registered platform RTC device (no PNP device found)\n");
  175. return 0;
  176. }
  177. device_initcall(add_rtc_cmos);