rtc.c 4.7 KB

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