rtc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * RTC related functions
  3. */
  4. #include <linux/bcd.h>
  5. #include <linux/mc146818rtc.h>
  6. #include <asm/time.h>
  7. /*
  8. * In order to set the CMOS clock precisely, set_rtc_mmss has to be
  9. * called 500 ms after the second nowtime has started, because when
  10. * nowtime is written into the registers of the CMOS clock, it will
  11. * jump to the next second precisely 500 ms later. Check the Motorola
  12. * MC146818A or Dallas DS12887 data sheet for details.
  13. *
  14. * BUG: This routine does not handle hour overflow properly; it just
  15. * sets the minutes. Usually you'll only notice that after reboot!
  16. */
  17. int mach_set_rtc_mmss(unsigned long nowtime)
  18. {
  19. int retval = 0;
  20. int real_seconds, real_minutes, cmos_minutes;
  21. unsigned char save_control, save_freq_select;
  22. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  23. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  24. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  25. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  26. cmos_minutes = CMOS_READ(RTC_MINUTES);
  27. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  28. BCD_TO_BIN(cmos_minutes);
  29. /*
  30. * since we're only adjusting minutes and seconds,
  31. * don't interfere with hour overflow. This avoids
  32. * messing with unknown time zones but requires your
  33. * RTC not to be off by more than 15 minutes
  34. */
  35. real_seconds = nowtime % 60;
  36. real_minutes = nowtime / 60;
  37. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  38. real_minutes += 30; /* correct for half hour time zone */
  39. real_minutes %= 60;
  40. if (abs(real_minutes - cmos_minutes) < 30) {
  41. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  42. BIN_TO_BCD(real_seconds);
  43. BIN_TO_BCD(real_minutes);
  44. }
  45. CMOS_WRITE(real_seconds,RTC_SECONDS);
  46. CMOS_WRITE(real_minutes,RTC_MINUTES);
  47. } else {
  48. printk(KERN_WARNING
  49. "set_rtc_mmss: can't update from %d to %d\n",
  50. cmos_minutes, real_minutes);
  51. retval = -1;
  52. }
  53. /* The following flags have to be released exactly in this order,
  54. * otherwise the DS12887 (popular MC146818A clone with integrated
  55. * battery and quartz) will not reset the oscillator and will not
  56. * update precisely 500 ms later. You won't find this mentioned in
  57. * the Dallas Semiconductor data sheets, but who believes data
  58. * sheets anyway ... -- Markus Kuhn
  59. */
  60. CMOS_WRITE(save_control, RTC_CONTROL);
  61. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  62. return retval;
  63. }
  64. unsigned long mach_get_cmos_time(void)
  65. {
  66. unsigned int year, mon, day, hour, min, sec;
  67. do {
  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. } while (sec != CMOS_READ(RTC_SECONDS));
  75. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  76. BCD_TO_BIN(sec);
  77. BCD_TO_BIN(min);
  78. BCD_TO_BIN(hour);
  79. BCD_TO_BIN(day);
  80. BCD_TO_BIN(mon);
  81. BCD_TO_BIN(year);
  82. }
  83. year += 1900;
  84. if (year < 1970)
  85. year += 100;
  86. return mktime(year, mon, day, hour, min, sec);
  87. }
  88. DEFINE_SPINLOCK(rtc_lock);
  89. EXPORT_SYMBOL(rtc_lock);
  90. /*
  91. * This is a special lock that is owned by the CPU and holds the index
  92. * register we are working with. It is required for NMI access to the
  93. * CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details.
  94. */
  95. volatile unsigned long cmos_lock = 0;
  96. EXPORT_SYMBOL(cmos_lock);
  97. /* Routines for accessing the CMOS RAM/RTC. */
  98. unsigned char rtc_cmos_read(unsigned char addr)
  99. {
  100. unsigned char val;
  101. lock_cmos_prefix(addr);
  102. outb_p(addr, RTC_PORT(0));
  103. val = inb_p(RTC_PORT(1));
  104. lock_cmos_suffix(addr);
  105. return val;
  106. }
  107. EXPORT_SYMBOL(rtc_cmos_read);
  108. void rtc_cmos_write(unsigned char val, unsigned char addr)
  109. {
  110. lock_cmos_prefix(addr);
  111. outb_p(addr, RTC_PORT(0));
  112. outb_p(val, RTC_PORT(1));
  113. lock_cmos_suffix(addr);
  114. }
  115. EXPORT_SYMBOL(rtc_cmos_write);
  116. static int set_rtc_mmss(unsigned long nowtime)
  117. {
  118. int retval;
  119. unsigned long flags;
  120. /* gets recalled with irq locally disabled */
  121. /* XXX - does irqsave resolve this? -johnstul */
  122. spin_lock_irqsave(&rtc_lock, flags);
  123. retval = set_wallclock(nowtime);
  124. spin_unlock_irqrestore(&rtc_lock, flags);
  125. return retval;
  126. }
  127. /* not static: needed by APM */
  128. unsigned long read_persistent_clock(void)
  129. {
  130. unsigned long retval;
  131. unsigned long flags;
  132. spin_lock_irqsave(&rtc_lock, flags);
  133. retval = get_wallclock();
  134. spin_unlock_irqrestore(&rtc_lock, flags);
  135. return retval;
  136. }
  137. int update_persistent_clock(struct timespec now)
  138. {
  139. return set_rtc_mmss(now.tv_sec);
  140. }