rtc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/export.h>
  8. #include <linux/pnp.h>
  9. #include <asm/vsyscall.h>
  10. #include <asm/x86_init.h>
  11. #include <asm/time.h>
  12. #include <asm/setup.h>
  13. #ifdef CONFIG_X86_32
  14. /*
  15. * This is a special lock that is owned by the CPU and holds the index
  16. * register we are working with. It is required for NMI access to the
  17. * CMOS/RTC registers. See arch/x86/include/asm/mc146818rtc.h for details.
  18. */
  19. volatile unsigned long cmos_lock;
  20. EXPORT_SYMBOL(cmos_lock);
  21. #endif /* CONFIG_X86_32 */
  22. DEFINE_SPINLOCK(rtc_lock);
  23. EXPORT_SYMBOL(rtc_lock);
  24. /*
  25. * In order to set the CMOS clock precisely, mach_set_cmos_time has to be
  26. * called 500 ms after the second nowtime has started, because when
  27. * nowtime is written into the registers of the CMOS clock, it will
  28. * jump to the next second precisely 500 ms later. Check the Motorola
  29. * MC146818A or Dallas DS12887 data sheet for details.
  30. */
  31. int mach_set_cmos_time(const struct timespec64 *now)
  32. {
  33. unsigned long long nowtime = now->tv_sec;
  34. struct rtc_time tm;
  35. int retval = 0;
  36. rtc_time64_to_tm(nowtime, &tm);
  37. if (!rtc_valid_tm(&tm)) {
  38. retval = mc146818_set_time(&tm);
  39. if (retval)
  40. printk(KERN_ERR "%s: RTC write failed with error %d\n",
  41. __func__, retval);
  42. } else {
  43. printk(KERN_ERR
  44. "%s: Invalid RTC value: write of %llx to RTC failed\n",
  45. __func__, nowtime);
  46. retval = -EINVAL;
  47. }
  48. return retval;
  49. }
  50. void mach_get_cmos_time(struct timespec64 *now)
  51. {
  52. struct rtc_time tm;
  53. /*
  54. * If pm_trace abused the RTC as storage, set the timespec to 0,
  55. * which tells the caller that this RTC value is unusable.
  56. */
  57. if (!pm_trace_rtc_valid()) {
  58. now->tv_sec = now->tv_nsec = 0;
  59. return;
  60. }
  61. if (mc146818_get_time(&tm, 1000)) {
  62. pr_err("Unable to read current time from RTC\n");
  63. now->tv_sec = now->tv_nsec = 0;
  64. return;
  65. }
  66. now->tv_sec = rtc_tm_to_time64(&tm);
  67. now->tv_nsec = 0;
  68. }
  69. /* Routines for accessing the CMOS RAM/RTC. */
  70. unsigned char rtc_cmos_read(unsigned char addr)
  71. {
  72. unsigned char val;
  73. lock_cmos_prefix(addr);
  74. outb(addr, RTC_PORT(0));
  75. val = inb(RTC_PORT(1));
  76. lock_cmos_suffix(addr);
  77. return val;
  78. }
  79. EXPORT_SYMBOL(rtc_cmos_read);
  80. void rtc_cmos_write(unsigned char val, unsigned char addr)
  81. {
  82. lock_cmos_prefix(addr);
  83. outb(addr, RTC_PORT(0));
  84. outb(val, RTC_PORT(1));
  85. lock_cmos_suffix(addr);
  86. }
  87. EXPORT_SYMBOL(rtc_cmos_write);
  88. int update_persistent_clock64(struct timespec64 now)
  89. {
  90. return x86_platform.set_wallclock(&now);
  91. }
  92. /* not static: needed by APM */
  93. void read_persistent_clock64(struct timespec64 *ts)
  94. {
  95. x86_platform.get_wallclock(ts);
  96. }
  97. static struct resource rtc_resources[] = {
  98. [0] = {
  99. .start = RTC_PORT(0),
  100. .end = RTC_PORT(1),
  101. .flags = IORESOURCE_IO,
  102. },
  103. [1] = {
  104. .start = RTC_IRQ,
  105. .end = RTC_IRQ,
  106. .flags = IORESOURCE_IRQ,
  107. }
  108. };
  109. static struct platform_device rtc_device = {
  110. .name = "rtc_cmos",
  111. .id = -1,
  112. .resource = rtc_resources,
  113. .num_resources = ARRAY_SIZE(rtc_resources),
  114. };
  115. static __init int add_rtc_cmos(void)
  116. {
  117. #ifdef CONFIG_PNP
  118. static const char * const ids[] __initconst =
  119. { "PNP0b00", "PNP0b01", "PNP0b02", };
  120. struct pnp_dev *dev;
  121. int i;
  122. pnp_for_each_dev(dev) {
  123. for (i = 0; i < ARRAY_SIZE(ids); i++) {
  124. if (compare_pnp_id(dev->id, ids[i]) != 0)
  125. return 0;
  126. }
  127. }
  128. #endif
  129. if (!x86_platform.legacy.rtc)
  130. return -ENODEV;
  131. platform_device_register(&rtc_device);
  132. dev_info(&rtc_device.dev,
  133. "registered platform RTC device (no PNP device found)\n");
  134. return 0;
  135. }
  136. device_initcall(add_rtc_cmos);