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