blob: 787483b2f86b6516c479c67f58c71c3e8d8d8c85 [file] [log] [blame]
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/io.h>
16#include <linux/delay.h>
17#include <linux/mutex.h>
18#include <linux/spinlock.h>
19#include <linux/errno.h>
20#include <linux/cpufreq.h>
21#include <linux/cpu.h>
22#include <linux/regulator/consumer.h>
23
24#include <asm/cpu.h>
25
26#include <mach/board.h>
27#include <mach/msm_iomap.h>
28#include <mach/msm_bus.h>
29#include <mach/msm_bus_board.h>
30#include <mach/socinfo.h>
31#include <mach/rpm-regulator.h>
32
33#include "acpuclock.h"
34#include "avs.h"
35
36/* Frequency switch modes. */
37#define SHOT_SWITCH 4
38#define HOP_SWITCH 5
39#define SIMPLE_SLEW 6
40#define COMPLEX_SLEW 7
41
42/* PLL calibration limits.
Matt Wagantall2ecbec22012-03-13 23:18:07 -070043 * The PLL hardware has a minimum frequency of 384MHz.
44 * Calibration should respect this limit. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045#define L_VAL_SCPLL_CAL_MIN 0x08 /* = 432 MHz with 27MHz source */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046
Matt Wagantall2ecbec22012-03-13 23:18:07 -070047#define MAX_VDD_SC 1325000 /* uV */
48#define MAX_VDD_MEM 1325000 /* uV */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049#define MAX_VDD_DIG 1200000 /* uV */
50#define MAX_AXI 310500 /* KHz */
51#define SCPLL_LOW_VDD_FMAX 594000 /* KHz */
52#define SCPLL_LOW_VDD 1000000 /* uV */
53#define SCPLL_NOMINAL_VDD 1100000 /* uV */
54
55/* SCPLL Modes. */
56#define SCPLL_POWER_DOWN 0
57#define SCPLL_BYPASS 1
58#define SCPLL_STANDBY 2
59#define SCPLL_FULL_CAL 4
60#define SCPLL_HALF_CAL 5
61#define SCPLL_STEP_CAL 6
62#define SCPLL_NORMAL 7
63
64#define SCPLL_DEBUG_NONE 0
65#define SCPLL_DEBUG_FULL 3
66
67/* SCPLL registers offsets. */
68#define SCPLL_DEBUG_OFFSET 0x0
69#define SCPLL_CTL_OFFSET 0x4
70#define SCPLL_CAL_OFFSET 0x8
71#define SCPLL_STATUS_OFFSET 0x10
72#define SCPLL_CFG_OFFSET 0x1C
73#define SCPLL_FSM_CTL_EXT_OFFSET 0x24
Matt Wagantall2ecbec22012-03-13 23:18:07 -070074#define SCPLL_LUT_OFFSET(l_val) (0x38 + (((l_val) / 4) * 4))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075
76/* Clock registers. */
77#define SPSS0_CLK_CTL_ADDR (MSM_ACC0_BASE + 0x04)
78#define SPSS0_CLK_SEL_ADDR (MSM_ACC0_BASE + 0x08)
79#define SPSS1_CLK_CTL_ADDR (MSM_ACC1_BASE + 0x04)
80#define SPSS1_CLK_SEL_ADDR (MSM_ACC1_BASE + 0x08)
81#define SPSS_L2_CLK_SEL_ADDR (MSM_GCC_BASE + 0x38)
82
83/* PTE EFUSE register. */
84#define QFPROM_PTE_EFUSE_ADDR (MSM_QFPROM_BASE + 0x00C0)
85
86static const void * const clk_ctl_addr[] = {SPSS0_CLK_CTL_ADDR,
87 SPSS1_CLK_CTL_ADDR};
88static const void * const clk_sel_addr[] = {SPSS0_CLK_SEL_ADDR,
89 SPSS1_CLK_SEL_ADDR, SPSS_L2_CLK_SEL_ADDR};
90
91static const int rpm_vreg_voter[] = { RPM_VREG_VOTER1, RPM_VREG_VOTER2 };
92static struct regulator *regulator_sc[NR_CPUS];
93
94enum scplls {
95 CPU0 = 0,
96 CPU1,
97 L2,
98};
99
100static const void * const sc_pll_base[] = {
101 [CPU0] = MSM_SCPLL_BASE + 0x200,
102 [CPU1] = MSM_SCPLL_BASE + 0x300,
103 [L2] = MSM_SCPLL_BASE + 0x400,
104};
105
106enum sc_src {
107 ACPU_AFAB,
108 ACPU_PLL_8,
109 ACPU_SCPLL,
110};
111
112static struct clock_state {
113 struct clkctl_acpu_speed *current_speed[NR_CPUS];
114 struct clkctl_l2_speed *current_l2_speed;
115 spinlock_t l2_lock;
116 struct mutex lock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117} drv_state;
118
119struct clkctl_l2_speed {
120 unsigned int khz;
121 unsigned int src_sel;
122 unsigned int l_val;
123 unsigned int vdd_dig;
124 unsigned int vdd_mem;
125 unsigned int bw_level;
126};
127
128static struct clkctl_l2_speed *l2_vote[NR_CPUS];
129
130struct clkctl_acpu_speed {
131 unsigned int use_for_scaling[2]; /* One for each CPU. */
132 unsigned int acpuclk_khz;
133 int pll;
134 unsigned int acpuclk_src_sel;
135 unsigned int acpuclk_src_div;
136 unsigned int core_src_sel;
137 unsigned int l_val;
138 struct clkctl_l2_speed *l2_level;
139 unsigned int vdd_sc;
140 unsigned int avsdscr_setting;
141};
142
143/* Instantaneous bandwidth requests in MB/s. */
144#define BW_MBPS(_bw) \
145 { \
146 .vectors = &(struct msm_bus_vectors){ \
147 .src = MSM_BUS_MASTER_AMPSS_M0, \
148 .dst = MSM_BUS_SLAVE_EBI_CH0, \
149 .ib = (_bw) * 1000000UL, \
150 .ab = 0, \
151 }, \
152 .num_paths = 1, \
153 }
154static struct msm_bus_paths bw_level_tbl[] = {
155 [0] = BW_MBPS(824), /* At least 103 MHz on bus. */
156 [1] = BW_MBPS(1336), /* At least 167 MHz on bus. */
157 [2] = BW_MBPS(2008), /* At least 251 MHz on bus. */
158 [3] = BW_MBPS(2480), /* At least 310 MHz on bus. */
159};
160
161static struct msm_bus_scale_pdata bus_client_pdata = {
162 .usecase = bw_level_tbl,
163 .num_usecases = ARRAY_SIZE(bw_level_tbl),
164 .active_only = 1,
165 .name = "acpuclock",
166};
167
168static uint32_t bus_perf_client;
169
170/* L2 frequencies = 2 * 27 MHz * L_VAL */
171static struct clkctl_l2_speed l2_freq_tbl_v2[] = {
172 [0] = { MAX_AXI, 0, 0, 1000000, 1100000, 0},
173 [1] = { 432000, 1, 0x08, 1000000, 1100000, 0},
174 [2] = { 486000, 1, 0x09, 1000000, 1100000, 0},
175 [3] = { 540000, 1, 0x0A, 1000000, 1100000, 0},
176 [4] = { 594000, 1, 0x0B, 1000000, 1100000, 0},
177 [5] = { 648000, 1, 0x0C, 1000000, 1100000, 1},
178 [6] = { 702000, 1, 0x0D, 1100000, 1100000, 1},
179 [7] = { 756000, 1, 0x0E, 1100000, 1100000, 1},
180 [8] = { 810000, 1, 0x0F, 1100000, 1100000, 1},
181 [9] = { 864000, 1, 0x10, 1100000, 1100000, 1},
182 [10] = { 918000, 1, 0x11, 1100000, 1100000, 2},
183 [11] = { 972000, 1, 0x12, 1100000, 1100000, 2},
184 [12] = {1026000, 1, 0x13, 1100000, 1100000, 2},
185 [13] = {1080000, 1, 0x14, 1100000, 1200000, 2},
186 [14] = {1134000, 1, 0x15, 1100000, 1200000, 2},
187 [15] = {1188000, 1, 0x16, 1200000, 1200000, 3},
188 [16] = {1242000, 1, 0x17, 1200000, 1212500, 3},
189 [17] = {1296000, 1, 0x18, 1200000, 1225000, 3},
190 [18] = {1350000, 1, 0x19, 1200000, 1225000, 3},
191 [19] = {1404000, 1, 0x1A, 1200000, 1250000, 3},
192};
193
194#define L2(x) (&l2_freq_tbl_v2[(x)])
195/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
196static struct clkctl_acpu_speed acpu_freq_tbl_1188mhz[] = {
197 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 812500, 0x03006000},
198 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
199 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 875000, 0x03006000},
200 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 875000, 0x03006000},
201 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 887500, 0x03006000},
202 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 912500, 0x03006000},
203 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 925000, 0x03006000},
204 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 937500, 0x03006000},
205 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 950000, 0x03006000},
206 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 975000, 0x03006000},
207 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 1000000, 0x03006000},
208 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 1012500, 0x03006000},
209 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 1037500, 0x03006000},
210 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 1062500, 0x03006000},
211 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 1087500, 0x03006000},
212 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 1125000, 0x03006000},
213 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1137500, 0x03006000},
214 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1162500, 0x03006000},
215 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1187500, 0x03006000},
216 { {0, 0}, 0 },
217};
218
219/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700220static struct clkctl_acpu_speed acpu_freq_tbl_slowest[] = {
221 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 800000, 0x03006000},
222 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
223 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 825000, 0x03006000},
224 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 825000, 0x03006000},
225 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 850000, 0x03006000},
226 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 850000, 0x03006000},
227 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 875000, 0x03006000},
228 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 875000, 0x03006000},
229 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 900000, 0x03006000},
230 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 900000, 0x03006000},
231 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 925000, 0x03006000},
232 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 975000, 0x03006000},
233 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 975000, 0x03006000},
234 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 1000000, 0x03006000},
235 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 1025000, 0x03006000},
236 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 1025000, 0x03006000},
237 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1050000, 0x03006000},
238 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1075000, 0x03006000},
239 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1100000, 0x03006000},
240 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1125000, 0x03006000},
241 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1150000, 0x03006000},
242 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1175000, 0x03006000},
243 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1200000, 0x03006000},
244 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1225000, 0x03006000},
245 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1250000, 0x03006000},
246 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1275000, 0x03006000},
247 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1300000, 0x03006000},
248 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1325000, 0x03006000},
249 { {0, 0}, 0 },
250};
251
252/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
253static struct clkctl_acpu_speed acpu_freq_tbl_slower[] = {
254 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 800000, 0x03006000},
255 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
256 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 825000, 0x03006000},
257 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 825000, 0x03006000},
258 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 850000, 0x03006000},
259 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 850000, 0x03006000},
260 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 875000, 0x03006000},
261 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 875000, 0x03006000},
262 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 900000, 0x03006000},
263 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 900000, 0x03006000},
264 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 925000, 0x03006000},
265 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 975000, 0x03006000},
266 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 975000, 0x03006000},
267 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 1000000, 0x03006000},
268 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 1025000, 0x03006000},
269 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 1025000, 0x03006000},
270 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1050000, 0x03006000},
271 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1075000, 0x03006000},
272 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1100000, 0x03006000},
273 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1125000, 0x03006000},
274 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1150000, 0x03006000},
275 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1150000, 0x03006000},
276 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1175000, 0x03006000},
277 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1200000, 0x03006000},
278 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1225000, 0x03006000},
279 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1250000, 0x03006000},
280 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1275000, 0x03006000},
281 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1300000, 0x03006000},
282 { {0, 0}, 0 },
283};
284
285/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286static struct clkctl_acpu_speed acpu_freq_tbl_slow[] = {
Tianyi Gou66351ff2011-07-19 20:48:41 -0700287 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 800000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
Tianyi Gou66351ff2011-07-19 20:48:41 -0700289 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 825000, 0x03006000},
290 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 825000, 0x03006000},
291 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 850000, 0x03006000},
292 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 850000, 0x03006000},
293 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 875000, 0x03006000},
294 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 875000, 0x03006000},
295 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 900000, 0x03006000},
296 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 900000, 0x03006000},
297 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 925000, 0x03006000},
298 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 975000, 0x03006000},
299 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 975000, 0x03006000},
300 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 1000000, 0x03006000},
301 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 1025000, 0x03006000},
302 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 1025000, 0x03006000},
303 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1050000, 0x03006000},
304 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1075000, 0x03006000},
305 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1100000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1125000, 0x03006000},
307 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1150000, 0x03006000},
Tianyi Gou66351ff2011-07-19 20:48:41 -0700308 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1150000, 0x03006000},
309 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1175000, 0x03006000},
310 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1200000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700311 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1225000, 0x03006000},
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700312 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1225000, 0x03006000},
313 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1225000, 0x03006000},
314 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1250000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700315 { {0, 0}, 0 },
316};
317
318/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
Tianyi Gou66351ff2011-07-19 20:48:41 -0700319static struct clkctl_acpu_speed acpu_freq_tbl_nom[] = {
320 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 800000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
Tianyi Gou66351ff2011-07-19 20:48:41 -0700322 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 825000, 0x03006000},
323 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 825000, 0x03006000},
324 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 850000, 0x03006000},
325 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 850000, 0x03006000},
326 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 875000, 0x03006000},
327 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 875000, 0x03006000},
328 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 900000, 0x03006000},
329 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 900000, 0x03006000},
330 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 925000, 0x03006000},
331 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 950000, 0x03006000},
332 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 975000, 0x03006000},
333 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 975000, 0x03006000},
334 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 1000000, 0x03006000},
335 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 1000000, 0x03006000},
336 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1025000, 0x03006000},
337 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1025000, 0x03006000},
338 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1050000, 0x03006000},
339 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1075000, 0x03006000},
340 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1100000, 0x03006000},
341 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1125000, 0x03006000},
342 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1150000, 0x03006000},
343 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1150000, 0x03006000},
344 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1175000, 0x03006000},
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700345 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1175000, 0x03006000},
346 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1200000, 0x03006000},
347 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1200000, 0x03006000},
Tianyi Gou66351ff2011-07-19 20:48:41 -0700348 { {0, 0}, 0 },
349};
350
351/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
352static struct clkctl_acpu_speed acpu_freq_tbl_fast[] = {
353 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 800000, 0x03006000},
354 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
355 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 825000, 0x03006000},
356 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 825000, 0x03006000},
357 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 850000, 0x03006000},
358 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 850000, 0x03006000},
359 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 875000, 0x03006000},
360 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 875000, 0x03006000},
361 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 900000, 0x03006000},
362 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 900000, 0x03006000},
363 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 925000, 0x03006000},
364 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 925000, 0x03006000},
365 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 950000, 0x03006000},
366 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 950000, 0x03006000},
367 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 950000, 0x03006000},
368 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 975000, 0x03006000},
369 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1000000, 0x03006000},
370 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1000000, 0x03006000},
371 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1025000, 0x03006000},
372 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1050000, 0x03006000},
373 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1075000, 0x03006000},
374 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1100000, 0x03006000},
375 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1100000, 0x03006000},
376 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1100000, 0x03006000},
377 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1125000, 0x03006000},
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700378 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1125000, 0x03006000},
379 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1125000, 0x03006000},
380 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1150000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 { {0, 0}, 0 },
382};
383
384
385/* acpu_freq_tbl row to use when reconfiguring SC/L2 PLLs. */
386#define CAL_IDX 1
387
388static struct clkctl_acpu_speed *acpu_freq_tbl;
389static struct clkctl_l2_speed *l2_freq_tbl = l2_freq_tbl_v2;
390static unsigned int l2_freq_tbl_size = ARRAY_SIZE(l2_freq_tbl_v2);
391
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700392static unsigned long acpuclk_8x60_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393{
394 return drv_state.current_speed[cpu]->acpuclk_khz;
395}
396
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700397static void select_core_source(unsigned int id, unsigned int src)
398{
399 uint32_t regval;
400 int shift;
401
402 shift = (id == L2) ? 0 : 1;
403 regval = readl_relaxed(clk_sel_addr[id]);
404 regval &= ~(0x3 << shift);
405 regval |= (src << shift);
406 writel_relaxed(regval, clk_sel_addr[id]);
407}
408
409static void select_clk_source_div(unsigned int id, struct clkctl_acpu_speed *s)
410{
411 uint32_t reg_clksel, reg_clkctl, src_sel;
412
413 /* Configure the PLL divider mux if we plan to use it. */
414 if (s->core_src_sel == 0) {
415
416 reg_clksel = readl_relaxed(clk_sel_addr[id]);
417
418 /* CLK_SEL_SRC1N0 (bank) bit. */
419 src_sel = reg_clksel & 1;
420
421 /* Program clock source and divider. */
422 reg_clkctl = readl_relaxed(clk_ctl_addr[id]);
423 reg_clkctl &= ~(0xFF << (8 * src_sel));
424 reg_clkctl |= s->acpuclk_src_sel << (4 + 8 * src_sel);
425 reg_clkctl |= s->acpuclk_src_div << (0 + 8 * src_sel);
426 writel_relaxed(reg_clkctl, clk_ctl_addr[id]);
427
428 /* Toggle clock source. */
429 reg_clksel ^= 1;
430
431 /* Program clock source selection. */
432 writel_relaxed(reg_clksel, clk_sel_addr[id]);
433 }
434}
435
436static void scpll_enable(int sc_pll, uint32_t l_val)
437{
438 uint32_t regval;
439
440 /* Power-up SCPLL into standby mode. */
441 writel_relaxed(SCPLL_STANDBY, sc_pll_base[sc_pll] + SCPLL_CTL_OFFSET);
442 mb();
443 udelay(10);
444
445 /* Shot-switch to target frequency. */
446 regval = (l_val << 3) | SHOT_SWITCH;
447 writel_relaxed(regval, sc_pll_base[sc_pll] + SCPLL_FSM_CTL_EXT_OFFSET);
448 writel_relaxed(SCPLL_NORMAL, sc_pll_base[sc_pll] + SCPLL_CTL_OFFSET);
449 mb();
450 udelay(20);
451}
452
453static void scpll_disable(int sc_pll)
454{
455 /* Power down SCPLL. */
456 writel_relaxed(SCPLL_POWER_DOWN,
457 sc_pll_base[sc_pll] + SCPLL_CTL_OFFSET);
458}
459
460static void scpll_change_freq(int sc_pll, uint32_t l_val)
461{
462 uint32_t regval;
463 const void *base_addr = sc_pll_base[sc_pll];
464
465 /* Complex-slew switch to target frequency. */
466 regval = (l_val << 3) | COMPLEX_SLEW;
467 writel_relaxed(regval, base_addr + SCPLL_FSM_CTL_EXT_OFFSET);
468 writel_relaxed(SCPLL_NORMAL, base_addr + SCPLL_CTL_OFFSET);
469
470 /* Wait for frequency switch to start. */
471 while (((readl_relaxed(base_addr + SCPLL_CTL_OFFSET) >> 3) & 0x3F)
472 != l_val)
473 cpu_relax();
474 /* Wait for frequency switch to finish. */
475 while (readl_relaxed(base_addr + SCPLL_STATUS_OFFSET) & 0x1)
476 cpu_relax();
477}
478
479/* Vote for the L2 speed and return the speed that should be applied. */
480static struct clkctl_l2_speed *compute_l2_speed(unsigned int voting_cpu,
481 struct clkctl_l2_speed *tgt_s)
482{
483 struct clkctl_l2_speed *new_s;
484 int cpu;
485
486 /* Bounds check. */
487 BUG_ON(tgt_s >= (l2_freq_tbl + l2_freq_tbl_size));
488
489 /* Find max L2 speed vote. */
490 l2_vote[voting_cpu] = tgt_s;
491 new_s = l2_freq_tbl;
492 for_each_present_cpu(cpu)
493 new_s = max(new_s, l2_vote[cpu]);
494
495 return new_s;
496}
497
498/* Set the L2's clock speed. */
499static void set_l2_speed(struct clkctl_l2_speed *tgt_s)
500{
501 if (tgt_s == drv_state.current_l2_speed)
502 return;
503
504 if (drv_state.current_l2_speed->src_sel == 1
505 && tgt_s->src_sel == 1)
506 scpll_change_freq(L2, tgt_s->l_val);
507 else {
508 if (tgt_s->src_sel == 1) {
509 scpll_enable(L2, tgt_s->l_val);
510 mb();
511 select_core_source(L2, tgt_s->src_sel);
512 } else {
513 select_core_source(L2, tgt_s->src_sel);
514 mb();
515 scpll_disable(L2);
516 }
517 }
518 drv_state.current_l2_speed = tgt_s;
519}
520
521/* Update the bus bandwidth request. */
522static void set_bus_bw(unsigned int bw)
523{
524 int ret;
525
526 /* Bounds check. */
527 if (bw >= ARRAY_SIZE(bw_level_tbl)) {
528 pr_err("%s: invalid bandwidth request (%d)\n", __func__, bw);
529 return;
530 }
531
532 /* Update bandwidth if requst has changed. This may sleep. */
533 ret = msm_bus_scale_client_update_request(bus_perf_client, bw);
534 if (ret)
535 pr_err("%s: bandwidth request failed (%d)\n", __func__, ret);
536
537 return;
538}
539
540/* Apply any per-cpu voltage increases. */
541static int increase_vdd(int cpu, unsigned int vdd_sc, unsigned int vdd_mem,
542 unsigned int vdd_dig, enum setrate_reason reason)
543{
544 int rc = 0;
545
546 /* Increase vdd_mem active-set before vdd_dig and vdd_sc.
547 * vdd_mem should be >= both vdd_sc and vdd_dig. */
548 rc = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S0, rpm_vreg_voter[cpu],
549 vdd_mem, MAX_VDD_MEM, 0);
550 if (rc) {
551 pr_err("%s: vdd_mem (cpu%d) increase failed (%d)\n",
552 __func__, cpu, rc);
553 return rc;
554 }
555
556 /* Increase vdd_dig active-set vote. */
557 rc = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S1, rpm_vreg_voter[cpu],
558 vdd_dig, MAX_VDD_DIG, 0);
559 if (rc) {
560 pr_err("%s: vdd_dig (cpu%d) increase failed (%d)\n",
561 __func__, cpu, rc);
562 return rc;
563 }
564
565 /* Don't update the Scorpion voltage in the hotplug path. It should
566 * already be correct. Attempting to set it is bad because we don't
567 * know what CPU we are running on at this point, but the Scorpion
568 * regulator API requires we call it from the affected CPU. */
569 if (reason == SETRATE_HOTPLUG)
570 return rc;
571
572 /* Update per-core Scorpion voltage. */
573 rc = regulator_set_voltage(regulator_sc[cpu], vdd_sc, MAX_VDD_SC);
574 if (rc) {
575 pr_err("%s: vdd_sc (cpu%d) increase failed (%d)\n",
576 __func__, cpu, rc);
577 return rc;
578 }
579
580 return rc;
581}
582
583/* Apply any per-cpu voltage decreases. */
584static void decrease_vdd(int cpu, unsigned int vdd_sc, unsigned int vdd_mem,
585 unsigned int vdd_dig, enum setrate_reason reason)
586{
587 int ret;
588
589 /* Update per-core Scorpion voltage. This must be called on the CPU
590 * that's being affected. Don't do this in the hotplug remove path,
591 * where the rail is off and we're executing on the other CPU. */
592 if (reason != SETRATE_HOTPLUG) {
593 ret = regulator_set_voltage(regulator_sc[cpu], vdd_sc,
594 MAX_VDD_SC);
595 if (ret) {
596 pr_err("%s: vdd_sc (cpu%d) decrease failed (%d)\n",
597 __func__, cpu, ret);
598 return;
599 }
600 }
601
602 /* Decrease vdd_dig active-set vote. */
603 ret = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S1, rpm_vreg_voter[cpu],
604 vdd_dig, MAX_VDD_DIG, 0);
605 if (ret) {
606 pr_err("%s: vdd_dig (cpu%d) decrease failed (%d)\n",
607 __func__, cpu, ret);
608 return;
609 }
610
611 /* Decrease vdd_mem active-set after vdd_dig and vdd_sc.
612 * vdd_mem should be >= both vdd_sc and vdd_dig. */
613 ret = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S0, rpm_vreg_voter[cpu],
614 vdd_mem, MAX_VDD_MEM, 0);
615 if (ret) {
616 pr_err("%s: vdd_mem (cpu%d) decrease failed (%d)\n",
617 __func__, cpu, ret);
618 return;
619 }
620}
621
622static void switch_sc_speed(int cpu, struct clkctl_acpu_speed *tgt_s)
623{
624 struct clkctl_acpu_speed *strt_s = drv_state.current_speed[cpu];
625
626 if (strt_s->pll != ACPU_SCPLL && tgt_s->pll != ACPU_SCPLL) {
627 select_clk_source_div(cpu, tgt_s);
628 /* Select core source because target may be AFAB. */
629 select_core_source(cpu, tgt_s->core_src_sel);
630 } else if (strt_s->pll != ACPU_SCPLL && tgt_s->pll == ACPU_SCPLL) {
631 scpll_enable(cpu, tgt_s->l_val);
632 mb();
633 select_core_source(cpu, tgt_s->core_src_sel);
634 } else if (strt_s->pll == ACPU_SCPLL && tgt_s->pll != ACPU_SCPLL) {
635 select_clk_source_div(cpu, tgt_s);
636 select_core_source(cpu, tgt_s->core_src_sel);
637 /* Core source switch must complete before disabling SCPLL. */
638 mb();
639 udelay(1);
640 scpll_disable(cpu);
641 } else
642 scpll_change_freq(cpu, tgt_s->l_val);
643
644 /* Update the driver state with the new clock freq */
645 drv_state.current_speed[cpu] = tgt_s;
646}
647
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700648static int acpuclk_8x60_set_rate(int cpu, unsigned long rate,
649 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650{
651 struct clkctl_acpu_speed *tgt_s, *strt_s;
652 struct clkctl_l2_speed *tgt_l2;
653 unsigned int vdd_mem, vdd_dig, pll_vdd_dig;
654 unsigned long flags;
655 int rc = 0;
656
657 if (cpu > num_possible_cpus()) {
658 rc = -EINVAL;
659 goto out;
660 }
661
662 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
663 mutex_lock(&drv_state.lock);
664
665 strt_s = drv_state.current_speed[cpu];
666
667 /* Return early if rate didn't change. */
668 if (rate == strt_s->acpuclk_khz)
669 goto out;
670
671 /* Find target frequency. */
672 for (tgt_s = acpu_freq_tbl; tgt_s->acpuclk_khz != 0; tgt_s++)
673 if (tgt_s->acpuclk_khz == rate)
674 break;
675 if (tgt_s->acpuclk_khz == 0) {
676 rc = -EINVAL;
677 goto out;
678 }
679
680 /* AVS needs SAW_VCTL to be intitialized correctly, before enable,
681 * and is not initialized at acpuclk_init().
682 */
683 if (reason == SETRATE_CPUFREQ)
684 AVS_DISABLE(cpu);
685
686 /* Calculate vdd_mem and vdd_dig requirements.
687 * vdd_mem must be >= vdd_sc */
688 vdd_mem = max(tgt_s->vdd_sc, tgt_s->l2_level->vdd_mem);
689 /* Factor-in PLL vdd_dig requirements. */
690 if ((tgt_s->l2_level->khz > SCPLL_LOW_VDD_FMAX) ||
691 (tgt_s->pll == ACPU_SCPLL
692 && tgt_s->acpuclk_khz > SCPLL_LOW_VDD_FMAX))
693 pll_vdd_dig = SCPLL_NOMINAL_VDD;
694 else
695 pll_vdd_dig = SCPLL_LOW_VDD;
696 vdd_dig = max(tgt_s->l2_level->vdd_dig, pll_vdd_dig);
697
698 /* Increase VDD levels if needed. */
699 if ((reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG
700 || reason == SETRATE_INIT)
701 && (tgt_s->acpuclk_khz > strt_s->acpuclk_khz)) {
702 rc = increase_vdd(cpu, tgt_s->vdd_sc, vdd_mem, vdd_dig, reason);
703 if (rc)
704 goto out;
705 }
706
707 pr_debug("Switching from ACPU%d rate %u KHz -> %u KHz\n",
708 cpu, strt_s->acpuclk_khz, tgt_s->acpuclk_khz);
709
710 /* Switch CPU speed. */
711 switch_sc_speed(cpu, tgt_s);
712
713 /* Update the L2 vote and apply the rate change. */
714 spin_lock_irqsave(&drv_state.l2_lock, flags);
715 tgt_l2 = compute_l2_speed(cpu, tgt_s->l2_level);
716 set_l2_speed(tgt_l2);
717 spin_unlock_irqrestore(&drv_state.l2_lock, flags);
718
719 /* Nothing else to do for SWFI. */
720 if (reason == SETRATE_SWFI)
721 goto out;
722
723 /* Nothing else to do for power collapse. */
724 if (reason == SETRATE_PC)
725 goto out;
726
727 /* Update bus bandwith request. */
728 set_bus_bw(tgt_l2->bw_level);
729
730 /* Drop VDD levels if we can. */
731 if (tgt_s->acpuclk_khz < strt_s->acpuclk_khz)
732 decrease_vdd(cpu, tgt_s->vdd_sc, vdd_mem, vdd_dig, reason);
733
734 pr_debug("ACPU%d speed change complete\n", cpu);
735
736 /* Re-enable AVS */
737 if (reason == SETRATE_CPUFREQ)
738 AVS_ENABLE(cpu, tgt_s->avsdscr_setting);
739
740out:
741 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
742 mutex_unlock(&drv_state.lock);
743 return rc;
744}
745
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700746static void __init scpll_init(int pll, unsigned int max_l_val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747{
748 uint32_t regval;
749
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700750 pr_debug("Initializing SCPLL%d\n", pll);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751
752 /* Clear calibration LUT registers containing max frequency entry.
753 * LUT registers are only writeable in debug mode. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700754 writel_relaxed(SCPLL_DEBUG_FULL, sc_pll_base[pll] + SCPLL_DEBUG_OFFSET);
755 writel_relaxed(0x0, sc_pll_base[pll] + SCPLL_LUT_OFFSET(max_l_val));
756 writel_relaxed(SCPLL_DEBUG_NONE, sc_pll_base[pll] + SCPLL_DEBUG_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700757
758 /* Power-up SCPLL into standby mode. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700759 writel_relaxed(SCPLL_STANDBY, sc_pll_base[pll] + SCPLL_CTL_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700760 mb();
761 udelay(10);
762
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700763 /* Calibrate the SCPLL for the frequency range needed. */
764 regval = (max_l_val << 24) | (L_VAL_SCPLL_CAL_MIN << 16);
765 writel_relaxed(regval, sc_pll_base[pll] + SCPLL_CAL_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766
767 /* Start calibration */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700768 writel_relaxed(SCPLL_FULL_CAL, sc_pll_base[pll] + SCPLL_CTL_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769
770 /* Wait for proof that calibration has started before checking the
771 * 'calibration done' bit in the status register. Waiting for the
772 * LUT register we cleared to contain data accomplishes this.
773 * This is required since the 'calibration done' bit takes time to
774 * transition from 'done' to 'not done' when starting a calibration.
775 */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700776 while (!readl_relaxed(sc_pll_base[pll] + SCPLL_LUT_OFFSET(max_l_val)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777 cpu_relax();
778
779 /* Wait for calibration to complete. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700780 while (readl_relaxed(sc_pll_base[pll] + SCPLL_STATUS_OFFSET) & 0x2)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781 cpu_relax();
782
783 /* Power-down SCPLL. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700784 scpll_disable(pll);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785}
786
787/* Force ACPU core and L2 cache clocks to rates that don't require SCPLLs. */
788static void __init unselect_scplls(void)
789{
790 int cpu;
791
792 /* Ensure CAL_IDX frequency uses AFAB sources for CPU cores and L2. */
793 BUG_ON(acpu_freq_tbl[CAL_IDX].core_src_sel != 0);
794 BUG_ON(acpu_freq_tbl[CAL_IDX].l2_level->src_sel != 0);
795
796 for_each_possible_cpu(cpu) {
797 select_clk_source_div(cpu, &acpu_freq_tbl[CAL_IDX]);
798 select_core_source(cpu, acpu_freq_tbl[CAL_IDX].core_src_sel);
799 drv_state.current_speed[cpu] = &acpu_freq_tbl[CAL_IDX];
800 l2_vote[cpu] = acpu_freq_tbl[CAL_IDX].l2_level;
801 }
802
803 select_core_source(L2, acpu_freq_tbl[CAL_IDX].l2_level->src_sel);
804 drv_state.current_l2_speed = acpu_freq_tbl[CAL_IDX].l2_level;
805}
806
807/* Ensure SCPLLs use the 27MHz PXO. */
808static void __init scpll_set_refs(void)
809{
810 int cpu;
811 uint32_t regval;
812
813 /* Bit 4 = 0:PXO, 1:MXO. */
814 for_each_possible_cpu(cpu) {
815 regval = readl_relaxed(sc_pll_base[cpu] + SCPLL_CFG_OFFSET);
816 regval &= ~BIT(4);
817 writel_relaxed(regval, sc_pll_base[cpu] + SCPLL_CFG_OFFSET);
818 }
819 regval = readl_relaxed(sc_pll_base[L2] + SCPLL_CFG_OFFSET);
820 regval &= ~BIT(4);
821 writel_relaxed(regval, sc_pll_base[L2] + SCPLL_CFG_OFFSET);
822}
823
824/* Voltage regulator initialization. */
825static void __init regulator_init(void)
826{
827 struct clkctl_acpu_speed **freq = drv_state.current_speed;
828 const char *regulator_sc_name[] = {"8901_s0", "8901_s1"};
829 int cpu, ret;
830
831 for_each_possible_cpu(cpu) {
832 /* VDD_SC0, VDD_SC1 */
833 regulator_sc[cpu] = regulator_get(NULL, regulator_sc_name[cpu]);
834 if (IS_ERR(regulator_sc[cpu]))
835 goto err;
836 ret = regulator_set_voltage(regulator_sc[cpu],
837 freq[cpu]->vdd_sc, MAX_VDD_SC);
838 if (ret)
839 goto err;
840 ret = regulator_enable(regulator_sc[cpu]);
841 if (ret)
842 goto err;
843 }
844
845 return;
846
847err:
848 pr_err("%s: Failed to initialize voltage regulators\n", __func__);
849 BUG();
850}
851
852/* Register with bus driver. */
853static void __init bus_init(void)
854{
855 bus_perf_client = msm_bus_scale_register_client(&bus_client_pdata);
856 if (!bus_perf_client) {
857 pr_err("%s: unable register bus client\n", __func__);
858 BUG();
859 }
860}
861
862#ifdef CONFIG_CPU_FREQ_MSM
863static struct cpufreq_frequency_table freq_table[NR_CPUS][30];
864
865static void __init cpufreq_table_init(void)
866{
867 int cpu;
868
869 for_each_possible_cpu(cpu) {
870 int i, freq_cnt = 0;
871 /* Construct the freq_table tables from acpu_freq_tbl. */
872 for (i = 0; acpu_freq_tbl[i].acpuclk_khz != 0
873 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
874 if (acpu_freq_tbl[i].use_for_scaling[cpu]) {
875 freq_table[cpu][freq_cnt].index = freq_cnt;
876 freq_table[cpu][freq_cnt].frequency
877 = acpu_freq_tbl[i].acpuclk_khz;
878 freq_cnt++;
879 }
880 }
881 /* freq_table not big enough to store all usable freqs. */
882 BUG_ON(acpu_freq_tbl[i].acpuclk_khz != 0);
883
884 freq_table[cpu][freq_cnt].index = freq_cnt;
885 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
886
887 pr_info("CPU%d: %d scaling frequencies supported.\n",
888 cpu, freq_cnt);
889
890 /* Register table with CPUFreq. */
891 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
892 }
893}
894#else
895static void __init cpufreq_table_init(void) {}
896#endif
897
898#define HOT_UNPLUG_KHZ MAX_AXI
899static int __cpuinit acpuclock_cpu_callback(struct notifier_block *nfb,
900 unsigned long action, void *hcpu)
901{
902 static int prev_khz[NR_CPUS];
903 int cpu = (int)hcpu;
904
905 switch (action) {
906 case CPU_DEAD:
907 case CPU_DEAD_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700908 prev_khz[cpu] = acpuclk_8x60_get_rate(cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 /* Fall through. */
910 case CPU_UP_CANCELED:
911 case CPU_UP_CANCELED_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700912 acpuclk_8x60_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700913 break;
914 case CPU_UP_PREPARE:
915 case CPU_UP_PREPARE_FROZEN:
916 if (WARN_ON(!prev_khz[cpu]))
Stephen Boydf7e53c12011-12-19 16:37:15 -0800917 return NOTIFY_BAD;
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700918 acpuclk_8x60_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 break;
920 default:
921 break;
922 }
923
924 return NOTIFY_OK;
925}
926
927static struct notifier_block __cpuinitdata acpuclock_cpu_notifier = {
928 .notifier_call = acpuclock_cpu_callback,
929};
930
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700931static __init struct clkctl_acpu_speed *select_freq_plan(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932{
933 uint32_t pte_efuse, speed_bin, pvs, max_khz;
934 struct clkctl_acpu_speed *f;
935
936 pte_efuse = readl_relaxed(QFPROM_PTE_EFUSE_ADDR);
937
938 speed_bin = pte_efuse & 0xF;
939 if (speed_bin == 0xF)
940 speed_bin = (pte_efuse >> 4) & 0xF;
941
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700942 pvs = (pte_efuse >> 10) & 0x7;
943 if (pvs == 0x7)
944 pvs = (pte_efuse >> 13) & 0x7;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700946 if (speed_bin == 0x2) {
947 max_khz = 1674000;
948 switch (pvs) {
949 case 0x7:
950 case 0x5:
951 acpu_freq_tbl = acpu_freq_tbl_slowest;
952 pr_info("ACPU PVS: Slowest\n");
953 break;
954 case 0x4:
955 acpu_freq_tbl = acpu_freq_tbl_slower;
956 pr_info("ACPU PVS: Slower\n");
957 break;
958 case 0x0:
959 acpu_freq_tbl = acpu_freq_tbl_slow;
960 pr_info("ACPU PVS: Slow\n");
961 break;
962 case 0x1:
963 acpu_freq_tbl = acpu_freq_tbl_nom;
964 pr_info("ACPU PVS: Nominal\n");
965 break;
966 case 0x3:
967 acpu_freq_tbl = acpu_freq_tbl_fast;
968 pr_info("ACPU PVS: Fast\n");
969 break;
970 default:
971 acpu_freq_tbl = acpu_freq_tbl_slowest;
972 pr_warn("ACPU PVS: Unknown. Defaulting to slowest.\n");
973 break;
974 }
975 } else if (speed_bin == 0x1) {
976 max_khz = 1512000;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700977 switch (pvs) {
978 case 0x0:
979 case 0x7:
980 acpu_freq_tbl = acpu_freq_tbl_slow;
981 pr_info("ACPU PVS: Slow\n");
982 break;
983 case 0x1:
984 acpu_freq_tbl = acpu_freq_tbl_nom;
985 pr_info("ACPU PVS: Nominal\n");
986 break;
987 case 0x3:
988 acpu_freq_tbl = acpu_freq_tbl_fast;
989 pr_info("ACPU PVS: Fast\n");
990 break;
991 default:
992 acpu_freq_tbl = acpu_freq_tbl_slow;
993 pr_warn("ACPU PVS: Unknown. Defaulting to slow.\n");
994 break;
995 }
996 } else {
997 max_khz = 1188000;
998 acpu_freq_tbl = acpu_freq_tbl_1188mhz;
999 }
1000
1001 /* Truncate the table based to max_khz. */
1002 for (f = acpu_freq_tbl; f->acpuclk_khz != 0; f++) {
1003 if (f->acpuclk_khz > max_khz) {
1004 f->acpuclk_khz = 0;
1005 break;
1006 }
1007 }
1008 f--;
1009 pr_info("Max ACPU freq: %u KHz\n", f->acpuclk_khz);
1010
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001011 return f;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001012}
1013
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001014static struct acpuclk_data acpuclk_8x60_data = {
1015 .set_rate = acpuclk_8x60_set_rate,
1016 .get_rate = acpuclk_8x60_get_rate,
1017 .power_collapse_khz = MAX_AXI,
1018 .wait_for_irq_khz = MAX_AXI,
1019};
1020
Matt Wagantallec57f062011-08-16 23:54:46 -07001021static int __init acpuclk_8x60_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001022{
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001023 struct clkctl_acpu_speed *max_freq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 int cpu;
1025
1026 mutex_init(&drv_state.lock);
1027 spin_lock_init(&drv_state.l2_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001028
1029 /* Configure hardware. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001030 max_freq = select_freq_plan();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031 unselect_scplls();
1032 scpll_set_refs();
1033 for_each_possible_cpu(cpu)
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001034 scpll_init(cpu, max_freq->l_val);
1035 scpll_init(L2, max_freq->l2_level->l_val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001036 regulator_init();
1037 bus_init();
1038
1039 /* Improve boot time by ramping up CPUs immediately. */
1040 for_each_online_cpu(cpu)
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001041 acpuclk_8x60_set_rate(cpu, max_freq->acpuclk_khz, SETRATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001043 acpuclk_register(&acpuclk_8x60_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044 cpufreq_table_init();
1045 register_hotcpu_notifier(&acpuclock_cpu_notifier);
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001046
1047 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048}
Matt Wagantallec57f062011-08-16 23:54:46 -07001049
1050struct acpuclk_soc_data acpuclk_8x60_soc_data __initdata = {
1051 .init = acpuclk_8x60_init,
1052};