blob: 0da4322bd54fe8caea89eae5f39352ad5f77602f [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;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059 uint32_t vdd_switch_time_us;
60 struct clk *ebi1_clk;
61};
62
63struct pll {
64 unsigned int l;
65 unsigned int m;
66 unsigned int n;
67 unsigned int pre_div;
68};
69
70struct clkctl_acpu_speed {
71 unsigned int use_for_scaling;
72 unsigned int acpu_clk_khz;
73 int src;
74 unsigned int acpu_src_sel;
75 unsigned int acpu_src_div;
76 unsigned int axi_clk_hz;
77 unsigned int vdd_mv;
78 unsigned int vdd_raw;
79 struct pll *pll_rate;
80 unsigned long lpj; /* loops_per_jiffy */
81};
82
83static struct clock_state drv_state = { 0 };
84
85/* Switch to this when reprogramming PLL2 */
86static struct clkctl_acpu_speed *backup_s;
87
88static struct pll pll2_tbl[] = {
89 { 42, 0, 1, 0 }, /* 806 MHz */
90 { 53, 1, 3, 0 }, /* 1024 MHz */
91 { 125, 0, 1, 1 }, /* 1200 MHz */
92 { 73, 0, 1, 0 }, /* 1401 MHz */
93};
94
95/* Use negative numbers for sources that can't be enabled/disabled */
96
97enum acpuclk_source {
98 LPXO = -2,
99 AXI = -1,
100 PLL_0 = 0,
101 PLL_1,
102 PLL_2,
103 PLL_3,
104 MAX_SOURCE
105};
106
107static struct clk *acpuclk_sources[MAX_SOURCE];
108
109/*
110 * Each ACPU frequency has a certain minimum MSMC1 voltage requirement
111 * that is implicitly met by voting for a specific minimum AXI frequency.
112 * Do NOT change the AXI frequency unless you are _absoulutely_ sure you
113 * know all the h/w requirements.
114 */
115static struct clkctl_acpu_speed acpu_freq_tbl[] = {
116 { 0, 24576, LPXO, 0, 0, 30720000, 900, VDD_RAW(900) },
117 { 0, 61440, PLL_3, 5, 11, 61440000, 900, VDD_RAW(900) },
118 { 1, 122880, PLL_3, 5, 5, 61440000, 900, VDD_RAW(900) },
119 { 0, 184320, PLL_3, 5, 4, 61440000, 900, VDD_RAW(900) },
120 { 0, MAX_AXI_KHZ, AXI, 1, 0, 61440000, 900, VDD_RAW(900) },
121 { 1, 245760, PLL_3, 5, 2, 61440000, 900, VDD_RAW(900) },
122 { 1, 368640, PLL_3, 5, 1, 122800000, 900, VDD_RAW(900) },
123 /* AXI has MSMC1 implications. See above. */
124 { 1, 768000, PLL_1, 2, 0, 153600000, 1050, VDD_RAW(1050) },
125 /*
126 * AXI has MSMC1 implications. See above.
127 */
128 { 1, 806400, PLL_2, 3, 0, UINT_MAX, 1100, VDD_RAW(1100), &pll2_tbl[0]},
129 { 1, 1024000, PLL_2, 3, 0, UINT_MAX, 1200, VDD_RAW(1200), &pll2_tbl[1]},
130 { 1, 1200000, PLL_2, 3, 0, UINT_MAX, 1200, VDD_RAW(1200), &pll2_tbl[2]},
131 { 1, 1401600, PLL_2, 3, 0, UINT_MAX, 1250, VDD_RAW(1250), &pll2_tbl[3]},
132 { 0 }
133};
134
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135static int acpuclk_set_acpu_vdd(struct clkctl_acpu_speed *s)
136{
137 int ret = msm_spm_set_vdd(0, s->vdd_raw);
138 if (ret)
139 return ret;
140
141 /* Wait for voltage to stabilize. */
142 udelay(drv_state.vdd_switch_time_us);
143 return 0;
144}
145
146/* Assumes PLL2 is off and the acpuclock isn't sourced from PLL2 */
147static void acpuclk_config_pll2(struct pll *pll)
148{
149 uint32_t config = readl_relaxed(PLL2_CONFIG_ADDR);
150
151 /* Make sure write to disable PLL_2 has completed
152 * before reconfiguring that PLL. */
153 mb();
154 writel_relaxed(pll->l, PLL2_L_VAL_ADDR);
155 writel_relaxed(pll->m, PLL2_M_VAL_ADDR);
156 writel_relaxed(pll->n, PLL2_N_VAL_ADDR);
157 if (pll->pre_div)
158 config |= BIT(15);
159 else
160 config &= ~BIT(15);
161 writel_relaxed(config, PLL2_CONFIG_ADDR);
162 /* Make sure PLL is programmed before returning. */
163 mb();
164}
165
166/* Set clock source and divider given a clock speed */
167static void acpuclk_set_src(const struct clkctl_acpu_speed *s)
168{
169 uint32_t reg_clksel, reg_clkctl, src_sel;
170
171 reg_clksel = readl_relaxed(SCSS_CLK_SEL_ADDR);
172
173 /* CLK_SEL_SRC1NO */
174 src_sel = reg_clksel & 1;
175
176 /* Program clock source and divider. */
177 reg_clkctl = readl_relaxed(SCSS_CLK_CTL_ADDR);
178 reg_clkctl &= ~(0xFF << (8 * src_sel));
179 reg_clkctl |= s->acpu_src_sel << (4 + 8 * src_sel);
180 reg_clkctl |= s->acpu_src_div << (0 + 8 * src_sel);
181 writel_relaxed(reg_clkctl, SCSS_CLK_CTL_ADDR);
182
183 /* Toggle clock source. */
184 reg_clksel ^= 1;
185
186 /* Program clock source selection. */
187 writel_relaxed(reg_clksel, SCSS_CLK_SEL_ADDR);
188
189 /* Make sure switch to new source is complete. */
190 mb();
191}
192
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700193static int acpuclk_7x30_set_rate(int cpu, unsigned long rate,
194 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195{
196 struct clkctl_acpu_speed *tgt_s, *strt_s;
197 int res, rc = 0;
198
199 if (reason == SETRATE_CPUFREQ)
200 mutex_lock(&drv_state.lock);
201
202 strt_s = drv_state.current_speed;
203
204 if (rate == strt_s->acpu_clk_khz)
205 goto out;
206
207 for (tgt_s = acpu_freq_tbl; tgt_s->acpu_clk_khz != 0; tgt_s++) {
208 if (tgt_s->acpu_clk_khz == rate)
209 break;
210 }
211 if (tgt_s->acpu_clk_khz == 0) {
212 rc = -EINVAL;
213 goto out;
214 }
215
216 if (reason == SETRATE_CPUFREQ) {
217 /* Increase VDD if needed. */
218 if (tgt_s->vdd_mv > strt_s->vdd_mv) {
219 rc = acpuclk_set_acpu_vdd(tgt_s);
220 if (rc < 0) {
221 pr_err("ACPU VDD increase to %d mV failed "
222 "(%d)\n", tgt_s->vdd_mv, rc);
223 goto out;
224 }
225 }
226 }
227
228 pr_debug("Switching from ACPU rate %u KHz -> %u KHz\n",
229 strt_s->acpu_clk_khz, tgt_s->acpu_clk_khz);
230
231 /* Increase the AXI bus frequency if needed. This must be done before
232 * increasing the ACPU frequency, since voting for high AXI rates
233 * implicitly takes care of increasing the MSMC1 voltage, as needed. */
234 if (tgt_s->axi_clk_hz > strt_s->axi_clk_hz) {
235 rc = clk_set_min_rate(drv_state.ebi1_clk,
236 tgt_s->axi_clk_hz);
237 if (rc < 0) {
238 pr_err("Setting AXI min rate failed (%d)\n", rc);
239 goto out;
240 }
241 }
242
243 /* Move off of PLL2 if we're reprogramming it */
244 if (tgt_s->src == PLL_2 && strt_s->src == PLL_2) {
245 clk_enable(acpuclk_sources[backup_s->src]);
246 acpuclk_set_src(backup_s);
247 clk_disable(acpuclk_sources[strt_s->src]);
248 }
249
250 /* Reconfigure PLL2 if we're moving to it */
251 if (tgt_s->src == PLL_2)
252 acpuclk_config_pll2(tgt_s->pll_rate);
253
254 /* Make sure target PLL is on. */
255 if ((strt_s->src != tgt_s->src && tgt_s->src >= 0) ||
256 (tgt_s->src == PLL_2 && strt_s->src == PLL_2)) {
257 pr_debug("Enabling PLL %d\n", tgt_s->src);
258 clk_enable(acpuclk_sources[tgt_s->src]);
259 }
260
261 /* Perform the frequency switch */
262 acpuclk_set_src(tgt_s);
263 drv_state.current_speed = tgt_s;
264 loops_per_jiffy = tgt_s->lpj;
265
266 if (tgt_s->src == PLL_2 && strt_s->src == PLL_2)
267 clk_disable(acpuclk_sources[backup_s->src]);
268
269 /* Nothing else to do for SWFI. */
270 if (reason == SETRATE_SWFI)
271 goto out;
272
273 /* Turn off previous PLL if not used. */
274 if (strt_s->src != tgt_s->src && strt_s->src >= 0) {
275 pr_debug("Disabling PLL %d\n", strt_s->src);
276 clk_disable(acpuclk_sources[strt_s->src]);
277 }
278
279 /* Decrease the AXI bus frequency if we can. */
280 if (tgt_s->axi_clk_hz < strt_s->axi_clk_hz) {
281 res = clk_set_min_rate(drv_state.ebi1_clk,
282 tgt_s->axi_clk_hz);
283 if (res < 0)
284 pr_warning("Setting AXI min rate failed (%d)\n", res);
285 }
286
287 /* Nothing else to do for power collapse. */
288 if (reason == SETRATE_PC)
289 goto out;
290
291 /* Drop VDD level if we can. */
292 if (tgt_s->vdd_mv < strt_s->vdd_mv) {
293 res = acpuclk_set_acpu_vdd(tgt_s);
294 if (res)
295 pr_warning("ACPU VDD decrease to %d mV failed (%d)\n",
296 tgt_s->vdd_mv, res);
297 }
298
299 pr_debug("ACPU speed change complete\n");
300out:
301 if (reason == SETRATE_CPUFREQ)
302 mutex_unlock(&drv_state.lock);
303
304 return rc;
305}
306
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700307static unsigned long acpuclk_7x30_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308{
309 WARN_ONCE(drv_state.current_speed == NULL,
310 "acpuclk_get_rate: not initialized\n");
311 if (drv_state.current_speed)
312 return drv_state.current_speed->acpu_clk_khz;
313 else
314 return 0;
315}
316
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317/*----------------------------------------------------------------------------
318 * Clock driver initialization
319 *---------------------------------------------------------------------------*/
320
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700321static void __init acpuclk_hw_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700322{
323 struct clkctl_acpu_speed *s;
324 uint32_t div, sel, src_num;
325 uint32_t reg_clksel, reg_clkctl;
326 int res;
327 u8 pll2_l = readl_relaxed(PLL2_L_VAL_ADDR) & 0xFF;
328
329 drv_state.ebi1_clk = clk_get(NULL, "ebi1_clk");
330 BUG_ON(IS_ERR(drv_state.ebi1_clk));
331
332 reg_clksel = readl_relaxed(SCSS_CLK_SEL_ADDR);
333
334 /* Determine the ACPU clock rate. */
335 switch ((reg_clksel >> 1) & 0x3) {
336 case 0: /* Running off the output of the raw clock source mux. */
337 reg_clkctl = readl_relaxed(SCSS_CLK_CTL_ADDR);
338 src_num = reg_clksel & 0x1;
339 sel = (reg_clkctl >> (12 - (8 * src_num))) & 0x7;
340 div = (reg_clkctl >> (8 - (8 * src_num))) & 0xF;
341
342 /* Check frequency table for matching sel/div pair. */
343 for (s = acpu_freq_tbl; s->acpu_clk_khz != 0; s++) {
344 if (s->acpu_src_sel == sel && s->acpu_src_div == div)
345 break;
346 }
347 if (s->acpu_clk_khz == 0) {
348 pr_err("Error - ACPU clock reports invalid speed\n");
349 return;
350 }
351 break;
352 case 2: /* Running off of the SCPLL selected through the core mux. */
353 /* Switch to run off of the SCPLL selected through the raw
354 * clock source mux. */
355 for (s = acpu_freq_tbl; s->acpu_clk_khz != 0
356 && s->src != PLL_2 && s->acpu_src_div == 0; s++)
357 ;
358 if (s->acpu_clk_khz != 0) {
359 /* Program raw clock source mux. */
360 acpuclk_set_src(s);
361
362 /* Switch to raw clock source input of the core mux. */
363 reg_clksel = readl_relaxed(SCSS_CLK_SEL_ADDR);
364 reg_clksel &= ~(0x3 << 1);
365 writel_relaxed(reg_clksel, SCSS_CLK_SEL_ADDR);
366 break;
367 }
368 /* else fall through */
369 default:
370 pr_err("Error - ACPU clock reports invalid source\n");
371 return;
372 }
373
374 /* Look at PLL2's L val to determine what speed PLL2 is running at */
375 if (s->src == PLL_2)
376 for ( ; s->acpu_clk_khz; s++)
377 if (s->pll_rate && s->pll_rate->l == pll2_l)
378 break;
379
380 /* Set initial ACPU VDD. */
381 acpuclk_set_acpu_vdd(s);
382
383 drv_state.current_speed = s;
384
385 /* Initialize current PLL's reference count. */
386 if (s->src >= 0)
387 clk_enable(acpuclk_sources[s->src]);
388
389 res = clk_set_min_rate(drv_state.ebi1_clk, s->axi_clk_hz);
390 if (res < 0)
391 pr_warning("Setting AXI min rate failed!\n");
392
393 pr_info("ACPU running at %d KHz\n", s->acpu_clk_khz);
394
395 return;
396}
397
398/* Initalize the lpj field in the acpu_freq_tbl. */
399static void __init lpj_init(void)
400{
401 int i;
402 const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
403
404 for (i = 0; acpu_freq_tbl[i].acpu_clk_khz; i++) {
405 acpu_freq_tbl[i].lpj = cpufreq_scale(loops_per_jiffy,
406 base_clk->acpu_clk_khz,
407 acpu_freq_tbl[i].acpu_clk_khz);
408 }
409}
410
411#ifdef CONFIG_CPU_FREQ_MSM
412static struct cpufreq_frequency_table cpufreq_tbl[ARRAY_SIZE(acpu_freq_tbl)];
413
414static void setup_cpufreq_table(void)
415{
416 unsigned i = 0;
417 const struct clkctl_acpu_speed *speed;
418
419 for (speed = acpu_freq_tbl; speed->acpu_clk_khz; speed++)
420 if (speed->use_for_scaling) {
421 cpufreq_tbl[i].index = i;
422 cpufreq_tbl[i].frequency = speed->acpu_clk_khz;
423 i++;
424 }
425 cpufreq_tbl[i].frequency = CPUFREQ_TABLE_END;
426
427 cpufreq_frequency_table_get_attr(cpufreq_tbl, smp_processor_id());
428}
429#else
430static inline void setup_cpufreq_table(void) { }
431#endif
432
433/*
434 * Truncate the frequency table at the current PLL2 rate and determine the
435 * backup PLL to use when scaling PLL2.
436 */
437void __init pll2_fixup(void)
438{
439 struct clkctl_acpu_speed *speed = acpu_freq_tbl;
440 u8 pll2_l = readl_relaxed(PLL2_L_VAL_ADDR) & 0xFF;
441
442 for ( ; speed->acpu_clk_khz; speed++) {
443 if (speed->src != PLL_2)
444 backup_s = speed;
445 if (speed->pll_rate && speed->pll_rate->l == pll2_l) {
446 speed++;
447 speed->acpu_clk_khz = 0;
448 return;
449 }
450 }
451
452 pr_err("Unknown PLL2 lval %d\n", pll2_l);
453 BUG();
454}
455
456#define RPM_BYPASS_MASK (1 << 3)
457#define PMIC_MODE_MASK (1 << 4)
458
459static void __init populate_plls(void)
460{
461 acpuclk_sources[PLL_1] = clk_get_sys("acpu", "pll1_clk");
462 BUG_ON(IS_ERR(acpuclk_sources[PLL_1]));
463 acpuclk_sources[PLL_2] = clk_get_sys("acpu", "pll2_clk");
464 BUG_ON(IS_ERR(acpuclk_sources[PLL_2]));
465 acpuclk_sources[PLL_3] = clk_get_sys("acpu", "pll3_clk");
466 BUG_ON(IS_ERR(acpuclk_sources[PLL_3]));
467}
468
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700469static struct acpuclk_data acpuclk_7x30_data = {
470 .set_rate = acpuclk_7x30_set_rate,
471 .get_rate = acpuclk_7x30_get_rate,
472 .power_collapse_khz = MAX_AXI_KHZ,
473 .wait_for_irq_khz = MAX_AXI_KHZ,
474};
475
476int __init acpuclk_7x30_init(struct acpuclk_platform_data *clkdata)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477{
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700478 pr_info("%s()\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479
480 mutex_init(&drv_state.lock);
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700481 acpuclk_7x30_data.switch_time_us = clkdata->acpu_switch_time_us;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700482 drv_state.vdd_switch_time_us = clkdata->vdd_switch_time_us;
483 pll2_fixup();
484 populate_plls();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700485 acpuclk_hw_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486 lpj_init();
487 setup_cpufreq_table();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700488 acpuclk_register(&acpuclk_7x30_data);
489
490 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491}