rtc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * RTC related functions
  3. */
  4. #include <linux/acpi.h>
  5. #include <linux/bcd.h>
  6. #include <linux/mc146818rtc.h>
  7. #include <asm/time.h>
  8. #include <asm/vsyscall.h>
  9. #ifdef CONFIG_X86_32
  10. # define CMOS_YEARS_OFFS 1900
  11. /*
  12. * This is a special lock that is owned by the CPU and holds the index
  13. * register we are working with. It is required for NMI access to the
  14. * CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details.
  15. */
  16. volatile unsigned long cmos_lock = 0;
  17. EXPORT_SYMBOL(cmos_lock);
  18. #else
  19. /*
  20. * x86-64 systems only exists since 2002.
  21. * This will work up to Dec 31, 2100
  22. */
  23. # define CMOS_YEARS_OFFS 2000
  24. #endif
  25. DEFINE_SPINLOCK(rtc_lock);
  26. EXPORT_SYMBOL(rtc_lock);
  27. /*
  28. * In order to set the CMOS clock precisely, set_rtc_mmss has to be
  29. * called 500 ms after the second nowtime has started, because when
  30. * nowtime is written into the registers of the CMOS clock, it will
  31. * jump to the next second precisely 500 ms later. Check the Motorola
  32. * MC146818A or Dallas DS12887 data sheet for details.
  33. *
  34. * BUG: This routine does not handle hour overflow properly; it just
  35. * sets the minutes. Usually you'll only notice that after reboot!
  36. */
  37. int mach_set_rtc_mmss(unsigned long nowtime)
  38. {
  39. int retval = 0;
  40. int real_seconds, real_minutes, cmos_minutes;
  41. unsigned char save_control, save_freq_select;
  42. /* tell the clock it's being set */
  43. save_control = CMOS_READ(RTC_CONTROL);
  44. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  45. /* stop and reset prescaler */
  46. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  47. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  48. cmos_minutes = CMOS_READ(RTC_MINUTES);
  49. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  50. BCD_TO_BIN(cmos_minutes);
  51. /*
  52. * since we're only adjusting minutes and seconds,
  53. * don't interfere with hour overflow. This avoids
  54. * messing with unknown time zones but requires your
  55. * RTC not to be off by more than 15 minutes
  56. */
  57. real_seconds = nowtime % 60;
  58. real_minutes = nowtime / 60;
  59. /* correct for half hour time zone */
  60. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  61. real_minutes += 30;
  62. real_minutes %= 60;
  63. if (abs(real_minutes - cmos_minutes) < 30) {
  64. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  65. BIN_TO_BCD(real_seconds);
  66. BIN_TO_BCD(real_minutes);
  67. }
  68. CMOS_WRITE(real_seconds,RTC_SECONDS);
  69. CMOS_WRITE(real_minutes,RTC_MINUTES);
  70. } else {
  71. printk(KERN_WARNING
  72. "set_rtc_mmss: can't update from %d to %d\n",
  73. cmos_minutes, real_minutes);
  74. retval = -1;
  75. }
  76. /* The following flags have to be released exactly in this order,
  77. * otherwise the DS12887 (popular MC146818A clone with integrated
  78. * battery and quartz) will not reset the oscillator and will not
  79. * update precisely 500 ms later. You won't find this mentioned in
  80. * the Dallas Semiconductor data sheets, but who believes data
  81. * sheets anyway ... -- Markus Kuhn
  82. */
  83. CMOS_WRITE(save_control, RTC_CONTROL);
  84. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  85. return retval;
  86. }
  87. unsigned long mach_get_cmos_time(void)
  88. {
  89. unsigned int year, mon, day, hour, min, sec, century = 0;
  90. /*
  91. * If UIP is clear, then we have >= 244 microseconds before
  92. * RTC registers will be updated. Spec sheet says that this
  93. * is the reliable way to read RTC - registers. If UIP is set
  94. * then the register access might be invalid.
  95. */
  96. while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  97. cpu_relax();
  98. sec = CMOS_READ(RTC_SECONDS);
  99. min = CMOS_READ(RTC_MINUTES);
  100. hour = CMOS_READ(RTC_HOURS);
  101. day = CMOS_READ(RTC_DAY_OF_MONTH);
  102. mon = CMOS_READ(RTC_MONTH);
  103. year = CMOS_READ(RTC_YEAR);
  104. #if defined(CONFIG_ACPI) && defined(CONFIG_X86_64)
  105. /* CHECKME: Is this really 64bit only ??? */
  106. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  107. acpi_gbl_FADT.century)
  108. century = CMOS_READ(acpi_gbl_FADT.century);
  109. #endif
  110. if (RTC_ALWAYS_BCD || !(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)) {
  111. BCD_TO_BIN(sec);
  112. BCD_TO_BIN(min);
  113. BCD_TO_BIN(hour);
  114. BCD_TO_BIN(day);
  115. BCD_TO_BIN(mon);
  116. BCD_TO_BIN(year);
  117. }
  118. if (century) {
  119. BCD_TO_BIN(century);
  120. year += century * 100;
  121. printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
  122. } else {
  123. year += CMOS_YEARS_OFFS;
  124. if (year < 1970)
  125. year += 100;
  126. }
  127. return mktime(year, mon, day, hour, min, sec);
  128. }
  129. /* Routines for accessing the CMOS RAM/RTC. */
  130. unsigned char rtc_cmos_read(unsigned char addr)
  131. {
  132. unsigned char val;
  133. lock_cmos_prefix(addr);
  134. outb_p(addr, RTC_PORT(0));
  135. val = inb_p(RTC_PORT(1));
  136. lock_cmos_suffix(addr);
  137. return val;
  138. }
  139. EXPORT_SYMBOL(rtc_cmos_read);
  140. void rtc_cmos_write(unsigned char val, unsigned char addr)
  141. {
  142. lock_cmos_prefix(addr);
  143. outb_p(addr, RTC_PORT(0));
  144. outb_p(val, RTC_PORT(1));
  145. lock_cmos_suffix(addr);
  146. }
  147. EXPORT_SYMBOL(rtc_cmos_write);
  148. static int set_rtc_mmss(unsigned long nowtime)
  149. {
  150. int retval;
  151. unsigned long flags;
  152. spin_lock_irqsave(&rtc_lock, flags);
  153. retval = set_wallclock(nowtime);
  154. spin_unlock_irqrestore(&rtc_lock, flags);
  155. return retval;
  156. }
  157. /* not static: needed by APM */
  158. unsigned long read_persistent_clock(void)
  159. {
  160. unsigned long retval, flags;
  161. spin_lock_irqsave(&rtc_lock, flags);
  162. retval = get_wallclock();
  163. spin_unlock_irqrestore(&rtc_lock, flags);
  164. return retval;
  165. }
  166. int update_persistent_clock(struct timespec now)
  167. {
  168. return set_rtc_mmss(now.tv_sec);
  169. }
  170. unsigned long long native_read_tsc(void)
  171. {
  172. return __native_read_tsc();
  173. }
  174. EXPORT_SYMBOL(native_read_tsc);