2 * Carsten Langgaard, carstenl@mips.com
3 * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
5 * This program is free software; you can distribute it and/or modify it
6 * under the terms of the GNU General Public License (Version 2) as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
18 * Setting up the clock on the MIPS boards.
20 #include <linux/types.h>
21 #include <linux/i8253.h>
22 #include <linux/init.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/libfdt.h>
25 #include <linux/math64.h>
26 #include <linux/sched.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/timex.h>
30 #include <linux/mc146818rtc.h>
33 #include <asm/mipsregs.h>
34 #include <asm/mipsmtregs.h>
35 #include <asm/hardirq.h>
37 #include <asm/div64.h>
38 #include <asm/setup.h>
40 #include <asm/mc146818-time.h>
41 #include <asm/msc01_ic.h>
42 #include <asm/mips-cps.h>
44 #include <asm/mips-boards/generic.h>
45 #include <asm/mips-boards/maltaint.h>
47 static int mips_cpu_timer_irq
;
48 static int mips_cpu_perf_irq
;
49 extern int cp0_perfcount_irq
;
51 static unsigned int gic_frequency
;
53 static void mips_timer_dispatch(void)
55 do_IRQ(mips_cpu_timer_irq
);
58 static void mips_perf_dispatch(void)
60 do_IRQ(mips_cpu_perf_irq
);
63 static unsigned int freqround(unsigned int freq
, unsigned int amount
)
66 freq
-= freq
% (amount
*2);
71 * Estimate CPU and GIC frequencies.
73 static void __init
estimate_frequencies(void)
76 unsigned int count
, start
;
77 unsigned char secs1
, secs2
, ctrl
;
79 u64 giccount
= 0, gicstart
= 0;
81 #if defined(CONFIG_KVM_GUEST) && CONFIG_KVM_GUEST_TIMER_FREQ
82 mips_hpt_frequency
= CONFIG_KVM_GUEST_TIMER_FREQ
* 1000000;
86 local_irq_save(flags
);
88 if (mips_gic_present())
89 clear_gic_config(GIC_CONFIG_COUNTSTOP
);
92 * Read counters exactly on rising edge of update flag.
93 * This helps get an accurate reading under virtualisation.
95 while (CMOS_READ(RTC_REG_A
) & RTC_UIP
);
96 while (!(CMOS_READ(RTC_REG_A
) & RTC_UIP
));
97 start
= read_c0_count();
98 if (mips_gic_present())
99 gicstart
= read_gic_counter();
101 /* Wait for falling edge before reading RTC. */
102 while (CMOS_READ(RTC_REG_A
) & RTC_UIP
);
103 secs1
= CMOS_READ(RTC_SECONDS
);
105 /* Read counters again exactly on rising edge of update flag. */
106 while (!(CMOS_READ(RTC_REG_A
) & RTC_UIP
));
107 count
= read_c0_count();
108 if (mips_gic_present())
109 giccount
= read_gic_counter();
111 /* Wait for falling edge before reading RTC again. */
112 while (CMOS_READ(RTC_REG_A
) & RTC_UIP
);
113 secs2
= CMOS_READ(RTC_SECONDS
);
115 ctrl
= CMOS_READ(RTC_CONTROL
);
117 local_irq_restore(flags
);
119 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
120 secs1
= bcd2bin(secs1
);
121 secs2
= bcd2bin(secs2
);
123 secs
= secs2
- secs1
;
129 mips_hpt_frequency
= count
;
131 if (mips_gic_present()) {
132 giccount
= div_u64(giccount
- gicstart
, secs
);
133 gic_frequency
= giccount
;
137 void read_persistent_clock(struct timespec
*ts
)
139 ts
->tv_sec
= mc146818_get_cmos_time();
143 int get_c0_fdc_int(void)
146 * Some cores claim the FDC is routable through the GIC, but it doesn't
147 * actually seem to be connected for those Malta bitstreams.
149 switch (current_cpu_type()) {
157 else if (mips_gic_present())
158 return gic_get_c0_fdc_int();
159 else if (cp0_fdc_irq
>= 0)
160 return MIPS_CPU_IRQ_BASE
+ cp0_fdc_irq
;
165 int get_c0_perfcount_int(void)
168 set_vi_handler(MSC01E_INT_PERFCTR
, mips_perf_dispatch
);
169 mips_cpu_perf_irq
= MSC01E_INT_BASE
+ MSC01E_INT_PERFCTR
;
170 } else if (mips_gic_present()) {
171 mips_cpu_perf_irq
= gic_get_c0_perfcount_int();
172 } else if (cp0_perfcount_irq
>= 0) {
173 mips_cpu_perf_irq
= MIPS_CPU_IRQ_BASE
+ cp0_perfcount_irq
;
175 mips_cpu_perf_irq
= -1;
178 return mips_cpu_perf_irq
;
180 EXPORT_SYMBOL_GPL(get_c0_perfcount_int
);
182 unsigned int get_c0_compare_int(void)
185 set_vi_handler(MSC01E_INT_CPUCTR
, mips_timer_dispatch
);
186 mips_cpu_timer_irq
= MSC01E_INT_BASE
+ MSC01E_INT_CPUCTR
;
187 } else if (mips_gic_present()) {
188 mips_cpu_timer_irq
= gic_get_c0_compare_int();
190 mips_cpu_timer_irq
= MIPS_CPU_IRQ_BASE
+ cp0_compare_irq
;
193 return mips_cpu_timer_irq
;
196 static void __init
init_rtc(void)
198 unsigned char freq
, ctrl
;
200 /* Set 32KHz time base if not already set */
201 freq
= CMOS_READ(RTC_FREQ_SELECT
);
202 if ((freq
& RTC_DIV_CTL
) != RTC_REF_CLCK_32KHZ
)
203 CMOS_WRITE(RTC_REF_CLCK_32KHZ
, RTC_FREQ_SELECT
);
205 /* Ensure SET bit is clear so RTC can run */
206 ctrl
= CMOS_READ(RTC_CONTROL
);
208 CMOS_WRITE(ctrl
& ~RTC_SET
, RTC_CONTROL
);
211 #ifdef CONFIG_CLKSRC_MIPS_GIC
212 static u32 gic_frequency_dt
;
214 static struct property gic_frequency_prop
= {
215 .name
= "clock-frequency",
216 .length
= sizeof(u32
),
217 .value
= &gic_frequency_dt
,
220 static void update_gic_frequency_dt(void)
222 struct device_node
*node
;
224 gic_frequency_dt
= cpu_to_be32(gic_frequency
);
226 node
= of_find_compatible_node(NULL
, NULL
, "mti,gic-timer");
228 pr_err("mti,gic-timer device node not found\n");
232 if (of_update_property(node
, &gic_frequency_prop
) < 0)
233 pr_err("error updating gic frequency property\n");
238 void __init
plat_time_init(void)
240 unsigned int prid
= read_c0_prid() & (PRID_COMP_MASK
| PRID_IMP_MASK
);
244 estimate_frequencies();
246 freq
= mips_hpt_frequency
;
247 if ((prid
!= (PRID_COMP_MIPS
| PRID_IMP_20KC
)) &&
248 (prid
!= (PRID_COMP_MIPS
| PRID_IMP_25KF
)))
250 freq
= freqround(freq
, 5000);
251 printk("CPU frequency %d.%02d MHz\n", freq
/1000000,
252 (freq
%1000000)*100/1000000);
254 mips_scroll_message();
257 /* Only Malta has a PIT. */
261 if (mips_gic_present()) {
262 freq
= freqround(gic_frequency
, 5000);
263 printk("GIC frequency %d.%02d MHz\n", freq
/1000000,
264 (freq
%1000000)*100/1000000);
265 #ifdef CONFIG_CLKSRC_MIPS_GIC
266 update_gic_frequency_dt();