Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / arch / mips / mips-boards / sim / sim_time.c
1 #include <linux/types.h>
2 #include <linux/init.h>
3 #include <linux/kernel_stat.h>
4 #include <linux/sched.h>
5 #include <linux/spinlock.h>
6
7 #include <asm/mipsregs.h>
8 #include <asm/ptrace.h>
9 #include <asm/hardirq.h>
10 #include <asm/div64.h>
11 #include <asm/cpu.h>
12 #include <asm/time.h>
13
14 #include <linux/interrupt.h>
15 #include <linux/mc146818rtc.h>
16 #include <linux/timex.h>
17 #include <asm/mipsregs.h>
18 #include <asm/ptrace.h>
19 #include <asm/hardirq.h>
20 #include <asm/irq.h>
21 #include <asm/div64.h>
22 #include <asm/cpu.h>
23 #include <asm/time.h>
24 #include <asm/mc146818-time.h>
25 #include <asm/msc01_ic.h>
26
27 #include <asm/mips-boards/generic.h>
28 #include <asm/mips-boards/prom.h>
29 #include <asm/mips-boards/simint.h>
30 #include <asm/mc146818-time.h>
31 #include <asm/smp.h>
32
33
34 unsigned long cpu_khz;
35
36 irqreturn_t sim_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
37 {
38 #ifdef CONFIG_SMP
39         int cpu = smp_processor_id();
40
41         /*
42          * CPU 0 handles the global timer interrupt job
43          * resets count/compare registers to trigger next timer int.
44          */
45 #ifndef CONFIG_MIPS_MT_SMTC
46         if (cpu == 0) {
47                 timer_interrupt(irq, dev_id, regs);
48         }
49         else {
50                 /* Everyone else needs to reset the timer int here as
51                    ll_local_timer_interrupt doesn't */
52                 /*
53                  * FIXME: need to cope with counter underflow.
54                  * More support needs to be added to kernel/time for
55                  * counter/timer interrupts on multiple CPU's
56                  */
57                 write_c0_compare (read_c0_count() + ( mips_hpt_frequency/HZ));
58         }
59 #else /* SMTC */
60         /*
61          *  In SMTC system, one Count/Compare set exists per VPE.
62          *  Which TC within a VPE gets the interrupt is essentially
63          *  random - we only know that it shouldn't be one with
64          *  IXMT set. Whichever TC gets the interrupt needs to
65          *  send special interprocessor interrupts to the other
66          *  TCs to make sure that they schedule, etc.
67          *
68          *  That code is specific to the SMTC kernel, not to
69          *  the simulation platform, so it's invoked from
70          *  the general MIPS timer_interrupt routine.
71          *
72          * We have a problem in that the interrupt vector code
73          * had to turn off the timer IM bit to avoid redundant
74          * entries, but we may never get to mips_cpu_irq_end
75          * to turn it back on again if the scheduler gets
76          * involved.  So we clear the pending timer here,
77          * and re-enable the mask...
78          */
79
80         int vpflags = dvpe();
81         write_c0_compare (read_c0_count() - 1);
82         clear_c0_cause(0x100 << MIPSCPU_INT_CPUCTR);
83         set_c0_status(0x100 << MIPSCPU_INT_CPUCTR);
84         irq_enable_hazard();
85         evpe(vpflags);
86
87         if(cpu_data[cpu].vpe_id == 0) timer_interrupt(irq, dev_id, regs);
88         else write_c0_compare (read_c0_count() + ( mips_hpt_frequency/HZ));
89         smtc_timer_broadcast(cpu_data[cpu].vpe_id);
90
91 #endif /* CONFIG_MIPS_MT_SMTC */
92
93         /*
94          * every CPU should do profiling and process accounting
95          */
96         local_timer_interrupt (irq, dev_id, regs);
97         return IRQ_HANDLED;
98 #else
99         return timer_interrupt (irq, dev_id, regs);
100 #endif
101 }
102
103
104
105 /*
106  * Estimate CPU frequency.  Sets mips_counter_frequency as a side-effect
107  */
108 static unsigned int __init estimate_cpu_frequency(void)
109 {
110         unsigned int prid = read_c0_prid() & 0xffff00;
111         unsigned int count;
112
113 #if 1
114         /*
115          * hardwire the board frequency to 12MHz.
116          */
117
118         if ((prid == (PRID_COMP_MIPS | PRID_IMP_20KC)) ||
119             (prid == (PRID_COMP_MIPS | PRID_IMP_25KF)))
120                 count = 12000000;
121         else
122                 count =  6000000;
123 #else
124         unsigned int flags;
125
126         local_irq_save(flags);
127
128         /* Start counter exactly on falling edge of update flag */
129         while (CMOS_READ(RTC_REG_A) & RTC_UIP);
130         while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
131
132         /* Start r4k counter. */
133         write_c0_count(0);
134
135         /* Read counter exactly on falling edge of update flag */
136         while (CMOS_READ(RTC_REG_A) & RTC_UIP);
137         while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
138
139         count = read_c0_count();
140
141         /* restore interrupts */
142         local_irq_restore(flags);
143 #endif
144
145         mips_hpt_frequency = count;
146
147         if ((prid != (PRID_COMP_MIPS | PRID_IMP_20KC)) &&
148             (prid != (PRID_COMP_MIPS | PRID_IMP_25KF)))
149                 count *= 2;
150
151         count += 5000;    /* round */
152         count -= count%10000;
153
154         return count;
155 }
156
157 void __init sim_time_init(void)
158 {
159         unsigned int est_freq, flags;
160
161         local_irq_save(flags);
162
163
164         /* Set Data mode - binary. */
165         CMOS_WRITE(CMOS_READ(RTC_CONTROL) | RTC_DM_BINARY, RTC_CONTROL);
166
167
168         est_freq = estimate_cpu_frequency ();
169
170         printk("CPU frequency %d.%02d MHz\n", est_freq/1000000,
171                (est_freq%1000000)*100/1000000);
172
173         cpu_khz = est_freq / 1000;
174
175         local_irq_restore(flags);
176 }
177
178 static int mips_cpu_timer_irq;
179
180 static void mips_timer_dispatch (struct pt_regs *regs)
181 {
182         do_IRQ (mips_cpu_timer_irq, regs);
183 }
184
185
186 void __init plat_timer_setup(struct irqaction *irq)
187 {
188         if (cpu_has_veic) {
189                 set_vi_handler(MSC01E_INT_CPUCTR, mips_timer_dispatch);
190                 mips_cpu_timer_irq = MSC01E_INT_BASE + MSC01E_INT_CPUCTR;
191         }
192         else {
193                 if (cpu_has_vint)
194                         set_vi_handler(MIPSCPU_INT_CPUCTR, mips_timer_dispatch);
195                 mips_cpu_timer_irq = MIPSCPU_INT_BASE + MIPSCPU_INT_CPUCTR;
196         }
197
198         /* we are using the cpu counter for timer interrupts */
199         irq->handler = sim_timer_interrupt;
200         setup_irq(mips_cpu_timer_irq, irq);
201
202 #ifdef CONFIG_SMP
203         /* irq_desc(riptor) is a global resource, when the interrupt overlaps
204            on seperate cpu's the first one tries to handle the second interrupt.
205            The effect is that the int remains disabled on the second cpu.
206            Mark the interrupt with IRQ_PER_CPU to avoid any confusion */
207         irq_desc[mips_cpu_timer_irq].status |= IRQ_PER_CPU;
208 #endif
209
210         /* to generate the first timer interrupt */
211         write_c0_compare(read_c0_count() + (mips_hpt_frequency/HZ));
212 }