ip22-time.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/init.h>
  14. #include <linux/irq.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/i8253.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. static unsigned long dosample(void)
  30. {
  31. u32 ct0, ct1;
  32. u8 msb, lsb;
  33. /* Start the counter. */
  34. sgint->tcword = (SGINT_TCWORD_CNT2 | SGINT_TCWORD_CALL |
  35. SGINT_TCWORD_MRGEN);
  36. sgint->tcnt2 = SGINT_TCSAMP_COUNTER & 0xff;
  37. sgint->tcnt2 = SGINT_TCSAMP_COUNTER >> 8;
  38. /* Get initial counter invariant */
  39. ct0 = read_c0_count();
  40. /* Latch and spin until top byte of counter2 is zero */
  41. do {
  42. writeb(SGINT_TCWORD_CNT2 | SGINT_TCWORD_CLAT, &sgint->tcword);
  43. lsb = readb(&sgint->tcnt2);
  44. msb = readb(&sgint->tcnt2);
  45. ct1 = read_c0_count();
  46. } while (msb);
  47. /* Stop the counter. */
  48. writeb(SGINT_TCWORD_CNT2 | SGINT_TCWORD_CALL | SGINT_TCWORD_MSWST,
  49. &sgint->tcword);
  50. /*
  51. * Return the difference, this is how far the r4k counter increments
  52. * for every 1/HZ seconds. We round off the nearest 1 MHz of master
  53. * clock (= 1000000 / HZ / 2).
  54. */
  55. return (ct1 - ct0) / (500000/HZ) * (500000/HZ);
  56. }
  57. /*
  58. * Here we need to calibrate the cycle counter to at least be close.
  59. */
  60. __init void plat_time_init(void)
  61. {
  62. unsigned long r4k_ticks[3];
  63. unsigned long r4k_tick;
  64. /*
  65. * Figure out the r4k offset, the algorithm is very simple and works in
  66. * _all_ cases as long as the 8254 counter register itself works ok (as
  67. * an interrupt driving timer it does not because of bug, this is why
  68. * we are using the onchip r4k counter/compare register to serve this
  69. * purpose, but for r4k_offset calculation it will work ok for us).
  70. * There are other very complicated ways of performing this calculation
  71. * but this one works just fine so I am not going to futz around. ;-)
  72. */
  73. printk(KERN_INFO "Calibrating system timer... ");
  74. dosample(); /* Prime cache. */
  75. dosample(); /* Prime cache. */
  76. /* Zero is NOT an option. */
  77. do {
  78. r4k_ticks[0] = dosample();
  79. } while (!r4k_ticks[0]);
  80. do {
  81. r4k_ticks[1] = dosample();
  82. } while (!r4k_ticks[1]);
  83. if (r4k_ticks[0] != r4k_ticks[1]) {
  84. printk("warning: timer counts differ, retrying... ");
  85. r4k_ticks[2] = dosample();
  86. if (r4k_ticks[2] == r4k_ticks[0]
  87. || r4k_ticks[2] == r4k_ticks[1])
  88. r4k_tick = r4k_ticks[2];
  89. else {
  90. printk("disagreement, using average... ");
  91. r4k_tick = (r4k_ticks[0] + r4k_ticks[1]
  92. + r4k_ticks[2]) / 3;
  93. }
  94. } else
  95. r4k_tick = r4k_ticks[0];
  96. printk("%d [%d.%04d MHz CPU]\n", (int) r4k_tick,
  97. (int) (r4k_tick / (500000 / HZ)),
  98. (int) (r4k_tick % (500000 / HZ)));
  99. mips_hpt_frequency = r4k_tick * HZ;
  100. if (ip22_is_fullhouse())
  101. setup_pit_timer();
  102. }
  103. /* Generic SGI handler for (spurious) 8254 interrupts */
  104. void indy_8254timer_irq(void)
  105. {
  106. int irq = SGI_8254_0_IRQ;
  107. ULONG cnt;
  108. char c;
  109. irq_enter();
  110. kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));
  111. printk(KERN_ALERT "Oops, got 8254 interrupt.\n");
  112. ArcRead(0, &c, 1, &cnt);
  113. ArcEnterInteractiveMode();
  114. irq_exit();
  115. }