Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/mach-omap2/clock.c |
| 3 | * |
Tony Lindgren | a16e970 | 2008-03-18 11:56:39 +0200 | [diff] [blame] | 4 | * Copyright (C) 2005-2008 Texas Instruments, Inc. |
| 5 | * Copyright (C) 2004-2008 Nokia Corporation |
| 6 | * |
| 7 | * Contacts: |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 8 | * Richard Woodruff <r-woodruff2@ti.com> |
Tony Lindgren | a16e970 | 2008-03-18 11:56:39 +0200 | [diff] [blame] | 9 | * Paul Walmsley |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 10 | * |
Tony Lindgren | a16e970 | 2008-03-18 11:56:39 +0200 | [diff] [blame] | 11 | * Based on earlier work by Tuukka Tikkanen, Tony Lindgren, |
| 12 | * Gordon McNutt and RidgeRun, Inc. |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License version 2 as |
| 16 | * published by the Free Software Foundation. |
| 17 | */ |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 18 | #undef DEBUG |
| 19 | |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/delay.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 26 | #include <linux/clk.h> |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 27 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 28 | #include <linux/io.h> |
| 29 | #include <linux/cpufreq.h> |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 30 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 31 | #include <mach/clock.h> |
| 32 | #include <mach/sram.h> |
Tony Lindgren | 7663148 | 2006-12-12 23:02:43 -0800 | [diff] [blame] | 33 | #include <asm/div64.h> |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 34 | #include <asm/bitops.h> |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 35 | |
Tony Lindgren | b824efa | 2006-04-02 17:46:20 +0100 | [diff] [blame] | 36 | #include "memory.h" |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 37 | #include "clock.h" |
Paul Walmsley | 32ab2cb | 2008-03-18 10:15:28 +0200 | [diff] [blame] | 38 | #include "clock24xx.h" |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 39 | #include "prm.h" |
| 40 | #include "prm-regbits-24xx.h" |
| 41 | #include "cm.h" |
| 42 | #include "cm-regbits-24xx.h" |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 43 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 44 | /* CM_CLKEN_PLL.EN_{54,96}M_PLL options (24XX) */ |
| 45 | #define EN_APLL_STOPPED 0 |
| 46 | #define EN_APLL_LOCKED 3 |
Juha Yrjola | ddc32a8 | 2006-09-25 12:41:50 +0300 | [diff] [blame] | 47 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 48 | /* CM_CLKSEL1_PLL.APLLS_CLKIN options (24XX) */ |
| 49 | #define APLLS_CLKIN_19_2MHZ 0 |
| 50 | #define APLLS_CLKIN_13MHZ 2 |
| 51 | #define APLLS_CLKIN_12MHZ 3 |
| 52 | |
| 53 | /* #define DOWN_VARIABLE_DPLL 1 */ /* Experimental */ |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 54 | |
| 55 | static struct prcm_config *curr_prcm_set; |
Tony Lindgren | ae78dcf | 2006-09-25 12:41:20 +0300 | [diff] [blame] | 56 | static struct clk *vclk; |
| 57 | static struct clk *sclk; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 58 | |
| 59 | /*------------------------------------------------------------------------- |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 60 | * Omap24xx specific clock functions |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 61 | *-------------------------------------------------------------------------*/ |
| 62 | |
Tony Lindgren | a16e970 | 2008-03-18 11:56:39 +0200 | [diff] [blame] | 63 | /* This actually returns the rate of core_ck, not dpll_ck. */ |
| 64 | static u32 omap2_get_dpll_rate_24xx(struct clk *tclk) |
| 65 | { |
| 66 | long long dpll_clk; |
| 67 | u8 amult; |
| 68 | |
| 69 | dpll_clk = omap2_get_dpll_rate(tclk); |
| 70 | |
| 71 | amult = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2); |
| 72 | amult &= OMAP24XX_CORE_CLK_SRC_MASK; |
| 73 | dpll_clk *= amult; |
| 74 | |
| 75 | return dpll_clk; |
| 76 | } |
| 77 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 78 | static int omap2_enable_osc_ck(struct clk *clk) |
| 79 | { |
| 80 | u32 pcc; |
| 81 | |
| 82 | pcc = __raw_readl(OMAP24XX_PRCM_CLKSRC_CTRL); |
| 83 | |
| 84 | __raw_writel(pcc & ~OMAP_AUTOEXTCLKMODE_MASK, |
| 85 | OMAP24XX_PRCM_CLKSRC_CTRL); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static void omap2_disable_osc_ck(struct clk *clk) |
| 91 | { |
| 92 | u32 pcc; |
| 93 | |
| 94 | pcc = __raw_readl(OMAP24XX_PRCM_CLKSRC_CTRL); |
| 95 | |
| 96 | __raw_writel(pcc | OMAP_AUTOEXTCLKMODE_MASK, |
| 97 | OMAP24XX_PRCM_CLKSRC_CTRL); |
| 98 | } |
| 99 | |
| 100 | #ifdef OLD_CK |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 101 | /* Recalculate SYST_CLK */ |
| 102 | static void omap2_sys_clk_recalc(struct clk * clk) |
| 103 | { |
| 104 | u32 div = PRCM_CLKSRC_CTRL; |
| 105 | div &= (1 << 7) | (1 << 6); /* Test if ext clk divided by 1 or 2 */ |
| 106 | div >>= clk->rate_offset; |
| 107 | clk->rate = (clk->parent->rate / div); |
| 108 | propagate_rate(clk); |
| 109 | } |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 110 | #endif /* OLD_CK */ |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 111 | |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 112 | /* Enable an APLL if off */ |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 113 | static int omap2_clk_fixed_enable(struct clk *clk) |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 114 | { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 115 | u32 cval, apll_mask; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 116 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 117 | apll_mask = EN_APLL_LOCKED << clk->enable_bit; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 118 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 119 | cval = cm_read_mod_reg(PLL_MOD, CM_CLKEN); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 120 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 121 | if ((cval & apll_mask) == apll_mask) |
| 122 | return 0; /* apll already enabled */ |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 123 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 124 | cval &= ~apll_mask; |
| 125 | cval |= apll_mask; |
| 126 | cm_write_mod_reg(cval, PLL_MOD, CM_CLKEN); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 127 | |
| 128 | if (clk == &apll96_ck) |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 129 | cval = OMAP24XX_ST_96M_APLL; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 130 | else if (clk == &apll54_ck) |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 131 | cval = OMAP24XX_ST_54M_APLL; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 132 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 133 | omap2_wait_clock_ready(OMAP_CM_REGADDR(PLL_MOD, CM_IDLEST), cval, |
| 134 | clk->name); |
| 135 | |
| 136 | /* |
| 137 | * REVISIT: Should we return an error code if omap2_wait_clock_ready() |
| 138 | * fails? |
| 139 | */ |
| 140 | return 0; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 143 | /* Stop APLL */ |
| 144 | static void omap2_clk_fixed_disable(struct clk *clk) |
| 145 | { |
| 146 | u32 cval; |
| 147 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 148 | cval = cm_read_mod_reg(PLL_MOD, CM_CLKEN); |
| 149 | cval &= ~(EN_APLL_LOCKED << clk->enable_bit); |
| 150 | cm_write_mod_reg(cval, PLL_MOD, CM_CLKEN); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 153 | /* |
| 154 | * Uses the current prcm set to tell if a rate is valid. |
| 155 | * You can go slower, but not faster within a given rate set. |
| 156 | */ |
Paul Walmsley | 88b8ba9 | 2008-07-03 12:24:46 +0300 | [diff] [blame] | 157 | long omap2_dpllcore_round_rate(unsigned long target_rate) |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 158 | { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 159 | u32 high, low, core_clk_src; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 160 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 161 | core_clk_src = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2); |
| 162 | core_clk_src &= OMAP24XX_CORE_CLK_SRC_MASK; |
| 163 | |
| 164 | if (core_clk_src == CORE_CLK_SRC_DPLL) { /* DPLL clockout */ |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 165 | high = curr_prcm_set->dpll_speed * 2; |
| 166 | low = curr_prcm_set->dpll_speed; |
| 167 | } else { /* DPLL clockout x 2 */ |
| 168 | high = curr_prcm_set->dpll_speed; |
| 169 | low = curr_prcm_set->dpll_speed / 2; |
| 170 | } |
| 171 | |
| 172 | #ifdef DOWN_VARIABLE_DPLL |
| 173 | if (target_rate > high) |
| 174 | return high; |
| 175 | else |
| 176 | return target_rate; |
| 177 | #else |
| 178 | if (target_rate > low) |
| 179 | return high; |
| 180 | else |
| 181 | return low; |
| 182 | #endif |
| 183 | |
| 184 | } |
| 185 | |
Paul Walmsley | 88b8ba9 | 2008-07-03 12:24:46 +0300 | [diff] [blame] | 186 | static void omap2_dpllcore_recalc(struct clk *clk) |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 187 | { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 188 | clk->rate = omap2_get_dpll_rate_24xx(clk); |
| 189 | |
| 190 | propagate_rate(clk); |
| 191 | } |
| 192 | |
Paul Walmsley | 88b8ba9 | 2008-07-03 12:24:46 +0300 | [diff] [blame] | 193 | static int omap2_reprogram_dpllcore(struct clk *clk, unsigned long rate) |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 194 | { |
| 195 | u32 cur_rate, low, mult, div, valid_rate, done_rate; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 196 | u32 bypass = 0; |
| 197 | struct prcm_config tmpset; |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 198 | const struct dpll_data *dd; |
| 199 | unsigned long flags; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 200 | int ret = -EINVAL; |
| 201 | |
| 202 | local_irq_save(flags); |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 203 | cur_rate = omap2_get_dpll_rate_24xx(&dpll_ck); |
| 204 | mult = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2); |
| 205 | mult &= OMAP24XX_CORE_CLK_SRC_MASK; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 206 | |
| 207 | if ((rate == (cur_rate / 2)) && (mult == 2)) { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 208 | omap2_reprogram_sdrc(CORE_CLK_SRC_DPLL, 1); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 209 | } else if ((rate == (cur_rate * 2)) && (mult == 1)) { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 210 | omap2_reprogram_sdrc(CORE_CLK_SRC_DPLL_X2, 1); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 211 | } else if (rate != cur_rate) { |
Paul Walmsley | 88b8ba9 | 2008-07-03 12:24:46 +0300 | [diff] [blame] | 212 | valid_rate = omap2_dpllcore_round_rate(rate); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 213 | if (valid_rate != rate) |
| 214 | goto dpll_exit; |
| 215 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 216 | if (mult == 1) |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 217 | low = curr_prcm_set->dpll_speed; |
| 218 | else |
| 219 | low = curr_prcm_set->dpll_speed / 2; |
| 220 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 221 | dd = clk->dpll_data; |
| 222 | if (!dd) |
| 223 | goto dpll_exit; |
| 224 | |
| 225 | tmpset.cm_clksel1_pll = __raw_readl(dd->mult_div1_reg); |
| 226 | tmpset.cm_clksel1_pll &= ~(dd->mult_mask | |
| 227 | dd->div1_mask); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 228 | div = ((curr_prcm_set->xtal_speed / 1000000) - 1); |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 229 | tmpset.cm_clksel2_pll = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2); |
| 230 | tmpset.cm_clksel2_pll &= ~OMAP24XX_CORE_CLK_SRC_MASK; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 231 | if (rate > low) { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 232 | tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL_X2; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 233 | mult = ((rate / 2) / 1000000); |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 234 | done_rate = CORE_CLK_SRC_DPLL_X2; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 235 | } else { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 236 | tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 237 | mult = (rate / 1000000); |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 238 | done_rate = CORE_CLK_SRC_DPLL; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 239 | } |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 240 | tmpset.cm_clksel1_pll |= (div << __ffs(dd->mult_mask)); |
| 241 | tmpset.cm_clksel1_pll |= (mult << __ffs(dd->div1_mask)); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 242 | |
| 243 | /* Worst case */ |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 244 | tmpset.base_sdrc_rfr = SDRC_RFR_CTRL_BYPASS; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 245 | |
| 246 | if (rate == curr_prcm_set->xtal_speed) /* If asking for 1-1 */ |
| 247 | bypass = 1; |
| 248 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 249 | omap2_reprogram_sdrc(CORE_CLK_SRC_DPLL_X2, 1); /* For init_mem */ |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 250 | |
| 251 | /* Force dll lock mode */ |
| 252 | omap2_set_prcm(tmpset.cm_clksel1_pll, tmpset.base_sdrc_rfr, |
| 253 | bypass); |
| 254 | |
| 255 | /* Errata: ret dll entry state */ |
| 256 | omap2_init_memory_params(omap2_dll_force_needed()); |
| 257 | omap2_reprogram_sdrc(done_rate, 0); |
| 258 | } |
Paul Walmsley | 88b8ba9 | 2008-07-03 12:24:46 +0300 | [diff] [blame] | 259 | omap2_dpllcore_recalc(&dpll_ck); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 260 | ret = 0; |
| 261 | |
| 262 | dpll_exit: |
| 263 | local_irq_restore(flags); |
| 264 | return(ret); |
| 265 | } |
| 266 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 267 | /** |
| 268 | * omap2_table_mpu_recalc - just return the MPU speed |
| 269 | * @clk: virt_prcm_set struct clk |
| 270 | * |
| 271 | * Set virt_prcm_set's rate to the mpu_speed field of the current PRCM set. |
| 272 | */ |
| 273 | static void omap2_table_mpu_recalc(struct clk *clk) |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 274 | { |
| 275 | clk->rate = curr_prcm_set->mpu_speed; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Look for a rate equal or less than the target rate given a configuration set. |
| 280 | * |
| 281 | * What's not entirely clear is "which" field represents the key field. |
| 282 | * Some might argue L3-DDR, others ARM, others IVA. This code is simple and |
| 283 | * just uses the ARM rates. |
| 284 | */ |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 285 | static long omap2_round_to_table_rate(struct clk *clk, unsigned long rate) |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 286 | { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 287 | struct prcm_config *ptr; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 288 | long highest_rate; |
| 289 | |
| 290 | if (clk != &virt_prcm_set) |
| 291 | return -EINVAL; |
| 292 | |
| 293 | highest_rate = -EINVAL; |
| 294 | |
| 295 | for (ptr = rate_table; ptr->mpu_speed; ptr++) { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 296 | if (!(ptr->flags & cpu_mask)) |
| 297 | continue; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 298 | if (ptr->xtal_speed != sys_ck.rate) |
| 299 | continue; |
| 300 | |
| 301 | highest_rate = ptr->mpu_speed; |
| 302 | |
| 303 | /* Can check only after xtal frequency check */ |
| 304 | if (ptr->mpu_speed <= rate) |
| 305 | break; |
| 306 | } |
| 307 | return highest_rate; |
| 308 | } |
| 309 | |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 310 | /* Sets basic clocks based on the specified rate */ |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 311 | static int omap2_select_table_rate(struct clk *clk, unsigned long rate) |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 312 | { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 313 | u32 cur_rate, done_rate, bypass = 0, tmp; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 314 | struct prcm_config *prcm; |
| 315 | unsigned long found_speed = 0; |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 316 | unsigned long flags; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 317 | |
| 318 | if (clk != &virt_prcm_set) |
| 319 | return -EINVAL; |
| 320 | |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 321 | for (prcm = rate_table; prcm->mpu_speed; prcm++) { |
| 322 | if (!(prcm->flags & cpu_mask)) |
| 323 | continue; |
| 324 | |
| 325 | if (prcm->xtal_speed != sys_ck.rate) |
| 326 | continue; |
| 327 | |
| 328 | if (prcm->mpu_speed <= rate) { |
| 329 | found_speed = prcm->mpu_speed; |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if (!found_speed) { |
| 335 | printk(KERN_INFO "Could not set MPU rate to %luMHz\n", |
Tony Lindgren | a16e970 | 2008-03-18 11:56:39 +0200 | [diff] [blame] | 336 | rate / 1000000); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 337 | return -EINVAL; |
| 338 | } |
| 339 | |
| 340 | curr_prcm_set = prcm; |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 341 | cur_rate = omap2_get_dpll_rate_24xx(&dpll_ck); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 342 | |
| 343 | if (prcm->dpll_speed == cur_rate / 2) { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 344 | omap2_reprogram_sdrc(CORE_CLK_SRC_DPLL, 1); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 345 | } else if (prcm->dpll_speed == cur_rate * 2) { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 346 | omap2_reprogram_sdrc(CORE_CLK_SRC_DPLL_X2, 1); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 347 | } else if (prcm->dpll_speed != cur_rate) { |
| 348 | local_irq_save(flags); |
| 349 | |
| 350 | if (prcm->dpll_speed == prcm->xtal_speed) |
| 351 | bypass = 1; |
| 352 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 353 | if ((prcm->cm_clksel2_pll & OMAP24XX_CORE_CLK_SRC_MASK) == |
| 354 | CORE_CLK_SRC_DPLL_X2) |
| 355 | done_rate = CORE_CLK_SRC_DPLL_X2; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 356 | else |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 357 | done_rate = CORE_CLK_SRC_DPLL; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 358 | |
| 359 | /* MPU divider */ |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 360 | cm_write_mod_reg(prcm->cm_clksel_mpu, MPU_MOD, CM_CLKSEL); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 361 | |
| 362 | /* dsp + iva1 div(2420), iva2.1(2430) */ |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 363 | cm_write_mod_reg(prcm->cm_clksel_dsp, |
| 364 | OMAP24XX_DSP_MOD, CM_CLKSEL); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 365 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 366 | cm_write_mod_reg(prcm->cm_clksel_gfx, GFX_MOD, CM_CLKSEL); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 367 | |
| 368 | /* Major subsystem dividers */ |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 369 | tmp = cm_read_mod_reg(CORE_MOD, CM_CLKSEL1) & OMAP24XX_CLKSEL_DSS2_MASK; |
| 370 | cm_write_mod_reg(prcm->cm_clksel1_core | tmp, CORE_MOD, CM_CLKSEL1); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 371 | if (cpu_is_omap2430()) |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 372 | cm_write_mod_reg(prcm->cm_clksel_mdm, |
| 373 | OMAP2430_MDM_MOD, CM_CLKSEL); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 374 | |
| 375 | /* x2 to enter init_mem */ |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 376 | omap2_reprogram_sdrc(CORE_CLK_SRC_DPLL_X2, 1); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 377 | |
| 378 | omap2_set_prcm(prcm->cm_clksel1_pll, prcm->base_sdrc_rfr, |
| 379 | bypass); |
| 380 | |
| 381 | omap2_init_memory_params(omap2_dll_force_needed()); |
| 382 | omap2_reprogram_sdrc(done_rate, 0); |
| 383 | |
| 384 | local_irq_restore(flags); |
| 385 | } |
Paul Walmsley | 88b8ba9 | 2008-07-03 12:24:46 +0300 | [diff] [blame] | 386 | omap2_dpllcore_recalc(&dpll_ck); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 391 | static struct clk_functions omap2_clk_functions = { |
| 392 | .clk_enable = omap2_clk_enable, |
| 393 | .clk_disable = omap2_clk_disable, |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 394 | .clk_round_rate = omap2_clk_round_rate, |
| 395 | .clk_set_rate = omap2_clk_set_rate, |
| 396 | .clk_set_parent = omap2_clk_set_parent, |
Tony Lindgren | 90afd5c | 2006-09-25 13:27:20 +0300 | [diff] [blame] | 397 | .clk_disable_unused = omap2_clk_disable_unused, |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 398 | }; |
| 399 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 400 | static u32 omap2_get_apll_clkin(void) |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 401 | { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 402 | u32 aplls, sclk = 0; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 403 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 404 | aplls = cm_read_mod_reg(PLL_MOD, CM_CLKSEL1); |
| 405 | aplls &= OMAP24XX_APLLS_CLKIN_MASK; |
| 406 | aplls >>= OMAP24XX_APLLS_CLKIN_SHIFT; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 407 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 408 | if (aplls == APLLS_CLKIN_19_2MHZ) |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 409 | sclk = 19200000; |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 410 | else if (aplls == APLLS_CLKIN_13MHZ) |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 411 | sclk = 13000000; |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 412 | else if (aplls == APLLS_CLKIN_12MHZ) |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 413 | sclk = 12000000; |
| 414 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 415 | return sclk; |
| 416 | } |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 417 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 418 | static u32 omap2_get_sysclkdiv(void) |
| 419 | { |
| 420 | u32 div; |
| 421 | |
| 422 | div = __raw_readl(OMAP24XX_PRCM_CLKSRC_CTRL); |
| 423 | div &= OMAP_SYSCLKDIV_MASK; |
| 424 | div >>= OMAP_SYSCLKDIV_SHIFT; |
| 425 | |
| 426 | return div; |
| 427 | } |
| 428 | |
| 429 | static void omap2_osc_clk_recalc(struct clk *clk) |
| 430 | { |
| 431 | clk->rate = omap2_get_apll_clkin() * omap2_get_sysclkdiv(); |
| 432 | propagate_rate(clk); |
| 433 | } |
| 434 | |
| 435 | static void omap2_sys_clk_recalc(struct clk *clk) |
| 436 | { |
| 437 | clk->rate = clk->parent->rate / omap2_get_sysclkdiv(); |
| 438 | propagate_rate(clk); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Tony Lindgren | ae78dcf | 2006-09-25 12:41:20 +0300 | [diff] [blame] | 441 | /* |
| 442 | * Set clocks for bypass mode for reboot to work. |
| 443 | */ |
| 444 | void omap2_clk_prepare_for_reboot(void) |
| 445 | { |
| 446 | u32 rate; |
| 447 | |
| 448 | if (vclk == NULL || sclk == NULL) |
| 449 | return; |
| 450 | |
| 451 | rate = clk_get_rate(sclk); |
| 452 | clk_set_rate(vclk, rate); |
| 453 | } |
| 454 | |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 455 | /* |
| 456 | * Switch the MPU rate if specified on cmdline. |
| 457 | * We cannot do this early until cmdline is parsed. |
| 458 | */ |
| 459 | static int __init omap2_clk_arch_init(void) |
| 460 | { |
| 461 | if (!mpurate) |
| 462 | return -EINVAL; |
| 463 | |
| 464 | if (omap2_select_table_rate(&virt_prcm_set, mpurate)) |
| 465 | printk(KERN_ERR "Could not find matching MPU rate\n"); |
| 466 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 467 | recalculate_root_clocks(); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 468 | |
| 469 | printk(KERN_INFO "Switched to new clocking rate (Crystal/DPLL/MPU): " |
| 470 | "%ld.%01ld/%ld/%ld MHz\n", |
| 471 | (sys_ck.rate / 1000000), (sys_ck.rate / 100000) % 10, |
| 472 | (dpll_ck.rate / 1000000), (mpu_ck.rate / 1000000)) ; |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | arch_initcall(omap2_clk_arch_init); |
| 477 | |
| 478 | int __init omap2_clk_init(void) |
| 479 | { |
| 480 | struct prcm_config *prcm; |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 481 | struct clk **clkp; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 482 | u32 clkrate; |
| 483 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 484 | if (cpu_is_omap242x()) |
| 485 | cpu_mask = RATE_IN_242X; |
| 486 | else if (cpu_is_omap2430()) |
| 487 | cpu_mask = RATE_IN_243X; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 488 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 489 | clk_init(&omap2_clk_functions); |
| 490 | |
| 491 | omap2_osc_clk_recalc(&osc_ck); |
| 492 | omap2_sys_clk_recalc(&sys_ck); |
| 493 | |
| 494 | for (clkp = onchip_24xx_clks; |
| 495 | clkp < onchip_24xx_clks + ARRAY_SIZE(onchip_24xx_clks); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 496 | clkp++) { |
| 497 | |
| 498 | if ((*clkp)->flags & CLOCK_IN_OMAP242X && cpu_is_omap2420()) { |
| 499 | clk_register(*clkp); |
| 500 | continue; |
| 501 | } |
| 502 | |
| 503 | if ((*clkp)->flags & CLOCK_IN_OMAP243X && cpu_is_omap2430()) { |
| 504 | clk_register(*clkp); |
| 505 | continue; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /* Check the MPU rate set by bootloader */ |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 510 | clkrate = omap2_get_dpll_rate_24xx(&dpll_ck); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 511 | for (prcm = rate_table; prcm->mpu_speed; prcm++) { |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 512 | if (!(prcm->flags & cpu_mask)) |
| 513 | continue; |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 514 | if (prcm->xtal_speed != sys_ck.rate) |
| 515 | continue; |
| 516 | if (prcm->dpll_speed <= clkrate) |
| 517 | break; |
| 518 | } |
| 519 | curr_prcm_set = prcm; |
| 520 | |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 521 | recalculate_root_clocks(); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 522 | |
| 523 | printk(KERN_INFO "Clocking rate (Crystal/DPLL/MPU): " |
| 524 | "%ld.%01ld/%ld/%ld MHz\n", |
| 525 | (sys_ck.rate / 1000000), (sys_ck.rate / 100000) % 10, |
| 526 | (dpll_ck.rate / 1000000), (mpu_ck.rate / 1000000)) ; |
| 527 | |
| 528 | /* |
| 529 | * Only enable those clocks we will need, let the drivers |
| 530 | * enable other clocks as necessary |
| 531 | */ |
Paul Walmsley | 6b8858a | 2008-03-18 10:35:15 +0200 | [diff] [blame] | 532 | clk_enable_init_clocks(); |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 533 | |
Tony Lindgren | ae78dcf | 2006-09-25 12:41:20 +0300 | [diff] [blame] | 534 | /* Avoid sleeping sleeping during omap2_clk_prepare_for_reboot() */ |
| 535 | vclk = clk_get(NULL, "virt_prcm_set"); |
| 536 | sclk = clk_get(NULL, "sys_ck"); |
| 537 | |
Tony Lindgren | 046d6b2 | 2005-11-10 14:26:52 +0000 | [diff] [blame] | 538 | return 0; |
| 539 | } |