blob: 48efa18b46abcd72c1ef057398399c65d570930f [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 Wagantall3b492ab2012-05-29 20:05:23 -0700220static struct clkctl_acpu_speed acpu_freq_tbl_1512mhz_slow[] = {
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700221 { {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},
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700246 { {0, 0}, 0 },
247};
248
249/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
Matt Wagantall3b492ab2012-05-29 20:05:23 -0700250static struct clkctl_acpu_speed acpu_freq_tbl_1512mhz_nom[] = {
Tianyi Gou66351ff2011-07-19 20:48:41 -0700251 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 800000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
Tianyi Gou66351ff2011-07-19 20:48:41 -0700253 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 825000, 0x03006000},
254 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 825000, 0x03006000},
255 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 850000, 0x03006000},
256 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 850000, 0x03006000},
257 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 875000, 0x03006000},
258 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 875000, 0x03006000},
259 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 900000, 0x03006000},
260 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 900000, 0x03006000},
261 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 925000, 0x03006000},
262 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 950000, 0x03006000},
263 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 975000, 0x03006000},
264 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 975000, 0x03006000},
265 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 1000000, 0x03006000},
266 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 1000000, 0x03006000},
267 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1025000, 0x03006000},
268 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1025000, 0x03006000},
269 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1050000, 0x03006000},
270 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1075000, 0x03006000},
271 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1100000, 0x03006000},
272 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1125000, 0x03006000},
273 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1150000, 0x03006000},
274 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1150000, 0x03006000},
275 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1175000, 0x03006000},
276 { {0, 0}, 0 },
277};
278
279/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
Matt Wagantall3b492ab2012-05-29 20:05:23 -0700280static struct clkctl_acpu_speed acpu_freq_tbl_1512mhz_fast[] = {
Tianyi Gou66351ff2011-07-19 20:48:41 -0700281 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 800000, 0x03006000},
282 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
283 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 825000, 0x03006000},
284 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 825000, 0x03006000},
285 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 850000, 0x03006000},
286 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 850000, 0x03006000},
287 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 875000, 0x03006000},
288 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 875000, 0x03006000},
289 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 900000, 0x03006000},
290 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 900000, 0x03006000},
291 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 925000, 0x03006000},
292 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 925000, 0x03006000},
293 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 950000, 0x03006000},
294 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 950000, 0x03006000},
295 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 950000, 0x03006000},
296 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 975000, 0x03006000},
297 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1000000, 0x03006000},
298 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1000000, 0x03006000},
299 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1025000, 0x03006000},
300 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1050000, 0x03006000},
301 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1075000, 0x03006000},
302 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1100000, 0x03006000},
303 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1100000, 0x03006000},
304 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1100000, 0x03006000},
305 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1125000, 0x03006000},
Matt Wagantall3b492ab2012-05-29 20:05:23 -0700306 { {0, 0}, 0 },
307};
308
309/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
310static struct clkctl_acpu_speed acpu_freq_tbl_1674mhz_slower[] = {
311 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 775000, 0x03006000},
312 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
313 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 775000, 0x03006000},
314 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 775000, 0x03006000},
315 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 775000, 0x03006000},
316 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 775000, 0x03006000},
317 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 787500, 0x03006000},
318 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 800000, 0x03006000},
319 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 825000, 0x03006000},
320 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 837500, 0x03006000},
321 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 850000, 0x03006000},
322 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 875000, 0x03006000},
323 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 900000, 0x03006000},
324 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 912500, 0x03006000},
325 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 937500, 0x03006000},
326 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 962500, 0x03006000},
327 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 987500, 0x03006000},
328 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1012500, 0x03006000},
329 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1025000, 0x03006000},
330 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1062500, 0x03006000},
331 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1087500, 0x03006000},
332 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1100000, 0x03006000},
333 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1125000, 0x03006000},
334 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1150000, 0x03006000},
335 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1187500, 0x03006000},
336 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1225000, 0x03006000},
337 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1262500, 0x03006000},
338 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1300000, 0x03006000},
339 { {0, 0}, 0 },
340};
341
342/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
343static struct clkctl_acpu_speed acpu_freq_tbl_1674mhz_slow[] = {
344 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 775000, 0x03006000},
345 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
346 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 775000, 0x03006000},
347 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 775000, 0x03006000},
348 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 775000, 0x03006000},
349 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 775000, 0x03006000},
350 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 787500, 0x03006000},
351 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 800000, 0x03006000},
352 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 825000, 0x03006000},
353 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 837500, 0x03006000},
354 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 850000, 0x03006000},
355 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 862500, 0x03006000},
356 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 887500, 0x03006000},
357 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 900000, 0x03006000},
358 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 925000, 0x03006000},
359 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 937500, 0x03006000},
360 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 962500, 0x03006000},
361 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 987500, 0x03006000},
362 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1000000, 0x03006000},
363 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1025000, 0x03006000},
364 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1050000, 0x03006000},
365 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1062500, 0x03006000},
366 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1087500, 0x03006000},
367 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1112500, 0x03006000},
368 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1150000, 0x03006000},
369 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1175000, 0x03006000},
370 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1212500, 0x03006000},
371 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1250000, 0x03006000},
372 { {0, 0}, 0 },
373};
374
375/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
376static struct clkctl_acpu_speed acpu_freq_tbl_1674mhz_nom[] = {
377 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 775000, 0x03006000},
378 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
379 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 775000, 0x03006000},
380 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 775000, 0x03006000},
381 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 775000, 0x03006000},
382 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 775000, 0x03006000},
383 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 787500, 0x03006000},
384 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 800000, 0x03006000},
385 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 812500, 0x03006000},
386 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 825000, 0x03006000},
387 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 837500, 0x03006000},
388 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 850000, 0x03006000},
389 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 875000, 0x03006000},
390 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 887500, 0x03006000},
391 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 900000, 0x03006000},
392 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 912500, 0x03006000},
393 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 937500, 0x03006000},
394 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 950000, 0x03006000},
395 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 975000, 0x03006000},
396 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 987500, 0x03006000},
397 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1012500, 0x03006000},
398 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1025000, 0x03006000},
399 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1050000, 0x03006000},
400 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1075000, 0x03006000},
401 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1112500, 0x03006000},
402 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1137500, 0x03006000},
403 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1175000, 0x03006000},
404 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1200000, 0x03006000},
405 { {0, 0}, 0 },
406};
407
408/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
409static struct clkctl_acpu_speed acpu_freq_tbl_1674mhz_fast[] = {
410 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 775000, 0x03006000},
411 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
412 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 775000, 0x03006000},
413 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 775000, 0x03006000},
414 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 775000, 0x03006000},
415 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 775000, 0x03006000},
416 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 775000, 0x03006000},
417 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 787500, 0x03006000},
418 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 800000, 0x03006000},
419 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 812500, 0x03006000},
420 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 825000, 0x03006000},
421 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 837500, 0x03006000},
422 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 862500, 0x03006000},
423 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 875000, 0x03006000},
424 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 887500, 0x03006000},
425 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 900000, 0x03006000},
426 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 925000, 0x03006000},
427 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 937500, 0x03006000},
428 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 950000, 0x03006000},
429 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 962500, 0x03006000},
430 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 975000, 0x03006000},
431 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1000000, 0x03006000},
432 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1025000, 0x03006000},
433 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1050000, 0x03006000},
434 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1075000, 0x03006000},
435 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1100000, 0x03006000},
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700436 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1125000, 0x03006000},
437 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1150000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438 { {0, 0}, 0 },
439};
440
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441/* acpu_freq_tbl row to use when reconfiguring SC/L2 PLLs. */
442#define CAL_IDX 1
443
444static struct clkctl_acpu_speed *acpu_freq_tbl;
445static struct clkctl_l2_speed *l2_freq_tbl = l2_freq_tbl_v2;
446static unsigned int l2_freq_tbl_size = ARRAY_SIZE(l2_freq_tbl_v2);
447
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700448static unsigned long acpuclk_8x60_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449{
450 return drv_state.current_speed[cpu]->acpuclk_khz;
451}
452
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453static void select_core_source(unsigned int id, unsigned int src)
454{
455 uint32_t regval;
456 int shift;
457
458 shift = (id == L2) ? 0 : 1;
459 regval = readl_relaxed(clk_sel_addr[id]);
460 regval &= ~(0x3 << shift);
461 regval |= (src << shift);
462 writel_relaxed(regval, clk_sel_addr[id]);
463}
464
465static void select_clk_source_div(unsigned int id, struct clkctl_acpu_speed *s)
466{
467 uint32_t reg_clksel, reg_clkctl, src_sel;
468
469 /* Configure the PLL divider mux if we plan to use it. */
470 if (s->core_src_sel == 0) {
471
472 reg_clksel = readl_relaxed(clk_sel_addr[id]);
473
474 /* CLK_SEL_SRC1N0 (bank) bit. */
475 src_sel = reg_clksel & 1;
476
477 /* Program clock source and divider. */
478 reg_clkctl = readl_relaxed(clk_ctl_addr[id]);
479 reg_clkctl &= ~(0xFF << (8 * src_sel));
480 reg_clkctl |= s->acpuclk_src_sel << (4 + 8 * src_sel);
481 reg_clkctl |= s->acpuclk_src_div << (0 + 8 * src_sel);
482 writel_relaxed(reg_clkctl, clk_ctl_addr[id]);
483
484 /* Toggle clock source. */
485 reg_clksel ^= 1;
486
487 /* Program clock source selection. */
488 writel_relaxed(reg_clksel, clk_sel_addr[id]);
489 }
490}
491
492static void scpll_enable(int sc_pll, uint32_t l_val)
493{
494 uint32_t regval;
495
496 /* Power-up SCPLL into standby mode. */
497 writel_relaxed(SCPLL_STANDBY, sc_pll_base[sc_pll] + SCPLL_CTL_OFFSET);
498 mb();
499 udelay(10);
500
501 /* Shot-switch to target frequency. */
502 regval = (l_val << 3) | SHOT_SWITCH;
503 writel_relaxed(regval, sc_pll_base[sc_pll] + SCPLL_FSM_CTL_EXT_OFFSET);
504 writel_relaxed(SCPLL_NORMAL, sc_pll_base[sc_pll] + SCPLL_CTL_OFFSET);
505 mb();
506 udelay(20);
507}
508
509static void scpll_disable(int sc_pll)
510{
511 /* Power down SCPLL. */
512 writel_relaxed(SCPLL_POWER_DOWN,
513 sc_pll_base[sc_pll] + SCPLL_CTL_OFFSET);
514}
515
516static void scpll_change_freq(int sc_pll, uint32_t l_val)
517{
518 uint32_t regval;
519 const void *base_addr = sc_pll_base[sc_pll];
520
521 /* Complex-slew switch to target frequency. */
522 regval = (l_val << 3) | COMPLEX_SLEW;
523 writel_relaxed(regval, base_addr + SCPLL_FSM_CTL_EXT_OFFSET);
524 writel_relaxed(SCPLL_NORMAL, base_addr + SCPLL_CTL_OFFSET);
525
526 /* Wait for frequency switch to start. */
527 while (((readl_relaxed(base_addr + SCPLL_CTL_OFFSET) >> 3) & 0x3F)
528 != l_val)
529 cpu_relax();
530 /* Wait for frequency switch to finish. */
531 while (readl_relaxed(base_addr + SCPLL_STATUS_OFFSET) & 0x1)
532 cpu_relax();
533}
534
535/* Vote for the L2 speed and return the speed that should be applied. */
536static struct clkctl_l2_speed *compute_l2_speed(unsigned int voting_cpu,
537 struct clkctl_l2_speed *tgt_s)
538{
539 struct clkctl_l2_speed *new_s;
540 int cpu;
541
542 /* Bounds check. */
543 BUG_ON(tgt_s >= (l2_freq_tbl + l2_freq_tbl_size));
544
545 /* Find max L2 speed vote. */
546 l2_vote[voting_cpu] = tgt_s;
547 new_s = l2_freq_tbl;
548 for_each_present_cpu(cpu)
549 new_s = max(new_s, l2_vote[cpu]);
550
551 return new_s;
552}
553
554/* Set the L2's clock speed. */
555static void set_l2_speed(struct clkctl_l2_speed *tgt_s)
556{
557 if (tgt_s == drv_state.current_l2_speed)
558 return;
559
560 if (drv_state.current_l2_speed->src_sel == 1
561 && tgt_s->src_sel == 1)
562 scpll_change_freq(L2, tgt_s->l_val);
563 else {
564 if (tgt_s->src_sel == 1) {
565 scpll_enable(L2, tgt_s->l_val);
566 mb();
567 select_core_source(L2, tgt_s->src_sel);
568 } else {
569 select_core_source(L2, tgt_s->src_sel);
570 mb();
571 scpll_disable(L2);
572 }
573 }
574 drv_state.current_l2_speed = tgt_s;
575}
576
577/* Update the bus bandwidth request. */
578static void set_bus_bw(unsigned int bw)
579{
580 int ret;
581
582 /* Bounds check. */
583 if (bw >= ARRAY_SIZE(bw_level_tbl)) {
584 pr_err("%s: invalid bandwidth request (%d)\n", __func__, bw);
585 return;
586 }
587
588 /* Update bandwidth if requst has changed. This may sleep. */
589 ret = msm_bus_scale_client_update_request(bus_perf_client, bw);
590 if (ret)
591 pr_err("%s: bandwidth request failed (%d)\n", __func__, ret);
592
593 return;
594}
595
596/* Apply any per-cpu voltage increases. */
597static int increase_vdd(int cpu, unsigned int vdd_sc, unsigned int vdd_mem,
598 unsigned int vdd_dig, enum setrate_reason reason)
599{
600 int rc = 0;
601
602 /* Increase vdd_mem active-set before vdd_dig and vdd_sc.
603 * vdd_mem should be >= both vdd_sc and vdd_dig. */
604 rc = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S0, rpm_vreg_voter[cpu],
605 vdd_mem, MAX_VDD_MEM, 0);
606 if (rc) {
607 pr_err("%s: vdd_mem (cpu%d) increase failed (%d)\n",
608 __func__, cpu, rc);
609 return rc;
610 }
611
612 /* Increase vdd_dig active-set vote. */
613 rc = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S1, rpm_vreg_voter[cpu],
614 vdd_dig, MAX_VDD_DIG, 0);
615 if (rc) {
616 pr_err("%s: vdd_dig (cpu%d) increase failed (%d)\n",
617 __func__, cpu, rc);
618 return rc;
619 }
620
621 /* Don't update the Scorpion voltage in the hotplug path. It should
622 * already be correct. Attempting to set it is bad because we don't
623 * know what CPU we are running on at this point, but the Scorpion
624 * regulator API requires we call it from the affected CPU. */
625 if (reason == SETRATE_HOTPLUG)
626 return rc;
627
628 /* Update per-core Scorpion voltage. */
629 rc = regulator_set_voltage(regulator_sc[cpu], vdd_sc, MAX_VDD_SC);
630 if (rc) {
631 pr_err("%s: vdd_sc (cpu%d) increase failed (%d)\n",
632 __func__, cpu, rc);
633 return rc;
634 }
635
636 return rc;
637}
638
639/* Apply any per-cpu voltage decreases. */
640static void decrease_vdd(int cpu, unsigned int vdd_sc, unsigned int vdd_mem,
641 unsigned int vdd_dig, enum setrate_reason reason)
642{
643 int ret;
644
645 /* Update per-core Scorpion voltage. This must be called on the CPU
646 * that's being affected. Don't do this in the hotplug remove path,
647 * where the rail is off and we're executing on the other CPU. */
648 if (reason != SETRATE_HOTPLUG) {
649 ret = regulator_set_voltage(regulator_sc[cpu], vdd_sc,
650 MAX_VDD_SC);
651 if (ret) {
652 pr_err("%s: vdd_sc (cpu%d) decrease failed (%d)\n",
653 __func__, cpu, ret);
654 return;
655 }
656 }
657
658 /* Decrease vdd_dig active-set vote. */
659 ret = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S1, rpm_vreg_voter[cpu],
660 vdd_dig, MAX_VDD_DIG, 0);
661 if (ret) {
662 pr_err("%s: vdd_dig (cpu%d) decrease failed (%d)\n",
663 __func__, cpu, ret);
664 return;
665 }
666
667 /* Decrease vdd_mem active-set after vdd_dig and vdd_sc.
668 * vdd_mem should be >= both vdd_sc and vdd_dig. */
669 ret = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S0, rpm_vreg_voter[cpu],
670 vdd_mem, MAX_VDD_MEM, 0);
671 if (ret) {
672 pr_err("%s: vdd_mem (cpu%d) decrease failed (%d)\n",
673 __func__, cpu, ret);
674 return;
675 }
676}
677
678static void switch_sc_speed(int cpu, struct clkctl_acpu_speed *tgt_s)
679{
680 struct clkctl_acpu_speed *strt_s = drv_state.current_speed[cpu];
681
682 if (strt_s->pll != ACPU_SCPLL && tgt_s->pll != ACPU_SCPLL) {
683 select_clk_source_div(cpu, tgt_s);
684 /* Select core source because target may be AFAB. */
685 select_core_source(cpu, tgt_s->core_src_sel);
686 } else if (strt_s->pll != ACPU_SCPLL && tgt_s->pll == ACPU_SCPLL) {
687 scpll_enable(cpu, tgt_s->l_val);
688 mb();
689 select_core_source(cpu, tgt_s->core_src_sel);
690 } else if (strt_s->pll == ACPU_SCPLL && tgt_s->pll != ACPU_SCPLL) {
691 select_clk_source_div(cpu, tgt_s);
692 select_core_source(cpu, tgt_s->core_src_sel);
693 /* Core source switch must complete before disabling SCPLL. */
694 mb();
695 udelay(1);
696 scpll_disable(cpu);
697 } else
698 scpll_change_freq(cpu, tgt_s->l_val);
699
700 /* Update the driver state with the new clock freq */
701 drv_state.current_speed[cpu] = tgt_s;
702}
703
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700704static int acpuclk_8x60_set_rate(int cpu, unsigned long rate,
705 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706{
707 struct clkctl_acpu_speed *tgt_s, *strt_s;
708 struct clkctl_l2_speed *tgt_l2;
709 unsigned int vdd_mem, vdd_dig, pll_vdd_dig;
710 unsigned long flags;
711 int rc = 0;
712
713 if (cpu > num_possible_cpus()) {
714 rc = -EINVAL;
715 goto out;
716 }
717
718 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
719 mutex_lock(&drv_state.lock);
720
721 strt_s = drv_state.current_speed[cpu];
722
723 /* Return early if rate didn't change. */
724 if (rate == strt_s->acpuclk_khz)
725 goto out;
726
727 /* Find target frequency. */
728 for (tgt_s = acpu_freq_tbl; tgt_s->acpuclk_khz != 0; tgt_s++)
729 if (tgt_s->acpuclk_khz == rate)
730 break;
731 if (tgt_s->acpuclk_khz == 0) {
732 rc = -EINVAL;
733 goto out;
734 }
735
736 /* AVS needs SAW_VCTL to be intitialized correctly, before enable,
737 * and is not initialized at acpuclk_init().
738 */
739 if (reason == SETRATE_CPUFREQ)
740 AVS_DISABLE(cpu);
741
742 /* Calculate vdd_mem and vdd_dig requirements.
743 * vdd_mem must be >= vdd_sc */
744 vdd_mem = max(tgt_s->vdd_sc, tgt_s->l2_level->vdd_mem);
745 /* Factor-in PLL vdd_dig requirements. */
746 if ((tgt_s->l2_level->khz > SCPLL_LOW_VDD_FMAX) ||
747 (tgt_s->pll == ACPU_SCPLL
748 && tgt_s->acpuclk_khz > SCPLL_LOW_VDD_FMAX))
749 pll_vdd_dig = SCPLL_NOMINAL_VDD;
750 else
751 pll_vdd_dig = SCPLL_LOW_VDD;
752 vdd_dig = max(tgt_s->l2_level->vdd_dig, pll_vdd_dig);
753
754 /* Increase VDD levels if needed. */
755 if ((reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG
756 || reason == SETRATE_INIT)
757 && (tgt_s->acpuclk_khz > strt_s->acpuclk_khz)) {
758 rc = increase_vdd(cpu, tgt_s->vdd_sc, vdd_mem, vdd_dig, reason);
759 if (rc)
760 goto out;
761 }
762
763 pr_debug("Switching from ACPU%d rate %u KHz -> %u KHz\n",
764 cpu, strt_s->acpuclk_khz, tgt_s->acpuclk_khz);
765
766 /* Switch CPU speed. */
767 switch_sc_speed(cpu, tgt_s);
768
769 /* Update the L2 vote and apply the rate change. */
770 spin_lock_irqsave(&drv_state.l2_lock, flags);
771 tgt_l2 = compute_l2_speed(cpu, tgt_s->l2_level);
772 set_l2_speed(tgt_l2);
773 spin_unlock_irqrestore(&drv_state.l2_lock, flags);
774
775 /* Nothing else to do for SWFI. */
776 if (reason == SETRATE_SWFI)
777 goto out;
778
779 /* Nothing else to do for power collapse. */
780 if (reason == SETRATE_PC)
781 goto out;
782
783 /* Update bus bandwith request. */
784 set_bus_bw(tgt_l2->bw_level);
785
786 /* Drop VDD levels if we can. */
787 if (tgt_s->acpuclk_khz < strt_s->acpuclk_khz)
788 decrease_vdd(cpu, tgt_s->vdd_sc, vdd_mem, vdd_dig, reason);
789
790 pr_debug("ACPU%d speed change complete\n", cpu);
791
792 /* Re-enable AVS */
793 if (reason == SETRATE_CPUFREQ)
794 AVS_ENABLE(cpu, tgt_s->avsdscr_setting);
795
796out:
797 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
798 mutex_unlock(&drv_state.lock);
799 return rc;
800}
801
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700802static void __init scpll_init(int pll, unsigned int max_l_val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803{
804 uint32_t regval;
805
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700806 pr_debug("Initializing SCPLL%d\n", pll);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807
808 /* Clear calibration LUT registers containing max frequency entry.
809 * LUT registers are only writeable in debug mode. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700810 writel_relaxed(SCPLL_DEBUG_FULL, sc_pll_base[pll] + SCPLL_DEBUG_OFFSET);
811 writel_relaxed(0x0, sc_pll_base[pll] + SCPLL_LUT_OFFSET(max_l_val));
812 writel_relaxed(SCPLL_DEBUG_NONE, sc_pll_base[pll] + SCPLL_DEBUG_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813
814 /* Power-up SCPLL into standby mode. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700815 writel_relaxed(SCPLL_STANDBY, sc_pll_base[pll] + SCPLL_CTL_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700816 mb();
817 udelay(10);
818
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700819 /* Calibrate the SCPLL for the frequency range needed. */
820 regval = (max_l_val << 24) | (L_VAL_SCPLL_CAL_MIN << 16);
821 writel_relaxed(regval, sc_pll_base[pll] + SCPLL_CAL_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822
823 /* Start calibration */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700824 writel_relaxed(SCPLL_FULL_CAL, sc_pll_base[pll] + SCPLL_CTL_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825
826 /* Wait for proof that calibration has started before checking the
827 * 'calibration done' bit in the status register. Waiting for the
828 * LUT register we cleared to contain data accomplishes this.
829 * This is required since the 'calibration done' bit takes time to
830 * transition from 'done' to 'not done' when starting a calibration.
831 */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700832 while (!readl_relaxed(sc_pll_base[pll] + SCPLL_LUT_OFFSET(max_l_val)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 cpu_relax();
834
835 /* Wait for calibration to complete. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700836 while (readl_relaxed(sc_pll_base[pll] + SCPLL_STATUS_OFFSET) & 0x2)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837 cpu_relax();
838
839 /* Power-down SCPLL. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700840 scpll_disable(pll);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700841}
842
843/* Force ACPU core and L2 cache clocks to rates that don't require SCPLLs. */
844static void __init unselect_scplls(void)
845{
846 int cpu;
847
848 /* Ensure CAL_IDX frequency uses AFAB sources for CPU cores and L2. */
849 BUG_ON(acpu_freq_tbl[CAL_IDX].core_src_sel != 0);
850 BUG_ON(acpu_freq_tbl[CAL_IDX].l2_level->src_sel != 0);
851
852 for_each_possible_cpu(cpu) {
853 select_clk_source_div(cpu, &acpu_freq_tbl[CAL_IDX]);
854 select_core_source(cpu, acpu_freq_tbl[CAL_IDX].core_src_sel);
855 drv_state.current_speed[cpu] = &acpu_freq_tbl[CAL_IDX];
856 l2_vote[cpu] = acpu_freq_tbl[CAL_IDX].l2_level;
857 }
858
859 select_core_source(L2, acpu_freq_tbl[CAL_IDX].l2_level->src_sel);
860 drv_state.current_l2_speed = acpu_freq_tbl[CAL_IDX].l2_level;
861}
862
863/* Ensure SCPLLs use the 27MHz PXO. */
864static void __init scpll_set_refs(void)
865{
866 int cpu;
867 uint32_t regval;
868
869 /* Bit 4 = 0:PXO, 1:MXO. */
870 for_each_possible_cpu(cpu) {
871 regval = readl_relaxed(sc_pll_base[cpu] + SCPLL_CFG_OFFSET);
872 regval &= ~BIT(4);
873 writel_relaxed(regval, sc_pll_base[cpu] + SCPLL_CFG_OFFSET);
874 }
875 regval = readl_relaxed(sc_pll_base[L2] + SCPLL_CFG_OFFSET);
876 regval &= ~BIT(4);
877 writel_relaxed(regval, sc_pll_base[L2] + SCPLL_CFG_OFFSET);
878}
879
880/* Voltage regulator initialization. */
881static void __init regulator_init(void)
882{
883 struct clkctl_acpu_speed **freq = drv_state.current_speed;
884 const char *regulator_sc_name[] = {"8901_s0", "8901_s1"};
885 int cpu, ret;
886
887 for_each_possible_cpu(cpu) {
888 /* VDD_SC0, VDD_SC1 */
889 regulator_sc[cpu] = regulator_get(NULL, regulator_sc_name[cpu]);
890 if (IS_ERR(regulator_sc[cpu]))
891 goto err;
892 ret = regulator_set_voltage(regulator_sc[cpu],
893 freq[cpu]->vdd_sc, MAX_VDD_SC);
894 if (ret)
895 goto err;
896 ret = regulator_enable(regulator_sc[cpu]);
897 if (ret)
898 goto err;
899 }
900
901 return;
902
903err:
904 pr_err("%s: Failed to initialize voltage regulators\n", __func__);
905 BUG();
906}
907
908/* Register with bus driver. */
909static void __init bus_init(void)
910{
911 bus_perf_client = msm_bus_scale_register_client(&bus_client_pdata);
912 if (!bus_perf_client) {
913 pr_err("%s: unable register bus client\n", __func__);
914 BUG();
915 }
916}
917
918#ifdef CONFIG_CPU_FREQ_MSM
919static struct cpufreq_frequency_table freq_table[NR_CPUS][30];
920
921static void __init cpufreq_table_init(void)
922{
923 int cpu;
924
925 for_each_possible_cpu(cpu) {
926 int i, freq_cnt = 0;
927 /* Construct the freq_table tables from acpu_freq_tbl. */
928 for (i = 0; acpu_freq_tbl[i].acpuclk_khz != 0
929 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
930 if (acpu_freq_tbl[i].use_for_scaling[cpu]) {
931 freq_table[cpu][freq_cnt].index = freq_cnt;
932 freq_table[cpu][freq_cnt].frequency
933 = acpu_freq_tbl[i].acpuclk_khz;
934 freq_cnt++;
935 }
936 }
937 /* freq_table not big enough to store all usable freqs. */
938 BUG_ON(acpu_freq_tbl[i].acpuclk_khz != 0);
939
940 freq_table[cpu][freq_cnt].index = freq_cnt;
941 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
942
943 pr_info("CPU%d: %d scaling frequencies supported.\n",
944 cpu, freq_cnt);
945
946 /* Register table with CPUFreq. */
947 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
948 }
949}
950#else
951static void __init cpufreq_table_init(void) {}
952#endif
953
954#define HOT_UNPLUG_KHZ MAX_AXI
955static int __cpuinit acpuclock_cpu_callback(struct notifier_block *nfb,
956 unsigned long action, void *hcpu)
957{
958 static int prev_khz[NR_CPUS];
959 int cpu = (int)hcpu;
960
961 switch (action) {
962 case CPU_DEAD:
963 case CPU_DEAD_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700964 prev_khz[cpu] = acpuclk_8x60_get_rate(cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 /* Fall through. */
966 case CPU_UP_CANCELED:
967 case CPU_UP_CANCELED_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700968 acpuclk_8x60_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700969 break;
970 case CPU_UP_PREPARE:
971 case CPU_UP_PREPARE_FROZEN:
972 if (WARN_ON(!prev_khz[cpu]))
Stephen Boydf7e53c12011-12-19 16:37:15 -0800973 return NOTIFY_BAD;
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700974 acpuclk_8x60_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700975 break;
976 default:
977 break;
978 }
979
980 return NOTIFY_OK;
981}
982
983static struct notifier_block __cpuinitdata acpuclock_cpu_notifier = {
984 .notifier_call = acpuclock_cpu_callback,
985};
986
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700987static __init struct clkctl_acpu_speed *select_freq_plan(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988{
Matt Wagantall3b492ab2012-05-29 20:05:23 -0700989 uint32_t pte_efuse, speed_bin, pvs;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990 struct clkctl_acpu_speed *f;
991
992 pte_efuse = readl_relaxed(QFPROM_PTE_EFUSE_ADDR);
993
994 speed_bin = pte_efuse & 0xF;
995 if (speed_bin == 0xF)
996 speed_bin = (pte_efuse >> 4) & 0xF;
997
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700998 pvs = (pte_efuse >> 10) & 0x7;
999 if (pvs == 0x7)
1000 pvs = (pte_efuse >> 13) & 0x7;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001001
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001002 if (speed_bin == 0x2) {
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001003 switch (pvs) {
1004 case 0x7:
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001005 case 0x4:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001006 acpu_freq_tbl = acpu_freq_tbl_1674mhz_slower;
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001007 pr_info("ACPU PVS: Slower\n");
1008 break;
1009 case 0x0:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001010 acpu_freq_tbl = acpu_freq_tbl_1674mhz_slow;
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001011 pr_info("ACPU PVS: Slow\n");
1012 break;
1013 case 0x1:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001014 acpu_freq_tbl = acpu_freq_tbl_1674mhz_nom;
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001015 pr_info("ACPU PVS: Nominal\n");
1016 break;
1017 case 0x3:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001018 acpu_freq_tbl = acpu_freq_tbl_1674mhz_fast;
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001019 pr_info("ACPU PVS: Fast\n");
1020 break;
1021 default:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001022 acpu_freq_tbl = acpu_freq_tbl_1674mhz_slower;
Matt Wagantall0ac31752012-05-29 19:43:48 -07001023 pr_warn("ACPU PVS: Unknown. Defaulting to slower.\n");
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001024 break;
1025 }
1026 } else if (speed_bin == 0x1) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001027 switch (pvs) {
1028 case 0x0:
1029 case 0x7:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001030 acpu_freq_tbl = acpu_freq_tbl_1512mhz_slow;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001031 pr_info("ACPU PVS: Slow\n");
1032 break;
1033 case 0x1:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001034 acpu_freq_tbl = acpu_freq_tbl_1512mhz_nom;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001035 pr_info("ACPU PVS: Nominal\n");
1036 break;
1037 case 0x3:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001038 acpu_freq_tbl = acpu_freq_tbl_1512mhz_fast;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039 pr_info("ACPU PVS: Fast\n");
1040 break;
1041 default:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001042 acpu_freq_tbl = acpu_freq_tbl_1512mhz_slow;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043 pr_warn("ACPU PVS: Unknown. Defaulting to slow.\n");
1044 break;
1045 }
1046 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047 acpu_freq_tbl = acpu_freq_tbl_1188mhz;
1048 }
1049
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001050 for (f = acpu_freq_tbl; f->acpuclk_khz != 0; f++)
1051 ;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001052 f--;
1053 pr_info("Max ACPU freq: %u KHz\n", f->acpuclk_khz);
1054
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001055 return f;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056}
1057
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001058static struct acpuclk_data acpuclk_8x60_data = {
1059 .set_rate = acpuclk_8x60_set_rate,
1060 .get_rate = acpuclk_8x60_get_rate,
1061 .power_collapse_khz = MAX_AXI,
1062 .wait_for_irq_khz = MAX_AXI,
1063};
1064
Matt Wagantallec57f062011-08-16 23:54:46 -07001065static int __init acpuclk_8x60_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001066{
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001067 struct clkctl_acpu_speed *max_freq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 int cpu;
1069
1070 mutex_init(&drv_state.lock);
1071 spin_lock_init(&drv_state.l2_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001072
1073 /* Configure hardware. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001074 max_freq = select_freq_plan();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001075 unselect_scplls();
1076 scpll_set_refs();
1077 for_each_possible_cpu(cpu)
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001078 scpll_init(cpu, max_freq->l_val);
1079 scpll_init(L2, max_freq->l2_level->l_val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080 regulator_init();
1081 bus_init();
1082
1083 /* Improve boot time by ramping up CPUs immediately. */
1084 for_each_online_cpu(cpu)
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001085 acpuclk_8x60_set_rate(cpu, max_freq->acpuclk_khz, SETRATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001087 acpuclk_register(&acpuclk_8x60_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001088 cpufreq_table_init();
1089 register_hotcpu_notifier(&acpuclock_cpu_notifier);
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001090
1091 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092}
Matt Wagantallec57f062011-08-16 23:54:46 -07001093
1094struct acpuclk_soc_data acpuclk_8x60_soc_data __initdata = {
1095 .init = acpuclk_8x60_init,
1096};