blob: 2180a8daf4b28b58522bf500cc07cec57366d305 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 *
3 * Copyright (C) 2007 Google, Inc.
4 * Copyright (c) 2007-2011, Code Aurora Forum. All rights reserved.
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/version.h>
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/errno.h>
21#include <linux/string.h>
22#include <linux/delay.h>
23#include <linux/clk.h>
24#include <linux/cpufreq.h>
25#include <linux/mutex.h>
26#include <linux/io.h>
27#include <linux/sort.h>
28#include <mach/board.h>
29#include <mach/msm_iomap.h>
30#include <asm/mach-types.h>
31
32#include "smd_private.h"
33#include "clock.h"
34#include "acpuclock.h"
35#include "spm.h"
36
37#define SCSS_CLK_CTL_ADDR (MSM_ACC_BASE + 0x04)
38#define SCSS_CLK_SEL_ADDR (MSM_ACC_BASE + 0x08)
39
40#define PLL2_L_VAL_ADDR (MSM_CLK_CTL_BASE + 0x33C)
41#define PLL2_M_VAL_ADDR (MSM_CLK_CTL_BASE + 0x340)
42#define PLL2_N_VAL_ADDR (MSM_CLK_CTL_BASE + 0x344)
43#define PLL2_CONFIG_ADDR (MSM_CLK_CTL_BASE + 0x34C)
44
45#define VREF_SEL 1 /* 0: 0.625V (50mV step), 1: 0.3125V (25mV step). */
46#define V_STEP (25 * (2 - VREF_SEL)) /* Minimum voltage step size. */
47#define VREG_DATA (VREG_CONFIG | (VREF_SEL << 5))
48#define VREG_CONFIG (BIT(7) | BIT(6)) /* Enable VREG, pull-down if disabled. */
49/* Cause a compile error if the voltage is not a multiple of the step size. */
50#define MV(mv) ((mv) / (!((mv) % V_STEP)))
51/* mv = (750mV + (raw * 25mV)) * (2 - VREF_SEL) */
52#define VDD_RAW(mv) (((MV(mv) / V_STEP) - 30) | VREG_DATA)
53
54#define MAX_AXI_KHZ 192000
55
56struct clock_state {
57 struct clkctl_acpu_speed *current_speed;
58 struct mutex lock;
59 uint32_t acpu_switch_time_us;
60 uint32_t vdd_switch_time_us;
61 struct clk *ebi1_clk;
62};
63
64struct pll {
65 unsigned int l;
66 unsigned int m;
67 unsigned int n;
68 unsigned int pre_div;
69};
70
71struct clkctl_acpu_speed {
72 unsigned int use_for_scaling;
73 unsigned int acpu_clk_khz;
74 int src;
75 unsigned int acpu_src_sel;
76 unsigned int acpu_src_div;
77 unsigned int axi_clk_hz;
78 unsigned int vdd_mv;
79 unsigned int vdd_raw;
80 struct pll *pll_rate;
81 unsigned long lpj; /* loops_per_jiffy */
82};
83
84static struct clock_state drv_state = { 0 };
85
86/* Switch to this when reprogramming PLL2 */
87static struct clkctl_acpu_speed *backup_s;
88
89static struct pll pll2_tbl[] = {
90 { 42, 0, 1, 0 }, /* 806 MHz */
91 { 53, 1, 3, 0 }, /* 1024 MHz */
92 { 125, 0, 1, 1 }, /* 1200 MHz */
93 { 73, 0, 1, 0 }, /* 1401 MHz */
94};
95
96/* Use negative numbers for sources that can't be enabled/disabled */
97
98enum acpuclk_source {
99 LPXO = -2,
100 AXI = -1,
101 PLL_0 = 0,
102 PLL_1,
103 PLL_2,
104 PLL_3,
105 MAX_SOURCE
106};
107
108static struct clk *acpuclk_sources[MAX_SOURCE];
109
110/*
111 * Each ACPU frequency has a certain minimum MSMC1 voltage requirement
112 * that is implicitly met by voting for a specific minimum AXI frequency.
113 * Do NOT change the AXI frequency unless you are _absoulutely_ sure you
114 * know all the h/w requirements.
115 */
116static struct clkctl_acpu_speed acpu_freq_tbl[] = {
117 { 0, 24576, LPXO, 0, 0, 30720000, 900, VDD_RAW(900) },
118 { 0, 61440, PLL_3, 5, 11, 61440000, 900, VDD_RAW(900) },
119 { 1, 122880, PLL_3, 5, 5, 61440000, 900, VDD_RAW(900) },
120 { 0, 184320, PLL_3, 5, 4, 61440000, 900, VDD_RAW(900) },
121 { 0, MAX_AXI_KHZ, AXI, 1, 0, 61440000, 900, VDD_RAW(900) },
122 { 1, 245760, PLL_3, 5, 2, 61440000, 900, VDD_RAW(900) },
123 { 1, 368640, PLL_3, 5, 1, 122800000, 900, VDD_RAW(900) },
124 /* AXI has MSMC1 implications. See above. */
125 { 1, 768000, PLL_1, 2, 0, 153600000, 1050, VDD_RAW(1050) },
126 /*
127 * AXI has MSMC1 implications. See above.
128 */
129 { 1, 806400, PLL_2, 3, 0, UINT_MAX, 1100, VDD_RAW(1100), &pll2_tbl[0]},
130 { 1, 1024000, PLL_2, 3, 0, UINT_MAX, 1200, VDD_RAW(1200), &pll2_tbl[1]},
131 { 1, 1200000, PLL_2, 3, 0, UINT_MAX, 1200, VDD_RAW(1200), &pll2_tbl[2]},
132 { 1, 1401600, PLL_2, 3, 0, UINT_MAX, 1250, VDD_RAW(1250), &pll2_tbl[3]},
133 { 0 }
134};
135
136#define POWER_COLLAPSE_KHZ MAX_AXI_KHZ
137unsigned long acpuclk_power_collapse(void)
138{
139 int ret = acpuclk_get_rate(smp_processor_id());
140 acpuclk_set_rate(smp_processor_id(), POWER_COLLAPSE_KHZ, SETRATE_PC);
141 return ret;
142}
143
144#define WAIT_FOR_IRQ_KHZ MAX_AXI_KHZ
145unsigned long acpuclk_wait_for_irq(void)
146{
147 int ret = acpuclk_get_rate(smp_processor_id());
148 acpuclk_set_rate(smp_processor_id(), WAIT_FOR_IRQ_KHZ, SETRATE_SWFI);
149 return ret;
150}
151
152static int acpuclk_set_acpu_vdd(struct clkctl_acpu_speed *s)
153{
154 int ret = msm_spm_set_vdd(0, s->vdd_raw);
155 if (ret)
156 return ret;
157
158 /* Wait for voltage to stabilize. */
159 udelay(drv_state.vdd_switch_time_us);
160 return 0;
161}
162
163/* Assumes PLL2 is off and the acpuclock isn't sourced from PLL2 */
164static void acpuclk_config_pll2(struct pll *pll)
165{
166 uint32_t config = readl_relaxed(PLL2_CONFIG_ADDR);
167
168 /* Make sure write to disable PLL_2 has completed
169 * before reconfiguring that PLL. */
170 mb();
171 writel_relaxed(pll->l, PLL2_L_VAL_ADDR);
172 writel_relaxed(pll->m, PLL2_M_VAL_ADDR);
173 writel_relaxed(pll->n, PLL2_N_VAL_ADDR);
174 if (pll->pre_div)
175 config |= BIT(15);
176 else
177 config &= ~BIT(15);
178 writel_relaxed(config, PLL2_CONFIG_ADDR);
179 /* Make sure PLL is programmed before returning. */
180 mb();
181}
182
183/* Set clock source and divider given a clock speed */
184static void acpuclk_set_src(const struct clkctl_acpu_speed *s)
185{
186 uint32_t reg_clksel, reg_clkctl, src_sel;
187
188 reg_clksel = readl_relaxed(SCSS_CLK_SEL_ADDR);
189
190 /* CLK_SEL_SRC1NO */
191 src_sel = reg_clksel & 1;
192
193 /* Program clock source and divider. */
194 reg_clkctl = readl_relaxed(SCSS_CLK_CTL_ADDR);
195 reg_clkctl &= ~(0xFF << (8 * src_sel));
196 reg_clkctl |= s->acpu_src_sel << (4 + 8 * src_sel);
197 reg_clkctl |= s->acpu_src_div << (0 + 8 * src_sel);
198 writel_relaxed(reg_clkctl, SCSS_CLK_CTL_ADDR);
199
200 /* Toggle clock source. */
201 reg_clksel ^= 1;
202
203 /* Program clock source selection. */
204 writel_relaxed(reg_clksel, SCSS_CLK_SEL_ADDR);
205
206 /* Make sure switch to new source is complete. */
207 mb();
208}
209
210int acpuclk_set_rate(int cpu, unsigned long rate, enum setrate_reason reason)
211{
212 struct clkctl_acpu_speed *tgt_s, *strt_s;
213 int res, rc = 0;
214
215 if (reason == SETRATE_CPUFREQ)
216 mutex_lock(&drv_state.lock);
217
218 strt_s = drv_state.current_speed;
219
220 if (rate == strt_s->acpu_clk_khz)
221 goto out;
222
223 for (tgt_s = acpu_freq_tbl; tgt_s->acpu_clk_khz != 0; tgt_s++) {
224 if (tgt_s->acpu_clk_khz == rate)
225 break;
226 }
227 if (tgt_s->acpu_clk_khz == 0) {
228 rc = -EINVAL;
229 goto out;
230 }
231
232 if (reason == SETRATE_CPUFREQ) {
233 /* Increase VDD if needed. */
234 if (tgt_s->vdd_mv > strt_s->vdd_mv) {
235 rc = acpuclk_set_acpu_vdd(tgt_s);
236 if (rc < 0) {
237 pr_err("ACPU VDD increase to %d mV failed "
238 "(%d)\n", tgt_s->vdd_mv, rc);
239 goto out;
240 }
241 }
242 }
243
244 pr_debug("Switching from ACPU rate %u KHz -> %u KHz\n",
245 strt_s->acpu_clk_khz, tgt_s->acpu_clk_khz);
246
247 /* Increase the AXI bus frequency if needed. This must be done before
248 * increasing the ACPU frequency, since voting for high AXI rates
249 * implicitly takes care of increasing the MSMC1 voltage, as needed. */
250 if (tgt_s->axi_clk_hz > strt_s->axi_clk_hz) {
251 rc = clk_set_min_rate(drv_state.ebi1_clk,
252 tgt_s->axi_clk_hz);
253 if (rc < 0) {
254 pr_err("Setting AXI min rate failed (%d)\n", rc);
255 goto out;
256 }
257 }
258
259 /* Move off of PLL2 if we're reprogramming it */
260 if (tgt_s->src == PLL_2 && strt_s->src == PLL_2) {
261 clk_enable(acpuclk_sources[backup_s->src]);
262 acpuclk_set_src(backup_s);
263 clk_disable(acpuclk_sources[strt_s->src]);
264 }
265
266 /* Reconfigure PLL2 if we're moving to it */
267 if (tgt_s->src == PLL_2)
268 acpuclk_config_pll2(tgt_s->pll_rate);
269
270 /* Make sure target PLL is on. */
271 if ((strt_s->src != tgt_s->src && tgt_s->src >= 0) ||
272 (tgt_s->src == PLL_2 && strt_s->src == PLL_2)) {
273 pr_debug("Enabling PLL %d\n", tgt_s->src);
274 clk_enable(acpuclk_sources[tgt_s->src]);
275 }
276
277 /* Perform the frequency switch */
278 acpuclk_set_src(tgt_s);
279 drv_state.current_speed = tgt_s;
280 loops_per_jiffy = tgt_s->lpj;
281
282 if (tgt_s->src == PLL_2 && strt_s->src == PLL_2)
283 clk_disable(acpuclk_sources[backup_s->src]);
284
285 /* Nothing else to do for SWFI. */
286 if (reason == SETRATE_SWFI)
287 goto out;
288
289 /* Turn off previous PLL if not used. */
290 if (strt_s->src != tgt_s->src && strt_s->src >= 0) {
291 pr_debug("Disabling PLL %d\n", strt_s->src);
292 clk_disable(acpuclk_sources[strt_s->src]);
293 }
294
295 /* Decrease the AXI bus frequency if we can. */
296 if (tgt_s->axi_clk_hz < strt_s->axi_clk_hz) {
297 res = clk_set_min_rate(drv_state.ebi1_clk,
298 tgt_s->axi_clk_hz);
299 if (res < 0)
300 pr_warning("Setting AXI min rate failed (%d)\n", res);
301 }
302
303 /* Nothing else to do for power collapse. */
304 if (reason == SETRATE_PC)
305 goto out;
306
307 /* Drop VDD level if we can. */
308 if (tgt_s->vdd_mv < strt_s->vdd_mv) {
309 res = acpuclk_set_acpu_vdd(tgt_s);
310 if (res)
311 pr_warning("ACPU VDD decrease to %d mV failed (%d)\n",
312 tgt_s->vdd_mv, res);
313 }
314
315 pr_debug("ACPU speed change complete\n");
316out:
317 if (reason == SETRATE_CPUFREQ)
318 mutex_unlock(&drv_state.lock);
319
320 return rc;
321}
322
323unsigned long acpuclk_get_rate(int cpu)
324{
325 WARN_ONCE(drv_state.current_speed == NULL,
326 "acpuclk_get_rate: not initialized\n");
327 if (drv_state.current_speed)
328 return drv_state.current_speed->acpu_clk_khz;
329 else
330 return 0;
331}
332
333uint32_t acpuclk_get_switch_time(void)
334{
335 return drv_state.acpu_switch_time_us;
336}
337
338/*----------------------------------------------------------------------------
339 * Clock driver initialization
340 *---------------------------------------------------------------------------*/
341
342static void __init acpuclk_init(void)
343{
344 struct clkctl_acpu_speed *s;
345 uint32_t div, sel, src_num;
346 uint32_t reg_clksel, reg_clkctl;
347 int res;
348 u8 pll2_l = readl_relaxed(PLL2_L_VAL_ADDR) & 0xFF;
349
350 drv_state.ebi1_clk = clk_get(NULL, "ebi1_clk");
351 BUG_ON(IS_ERR(drv_state.ebi1_clk));
352
353 reg_clksel = readl_relaxed(SCSS_CLK_SEL_ADDR);
354
355 /* Determine the ACPU clock rate. */
356 switch ((reg_clksel >> 1) & 0x3) {
357 case 0: /* Running off the output of the raw clock source mux. */
358 reg_clkctl = readl_relaxed(SCSS_CLK_CTL_ADDR);
359 src_num = reg_clksel & 0x1;
360 sel = (reg_clkctl >> (12 - (8 * src_num))) & 0x7;
361 div = (reg_clkctl >> (8 - (8 * src_num))) & 0xF;
362
363 /* Check frequency table for matching sel/div pair. */
364 for (s = acpu_freq_tbl; s->acpu_clk_khz != 0; s++) {
365 if (s->acpu_src_sel == sel && s->acpu_src_div == div)
366 break;
367 }
368 if (s->acpu_clk_khz == 0) {
369 pr_err("Error - ACPU clock reports invalid speed\n");
370 return;
371 }
372 break;
373 case 2: /* Running off of the SCPLL selected through the core mux. */
374 /* Switch to run off of the SCPLL selected through the raw
375 * clock source mux. */
376 for (s = acpu_freq_tbl; s->acpu_clk_khz != 0
377 && s->src != PLL_2 && s->acpu_src_div == 0; s++)
378 ;
379 if (s->acpu_clk_khz != 0) {
380 /* Program raw clock source mux. */
381 acpuclk_set_src(s);
382
383 /* Switch to raw clock source input of the core mux. */
384 reg_clksel = readl_relaxed(SCSS_CLK_SEL_ADDR);
385 reg_clksel &= ~(0x3 << 1);
386 writel_relaxed(reg_clksel, SCSS_CLK_SEL_ADDR);
387 break;
388 }
389 /* else fall through */
390 default:
391 pr_err("Error - ACPU clock reports invalid source\n");
392 return;
393 }
394
395 /* Look at PLL2's L val to determine what speed PLL2 is running at */
396 if (s->src == PLL_2)
397 for ( ; s->acpu_clk_khz; s++)
398 if (s->pll_rate && s->pll_rate->l == pll2_l)
399 break;
400
401 /* Set initial ACPU VDD. */
402 acpuclk_set_acpu_vdd(s);
403
404 drv_state.current_speed = s;
405
406 /* Initialize current PLL's reference count. */
407 if (s->src >= 0)
408 clk_enable(acpuclk_sources[s->src]);
409
410 res = clk_set_min_rate(drv_state.ebi1_clk, s->axi_clk_hz);
411 if (res < 0)
412 pr_warning("Setting AXI min rate failed!\n");
413
414 pr_info("ACPU running at %d KHz\n", s->acpu_clk_khz);
415
416 return;
417}
418
419/* Initalize the lpj field in the acpu_freq_tbl. */
420static void __init lpj_init(void)
421{
422 int i;
423 const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
424
425 for (i = 0; acpu_freq_tbl[i].acpu_clk_khz; i++) {
426 acpu_freq_tbl[i].lpj = cpufreq_scale(loops_per_jiffy,
427 base_clk->acpu_clk_khz,
428 acpu_freq_tbl[i].acpu_clk_khz);
429 }
430}
431
432#ifdef CONFIG_CPU_FREQ_MSM
433static struct cpufreq_frequency_table cpufreq_tbl[ARRAY_SIZE(acpu_freq_tbl)];
434
435static void setup_cpufreq_table(void)
436{
437 unsigned i = 0;
438 const struct clkctl_acpu_speed *speed;
439
440 for (speed = acpu_freq_tbl; speed->acpu_clk_khz; speed++)
441 if (speed->use_for_scaling) {
442 cpufreq_tbl[i].index = i;
443 cpufreq_tbl[i].frequency = speed->acpu_clk_khz;
444 i++;
445 }
446 cpufreq_tbl[i].frequency = CPUFREQ_TABLE_END;
447
448 cpufreq_frequency_table_get_attr(cpufreq_tbl, smp_processor_id());
449}
450#else
451static inline void setup_cpufreq_table(void) { }
452#endif
453
454/*
455 * Truncate the frequency table at the current PLL2 rate and determine the
456 * backup PLL to use when scaling PLL2.
457 */
458void __init pll2_fixup(void)
459{
460 struct clkctl_acpu_speed *speed = acpu_freq_tbl;
461 u8 pll2_l = readl_relaxed(PLL2_L_VAL_ADDR) & 0xFF;
462
463 for ( ; speed->acpu_clk_khz; speed++) {
464 if (speed->src != PLL_2)
465 backup_s = speed;
466 if (speed->pll_rate && speed->pll_rate->l == pll2_l) {
467 speed++;
468 speed->acpu_clk_khz = 0;
469 return;
470 }
471 }
472
473 pr_err("Unknown PLL2 lval %d\n", pll2_l);
474 BUG();
475}
476
477#define RPM_BYPASS_MASK (1 << 3)
478#define PMIC_MODE_MASK (1 << 4)
479
480static void __init populate_plls(void)
481{
482 acpuclk_sources[PLL_1] = clk_get_sys("acpu", "pll1_clk");
483 BUG_ON(IS_ERR(acpuclk_sources[PLL_1]));
484 acpuclk_sources[PLL_2] = clk_get_sys("acpu", "pll2_clk");
485 BUG_ON(IS_ERR(acpuclk_sources[PLL_2]));
486 acpuclk_sources[PLL_3] = clk_get_sys("acpu", "pll3_clk");
487 BUG_ON(IS_ERR(acpuclk_sources[PLL_3]));
488}
489
490void __init msm_acpu_clock_init(struct msm_acpu_clock_platform_data *clkdata)
491{
492 pr_info("acpu_clock_init()\n");
493
494 mutex_init(&drv_state.lock);
495 drv_state.acpu_switch_time_us = clkdata->acpu_switch_time_us;
496 drv_state.vdd_switch_time_us = clkdata->vdd_switch_time_us;
497 pll2_fixup();
498 populate_plls();
499 acpuclk_init();
500 lpj_init();
501 setup_cpufreq_table();
502}