blob: ef34b3c70b4fc4f361ce7c54b313c229a7c21310 [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>
Matt Wagantallbf430eb2012-03-22 11:45:49 -070014#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070015#include <linux/init.h>
16#include <linux/io.h>
17#include <linux/delay.h>
18#include <linux/mutex.h>
19#include <linux/spinlock.h>
20#include <linux/errno.h>
21#include <linux/cpufreq.h>
22#include <linux/cpu.h>
23#include <linux/regulator/consumer.h>
Matt Wagantallbf430eb2012-03-22 11:45:49 -070024#include <linux/platform_device.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025
26#include <asm/cpu.h>
27
28#include <mach/board.h>
29#include <mach/msm_iomap.h>
30#include <mach/msm_bus.h>
31#include <mach/msm_bus_board.h>
32#include <mach/socinfo.h>
33#include <mach/rpm-regulator.h>
34
35#include "acpuclock.h"
36#include "avs.h"
37
38/* Frequency switch modes. */
39#define SHOT_SWITCH 4
40#define HOP_SWITCH 5
41#define SIMPLE_SLEW 6
42#define COMPLEX_SLEW 7
43
44/* PLL calibration limits.
Matt Wagantall2ecbec22012-03-13 23:18:07 -070045 * The PLL hardware has a minimum frequency of 384MHz.
46 * Calibration should respect this limit. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047#define L_VAL_SCPLL_CAL_MIN 0x08 /* = 432 MHz with 27MHz source */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048
Matt Wagantall2ecbec22012-03-13 23:18:07 -070049#define MAX_VDD_SC 1325000 /* uV */
50#define MAX_VDD_MEM 1325000 /* uV */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051#define MAX_VDD_DIG 1200000 /* uV */
52#define MAX_AXI 310500 /* KHz */
53#define SCPLL_LOW_VDD_FMAX 594000 /* KHz */
54#define SCPLL_LOW_VDD 1000000 /* uV */
55#define SCPLL_NOMINAL_VDD 1100000 /* uV */
56
57/* SCPLL Modes. */
58#define SCPLL_POWER_DOWN 0
59#define SCPLL_BYPASS 1
60#define SCPLL_STANDBY 2
61#define SCPLL_FULL_CAL 4
62#define SCPLL_HALF_CAL 5
63#define SCPLL_STEP_CAL 6
64#define SCPLL_NORMAL 7
65
66#define SCPLL_DEBUG_NONE 0
67#define SCPLL_DEBUG_FULL 3
68
69/* SCPLL registers offsets. */
70#define SCPLL_DEBUG_OFFSET 0x0
71#define SCPLL_CTL_OFFSET 0x4
72#define SCPLL_CAL_OFFSET 0x8
73#define SCPLL_STATUS_OFFSET 0x10
74#define SCPLL_CFG_OFFSET 0x1C
75#define SCPLL_FSM_CTL_EXT_OFFSET 0x24
Matt Wagantall2ecbec22012-03-13 23:18:07 -070076#define SCPLL_LUT_OFFSET(l_val) (0x38 + (((l_val) / 4) * 4))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077
78/* Clock registers. */
79#define SPSS0_CLK_CTL_ADDR (MSM_ACC0_BASE + 0x04)
80#define SPSS0_CLK_SEL_ADDR (MSM_ACC0_BASE + 0x08)
81#define SPSS1_CLK_CTL_ADDR (MSM_ACC1_BASE + 0x04)
82#define SPSS1_CLK_SEL_ADDR (MSM_ACC1_BASE + 0x08)
83#define SPSS_L2_CLK_SEL_ADDR (MSM_GCC_BASE + 0x38)
84
85/* PTE EFUSE register. */
86#define QFPROM_PTE_EFUSE_ADDR (MSM_QFPROM_BASE + 0x00C0)
87
88static const void * const clk_ctl_addr[] = {SPSS0_CLK_CTL_ADDR,
89 SPSS1_CLK_CTL_ADDR};
90static const void * const clk_sel_addr[] = {SPSS0_CLK_SEL_ADDR,
91 SPSS1_CLK_SEL_ADDR, SPSS_L2_CLK_SEL_ADDR};
92
93static const int rpm_vreg_voter[] = { RPM_VREG_VOTER1, RPM_VREG_VOTER2 };
94static struct regulator *regulator_sc[NR_CPUS];
95
96enum scplls {
97 CPU0 = 0,
98 CPU1,
99 L2,
100};
101
102static const void * const sc_pll_base[] = {
103 [CPU0] = MSM_SCPLL_BASE + 0x200,
104 [CPU1] = MSM_SCPLL_BASE + 0x300,
105 [L2] = MSM_SCPLL_BASE + 0x400,
106};
107
108enum sc_src {
109 ACPU_AFAB,
110 ACPU_PLL_8,
111 ACPU_SCPLL,
112};
113
114static struct clock_state {
115 struct clkctl_acpu_speed *current_speed[NR_CPUS];
116 struct clkctl_l2_speed *current_l2_speed;
117 spinlock_t l2_lock;
118 struct mutex lock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700119} drv_state;
120
121struct clkctl_l2_speed {
122 unsigned int khz;
123 unsigned int src_sel;
124 unsigned int l_val;
125 unsigned int vdd_dig;
126 unsigned int vdd_mem;
127 unsigned int bw_level;
128};
129
130static struct clkctl_l2_speed *l2_vote[NR_CPUS];
131
132struct clkctl_acpu_speed {
133 unsigned int use_for_scaling[2]; /* One for each CPU. */
134 unsigned int acpuclk_khz;
135 int pll;
136 unsigned int acpuclk_src_sel;
137 unsigned int acpuclk_src_div;
138 unsigned int core_src_sel;
139 unsigned int l_val;
140 struct clkctl_l2_speed *l2_level;
141 unsigned int vdd_sc;
142 unsigned int avsdscr_setting;
143};
144
145/* Instantaneous bandwidth requests in MB/s. */
146#define BW_MBPS(_bw) \
147 { \
148 .vectors = &(struct msm_bus_vectors){ \
149 .src = MSM_BUS_MASTER_AMPSS_M0, \
150 .dst = MSM_BUS_SLAVE_EBI_CH0, \
151 .ib = (_bw) * 1000000UL, \
152 .ab = 0, \
153 }, \
154 .num_paths = 1, \
155 }
156static struct msm_bus_paths bw_level_tbl[] = {
157 [0] = BW_MBPS(824), /* At least 103 MHz on bus. */
158 [1] = BW_MBPS(1336), /* At least 167 MHz on bus. */
159 [2] = BW_MBPS(2008), /* At least 251 MHz on bus. */
160 [3] = BW_MBPS(2480), /* At least 310 MHz on bus. */
161};
162
163static struct msm_bus_scale_pdata bus_client_pdata = {
164 .usecase = bw_level_tbl,
165 .num_usecases = ARRAY_SIZE(bw_level_tbl),
166 .active_only = 1,
167 .name = "acpuclock",
168};
169
170static uint32_t bus_perf_client;
171
172/* L2 frequencies = 2 * 27 MHz * L_VAL */
173static struct clkctl_l2_speed l2_freq_tbl_v2[] = {
174 [0] = { MAX_AXI, 0, 0, 1000000, 1100000, 0},
175 [1] = { 432000, 1, 0x08, 1000000, 1100000, 0},
176 [2] = { 486000, 1, 0x09, 1000000, 1100000, 0},
177 [3] = { 540000, 1, 0x0A, 1000000, 1100000, 0},
178 [4] = { 594000, 1, 0x0B, 1000000, 1100000, 0},
179 [5] = { 648000, 1, 0x0C, 1000000, 1100000, 1},
180 [6] = { 702000, 1, 0x0D, 1100000, 1100000, 1},
181 [7] = { 756000, 1, 0x0E, 1100000, 1100000, 1},
182 [8] = { 810000, 1, 0x0F, 1100000, 1100000, 1},
183 [9] = { 864000, 1, 0x10, 1100000, 1100000, 1},
184 [10] = { 918000, 1, 0x11, 1100000, 1100000, 2},
185 [11] = { 972000, 1, 0x12, 1100000, 1100000, 2},
186 [12] = {1026000, 1, 0x13, 1100000, 1100000, 2},
187 [13] = {1080000, 1, 0x14, 1100000, 1200000, 2},
188 [14] = {1134000, 1, 0x15, 1100000, 1200000, 2},
189 [15] = {1188000, 1, 0x16, 1200000, 1200000, 3},
190 [16] = {1242000, 1, 0x17, 1200000, 1212500, 3},
191 [17] = {1296000, 1, 0x18, 1200000, 1225000, 3},
192 [18] = {1350000, 1, 0x19, 1200000, 1225000, 3},
193 [19] = {1404000, 1, 0x1A, 1200000, 1250000, 3},
194};
195
196#define L2(x) (&l2_freq_tbl_v2[(x)])
197/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
198static struct clkctl_acpu_speed acpu_freq_tbl_1188mhz[] = {
199 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 812500, 0x03006000},
200 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
201 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 875000, 0x03006000},
202 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 875000, 0x03006000},
203 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 887500, 0x03006000},
204 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 912500, 0x03006000},
205 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 925000, 0x03006000},
206 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 937500, 0x03006000},
207 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 950000, 0x03006000},
208 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 975000, 0x03006000},
209 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 1000000, 0x03006000},
210 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 1012500, 0x03006000},
211 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 1037500, 0x03006000},
212 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 1062500, 0x03006000},
213 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 1087500, 0x03006000},
214 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 1125000, 0x03006000},
215 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1137500, 0x03006000},
216 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1162500, 0x03006000},
217 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1187500, 0x03006000},
218 { {0, 0}, 0 },
219};
220
221/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
Matt Wagantall3b492ab2012-05-29 20:05:23 -0700222static struct clkctl_acpu_speed acpu_freq_tbl_1512mhz_slow[] = {
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700223 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 800000, 0x03006000},
224 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
225 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 825000, 0x03006000},
226 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 825000, 0x03006000},
227 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 850000, 0x03006000},
228 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 850000, 0x03006000},
229 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 875000, 0x03006000},
230 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 875000, 0x03006000},
231 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 900000, 0x03006000},
232 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 900000, 0x03006000},
233 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 925000, 0x03006000},
234 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 975000, 0x03006000},
235 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 975000, 0x03006000},
236 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 1000000, 0x03006000},
237 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 1025000, 0x03006000},
238 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 1025000, 0x03006000},
239 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1050000, 0x03006000},
240 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1075000, 0x03006000},
241 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1100000, 0x03006000},
242 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1125000, 0x03006000},
243 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1150000, 0x03006000},
244 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1150000, 0x03006000},
245 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1175000, 0x03006000},
246 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1200000, 0x03006000},
247 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1225000, 0x03006000},
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700248 { {0, 0}, 0 },
249};
250
251/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
Matt Wagantall3b492ab2012-05-29 20:05:23 -0700252static struct clkctl_acpu_speed acpu_freq_tbl_1512mhz_nom[] = {
Tianyi Gou66351ff2011-07-19 20:48:41 -0700253 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 800000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
Tianyi Gou66351ff2011-07-19 20:48:41 -0700255 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 825000, 0x03006000},
256 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 825000, 0x03006000},
257 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 850000, 0x03006000},
258 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 850000, 0x03006000},
259 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 875000, 0x03006000},
260 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 875000, 0x03006000},
261 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 900000, 0x03006000},
262 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 900000, 0x03006000},
263 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 925000, 0x03006000},
264 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 950000, 0x03006000},
265 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 975000, 0x03006000},
266 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 975000, 0x03006000},
267 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 1000000, 0x03006000},
268 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 1000000, 0x03006000},
269 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1025000, 0x03006000},
270 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1025000, 0x03006000},
271 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1050000, 0x03006000},
272 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1075000, 0x03006000},
273 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1100000, 0x03006000},
274 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1125000, 0x03006000},
275 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1150000, 0x03006000},
276 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1150000, 0x03006000},
277 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1175000, 0x03006000},
278 { {0, 0}, 0 },
279};
280
281/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
Matt Wagantall3b492ab2012-05-29 20:05:23 -0700282static struct clkctl_acpu_speed acpu_freq_tbl_1512mhz_fast[] = {
Tianyi Gou66351ff2011-07-19 20:48:41 -0700283 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 800000, 0x03006000},
284 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
285 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 825000, 0x03006000},
286 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 825000, 0x03006000},
287 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 850000, 0x03006000},
288 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 850000, 0x03006000},
289 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 875000, 0x03006000},
290 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 875000, 0x03006000},
291 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 900000, 0x03006000},
292 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 900000, 0x03006000},
293 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 925000, 0x03006000},
294 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 925000, 0x03006000},
295 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 950000, 0x03006000},
296 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 950000, 0x03006000},
297 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 950000, 0x03006000},
298 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 975000, 0x03006000},
299 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 1000000, 0x03006000},
300 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1000000, 0x03006000},
301 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1025000, 0x03006000},
302 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1050000, 0x03006000},
303 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1075000, 0x03006000},
304 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1100000, 0x03006000},
305 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1100000, 0x03006000},
306 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1100000, 0x03006000},
307 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1125000, 0x03006000},
Matt Wagantall3b492ab2012-05-29 20:05:23 -0700308 { {0, 0}, 0 },
309};
310
311/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
312static struct clkctl_acpu_speed acpu_freq_tbl_1674mhz_slower[] = {
313 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 775000, 0x03006000},
314 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
315 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 775000, 0x03006000},
316 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 775000, 0x03006000},
317 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 775000, 0x03006000},
318 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 775000, 0x03006000},
319 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 787500, 0x03006000},
320 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 800000, 0x03006000},
321 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 825000, 0x03006000},
322 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 837500, 0x03006000},
323 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 850000, 0x03006000},
324 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 875000, 0x03006000},
325 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 900000, 0x03006000},
326 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 912500, 0x03006000},
327 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 937500, 0x03006000},
328 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 962500, 0x03006000},
329 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 987500, 0x03006000},
330 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 1012500, 0x03006000},
331 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1025000, 0x03006000},
332 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1062500, 0x03006000},
333 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1087500, 0x03006000},
334 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1100000, 0x03006000},
335 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1125000, 0x03006000},
336 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1150000, 0x03006000},
337 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1187500, 0x03006000},
338 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1225000, 0x03006000},
339 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1262500, 0x03006000},
340 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1300000, 0x03006000},
341 { {0, 0}, 0 },
342};
343
344/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
345static struct clkctl_acpu_speed acpu_freq_tbl_1674mhz_slow[] = {
346 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 775000, 0x03006000},
347 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
348 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 775000, 0x03006000},
349 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 775000, 0x03006000},
350 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 775000, 0x03006000},
351 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 775000, 0x03006000},
352 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 787500, 0x03006000},
353 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 800000, 0x03006000},
354 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 825000, 0x03006000},
355 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 837500, 0x03006000},
356 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 850000, 0x03006000},
357 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 862500, 0x03006000},
358 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 887500, 0x03006000},
359 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 900000, 0x03006000},
360 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 925000, 0x03006000},
361 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 937500, 0x03006000},
362 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 962500, 0x03006000},
363 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 987500, 0x03006000},
364 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 1000000, 0x03006000},
365 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 1025000, 0x03006000},
366 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1050000, 0x03006000},
367 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1062500, 0x03006000},
368 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1087500, 0x03006000},
369 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1112500, 0x03006000},
370 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1150000, 0x03006000},
371 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1175000, 0x03006000},
372 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1212500, 0x03006000},
373 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1250000, 0x03006000},
374 { {0, 0}, 0 },
375};
376
377/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
378static struct clkctl_acpu_speed acpu_freq_tbl_1674mhz_nom[] = {
379 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 775000, 0x03006000},
380 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
381 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 775000, 0x03006000},
382 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 775000, 0x03006000},
383 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 775000, 0x03006000},
384 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 775000, 0x03006000},
385 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 787500, 0x03006000},
386 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 800000, 0x03006000},
387 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 812500, 0x03006000},
388 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 825000, 0x03006000},
389 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 837500, 0x03006000},
390 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 850000, 0x03006000},
391 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 875000, 0x03006000},
392 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 887500, 0x03006000},
393 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 900000, 0x03006000},
394 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 912500, 0x03006000},
395 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 937500, 0x03006000},
396 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 950000, 0x03006000},
397 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 975000, 0x03006000},
398 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 987500, 0x03006000},
399 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 1012500, 0x03006000},
400 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1025000, 0x03006000},
401 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1050000, 0x03006000},
402 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1075000, 0x03006000},
403 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1112500, 0x03006000},
404 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1137500, 0x03006000},
405 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1175000, 0x03006000},
406 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1200000, 0x03006000},
407 { {0, 0}, 0 },
408};
409
410/* SCPLL frequencies = 2 * 27 MHz * L_VAL */
411static struct clkctl_acpu_speed acpu_freq_tbl_1674mhz_fast[] = {
412 { {1, 1}, 192000, ACPU_PLL_8, 3, 1, 0, 0, L2(1), 775000, 0x03006000},
413 /* MAX_AXI row is used to source CPU cores and L2 from the AFAB clock. */
414 { {0, 0}, MAX_AXI, ACPU_AFAB, 1, 0, 0, 0, L2(0), 775000, 0x03006000},
415 { {1, 1}, 384000, ACPU_PLL_8, 3, 0, 0, 0, L2(1), 775000, 0x03006000},
416 { {1, 1}, 432000, ACPU_SCPLL, 0, 0, 1, 0x08, L2(1), 775000, 0x03006000},
417 { {1, 1}, 486000, ACPU_SCPLL, 0, 0, 1, 0x09, L2(2), 775000, 0x03006000},
418 { {1, 1}, 540000, ACPU_SCPLL, 0, 0, 1, 0x0A, L2(3), 775000, 0x03006000},
419 { {1, 1}, 594000, ACPU_SCPLL, 0, 0, 1, 0x0B, L2(4), 787500, 0x03006000},
420 { {1, 1}, 648000, ACPU_SCPLL, 0, 0, 1, 0x0C, L2(5), 800000, 0x03006000},
421 { {1, 1}, 702000, ACPU_SCPLL, 0, 0, 1, 0x0D, L2(6), 812500, 0x03006000},
422 { {1, 1}, 756000, ACPU_SCPLL, 0, 0, 1, 0x0E, L2(7), 825000, 0x03006000},
423 { {1, 1}, 810000, ACPU_SCPLL, 0, 0, 1, 0x0F, L2(8), 837500, 0x03006000},
424 { {1, 1}, 864000, ACPU_SCPLL, 0, 0, 1, 0x10, L2(9), 862500, 0x03006000},
425 { {1, 1}, 918000, ACPU_SCPLL, 0, 0, 1, 0x11, L2(10), 875000, 0x03006000},
426 { {1, 1}, 972000, ACPU_SCPLL, 0, 0, 1, 0x12, L2(11), 887500, 0x03006000},
427 { {1, 1}, 1026000, ACPU_SCPLL, 0, 0, 1, 0x13, L2(12), 900000, 0x03006000},
428 { {1, 1}, 1080000, ACPU_SCPLL, 0, 0, 1, 0x14, L2(13), 925000, 0x03006000},
429 { {1, 1}, 1134000, ACPU_SCPLL, 0, 0, 1, 0x15, L2(14), 937500, 0x03006000},
430 { {1, 1}, 1188000, ACPU_SCPLL, 0, 0, 1, 0x16, L2(15), 950000, 0x03006000},
431 { {1, 1}, 1242000, ACPU_SCPLL, 0, 0, 1, 0x17, L2(16), 962500, 0x03006000},
432 { {1, 1}, 1296000, ACPU_SCPLL, 0, 0, 1, 0x18, L2(17), 975000, 0x03006000},
433 { {1, 1}, 1350000, ACPU_SCPLL, 0, 0, 1, 0x19, L2(18), 1000000, 0x03006000},
434 { {1, 1}, 1404000, ACPU_SCPLL, 0, 0, 1, 0x1A, L2(19), 1025000, 0x03006000},
435 { {1, 1}, 1458000, ACPU_SCPLL, 0, 0, 1, 0x1B, L2(19), 1050000, 0x03006000},
436 { {1, 1}, 1512000, ACPU_SCPLL, 0, 0, 1, 0x1C, L2(19), 1075000, 0x03006000},
437 { {1, 1}, 1566000, ACPU_SCPLL, 0, 0, 1, 0x1D, L2(19), 1100000, 0x03006000},
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700438 { {1, 1}, 1620000, ACPU_SCPLL, 0, 0, 1, 0x1E, L2(19), 1125000, 0x03006000},
439 { {1, 1}, 1674000, ACPU_SCPLL, 0, 0, 1, 0x1F, L2(19), 1150000, 0x03006000},
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440 { {0, 0}, 0 },
441};
442
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443/* acpu_freq_tbl row to use when reconfiguring SC/L2 PLLs. */
444#define CAL_IDX 1
445
446static struct clkctl_acpu_speed *acpu_freq_tbl;
447static struct clkctl_l2_speed *l2_freq_tbl = l2_freq_tbl_v2;
448static unsigned int l2_freq_tbl_size = ARRAY_SIZE(l2_freq_tbl_v2);
449
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700450static unsigned long acpuclk_8x60_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451{
452 return drv_state.current_speed[cpu]->acpuclk_khz;
453}
454
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455static void select_core_source(unsigned int id, unsigned int src)
456{
457 uint32_t regval;
458 int shift;
459
460 shift = (id == L2) ? 0 : 1;
461 regval = readl_relaxed(clk_sel_addr[id]);
462 regval &= ~(0x3 << shift);
463 regval |= (src << shift);
464 writel_relaxed(regval, clk_sel_addr[id]);
465}
466
467static void select_clk_source_div(unsigned int id, struct clkctl_acpu_speed *s)
468{
469 uint32_t reg_clksel, reg_clkctl, src_sel;
470
471 /* Configure the PLL divider mux if we plan to use it. */
472 if (s->core_src_sel == 0) {
473
474 reg_clksel = readl_relaxed(clk_sel_addr[id]);
475
476 /* CLK_SEL_SRC1N0 (bank) bit. */
477 src_sel = reg_clksel & 1;
478
479 /* Program clock source and divider. */
480 reg_clkctl = readl_relaxed(clk_ctl_addr[id]);
481 reg_clkctl &= ~(0xFF << (8 * src_sel));
482 reg_clkctl |= s->acpuclk_src_sel << (4 + 8 * src_sel);
483 reg_clkctl |= s->acpuclk_src_div << (0 + 8 * src_sel);
484 writel_relaxed(reg_clkctl, clk_ctl_addr[id]);
485
486 /* Toggle clock source. */
487 reg_clksel ^= 1;
488
489 /* Program clock source selection. */
490 writel_relaxed(reg_clksel, clk_sel_addr[id]);
491 }
492}
493
494static void scpll_enable(int sc_pll, uint32_t l_val)
495{
496 uint32_t regval;
497
498 /* Power-up SCPLL into standby mode. */
499 writel_relaxed(SCPLL_STANDBY, sc_pll_base[sc_pll] + SCPLL_CTL_OFFSET);
500 mb();
501 udelay(10);
502
503 /* Shot-switch to target frequency. */
504 regval = (l_val << 3) | SHOT_SWITCH;
505 writel_relaxed(regval, sc_pll_base[sc_pll] + SCPLL_FSM_CTL_EXT_OFFSET);
506 writel_relaxed(SCPLL_NORMAL, sc_pll_base[sc_pll] + SCPLL_CTL_OFFSET);
507 mb();
508 udelay(20);
509}
510
511static void scpll_disable(int sc_pll)
512{
513 /* Power down SCPLL. */
514 writel_relaxed(SCPLL_POWER_DOWN,
515 sc_pll_base[sc_pll] + SCPLL_CTL_OFFSET);
516}
517
518static void scpll_change_freq(int sc_pll, uint32_t l_val)
519{
520 uint32_t regval;
521 const void *base_addr = sc_pll_base[sc_pll];
522
523 /* Complex-slew switch to target frequency. */
524 regval = (l_val << 3) | COMPLEX_SLEW;
525 writel_relaxed(regval, base_addr + SCPLL_FSM_CTL_EXT_OFFSET);
526 writel_relaxed(SCPLL_NORMAL, base_addr + SCPLL_CTL_OFFSET);
527
528 /* Wait for frequency switch to start. */
529 while (((readl_relaxed(base_addr + SCPLL_CTL_OFFSET) >> 3) & 0x3F)
530 != l_val)
531 cpu_relax();
532 /* Wait for frequency switch to finish. */
533 while (readl_relaxed(base_addr + SCPLL_STATUS_OFFSET) & 0x1)
534 cpu_relax();
535}
536
537/* Vote for the L2 speed and return the speed that should be applied. */
538static struct clkctl_l2_speed *compute_l2_speed(unsigned int voting_cpu,
539 struct clkctl_l2_speed *tgt_s)
540{
541 struct clkctl_l2_speed *new_s;
542 int cpu;
543
544 /* Bounds check. */
545 BUG_ON(tgt_s >= (l2_freq_tbl + l2_freq_tbl_size));
546
547 /* Find max L2 speed vote. */
548 l2_vote[voting_cpu] = tgt_s;
549 new_s = l2_freq_tbl;
550 for_each_present_cpu(cpu)
551 new_s = max(new_s, l2_vote[cpu]);
552
553 return new_s;
554}
555
556/* Set the L2's clock speed. */
557static void set_l2_speed(struct clkctl_l2_speed *tgt_s)
558{
559 if (tgt_s == drv_state.current_l2_speed)
560 return;
561
562 if (drv_state.current_l2_speed->src_sel == 1
563 && tgt_s->src_sel == 1)
564 scpll_change_freq(L2, tgt_s->l_val);
565 else {
566 if (tgt_s->src_sel == 1) {
567 scpll_enable(L2, tgt_s->l_val);
568 mb();
569 select_core_source(L2, tgt_s->src_sel);
570 } else {
571 select_core_source(L2, tgt_s->src_sel);
572 mb();
573 scpll_disable(L2);
574 }
575 }
576 drv_state.current_l2_speed = tgt_s;
577}
578
579/* Update the bus bandwidth request. */
580static void set_bus_bw(unsigned int bw)
581{
582 int ret;
583
584 /* Bounds check. */
585 if (bw >= ARRAY_SIZE(bw_level_tbl)) {
586 pr_err("%s: invalid bandwidth request (%d)\n", __func__, bw);
587 return;
588 }
589
590 /* Update bandwidth if requst has changed. This may sleep. */
591 ret = msm_bus_scale_client_update_request(bus_perf_client, bw);
592 if (ret)
593 pr_err("%s: bandwidth request failed (%d)\n", __func__, ret);
594
595 return;
596}
597
598/* Apply any per-cpu voltage increases. */
599static int increase_vdd(int cpu, unsigned int vdd_sc, unsigned int vdd_mem,
600 unsigned int vdd_dig, enum setrate_reason reason)
601{
602 int rc = 0;
603
604 /* Increase vdd_mem active-set before vdd_dig and vdd_sc.
605 * vdd_mem should be >= both vdd_sc and vdd_dig. */
606 rc = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S0, rpm_vreg_voter[cpu],
607 vdd_mem, MAX_VDD_MEM, 0);
608 if (rc) {
609 pr_err("%s: vdd_mem (cpu%d) increase failed (%d)\n",
610 __func__, cpu, rc);
611 return rc;
612 }
613
614 /* Increase vdd_dig active-set vote. */
615 rc = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S1, rpm_vreg_voter[cpu],
616 vdd_dig, MAX_VDD_DIG, 0);
617 if (rc) {
618 pr_err("%s: vdd_dig (cpu%d) increase failed (%d)\n",
619 __func__, cpu, rc);
620 return rc;
621 }
622
623 /* Don't update the Scorpion voltage in the hotplug path. It should
624 * already be correct. Attempting to set it is bad because we don't
625 * know what CPU we are running on at this point, but the Scorpion
626 * regulator API requires we call it from the affected CPU. */
627 if (reason == SETRATE_HOTPLUG)
628 return rc;
629
630 /* Update per-core Scorpion voltage. */
631 rc = regulator_set_voltage(regulator_sc[cpu], vdd_sc, MAX_VDD_SC);
632 if (rc) {
633 pr_err("%s: vdd_sc (cpu%d) increase failed (%d)\n",
634 __func__, cpu, rc);
635 return rc;
636 }
637
638 return rc;
639}
640
641/* Apply any per-cpu voltage decreases. */
642static void decrease_vdd(int cpu, unsigned int vdd_sc, unsigned int vdd_mem,
643 unsigned int vdd_dig, enum setrate_reason reason)
644{
645 int ret;
646
647 /* Update per-core Scorpion voltage. This must be called on the CPU
648 * that's being affected. Don't do this in the hotplug remove path,
649 * where the rail is off and we're executing on the other CPU. */
650 if (reason != SETRATE_HOTPLUG) {
651 ret = regulator_set_voltage(regulator_sc[cpu], vdd_sc,
652 MAX_VDD_SC);
653 if (ret) {
654 pr_err("%s: vdd_sc (cpu%d) decrease failed (%d)\n",
655 __func__, cpu, ret);
656 return;
657 }
658 }
659
660 /* Decrease vdd_dig active-set vote. */
661 ret = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S1, rpm_vreg_voter[cpu],
662 vdd_dig, MAX_VDD_DIG, 0);
663 if (ret) {
664 pr_err("%s: vdd_dig (cpu%d) decrease failed (%d)\n",
665 __func__, cpu, ret);
666 return;
667 }
668
669 /* Decrease vdd_mem active-set after vdd_dig and vdd_sc.
670 * vdd_mem should be >= both vdd_sc and vdd_dig. */
671 ret = rpm_vreg_set_voltage(RPM_VREG_ID_PM8058_S0, rpm_vreg_voter[cpu],
672 vdd_mem, MAX_VDD_MEM, 0);
673 if (ret) {
674 pr_err("%s: vdd_mem (cpu%d) decrease failed (%d)\n",
675 __func__, cpu, ret);
676 return;
677 }
678}
679
680static void switch_sc_speed(int cpu, struct clkctl_acpu_speed *tgt_s)
681{
682 struct clkctl_acpu_speed *strt_s = drv_state.current_speed[cpu];
683
684 if (strt_s->pll != ACPU_SCPLL && tgt_s->pll != ACPU_SCPLL) {
685 select_clk_source_div(cpu, tgt_s);
686 /* Select core source because target may be AFAB. */
687 select_core_source(cpu, tgt_s->core_src_sel);
688 } else if (strt_s->pll != ACPU_SCPLL && tgt_s->pll == ACPU_SCPLL) {
689 scpll_enable(cpu, tgt_s->l_val);
690 mb();
691 select_core_source(cpu, tgt_s->core_src_sel);
692 } else if (strt_s->pll == ACPU_SCPLL && tgt_s->pll != ACPU_SCPLL) {
693 select_clk_source_div(cpu, tgt_s);
694 select_core_source(cpu, tgt_s->core_src_sel);
695 /* Core source switch must complete before disabling SCPLL. */
696 mb();
697 udelay(1);
698 scpll_disable(cpu);
699 } else
700 scpll_change_freq(cpu, tgt_s->l_val);
701
702 /* Update the driver state with the new clock freq */
703 drv_state.current_speed[cpu] = tgt_s;
704}
705
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700706static int acpuclk_8x60_set_rate(int cpu, unsigned long rate,
707 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708{
709 struct clkctl_acpu_speed *tgt_s, *strt_s;
710 struct clkctl_l2_speed *tgt_l2;
711 unsigned int vdd_mem, vdd_dig, pll_vdd_dig;
712 unsigned long flags;
713 int rc = 0;
714
715 if (cpu > num_possible_cpus()) {
716 rc = -EINVAL;
717 goto out;
718 }
719
720 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
721 mutex_lock(&drv_state.lock);
722
723 strt_s = drv_state.current_speed[cpu];
724
725 /* Return early if rate didn't change. */
726 if (rate == strt_s->acpuclk_khz)
727 goto out;
728
729 /* Find target frequency. */
730 for (tgt_s = acpu_freq_tbl; tgt_s->acpuclk_khz != 0; tgt_s++)
731 if (tgt_s->acpuclk_khz == rate)
732 break;
733 if (tgt_s->acpuclk_khz == 0) {
734 rc = -EINVAL;
735 goto out;
736 }
737
738 /* AVS needs SAW_VCTL to be intitialized correctly, before enable,
Matt Wagantallbf430eb2012-03-22 11:45:49 -0700739 * and is not initialized during probe.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 */
741 if (reason == SETRATE_CPUFREQ)
742 AVS_DISABLE(cpu);
743
744 /* Calculate vdd_mem and vdd_dig requirements.
745 * vdd_mem must be >= vdd_sc */
746 vdd_mem = max(tgt_s->vdd_sc, tgt_s->l2_level->vdd_mem);
747 /* Factor-in PLL vdd_dig requirements. */
748 if ((tgt_s->l2_level->khz > SCPLL_LOW_VDD_FMAX) ||
749 (tgt_s->pll == ACPU_SCPLL
750 && tgt_s->acpuclk_khz > SCPLL_LOW_VDD_FMAX))
751 pll_vdd_dig = SCPLL_NOMINAL_VDD;
752 else
753 pll_vdd_dig = SCPLL_LOW_VDD;
754 vdd_dig = max(tgt_s->l2_level->vdd_dig, pll_vdd_dig);
755
756 /* Increase VDD levels if needed. */
757 if ((reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG
758 || reason == SETRATE_INIT)
759 && (tgt_s->acpuclk_khz > strt_s->acpuclk_khz)) {
760 rc = increase_vdd(cpu, tgt_s->vdd_sc, vdd_mem, vdd_dig, reason);
761 if (rc)
762 goto out;
763 }
764
765 pr_debug("Switching from ACPU%d rate %u KHz -> %u KHz\n",
766 cpu, strt_s->acpuclk_khz, tgt_s->acpuclk_khz);
767
768 /* Switch CPU speed. */
769 switch_sc_speed(cpu, tgt_s);
770
771 /* Update the L2 vote and apply the rate change. */
772 spin_lock_irqsave(&drv_state.l2_lock, flags);
773 tgt_l2 = compute_l2_speed(cpu, tgt_s->l2_level);
774 set_l2_speed(tgt_l2);
775 spin_unlock_irqrestore(&drv_state.l2_lock, flags);
776
777 /* Nothing else to do for SWFI. */
778 if (reason == SETRATE_SWFI)
779 goto out;
780
781 /* Nothing else to do for power collapse. */
782 if (reason == SETRATE_PC)
783 goto out;
784
785 /* Update bus bandwith request. */
786 set_bus_bw(tgt_l2->bw_level);
787
788 /* Drop VDD levels if we can. */
789 if (tgt_s->acpuclk_khz < strt_s->acpuclk_khz)
790 decrease_vdd(cpu, tgt_s->vdd_sc, vdd_mem, vdd_dig, reason);
791
792 pr_debug("ACPU%d speed change complete\n", cpu);
793
794 /* Re-enable AVS */
795 if (reason == SETRATE_CPUFREQ)
796 AVS_ENABLE(cpu, tgt_s->avsdscr_setting);
797
798out:
799 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
800 mutex_unlock(&drv_state.lock);
801 return rc;
802}
803
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700804static void __init scpll_init(int pll, unsigned int max_l_val)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805{
806 uint32_t regval;
807
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700808 pr_debug("Initializing SCPLL%d\n", pll);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700809
810 /* Clear calibration LUT registers containing max frequency entry.
811 * LUT registers are only writeable in debug mode. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700812 writel_relaxed(SCPLL_DEBUG_FULL, sc_pll_base[pll] + SCPLL_DEBUG_OFFSET);
813 writel_relaxed(0x0, sc_pll_base[pll] + SCPLL_LUT_OFFSET(max_l_val));
814 writel_relaxed(SCPLL_DEBUG_NONE, sc_pll_base[pll] + SCPLL_DEBUG_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815
816 /* Power-up SCPLL into standby mode. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700817 writel_relaxed(SCPLL_STANDBY, sc_pll_base[pll] + SCPLL_CTL_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818 mb();
819 udelay(10);
820
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700821 /* Calibrate the SCPLL for the frequency range needed. */
822 regval = (max_l_val << 24) | (L_VAL_SCPLL_CAL_MIN << 16);
823 writel_relaxed(regval, sc_pll_base[pll] + SCPLL_CAL_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824
825 /* Start calibration */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700826 writel_relaxed(SCPLL_FULL_CAL, sc_pll_base[pll] + SCPLL_CTL_OFFSET);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827
828 /* Wait for proof that calibration has started before checking the
829 * 'calibration done' bit in the status register. Waiting for the
830 * LUT register we cleared to contain data accomplishes this.
831 * This is required since the 'calibration done' bit takes time to
832 * transition from 'done' to 'not done' when starting a calibration.
833 */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700834 while (!readl_relaxed(sc_pll_base[pll] + SCPLL_LUT_OFFSET(max_l_val)))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835 cpu_relax();
836
837 /* Wait for calibration to complete. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700838 while (readl_relaxed(sc_pll_base[pll] + SCPLL_STATUS_OFFSET) & 0x2)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700839 cpu_relax();
840
841 /* Power-down SCPLL. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700842 scpll_disable(pll);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843}
844
845/* Force ACPU core and L2 cache clocks to rates that don't require SCPLLs. */
846static void __init unselect_scplls(void)
847{
848 int cpu;
849
850 /* Ensure CAL_IDX frequency uses AFAB sources for CPU cores and L2. */
851 BUG_ON(acpu_freq_tbl[CAL_IDX].core_src_sel != 0);
852 BUG_ON(acpu_freq_tbl[CAL_IDX].l2_level->src_sel != 0);
853
854 for_each_possible_cpu(cpu) {
855 select_clk_source_div(cpu, &acpu_freq_tbl[CAL_IDX]);
856 select_core_source(cpu, acpu_freq_tbl[CAL_IDX].core_src_sel);
857 drv_state.current_speed[cpu] = &acpu_freq_tbl[CAL_IDX];
858 l2_vote[cpu] = acpu_freq_tbl[CAL_IDX].l2_level;
859 }
860
861 select_core_source(L2, acpu_freq_tbl[CAL_IDX].l2_level->src_sel);
862 drv_state.current_l2_speed = acpu_freq_tbl[CAL_IDX].l2_level;
863}
864
865/* Ensure SCPLLs use the 27MHz PXO. */
866static void __init scpll_set_refs(void)
867{
868 int cpu;
869 uint32_t regval;
870
871 /* Bit 4 = 0:PXO, 1:MXO. */
872 for_each_possible_cpu(cpu) {
873 regval = readl_relaxed(sc_pll_base[cpu] + SCPLL_CFG_OFFSET);
874 regval &= ~BIT(4);
875 writel_relaxed(regval, sc_pll_base[cpu] + SCPLL_CFG_OFFSET);
876 }
877 regval = readl_relaxed(sc_pll_base[L2] + SCPLL_CFG_OFFSET);
878 regval &= ~BIT(4);
879 writel_relaxed(regval, sc_pll_base[L2] + SCPLL_CFG_OFFSET);
880}
881
882/* Voltage regulator initialization. */
883static void __init regulator_init(void)
884{
885 struct clkctl_acpu_speed **freq = drv_state.current_speed;
886 const char *regulator_sc_name[] = {"8901_s0", "8901_s1"};
887 int cpu, ret;
888
889 for_each_possible_cpu(cpu) {
890 /* VDD_SC0, VDD_SC1 */
891 regulator_sc[cpu] = regulator_get(NULL, regulator_sc_name[cpu]);
892 if (IS_ERR(regulator_sc[cpu]))
893 goto err;
894 ret = regulator_set_voltage(regulator_sc[cpu],
895 freq[cpu]->vdd_sc, MAX_VDD_SC);
896 if (ret)
897 goto err;
898 ret = regulator_enable(regulator_sc[cpu]);
899 if (ret)
900 goto err;
901 }
902
903 return;
904
905err:
906 pr_err("%s: Failed to initialize voltage regulators\n", __func__);
907 BUG();
908}
909
910/* Register with bus driver. */
911static void __init bus_init(void)
912{
913 bus_perf_client = msm_bus_scale_register_client(&bus_client_pdata);
914 if (!bus_perf_client) {
915 pr_err("%s: unable register bus client\n", __func__);
916 BUG();
917 }
918}
919
920#ifdef CONFIG_CPU_FREQ_MSM
921static struct cpufreq_frequency_table freq_table[NR_CPUS][30];
922
923static void __init cpufreq_table_init(void)
924{
925 int cpu;
926
927 for_each_possible_cpu(cpu) {
928 int i, freq_cnt = 0;
929 /* Construct the freq_table tables from acpu_freq_tbl. */
930 for (i = 0; acpu_freq_tbl[i].acpuclk_khz != 0
931 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
932 if (acpu_freq_tbl[i].use_for_scaling[cpu]) {
933 freq_table[cpu][freq_cnt].index = freq_cnt;
934 freq_table[cpu][freq_cnt].frequency
935 = acpu_freq_tbl[i].acpuclk_khz;
936 freq_cnt++;
937 }
938 }
939 /* freq_table not big enough to store all usable freqs. */
940 BUG_ON(acpu_freq_tbl[i].acpuclk_khz != 0);
941
942 freq_table[cpu][freq_cnt].index = freq_cnt;
943 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
944
945 pr_info("CPU%d: %d scaling frequencies supported.\n",
946 cpu, freq_cnt);
947
948 /* Register table with CPUFreq. */
949 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
950 }
951}
952#else
953static void __init cpufreq_table_init(void) {}
954#endif
955
956#define HOT_UNPLUG_KHZ MAX_AXI
957static int __cpuinit acpuclock_cpu_callback(struct notifier_block *nfb,
958 unsigned long action, void *hcpu)
959{
960 static int prev_khz[NR_CPUS];
961 int cpu = (int)hcpu;
962
963 switch (action) {
964 case CPU_DEAD:
965 case CPU_DEAD_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700966 prev_khz[cpu] = acpuclk_8x60_get_rate(cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967 /* Fall through. */
968 case CPU_UP_CANCELED:
969 case CPU_UP_CANCELED_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700970 acpuclk_8x60_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971 break;
972 case CPU_UP_PREPARE:
973 case CPU_UP_PREPARE_FROZEN:
974 if (WARN_ON(!prev_khz[cpu]))
Stephen Boydf7e53c12011-12-19 16:37:15 -0800975 return NOTIFY_BAD;
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700976 acpuclk_8x60_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700977 break;
978 default:
979 break;
980 }
981
982 return NOTIFY_OK;
983}
984
985static struct notifier_block __cpuinitdata acpuclock_cpu_notifier = {
986 .notifier_call = acpuclock_cpu_callback,
987};
988
Matt Wagantall2ecbec22012-03-13 23:18:07 -0700989static __init struct clkctl_acpu_speed *select_freq_plan(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990{
Matt Wagantall3b492ab2012-05-29 20:05:23 -0700991 uint32_t pte_efuse, speed_bin, pvs;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992 struct clkctl_acpu_speed *f;
993
994 pte_efuse = readl_relaxed(QFPROM_PTE_EFUSE_ADDR);
995
996 speed_bin = pte_efuse & 0xF;
997 if (speed_bin == 0xF)
998 speed_bin = (pte_efuse >> 4) & 0xF;
999
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001000 pvs = (pte_efuse >> 10) & 0x7;
1001 if (pvs == 0x7)
1002 pvs = (pte_efuse >> 13) & 0x7;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001004 if (speed_bin == 0x2) {
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001005 switch (pvs) {
1006 case 0x7:
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001007 case 0x4:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001008 acpu_freq_tbl = acpu_freq_tbl_1674mhz_slower;
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001009 pr_info("ACPU PVS: Slower\n");
1010 break;
1011 case 0x0:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001012 acpu_freq_tbl = acpu_freq_tbl_1674mhz_slow;
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001013 pr_info("ACPU PVS: Slow\n");
1014 break;
1015 case 0x1:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001016 acpu_freq_tbl = acpu_freq_tbl_1674mhz_nom;
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001017 pr_info("ACPU PVS: Nominal\n");
1018 break;
1019 case 0x3:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001020 acpu_freq_tbl = acpu_freq_tbl_1674mhz_fast;
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001021 pr_info("ACPU PVS: Fast\n");
1022 break;
1023 default:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001024 acpu_freq_tbl = acpu_freq_tbl_1674mhz_slower;
Matt Wagantall0ac31752012-05-29 19:43:48 -07001025 pr_warn("ACPU PVS: Unknown. Defaulting to slower.\n");
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001026 break;
1027 }
1028 } else if (speed_bin == 0x1) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001029 switch (pvs) {
1030 case 0x0:
1031 case 0x7:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001032 acpu_freq_tbl = acpu_freq_tbl_1512mhz_slow;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001033 pr_info("ACPU PVS: Slow\n");
1034 break;
1035 case 0x1:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001036 acpu_freq_tbl = acpu_freq_tbl_1512mhz_nom;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001037 pr_info("ACPU PVS: Nominal\n");
1038 break;
1039 case 0x3:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001040 acpu_freq_tbl = acpu_freq_tbl_1512mhz_fast;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041 pr_info("ACPU PVS: Fast\n");
1042 break;
1043 default:
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001044 acpu_freq_tbl = acpu_freq_tbl_1512mhz_slow;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045 pr_warn("ACPU PVS: Unknown. Defaulting to slow.\n");
1046 break;
1047 }
1048 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049 acpu_freq_tbl = acpu_freq_tbl_1188mhz;
1050 }
1051
Matt Wagantall3b492ab2012-05-29 20:05:23 -07001052 for (f = acpu_freq_tbl; f->acpuclk_khz != 0; f++)
1053 ;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001054 f--;
1055 pr_info("Max ACPU freq: %u KHz\n", f->acpuclk_khz);
1056
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001057 return f;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001058}
1059
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001060static struct acpuclk_data acpuclk_8x60_data = {
1061 .set_rate = acpuclk_8x60_set_rate,
1062 .get_rate = acpuclk_8x60_get_rate,
1063 .power_collapse_khz = MAX_AXI,
1064 .wait_for_irq_khz = MAX_AXI,
1065};
1066
Matt Wagantallbf430eb2012-03-22 11:45:49 -07001067static int __init acpuclk_8x60_probe(struct platform_device *pdev)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068{
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001069 struct clkctl_acpu_speed *max_freq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001070 int cpu;
1071
1072 mutex_init(&drv_state.lock);
1073 spin_lock_init(&drv_state.l2_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074
1075 /* Configure hardware. */
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001076 max_freq = select_freq_plan();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077 unselect_scplls();
1078 scpll_set_refs();
1079 for_each_possible_cpu(cpu)
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001080 scpll_init(cpu, max_freq->l_val);
1081 scpll_init(L2, max_freq->l2_level->l_val);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082 regulator_init();
1083 bus_init();
1084
1085 /* Improve boot time by ramping up CPUs immediately. */
1086 for_each_online_cpu(cpu)
Matt Wagantall2ecbec22012-03-13 23:18:07 -07001087 acpuclk_8x60_set_rate(cpu, max_freq->acpuclk_khz, SETRATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001088
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001089 acpuclk_register(&acpuclk_8x60_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 cpufreq_table_init();
1091 register_hotcpu_notifier(&acpuclock_cpu_notifier);
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001092
1093 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094}
Matt Wagantallec57f062011-08-16 23:54:46 -07001095
Matt Wagantallbf430eb2012-03-22 11:45:49 -07001096static struct platform_driver acpuclk_8x60_driver = {
1097 .driver = {
1098 .name = "acpuclk-8x60",
1099 .owner = THIS_MODULE,
1100 },
Matt Wagantallec57f062011-08-16 23:54:46 -07001101};
Matt Wagantallbf430eb2012-03-22 11:45:49 -07001102
1103static int __init acpuclk_8x60_init(void)
1104{
1105 return platform_driver_probe(&acpuclk_8x60_driver, acpuclk_8x60_probe);
1106}
1107device_initcall(acpuclk_8x60_init);