1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * This file is subject to the terms and conditions of the GNU General Public
3*4882a593Smuzhiyun * License. See the file "COPYING" in the main directory of this archive
4*4882a593Smuzhiyun * for more details.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Time operations for IP22 machines. Original code may come from
7*4882a593Smuzhiyun * Ralf Baechle or David S. Miller (sorry guys, i'm really not sure)
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Copyright (C) 2001 by Ladislav Michl
10*4882a593Smuzhiyun * Copyright (C) 2003, 06 Ralf Baechle (ralf@linux-mips.org)
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun #include <linux/bcd.h>
13*4882a593Smuzhiyun #include <linux/i8253.h>
14*4882a593Smuzhiyun #include <linux/init.h>
15*4882a593Smuzhiyun #include <linux/irq.h>
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/interrupt.h>
18*4882a593Smuzhiyun #include <linux/kernel_stat.h>
19*4882a593Smuzhiyun #include <linux/time.h>
20*4882a593Smuzhiyun #include <linux/ftrace.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include <asm/cpu.h>
23*4882a593Smuzhiyun #include <asm/mipsregs.h>
24*4882a593Smuzhiyun #include <asm/io.h>
25*4882a593Smuzhiyun #include <asm/irq.h>
26*4882a593Smuzhiyun #include <asm/time.h>
27*4882a593Smuzhiyun #include <asm/sgialib.h>
28*4882a593Smuzhiyun #include <asm/sgi/ioc.h>
29*4882a593Smuzhiyun #include <asm/sgi/hpc3.h>
30*4882a593Smuzhiyun #include <asm/sgi/ip22.h>
31*4882a593Smuzhiyun
dosample(void)32*4882a593Smuzhiyun static unsigned long dosample(void)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun u32 ct0, ct1;
35*4882a593Smuzhiyun u8 msb;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /* Start the counter. */
38*4882a593Smuzhiyun sgint->tcword = (SGINT_TCWORD_CNT2 | SGINT_TCWORD_CALL |
39*4882a593Smuzhiyun SGINT_TCWORD_MRGEN);
40*4882a593Smuzhiyun sgint->tcnt2 = SGINT_TCSAMP_COUNTER & 0xff;
41*4882a593Smuzhiyun sgint->tcnt2 = SGINT_TCSAMP_COUNTER >> 8;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* Get initial counter invariant */
44*4882a593Smuzhiyun ct0 = read_c0_count();
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /* Latch and spin until top byte of counter2 is zero */
47*4882a593Smuzhiyun do {
48*4882a593Smuzhiyun writeb(SGINT_TCWORD_CNT2 | SGINT_TCWORD_CLAT, &sgint->tcword);
49*4882a593Smuzhiyun (void) readb(&sgint->tcnt2);
50*4882a593Smuzhiyun msb = readb(&sgint->tcnt2);
51*4882a593Smuzhiyun ct1 = read_c0_count();
52*4882a593Smuzhiyun } while (msb);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* Stop the counter. */
55*4882a593Smuzhiyun writeb(SGINT_TCWORD_CNT2 | SGINT_TCWORD_CALL | SGINT_TCWORD_MSWST,
56*4882a593Smuzhiyun &sgint->tcword);
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * Return the difference, this is how far the r4k counter increments
59*4882a593Smuzhiyun * for every 1/HZ seconds. We round off the nearest 1 MHz of master
60*4882a593Smuzhiyun * clock (= 1000000 / HZ / 2).
61*4882a593Smuzhiyun */
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun return (ct1 - ct0) / (500000/HZ) * (500000/HZ);
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun * Here we need to calibrate the cycle counter to at least be close.
68*4882a593Smuzhiyun */
plat_time_init(void)69*4882a593Smuzhiyun __init void plat_time_init(void)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun unsigned long r4k_ticks[3];
72*4882a593Smuzhiyun unsigned long r4k_tick;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun * Figure out the r4k offset, the algorithm is very simple and works in
76*4882a593Smuzhiyun * _all_ cases as long as the 8254 counter register itself works ok (as
77*4882a593Smuzhiyun * an interrupt driving timer it does not because of bug, this is why
78*4882a593Smuzhiyun * we are using the onchip r4k counter/compare register to serve this
79*4882a593Smuzhiyun * purpose, but for r4k_offset calculation it will work ok for us).
80*4882a593Smuzhiyun * There are other very complicated ways of performing this calculation
81*4882a593Smuzhiyun * but this one works just fine so I am not going to futz around. ;-)
82*4882a593Smuzhiyun */
83*4882a593Smuzhiyun printk(KERN_INFO "Calibrating system timer... ");
84*4882a593Smuzhiyun dosample(); /* Prime cache. */
85*4882a593Smuzhiyun dosample(); /* Prime cache. */
86*4882a593Smuzhiyun /* Zero is NOT an option. */
87*4882a593Smuzhiyun do {
88*4882a593Smuzhiyun r4k_ticks[0] = dosample();
89*4882a593Smuzhiyun } while (!r4k_ticks[0]);
90*4882a593Smuzhiyun do {
91*4882a593Smuzhiyun r4k_ticks[1] = dosample();
92*4882a593Smuzhiyun } while (!r4k_ticks[1]);
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun if (r4k_ticks[0] != r4k_ticks[1]) {
95*4882a593Smuzhiyun printk("warning: timer counts differ, retrying... ");
96*4882a593Smuzhiyun r4k_ticks[2] = dosample();
97*4882a593Smuzhiyun if (r4k_ticks[2] == r4k_ticks[0]
98*4882a593Smuzhiyun || r4k_ticks[2] == r4k_ticks[1])
99*4882a593Smuzhiyun r4k_tick = r4k_ticks[2];
100*4882a593Smuzhiyun else {
101*4882a593Smuzhiyun printk("disagreement, using average... ");
102*4882a593Smuzhiyun r4k_tick = (r4k_ticks[0] + r4k_ticks[1]
103*4882a593Smuzhiyun + r4k_ticks[2]) / 3;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun } else
106*4882a593Smuzhiyun r4k_tick = r4k_ticks[0];
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun printk("%d [%d.%04d MHz CPU]\n", (int) r4k_tick,
109*4882a593Smuzhiyun (int) (r4k_tick / (500000 / HZ)),
110*4882a593Smuzhiyun (int) (r4k_tick % (500000 / HZ)));
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun mips_hpt_frequency = r4k_tick * HZ;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun if (ip22_is_fullhouse())
115*4882a593Smuzhiyun setup_pit_timer();
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun /* Generic SGI handler for (spurious) 8254 interrupts */
indy_8254timer_irq(void)119*4882a593Smuzhiyun void __irq_entry indy_8254timer_irq(void)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun int irq = SGI_8254_0_IRQ;
122*4882a593Smuzhiyun ULONG cnt;
123*4882a593Smuzhiyun char c;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun irq_enter();
126*4882a593Smuzhiyun kstat_incr_irq_this_cpu(irq);
127*4882a593Smuzhiyun printk(KERN_ALERT "Oops, got 8254 interrupt.\n");
128*4882a593Smuzhiyun ArcRead(0, &c, 1, &cnt);
129*4882a593Smuzhiyun ArcEnterInteractiveMode();
130*4882a593Smuzhiyun irq_exit();
131*4882a593Smuzhiyun }
132