mach_time.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. do {
  75. sec = CMOS_READ(RTC_SECONDS);
  76. min = CMOS_READ(RTC_MINUTES);
  77. hour = CMOS_READ(RTC_HOURS);
  78. day = CMOS_READ(RTC_DAY_OF_MONTH);
  79. mon = CMOS_READ(RTC_MONTH);
  80. year = CMOS_READ(RTC_YEAR);
  81. } while (sec != CMOS_READ(RTC_SECONDS));
  82. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  83. BCD_TO_BIN(sec);
  84. BCD_TO_BIN(min);
  85. BCD_TO_BIN(hour);
  86. BCD_TO_BIN(day);
  87. BCD_TO_BIN(mon);
  88. BCD_TO_BIN(year);
  89. }
  90. year += 1900;
  91. if (year < 1970)
  92. year += 100;
  93. return mktime(year, mon, day, hour, min, sec);
  94. }
  95. #endif /* !_MACH_TIME_H */