mc146818-time.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Machine dependent access functions for RTC registers.
  7. */
  8. #ifndef __ASM_MC146818_TIME_H
  9. #define __ASM_MC146818_TIME_H
  10. #include <linux/bcd.h>
  11. #include <linux/mc146818rtc.h>
  12. #include <linux/time.h>
  13. /*
  14. * For check timing call set_rtc_mmss() 500ms; used in timer interrupt.
  15. */
  16. #define USEC_AFTER 500000
  17. #define USEC_BEFORE 500000
  18. /*
  19. * In order to set the CMOS clock precisely, set_rtc_mmss has to be
  20. * called 500 ms after the second nowtime has started, because when
  21. * nowtime is written into the registers of the CMOS clock, it will
  22. * jump to the next second precisely 500 ms later. Check the Motorola
  23. * MC146818A or Dallas DS12887 data sheet for details.
  24. *
  25. * BUG: This routine does not handle hour overflow properly; it just
  26. * sets the minutes. Usually you'll only notice that after reboot!
  27. */
  28. static inline int mc146818_set_rtc_mmss(unsigned long nowtime)
  29. {
  30. int real_seconds, real_minutes, cmos_minutes;
  31. unsigned char save_control, save_freq_select;
  32. int retval = 0;
  33. unsigned long flags;
  34. spin_lock_irqsave(&rtc_lock, flags);
  35. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  36. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  37. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  38. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  39. cmos_minutes = CMOS_READ(RTC_MINUTES);
  40. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  41. BCD_TO_BIN(cmos_minutes);
  42. /*
  43. * since we're only adjusting minutes and seconds,
  44. * don't interfere with hour overflow. This avoids
  45. * messing with unknown time zones but requires your
  46. * RTC not to be off by more than 15 minutes
  47. */
  48. real_seconds = nowtime % 60;
  49. real_minutes = nowtime / 60;
  50. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  51. real_minutes += 30; /* correct for half hour time zone */
  52. real_minutes %= 60;
  53. if (abs(real_minutes - cmos_minutes) < 30) {
  54. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  55. BIN_TO_BCD(real_seconds);
  56. BIN_TO_BCD(real_minutes);
  57. }
  58. CMOS_WRITE(real_seconds,RTC_SECONDS);
  59. CMOS_WRITE(real_minutes,RTC_MINUTES);
  60. } else {
  61. printk(KERN_WARNING
  62. "set_rtc_mmss: can't update from %d to %d\n",
  63. cmos_minutes, real_minutes);
  64. retval = -1;
  65. }
  66. /* The following flags have to be released exactly in this order,
  67. * otherwise the DS12887 (popular MC146818A clone with integrated
  68. * battery and quartz) will not reset the oscillator and will not
  69. * update precisely 500 ms later. You won't find this mentioned in
  70. * the Dallas Semiconductor data sheets, but who believes data
  71. * sheets anyway ... -- Markus Kuhn
  72. */
  73. CMOS_WRITE(save_control, RTC_CONTROL);
  74. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  75. spin_unlock_irqrestore(&rtc_lock, flags);
  76. return retval;
  77. }
  78. /*
  79. * Returns true if a clock update is in progress
  80. */
  81. static inline unsigned char rtc_is_updating(void)
  82. {
  83. unsigned char uip;
  84. unsigned long flags;
  85. spin_lock_irqsave(&rtc_lock, flags);
  86. uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
  87. spin_unlock_irqrestore(&rtc_lock, flags);
  88. return uip;
  89. }
  90. static inline unsigned long mc146818_get_cmos_time(void)
  91. {
  92. unsigned int year, mon, day, hour, min, sec;
  93. int i;
  94. unsigned long flags;
  95. /*
  96. * The Linux interpretation of the CMOS clock register contents:
  97. * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
  98. * RTC registers show the second which has precisely just started.
  99. * Let's hope other operating systems interpret the RTC the same way.
  100. */
  101. /* read RTC exactly on falling edge of update flag */
  102. for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
  103. if (rtc_is_updating())
  104. break;
  105. for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
  106. if (!rtc_is_updating())
  107. break;
  108. spin_lock_irqsave(&rtc_lock, flags);
  109. do { /* Isn't this overkill ? UIP above should guarantee consistency */
  110. sec = CMOS_READ(RTC_SECONDS);
  111. min = CMOS_READ(RTC_MINUTES);
  112. hour = CMOS_READ(RTC_HOURS);
  113. day = CMOS_READ(RTC_DAY_OF_MONTH);
  114. mon = CMOS_READ(RTC_MONTH);
  115. year = CMOS_READ(RTC_YEAR);
  116. } while (sec != CMOS_READ(RTC_SECONDS));
  117. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  118. BCD_TO_BIN(sec);
  119. BCD_TO_BIN(min);
  120. BCD_TO_BIN(hour);
  121. BCD_TO_BIN(day);
  122. BCD_TO_BIN(mon);
  123. BCD_TO_BIN(year);
  124. }
  125. spin_unlock_irqrestore(&rtc_lock, flags);
  126. year = mc146818_decode_year(year);
  127. return mktime(year, mon, day, hour, min, sec);
  128. }
  129. #endif /* __ASM_MC146818_TIME_H */