blob: 02193953eb9e6ccb56529ee6880027ad92219a04 [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>
David Daney5b3b1682009-01-08 16:46:40 -080015#include <asm/time.h>
16
17#include <asm/octeon/octeon.h>
18#include <asm/octeon/cvmx-ipd-defs.h>
David Daney54954a62010-10-07 16:03:47 -070019#include <asm/octeon/cvmx-mio-defs.h>
David Daney5b3b1682009-01-08 16:46:40 -080020
David Daney70a26a22012-08-21 11:45:05 -070021
22static u64 f;
23static u64 rdiv;
24static u64 sdiv;
25static u64 octeon_udelay_factor;
26static u64 octeon_ndelay_factor;
27
28void __init octeon_setup_delays(void)
29{
30 octeon_udelay_factor = octeon_get_clock_rate() / 1000000;
31 /*
32 * For __ndelay we divide by 2^16, so the factor is multiplied
33 * by the same amount.
34 */
35 octeon_ndelay_factor = (octeon_udelay_factor * 0x10000ull) / 1000ull;
36
37 preset_lpj = octeon_get_clock_rate() / HZ;
38
39 if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {
40 union cvmx_mio_rst_boot rst_boot;
41 rst_boot.u64 = cvmx_read_csr(CVMX_MIO_RST_BOOT);
42 rdiv = rst_boot.s.c_mul; /* CPU clock */
43 sdiv = rst_boot.s.pnr_mul; /* I/O clock */
44 f = (0x8000000000000000ull / sdiv) * 2;
45 }
46}
47
David Daney5b3b1682009-01-08 16:46:40 -080048/*
49 * Set the current core's cvmcount counter to the value of the
50 * IPD_CLK_COUNT. We do this on all cores as they are brought
51 * on-line. This allows for a read from a local cpu register to
52 * access a synchronized counter.
53 *
David Daney54954a62010-10-07 16:03:47 -070054 * On CPU_CAVIUM_OCTEON2 the IPD_CLK_COUNT is scaled by rdiv/sdiv.
David Daney5b3b1682009-01-08 16:46:40 -080055 */
56void octeon_init_cvmcount(void)
57{
58 unsigned long flags;
59 unsigned loops = 2;
60
61 /* Clobber loops so GCC will not unroll the following while loop. */
62 asm("" : "+r" (loops));
63
64 local_irq_save(flags);
65 /*
66 * Loop several times so we are executing from the cache,
67 * which should give more deterministic timing.
68 */
David Daney54954a62010-10-07 16:03:47 -070069 while (loops--) {
70 u64 ipd_clk_count = cvmx_read_csr(CVMX_IPD_CLK_COUNT);
71 if (rdiv != 0) {
72 ipd_clk_count *= rdiv;
73 if (f != 0) {
74 asm("dmultu\t%[cnt],%[f]\n\t"
75 "mfhi\t%[cnt]"
David Daney70a26a22012-08-21 11:45:05 -070076 : [cnt] "+r" (ipd_clk_count)
77 : [f] "r" (f)
78 : "hi", "lo");
David Daney54954a62010-10-07 16:03:47 -070079 }
80 }
81 write_c0_cvmcount(ipd_clk_count);
82 }
David Daney5b3b1682009-01-08 16:46:40 -080083 local_irq_restore(flags);
84}
85
Coly Lid0ce9a52009-04-23 03:06:06 +080086static cycle_t octeon_cvmcount_read(struct clocksource *cs)
David Daney5b3b1682009-01-08 16:46:40 -080087{
88 return read_c0_cvmcount();
89}
90
91static struct clocksource clocksource_mips = {
92 .name = "OCTEON_CVMCOUNT",
93 .read = octeon_cvmcount_read,
94 .mask = CLOCKSOURCE_MASK(64),
95 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
96};
97
David Daneyc6a3c852009-12-23 13:18:54 -080098unsigned long long notrace sched_clock(void)
99{
David Daney0e8a1d82010-01-08 14:47:36 -0800100 /* 64-bit arithmatic can overflow, so use 128-bit. */
David Daney0e8a1d82010-01-08 14:47:36 -0800101 u64 t1, t2, t3;
102 unsigned long long rv;
103 u64 mult = clocksource_mips.mult;
104 u64 shift = clocksource_mips.shift;
105 u64 cnt = read_c0_cvmcount();
106
107 asm (
108 "dmultu\t%[cnt],%[mult]\n\t"
109 "nor\t%[t1],$0,%[shift]\n\t"
110 "mfhi\t%[t2]\n\t"
111 "mflo\t%[t3]\n\t"
112 "dsll\t%[t2],%[t2],1\n\t"
113 "dsrlv\t%[rv],%[t3],%[shift]\n\t"
114 "dsllv\t%[t1],%[t2],%[t1]\n\t"
115 "or\t%[rv],%[t1],%[rv]\n\t"
116 : [rv] "=&r" (rv), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3)
117 : [cnt] "r" (cnt), [mult] "r" (mult), [shift] "r" (shift)
118 : "hi", "lo");
119 return rv;
David Daneyc6a3c852009-12-23 13:18:54 -0800120}
121
David Daney5b3b1682009-01-08 16:46:40 -0800122void __init plat_time_init(void)
123{
124 clocksource_mips.rating = 300;
John Stultz75c4fd82010-04-26 20:23:11 -0700125 clocksource_register_hz(&clocksource_mips, octeon_get_clock_rate());
David Daney5b3b1682009-01-08 16:46:40 -0800126}
David Daneyca148122010-06-01 13:18:15 -0700127
David Daneyca148122010-06-01 13:18:15 -0700128void __udelay(unsigned long us)
129{
130 u64 cur, end, inc;
131
132 cur = read_c0_cvmcount();
133
134 inc = us * octeon_udelay_factor;
135 end = cur + inc;
136
137 while (end > cur)
138 cur = read_c0_cvmcount();
139}
140EXPORT_SYMBOL(__udelay);
141
142void __ndelay(unsigned long ns)
143{
144 u64 cur, end, inc;
145
146 cur = read_c0_cvmcount();
147
148 inc = ((ns * octeon_ndelay_factor) >> 16);
149 end = cur + inc;
150
151 while (end > cur)
152 cur = read_c0_cvmcount();
153}
154EXPORT_SYMBOL(__ndelay);
155
156void __delay(unsigned long loops)
157{
158 u64 cur, end;
159
160 cur = read_c0_cvmcount();
161 end = cur + loops;
162
163 while (end > cur)
164 cur = read_c0_cvmcount();
165}
166EXPORT_SYMBOL(__delay);
David Daney70a26a22012-08-21 11:45:05 -0700167
168
169/**
170 * octeon_io_clk_delay - wait for a given number of io clock cycles to pass.
171 *
172 * We scale the wait by the clock ratio, and then wait for the
173 * corresponding number of core clocks.
174 *
175 * @count: The number of clocks to wait.
176 */
177void octeon_io_clk_delay(unsigned long count)
178{
179 u64 cur, end;
180
181 cur = read_c0_cvmcount();
182 if (rdiv != 0) {
183 end = count * rdiv;
184 if (f != 0) {
185 asm("dmultu\t%[cnt],%[f]\n\t"
186 "mfhi\t%[cnt]"
187 : [cnt] "+r" (end)
188 : [f] "r" (f)
189 : "hi", "lo");
190 }
191 end = cur + end;
192 } else {
193 end = cur + count;
194 }
195 while (end > cur)
196 cur = read_c0_cvmcount();
197}
198EXPORT_SYMBOL(octeon_io_clk_delay);