ip22-time.c 5.6 KB

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