rtc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/rtc.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 = set_rtc_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. spin_lock_irqsave(&rtc_lock, flags);
  60. /*
  61. * If UIP is clear, then we have >= 244 microseconds before
  62. * RTC registers will be updated. Spec sheet says that this
  63. * is the reliable way to read RTC - registers. If UIP is set
  64. * then the register access might be invalid.
  65. */
  66. while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  67. cpu_relax();
  68. sec = CMOS_READ(RTC_SECONDS);
  69. min = CMOS_READ(RTC_MINUTES);
  70. hour = CMOS_READ(RTC_HOURS);
  71. day = CMOS_READ(RTC_DAY_OF_MONTH);
  72. mon = CMOS_READ(RTC_MONTH);
  73. year = CMOS_READ(RTC_YEAR);
  74. #ifdef CONFIG_ACPI
  75. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  76. acpi_gbl_FADT.century)
  77. century = CMOS_READ(acpi_gbl_FADT.century);
  78. #endif
  79. status = CMOS_READ(RTC_CONTROL);
  80. WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
  81. spin_unlock_irqrestore(&rtc_lock, flags);
  82. if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
  83. sec = bcd2bin(sec);
  84. min = bcd2bin(min);
  85. hour = bcd2bin(hour);
  86. day = bcd2bin(day);
  87. mon = bcd2bin(mon);
  88. year = bcd2bin(year);
  89. }
  90. if (century) {
  91. century = bcd2bin(century);
  92. year += century * 100;
  93. } else
  94. year += CMOS_YEARS_OFFS;
  95. now->tv_sec = mktime(year, mon, day, hour, min, sec);
  96. now->tv_nsec = 0;
  97. }
  98. /* Routines for accessing the CMOS RAM/RTC. */
  99. unsigned char rtc_cmos_read(unsigned char addr)
  100. {
  101. unsigned char val;
  102. lock_cmos_prefix(addr);
  103. outb(addr, RTC_PORT(0));
  104. val = inb(RTC_PORT(1));
  105. lock_cmos_suffix(addr);
  106. return val;
  107. }
  108. EXPORT_SYMBOL(rtc_cmos_read);
  109. void rtc_cmos_write(unsigned char val, unsigned char addr)
  110. {
  111. lock_cmos_prefix(addr);
  112. outb(addr, RTC_PORT(0));
  113. outb(val, RTC_PORT(1));
  114. lock_cmos_suffix(addr);
  115. }
  116. EXPORT_SYMBOL(rtc_cmos_write);
  117. int update_persistent_clock(struct timespec now)
  118. {
  119. return x86_platform.set_wallclock(&now);
  120. }
  121. /* not static: needed by APM */
  122. void read_persistent_clock(struct timespec *ts)
  123. {
  124. x86_platform.get_wallclock(ts);
  125. }
  126. static struct resource rtc_resources[] = {
  127. [0] = {
  128. .start = RTC_PORT(0),
  129. .end = RTC_PORT(1),
  130. .flags = IORESOURCE_IO,
  131. },
  132. [1] = {
  133. .start = RTC_IRQ,
  134. .end = RTC_IRQ,
  135. .flags = IORESOURCE_IRQ,
  136. }
  137. };
  138. static struct platform_device rtc_device = {
  139. .name = "rtc_cmos",
  140. .id = -1,
  141. .resource = rtc_resources,
  142. .num_resources = ARRAY_SIZE(rtc_resources),
  143. };
  144. static __init int add_rtc_cmos(void)
  145. {
  146. #ifdef CONFIG_PNP
  147. static const char * const ids[] __initconst =
  148. { "PNP0b00", "PNP0b01", "PNP0b02", };
  149. struct pnp_dev *dev;
  150. struct pnp_id *id;
  151. int i;
  152. pnp_for_each_dev(dev) {
  153. for (id = dev->id; id; id = id->next) {
  154. for (i = 0; i < ARRAY_SIZE(ids); i++) {
  155. if (compare_pnp_id(id, ids[i]) != 0)
  156. return 0;
  157. }
  158. }
  159. }
  160. #endif
  161. if (of_have_populated_dt())
  162. return 0;
  163. /* Intel MID platforms don't have ioport rtc */
  164. if (intel_mid_identify_cpu())
  165. return -ENODEV;
  166. #ifdef CONFIG_ACPI
  167. if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_CMOS_RTC) {
  168. /* This warning can likely go away again in a year or two. */
  169. pr_info("ACPI: not registering RTC platform device\n");
  170. return -ENODEV;
  171. }
  172. #endif
  173. if (paravirt_enabled() && !paravirt_has(RTC))
  174. return -ENODEV;
  175. platform_device_register(&rtc_device);
  176. dev_info(&rtc_device.dev,
  177. "registered platform RTC device (no PNP device found)\n");
  178. return 0;
  179. }
  180. device_initcall(add_rtc_cmos);