rtc.c 3.6 KB

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