2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2007 by Ralf Baechle
7 * Copyright (C) 2009, 2010 Cavium Networks, Inc.
9 #include <linux/clocksource.h>
10 #include <linux/export.h>
11 #include <linux/init.h>
12 #include <linux/smp.h>
14 #include <asm/cpu-info.h>
17 #include <asm/octeon/octeon.h>
18 #include <asm/octeon/cvmx-ipd-defs.h>
19 #include <asm/octeon/cvmx-mio-defs.h>
22 * Set the current core's cvmcount counter to the value of the
23 * IPD_CLK_COUNT. We do this on all cores as they are brought
24 * on-line. This allows for a read from a local cpu register to
25 * access a synchronized counter.
27 * On CPU_CAVIUM_OCTEON2 the IPD_CLK_COUNT is scaled by rdiv/sdiv.
29 void octeon_init_cvmcount(void)
36 if (current_cpu_type() == CPU_CAVIUM_OCTEON2
) {
37 union cvmx_mio_rst_boot rst_boot
;
38 rst_boot
.u64
= cvmx_read_csr(CVMX_MIO_RST_BOOT
);
39 rdiv
= rst_boot
.s
.c_mul
; /* CPU clock */
40 sdiv
= rst_boot
.s
.pnr_mul
; /* I/O clock */
41 f
= (0x8000000000000000ull
/ sdiv
) * 2;
45 /* Clobber loops so GCC will not unroll the following while loop. */
46 asm("" : "+r" (loops
));
48 local_irq_save(flags
);
50 * Loop several times so we are executing from the cache,
51 * which should give more deterministic timing.
54 u64 ipd_clk_count
= cvmx_read_csr(CVMX_IPD_CLK_COUNT
);
56 ipd_clk_count
*= rdiv
;
58 asm("dmultu\t%[cnt],%[f]\n\t"
60 : [cnt
] "+r" (ipd_clk_count
),
65 write_c0_cvmcount(ipd_clk_count
);
67 local_irq_restore(flags
);
70 static cycle_t
octeon_cvmcount_read(struct clocksource
*cs
)
72 return read_c0_cvmcount();
75 static struct clocksource clocksource_mips
= {
76 .name
= "OCTEON_CVMCOUNT",
77 .read
= octeon_cvmcount_read
,
78 .mask
= CLOCKSOURCE_MASK(64),
79 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
82 unsigned long long notrace
sched_clock(void)
84 /* 64-bit arithmatic can overflow, so use 128-bit. */
86 unsigned long long rv
;
87 u64 mult
= clocksource_mips
.mult
;
88 u64 shift
= clocksource_mips
.shift
;
89 u64 cnt
= read_c0_cvmcount();
92 "dmultu\t%[cnt],%[mult]\n\t"
93 "nor\t%[t1],$0,%[shift]\n\t"
96 "dsll\t%[t2],%[t2],1\n\t"
97 "dsrlv\t%[rv],%[t3],%[shift]\n\t"
98 "dsllv\t%[t1],%[t2],%[t1]\n\t"
99 "or\t%[rv],%[t1],%[rv]\n\t"
100 : [rv
] "=&r" (rv
), [t1
] "=&r" (t1
), [t2
] "=&r" (t2
), [t3
] "=&r" (t3
)
101 : [cnt
] "r" (cnt
), [mult
] "r" (mult
), [shift
] "r" (shift
)
106 void __init
plat_time_init(void)
108 clocksource_mips
.rating
= 300;
109 clocksource_register_hz(&clocksource_mips
, octeon_get_clock_rate());
112 static u64 octeon_udelay_factor
;
113 static u64 octeon_ndelay_factor
;
115 void __init
octeon_setup_delays(void)
117 octeon_udelay_factor
= octeon_get_clock_rate() / 1000000;
119 * For __ndelay we divide by 2^16, so the factor is multiplied
120 * by the same amount.
122 octeon_ndelay_factor
= (octeon_udelay_factor
* 0x10000ull
) / 1000ull;
124 preset_lpj
= octeon_get_clock_rate() / HZ
;
127 void __udelay(unsigned long us
)
131 cur
= read_c0_cvmcount();
133 inc
= us
* octeon_udelay_factor
;
137 cur
= read_c0_cvmcount();
139 EXPORT_SYMBOL(__udelay
);
141 void __ndelay(unsigned long ns
)
145 cur
= read_c0_cvmcount();
147 inc
= ((ns
* octeon_ndelay_factor
) >> 16);
151 cur
= read_c0_cvmcount();
153 EXPORT_SYMBOL(__ndelay
);
155 void __delay(unsigned long loops
)
159 cur
= read_c0_cvmcount();
163 cur
= read_c0_cvmcount();
165 EXPORT_SYMBOL(__delay
);