blob: 23c2344a35525fa9a0badfb3bbca9a89d60293c5 [file] [log] [blame]
David Daney5b3b1682009-01-08 16:46:40 -08001/*
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
4 * for more details.
5 *
6 * Copyright (C) 2007 by Ralf Baechle
David Daney70a26a22012-08-21 11:45:05 -07007 * Copyright (C) 2009, 2012 Cavium, Inc.
David Daney5b3b1682009-01-08 16:46:40 -08008 */
9#include <linux/clocksource.h>
Paul Gortmakercae39d12011-07-28 18:46:31 -040010#include <linux/export.h>
David Daney5b3b1682009-01-08 16:46:40 -080011#include <linux/init.h>
David Daney54954a62010-10-07 16:03:47 -070012#include <linux/smp.h>
David Daney5b3b1682009-01-08 16:46:40 -080013
David Daney54954a62010-10-07 16:03:47 -070014#include <asm/cpu-info.h>
Ralf Baechle69f24d12013-09-17 10:25:47 +020015#include <asm/cpu-type.h>
David Daney5b3b1682009-01-08 16:46:40 -080016#include <asm/time.h>
17
18#include <asm/octeon/octeon.h>
19#include <asm/octeon/cvmx-ipd-defs.h>
David Daney54954a62010-10-07 16:03:47 -070020#include <asm/octeon/cvmx-mio-defs.h>
Chandrakala Chavvaac6d9b32015-01-15 16:11:17 +030021#include <asm/octeon/cvmx-rst-defs.h>
David Daney9bc22232016-02-09 11:00:10 -080022#include <asm/octeon/cvmx-fpa-defs.h>
David Daney70a26a22012-08-21 11:45:05 -070023
24static u64 f;
25static u64 rdiv;
26static u64 sdiv;
27static u64 octeon_udelay_factor;
28static u64 octeon_ndelay_factor;
29
30void __init octeon_setup_delays(void)
31{
32 octeon_udelay_factor = octeon_get_clock_rate() / 1000000;
33 /*
34 * For __ndelay we divide by 2^16, so the factor is multiplied
35 * by the same amount.
36 */
37 octeon_ndelay_factor = (octeon_udelay_factor * 0x10000ull) / 1000ull;
38
39 preset_lpj = octeon_get_clock_rate() / HZ;
40
41 if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {
42 union cvmx_mio_rst_boot rst_boot;
Chandrakala Chavvaac6d9b32015-01-15 16:11:17 +030043
David Daney70a26a22012-08-21 11:45:05 -070044 rst_boot.u64 = cvmx_read_csr(CVMX_MIO_RST_BOOT);
45 rdiv = rst_boot.s.c_mul; /* CPU clock */
46 sdiv = rst_boot.s.pnr_mul; /* I/O clock */
47 f = (0x8000000000000000ull / sdiv) * 2;
Chandrakala Chavvaac6d9b32015-01-15 16:11:17 +030048 } else if (current_cpu_type() == CPU_CAVIUM_OCTEON3) {
49 union cvmx_rst_boot rst_boot;
50
51 rst_boot.u64 = cvmx_read_csr(CVMX_RST_BOOT);
52 rdiv = rst_boot.s.c_mul; /* CPU clock */
53 sdiv = rst_boot.s.pnr_mul; /* I/O clock */
54 f = (0x8000000000000000ull / sdiv) * 2;
David Daney70a26a22012-08-21 11:45:05 -070055 }
Chandrakala Chavvaac6d9b32015-01-15 16:11:17 +030056
David Daney70a26a22012-08-21 11:45:05 -070057}
58
David Daney5b3b1682009-01-08 16:46:40 -080059/*
60 * Set the current core's cvmcount counter to the value of the
61 * IPD_CLK_COUNT. We do this on all cores as they are brought
62 * on-line. This allows for a read from a local cpu register to
63 * access a synchronized counter.
64 *
David Daney54954a62010-10-07 16:03:47 -070065 * On CPU_CAVIUM_OCTEON2 the IPD_CLK_COUNT is scaled by rdiv/sdiv.
David Daney5b3b1682009-01-08 16:46:40 -080066 */
67void octeon_init_cvmcount(void)
68{
David Daney9bc22232016-02-09 11:00:10 -080069 u64 clk_reg;
David Daney5b3b1682009-01-08 16:46:40 -080070 unsigned long flags;
71 unsigned loops = 2;
72
David Daney9bc22232016-02-09 11:00:10 -080073 clk_reg = octeon_has_feature(OCTEON_FEATURE_FPA3) ?
74 CVMX_FPA_CLK_COUNT : CVMX_IPD_CLK_COUNT;
75
David Daney5b3b1682009-01-08 16:46:40 -080076 /* Clobber loops so GCC will not unroll the following while loop. */
77 asm("" : "+r" (loops));
78
79 local_irq_save(flags);
80 /*
81 * Loop several times so we are executing from the cache,
82 * which should give more deterministic timing.
83 */
David Daney54954a62010-10-07 16:03:47 -070084 while (loops--) {
David Daney9bc22232016-02-09 11:00:10 -080085 u64 clk_count = cvmx_read_csr(clk_reg);
David Daney54954a62010-10-07 16:03:47 -070086 if (rdiv != 0) {
David Daney9bc22232016-02-09 11:00:10 -080087 clk_count *= rdiv;
David Daney54954a62010-10-07 16:03:47 -070088 if (f != 0) {
89 asm("dmultu\t%[cnt],%[f]\n\t"
90 "mfhi\t%[cnt]"
David Daney9bc22232016-02-09 11:00:10 -080091 : [cnt] "+r" (clk_count)
David Daney70a26a22012-08-21 11:45:05 -070092 : [f] "r" (f)
93 : "hi", "lo");
David Daney54954a62010-10-07 16:03:47 -070094 }
95 }
David Daney9bc22232016-02-09 11:00:10 -080096 write_c0_cvmcount(clk_count);
David Daney54954a62010-10-07 16:03:47 -070097 }
David Daney5b3b1682009-01-08 16:46:40 -080098 local_irq_restore(flags);
99}
100
Coly Lid0ce9a52009-04-23 03:06:06 +0800101static cycle_t octeon_cvmcount_read(struct clocksource *cs)
David Daney5b3b1682009-01-08 16:46:40 -0800102{
103 return read_c0_cvmcount();
104}
105
106static struct clocksource clocksource_mips = {
107 .name = "OCTEON_CVMCOUNT",
108 .read = octeon_cvmcount_read,
109 .mask = CLOCKSOURCE_MASK(64),
110 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
111};
112
David Daneyc6a3c852009-12-23 13:18:54 -0800113unsigned long long notrace sched_clock(void)
114{
David Daney0e8a1d82010-01-08 14:47:36 -0800115 /* 64-bit arithmatic can overflow, so use 128-bit. */
David Daney0e8a1d82010-01-08 14:47:36 -0800116 u64 t1, t2, t3;
117 unsigned long long rv;
118 u64 mult = clocksource_mips.mult;
119 u64 shift = clocksource_mips.shift;
120 u64 cnt = read_c0_cvmcount();
121
122 asm (
123 "dmultu\t%[cnt],%[mult]\n\t"
124 "nor\t%[t1],$0,%[shift]\n\t"
125 "mfhi\t%[t2]\n\t"
126 "mflo\t%[t3]\n\t"
127 "dsll\t%[t2],%[t2],1\n\t"
128 "dsrlv\t%[rv],%[t3],%[shift]\n\t"
129 "dsllv\t%[t1],%[t2],%[t1]\n\t"
130 "or\t%[rv],%[t1],%[rv]\n\t"
131 : [rv] "=&r" (rv), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3)
132 : [cnt] "r" (cnt), [mult] "r" (mult), [shift] "r" (shift)
133 : "hi", "lo");
134 return rv;
David Daneyc6a3c852009-12-23 13:18:54 -0800135}
136
David Daney5b3b1682009-01-08 16:46:40 -0800137void __init plat_time_init(void)
138{
139 clocksource_mips.rating = 300;
John Stultz75c4fd82010-04-26 20:23:11 -0700140 clocksource_register_hz(&clocksource_mips, octeon_get_clock_rate());
David Daney5b3b1682009-01-08 16:46:40 -0800141}
David Daneyca148122010-06-01 13:18:15 -0700142
David Daneyca148122010-06-01 13:18:15 -0700143void __udelay(unsigned long us)
144{
145 u64 cur, end, inc;
146
147 cur = read_c0_cvmcount();
148
149 inc = us * octeon_udelay_factor;
150 end = cur + inc;
151
152 while (end > cur)
153 cur = read_c0_cvmcount();
154}
155EXPORT_SYMBOL(__udelay);
156
157void __ndelay(unsigned long ns)
158{
159 u64 cur, end, inc;
160
161 cur = read_c0_cvmcount();
162
163 inc = ((ns * octeon_ndelay_factor) >> 16);
164 end = cur + inc;
165
166 while (end > cur)
167 cur = read_c0_cvmcount();
168}
169EXPORT_SYMBOL(__ndelay);
170
171void __delay(unsigned long loops)
172{
173 u64 cur, end;
174
175 cur = read_c0_cvmcount();
176 end = cur + loops;
177
178 while (end > cur)
179 cur = read_c0_cvmcount();
180}
181EXPORT_SYMBOL(__delay);
David Daney70a26a22012-08-21 11:45:05 -0700182
183
184/**
185 * octeon_io_clk_delay - wait for a given number of io clock cycles to pass.
186 *
187 * We scale the wait by the clock ratio, and then wait for the
188 * corresponding number of core clocks.
189 *
190 * @count: The number of clocks to wait.
191 */
192void octeon_io_clk_delay(unsigned long count)
193{
194 u64 cur, end;
195
196 cur = read_c0_cvmcount();
197 if (rdiv != 0) {
198 end = count * rdiv;
199 if (f != 0) {
200 asm("dmultu\t%[cnt],%[f]\n\t"
201 "mfhi\t%[cnt]"
202 : [cnt] "+r" (end)
203 : [f] "r" (f)
204 : "hi", "lo");
205 }
206 end = cur + end;
207 } else {
208 end = cur + count;
209 }
210 while (end > cur)
211 cur = read_c0_cvmcount();
212}
213EXPORT_SYMBOL(octeon_io_clk_delay);