mach_time.h 4.0 KB

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