Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/mach-omap1/clock.c |
| 3 | * |
Paul Walmsley | 51c1954 | 2010-02-22 22:09:26 -0700 | [diff] [blame] | 4 | * Copyright (C) 2004 - 2005, 2009-2010 Nokia Corporation |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 5 | * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com> |
| 6 | * |
| 7 | * Modified to use omap shared clock framework by |
| 8 | * Tony Lindgren <tony@atomide.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Paul Walmsley | 1fe9be8 | 2012-09-27 10:33:33 -0600 | [diff] [blame] | 15 | #include <linux/export.h> |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 16 | #include <linux/list.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/err.h> |
Tony Lindgren | 2c799ce | 2012-02-24 10:34:35 -0800 | [diff] [blame] | 19 | #include <linux/io.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 20 | #include <linux/clk.h> |
Jean-Christop PLAGNIOL-VILLARD | 6d803ba | 2010-11-17 10:04:33 +0100 | [diff] [blame] | 21 | #include <linux/clkdev.h> |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 22 | |
Tony Lindgren | 90afd5c | 2006-09-25 13:27:20 +0300 | [diff] [blame] | 23 | #include <asm/mach-types.h> |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 24 | |
Tony Lindgren | 2c799ce | 2012-02-24 10:34:35 -0800 | [diff] [blame] | 25 | #include <mach/hardware.h> |
| 26 | |
Tony Lindgren | 622297f | 2012-10-02 14:19:52 -0700 | [diff] [blame] | 27 | #include "../plat-omap/sram.h" |
| 28 | |
Tony Lindgren | 6d02643 | 2012-10-24 15:05:45 -0700 | [diff] [blame^] | 29 | #include "soc.h" |
Tony Lindgren | 2e3ee9f | 2012-02-24 10:34:34 -0800 | [diff] [blame] | 30 | #include "iomap.h" |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 31 | #include "clock.h" |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 32 | #include "opp.h" |
| 33 | |
| 34 | __u32 arm_idlect1_mask; |
| 35 | struct clk *api_ck_p, *ck_dpll1_p, *ck_ref_p; |
| 36 | |
Paul Walmsley | 1fe9be8 | 2012-09-27 10:33:33 -0600 | [diff] [blame] | 37 | static LIST_HEAD(clocks); |
| 38 | static DEFINE_MUTEX(clocks_mutex); |
| 39 | static DEFINE_SPINLOCK(clockfw_lock); |
| 40 | |
Paul Walmsley | fb2fc92 | 2010-07-26 16:34:28 -0600 | [diff] [blame] | 41 | /* |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 42 | * Omap1 specific clock functions |
Paul Walmsley | fb2fc92 | 2010-07-26 16:34:28 -0600 | [diff] [blame] | 43 | */ |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 44 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 45 | unsigned long omap1_uart_recalc(struct clk *clk) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 46 | { |
Tony Lindgren | fed415e | 2009-01-28 12:18:48 -0700 | [diff] [blame] | 47 | unsigned int val = __raw_readl(clk->enable_reg); |
Russell King | 8b9dbc1 | 2009-02-12 10:12:59 +0000 | [diff] [blame] | 48 | return val & clk->enable_bit ? 48000000 : 12000000; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 51 | unsigned long omap1_sossi_recalc(struct clk *clk) |
Imre Deak | df2c2e7 | 2007-03-05 17:22:58 +0200 | [diff] [blame] | 52 | { |
| 53 | u32 div = omap_readl(MOD_CONF_CTRL_1); |
| 54 | |
| 55 | div = (div >> 17) & 0x7; |
| 56 | div++; |
Russell King | 8b9dbc1 | 2009-02-12 10:12:59 +0000 | [diff] [blame] | 57 | |
| 58 | return clk->parent->rate / div; |
Imre Deak | df2c2e7 | 2007-03-05 17:22:58 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 61 | static void omap1_clk_allow_idle(struct clk *clk) |
| 62 | { |
| 63 | struct arm_idlect1_clk * iclk = (struct arm_idlect1_clk *)clk; |
| 64 | |
| 65 | if (!(clk->flags & CLOCK_IDLE_CONTROL)) |
| 66 | return; |
| 67 | |
| 68 | if (iclk->no_idle_count > 0 && !(--iclk->no_idle_count)) |
| 69 | arm_idlect1_mask |= 1 << iclk->idlect_shift; |
| 70 | } |
| 71 | |
| 72 | static void omap1_clk_deny_idle(struct clk *clk) |
| 73 | { |
| 74 | struct arm_idlect1_clk * iclk = (struct arm_idlect1_clk *)clk; |
| 75 | |
| 76 | if (!(clk->flags & CLOCK_IDLE_CONTROL)) |
| 77 | return; |
| 78 | |
| 79 | if (iclk->no_idle_count++ == 0) |
| 80 | arm_idlect1_mask &= ~(1 << iclk->idlect_shift); |
| 81 | } |
| 82 | |
| 83 | static __u16 verify_ckctl_value(__u16 newval) |
| 84 | { |
| 85 | /* This function checks for following limitations set |
| 86 | * by the hardware (all conditions must be true): |
| 87 | * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2 |
| 88 | * ARM_CK >= TC_CK |
| 89 | * DSP_CK >= TC_CK |
| 90 | * DSPMMU_CK >= TC_CK |
| 91 | * |
| 92 | * In addition following rules are enforced: |
| 93 | * LCD_CK <= TC_CK |
| 94 | * ARMPER_CK <= TC_CK |
| 95 | * |
| 96 | * However, maximum frequencies are not checked for! |
| 97 | */ |
| 98 | __u8 per_exp; |
| 99 | __u8 lcd_exp; |
| 100 | __u8 arm_exp; |
| 101 | __u8 dsp_exp; |
| 102 | __u8 tc_exp; |
| 103 | __u8 dspmmu_exp; |
| 104 | |
| 105 | per_exp = (newval >> CKCTL_PERDIV_OFFSET) & 3; |
| 106 | lcd_exp = (newval >> CKCTL_LCDDIV_OFFSET) & 3; |
| 107 | arm_exp = (newval >> CKCTL_ARMDIV_OFFSET) & 3; |
| 108 | dsp_exp = (newval >> CKCTL_DSPDIV_OFFSET) & 3; |
| 109 | tc_exp = (newval >> CKCTL_TCDIV_OFFSET) & 3; |
| 110 | dspmmu_exp = (newval >> CKCTL_DSPMMUDIV_OFFSET) & 3; |
| 111 | |
| 112 | if (dspmmu_exp < dsp_exp) |
| 113 | dspmmu_exp = dsp_exp; |
| 114 | if (dspmmu_exp > dsp_exp+1) |
| 115 | dspmmu_exp = dsp_exp+1; |
| 116 | if (tc_exp < arm_exp) |
| 117 | tc_exp = arm_exp; |
| 118 | if (tc_exp < dspmmu_exp) |
| 119 | tc_exp = dspmmu_exp; |
| 120 | if (tc_exp > lcd_exp) |
| 121 | lcd_exp = tc_exp; |
| 122 | if (tc_exp > per_exp) |
| 123 | per_exp = tc_exp; |
| 124 | |
| 125 | newval &= 0xf000; |
| 126 | newval |= per_exp << CKCTL_PERDIV_OFFSET; |
| 127 | newval |= lcd_exp << CKCTL_LCDDIV_OFFSET; |
| 128 | newval |= arm_exp << CKCTL_ARMDIV_OFFSET; |
| 129 | newval |= dsp_exp << CKCTL_DSPDIV_OFFSET; |
| 130 | newval |= tc_exp << CKCTL_TCDIV_OFFSET; |
| 131 | newval |= dspmmu_exp << CKCTL_DSPMMUDIV_OFFSET; |
| 132 | |
| 133 | return newval; |
| 134 | } |
| 135 | |
| 136 | static int calc_dsor_exp(struct clk *clk, unsigned long rate) |
| 137 | { |
| 138 | /* Note: If target frequency is too low, this function will return 4, |
| 139 | * which is invalid value. Caller must check for this value and act |
| 140 | * accordingly. |
| 141 | * |
| 142 | * Note: This function does not check for following limitations set |
| 143 | * by the hardware (all conditions must be true): |
| 144 | * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2 |
| 145 | * ARM_CK >= TC_CK |
| 146 | * DSP_CK >= TC_CK |
| 147 | * DSPMMU_CK >= TC_CK |
| 148 | */ |
| 149 | unsigned long realrate; |
| 150 | struct clk * parent; |
| 151 | unsigned dsor_exp; |
| 152 | |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 153 | parent = clk->parent; |
Russell King | c0fc18c5 | 2008-09-05 15:10:27 +0100 | [diff] [blame] | 154 | if (unlikely(parent == NULL)) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 155 | return -EIO; |
| 156 | |
| 157 | realrate = parent->rate; |
| 158 | for (dsor_exp=0; dsor_exp<4; dsor_exp++) { |
| 159 | if (realrate <= rate) |
| 160 | break; |
| 161 | |
| 162 | realrate /= 2; |
| 163 | } |
| 164 | |
| 165 | return dsor_exp; |
| 166 | } |
| 167 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 168 | unsigned long omap1_ckctl_recalc(struct clk *clk) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 169 | { |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 170 | /* Calculate divisor encoded as 2-bit exponent */ |
Russell King | 8b9dbc1 | 2009-02-12 10:12:59 +0000 | [diff] [blame] | 171 | int dsor = 1 << (3 & (omap_readw(ARM_CKCTL) >> clk->rate_offset)); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 172 | |
Russell King | 8b9dbc1 | 2009-02-12 10:12:59 +0000 | [diff] [blame] | 173 | return clk->parent->rate / dsor; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 176 | unsigned long omap1_ckctl_recalc_dsp_domain(struct clk *clk) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 177 | { |
| 178 | int dsor; |
| 179 | |
| 180 | /* Calculate divisor encoded as 2-bit exponent |
| 181 | * |
| 182 | * The clock control bits are in DSP domain, |
| 183 | * so api_ck is needed for access. |
| 184 | * Note that DSP_CKCTL virt addr = phys addr, so |
| 185 | * we must use __raw_readw() instead of omap_readw(). |
| 186 | */ |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 187 | omap1_clk_enable(api_ck_p); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 188 | dsor = 1 << (3 & (__raw_readw(DSP_CKCTL) >> clk->rate_offset)); |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 189 | omap1_clk_disable(api_ck_p); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 190 | |
Russell King | 8b9dbc1 | 2009-02-12 10:12:59 +0000 | [diff] [blame] | 191 | return clk->parent->rate / dsor; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* MPU virtual clock functions */ |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 195 | int omap1_select_table_rate(struct clk *clk, unsigned long rate) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 196 | { |
| 197 | /* Find the highest supported frequency <= rate and switch to it */ |
| 198 | struct mpu_rate * ptr; |
Paul Walmsley | eeb3711 | 2012-04-13 06:34:32 -0600 | [diff] [blame] | 199 | unsigned long ref_rate; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 200 | |
Paul Walmsley | af022fa | 2010-01-19 17:30:55 -0700 | [diff] [blame] | 201 | ref_rate = ck_ref_p->rate; |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 202 | |
| 203 | for (ptr = omap1_rate_table; ptr->rate; ptr++) { |
Janusz Krzysztofik | 24ce270 | 2011-12-08 18:01:41 -0800 | [diff] [blame] | 204 | if (!(ptr->flags & cpu_mask)) |
| 205 | continue; |
| 206 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 207 | if (ptr->xtal != ref_rate) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 208 | continue; |
| 209 | |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 210 | /* Can check only after xtal frequency check */ |
| 211 | if (ptr->rate <= rate) |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | if (!ptr->rate) |
| 216 | return -EINVAL; |
| 217 | |
| 218 | /* |
| 219 | * In most cases we should not need to reprogram DPLL. |
| 220 | * Reprogramming the DPLL is tricky, it must be done from SRAM. |
| 221 | */ |
Janusz Krzysztofik | f9e5908 | 2011-12-01 22:16:26 +0100 | [diff] [blame] | 222 | omap_sram_reprogram_clock(ptr->dpllctl_val, ptr->ckctl_val); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 223 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 224 | /* XXX Do we need to recalculate the tree below DPLL1 at this point? */ |
| 225 | ck_dpll1_p->rate = ptr->pll_rate; |
| 226 | |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 230 | int omap1_clk_set_rate_dsp_domain(struct clk *clk, unsigned long rate) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 231 | { |
Russell King | d5e6072 | 2009-02-08 16:07:46 +0000 | [diff] [blame] | 232 | int dsor_exp; |
| 233 | u16 regval; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 234 | |
Russell King | d5e6072 | 2009-02-08 16:07:46 +0000 | [diff] [blame] | 235 | dsor_exp = calc_dsor_exp(clk, rate); |
| 236 | if (dsor_exp > 3) |
| 237 | dsor_exp = -EINVAL; |
| 238 | if (dsor_exp < 0) |
| 239 | return dsor_exp; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 240 | |
Russell King | d5e6072 | 2009-02-08 16:07:46 +0000 | [diff] [blame] | 241 | regval = __raw_readw(DSP_CKCTL); |
| 242 | regval &= ~(3 << clk->rate_offset); |
| 243 | regval |= dsor_exp << clk->rate_offset; |
| 244 | __raw_writew(regval, DSP_CKCTL); |
| 245 | clk->rate = clk->parent->rate / (1 << dsor_exp); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 246 | |
Russell King | d5e6072 | 2009-02-08 16:07:46 +0000 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 250 | long omap1_clk_round_rate_ckctl_arm(struct clk *clk, unsigned long rate) |
Russell King | d5e6072 | 2009-02-08 16:07:46 +0000 | [diff] [blame] | 251 | { |
| 252 | int dsor_exp = calc_dsor_exp(clk, rate); |
| 253 | if (dsor_exp < 0) |
| 254 | return dsor_exp; |
| 255 | if (dsor_exp > 3) |
| 256 | dsor_exp = 3; |
| 257 | return clk->parent->rate / (1 << dsor_exp); |
| 258 | } |
| 259 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 260 | int omap1_clk_set_rate_ckctl_arm(struct clk *clk, unsigned long rate) |
Russell King | d5e6072 | 2009-02-08 16:07:46 +0000 | [diff] [blame] | 261 | { |
| 262 | int dsor_exp; |
| 263 | u16 regval; |
| 264 | |
| 265 | dsor_exp = calc_dsor_exp(clk, rate); |
| 266 | if (dsor_exp > 3) |
| 267 | dsor_exp = -EINVAL; |
| 268 | if (dsor_exp < 0) |
| 269 | return dsor_exp; |
| 270 | |
| 271 | regval = omap_readw(ARM_CKCTL); |
| 272 | regval &= ~(3 << clk->rate_offset); |
| 273 | regval |= dsor_exp << clk->rate_offset; |
| 274 | regval = verify_ckctl_value(regval); |
| 275 | omap_writew(regval, ARM_CKCTL); |
| 276 | clk->rate = clk->parent->rate / (1 << dsor_exp); |
| 277 | return 0; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 280 | long omap1_round_to_table_rate(struct clk *clk, unsigned long rate) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 281 | { |
| 282 | /* Find the highest supported frequency <= rate */ |
| 283 | struct mpu_rate * ptr; |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 284 | long highest_rate; |
| 285 | unsigned long ref_rate; |
| 286 | |
Paul Walmsley | af022fa | 2010-01-19 17:30:55 -0700 | [diff] [blame] | 287 | ref_rate = ck_ref_p->rate; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 288 | |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 289 | highest_rate = -EINVAL; |
| 290 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 291 | for (ptr = omap1_rate_table; ptr->rate; ptr++) { |
Janusz Krzysztofik | 24ce270 | 2011-12-08 18:01:41 -0800 | [diff] [blame] | 292 | if (!(ptr->flags & cpu_mask)) |
| 293 | continue; |
| 294 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 295 | if (ptr->xtal != ref_rate) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 296 | continue; |
| 297 | |
| 298 | highest_rate = ptr->rate; |
| 299 | |
| 300 | /* Can check only after xtal frequency check */ |
| 301 | if (ptr->rate <= rate) |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | return highest_rate; |
| 306 | } |
| 307 | |
| 308 | static unsigned calc_ext_dsor(unsigned long rate) |
| 309 | { |
| 310 | unsigned dsor; |
| 311 | |
| 312 | /* MCLK and BCLK divisor selection is not linear: |
| 313 | * freq = 96MHz / dsor |
| 314 | * |
| 315 | * RATIO_SEL range: dsor <-> RATIO_SEL |
| 316 | * 0..6: (RATIO_SEL+2) <-> (dsor-2) |
| 317 | * 6..48: (8+(RATIO_SEL-6)*2) <-> ((dsor-8)/2+6) |
| 318 | * Minimum dsor is 2 and maximum is 96. Odd divisors starting from 9 |
| 319 | * can not be used. |
| 320 | */ |
| 321 | for (dsor = 2; dsor < 96; ++dsor) { |
| 322 | if ((dsor & 1) && dsor > 8) |
Tony Lindgren | b824efa | 2006-04-02 17:46:20 +0100 | [diff] [blame] | 323 | continue; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 324 | if (rate >= 96000000 / dsor) |
| 325 | break; |
| 326 | } |
| 327 | return dsor; |
| 328 | } |
| 329 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 330 | /* XXX Only needed on 1510 */ |
| 331 | int omap1_set_uart_rate(struct clk *clk, unsigned long rate) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 332 | { |
| 333 | unsigned int val; |
| 334 | |
Tony Lindgren | fed415e | 2009-01-28 12:18:48 -0700 | [diff] [blame] | 335 | val = __raw_readl(clk->enable_reg); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 336 | if (rate == 12000000) |
| 337 | val &= ~(1 << clk->enable_bit); |
| 338 | else if (rate == 48000000) |
| 339 | val |= (1 << clk->enable_bit); |
| 340 | else |
| 341 | return -EINVAL; |
Tony Lindgren | fed415e | 2009-01-28 12:18:48 -0700 | [diff] [blame] | 342 | __raw_writel(val, clk->enable_reg); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 343 | clk->rate = rate; |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | /* External clock (MCLK & BCLK) functions */ |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 349 | int omap1_set_ext_clk_rate(struct clk *clk, unsigned long rate) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 350 | { |
| 351 | unsigned dsor; |
| 352 | __u16 ratio_bits; |
| 353 | |
| 354 | dsor = calc_ext_dsor(rate); |
| 355 | clk->rate = 96000000 / dsor; |
| 356 | if (dsor > 8) |
| 357 | ratio_bits = ((dsor - 8) / 2 + 6) << 2; |
| 358 | else |
| 359 | ratio_bits = (dsor - 2) << 2; |
| 360 | |
Tony Lindgren | fed415e | 2009-01-28 12:18:48 -0700 | [diff] [blame] | 361 | ratio_bits |= __raw_readw(clk->enable_reg) & ~0xfd; |
| 362 | __raw_writew(ratio_bits, clk->enable_reg); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 367 | int omap1_set_sossi_rate(struct clk *clk, unsigned long rate) |
Imre Deak | df2c2e7 | 2007-03-05 17:22:58 +0200 | [diff] [blame] | 368 | { |
| 369 | u32 l; |
| 370 | int div; |
| 371 | unsigned long p_rate; |
| 372 | |
| 373 | p_rate = clk->parent->rate; |
| 374 | /* Round towards slower frequency */ |
| 375 | div = (p_rate + rate - 1) / rate; |
| 376 | div--; |
| 377 | if (div < 0 || div > 7) |
| 378 | return -EINVAL; |
| 379 | |
| 380 | l = omap_readl(MOD_CONF_CTRL_1); |
| 381 | l &= ~(7 << 17); |
| 382 | l |= div << 17; |
| 383 | omap_writel(l, MOD_CONF_CTRL_1); |
| 384 | |
| 385 | clk->rate = p_rate / (div + 1); |
Imre Deak | df2c2e7 | 2007-03-05 17:22:58 +0200 | [diff] [blame] | 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 390 | long omap1_round_ext_clk_rate(struct clk *clk, unsigned long rate) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 391 | { |
| 392 | return 96000000 / calc_ext_dsor(rate); |
| 393 | } |
| 394 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 395 | void omap1_init_ext_clk(struct clk *clk) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 396 | { |
| 397 | unsigned dsor; |
| 398 | __u16 ratio_bits; |
| 399 | |
| 400 | /* Determine current rate and ensure clock is based on 96MHz APLL */ |
Tony Lindgren | fed415e | 2009-01-28 12:18:48 -0700 | [diff] [blame] | 401 | ratio_bits = __raw_readw(clk->enable_reg) & ~1; |
| 402 | __raw_writew(ratio_bits, clk->enable_reg); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 403 | |
| 404 | ratio_bits = (ratio_bits & 0xfc) >> 2; |
| 405 | if (ratio_bits > 6) |
| 406 | dsor = (ratio_bits - 6) * 2 + 8; |
| 407 | else |
| 408 | dsor = ratio_bits + 2; |
| 409 | |
| 410 | clk-> rate = 96000000 / dsor; |
| 411 | } |
| 412 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 413 | int omap1_clk_enable(struct clk *clk) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 414 | { |
| 415 | int ret = 0; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 416 | |
Russell King | 3ef48fa | 2009-04-05 12:27:24 +0100 | [diff] [blame] | 417 | if (clk->usecount++ == 0) { |
| 418 | if (clk->parent) { |
| 419 | ret = omap1_clk_enable(clk->parent); |
| 420 | if (ret) |
| 421 | goto err; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 422 | |
| 423 | if (clk->flags & CLOCK_NO_IDLE_PARENT) |
Dirk Behme | 6f9c92f | 2006-12-06 17:13:51 -0800 | [diff] [blame] | 424 | omap1_clk_deny_idle(clk->parent); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Russell King | 548d849 | 2008-11-04 14:02:46 +0000 | [diff] [blame] | 427 | ret = clk->ops->enable(clk); |
Russell King | 3ef48fa | 2009-04-05 12:27:24 +0100 | [diff] [blame] | 428 | if (ret) { |
| 429 | if (clk->parent) |
| 430 | omap1_clk_disable(clk->parent); |
| 431 | goto err; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
Russell King | 3ef48fa | 2009-04-05 12:27:24 +0100 | [diff] [blame] | 434 | return ret; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 435 | |
Russell King | 3ef48fa | 2009-04-05 12:27:24 +0100 | [diff] [blame] | 436 | err: |
| 437 | clk->usecount--; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 438 | return ret; |
| 439 | } |
| 440 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 441 | void omap1_clk_disable(struct clk *clk) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 442 | { |
| 443 | if (clk->usecount > 0 && !(--clk->usecount)) { |
Russell King | 548d849 | 2008-11-04 14:02:46 +0000 | [diff] [blame] | 444 | clk->ops->disable(clk); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 445 | if (likely(clk->parent)) { |
Tony Lindgren | 10b5579 | 2006-01-17 15:30:42 -0800 | [diff] [blame] | 446 | omap1_clk_disable(clk->parent); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 447 | if (clk->flags & CLOCK_NO_IDLE_PARENT) |
Dirk Behme | 6f9c92f | 2006-12-06 17:13:51 -0800 | [diff] [blame] | 448 | omap1_clk_allow_idle(clk->parent); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
Tony Lindgren | 10b5579 | 2006-01-17 15:30:42 -0800 | [diff] [blame] | 453 | static int omap1_clk_enable_generic(struct clk *clk) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 454 | { |
| 455 | __u16 regval16; |
| 456 | __u32 regval32; |
| 457 | |
Russell King | c0fc18c5 | 2008-09-05 15:10:27 +0100 | [diff] [blame] | 458 | if (unlikely(clk->enable_reg == NULL)) { |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 459 | printk(KERN_ERR "clock.c: Enable for %s without enable code\n", |
| 460 | clk->name); |
Dirk Behme | 6f9c92f | 2006-12-06 17:13:51 -0800 | [diff] [blame] | 461 | return -EINVAL; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | if (clk->flags & ENABLE_REG_32BIT) { |
Tony Lindgren | fed415e | 2009-01-28 12:18:48 -0700 | [diff] [blame] | 465 | regval32 = __raw_readl(clk->enable_reg); |
| 466 | regval32 |= (1 << clk->enable_bit); |
| 467 | __raw_writel(regval32, clk->enable_reg); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 468 | } else { |
Tony Lindgren | fed415e | 2009-01-28 12:18:48 -0700 | [diff] [blame] | 469 | regval16 = __raw_readw(clk->enable_reg); |
| 470 | regval16 |= (1 << clk->enable_bit); |
| 471 | __raw_writew(regval16, clk->enable_reg); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Dirk Behme | 6f9c92f | 2006-12-06 17:13:51 -0800 | [diff] [blame] | 474 | return 0; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Tony Lindgren | 10b5579 | 2006-01-17 15:30:42 -0800 | [diff] [blame] | 477 | static void omap1_clk_disable_generic(struct clk *clk) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 478 | { |
| 479 | __u16 regval16; |
| 480 | __u32 regval32; |
| 481 | |
Russell King | c0fc18c5 | 2008-09-05 15:10:27 +0100 | [diff] [blame] | 482 | if (clk->enable_reg == NULL) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 483 | return; |
| 484 | |
| 485 | if (clk->flags & ENABLE_REG_32BIT) { |
Tony Lindgren | fed415e | 2009-01-28 12:18:48 -0700 | [diff] [blame] | 486 | regval32 = __raw_readl(clk->enable_reg); |
| 487 | regval32 &= ~(1 << clk->enable_bit); |
| 488 | __raw_writel(regval32, clk->enable_reg); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 489 | } else { |
Tony Lindgren | fed415e | 2009-01-28 12:18:48 -0700 | [diff] [blame] | 490 | regval16 = __raw_readw(clk->enable_reg); |
| 491 | regval16 &= ~(1 << clk->enable_bit); |
| 492 | __raw_writew(regval16, clk->enable_reg); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 496 | const struct clkops clkops_generic = { |
| 497 | .enable = omap1_clk_enable_generic, |
| 498 | .disable = omap1_clk_disable_generic, |
Russell King | 548d849 | 2008-11-04 14:02:46 +0000 | [diff] [blame] | 499 | }; |
| 500 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 501 | static int omap1_clk_enable_dsp_domain(struct clk *clk) |
| 502 | { |
| 503 | int retval; |
| 504 | |
| 505 | retval = omap1_clk_enable(api_ck_p); |
| 506 | if (!retval) { |
| 507 | retval = omap1_clk_enable_generic(clk); |
| 508 | omap1_clk_disable(api_ck_p); |
| 509 | } |
| 510 | |
| 511 | return retval; |
| 512 | } |
| 513 | |
| 514 | static void omap1_clk_disable_dsp_domain(struct clk *clk) |
| 515 | { |
| 516 | if (omap1_clk_enable(api_ck_p) == 0) { |
| 517 | omap1_clk_disable_generic(clk); |
| 518 | omap1_clk_disable(api_ck_p); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | const struct clkops clkops_dspck = { |
| 523 | .enable = omap1_clk_enable_dsp_domain, |
| 524 | .disable = omap1_clk_disable_dsp_domain, |
| 525 | }; |
| 526 | |
Paul Walmsley | fb2fc92 | 2010-07-26 16:34:28 -0600 | [diff] [blame] | 527 | /* XXX SYSC register handling does not belong in the clock framework */ |
| 528 | static int omap1_clk_enable_uart_functional_16xx(struct clk *clk) |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 529 | { |
| 530 | int ret; |
| 531 | struct uart_clk *uclk; |
| 532 | |
| 533 | ret = omap1_clk_enable_generic(clk); |
| 534 | if (ret == 0) { |
| 535 | /* Set smart idle acknowledgement mode */ |
| 536 | uclk = (struct uart_clk *)clk; |
| 537 | omap_writeb((omap_readb(uclk->sysc_addr) & ~0x10) | 8, |
| 538 | uclk->sysc_addr); |
| 539 | } |
| 540 | |
| 541 | return ret; |
| 542 | } |
| 543 | |
Paul Walmsley | fb2fc92 | 2010-07-26 16:34:28 -0600 | [diff] [blame] | 544 | /* XXX SYSC register handling does not belong in the clock framework */ |
| 545 | static void omap1_clk_disable_uart_functional_16xx(struct clk *clk) |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 546 | { |
| 547 | struct uart_clk *uclk; |
| 548 | |
| 549 | /* Set force idle acknowledgement mode */ |
| 550 | uclk = (struct uart_clk *)clk; |
| 551 | omap_writeb((omap_readb(uclk->sysc_addr) & ~0x18), uclk->sysc_addr); |
| 552 | |
| 553 | omap1_clk_disable_generic(clk); |
| 554 | } |
| 555 | |
Paul Walmsley | fb2fc92 | 2010-07-26 16:34:28 -0600 | [diff] [blame] | 556 | /* XXX SYSC register handling does not belong in the clock framework */ |
| 557 | const struct clkops clkops_uart_16xx = { |
| 558 | .enable = omap1_clk_enable_uart_functional_16xx, |
| 559 | .disable = omap1_clk_disable_uart_functional_16xx, |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 560 | }; |
| 561 | |
| 562 | long omap1_clk_round_rate(struct clk *clk, unsigned long rate) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 563 | { |
Russell King | c0fc18c5 | 2008-09-05 15:10:27 +0100 | [diff] [blame] | 564 | if (clk->round_rate != NULL) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 565 | return clk->round_rate(clk, rate); |
| 566 | |
| 567 | return clk->rate; |
| 568 | } |
| 569 | |
Paul Walmsley | 5265050 | 2009-12-08 16:29:38 -0700 | [diff] [blame] | 570 | int omap1_clk_set_rate(struct clk *clk, unsigned long rate) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 571 | { |
| 572 | int ret = -EINVAL; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 573 | |
| 574 | if (clk->set_rate) |
| 575 | ret = clk->set_rate(clk, rate); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 576 | return ret; |
| 577 | } |
| 578 | |
Paul Walmsley | fb2fc92 | 2010-07-26 16:34:28 -0600 | [diff] [blame] | 579 | /* |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 580 | * Omap1 clock reset and init functions |
Paul Walmsley | fb2fc92 | 2010-07-26 16:34:28 -0600 | [diff] [blame] | 581 | */ |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 582 | |
| 583 | #ifdef CONFIG_OMAP_RESET_CLOCKS |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 584 | |
Felipe Balbi | 5838bb6 | 2010-05-20 12:31:04 -0600 | [diff] [blame] | 585 | void omap1_clk_disable_unused(struct clk *clk) |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 586 | { |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 587 | __u32 regval32; |
| 588 | |
Tony Lindgren | 90afd5c | 2006-09-25 13:27:20 +0300 | [diff] [blame] | 589 | /* Clocks in the DSP domain need api_ck. Just assume bootloader |
| 590 | * has not enabled any DSP clocks */ |
Russell King | 397fcaf | 2008-09-05 15:46:19 +0100 | [diff] [blame] | 591 | if (clk->enable_reg == DSP_IDLECT2) { |
Paul Walmsley | 7852ec0 | 2012-07-26 00:54:26 -0600 | [diff] [blame] | 592 | pr_info("Skipping reset check for DSP domain clock \"%s\"\n", |
| 593 | clk->name); |
Tony Lindgren | 90afd5c | 2006-09-25 13:27:20 +0300 | [diff] [blame] | 594 | return; |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Tony Lindgren | 90afd5c | 2006-09-25 13:27:20 +0300 | [diff] [blame] | 597 | /* Is the clock already disabled? */ |
Tony Lindgren | fed415e | 2009-01-28 12:18:48 -0700 | [diff] [blame] | 598 | if (clk->flags & ENABLE_REG_32BIT) |
| 599 | regval32 = __raw_readl(clk->enable_reg); |
| 600 | else |
| 601 | regval32 = __raw_readw(clk->enable_reg); |
Tony Lindgren | 90afd5c | 2006-09-25 13:27:20 +0300 | [diff] [blame] | 602 | |
| 603 | if ((regval32 & (1 << clk->enable_bit)) == 0) |
| 604 | return; |
| 605 | |
Tony Lindgren | 90afd5c | 2006-09-25 13:27:20 +0300 | [diff] [blame] | 606 | printk(KERN_INFO "Disabling unused clock \"%s\"... ", clk->name); |
Russell King | 548d849 | 2008-11-04 14:02:46 +0000 | [diff] [blame] | 607 | clk->ops->disable(clk); |
Tony Lindgren | 90afd5c | 2006-09-25 13:27:20 +0300 | [diff] [blame] | 608 | printk(" done\n"); |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 609 | } |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 610 | |
Tony Lindgren | 3179a01 | 2005-11-10 14:26:48 +0000 | [diff] [blame] | 611 | #endif |
Paul Walmsley | 1fe9be8 | 2012-09-27 10:33:33 -0600 | [diff] [blame] | 612 | |
| 613 | |
| 614 | int clk_enable(struct clk *clk) |
| 615 | { |
| 616 | unsigned long flags; |
| 617 | int ret; |
| 618 | |
| 619 | if (clk == NULL || IS_ERR(clk)) |
| 620 | return -EINVAL; |
| 621 | |
| 622 | spin_lock_irqsave(&clockfw_lock, flags); |
| 623 | ret = omap1_clk_enable(clk); |
| 624 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 625 | |
| 626 | return ret; |
| 627 | } |
| 628 | EXPORT_SYMBOL(clk_enable); |
| 629 | |
| 630 | void clk_disable(struct clk *clk) |
| 631 | { |
| 632 | unsigned long flags; |
| 633 | |
| 634 | if (clk == NULL || IS_ERR(clk)) |
| 635 | return; |
| 636 | |
| 637 | spin_lock_irqsave(&clockfw_lock, flags); |
| 638 | if (clk->usecount == 0) { |
| 639 | pr_err("Trying disable clock %s with 0 usecount\n", |
| 640 | clk->name); |
| 641 | WARN_ON(1); |
| 642 | goto out; |
| 643 | } |
| 644 | |
| 645 | omap1_clk_disable(clk); |
| 646 | |
| 647 | out: |
| 648 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 649 | } |
| 650 | EXPORT_SYMBOL(clk_disable); |
| 651 | |
| 652 | unsigned long clk_get_rate(struct clk *clk) |
| 653 | { |
| 654 | unsigned long flags; |
| 655 | unsigned long ret; |
| 656 | |
| 657 | if (clk == NULL || IS_ERR(clk)) |
| 658 | return 0; |
| 659 | |
| 660 | spin_lock_irqsave(&clockfw_lock, flags); |
| 661 | ret = clk->rate; |
| 662 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 663 | |
| 664 | return ret; |
| 665 | } |
| 666 | EXPORT_SYMBOL(clk_get_rate); |
| 667 | |
| 668 | /* |
| 669 | * Optional clock functions defined in include/linux/clk.h |
| 670 | */ |
| 671 | |
| 672 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 673 | { |
| 674 | unsigned long flags; |
| 675 | long ret; |
| 676 | |
| 677 | if (clk == NULL || IS_ERR(clk)) |
| 678 | return 0; |
| 679 | |
| 680 | spin_lock_irqsave(&clockfw_lock, flags); |
| 681 | ret = omap1_clk_round_rate(clk, rate); |
| 682 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 683 | |
| 684 | return ret; |
| 685 | } |
| 686 | EXPORT_SYMBOL(clk_round_rate); |
| 687 | |
| 688 | int clk_set_rate(struct clk *clk, unsigned long rate) |
| 689 | { |
| 690 | unsigned long flags; |
| 691 | int ret = -EINVAL; |
| 692 | |
| 693 | if (clk == NULL || IS_ERR(clk)) |
| 694 | return ret; |
| 695 | |
| 696 | spin_lock_irqsave(&clockfw_lock, flags); |
| 697 | ret = omap1_clk_set_rate(clk, rate); |
| 698 | if (ret == 0) |
| 699 | propagate_rate(clk); |
| 700 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 701 | |
| 702 | return ret; |
| 703 | } |
| 704 | EXPORT_SYMBOL(clk_set_rate); |
| 705 | |
| 706 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 707 | { |
| 708 | WARN_ONCE(1, "clk_set_parent() not implemented for OMAP1\n"); |
| 709 | |
| 710 | return -EINVAL; |
| 711 | } |
| 712 | EXPORT_SYMBOL(clk_set_parent); |
| 713 | |
| 714 | struct clk *clk_get_parent(struct clk *clk) |
| 715 | { |
| 716 | return clk->parent; |
| 717 | } |
| 718 | EXPORT_SYMBOL(clk_get_parent); |
| 719 | |
| 720 | /* |
| 721 | * OMAP specific clock functions shared between omap1 and omap2 |
| 722 | */ |
| 723 | |
| 724 | int __initdata mpurate; |
| 725 | |
| 726 | /* |
| 727 | * By default we use the rate set by the bootloader. |
| 728 | * You can override this with mpurate= cmdline option. |
| 729 | */ |
| 730 | static int __init omap_clk_setup(char *str) |
| 731 | { |
| 732 | get_option(&str, &mpurate); |
| 733 | |
| 734 | if (!mpurate) |
| 735 | return 1; |
| 736 | |
| 737 | if (mpurate < 1000) |
| 738 | mpurate *= 1000000; |
| 739 | |
| 740 | return 1; |
| 741 | } |
| 742 | __setup("mpurate=", omap_clk_setup); |
| 743 | |
| 744 | /* Used for clocks that always have same value as the parent clock */ |
| 745 | unsigned long followparent_recalc(struct clk *clk) |
| 746 | { |
| 747 | return clk->parent->rate; |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * Used for clocks that have the same value as the parent clock, |
| 752 | * divided by some factor |
| 753 | */ |
| 754 | unsigned long omap_fixed_divisor_recalc(struct clk *clk) |
| 755 | { |
| 756 | WARN_ON(!clk->fixed_div); |
| 757 | |
| 758 | return clk->parent->rate / clk->fixed_div; |
| 759 | } |
| 760 | |
| 761 | void clk_reparent(struct clk *child, struct clk *parent) |
| 762 | { |
| 763 | list_del_init(&child->sibling); |
| 764 | if (parent) |
| 765 | list_add(&child->sibling, &parent->children); |
| 766 | child->parent = parent; |
| 767 | |
| 768 | /* now do the debugfs renaming to reattach the child |
| 769 | to the proper parent */ |
| 770 | } |
| 771 | |
| 772 | /* Propagate rate to children */ |
| 773 | void propagate_rate(struct clk *tclk) |
| 774 | { |
| 775 | struct clk *clkp; |
| 776 | |
| 777 | list_for_each_entry(clkp, &tclk->children, sibling) { |
| 778 | if (clkp->recalc) |
| 779 | clkp->rate = clkp->recalc(clkp); |
| 780 | propagate_rate(clkp); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | static LIST_HEAD(root_clks); |
| 785 | |
| 786 | /** |
| 787 | * recalculate_root_clocks - recalculate and propagate all root clocks |
| 788 | * |
| 789 | * Recalculates all root clocks (clocks with no parent), which if the |
| 790 | * clock's .recalc is set correctly, should also propagate their rates. |
| 791 | * Called at init. |
| 792 | */ |
| 793 | void recalculate_root_clocks(void) |
| 794 | { |
| 795 | struct clk *clkp; |
| 796 | |
| 797 | list_for_each_entry(clkp, &root_clks, sibling) { |
| 798 | if (clkp->recalc) |
| 799 | clkp->rate = clkp->recalc(clkp); |
| 800 | propagate_rate(clkp); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * clk_preinit - initialize any fields in the struct clk before clk init |
| 806 | * @clk: struct clk * to initialize |
| 807 | * |
| 808 | * Initialize any struct clk fields needed before normal clk initialization |
| 809 | * can run. No return value. |
| 810 | */ |
| 811 | void clk_preinit(struct clk *clk) |
| 812 | { |
| 813 | INIT_LIST_HEAD(&clk->children); |
| 814 | } |
| 815 | |
| 816 | int clk_register(struct clk *clk) |
| 817 | { |
| 818 | if (clk == NULL || IS_ERR(clk)) |
| 819 | return -EINVAL; |
| 820 | |
| 821 | /* |
| 822 | * trap out already registered clocks |
| 823 | */ |
| 824 | if (clk->node.next || clk->node.prev) |
| 825 | return 0; |
| 826 | |
| 827 | mutex_lock(&clocks_mutex); |
| 828 | if (clk->parent) |
| 829 | list_add(&clk->sibling, &clk->parent->children); |
| 830 | else |
| 831 | list_add(&clk->sibling, &root_clks); |
| 832 | |
| 833 | list_add(&clk->node, &clocks); |
| 834 | if (clk->init) |
| 835 | clk->init(clk); |
| 836 | mutex_unlock(&clocks_mutex); |
| 837 | |
| 838 | return 0; |
| 839 | } |
| 840 | EXPORT_SYMBOL(clk_register); |
| 841 | |
| 842 | void clk_unregister(struct clk *clk) |
| 843 | { |
| 844 | if (clk == NULL || IS_ERR(clk)) |
| 845 | return; |
| 846 | |
| 847 | mutex_lock(&clocks_mutex); |
| 848 | list_del(&clk->sibling); |
| 849 | list_del(&clk->node); |
| 850 | mutex_unlock(&clocks_mutex); |
| 851 | } |
| 852 | EXPORT_SYMBOL(clk_unregister); |
| 853 | |
| 854 | void clk_enable_init_clocks(void) |
| 855 | { |
| 856 | struct clk *clkp; |
| 857 | |
| 858 | list_for_each_entry(clkp, &clocks, node) |
| 859 | if (clkp->flags & ENABLE_ON_INIT) |
| 860 | clk_enable(clkp); |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * omap_clk_get_by_name - locate OMAP struct clk by its name |
| 865 | * @name: name of the struct clk to locate |
| 866 | * |
| 867 | * Locate an OMAP struct clk by its name. Assumes that struct clk |
| 868 | * names are unique. Returns NULL if not found or a pointer to the |
| 869 | * struct clk if found. |
| 870 | */ |
| 871 | struct clk *omap_clk_get_by_name(const char *name) |
| 872 | { |
| 873 | struct clk *c; |
| 874 | struct clk *ret = NULL; |
| 875 | |
| 876 | mutex_lock(&clocks_mutex); |
| 877 | |
| 878 | list_for_each_entry(c, &clocks, node) { |
| 879 | if (!strcmp(c->name, name)) { |
| 880 | ret = c; |
| 881 | break; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | mutex_unlock(&clocks_mutex); |
| 886 | |
| 887 | return ret; |
| 888 | } |
| 889 | |
| 890 | int omap_clk_enable_autoidle_all(void) |
| 891 | { |
| 892 | struct clk *c; |
| 893 | unsigned long flags; |
| 894 | |
| 895 | spin_lock_irqsave(&clockfw_lock, flags); |
| 896 | |
| 897 | list_for_each_entry(c, &clocks, node) |
| 898 | if (c->ops->allow_idle) |
| 899 | c->ops->allow_idle(c); |
| 900 | |
| 901 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | int omap_clk_disable_autoidle_all(void) |
| 907 | { |
| 908 | struct clk *c; |
| 909 | unsigned long flags; |
| 910 | |
| 911 | spin_lock_irqsave(&clockfw_lock, flags); |
| 912 | |
| 913 | list_for_each_entry(c, &clocks, node) |
| 914 | if (c->ops->deny_idle) |
| 915 | c->ops->deny_idle(c); |
| 916 | |
| 917 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 918 | |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Low level helpers |
| 924 | */ |
| 925 | static int clkll_enable_null(struct clk *clk) |
| 926 | { |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | static void clkll_disable_null(struct clk *clk) |
| 931 | { |
| 932 | } |
| 933 | |
| 934 | const struct clkops clkops_null = { |
| 935 | .enable = clkll_enable_null, |
| 936 | .disable = clkll_disable_null, |
| 937 | }; |
| 938 | |
| 939 | /* |
| 940 | * Dummy clock |
| 941 | * |
| 942 | * Used for clock aliases that are needed on some OMAPs, but not others |
| 943 | */ |
| 944 | struct clk dummy_ck = { |
| 945 | .name = "dummy", |
| 946 | .ops = &clkops_null, |
| 947 | }; |
| 948 | |
| 949 | /* |
| 950 | * |
| 951 | */ |
| 952 | |
| 953 | #ifdef CONFIG_OMAP_RESET_CLOCKS |
| 954 | /* |
| 955 | * Disable any unused clocks left on by the bootloader |
| 956 | */ |
| 957 | static int __init clk_disable_unused(void) |
| 958 | { |
| 959 | struct clk *ck; |
| 960 | unsigned long flags; |
| 961 | |
| 962 | pr_info("clock: disabling unused clocks to save power\n"); |
| 963 | |
| 964 | spin_lock_irqsave(&clockfw_lock, flags); |
| 965 | list_for_each_entry(ck, &clocks, node) { |
| 966 | if (ck->ops == &clkops_null) |
| 967 | continue; |
| 968 | |
| 969 | if (ck->usecount > 0 || !ck->enable_reg) |
| 970 | continue; |
| 971 | |
| 972 | omap1_clk_disable_unused(ck); |
| 973 | } |
| 974 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 975 | |
| 976 | return 0; |
| 977 | } |
| 978 | late_initcall(clk_disable_unused); |
| 979 | late_initcall(omap_clk_enable_autoidle_all); |
| 980 | #endif |
| 981 | |
| 982 | #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) |
| 983 | /* |
| 984 | * debugfs support to trace clock tree hierarchy and attributes |
| 985 | */ |
| 986 | |
| 987 | #include <linux/debugfs.h> |
| 988 | #include <linux/seq_file.h> |
| 989 | |
| 990 | static struct dentry *clk_debugfs_root; |
| 991 | |
| 992 | static int clk_dbg_show_summary(struct seq_file *s, void *unused) |
| 993 | { |
| 994 | struct clk *c; |
| 995 | struct clk *pa; |
| 996 | |
| 997 | mutex_lock(&clocks_mutex); |
| 998 | seq_printf(s, "%-30s %-30s %-10s %s\n", |
| 999 | "clock-name", "parent-name", "rate", "use-count"); |
| 1000 | |
| 1001 | list_for_each_entry(c, &clocks, node) { |
| 1002 | pa = c->parent; |
| 1003 | seq_printf(s, "%-30s %-30s %-10lu %d\n", |
| 1004 | c->name, pa ? pa->name : "none", c->rate, |
| 1005 | c->usecount); |
| 1006 | } |
| 1007 | mutex_unlock(&clocks_mutex); |
| 1008 | |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | static int clk_dbg_open(struct inode *inode, struct file *file) |
| 1013 | { |
| 1014 | return single_open(file, clk_dbg_show_summary, inode->i_private); |
| 1015 | } |
| 1016 | |
| 1017 | static const struct file_operations debug_clock_fops = { |
| 1018 | .open = clk_dbg_open, |
| 1019 | .read = seq_read, |
| 1020 | .llseek = seq_lseek, |
| 1021 | .release = single_release, |
| 1022 | }; |
| 1023 | |
| 1024 | static int clk_debugfs_register_one(struct clk *c) |
| 1025 | { |
| 1026 | int err; |
| 1027 | struct dentry *d; |
| 1028 | struct clk *pa = c->parent; |
| 1029 | |
| 1030 | d = debugfs_create_dir(c->name, pa ? pa->dent : clk_debugfs_root); |
| 1031 | if (!d) |
| 1032 | return -ENOMEM; |
| 1033 | c->dent = d; |
| 1034 | |
| 1035 | d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount); |
| 1036 | if (!d) { |
| 1037 | err = -ENOMEM; |
| 1038 | goto err_out; |
| 1039 | } |
| 1040 | d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate); |
| 1041 | if (!d) { |
| 1042 | err = -ENOMEM; |
| 1043 | goto err_out; |
| 1044 | } |
| 1045 | d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags); |
| 1046 | if (!d) { |
| 1047 | err = -ENOMEM; |
| 1048 | goto err_out; |
| 1049 | } |
| 1050 | return 0; |
| 1051 | |
| 1052 | err_out: |
| 1053 | debugfs_remove_recursive(c->dent); |
| 1054 | return err; |
| 1055 | } |
| 1056 | |
| 1057 | static int clk_debugfs_register(struct clk *c) |
| 1058 | { |
| 1059 | int err; |
| 1060 | struct clk *pa = c->parent; |
| 1061 | |
| 1062 | if (pa && !pa->dent) { |
| 1063 | err = clk_debugfs_register(pa); |
| 1064 | if (err) |
| 1065 | return err; |
| 1066 | } |
| 1067 | |
| 1068 | if (!c->dent) { |
| 1069 | err = clk_debugfs_register_one(c); |
| 1070 | if (err) |
| 1071 | return err; |
| 1072 | } |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | static int __init clk_debugfs_init(void) |
| 1077 | { |
| 1078 | struct clk *c; |
| 1079 | struct dentry *d; |
| 1080 | int err; |
| 1081 | |
| 1082 | d = debugfs_create_dir("clock", NULL); |
| 1083 | if (!d) |
| 1084 | return -ENOMEM; |
| 1085 | clk_debugfs_root = d; |
| 1086 | |
| 1087 | list_for_each_entry(c, &clocks, node) { |
| 1088 | err = clk_debugfs_register(c); |
| 1089 | if (err) |
| 1090 | goto err_out; |
| 1091 | } |
| 1092 | |
| 1093 | d = debugfs_create_file("summary", S_IRUGO, |
| 1094 | d, NULL, &debug_clock_fops); |
| 1095 | if (!d) |
| 1096 | return -ENOMEM; |
| 1097 | |
| 1098 | return 0; |
| 1099 | err_out: |
| 1100 | debugfs_remove_recursive(clk_debugfs_root); |
| 1101 | return err; |
| 1102 | } |
| 1103 | late_initcall(clk_debugfs_init); |
| 1104 | |
| 1105 | #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */ |