time.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mc146818rtc.h>
  17. #include <linux/mm.h>
  18. #include <linux/module.h>
  19. #include <linux/param.h>
  20. #include <linux/sched.h>
  21. #include <linux/string.h>
  22. #include <linux/time.h>
  23. #include <linux/types.h>
  24. #include <asm/bootinfo.h>
  25. #include <asm/cpu.h>
  26. #include <asm/div64.h>
  27. #include <asm/io.h>
  28. #include <asm/irq.h>
  29. #include <asm/mipsregs.h>
  30. #include <asm/sections.h>
  31. #include <asm/time.h>
  32. #include <asm/dec/interrupts.h>
  33. #include <asm/dec/ioasic.h>
  34. #include <asm/dec/ioasic_addrs.h>
  35. #include <asm/dec/machtype.h>
  36. /*
  37. * Returns true if a clock update is in progress
  38. */
  39. static inline unsigned char dec_rtc_is_updating(void)
  40. {
  41. unsigned char uip;
  42. unsigned long flags;
  43. spin_lock_irqsave(&rtc_lock, flags);
  44. uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
  45. spin_unlock_irqrestore(&rtc_lock, flags);
  46. return uip;
  47. }
  48. static unsigned long dec_rtc_get_time(void)
  49. {
  50. unsigned int year, mon, day, hour, min, sec, real_year;
  51. int i;
  52. unsigned long flags;
  53. /* The Linux interpretation of the DS1287 clock register contents:
  54. * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
  55. * RTC registers show the second which has precisely just started.
  56. * Let's hope other operating systems interpret the RTC the same way.
  57. */
  58. /* read RTC exactly on falling edge of update flag */
  59. for (i = 0; i < 1000000; i++) /* may take up to 1 second... */
  60. if (dec_rtc_is_updating())
  61. break;
  62. for (i = 0; i < 1000000; i++) /* must try at least 2.228 ms */
  63. if (!dec_rtc_is_updating())
  64. break;
  65. spin_lock_irqsave(&rtc_lock, flags);
  66. /* Isn't this overkill? UIP above should guarantee consistency */
  67. do {
  68. sec = CMOS_READ(RTC_SECONDS);
  69. min = CMOS_READ(RTC_MINUTES);
  70. hour = CMOS_READ(RTC_HOURS);
  71. day = CMOS_READ(RTC_DAY_OF_MONTH);
  72. mon = CMOS_READ(RTC_MONTH);
  73. year = CMOS_READ(RTC_YEAR);
  74. } while (sec != CMOS_READ(RTC_SECONDS));
  75. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  76. sec = BCD2BIN(sec);
  77. min = BCD2BIN(min);
  78. hour = BCD2BIN(hour);
  79. day = BCD2BIN(day);
  80. mon = BCD2BIN(mon);
  81. year = BCD2BIN(year);
  82. }
  83. /*
  84. * The PROM will reset the year to either '72 or '73.
  85. * Therefore we store the real year separately, in one
  86. * of unused BBU RAM locations.
  87. */
  88. real_year = CMOS_READ(RTC_DEC_YEAR);
  89. spin_unlock_irqrestore(&rtc_lock, flags);
  90. year += real_year - 72 + 2000;
  91. return mktime(year, mon, day, hour, min, sec);
  92. }
  93. /*
  94. * In order to set the CMOS clock precisely, dec_rtc_set_mmss has to
  95. * be called 500 ms after the second nowtime has started, because when
  96. * nowtime is written into the registers of the CMOS clock, it will
  97. * jump to the next second precisely 500 ms later. Check the Dallas
  98. * DS1287 data sheet for details.
  99. */
  100. static int dec_rtc_set_mmss(unsigned long nowtime)
  101. {
  102. int retval = 0;
  103. int real_seconds, real_minutes, cmos_minutes;
  104. unsigned char save_control, save_freq_select;
  105. /* irq are locally disabled here */
  106. spin_lock(&rtc_lock);
  107. /* tell the clock it's being set */
  108. save_control = CMOS_READ(RTC_CONTROL);
  109. CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL);
  110. /* stop and reset prescaler */
  111. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  112. CMOS_WRITE((save_freq_select | RTC_DIV_RESET2), RTC_FREQ_SELECT);
  113. cmos_minutes = CMOS_READ(RTC_MINUTES);
  114. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  115. cmos_minutes = BCD2BIN(cmos_minutes);
  116. /*
  117. * since we're only adjusting minutes and seconds,
  118. * don't interfere with hour overflow. This avoids
  119. * messing with unknown time zones but requires your
  120. * RTC not to be off by more than 15 minutes
  121. */
  122. real_seconds = nowtime % 60;
  123. real_minutes = nowtime / 60;
  124. if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
  125. real_minutes += 30; /* correct for half hour time zone */
  126. real_minutes %= 60;
  127. if (abs(real_minutes - cmos_minutes) < 30) {
  128. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  129. real_seconds = BIN2BCD(real_seconds);
  130. real_minutes = BIN2BCD(real_minutes);
  131. }
  132. CMOS_WRITE(real_seconds, RTC_SECONDS);
  133. CMOS_WRITE(real_minutes, RTC_MINUTES);
  134. } else {
  135. printk(KERN_WARNING
  136. "set_rtc_mmss: can't update from %d to %d\n",
  137. cmos_minutes, real_minutes);
  138. retval = -1;
  139. }
  140. /* The following flags have to be released exactly in this order,
  141. * otherwise the DS1287 will not reset the oscillator and will not
  142. * update precisely 500 ms later. You won't find this mentioned
  143. * in the Dallas Semiconductor data sheets, but who believes data
  144. * sheets anyway ... -- Markus Kuhn
  145. */
  146. CMOS_WRITE(save_control, RTC_CONTROL);
  147. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  148. spin_unlock(&rtc_lock);
  149. return retval;
  150. }
  151. static int dec_timer_state(void)
  152. {
  153. return (CMOS_READ(RTC_REG_C) & RTC_PF) != 0;
  154. }
  155. static void dec_timer_ack(void)
  156. {
  157. CMOS_READ(RTC_REG_C); /* Ack the RTC interrupt. */
  158. }
  159. static unsigned int dec_ioasic_hpt_read(void)
  160. {
  161. /*
  162. * The free-running counter is 32-bit which is good for about
  163. * 2 minutes, 50 seconds at possible count rates of up to 25MHz.
  164. */
  165. return ioasic_read(IO_REG_FCTR);
  166. }
  167. static void dec_ioasic_hpt_init(unsigned int count)
  168. {
  169. ioasic_write(IO_REG_FCTR, ioasic_read(IO_REG_FCTR) - count);
  170. }
  171. void __init dec_time_init(void)
  172. {
  173. rtc_get_time = dec_rtc_get_time;
  174. rtc_set_mmss = dec_rtc_set_mmss;
  175. mips_timer_state = dec_timer_state;
  176. mips_timer_ack = dec_timer_ack;
  177. if (!cpu_has_counter && IOASIC) {
  178. /* For pre-R4k systems we use the I/O ASIC's counter. */
  179. mips_hpt_read = dec_ioasic_hpt_read;
  180. mips_hpt_init = dec_ioasic_hpt_init;
  181. }
  182. /* Set up the rate of periodic DS1287 interrupts. */
  183. CMOS_WRITE(RTC_REF_CLCK_32KHZ | (16 - LOG_2_HZ), RTC_REG_A);
  184. }
  185. EXPORT_SYMBOL(do_settimeofday);
  186. void __init dec_timer_setup(struct irqaction *irq)
  187. {
  188. setup_irq(dec_interrupt[DEC_IRQ_RTC], irq);
  189. /* Enable periodic DS1287 interrupts. */
  190. CMOS_WRITE(CMOS_READ(RTC_REG_B) | RTC_PIE, RTC_REG_B);
  191. }