time.c 4.5 KB

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