ip22-time.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Time operations for IP22 machines. Original code may come from
  7. * Ralf Baechle or David S. Miller (sorry guys, i'm really not sure)
  8. *
  9. * Copyright (C) 2001 by Ladislav Michl
  10. * Copyright (C) 2003, 06 Ralf Baechle (ralf@linux-mips.org)
  11. */
  12. #include <linux/bcd.h>
  13. #include <linux/ds1286.h>
  14. #include <linux/init.h>
  15. #include <linux/irq.h>
  16. #include <linux/kernel.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/time.h>
  20. #include <asm/cpu.h>
  21. #include <asm/mipsregs.h>
  22. #include <asm/io.h>
  23. #include <asm/irq.h>
  24. #include <asm/time.h>
  25. #include <asm/sgialib.h>
  26. #include <asm/sgi/ioc.h>
  27. #include <asm/sgi/hpc3.h>
  28. #include <asm/sgi/ip22.h>
  29. /*
  30. * note that mktime uses month from 1 to 12 while to_tm
  31. * uses 0 to 11.
  32. */
  33. static unsigned long indy_rtc_get_time(void)
  34. {
  35. unsigned int yrs, mon, day, hrs, min, sec;
  36. unsigned int save_control;
  37. unsigned long flags;
  38. spin_lock_irqsave(&rtc_lock, flags);
  39. save_control = hpc3c0->rtcregs[RTC_CMD] & 0xff;
  40. hpc3c0->rtcregs[RTC_CMD] = save_control | RTC_TE;
  41. sec = BCD2BIN(hpc3c0->rtcregs[RTC_SECONDS] & 0xff);
  42. min = BCD2BIN(hpc3c0->rtcregs[RTC_MINUTES] & 0xff);
  43. hrs = BCD2BIN(hpc3c0->rtcregs[RTC_HOURS] & 0x3f);
  44. day = BCD2BIN(hpc3c0->rtcregs[RTC_DATE] & 0xff);
  45. mon = BCD2BIN(hpc3c0->rtcregs[RTC_MONTH] & 0x1f);
  46. yrs = BCD2BIN(hpc3c0->rtcregs[RTC_YEAR] & 0xff);
  47. hpc3c0->rtcregs[RTC_CMD] = save_control;
  48. spin_unlock_irqrestore(&rtc_lock, flags);
  49. if (yrs < 45)
  50. yrs += 30;
  51. if ((yrs += 40) < 70)
  52. yrs += 100;
  53. return mktime(yrs + 1900, mon, day, hrs, min, sec);
  54. }
  55. static int indy_rtc_set_time(unsigned long tim)
  56. {
  57. struct rtc_time tm;
  58. unsigned int save_control;
  59. unsigned long flags;
  60. to_tm(tim, &tm);
  61. tm.tm_mon += 1; /* tm_mon starts at zero */
  62. tm.tm_year -= 1940;
  63. if (tm.tm_year >= 100)
  64. tm.tm_year -= 100;
  65. spin_lock_irqsave(&rtc_lock, flags);
  66. save_control = hpc3c0->rtcregs[RTC_CMD] & 0xff;
  67. hpc3c0->rtcregs[RTC_CMD] = save_control | RTC_TE;
  68. hpc3c0->rtcregs[RTC_YEAR] = BIN2BCD(tm.tm_year);
  69. hpc3c0->rtcregs[RTC_MONTH] = BIN2BCD(tm.tm_mon);
  70. hpc3c0->rtcregs[RTC_DATE] = BIN2BCD(tm.tm_mday);
  71. hpc3c0->rtcregs[RTC_HOURS] = BIN2BCD(tm.tm_hour);
  72. hpc3c0->rtcregs[RTC_MINUTES] = BIN2BCD(tm.tm_min);
  73. hpc3c0->rtcregs[RTC_SECONDS] = BIN2BCD(tm.tm_sec);
  74. hpc3c0->rtcregs[RTC_HUNDREDTH_SECOND] = 0;
  75. hpc3c0->rtcregs[RTC_CMD] = save_control;
  76. spin_unlock_irqrestore(&rtc_lock, flags);
  77. return 0;
  78. }
  79. static unsigned long dosample(void)
  80. {
  81. u32 ct0, ct1;
  82. volatile u8 msb, lsb;
  83. /* Start the counter. */
  84. sgint->tcword = (SGINT_TCWORD_CNT2 | SGINT_TCWORD_CALL |
  85. SGINT_TCWORD_MRGEN);
  86. sgint->tcnt2 = SGINT_TCSAMP_COUNTER & 0xff;
  87. sgint->tcnt2 = SGINT_TCSAMP_COUNTER >> 8;
  88. /* Get initial counter invariant */
  89. ct0 = read_c0_count();
  90. /* Latch and spin until top byte of counter2 is zero */
  91. do {
  92. sgint->tcword = SGINT_TCWORD_CNT2 | SGINT_TCWORD_CLAT;
  93. lsb = sgint->tcnt2;
  94. msb = sgint->tcnt2;
  95. ct1 = read_c0_count();
  96. } while (msb);
  97. /* Stop the counter. */
  98. sgint->tcword = (SGINT_TCWORD_CNT2 | SGINT_TCWORD_CALL |
  99. SGINT_TCWORD_MSWST);
  100. /*
  101. * Return the difference, this is how far the r4k counter increments
  102. * for every 1/HZ seconds. We round off the nearest 1 MHz of master
  103. * clock (= 1000000 / HZ / 2).
  104. */
  105. /*return (ct1 - ct0 + (500000/HZ/2)) / (500000/HZ) * (500000/HZ);*/
  106. return (ct1 - ct0) / (500000/HZ) * (500000/HZ);
  107. }
  108. /*
  109. * Here we need to calibrate the cycle counter to at least be close.
  110. */
  111. static __init void indy_time_init(void)
  112. {
  113. unsigned long r4k_ticks[3];
  114. unsigned long r4k_tick;
  115. /*
  116. * Figure out the r4k offset, the algorithm is very simple and works in
  117. * _all_ cases as long as the 8254 counter register itself works ok (as
  118. * an interrupt driving timer it does not because of bug, this is why
  119. * we are using the onchip r4k counter/compare register to serve this
  120. * purpose, but for r4k_offset calculation it will work ok for us).
  121. * There are other very complicated ways of performing this calculation
  122. * but this one works just fine so I am not going to futz around. ;-)
  123. */
  124. printk(KERN_INFO "Calibrating system timer... ");
  125. dosample(); /* Prime cache. */
  126. dosample(); /* Prime cache. */
  127. /* Zero is NOT an option. */
  128. do {
  129. r4k_ticks[0] = dosample();
  130. } while (!r4k_ticks[0]);
  131. do {
  132. r4k_ticks[1] = dosample();
  133. } while (!r4k_ticks[1]);
  134. if (r4k_ticks[0] != r4k_ticks[1]) {
  135. printk("warning: timer counts differ, retrying... ");
  136. r4k_ticks[2] = dosample();
  137. if (r4k_ticks[2] == r4k_ticks[0]
  138. || r4k_ticks[2] == r4k_ticks[1])
  139. r4k_tick = r4k_ticks[2];
  140. else {
  141. printk("disagreement, using average... ");
  142. r4k_tick = (r4k_ticks[0] + r4k_ticks[1]
  143. + r4k_ticks[2]) / 3;
  144. }
  145. } else
  146. r4k_tick = r4k_ticks[0];
  147. printk("%d [%d.%04d MHz CPU]\n", (int) r4k_tick,
  148. (int) (r4k_tick / (500000 / HZ)),
  149. (int) (r4k_tick % (500000 / HZ)));
  150. mips_hpt_frequency = r4k_tick * HZ;
  151. }
  152. /* Generic SGI handler for (spurious) 8254 interrupts */
  153. void indy_8254timer_irq(void)
  154. {
  155. int irq = SGI_8254_0_IRQ;
  156. ULONG cnt;
  157. char c;
  158. irq_enter();
  159. kstat_this_cpu.irqs[irq]++;
  160. printk(KERN_ALERT "Oops, got 8254 interrupt.\n");
  161. ArcRead(0, &c, 1, &cnt);
  162. ArcEnterInteractiveMode();
  163. irq_exit();
  164. }
  165. void indy_r4k_timer_interrupt(void)
  166. {
  167. int irq = SGI_TIMER_IRQ;
  168. irq_enter();
  169. kstat_this_cpu.irqs[irq]++;
  170. timer_interrupt(irq, NULL);
  171. irq_exit();
  172. }
  173. void __init plat_timer_setup(struct irqaction *irq)
  174. {
  175. /* over-write the handler, we use our own way */
  176. irq->handler = no_action;
  177. /* setup irqaction */
  178. setup_irq(SGI_TIMER_IRQ, irq);
  179. }
  180. void __init ip22_time_init(void)
  181. {
  182. /* setup hookup functions */
  183. rtc_mips_get_time = indy_rtc_get_time;
  184. rtc_mips_set_time = indy_rtc_set_time;
  185. board_time_init = indy_time_init;
  186. }