time.c 4.4 KB

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