time.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  3. * Copyright (C) 2000, 2003 Maciej W. Rozycki
  4. *
  5. * This file contains the time handling details for PC-style clocks as
  6. * found in some MIPS systems.
  7. *
  8. */
  9. #include <linux/bcd.h>
  10. #include <linux/init.h>
  11. #include <linux/mc146818rtc.h>
  12. #include <linux/param.h>
  13. #include <asm/cpu-features.h>
  14. #include <asm/ds1287.h>
  15. #include <asm/time.h>
  16. #include <asm/dec/interrupts.h>
  17. #include <asm/dec/ioasic.h>
  18. #include <asm/dec/machtype.h>
  19. void read_persistent_clock(struct timespec *ts)
  20. {
  21. unsigned int year, mon, day, hour, min, sec, real_year;
  22. unsigned long flags;
  23. spin_lock_irqsave(&rtc_lock, flags);
  24. do {
  25. sec = CMOS_READ(RTC_SECONDS);
  26. min = CMOS_READ(RTC_MINUTES);
  27. hour = CMOS_READ(RTC_HOURS);
  28. day = CMOS_READ(RTC_DAY_OF_MONTH);
  29. mon = CMOS_READ(RTC_MONTH);
  30. year = CMOS_READ(RTC_YEAR);
  31. /*
  32. * The PROM will reset the year to either '72 or '73.
  33. * Therefore we store the real year separately, in one
  34. * of unused BBU RAM locations.
  35. */
  36. real_year = CMOS_READ(RTC_DEC_YEAR);
  37. } while (sec != CMOS_READ(RTC_SECONDS));
  38. spin_unlock_irqrestore(&rtc_lock, flags);
  39. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  40. sec = bcd2bin(sec);
  41. min = bcd2bin(min);
  42. hour = bcd2bin(hour);
  43. day = bcd2bin(day);
  44. mon = bcd2bin(mon);
  45. year = bcd2bin(year);
  46. }
  47. year += real_year - 72 + 2000;
  48. ts->tv_sec = mktime(year, mon, day, hour, min, sec);
  49. ts->tv_nsec = 0;
  50. }
  51. /*
  52. * In order to set the CMOS clock precisely, rtc_mips_set_mmss has to
  53. * be called 500 ms after the second nowtime has started, because when
  54. * nowtime is written into the registers of the CMOS clock, it will
  55. * jump to the next second precisely 500 ms later. Check the Dallas
  56. * DS1287 data sheet for details.
  57. */
  58. int rtc_mips_set_mmss(unsigned long nowtime)
  59. {
  60. int retval = 0;
  61. int real_seconds, real_minutes, cmos_minutes;
  62. unsigned char save_control, save_freq_select;
  63. /* irq are locally disabled here */
  64. spin_lock(&rtc_lock);
  65. /* tell the clock it's being set */
  66. save_control = CMOS_READ(RTC_CONTROL);
  67. CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL);
  68. /* stop and reset prescaler */
  69. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  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. cmos_minutes = bcd2bin(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. real_seconds = bin2bcd(real_seconds);
  88. real_minutes = bin2bcd(real_minutes);
  89. }
  90. CMOS_WRITE(real_seconds, RTC_SECONDS);
  91. CMOS_WRITE(real_minutes, RTC_MINUTES);
  92. } else {
  93. printk_once(KERN_NOTICE
  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 DS1287 will not reset the oscillator and will not
  100. * update precisely 500 ms later. You won't find this mentioned
  101. * in the Dallas Semiconductor data sheets, but who believes data
  102. * sheets anyway ... -- Markus Kuhn
  103. */
  104. CMOS_WRITE(save_control, RTC_CONTROL);
  105. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  106. spin_unlock(&rtc_lock);
  107. return retval;
  108. }
  109. void __init plat_time_init(void)
  110. {
  111. int ioasic_clock = 0;
  112. u32 start, end;
  113. int i = HZ / 8;
  114. /* Set up the rate of periodic DS1287 interrupts. */
  115. ds1287_set_base_clock(HZ);
  116. /* On some I/O ASIC systems we have the I/O ASIC's counter. */
  117. if (IOASIC)
  118. ioasic_clock = dec_ioasic_clocksource_init() == 0;
  119. if (cpu_has_counter) {
  120. ds1287_timer_state();
  121. while (!ds1287_timer_state())
  122. ;
  123. start = read_c0_count();
  124. while (i--)
  125. while (!ds1287_timer_state())
  126. ;
  127. end = read_c0_count();
  128. mips_hpt_frequency = (end - start) * 8;
  129. printk(KERN_INFO "MIPS counter frequency %dHz\n",
  130. mips_hpt_frequency);
  131. /*
  132. * All R4k DECstations suffer from the CP0 Count erratum,
  133. * so we can't use the timer as a clock source, and a clock
  134. * event both at a time. An accurate wall clock is more
  135. * important than a high-precision interval timer so only
  136. * use the timer as a clock source, and not a clock event
  137. * if there's no I/O ASIC counter available to serve as a
  138. * clock source.
  139. */
  140. if (!ioasic_clock) {
  141. init_r4k_clocksource();
  142. mips_hpt_frequency = 0;
  143. }
  144. }
  145. ds1287_clockevent_init(dec_interrupt[DEC_IRQ_RTC]);
  146. }