1 #include <linux/types.h>
2 #include <linux/interrupt.h>
3 #include <linux/time.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)
12 static void sni_a20r_timer_ack(void)
14 *(volatile u8 *)A20R_PT_TIM0_ACK = 0x0; wmb();
18 * a20r platform uses 2 counters to divide the input frequency.
19 * Counter 2 output is connected to Counter 0 & 1 input.
21 static void __init sni_a20r_timer_setup(struct irqaction *irq)
23 *(volatile u8 *)(A20R_PT_CLOCK_BASE + 12) = 0x34; wmb();
24 *(volatile u8 *)(A20R_PT_CLOCK_BASE + 0) = (SNI_COUNTER0_DIV) & 0xff; wmb();
25 *(volatile u8 *)(A20R_PT_CLOCK_BASE + 0) = (SNI_COUNTER0_DIV >> 8) & 0xff; wmb();
27 *(volatile u8 *)(A20R_PT_CLOCK_BASE + 12) = 0xb4; wmb();
28 *(volatile u8 *)(A20R_PT_CLOCK_BASE + 8) = (SNI_COUNTER2_DIV) & 0xff; wmb();
29 *(volatile u8 *)(A20R_PT_CLOCK_BASE + 8) = (SNI_COUNTER2_DIV >> 8) & 0xff; wmb();
31 setup_irq(SNI_A20R_IRQ_TIMER, irq);
32 mips_timer_ack = sni_a20r_timer_ack;
35 #define SNI_8254_TICK_RATE 1193182UL
37 #define SNI_8254_TCSAMP_COUNTER ((SNI_8254_TICK_RATE / HZ) + 255)
39 static __init unsigned long dosample(void)
44 /* Start the counter. */
46 outb_p(SNI_8254_TCSAMP_COUNTER & 0xff, 0x40);
47 outb (SNI_8254_TCSAMP_COUNTER >> 8, 0x40);
49 /* Get initial counter invariant */
50 ct0 = read_c0_count();
52 /* Latch and spin until top byte of counter0 is zero */
57 ct1 = read_c0_count();
60 /* Stop the counter. */
63 * Return the difference, this is how far the r4k counter increments
64 * for every 1/HZ seconds. We round off the nearest 1 MHz of master
65 * clock (= 1000000 / HZ / 2).
67 /*return (ct1 - ct0 + (500000/HZ/2)) / (500000/HZ) * (500000/HZ);*/
68 return (ct1 - ct0) / (500000/HZ) * (500000/HZ);
72 * Here we need to calibrate the cycle counter to at least be close.
74 __init void sni_cpu_time_init(void)
76 unsigned long r4k_ticks[3];
77 unsigned long r4k_tick;
80 * Figure out the r4k offset, the algorithm is very simple and works in
81 * _all_ cases as long as the 8254 counter register itself works ok (as
82 * an interrupt driving timer it does not because of bug, this is why
83 * we are using the onchip r4k counter/compare register to serve this
84 * purpose, but for r4k_offset calculation it will work ok for us).
85 * There are other very complicated ways of performing this calculation
86 * but this one works just fine so I am not going to futz around. ;-)
88 printk(KERN_INFO "Calibrating system timer... ");
89 dosample(); /* Prime cache. */
90 dosample(); /* Prime cache. */
91 /* Zero is NOT an option. */
93 r4k_ticks[0] = dosample();
94 } while (!r4k_ticks[0]);
96 r4k_ticks[1] = dosample();
97 } while (!r4k_ticks[1]);
99 if (r4k_ticks[0] != r4k_ticks[1]) {
100 printk("warning: timer counts differ, retrying... ");
101 r4k_ticks[2] = dosample();
102 if (r4k_ticks[2] == r4k_ticks[0]
103 || r4k_ticks[2] == r4k_ticks[1])
104 r4k_tick = r4k_ticks[2];
106 printk("disagreement, using average... ");
107 r4k_tick = (r4k_ticks[0] + r4k_ticks[1]
111 r4k_tick = r4k_ticks[0];
113 printk("%d [%d.%04d MHz CPU]\n", (int) r4k_tick,
114 (int) (r4k_tick / (500000 / HZ)),
115 (int) (r4k_tick % (500000 / HZ)));
117 mips_hpt_frequency = r4k_tick * HZ;
121 * R4k counter based timer interrupt. Works on RM200-225 and possibly
122 * others but not on RM400
124 static void __init sni_cpu_timer_setup(struct irqaction *irq)
126 setup_irq(SNI_MIPS_IRQ_CPU_TIMER, irq);
129 void __init plat_timer_setup(struct irqaction *irq)
131 switch (sni_brd_type) {
134 case SNI_BRD_TOWER_OASIC:
135 case SNI_BRD_MINITOWER:
136 sni_a20r_timer_setup (irq);
139 case SNI_BRD_PCI_TOWER:
141 case SNI_BRD_PCI_MTOWER:
142 case SNI_BRD_PCI_DESKTOP:
143 case SNI_BRD_PCI_TOWER_CPLUS:
144 case SNI_BRD_PCI_MTOWER_CPLUS:
145 sni_cpu_timer_setup (irq);