rtc.c 3.7 KB

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