time.c 4.8 KB

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