time.c 4.8 KB

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