Path: blob/master/arch/arm/mach-msm/acpuclock-arm11.c
10817 views
/* arch/arm/mach-msm/acpuclock.c1*2* MSM architecture clock driver3*4* Copyright (C) 2007 Google, Inc.5* Copyright (c) 2007 QUALCOMM Incorporated6* Author: San Mehat <[email protected]>7*8* This software is licensed under the terms of the GNU General Public9* License version 2, as published by the Free Software Foundation, and10* may be copied, distributed, and modified under those terms.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17*/1819#include <linux/kernel.h>20#include <linux/init.h>21#include <linux/list.h>22#include <linux/errno.h>23#include <linux/string.h>24#include <linux/delay.h>25#include <linux/clk.h>26#include <linux/cpufreq.h>27#include <linux/mutex.h>28#include <linux/io.h>29#include <mach/board.h>30#include <mach/msm_iomap.h>3132#include "proc_comm.h"33#include "acpuclock.h"343536#define A11S_CLK_CNTL_ADDR (MSM_CSR_BASE + 0x100)37#define A11S_CLK_SEL_ADDR (MSM_CSR_BASE + 0x104)38#define A11S_VDD_SVS_PLEVEL_ADDR (MSM_CSR_BASE + 0x124)3940/*41* ARM11 clock configuration for specific ACPU speeds42*/4344#define ACPU_PLL_TCXO -145#define ACPU_PLL_0 046#define ACPU_PLL_1 147#define ACPU_PLL_2 248#define ACPU_PLL_3 34950#define PERF_SWITCH_DEBUG 051#define PERF_SWITCH_STEP_DEBUG 05253struct clock_state54{55struct clkctl_acpu_speed *current_speed;56struct mutex lock;57uint32_t acpu_switch_time_us;58uint32_t max_speed_delta_khz;59uint32_t vdd_switch_time_us;60unsigned long power_collapse_khz;61unsigned long wait_for_irq_khz;62};6364static struct clk *ebi1_clk;65static struct clock_state drv_state = { 0 };6667static void __init acpuclk_init(void);6869/* MSM7201A Levels 3-6 all correspond to 1.2V, level 7 corresponds to 1.325V. */70enum {71VDD_0 = 0,72VDD_1 = 1,73VDD_2 = 2,74VDD_3 = 3,75VDD_4 = 3,76VDD_5 = 3,77VDD_6 = 3,78VDD_7 = 7,79VDD_END80};8182struct clkctl_acpu_speed {83unsigned int a11clk_khz;84int pll;85unsigned int a11clk_src_sel;86unsigned int a11clk_src_div;87unsigned int ahbclk_khz;88unsigned int ahbclk_div;89int vdd;90unsigned int axiclk_khz;91unsigned long lpj; /* loops_per_jiffy */92/* Index in acpu_freq_tbl[] for steppings. */93short down;94short up;95};9697/*98* ACPU speed table. Complete table is shown but certain speeds are commented99* out to optimized speed switching. Initialize loops_per_jiffy to 0.100*101* Table stepping up/down is optimized for 256mhz jumps while staying on the102* same PLL.103*/104#if (0)105static struct clkctl_acpu_speed acpu_freq_tbl[] = {106{ 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, VDD_0, 30720, 0, 0, 8 },107{ 61440, ACPU_PLL_0, 4, 3, 61440, 0, VDD_0, 30720, 0, 0, 8 },108{ 81920, ACPU_PLL_0, 4, 2, 40960, 1, VDD_0, 61440, 0, 0, 8 },109{ 96000, ACPU_PLL_1, 1, 7, 48000, 1, VDD_0, 61440, 0, 0, 9 },110{ 122880, ACPU_PLL_0, 4, 1, 61440, 1, VDD_3, 61440, 0, 0, 8 },111{ 128000, ACPU_PLL_1, 1, 5, 64000, 1, VDD_3, 61440, 0, 0, 12 },112{ 176000, ACPU_PLL_2, 2, 5, 88000, 1, VDD_3, 61440, 0, 0, 11 },113{ 192000, ACPU_PLL_1, 1, 3, 64000, 2, VDD_3, 61440, 0, 0, 12 },114{ 245760, ACPU_PLL_0, 4, 0, 81920, 2, VDD_4, 61440, 0, 0, 12 },115{ 256000, ACPU_PLL_1, 1, 2, 128000, 2, VDD_5, 128000, 0, 0, 12 },116{ 264000, ACPU_PLL_2, 2, 3, 88000, 2, VDD_5, 128000, 0, 6, 13 },117{ 352000, ACPU_PLL_2, 2, 2, 88000, 3, VDD_5, 128000, 0, 6, 13 },118{ 384000, ACPU_PLL_1, 1, 1, 128000, 2, VDD_6, 128000, 0, 5, -1 },119{ 528000, ACPU_PLL_2, 2, 1, 132000, 3, VDD_7, 128000, 0, 11, -1 },120{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },121};122#else /* Table of freq we currently use. */123static struct clkctl_acpu_speed acpu_freq_tbl[] = {124{ 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, VDD_0, 30720, 0, 0, 4 },125{ 122880, ACPU_PLL_0, 4, 1, 61440, 1, VDD_3, 61440, 0, 0, 4 },126{ 128000, ACPU_PLL_1, 1, 5, 64000, 1, VDD_3, 61440, 0, 0, 6 },127{ 176000, ACPU_PLL_2, 2, 5, 88000, 1, VDD_3, 61440, 0, 0, 5 },128{ 245760, ACPU_PLL_0, 4, 0, 81920, 2, VDD_4, 61440, 0, 0, 5 },129{ 352000, ACPU_PLL_2, 2, 2, 88000, 3, VDD_5, 128000, 0, 3, 7 },130{ 384000, ACPU_PLL_1, 1, 1, 128000, 2, VDD_6, 128000, 0, 2, -1 },131{ 528000, ACPU_PLL_2, 2, 1, 132000, 3, VDD_7, 128000, 0, 5, -1 },132{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },133};134#endif135136137#ifdef CONFIG_CPU_FREQ_TABLE138static struct cpufreq_frequency_table freq_table[] = {139{ 0, 122880 },140{ 1, 128000 },141{ 2, 245760 },142{ 3, 384000 },143{ 4, 528000 },144{ 5, CPUFREQ_TABLE_END },145};146#endif147148static int pc_pll_request(unsigned id, unsigned on)149{150int res;151on = !!on;152153#if PERF_SWITCH_DEBUG154if (on)155printk(KERN_DEBUG "Enabling PLL %d\n", id);156else157printk(KERN_DEBUG "Disabling PLL %d\n", id);158#endif159160res = msm_proc_comm(PCOM_CLKCTL_RPC_PLL_REQUEST, &id, &on);161if (res < 0)162return res;163164#if PERF_SWITCH_DEBUG165if (on)166printk(KERN_DEBUG "PLL %d enabled\n", id);167else168printk(KERN_DEBUG "PLL %d disabled\n", id);169#endif170return res;171}172173174/*----------------------------------------------------------------------------175* ARM11 'owned' clock control176*---------------------------------------------------------------------------*/177178unsigned long acpuclk_power_collapse(void) {179int ret = acpuclk_get_rate();180ret *= 1000;181if (ret > drv_state.power_collapse_khz)182acpuclk_set_rate(drv_state.power_collapse_khz, 1);183return ret;184}185186unsigned long acpuclk_get_wfi_rate(void)187{188return drv_state.wait_for_irq_khz;189}190191unsigned long acpuclk_wait_for_irq(void) {192int ret = acpuclk_get_rate();193ret *= 1000;194if (ret > drv_state.wait_for_irq_khz)195acpuclk_set_rate(drv_state.wait_for_irq_khz, 1);196return ret;197}198199static int acpuclk_set_vdd_level(int vdd)200{201uint32_t current_vdd;202203current_vdd = readl(A11S_VDD_SVS_PLEVEL_ADDR) & 0x07;204205#if PERF_SWITCH_DEBUG206printk(KERN_DEBUG "acpuclock: Switching VDD from %u -> %d\n",207current_vdd, vdd);208#endif209writel((1 << 7) | (vdd << 3), A11S_VDD_SVS_PLEVEL_ADDR);210udelay(drv_state.vdd_switch_time_us);211if ((readl(A11S_VDD_SVS_PLEVEL_ADDR) & 0x7) != vdd) {212#if PERF_SWITCH_DEBUG213printk(KERN_ERR "acpuclock: VDD set failed\n");214#endif215return -EIO;216}217218#if PERF_SWITCH_DEBUG219printk(KERN_DEBUG "acpuclock: VDD switched\n");220#endif221return 0;222}223224/* Set proper dividers for the given clock speed. */225static void acpuclk_set_div(const struct clkctl_acpu_speed *hunt_s) {226uint32_t reg_clkctl, reg_clksel, clk_div;227228/* AHB_CLK_DIV */229clk_div = (readl(A11S_CLK_SEL_ADDR) >> 1) & 0x03;230/*231* If the new clock divider is higher than the previous, then232* program the divider before switching the clock233*/234if (hunt_s->ahbclk_div > clk_div) {235reg_clksel = readl(A11S_CLK_SEL_ADDR);236reg_clksel &= ~(0x3 << 1);237reg_clksel |= (hunt_s->ahbclk_div << 1);238writel(reg_clksel, A11S_CLK_SEL_ADDR);239}240if ((readl(A11S_CLK_SEL_ADDR) & 0x01) == 0) {241/* SRC0 */242243/* Program clock source */244reg_clkctl = readl(A11S_CLK_CNTL_ADDR);245reg_clkctl &= ~(0x07 << 4);246reg_clkctl |= (hunt_s->a11clk_src_sel << 4);247writel(reg_clkctl, A11S_CLK_CNTL_ADDR);248249/* Program clock divider */250reg_clkctl = readl(A11S_CLK_CNTL_ADDR);251reg_clkctl &= ~0xf;252reg_clkctl |= hunt_s->a11clk_src_div;253writel(reg_clkctl, A11S_CLK_CNTL_ADDR);254255/* Program clock source selection */256reg_clksel = readl(A11S_CLK_SEL_ADDR);257reg_clksel |= 1; /* CLK_SEL_SRC1NO == SRC1 */258writel(reg_clksel, A11S_CLK_SEL_ADDR);259} else {260/* SRC1 */261262/* Program clock source */263reg_clkctl = readl(A11S_CLK_CNTL_ADDR);264reg_clkctl &= ~(0x07 << 12);265reg_clkctl |= (hunt_s->a11clk_src_sel << 12);266writel(reg_clkctl, A11S_CLK_CNTL_ADDR);267268/* Program clock divider */269reg_clkctl = readl(A11S_CLK_CNTL_ADDR);270reg_clkctl &= ~(0xf << 8);271reg_clkctl |= (hunt_s->a11clk_src_div << 8);272writel(reg_clkctl, A11S_CLK_CNTL_ADDR);273274/* Program clock source selection */275reg_clksel = readl(A11S_CLK_SEL_ADDR);276reg_clksel &= ~1; /* CLK_SEL_SRC1NO == SRC0 */277writel(reg_clksel, A11S_CLK_SEL_ADDR);278}279280/*281* If the new clock divider is lower than the previous, then282* program the divider after switching the clock283*/284if (hunt_s->ahbclk_div < clk_div) {285reg_clksel = readl(A11S_CLK_SEL_ADDR);286reg_clksel &= ~(0x3 << 1);287reg_clksel |= (hunt_s->ahbclk_div << 1);288writel(reg_clksel, A11S_CLK_SEL_ADDR);289}290}291292int acpuclk_set_rate(unsigned long rate, int for_power_collapse)293{294uint32_t reg_clkctl;295struct clkctl_acpu_speed *cur_s, *tgt_s, *strt_s;296int rc = 0;297unsigned int plls_enabled = 0, pll;298299strt_s = cur_s = drv_state.current_speed;300301WARN_ONCE(cur_s == NULL, "acpuclk_set_rate: not initialized\n");302if (cur_s == NULL)303return -ENOENT;304305if (rate == (cur_s->a11clk_khz * 1000))306return 0;307308for (tgt_s = acpu_freq_tbl; tgt_s->a11clk_khz != 0; tgt_s++) {309if (tgt_s->a11clk_khz == (rate / 1000))310break;311}312313if (tgt_s->a11clk_khz == 0)314return -EINVAL;315316/* Choose the highest speed speed at or below 'rate' with same PLL. */317if (for_power_collapse && tgt_s->a11clk_khz < cur_s->a11clk_khz) {318while (tgt_s->pll != ACPU_PLL_TCXO && tgt_s->pll != cur_s->pll)319tgt_s--;320}321322if (strt_s->pll != ACPU_PLL_TCXO)323plls_enabled |= 1 << strt_s->pll;324325if (!for_power_collapse) {326mutex_lock(&drv_state.lock);327if (strt_s->pll != tgt_s->pll && tgt_s->pll != ACPU_PLL_TCXO) {328rc = pc_pll_request(tgt_s->pll, 1);329if (rc < 0) {330pr_err("PLL%d enable failed (%d)\n",331tgt_s->pll, rc);332goto out;333}334plls_enabled |= 1 << tgt_s->pll;335}336/* Increase VDD if needed. */337if (tgt_s->vdd > cur_s->vdd) {338if ((rc = acpuclk_set_vdd_level(tgt_s->vdd)) < 0) {339printk(KERN_ERR "Unable to switch ACPU vdd\n");340goto out;341}342}343}344345/* Set wait states for CPU between frequency changes */346reg_clkctl = readl(A11S_CLK_CNTL_ADDR);347reg_clkctl |= (100 << 16); /* set WT_ST_CNT */348writel(reg_clkctl, A11S_CLK_CNTL_ADDR);349350#if PERF_SWITCH_DEBUG351printk(KERN_INFO "acpuclock: Switching from ACPU rate %u -> %u\n",352strt_s->a11clk_khz * 1000, tgt_s->a11clk_khz * 1000);353#endif354355while (cur_s != tgt_s) {356/*357* Always jump to target freq if within 256mhz, regulardless of358* PLL. If differnece is greater, use the predefinied359* steppings in the table.360*/361int d = abs((int)(cur_s->a11clk_khz - tgt_s->a11clk_khz));362if (d > drv_state.max_speed_delta_khz) {363/* Step up or down depending on target vs current. */364int clk_index = tgt_s->a11clk_khz > cur_s->a11clk_khz ?365cur_s->up : cur_s->down;366if (clk_index < 0) { /* This should not happen. */367printk(KERN_ERR "cur:%u target: %u\n",368cur_s->a11clk_khz, tgt_s->a11clk_khz);369rc = -EINVAL;370goto out;371}372cur_s = &acpu_freq_tbl[clk_index];373} else {374cur_s = tgt_s;375}376#if PERF_SWITCH_STEP_DEBUG377printk(KERN_DEBUG "%s: STEP khz = %u, pll = %d\n",378__FUNCTION__, cur_s->a11clk_khz, cur_s->pll);379#endif380if (!for_power_collapse&& cur_s->pll != ACPU_PLL_TCXO381&& !(plls_enabled & (1 << cur_s->pll))) {382rc = pc_pll_request(cur_s->pll, 1);383if (rc < 0) {384pr_err("PLL%d enable failed (%d)\n",385cur_s->pll, rc);386goto out;387}388plls_enabled |= 1 << cur_s->pll;389}390391acpuclk_set_div(cur_s);392drv_state.current_speed = cur_s;393/* Re-adjust lpj for the new clock speed. */394loops_per_jiffy = cur_s->lpj;395udelay(drv_state.acpu_switch_time_us);396}397398/* Nothing else to do for power collapse. */399if (for_power_collapse)400return 0;401402/* Disable PLLs we are not using anymore. */403plls_enabled &= ~(1 << tgt_s->pll);404for (pll = ACPU_PLL_0; pll <= ACPU_PLL_2; pll++)405if (plls_enabled & (1 << pll)) {406rc = pc_pll_request(pll, 0);407if (rc < 0) {408pr_err("PLL%d disable failed (%d)\n", pll, rc);409goto out;410}411}412413/* Change the AXI bus frequency if we can. */414if (strt_s->axiclk_khz != tgt_s->axiclk_khz) {415rc = clk_set_rate(ebi1_clk, tgt_s->axiclk_khz * 1000);416if (rc < 0)417pr_err("Setting AXI min rate failed!\n");418}419420/* Drop VDD level if we can. */421if (tgt_s->vdd < strt_s->vdd) {422if (acpuclk_set_vdd_level(tgt_s->vdd) < 0)423printk(KERN_ERR "acpuclock: Unable to drop ACPU vdd\n");424}425426#if PERF_SWITCH_DEBUG427printk(KERN_DEBUG "%s: ACPU speed change complete\n", __FUNCTION__);428#endif429out:430if (!for_power_collapse)431mutex_unlock(&drv_state.lock);432return rc;433}434435static void __init acpuclk_init(void)436{437struct clkctl_acpu_speed *speed;438uint32_t div, sel;439int rc;440441/*442* Determine the rate of ACPU clock443*/444445if (!(readl(A11S_CLK_SEL_ADDR) & 0x01)) { /* CLK_SEL_SRC1N0 */446/* CLK_SRC0_SEL */447sel = (readl(A11S_CLK_CNTL_ADDR) >> 12) & 0x7;448/* CLK_SRC0_DIV */449div = (readl(A11S_CLK_CNTL_ADDR) >> 8) & 0x0f;450} else {451/* CLK_SRC1_SEL */452sel = (readl(A11S_CLK_CNTL_ADDR) >> 4) & 0x07;453/* CLK_SRC1_DIV */454div = readl(A11S_CLK_CNTL_ADDR) & 0x0f;455}456457for (speed = acpu_freq_tbl; speed->a11clk_khz != 0; speed++) {458if (speed->a11clk_src_sel == sel459&& (speed->a11clk_src_div == div))460break;461}462if (speed->a11clk_khz == 0) {463printk(KERN_WARNING "Warning - ACPU clock reports invalid speed\n");464return;465}466467drv_state.current_speed = speed;468469rc = clk_set_rate(ebi1_clk, speed->axiclk_khz * 1000);470if (rc < 0)471pr_err("Setting AXI min rate failed!\n");472473printk(KERN_INFO "ACPU running at %d KHz\n", speed->a11clk_khz);474}475476unsigned long acpuclk_get_rate(void)477{478WARN_ONCE(drv_state.current_speed == NULL,479"acpuclk_get_rate: not initialized\n");480if (drv_state.current_speed)481return drv_state.current_speed->a11clk_khz;482else483return 0;484}485486uint32_t acpuclk_get_switch_time(void)487{488return drv_state.acpu_switch_time_us;489}490491/*----------------------------------------------------------------------------492* Clock driver initialization493*---------------------------------------------------------------------------*/494495/* Initialize the lpj field in the acpu_freq_tbl. */496static void __init lpj_init(void)497{498int i;499const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;500for (i = 0; acpu_freq_tbl[i].a11clk_khz; i++) {501acpu_freq_tbl[i].lpj = cpufreq_scale(loops_per_jiffy,502base_clk->a11clk_khz,503acpu_freq_tbl[i].a11clk_khz);504}505}506507void __init msm_acpu_clock_init(struct msm_acpu_clock_platform_data *clkdata)508{509pr_info("acpu_clock_init()\n");510511ebi1_clk = clk_get(NULL, "ebi1_clk");512513mutex_init(&drv_state.lock);514drv_state.acpu_switch_time_us = clkdata->acpu_switch_time_us;515drv_state.max_speed_delta_khz = clkdata->max_speed_delta_khz;516drv_state.vdd_switch_time_us = clkdata->vdd_switch_time_us;517drv_state.power_collapse_khz = clkdata->power_collapse_khz;518drv_state.wait_for_irq_khz = clkdata->wait_for_irq_khz;519acpuclk_init();520lpj_init();521#ifdef CONFIG_CPU_FREQ_TABLE522cpufreq_frequency_table_get_attr(freq_table, smp_processor_id());523#endif524}525526527