time.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. unsigned long read_persistent_clock(void)
  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. return mktime(year, mon, day, hour, min, sec);
  49. }
  50. /*
  51. * In order to set the CMOS clock precisely, rtc_mips_set_mmss has to
  52. * be 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 Dallas
  55. * DS1287 data sheet for details.
  56. */
  57. int rtc_mips_set_mmss(unsigned long nowtime)
  58. {
  59. int retval = 0;
  60. int real_seconds, real_minutes, cmos_minutes;
  61. unsigned char save_control, save_freq_select;
  62. /* irq are locally disabled here */
  63. spin_lock(&rtc_lock);
  64. /* tell the clock it's being set */
  65. save_control = CMOS_READ(RTC_CONTROL);
  66. CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL);
  67. /* stop and reset prescaler */
  68. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  69. CMOS_WRITE((save_freq_select | RTC_DIV_RESET2), RTC_FREQ_SELECT);
  70. cmos_minutes = CMOS_READ(RTC_MINUTES);
  71. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  72. cmos_minutes = bcd2bin(cmos_minutes);
  73. /*
  74. * since we're only adjusting minutes and seconds,
  75. * don't interfere with hour overflow. This avoids
  76. * messing with unknown time zones but requires your
  77. * RTC not to be off by more than 15 minutes
  78. */
  79. real_seconds = nowtime % 60;
  80. real_minutes = nowtime / 60;
  81. if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
  82. real_minutes += 30; /* correct for half hour time zone */
  83. real_minutes %= 60;
  84. if (abs(real_minutes - cmos_minutes) < 30) {
  85. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  86. real_seconds = bin2bcd(real_seconds);
  87. real_minutes = bin2bcd(real_minutes);
  88. }
  89. CMOS_WRITE(real_seconds, RTC_SECONDS);
  90. CMOS_WRITE(real_minutes, RTC_MINUTES);
  91. } else {
  92. printk(KERN_WARNING
  93. "set_rtc_mmss: can't update from %d to %d\n",
  94. cmos_minutes, real_minutes);
  95. retval = -1;
  96. }
  97. /* The following flags have to be released exactly in this order,
  98. * otherwise the DS1287 will not reset the oscillator and will not
  99. * update precisely 500 ms later. You won't find this mentioned
  100. * in the Dallas Semiconductor data sheets, but who believes data
  101. * sheets anyway ... -- Markus Kuhn
  102. */
  103. CMOS_WRITE(save_control, RTC_CONTROL);
  104. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  105. spin_unlock(&rtc_lock);
  106. return retval;
  107. }
  108. void __init plat_time_init(void)
  109. {
  110. u32 start, end;
  111. int i = HZ / 10;
  112. /* Set up the rate of periodic DS1287 interrupts. */
  113. ds1287_set_base_clock(HZ);
  114. if (cpu_has_counter) {
  115. while (!ds1287_timer_state())
  116. ;
  117. start = read_c0_count();
  118. while (i--)
  119. while (!ds1287_timer_state())
  120. ;
  121. end = read_c0_count();
  122. mips_hpt_frequency = (end - start) * 10;
  123. printk(KERN_INFO "MIPS counter frequency %dHz\n",
  124. mips_hpt_frequency);
  125. } else if (IOASIC)
  126. /* For pre-R4k systems we use the I/O ASIC's counter. */
  127. dec_ioasic_clocksource_init();
  128. ds1287_clockevent_init(dec_interrupt[DEC_IRQ_RTC]);
  129. }