rtc.c 5.2 KB

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