15b3b1688SDavid Daney /*
25b3b1688SDavid Daney  * This file is subject to the terms and conditions of the GNU General Public
35b3b1688SDavid Daney  * License.  See the file "COPYING" in the main directory of this archive
45b3b1688SDavid Daney  * for more details.
55b3b1688SDavid Daney  *
65b3b1688SDavid Daney  * Copyright (C) 2007 by Ralf Baechle
770a26a21SDavid Daney  * Copyright (C) 2009, 2012 Cavium, Inc.
85b3b1688SDavid Daney  */
95b3b1688SDavid Daney #include <linux/clocksource.h>
104d1f0116SAaro Koskinen #include <linux/sched/clock.h>
11cae39d13SPaul Gortmaker #include <linux/export.h>
125b3b1688SDavid Daney #include <linux/init.h>
1354954a6dSDavid Daney #include <linux/smp.h>
145b3b1688SDavid Daney 
1554954a6dSDavid Daney #include <asm/cpu-info.h>
1669f24d17SRalf Baechle #include <asm/cpu-type.h>
175b3b1688SDavid Daney #include <asm/time.h>
185b3b1688SDavid Daney 
195b3b1688SDavid Daney #include <asm/octeon/octeon.h>
205b3b1688SDavid Daney #include <asm/octeon/cvmx-ipd-defs.h>
2154954a6dSDavid Daney #include <asm/octeon/cvmx-mio-defs.h>
22ac6d9b3aSChandrakala Chavva #include <asm/octeon/cvmx-rst-defs.h>
239bc22239SDavid Daney #include <asm/octeon/cvmx-fpa-defs.h>
2470a26a21SDavid Daney 
2570a26a21SDavid Daney static u64 f;
2670a26a21SDavid Daney static u64 rdiv;
2770a26a21SDavid Daney static u64 sdiv;
2870a26a21SDavid Daney static u64 octeon_udelay_factor;
2970a26a21SDavid Daney static u64 octeon_ndelay_factor;
3070a26a21SDavid Daney 
octeon_setup_delays(void)3170a26a21SDavid Daney void __init octeon_setup_delays(void)
3270a26a21SDavid Daney {
3370a26a21SDavid Daney 	octeon_udelay_factor = octeon_get_clock_rate() / 1000000;
3470a26a21SDavid Daney 	/*
3570a26a21SDavid Daney 	 * For __ndelay we divide by 2^16, so the factor is multiplied
3670a26a21SDavid Daney 	 * by the same amount.
3770a26a21SDavid Daney 	 */
3870a26a21SDavid Daney 	octeon_ndelay_factor = (octeon_udelay_factor * 0x10000ull) / 1000ull;
3970a26a21SDavid Daney 
4070a26a21SDavid Daney 	preset_lpj = octeon_get_clock_rate() / HZ;
4170a26a21SDavid Daney 
4270a26a21SDavid Daney 	if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {
4370a26a21SDavid Daney 		union cvmx_mio_rst_boot rst_boot;
44ac6d9b3aSChandrakala Chavva 
4570a26a21SDavid Daney 		rst_boot.u64 = cvmx_read_csr(CVMX_MIO_RST_BOOT);
4670a26a21SDavid Daney 		rdiv = rst_boot.s.c_mul;	/* CPU clock */
4770a26a21SDavid Daney 		sdiv = rst_boot.s.pnr_mul;	/* I/O clock */
4870a26a21SDavid Daney 		f = (0x8000000000000000ull / sdiv) * 2;
49ac6d9b3aSChandrakala Chavva 	} else if (current_cpu_type() == CPU_CAVIUM_OCTEON3) {
50ac6d9b3aSChandrakala Chavva 		union cvmx_rst_boot rst_boot;
51ac6d9b3aSChandrakala Chavva 
52ac6d9b3aSChandrakala Chavva 		rst_boot.u64 = cvmx_read_csr(CVMX_RST_BOOT);
53ac6d9b3aSChandrakala Chavva 		rdiv = rst_boot.s.c_mul;	/* CPU clock */
54ac6d9b3aSChandrakala Chavva 		sdiv = rst_boot.s.pnr_mul;	/* I/O clock */
55ac6d9b3aSChandrakala Chavva 		f = (0x8000000000000000ull / sdiv) * 2;
5670a26a21SDavid Daney 	}
57ac6d9b3aSChandrakala Chavva 
5870a26a21SDavid Daney }
5970a26a21SDavid Daney 
605b3b1688SDavid Daney /*
615b3b1688SDavid Daney  * Set the current core's cvmcount counter to the value of the
625b3b1688SDavid Daney  * IPD_CLK_COUNT.  We do this on all cores as they are brought
635b3b1688SDavid Daney  * on-line.  This allows for a read from a local cpu register to
645b3b1688SDavid Daney  * access a synchronized counter.
655b3b1688SDavid Daney  *
6654954a6dSDavid Daney  * On CPU_CAVIUM_OCTEON2 the IPD_CLK_COUNT is scaled by rdiv/sdiv.
675b3b1688SDavid Daney  */
octeon_init_cvmcount(void)685b3b1688SDavid Daney void octeon_init_cvmcount(void)
695b3b1688SDavid Daney {
709bc22239SDavid Daney 	u64 clk_reg;
715b3b1688SDavid Daney 	unsigned long flags;
725b3b1688SDavid Daney 	unsigned loops = 2;
735b3b1688SDavid Daney 
749bc22239SDavid Daney 	clk_reg = octeon_has_feature(OCTEON_FEATURE_FPA3) ?
759bc22239SDavid Daney 		CVMX_FPA_CLK_COUNT : CVMX_IPD_CLK_COUNT;
769bc22239SDavid Daney 
775b3b1688SDavid Daney 	/* Clobber loops so GCC will not unroll the following while loop. */
785b3b1688SDavid Daney 	asm("" : "+r" (loops));
795b3b1688SDavid Daney 
805b3b1688SDavid Daney 	local_irq_save(flags);
815b3b1688SDavid Daney 	/*
825b3b1688SDavid Daney 	 * Loop several times so we are executing from the cache,
835b3b1688SDavid Daney 	 * which should give more deterministic timing.
845b3b1688SDavid Daney 	 */
8554954a6dSDavid Daney 	while (loops--) {
869bc22239SDavid Daney 		u64 clk_count = cvmx_read_csr(clk_reg);
8754954a6dSDavid Daney 		if (rdiv != 0) {
889bc22239SDavid Daney 			clk_count *= rdiv;
8954954a6dSDavid Daney 			if (f != 0) {
9054954a6dSDavid Daney 				asm("dmultu\t%[cnt],%[f]\n\t"
9154954a6dSDavid Daney 				    "mfhi\t%[cnt]"
929bc22239SDavid Daney 				    : [cnt] "+r" (clk_count)
9370a26a21SDavid Daney 				    : [f] "r" (f)
9470a26a21SDavid Daney 				    : "hi", "lo");
9554954a6dSDavid Daney 			}
9654954a6dSDavid Daney 		}
979bc22239SDavid Daney 		write_c0_cvmcount(clk_count);
9854954a6dSDavid Daney 	}
995b3b1688SDavid Daney 	local_irq_restore(flags);
1005b3b1688SDavid Daney }
1015b3b1688SDavid Daney 
octeon_cvmcount_read(struct clocksource * cs)102a5a1d1c2SThomas Gleixner static u64 octeon_cvmcount_read(struct clocksource *cs)
1035b3b1688SDavid Daney {
1045b3b1688SDavid Daney 	return read_c0_cvmcount();
1055b3b1688SDavid Daney }
1065b3b1688SDavid Daney 
1075b3b1688SDavid Daney static struct clocksource clocksource_mips = {
1085b3b1688SDavid Daney 	.name		= "OCTEON_CVMCOUNT",
1095b3b1688SDavid Daney 	.read		= octeon_cvmcount_read,
1105b3b1688SDavid Daney 	.mask		= CLOCKSOURCE_MASK(64),
1115b3b1688SDavid Daney 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
1125b3b1688SDavid Daney };
1135b3b1688SDavid Daney 
sched_clock(void)114c6a3c851SDavid Daney unsigned long long notrace sched_clock(void)
115c6a3c851SDavid Daney {
1160e8a1d82SDavid Daney 	/* 64-bit arithmatic can overflow, so use 128-bit.  */
1170e8a1d82SDavid Daney 	u64 t1, t2, t3;
1180e8a1d82SDavid Daney 	unsigned long long rv;
1190e8a1d82SDavid Daney 	u64 mult = clocksource_mips.mult;
1200e8a1d82SDavid Daney 	u64 shift = clocksource_mips.shift;
1210e8a1d82SDavid Daney 	u64 cnt = read_c0_cvmcount();
1220e8a1d82SDavid Daney 
1230e8a1d82SDavid Daney 	asm (
1240e8a1d82SDavid Daney 		"dmultu\t%[cnt],%[mult]\n\t"
1250e8a1d82SDavid Daney 		"nor\t%[t1],$0,%[shift]\n\t"
1260e8a1d82SDavid Daney 		"mfhi\t%[t2]\n\t"
1270e8a1d82SDavid Daney 		"mflo\t%[t3]\n\t"
1280e8a1d82SDavid Daney 		"dsll\t%[t2],%[t2],1\n\t"
1290e8a1d82SDavid Daney 		"dsrlv\t%[rv],%[t3],%[shift]\n\t"
1300e8a1d82SDavid Daney 		"dsllv\t%[t1],%[t2],%[t1]\n\t"
1310e8a1d82SDavid Daney 		"or\t%[rv],%[t1],%[rv]\n\t"
1320e8a1d82SDavid Daney 		: [rv] "=&r" (rv), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3)
1330e8a1d82SDavid Daney 		: [cnt] "r" (cnt), [mult] "r" (mult), [shift] "r" (shift)
1340e8a1d82SDavid Daney 		: "hi", "lo");
1350e8a1d82SDavid Daney 	return rv;
136c6a3c851SDavid Daney }
137c6a3c851SDavid Daney 
plat_time_init(void)1385b3b1688SDavid Daney void __init plat_time_init(void)
1395b3b1688SDavid Daney {
1405b3b1688SDavid Daney 	clocksource_mips.rating = 300;
14175c4fd8cSJohn Stultz 	clocksource_register_hz(&clocksource_mips, octeon_get_clock_rate());
1425b3b1688SDavid Daney }
143ca148125SDavid Daney 
__udelay(unsigned long us)144ca148125SDavid Daney void __udelay(unsigned long us)
145ca148125SDavid Daney {
146ca148125SDavid Daney 	u64 cur, end, inc;
147ca148125SDavid Daney 
148ca148125SDavid Daney 	cur = read_c0_cvmcount();
149ca148125SDavid Daney 
150ca148125SDavid Daney 	inc = us * octeon_udelay_factor;
151ca148125SDavid Daney 	end = cur + inc;
152ca148125SDavid Daney 
153ca148125SDavid Daney 	while (end > cur)
154ca148125SDavid Daney 		cur = read_c0_cvmcount();
155ca148125SDavid Daney }
156ca148125SDavid Daney EXPORT_SYMBOL(__udelay);
157ca148125SDavid Daney 
__ndelay(unsigned long ns)158ca148125SDavid Daney void __ndelay(unsigned long ns)
159ca148125SDavid Daney {
160ca148125SDavid Daney 	u64 cur, end, inc;
161ca148125SDavid Daney 
162ca148125SDavid Daney 	cur = read_c0_cvmcount();
163ca148125SDavid Daney 
164ca148125SDavid Daney 	inc = ((ns * octeon_ndelay_factor) >> 16);
165ca148125SDavid Daney 	end = cur + inc;
166ca148125SDavid Daney 
167ca148125SDavid Daney 	while (end > cur)
168ca148125SDavid Daney 		cur = read_c0_cvmcount();
169ca148125SDavid Daney }
170ca148125SDavid Daney EXPORT_SYMBOL(__ndelay);
171ca148125SDavid Daney 
__delay(unsigned long loops)172ca148125SDavid Daney void __delay(unsigned long loops)
173ca148125SDavid Daney {
174ca148125SDavid Daney 	u64 cur, end;
175ca148125SDavid Daney 
176ca148125SDavid Daney 	cur = read_c0_cvmcount();
177ca148125SDavid Daney 	end = cur + loops;
178ca148125SDavid Daney 
179ca148125SDavid Daney 	while (end > cur)
180ca148125SDavid Daney 		cur = read_c0_cvmcount();
181ca148125SDavid Daney }
182ca148125SDavid Daney EXPORT_SYMBOL(__delay);
18370a26a21SDavid Daney 
18470a26a21SDavid Daney 
18570a26a21SDavid Daney /**
18670a26a21SDavid Daney  * octeon_io_clk_delay - wait for a given number of io clock cycles to pass.
18770a26a21SDavid Daney  *
18870a26a21SDavid Daney  * We scale the wait by the clock ratio, and then wait for the
18970a26a21SDavid Daney  * corresponding number of core clocks.
19070a26a21SDavid Daney  *
19170a26a21SDavid Daney  * @count: The number of clocks to wait.
19270a26a21SDavid Daney  */
octeon_io_clk_delay(unsigned long count)19370a26a21SDavid Daney void octeon_io_clk_delay(unsigned long count)
19470a26a21SDavid Daney {
19570a26a21SDavid Daney 	u64 cur, end;
19670a26a21SDavid Daney 
19770a26a21SDavid Daney 	cur = read_c0_cvmcount();
19870a26a21SDavid Daney 	if (rdiv != 0) {
19970a26a21SDavid Daney 		end = count * rdiv;
20070a26a21SDavid Daney 		if (f != 0) {
20170a26a21SDavid Daney 			asm("dmultu\t%[cnt],%[f]\n\t"
20270a26a21SDavid Daney 				"mfhi\t%[cnt]"
20370a26a21SDavid Daney 				: [cnt] "+r" (end)
20470a26a21SDavid Daney 				: [f] "r" (f)
20570a26a21SDavid Daney 				: "hi", "lo");
20670a26a21SDavid Daney 		}
20770a26a21SDavid Daney 		end = cur + end;
20870a26a21SDavid Daney 	} else {
20970a26a21SDavid Daney 		end = cur + count;
21070a26a21SDavid Daney 	}
21170a26a21SDavid Daney 	while (end > cur)
21270a26a21SDavid Daney 		cur = read_c0_cvmcount();
21370a26a21SDavid Daney }
21470a26a21SDavid Daney EXPORT_SYMBOL(octeon_io_clk_delay);
215