time.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * linux/arch/mips/dec/time.c
  3. *
  4. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  5. * Copyright (C) 2000, 2003 Maciej W. Rozycki
  6. *
  7. * This file contains the time handling details for PC-style clocks as
  8. * found in some MIPS systems.
  9. *
  10. */
  11. #include <linux/bcd.h>
  12. #include <linux/init.h>
  13. #include <linux/mc146818rtc.h>
  14. #include <linux/param.h>
  15. #include <asm/cpu-features.h>
  16. #include <asm/ds1287.h>
  17. #include <asm/time.h>
  18. #include <asm/dec/interrupts.h>
  19. #include <asm/dec/ioasic.h>
  20. #include <asm/dec/machtype.h>
  21. unsigned long read_persistent_clock(void)
  22. {
  23. unsigned int year, mon, day, hour, min, sec, real_year;
  24. unsigned long flags;
  25. spin_lock_irqsave(&rtc_lock, flags);
  26. do {
  27. sec = CMOS_READ(RTC_SECONDS);
  28. min = CMOS_READ(RTC_MINUTES);
  29. hour = CMOS_READ(RTC_HOURS);
  30. day = CMOS_READ(RTC_DAY_OF_MONTH);
  31. mon = CMOS_READ(RTC_MONTH);
  32. year = CMOS_READ(RTC_YEAR);
  33. /*
  34. * The PROM will reset the year to either '72 or '73.
  35. * Therefore we store the real year separately, in one
  36. * of unused BBU RAM locations.
  37. */
  38. real_year = CMOS_READ(RTC_DEC_YEAR);
  39. } while (sec != CMOS_READ(RTC_SECONDS));
  40. spin_unlock_irqrestore(&rtc_lock, flags);
  41. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  42. sec = bcd2bin(sec);
  43. min = bcd2bin(min);
  44. hour = bcd2bin(hour);
  45. day = bcd2bin(day);
  46. mon = bcd2bin(mon);
  47. year = bcd2bin(year);
  48. }
  49. year += real_year - 72 + 2000;
  50. return mktime(year, mon, day, hour, min, sec);
  51. }
  52. /*
  53. * In order to set the CMOS clock precisely, rtc_mips_set_mmss 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 rtc_mips_set_mmss(unsigned long nowtime)
  60. {
  61. int retval = 0;
  62. int real_seconds, real_minutes, cmos_minutes;
  63. unsigned char save_control, save_freq_select;
  64. /* irq are locally disabled here */
  65. spin_lock(&rtc_lock);
  66. /* tell the clock it's being set */
  67. save_control = CMOS_READ(RTC_CONTROL);
  68. CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL);
  69. /* stop and reset prescaler */
  70. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  71. CMOS_WRITE((save_freq_select | RTC_DIV_RESET2), RTC_FREQ_SELECT);
  72. cmos_minutes = CMOS_READ(RTC_MINUTES);
  73. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  74. cmos_minutes = bcd2bin(cmos_minutes);
  75. /*
  76. * since we're only adjusting minutes and seconds,
  77. * don't interfere with hour overflow. This avoids
  78. * messing with unknown time zones but requires your
  79. * RTC not to be off by more than 15 minutes
  80. */
  81. real_seconds = nowtime % 60;
  82. real_minutes = nowtime / 60;
  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(KERN_WARNING
  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. u32 start, end;
  113. int i = HZ / 10;
  114. /* Set up the rate of periodic DS1287 interrupts. */
  115. ds1287_set_base_clock(HZ);
  116. if (cpu_has_counter) {
  117. while (!ds1287_timer_state())
  118. ;
  119. start = read_c0_count();
  120. while (i--)
  121. while (!ds1287_timer_state())
  122. ;
  123. end = read_c0_count();
  124. mips_hpt_frequency = (end - start) * 10;
  125. printk(KERN_INFO "MIPS counter frequency %dHz\n",
  126. mips_hpt_frequency);
  127. } else if (IOASIC)
  128. /* For pre-R4k systems we use the I/O ASIC's counter. */
  129. dec_ioasic_clocksource_init();
  130. ds1287_clockevent_init(dec_interrupt[DEC_IRQ_RTC]);
  131. }