rtc.c 5.2 KB

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