rtc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* MN10300 RTC management
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/mc146818rtc.h>
  15. #include <linux/bcd.h>
  16. #include <linux/timex.h>
  17. #include <asm/rtc-regs.h>
  18. #include <asm/rtc.h>
  19. DEFINE_SPINLOCK(rtc_lock);
  20. EXPORT_SYMBOL(rtc_lock);
  21. /* time for RTC to update itself in ioclks */
  22. static unsigned long mn10300_rtc_update_period;
  23. void read_persistent_clock(struct timespec *ts)
  24. {
  25. struct rtc_time tm;
  26. get_rtc_time(&tm);
  27. ts->tv_sec = mktime(tm.tm_year, tm.tm_mon, tm.tm_mday,
  28. tm.tm_hour, tm.tm_min, tm.tm_sec);
  29. ts->tv_nsec = 0;
  30. }
  31. /*
  32. * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
  33. * ms after the second nowtime has started, because when nowtime is written
  34. * into the registers of the CMOS clock, it will jump to the next second
  35. * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
  36. * sheet for details.
  37. *
  38. * BUG: This routine does not handle hour overflow properly; it just
  39. * sets the minutes. Usually you'll only notice that after reboot!
  40. */
  41. static int set_rtc_mmss(unsigned long nowtime)
  42. {
  43. unsigned char save_control, save_freq_select;
  44. int retval = 0;
  45. int real_seconds, real_minutes, cmos_minutes;
  46. /* gets recalled with irq locally disabled */
  47. spin_lock(&rtc_lock);
  48. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being
  49. * set */
  50. CMOS_WRITE(save_control | RTC_SET, RTC_CONTROL);
  51. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset
  52. * prescaler */
  53. CMOS_WRITE(save_freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
  54. cmos_minutes = CMOS_READ(RTC_MINUTES);
  55. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  56. cmos_minutes = bcd2bin(cmos_minutes);
  57. /*
  58. * since we're only adjusting minutes and seconds,
  59. * don't interfere with hour overflow. This avoids
  60. * messing with unknown time zones but requires your
  61. * RTC not to be off by more than 15 minutes
  62. */
  63. real_seconds = nowtime % 60;
  64. real_minutes = nowtime / 60;
  65. if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
  66. /* correct for half hour time zone */
  67. real_minutes += 30;
  68. real_minutes %= 60;
  69. if (abs(real_minutes - cmos_minutes) < 30) {
  70. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  71. real_seconds = bin2bcd(real_seconds);
  72. real_minutes = bin2bcd(real_minutes);
  73. }
  74. CMOS_WRITE(real_seconds, RTC_SECONDS);
  75. CMOS_WRITE(real_minutes, RTC_MINUTES);
  76. } else {
  77. printk(KERN_WARNING
  78. "set_rtc_mmss: can't update from %d to %d\n",
  79. cmos_minutes, real_minutes);
  80. retval = -1;
  81. }
  82. /* The following flags have to be released exactly in this order,
  83. * otherwise the DS12887 (popular MC146818A clone with integrated
  84. * battery and quartz) will not reset the oscillator and will not
  85. * update precisely 500 ms later. You won't find this mentioned in
  86. * the Dallas Semiconductor data sheets, but who believes data
  87. * sheets anyway ... -- Markus Kuhn
  88. */
  89. CMOS_WRITE(save_control, RTC_CONTROL);
  90. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  91. spin_unlock(&rtc_lock);
  92. return retval;
  93. }
  94. int update_persistent_clock(struct timespec now)
  95. {
  96. return set_rtc_mmss(now.tv_sec);
  97. }
  98. /*
  99. * calibrate the TSC clock against the RTC
  100. */
  101. void __init calibrate_clock(void)
  102. {
  103. unsigned long count0, counth, count1;
  104. unsigned char status;
  105. /* make sure the RTC is running and is set to operate in 24hr mode */
  106. status = RTSRC;
  107. RTCRB |= RTCRB_SET;
  108. RTCRB |= RTCRB_TM_24HR;
  109. RTCRA |= RTCRA_DVR;
  110. RTCRA &= ~RTCRA_DVR;
  111. RTCRB &= ~RTCRB_SET;
  112. /* work out the clock speed by counting clock cycles between ends of
  113. * the RTC update cycle - track the RTC through one complete update
  114. * cycle (1 second)
  115. */
  116. startup_timestamp_counter();
  117. while (!(RTCRA & RTCRA_UIP)) {}
  118. while ((RTCRA & RTCRA_UIP)) {}
  119. count0 = TMTSCBC;
  120. while (!(RTCRA & RTCRA_UIP)) {}
  121. counth = TMTSCBC;
  122. while ((RTCRA & RTCRA_UIP)) {}
  123. count1 = TMTSCBC;
  124. shutdown_timestamp_counter();
  125. MN10300_TSCCLK = count0 - count1; /* the timers count down */
  126. mn10300_rtc_update_period = counth - count1;
  127. MN10300_TSC_PER_HZ = MN10300_TSCCLK / HZ;
  128. }