Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * OMAP3/4 - specific DPLL control functions |
| 3 | * |
Richard Woodruff | 358965d | 2010-02-22 22:09:08 -0700 | [diff] [blame] | 4 | * Copyright (C) 2009-2010 Texas Instruments, Inc. |
| 5 | * Copyright (C) 2009-2010 Nokia Corporation |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 6 | * |
| 7 | * Written by Paul Walmsley |
Richard Woodruff | 358965d | 2010-02-22 22:09:08 -0700 | [diff] [blame] | 8 | * Testing and integration fixes by Jouni Högander |
| 9 | * |
| 10 | * 36xx support added by Vishwanath BS, Richard Woodruff, and Nishanth |
| 11 | * Menon |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 12 | * |
| 13 | * Parts of this code are based on code written by |
| 14 | * Richard Woodruff, Tony Lindgren, Tuukka Tikkanen, Karthik Dasu |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License version 2 as |
| 18 | * published by the Free Software Foundation. |
| 19 | */ |
| 20 | |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 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> |
| 26 | #include <linux/clk.h> |
| 27 | #include <linux/io.h> |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 28 | #include <linux/bitops.h> |
Jean-Christop PLAGNIOL-VILLARD | 6d803ba | 2010-11-17 10:04:33 +0100 | [diff] [blame] | 29 | #include <linux/clkdev.h> |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 30 | |
Tony Lindgren | dbc0416 | 2012-08-31 10:59:07 -0700 | [diff] [blame] | 31 | #include "soc.h" |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 32 | #include "clock.h" |
Paul Walmsley | 59fb659 | 2010-12-21 15:30:55 -0700 | [diff] [blame] | 33 | #include "cm2xxx_3xxx.h" |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 34 | #include "cm-regbits-34xx.h" |
| 35 | |
| 36 | /* CM_AUTOIDLE_PLL*.AUTO_* bit values */ |
| 37 | #define DPLL_AUTOIDLE_DISABLE 0x0 |
| 38 | #define DPLL_AUTOIDLE_LOW_POWER_STOP 0x1 |
| 39 | |
| 40 | #define MAX_DPLL_WAIT_TRIES 1000000 |
| 41 | |
Paul Walmsley | 60c3f65 | 2010-01-26 20:13:11 -0700 | [diff] [blame] | 42 | /* Private functions */ |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 43 | |
| 44 | /* _omap3_dpll_write_clken - write clken_bits arg to a DPLL's enable bits */ |
| 45 | static void _omap3_dpll_write_clken(struct clk *clk, u8 clken_bits) |
| 46 | { |
| 47 | const struct dpll_data *dd; |
| 48 | u32 v; |
| 49 | |
| 50 | dd = clk->dpll_data; |
| 51 | |
| 52 | v = __raw_readl(dd->control_reg); |
| 53 | v &= ~dd->enable_mask; |
| 54 | v |= clken_bits << __ffs(dd->enable_mask); |
| 55 | __raw_writel(v, dd->control_reg); |
| 56 | } |
| 57 | |
| 58 | /* _omap3_wait_dpll_status: wait for a DPLL to enter a specific state */ |
| 59 | static int _omap3_wait_dpll_status(struct clk *clk, u8 state) |
| 60 | { |
| 61 | const struct dpll_data *dd; |
| 62 | int i = 0; |
| 63 | int ret = -EINVAL; |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 64 | const char *clk_name; |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 65 | |
| 66 | dd = clk->dpll_data; |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 67 | clk_name = __clk_get_name(clk); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 68 | |
| 69 | state <<= __ffs(dd->idlest_mask); |
| 70 | |
| 71 | while (((__raw_readl(dd->idlest_reg) & dd->idlest_mask) != state) && |
| 72 | i < MAX_DPLL_WAIT_TRIES) { |
| 73 | i++; |
| 74 | udelay(1); |
| 75 | } |
| 76 | |
| 77 | if (i == MAX_DPLL_WAIT_TRIES) { |
| 78 | printk(KERN_ERR "clock: %s failed transition to '%s'\n", |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 79 | clk_name, (state) ? "locked" : "bypassed"); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 80 | } else { |
| 81 | pr_debug("clock: %s transition to '%s' in %d loops\n", |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 82 | clk_name, (state) ? "locked" : "bypassed", i); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 83 | |
| 84 | ret = 0; |
| 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | /* From 3430 TRM ES2 4.7.6.2 */ |
| 91 | static u16 _omap3_dpll_compute_freqsel(struct clk *clk, u8 n) |
| 92 | { |
| 93 | unsigned long fint; |
| 94 | u16 f = 0; |
| 95 | |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 96 | fint = __clk_get_rate(clk->dpll_data->clk_ref) / n; |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 97 | |
| 98 | pr_debug("clock: fint is %lu\n", fint); |
| 99 | |
| 100 | if (fint >= 750000 && fint <= 1000000) |
| 101 | f = 0x3; |
| 102 | else if (fint > 1000000 && fint <= 1250000) |
| 103 | f = 0x4; |
| 104 | else if (fint > 1250000 && fint <= 1500000) |
| 105 | f = 0x5; |
| 106 | else if (fint > 1500000 && fint <= 1750000) |
| 107 | f = 0x6; |
| 108 | else if (fint > 1750000 && fint <= 2100000) |
| 109 | f = 0x7; |
| 110 | else if (fint > 7500000 && fint <= 10000000) |
| 111 | f = 0xB; |
| 112 | else if (fint > 10000000 && fint <= 12500000) |
| 113 | f = 0xC; |
| 114 | else if (fint > 12500000 && fint <= 15000000) |
| 115 | f = 0xD; |
| 116 | else if (fint > 15000000 && fint <= 17500000) |
| 117 | f = 0xE; |
| 118 | else if (fint > 17500000 && fint <= 21000000) |
| 119 | f = 0xF; |
| 120 | else |
| 121 | pr_debug("clock: unknown freqsel setting for %d\n", n); |
| 122 | |
| 123 | return f; |
| 124 | } |
| 125 | |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 126 | /* |
| 127 | * _omap3_noncore_dpll_lock - instruct a DPLL to lock and wait for readiness |
| 128 | * @clk: pointer to a DPLL struct clk |
| 129 | * |
| 130 | * Instructs a non-CORE DPLL to lock. Waits for the DPLL to report |
| 131 | * readiness before returning. Will save and restore the DPLL's |
| 132 | * autoidle state across the enable, per the CDP code. If the DPLL |
| 133 | * locked successfully, return 0; if the DPLL did not lock in the time |
| 134 | * allotted, or DPLL3 was passed in, return -EINVAL. |
| 135 | */ |
| 136 | static int _omap3_noncore_dpll_lock(struct clk *clk) |
| 137 | { |
Vikram Pandita | 55ffe16 | 2012-07-04 05:00:44 -0600 | [diff] [blame] | 138 | const struct dpll_data *dd; |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 139 | u8 ai; |
Vikram Pandita | 55ffe16 | 2012-07-04 05:00:44 -0600 | [diff] [blame] | 140 | u8 state = 1; |
| 141 | int r = 0; |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 142 | |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 143 | pr_debug("clock: locking DPLL %s\n", __clk_get_name(clk)); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 144 | |
Vikram Pandita | 55ffe16 | 2012-07-04 05:00:44 -0600 | [diff] [blame] | 145 | dd = clk->dpll_data; |
| 146 | state <<= __ffs(dd->idlest_mask); |
| 147 | |
| 148 | /* Check if already locked */ |
| 149 | if ((__raw_readl(dd->idlest_reg) & dd->idlest_mask) == state) |
| 150 | goto done; |
| 151 | |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 152 | ai = omap3_dpll_autoidle_read(clk); |
| 153 | |
Vaibhav Bedia | d76316f | 2012-05-07 23:55:30 -0600 | [diff] [blame] | 154 | if (ai) |
| 155 | omap3_dpll_deny_idle(clk); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 156 | |
| 157 | _omap3_dpll_write_clken(clk, DPLL_LOCKED); |
| 158 | |
| 159 | r = _omap3_wait_dpll_status(clk, 1); |
| 160 | |
| 161 | if (ai) |
| 162 | omap3_dpll_allow_idle(clk); |
| 163 | |
Vikram Pandita | 55ffe16 | 2012-07-04 05:00:44 -0600 | [diff] [blame] | 164 | done: |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 165 | return r; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * _omap3_noncore_dpll_bypass - instruct a DPLL to bypass and wait for readiness |
| 170 | * @clk: pointer to a DPLL struct clk |
| 171 | * |
| 172 | * Instructs a non-CORE DPLL to enter low-power bypass mode. In |
| 173 | * bypass mode, the DPLL's rate is set equal to its parent clock's |
| 174 | * rate. Waits for the DPLL to report readiness before returning. |
| 175 | * Will save and restore the DPLL's autoidle state across the enable, |
| 176 | * per the CDP code. If the DPLL entered bypass mode successfully, |
| 177 | * return 0; if the DPLL did not enter bypass in the time allotted, or |
| 178 | * DPLL3 was passed in, or the DPLL does not support low-power bypass, |
| 179 | * return -EINVAL. |
| 180 | */ |
| 181 | static int _omap3_noncore_dpll_bypass(struct clk *clk) |
| 182 | { |
| 183 | int r; |
| 184 | u8 ai; |
| 185 | |
| 186 | if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS))) |
| 187 | return -EINVAL; |
| 188 | |
| 189 | pr_debug("clock: configuring DPLL %s for low-power bypass\n", |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 190 | __clk_get_name(clk)); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 191 | |
| 192 | ai = omap3_dpll_autoidle_read(clk); |
| 193 | |
| 194 | _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_BYPASS); |
| 195 | |
| 196 | r = _omap3_wait_dpll_status(clk, 0); |
| 197 | |
| 198 | if (ai) |
| 199 | omap3_dpll_allow_idle(clk); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 200 | |
| 201 | return r; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * _omap3_noncore_dpll_stop - instruct a DPLL to stop |
| 206 | * @clk: pointer to a DPLL struct clk |
| 207 | * |
| 208 | * Instructs a non-CORE DPLL to enter low-power stop. Will save and |
| 209 | * restore the DPLL's autoidle state across the stop, per the CDP |
| 210 | * code. If DPLL3 was passed in, or the DPLL does not support |
| 211 | * low-power stop, return -EINVAL; otherwise, return 0. |
| 212 | */ |
| 213 | static int _omap3_noncore_dpll_stop(struct clk *clk) |
| 214 | { |
| 215 | u8 ai; |
| 216 | |
| 217 | if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_STOP))) |
| 218 | return -EINVAL; |
| 219 | |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 220 | pr_debug("clock: stopping DPLL %s\n", __clk_get_name(clk)); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 221 | |
| 222 | ai = omap3_dpll_autoidle_read(clk); |
| 223 | |
| 224 | _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_STOP); |
| 225 | |
| 226 | if (ai) |
| 227 | omap3_dpll_allow_idle(clk); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Richard Woodruff | 358965d | 2010-02-22 22:09:08 -0700 | [diff] [blame] | 232 | /** |
Jon Hunter | a36795c | 2010-12-21 21:31:43 -0700 | [diff] [blame] | 233 | * _lookup_dco - Lookup DCO used by j-type DPLL |
Richard Woodruff | 358965d | 2010-02-22 22:09:08 -0700 | [diff] [blame] | 234 | * @clk: pointer to a DPLL struct clk |
| 235 | * @dco: digital control oscillator selector |
Jon Hunter | a36795c | 2010-12-21 21:31:43 -0700 | [diff] [blame] | 236 | * @m: DPLL multiplier to set |
| 237 | * @n: DPLL divider to set |
| 238 | * |
| 239 | * See 36xx TRM section 3.5.3.3.3.2 "Type B DPLL (Low-Jitter)" |
| 240 | * |
| 241 | * XXX This code is not needed for 3430/AM35xx; can it be optimized |
| 242 | * out in non-multi-OMAP builds for those chips? |
| 243 | */ |
| 244 | static void _lookup_dco(struct clk *clk, u8 *dco, u16 m, u8 n) |
| 245 | { |
| 246 | unsigned long fint, clkinp; /* watch out for overflow */ |
| 247 | |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 248 | clkinp = __clk_get_rate(__clk_get_parent(clk)); |
Jon Hunter | a36795c | 2010-12-21 21:31:43 -0700 | [diff] [blame] | 249 | fint = (clkinp / n) * m; |
| 250 | |
| 251 | if (fint < 1000000000) |
| 252 | *dco = 2; |
| 253 | else |
| 254 | *dco = 4; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * _lookup_sddiv - Calculate sigma delta divider for j-type DPLL |
| 259 | * @clk: pointer to a DPLL struct clk |
Richard Woodruff | 358965d | 2010-02-22 22:09:08 -0700 | [diff] [blame] | 260 | * @sd_div: target sigma-delta divider |
| 261 | * @m: DPLL multiplier to set |
| 262 | * @n: DPLL divider to set |
| 263 | * |
| 264 | * See 36xx TRM section 3.5.3.3.3.2 "Type B DPLL (Low-Jitter)" |
| 265 | * |
| 266 | * XXX This code is not needed for 3430/AM35xx; can it be optimized |
| 267 | * out in non-multi-OMAP builds for those chips? |
| 268 | */ |
Jon Hunter | a36795c | 2010-12-21 21:31:43 -0700 | [diff] [blame] | 269 | static void _lookup_sddiv(struct clk *clk, u8 *sd_div, u16 m, u8 n) |
Richard Woodruff | 358965d | 2010-02-22 22:09:08 -0700 | [diff] [blame] | 270 | { |
Jon Hunter | a36795c | 2010-12-21 21:31:43 -0700 | [diff] [blame] | 271 | unsigned long clkinp, sd; /* watch out for overflow */ |
Richard Woodruff | 358965d | 2010-02-22 22:09:08 -0700 | [diff] [blame] | 272 | int mod1, mod2; |
| 273 | |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 274 | clkinp = __clk_get_rate(__clk_get_parent(clk)); |
Richard Woodruff | 358965d | 2010-02-22 22:09:08 -0700 | [diff] [blame] | 275 | |
Richard Woodruff | 358965d | 2010-02-22 22:09:08 -0700 | [diff] [blame] | 276 | /* |
| 277 | * target sigma-delta to near 250MHz |
| 278 | * sd = ceil[(m/(n+1)) * (clkinp_MHz / 250)] |
| 279 | */ |
| 280 | clkinp /= 100000; /* shift from MHz to 10*Hz for 38.4 and 19.2 */ |
| 281 | mod1 = (clkinp * m) % (250 * n); |
| 282 | sd = (clkinp * m) / (250 * n); |
| 283 | mod2 = sd % 10; |
| 284 | sd /= 10; |
| 285 | |
| 286 | if (mod1 || mod2) |
| 287 | sd++; |
| 288 | *sd_div = sd; |
| 289 | } |
| 290 | |
Paul Walmsley | 60c3f65 | 2010-01-26 20:13:11 -0700 | [diff] [blame] | 291 | /* |
| 292 | * _omap3_noncore_dpll_program - set non-core DPLL M,N values directly |
| 293 | * @clk: struct clk * of DPLL to set |
| 294 | * @m: DPLL multiplier to set |
| 295 | * @n: DPLL divider to set |
| 296 | * @freqsel: FREQSEL value to set |
| 297 | * |
| 298 | * Program the DPLL with the supplied M, N values, and wait for the DPLL to |
| 299 | * lock.. Returns -EINVAL upon error, or 0 upon success. |
| 300 | */ |
| 301 | static int omap3_noncore_dpll_program(struct clk *clk, u16 m, u8 n, u16 freqsel) |
| 302 | { |
| 303 | struct dpll_data *dd = clk->dpll_data; |
Jon Hunter | a36795c | 2010-12-21 21:31:43 -0700 | [diff] [blame] | 304 | u8 dco, sd_div; |
Paul Walmsley | 60c3f65 | 2010-01-26 20:13:11 -0700 | [diff] [blame] | 305 | u32 v; |
| 306 | |
| 307 | /* 3430 ES2 TRM: 4.7.6.9 DPLL Programming Sequence */ |
| 308 | _omap3_noncore_dpll_bypass(clk); |
| 309 | |
Vishwanath BS | 5eb75f5 | 2010-02-24 12:05:57 -0700 | [diff] [blame] | 310 | /* |
| 311 | * Set jitter correction. No jitter correction for OMAP4 and 3630 |
| 312 | * since freqsel field is no longer present |
| 313 | */ |
Vaibhav Hiremath | 78da264 | 2012-08-24 20:24:24 +0530 | [diff] [blame] | 314 | if (!soc_is_am33xx() && !cpu_is_omap44xx() && !cpu_is_omap3630()) { |
Paul Walmsley | 60c3f65 | 2010-01-26 20:13:11 -0700 | [diff] [blame] | 315 | v = __raw_readl(dd->control_reg); |
| 316 | v &= ~dd->freqsel_mask; |
| 317 | v |= freqsel << __ffs(dd->freqsel_mask); |
| 318 | __raw_writel(v, dd->control_reg); |
| 319 | } |
| 320 | |
| 321 | /* Set DPLL multiplier, divider */ |
| 322 | v = __raw_readl(dd->mult_div1_reg); |
| 323 | v &= ~(dd->mult_mask | dd->div1_mask); |
| 324 | v |= m << __ffs(dd->mult_mask); |
| 325 | v |= (n - 1) << __ffs(dd->div1_mask); |
Richard Woodruff | 358965d | 2010-02-22 22:09:08 -0700 | [diff] [blame] | 326 | |
Jon Hunter | a36795c | 2010-12-21 21:31:43 -0700 | [diff] [blame] | 327 | /* Configure dco and sd_div for dplls that have these fields */ |
| 328 | if (dd->dco_mask) { |
| 329 | _lookup_dco(clk, &dco, m, n); |
| 330 | v &= ~(dd->dco_mask); |
| 331 | v |= dco << __ffs(dd->dco_mask); |
| 332 | } |
| 333 | if (dd->sddiv_mask) { |
| 334 | _lookup_sddiv(clk, &sd_div, m, n); |
| 335 | v &= ~(dd->sddiv_mask); |
| 336 | v |= sd_div << __ffs(dd->sddiv_mask); |
Richard Woodruff | 358965d | 2010-02-22 22:09:08 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Paul Walmsley | 60c3f65 | 2010-01-26 20:13:11 -0700 | [diff] [blame] | 339 | __raw_writel(v, dd->mult_div1_reg); |
| 340 | |
| 341 | /* We let the clock framework set the other output dividers later */ |
| 342 | |
| 343 | /* REVISIT: Set ramp-up delay? */ |
| 344 | |
| 345 | _omap3_noncore_dpll_lock(clk); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | /* Public functions */ |
| 351 | |
| 352 | /** |
| 353 | * omap3_dpll_recalc - recalculate DPLL rate |
| 354 | * @clk: DPLL struct clk |
| 355 | * |
| 356 | * Recalculate and propagate the DPLL rate. |
| 357 | */ |
| 358 | unsigned long omap3_dpll_recalc(struct clk *clk) |
| 359 | { |
| 360 | return omap2_get_dpll_rate(clk); |
| 361 | } |
| 362 | |
| 363 | /* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */ |
| 364 | |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 365 | /** |
| 366 | * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode |
| 367 | * @clk: pointer to a DPLL struct clk |
| 368 | * |
| 369 | * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock. |
| 370 | * The choice of modes depends on the DPLL's programmed rate: if it is |
| 371 | * the same as the DPLL's parent clock, it will enter bypass; |
| 372 | * otherwise, it will enter lock. This code will wait for the DPLL to |
| 373 | * indicate readiness before returning, unless the DPLL takes too long |
| 374 | * to enter the target state. Intended to be used as the struct clk's |
| 375 | * enable function. If DPLL3 was passed in, or the DPLL does not |
| 376 | * support low-power stop, or if the DPLL took too long to enter |
| 377 | * bypass or lock, return -EINVAL; otherwise, return 0. |
| 378 | */ |
| 379 | int omap3_noncore_dpll_enable(struct clk *clk) |
| 380 | { |
| 381 | int r; |
| 382 | struct dpll_data *dd; |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 383 | struct clk *parent; |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 384 | |
| 385 | dd = clk->dpll_data; |
| 386 | if (!dd) |
| 387 | return -EINVAL; |
| 388 | |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 389 | parent = __clk_get_parent(clk); |
| 390 | |
| 391 | if (__clk_get_rate(clk) == __clk_get_rate(dd->clk_bypass)) { |
| 392 | WARN_ON(parent != dd->clk_bypass); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 393 | r = _omap3_noncore_dpll_bypass(clk); |
| 394 | } else { |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 395 | WARN_ON(parent != dd->clk_ref); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 396 | r = _omap3_noncore_dpll_lock(clk); |
| 397 | } |
| 398 | /* |
| 399 | *FIXME: this is dubious - if clk->rate has changed, what about |
| 400 | * propagating? |
| 401 | */ |
| 402 | if (!r) |
Jon Hunter | 49642ac | 2011-10-07 00:53:01 -0600 | [diff] [blame] | 403 | clk->rate = (clk->recalc) ? clk->recalc(clk) : |
| 404 | omap2_get_dpll_rate(clk); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 405 | |
| 406 | return r; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * omap3_noncore_dpll_disable - instruct a DPLL to enter low-power stop |
| 411 | * @clk: pointer to a DPLL struct clk |
| 412 | * |
| 413 | * Instructs a non-CORE DPLL to enter low-power stop. This function is |
| 414 | * intended for use in struct clkops. No return value. |
| 415 | */ |
| 416 | void omap3_noncore_dpll_disable(struct clk *clk) |
| 417 | { |
| 418 | _omap3_noncore_dpll_stop(clk); |
| 419 | } |
| 420 | |
| 421 | |
| 422 | /* Non-CORE DPLL rate set code */ |
| 423 | |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 424 | /** |
| 425 | * omap3_noncore_dpll_set_rate - set non-core DPLL rate |
| 426 | * @clk: struct clk * of DPLL to set |
| 427 | * @rate: rounded target rate |
| 428 | * |
| 429 | * Set the DPLL CLKOUT to the target rate. If the DPLL can enter |
| 430 | * low-power bypass, and the target rate is the bypass source clock |
| 431 | * rate, then configure the DPLL for bypass. Otherwise, round the |
| 432 | * target rate if it hasn't been done already, then program and lock |
| 433 | * the DPLL. Returns -EINVAL upon error, or 0 upon success. |
| 434 | */ |
| 435 | int omap3_noncore_dpll_set_rate(struct clk *clk, unsigned long rate) |
| 436 | { |
| 437 | struct clk *new_parent = NULL; |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 438 | unsigned long hw_rate, bypass_rate; |
Rajendra Nayak | 16975a7 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 439 | u16 freqsel = 0; |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 440 | struct dpll_data *dd; |
| 441 | int ret; |
| 442 | |
| 443 | if (!clk || !rate) |
| 444 | return -EINVAL; |
| 445 | |
| 446 | dd = clk->dpll_data; |
| 447 | if (!dd) |
| 448 | return -EINVAL; |
| 449 | |
Jon Hunter | 49642ac | 2011-10-07 00:53:01 -0600 | [diff] [blame] | 450 | hw_rate = (clk->recalc) ? clk->recalc(clk) : omap2_get_dpll_rate(clk); |
| 451 | if (rate == hw_rate) |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 452 | return 0; |
| 453 | |
| 454 | /* |
| 455 | * Ensure both the bypass and ref clocks are enabled prior to |
| 456 | * doing anything; we need the bypass clock running to reprogram |
| 457 | * the DPLL. |
| 458 | */ |
| 459 | omap2_clk_enable(dd->clk_bypass); |
| 460 | omap2_clk_enable(dd->clk_ref); |
| 461 | |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 462 | bypass_rate = __clk_get_rate(dd->clk_bypass); |
| 463 | if (bypass_rate == rate && |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 464 | (clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS))) { |
| 465 | pr_debug("clock: %s: set rate: entering bypass.\n", clk->name); |
| 466 | |
| 467 | ret = _omap3_noncore_dpll_bypass(clk); |
| 468 | if (!ret) |
| 469 | new_parent = dd->clk_bypass; |
| 470 | } else { |
| 471 | if (dd->last_rounded_rate != rate) |
Mike Turquette | 273a1ce | 2011-10-07 00:53:00 -0600 | [diff] [blame] | 472 | rate = clk->round_rate(clk, rate); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 473 | |
| 474 | if (dd->last_rounded_rate == 0) |
| 475 | return -EINVAL; |
| 476 | |
Vishwanath BS | 5eb75f5 | 2010-02-24 12:05:57 -0700 | [diff] [blame] | 477 | /* No freqsel on OMAP4 and OMAP3630 */ |
Vaibhav Hiremath | 78da264 | 2012-08-24 20:24:24 +0530 | [diff] [blame] | 478 | if (!soc_is_am33xx() && !cpu_is_omap44xx() && !cpu_is_omap3630()) { |
Rajendra Nayak | 16975a7 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 479 | freqsel = _omap3_dpll_compute_freqsel(clk, |
| 480 | dd->last_rounded_n); |
| 481 | if (!freqsel) |
| 482 | WARN_ON(1); |
| 483 | } |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 484 | |
| 485 | pr_debug("clock: %s: set rate: locking rate to %lu.\n", |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 486 | __clk_get_name(clk), rate); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 487 | |
| 488 | ret = omap3_noncore_dpll_program(clk, dd->last_rounded_m, |
| 489 | dd->last_rounded_n, freqsel); |
| 490 | if (!ret) |
| 491 | new_parent = dd->clk_ref; |
| 492 | } |
| 493 | if (!ret) { |
| 494 | /* |
Uwe Kleine-König | 732bee7 | 2010-06-11 12:16:59 +0200 | [diff] [blame] | 495 | * Switch the parent clock in the hierarchy, and make sure |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 496 | * that the new parent's usecount is correct. Note: we |
| 497 | * enable the new parent before disabling the old to avoid |
| 498 | * any unnecessary hardware disable->enable transitions. |
| 499 | */ |
| 500 | if (clk->usecount) { |
| 501 | omap2_clk_enable(new_parent); |
| 502 | omap2_clk_disable(clk->parent); |
| 503 | } |
| 504 | clk_reparent(clk, new_parent); |
| 505 | clk->rate = rate; |
| 506 | } |
| 507 | omap2_clk_disable(dd->clk_ref); |
| 508 | omap2_clk_disable(dd->clk_bypass); |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | /* DPLL autoidle read/set code */ |
| 514 | |
| 515 | /** |
| 516 | * omap3_dpll_autoidle_read - read a DPLL's autoidle bits |
| 517 | * @clk: struct clk * of the DPLL to read |
| 518 | * |
| 519 | * Return the DPLL's autoidle bits, shifted down to bit 0. Returns |
| 520 | * -EINVAL if passed a null pointer or if the struct clk does not |
| 521 | * appear to refer to a DPLL. |
| 522 | */ |
| 523 | u32 omap3_dpll_autoidle_read(struct clk *clk) |
| 524 | { |
| 525 | const struct dpll_data *dd; |
| 526 | u32 v; |
| 527 | |
| 528 | if (!clk || !clk->dpll_data) |
| 529 | return -EINVAL; |
| 530 | |
| 531 | dd = clk->dpll_data; |
| 532 | |
Vaibhav Bedia | d76316f | 2012-05-07 23:55:30 -0600 | [diff] [blame] | 533 | if (!dd->autoidle_reg) |
| 534 | return -EINVAL; |
| 535 | |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 536 | v = __raw_readl(dd->autoidle_reg); |
| 537 | v &= dd->autoidle_mask; |
| 538 | v >>= __ffs(dd->autoidle_mask); |
| 539 | |
| 540 | return v; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * omap3_dpll_allow_idle - enable DPLL autoidle bits |
| 545 | * @clk: struct clk * of the DPLL to operate on |
| 546 | * |
| 547 | * Enable DPLL automatic idle control. This automatic idle mode |
| 548 | * switching takes effect only when the DPLL is locked, at least on |
| 549 | * OMAP3430. The DPLL will enter low-power stop when its downstream |
| 550 | * clocks are gated. No return value. |
| 551 | */ |
| 552 | void omap3_dpll_allow_idle(struct clk *clk) |
| 553 | { |
| 554 | const struct dpll_data *dd; |
| 555 | u32 v; |
| 556 | |
| 557 | if (!clk || !clk->dpll_data) |
| 558 | return; |
| 559 | |
| 560 | dd = clk->dpll_data; |
| 561 | |
Vaibhav Bedia | d76316f | 2012-05-07 23:55:30 -0600 | [diff] [blame] | 562 | if (!dd->autoidle_reg) { |
| 563 | pr_debug("clock: DPLL %s: autoidle not supported\n", |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 564 | __clk_get_name(clk)); |
Vaibhav Bedia | d76316f | 2012-05-07 23:55:30 -0600 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 568 | /* |
| 569 | * REVISIT: CORE DPLL can optionally enter low-power bypass |
| 570 | * by writing 0x5 instead of 0x1. Add some mechanism to |
| 571 | * optionally enter this mode. |
| 572 | */ |
| 573 | v = __raw_readl(dd->autoidle_reg); |
| 574 | v &= ~dd->autoidle_mask; |
| 575 | v |= DPLL_AUTOIDLE_LOW_POWER_STOP << __ffs(dd->autoidle_mask); |
| 576 | __raw_writel(v, dd->autoidle_reg); |
Vaibhav Bedia | d76316f | 2012-05-07 23:55:30 -0600 | [diff] [blame] | 577 | |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /** |
| 581 | * omap3_dpll_deny_idle - prevent DPLL from automatically idling |
| 582 | * @clk: struct clk * of the DPLL to operate on |
| 583 | * |
| 584 | * Disable DPLL automatic idle control. No return value. |
| 585 | */ |
| 586 | void omap3_dpll_deny_idle(struct clk *clk) |
| 587 | { |
| 588 | const struct dpll_data *dd; |
| 589 | u32 v; |
| 590 | |
| 591 | if (!clk || !clk->dpll_data) |
| 592 | return; |
| 593 | |
| 594 | dd = clk->dpll_data; |
| 595 | |
Vaibhav Bedia | d76316f | 2012-05-07 23:55:30 -0600 | [diff] [blame] | 596 | if (!dd->autoidle_reg) { |
| 597 | pr_debug("clock: DPLL %s: autoidle not supported\n", |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 598 | __clk_get_name(clk)); |
Vaibhav Bedia | d76316f | 2012-05-07 23:55:30 -0600 | [diff] [blame] | 599 | return; |
| 600 | } |
| 601 | |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 602 | v = __raw_readl(dd->autoidle_reg); |
| 603 | v &= ~dd->autoidle_mask; |
| 604 | v |= DPLL_AUTOIDLE_DISABLE << __ffs(dd->autoidle_mask); |
| 605 | __raw_writel(v, dd->autoidle_reg); |
| 606 | |
| 607 | } |
| 608 | |
| 609 | /* Clock control for DPLL outputs */ |
| 610 | |
| 611 | /** |
| 612 | * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate |
| 613 | * @clk: DPLL output struct clk |
| 614 | * |
| 615 | * Using parent clock DPLL data, look up DPLL state. If locked, set our |
| 616 | * rate to the dpll_clk * 2; otherwise, just use dpll_clk. |
| 617 | */ |
| 618 | unsigned long omap3_clkoutx2_recalc(struct clk *clk) |
| 619 | { |
| 620 | const struct dpll_data *dd; |
| 621 | unsigned long rate; |
| 622 | u32 v; |
| 623 | struct clk *pclk; |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 624 | unsigned long parent_rate; |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 625 | |
| 626 | /* Walk up the parents of clk, looking for a DPLL */ |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 627 | pclk = __clk_get_parent(clk); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 628 | while (pclk && !pclk->dpll_data) |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 629 | pclk = __clk_get_parent(pclk); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 630 | |
Paul Walmsley | a032d33 | 2012-08-03 09:21:10 -0600 | [diff] [blame] | 631 | /* clk does not have a DPLL as a parent? error in the clock data */ |
| 632 | if (!pclk) { |
| 633 | WARN_ON(1); |
| 634 | return 0; |
| 635 | } |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 636 | |
| 637 | dd = pclk->dpll_data; |
| 638 | |
| 639 | WARN_ON(!dd->enable_mask); |
| 640 | |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 641 | parent_rate = __clk_get_rate(__clk_get_parent(clk)); |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 642 | v = __raw_readl(dd->control_reg) & dd->enable_mask; |
| 643 | v >>= __ffs(dd->enable_mask); |
Richard Woodruff | 358965d | 2010-02-22 22:09:08 -0700 | [diff] [blame] | 644 | if ((v != OMAP3XXX_EN_DPLL_LOCKED) || (dd->flags & DPLL_J_TYPE)) |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 645 | rate = parent_rate; |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 646 | else |
Rajendra Nayak | 5dcc3b9 | 2012-09-22 02:24:17 -0600 | [diff] [blame] | 647 | rate = parent_rate * 2; |
Rajendra Nayak | a1391d2 | 2009-12-08 18:47:16 -0700 | [diff] [blame] | 648 | return rate; |
| 649 | } |
Vaibhav Hiremath | 353cec4 | 2012-07-05 08:05:15 -0700 | [diff] [blame] | 650 | |
| 651 | /* OMAP3/4 non-CORE DPLL clkops */ |
| 652 | |
| 653 | const struct clkops clkops_omap3_noncore_dpll_ops = { |
| 654 | .enable = omap3_noncore_dpll_enable, |
| 655 | .disable = omap3_noncore_dpll_disable, |
| 656 | .allow_idle = omap3_dpll_allow_idle, |
| 657 | .deny_idle = omap3_dpll_deny_idle, |
| 658 | }; |
| 659 | |
| 660 | const struct clkops clkops_omap3_core_dpll_ops = { |
| 661 | .allow_idle = omap3_dpll_allow_idle, |
| 662 | .deny_idle = omap3_dpll_deny_idle, |
| 663 | }; |