mc146818-time.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  34. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  35. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  36. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  37. cmos_minutes = CMOS_READ(RTC_MINUTES);
  38. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  39. BCD_TO_BIN(cmos_minutes);
  40. /*
  41. * since we're only adjusting minutes and seconds,
  42. * don't interfere with hour overflow. This avoids
  43. * messing with unknown time zones but requires your
  44. * RTC not to be off by more than 15 minutes
  45. */
  46. real_seconds = nowtime % 60;
  47. real_minutes = nowtime / 60;
  48. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  49. real_minutes += 30; /* correct for half hour time zone */
  50. real_minutes %= 60;
  51. if (abs(real_minutes - cmos_minutes) < 30) {
  52. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  53. BIN_TO_BCD(real_seconds);
  54. BIN_TO_BCD(real_minutes);
  55. }
  56. CMOS_WRITE(real_seconds,RTC_SECONDS);
  57. CMOS_WRITE(real_minutes,RTC_MINUTES);
  58. } else {
  59. printk(KERN_WARNING
  60. "set_rtc_mmss: can't update from %d to %d\n",
  61. cmos_minutes, real_minutes);
  62. retval = -1;
  63. }
  64. /* The following flags have to be released exactly in this order,
  65. * otherwise the DS12887 (popular MC146818A clone with integrated
  66. * battery and quartz) will not reset the oscillator and will not
  67. * update precisely 500 ms later. You won't find this mentioned in
  68. * the Dallas Semiconductor data sheets, but who believes data
  69. * sheets anyway ... -- Markus Kuhn
  70. */
  71. CMOS_WRITE(save_control, RTC_CONTROL);
  72. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  73. return retval;
  74. }
  75. static inline unsigned long mc146818_get_cmos_time(void)
  76. {
  77. unsigned int year, mon, day, hour, min, sec;
  78. int i;
  79. /*
  80. * The Linux interpretation of the CMOS clock register contents:
  81. * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
  82. * RTC registers show the second which has precisely just started.
  83. * Let's hope other operating systems interpret the RTC the same way.
  84. */
  85. /* read RTC exactly on falling edge of update flag */
  86. for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
  87. if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
  88. break;
  89. for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
  90. if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  91. break;
  92. do { /* Isn't this overkill ? UIP above should guarantee consistency */
  93. sec = CMOS_READ(RTC_SECONDS);
  94. min = CMOS_READ(RTC_MINUTES);
  95. hour = CMOS_READ(RTC_HOURS);
  96. day = CMOS_READ(RTC_DAY_OF_MONTH);
  97. mon = CMOS_READ(RTC_MONTH);
  98. year = CMOS_READ(RTC_YEAR);
  99. } while (sec != CMOS_READ(RTC_SECONDS));
  100. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  101. BCD_TO_BIN(sec);
  102. BCD_TO_BIN(min);
  103. BCD_TO_BIN(hour);
  104. BCD_TO_BIN(day);
  105. BCD_TO_BIN(mon);
  106. BCD_TO_BIN(year);
  107. }
  108. year = mc146818_decode_year(year);
  109. return mktime(year, mon, day, hour, min, sec);
  110. }
  111. #endif /* __ASM_MC146818_TIME_H */