time.c 4.9 KB

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