mc146818-time.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. cmos_minutes = bcd2bin(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. real_seconds = bin2bcd(real_seconds);
  56. real_minutes = bin2bcd(real_minutes);
  57. }
  58. CMOS_WRITE(real_seconds, RTC_SECONDS);
  59. CMOS_WRITE(real_minutes, RTC_MINUTES);
  60. } else {
  61. printk_once(KERN_NOTICE
  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. static inline unsigned long mc146818_get_cmos_time(void)
  79. {
  80. unsigned int year, mon, day, hour, min, sec;
  81. unsigned long flags;
  82. spin_lock_irqsave(&rtc_lock, flags);
  83. do {
  84. sec = CMOS_READ(RTC_SECONDS);
  85. min = CMOS_READ(RTC_MINUTES);
  86. hour = CMOS_READ(RTC_HOURS);
  87. day = CMOS_READ(RTC_DAY_OF_MONTH);
  88. mon = CMOS_READ(RTC_MONTH);
  89. year = CMOS_READ(RTC_YEAR);
  90. } while (sec != CMOS_READ(RTC_SECONDS));
  91. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  92. sec = bcd2bin(sec);
  93. min = bcd2bin(min);
  94. hour = bcd2bin(hour);
  95. day = bcd2bin(day);
  96. mon = bcd2bin(mon);
  97. year = bcd2bin(year);
  98. }
  99. spin_unlock_irqrestore(&rtc_lock, flags);
  100. year = mc146818_decode_year(year);
  101. return mktime(year, mon, day, hour, min, sec);
  102. }
  103. #endif /* __ASM_MC146818_TIME_H */