xref: /OK3568_Linux_fs/kernel/arch/mips/mti-malta/malta-time.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Carsten Langgaard, carstenl@mips.com
4*4882a593Smuzhiyun  * Copyright (C) 1999,2000 MIPS Technologies, Inc.  All rights reserved.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Setting up the clock on the MIPS boards.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun #include <linux/types.h>
9*4882a593Smuzhiyun #include <linux/i8253.h>
10*4882a593Smuzhiyun #include <linux/init.h>
11*4882a593Smuzhiyun #include <linux/kernel_stat.h>
12*4882a593Smuzhiyun #include <linux/libfdt.h>
13*4882a593Smuzhiyun #include <linux/math64.h>
14*4882a593Smuzhiyun #include <linux/sched.h>
15*4882a593Smuzhiyun #include <linux/spinlock.h>
16*4882a593Smuzhiyun #include <linux/interrupt.h>
17*4882a593Smuzhiyun #include <linux/timex.h>
18*4882a593Smuzhiyun #include <linux/mc146818rtc.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <asm/cpu.h>
21*4882a593Smuzhiyun #include <asm/mipsregs.h>
22*4882a593Smuzhiyun #include <asm/mipsmtregs.h>
23*4882a593Smuzhiyun #include <asm/hardirq.h>
24*4882a593Smuzhiyun #include <asm/irq.h>
25*4882a593Smuzhiyun #include <asm/div64.h>
26*4882a593Smuzhiyun #include <asm/setup.h>
27*4882a593Smuzhiyun #include <asm/time.h>
28*4882a593Smuzhiyun #include <asm/mc146818-time.h>
29*4882a593Smuzhiyun #include <asm/msc01_ic.h>
30*4882a593Smuzhiyun #include <asm/mips-cps.h>
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #include <asm/mips-boards/generic.h>
33*4882a593Smuzhiyun #include <asm/mips-boards/maltaint.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun static int mips_cpu_timer_irq;
36*4882a593Smuzhiyun static int mips_cpu_perf_irq;
37*4882a593Smuzhiyun extern int cp0_perfcount_irq;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun static unsigned int gic_frequency;
40*4882a593Smuzhiyun 
mips_timer_dispatch(void)41*4882a593Smuzhiyun static void mips_timer_dispatch(void)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	do_IRQ(mips_cpu_timer_irq);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun 
mips_perf_dispatch(void)46*4882a593Smuzhiyun static void mips_perf_dispatch(void)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	do_IRQ(mips_cpu_perf_irq);
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
freqround(unsigned int freq,unsigned int amount)51*4882a593Smuzhiyun static unsigned int freqround(unsigned int freq, unsigned int amount)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	freq += amount;
54*4882a593Smuzhiyun 	freq -= freq % (amount*2);
55*4882a593Smuzhiyun 	return freq;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun  * Estimate CPU and GIC frequencies.
60*4882a593Smuzhiyun  */
estimate_frequencies(void)61*4882a593Smuzhiyun static void __init estimate_frequencies(void)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	unsigned long flags;
64*4882a593Smuzhiyun 	unsigned int count, start;
65*4882a593Smuzhiyun 	unsigned char secs1, secs2, ctrl;
66*4882a593Smuzhiyun 	int secs;
67*4882a593Smuzhiyun 	u64 giccount = 0, gicstart = 0;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun #if defined(CONFIG_KVM_GUEST) && CONFIG_KVM_GUEST_TIMER_FREQ
70*4882a593Smuzhiyun 	mips_hpt_frequency = CONFIG_KVM_GUEST_TIMER_FREQ * 1000000;
71*4882a593Smuzhiyun 	return;
72*4882a593Smuzhiyun #endif
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	local_irq_save(flags);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	if (mips_gic_present())
77*4882a593Smuzhiyun 		clear_gic_config(GIC_CONFIG_COUNTSTOP);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	/*
80*4882a593Smuzhiyun 	 * Read counters exactly on rising edge of update flag.
81*4882a593Smuzhiyun 	 * This helps get an accurate reading under virtualisation.
82*4882a593Smuzhiyun 	 */
83*4882a593Smuzhiyun 	while (CMOS_READ(RTC_REG_A) & RTC_UIP);
84*4882a593Smuzhiyun 	while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
85*4882a593Smuzhiyun 	start = read_c0_count();
86*4882a593Smuzhiyun 	if (mips_gic_present())
87*4882a593Smuzhiyun 		gicstart = read_gic_counter();
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	/* Wait for falling edge before reading RTC. */
90*4882a593Smuzhiyun 	while (CMOS_READ(RTC_REG_A) & RTC_UIP);
91*4882a593Smuzhiyun 	secs1 = CMOS_READ(RTC_SECONDS);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	/* Read counters again exactly on rising edge of update flag. */
94*4882a593Smuzhiyun 	while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
95*4882a593Smuzhiyun 	count = read_c0_count();
96*4882a593Smuzhiyun 	if (mips_gic_present())
97*4882a593Smuzhiyun 		giccount = read_gic_counter();
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/* Wait for falling edge before reading RTC again. */
100*4882a593Smuzhiyun 	while (CMOS_READ(RTC_REG_A) & RTC_UIP);
101*4882a593Smuzhiyun 	secs2 = CMOS_READ(RTC_SECONDS);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	ctrl = CMOS_READ(RTC_CONTROL);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	local_irq_restore(flags);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
108*4882a593Smuzhiyun 		secs1 = bcd2bin(secs1);
109*4882a593Smuzhiyun 		secs2 = bcd2bin(secs2);
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun 	secs = secs2 - secs1;
112*4882a593Smuzhiyun 	if (secs < 1)
113*4882a593Smuzhiyun 		secs += 60;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	count -= start;
116*4882a593Smuzhiyun 	count /= secs;
117*4882a593Smuzhiyun 	mips_hpt_frequency = count;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	if (mips_gic_present()) {
120*4882a593Smuzhiyun 		giccount = div_u64(giccount - gicstart, secs);
121*4882a593Smuzhiyun 		gic_frequency = giccount;
122*4882a593Smuzhiyun 	}
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
read_persistent_clock64(struct timespec64 * ts)125*4882a593Smuzhiyun void read_persistent_clock64(struct timespec64 *ts)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	ts->tv_sec = mc146818_get_cmos_time();
128*4882a593Smuzhiyun 	ts->tv_nsec = 0;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
get_c0_fdc_int(void)131*4882a593Smuzhiyun int get_c0_fdc_int(void)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	/*
134*4882a593Smuzhiyun 	 * Some cores claim the FDC is routable through the GIC, but it doesn't
135*4882a593Smuzhiyun 	 * actually seem to be connected for those Malta bitstreams.
136*4882a593Smuzhiyun 	 */
137*4882a593Smuzhiyun 	switch (current_cpu_type()) {
138*4882a593Smuzhiyun 	case CPU_INTERAPTIV:
139*4882a593Smuzhiyun 	case CPU_PROAPTIV:
140*4882a593Smuzhiyun 		return -1;
141*4882a593Smuzhiyun 	};
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	if (cpu_has_veic)
144*4882a593Smuzhiyun 		return -1;
145*4882a593Smuzhiyun 	else if (mips_gic_present())
146*4882a593Smuzhiyun 		return gic_get_c0_fdc_int();
147*4882a593Smuzhiyun 	else if (cp0_fdc_irq >= 0)
148*4882a593Smuzhiyun 		return MIPS_CPU_IRQ_BASE + cp0_fdc_irq;
149*4882a593Smuzhiyun 	else
150*4882a593Smuzhiyun 		return -1;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun 
get_c0_perfcount_int(void)153*4882a593Smuzhiyun int get_c0_perfcount_int(void)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	if (cpu_has_veic) {
156*4882a593Smuzhiyun 		set_vi_handler(MSC01E_INT_PERFCTR, mips_perf_dispatch);
157*4882a593Smuzhiyun 		mips_cpu_perf_irq = MSC01E_INT_BASE + MSC01E_INT_PERFCTR;
158*4882a593Smuzhiyun 	} else if (mips_gic_present()) {
159*4882a593Smuzhiyun 		mips_cpu_perf_irq = gic_get_c0_perfcount_int();
160*4882a593Smuzhiyun 	} else if (cp0_perfcount_irq >= 0) {
161*4882a593Smuzhiyun 		mips_cpu_perf_irq = MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
162*4882a593Smuzhiyun 	} else {
163*4882a593Smuzhiyun 		mips_cpu_perf_irq = -1;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	return mips_cpu_perf_irq;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(get_c0_perfcount_int);
169*4882a593Smuzhiyun 
get_c0_compare_int(void)170*4882a593Smuzhiyun unsigned int get_c0_compare_int(void)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	if (cpu_has_veic) {
173*4882a593Smuzhiyun 		set_vi_handler(MSC01E_INT_CPUCTR, mips_timer_dispatch);
174*4882a593Smuzhiyun 		mips_cpu_timer_irq = MSC01E_INT_BASE + MSC01E_INT_CPUCTR;
175*4882a593Smuzhiyun 	} else if (mips_gic_present()) {
176*4882a593Smuzhiyun 		mips_cpu_timer_irq = gic_get_c0_compare_int();
177*4882a593Smuzhiyun 	} else {
178*4882a593Smuzhiyun 		mips_cpu_timer_irq = MIPS_CPU_IRQ_BASE + cp0_compare_irq;
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	return mips_cpu_timer_irq;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
init_rtc(void)184*4882a593Smuzhiyun static void __init init_rtc(void)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	unsigned char freq, ctrl;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	/* Set 32KHz time base if not already set */
189*4882a593Smuzhiyun 	freq = CMOS_READ(RTC_FREQ_SELECT);
190*4882a593Smuzhiyun 	if ((freq & RTC_DIV_CTL) != RTC_REF_CLCK_32KHZ)
191*4882a593Smuzhiyun 		CMOS_WRITE(RTC_REF_CLCK_32KHZ, RTC_FREQ_SELECT);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	/* Ensure SET bit is clear so RTC can run */
194*4882a593Smuzhiyun 	ctrl = CMOS_READ(RTC_CONTROL);
195*4882a593Smuzhiyun 	if (ctrl & RTC_SET)
196*4882a593Smuzhiyun 		CMOS_WRITE(ctrl & ~RTC_SET, RTC_CONTROL);
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun #ifdef CONFIG_CLKSRC_MIPS_GIC
200*4882a593Smuzhiyun static u32 gic_frequency_dt;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun static struct property gic_frequency_prop = {
203*4882a593Smuzhiyun 	.name = "clock-frequency",
204*4882a593Smuzhiyun 	.length = sizeof(u32),
205*4882a593Smuzhiyun 	.value = &gic_frequency_dt,
206*4882a593Smuzhiyun };
207*4882a593Smuzhiyun 
update_gic_frequency_dt(void)208*4882a593Smuzhiyun static void update_gic_frequency_dt(void)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun 	struct device_node *node;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	gic_frequency_dt = cpu_to_be32(gic_frequency);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	node = of_find_compatible_node(NULL, NULL, "mti,gic-timer");
215*4882a593Smuzhiyun 	if (!node) {
216*4882a593Smuzhiyun 		pr_err("mti,gic-timer device node not found\n");
217*4882a593Smuzhiyun 		return;
218*4882a593Smuzhiyun 	}
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	if (of_update_property(node, &gic_frequency_prop) < 0)
221*4882a593Smuzhiyun 		pr_err("error updating gic frequency property\n");
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun #endif
225*4882a593Smuzhiyun 
plat_time_init(void)226*4882a593Smuzhiyun void __init plat_time_init(void)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	unsigned int prid = read_c0_prid() & (PRID_COMP_MASK | PRID_IMP_MASK);
229*4882a593Smuzhiyun 	unsigned int freq;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	init_rtc();
232*4882a593Smuzhiyun 	estimate_frequencies();
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	freq = mips_hpt_frequency;
235*4882a593Smuzhiyun 	if ((prid != (PRID_COMP_MIPS | PRID_IMP_20KC)) &&
236*4882a593Smuzhiyun 	    (prid != (PRID_COMP_MIPS | PRID_IMP_25KF)))
237*4882a593Smuzhiyun 		freq *= 2;
238*4882a593Smuzhiyun 	freq = freqround(freq, 5000);
239*4882a593Smuzhiyun 	printk("CPU frequency %d.%02d MHz\n", freq/1000000,
240*4882a593Smuzhiyun 	       (freq%1000000)*100/1000000);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun #ifdef CONFIG_I8253
243*4882a593Smuzhiyun 	/* Only Malta has a PIT. */
244*4882a593Smuzhiyun 	setup_pit_timer();
245*4882a593Smuzhiyun #endif
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	if (mips_gic_present()) {
248*4882a593Smuzhiyun 		freq = freqround(gic_frequency, 5000);
249*4882a593Smuzhiyun 		printk("GIC frequency %d.%02d MHz\n", freq/1000000,
250*4882a593Smuzhiyun 		       (freq%1000000)*100/1000000);
251*4882a593Smuzhiyun #ifdef CONFIG_CLKSRC_MIPS_GIC
252*4882a593Smuzhiyun 		update_gic_frequency_dt();
253*4882a593Smuzhiyun 		timer_probe();
254*4882a593Smuzhiyun #endif
255*4882a593Smuzhiyun 	}
256*4882a593Smuzhiyun }
257