blob: c429eecf48cb9c11d06fd30da77ab686d623f255 [file] [log] [blame]
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001/*
2 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
Matt Wagantalle9b715a2012-01-04 18:16:14 -080014#include <linux/kernel.h>
Matt Wagantall9515bc22012-07-19 18:13:40 -070015#include <linux/module.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080016#include <linux/init.h>
17#include <linux/io.h>
18#include <linux/delay.h>
19#include <linux/mutex.h>
20#include <linux/err.h>
21#include <linux/errno.h>
22#include <linux/cpufreq.h>
23#include <linux/cpu.h>
24#include <linux/regulator/consumer.h>
25
26#include <asm/mach-types.h>
27#include <asm/cpu.h>
28
29#include <mach/board.h>
30#include <mach/msm_iomap.h>
31#include <mach/socinfo.h>
32#include <mach/msm-krait-l2-accessors.h>
33#include <mach/rpm-regulator.h>
Matt Wagantall75473eb2012-05-31 15:23:22 -070034#include <mach/rpm-regulator-smd.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080035#include <mach/msm_bus.h>
36
37#include "acpuclock.h"
38#include "acpuclock-krait.h"
Stephen Boydc13b6792012-09-14 11:25:34 -070039#include "avs.h"
Matt Wagantalle9b715a2012-01-04 18:16:14 -080040
41/* MUX source selects. */
42#define PRI_SRC_SEL_SEC_SRC 0
43#define PRI_SRC_SEL_HFPLL 1
44#define PRI_SRC_SEL_HFPLL_DIV2 2
Matt Wagantalle9b715a2012-01-04 18:16:14 -080045
Matt Wagantalle9b715a2012-01-04 18:16:14 -080046static DEFINE_MUTEX(driver_lock);
47static DEFINE_SPINLOCK(l2_lock);
48
49static struct drv_data {
Matt Wagantall06e4a1f2012-06-07 18:38:13 -070050 struct acpu_level *acpu_freq_tbl;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080051 const struct l2_level *l2_freq_tbl;
52 struct scalable *scalable;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070053 struct hfpll_data *hfpll_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080054 u32 bus_perf_client;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070055 struct msm_bus_scale_pdata *bus_scale;
Matt Wagantall9515bc22012-07-19 18:13:40 -070056 int boost_uv;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080057 struct device *dev;
58} drv;
59
60static unsigned long acpuclk_krait_get_rate(int cpu)
61{
62 return drv.scalable[cpu].cur_speed->khz;
63}
64
65/* Select a source on the primary MUX. */
66static void set_pri_clk_src(struct scalable *sc, u32 pri_src_sel)
67{
68 u32 regval;
69
70 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
71 regval &= ~0x3;
72 regval |= (pri_src_sel & 0x3);
73 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
74 /* Wait for switch to complete. */
75 mb();
76 udelay(1);
77}
78
79/* Select a source on the secondary MUX. */
Matt Wagantalla133dbf2012-09-27 19:56:57 -070080static void __cpuinit set_sec_clk_src(struct scalable *sc, u32 sec_src_sel)
Matt Wagantalle9b715a2012-01-04 18:16:14 -080081{
82 u32 regval;
83
84 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
85 regval &= ~(0x3 << 2);
86 regval |= ((sec_src_sel & 0x3) << 2);
87 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
88 /* Wait for switch to complete. */
89 mb();
90 udelay(1);
91}
92
Matt Wagantall302d9a32012-07-03 13:37:29 -070093static int enable_rpm_vreg(struct vreg *vreg)
Matt Wagantalle9b715a2012-01-04 18:16:14 -080094{
Matt Wagantall302d9a32012-07-03 13:37:29 -070095 int ret = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080096
Matt Wagantall75473eb2012-05-31 15:23:22 -070097 if (vreg->rpm_reg) {
Matt Wagantall302d9a32012-07-03 13:37:29 -070098 ret = rpm_regulator_enable(vreg->rpm_reg);
99 if (ret)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700100 dev_err(drv.dev, "%s regulator enable failed (%d)\n",
Matt Wagantall302d9a32012-07-03 13:37:29 -0700101 vreg->name, ret);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700102 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700103
104 return ret;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700105}
106
107static void disable_rpm_vreg(struct vreg *vreg)
108{
109 int rc;
110
111 if (vreg->rpm_reg) {
112 rc = rpm_regulator_disable(vreg->rpm_reg);
113 if (rc)
114 dev_err(drv.dev, "%s regulator disable failed (%d)\n",
115 vreg->name, rc);
116 }
117}
118
119/* Enable an already-configured HFPLL. */
120static void hfpll_enable(struct scalable *sc, bool skip_regulators)
121{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800122 if (!skip_regulators) {
123 /* Enable regulators required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700124 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
125 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800126 }
127
128 /* Disable PLL bypass mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700129 writel_relaxed(0x2, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800130
131 /*
132 * H/W requires a 5us delay between disabling the bypass and
133 * de-asserting the reset. Delay 10us just to be safe.
134 */
135 mb();
136 udelay(10);
137
138 /* De-assert active-low PLL reset. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700139 writel_relaxed(0x6, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800140
141 /* Wait for PLL to lock. */
142 mb();
143 udelay(60);
144
145 /* Enable PLL output. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700146 writel_relaxed(0x7, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800147}
148
149/* Disable a HFPLL for power-savings or while it's being reprogrammed. */
150static void hfpll_disable(struct scalable *sc, bool skip_regulators)
151{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800152 /*
153 * Disable the PLL output, disable test mode, enable the bypass mode,
154 * and assert the reset.
155 */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700156 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800157
158 if (!skip_regulators) {
159 /* Remove voltage votes required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700160 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
161 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800162 }
163}
164
165/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
166static void hfpll_set_rate(struct scalable *sc, const struct core_speed *tgt_s)
167{
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700168 void __iomem *base = sc->hfpll_base;
169 u32 regval;
170
171 writel_relaxed(tgt_s->pll_l_val, base + drv.hfpll_data->l_offset);
172
173 if (drv.hfpll_data->has_user_reg) {
174 regval = readl_relaxed(base + drv.hfpll_data->user_offset);
175 if (tgt_s->pll_l_val <= drv.hfpll_data->low_vco_l_max)
176 regval &= ~drv.hfpll_data->user_vco_mask;
177 else
178 regval |= drv.hfpll_data->user_vco_mask;
179 writel_relaxed(regval, base + drv.hfpll_data->user_offset);
180 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800181}
182
183/* Return the L2 speed that should be applied. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700184static unsigned int compute_l2_level(struct scalable *sc, unsigned int vote_l)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800185{
Matt Wagantall600ea502012-06-08 18:49:53 -0700186 unsigned int new_l = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800187 int cpu;
188
189 /* Find max L2 speed vote. */
190 sc->l2_vote = vote_l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800191 for_each_present_cpu(cpu)
192 new_l = max(new_l, drv.scalable[cpu].l2_vote);
193
194 return new_l;
195}
196
197/* Update the bus bandwidth request. */
198static void set_bus_bw(unsigned int bw)
199{
200 int ret;
201
202 /* Update bandwidth if request has changed. This may sleep. */
203 ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw);
204 if (ret)
205 dev_err(drv.dev, "bandwidth request failed (%d)\n", ret);
206}
207
208/* Set the CPU or L2 clock speed. */
209static void set_speed(struct scalable *sc, const struct core_speed *tgt_s)
210{
211 const struct core_speed *strt_s = sc->cur_speed;
212
Stephen Boyd14a47392012-08-06 20:15:15 -0700213 if (strt_s == tgt_s)
214 return;
215
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800216 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
217 /*
218 * Move to an always-on source running at a frequency
219 * that does not require an elevated CPU voltage.
220 */
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800221 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
222
223 /* Re-program HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700224 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800225 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700226 hfpll_enable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800227
228 /* Move to HFPLL. */
229 set_pri_clk_src(sc, tgt_s->pri_src_sel);
230 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800231 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700232 hfpll_disable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800233 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
234 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700235 hfpll_enable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800236 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800237 }
238
239 sc->cur_speed = tgt_s;
240}
241
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700242struct vdd_data {
243 int vdd_mem;
244 int vdd_dig;
245 int vdd_core;
246 int ua_core;
247};
248
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800249/* Apply any per-cpu voltage increases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700250static int increase_vdd(int cpu, struct vdd_data *data,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800251 enum setrate_reason reason)
252{
253 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700254 int rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800255
256 /*
257 * Increase vdd_mem active-set before vdd_dig.
258 * vdd_mem should be >= vdd_dig.
259 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700260 if (data->vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700261 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700262 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800263 if (rc) {
264 dev_err(drv.dev,
265 "vdd_mem (cpu%d) increase failed (%d)\n",
266 cpu, rc);
267 return rc;
268 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700269 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800270 }
271
272 /* Increase vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700273 if (data->vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700274 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700275 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800276 if (rc) {
277 dev_err(drv.dev,
278 "vdd_dig (cpu%d) increase failed (%d)\n",
279 cpu, rc);
280 return rc;
281 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700282 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
283 }
284
285 /* Increase current request. */
286 if (data->ua_core > sc->vreg[VREG_CORE].cur_ua) {
287 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
288 data->ua_core);
289 if (rc < 0) {
290 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
291 sc->vreg[VREG_CORE].name, rc);
292 return rc;
293 }
294 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800295 }
296
297 /*
298 * Update per-CPU core voltage. Don't do this for the hotplug path for
299 * which it should already be correct. Attempting to set it is bad
300 * because we don't know what CPU we are running on at this point, but
301 * the CPU regulator API requires we call it from the affected CPU.
302 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700303 if (data->vdd_core > sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800304 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700305 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
306 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800307 if (rc) {
308 dev_err(drv.dev,
309 "vdd_core (cpu%d) increase failed (%d)\n",
310 cpu, rc);
311 return rc;
312 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700313 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800314 }
315
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700316 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800317}
318
319/* Apply any per-cpu voltage decreases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700320static void decrease_vdd(int cpu, struct vdd_data *data,
321 enum setrate_reason reason)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800322{
323 struct scalable *sc = &drv.scalable[cpu];
324 int ret;
325
326 /*
327 * Update per-CPU core voltage. This must be called on the CPU
328 * that's being affected. Don't do this in the hotplug remove path,
329 * where the rail is off and we're executing on the other CPU.
330 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700331 if (data->vdd_core < sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800332 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700333 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
334 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800335 if (ret) {
336 dev_err(drv.dev,
337 "vdd_core (cpu%d) decrease failed (%d)\n",
338 cpu, ret);
339 return;
340 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700341 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
342 }
343
344 /* Decrease current request. */
345 if (data->ua_core < sc->vreg[VREG_CORE].cur_ua) {
346 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
347 data->ua_core);
348 if (ret < 0) {
349 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
350 sc->vreg[VREG_CORE].name, ret);
351 return;
352 }
353 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800354 }
355
356 /* Decrease vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700357 if (data->vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700358 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700359 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800360 if (ret) {
361 dev_err(drv.dev,
362 "vdd_dig (cpu%d) decrease failed (%d)\n",
363 cpu, ret);
364 return;
365 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700366 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800367 }
368
369 /*
370 * Decrease vdd_mem active-set after vdd_dig.
371 * vdd_mem should be >= vdd_dig.
372 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700373 if (data->vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700374 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700375 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800376 if (ret) {
377 dev_err(drv.dev,
378 "vdd_mem (cpu%d) decrease failed (%d)\n",
379 cpu, ret);
380 return;
381 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700382 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800383 }
384}
385
386static int calculate_vdd_mem(const struct acpu_level *tgt)
387{
Matt Wagantall600ea502012-06-08 18:49:53 -0700388 return drv.l2_freq_tbl[tgt->l2_level].vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800389}
390
Matt Wagantall72a38002012-07-18 13:42:55 -0700391static int get_src_dig(const struct core_speed *s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800392{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700393 const int *hfpll_vdd = drv.hfpll_data->vdd;
394 const u32 low_vdd_l_max = drv.hfpll_data->low_vdd_l_max;
Matt Wagantall87465f52012-07-23 22:03:06 -0700395 const u32 nom_vdd_l_max = drv.hfpll_data->nom_vdd_l_max;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800396
Matt Wagantall72a38002012-07-18 13:42:55 -0700397 if (s->src != HFPLL)
398 return hfpll_vdd[HFPLL_VDD_NONE];
Matt Wagantall87465f52012-07-23 22:03:06 -0700399 else if (s->pll_l_val > nom_vdd_l_max)
400 return hfpll_vdd[HFPLL_VDD_HIGH];
Matt Wagantall72a38002012-07-18 13:42:55 -0700401 else if (s->pll_l_val > low_vdd_l_max)
402 return hfpll_vdd[HFPLL_VDD_NOM];
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800403 else
Matt Wagantall72a38002012-07-18 13:42:55 -0700404 return hfpll_vdd[HFPLL_VDD_LOW];
405}
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800406
Matt Wagantall72a38002012-07-18 13:42:55 -0700407static int calculate_vdd_dig(const struct acpu_level *tgt)
408{
409 int l2_pll_vdd_dig, cpu_pll_vdd_dig;
410
411 l2_pll_vdd_dig = get_src_dig(&drv.l2_freq_tbl[tgt->l2_level].speed);
412 cpu_pll_vdd_dig = get_src_dig(&tgt->speed);
413
414 return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig,
415 max(l2_pll_vdd_dig, cpu_pll_vdd_dig));
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800416}
417
Matt Wagantall9515bc22012-07-19 18:13:40 -0700418static bool enable_boost = true;
419module_param_named(boost, enable_boost, bool, S_IRUGO | S_IWUSR);
420
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800421static int calculate_vdd_core(const struct acpu_level *tgt)
422{
Matt Wagantall9515bc22012-07-19 18:13:40 -0700423 return tgt->vdd_core + (enable_boost ? drv.boost_uv : 0);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800424}
425
426/* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */
427static int acpuclk_krait_set_rate(int cpu, unsigned long rate,
428 enum setrate_reason reason)
429{
430 const struct core_speed *strt_acpu_s, *tgt_acpu_s;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800431 const struct acpu_level *tgt;
Matt Wagantall600ea502012-06-08 18:49:53 -0700432 int tgt_l2_l;
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700433 struct vdd_data vdd_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800434 unsigned long flags;
435 int rc = 0;
436
Matt Wagantall5941a332012-07-10 23:20:44 -0700437 if (cpu > num_possible_cpus())
438 return -EINVAL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800439
440 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
441 mutex_lock(&driver_lock);
442
443 strt_acpu_s = drv.scalable[cpu].cur_speed;
444
445 /* Return early if rate didn't change. */
446 if (rate == strt_acpu_s->khz)
447 goto out;
448
449 /* Find target frequency. */
450 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
451 if (tgt->speed.khz == rate) {
452 tgt_acpu_s = &tgt->speed;
453 break;
454 }
455 }
456 if (tgt->speed.khz == 0) {
457 rc = -EINVAL;
458 goto out;
459 }
460
461 /* Calculate voltage requirements for the current CPU. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700462 vdd_data.vdd_mem = calculate_vdd_mem(tgt);
463 vdd_data.vdd_dig = calculate_vdd_dig(tgt);
464 vdd_data.vdd_core = calculate_vdd_core(tgt);
465 vdd_data.ua_core = tgt->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800466
Stephen Boydc13b6792012-09-14 11:25:34 -0700467 /* Disable AVS before voltage switch */
468 if (reason == SETRATE_CPUFREQ && drv.scalable[cpu].avs_enabled) {
469 AVS_DISABLE(cpu);
470 drv.scalable[cpu].avs_enabled = false;
471 }
472
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800473 /* Increase VDD levels if needed. */
474 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700475 rc = increase_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800476 if (rc)
477 goto out;
478 }
479
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700480 dev_dbg(drv.dev, "Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
481 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800482
483 /* Set the new CPU speed. */
484 set_speed(&drv.scalable[cpu], tgt_acpu_s);
485
486 /*
487 * Update the L2 vote and apply the rate change. A spinlock is
488 * necessary to ensure L2 rate is calculated and set atomically
489 * with the CPU frequency, even if acpuclk_krait_set_rate() is
490 * called from an atomic context and the driver_lock mutex is not
491 * acquired.
492 */
493 spin_lock_irqsave(&l2_lock, flags);
494 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
Matt Wagantall600ea502012-06-08 18:49:53 -0700495 set_speed(&drv.scalable[L2], &drv.l2_freq_tbl[tgt_l2_l].speed);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800496 spin_unlock_irqrestore(&l2_lock, flags);
497
498 /* Nothing else to do for power collapse or SWFI. */
499 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
500 goto out;
501
502 /* Update bus bandwith request. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700503 set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800504
505 /* Drop VDD levels if we can. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700506 decrease_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800507
Stephen Boydc13b6792012-09-14 11:25:34 -0700508 /* Re-enable AVS */
509 if (reason == SETRATE_CPUFREQ && tgt->avsdscr_setting) {
510 AVS_ENABLE(cpu, tgt->avsdscr_setting);
511 drv.scalable[cpu].avs_enabled = true;
512 }
513
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700514 dev_dbg(drv.dev, "ACPU%d speed change complete\n", cpu);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800515
516out:
517 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
518 mutex_unlock(&driver_lock);
519 return rc;
520}
521
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700522static struct acpuclk_data acpuclk_krait_data = {
523 .set_rate = acpuclk_krait_set_rate,
524 .get_rate = acpuclk_krait_get_rate,
525};
526
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800527/* Initialize a HFPLL at a given rate and enable it. */
Iliyan Malchev16aea522012-10-16 00:35:07 -0700528static void __cpuinit hfpll_init(struct scalable *sc,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800529 const struct core_speed *tgt_s)
530{
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700531 dev_dbg(drv.dev, "Initializing HFPLL%d\n", sc - drv.scalable);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800532
533 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700534 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800535
536 /* Configure PLL parameters for integer mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700537 writel_relaxed(drv.hfpll_data->config_val,
538 sc->hfpll_base + drv.hfpll_data->config_offset);
539 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->m_offset);
540 writel_relaxed(1, sc->hfpll_base + drv.hfpll_data->n_offset);
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700541 if (drv.hfpll_data->has_user_reg)
542 writel_relaxed(drv.hfpll_data->user_val,
543 sc->hfpll_base + drv.hfpll_data->user_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800544
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700545 /* Program droop controller, if supported */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700546 if (drv.hfpll_data->has_droop_ctl)
547 writel_relaxed(drv.hfpll_data->droop_val,
548 sc->hfpll_base + drv.hfpll_data->droop_offset);
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700549
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800550 /* Set an initial rate and enable the PLL. */
551 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700552 hfpll_enable(sc, false);
553}
554
Matt Wagantall302d9a32012-07-03 13:37:29 -0700555static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700556 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700557{
558 int ret;
559
560 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700561 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700562
563 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
564 sc->vreg[vreg].name);
565 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700566 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
567 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
568 sc->vreg[vreg].name, ret);
569 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700570 }
571
572 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
573 sc->vreg[vreg].max_vdd);
574 if (ret) {
575 dev_err(drv.dev, "%s initialization failed (%d)\n",
576 sc->vreg[vreg].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700577 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700578 }
579 sc->vreg[vreg].cur_vdd = vdd;
580
Matt Wagantall302d9a32012-07-03 13:37:29 -0700581 if (enable) {
582 ret = enable_rpm_vreg(&sc->vreg[vreg]);
583 if (ret)
584 goto err_conf;
585 }
586
587 return 0;
588
589err_conf:
590 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
591err_get:
592 return ret;
593}
594
595static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
596 enum vregs vreg)
597{
598 if (!sc->vreg[vreg].rpm_reg)
599 return;
600
601 disable_rpm_vreg(&sc->vreg[vreg]);
602 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800603}
604
605/* Voltage regulator initialization. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700606static int __cpuinit regulator_init(struct scalable *sc,
607 const struct acpu_level *acpu_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800608{
Matt Wagantall754ee272012-06-18 13:40:26 -0700609 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800610
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700611 vdd_mem = calculate_vdd_mem(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700612 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
613 if (ret)
614 goto err_mem;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700615
616 vdd_dig = calculate_vdd_dig(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700617 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
618 if (ret)
619 goto err_dig;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700620
Matt Wagantall302d9a32012-07-03 13:37:29 -0700621 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700622 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700623 if (ret)
624 goto err_hfpll_a;
625 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700626 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700627 if (ret)
628 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700629
Matt Wagantall754ee272012-06-18 13:40:26 -0700630 /* Setup Krait CPU regulators and initial core voltage. */
631 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
632 sc->vreg[VREG_CORE].name);
633 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700634 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
635 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
636 sc->vreg[VREG_CORE].name, ret);
637 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700638 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700639 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
640 acpu_level->ua_core);
641 if (ret < 0) {
642 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
643 sc->vreg[VREG_CORE].name, ret);
644 goto err_core_conf;
645 }
646 sc->vreg[VREG_CORE].cur_ua = acpu_level->ua_core;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700647 vdd_core = calculate_vdd_core(acpu_level);
Matt Wagantall754ee272012-06-18 13:40:26 -0700648 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
649 sc->vreg[VREG_CORE].max_vdd);
650 if (ret) {
651 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
652 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700653 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700654 }
655 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
Matt Wagantall754ee272012-06-18 13:40:26 -0700656 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
657 if (ret) {
658 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
659 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700660 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800661 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700662
663 return 0;
664
665err_core_conf:
666 regulator_put(sc->vreg[VREG_CORE].reg);
667err_core_get:
668 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
669err_hfpll_b:
670 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
671err_hfpll_a:
672 rpm_regulator_cleanup(sc, VREG_DIG);
673err_dig:
674 rpm_regulator_cleanup(sc, VREG_MEM);
675err_mem:
676 return ret;
677}
678
679static void __cpuinit regulator_cleanup(struct scalable *sc)
680{
681 regulator_disable(sc->vreg[VREG_CORE].reg);
682 regulator_put(sc->vreg[VREG_CORE].reg);
683 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
684 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
685 rpm_regulator_cleanup(sc, VREG_DIG);
686 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800687}
688
689/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700690static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700691 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800692{
693 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700694 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800695
696 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700697 if (sc->aux_clk_sel_phys) {
698 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700699 if (!aux_reg)
700 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700701 writel_relaxed(sc->aux_clk_sel, aux_reg);
702 iounmap(aux_reg);
703 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800704
705 /* Switch away from the HFPLL while it's re-initialized. */
Matt Wagantalla133dbf2012-09-27 19:56:57 -0700706 set_sec_clk_src(sc, sc->sec_clk_sel);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800707 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
708 hfpll_init(sc, tgt_s);
709
710 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
711 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
712 regval &= ~(0x3 << 6);
713 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
714
715 /* Switch to the target clock source. */
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800716 set_pri_clk_src(sc, tgt_s->pri_src_sel);
717 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700718
719 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800720}
721
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700722static void __cpuinit fill_cur_core_speed(struct core_speed *s,
723 struct scalable *sc)
724{
725 s->pri_src_sel = get_l2_indirect_reg(sc->l2cpmr_iaddr) & 0x3;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700726 s->pll_l_val = readl_relaxed(sc->hfpll_base + drv.hfpll_data->l_offset);
727}
728
729static bool __cpuinit speed_equal(const struct core_speed *s1,
730 const struct core_speed *s2)
731{
732 return (s1->pri_src_sel == s2->pri_src_sel &&
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700733 s1->pll_l_val == s2->pll_l_val);
734}
735
736static const struct acpu_level __cpuinit *find_cur_acpu_level(int cpu)
737{
738 struct scalable *sc = &drv.scalable[cpu];
739 const struct acpu_level *l;
740 struct core_speed cur_speed;
741
742 fill_cur_core_speed(&cur_speed, sc);
743 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
744 if (speed_equal(&l->speed, &cur_speed))
745 return l;
746 return NULL;
747}
748
749static const struct l2_level __init *find_cur_l2_level(void)
750{
751 struct scalable *sc = &drv.scalable[L2];
752 const struct l2_level *l;
753 struct core_speed cur_speed;
754
755 fill_cur_core_speed(&cur_speed, sc);
756 for (l = drv.l2_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 acpu_level __cpuinit *find_min_acpu_level(void)
763{
764 struct acpu_level *l;
765
766 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
767 if (l->use_for_scaling)
768 return l;
769
770 return NULL;
771}
772
Matt Wagantall302d9a32012-07-03 13:37:29 -0700773static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800774{
Matt Wagantall754ee272012-06-18 13:40:26 -0700775 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700776 const struct acpu_level *acpu_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700777 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800778
Matt Wagantall754ee272012-06-18 13:40:26 -0700779 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700780 if (!sc->hfpll_base) {
781 ret = -ENOMEM;
782 goto err_ioremap;
783 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700784
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700785 acpu_level = find_cur_acpu_level(cpu);
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700786 if (!acpu_level) {
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700787 acpu_level = find_min_acpu_level();
788 if (!acpu_level) {
789 ret = -ENODEV;
790 goto err_table;
791 }
792 dev_dbg(drv.dev, "CPU%d is running at an unknown rate. Defaulting to %lu KHz.\n",
793 cpu, acpu_level->speed.khz);
794 } else {
795 dev_dbg(drv.dev, "CPU%d is running at %lu KHz\n", cpu,
796 acpu_level->speed.khz);
797 }
798
799 ret = regulator_init(sc, acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700800 if (ret)
801 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700802
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700803 ret = init_clock_sources(sc, &acpu_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700804 if (ret)
805 goto err_clocks;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700806
807 sc->l2_vote = acpu_level->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700808 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700809
810 return 0;
811
812err_clocks:
813 regulator_cleanup(sc);
814err_regulators:
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700815err_table:
Matt Wagantall302d9a32012-07-03 13:37:29 -0700816 iounmap(sc->hfpll_base);
817err_ioremap:
818 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800819}
820
821/* Register with bus driver. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700822static void __init bus_init(const struct l2_level *l2_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800823{
824 int ret;
825
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700826 drv.bus_perf_client = msm_bus_scale_register_client(drv.bus_scale);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800827 if (!drv.bus_perf_client) {
828 dev_err(drv.dev, "unable to register bus client\n");
829 BUG();
830 }
831
Matt Wagantall754ee272012-06-18 13:40:26 -0700832 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700833 l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800834 if (ret)
835 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
836}
837
838#ifdef CONFIG_CPU_FREQ_MSM
839static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
840
841static void __init cpufreq_table_init(void)
842{
843 int cpu;
844
845 for_each_possible_cpu(cpu) {
846 int i, freq_cnt = 0;
847 /* Construct the freq_table tables from acpu_freq_tbl. */
848 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
849 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
850 if (drv.acpu_freq_tbl[i].use_for_scaling) {
851 freq_table[cpu][freq_cnt].index = freq_cnt;
852 freq_table[cpu][freq_cnt].frequency
853 = drv.acpu_freq_tbl[i].speed.khz;
854 freq_cnt++;
855 }
856 }
857 /* freq_table not big enough to store all usable freqs. */
858 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
859
860 freq_table[cpu][freq_cnt].index = freq_cnt;
861 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
862
863 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
864 cpu, freq_cnt);
865
866 /* Register table with CPUFreq. */
867 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
868 }
869}
870#else
871static void __init cpufreq_table_init(void) {}
872#endif
873
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800874static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
875 unsigned long action, void *hcpu)
876{
877 static int prev_khz[NR_CPUS];
878 int rc, cpu = (int)hcpu;
879 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700880 unsigned long hot_unplug_khz = acpuclk_krait_data.power_collapse_khz;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800881
882 switch (action & ~CPU_TASKS_FROZEN) {
883 case CPU_DEAD:
884 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
885 /* Fall through. */
886 case CPU_UP_CANCELED:
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700887 acpuclk_krait_set_rate(cpu, hot_unplug_khz, SETRATE_HOTPLUG);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800888 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
889 break;
890 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700891 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700892 rc = per_cpu_init(cpu);
893 if (rc)
894 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700895 break;
896 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800897 if (WARN_ON(!prev_khz[cpu]))
898 return NOTIFY_BAD;
899 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700900 sc->vreg[VREG_CORE].cur_ua);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800901 if (rc < 0)
902 return NOTIFY_BAD;
903 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
904 break;
905 default:
906 break;
907 }
908
909 return NOTIFY_OK;
910}
911
912static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
913 .notifier_call = acpuclk_cpu_callback,
914};
915
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700916static const int krait_needs_vmin(void)
917{
918 switch (read_cpuid_id()) {
919 case 0x511F04D0: /* KR28M2A20 */
920 case 0x511F04D1: /* KR28M2A21 */
921 case 0x510F06F0: /* KR28M4A10 */
922 return 1;
923 default:
924 return 0;
925 };
926}
927
928static void krait_apply_vmin(struct acpu_level *tbl)
929{
Stephen Boydc13b6792012-09-14 11:25:34 -0700930 for (; tbl->speed.khz != 0; tbl++) {
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700931 if (tbl->vdd_core < 1150000)
932 tbl->vdd_core = 1150000;
Stephen Boydc13b6792012-09-14 11:25:34 -0700933 tbl->avsdscr_setting = 0;
934 }
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700935}
936
Patrick Daly02db5a82012-08-24 14:22:06 -0700937static int __init get_speed_bin(u32 pte_efuse)
938{
939 uint32_t speed_bin;
940
941 speed_bin = pte_efuse & 0xF;
942 if (speed_bin == 0xF)
943 speed_bin = (pte_efuse >> 4) & 0xF;
944
945 if (speed_bin == 0xF) {
946 speed_bin = 0;
947 dev_warn(drv.dev, "SPEED BIN: Defaulting to %d\n", speed_bin);
948 } else {
949 dev_info(drv.dev, "SPEED BIN: %d\n", speed_bin);
950 }
951
952 return speed_bin;
953}
954
955static int __init get_pvs_bin(u32 pte_efuse)
956{
957 uint32_t pvs_bin;
958
959 pvs_bin = (pte_efuse >> 10) & 0x7;
960 if (pvs_bin == 0x7)
961 pvs_bin = (pte_efuse >> 13) & 0x7;
962
963 if (pvs_bin == 0x7) {
964 pvs_bin = 0;
965 dev_warn(drv.dev, "ACPU PVS: Defaulting to %d\n", pvs_bin);
966 } else {
967 dev_info(drv.dev, "ACPU PVS: %d\n", pvs_bin);
968 }
969
970 return pvs_bin;
971}
972
973static struct pvs_table * __init select_freq_plan(u32 pte_efuse_phys,
974 struct pvs_table (*pvs_tables)[NUM_PVS])
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800975{
Matt Wagantall519e94f2012-09-17 17:51:06 -0700976 void __iomem *pte_efuse;
Patrick Daly02db5a82012-08-24 14:22:06 -0700977 u32 pte_efuse_val, tbl_idx, bin_idx;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800978
Matt Wagantall519e94f2012-09-17 17:51:06 -0700979 pte_efuse = ioremap(pte_efuse_phys, 4);
Patrick Daly02db5a82012-08-24 14:22:06 -0700980 if (!pte_efuse) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800981 dev_err(drv.dev, "Unable to map QFPROM base\n");
Patrick Daly02db5a82012-08-24 14:22:06 -0700982 return NULL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800983 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800984
Patrick Daly02db5a82012-08-24 14:22:06 -0700985 pte_efuse_val = readl_relaxed(pte_efuse);
986 iounmap(pte_efuse);
987
988 /* Select frequency tables. */
989 bin_idx = get_speed_bin(pte_efuse_val);
990 tbl_idx = get_pvs_bin(pte_efuse_val);
991
992 return &pvs_tables[bin_idx][tbl_idx];
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700993}
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700994
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700995static void __init drv_data_init(struct device *dev,
996 const struct acpuclk_krait_params *params)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800997{
Patrick Daly02db5a82012-08-24 14:22:06 -0700998 struct pvs_table *pvs;
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700999
1000 drv.dev = dev;
1001 drv.scalable = kmemdup(params->scalable, params->scalable_size,
1002 GFP_KERNEL);
1003 BUG_ON(!drv.scalable);
1004
1005 drv.hfpll_data = kmemdup(params->hfpll_data, sizeof(*drv.hfpll_data),
1006 GFP_KERNEL);
1007 BUG_ON(!drv.hfpll_data);
1008
1009 drv.l2_freq_tbl = kmemdup(params->l2_freq_tbl, params->l2_freq_tbl_size,
1010 GFP_KERNEL);
1011 BUG_ON(!drv.l2_freq_tbl);
1012
1013 drv.bus_scale = kmemdup(params->bus_scale, sizeof(*drv.bus_scale),
1014 GFP_KERNEL);
1015 BUG_ON(!drv.bus_scale);
1016 drv.bus_scale->usecase = kmemdup(drv.bus_scale->usecase,
1017 drv.bus_scale->num_usecases * sizeof(*drv.bus_scale->usecase),
1018 GFP_KERNEL);
1019 BUG_ON(!drv.bus_scale->usecase);
1020
Patrick Daly02db5a82012-08-24 14:22:06 -07001021 pvs = select_freq_plan(params->pte_efuse_phys, params->pvs_tables);
1022 BUG_ON(!pvs->table);
1023
1024 drv.acpu_freq_tbl = kmemdup(pvs->table, pvs->size, GFP_KERNEL);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001025 BUG_ON(!drv.acpu_freq_tbl);
Patrick Daly02db5a82012-08-24 14:22:06 -07001026 drv.boost_uv = pvs->boost_uv;
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001027
1028 acpuclk_krait_data.power_collapse_khz = params->stby_khz;
1029 acpuclk_krait_data.wait_for_irq_khz = params->stby_khz;
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001030}
1031
1032static void __init hw_init(void)
1033{
1034 struct scalable *l2 = &drv.scalable[L2];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001035 const struct l2_level *l2_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -07001036 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001037
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001038 if (krait_needs_vmin())
1039 krait_apply_vmin(drv.acpu_freq_tbl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001040
Matt Wagantall754ee272012-06-18 13:40:26 -07001041 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
1042 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -07001043
Matt Wagantall302d9a32012-07-03 13:37:29 -07001044 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
1045 l2->vreg[VREG_HFPLL_A].max_vdd, false);
1046 BUG_ON(rc);
1047 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
1048 l2->vreg[VREG_HFPLL_B].max_vdd, false);
1049 BUG_ON(rc);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001050
1051 l2_level = find_cur_l2_level();
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001052 if (!l2_level) {
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001053 l2_level = drv.l2_freq_tbl;
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001054 dev_dbg(drv.dev, "L2 is running at an unknown rate. Defaulting to %lu KHz.\n",
1055 l2_level->speed.khz);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001056 } else {
1057 dev_dbg(drv.dev, "L2 is running at %lu KHz\n",
1058 l2_level->speed.khz);
1059 }
1060
1061 rc = init_clock_sources(l2, &l2_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -07001062 BUG_ON(rc);
1063
1064 for_each_online_cpu(cpu) {
1065 rc = per_cpu_init(cpu);
1066 BUG_ON(rc);
1067 }
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001068
1069 bus_init(l2_level);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001070}
1071
1072int __init acpuclk_krait_init(struct device *dev,
1073 const struct acpuclk_krait_params *params)
1074{
1075 drv_data_init(dev, params);
1076 hw_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001077
1078 cpufreq_table_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001079 acpuclk_register(&acpuclk_krait_data);
1080 register_hotcpu_notifier(&acpuclk_cpu_notifier);
1081
1082 return 0;
1083}