blob: bfb3df37aaa0ba22895f627a6235e707a9b7e2ce [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>
Steve Mucklea9aac292012-11-02 15:41:00 -070036#include <mach/msm_dcvs.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080037
38#include "acpuclock.h"
39#include "acpuclock-krait.h"
Stephen Boydc13b6792012-09-14 11:25:34 -070040#include "avs.h"
Matt Wagantalle9b715a2012-01-04 18:16:14 -080041
42/* MUX source selects. */
43#define PRI_SRC_SEL_SEC_SRC 0
44#define PRI_SRC_SEL_HFPLL 1
45#define PRI_SRC_SEL_HFPLL_DIV2 2
Matt Wagantalle9b715a2012-01-04 18:16:14 -080046
Matt Wagantall7c705e72012-09-25 12:47:24 -070047#define SECCLKAGD BIT(4)
48
Matt Wagantalle9b715a2012-01-04 18:16:14 -080049static DEFINE_MUTEX(driver_lock);
50static DEFINE_SPINLOCK(l2_lock);
51
52static struct drv_data {
Matt Wagantall06e4a1f2012-06-07 18:38:13 -070053 struct acpu_level *acpu_freq_tbl;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080054 const struct l2_level *l2_freq_tbl;
55 struct scalable *scalable;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070056 struct hfpll_data *hfpll_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080057 u32 bus_perf_client;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070058 struct msm_bus_scale_pdata *bus_scale;
Matt Wagantall9515bc22012-07-19 18:13:40 -070059 int boost_uv;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080060 struct device *dev;
61} drv;
62
63static unsigned long acpuclk_krait_get_rate(int cpu)
64{
65 return drv.scalable[cpu].cur_speed->khz;
66}
67
68/* Select a source on the primary MUX. */
69static void set_pri_clk_src(struct scalable *sc, u32 pri_src_sel)
70{
71 u32 regval;
72
73 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
74 regval &= ~0x3;
75 regval |= (pri_src_sel & 0x3);
76 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
77 /* Wait for switch to complete. */
78 mb();
79 udelay(1);
80}
81
82/* Select a source on the secondary MUX. */
Matt Wagantalla133dbf2012-09-27 19:56:57 -070083static void __cpuinit set_sec_clk_src(struct scalable *sc, u32 sec_src_sel)
Matt Wagantalle9b715a2012-01-04 18:16:14 -080084{
85 u32 regval;
86
Matt Wagantall7c705e72012-09-25 12:47:24 -070087 /* 8064 Errata: disable sec_src clock gating during switch. */
Matt Wagantalle9b715a2012-01-04 18:16:14 -080088 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Matt Wagantall7c705e72012-09-25 12:47:24 -070089 regval |= SECCLKAGD;
90 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
91
92 /* Program the MUX */
Matt Wagantalle9b715a2012-01-04 18:16:14 -080093 regval &= ~(0x3 << 2);
94 regval |= ((sec_src_sel & 0x3) << 2);
95 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Matt Wagantall7c705e72012-09-25 12:47:24 -070096
97 /* 8064 Errata: re-enabled sec_src clock gating. */
98 regval &= ~SECCLKAGD;
99 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
100
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800101 /* Wait for switch to complete. */
102 mb();
103 udelay(1);
104}
105
Matt Wagantall302d9a32012-07-03 13:37:29 -0700106static int enable_rpm_vreg(struct vreg *vreg)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800107{
Matt Wagantall302d9a32012-07-03 13:37:29 -0700108 int ret = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800109
Matt Wagantall75473eb2012-05-31 15:23:22 -0700110 if (vreg->rpm_reg) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700111 ret = rpm_regulator_enable(vreg->rpm_reg);
112 if (ret)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700113 dev_err(drv.dev, "%s regulator enable failed (%d)\n",
Matt Wagantall302d9a32012-07-03 13:37:29 -0700114 vreg->name, ret);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700115 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700116
117 return ret;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700118}
119
120static void disable_rpm_vreg(struct vreg *vreg)
121{
122 int rc;
123
124 if (vreg->rpm_reg) {
125 rc = rpm_regulator_disable(vreg->rpm_reg);
126 if (rc)
127 dev_err(drv.dev, "%s regulator disable failed (%d)\n",
128 vreg->name, rc);
129 }
130}
131
132/* Enable an already-configured HFPLL. */
133static void hfpll_enable(struct scalable *sc, bool skip_regulators)
134{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800135 if (!skip_regulators) {
136 /* Enable regulators required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700137 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
138 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800139 }
140
141 /* Disable PLL bypass mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700142 writel_relaxed(0x2, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800143
144 /*
145 * H/W requires a 5us delay between disabling the bypass and
146 * de-asserting the reset. Delay 10us just to be safe.
147 */
148 mb();
149 udelay(10);
150
151 /* De-assert active-low PLL reset. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700152 writel_relaxed(0x6, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800153
154 /* Wait for PLL to lock. */
155 mb();
156 udelay(60);
157
158 /* Enable PLL output. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700159 writel_relaxed(0x7, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800160}
161
162/* Disable a HFPLL for power-savings or while it's being reprogrammed. */
163static void hfpll_disable(struct scalable *sc, bool skip_regulators)
164{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800165 /*
166 * Disable the PLL output, disable test mode, enable the bypass mode,
167 * and assert the reset.
168 */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700169 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800170
171 if (!skip_regulators) {
172 /* Remove voltage votes required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700173 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
174 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800175 }
176}
177
178/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
179static void hfpll_set_rate(struct scalable *sc, const struct core_speed *tgt_s)
180{
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700181 void __iomem *base = sc->hfpll_base;
182 u32 regval;
183
184 writel_relaxed(tgt_s->pll_l_val, base + drv.hfpll_data->l_offset);
185
186 if (drv.hfpll_data->has_user_reg) {
187 regval = readl_relaxed(base + drv.hfpll_data->user_offset);
188 if (tgt_s->pll_l_val <= drv.hfpll_data->low_vco_l_max)
189 regval &= ~drv.hfpll_data->user_vco_mask;
190 else
191 regval |= drv.hfpll_data->user_vco_mask;
192 writel_relaxed(regval, base + drv.hfpll_data->user_offset);
193 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800194}
195
196/* Return the L2 speed that should be applied. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700197static unsigned int compute_l2_level(struct scalable *sc, unsigned int vote_l)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800198{
Matt Wagantall600ea502012-06-08 18:49:53 -0700199 unsigned int new_l = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800200 int cpu;
201
202 /* Find max L2 speed vote. */
203 sc->l2_vote = vote_l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800204 for_each_present_cpu(cpu)
205 new_l = max(new_l, drv.scalable[cpu].l2_vote);
206
207 return new_l;
208}
209
210/* Update the bus bandwidth request. */
211static void set_bus_bw(unsigned int bw)
212{
213 int ret;
214
215 /* Update bandwidth if request has changed. This may sleep. */
216 ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw);
217 if (ret)
218 dev_err(drv.dev, "bandwidth request failed (%d)\n", ret);
219}
220
221/* Set the CPU or L2 clock speed. */
222static void set_speed(struct scalable *sc, const struct core_speed *tgt_s)
223{
224 const struct core_speed *strt_s = sc->cur_speed;
225
Stephen Boyd14a47392012-08-06 20:15:15 -0700226 if (strt_s == tgt_s)
227 return;
228
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800229 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
230 /*
231 * Move to an always-on source running at a frequency
232 * that does not require an elevated CPU voltage.
233 */
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800234 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
235
236 /* Re-program HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700237 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800238 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700239 hfpll_enable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800240
241 /* Move to HFPLL. */
242 set_pri_clk_src(sc, tgt_s->pri_src_sel);
243 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800244 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700245 hfpll_disable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800246 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
247 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700248 hfpll_enable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800249 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800250 }
251
252 sc->cur_speed = tgt_s;
253}
254
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700255struct vdd_data {
256 int vdd_mem;
257 int vdd_dig;
258 int vdd_core;
259 int ua_core;
260};
261
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800262/* Apply any per-cpu voltage increases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700263static int increase_vdd(int cpu, struct vdd_data *data,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800264 enum setrate_reason reason)
265{
266 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700267 int rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800268
269 /*
270 * Increase vdd_mem active-set before vdd_dig.
271 * vdd_mem should be >= vdd_dig.
272 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700273 if (data->vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700274 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700275 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800276 if (rc) {
277 dev_err(drv.dev,
278 "vdd_mem (cpu%d) increase failed (%d)\n",
279 cpu, rc);
280 return rc;
281 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700282 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800283 }
284
285 /* Increase vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700286 if (data->vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700287 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700288 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800289 if (rc) {
290 dev_err(drv.dev,
291 "vdd_dig (cpu%d) increase failed (%d)\n",
292 cpu, rc);
293 return rc;
294 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700295 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
296 }
297
298 /* Increase current request. */
299 if (data->ua_core > sc->vreg[VREG_CORE].cur_ua) {
300 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
301 data->ua_core);
302 if (rc < 0) {
303 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
304 sc->vreg[VREG_CORE].name, rc);
305 return rc;
306 }
307 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800308 }
309
310 /*
311 * Update per-CPU core voltage. Don't do this for the hotplug path for
312 * which it should already be correct. Attempting to set it is bad
313 * because we don't know what CPU we are running on at this point, but
314 * the CPU regulator API requires we call it from the affected CPU.
315 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700316 if (data->vdd_core > sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800317 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700318 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
319 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800320 if (rc) {
321 dev_err(drv.dev,
322 "vdd_core (cpu%d) increase failed (%d)\n",
323 cpu, rc);
324 return rc;
325 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700326 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800327 }
328
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700329 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800330}
331
332/* Apply any per-cpu voltage decreases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700333static void decrease_vdd(int cpu, struct vdd_data *data,
334 enum setrate_reason reason)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800335{
336 struct scalable *sc = &drv.scalable[cpu];
337 int ret;
338
339 /*
340 * Update per-CPU core voltage. This must be called on the CPU
341 * that's being affected. Don't do this in the hotplug remove path,
342 * where the rail is off and we're executing on the other CPU.
343 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700344 if (data->vdd_core < sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800345 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700346 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
347 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800348 if (ret) {
349 dev_err(drv.dev,
350 "vdd_core (cpu%d) decrease failed (%d)\n",
351 cpu, ret);
352 return;
353 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700354 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
355 }
356
357 /* Decrease current request. */
358 if (data->ua_core < sc->vreg[VREG_CORE].cur_ua) {
359 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
360 data->ua_core);
361 if (ret < 0) {
362 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
363 sc->vreg[VREG_CORE].name, ret);
364 return;
365 }
366 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800367 }
368
369 /* Decrease vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700370 if (data->vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700371 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700372 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800373 if (ret) {
374 dev_err(drv.dev,
375 "vdd_dig (cpu%d) decrease failed (%d)\n",
376 cpu, ret);
377 return;
378 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700379 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800380 }
381
382 /*
383 * Decrease vdd_mem active-set after vdd_dig.
384 * vdd_mem should be >= vdd_dig.
385 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700386 if (data->vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700387 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700388 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800389 if (ret) {
390 dev_err(drv.dev,
391 "vdd_mem (cpu%d) decrease failed (%d)\n",
392 cpu, ret);
393 return;
394 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700395 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800396 }
397}
398
399static int calculate_vdd_mem(const struct acpu_level *tgt)
400{
Matt Wagantall600ea502012-06-08 18:49:53 -0700401 return drv.l2_freq_tbl[tgt->l2_level].vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800402}
403
Matt Wagantall72a38002012-07-18 13:42:55 -0700404static int get_src_dig(const struct core_speed *s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800405{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700406 const int *hfpll_vdd = drv.hfpll_data->vdd;
407 const u32 low_vdd_l_max = drv.hfpll_data->low_vdd_l_max;
Matt Wagantall87465f52012-07-23 22:03:06 -0700408 const u32 nom_vdd_l_max = drv.hfpll_data->nom_vdd_l_max;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800409
Matt Wagantall72a38002012-07-18 13:42:55 -0700410 if (s->src != HFPLL)
411 return hfpll_vdd[HFPLL_VDD_NONE];
Matt Wagantall87465f52012-07-23 22:03:06 -0700412 else if (s->pll_l_val > nom_vdd_l_max)
413 return hfpll_vdd[HFPLL_VDD_HIGH];
Matt Wagantall72a38002012-07-18 13:42:55 -0700414 else if (s->pll_l_val > low_vdd_l_max)
415 return hfpll_vdd[HFPLL_VDD_NOM];
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800416 else
Matt Wagantall72a38002012-07-18 13:42:55 -0700417 return hfpll_vdd[HFPLL_VDD_LOW];
418}
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800419
Matt Wagantall72a38002012-07-18 13:42:55 -0700420static int calculate_vdd_dig(const struct acpu_level *tgt)
421{
422 int l2_pll_vdd_dig, cpu_pll_vdd_dig;
423
424 l2_pll_vdd_dig = get_src_dig(&drv.l2_freq_tbl[tgt->l2_level].speed);
425 cpu_pll_vdd_dig = get_src_dig(&tgt->speed);
426
427 return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig,
428 max(l2_pll_vdd_dig, cpu_pll_vdd_dig));
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800429}
430
Matt Wagantall9515bc22012-07-19 18:13:40 -0700431static bool enable_boost = true;
432module_param_named(boost, enable_boost, bool, S_IRUGO | S_IWUSR);
433
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800434static int calculate_vdd_core(const struct acpu_level *tgt)
435{
Matt Wagantall9515bc22012-07-19 18:13:40 -0700436 return tgt->vdd_core + (enable_boost ? drv.boost_uv : 0);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800437}
438
439/* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */
440static int acpuclk_krait_set_rate(int cpu, unsigned long rate,
441 enum setrate_reason reason)
442{
443 const struct core_speed *strt_acpu_s, *tgt_acpu_s;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800444 const struct acpu_level *tgt;
Matt Wagantall600ea502012-06-08 18:49:53 -0700445 int tgt_l2_l;
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700446 struct vdd_data vdd_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800447 unsigned long flags;
448 int rc = 0;
449
Matt Wagantall5941a332012-07-10 23:20:44 -0700450 if (cpu > num_possible_cpus())
451 return -EINVAL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800452
453 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
454 mutex_lock(&driver_lock);
455
456 strt_acpu_s = drv.scalable[cpu].cur_speed;
457
458 /* Return early if rate didn't change. */
459 if (rate == strt_acpu_s->khz)
460 goto out;
461
462 /* Find target frequency. */
463 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
464 if (tgt->speed.khz == rate) {
465 tgt_acpu_s = &tgt->speed;
466 break;
467 }
468 }
469 if (tgt->speed.khz == 0) {
470 rc = -EINVAL;
471 goto out;
472 }
473
474 /* Calculate voltage requirements for the current CPU. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700475 vdd_data.vdd_mem = calculate_vdd_mem(tgt);
476 vdd_data.vdd_dig = calculate_vdd_dig(tgt);
477 vdd_data.vdd_core = calculate_vdd_core(tgt);
478 vdd_data.ua_core = tgt->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800479
Stephen Boydc13b6792012-09-14 11:25:34 -0700480 /* Disable AVS before voltage switch */
481 if (reason == SETRATE_CPUFREQ && drv.scalable[cpu].avs_enabled) {
482 AVS_DISABLE(cpu);
483 drv.scalable[cpu].avs_enabled = false;
484 }
485
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800486 /* Increase VDD levels if needed. */
487 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700488 rc = increase_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800489 if (rc)
490 goto out;
491 }
492
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700493 dev_dbg(drv.dev, "Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
494 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800495
496 /* Set the new CPU speed. */
497 set_speed(&drv.scalable[cpu], tgt_acpu_s);
498
499 /*
500 * Update the L2 vote and apply the rate change. A spinlock is
501 * necessary to ensure L2 rate is calculated and set atomically
502 * with the CPU frequency, even if acpuclk_krait_set_rate() is
503 * called from an atomic context and the driver_lock mutex is not
504 * acquired.
505 */
506 spin_lock_irqsave(&l2_lock, flags);
507 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
Matt Wagantall600ea502012-06-08 18:49:53 -0700508 set_speed(&drv.scalable[L2], &drv.l2_freq_tbl[tgt_l2_l].speed);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800509 spin_unlock_irqrestore(&l2_lock, flags);
510
511 /* Nothing else to do for power collapse or SWFI. */
512 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
513 goto out;
514
515 /* Update bus bandwith request. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700516 set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800517
518 /* Drop VDD levels if we can. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700519 decrease_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800520
Stephen Boydc13b6792012-09-14 11:25:34 -0700521 /* Re-enable AVS */
522 if (reason == SETRATE_CPUFREQ && tgt->avsdscr_setting) {
523 AVS_ENABLE(cpu, tgt->avsdscr_setting);
524 drv.scalable[cpu].avs_enabled = true;
525 }
526
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700527 dev_dbg(drv.dev, "ACPU%d speed change complete\n", cpu);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800528
529out:
530 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
531 mutex_unlock(&driver_lock);
532 return rc;
533}
534
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700535static struct acpuclk_data acpuclk_krait_data = {
536 .set_rate = acpuclk_krait_set_rate,
537 .get_rate = acpuclk_krait_get_rate,
538};
539
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800540/* Initialize a HFPLL at a given rate and enable it. */
Iliyan Malchev16aea522012-10-16 00:35:07 -0700541static void __cpuinit hfpll_init(struct scalable *sc,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800542 const struct core_speed *tgt_s)
543{
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700544 dev_dbg(drv.dev, "Initializing HFPLL%d\n", sc - drv.scalable);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800545
546 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700547 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800548
549 /* Configure PLL parameters for integer mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700550 writel_relaxed(drv.hfpll_data->config_val,
551 sc->hfpll_base + drv.hfpll_data->config_offset);
552 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->m_offset);
553 writel_relaxed(1, sc->hfpll_base + drv.hfpll_data->n_offset);
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700554 if (drv.hfpll_data->has_user_reg)
555 writel_relaxed(drv.hfpll_data->user_val,
556 sc->hfpll_base + drv.hfpll_data->user_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800557
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700558 /* Program droop controller, if supported */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700559 if (drv.hfpll_data->has_droop_ctl)
560 writel_relaxed(drv.hfpll_data->droop_val,
561 sc->hfpll_base + drv.hfpll_data->droop_offset);
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700562
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800563 /* Set an initial rate and enable the PLL. */
564 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700565 hfpll_enable(sc, false);
566}
567
Matt Wagantall302d9a32012-07-03 13:37:29 -0700568static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700569 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700570{
571 int ret;
572
573 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700574 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700575
576 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
577 sc->vreg[vreg].name);
578 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700579 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
580 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
581 sc->vreg[vreg].name, ret);
582 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700583 }
584
585 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
586 sc->vreg[vreg].max_vdd);
587 if (ret) {
588 dev_err(drv.dev, "%s initialization failed (%d)\n",
589 sc->vreg[vreg].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700590 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700591 }
592 sc->vreg[vreg].cur_vdd = vdd;
593
Matt Wagantall302d9a32012-07-03 13:37:29 -0700594 if (enable) {
595 ret = enable_rpm_vreg(&sc->vreg[vreg]);
596 if (ret)
597 goto err_conf;
598 }
599
600 return 0;
601
602err_conf:
603 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
604err_get:
605 return ret;
606}
607
608static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
609 enum vregs vreg)
610{
611 if (!sc->vreg[vreg].rpm_reg)
612 return;
613
614 disable_rpm_vreg(&sc->vreg[vreg]);
615 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800616}
617
618/* Voltage regulator initialization. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700619static int __cpuinit regulator_init(struct scalable *sc,
620 const struct acpu_level *acpu_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800621{
Matt Wagantall754ee272012-06-18 13:40:26 -0700622 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800623
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700624 vdd_mem = calculate_vdd_mem(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700625 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
626 if (ret)
627 goto err_mem;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700628
629 vdd_dig = calculate_vdd_dig(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700630 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
631 if (ret)
632 goto err_dig;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700633
Matt Wagantall302d9a32012-07-03 13:37:29 -0700634 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700635 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700636 if (ret)
637 goto err_hfpll_a;
638 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700639 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700640 if (ret)
641 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700642
Matt Wagantall754ee272012-06-18 13:40:26 -0700643 /* Setup Krait CPU regulators and initial core voltage. */
644 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
645 sc->vreg[VREG_CORE].name);
646 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700647 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
648 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
649 sc->vreg[VREG_CORE].name, ret);
650 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700651 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700652 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
653 acpu_level->ua_core);
654 if (ret < 0) {
655 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
656 sc->vreg[VREG_CORE].name, ret);
657 goto err_core_conf;
658 }
659 sc->vreg[VREG_CORE].cur_ua = acpu_level->ua_core;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700660 vdd_core = calculate_vdd_core(acpu_level);
Matt Wagantall754ee272012-06-18 13:40:26 -0700661 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
662 sc->vreg[VREG_CORE].max_vdd);
663 if (ret) {
664 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
665 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700666 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700667 }
668 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
Matt Wagantall754ee272012-06-18 13:40:26 -0700669 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
670 if (ret) {
671 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
672 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700673 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800674 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700675
676 return 0;
677
678err_core_conf:
679 regulator_put(sc->vreg[VREG_CORE].reg);
680err_core_get:
681 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
682err_hfpll_b:
683 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
684err_hfpll_a:
685 rpm_regulator_cleanup(sc, VREG_DIG);
686err_dig:
687 rpm_regulator_cleanup(sc, VREG_MEM);
688err_mem:
689 return ret;
690}
691
692static void __cpuinit regulator_cleanup(struct scalable *sc)
693{
694 regulator_disable(sc->vreg[VREG_CORE].reg);
695 regulator_put(sc->vreg[VREG_CORE].reg);
696 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
697 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
698 rpm_regulator_cleanup(sc, VREG_DIG);
699 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800700}
701
702/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700703static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700704 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800705{
706 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700707 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800708
709 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700710 if (sc->aux_clk_sel_phys) {
711 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700712 if (!aux_reg)
713 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700714 writel_relaxed(sc->aux_clk_sel, aux_reg);
715 iounmap(aux_reg);
716 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800717
718 /* Switch away from the HFPLL while it's re-initialized. */
Matt Wagantalla133dbf2012-09-27 19:56:57 -0700719 set_sec_clk_src(sc, sc->sec_clk_sel);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800720 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
721 hfpll_init(sc, tgt_s);
722
723 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
724 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
725 regval &= ~(0x3 << 6);
726 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
727
728 /* Switch to the target clock source. */
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800729 set_pri_clk_src(sc, tgt_s->pri_src_sel);
730 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700731
732 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800733}
734
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700735static void __cpuinit fill_cur_core_speed(struct core_speed *s,
736 struct scalable *sc)
737{
738 s->pri_src_sel = get_l2_indirect_reg(sc->l2cpmr_iaddr) & 0x3;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700739 s->pll_l_val = readl_relaxed(sc->hfpll_base + drv.hfpll_data->l_offset);
740}
741
742static bool __cpuinit speed_equal(const struct core_speed *s1,
743 const struct core_speed *s2)
744{
745 return (s1->pri_src_sel == s2->pri_src_sel &&
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700746 s1->pll_l_val == s2->pll_l_val);
747}
748
749static const struct acpu_level __cpuinit *find_cur_acpu_level(int cpu)
750{
751 struct scalable *sc = &drv.scalable[cpu];
752 const struct acpu_level *l;
753 struct core_speed cur_speed;
754
755 fill_cur_core_speed(&cur_speed, sc);
756 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
757 if (speed_equal(&l->speed, &cur_speed))
758 return l;
759 return NULL;
760}
761
762static const struct l2_level __init *find_cur_l2_level(void)
763{
764 struct scalable *sc = &drv.scalable[L2];
765 const struct l2_level *l;
766 struct core_speed cur_speed;
767
768 fill_cur_core_speed(&cur_speed, sc);
769 for (l = drv.l2_freq_tbl; l->speed.khz != 0; l++)
770 if (speed_equal(&l->speed, &cur_speed))
771 return l;
772 return NULL;
773}
774
775static const struct acpu_level __cpuinit *find_min_acpu_level(void)
776{
777 struct acpu_level *l;
778
779 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
780 if (l->use_for_scaling)
781 return l;
782
783 return NULL;
784}
785
Matt Wagantall302d9a32012-07-03 13:37:29 -0700786static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800787{
Matt Wagantall754ee272012-06-18 13:40:26 -0700788 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700789 const struct acpu_level *acpu_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700790 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800791
Matt Wagantall754ee272012-06-18 13:40:26 -0700792 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700793 if (!sc->hfpll_base) {
794 ret = -ENOMEM;
795 goto err_ioremap;
796 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700797
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700798 acpu_level = find_cur_acpu_level(cpu);
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700799 if (!acpu_level) {
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700800 acpu_level = find_min_acpu_level();
801 if (!acpu_level) {
802 ret = -ENODEV;
803 goto err_table;
804 }
805 dev_dbg(drv.dev, "CPU%d is running at an unknown rate. Defaulting to %lu KHz.\n",
806 cpu, acpu_level->speed.khz);
807 } else {
808 dev_dbg(drv.dev, "CPU%d is running at %lu KHz\n", cpu,
809 acpu_level->speed.khz);
810 }
811
812 ret = regulator_init(sc, acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700813 if (ret)
814 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700815
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700816 ret = init_clock_sources(sc, &acpu_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700817 if (ret)
818 goto err_clocks;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700819
820 sc->l2_vote = acpu_level->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700821 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700822
823 return 0;
824
825err_clocks:
826 regulator_cleanup(sc);
827err_regulators:
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700828err_table:
Matt Wagantall302d9a32012-07-03 13:37:29 -0700829 iounmap(sc->hfpll_base);
830err_ioremap:
831 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800832}
833
834/* Register with bus driver. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700835static void __init bus_init(const struct l2_level *l2_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800836{
837 int ret;
838
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700839 drv.bus_perf_client = msm_bus_scale_register_client(drv.bus_scale);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800840 if (!drv.bus_perf_client) {
841 dev_err(drv.dev, "unable to register bus client\n");
842 BUG();
843 }
844
Matt Wagantall754ee272012-06-18 13:40:26 -0700845 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700846 l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800847 if (ret)
848 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
849}
850
851#ifdef CONFIG_CPU_FREQ_MSM
852static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
853
854static void __init cpufreq_table_init(void)
855{
856 int cpu;
857
858 for_each_possible_cpu(cpu) {
859 int i, freq_cnt = 0;
860 /* Construct the freq_table tables from acpu_freq_tbl. */
861 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
862 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
863 if (drv.acpu_freq_tbl[i].use_for_scaling) {
864 freq_table[cpu][freq_cnt].index = freq_cnt;
865 freq_table[cpu][freq_cnt].frequency
866 = drv.acpu_freq_tbl[i].speed.khz;
867 freq_cnt++;
868 }
869 }
870 /* freq_table not big enough to store all usable freqs. */
871 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
872
873 freq_table[cpu][freq_cnt].index = freq_cnt;
874 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
875
876 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
877 cpu, freq_cnt);
878
879 /* Register table with CPUFreq. */
880 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
881 }
882}
883#else
884static void __init cpufreq_table_init(void) {}
885#endif
886
Steve Mucklea9aac292012-11-02 15:41:00 -0700887static void __init dcvs_freq_init(void)
888{
889 int i;
890
891 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0; i++)
892 if (drv.acpu_freq_tbl[i].use_for_scaling)
893 msm_dcvs_register_cpu_freq(
894 drv.acpu_freq_tbl[i].speed.khz,
895 drv.acpu_freq_tbl[i].vdd_core / 1000);
896}
897
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800898static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
899 unsigned long action, void *hcpu)
900{
901 static int prev_khz[NR_CPUS];
902 int rc, cpu = (int)hcpu;
903 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700904 unsigned long hot_unplug_khz = acpuclk_krait_data.power_collapse_khz;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800905
906 switch (action & ~CPU_TASKS_FROZEN) {
907 case CPU_DEAD:
908 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
909 /* Fall through. */
910 case CPU_UP_CANCELED:
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700911 acpuclk_krait_set_rate(cpu, hot_unplug_khz, SETRATE_HOTPLUG);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800912 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
913 break;
914 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700915 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700916 rc = per_cpu_init(cpu);
917 if (rc)
918 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700919 break;
920 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800921 if (WARN_ON(!prev_khz[cpu]))
922 return NOTIFY_BAD;
923 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700924 sc->vreg[VREG_CORE].cur_ua);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800925 if (rc < 0)
926 return NOTIFY_BAD;
927 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
928 break;
929 default:
930 break;
931 }
932
933 return NOTIFY_OK;
934}
935
936static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
937 .notifier_call = acpuclk_cpu_callback,
938};
939
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700940static const int krait_needs_vmin(void)
941{
942 switch (read_cpuid_id()) {
943 case 0x511F04D0: /* KR28M2A20 */
944 case 0x511F04D1: /* KR28M2A21 */
945 case 0x510F06F0: /* KR28M4A10 */
946 return 1;
947 default:
948 return 0;
949 };
950}
951
952static void krait_apply_vmin(struct acpu_level *tbl)
953{
Stephen Boydc13b6792012-09-14 11:25:34 -0700954 for (; tbl->speed.khz != 0; tbl++) {
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700955 if (tbl->vdd_core < 1150000)
956 tbl->vdd_core = 1150000;
Stephen Boydc13b6792012-09-14 11:25:34 -0700957 tbl->avsdscr_setting = 0;
958 }
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700959}
960
Patrick Daly02db5a82012-08-24 14:22:06 -0700961static int __init get_speed_bin(u32 pte_efuse)
962{
963 uint32_t speed_bin;
964
965 speed_bin = pte_efuse & 0xF;
966 if (speed_bin == 0xF)
967 speed_bin = (pte_efuse >> 4) & 0xF;
968
969 if (speed_bin == 0xF) {
970 speed_bin = 0;
971 dev_warn(drv.dev, "SPEED BIN: Defaulting to %d\n", speed_bin);
972 } else {
973 dev_info(drv.dev, "SPEED BIN: %d\n", speed_bin);
974 }
975
976 return speed_bin;
977}
978
979static int __init get_pvs_bin(u32 pte_efuse)
980{
981 uint32_t pvs_bin;
982
983 pvs_bin = (pte_efuse >> 10) & 0x7;
984 if (pvs_bin == 0x7)
985 pvs_bin = (pte_efuse >> 13) & 0x7;
986
987 if (pvs_bin == 0x7) {
988 pvs_bin = 0;
989 dev_warn(drv.dev, "ACPU PVS: Defaulting to %d\n", pvs_bin);
990 } else {
991 dev_info(drv.dev, "ACPU PVS: %d\n", pvs_bin);
992 }
993
994 return pvs_bin;
995}
996
997static struct pvs_table * __init select_freq_plan(u32 pte_efuse_phys,
998 struct pvs_table (*pvs_tables)[NUM_PVS])
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800999{
Matt Wagantall519e94f2012-09-17 17:51:06 -07001000 void __iomem *pte_efuse;
Patrick Daly02db5a82012-08-24 14:22:06 -07001001 u32 pte_efuse_val, tbl_idx, bin_idx;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001002
Matt Wagantall519e94f2012-09-17 17:51:06 -07001003 pte_efuse = ioremap(pte_efuse_phys, 4);
Patrick Daly02db5a82012-08-24 14:22:06 -07001004 if (!pte_efuse) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001005 dev_err(drv.dev, "Unable to map QFPROM base\n");
Patrick Daly02db5a82012-08-24 14:22:06 -07001006 return NULL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001007 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001008
Patrick Daly02db5a82012-08-24 14:22:06 -07001009 pte_efuse_val = readl_relaxed(pte_efuse);
1010 iounmap(pte_efuse);
1011
1012 /* Select frequency tables. */
1013 bin_idx = get_speed_bin(pte_efuse_val);
1014 tbl_idx = get_pvs_bin(pte_efuse_val);
1015
1016 return &pvs_tables[bin_idx][tbl_idx];
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001017}
Matt Wagantall06e4a1f2012-06-07 18:38:13 -07001018
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001019static void __init drv_data_init(struct device *dev,
1020 const struct acpuclk_krait_params *params)
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001021{
Patrick Daly02db5a82012-08-24 14:22:06 -07001022 struct pvs_table *pvs;
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001023
1024 drv.dev = dev;
1025 drv.scalable = kmemdup(params->scalable, params->scalable_size,
1026 GFP_KERNEL);
1027 BUG_ON(!drv.scalable);
1028
1029 drv.hfpll_data = kmemdup(params->hfpll_data, sizeof(*drv.hfpll_data),
1030 GFP_KERNEL);
1031 BUG_ON(!drv.hfpll_data);
1032
1033 drv.l2_freq_tbl = kmemdup(params->l2_freq_tbl, params->l2_freq_tbl_size,
1034 GFP_KERNEL);
1035 BUG_ON(!drv.l2_freq_tbl);
1036
1037 drv.bus_scale = kmemdup(params->bus_scale, sizeof(*drv.bus_scale),
1038 GFP_KERNEL);
1039 BUG_ON(!drv.bus_scale);
1040 drv.bus_scale->usecase = kmemdup(drv.bus_scale->usecase,
1041 drv.bus_scale->num_usecases * sizeof(*drv.bus_scale->usecase),
1042 GFP_KERNEL);
1043 BUG_ON(!drv.bus_scale->usecase);
1044
Patrick Daly02db5a82012-08-24 14:22:06 -07001045 pvs = select_freq_plan(params->pte_efuse_phys, params->pvs_tables);
1046 BUG_ON(!pvs->table);
1047
1048 drv.acpu_freq_tbl = kmemdup(pvs->table, pvs->size, GFP_KERNEL);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001049 BUG_ON(!drv.acpu_freq_tbl);
Patrick Daly02db5a82012-08-24 14:22:06 -07001050 drv.boost_uv = pvs->boost_uv;
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001051
1052 acpuclk_krait_data.power_collapse_khz = params->stby_khz;
1053 acpuclk_krait_data.wait_for_irq_khz = params->stby_khz;
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001054}
1055
1056static void __init hw_init(void)
1057{
1058 struct scalable *l2 = &drv.scalable[L2];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001059 const struct l2_level *l2_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -07001060 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001061
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001062 if (krait_needs_vmin())
1063 krait_apply_vmin(drv.acpu_freq_tbl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001064
Matt Wagantall754ee272012-06-18 13:40:26 -07001065 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
1066 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -07001067
Matt Wagantall302d9a32012-07-03 13:37:29 -07001068 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
1069 l2->vreg[VREG_HFPLL_A].max_vdd, false);
1070 BUG_ON(rc);
1071 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
1072 l2->vreg[VREG_HFPLL_B].max_vdd, false);
1073 BUG_ON(rc);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001074
1075 l2_level = find_cur_l2_level();
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001076 if (!l2_level) {
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001077 l2_level = drv.l2_freq_tbl;
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001078 dev_dbg(drv.dev, "L2 is running at an unknown rate. Defaulting to %lu KHz.\n",
1079 l2_level->speed.khz);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001080 } else {
1081 dev_dbg(drv.dev, "L2 is running at %lu KHz\n",
1082 l2_level->speed.khz);
1083 }
1084
1085 rc = init_clock_sources(l2, &l2_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -07001086 BUG_ON(rc);
1087
1088 for_each_online_cpu(cpu) {
1089 rc = per_cpu_init(cpu);
1090 BUG_ON(rc);
1091 }
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001092
1093 bus_init(l2_level);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001094}
1095
1096int __init acpuclk_krait_init(struct device *dev,
1097 const struct acpuclk_krait_params *params)
1098{
1099 drv_data_init(dev, params);
1100 hw_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001101
1102 cpufreq_table_init();
Steve Mucklea9aac292012-11-02 15:41:00 -07001103 dcvs_freq_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001104 acpuclk_register(&acpuclk_krait_data);
1105 register_hotcpu_notifier(&acpuclk_cpu_notifier);
1106
1107 return 0;
1108}