1 /* arch/arm/mach-msm/acpuclock.c
3 * MSM architecture clock driver
5 * Copyright (C) 2007 Google, Inc.
6 * Copyright (c) 2007 QUALCOMM Incorporated
7 * Author: San Mehat <san@android.com>
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include <linux/version.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/errno.h>
25 #include <linux/string.h>
26 #include <linux/delay.h>
27 #include <linux/clk.h>
28 #include <linux/cpufreq.h>
29 #include <linux/mutex.h>
31 #include <mach/board.h>
32 #include <mach/msm_iomap.h>
34 #include "proc_comm.h"
35 #include "acpuclock.h"
38 #define A11S_CLK_CNTL_ADDR (MSM_CSR_BASE + 0x100)
39 #define A11S_CLK_SEL_ADDR (MSM_CSR_BASE + 0x104)
40 #define A11S_VDD_SVS_PLEVEL_ADDR (MSM_CSR_BASE + 0x124)
43 * ARM11 clock configuration for specific ACPU speeds
46 #define ACPU_PLL_TCXO -1
52 #define PERF_SWITCH_DEBUG 0
53 #define PERF_SWITCH_STEP_DEBUG 0
57 struct clkctl_acpu_speed *current_speed;
59 uint32_t acpu_switch_time_us;
60 uint32_t max_speed_delta_khz;
61 uint32_t vdd_switch_time_us;
62 unsigned long power_collapse_khz;
63 unsigned long wait_for_irq_khz;
66 static struct clk *ebi1_clk;
67 static struct clock_state drv_state = { 0 };
69 static void __init acpuclk_init(void);
71 /* MSM7201A Levels 3-6 all correspond to 1.2V, level 7 corresponds to 1.325V. */
84 struct clkctl_acpu_speed {
85 unsigned int a11clk_khz;
87 unsigned int a11clk_src_sel;
88 unsigned int a11clk_src_div;
89 unsigned int ahbclk_khz;
90 unsigned int ahbclk_div;
92 unsigned int axiclk_khz;
93 unsigned long lpj; /* loops_per_jiffy */
94 /* Index in acpu_freq_tbl[] for steppings. */
100 * ACPU speed table. Complete table is shown but certain speeds are commented
101 * out to optimized speed switching. Initalize loops_per_jiffy to 0.
103 * Table stepping up/down is optimized for 256mhz jumps while staying on the
107 static struct clkctl_acpu_speed acpu_freq_tbl[] = {
108 { 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, VDD_0, 30720, 0, 0, 8 },
109 { 61440, ACPU_PLL_0, 4, 3, 61440, 0, VDD_0, 30720, 0, 0, 8 },
110 { 81920, ACPU_PLL_0, 4, 2, 40960, 1, VDD_0, 61440, 0, 0, 8 },
111 { 96000, ACPU_PLL_1, 1, 7, 48000, 1, VDD_0, 61440, 0, 0, 9 },
112 { 122880, ACPU_PLL_0, 4, 1, 61440, 1, VDD_3, 61440, 0, 0, 8 },
113 { 128000, ACPU_PLL_1, 1, 5, 64000, 1, VDD_3, 61440, 0, 0, 12 },
114 { 176000, ACPU_PLL_2, 2, 5, 88000, 1, VDD_3, 61440, 0, 0, 11 },
115 { 192000, ACPU_PLL_1, 1, 3, 64000, 2, VDD_3, 61440, 0, 0, 12 },
116 { 245760, ACPU_PLL_0, 4, 0, 81920, 2, VDD_4, 61440, 0, 0, 12 },
117 { 256000, ACPU_PLL_1, 1, 2, 128000, 2, VDD_5, 128000, 0, 0, 12 },
118 { 264000, ACPU_PLL_2, 2, 3, 88000, 2, VDD_5, 128000, 0, 6, 13 },
119 { 352000, ACPU_PLL_2, 2, 2, 88000, 3, VDD_5, 128000, 0, 6, 13 },
120 { 384000, ACPU_PLL_1, 1, 1, 128000, 2, VDD_6, 128000, 0, 5, -1 },
121 { 528000, ACPU_PLL_2, 2, 1, 132000, 3, VDD_7, 128000, 0, 11, -1 },
122 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
124 #else /* Table of freq we currently use. */
125 static struct clkctl_acpu_speed acpu_freq_tbl[] = {
126 { 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, VDD_0, 30720, 0, 0, 4 },
127 { 122880, ACPU_PLL_0, 4, 1, 61440, 1, VDD_3, 61440, 0, 0, 4 },
128 { 128000, ACPU_PLL_1, 1, 5, 64000, 1, VDD_3, 61440, 0, 0, 6 },
129 { 176000, ACPU_PLL_2, 2, 5, 88000, 1, VDD_3, 61440, 0, 0, 5 },
130 { 245760, ACPU_PLL_0, 4, 0, 81920, 2, VDD_4, 61440, 0, 0, 5 },
131 { 352000, ACPU_PLL_2, 2, 2, 88000, 3, VDD_5, 128000, 0, 3, 7 },
132 { 384000, ACPU_PLL_1, 1, 1, 128000, 2, VDD_6, 128000, 0, 2, -1 },
133 { 528000, ACPU_PLL_2, 2, 1, 132000, 3, VDD_7, 128000, 0, 5, -1 },
134 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
138 static struct cpufreq_frequency_table freq_table[] = {
144 { 5, CPUFREQ_TABLE_END },
147 static int pc_pll_request(unsigned id, unsigned on)
152 #if PERF_SWITCH_DEBUG
154 printk(KERN_DEBUG "Enabling PLL %d\n", id);
156 printk(KERN_DEBUG "Disabling PLL %d\n", id);
159 res = msm_proc_comm(PCOM_CLKCTL_RPC_PLL_REQUEST, &id, &on);
163 #if PERF_SWITCH_DEBUG
165 printk(KERN_DEBUG "PLL %d enabled\n", id);
167 printk(KERN_DEBUG "PLL %d disabled\n", id);
173 /*----------------------------------------------------------------------------
174 * ARM11 'owned' clock control
175 *---------------------------------------------------------------------------*/
177 unsigned long acpuclk_power_collapse(void) {
178 int ret = acpuclk_get_rate();
180 if (ret > drv_state.power_collapse_khz)
181 acpuclk_set_rate(drv_state.power_collapse_khz, 1);
185 unsigned long acpuclk_get_wfi_rate(void)
187 return drv_state.wait_for_irq_khz;
190 unsigned long acpuclk_wait_for_irq(void) {
191 int ret = acpuclk_get_rate();
193 if (ret > drv_state.wait_for_irq_khz)
194 acpuclk_set_rate(drv_state.wait_for_irq_khz, 1);
198 static int acpuclk_set_vdd_level(int vdd)
200 uint32_t current_vdd;
202 current_vdd = readl(A11S_VDD_SVS_PLEVEL_ADDR) & 0x07;
204 #if PERF_SWITCH_DEBUG
205 printk(KERN_DEBUG "acpuclock: Switching VDD from %u -> %d\n",
208 writel((1 << 7) | (vdd << 3), A11S_VDD_SVS_PLEVEL_ADDR);
209 udelay(drv_state.vdd_switch_time_us);
210 if ((readl(A11S_VDD_SVS_PLEVEL_ADDR) & 0x7) != vdd) {
211 #if PERF_SWITCH_DEBUG
212 printk(KERN_ERR "acpuclock: VDD set failed\n");
217 #if PERF_SWITCH_DEBUG
218 printk(KERN_DEBUG "acpuclock: VDD switched\n");
223 /* Set proper dividers for the given clock speed. */
224 static void acpuclk_set_div(const struct clkctl_acpu_speed *hunt_s) {
225 uint32_t reg_clkctl, reg_clksel, clk_div;
228 clk_div = (readl(A11S_CLK_SEL_ADDR) >> 1) & 0x03;
230 * If the new clock divider is higher than the previous, then
231 * program the divider before switching the clock
233 if (hunt_s->ahbclk_div > clk_div) {
234 reg_clksel = readl(A11S_CLK_SEL_ADDR);
235 reg_clksel &= ~(0x3 << 1);
236 reg_clksel |= (hunt_s->ahbclk_div << 1);
237 writel(reg_clksel, A11S_CLK_SEL_ADDR);
239 if ((readl(A11S_CLK_SEL_ADDR) & 0x01) == 0) {
242 /* Program clock source */
243 reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
244 reg_clkctl &= ~(0x07 << 4);
245 reg_clkctl |= (hunt_s->a11clk_src_sel << 4);
246 writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
248 /* Program clock divider */
249 reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
251 reg_clkctl |= hunt_s->a11clk_src_div;
252 writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
254 /* Program clock source selection */
255 reg_clksel = readl(A11S_CLK_SEL_ADDR);
256 reg_clksel |= 1; /* CLK_SEL_SRC1NO == SRC1 */
257 writel(reg_clksel, A11S_CLK_SEL_ADDR);
261 /* Program clock source */
262 reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
263 reg_clkctl &= ~(0x07 << 12);
264 reg_clkctl |= (hunt_s->a11clk_src_sel << 12);
265 writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
267 /* Program clock divider */
268 reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
269 reg_clkctl &= ~(0xf << 8);
270 reg_clkctl |= (hunt_s->a11clk_src_div << 8);
271 writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
273 /* Program clock source selection */
274 reg_clksel = readl(A11S_CLK_SEL_ADDR);
275 reg_clksel &= ~1; /* CLK_SEL_SRC1NO == SRC0 */
276 writel(reg_clksel, A11S_CLK_SEL_ADDR);
280 * If the new clock divider is lower than the previous, then
281 * program the divider after switching the clock
283 if (hunt_s->ahbclk_div < clk_div) {
284 reg_clksel = readl(A11S_CLK_SEL_ADDR);
285 reg_clksel &= ~(0x3 << 1);
286 reg_clksel |= (hunt_s->ahbclk_div << 1);
287 writel(reg_clksel, A11S_CLK_SEL_ADDR);
291 int acpuclk_set_rate(unsigned long rate, int for_power_collapse)
294 struct clkctl_acpu_speed *cur_s, *tgt_s, *strt_s;
296 unsigned int plls_enabled = 0, pll;
298 strt_s = cur_s = drv_state.current_speed;
300 WARN_ONCE(cur_s == NULL, "acpuclk_set_rate: not initialized\n");
304 if (rate == (cur_s->a11clk_khz * 1000))
307 for (tgt_s = acpu_freq_tbl; tgt_s->a11clk_khz != 0; tgt_s++) {
308 if (tgt_s->a11clk_khz == (rate / 1000))
312 if (tgt_s->a11clk_khz == 0)
315 /* Choose the highest speed speed at or below 'rate' with same PLL. */
316 if (for_power_collapse && tgt_s->a11clk_khz < cur_s->a11clk_khz) {
317 while (tgt_s->pll != ACPU_PLL_TCXO && tgt_s->pll != cur_s->pll)
321 if (strt_s->pll != ACPU_PLL_TCXO)
322 plls_enabled |= 1 << strt_s->pll;
324 if (!for_power_collapse) {
325 mutex_lock(&drv_state.lock);
326 if (strt_s->pll != tgt_s->pll && tgt_s->pll != ACPU_PLL_TCXO) {
327 rc = pc_pll_request(tgt_s->pll, 1);
329 pr_err("PLL%d enable failed (%d)\n",
333 plls_enabled |= 1 << tgt_s->pll;
335 /* Increase VDD if needed. */
336 if (tgt_s->vdd > cur_s->vdd) {
337 if ((rc = acpuclk_set_vdd_level(tgt_s->vdd)) < 0) {
338 printk(KERN_ERR "Unable to switch ACPU vdd\n");
344 /* Set wait states for CPU inbetween frequency changes */
345 reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
346 reg_clkctl |= (100 << 16); /* set WT_ST_CNT */
347 writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
349 #if PERF_SWITCH_DEBUG
350 printk(KERN_INFO "acpuclock: Switching from ACPU rate %u -> %u\n",
351 strt_s->a11clk_khz * 1000, tgt_s->a11clk_khz * 1000);
354 while (cur_s != tgt_s) {
356 * Always jump to target freq if within 256mhz, regulardless of
357 * PLL. If differnece is greater, use the predefinied
358 * steppings in the table.
360 int d = abs((int)(cur_s->a11clk_khz - tgt_s->a11clk_khz));
361 if (d > drv_state.max_speed_delta_khz) {
362 /* Step up or down depending on target vs current. */
363 int clk_index = tgt_s->a11clk_khz > cur_s->a11clk_khz ?
364 cur_s->up : cur_s->down;
365 if (clk_index < 0) { /* This should not happen. */
366 printk(KERN_ERR "cur:%u target: %u\n",
367 cur_s->a11clk_khz, tgt_s->a11clk_khz);
371 cur_s = &acpu_freq_tbl[clk_index];
375 #if PERF_SWITCH_STEP_DEBUG
376 printk(KERN_DEBUG "%s: STEP khz = %u, pll = %d\n",
377 __FUNCTION__, cur_s->a11clk_khz, cur_s->pll);
379 if (!for_power_collapse&& cur_s->pll != ACPU_PLL_TCXO
380 && !(plls_enabled & (1 << cur_s->pll))) {
381 rc = pc_pll_request(cur_s->pll, 1);
383 pr_err("PLL%d enable failed (%d)\n",
387 plls_enabled |= 1 << cur_s->pll;
390 acpuclk_set_div(cur_s);
391 drv_state.current_speed = cur_s;
392 /* Re-adjust lpj for the new clock speed. */
393 loops_per_jiffy = cur_s->lpj;
394 udelay(drv_state.acpu_switch_time_us);
397 /* Nothing else to do for power collapse. */
398 if (for_power_collapse)
401 /* Disable PLLs we are not using anymore. */
402 plls_enabled &= ~(1 << tgt_s->pll);
403 for (pll = ACPU_PLL_0; pll <= ACPU_PLL_2; pll++)
404 if (plls_enabled & (1 << pll)) {
405 rc = pc_pll_request(pll, 0);
407 pr_err("PLL%d disable failed (%d)\n", pll, rc);
412 /* Change the AXI bus frequency if we can. */
413 if (strt_s->axiclk_khz != tgt_s->axiclk_khz) {
414 rc = clk_set_rate(ebi1_clk, tgt_s->axiclk_khz * 1000);
416 pr_err("Setting AXI min rate failed!\n");
419 /* Drop VDD level if we can. */
420 if (tgt_s->vdd < strt_s->vdd) {
421 if (acpuclk_set_vdd_level(tgt_s->vdd) < 0)
422 printk(KERN_ERR "acpuclock: Unable to drop ACPU vdd\n");
425 #if PERF_SWITCH_DEBUG
426 printk(KERN_DEBUG "%s: ACPU speed change complete\n", __FUNCTION__);
429 if (!for_power_collapse)
430 mutex_unlock(&drv_state.lock);
434 static void __init acpuclk_init(void)
436 struct clkctl_acpu_speed *speed;
441 * Determine the rate of ACPU clock
444 if (!(readl(A11S_CLK_SEL_ADDR) & 0x01)) { /* CLK_SEL_SRC1N0 */
446 sel = (readl(A11S_CLK_CNTL_ADDR) >> 12) & 0x7;
448 div = (readl(A11S_CLK_CNTL_ADDR) >> 8) & 0x0f;
451 sel = (readl(A11S_CLK_CNTL_ADDR) >> 4) & 0x07;
453 div = readl(A11S_CLK_CNTL_ADDR) & 0x0f;
456 for (speed = acpu_freq_tbl; speed->a11clk_khz != 0; speed++) {
457 if (speed->a11clk_src_sel == sel
458 && (speed->a11clk_src_div == div))
461 if (speed->a11clk_khz == 0) {
462 printk(KERN_WARNING "Warning - ACPU clock reports invalid speed\n");
466 drv_state.current_speed = speed;
468 rc = clk_set_rate(ebi1_clk, speed->axiclk_khz * 1000);
470 pr_err("Setting AXI min rate failed!\n");
472 printk(KERN_INFO "ACPU running at %d KHz\n", speed->a11clk_khz);
475 unsigned long acpuclk_get_rate(void)
477 WARN_ONCE(drv_state.current_speed == NULL,
478 "acpuclk_get_rate: not initialized\n");
479 if (drv_state.current_speed)
480 return drv_state.current_speed->a11clk_khz;
485 uint32_t acpuclk_get_switch_time(void)
487 return drv_state.acpu_switch_time_us;
490 /*----------------------------------------------------------------------------
491 * Clock driver initialization
492 *---------------------------------------------------------------------------*/
494 /* Initalize the lpj field in the acpu_freq_tbl. */
495 static void __init lpj_init(void)
498 const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
499 for (i = 0; acpu_freq_tbl[i].a11clk_khz; i++) {
500 acpu_freq_tbl[i].lpj = cpufreq_scale(loops_per_jiffy,
501 base_clk->a11clk_khz,
502 acpu_freq_tbl[i].a11clk_khz);
506 void __init msm_acpu_clock_init(struct msm_acpu_clock_platform_data *clkdata)
508 pr_info("acpu_clock_init()\n");
510 ebi1_clk = clk_get(NULL, "ebi1_clk");
512 mutex_init(&drv_state.lock);
513 drv_state.acpu_switch_time_us = clkdata->acpu_switch_time_us;
514 drv_state.max_speed_delta_khz = clkdata->max_speed_delta_khz;
515 drv_state.vdd_switch_time_us = clkdata->vdd_switch_time_us;
516 drv_state.power_collapse_khz = clkdata->power_collapse_khz;
517 drv_state.wait_for_irq_khz = clkdata->wait_for_irq_khz;
520 #ifdef CONFIG_CPU_FREQ_TABLE
521 cpufreq_frequency_table_get_attr(freq_table, smp_processor_id());