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