blob: 1882e6475dd093d546c70cf9f7345f8121589921 [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 Daney70a26a22012-08-21 11:45:05 -070022
23static u64 f;
24static u64 rdiv;
25static u64 sdiv;
26static u64 octeon_udelay_factor;
27static u64 octeon_ndelay_factor;
28
29void __init octeon_setup_delays(void)
30{
31 octeon_udelay_factor = octeon_get_clock_rate() / 1000000;
32 /*
33 * For __ndelay we divide by 2^16, so the factor is multiplied
34 * by the same amount.
35 */
36 octeon_ndelay_factor = (octeon_udelay_factor * 0x10000ull) / 1000ull;
37
38 preset_lpj = octeon_get_clock_rate() / HZ;
39
40 if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {
41 union cvmx_mio_rst_boot rst_boot;
Chandrakala Chavvaac6d9b32015-01-15 16:11:17 +030042
David Daney70a26a22012-08-21 11:45:05 -070043 rst_boot.u64 = cvmx_read_csr(CVMX_MIO_RST_BOOT);
44 rdiv = rst_boot.s.c_mul; /* CPU clock */
45 sdiv = rst_boot.s.pnr_mul; /* I/O clock */
46 f = (0x8000000000000000ull / sdiv) * 2;
Chandrakala Chavvaac6d9b32015-01-15 16:11:17 +030047 } else if (current_cpu_type() == CPU_CAVIUM_OCTEON3) {
48 union cvmx_rst_boot rst_boot;
49
50 rst_boot.u64 = cvmx_read_csr(CVMX_RST_BOOT);
51 rdiv = rst_boot.s.c_mul; /* CPU clock */
52 sdiv = rst_boot.s.pnr_mul; /* I/O clock */
53 f = (0x8000000000000000ull / sdiv) * 2;
David Daney70a26a22012-08-21 11:45:05 -070054 }
Chandrakala Chavvaac6d9b32015-01-15 16:11:17 +030055
David Daney70a26a22012-08-21 11:45:05 -070056}
57
David Daney5b3b1682009-01-08 16:46:40 -080058/*
59 * Set the current core's cvmcount counter to the value of the
60 * IPD_CLK_COUNT. We do this on all cores as they are brought
61 * on-line. This allows for a read from a local cpu register to
62 * access a synchronized counter.
63 *
David Daney54954a62010-10-07 16:03:47 -070064 * On CPU_CAVIUM_OCTEON2 the IPD_CLK_COUNT is scaled by rdiv/sdiv.
David Daney5b3b1682009-01-08 16:46:40 -080065 */
66void octeon_init_cvmcount(void)
67{
68 unsigned long flags;
69 unsigned loops = 2;
70
71 /* Clobber loops so GCC will not unroll the following while loop. */
72 asm("" : "+r" (loops));
73
74 local_irq_save(flags);
75 /*
76 * Loop several times so we are executing from the cache,
77 * which should give more deterministic timing.
78 */
David Daney54954a62010-10-07 16:03:47 -070079 while (loops--) {
80 u64 ipd_clk_count = cvmx_read_csr(CVMX_IPD_CLK_COUNT);
81 if (rdiv != 0) {
82 ipd_clk_count *= rdiv;
83 if (f != 0) {
84 asm("dmultu\t%[cnt],%[f]\n\t"
85 "mfhi\t%[cnt]"
David Daney70a26a22012-08-21 11:45:05 -070086 : [cnt] "+r" (ipd_clk_count)
87 : [f] "r" (f)
88 : "hi", "lo");
David Daney54954a62010-10-07 16:03:47 -070089 }
90 }
91 write_c0_cvmcount(ipd_clk_count);
92 }
David Daney5b3b1682009-01-08 16:46:40 -080093 local_irq_restore(flags);
94}
95
Coly Lid0ce9a52009-04-23 03:06:06 +080096static cycle_t octeon_cvmcount_read(struct clocksource *cs)
David Daney5b3b1682009-01-08 16:46:40 -080097{
98 return read_c0_cvmcount();
99}
100
101static struct clocksource clocksource_mips = {
102 .name = "OCTEON_CVMCOUNT",
103 .read = octeon_cvmcount_read,
104 .mask = CLOCKSOURCE_MASK(64),
105 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
106};
107
David Daneyc6a3c852009-12-23 13:18:54 -0800108unsigned long long notrace sched_clock(void)
109{
David Daney0e8a1d82010-01-08 14:47:36 -0800110 /* 64-bit arithmatic can overflow, so use 128-bit. */
David Daney0e8a1d82010-01-08 14:47:36 -0800111 u64 t1, t2, t3;
112 unsigned long long rv;
113 u64 mult = clocksource_mips.mult;
114 u64 shift = clocksource_mips.shift;
115 u64 cnt = read_c0_cvmcount();
116
117 asm (
118 "dmultu\t%[cnt],%[mult]\n\t"
119 "nor\t%[t1],$0,%[shift]\n\t"
120 "mfhi\t%[t2]\n\t"
121 "mflo\t%[t3]\n\t"
122 "dsll\t%[t2],%[t2],1\n\t"
123 "dsrlv\t%[rv],%[t3],%[shift]\n\t"
124 "dsllv\t%[t1],%[t2],%[t1]\n\t"
125 "or\t%[rv],%[t1],%[rv]\n\t"
126 : [rv] "=&r" (rv), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3)
127 : [cnt] "r" (cnt), [mult] "r" (mult), [shift] "r" (shift)
128 : "hi", "lo");
129 return rv;
David Daneyc6a3c852009-12-23 13:18:54 -0800130}
131
David Daney5b3b1682009-01-08 16:46:40 -0800132void __init plat_time_init(void)
133{
134 clocksource_mips.rating = 300;
John Stultz75c4fd82010-04-26 20:23:11 -0700135 clocksource_register_hz(&clocksource_mips, octeon_get_clock_rate());
David Daney5b3b1682009-01-08 16:46:40 -0800136}
David Daneyca148122010-06-01 13:18:15 -0700137
David Daneyca148122010-06-01 13:18:15 -0700138void __udelay(unsigned long us)
139{
140 u64 cur, end, inc;
141
142 cur = read_c0_cvmcount();
143
144 inc = us * octeon_udelay_factor;
145 end = cur + inc;
146
147 while (end > cur)
148 cur = read_c0_cvmcount();
149}
150EXPORT_SYMBOL(__udelay);
151
152void __ndelay(unsigned long ns)
153{
154 u64 cur, end, inc;
155
156 cur = read_c0_cvmcount();
157
158 inc = ((ns * octeon_ndelay_factor) >> 16);
159 end = cur + inc;
160
161 while (end > cur)
162 cur = read_c0_cvmcount();
163}
164EXPORT_SYMBOL(__ndelay);
165
166void __delay(unsigned long loops)
167{
168 u64 cur, end;
169
170 cur = read_c0_cvmcount();
171 end = cur + loops;
172
173 while (end > cur)
174 cur = read_c0_cvmcount();
175}
176EXPORT_SYMBOL(__delay);
David Daney70a26a22012-08-21 11:45:05 -0700177
178
179/**
180 * octeon_io_clk_delay - wait for a given number of io clock cycles to pass.
181 *
182 * We scale the wait by the clock ratio, and then wait for the
183 * corresponding number of core clocks.
184 *
185 * @count: The number of clocks to wait.
186 */
187void octeon_io_clk_delay(unsigned long count)
188{
189 u64 cur, end;
190
191 cur = read_c0_cvmcount();
192 if (rdiv != 0) {
193 end = count * rdiv;
194 if (f != 0) {
195 asm("dmultu\t%[cnt],%[f]\n\t"
196 "mfhi\t%[cnt]"
197 : [cnt] "+r" (end)
198 : [f] "r" (f)
199 : "hi", "lo");
200 }
201 end = cur + end;
202 } else {
203 end = cur + count;
204 }
205 while (end > cur)
206 cur = read_c0_cvmcount();
207}
208EXPORT_SYMBOL(octeon_io_clk_delay);