blob: b3e61451a10a9f29fa916c059c7d32c68a33bc77 [file] [log] [blame]
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001/*
Vikram Mulukutla81796c02012-10-19 07:42:45 -07002 * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
Matt Wagantalle9b715a2012-01-04 18:16:14 -08003 *
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. */
Vikram Mulukutla81796c02012-10-19 07:42:45 -0700222static void set_speed(struct scalable *sc, const struct core_speed *tgt_s,
223 bool skip_regulators)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800224{
225 const struct core_speed *strt_s = sc->cur_speed;
226
Stephen Boyd14a47392012-08-06 20:15:15 -0700227 if (strt_s == tgt_s)
228 return;
229
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800230 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
231 /*
232 * Move to an always-on source running at a frequency
233 * that does not require an elevated CPU voltage.
234 */
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800235 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
236
237 /* Re-program HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700238 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800239 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700240 hfpll_enable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800241
242 /* Move to HFPLL. */
243 set_pri_clk_src(sc, tgt_s->pri_src_sel);
244 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800245 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Vikram Mulukutla81796c02012-10-19 07:42:45 -0700246 hfpll_disable(sc, skip_regulators);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800247 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
248 hfpll_set_rate(sc, tgt_s);
Vikram Mulukutla81796c02012-10-19 07:42:45 -0700249 hfpll_enable(sc, skip_regulators);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800250 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800251 }
252
253 sc->cur_speed = tgt_s;
254}
255
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700256struct vdd_data {
257 int vdd_mem;
258 int vdd_dig;
259 int vdd_core;
260 int ua_core;
261};
262
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800263/* Apply any per-cpu voltage increases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700264static int increase_vdd(int cpu, struct vdd_data *data,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800265 enum setrate_reason reason)
266{
267 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700268 int rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800269
270 /*
271 * Increase vdd_mem active-set before vdd_dig.
272 * vdd_mem should be >= vdd_dig.
273 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700274 if (data->vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700275 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700276 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800277 if (rc) {
278 dev_err(drv.dev,
279 "vdd_mem (cpu%d) increase failed (%d)\n",
280 cpu, rc);
281 return rc;
282 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700283 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800284 }
285
286 /* Increase vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700287 if (data->vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700288 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700289 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800290 if (rc) {
291 dev_err(drv.dev,
292 "vdd_dig (cpu%d) increase failed (%d)\n",
293 cpu, rc);
294 return rc;
295 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700296 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
297 }
298
299 /* Increase current request. */
300 if (data->ua_core > sc->vreg[VREG_CORE].cur_ua) {
301 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
302 data->ua_core);
303 if (rc < 0) {
304 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
305 sc->vreg[VREG_CORE].name, rc);
306 return rc;
307 }
308 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800309 }
310
311 /*
312 * Update per-CPU core voltage. Don't do this for the hotplug path for
313 * which it should already be correct. Attempting to set it is bad
314 * because we don't know what CPU we are running on at this point, but
315 * the CPU regulator API requires we call it from the affected CPU.
316 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700317 if (data->vdd_core > sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800318 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700319 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
320 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800321 if (rc) {
322 dev_err(drv.dev,
323 "vdd_core (cpu%d) increase failed (%d)\n",
324 cpu, rc);
325 return rc;
326 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700327 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800328 }
329
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700330 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800331}
332
333/* Apply any per-cpu voltage decreases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700334static void decrease_vdd(int cpu, struct vdd_data *data,
335 enum setrate_reason reason)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800336{
337 struct scalable *sc = &drv.scalable[cpu];
338 int ret;
339
340 /*
341 * Update per-CPU core voltage. This must be called on the CPU
342 * that's being affected. Don't do this in the hotplug remove path,
343 * where the rail is off and we're executing on the other CPU.
344 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700345 if (data->vdd_core < sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800346 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700347 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
348 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800349 if (ret) {
350 dev_err(drv.dev,
351 "vdd_core (cpu%d) decrease failed (%d)\n",
352 cpu, ret);
353 return;
354 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700355 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
356 }
357
358 /* Decrease current request. */
359 if (data->ua_core < sc->vreg[VREG_CORE].cur_ua) {
360 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
361 data->ua_core);
362 if (ret < 0) {
363 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
364 sc->vreg[VREG_CORE].name, ret);
365 return;
366 }
367 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800368 }
369
370 /* Decrease vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700371 if (data->vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700372 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700373 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800374 if (ret) {
375 dev_err(drv.dev,
376 "vdd_dig (cpu%d) decrease failed (%d)\n",
377 cpu, ret);
378 return;
379 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700380 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800381 }
382
383 /*
384 * Decrease vdd_mem active-set after vdd_dig.
385 * vdd_mem should be >= vdd_dig.
386 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700387 if (data->vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700388 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700389 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800390 if (ret) {
391 dev_err(drv.dev,
392 "vdd_mem (cpu%d) decrease failed (%d)\n",
393 cpu, ret);
394 return;
395 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700396 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800397 }
398}
399
400static int calculate_vdd_mem(const struct acpu_level *tgt)
401{
Matt Wagantall600ea502012-06-08 18:49:53 -0700402 return drv.l2_freq_tbl[tgt->l2_level].vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800403}
404
Matt Wagantall72a38002012-07-18 13:42:55 -0700405static int get_src_dig(const struct core_speed *s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800406{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700407 const int *hfpll_vdd = drv.hfpll_data->vdd;
408 const u32 low_vdd_l_max = drv.hfpll_data->low_vdd_l_max;
Matt Wagantall87465f52012-07-23 22:03:06 -0700409 const u32 nom_vdd_l_max = drv.hfpll_data->nom_vdd_l_max;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800410
Matt Wagantall72a38002012-07-18 13:42:55 -0700411 if (s->src != HFPLL)
412 return hfpll_vdd[HFPLL_VDD_NONE];
Matt Wagantall87465f52012-07-23 22:03:06 -0700413 else if (s->pll_l_val > nom_vdd_l_max)
414 return hfpll_vdd[HFPLL_VDD_HIGH];
Matt Wagantall72a38002012-07-18 13:42:55 -0700415 else if (s->pll_l_val > low_vdd_l_max)
416 return hfpll_vdd[HFPLL_VDD_NOM];
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800417 else
Matt Wagantall72a38002012-07-18 13:42:55 -0700418 return hfpll_vdd[HFPLL_VDD_LOW];
419}
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800420
Matt Wagantall72a38002012-07-18 13:42:55 -0700421static int calculate_vdd_dig(const struct acpu_level *tgt)
422{
423 int l2_pll_vdd_dig, cpu_pll_vdd_dig;
424
425 l2_pll_vdd_dig = get_src_dig(&drv.l2_freq_tbl[tgt->l2_level].speed);
426 cpu_pll_vdd_dig = get_src_dig(&tgt->speed);
427
428 return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig,
429 max(l2_pll_vdd_dig, cpu_pll_vdd_dig));
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800430}
431
Matt Wagantall9515bc22012-07-19 18:13:40 -0700432static bool enable_boost = true;
433module_param_named(boost, enable_boost, bool, S_IRUGO | S_IWUSR);
434
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800435static int calculate_vdd_core(const struct acpu_level *tgt)
436{
Matt Wagantall9515bc22012-07-19 18:13:40 -0700437 return tgt->vdd_core + (enable_boost ? drv.boost_uv : 0);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800438}
439
440/* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */
441static int acpuclk_krait_set_rate(int cpu, unsigned long rate,
442 enum setrate_reason reason)
443{
444 const struct core_speed *strt_acpu_s, *tgt_acpu_s;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800445 const struct acpu_level *tgt;
Matt Wagantall600ea502012-06-08 18:49:53 -0700446 int tgt_l2_l;
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700447 struct vdd_data vdd_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800448 unsigned long flags;
Vikram Mulukutla81796c02012-10-19 07:42:45 -0700449 bool skip_regulators;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800450 int rc = 0;
451
Matt Wagantall5941a332012-07-10 23:20:44 -0700452 if (cpu > num_possible_cpus())
453 return -EINVAL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800454
455 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
456 mutex_lock(&driver_lock);
457
458 strt_acpu_s = drv.scalable[cpu].cur_speed;
459
460 /* Return early if rate didn't change. */
461 if (rate == strt_acpu_s->khz)
462 goto out;
463
464 /* Find target frequency. */
465 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
466 if (tgt->speed.khz == rate) {
467 tgt_acpu_s = &tgt->speed;
468 break;
469 }
470 }
471 if (tgt->speed.khz == 0) {
472 rc = -EINVAL;
473 goto out;
474 }
475
476 /* Calculate voltage requirements for the current CPU. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700477 vdd_data.vdd_mem = calculate_vdd_mem(tgt);
478 vdd_data.vdd_dig = calculate_vdd_dig(tgt);
479 vdd_data.vdd_core = calculate_vdd_core(tgt);
480 vdd_data.ua_core = tgt->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800481
Stephen Boydc13b6792012-09-14 11:25:34 -0700482 /* Disable AVS before voltage switch */
483 if (reason == SETRATE_CPUFREQ && drv.scalable[cpu].avs_enabled) {
484 AVS_DISABLE(cpu);
485 drv.scalable[cpu].avs_enabled = false;
486 }
487
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800488 /* Increase VDD levels if needed. */
489 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700490 rc = increase_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800491 if (rc)
492 goto out;
493 }
494
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700495 dev_dbg(drv.dev, "Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
496 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800497
Vikram Mulukutla81796c02012-10-19 07:42:45 -0700498 /*
499 * If we are setting the rate as part of power collapse or in the resume
500 * path after power collapse, skip the vote for the HFPLL regulators,
501 * which are active-set-only votes that will be removed when apps enters
502 * its sleep set. This is needed to avoid voting for regulators with
503 * sleeping APIs from an atomic context.
504 */
505 skip_regulators = (reason == SETRATE_PC);
506
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800507 /* Set the new CPU speed. */
Vikram Mulukutla81796c02012-10-19 07:42:45 -0700508 set_speed(&drv.scalable[cpu], tgt_acpu_s, skip_regulators);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800509
510 /*
511 * Update the L2 vote and apply the rate change. A spinlock is
512 * necessary to ensure L2 rate is calculated and set atomically
513 * with the CPU frequency, even if acpuclk_krait_set_rate() is
514 * called from an atomic context and the driver_lock mutex is not
515 * acquired.
516 */
517 spin_lock_irqsave(&l2_lock, flags);
518 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
Vikram Mulukutla81796c02012-10-19 07:42:45 -0700519 set_speed(&drv.scalable[L2], &drv.l2_freq_tbl[tgt_l2_l].speed,
520 skip_regulators);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800521 spin_unlock_irqrestore(&l2_lock, flags);
522
523 /* Nothing else to do for power collapse or SWFI. */
524 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
525 goto out;
526
527 /* Update bus bandwith request. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700528 set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800529
530 /* Drop VDD levels if we can. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700531 decrease_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800532
Stephen Boydc13b6792012-09-14 11:25:34 -0700533 /* Re-enable AVS */
534 if (reason == SETRATE_CPUFREQ && tgt->avsdscr_setting) {
535 AVS_ENABLE(cpu, tgt->avsdscr_setting);
536 drv.scalable[cpu].avs_enabled = true;
537 }
538
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700539 dev_dbg(drv.dev, "ACPU%d speed change complete\n", cpu);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800540
541out:
542 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
543 mutex_unlock(&driver_lock);
544 return rc;
545}
546
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700547static struct acpuclk_data acpuclk_krait_data = {
548 .set_rate = acpuclk_krait_set_rate,
549 .get_rate = acpuclk_krait_get_rate,
550};
551
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800552/* Initialize a HFPLL at a given rate and enable it. */
Iliyan Malchev16aea522012-10-16 00:35:07 -0700553static void __cpuinit hfpll_init(struct scalable *sc,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800554 const struct core_speed *tgt_s)
555{
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700556 dev_dbg(drv.dev, "Initializing HFPLL%d\n", sc - drv.scalable);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800557
558 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700559 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800560
561 /* Configure PLL parameters for integer mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700562 writel_relaxed(drv.hfpll_data->config_val,
563 sc->hfpll_base + drv.hfpll_data->config_offset);
564 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->m_offset);
565 writel_relaxed(1, sc->hfpll_base + drv.hfpll_data->n_offset);
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700566 if (drv.hfpll_data->has_user_reg)
567 writel_relaxed(drv.hfpll_data->user_val,
568 sc->hfpll_base + drv.hfpll_data->user_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800569
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700570 /* Program droop controller, if supported */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700571 if (drv.hfpll_data->has_droop_ctl)
572 writel_relaxed(drv.hfpll_data->droop_val,
573 sc->hfpll_base + drv.hfpll_data->droop_offset);
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700574
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800575 /* Set an initial rate and enable the PLL. */
576 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700577 hfpll_enable(sc, false);
578}
579
Matt Wagantall302d9a32012-07-03 13:37:29 -0700580static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700581 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700582{
583 int ret;
584
585 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700586 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700587
588 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
589 sc->vreg[vreg].name);
590 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700591 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
592 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
593 sc->vreg[vreg].name, ret);
594 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700595 }
596
597 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
598 sc->vreg[vreg].max_vdd);
599 if (ret) {
600 dev_err(drv.dev, "%s initialization failed (%d)\n",
601 sc->vreg[vreg].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700602 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700603 }
604 sc->vreg[vreg].cur_vdd = vdd;
605
Matt Wagantall302d9a32012-07-03 13:37:29 -0700606 if (enable) {
607 ret = enable_rpm_vreg(&sc->vreg[vreg]);
608 if (ret)
609 goto err_conf;
610 }
611
612 return 0;
613
614err_conf:
615 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
616err_get:
617 return ret;
618}
619
620static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
621 enum vregs vreg)
622{
623 if (!sc->vreg[vreg].rpm_reg)
624 return;
625
626 disable_rpm_vreg(&sc->vreg[vreg]);
627 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800628}
629
630/* Voltage regulator initialization. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700631static int __cpuinit regulator_init(struct scalable *sc,
632 const struct acpu_level *acpu_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800633{
Matt Wagantall754ee272012-06-18 13:40:26 -0700634 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800635
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700636 vdd_mem = calculate_vdd_mem(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700637 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
638 if (ret)
639 goto err_mem;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700640
641 vdd_dig = calculate_vdd_dig(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700642 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
643 if (ret)
644 goto err_dig;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700645
Matt Wagantall302d9a32012-07-03 13:37:29 -0700646 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700647 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700648 if (ret)
649 goto err_hfpll_a;
650 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700651 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700652 if (ret)
653 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700654
Matt Wagantall754ee272012-06-18 13:40:26 -0700655 /* Setup Krait CPU regulators and initial core voltage. */
656 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
657 sc->vreg[VREG_CORE].name);
658 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700659 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
660 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
661 sc->vreg[VREG_CORE].name, ret);
662 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700663 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700664 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
665 acpu_level->ua_core);
666 if (ret < 0) {
667 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
668 sc->vreg[VREG_CORE].name, ret);
669 goto err_core_conf;
670 }
671 sc->vreg[VREG_CORE].cur_ua = acpu_level->ua_core;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700672 vdd_core = calculate_vdd_core(acpu_level);
Matt Wagantall754ee272012-06-18 13:40:26 -0700673 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
674 sc->vreg[VREG_CORE].max_vdd);
675 if (ret) {
676 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
677 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700678 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700679 }
680 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
Matt Wagantall754ee272012-06-18 13:40:26 -0700681 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
682 if (ret) {
683 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
684 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700685 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800686 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700687
688 return 0;
689
690err_core_conf:
691 regulator_put(sc->vreg[VREG_CORE].reg);
692err_core_get:
693 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
694err_hfpll_b:
695 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
696err_hfpll_a:
697 rpm_regulator_cleanup(sc, VREG_DIG);
698err_dig:
699 rpm_regulator_cleanup(sc, VREG_MEM);
700err_mem:
701 return ret;
702}
703
704static void __cpuinit regulator_cleanup(struct scalable *sc)
705{
706 regulator_disable(sc->vreg[VREG_CORE].reg);
707 regulator_put(sc->vreg[VREG_CORE].reg);
708 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
709 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
710 rpm_regulator_cleanup(sc, VREG_DIG);
711 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800712}
713
714/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700715static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700716 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800717{
718 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700719 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800720
721 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700722 if (sc->aux_clk_sel_phys) {
723 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700724 if (!aux_reg)
725 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700726 writel_relaxed(sc->aux_clk_sel, aux_reg);
727 iounmap(aux_reg);
728 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800729
730 /* Switch away from the HFPLL while it's re-initialized. */
Matt Wagantalla133dbf2012-09-27 19:56:57 -0700731 set_sec_clk_src(sc, sc->sec_clk_sel);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800732 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
733 hfpll_init(sc, tgt_s);
734
735 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
736 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
737 regval &= ~(0x3 << 6);
738 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
739
740 /* Switch to the target clock source. */
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800741 set_pri_clk_src(sc, tgt_s->pri_src_sel);
742 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700743
744 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800745}
746
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700747static void __cpuinit fill_cur_core_speed(struct core_speed *s,
748 struct scalable *sc)
749{
750 s->pri_src_sel = get_l2_indirect_reg(sc->l2cpmr_iaddr) & 0x3;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700751 s->pll_l_val = readl_relaxed(sc->hfpll_base + drv.hfpll_data->l_offset);
752}
753
754static bool __cpuinit speed_equal(const struct core_speed *s1,
755 const struct core_speed *s2)
756{
757 return (s1->pri_src_sel == s2->pri_src_sel &&
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700758 s1->pll_l_val == s2->pll_l_val);
759}
760
761static const struct acpu_level __cpuinit *find_cur_acpu_level(int cpu)
762{
763 struct scalable *sc = &drv.scalable[cpu];
764 const struct acpu_level *l;
765 struct core_speed cur_speed;
766
767 fill_cur_core_speed(&cur_speed, sc);
768 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
769 if (speed_equal(&l->speed, &cur_speed))
770 return l;
771 return NULL;
772}
773
774static const struct l2_level __init *find_cur_l2_level(void)
775{
776 struct scalable *sc = &drv.scalable[L2];
777 const struct l2_level *l;
778 struct core_speed cur_speed;
779
780 fill_cur_core_speed(&cur_speed, sc);
781 for (l = drv.l2_freq_tbl; l->speed.khz != 0; l++)
782 if (speed_equal(&l->speed, &cur_speed))
783 return l;
784 return NULL;
785}
786
787static const struct acpu_level __cpuinit *find_min_acpu_level(void)
788{
789 struct acpu_level *l;
790
791 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
792 if (l->use_for_scaling)
793 return l;
794
795 return NULL;
796}
797
Matt Wagantall302d9a32012-07-03 13:37:29 -0700798static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800799{
Matt Wagantall754ee272012-06-18 13:40:26 -0700800 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700801 const struct acpu_level *acpu_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700802 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800803
Matt Wagantall754ee272012-06-18 13:40:26 -0700804 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700805 if (!sc->hfpll_base) {
806 ret = -ENOMEM;
807 goto err_ioremap;
808 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700809
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700810 acpu_level = find_cur_acpu_level(cpu);
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700811 if (!acpu_level) {
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700812 acpu_level = find_min_acpu_level();
813 if (!acpu_level) {
814 ret = -ENODEV;
815 goto err_table;
816 }
817 dev_dbg(drv.dev, "CPU%d is running at an unknown rate. Defaulting to %lu KHz.\n",
818 cpu, acpu_level->speed.khz);
819 } else {
820 dev_dbg(drv.dev, "CPU%d is running at %lu KHz\n", cpu,
821 acpu_level->speed.khz);
822 }
823
824 ret = regulator_init(sc, acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700825 if (ret)
826 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700827
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700828 ret = init_clock_sources(sc, &acpu_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700829 if (ret)
830 goto err_clocks;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700831
832 sc->l2_vote = acpu_level->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700833 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700834
835 return 0;
836
837err_clocks:
838 regulator_cleanup(sc);
839err_regulators:
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700840err_table:
Matt Wagantall302d9a32012-07-03 13:37:29 -0700841 iounmap(sc->hfpll_base);
842err_ioremap:
843 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800844}
845
846/* Register with bus driver. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700847static void __init bus_init(const struct l2_level *l2_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800848{
849 int ret;
850
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700851 drv.bus_perf_client = msm_bus_scale_register_client(drv.bus_scale);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800852 if (!drv.bus_perf_client) {
853 dev_err(drv.dev, "unable to register bus client\n");
854 BUG();
855 }
856
Matt Wagantall754ee272012-06-18 13:40:26 -0700857 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700858 l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800859 if (ret)
860 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
861}
862
863#ifdef CONFIG_CPU_FREQ_MSM
864static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
865
866static void __init cpufreq_table_init(void)
867{
868 int cpu;
869
870 for_each_possible_cpu(cpu) {
871 int i, freq_cnt = 0;
872 /* Construct the freq_table tables from acpu_freq_tbl. */
873 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
874 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
875 if (drv.acpu_freq_tbl[i].use_for_scaling) {
876 freq_table[cpu][freq_cnt].index = freq_cnt;
877 freq_table[cpu][freq_cnt].frequency
878 = drv.acpu_freq_tbl[i].speed.khz;
879 freq_cnt++;
880 }
881 }
882 /* freq_table not big enough to store all usable freqs. */
883 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
884
885 freq_table[cpu][freq_cnt].index = freq_cnt;
886 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
887
888 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
889 cpu, freq_cnt);
890
891 /* Register table with CPUFreq. */
892 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
893 }
894}
895#else
896static void __init cpufreq_table_init(void) {}
897#endif
898
Steve Mucklea9aac292012-11-02 15:41:00 -0700899static void __init dcvs_freq_init(void)
900{
901 int i;
902
903 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0; i++)
904 if (drv.acpu_freq_tbl[i].use_for_scaling)
905 msm_dcvs_register_cpu_freq(
906 drv.acpu_freq_tbl[i].speed.khz,
907 drv.acpu_freq_tbl[i].vdd_core / 1000);
908}
909
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800910static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
911 unsigned long action, void *hcpu)
912{
913 static int prev_khz[NR_CPUS];
914 int rc, cpu = (int)hcpu;
915 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700916 unsigned long hot_unplug_khz = acpuclk_krait_data.power_collapse_khz;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800917
918 switch (action & ~CPU_TASKS_FROZEN) {
919 case CPU_DEAD:
920 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
921 /* Fall through. */
922 case CPU_UP_CANCELED:
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700923 acpuclk_krait_set_rate(cpu, hot_unplug_khz, SETRATE_HOTPLUG);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800924 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
925 break;
926 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700927 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700928 rc = per_cpu_init(cpu);
929 if (rc)
930 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700931 break;
932 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800933 if (WARN_ON(!prev_khz[cpu]))
934 return NOTIFY_BAD;
935 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700936 sc->vreg[VREG_CORE].cur_ua);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800937 if (rc < 0)
938 return NOTIFY_BAD;
939 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
940 break;
941 default:
942 break;
943 }
944
945 return NOTIFY_OK;
946}
947
948static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
949 .notifier_call = acpuclk_cpu_callback,
950};
951
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700952static const int krait_needs_vmin(void)
953{
954 switch (read_cpuid_id()) {
955 case 0x511F04D0: /* KR28M2A20 */
956 case 0x511F04D1: /* KR28M2A21 */
957 case 0x510F06F0: /* KR28M4A10 */
958 return 1;
959 default:
960 return 0;
961 };
962}
963
964static void krait_apply_vmin(struct acpu_level *tbl)
965{
Stephen Boydc13b6792012-09-14 11:25:34 -0700966 for (; tbl->speed.khz != 0; tbl++) {
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700967 if (tbl->vdd_core < 1150000)
968 tbl->vdd_core = 1150000;
Stephen Boydc13b6792012-09-14 11:25:34 -0700969 tbl->avsdscr_setting = 0;
970 }
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700971}
972
Patrick Daly02db5a82012-08-24 14:22:06 -0700973static int __init get_speed_bin(u32 pte_efuse)
974{
975 uint32_t speed_bin;
976
977 speed_bin = pte_efuse & 0xF;
978 if (speed_bin == 0xF)
979 speed_bin = (pte_efuse >> 4) & 0xF;
980
981 if (speed_bin == 0xF) {
982 speed_bin = 0;
983 dev_warn(drv.dev, "SPEED BIN: Defaulting to %d\n", speed_bin);
984 } else {
985 dev_info(drv.dev, "SPEED BIN: %d\n", speed_bin);
986 }
987
988 return speed_bin;
989}
990
991static int __init get_pvs_bin(u32 pte_efuse)
992{
993 uint32_t pvs_bin;
994
995 pvs_bin = (pte_efuse >> 10) & 0x7;
996 if (pvs_bin == 0x7)
997 pvs_bin = (pte_efuse >> 13) & 0x7;
998
999 if (pvs_bin == 0x7) {
1000 pvs_bin = 0;
1001 dev_warn(drv.dev, "ACPU PVS: Defaulting to %d\n", pvs_bin);
1002 } else {
1003 dev_info(drv.dev, "ACPU PVS: %d\n", pvs_bin);
1004 }
1005
1006 return pvs_bin;
1007}
1008
1009static struct pvs_table * __init select_freq_plan(u32 pte_efuse_phys,
1010 struct pvs_table (*pvs_tables)[NUM_PVS])
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001011{
Matt Wagantall519e94f2012-09-17 17:51:06 -07001012 void __iomem *pte_efuse;
Patrick Daly02db5a82012-08-24 14:22:06 -07001013 u32 pte_efuse_val, tbl_idx, bin_idx;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001014
Matt Wagantall519e94f2012-09-17 17:51:06 -07001015 pte_efuse = ioremap(pte_efuse_phys, 4);
Patrick Daly02db5a82012-08-24 14:22:06 -07001016 if (!pte_efuse) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001017 dev_err(drv.dev, "Unable to map QFPROM base\n");
Patrick Daly02db5a82012-08-24 14:22:06 -07001018 return NULL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001019 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001020
Patrick Daly02db5a82012-08-24 14:22:06 -07001021 pte_efuse_val = readl_relaxed(pte_efuse);
1022 iounmap(pte_efuse);
1023
1024 /* Select frequency tables. */
1025 bin_idx = get_speed_bin(pte_efuse_val);
1026 tbl_idx = get_pvs_bin(pte_efuse_val);
1027
1028 return &pvs_tables[bin_idx][tbl_idx];
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001029}
Matt Wagantall06e4a1f2012-06-07 18:38:13 -07001030
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001031static void __init drv_data_init(struct device *dev,
1032 const struct acpuclk_krait_params *params)
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001033{
Patrick Daly02db5a82012-08-24 14:22:06 -07001034 struct pvs_table *pvs;
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001035
1036 drv.dev = dev;
1037 drv.scalable = kmemdup(params->scalable, params->scalable_size,
1038 GFP_KERNEL);
1039 BUG_ON(!drv.scalable);
1040
1041 drv.hfpll_data = kmemdup(params->hfpll_data, sizeof(*drv.hfpll_data),
1042 GFP_KERNEL);
1043 BUG_ON(!drv.hfpll_data);
1044
1045 drv.l2_freq_tbl = kmemdup(params->l2_freq_tbl, params->l2_freq_tbl_size,
1046 GFP_KERNEL);
1047 BUG_ON(!drv.l2_freq_tbl);
1048
1049 drv.bus_scale = kmemdup(params->bus_scale, sizeof(*drv.bus_scale),
1050 GFP_KERNEL);
1051 BUG_ON(!drv.bus_scale);
1052 drv.bus_scale->usecase = kmemdup(drv.bus_scale->usecase,
1053 drv.bus_scale->num_usecases * sizeof(*drv.bus_scale->usecase),
1054 GFP_KERNEL);
1055 BUG_ON(!drv.bus_scale->usecase);
1056
Patrick Daly02db5a82012-08-24 14:22:06 -07001057 pvs = select_freq_plan(params->pte_efuse_phys, params->pvs_tables);
1058 BUG_ON(!pvs->table);
1059
1060 drv.acpu_freq_tbl = kmemdup(pvs->table, pvs->size, GFP_KERNEL);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001061 BUG_ON(!drv.acpu_freq_tbl);
Patrick Daly02db5a82012-08-24 14:22:06 -07001062 drv.boost_uv = pvs->boost_uv;
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001063
1064 acpuclk_krait_data.power_collapse_khz = params->stby_khz;
1065 acpuclk_krait_data.wait_for_irq_khz = params->stby_khz;
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001066}
1067
1068static void __init hw_init(void)
1069{
1070 struct scalable *l2 = &drv.scalable[L2];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001071 const struct l2_level *l2_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -07001072 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001073
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001074 if (krait_needs_vmin())
1075 krait_apply_vmin(drv.acpu_freq_tbl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001076
Matt Wagantall754ee272012-06-18 13:40:26 -07001077 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
1078 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -07001079
Matt Wagantall302d9a32012-07-03 13:37:29 -07001080 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
1081 l2->vreg[VREG_HFPLL_A].max_vdd, false);
1082 BUG_ON(rc);
1083 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
1084 l2->vreg[VREG_HFPLL_B].max_vdd, false);
1085 BUG_ON(rc);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001086
1087 l2_level = find_cur_l2_level();
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001088 if (!l2_level) {
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001089 l2_level = drv.l2_freq_tbl;
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001090 dev_dbg(drv.dev, "L2 is running at an unknown rate. Defaulting to %lu KHz.\n",
1091 l2_level->speed.khz);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001092 } else {
1093 dev_dbg(drv.dev, "L2 is running at %lu KHz\n",
1094 l2_level->speed.khz);
1095 }
1096
1097 rc = init_clock_sources(l2, &l2_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -07001098 BUG_ON(rc);
1099
1100 for_each_online_cpu(cpu) {
1101 rc = per_cpu_init(cpu);
1102 BUG_ON(rc);
1103 }
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001104
1105 bus_init(l2_level);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001106}
1107
1108int __init acpuclk_krait_init(struct device *dev,
1109 const struct acpuclk_krait_params *params)
1110{
1111 drv_data_init(dev, params);
1112 hw_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001113
1114 cpufreq_table_init();
Steve Mucklea9aac292012-11-02 15:41:00 -07001115 dcvs_freq_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001116 acpuclk_register(&acpuclk_krait_data);
1117 register_hotcpu_notifier(&acpuclk_cpu_notifier);
1118
1119 return 0;
1120}