blob: 69e15697e02d6dc4fb02376a6a35f6c68badf880 [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_slower[] = {
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), 1150000, 0x03006000},
243 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1175000, 0x03006000},
244 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1200000, 0x03006000},
245 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1225000, 0x03006000},
246 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1250000, 0x03006000},
247 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1275000, 0x03006000},
248 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1300000, 0x03006000},
249 { {0, 0}, 0 },
250};
251
252/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253static struct clkctl_acpu_speed acpu_freq_tbl_slow[] = {
Tianyi Gou66351ff2011-07-19 20:48:41 -0700254 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 800000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
Tianyi Gou66351ff2011-07-19 20:48:41 -0700256 { {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},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700273 { {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},
Tianyi Gou66351ff2011-07-19 20:48:41 -0700275 { {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},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1225000, 0x03006000},
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700279 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1225000, 0x03006000},
280 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1225000, 0x03006000},
281 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1250000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282 { {0, 0}, 0 },
283};
284
285/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
Tianyi Gou66351ff2011-07-19 20:48:41 -0700286static struct clkctl_acpu_speed acpu_freq_tbl_nom[] = {
287 { {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), 950000, 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), 975000, 0x03006000},
301 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 1000000, 0x03006000},
302 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 1000000, 0x03006000},
303 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1025000, 0x03006000},
304 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1025000, 0x03006000},
305 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1050000, 0x03006000},
306 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1075000, 0x03006000},
307 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1100000, 0x03006000},
308 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1125000, 0x03006000},
309 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1150000, 0x03006000},
310 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1150000, 0x03006000},
311 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1175000, 0x03006000},
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700312 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1175000, 0x03006000},
313 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1200000, 0x03006000},
314 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1200000, 0x03006000},
Tianyi Gou66351ff2011-07-19 20:48:41 -0700315 { {0, 0}, 0 },
316};
317
318/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
319static struct clkctl_acpu_speed acpu_freq_tbl_fast[] = {
320 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 800000, 0x03006000},
321 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
322 { {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), 925000, 0x03006000},
332 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 950000, 0x03006000},
333 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 950000, 0x03006000},
334 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 950000, 0x03006000},
335 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 975000, 0x03006000},
336 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1000000, 0x03006000},
337 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1000000, 0x03006000},
338 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1025000, 0x03006000},
339 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1050000, 0x03006000},
340 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1075000, 0x03006000},
341 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1100000, 0x03006000},
342 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1100000, 0x03006000},
343 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1100000, 0x03006000},
344 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1125000, 0x03006000},
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700345 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1125000, 0x03006000},
346 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1125000, 0x03006000},
347 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1150000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700348 { {0, 0}, 0 },
349};
350
351
352/* acpu_freq_tbl row to use when reconfiguring SC/L2 PLLs. */
353#define CAL_IDX 1
354
355static struct clkctl_acpu_speed *acpu_freq_tbl;
356static struct clkctl_l2_speed *l2_freq_tbl = l2_freq_tbl_v2;
357static unsigned int l2_freq_tbl_size = ARRAY_SIZE(l2_freq_tbl_v2);
358
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700359static unsigned long acpuclk_8x60_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360{
361 return drv_state.current_speed[cpu]->acpuclk_khz;
362}
363
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364static void select_core_source(unsigned int id, unsigned int src)
365{
366 uint32_t regval;
367 int shift;
368
369 shift = (id == L2) ? 0 : 1;
370 regval = readl_relaxed(clk_sel_addr[id]);
371 regval &= ~(0x3 << shift);
372 regval |= (src << shift);
373 writel_relaxed(regval, clk_sel_addr[id]);
374}
375
376static void select_clk_source_div(unsigned int id, struct clkctl_acpu_speed *s)
377{
378 uint32_t reg_clksel, reg_clkctl, src_sel;
379
380 /* Configure the PLL divider mux if we plan to use it. */
381 if (s->core_src_sel == 0) {
382
383 reg_clksel = readl_relaxed(clk_sel_addr[id]);
384
385 /* CLK_SEL_SRC1N0 (bank) bit. */
386 src_sel = reg_clksel & 1;
387
388 /* Program clock source and divider. */
389 reg_clkctl = readl_relaxed(clk_ctl_addr[id]);
390 reg_clkctl &= ~(0xFF << (8 * src_sel));
391 reg_clkctl |= s->acpuclk_src_sel << (4 + 8 * src_sel);
392 reg_clkctl |= s->acpuclk_src_div << (0 + 8 * src_sel);
393 writel_relaxed(reg_clkctl, clk_ctl_addr[id]);
394
395 /* Toggle clock source. */
396 reg_clksel ^= 1;
397
398 /* Program clock source selection. */
399 writel_relaxed(reg_clksel, clk_sel_addr[id]);
400 }
401}
402
403static void scpll_enable(int sc_pll, uint32_t l_val)
404{
405 uint32_t regval;
406
407 /* Power-up SCPLL into standby mode. */
408 writel_relaxed(SCPLL_STANDBY, sc_pll_base[sc_pll] + SCPLL_CTL_OFFSET);
409 mb();
410 udelay(10);
411
412 /* Shot-switch to target frequency. */
413 regval = (l_val << 3) | SHOT_SWITCH;
414 writel_relaxed(regval, sc_pll_base[sc_pll] + SCPLL_FSM_CTL_EXT_OFFSET);
415 writel_relaxed(SCPLL_NORMAL, sc_pll_base[sc_pll] + SCPLL_CTL_OFFSET);
416 mb();
417 udelay(20);
418}
419
420static void scpll_disable(int sc_pll)
421{
422 /* Power down SCPLL. */
423 writel_relaxed(SCPLL_POWER_DOWN,
424 sc_pll_base[sc_pll] + SCPLL_CTL_OFFSET);
425}
426
427static void scpll_change_freq(int sc_pll, uint32_t l_val)
428{
429 uint32_t regval;
430 const void *base_addr = sc_pll_base[sc_pll];
431
432 /* Complex-slew switch to target frequency. */
433 regval = (l_val << 3) | COMPLEX_SLEW;
434 writel_relaxed(regval, base_addr + SCPLL_FSM_CTL_EXT_OFFSET);
435 writel_relaxed(SCPLL_NORMAL, base_addr + SCPLL_CTL_OFFSET);
436
437 /* Wait for frequency switch to start. */
438 while (((readl_relaxed(base_addr + SCPLL_CTL_OFFSET) >> 3) & 0x3F)
439 != l_val)
440 cpu_relax();
441 /* Wait for frequency switch to finish. */
442 while (readl_relaxed(base_addr + SCPLL_STATUS_OFFSET) & 0x1)
443 cpu_relax();
444}
445
446/* Vote for the L2 speed and return the speed that should be applied. */
447static struct clkctl_l2_speed *compute_l2_speed(unsigned int voting_cpu,
448 struct clkctl_l2_speed *tgt_s)
449{
450 struct clkctl_l2_speed *new_s;
451 int cpu;
452
453 /* Bounds check. */
454 BUG_ON(tgt_s >= (l2_freq_tbl + l2_freq_tbl_size));
455
456 /* Find max L2 speed vote. */
457 l2_vote[voting_cpu] = tgt_s;
458 new_s = l2_freq_tbl;
459 for_each_present_cpu(cpu)
460 new_s = max(new_s, l2_vote[cpu]);
461
462 return new_s;
463}
464
465/* Set the L2's clock speed. */
466static void set_l2_speed(struct clkctl_l2_speed *tgt_s)
467{
468 if (tgt_s == drv_state.current_l2_speed)
469 return;
470
471 if (drv_state.current_l2_speed->src_sel == 1
472 && tgt_s->src_sel == 1)
473 scpll_change_freq(L2, tgt_s->l_val);
474 else {
475 if (tgt_s->src_sel == 1) {
476 scpll_enable(L2, tgt_s->l_val);
477 mb();
478 select_core_source(L2, tgt_s->src_sel);
479 } else {
480 select_core_source(L2, tgt_s->src_sel);
481 mb();
482 scpll_disable(L2);
483 }
484 }
485 drv_state.current_l2_speed = tgt_s;
486}
487
488/* Update the bus bandwidth request. */
489static void set_bus_bw(unsigned int bw)
490{
491 int ret;
492
493 /* Bounds check. */
494 if (bw >= ARRAY_SIZE(bw_level_tbl)) {
495 pr_err("%s: invalid bandwidth request (%d)\n", __func__, bw);
496 return;
497 }
498
499 /* Update bandwidth if requst has changed. This may sleep. */
500 ret = msm_bus_scale_client_update_request(bus_perf_client, bw);
501 if (ret)
502 pr_err("%s: bandwidth request failed (%d)\n", __func__, ret);
503
504 return;
505}
506
507/* Apply any per-cpu voltage increases. */
508static int increase_vdd(int cpu, unsigned int vdd_sc, unsigned int vdd_mem,
509 unsigned int vdd_dig, enum setrate_reason reason)
510{
511 int rc = 0;
512
513 /* Increase vdd_mem active-set before vdd_dig and vdd_sc.
514 * vdd_mem should be >= both vdd_sc and vdd_dig. */
515 rc = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S0, rpm_vreg_voter[cpu],
516 vdd_mem, MAX_VDD_MEM, 0);
517 if (rc) {
518 pr_err("%s: vdd_mem (cpu%d) increase failed (%d)\n",
519 __func__, cpu, rc);
520 return rc;
521 }
522
523 /* Increase vdd_dig active-set vote. */
524 rc = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S1, rpm_vreg_voter[cpu],
525 vdd_dig, MAX_VDD_DIG, 0);
526 if (rc) {
527 pr_err("%s: vdd_dig (cpu%d) increase failed (%d)\n",
528 __func__, cpu, rc);
529 return rc;
530 }
531
532 /* Don't update the Scorpion voltage in the hotplug path. It should
533 * already be correct. Attempting to set it is bad because we don't
534 * know what CPU we are running on at this point, but the Scorpion
535 * regulator API requires we call it from the affected CPU. */
536 if (reason == SETRATE_HOTPLUG)
537 return rc;
538
539 /* Update per-core Scorpion voltage. */
540 rc = regulator_set_voltage(regulator_sc[cpu], vdd_sc, MAX_VDD_SC);
541 if (rc) {
542 pr_err("%s: vdd_sc (cpu%d) increase failed (%d)\n",
543 __func__, cpu, rc);
544 return rc;
545 }
546
547 return rc;
548}
549
550/* Apply any per-cpu voltage decreases. */
551static void decrease_vdd(int cpu, unsigned int vdd_sc, unsigned int vdd_mem,
552 unsigned int vdd_dig, enum setrate_reason reason)
553{
554 int ret;
555
556 /* Update per-core Scorpion voltage. This must be called on the CPU
557 * that's being affected. Don't do this in the hotplug remove path,
558 * where the rail is off and we're executing on the other CPU. */
559 if (reason != SETRATE_HOTPLUG) {
560 ret = regulator_set_voltage(regulator_sc[cpu], vdd_sc,
561 MAX_VDD_SC);
562 if (ret) {
563 pr_err("%s: vdd_sc (cpu%d) decrease failed (%d)\n",
564 __func__, cpu, ret);
565 return;
566 }
567 }
568
569 /* Decrease vdd_dig active-set vote. */
570 ret = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S1, rpm_vreg_voter[cpu],
571 vdd_dig, MAX_VDD_DIG, 0);
572 if (ret) {
573 pr_err("%s: vdd_dig (cpu%d) decrease failed (%d)\n",
574 __func__, cpu, ret);
575 return;
576 }
577
578 /* Decrease vdd_mem active-set after vdd_dig and vdd_sc.
579 * vdd_mem should be >= both vdd_sc and vdd_dig. */
580 ret = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S0, rpm_vreg_voter[cpu],
581 vdd_mem, MAX_VDD_MEM, 0);
582 if (ret) {
583 pr_err("%s: vdd_mem (cpu%d) decrease failed (%d)\n",
584 __func__, cpu, ret);
585 return;
586 }
587}
588
589static void switch_sc_speed(int cpu, struct clkctl_acpu_speed *tgt_s)
590{
591 struct clkctl_acpu_speed *strt_s = drv_state.current_speed[cpu];
592
593 if (strt_s->pll != ACPU_SCPLL && tgt_s->pll != ACPU_SCPLL) {
594 select_clk_source_div(cpu, tgt_s);
595 /* Select core source because target may be AFAB. */
596 select_core_source(cpu, tgt_s->core_src_sel);
597 } else if (strt_s->pll != ACPU_SCPLL && tgt_s->pll == ACPU_SCPLL) {
598 scpll_enable(cpu, tgt_s->l_val);
599 mb();
600 select_core_source(cpu, tgt_s->core_src_sel);
601 } else if (strt_s->pll == ACPU_SCPLL && tgt_s->pll != ACPU_SCPLL) {
602 select_clk_source_div(cpu, tgt_s);
603 select_core_source(cpu, tgt_s->core_src_sel);
604 /* Core source switch must complete before disabling SCPLL. */
605 mb();
606 udelay(1);
607 scpll_disable(cpu);
608 } else
609 scpll_change_freq(cpu, tgt_s->l_val);
610
611 /* Update the driver state with the new clock freq */
612 drv_state.current_speed[cpu] = tgt_s;
613}
614
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700615static int acpuclk_8x60_set_rate(int cpu, unsigned long rate,
616 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617{
618 struct clkctl_acpu_speed *tgt_s, *strt_s;
619 struct clkctl_l2_speed *tgt_l2;
620 unsigned int vdd_mem, vdd_dig, pll_vdd_dig;
621 unsigned long flags;
622 int rc = 0;
623
624 if (cpu > num_possible_cpus()) {
625 rc = -EINVAL;
626 goto out;
627 }
628
629 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
630 mutex_lock(&drv_state.lock);
631
632 strt_s = drv_state.current_speed[cpu];
633
634 /* Return early if rate didn't change. */
635 if (rate == strt_s->acpuclk_khz)
636 goto out;
637
638 /* Find target frequency. */
639 for (tgt_s = acpu_freq_tbl; tgt_s->acpuclk_khz != 0; tgt_s++)
640 if (tgt_s->acpuclk_khz == rate)
641 break;
642 if (tgt_s->acpuclk_khz == 0) {
643 rc = -EINVAL;
644 goto out;
645 }
646
647 /* AVS needs SAW_VCTL to be intitialized correctly, before enable,
648 * and is not initialized at acpuclk_init().
649 */
650 if (reason == SETRATE_CPUFREQ)
651 AVS_DISABLE(cpu);
652
653 /* Calculate vdd_mem and vdd_dig requirements.
654 * vdd_mem must be >= vdd_sc */
655 vdd_mem = max(tgt_s->vdd_sc, tgt_s->l2_level->vdd_mem);
656 /* Factor-in PLL vdd_dig requirements. */
657 if ((tgt_s->l2_level->khz > SCPLL_LOW_VDD_FMAX) ||
658 (tgt_s->pll == ACPU_SCPLL
659 && tgt_s->acpuclk_khz > SCPLL_LOW_VDD_FMAX))
660 pll_vdd_dig = SCPLL_NOMINAL_VDD;
661 else
662 pll_vdd_dig = SCPLL_LOW_VDD;
663 vdd_dig = max(tgt_s->l2_level->vdd_dig, pll_vdd_dig);
664
665 /* Increase VDD levels if needed. */
666 if ((reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG
667 || reason == SETRATE_INIT)
668 && (tgt_s->acpuclk_khz > strt_s->acpuclk_khz)) {
669 rc = increase_vdd(cpu, tgt_s->vdd_sc, vdd_mem, vdd_dig, reason);
670 if (rc)
671 goto out;
672 }
673
674 pr_debug("Switching from ACPU%d rate %u KHz -> %u KHz\n",
675 cpu, strt_s->acpuclk_khz, tgt_s->acpuclk_khz);
676
677 /* Switch CPU speed. */
678 switch_sc_speed(cpu, tgt_s);
679
680 /* Update the L2 vote and apply the rate change. */
681 spin_lock_irqsave(&drv_state.l2_lock, flags);
682 tgt_l2 = compute_l2_speed(cpu, tgt_s->l2_level);
683 set_l2_speed(tgt_l2);
684 spin_unlock_irqrestore(&drv_state.l2_lock, flags);
685
686 /* Nothing else to do for SWFI. */
687 if (reason == SETRATE_SWFI)
688 goto out;
689
690 /* Nothing else to do for power collapse. */
691 if (reason == SETRATE_PC)
692 goto out;
693
694 /* Update bus bandwith request. */
695 set_bus_bw(tgt_l2->bw_level);
696
697 /* Drop VDD levels if we can. */
698 if (tgt_s->acpuclk_khz < strt_s->acpuclk_khz)
699 decrease_vdd(cpu, tgt_s->vdd_sc, vdd_mem, vdd_dig, reason);
700
701 pr_debug("ACPU%d speed change complete\n", cpu);
702
703 /* Re-enable AVS */
704 if (reason == SETRATE_CPUFREQ)
705 AVS_ENABLE(cpu, tgt_s->avsdscr_setting);
706
707out:
708 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
709 mutex_unlock(&drv_state.lock);
710 return rc;
711}
712
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700713static void __init scpll_init(int pll, unsigned int max_l_val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714{
715 uint32_t regval;
716
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700717 pr_debug("Initializing SCPLL%d\n", pll);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718
719 /* Clear calibration LUT registers containing max frequency entry.
720 * LUT registers are only writeable in debug mode. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700721 writel_relaxed(SCPLL_DEBUG_FULL, sc_pll_base[pll] + SCPLL_DEBUG_OFFSET);
722 writel_relaxed(0x0, sc_pll_base[pll] + SCPLL_LUT_OFFSET(max_l_val));
723 writel_relaxed(SCPLL_DEBUG_NONE, sc_pll_base[pll] + SCPLL_DEBUG_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724
725 /* Power-up SCPLL into standby mode. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700726 writel_relaxed(SCPLL_STANDBY, sc_pll_base[pll] + SCPLL_CTL_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727 mb();
728 udelay(10);
729
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700730 /* Calibrate the SCPLL for the frequency range needed. */
731 regval = (max_l_val << 24) | (L_VAL_SCPLL_CAL_MIN << 16);
732 writel_relaxed(regval, sc_pll_base[pll] + SCPLL_CAL_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733
734 /* Start calibration */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700735 writel_relaxed(SCPLL_FULL_CAL, sc_pll_base[pll] + SCPLL_CTL_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736
737 /* Wait for proof that calibration has started before checking the
738 * 'calibration done' bit in the status register. Waiting for the
739 * LUT register we cleared to contain data accomplishes this.
740 * This is required since the 'calibration done' bit takes time to
741 * transition from 'done' to 'not done' when starting a calibration.
742 */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700743 while (!readl_relaxed(sc_pll_base[pll] + SCPLL_LUT_OFFSET(max_l_val)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 cpu_relax();
745
746 /* Wait for calibration to complete. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700747 while (readl_relaxed(sc_pll_base[pll] + SCPLL_STATUS_OFFSET) & 0x2)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 cpu_relax();
749
750 /* Power-down SCPLL. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700751 scpll_disable(pll);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752}
753
754/* Force ACPU core and L2 cache clocks to rates that don't require SCPLLs. */
755static void __init unselect_scplls(void)
756{
757 int cpu;
758
759 /* Ensure CAL_IDX frequency uses AFAB sources for CPU cores and L2. */
760 BUG_ON(acpu_freq_tbl[CAL_IDX].core_src_sel != 0);
761 BUG_ON(acpu_freq_tbl[CAL_IDX].l2_level->src_sel != 0);
762
763 for_each_possible_cpu(cpu) {
764 select_clk_source_div(cpu, &acpu_freq_tbl[CAL_IDX]);
765 select_core_source(cpu, acpu_freq_tbl[CAL_IDX].core_src_sel);
766 drv_state.current_speed[cpu] = &acpu_freq_tbl[CAL_IDX];
767 l2_vote[cpu] = acpu_freq_tbl[CAL_IDX].l2_level;
768 }
769
770 select_core_source(L2, acpu_freq_tbl[CAL_IDX].l2_level->src_sel);
771 drv_state.current_l2_speed = acpu_freq_tbl[CAL_IDX].l2_level;
772}
773
774/* Ensure SCPLLs use the 27MHz PXO. */
775static void __init scpll_set_refs(void)
776{
777 int cpu;
778 uint32_t regval;
779
780 /* Bit 4 = 0:PXO, 1:MXO. */
781 for_each_possible_cpu(cpu) {
782 regval = readl_relaxed(sc_pll_base[cpu] + SCPLL_CFG_OFFSET);
783 regval &= ~BIT(4);
784 writel_relaxed(regval, sc_pll_base[cpu] + SCPLL_CFG_OFFSET);
785 }
786 regval = readl_relaxed(sc_pll_base[L2] + SCPLL_CFG_OFFSET);
787 regval &= ~BIT(4);
788 writel_relaxed(regval, sc_pll_base[L2] + SCPLL_CFG_OFFSET);
789}
790
791/* Voltage regulator initialization. */
792static void __init regulator_init(void)
793{
794 struct clkctl_acpu_speed **freq = drv_state.current_speed;
795 const char *regulator_sc_name[] = {"8901_s0", "8901_s1"};
796 int cpu, ret;
797
798 for_each_possible_cpu(cpu) {
799 /* VDD_SC0, VDD_SC1 */
800 regulator_sc[cpu] = regulator_get(NULL, regulator_sc_name[cpu]);
801 if (IS_ERR(regulator_sc[cpu]))
802 goto err;
803 ret = regulator_set_voltage(regulator_sc[cpu],
804 freq[cpu]->vdd_sc, MAX_VDD_SC);
805 if (ret)
806 goto err;
807 ret = regulator_enable(regulator_sc[cpu]);
808 if (ret)
809 goto err;
810 }
811
812 return;
813
814err:
815 pr_err("%s: Failed to initialize voltage regulators\n", __func__);
816 BUG();
817}
818
819/* Register with bus driver. */
820static void __init bus_init(void)
821{
822 bus_perf_client = msm_bus_scale_register_client(&bus_client_pdata);
823 if (!bus_perf_client) {
824 pr_err("%s: unable register bus client\n", __func__);
825 BUG();
826 }
827}
828
829#ifdef CONFIG_CPU_FREQ_MSM
830static struct cpufreq_frequency_table freq_table[NR_CPUS][30];
831
832static void __init cpufreq_table_init(void)
833{
834 int cpu;
835
836 for_each_possible_cpu(cpu) {
837 int i, freq_cnt = 0;
838 /* Construct the freq_table tables from acpu_freq_tbl. */
839 for (i = 0; acpu_freq_tbl[i].acpuclk_khz != 0
840 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
841 if (acpu_freq_tbl[i].use_for_scaling[cpu]) {
842 freq_table[cpu][freq_cnt].index = freq_cnt;
843 freq_table[cpu][freq_cnt].frequency
844 = acpu_freq_tbl[i].acpuclk_khz;
845 freq_cnt++;
846 }
847 }
848 /* freq_table not big enough to store all usable freqs. */
849 BUG_ON(acpu_freq_tbl[i].acpuclk_khz != 0);
850
851 freq_table[cpu][freq_cnt].index = freq_cnt;
852 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
853
854 pr_info("CPU%d: %d scaling frequencies supported.\n",
855 cpu, freq_cnt);
856
857 /* Register table with CPUFreq. */
858 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
859 }
860}
861#else
862static void __init cpufreq_table_init(void) {}
863#endif
864
865#define HOT_UNPLUG_KHZ MAX_AXI
866static int __cpuinit acpuclock_cpu_callback(struct notifier_block *nfb,
867 unsigned long action, void *hcpu)
868{
869 static int prev_khz[NR_CPUS];
870 int cpu = (int)hcpu;
871
872 switch (action) {
873 case CPU_DEAD:
874 case CPU_DEAD_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700875 prev_khz[cpu] = acpuclk_8x60_get_rate(cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 /* Fall through. */
877 case CPU_UP_CANCELED:
878 case CPU_UP_CANCELED_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700879 acpuclk_8x60_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 break;
881 case CPU_UP_PREPARE:
882 case CPU_UP_PREPARE_FROZEN:
883 if (WARN_ON(!prev_khz[cpu]))
Stephen Boydf7e53c12011-12-19 16:37:15 -0800884 return NOTIFY_BAD;
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700885 acpuclk_8x60_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700886 break;
887 default:
888 break;
889 }
890
891 return NOTIFY_OK;
892}
893
894static struct notifier_block __cpuinitdata acpuclock_cpu_notifier = {
895 .notifier_call = acpuclock_cpu_callback,
896};
897
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700898static __init struct clkctl_acpu_speed *select_freq_plan(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899{
900 uint32_t pte_efuse, speed_bin, pvs, max_khz;
901 struct clkctl_acpu_speed *f;
902
903 pte_efuse = readl_relaxed(QFPROM_PTE_EFUSE_ADDR);
904
905 speed_bin = pte_efuse & 0xF;
906 if (speed_bin == 0xF)
907 speed_bin = (pte_efuse >> 4) & 0xF;
908
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700909 pvs = (pte_efuse >> 10) & 0x7;
910 if (pvs == 0x7)
911 pvs = (pte_efuse >> 13) & 0x7;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700913 if (speed_bin == 0x2) {
914 max_khz = 1674000;
915 switch (pvs) {
916 case 0x7:
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700917 case 0x4:
918 acpu_freq_tbl = acpu_freq_tbl_slower;
919 pr_info("ACPU PVS: Slower\n");
920 break;
921 case 0x0:
922 acpu_freq_tbl = acpu_freq_tbl_slow;
923 pr_info("ACPU PVS: Slow\n");
924 break;
925 case 0x1:
926 acpu_freq_tbl = acpu_freq_tbl_nom;
927 pr_info("ACPU PVS: Nominal\n");
928 break;
929 case 0x3:
930 acpu_freq_tbl = acpu_freq_tbl_fast;
931 pr_info("ACPU PVS: Fast\n");
932 break;
933 default:
Matt Wagantall0ac31752012-05-29 19:43:48 -0700934 acpu_freq_tbl = acpu_freq_tbl_slower;
935 pr_warn("ACPU PVS: Unknown. Defaulting to slower.\n");
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700936 break;
937 }
938 } else if (speed_bin == 0x1) {
939 max_khz = 1512000;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 switch (pvs) {
941 case 0x0:
942 case 0x7:
943 acpu_freq_tbl = acpu_freq_tbl_slow;
944 pr_info("ACPU PVS: Slow\n");
945 break;
946 case 0x1:
947 acpu_freq_tbl = acpu_freq_tbl_nom;
948 pr_info("ACPU PVS: Nominal\n");
949 break;
950 case 0x3:
951 acpu_freq_tbl = acpu_freq_tbl_fast;
952 pr_info("ACPU PVS: Fast\n");
953 break;
954 default:
955 acpu_freq_tbl = acpu_freq_tbl_slow;
956 pr_warn("ACPU PVS: Unknown. Defaulting to slow.\n");
957 break;
958 }
959 } else {
960 max_khz = 1188000;
961 acpu_freq_tbl = acpu_freq_tbl_1188mhz;
962 }
963
964 /* Truncate the table based to max_khz. */
965 for (f = acpu_freq_tbl; f->acpuclk_khz != 0; f++) {
966 if (f->acpuclk_khz > max_khz) {
967 f->acpuclk_khz = 0;
968 break;
969 }
970 }
971 f--;
972 pr_info("Max ACPU freq: %u KHz\n", f->acpuclk_khz);
973
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700974 return f;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700975}
976
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700977static struct acpuclk_data acpuclk_8x60_data = {
978 .set_rate = acpuclk_8x60_set_rate,
979 .get_rate = acpuclk_8x60_get_rate,
980 .power_collapse_khz = MAX_AXI,
981 .wait_for_irq_khz = MAX_AXI,
982};
983
Matt Wagantallec57f062011-08-16 23:54:46 -0700984static int __init acpuclk_8x60_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985{
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700986 struct clkctl_acpu_speed *max_freq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700987 int cpu;
988
989 mutex_init(&drv_state.lock);
990 spin_lock_init(&drv_state.l2_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991
992 /* Configure hardware. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700993 max_freq = select_freq_plan();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994 unselect_scplls();
995 scpll_set_refs();
996 for_each_possible_cpu(cpu)
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700997 scpll_init(cpu, max_freq->l_val);
998 scpll_init(L2, max_freq->l2_level->l_val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700999 regulator_init();
1000 bus_init();
1001
1002 /* Improve boot time by ramping up CPUs immediately. */
1003 for_each_online_cpu(cpu)
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001004 acpuclk_8x60_set_rate(cpu, max_freq->acpuclk_khz, SETRATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001005
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001006 acpuclk_register(&acpuclk_8x60_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001007 cpufreq_table_init();
1008 register_hotcpu_notifier(&acpuclock_cpu_notifier);
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001009
1010 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011}
Matt Wagantallec57f062011-08-16 23:54:46 -07001012
1013struct acpuclk_soc_data acpuclk_8x60_soc_data __initdata = {
1014 .init = acpuclk_8x60_init,
1015};