time.c 4.7 KB

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