blob: 9afa7c057126e94923f15bd91833605d34bcf6af [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
Matt Wagantalle9b715a2012-01-04 18:16:14 -080014#include <linux/kernel.h>
Matt Wagantall9515bc22012-07-19 18:13:40 -070015#include <linux/module.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080016#include <linux/init.h>
17#include <linux/io.h>
18#include <linux/delay.h>
19#include <linux/mutex.h>
20#include <linux/err.h>
21#include <linux/errno.h>
22#include <linux/cpufreq.h>
23#include <linux/cpu.h>
24#include <linux/regulator/consumer.h>
25
26#include <asm/mach-types.h>
27#include <asm/cpu.h>
28
29#include <mach/board.h>
30#include <mach/msm_iomap.h>
31#include <mach/socinfo.h>
32#include <mach/msm-krait-l2-accessors.h>
33#include <mach/rpm-regulator.h>
Matt Wagantall75473eb2012-05-31 15:23:22 -070034#include <mach/rpm-regulator-smd.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080035#include <mach/msm_bus.h>
36
37#include "acpuclock.h"
38#include "acpuclock-krait.h"
39
40/* MUX source selects. */
41#define PRI_SRC_SEL_SEC_SRC 0
42#define PRI_SRC_SEL_HFPLL 1
43#define PRI_SRC_SEL_HFPLL_DIV2 2
Matt Wagantalle9b715a2012-01-04 18:16:14 -080044#define SEC_SRC_SEL_L2PLL 1
45#define SEC_SRC_SEL_AUX 2
46
47/* PTE EFUSE register offset. */
48#define PTE_EFUSE 0xC0
49
50static DEFINE_MUTEX(driver_lock);
51static DEFINE_SPINLOCK(l2_lock);
52
53static struct drv_data {
Matt Wagantall06e4a1f2012-06-07 18:38:13 -070054 struct acpu_level *acpu_freq_tbl;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080055 const struct l2_level *l2_freq_tbl;
56 struct scalable *scalable;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070057 struct hfpll_data *hfpll_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080058 u32 bus_perf_client;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070059 struct msm_bus_scale_pdata *bus_scale;
Matt Wagantall9515bc22012-07-19 18:13:40 -070060 int boost_uv;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080061 struct device *dev;
62} drv;
63
64static unsigned long acpuclk_krait_get_rate(int cpu)
65{
66 return drv.scalable[cpu].cur_speed->khz;
67}
68
69/* Select a source on the primary MUX. */
70static void set_pri_clk_src(struct scalable *sc, u32 pri_src_sel)
71{
72 u32 regval;
73
74 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
75 regval &= ~0x3;
76 regval |= (pri_src_sel & 0x3);
77 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
78 /* Wait for switch to complete. */
79 mb();
80 udelay(1);
81}
82
83/* Select a source on the secondary MUX. */
84static void set_sec_clk_src(struct scalable *sc, u32 sec_src_sel)
85{
86 u32 regval;
87
88 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
89 regval &= ~(0x3 << 2);
90 regval |= ((sec_src_sel & 0x3) << 2);
91 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
92 /* Wait for switch to complete. */
93 mb();
94 udelay(1);
95}
96
Matt Wagantall302d9a32012-07-03 13:37:29 -070097static int enable_rpm_vreg(struct vreg *vreg)
Matt Wagantalle9b715a2012-01-04 18:16:14 -080098{
Matt Wagantall302d9a32012-07-03 13:37:29 -070099 int ret = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800100
Matt Wagantall75473eb2012-05-31 15:23:22 -0700101 if (vreg->rpm_reg) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700102 ret = rpm_regulator_enable(vreg->rpm_reg);
103 if (ret)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700104 dev_err(drv.dev, "%s regulator enable failed (%d)\n",
Matt Wagantall302d9a32012-07-03 13:37:29 -0700105 vreg->name, ret);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700106 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700107
108 return ret;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700109}
110
111static void disable_rpm_vreg(struct vreg *vreg)
112{
113 int rc;
114
115 if (vreg->rpm_reg) {
116 rc = rpm_regulator_disable(vreg->rpm_reg);
117 if (rc)
118 dev_err(drv.dev, "%s regulator disable failed (%d)\n",
119 vreg->name, rc);
120 }
121}
122
123/* Enable an already-configured HFPLL. */
124static void hfpll_enable(struct scalable *sc, bool skip_regulators)
125{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800126 if (!skip_regulators) {
127 /* Enable regulators required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700128 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
129 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800130 }
131
132 /* Disable PLL bypass mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700133 writel_relaxed(0x2, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800134
135 /*
136 * H/W requires a 5us delay between disabling the bypass and
137 * de-asserting the reset. Delay 10us just to be safe.
138 */
139 mb();
140 udelay(10);
141
142 /* De-assert active-low PLL reset. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700143 writel_relaxed(0x6, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800144
145 /* Wait for PLL to lock. */
146 mb();
147 udelay(60);
148
149 /* Enable PLL output. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700150 writel_relaxed(0x7, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800151}
152
153/* Disable a HFPLL for power-savings or while it's being reprogrammed. */
154static void hfpll_disable(struct scalable *sc, bool skip_regulators)
155{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800156 /*
157 * Disable the PLL output, disable test mode, enable the bypass mode,
158 * and assert the reset.
159 */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700160 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800161
162 if (!skip_regulators) {
163 /* Remove voltage votes required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700164 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
165 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800166 }
167}
168
169/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
170static void hfpll_set_rate(struct scalable *sc, const struct core_speed *tgt_s)
171{
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700172 void __iomem *base = sc->hfpll_base;
173 u32 regval;
174
175 writel_relaxed(tgt_s->pll_l_val, base + drv.hfpll_data->l_offset);
176
177 if (drv.hfpll_data->has_user_reg) {
178 regval = readl_relaxed(base + drv.hfpll_data->user_offset);
179 if (tgt_s->pll_l_val <= drv.hfpll_data->low_vco_l_max)
180 regval &= ~drv.hfpll_data->user_vco_mask;
181 else
182 regval |= drv.hfpll_data->user_vco_mask;
183 writel_relaxed(regval, base + drv.hfpll_data->user_offset);
184 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800185}
186
187/* Return the L2 speed that should be applied. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700188static unsigned int compute_l2_level(struct scalable *sc, unsigned int vote_l)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800189{
Matt Wagantall600ea502012-06-08 18:49:53 -0700190 unsigned int new_l = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800191 int cpu;
192
193 /* Find max L2 speed vote. */
194 sc->l2_vote = vote_l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800195 for_each_present_cpu(cpu)
196 new_l = max(new_l, drv.scalable[cpu].l2_vote);
197
198 return new_l;
199}
200
201/* Update the bus bandwidth request. */
202static void set_bus_bw(unsigned int bw)
203{
204 int ret;
205
206 /* Update bandwidth if request has changed. This may sleep. */
207 ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw);
208 if (ret)
209 dev_err(drv.dev, "bandwidth request failed (%d)\n", ret);
210}
211
212/* Set the CPU or L2 clock speed. */
213static void set_speed(struct scalable *sc, const struct core_speed *tgt_s)
214{
215 const struct core_speed *strt_s = sc->cur_speed;
216
Stephen Boyd14a47392012-08-06 20:15:15 -0700217 if (strt_s == tgt_s)
218 return;
219
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800220 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
221 /*
222 * Move to an always-on source running at a frequency
223 * that does not require an elevated CPU voltage.
224 */
225 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
226 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
227
228 /* Re-program HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700229 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800230 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700231 hfpll_enable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800232
233 /* Move to HFPLL. */
234 set_pri_clk_src(sc, tgt_s->pri_src_sel);
235 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
236 set_sec_clk_src(sc, tgt_s->sec_src_sel);
237 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700238 hfpll_disable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800239 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
240 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700241 hfpll_enable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800242 set_pri_clk_src(sc, tgt_s->pri_src_sel);
243 } else {
244 set_sec_clk_src(sc, tgt_s->sec_src_sel);
245 }
246
247 sc->cur_speed = tgt_s;
248}
249
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700250struct vdd_data {
251 int vdd_mem;
252 int vdd_dig;
253 int vdd_core;
254 int ua_core;
255};
256
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800257/* Apply any per-cpu voltage increases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700258static int increase_vdd(int cpu, struct vdd_data *data,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800259 enum setrate_reason reason)
260{
261 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700262 int rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800263
264 /*
265 * Increase vdd_mem active-set before vdd_dig.
266 * vdd_mem should be >= vdd_dig.
267 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700268 if (data->vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700269 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700270 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800271 if (rc) {
272 dev_err(drv.dev,
273 "vdd_mem (cpu%d) increase failed (%d)\n",
274 cpu, rc);
275 return rc;
276 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700277 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800278 }
279
280 /* Increase vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700281 if (data->vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700282 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700283 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800284 if (rc) {
285 dev_err(drv.dev,
286 "vdd_dig (cpu%d) increase failed (%d)\n",
287 cpu, rc);
288 return rc;
289 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700290 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
291 }
292
293 /* Increase current request. */
294 if (data->ua_core > sc->vreg[VREG_CORE].cur_ua) {
295 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
296 data->ua_core);
297 if (rc < 0) {
298 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
299 sc->vreg[VREG_CORE].name, rc);
300 return rc;
301 }
302 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800303 }
304
305 /*
306 * Update per-CPU core voltage. Don't do this for the hotplug path for
307 * which it should already be correct. Attempting to set it is bad
308 * because we don't know what CPU we are running on at this point, but
309 * the CPU regulator API requires we call it from the affected CPU.
310 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700311 if (data->vdd_core > sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800312 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700313 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
314 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800315 if (rc) {
316 dev_err(drv.dev,
317 "vdd_core (cpu%d) increase failed (%d)\n",
318 cpu, rc);
319 return rc;
320 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700321 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800322 }
323
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700324 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800325}
326
327/* Apply any per-cpu voltage decreases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700328static void decrease_vdd(int cpu, struct vdd_data *data,
329 enum setrate_reason reason)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800330{
331 struct scalable *sc = &drv.scalable[cpu];
332 int ret;
333
334 /*
335 * Update per-CPU core voltage. This must be called on the CPU
336 * that's being affected. Don't do this in the hotplug remove path,
337 * where the rail is off and we're executing on the other CPU.
338 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700339 if (data->vdd_core < sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800340 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700341 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
342 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800343 if (ret) {
344 dev_err(drv.dev,
345 "vdd_core (cpu%d) decrease failed (%d)\n",
346 cpu, ret);
347 return;
348 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700349 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
350 }
351
352 /* Decrease current request. */
353 if (data->ua_core < sc->vreg[VREG_CORE].cur_ua) {
354 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
355 data->ua_core);
356 if (ret < 0) {
357 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
358 sc->vreg[VREG_CORE].name, ret);
359 return;
360 }
361 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800362 }
363
364 /* Decrease vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700365 if (data->vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700366 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700367 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800368 if (ret) {
369 dev_err(drv.dev,
370 "vdd_dig (cpu%d) decrease failed (%d)\n",
371 cpu, ret);
372 return;
373 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700374 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800375 }
376
377 /*
378 * Decrease vdd_mem active-set after vdd_dig.
379 * vdd_mem should be >= vdd_dig.
380 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700381 if (data->vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700382 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700383 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800384 if (ret) {
385 dev_err(drv.dev,
386 "vdd_mem (cpu%d) decrease failed (%d)\n",
387 cpu, ret);
388 return;
389 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700390 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800391 }
392}
393
394static int calculate_vdd_mem(const struct acpu_level *tgt)
395{
Matt Wagantall600ea502012-06-08 18:49:53 -0700396 return drv.l2_freq_tbl[tgt->l2_level].vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800397}
398
Matt Wagantall72a38002012-07-18 13:42:55 -0700399static int get_src_dig(const struct core_speed *s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800400{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700401 const int *hfpll_vdd = drv.hfpll_data->vdd;
402 const u32 low_vdd_l_max = drv.hfpll_data->low_vdd_l_max;
Matt Wagantall87465f52012-07-23 22:03:06 -0700403 const u32 nom_vdd_l_max = drv.hfpll_data->nom_vdd_l_max;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800404
Matt Wagantall72a38002012-07-18 13:42:55 -0700405 if (s->src != HFPLL)
406 return hfpll_vdd[HFPLL_VDD_NONE];
Matt Wagantall87465f52012-07-23 22:03:06 -0700407 else if (s->pll_l_val > nom_vdd_l_max)
408 return hfpll_vdd[HFPLL_VDD_HIGH];
Matt Wagantall72a38002012-07-18 13:42:55 -0700409 else if (s->pll_l_val > low_vdd_l_max)
410 return hfpll_vdd[HFPLL_VDD_NOM];
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800411 else
Matt Wagantall72a38002012-07-18 13:42:55 -0700412 return hfpll_vdd[HFPLL_VDD_LOW];
413}
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800414
Matt Wagantall72a38002012-07-18 13:42:55 -0700415static int calculate_vdd_dig(const struct acpu_level *tgt)
416{
417 int l2_pll_vdd_dig, cpu_pll_vdd_dig;
418
419 l2_pll_vdd_dig = get_src_dig(&drv.l2_freq_tbl[tgt->l2_level].speed);
420 cpu_pll_vdd_dig = get_src_dig(&tgt->speed);
421
422 return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig,
423 max(l2_pll_vdd_dig, cpu_pll_vdd_dig));
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800424}
425
Matt Wagantall9515bc22012-07-19 18:13:40 -0700426static bool enable_boost = true;
427module_param_named(boost, enable_boost, bool, S_IRUGO | S_IWUSR);
428
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800429static int calculate_vdd_core(const struct acpu_level *tgt)
430{
Matt Wagantall9515bc22012-07-19 18:13:40 -0700431 return tgt->vdd_core + (enable_boost ? drv.boost_uv : 0);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800432}
433
434/* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */
435static int acpuclk_krait_set_rate(int cpu, unsigned long rate,
436 enum setrate_reason reason)
437{
438 const struct core_speed *strt_acpu_s, *tgt_acpu_s;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800439 const struct acpu_level *tgt;
Matt Wagantall600ea502012-06-08 18:49:53 -0700440 int tgt_l2_l;
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700441 struct vdd_data vdd_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800442 unsigned long flags;
443 int rc = 0;
444
Matt Wagantall5941a332012-07-10 23:20:44 -0700445 if (cpu > num_possible_cpus())
446 return -EINVAL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800447
448 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
449 mutex_lock(&driver_lock);
450
451 strt_acpu_s = drv.scalable[cpu].cur_speed;
452
453 /* Return early if rate didn't change. */
454 if (rate == strt_acpu_s->khz)
455 goto out;
456
457 /* Find target frequency. */
458 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
459 if (tgt->speed.khz == rate) {
460 tgt_acpu_s = &tgt->speed;
461 break;
462 }
463 }
464 if (tgt->speed.khz == 0) {
465 rc = -EINVAL;
466 goto out;
467 }
468
469 /* Calculate voltage requirements for the current CPU. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700470 vdd_data.vdd_mem = calculate_vdd_mem(tgt);
471 vdd_data.vdd_dig = calculate_vdd_dig(tgt);
472 vdd_data.vdd_core = calculate_vdd_core(tgt);
473 vdd_data.ua_core = tgt->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800474
475 /* Increase VDD levels if needed. */
476 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700477 rc = increase_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800478 if (rc)
479 goto out;
480 }
481
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700482 dev_dbg(drv.dev, "Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
483 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800484
485 /* Set the new CPU speed. */
486 set_speed(&drv.scalable[cpu], tgt_acpu_s);
487
488 /*
489 * Update the L2 vote and apply the rate change. A spinlock is
490 * necessary to ensure L2 rate is calculated and set atomically
491 * with the CPU frequency, even if acpuclk_krait_set_rate() is
492 * called from an atomic context and the driver_lock mutex is not
493 * acquired.
494 */
495 spin_lock_irqsave(&l2_lock, flags);
496 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
Matt Wagantall600ea502012-06-08 18:49:53 -0700497 set_speed(&drv.scalable[L2], &drv.l2_freq_tbl[tgt_l2_l].speed);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800498 spin_unlock_irqrestore(&l2_lock, flags);
499
500 /* Nothing else to do for power collapse or SWFI. */
501 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
502 goto out;
503
504 /* Update bus bandwith request. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700505 set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800506
507 /* Drop VDD levels if we can. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700508 decrease_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800509
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700510 dev_dbg(drv.dev, "ACPU%d speed change complete\n", cpu);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800511
512out:
513 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
514 mutex_unlock(&driver_lock);
515 return rc;
516}
517
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700518static struct acpuclk_data acpuclk_krait_data = {
519 .set_rate = acpuclk_krait_set_rate,
520 .get_rate = acpuclk_krait_get_rate,
521};
522
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800523/* Initialize a HFPLL at a given rate and enable it. */
524static void __init hfpll_init(struct scalable *sc,
525 const struct core_speed *tgt_s)
526{
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700527 dev_dbg(drv.dev, "Initializing HFPLL%d\n", sc - drv.scalable);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800528
529 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700530 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800531
532 /* Configure PLL parameters for integer mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700533 writel_relaxed(drv.hfpll_data->config_val,
534 sc->hfpll_base + drv.hfpll_data->config_offset);
535 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->m_offset);
536 writel_relaxed(1, sc->hfpll_base + drv.hfpll_data->n_offset);
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700537 if (drv.hfpll_data->has_user_reg)
538 writel_relaxed(drv.hfpll_data->user_val,
539 sc->hfpll_base + drv.hfpll_data->user_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800540
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700541 /* Program droop controller, if supported */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700542 if (drv.hfpll_data->has_droop_ctl)
543 writel_relaxed(drv.hfpll_data->droop_val,
544 sc->hfpll_base + drv.hfpll_data->droop_offset);
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700545
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800546 /* Set an initial rate and enable the PLL. */
547 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700548 hfpll_enable(sc, false);
549}
550
Matt Wagantall302d9a32012-07-03 13:37:29 -0700551static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700552 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700553{
554 int ret;
555
556 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700557 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700558
559 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
560 sc->vreg[vreg].name);
561 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700562 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
563 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
564 sc->vreg[vreg].name, ret);
565 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700566 }
567
568 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
569 sc->vreg[vreg].max_vdd);
570 if (ret) {
571 dev_err(drv.dev, "%s initialization failed (%d)\n",
572 sc->vreg[vreg].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700573 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700574 }
575 sc->vreg[vreg].cur_vdd = vdd;
576
Matt Wagantall302d9a32012-07-03 13:37:29 -0700577 if (enable) {
578 ret = enable_rpm_vreg(&sc->vreg[vreg]);
579 if (ret)
580 goto err_conf;
581 }
582
583 return 0;
584
585err_conf:
586 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
587err_get:
588 return ret;
589}
590
591static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
592 enum vregs vreg)
593{
594 if (!sc->vreg[vreg].rpm_reg)
595 return;
596
597 disable_rpm_vreg(&sc->vreg[vreg]);
598 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800599}
600
601/* Voltage regulator initialization. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700602static int __cpuinit regulator_init(struct scalable *sc,
603 const struct acpu_level *acpu_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800604{
Matt Wagantall754ee272012-06-18 13:40:26 -0700605 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800606
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700607 vdd_mem = calculate_vdd_mem(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700608 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
609 if (ret)
610 goto err_mem;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700611
612 vdd_dig = calculate_vdd_dig(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700613 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
614 if (ret)
615 goto err_dig;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700616
Matt Wagantall302d9a32012-07-03 13:37:29 -0700617 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700618 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700619 if (ret)
620 goto err_hfpll_a;
621 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700622 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700623 if (ret)
624 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700625
Matt Wagantall754ee272012-06-18 13:40:26 -0700626 /* Setup Krait CPU regulators and initial core voltage. */
627 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
628 sc->vreg[VREG_CORE].name);
629 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700630 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
631 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
632 sc->vreg[VREG_CORE].name, ret);
633 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700634 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700635 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
636 acpu_level->ua_core);
637 if (ret < 0) {
638 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
639 sc->vreg[VREG_CORE].name, ret);
640 goto err_core_conf;
641 }
642 sc->vreg[VREG_CORE].cur_ua = acpu_level->ua_core;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700643 vdd_core = calculate_vdd_core(acpu_level);
Matt Wagantall754ee272012-06-18 13:40:26 -0700644 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
645 sc->vreg[VREG_CORE].max_vdd);
646 if (ret) {
647 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
648 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700649 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700650 }
651 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
Matt Wagantall754ee272012-06-18 13:40:26 -0700652 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
653 if (ret) {
654 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
655 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700656 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800657 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700658
659 return 0;
660
661err_core_conf:
662 regulator_put(sc->vreg[VREG_CORE].reg);
663err_core_get:
664 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
665err_hfpll_b:
666 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
667err_hfpll_a:
668 rpm_regulator_cleanup(sc, VREG_DIG);
669err_dig:
670 rpm_regulator_cleanup(sc, VREG_MEM);
671err_mem:
672 return ret;
673}
674
675static void __cpuinit regulator_cleanup(struct scalable *sc)
676{
677 regulator_disable(sc->vreg[VREG_CORE].reg);
678 regulator_put(sc->vreg[VREG_CORE].reg);
679 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
680 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
681 rpm_regulator_cleanup(sc, VREG_DIG);
682 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800683}
684
685/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700686static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700687 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800688{
689 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700690 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800691
692 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700693 if (sc->aux_clk_sel_phys) {
694 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700695 if (!aux_reg)
696 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700697 writel_relaxed(sc->aux_clk_sel, aux_reg);
698 iounmap(aux_reg);
699 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800700
701 /* Switch away from the HFPLL while it's re-initialized. */
702 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
703 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
704 hfpll_init(sc, tgt_s);
705
706 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
707 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
708 regval &= ~(0x3 << 6);
709 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
710
711 /* Switch to the target clock source. */
712 set_sec_clk_src(sc, tgt_s->sec_src_sel);
713 set_pri_clk_src(sc, tgt_s->pri_src_sel);
714 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700715
716 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800717}
718
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700719static void __cpuinit fill_cur_core_speed(struct core_speed *s,
720 struct scalable *sc)
721{
722 s->pri_src_sel = get_l2_indirect_reg(sc->l2cpmr_iaddr) & 0x3;
723 s->sec_src_sel = (get_l2_indirect_reg(sc->l2cpmr_iaddr) >> 2) & 0x3;
724 s->pll_l_val = readl_relaxed(sc->hfpll_base + drv.hfpll_data->l_offset);
725}
726
727static bool __cpuinit speed_equal(const struct core_speed *s1,
728 const struct core_speed *s2)
729{
730 return (s1->pri_src_sel == s2->pri_src_sel &&
731 s1->sec_src_sel == s2->sec_src_sel &&
732 s1->pll_l_val == s2->pll_l_val);
733}
734
735static const struct acpu_level __cpuinit *find_cur_acpu_level(int cpu)
736{
737 struct scalable *sc = &drv.scalable[cpu];
738 const struct acpu_level *l;
739 struct core_speed cur_speed;
740
741 fill_cur_core_speed(&cur_speed, sc);
742 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
743 if (speed_equal(&l->speed, &cur_speed))
744 return l;
745 return NULL;
746}
747
748static const struct l2_level __init *find_cur_l2_level(void)
749{
750 struct scalable *sc = &drv.scalable[L2];
751 const struct l2_level *l;
752 struct core_speed cur_speed;
753
754 fill_cur_core_speed(&cur_speed, sc);
755 for (l = drv.l2_freq_tbl; l->speed.khz != 0; l++)
756 if (speed_equal(&l->speed, &cur_speed))
757 return l;
758 return NULL;
759}
760
761static const struct acpu_level __cpuinit *find_min_acpu_level(void)
762{
763 struct acpu_level *l;
764
765 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
766 if (l->use_for_scaling)
767 return l;
768
769 return NULL;
770}
771
Matt Wagantall302d9a32012-07-03 13:37:29 -0700772static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800773{
Matt Wagantall754ee272012-06-18 13:40:26 -0700774 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700775 const struct acpu_level *acpu_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700776 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800777
Matt Wagantall754ee272012-06-18 13:40:26 -0700778 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700779 if (!sc->hfpll_base) {
780 ret = -ENOMEM;
781 goto err_ioremap;
782 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700783
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700784 acpu_level = find_cur_acpu_level(cpu);
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700785 if (!acpu_level) {
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700786 acpu_level = find_min_acpu_level();
787 if (!acpu_level) {
788 ret = -ENODEV;
789 goto err_table;
790 }
791 dev_dbg(drv.dev, "CPU%d is running at an unknown rate. Defaulting to %lu KHz.\n",
792 cpu, acpu_level->speed.khz);
793 } else {
794 dev_dbg(drv.dev, "CPU%d is running at %lu KHz\n", cpu,
795 acpu_level->speed.khz);
796 }
797
798 ret = regulator_init(sc, acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700799 if (ret)
800 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700801
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700802 ret = init_clock_sources(sc, &acpu_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700803 if (ret)
804 goto err_clocks;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700805
806 sc->l2_vote = acpu_level->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700807 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700808
809 return 0;
810
811err_clocks:
812 regulator_cleanup(sc);
813err_regulators:
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700814err_table:
Matt Wagantall302d9a32012-07-03 13:37:29 -0700815 iounmap(sc->hfpll_base);
816err_ioremap:
817 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800818}
819
820/* Register with bus driver. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700821static void __init bus_init(const struct l2_level *l2_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800822{
823 int ret;
824
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700825 drv.bus_perf_client = msm_bus_scale_register_client(drv.bus_scale);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800826 if (!drv.bus_perf_client) {
827 dev_err(drv.dev, "unable to register bus client\n");
828 BUG();
829 }
830
Matt Wagantall754ee272012-06-18 13:40:26 -0700831 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700832 l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800833 if (ret)
834 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
835}
836
837#ifdef CONFIG_CPU_FREQ_MSM
838static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
839
840static void __init cpufreq_table_init(void)
841{
842 int cpu;
843
844 for_each_possible_cpu(cpu) {
845 int i, freq_cnt = 0;
846 /* Construct the freq_table tables from acpu_freq_tbl. */
847 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
848 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
849 if (drv.acpu_freq_tbl[i].use_for_scaling) {
850 freq_table[cpu][freq_cnt].index = freq_cnt;
851 freq_table[cpu][freq_cnt].frequency
852 = drv.acpu_freq_tbl[i].speed.khz;
853 freq_cnt++;
854 }
855 }
856 /* freq_table not big enough to store all usable freqs. */
857 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
858
859 freq_table[cpu][freq_cnt].index = freq_cnt;
860 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
861
862 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
863 cpu, freq_cnt);
864
865 /* Register table with CPUFreq. */
866 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
867 }
868}
869#else
870static void __init cpufreq_table_init(void) {}
871#endif
872
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800873static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
874 unsigned long action, void *hcpu)
875{
876 static int prev_khz[NR_CPUS];
877 int rc, cpu = (int)hcpu;
878 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700879 unsigned long hot_unplug_khz = acpuclk_krait_data.power_collapse_khz;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800880
881 switch (action & ~CPU_TASKS_FROZEN) {
882 case CPU_DEAD:
883 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
884 /* Fall through. */
885 case CPU_UP_CANCELED:
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700886 acpuclk_krait_set_rate(cpu, hot_unplug_khz, SETRATE_HOTPLUG);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800887 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
888 break;
889 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700890 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700891 rc = per_cpu_init(cpu);
892 if (rc)
893 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700894 break;
895 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800896 if (WARN_ON(!prev_khz[cpu]))
897 return NOTIFY_BAD;
898 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700899 sc->vreg[VREG_CORE].cur_ua);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800900 if (rc < 0)
901 return NOTIFY_BAD;
902 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
903 break;
904 default:
905 break;
906 }
907
908 return NOTIFY_OK;
909}
910
911static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
912 .notifier_call = acpuclk_cpu_callback,
913};
914
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700915static const int krait_needs_vmin(void)
916{
917 switch (read_cpuid_id()) {
918 case 0x511F04D0: /* KR28M2A20 */
919 case 0x511F04D1: /* KR28M2A21 */
920 case 0x510F06F0: /* KR28M4A10 */
921 return 1;
922 default:
923 return 0;
924 };
925}
926
927static void krait_apply_vmin(struct acpu_level *tbl)
928{
929 for (; tbl->speed.khz != 0; tbl++)
930 if (tbl->vdd_core < 1150000)
931 tbl->vdd_core = 1150000;
932}
933
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700934static int __init select_freq_plan(u32 qfprom_phys)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800935{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800936 void __iomem *qfprom_base;
937 u32 pte_efuse, pvs, tbl_idx;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700938 char *pvs_names[] = { "Slow", "Nominal", "Fast", "Faster", "Unknown" };
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800939
940 qfprom_base = ioremap(qfprom_phys, SZ_256);
941 /* Select frequency tables. */
942 if (qfprom_base) {
943 pte_efuse = readl_relaxed(qfprom_base + PTE_EFUSE);
944 pvs = (pte_efuse >> 10) & 0x7;
945 iounmap(qfprom_base);
946 if (pvs == 0x7)
947 pvs = (pte_efuse >> 13) & 0x7;
948
949 switch (pvs) {
950 case 0x0:
951 case 0x7:
952 tbl_idx = PVS_SLOW;
953 break;
954 case 0x1:
955 tbl_idx = PVS_NOMINAL;
956 break;
957 case 0x3:
958 tbl_idx = PVS_FAST;
959 break;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700960 case 0x4:
961 tbl_idx = PVS_FASTER;
962 break;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800963 default:
964 tbl_idx = PVS_UNKNOWN;
965 break;
966 }
967 } else {
968 tbl_idx = PVS_UNKNOWN;
969 dev_err(drv.dev, "Unable to map QFPROM base\n");
970 }
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700971 if (tbl_idx == PVS_UNKNOWN) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800972 tbl_idx = PVS_SLOW;
973 dev_warn(drv.dev, "ACPU PVS: Defaulting to %s\n",
974 pvs_names[tbl_idx]);
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700975 } else {
976 dev_info(drv.dev, "ACPU PVS: %s\n", pvs_names[tbl_idx]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800977 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800978
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700979 return tbl_idx;
980}
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700981
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700982static void __init drv_data_init(struct device *dev,
983 const struct acpuclk_krait_params *params)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800984{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700985 int tbl_idx;
986
987 drv.dev = dev;
988 drv.scalable = kmemdup(params->scalable, params->scalable_size,
989 GFP_KERNEL);
990 BUG_ON(!drv.scalable);
991
992 drv.hfpll_data = kmemdup(params->hfpll_data, sizeof(*drv.hfpll_data),
993 GFP_KERNEL);
994 BUG_ON(!drv.hfpll_data);
995
996 drv.l2_freq_tbl = kmemdup(params->l2_freq_tbl, params->l2_freq_tbl_size,
997 GFP_KERNEL);
998 BUG_ON(!drv.l2_freq_tbl);
999
1000 drv.bus_scale = kmemdup(params->bus_scale, sizeof(*drv.bus_scale),
1001 GFP_KERNEL);
1002 BUG_ON(!drv.bus_scale);
1003 drv.bus_scale->usecase = kmemdup(drv.bus_scale->usecase,
1004 drv.bus_scale->num_usecases * sizeof(*drv.bus_scale->usecase),
1005 GFP_KERNEL);
1006 BUG_ON(!drv.bus_scale->usecase);
1007
1008 tbl_idx = select_freq_plan(params->qfprom_phys_base);
1009 drv.acpu_freq_tbl = kmemdup(params->pvs_tables[tbl_idx].table,
1010 params->pvs_tables[tbl_idx].size,
1011 GFP_KERNEL);
1012 BUG_ON(!drv.acpu_freq_tbl);
Matt Wagantall9515bc22012-07-19 18:13:40 -07001013 drv.boost_uv = params->pvs_tables[tbl_idx].boost_uv;
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001014
1015 acpuclk_krait_data.power_collapse_khz = params->stby_khz;
1016 acpuclk_krait_data.wait_for_irq_khz = params->stby_khz;
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001017}
1018
1019static void __init hw_init(void)
1020{
1021 struct scalable *l2 = &drv.scalable[L2];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001022 const struct l2_level *l2_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -07001023 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001024
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001025 if (krait_needs_vmin())
1026 krait_apply_vmin(drv.acpu_freq_tbl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001027
Matt Wagantall754ee272012-06-18 13:40:26 -07001028 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
1029 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -07001030
Matt Wagantall302d9a32012-07-03 13:37:29 -07001031 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
1032 l2->vreg[VREG_HFPLL_A].max_vdd, false);
1033 BUG_ON(rc);
1034 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
1035 l2->vreg[VREG_HFPLL_B].max_vdd, false);
1036 BUG_ON(rc);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001037
1038 l2_level = find_cur_l2_level();
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001039 if (!l2_level) {
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001040 l2_level = drv.l2_freq_tbl;
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001041 dev_dbg(drv.dev, "L2 is running at an unknown rate. Defaulting to %lu KHz.\n",
1042 l2_level->speed.khz);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001043 } else {
1044 dev_dbg(drv.dev, "L2 is running at %lu KHz\n",
1045 l2_level->speed.khz);
1046 }
1047
1048 rc = init_clock_sources(l2, &l2_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -07001049 BUG_ON(rc);
1050
1051 for_each_online_cpu(cpu) {
1052 rc = per_cpu_init(cpu);
1053 BUG_ON(rc);
1054 }
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001055
1056 bus_init(l2_level);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001057}
1058
1059int __init acpuclk_krait_init(struct device *dev,
1060 const struct acpuclk_krait_params *params)
1061{
1062 drv_data_init(dev, params);
1063 hw_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001064
1065 cpufreq_table_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001066 acpuclk_register(&acpuclk_krait_data);
1067 register_hotcpu_notifier(&acpuclk_cpu_notifier);
1068
1069 return 0;
1070}