rtc.c 4.5 KB

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