time.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <linux/types.h>
  2. #include <linux/interrupt.h>
  3. #include <linux/time.h>
  4. #include <asm/i8253.h>
  5. #include <asm/sni.h>
  6. #include <asm/time.h>
  7. #include <asm-generic/rtc.h>
  8. #define SNI_CLOCK_TICK_RATE 3686400
  9. #define SNI_COUNTER2_DIV 64
  10. #define SNI_COUNTER0_DIV ((SNI_CLOCK_TICK_RATE / SNI_COUNTER2_DIV) / HZ)
  11. static void sni_a20r_timer_ack(void)
  12. {
  13. *(volatile u8 *)A20R_PT_TIM0_ACK = 0x0; wmb();
  14. }
  15. /*
  16. * a20r platform uses 2 counters to divide the input frequency.
  17. * Counter 2 output is connected to Counter 0 & 1 input.
  18. */
  19. static void __init sni_a20r_timer_setup(struct irqaction *irq)
  20. {
  21. *(volatile u8 *)(A20R_PT_CLOCK_BASE + 12) = 0x34; wmb();
  22. *(volatile u8 *)(A20R_PT_CLOCK_BASE + 0) = (SNI_COUNTER0_DIV) & 0xff; wmb();
  23. *(volatile u8 *)(A20R_PT_CLOCK_BASE + 0) = (SNI_COUNTER0_DIV >> 8) & 0xff; wmb();
  24. *(volatile u8 *)(A20R_PT_CLOCK_BASE + 12) = 0xb4; wmb();
  25. *(volatile u8 *)(A20R_PT_CLOCK_BASE + 8) = (SNI_COUNTER2_DIV) & 0xff; wmb();
  26. *(volatile u8 *)(A20R_PT_CLOCK_BASE + 8) = (SNI_COUNTER2_DIV >> 8) & 0xff; wmb();
  27. setup_irq(SNI_A20R_IRQ_TIMER, irq);
  28. mips_timer_ack = sni_a20r_timer_ack;
  29. }
  30. #define SNI_8254_TICK_RATE 1193182UL
  31. #define SNI_8254_TCSAMP_COUNTER ((SNI_8254_TICK_RATE / HZ) + 255)
  32. static __init unsigned long dosample(void)
  33. {
  34. u32 ct0, ct1;
  35. volatile u8 msb, lsb;
  36. /* Start the counter. */
  37. outb_p(0x34, 0x43);
  38. outb_p(SNI_8254_TCSAMP_COUNTER & 0xff, 0x40);
  39. outb(SNI_8254_TCSAMP_COUNTER >> 8, 0x40);
  40. /* Get initial counter invariant */
  41. ct0 = read_c0_count();
  42. /* Latch and spin until top byte of counter0 is zero */
  43. do {
  44. outb(0x00, 0x43);
  45. lsb = inb(0x40);
  46. msb = inb(0x40);
  47. ct1 = read_c0_count();
  48. } while (msb);
  49. /* Stop the counter. */
  50. outb(0x38, 0x43);
  51. /*
  52. * Return the difference, this is how far the r4k counter increments
  53. * for every 1/HZ seconds. We round off the nearest 1 MHz of master
  54. * clock (= 1000000 / HZ / 2).
  55. */
  56. /*return (ct1 - ct0 + (500000/HZ/2)) / (500000/HZ) * (500000/HZ);*/
  57. return (ct1 - ct0) / (500000/HZ) * (500000/HZ);
  58. }
  59. /*
  60. * Here we need to calibrate the cycle counter to at least be close.
  61. */
  62. void __init plat_time_init(void)
  63. {
  64. unsigned long r4k_ticks[3];
  65. unsigned long r4k_tick;
  66. /*
  67. * Figure out the r4k offset, the algorithm is very simple and works in
  68. * _all_ cases as long as the 8254 counter register itself works ok (as
  69. * an interrupt driving timer it does not because of bug, this is why
  70. * we are using the onchip r4k counter/compare register to serve this
  71. * purpose, but for r4k_offset calculation it will work ok for us).
  72. * There are other very complicated ways of performing this calculation
  73. * but this one works just fine so I am not going to futz around. ;-)
  74. */
  75. printk(KERN_INFO "Calibrating system timer... ");
  76. dosample(); /* Prime cache. */
  77. dosample(); /* Prime cache. */
  78. /* Zero is NOT an option. */
  79. do {
  80. r4k_ticks[0] = dosample();
  81. } while (!r4k_ticks[0]);
  82. do {
  83. r4k_ticks[1] = dosample();
  84. } while (!r4k_ticks[1]);
  85. if (r4k_ticks[0] != r4k_ticks[1]) {
  86. printk("warning: timer counts differ, retrying... ");
  87. r4k_ticks[2] = dosample();
  88. if (r4k_ticks[2] == r4k_ticks[0]
  89. || r4k_ticks[2] == r4k_ticks[1])
  90. r4k_tick = r4k_ticks[2];
  91. else {
  92. printk("disagreement, using average... ");
  93. r4k_tick = (r4k_ticks[0] + r4k_ticks[1]
  94. + r4k_ticks[2]) / 3;
  95. }
  96. } else
  97. r4k_tick = r4k_ticks[0];
  98. printk("%d [%d.%04d MHz CPU]\n", (int) r4k_tick,
  99. (int) (r4k_tick / (500000 / HZ)),
  100. (int) (r4k_tick % (500000 / HZ)));
  101. mips_hpt_frequency = r4k_tick * HZ;
  102. setup_pit_timer();
  103. }
  104. /*
  105. * R4k counter based timer interrupt. Works on RM200-225 and possibly
  106. * others but not on RM400
  107. */
  108. static void __init sni_cpu_timer_setup(struct irqaction *irq)
  109. {
  110. setup_irq(SNI_MIPS_IRQ_CPU_TIMER, irq);
  111. }
  112. void __init plat_timer_setup(struct irqaction *irq)
  113. {
  114. switch (sni_brd_type) {
  115. case SNI_BRD_10:
  116. case SNI_BRD_10NEW:
  117. case SNI_BRD_TOWER_OASIC:
  118. case SNI_BRD_MINITOWER:
  119. sni_a20r_timer_setup(irq);
  120. break;
  121. case SNI_BRD_PCI_TOWER:
  122. case SNI_BRD_RM200:
  123. case SNI_BRD_PCI_MTOWER:
  124. case SNI_BRD_PCI_DESKTOP:
  125. case SNI_BRD_PCI_TOWER_CPLUS:
  126. case SNI_BRD_PCI_MTOWER_CPLUS:
  127. sni_cpu_timer_setup(irq);
  128. break;
  129. }
  130. }
  131. unsigned long read_persistent_clock(void)
  132. {
  133. return -1;
  134. }