blob: 070ce7151c4ef88d76b53128e7a0ac96d66f31e5 [file] [log] [blame]
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001/*
2 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/io.h>
19#include <linux/delay.h>
20#include <linux/mutex.h>
21#include <linux/err.h>
22#include <linux/errno.h>
23#include <linux/cpufreq.h>
24#include <linux/cpu.h>
25#include <linux/regulator/consumer.h>
26
27#include <asm/mach-types.h>
28#include <asm/cpu.h>
29
30#include <mach/board.h>
31#include <mach/msm_iomap.h>
32#include <mach/socinfo.h>
33#include <mach/msm-krait-l2-accessors.h>
34#include <mach/rpm-regulator.h>
Matt Wagantall75473eb2012-05-31 15:23:22 -070035#include <mach/rpm-regulator-smd.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080036#include <mach/msm_bus.h>
37
38#include "acpuclock.h"
39#include "acpuclock-krait.h"
40
41/* MUX source selects. */
42#define PRI_SRC_SEL_SEC_SRC 0
43#define PRI_SRC_SEL_HFPLL 1
44#define PRI_SRC_SEL_HFPLL_DIV2 2
45#define SEC_SRC_SEL_QSB 0
46#define SEC_SRC_SEL_L2PLL 1
47#define SEC_SRC_SEL_AUX 2
48
49/* PTE EFUSE register offset. */
50#define PTE_EFUSE 0xC0
51
52static DEFINE_MUTEX(driver_lock);
53static DEFINE_SPINLOCK(l2_lock);
54
55static struct drv_data {
Matt Wagantall06e4a1f2012-06-07 18:38:13 -070056 struct acpu_level *acpu_freq_tbl;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080057 const struct l2_level *l2_freq_tbl;
58 struct scalable *scalable;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070059 struct hfpll_data *hfpll_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080060 u32 bus_perf_client;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070061 struct msm_bus_scale_pdata *bus_scale;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080062 struct device *dev;
63} drv;
64
65static unsigned long acpuclk_krait_get_rate(int cpu)
66{
67 return drv.scalable[cpu].cur_speed->khz;
68}
69
70/* Select a source on the primary MUX. */
71static void set_pri_clk_src(struct scalable *sc, u32 pri_src_sel)
72{
73 u32 regval;
74
75 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
76 regval &= ~0x3;
77 regval |= (pri_src_sel & 0x3);
78 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
79 /* Wait for switch to complete. */
80 mb();
81 udelay(1);
82}
83
84/* Select a source on the secondary MUX. */
85static void set_sec_clk_src(struct scalable *sc, u32 sec_src_sel)
86{
87 u32 regval;
88
89 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
90 regval &= ~(0x3 << 2);
91 regval |= ((sec_src_sel & 0x3) << 2);
92 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
93 /* Wait for switch to complete. */
94 mb();
95 udelay(1);
96}
97
Matt Wagantall302d9a32012-07-03 13:37:29 -070098static int enable_rpm_vreg(struct vreg *vreg)
Matt Wagantalle9b715a2012-01-04 18:16:14 -080099{
Matt Wagantall302d9a32012-07-03 13:37:29 -0700100 int ret = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800101
Matt Wagantall75473eb2012-05-31 15:23:22 -0700102 if (vreg->rpm_reg) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700103 ret = rpm_regulator_enable(vreg->rpm_reg);
104 if (ret)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700105 dev_err(drv.dev, "%s regulator enable failed (%d)\n",
Matt Wagantall302d9a32012-07-03 13:37:29 -0700106 vreg->name, ret);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700107 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700108
109 return ret;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700110}
111
112static void disable_rpm_vreg(struct vreg *vreg)
113{
114 int rc;
115
116 if (vreg->rpm_reg) {
117 rc = rpm_regulator_disable(vreg->rpm_reg);
118 if (rc)
119 dev_err(drv.dev, "%s regulator disable failed (%d)\n",
120 vreg->name, rc);
121 }
122}
123
124/* Enable an already-configured HFPLL. */
125static void hfpll_enable(struct scalable *sc, bool skip_regulators)
126{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800127 if (!skip_regulators) {
128 /* Enable regulators required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700129 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
130 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800131 }
132
133 /* Disable PLL bypass mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700134 writel_relaxed(0x2, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800135
136 /*
137 * H/W requires a 5us delay between disabling the bypass and
138 * de-asserting the reset. Delay 10us just to be safe.
139 */
140 mb();
141 udelay(10);
142
143 /* De-assert active-low PLL reset. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700144 writel_relaxed(0x6, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800145
146 /* Wait for PLL to lock. */
147 mb();
148 udelay(60);
149
150 /* Enable PLL output. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700151 writel_relaxed(0x7, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800152}
153
154/* Disable a HFPLL for power-savings or while it's being reprogrammed. */
155static void hfpll_disable(struct scalable *sc, bool skip_regulators)
156{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800157 /*
158 * Disable the PLL output, disable test mode, enable the bypass mode,
159 * and assert the reset.
160 */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700161 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800162
163 if (!skip_regulators) {
164 /* Remove voltage votes required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700165 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
166 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800167 }
168}
169
170/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
171static void hfpll_set_rate(struct scalable *sc, const struct core_speed *tgt_s)
172{
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700173 void __iomem *base = sc->hfpll_base;
174 u32 regval;
175
176 writel_relaxed(tgt_s->pll_l_val, base + drv.hfpll_data->l_offset);
177
178 if (drv.hfpll_data->has_user_reg) {
179 regval = readl_relaxed(base + drv.hfpll_data->user_offset);
180 if (tgt_s->pll_l_val <= drv.hfpll_data->low_vco_l_max)
181 regval &= ~drv.hfpll_data->user_vco_mask;
182 else
183 regval |= drv.hfpll_data->user_vco_mask;
184 writel_relaxed(regval, base + drv.hfpll_data->user_offset);
185 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800186}
187
188/* Return the L2 speed that should be applied. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700189static unsigned int compute_l2_level(struct scalable *sc, unsigned int vote_l)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800190{
Matt Wagantall600ea502012-06-08 18:49:53 -0700191 unsigned int new_l = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800192 int cpu;
193
194 /* Find max L2 speed vote. */
195 sc->l2_vote = vote_l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800196 for_each_present_cpu(cpu)
197 new_l = max(new_l, drv.scalable[cpu].l2_vote);
198
199 return new_l;
200}
201
202/* Update the bus bandwidth request. */
203static void set_bus_bw(unsigned int bw)
204{
205 int ret;
206
207 /* Update bandwidth if request has changed. This may sleep. */
208 ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw);
209 if (ret)
210 dev_err(drv.dev, "bandwidth request failed (%d)\n", ret);
211}
212
213/* Set the CPU or L2 clock speed. */
214static void set_speed(struct scalable *sc, const struct core_speed *tgt_s)
215{
216 const struct core_speed *strt_s = sc->cur_speed;
217
218 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
219 /*
220 * Move to an always-on source running at a frequency
221 * that does not require an elevated CPU voltage.
222 */
223 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
224 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
225
226 /* Re-program HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700227 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800228 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700229 hfpll_enable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800230
231 /* Move to HFPLL. */
232 set_pri_clk_src(sc, tgt_s->pri_src_sel);
233 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
234 set_sec_clk_src(sc, tgt_s->sec_src_sel);
235 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700236 hfpll_disable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800237 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
238 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700239 hfpll_enable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800240 set_pri_clk_src(sc, tgt_s->pri_src_sel);
241 } else {
242 set_sec_clk_src(sc, tgt_s->sec_src_sel);
243 }
244
245 sc->cur_speed = tgt_s;
246}
247
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700248struct vdd_data {
249 int vdd_mem;
250 int vdd_dig;
251 int vdd_core;
252 int ua_core;
253};
254
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800255/* Apply any per-cpu voltage increases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700256static int increase_vdd(int cpu, struct vdd_data *data,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800257 enum setrate_reason reason)
258{
259 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700260 int rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800261
262 /*
263 * Increase vdd_mem active-set before vdd_dig.
264 * vdd_mem should be >= vdd_dig.
265 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700266 if (data->vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700267 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700268 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800269 if (rc) {
270 dev_err(drv.dev,
271 "vdd_mem (cpu%d) increase failed (%d)\n",
272 cpu, rc);
273 return rc;
274 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700275 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800276 }
277
278 /* Increase vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700279 if (data->vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700280 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700281 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800282 if (rc) {
283 dev_err(drv.dev,
284 "vdd_dig (cpu%d) increase failed (%d)\n",
285 cpu, rc);
286 return rc;
287 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700288 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
289 }
290
291 /* Increase current request. */
292 if (data->ua_core > sc->vreg[VREG_CORE].cur_ua) {
293 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
294 data->ua_core);
295 if (rc < 0) {
296 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
297 sc->vreg[VREG_CORE].name, rc);
298 return rc;
299 }
300 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800301 }
302
303 /*
304 * Update per-CPU core voltage. Don't do this for the hotplug path for
305 * which it should already be correct. Attempting to set it is bad
306 * because we don't know what CPU we are running on at this point, but
307 * the CPU regulator API requires we call it from the affected CPU.
308 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700309 if (data->vdd_core > sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800310 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700311 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
312 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800313 if (rc) {
314 dev_err(drv.dev,
315 "vdd_core (cpu%d) increase failed (%d)\n",
316 cpu, rc);
317 return rc;
318 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700319 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800320 }
321
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700322 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800323}
324
325/* Apply any per-cpu voltage decreases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700326static void decrease_vdd(int cpu, struct vdd_data *data,
327 enum setrate_reason reason)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800328{
329 struct scalable *sc = &drv.scalable[cpu];
330 int ret;
331
332 /*
333 * Update per-CPU core voltage. This must be called on the CPU
334 * that's being affected. Don't do this in the hotplug remove path,
335 * where the rail is off and we're executing on the other CPU.
336 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700337 if (data->vdd_core < sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800338 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700339 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
340 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800341 if (ret) {
342 dev_err(drv.dev,
343 "vdd_core (cpu%d) decrease failed (%d)\n",
344 cpu, ret);
345 return;
346 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700347 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
348 }
349
350 /* Decrease current request. */
351 if (data->ua_core < sc->vreg[VREG_CORE].cur_ua) {
352 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
353 data->ua_core);
354 if (ret < 0) {
355 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
356 sc->vreg[VREG_CORE].name, ret);
357 return;
358 }
359 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800360 }
361
362 /* Decrease vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700363 if (data->vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700364 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700365 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800366 if (ret) {
367 dev_err(drv.dev,
368 "vdd_dig (cpu%d) decrease failed (%d)\n",
369 cpu, ret);
370 return;
371 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700372 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800373 }
374
375 /*
376 * Decrease vdd_mem active-set after vdd_dig.
377 * vdd_mem should be >= vdd_dig.
378 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700379 if (data->vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700380 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700381 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800382 if (ret) {
383 dev_err(drv.dev,
384 "vdd_mem (cpu%d) decrease failed (%d)\n",
385 cpu, ret);
386 return;
387 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700388 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800389 }
390}
391
392static int calculate_vdd_mem(const struct acpu_level *tgt)
393{
Matt Wagantall600ea502012-06-08 18:49:53 -0700394 return drv.l2_freq_tbl[tgt->l2_level].vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800395}
396
Matt Wagantall72a38002012-07-18 13:42:55 -0700397static int get_src_dig(const struct core_speed *s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800398{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700399 const int *hfpll_vdd = drv.hfpll_data->vdd;
400 const u32 low_vdd_l_max = drv.hfpll_data->low_vdd_l_max;
Matt Wagantall87465f52012-07-23 22:03:06 -0700401 const u32 nom_vdd_l_max = drv.hfpll_data->nom_vdd_l_max;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800402
Matt Wagantall72a38002012-07-18 13:42:55 -0700403 if (s->src != HFPLL)
404 return hfpll_vdd[HFPLL_VDD_NONE];
Matt Wagantall87465f52012-07-23 22:03:06 -0700405 else if (s->pll_l_val > nom_vdd_l_max)
406 return hfpll_vdd[HFPLL_VDD_HIGH];
Matt Wagantall72a38002012-07-18 13:42:55 -0700407 else if (s->pll_l_val > low_vdd_l_max)
408 return hfpll_vdd[HFPLL_VDD_NOM];
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800409 else
Matt Wagantall72a38002012-07-18 13:42:55 -0700410 return hfpll_vdd[HFPLL_VDD_LOW];
411}
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800412
Matt Wagantall72a38002012-07-18 13:42:55 -0700413static int calculate_vdd_dig(const struct acpu_level *tgt)
414{
415 int l2_pll_vdd_dig, cpu_pll_vdd_dig;
416
417 l2_pll_vdd_dig = get_src_dig(&drv.l2_freq_tbl[tgt->l2_level].speed);
418 cpu_pll_vdd_dig = get_src_dig(&tgt->speed);
419
420 return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig,
421 max(l2_pll_vdd_dig, cpu_pll_vdd_dig));
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800422}
423
424static int calculate_vdd_core(const struct acpu_level *tgt)
425{
426 return tgt->vdd_core;
427}
428
429/* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */
430static int acpuclk_krait_set_rate(int cpu, unsigned long rate,
431 enum setrate_reason reason)
432{
433 const struct core_speed *strt_acpu_s, *tgt_acpu_s;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800434 const struct acpu_level *tgt;
Matt Wagantall600ea502012-06-08 18:49:53 -0700435 int tgt_l2_l;
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700436 struct vdd_data vdd_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800437 unsigned long flags;
438 int rc = 0;
439
Matt Wagantall5941a332012-07-10 23:20:44 -0700440 if (cpu > num_possible_cpus())
441 return -EINVAL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800442
443 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
444 mutex_lock(&driver_lock);
445
446 strt_acpu_s = drv.scalable[cpu].cur_speed;
447
448 /* Return early if rate didn't change. */
449 if (rate == strt_acpu_s->khz)
450 goto out;
451
452 /* Find target frequency. */
453 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
454 if (tgt->speed.khz == rate) {
455 tgt_acpu_s = &tgt->speed;
456 break;
457 }
458 }
459 if (tgt->speed.khz == 0) {
460 rc = -EINVAL;
461 goto out;
462 }
463
464 /* Calculate voltage requirements for the current CPU. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700465 vdd_data.vdd_mem = calculate_vdd_mem(tgt);
466 vdd_data.vdd_dig = calculate_vdd_dig(tgt);
467 vdd_data.vdd_core = calculate_vdd_core(tgt);
468 vdd_data.ua_core = tgt->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800469
470 /* Increase VDD levels if needed. */
471 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700472 rc = increase_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800473 if (rc)
474 goto out;
475 }
476
477 pr_debug("Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
478 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
479
480 /* Set the new CPU speed. */
481 set_speed(&drv.scalable[cpu], tgt_acpu_s);
482
483 /*
484 * Update the L2 vote and apply the rate change. A spinlock is
485 * necessary to ensure L2 rate is calculated and set atomically
486 * with the CPU frequency, even if acpuclk_krait_set_rate() is
487 * called from an atomic context and the driver_lock mutex is not
488 * acquired.
489 */
490 spin_lock_irqsave(&l2_lock, flags);
491 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
Matt Wagantall600ea502012-06-08 18:49:53 -0700492 set_speed(&drv.scalable[L2], &drv.l2_freq_tbl[tgt_l2_l].speed);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800493 spin_unlock_irqrestore(&l2_lock, flags);
494
495 /* Nothing else to do for power collapse or SWFI. */
496 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
497 goto out;
498
499 /* Update bus bandwith request. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700500 set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800501
502 /* Drop VDD levels if we can. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700503 decrease_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800504
505 pr_debug("ACPU%d speed change complete\n", cpu);
506
507out:
508 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
509 mutex_unlock(&driver_lock);
510 return rc;
511}
512
513/* Initialize a HFPLL at a given rate and enable it. */
514static void __init hfpll_init(struct scalable *sc,
515 const struct core_speed *tgt_s)
516{
517 pr_debug("Initializing HFPLL%d\n", sc - drv.scalable);
518
519 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700520 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800521
522 /* Configure PLL parameters for integer mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700523 writel_relaxed(drv.hfpll_data->config_val,
524 sc->hfpll_base + drv.hfpll_data->config_offset);
525 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->m_offset);
526 writel_relaxed(1, sc->hfpll_base + drv.hfpll_data->n_offset);
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700527 if (drv.hfpll_data->has_user_reg)
528 writel_relaxed(drv.hfpll_data->user_val,
529 sc->hfpll_base + drv.hfpll_data->user_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800530
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700531 /* Program droop controller, if supported */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700532 if (drv.hfpll_data->has_droop_ctl)
533 writel_relaxed(drv.hfpll_data->droop_val,
534 sc->hfpll_base + drv.hfpll_data->droop_offset);
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700535
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800536 /* Set an initial rate and enable the PLL. */
537 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700538 hfpll_enable(sc, false);
539}
540
Matt Wagantall302d9a32012-07-03 13:37:29 -0700541static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700542 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700543{
544 int ret;
545
546 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700547 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700548
549 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
550 sc->vreg[vreg].name);
551 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700552 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
553 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
554 sc->vreg[vreg].name, ret);
555 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700556 }
557
558 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
559 sc->vreg[vreg].max_vdd);
560 if (ret) {
561 dev_err(drv.dev, "%s initialization failed (%d)\n",
562 sc->vreg[vreg].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700563 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700564 }
565 sc->vreg[vreg].cur_vdd = vdd;
566
Matt Wagantall302d9a32012-07-03 13:37:29 -0700567 if (enable) {
568 ret = enable_rpm_vreg(&sc->vreg[vreg]);
569 if (ret)
570 goto err_conf;
571 }
572
573 return 0;
574
575err_conf:
576 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
577err_get:
578 return ret;
579}
580
581static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
582 enum vregs vreg)
583{
584 if (!sc->vreg[vreg].rpm_reg)
585 return;
586
587 disable_rpm_vreg(&sc->vreg[vreg]);
588 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800589}
590
591/* Voltage regulator initialization. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700592static int __cpuinit regulator_init(struct scalable *sc,
593 const struct acpu_level *acpu_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800594{
Matt Wagantall754ee272012-06-18 13:40:26 -0700595 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800596
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700597 vdd_mem = calculate_vdd_mem(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700598 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
599 if (ret)
600 goto err_mem;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700601
602 vdd_dig = calculate_vdd_dig(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700603 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
604 if (ret)
605 goto err_dig;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700606
Matt Wagantall302d9a32012-07-03 13:37:29 -0700607 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700608 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700609 if (ret)
610 goto err_hfpll_a;
611 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700612 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700613 if (ret)
614 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700615
Matt Wagantall754ee272012-06-18 13:40:26 -0700616 /* Setup Krait CPU regulators and initial core voltage. */
617 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
618 sc->vreg[VREG_CORE].name);
619 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700620 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
621 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
622 sc->vreg[VREG_CORE].name, ret);
623 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700624 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700625 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
626 acpu_level->ua_core);
627 if (ret < 0) {
628 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
629 sc->vreg[VREG_CORE].name, ret);
630 goto err_core_conf;
631 }
632 sc->vreg[VREG_CORE].cur_ua = acpu_level->ua_core;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700633 vdd_core = calculate_vdd_core(acpu_level);
Matt Wagantall754ee272012-06-18 13:40:26 -0700634 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
635 sc->vreg[VREG_CORE].max_vdd);
636 if (ret) {
637 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
638 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700639 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700640 }
641 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
Matt Wagantall754ee272012-06-18 13:40:26 -0700642 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
643 if (ret) {
644 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
645 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700646 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800647 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700648
649 return 0;
650
651err_core_conf:
652 regulator_put(sc->vreg[VREG_CORE].reg);
653err_core_get:
654 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
655err_hfpll_b:
656 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
657err_hfpll_a:
658 rpm_regulator_cleanup(sc, VREG_DIG);
659err_dig:
660 rpm_regulator_cleanup(sc, VREG_MEM);
661err_mem:
662 return ret;
663}
664
665static void __cpuinit regulator_cleanup(struct scalable *sc)
666{
667 regulator_disable(sc->vreg[VREG_CORE].reg);
668 regulator_put(sc->vreg[VREG_CORE].reg);
669 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
670 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
671 rpm_regulator_cleanup(sc, VREG_DIG);
672 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800673}
674
675/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700676static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700677 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800678{
679 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700680 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800681
682 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700683 if (sc->aux_clk_sel_phys) {
684 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700685 if (!aux_reg)
686 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700687 writel_relaxed(sc->aux_clk_sel, aux_reg);
688 iounmap(aux_reg);
689 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800690
691 /* Switch away from the HFPLL while it's re-initialized. */
692 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
693 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
694 hfpll_init(sc, tgt_s);
695
696 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
697 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
698 regval &= ~(0x3 << 6);
699 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
700
701 /* Switch to the target clock source. */
702 set_sec_clk_src(sc, tgt_s->sec_src_sel);
703 set_pri_clk_src(sc, tgt_s->pri_src_sel);
704 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700705
706 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800707}
708
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700709static void __cpuinit fill_cur_core_speed(struct core_speed *s,
710 struct scalable *sc)
711{
712 s->pri_src_sel = get_l2_indirect_reg(sc->l2cpmr_iaddr) & 0x3;
713 s->sec_src_sel = (get_l2_indirect_reg(sc->l2cpmr_iaddr) >> 2) & 0x3;
714 s->pll_l_val = readl_relaxed(sc->hfpll_base + drv.hfpll_data->l_offset);
715}
716
717static bool __cpuinit speed_equal(const struct core_speed *s1,
718 const struct core_speed *s2)
719{
720 return (s1->pri_src_sel == s2->pri_src_sel &&
721 s1->sec_src_sel == s2->sec_src_sel &&
722 s1->pll_l_val == s2->pll_l_val);
723}
724
725static const struct acpu_level __cpuinit *find_cur_acpu_level(int cpu)
726{
727 struct scalable *sc = &drv.scalable[cpu];
728 const struct acpu_level *l;
729 struct core_speed cur_speed;
730
731 fill_cur_core_speed(&cur_speed, sc);
732 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
733 if (speed_equal(&l->speed, &cur_speed))
734 return l;
735 return NULL;
736}
737
738static const struct l2_level __init *find_cur_l2_level(void)
739{
740 struct scalable *sc = &drv.scalable[L2];
741 const struct l2_level *l;
742 struct core_speed cur_speed;
743
744 fill_cur_core_speed(&cur_speed, sc);
745 for (l = drv.l2_freq_tbl; l->speed.khz != 0; l++)
746 if (speed_equal(&l->speed, &cur_speed))
747 return l;
748 return NULL;
749}
750
751static const struct acpu_level __cpuinit *find_min_acpu_level(void)
752{
753 struct acpu_level *l;
754
755 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
756 if (l->use_for_scaling)
757 return l;
758
759 return NULL;
760}
761
Matt Wagantall302d9a32012-07-03 13:37:29 -0700762static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800763{
Matt Wagantall754ee272012-06-18 13:40:26 -0700764 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700765 const struct acpu_level *acpu_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700766 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800767
Matt Wagantall754ee272012-06-18 13:40:26 -0700768 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700769 if (!sc->hfpll_base) {
770 ret = -ENOMEM;
771 goto err_ioremap;
772 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700773
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700774 acpu_level = find_cur_acpu_level(cpu);
775 if (!acpu_level || acpu_level->speed.src == QSB) {
776 acpu_level = find_min_acpu_level();
777 if (!acpu_level) {
778 ret = -ENODEV;
779 goto err_table;
780 }
781 dev_dbg(drv.dev, "CPU%d is running at an unknown rate. Defaulting to %lu KHz.\n",
782 cpu, acpu_level->speed.khz);
783 } else {
784 dev_dbg(drv.dev, "CPU%d is running at %lu KHz\n", cpu,
785 acpu_level->speed.khz);
786 }
787
788 ret = regulator_init(sc, acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700789 if (ret)
790 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700791
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700792 ret = init_clock_sources(sc, &acpu_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700793 if (ret)
794 goto err_clocks;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700795
796 sc->l2_vote = acpu_level->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700797 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700798
799 return 0;
800
801err_clocks:
802 regulator_cleanup(sc);
803err_regulators:
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700804err_table:
Matt Wagantall302d9a32012-07-03 13:37:29 -0700805 iounmap(sc->hfpll_base);
806err_ioremap:
807 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800808}
809
810/* Register with bus driver. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700811static void __init bus_init(const struct l2_level *l2_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800812{
813 int ret;
814
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700815 drv.bus_perf_client = msm_bus_scale_register_client(drv.bus_scale);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800816 if (!drv.bus_perf_client) {
817 dev_err(drv.dev, "unable to register bus client\n");
818 BUG();
819 }
820
Matt Wagantall754ee272012-06-18 13:40:26 -0700821 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700822 l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800823 if (ret)
824 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
825}
826
827#ifdef CONFIG_CPU_FREQ_MSM
828static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
829
830static void __init cpufreq_table_init(void)
831{
832 int cpu;
833
834 for_each_possible_cpu(cpu) {
835 int i, freq_cnt = 0;
836 /* Construct the freq_table tables from acpu_freq_tbl. */
837 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
838 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
839 if (drv.acpu_freq_tbl[i].use_for_scaling) {
840 freq_table[cpu][freq_cnt].index = freq_cnt;
841 freq_table[cpu][freq_cnt].frequency
842 = drv.acpu_freq_tbl[i].speed.khz;
843 freq_cnt++;
844 }
845 }
846 /* freq_table not big enough to store all usable freqs. */
847 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
848
849 freq_table[cpu][freq_cnt].index = freq_cnt;
850 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
851
852 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
853 cpu, freq_cnt);
854
855 /* Register table with CPUFreq. */
856 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
857 }
858}
859#else
860static void __init cpufreq_table_init(void) {}
861#endif
862
863#define HOT_UNPLUG_KHZ STBY_KHZ
864static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
865 unsigned long action, void *hcpu)
866{
867 static int prev_khz[NR_CPUS];
868 int rc, cpu = (int)hcpu;
869 struct scalable *sc = &drv.scalable[cpu];
870
871 switch (action & ~CPU_TASKS_FROZEN) {
872 case CPU_DEAD:
873 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
874 /* Fall through. */
875 case CPU_UP_CANCELED:
876 acpuclk_krait_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
877 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
878 break;
879 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700880 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700881 rc = per_cpu_init(cpu);
882 if (rc)
883 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700884 break;
885 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800886 if (WARN_ON(!prev_khz[cpu]))
887 return NOTIFY_BAD;
888 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700889 sc->vreg[VREG_CORE].cur_ua);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800890 if (rc < 0)
891 return NOTIFY_BAD;
892 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
893 break;
894 default:
895 break;
896 }
897
898 return NOTIFY_OK;
899}
900
901static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
902 .notifier_call = acpuclk_cpu_callback,
903};
904
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700905static const int krait_needs_vmin(void)
906{
907 switch (read_cpuid_id()) {
908 case 0x511F04D0: /* KR28M2A20 */
909 case 0x511F04D1: /* KR28M2A21 */
910 case 0x510F06F0: /* KR28M4A10 */
911 return 1;
912 default:
913 return 0;
914 };
915}
916
917static void krait_apply_vmin(struct acpu_level *tbl)
918{
919 for (; tbl->speed.khz != 0; tbl++)
920 if (tbl->vdd_core < 1150000)
921 tbl->vdd_core = 1150000;
922}
923
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700924static int __init select_freq_plan(u32 qfprom_phys)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800925{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800926 void __iomem *qfprom_base;
927 u32 pte_efuse, pvs, tbl_idx;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700928 char *pvs_names[] = { "Slow", "Nominal", "Fast", "Faster", "Unknown" };
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800929
930 qfprom_base = ioremap(qfprom_phys, SZ_256);
931 /* Select frequency tables. */
932 if (qfprom_base) {
933 pte_efuse = readl_relaxed(qfprom_base + PTE_EFUSE);
934 pvs = (pte_efuse >> 10) & 0x7;
935 iounmap(qfprom_base);
936 if (pvs == 0x7)
937 pvs = (pte_efuse >> 13) & 0x7;
938
939 switch (pvs) {
940 case 0x0:
941 case 0x7:
942 tbl_idx = PVS_SLOW;
943 break;
944 case 0x1:
945 tbl_idx = PVS_NOMINAL;
946 break;
947 case 0x3:
948 tbl_idx = PVS_FAST;
949 break;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700950 case 0x4:
951 tbl_idx = PVS_FASTER;
952 break;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800953 default:
954 tbl_idx = PVS_UNKNOWN;
955 break;
956 }
957 } else {
958 tbl_idx = PVS_UNKNOWN;
959 dev_err(drv.dev, "Unable to map QFPROM base\n");
960 }
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700961 if (tbl_idx == PVS_UNKNOWN) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800962 tbl_idx = PVS_SLOW;
963 dev_warn(drv.dev, "ACPU PVS: Defaulting to %s\n",
964 pvs_names[tbl_idx]);
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700965 } else {
966 dev_info(drv.dev, "ACPU PVS: %s\n", pvs_names[tbl_idx]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800967 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800968
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700969 return tbl_idx;
970}
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700971
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800972static struct acpuclk_data acpuclk_krait_data = {
973 .set_rate = acpuclk_krait_set_rate,
974 .get_rate = acpuclk_krait_get_rate,
975 .power_collapse_khz = STBY_KHZ,
976 .wait_for_irq_khz = STBY_KHZ,
977};
978
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700979static void __init drv_data_init(struct device *dev,
980 const struct acpuclk_krait_params *params)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800981{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700982 int tbl_idx;
983
984 drv.dev = dev;
985 drv.scalable = kmemdup(params->scalable, params->scalable_size,
986 GFP_KERNEL);
987 BUG_ON(!drv.scalable);
988
989 drv.hfpll_data = kmemdup(params->hfpll_data, sizeof(*drv.hfpll_data),
990 GFP_KERNEL);
991 BUG_ON(!drv.hfpll_data);
992
993 drv.l2_freq_tbl = kmemdup(params->l2_freq_tbl, params->l2_freq_tbl_size,
994 GFP_KERNEL);
995 BUG_ON(!drv.l2_freq_tbl);
996
997 drv.bus_scale = kmemdup(params->bus_scale, sizeof(*drv.bus_scale),
998 GFP_KERNEL);
999 BUG_ON(!drv.bus_scale);
1000 drv.bus_scale->usecase = kmemdup(drv.bus_scale->usecase,
1001 drv.bus_scale->num_usecases * sizeof(*drv.bus_scale->usecase),
1002 GFP_KERNEL);
1003 BUG_ON(!drv.bus_scale->usecase);
1004
1005 tbl_idx = select_freq_plan(params->qfprom_phys_base);
1006 drv.acpu_freq_tbl = kmemdup(params->pvs_tables[tbl_idx].table,
1007 params->pvs_tables[tbl_idx].size,
1008 GFP_KERNEL);
1009 BUG_ON(!drv.acpu_freq_tbl);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001010}
1011
1012static void __init hw_init(void)
1013{
1014 struct scalable *l2 = &drv.scalable[L2];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001015 const struct l2_level *l2_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -07001016 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001017
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001018 if (krait_needs_vmin())
1019 krait_apply_vmin(drv.acpu_freq_tbl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001020
Matt Wagantall754ee272012-06-18 13:40:26 -07001021 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
1022 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -07001023
Matt Wagantall302d9a32012-07-03 13:37:29 -07001024 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
1025 l2->vreg[VREG_HFPLL_A].max_vdd, false);
1026 BUG_ON(rc);
1027 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
1028 l2->vreg[VREG_HFPLL_B].max_vdd, false);
1029 BUG_ON(rc);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001030
1031 l2_level = find_cur_l2_level();
1032 if (!l2_level || l2_level->speed.src == QSB) {
1033 l2_level = drv.l2_freq_tbl;
1034 dev_dbg(drv.dev, "L2 is running at an unknown rate. Defaulting to QSB.\n");
1035 } else {
1036 dev_dbg(drv.dev, "L2 is running at %lu KHz\n",
1037 l2_level->speed.khz);
1038 }
1039
1040 rc = init_clock_sources(l2, &l2_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -07001041 BUG_ON(rc);
1042
1043 for_each_online_cpu(cpu) {
1044 rc = per_cpu_init(cpu);
1045 BUG_ON(rc);
1046 }
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001047
1048 bus_init(l2_level);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001049}
1050
1051int __init acpuclk_krait_init(struct device *dev,
1052 const struct acpuclk_krait_params *params)
1053{
1054 drv_data_init(dev, params);
1055 hw_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001056
1057 cpufreq_table_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001058 acpuclk_register(&acpuclk_krait_data);
1059 register_hotcpu_notifier(&acpuclk_cpu_notifier);
1060
1061 return 0;
1062}