rtc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. unsigned long get_cmos_time(void)
  19. {
  20. unsigned int year, mon, day, hour, min, sec;
  21. spin_lock(&rtc_lock);
  22. do {
  23. sec = CMOS_READ(RTC_SECONDS);
  24. min = CMOS_READ(RTC_MINUTES);
  25. hour = CMOS_READ(RTC_HOURS);
  26. day = CMOS_READ(RTC_DAY_OF_MONTH);
  27. mon = CMOS_READ(RTC_MONTH);
  28. year = CMOS_READ(RTC_YEAR);
  29. } while (sec != CMOS_READ(RTC_SECONDS));
  30. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  31. BCD_TO_BIN(sec);
  32. BCD_TO_BIN(min);
  33. BCD_TO_BIN(hour);
  34. BCD_TO_BIN(day);
  35. BCD_TO_BIN(mon);
  36. BCD_TO_BIN(year);
  37. }
  38. spin_unlock(&rtc_lock);
  39. year += 1900;
  40. if (year < 1970)
  41. year += 100;
  42. return mktime(year, mon, day, hour, min, sec);
  43. }
  44. void mpc1211_rtc_gettimeofday(struct timeval *tv)
  45. {
  46. tv->tv_sec = get_cmos_time();
  47. tv->tv_usec = 0;
  48. }
  49. /* arc/i386/kernel/time.c */
  50. /*
  51. * In order to set the CMOS clock precisely, set_rtc_mmss has to be
  52. * called 500 ms after the second nowtime has started, because when
  53. * nowtime is written into the registers of the CMOS clock, it will
  54. * jump to the next second precisely 500 ms later. Check the Motorola
  55. * MC146818A or Dallas DS12887 data sheet for details.
  56. *
  57. * BUG: This routine does not handle hour overflow properly; it just
  58. * sets the minutes. Usually you'll only notice that after reboot!
  59. */
  60. static int set_rtc_mmss(unsigned long nowtime)
  61. {
  62. int retval = 0;
  63. int real_seconds, real_minutes, cmos_minutes;
  64. unsigned char save_control, save_freq_select;
  65. /* gets recalled with irq locally disabled */
  66. spin_lock(&rtc_lock);
  67. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  68. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  69. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  70. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  71. cmos_minutes = CMOS_READ(RTC_MINUTES);
  72. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  73. BCD_TO_BIN(cmos_minutes);
  74. /*
  75. * since we're only adjusting minutes and seconds,
  76. * don't interfere with hour overflow. This avoids
  77. * messing with unknown time zones but requires your
  78. * RTC not to be off by more than 15 minutes
  79. */
  80. real_seconds = nowtime % 60;
  81. real_minutes = nowtime / 60;
  82. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  83. real_minutes += 30; /* correct for half hour time zone */
  84. real_minutes %= 60;
  85. if (abs(real_minutes - cmos_minutes) < 30) {
  86. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  87. BIN_TO_BCD(real_seconds);
  88. BIN_TO_BCD(real_minutes);
  89. }
  90. CMOS_WRITE(real_seconds,RTC_SECONDS);
  91. CMOS_WRITE(real_minutes,RTC_MINUTES);
  92. } else {
  93. printk(KERN_WARNING
  94. "set_rtc_mmss: can't update from %d to %d\n",
  95. cmos_minutes, real_minutes);
  96. retval = -1;
  97. }
  98. /* The following flags have to be released exactly in this order,
  99. * otherwise the DS12887 (popular MC146818A clone with integrated
  100. * battery and quartz) will not reset the oscillator and will not
  101. * update precisely 500 ms later. You won't find this mentioned in
  102. * the Dallas Semiconductor data sheets, but who believes data
  103. * sheets anyway ... -- Markus Kuhn
  104. */
  105. CMOS_WRITE(save_control, RTC_CONTROL);
  106. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  107. spin_unlock(&rtc_lock);
  108. return retval;
  109. }
  110. int mpc1211_rtc_settimeofday(const struct timeval *tv)
  111. {
  112. unsigned long nowtime = tv->tv_sec;
  113. return set_rtc_mmss(nowtime);
  114. }
  115. void mpc1211_time_init(void)
  116. {
  117. rtc_get_time = mpc1211_rtc_gettimeofday;
  118. rtc_set_time = mpc1211_rtc_settimeofday;
  119. }