blob: 33396e5d4b135e72023288addc993955660b55e9 [file] [log] [blame]
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001/*
2 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
Matt Wagantalle9b715a2012-01-04 18:16:14 -080014#include <linux/kernel.h>
Matt Wagantall9515bc22012-07-19 18:13:40 -070015#include <linux/module.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080016#include <linux/init.h>
17#include <linux/io.h>
18#include <linux/delay.h>
19#include <linux/mutex.h>
20#include <linux/err.h>
21#include <linux/errno.h>
22#include <linux/cpufreq.h>
23#include <linux/cpu.h>
24#include <linux/regulator/consumer.h>
25
26#include <asm/mach-types.h>
27#include <asm/cpu.h>
28
29#include <mach/board.h>
30#include <mach/msm_iomap.h>
31#include <mach/socinfo.h>
32#include <mach/msm-krait-l2-accessors.h>
33#include <mach/rpm-regulator.h>
Matt Wagantall75473eb2012-05-31 15:23:22 -070034#include <mach/rpm-regulator-smd.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080035#include <mach/msm_bus.h>
36
37#include "acpuclock.h"
38#include "acpuclock-krait.h"
39
40/* MUX source selects. */
41#define PRI_SRC_SEL_SEC_SRC 0
42#define PRI_SRC_SEL_HFPLL 1
43#define PRI_SRC_SEL_HFPLL_DIV2 2
Matt Wagantalle9b715a2012-01-04 18:16:14 -080044#define SEC_SRC_SEL_L2PLL 1
45#define SEC_SRC_SEL_AUX 2
46
47/* PTE EFUSE register offset. */
48#define PTE_EFUSE 0xC0
49
50static DEFINE_MUTEX(driver_lock);
51static DEFINE_SPINLOCK(l2_lock);
52
53static struct drv_data {
Matt Wagantall06e4a1f2012-06-07 18:38:13 -070054 struct acpu_level *acpu_freq_tbl;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080055 const struct l2_level *l2_freq_tbl;
56 struct scalable *scalable;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070057 struct hfpll_data *hfpll_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080058 u32 bus_perf_client;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070059 struct msm_bus_scale_pdata *bus_scale;
Matt Wagantall9515bc22012-07-19 18:13:40 -070060 int boost_uv;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080061 struct device *dev;
62} drv;
63
64static unsigned long acpuclk_krait_get_rate(int cpu)
65{
66 return drv.scalable[cpu].cur_speed->khz;
67}
68
69/* Select a source on the primary MUX. */
70static void set_pri_clk_src(struct scalable *sc, u32 pri_src_sel)
71{
72 u32 regval;
73
74 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
75 regval &= ~0x3;
76 regval |= (pri_src_sel & 0x3);
77 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
78 /* Wait for switch to complete. */
79 mb();
80 udelay(1);
81}
82
83/* Select a source on the secondary MUX. */
84static void set_sec_clk_src(struct scalable *sc, u32 sec_src_sel)
85{
86 u32 regval;
87
88 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
89 regval &= ~(0x3 << 2);
90 regval |= ((sec_src_sel & 0x3) << 2);
91 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
92 /* Wait for switch to complete. */
93 mb();
94 udelay(1);
95}
96
Matt Wagantall302d9a32012-07-03 13:37:29 -070097static int enable_rpm_vreg(struct vreg *vreg)
Matt Wagantalle9b715a2012-01-04 18:16:14 -080098{
Matt Wagantall302d9a32012-07-03 13:37:29 -070099 int ret = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800100
Matt Wagantall75473eb2012-05-31 15:23:22 -0700101 if (vreg->rpm_reg) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700102 ret = rpm_regulator_enable(vreg->rpm_reg);
103 if (ret)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700104 dev_err(drv.dev, "%s regulator enable failed (%d)\n",
Matt Wagantall302d9a32012-07-03 13:37:29 -0700105 vreg->name, ret);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700106 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700107
108 return ret;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700109}
110
111static void disable_rpm_vreg(struct vreg *vreg)
112{
113 int rc;
114
115 if (vreg->rpm_reg) {
116 rc = rpm_regulator_disable(vreg->rpm_reg);
117 if (rc)
118 dev_err(drv.dev, "%s regulator disable failed (%d)\n",
119 vreg->name, rc);
120 }
121}
122
123/* Enable an already-configured HFPLL. */
124static void hfpll_enable(struct scalable *sc, bool skip_regulators)
125{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800126 if (!skip_regulators) {
127 /* Enable regulators required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700128 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
129 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800130 }
131
132 /* Disable PLL bypass mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700133 writel_relaxed(0x2, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800134
135 /*
136 * H/W requires a 5us delay between disabling the bypass and
137 * de-asserting the reset. Delay 10us just to be safe.
138 */
139 mb();
140 udelay(10);
141
142 /* De-assert active-low PLL reset. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700143 writel_relaxed(0x6, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800144
145 /* Wait for PLL to lock. */
146 mb();
147 udelay(60);
148
149 /* Enable PLL output. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700150 writel_relaxed(0x7, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800151}
152
153/* Disable a HFPLL for power-savings or while it's being reprogrammed. */
154static void hfpll_disable(struct scalable *sc, bool skip_regulators)
155{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800156 /*
157 * Disable the PLL output, disable test mode, enable the bypass mode,
158 * and assert the reset.
159 */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700160 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800161
162 if (!skip_regulators) {
163 /* Remove voltage votes required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700164 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
165 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800166 }
167}
168
169/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
170static void hfpll_set_rate(struct scalable *sc, const struct core_speed *tgt_s)
171{
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700172 void __iomem *base = sc->hfpll_base;
173 u32 regval;
174
175 writel_relaxed(tgt_s->pll_l_val, base + drv.hfpll_data->l_offset);
176
177 if (drv.hfpll_data->has_user_reg) {
178 regval = readl_relaxed(base + drv.hfpll_data->user_offset);
179 if (tgt_s->pll_l_val <= drv.hfpll_data->low_vco_l_max)
180 regval &= ~drv.hfpll_data->user_vco_mask;
181 else
182 regval |= drv.hfpll_data->user_vco_mask;
183 writel_relaxed(regval, base + drv.hfpll_data->user_offset);
184 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800185}
186
187/* Return the L2 speed that should be applied. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700188static unsigned int compute_l2_level(struct scalable *sc, unsigned int vote_l)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800189{
Matt Wagantall600ea502012-06-08 18:49:53 -0700190 unsigned int new_l = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800191 int cpu;
192
193 /* Find max L2 speed vote. */
194 sc->l2_vote = vote_l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800195 for_each_present_cpu(cpu)
196 new_l = max(new_l, drv.scalable[cpu].l2_vote);
197
198 return new_l;
199}
200
201/* Update the bus bandwidth request. */
202static void set_bus_bw(unsigned int bw)
203{
204 int ret;
205
206 /* Update bandwidth if request has changed. This may sleep. */
207 ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw);
208 if (ret)
209 dev_err(drv.dev, "bandwidth request failed (%d)\n", ret);
210}
211
212/* Set the CPU or L2 clock speed. */
213static void set_speed(struct scalable *sc, const struct core_speed *tgt_s)
214{
215 const struct core_speed *strt_s = sc->cur_speed;
216
217 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
218 /*
219 * Move to an always-on source running at a frequency
220 * that does not require an elevated CPU voltage.
221 */
222 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
223 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
224
225 /* Re-program HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700226 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800227 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700228 hfpll_enable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800229
230 /* Move to HFPLL. */
231 set_pri_clk_src(sc, tgt_s->pri_src_sel);
232 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
233 set_sec_clk_src(sc, tgt_s->sec_src_sel);
234 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700235 hfpll_disable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800236 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
237 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700238 hfpll_enable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800239 set_pri_clk_src(sc, tgt_s->pri_src_sel);
240 } else {
241 set_sec_clk_src(sc, tgt_s->sec_src_sel);
242 }
243
244 sc->cur_speed = tgt_s;
245}
246
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700247struct vdd_data {
248 int vdd_mem;
249 int vdd_dig;
250 int vdd_core;
251 int ua_core;
252};
253
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800254/* Apply any per-cpu voltage increases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700255static int increase_vdd(int cpu, struct vdd_data *data,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800256 enum setrate_reason reason)
257{
258 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700259 int rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800260
261 /*
262 * Increase vdd_mem active-set before vdd_dig.
263 * vdd_mem should be >= vdd_dig.
264 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700265 if (data->vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700266 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700267 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800268 if (rc) {
269 dev_err(drv.dev,
270 "vdd_mem (cpu%d) increase failed (%d)\n",
271 cpu, rc);
272 return rc;
273 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700274 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800275 }
276
277 /* Increase vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700278 if (data->vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700279 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700280 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800281 if (rc) {
282 dev_err(drv.dev,
283 "vdd_dig (cpu%d) increase failed (%d)\n",
284 cpu, rc);
285 return rc;
286 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700287 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
288 }
289
290 /* Increase current request. */
291 if (data->ua_core > sc->vreg[VREG_CORE].cur_ua) {
292 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
293 data->ua_core);
294 if (rc < 0) {
295 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
296 sc->vreg[VREG_CORE].name, rc);
297 return rc;
298 }
299 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800300 }
301
302 /*
303 * Update per-CPU core voltage. Don't do this for the hotplug path for
304 * which it should already be correct. Attempting to set it is bad
305 * because we don't know what CPU we are running on at this point, but
306 * the CPU regulator API requires we call it from the affected CPU.
307 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700308 if (data->vdd_core > sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800309 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700310 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
311 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800312 if (rc) {
313 dev_err(drv.dev,
314 "vdd_core (cpu%d) increase failed (%d)\n",
315 cpu, rc);
316 return rc;
317 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700318 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800319 }
320
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700321 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800322}
323
324/* Apply any per-cpu voltage decreases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700325static void decrease_vdd(int cpu, struct vdd_data *data,
326 enum setrate_reason reason)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800327{
328 struct scalable *sc = &drv.scalable[cpu];
329 int ret;
330
331 /*
332 * Update per-CPU core voltage. This must be called on the CPU
333 * that's being affected. Don't do this in the hotplug remove path,
334 * where the rail is off and we're executing on the other CPU.
335 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700336 if (data->vdd_core < sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800337 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700338 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
339 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800340 if (ret) {
341 dev_err(drv.dev,
342 "vdd_core (cpu%d) decrease failed (%d)\n",
343 cpu, ret);
344 return;
345 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700346 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
347 }
348
349 /* Decrease current request. */
350 if (data->ua_core < sc->vreg[VREG_CORE].cur_ua) {
351 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
352 data->ua_core);
353 if (ret < 0) {
354 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
355 sc->vreg[VREG_CORE].name, ret);
356 return;
357 }
358 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800359 }
360
361 /* Decrease vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700362 if (data->vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700363 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700364 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800365 if (ret) {
366 dev_err(drv.dev,
367 "vdd_dig (cpu%d) decrease failed (%d)\n",
368 cpu, ret);
369 return;
370 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700371 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800372 }
373
374 /*
375 * Decrease vdd_mem active-set after vdd_dig.
376 * vdd_mem should be >= vdd_dig.
377 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700378 if (data->vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700379 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700380 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800381 if (ret) {
382 dev_err(drv.dev,
383 "vdd_mem (cpu%d) decrease failed (%d)\n",
384 cpu, ret);
385 return;
386 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700387 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800388 }
389}
390
391static int calculate_vdd_mem(const struct acpu_level *tgt)
392{
Matt Wagantall600ea502012-06-08 18:49:53 -0700393 return drv.l2_freq_tbl[tgt->l2_level].vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800394}
395
Matt Wagantall72a38002012-07-18 13:42:55 -0700396static int get_src_dig(const struct core_speed *s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800397{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700398 const int *hfpll_vdd = drv.hfpll_data->vdd;
399 const u32 low_vdd_l_max = drv.hfpll_data->low_vdd_l_max;
Matt Wagantall87465f52012-07-23 22:03:06 -0700400 const u32 nom_vdd_l_max = drv.hfpll_data->nom_vdd_l_max;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800401
Matt Wagantall72a38002012-07-18 13:42:55 -0700402 if (s->src != HFPLL)
403 return hfpll_vdd[HFPLL_VDD_NONE];
Matt Wagantall87465f52012-07-23 22:03:06 -0700404 else if (s->pll_l_val > nom_vdd_l_max)
405 return hfpll_vdd[HFPLL_VDD_HIGH];
Matt Wagantall72a38002012-07-18 13:42:55 -0700406 else if (s->pll_l_val > low_vdd_l_max)
407 return hfpll_vdd[HFPLL_VDD_NOM];
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800408 else
Matt Wagantall72a38002012-07-18 13:42:55 -0700409 return hfpll_vdd[HFPLL_VDD_LOW];
410}
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800411
Matt Wagantall72a38002012-07-18 13:42:55 -0700412static int calculate_vdd_dig(const struct acpu_level *tgt)
413{
414 int l2_pll_vdd_dig, cpu_pll_vdd_dig;
415
416 l2_pll_vdd_dig = get_src_dig(&drv.l2_freq_tbl[tgt->l2_level].speed);
417 cpu_pll_vdd_dig = get_src_dig(&tgt->speed);
418
419 return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig,
420 max(l2_pll_vdd_dig, cpu_pll_vdd_dig));
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800421}
422
Matt Wagantall9515bc22012-07-19 18:13:40 -0700423static bool enable_boost = true;
424module_param_named(boost, enable_boost, bool, S_IRUGO | S_IWUSR);
425
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800426static int calculate_vdd_core(const struct acpu_level *tgt)
427{
Matt Wagantall9515bc22012-07-19 18:13:40 -0700428 return tgt->vdd_core + (enable_boost ? drv.boost_uv : 0);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800429}
430
431/* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */
432static int acpuclk_krait_set_rate(int cpu, unsigned long rate,
433 enum setrate_reason reason)
434{
435 const struct core_speed *strt_acpu_s, *tgt_acpu_s;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800436 const struct acpu_level *tgt;
Matt Wagantall600ea502012-06-08 18:49:53 -0700437 int tgt_l2_l;
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700438 struct vdd_data vdd_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800439 unsigned long flags;
440 int rc = 0;
441
Matt Wagantall5941a332012-07-10 23:20:44 -0700442 if (cpu > num_possible_cpus())
443 return -EINVAL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800444
445 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
446 mutex_lock(&driver_lock);
447
448 strt_acpu_s = drv.scalable[cpu].cur_speed;
449
450 /* Return early if rate didn't change. */
451 if (rate == strt_acpu_s->khz)
452 goto out;
453
454 /* Find target frequency. */
455 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
456 if (tgt->speed.khz == rate) {
457 tgt_acpu_s = &tgt->speed;
458 break;
459 }
460 }
461 if (tgt->speed.khz == 0) {
462 rc = -EINVAL;
463 goto out;
464 }
465
466 /* Calculate voltage requirements for the current CPU. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700467 vdd_data.vdd_mem = calculate_vdd_mem(tgt);
468 vdd_data.vdd_dig = calculate_vdd_dig(tgt);
469 vdd_data.vdd_core = calculate_vdd_core(tgt);
470 vdd_data.ua_core = tgt->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800471
472 /* Increase VDD levels if needed. */
473 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700474 rc = increase_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800475 if (rc)
476 goto out;
477 }
478
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700479 dev_dbg(drv.dev, "Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
480 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800481
482 /* Set the new CPU speed. */
483 set_speed(&drv.scalable[cpu], tgt_acpu_s);
484
485 /*
486 * Update the L2 vote and apply the rate change. A spinlock is
487 * necessary to ensure L2 rate is calculated and set atomically
488 * with the CPU frequency, even if acpuclk_krait_set_rate() is
489 * called from an atomic context and the driver_lock mutex is not
490 * acquired.
491 */
492 spin_lock_irqsave(&l2_lock, flags);
493 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
Matt Wagantall600ea502012-06-08 18:49:53 -0700494 set_speed(&drv.scalable[L2], &drv.l2_freq_tbl[tgt_l2_l].speed);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800495 spin_unlock_irqrestore(&l2_lock, flags);
496
497 /* Nothing else to do for power collapse or SWFI. */
498 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
499 goto out;
500
501 /* Update bus bandwith request. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700502 set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800503
504 /* Drop VDD levels if we can. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700505 decrease_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800506
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700507 dev_dbg(drv.dev, "ACPU%d speed change complete\n", cpu);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800508
509out:
510 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
511 mutex_unlock(&driver_lock);
512 return rc;
513}
514
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700515static struct acpuclk_data acpuclk_krait_data = {
516 .set_rate = acpuclk_krait_set_rate,
517 .get_rate = acpuclk_krait_get_rate,
518};
519
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800520/* Initialize a HFPLL at a given rate and enable it. */
521static void __init hfpll_init(struct scalable *sc,
522 const struct core_speed *tgt_s)
523{
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700524 dev_dbg(drv.dev, "Initializing HFPLL%d\n", sc - drv.scalable);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800525
526 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700527 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800528
529 /* Configure PLL parameters for integer mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700530 writel_relaxed(drv.hfpll_data->config_val,
531 sc->hfpll_base + drv.hfpll_data->config_offset);
532 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->m_offset);
533 writel_relaxed(1, sc->hfpll_base + drv.hfpll_data->n_offset);
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700534 if (drv.hfpll_data->has_user_reg)
535 writel_relaxed(drv.hfpll_data->user_val,
536 sc->hfpll_base + drv.hfpll_data->user_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800537
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700538 /* Program droop controller, if supported */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700539 if (drv.hfpll_data->has_droop_ctl)
540 writel_relaxed(drv.hfpll_data->droop_val,
541 sc->hfpll_base + drv.hfpll_data->droop_offset);
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700542
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800543 /* Set an initial rate and enable the PLL. */
544 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700545 hfpll_enable(sc, false);
546}
547
Matt Wagantall302d9a32012-07-03 13:37:29 -0700548static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700549 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700550{
551 int ret;
552
553 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700554 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700555
556 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
557 sc->vreg[vreg].name);
558 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700559 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
560 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
561 sc->vreg[vreg].name, ret);
562 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700563 }
564
565 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
566 sc->vreg[vreg].max_vdd);
567 if (ret) {
568 dev_err(drv.dev, "%s initialization failed (%d)\n",
569 sc->vreg[vreg].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700570 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700571 }
572 sc->vreg[vreg].cur_vdd = vdd;
573
Matt Wagantall302d9a32012-07-03 13:37:29 -0700574 if (enable) {
575 ret = enable_rpm_vreg(&sc->vreg[vreg]);
576 if (ret)
577 goto err_conf;
578 }
579
580 return 0;
581
582err_conf:
583 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
584err_get:
585 return ret;
586}
587
588static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
589 enum vregs vreg)
590{
591 if (!sc->vreg[vreg].rpm_reg)
592 return;
593
594 disable_rpm_vreg(&sc->vreg[vreg]);
595 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800596}
597
598/* Voltage regulator initialization. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700599static int __cpuinit regulator_init(struct scalable *sc,
600 const struct acpu_level *acpu_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800601{
Matt Wagantall754ee272012-06-18 13:40:26 -0700602 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800603
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700604 vdd_mem = calculate_vdd_mem(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700605 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
606 if (ret)
607 goto err_mem;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700608
609 vdd_dig = calculate_vdd_dig(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700610 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
611 if (ret)
612 goto err_dig;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700613
Matt Wagantall302d9a32012-07-03 13:37:29 -0700614 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700615 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700616 if (ret)
617 goto err_hfpll_a;
618 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700619 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700620 if (ret)
621 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700622
Matt Wagantall754ee272012-06-18 13:40:26 -0700623 /* Setup Krait CPU regulators and initial core voltage. */
624 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
625 sc->vreg[VREG_CORE].name);
626 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700627 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
628 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
629 sc->vreg[VREG_CORE].name, ret);
630 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700631 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700632 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
633 acpu_level->ua_core);
634 if (ret < 0) {
635 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
636 sc->vreg[VREG_CORE].name, ret);
637 goto err_core_conf;
638 }
639 sc->vreg[VREG_CORE].cur_ua = acpu_level->ua_core;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700640 vdd_core = calculate_vdd_core(acpu_level);
Matt Wagantall754ee272012-06-18 13:40:26 -0700641 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
642 sc->vreg[VREG_CORE].max_vdd);
643 if (ret) {
644 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
645 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700646 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700647 }
648 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
Matt Wagantall754ee272012-06-18 13:40:26 -0700649 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
650 if (ret) {
651 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
652 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700653 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800654 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700655
656 return 0;
657
658err_core_conf:
659 regulator_put(sc->vreg[VREG_CORE].reg);
660err_core_get:
661 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
662err_hfpll_b:
663 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
664err_hfpll_a:
665 rpm_regulator_cleanup(sc, VREG_DIG);
666err_dig:
667 rpm_regulator_cleanup(sc, VREG_MEM);
668err_mem:
669 return ret;
670}
671
672static void __cpuinit regulator_cleanup(struct scalable *sc)
673{
674 regulator_disable(sc->vreg[VREG_CORE].reg);
675 regulator_put(sc->vreg[VREG_CORE].reg);
676 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
677 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
678 rpm_regulator_cleanup(sc, VREG_DIG);
679 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800680}
681
682/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700683static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700684 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800685{
686 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700687 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800688
689 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700690 if (sc->aux_clk_sel_phys) {
691 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700692 if (!aux_reg)
693 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700694 writel_relaxed(sc->aux_clk_sel, aux_reg);
695 iounmap(aux_reg);
696 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800697
698 /* Switch away from the HFPLL while it's re-initialized. */
699 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
700 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
701 hfpll_init(sc, tgt_s);
702
703 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
704 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
705 regval &= ~(0x3 << 6);
706 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
707
708 /* Switch to the target clock source. */
709 set_sec_clk_src(sc, tgt_s->sec_src_sel);
710 set_pri_clk_src(sc, tgt_s->pri_src_sel);
711 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700712
713 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800714}
715
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700716static void __cpuinit fill_cur_core_speed(struct core_speed *s,
717 struct scalable *sc)
718{
719 s->pri_src_sel = get_l2_indirect_reg(sc->l2cpmr_iaddr) & 0x3;
720 s->sec_src_sel = (get_l2_indirect_reg(sc->l2cpmr_iaddr) >> 2) & 0x3;
721 s->pll_l_val = readl_relaxed(sc->hfpll_base + drv.hfpll_data->l_offset);
722}
723
724static bool __cpuinit speed_equal(const struct core_speed *s1,
725 const struct core_speed *s2)
726{
727 return (s1->pri_src_sel == s2->pri_src_sel &&
728 s1->sec_src_sel == s2->sec_src_sel &&
729 s1->pll_l_val == s2->pll_l_val);
730}
731
732static const struct acpu_level __cpuinit *find_cur_acpu_level(int cpu)
733{
734 struct scalable *sc = &drv.scalable[cpu];
735 const struct acpu_level *l;
736 struct core_speed cur_speed;
737
738 fill_cur_core_speed(&cur_speed, sc);
739 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
740 if (speed_equal(&l->speed, &cur_speed))
741 return l;
742 return NULL;
743}
744
745static const struct l2_level __init *find_cur_l2_level(void)
746{
747 struct scalable *sc = &drv.scalable[L2];
748 const struct l2_level *l;
749 struct core_speed cur_speed;
750
751 fill_cur_core_speed(&cur_speed, sc);
752 for (l = drv.l2_freq_tbl; l->speed.khz != 0; l++)
753 if (speed_equal(&l->speed, &cur_speed))
754 return l;
755 return NULL;
756}
757
758static const struct acpu_level __cpuinit *find_min_acpu_level(void)
759{
760 struct acpu_level *l;
761
762 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
763 if (l->use_for_scaling)
764 return l;
765
766 return NULL;
767}
768
Matt Wagantall302d9a32012-07-03 13:37:29 -0700769static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800770{
Matt Wagantall754ee272012-06-18 13:40:26 -0700771 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700772 const struct acpu_level *acpu_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700773 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800774
Matt Wagantall754ee272012-06-18 13:40:26 -0700775 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700776 if (!sc->hfpll_base) {
777 ret = -ENOMEM;
778 goto err_ioremap;
779 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700780
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700781 acpu_level = find_cur_acpu_level(cpu);
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700782 if (!acpu_level) {
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700783 acpu_level = find_min_acpu_level();
784 if (!acpu_level) {
785 ret = -ENODEV;
786 goto err_table;
787 }
788 dev_dbg(drv.dev, "CPU%d is running at an unknown rate. Defaulting to %lu KHz.\n",
789 cpu, acpu_level->speed.khz);
790 } else {
791 dev_dbg(drv.dev, "CPU%d is running at %lu KHz\n", cpu,
792 acpu_level->speed.khz);
793 }
794
795 ret = regulator_init(sc, acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700796 if (ret)
797 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700798
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700799 ret = init_clock_sources(sc, &acpu_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700800 if (ret)
801 goto err_clocks;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700802
803 sc->l2_vote = acpu_level->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700804 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700805
806 return 0;
807
808err_clocks:
809 regulator_cleanup(sc);
810err_regulators:
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700811err_table:
Matt Wagantall302d9a32012-07-03 13:37:29 -0700812 iounmap(sc->hfpll_base);
813err_ioremap:
814 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800815}
816
817/* Register with bus driver. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700818static void __init bus_init(const struct l2_level *l2_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800819{
820 int ret;
821
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700822 drv.bus_perf_client = msm_bus_scale_register_client(drv.bus_scale);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800823 if (!drv.bus_perf_client) {
824 dev_err(drv.dev, "unable to register bus client\n");
825 BUG();
826 }
827
Matt Wagantall754ee272012-06-18 13:40:26 -0700828 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700829 l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800830 if (ret)
831 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
832}
833
834#ifdef CONFIG_CPU_FREQ_MSM
835static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
836
837static void __init cpufreq_table_init(void)
838{
839 int cpu;
840
841 for_each_possible_cpu(cpu) {
842 int i, freq_cnt = 0;
843 /* Construct the freq_table tables from acpu_freq_tbl. */
844 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
845 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
846 if (drv.acpu_freq_tbl[i].use_for_scaling) {
847 freq_table[cpu][freq_cnt].index = freq_cnt;
848 freq_table[cpu][freq_cnt].frequency
849 = drv.acpu_freq_tbl[i].speed.khz;
850 freq_cnt++;
851 }
852 }
853 /* freq_table not big enough to store all usable freqs. */
854 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
855
856 freq_table[cpu][freq_cnt].index = freq_cnt;
857 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
858
859 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
860 cpu, freq_cnt);
861
862 /* Register table with CPUFreq. */
863 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
864 }
865}
866#else
867static void __init cpufreq_table_init(void) {}
868#endif
869
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800870static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
871 unsigned long action, void *hcpu)
872{
873 static int prev_khz[NR_CPUS];
874 int rc, cpu = (int)hcpu;
875 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700876 unsigned long hot_unplug_khz = acpuclk_krait_data.power_collapse_khz;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800877
878 switch (action & ~CPU_TASKS_FROZEN) {
879 case CPU_DEAD:
880 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
881 /* Fall through. */
882 case CPU_UP_CANCELED:
Matt Wagantallb7c231b2012-07-24 18:40:17 -0700883 acpuclk_krait_set_rate(cpu, hot_unplug_khz, SETRATE_HOTPLUG);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800884 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
885 break;
886 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700887 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700888 rc = per_cpu_init(cpu);
889 if (rc)
890 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700891 break;
892 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800893 if (WARN_ON(!prev_khz[cpu]))
894 return NOTIFY_BAD;
895 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700896 sc->vreg[VREG_CORE].cur_ua);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800897 if (rc < 0)
898 return NOTIFY_BAD;
899 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
900 break;
901 default:
902 break;
903 }
904
905 return NOTIFY_OK;
906}
907
908static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
909 .notifier_call = acpuclk_cpu_callback,
910};
911
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700912static const int krait_needs_vmin(void)
913{
914 switch (read_cpuid_id()) {
915 case 0x511F04D0: /* KR28M2A20 */
916 case 0x511F04D1: /* KR28M2A21 */
917 case 0x510F06F0: /* KR28M4A10 */
918 return 1;
919 default:
920 return 0;
921 };
922}
923
924static void krait_apply_vmin(struct acpu_level *tbl)
925{
926 for (; tbl->speed.khz != 0; tbl++)
927 if (tbl->vdd_core < 1150000)
928 tbl->vdd_core = 1150000;
929}
930
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700931static int __init select_freq_plan(u32 qfprom_phys)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800932{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800933 void __iomem *qfprom_base;
934 u32 pte_efuse, pvs, tbl_idx;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700935 char *pvs_names[] = { "Slow", "Nominal", "Fast", "Faster", "Unknown" };
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800936
937 qfprom_base = ioremap(qfprom_phys, SZ_256);
938 /* Select frequency tables. */
939 if (qfprom_base) {
940 pte_efuse = readl_relaxed(qfprom_base + PTE_EFUSE);
941 pvs = (pte_efuse >> 10) & 0x7;
942 iounmap(qfprom_base);
943 if (pvs == 0x7)
944 pvs = (pte_efuse >> 13) & 0x7;
945
946 switch (pvs) {
947 case 0x0:
948 case 0x7:
949 tbl_idx = PVS_SLOW;
950 break;
951 case 0x1:
952 tbl_idx = PVS_NOMINAL;
953 break;
954 case 0x3:
955 tbl_idx = PVS_FAST;
956 break;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700957 case 0x4:
958 tbl_idx = PVS_FASTER;
959 break;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800960 default:
961 tbl_idx = PVS_UNKNOWN;
962 break;
963 }
964 } else {
965 tbl_idx = PVS_UNKNOWN;
966 dev_err(drv.dev, "Unable to map QFPROM base\n");
967 }
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700968 if (tbl_idx == PVS_UNKNOWN) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800969 tbl_idx = PVS_SLOW;
970 dev_warn(drv.dev, "ACPU PVS: Defaulting to %s\n",
971 pvs_names[tbl_idx]);
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700972 } else {
973 dev_info(drv.dev, "ACPU PVS: %s\n", pvs_names[tbl_idx]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800974 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800975
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700976 return tbl_idx;
977}
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700978
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700979static void __init drv_data_init(struct device *dev,
980 const struct acpuclk_krait_params *params)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800981{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700982 int tbl_idx;
983
984 drv.dev = dev;
985 drv.scalable = kmemdup(params->scalable, params->scalable_size,
986 GFP_KERNEL);
987 BUG_ON(!drv.scalable);
988
989 drv.hfpll_data = kmemdup(params->hfpll_data, sizeof(*drv.hfpll_data),
990 GFP_KERNEL);
991 BUG_ON(!drv.hfpll_data);
992
993 drv.l2_freq_tbl = kmemdup(params->l2_freq_tbl, params->l2_freq_tbl_size,
994 GFP_KERNEL);
995 BUG_ON(!drv.l2_freq_tbl);
996
997 drv.bus_scale = kmemdup(params->bus_scale, sizeof(*drv.bus_scale),
998 GFP_KERNEL);
999 BUG_ON(!drv.bus_scale);
1000 drv.bus_scale->usecase = kmemdup(drv.bus_scale->usecase,
1001 drv.bus_scale->num_usecases * sizeof(*drv.bus_scale->usecase),
1002 GFP_KERNEL);
1003 BUG_ON(!drv.bus_scale->usecase);
1004
1005 tbl_idx = select_freq_plan(params->qfprom_phys_base);
1006 drv.acpu_freq_tbl = kmemdup(params->pvs_tables[tbl_idx].table,
1007 params->pvs_tables[tbl_idx].size,
1008 GFP_KERNEL);
1009 BUG_ON(!drv.acpu_freq_tbl);
Matt Wagantall9515bc22012-07-19 18:13:40 -07001010 drv.boost_uv = params->pvs_tables[tbl_idx].boost_uv;
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001011
1012 acpuclk_krait_data.power_collapse_khz = params->stby_khz;
1013 acpuclk_krait_data.wait_for_irq_khz = params->stby_khz;
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001014}
1015
1016static void __init hw_init(void)
1017{
1018 struct scalable *l2 = &drv.scalable[L2];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001019 const struct l2_level *l2_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -07001020 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001021
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001022 if (krait_needs_vmin())
1023 krait_apply_vmin(drv.acpu_freq_tbl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001024
Matt Wagantall754ee272012-06-18 13:40:26 -07001025 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
1026 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -07001027
Matt Wagantall302d9a32012-07-03 13:37:29 -07001028 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
1029 l2->vreg[VREG_HFPLL_A].max_vdd, false);
1030 BUG_ON(rc);
1031 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
1032 l2->vreg[VREG_HFPLL_B].max_vdd, false);
1033 BUG_ON(rc);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001034
1035 l2_level = find_cur_l2_level();
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001036 if (!l2_level) {
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001037 l2_level = drv.l2_freq_tbl;
Matt Wagantallb7c231b2012-07-24 18:40:17 -07001038 dev_dbg(drv.dev, "L2 is running at an unknown rate. Defaulting to %lu KHz.\n",
1039 l2_level->speed.khz);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001040 } else {
1041 dev_dbg(drv.dev, "L2 is running at %lu KHz\n",
1042 l2_level->speed.khz);
1043 }
1044
1045 rc = init_clock_sources(l2, &l2_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -07001046 BUG_ON(rc);
1047
1048 for_each_online_cpu(cpu) {
1049 rc = per_cpu_init(cpu);
1050 BUG_ON(rc);
1051 }
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001052
1053 bus_init(l2_level);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001054}
1055
1056int __init acpuclk_krait_init(struct device *dev,
1057 const struct acpuclk_krait_params *params)
1058{
1059 drv_data_init(dev, params);
1060 hw_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001061
1062 cpufreq_table_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001063 acpuclk_register(&acpuclk_krait_data);
1064 register_hotcpu_notifier(&acpuclk_cpu_notifier);
1065
1066 return 0;
1067}