blob: a386e783bd7ad0beb409befd66b2544c10a758c9 [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"
Stephen Boyda86214a2012-09-14 11:25:34 -070039#include "avs.h"
Matt Wagantalle9b715a2012-01-04 18:16:14 -080040
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
Matt Wagantalle9b715a2012-01-04 18:16:14 -080045
Matt Wagantallaf4669b2012-09-25 12:47:24 -070046#define SECCLKAGD BIT(4)
47
Matt Wagantalle9b715a2012-01-04 18:16:14 -080048static DEFINE_MUTEX(driver_lock);
49static DEFINE_SPINLOCK(l2_lock);
50
Matt Wagantall488bef32012-07-13 19:42:11 -070051static struct drv_data drv;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080052
53static unsigned long acpuclk_krait_get_rate(int cpu)
54{
55 return drv.scalable[cpu].cur_speed->khz;
56}
57
58/* Select a source on the primary MUX. */
59static void set_pri_clk_src(struct scalable *sc, u32 pri_src_sel)
60{
61 u32 regval;
62
63 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
64 regval &= ~0x3;
65 regval |= (pri_src_sel & 0x3);
66 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
67 /* Wait for switch to complete. */
68 mb();
69 udelay(1);
70}
71
72/* Select a source on the secondary MUX. */
Matt Wagantall6cd5d752012-09-27 19:56:57 -070073static void __cpuinit set_sec_clk_src(struct scalable *sc, u32 sec_src_sel)
Matt Wagantalle9b715a2012-01-04 18:16:14 -080074{
75 u32 regval;
76
Matt Wagantallaf4669b2012-09-25 12:47:24 -070077 /* 8064 Errata: disable sec_src clock gating during switch. */
Matt Wagantalle9b715a2012-01-04 18:16:14 -080078 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Matt Wagantallaf4669b2012-09-25 12:47:24 -070079 regval |= SECCLKAGD;
80 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
81
82 /* Program the MUX */
Matt Wagantalle9b715a2012-01-04 18:16:14 -080083 regval &= ~(0x3 << 2);
84 regval |= ((sec_src_sel & 0x3) << 2);
85 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Matt Wagantallaf4669b2012-09-25 12:47:24 -070086
87 /* 8064 Errata: re-enabled sec_src clock gating. */
88 regval &= ~SECCLKAGD;
89 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
90
Matt Wagantalle9b715a2012-01-04 18:16:14 -080091 /* Wait for switch to complete. */
92 mb();
93 udelay(1);
94}
95
Matt Wagantall302d9a32012-07-03 13:37:29 -070096static int enable_rpm_vreg(struct vreg *vreg)
Matt Wagantalle9b715a2012-01-04 18:16:14 -080097{
Matt Wagantall302d9a32012-07-03 13:37:29 -070098 int ret = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080099
Matt Wagantall75473eb2012-05-31 15:23:22 -0700100 if (vreg->rpm_reg) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700101 ret = rpm_regulator_enable(vreg->rpm_reg);
102 if (ret)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700103 dev_err(drv.dev, "%s regulator enable failed (%d)\n",
Matt Wagantall302d9a32012-07-03 13:37:29 -0700104 vreg->name, ret);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700105 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700106
107 return ret;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700108}
109
110static void disable_rpm_vreg(struct vreg *vreg)
111{
112 int rc;
113
114 if (vreg->rpm_reg) {
115 rc = rpm_regulator_disable(vreg->rpm_reg);
116 if (rc)
117 dev_err(drv.dev, "%s regulator disable failed (%d)\n",
118 vreg->name, rc);
119 }
120}
121
122/* Enable an already-configured HFPLL. */
123static void hfpll_enable(struct scalable *sc, bool skip_regulators)
124{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800125 if (!skip_regulators) {
126 /* Enable regulators required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700127 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
128 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800129 }
130
131 /* Disable PLL bypass mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700132 writel_relaxed(0x2, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800133
134 /*
135 * H/W requires a 5us delay between disabling the bypass and
136 * de-asserting the reset. Delay 10us just to be safe.
137 */
138 mb();
139 udelay(10);
140
141 /* De-assert active-low PLL reset. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700142 writel_relaxed(0x6, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800143
144 /* Wait for PLL to lock. */
145 mb();
146 udelay(60);
147
148 /* Enable PLL output. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700149 writel_relaxed(0x7, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800150}
151
152/* Disable a HFPLL for power-savings or while it's being reprogrammed. */
153static void hfpll_disable(struct scalable *sc, bool skip_regulators)
154{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800155 /*
156 * Disable the PLL output, disable test mode, enable the bypass mode,
157 * and assert the reset.
158 */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700159 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800160
161 if (!skip_regulators) {
162 /* Remove voltage votes required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700163 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
164 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800165 }
166}
167
168/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
169static void hfpll_set_rate(struct scalable *sc, const struct core_speed *tgt_s)
170{
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700171 void __iomem *base = sc->hfpll_base;
172 u32 regval;
173
174 writel_relaxed(tgt_s->pll_l_val, base + drv.hfpll_data->l_offset);
175
176 if (drv.hfpll_data->has_user_reg) {
177 regval = readl_relaxed(base + drv.hfpll_data->user_offset);
178 if (tgt_s->pll_l_val <= drv.hfpll_data->low_vco_l_max)
179 regval &= ~drv.hfpll_data->user_vco_mask;
180 else
181 regval |= drv.hfpll_data->user_vco_mask;
182 writel_relaxed(regval, base + drv.hfpll_data->user_offset);
183 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800184}
185
186/* Return the L2 speed that should be applied. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700187static unsigned int compute_l2_level(struct scalable *sc, unsigned int vote_l)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800188{
Matt Wagantall600ea502012-06-08 18:49:53 -0700189 unsigned int new_l = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800190 int cpu;
191
192 /* Find max L2 speed vote. */
193 sc->l2_vote = vote_l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800194 for_each_present_cpu(cpu)
195 new_l = max(new_l, drv.scalable[cpu].l2_vote);
196
197 return new_l;
198}
199
200/* Update the bus bandwidth request. */
201static void set_bus_bw(unsigned int bw)
202{
203 int ret;
204
205 /* Update bandwidth if request has changed. This may sleep. */
206 ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw);
207 if (ret)
208 dev_err(drv.dev, "bandwidth request failed (%d)\n", ret);
209}
210
211/* Set the CPU or L2 clock speed. */
212static void set_speed(struct scalable *sc, const struct core_speed *tgt_s)
213{
214 const struct core_speed *strt_s = sc->cur_speed;
215
Stephen Boyd14a47392012-08-06 20:15:15 -0700216 if (strt_s == tgt_s)
217 return;
218
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800219 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
220 /*
221 * Move to an always-on source running at a frequency
222 * that does not require an elevated CPU voltage.
223 */
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800224 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) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800234 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700235 hfpll_disable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800236 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
237 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700238 hfpll_enable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800239 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800240 }
241
242 sc->cur_speed = tgt_s;
243}
244
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700245struct vdd_data {
246 int vdd_mem;
247 int vdd_dig;
248 int vdd_core;
249 int ua_core;
250};
251
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800252/* Apply any per-cpu voltage increases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700253static int increase_vdd(int cpu, struct vdd_data *data,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800254 enum setrate_reason reason)
255{
256 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700257 int rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800258
259 /*
260 * Increase vdd_mem active-set before vdd_dig.
261 * vdd_mem should be >= vdd_dig.
262 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700263 if (data->vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700264 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700265 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800266 if (rc) {
267 dev_err(drv.dev,
268 "vdd_mem (cpu%d) increase failed (%d)\n",
269 cpu, rc);
270 return rc;
271 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700272 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800273 }
274
275 /* Increase vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700276 if (data->vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700277 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700278 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800279 if (rc) {
280 dev_err(drv.dev,
281 "vdd_dig (cpu%d) increase failed (%d)\n",
282 cpu, rc);
283 return rc;
284 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700285 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
286 }
287
288 /* Increase current request. */
289 if (data->ua_core > sc->vreg[VREG_CORE].cur_ua) {
290 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
291 data->ua_core);
292 if (rc < 0) {
293 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
294 sc->vreg[VREG_CORE].name, rc);
295 return rc;
296 }
297 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800298 }
299
300 /*
301 * Update per-CPU core voltage. Don't do this for the hotplug path for
302 * which it should already be correct. Attempting to set it is bad
303 * because we don't know what CPU we are running on at this point, but
304 * the CPU regulator API requires we call it from the affected CPU.
305 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700306 if (data->vdd_core > sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800307 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700308 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
309 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800310 if (rc) {
311 dev_err(drv.dev,
312 "vdd_core (cpu%d) increase failed (%d)\n",
313 cpu, rc);
314 return rc;
315 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700316 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800317 }
318
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700319 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800320}
321
322/* Apply any per-cpu voltage decreases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700323static void decrease_vdd(int cpu, struct vdd_data *data,
324 enum setrate_reason reason)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800325{
326 struct scalable *sc = &drv.scalable[cpu];
327 int ret;
328
329 /*
330 * Update per-CPU core voltage. This must be called on the CPU
331 * that's being affected. Don't do this in the hotplug remove path,
332 * where the rail is off and we're executing on the other CPU.
333 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700334 if (data->vdd_core < sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800335 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700336 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
337 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800338 if (ret) {
339 dev_err(drv.dev,
340 "vdd_core (cpu%d) decrease failed (%d)\n",
341 cpu, ret);
342 return;
343 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700344 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
345 }
346
347 /* Decrease current request. */
348 if (data->ua_core < sc->vreg[VREG_CORE].cur_ua) {
349 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
350 data->ua_core);
351 if (ret < 0) {
352 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
353 sc->vreg[VREG_CORE].name, ret);
354 return;
355 }
356 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800357 }
358
359 /* Decrease vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700360 if (data->vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700361 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700362 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800363 if (ret) {
364 dev_err(drv.dev,
365 "vdd_dig (cpu%d) decrease failed (%d)\n",
366 cpu, ret);
367 return;
368 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700369 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800370 }
371
372 /*
373 * Decrease vdd_mem active-set after vdd_dig.
374 * vdd_mem should be >= vdd_dig.
375 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700376 if (data->vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700377 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700378 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800379 if (ret) {
380 dev_err(drv.dev,
381 "vdd_mem (cpu%d) decrease failed (%d)\n",
382 cpu, ret);
383 return;
384 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700385 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800386 }
387}
388
389static int calculate_vdd_mem(const struct acpu_level *tgt)
390{
Matt Wagantall600ea502012-06-08 18:49:53 -0700391 return drv.l2_freq_tbl[tgt->l2_level].vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800392}
393
Matt Wagantall72a38002012-07-18 13:42:55 -0700394static int get_src_dig(const struct core_speed *s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800395{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700396 const int *hfpll_vdd = drv.hfpll_data->vdd;
397 const u32 low_vdd_l_max = drv.hfpll_data->low_vdd_l_max;
Matt Wagantall87465f52012-07-23 22:03:06 -0700398 const u32 nom_vdd_l_max = drv.hfpll_data->nom_vdd_l_max;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800399
Matt Wagantall72a38002012-07-18 13:42:55 -0700400 if (s->src != HFPLL)
401 return hfpll_vdd[HFPLL_VDD_NONE];
Matt Wagantall87465f52012-07-23 22:03:06 -0700402 else if (s->pll_l_val > nom_vdd_l_max)
403 return hfpll_vdd[HFPLL_VDD_HIGH];
Matt Wagantall72a38002012-07-18 13:42:55 -0700404 else if (s->pll_l_val > low_vdd_l_max)
405 return hfpll_vdd[HFPLL_VDD_NOM];
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800406 else
Matt Wagantall72a38002012-07-18 13:42:55 -0700407 return hfpll_vdd[HFPLL_VDD_LOW];
408}
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800409
Matt Wagantall72a38002012-07-18 13:42:55 -0700410static int calculate_vdd_dig(const struct acpu_level *tgt)
411{
412 int l2_pll_vdd_dig, cpu_pll_vdd_dig;
413
414 l2_pll_vdd_dig = get_src_dig(&drv.l2_freq_tbl[tgt->l2_level].speed);
415 cpu_pll_vdd_dig = get_src_dig(&tgt->speed);
416
417 return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig,
418 max(l2_pll_vdd_dig, cpu_pll_vdd_dig));
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800419}
420
Matt Wagantall9515bc22012-07-19 18:13:40 -0700421static bool enable_boost = true;
422module_param_named(boost, enable_boost, bool, S_IRUGO | S_IWUSR);
423
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800424static int calculate_vdd_core(const struct acpu_level *tgt)
425{
Matt Wagantall9515bc22012-07-19 18:13:40 -0700426 return tgt->vdd_core + (enable_boost ? drv.boost_uv : 0);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800427}
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
Stephen Boyda86214a2012-09-14 11:25:34 -0700470 /* Disable AVS before voltage switch */
471 if (reason == SETRATE_CPUFREQ && drv.scalable[cpu].avs_enabled) {
472 AVS_DISABLE(cpu);
473 drv.scalable[cpu].avs_enabled = false;
474 }
475
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800476 /* Increase VDD levels if needed. */
477 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700478 rc = increase_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800479 if (rc)
480 goto out;
481 }
482
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700483 dev_dbg(drv.dev, "Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
484 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800485
486 /* Set the new CPU speed. */
487 set_speed(&drv.scalable[cpu], tgt_acpu_s);
488
489 /*
490 * Update the L2 vote and apply the rate change. A spinlock is
491 * necessary to ensure L2 rate is calculated and set atomically
492 * with the CPU frequency, even if acpuclk_krait_set_rate() is
493 * called from an atomic context and the driver_lock mutex is not
494 * acquired.
495 */
496 spin_lock_irqsave(&l2_lock, flags);
497 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
Matt Wagantall600ea502012-06-08 18:49:53 -0700498 set_speed(&drv.scalable[L2], &drv.l2_freq_tbl[tgt_l2_l].speed);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800499 spin_unlock_irqrestore(&l2_lock, flags);
500
501 /* Nothing else to do for power collapse or SWFI. */
502 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
503 goto out;
504
505 /* Update bus bandwith request. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700506 set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800507
508 /* Drop VDD levels if we can. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700509 decrease_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800510
Stephen Boyda86214a2012-09-14 11:25:34 -0700511 /* Re-enable AVS */
512 if (reason == SETRATE_CPUFREQ && tgt->avsdscr_setting) {
513 AVS_ENABLE(cpu, tgt->avsdscr_setting);
514 drv.scalable[cpu].avs_enabled = true;
515 }
516
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700517 dev_dbg(drv.dev, "ACPU%d speed change complete\n", cpu);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800518
519out:
520 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
521 mutex_unlock(&driver_lock);
522 return rc;
523}
524
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700525static struct acpuclk_data acpuclk_krait_data = {
526 .set_rate = acpuclk_krait_set_rate,
527 .get_rate = acpuclk_krait_get_rate,
528};
529
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800530/* Initialize a HFPLL at a given rate and enable it. */
Matt Wagantall980d0672012-10-17 13:50:07 -0700531static void __cpuinit hfpll_init(struct scalable *sc,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800532 const struct core_speed *tgt_s)
533{
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700534 dev_dbg(drv.dev, "Initializing HFPLL%d\n", sc - drv.scalable);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800535
536 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700537 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800538
539 /* Configure PLL parameters for integer mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700540 writel_relaxed(drv.hfpll_data->config_val,
541 sc->hfpll_base + drv.hfpll_data->config_offset);
542 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->m_offset);
543 writel_relaxed(1, sc->hfpll_base + drv.hfpll_data->n_offset);
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700544 if (drv.hfpll_data->has_user_reg)
545 writel_relaxed(drv.hfpll_data->user_val,
546 sc->hfpll_base + drv.hfpll_data->user_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800547
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700548 /* Program droop controller, if supported */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700549 if (drv.hfpll_data->has_droop_ctl)
550 writel_relaxed(drv.hfpll_data->droop_val,
551 sc->hfpll_base + drv.hfpll_data->droop_offset);
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700552
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800553 /* Set an initial rate and enable the PLL. */
554 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700555 hfpll_enable(sc, false);
556}
557
Matt Wagantall302d9a32012-07-03 13:37:29 -0700558static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700559 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700560{
561 int ret;
562
563 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700564 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700565
566 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
567 sc->vreg[vreg].name);
568 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700569 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
570 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
571 sc->vreg[vreg].name, ret);
572 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700573 }
574
575 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
576 sc->vreg[vreg].max_vdd);
577 if (ret) {
578 dev_err(drv.dev, "%s initialization failed (%d)\n",
579 sc->vreg[vreg].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700580 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700581 }
582 sc->vreg[vreg].cur_vdd = vdd;
583
Matt Wagantall302d9a32012-07-03 13:37:29 -0700584 if (enable) {
585 ret = enable_rpm_vreg(&sc->vreg[vreg]);
586 if (ret)
587 goto err_conf;
588 }
589
590 return 0;
591
592err_conf:
593 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
594err_get:
595 return ret;
596}
597
598static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
599 enum vregs vreg)
600{
601 if (!sc->vreg[vreg].rpm_reg)
602 return;
603
604 disable_rpm_vreg(&sc->vreg[vreg]);
605 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800606}
607
608/* Voltage regulator initialization. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700609static int __cpuinit regulator_init(struct scalable *sc,
610 const struct acpu_level *acpu_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800611{
Matt Wagantall754ee272012-06-18 13:40:26 -0700612 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800613
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700614 vdd_mem = calculate_vdd_mem(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700615 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
616 if (ret)
617 goto err_mem;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700618
619 vdd_dig = calculate_vdd_dig(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700620 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
621 if (ret)
622 goto err_dig;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700623
Matt Wagantall302d9a32012-07-03 13:37:29 -0700624 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700625 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700626 if (ret)
627 goto err_hfpll_a;
628 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700629 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700630 if (ret)
631 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700632
Matt Wagantall754ee272012-06-18 13:40:26 -0700633 /* Setup Krait CPU regulators and initial core voltage. */
634 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
635 sc->vreg[VREG_CORE].name);
636 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700637 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
638 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
639 sc->vreg[VREG_CORE].name, ret);
640 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700641 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700642 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
643 acpu_level->ua_core);
644 if (ret < 0) {
645 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
646 sc->vreg[VREG_CORE].name, ret);
647 goto err_core_conf;
648 }
649 sc->vreg[VREG_CORE].cur_ua = acpu_level->ua_core;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700650 vdd_core = calculate_vdd_core(acpu_level);
Matt Wagantall754ee272012-06-18 13:40:26 -0700651 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
652 sc->vreg[VREG_CORE].max_vdd);
653 if (ret) {
654 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
655 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700656 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700657 }
658 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
Matt Wagantall754ee272012-06-18 13:40:26 -0700659 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
660 if (ret) {
661 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
662 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700663 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800664 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700665
666 return 0;
667
668err_core_conf:
669 regulator_put(sc->vreg[VREG_CORE].reg);
670err_core_get:
671 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
672err_hfpll_b:
673 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
674err_hfpll_a:
675 rpm_regulator_cleanup(sc, VREG_DIG);
676err_dig:
677 rpm_regulator_cleanup(sc, VREG_MEM);
678err_mem:
679 return ret;
680}
681
682static void __cpuinit regulator_cleanup(struct scalable *sc)
683{
684 regulator_disable(sc->vreg[VREG_CORE].reg);
685 regulator_put(sc->vreg[VREG_CORE].reg);
686 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
687 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
688 rpm_regulator_cleanup(sc, VREG_DIG);
689 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800690}
691
692/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700693static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700694 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800695{
696 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700697 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800698
699 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700700 if (sc->aux_clk_sel_phys) {
701 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700702 if (!aux_reg)
703 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700704 writel_relaxed(sc->aux_clk_sel, aux_reg);
705 iounmap(aux_reg);
706 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800707
708 /* Switch away from the HFPLL while it's re-initialized. */
Matt Wagantall6cd5d752012-09-27 19:56:57 -0700709 set_sec_clk_src(sc, sc->sec_clk_sel);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800710 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
711 hfpll_init(sc, tgt_s);
712
713 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
714 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
715 regval &= ~(0x3 << 6);
716 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
717
718 /* Switch to the target clock source. */
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800719 set_pri_clk_src(sc, tgt_s->pri_src_sel);
720 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700721
722 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800723}
724
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700725static void __cpuinit fill_cur_core_speed(struct core_speed *s,
726 struct scalable *sc)
727{
728 s->pri_src_sel = get_l2_indirect_reg(sc->l2cpmr_iaddr) & 0x3;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700729 s->pll_l_val = readl_relaxed(sc->hfpll_base + drv.hfpll_data->l_offset);
730}
731
732static bool __cpuinit speed_equal(const struct core_speed *s1,
733 const struct core_speed *s2)
734{
735 return (s1->pri_src_sel == s2->pri_src_sel &&
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700736 s1->pll_l_val == s2->pll_l_val);
737}
738
739static const struct acpu_level __cpuinit *find_cur_acpu_level(int cpu)
740{
741 struct scalable *sc = &drv.scalable[cpu];
742 const struct acpu_level *l;
743 struct core_speed cur_speed;
744
745 fill_cur_core_speed(&cur_speed, sc);
746 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
747 if (speed_equal(&l->speed, &cur_speed))
748 return l;
749 return NULL;
750}
751
752static const struct l2_level __init *find_cur_l2_level(void)
753{
754 struct scalable *sc = &drv.scalable[L2];
755 const struct l2_level *l;
756 struct core_speed cur_speed;
757
758 fill_cur_core_speed(&cur_speed, sc);
759 for (l = drv.l2_freq_tbl; l->speed.khz != 0; l++)
760 if (speed_equal(&l->speed, &cur_speed))
761 return l;
762 return NULL;
763}
764
765static const struct acpu_level __cpuinit *find_min_acpu_level(void)
766{
767 struct acpu_level *l;
768
769 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
770 if (l->use_for_scaling)
771 return l;
772
773 return NULL;
774}
775
Matt Wagantall302d9a32012-07-03 13:37:29 -0700776static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800777{
Matt Wagantall754ee272012-06-18 13:40:26 -0700778 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700779 const struct acpu_level *acpu_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700780 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800781
Matt Wagantall754ee272012-06-18 13:40:26 -0700782 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700783 if (!sc->hfpll_base) {
784 ret = -ENOMEM;
785 goto err_ioremap;
786 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700787
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700788 acpu_level = find_cur_acpu_level(cpu);
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700789 if (!acpu_level) {
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700790 acpu_level = find_min_acpu_level();
791 if (!acpu_level) {
792 ret = -ENODEV;
793 goto err_table;
794 }
795 dev_dbg(drv.dev, "CPU%d is running at an unknown rate. Defaulting to %lu KHz.\n",
796 cpu, acpu_level->speed.khz);
797 } else {
798 dev_dbg(drv.dev, "CPU%d is running at %lu KHz\n", cpu,
799 acpu_level->speed.khz);
800 }
801
802 ret = regulator_init(sc, acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700803 if (ret)
804 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700805
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700806 ret = init_clock_sources(sc, &acpu_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700807 if (ret)
808 goto err_clocks;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700809
810 sc->l2_vote = acpu_level->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700811 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700812
813 return 0;
814
815err_clocks:
816 regulator_cleanup(sc);
817err_regulators:
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700818err_table:
Matt Wagantall302d9a32012-07-03 13:37:29 -0700819 iounmap(sc->hfpll_base);
820err_ioremap:
821 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800822}
823
824/* Register with bus driver. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700825static void __init bus_init(const struct l2_level *l2_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800826{
827 int ret;
828
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700829 drv.bus_perf_client = msm_bus_scale_register_client(drv.bus_scale);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800830 if (!drv.bus_perf_client) {
831 dev_err(drv.dev, "unable to register bus client\n");
832 BUG();
833 }
834
Matt Wagantall754ee272012-06-18 13:40:26 -0700835 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700836 l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800837 if (ret)
838 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
839}
840
841#ifdef CONFIG_CPU_FREQ_MSM
842static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
843
844static void __init cpufreq_table_init(void)
845{
846 int cpu;
847
848 for_each_possible_cpu(cpu) {
849 int i, freq_cnt = 0;
850 /* Construct the freq_table tables from acpu_freq_tbl. */
851 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
852 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
853 if (drv.acpu_freq_tbl[i].use_for_scaling) {
854 freq_table[cpu][freq_cnt].index = freq_cnt;
855 freq_table[cpu][freq_cnt].frequency
856 = drv.acpu_freq_tbl[i].speed.khz;
857 freq_cnt++;
858 }
859 }
860 /* freq_table not big enough to store all usable freqs. */
861 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
862
863 freq_table[cpu][freq_cnt].index = freq_cnt;
864 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
865
866 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
867 cpu, freq_cnt);
868
869 /* Register table with CPUFreq. */
870 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
871 }
872}
873#else
874static void __init cpufreq_table_init(void) {}
875#endif
876
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800877static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
878 unsigned long action, void *hcpu)
879{
880 static int prev_khz[NR_CPUS];
881 int rc, cpu = (int)hcpu;
882 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700883 unsigned long hot_unplug_khz = acpuclk_krait_data.power_collapse_khz;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800884
885 switch (action & ~CPU_TASKS_FROZEN) {
886 case CPU_DEAD:
887 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
888 /* Fall through. */
889 case CPU_UP_CANCELED:
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700890 acpuclk_krait_set_rate(cpu, hot_unplug_khz, SETRATE_HOTPLUG);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800891 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
892 break;
893 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700894 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700895 rc = per_cpu_init(cpu);
896 if (rc)
897 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700898 break;
899 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800900 if (WARN_ON(!prev_khz[cpu]))
901 return NOTIFY_BAD;
902 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700903 sc->vreg[VREG_CORE].cur_ua);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800904 if (rc < 0)
905 return NOTIFY_BAD;
906 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
907 break;
908 default:
909 break;
910 }
911
912 return NOTIFY_OK;
913}
914
915static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
916 .notifier_call = acpuclk_cpu_callback,
917};
918
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700919static const int krait_needs_vmin(void)
920{
921 switch (read_cpuid_id()) {
922 case 0x511F04D0: /* KR28M2A20 */
923 case 0x511F04D1: /* KR28M2A21 */
924 case 0x510F06F0: /* KR28M4A10 */
925 return 1;
926 default:
927 return 0;
928 };
929}
930
931static void krait_apply_vmin(struct acpu_level *tbl)
932{
Stephen Boyda86214a2012-09-14 11:25:34 -0700933 for (; tbl->speed.khz != 0; tbl++) {
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700934 if (tbl->vdd_core < 1150000)
935 tbl->vdd_core = 1150000;
Stephen Boyda86214a2012-09-14 11:25:34 -0700936 tbl->avsdscr_setting = 0;
937 }
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700938}
939
Patrick Daly18d2d482012-08-24 14:22:06 -0700940static int __init get_speed_bin(u32 pte_efuse)
941{
942 uint32_t speed_bin;
943
944 speed_bin = pte_efuse & 0xF;
945 if (speed_bin == 0xF)
946 speed_bin = (pte_efuse >> 4) & 0xF;
947
948 if (speed_bin == 0xF) {
949 speed_bin = 0;
950 dev_warn(drv.dev, "SPEED BIN: Defaulting to %d\n", speed_bin);
951 } else {
952 dev_info(drv.dev, "SPEED BIN: %d\n", speed_bin);
953 }
954
955 return speed_bin;
956}
957
958static int __init get_pvs_bin(u32 pte_efuse)
959{
960 uint32_t pvs_bin;
961
962 pvs_bin = (pte_efuse >> 10) & 0x7;
963 if (pvs_bin == 0x7)
964 pvs_bin = (pte_efuse >> 13) & 0x7;
965
966 if (pvs_bin == 0x7) {
967 pvs_bin = 0;
968 dev_warn(drv.dev, "ACPU PVS: Defaulting to %d\n", pvs_bin);
969 } else {
970 dev_info(drv.dev, "ACPU PVS: %d\n", pvs_bin);
971 }
972
973 return pvs_bin;
974}
975
976static struct pvs_table * __init select_freq_plan(u32 pte_efuse_phys,
977 struct pvs_table (*pvs_tables)[NUM_PVS])
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800978{
Matt Wagantallee2b4372012-09-17 17:51:06 -0700979 void __iomem *pte_efuse;
Matt Wagantall488bef32012-07-13 19:42:11 -0700980 u32 pte_efuse_val;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800981
Matt Wagantallee2b4372012-09-17 17:51:06 -0700982 pte_efuse = ioremap(pte_efuse_phys, 4);
Patrick Daly18d2d482012-08-24 14:22:06 -0700983 if (!pte_efuse) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800984 dev_err(drv.dev, "Unable to map QFPROM base\n");
Patrick Daly18d2d482012-08-24 14:22:06 -0700985 return NULL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800986 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800987
Patrick Daly18d2d482012-08-24 14:22:06 -0700988 pte_efuse_val = readl_relaxed(pte_efuse);
989 iounmap(pte_efuse);
990
991 /* Select frequency tables. */
Matt Wagantall488bef32012-07-13 19:42:11 -0700992 drv.speed_bin = get_speed_bin(pte_efuse_val);
993 drv.pvs_bin = get_pvs_bin(pte_efuse_val);
Patrick Daly18d2d482012-08-24 14:22:06 -0700994
Matt Wagantall488bef32012-07-13 19:42:11 -0700995 return &pvs_tables[drv.speed_bin][drv.pvs_bin];
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700996}
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700997
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700998static void __init drv_data_init(struct device *dev,
999 const struct acpuclk_krait_params *params)
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001000{
Patrick Daly18d2d482012-08-24 14:22:06 -07001001 struct pvs_table *pvs;
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001002
1003 drv.dev = dev;
1004 drv.scalable = kmemdup(params->scalable, params->scalable_size,
1005 GFP_KERNEL);
1006 BUG_ON(!drv.scalable);
1007
1008 drv.hfpll_data = kmemdup(params->hfpll_data, sizeof(*drv.hfpll_data),
1009 GFP_KERNEL);
1010 BUG_ON(!drv.hfpll_data);
1011
1012 drv.l2_freq_tbl = kmemdup(params->l2_freq_tbl, params->l2_freq_tbl_size,
1013 GFP_KERNEL);
1014 BUG_ON(!drv.l2_freq_tbl);
1015
1016 drv.bus_scale = kmemdup(params->bus_scale, sizeof(*drv.bus_scale),
1017 GFP_KERNEL);
1018 BUG_ON(!drv.bus_scale);
1019 drv.bus_scale->usecase = kmemdup(drv.bus_scale->usecase,
1020 drv.bus_scale->num_usecases * sizeof(*drv.bus_scale->usecase),
1021 GFP_KERNEL);
1022 BUG_ON(!drv.bus_scale->usecase);
1023
Patrick Daly18d2d482012-08-24 14:22:06 -07001024 pvs = select_freq_plan(params->pte_efuse_phys, params->pvs_tables);
1025 BUG_ON(!pvs->table);
1026
1027 drv.acpu_freq_tbl = kmemdup(pvs->table, pvs->size, GFP_KERNEL);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001028 BUG_ON(!drv.acpu_freq_tbl);
Patrick Daly18d2d482012-08-24 14:22:06 -07001029 drv.boost_uv = pvs->boost_uv;
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001030
1031 acpuclk_krait_data.power_collapse_khz = params->stby_khz;
1032 acpuclk_krait_data.wait_for_irq_khz = params->stby_khz;
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001033}
1034
1035static void __init hw_init(void)
1036{
1037 struct scalable *l2 = &drv.scalable[L2];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001038 const struct l2_level *l2_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -07001039 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001040
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001041 if (krait_needs_vmin())
1042 krait_apply_vmin(drv.acpu_freq_tbl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001043
Matt Wagantall754ee272012-06-18 13:40:26 -07001044 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
1045 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -07001046
Matt Wagantall302d9a32012-07-03 13:37:29 -07001047 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
1048 l2->vreg[VREG_HFPLL_A].max_vdd, false);
1049 BUG_ON(rc);
1050 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
1051 l2->vreg[VREG_HFPLL_B].max_vdd, false);
1052 BUG_ON(rc);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001053
1054 l2_level = find_cur_l2_level();
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001055 if (!l2_level) {
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001056 l2_level = drv.l2_freq_tbl;
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001057 dev_dbg(drv.dev, "L2 is running at an unknown rate. Defaulting to %lu KHz.\n",
1058 l2_level->speed.khz);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001059 } else {
1060 dev_dbg(drv.dev, "L2 is running at %lu KHz\n",
1061 l2_level->speed.khz);
1062 }
1063
1064 rc = init_clock_sources(l2, &l2_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -07001065 BUG_ON(rc);
1066
1067 for_each_online_cpu(cpu) {
1068 rc = per_cpu_init(cpu);
1069 BUG_ON(rc);
1070 }
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001071
1072 bus_init(l2_level);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001073}
1074
1075int __init acpuclk_krait_init(struct device *dev,
1076 const struct acpuclk_krait_params *params)
1077{
1078 drv_data_init(dev, params);
1079 hw_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001080
1081 cpufreq_table_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001082 acpuclk_register(&acpuclk_krait_data);
1083 register_hotcpu_notifier(&acpuclk_cpu_notifier);
1084
Matt Wagantall488bef32012-07-13 19:42:11 -07001085 acpuclk_krait_debug_init(&drv);
1086
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001087 return 0;
1088}