Blame view

arch/mips/cavium-octeon/csrc-octeon.c 3.78 KB
5b3b16880   David Daney   MIPS: Add Cavium ...
1
2
3
4
5
6
  /*
   * This file is subject to the terms and conditions of the GNU General Public
   * License.  See the file "COPYING" in the main directory of this archive
   * for more details.
   *
   * Copyright (C) 2007 by Ralf Baechle
54954a6d6   David Daney   MIPS: Octeon: Sca...
7
   * Copyright (C) 2009, 2010 Cavium Networks, Inc.
5b3b16880   David Daney   MIPS: Add Cavium ...
8
9
   */
  #include <linux/clocksource.h>
cae39d138   Paul Gortmaker   mips: add export....
10
  #include <linux/export.h>
5b3b16880   David Daney   MIPS: Add Cavium ...
11
  #include <linux/init.h>
54954a6d6   David Daney   MIPS: Octeon: Sca...
12
  #include <linux/smp.h>
5b3b16880   David Daney   MIPS: Add Cavium ...
13

54954a6d6   David Daney   MIPS: Octeon: Sca...
14
  #include <asm/cpu-info.h>
5b3b16880   David Daney   MIPS: Add Cavium ...
15
16
17
18
  #include <asm/time.h>
  
  #include <asm/octeon/octeon.h>
  #include <asm/octeon/cvmx-ipd-defs.h>
54954a6d6   David Daney   MIPS: Octeon: Sca...
19
  #include <asm/octeon/cvmx-mio-defs.h>
5b3b16880   David Daney   MIPS: Add Cavium ...
20
21
22
23
24
25
26
  
  /*
   * Set the current core's cvmcount counter to the value of the
   * IPD_CLK_COUNT.  We do this on all cores as they are brought
   * on-line.  This allows for a read from a local cpu register to
   * access a synchronized counter.
   *
54954a6d6   David Daney   MIPS: Octeon: Sca...
27
   * On CPU_CAVIUM_OCTEON2 the IPD_CLK_COUNT is scaled by rdiv/sdiv.
5b3b16880   David Daney   MIPS: Add Cavium ...
28
29
30
31
32
   */
  void octeon_init_cvmcount(void)
  {
  	unsigned long flags;
  	unsigned loops = 2;
54954a6d6   David Daney   MIPS: Octeon: Sca...
33
34
35
36
37
38
39
40
41
42
  	u64 f = 0;
  	u64 rdiv = 0;
  	u64 sdiv = 0;
  	if (current_cpu_type() == CPU_CAVIUM_OCTEON2) {
  		union cvmx_mio_rst_boot rst_boot;
  		rst_boot.u64 = cvmx_read_csr(CVMX_MIO_RST_BOOT);
  		rdiv = rst_boot.s.c_mul;	/* CPU clock */
  		sdiv = rst_boot.s.pnr_mul;	/* I/O clock */
  		f = (0x8000000000000000ull / sdiv) * 2;
  	}
5b3b16880   David Daney   MIPS: Add Cavium ...
43
44
45
46
47
48
49
50
51
  
  	/* Clobber loops so GCC will not unroll the following while loop. */
  	asm("" : "+r" (loops));
  
  	local_irq_save(flags);
  	/*
  	 * Loop several times so we are executing from the cache,
  	 * which should give more deterministic timing.
  	 */
54954a6d6   David Daney   MIPS: Octeon: Sca...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  	while (loops--) {
  		u64 ipd_clk_count = cvmx_read_csr(CVMX_IPD_CLK_COUNT);
  		if (rdiv != 0) {
  			ipd_clk_count *= rdiv;
  			if (f != 0) {
  				asm("dmultu\t%[cnt],%[f]
  \t"
  				    "mfhi\t%[cnt]"
  				    : [cnt] "+r" (ipd_clk_count),
  				      [f] "=r" (f)
  				    : : "hi", "lo");
  			}
  		}
  		write_c0_cvmcount(ipd_clk_count);
  	}
5b3b16880   David Daney   MIPS: Add Cavium ...
67
68
  	local_irq_restore(flags);
  }
d0ce9a5a4   Coly Li   MIPS: Cavium: Add...
69
  static cycle_t octeon_cvmcount_read(struct clocksource *cs)
5b3b16880   David Daney   MIPS: Add Cavium ...
70
71
72
73
74
75
76
77
78
79
  {
  	return read_c0_cvmcount();
  }
  
  static struct clocksource clocksource_mips = {
  	.name		= "OCTEON_CVMCOUNT",
  	.read		= octeon_cvmcount_read,
  	.mask		= CLOCKSOURCE_MASK(64),
  	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
  };
c6a3c851a   David Daney   MIPS: Octeon: Add...
80
81
  unsigned long long notrace sched_clock(void)
  {
0e8a1d826   David Daney   MIPS: Octeon: Use...
82
  	/* 64-bit arithmatic can overflow, so use 128-bit.  */
0e8a1d826   David Daney   MIPS: Octeon: Use...
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  	u64 t1, t2, t3;
  	unsigned long long rv;
  	u64 mult = clocksource_mips.mult;
  	u64 shift = clocksource_mips.shift;
  	u64 cnt = read_c0_cvmcount();
  
  	asm (
  		"dmultu\t%[cnt],%[mult]
  \t"
  		"nor\t%[t1],$0,%[shift]
  \t"
  		"mfhi\t%[t2]
  \t"
  		"mflo\t%[t3]
  \t"
  		"dsll\t%[t2],%[t2],1
  \t"
  		"dsrlv\t%[rv],%[t3],%[shift]
  \t"
  		"dsllv\t%[t1],%[t2],%[t1]
  \t"
  		"or\t%[rv],%[t1],%[rv]
  \t"
  		: [rv] "=&r" (rv), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3)
  		: [cnt] "r" (cnt), [mult] "r" (mult), [shift] "r" (shift)
  		: "hi", "lo");
  	return rv;
c6a3c851a   David Daney   MIPS: Octeon: Add...
110
  }
5b3b16880   David Daney   MIPS: Add Cavium ...
111
112
113
  void __init plat_time_init(void)
  {
  	clocksource_mips.rating = 300;
75c4fd8c7   John Stultz   mips: convert to ...
114
  	clocksource_register_hz(&clocksource_mips, octeon_get_clock_rate());
5b3b16880   David Daney   MIPS: Add Cavium ...
115
  }
ca148125e   David Daney   MIPS: Octeon: Imp...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  
  static u64 octeon_udelay_factor;
  static u64 octeon_ndelay_factor;
  
  void __init octeon_setup_delays(void)
  {
  	octeon_udelay_factor = octeon_get_clock_rate() / 1000000;
  	/*
  	 * For __ndelay we divide by 2^16, so the factor is multiplied
  	 * by the same amount.
  	 */
  	octeon_ndelay_factor = (octeon_udelay_factor * 0x10000ull) / 1000ull;
  
  	preset_lpj = octeon_get_clock_rate() / HZ;
  }
  
  void __udelay(unsigned long us)
  {
  	u64 cur, end, inc;
  
  	cur = read_c0_cvmcount();
  
  	inc = us * octeon_udelay_factor;
  	end = cur + inc;
  
  	while (end > cur)
  		cur = read_c0_cvmcount();
  }
  EXPORT_SYMBOL(__udelay);
  
  void __ndelay(unsigned long ns)
  {
  	u64 cur, end, inc;
  
  	cur = read_c0_cvmcount();
  
  	inc = ((ns * octeon_ndelay_factor) >> 16);
  	end = cur + inc;
  
  	while (end > cur)
  		cur = read_c0_cvmcount();
  }
  EXPORT_SYMBOL(__ndelay);
  
  void __delay(unsigned long loops)
  {
  	u64 cur, end;
  
  	cur = read_c0_cvmcount();
  	end = cur + loops;
  
  	while (end > cur)
  		cur = read_c0_cvmcount();
  }
  EXPORT_SYMBOL(__delay);