blob: 22d77a3312879e3b62e29fdcdd95fa22f3843200 [file] [log] [blame]
Rajendra Nayaka1391d22009-12-08 18:47:16 -07001/*
2 * OMAP3/4 - specific DPLL control functions
3 *
Richard Woodruff358965d2010-02-22 22:09:08 -07004 * Copyright (C) 2009-2010 Texas Instruments, Inc.
5 * Copyright (C) 2009-2010 Nokia Corporation
Rajendra Nayaka1391d22009-12-08 18:47:16 -07006 *
7 * Written by Paul Walmsley
Richard Woodruff358965d2010-02-22 22:09:08 -07008 * Testing and integration fixes by Jouni Högander
9 *
10 * 36xx support added by Vishwanath BS, Richard Woodruff, and Nishanth
11 * Menon
Rajendra Nayaka1391d22009-12-08 18:47:16 -070012 *
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 Nayaka1391d22009-12-08 18:47:16 -070021#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 Nayaka1391d22009-12-08 18:47:16 -070028#include <linux/bitops.h>
Jean-Christop PLAGNIOL-VILLARD6d803ba2010-11-17 10:04:33 +010029#include <linux/clkdev.h>
Tero Kristo0565fb12015-03-03 13:27:48 +020030#include <linux/clk/ti.h>
Rajendra Nayaka1391d22009-12-08 18:47:16 -070031
Rajendra Nayaka1391d22009-12-08 18:47:16 -070032#include "clock.h"
Rajendra Nayaka1391d22009-12-08 18:47:16 -070033
34/* CM_AUTOIDLE_PLL*.AUTO_* bit values */
35#define DPLL_AUTOIDLE_DISABLE 0x0
36#define DPLL_AUTOIDLE_LOW_POWER_STOP 0x1
37
38#define MAX_DPLL_WAIT_TRIES 1000000
39
Tero Kristo0565fb12015-03-03 13:27:48 +020040#define OMAP3XXX_EN_DPLL_LOCKED 0x7
41
42/* Forward declarations */
43static u32 omap3_dpll_autoidle_read(struct clk_hw_omap *clk);
44static void omap3_dpll_deny_idle(struct clk_hw_omap *clk);
45static void omap3_dpll_allow_idle(struct clk_hw_omap *clk);
46
Paul Walmsley60c3f652010-01-26 20:13:11 -070047/* Private functions */
Rajendra Nayaka1391d22009-12-08 18:47:16 -070048
49/* _omap3_dpll_write_clken - write clken_bits arg to a DPLL's enable bits */
Mike Turquette32cc0022012-11-10 16:58:41 -070050static void _omap3_dpll_write_clken(struct clk_hw_omap *clk, u8 clken_bits)
Rajendra Nayaka1391d22009-12-08 18:47:16 -070051{
52 const struct dpll_data *dd;
53 u32 v;
54
55 dd = clk->dpll_data;
56
Tero Kristo0565fb12015-03-03 13:27:48 +020057 v = ti_clk_ll_ops->clk_readl(dd->control_reg);
Rajendra Nayaka1391d22009-12-08 18:47:16 -070058 v &= ~dd->enable_mask;
59 v |= clken_bits << __ffs(dd->enable_mask);
Tero Kristo0565fb12015-03-03 13:27:48 +020060 ti_clk_ll_ops->clk_writel(v, dd->control_reg);
Rajendra Nayaka1391d22009-12-08 18:47:16 -070061}
62
63/* _omap3_wait_dpll_status: wait for a DPLL to enter a specific state */
Mike Turquette32cc0022012-11-10 16:58:41 -070064static int _omap3_wait_dpll_status(struct clk_hw_omap *clk, u8 state)
Rajendra Nayaka1391d22009-12-08 18:47:16 -070065{
66 const struct dpll_data *dd;
67 int i = 0;
68 int ret = -EINVAL;
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -060069 const char *clk_name;
Rajendra Nayaka1391d22009-12-08 18:47:16 -070070
71 dd = clk->dpll_data;
Mike Turquette32cc0022012-11-10 16:58:41 -070072 clk_name = __clk_get_name(clk->hw.clk);
Rajendra Nayaka1391d22009-12-08 18:47:16 -070073
74 state <<= __ffs(dd->idlest_mask);
75
Tero Kristo0565fb12015-03-03 13:27:48 +020076 while (((ti_clk_ll_ops->clk_readl(dd->idlest_reg) & dd->idlest_mask)
Tero Kristo519ab8b2013-10-22 11:49:58 +030077 != state) && i < MAX_DPLL_WAIT_TRIES) {
Rajendra Nayaka1391d22009-12-08 18:47:16 -070078 i++;
79 udelay(1);
80 }
81
82 if (i == MAX_DPLL_WAIT_TRIES) {
Tero Kristo0565fb12015-03-03 13:27:48 +020083 pr_err("clock: %s failed transition to '%s'\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -060084 clk_name, (state) ? "locked" : "bypassed");
Rajendra Nayaka1391d22009-12-08 18:47:16 -070085 } else {
86 pr_debug("clock: %s transition to '%s' in %d loops\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -060087 clk_name, (state) ? "locked" : "bypassed", i);
Rajendra Nayaka1391d22009-12-08 18:47:16 -070088
89 ret = 0;
90 }
91
92 return ret;
93}
94
95/* From 3430 TRM ES2 4.7.6.2 */
Mike Turquette32cc0022012-11-10 16:58:41 -070096static u16 _omap3_dpll_compute_freqsel(struct clk_hw_omap *clk, u8 n)
Rajendra Nayaka1391d22009-12-08 18:47:16 -070097{
98 unsigned long fint;
99 u16 f = 0;
100
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600101 fint = __clk_get_rate(clk->dpll_data->clk_ref) / n;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700102
103 pr_debug("clock: fint is %lu\n", fint);
104
105 if (fint >= 750000 && fint <= 1000000)
106 f = 0x3;
107 else if (fint > 1000000 && fint <= 1250000)
108 f = 0x4;
109 else if (fint > 1250000 && fint <= 1500000)
110 f = 0x5;
111 else if (fint > 1500000 && fint <= 1750000)
112 f = 0x6;
113 else if (fint > 1750000 && fint <= 2100000)
114 f = 0x7;
115 else if (fint > 7500000 && fint <= 10000000)
116 f = 0xB;
117 else if (fint > 10000000 && fint <= 12500000)
118 f = 0xC;
119 else if (fint > 12500000 && fint <= 15000000)
120 f = 0xD;
121 else if (fint > 15000000 && fint <= 17500000)
122 f = 0xE;
123 else if (fint > 17500000 && fint <= 21000000)
124 f = 0xF;
125 else
126 pr_debug("clock: unknown freqsel setting for %d\n", n);
127
128 return f;
129}
130
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700131/*
132 * _omap3_noncore_dpll_lock - instruct a DPLL to lock and wait for readiness
133 * @clk: pointer to a DPLL struct clk
134 *
135 * Instructs a non-CORE DPLL to lock. Waits for the DPLL to report
136 * readiness before returning. Will save and restore the DPLL's
137 * autoidle state across the enable, per the CDP code. If the DPLL
138 * locked successfully, return 0; if the DPLL did not lock in the time
139 * allotted, or DPLL3 was passed in, return -EINVAL.
140 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700141static int _omap3_noncore_dpll_lock(struct clk_hw_omap *clk)
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700142{
Vikram Pandita55ffe162012-07-04 05:00:44 -0600143 const struct dpll_data *dd;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700144 u8 ai;
Vikram Pandita55ffe162012-07-04 05:00:44 -0600145 u8 state = 1;
146 int r = 0;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700147
Mike Turquette32cc0022012-11-10 16:58:41 -0700148 pr_debug("clock: locking DPLL %s\n", __clk_get_name(clk->hw.clk));
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700149
Vikram Pandita55ffe162012-07-04 05:00:44 -0600150 dd = clk->dpll_data;
151 state <<= __ffs(dd->idlest_mask);
152
153 /* Check if already locked */
Tero Kristo0565fb12015-03-03 13:27:48 +0200154 if ((ti_clk_ll_ops->clk_readl(dd->idlest_reg) & dd->idlest_mask) ==
155 state)
Vikram Pandita55ffe162012-07-04 05:00:44 -0600156 goto done;
157
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700158 ai = omap3_dpll_autoidle_read(clk);
159
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600160 if (ai)
161 omap3_dpll_deny_idle(clk);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700162
163 _omap3_dpll_write_clken(clk, DPLL_LOCKED);
164
165 r = _omap3_wait_dpll_status(clk, 1);
166
167 if (ai)
168 omap3_dpll_allow_idle(clk);
169
Vikram Pandita55ffe162012-07-04 05:00:44 -0600170done:
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700171 return r;
172}
173
174/*
175 * _omap3_noncore_dpll_bypass - instruct a DPLL to bypass and wait for readiness
176 * @clk: pointer to a DPLL struct clk
177 *
178 * Instructs a non-CORE DPLL to enter low-power bypass mode. In
179 * bypass mode, the DPLL's rate is set equal to its parent clock's
180 * rate. Waits for the DPLL to report readiness before returning.
181 * Will save and restore the DPLL's autoidle state across the enable,
182 * per the CDP code. If the DPLL entered bypass mode successfully,
183 * return 0; if the DPLL did not enter bypass in the time allotted, or
184 * DPLL3 was passed in, or the DPLL does not support low-power bypass,
185 * return -EINVAL.
186 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700187static int _omap3_noncore_dpll_bypass(struct clk_hw_omap *clk)
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700188{
189 int r;
190 u8 ai;
191
192 if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS)))
193 return -EINVAL;
194
195 pr_debug("clock: configuring DPLL %s for low-power bypass\n",
Mike Turquette32cc0022012-11-10 16:58:41 -0700196 __clk_get_name(clk->hw.clk));
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700197
198 ai = omap3_dpll_autoidle_read(clk);
199
200 _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_BYPASS);
201
202 r = _omap3_wait_dpll_status(clk, 0);
203
204 if (ai)
205 omap3_dpll_allow_idle(clk);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700206
207 return r;
208}
209
210/*
211 * _omap3_noncore_dpll_stop - instruct a DPLL to stop
212 * @clk: pointer to a DPLL struct clk
213 *
214 * Instructs a non-CORE DPLL to enter low-power stop. Will save and
215 * restore the DPLL's autoidle state across the stop, per the CDP
216 * code. If DPLL3 was passed in, or the DPLL does not support
217 * low-power stop, return -EINVAL; otherwise, return 0.
218 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700219static int _omap3_noncore_dpll_stop(struct clk_hw_omap *clk)
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700220{
221 u8 ai;
222
223 if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_STOP)))
224 return -EINVAL;
225
Mike Turquette32cc0022012-11-10 16:58:41 -0700226 pr_debug("clock: stopping DPLL %s\n", __clk_get_name(clk->hw.clk));
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700227
228 ai = omap3_dpll_autoidle_read(clk);
229
230 _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_STOP);
231
232 if (ai)
233 omap3_dpll_allow_idle(clk);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700234
235 return 0;
236}
237
Richard Woodruff358965d2010-02-22 22:09:08 -0700238/**
Jon Huntera36795c2010-12-21 21:31:43 -0700239 * _lookup_dco - Lookup DCO used by j-type DPLL
Richard Woodruff358965d2010-02-22 22:09:08 -0700240 * @clk: pointer to a DPLL struct clk
241 * @dco: digital control oscillator selector
Jon Huntera36795c2010-12-21 21:31:43 -0700242 * @m: DPLL multiplier to set
243 * @n: DPLL divider to set
244 *
245 * See 36xx TRM section 3.5.3.3.3.2 "Type B DPLL (Low-Jitter)"
246 *
247 * XXX This code is not needed for 3430/AM35xx; can it be optimized
248 * out in non-multi-OMAP builds for those chips?
249 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700250static void _lookup_dco(struct clk_hw_omap *clk, u8 *dco, u16 m, u8 n)
Jon Huntera36795c2010-12-21 21:31:43 -0700251{
252 unsigned long fint, clkinp; /* watch out for overflow */
253
Mike Turquette32cc0022012-11-10 16:58:41 -0700254 clkinp = __clk_get_rate(__clk_get_parent(clk->hw.clk));
Jon Huntera36795c2010-12-21 21:31:43 -0700255 fint = (clkinp / n) * m;
256
257 if (fint < 1000000000)
258 *dco = 2;
259 else
260 *dco = 4;
261}
262
263/**
264 * _lookup_sddiv - Calculate sigma delta divider for j-type DPLL
265 * @clk: pointer to a DPLL struct clk
Richard Woodruff358965d2010-02-22 22:09:08 -0700266 * @sd_div: target sigma-delta divider
267 * @m: DPLL multiplier to set
268 * @n: DPLL divider to set
269 *
270 * See 36xx TRM section 3.5.3.3.3.2 "Type B DPLL (Low-Jitter)"
271 *
272 * XXX This code is not needed for 3430/AM35xx; can it be optimized
273 * out in non-multi-OMAP builds for those chips?
274 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700275static void _lookup_sddiv(struct clk_hw_omap *clk, u8 *sd_div, u16 m, u8 n)
Richard Woodruff358965d2010-02-22 22:09:08 -0700276{
Jon Huntera36795c2010-12-21 21:31:43 -0700277 unsigned long clkinp, sd; /* watch out for overflow */
Richard Woodruff358965d2010-02-22 22:09:08 -0700278 int mod1, mod2;
279
Mike Turquette32cc0022012-11-10 16:58:41 -0700280 clkinp = __clk_get_rate(__clk_get_parent(clk->hw.clk));
Richard Woodruff358965d2010-02-22 22:09:08 -0700281
Richard Woodruff358965d2010-02-22 22:09:08 -0700282 /*
283 * target sigma-delta to near 250MHz
284 * sd = ceil[(m/(n+1)) * (clkinp_MHz / 250)]
285 */
286 clkinp /= 100000; /* shift from MHz to 10*Hz for 38.4 and 19.2 */
287 mod1 = (clkinp * m) % (250 * n);
288 sd = (clkinp * m) / (250 * n);
289 mod2 = sd % 10;
290 sd /= 10;
291
292 if (mod1 || mod2)
293 sd++;
294 *sd_div = sd;
295}
296
Paul Walmsley60c3f652010-01-26 20:13:11 -0700297/*
298 * _omap3_noncore_dpll_program - set non-core DPLL M,N values directly
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700299 * @clk: struct clk * of DPLL to set
300 * @freqsel: FREQSEL value to set
Paul Walmsley60c3f652010-01-26 20:13:11 -0700301 *
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700302 * Program the DPLL with the last M, N values calculated, and wait for
303 * the DPLL to lock. Returns -EINVAL upon error, or 0 upon success.
Paul Walmsley60c3f652010-01-26 20:13:11 -0700304 */
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700305static int omap3_noncore_dpll_program(struct clk_hw_omap *clk, u16 freqsel)
Paul Walmsley60c3f652010-01-26 20:13:11 -0700306{
307 struct dpll_data *dd = clk->dpll_data;
Jon Huntera36795c2010-12-21 21:31:43 -0700308 u8 dco, sd_div;
Paul Walmsley60c3f652010-01-26 20:13:11 -0700309 u32 v;
310
311 /* 3430 ES2 TRM: 4.7.6.9 DPLL Programming Sequence */
312 _omap3_noncore_dpll_bypass(clk);
313
Vishwanath BS5eb75f52010-02-24 12:05:57 -0700314 /*
Rajendra Nayakecf51642013-01-29 18:33:49 +0530315 * Set jitter correction. Jitter correction applicable for OMAP343X
316 * only since freqsel field is no longer present on other devices.
Vishwanath BS5eb75f52010-02-24 12:05:57 -0700317 */
Tero Kristof3b19aa2015-02-27 17:54:14 +0200318 if (ti_clk_get_features()->flags & TI_CLK_DPLL_HAS_FREQSEL) {
Tero Kristo0565fb12015-03-03 13:27:48 +0200319 v = ti_clk_ll_ops->clk_readl(dd->control_reg);
Paul Walmsley60c3f652010-01-26 20:13:11 -0700320 v &= ~dd->freqsel_mask;
321 v |= freqsel << __ffs(dd->freqsel_mask);
Tero Kristo0565fb12015-03-03 13:27:48 +0200322 ti_clk_ll_ops->clk_writel(v, dd->control_reg);
Paul Walmsley60c3f652010-01-26 20:13:11 -0700323 }
324
325 /* Set DPLL multiplier, divider */
Tero Kristo0565fb12015-03-03 13:27:48 +0200326 v = ti_clk_ll_ops->clk_readl(dd->mult_div1_reg);
Andrii Tseglytskyice369a52014-05-16 05:45:58 -0500327
328 /* Handle Duty Cycle Correction */
329 if (dd->dcc_mask) {
330 if (dd->last_rounded_rate >= dd->dcc_rate)
331 v |= dd->dcc_mask; /* Enable DCC */
332 else
333 v &= ~dd->dcc_mask; /* Disable DCC */
334 }
335
Paul Walmsley60c3f652010-01-26 20:13:11 -0700336 v &= ~(dd->mult_mask | dd->div1_mask);
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700337 v |= dd->last_rounded_m << __ffs(dd->mult_mask);
338 v |= (dd->last_rounded_n - 1) << __ffs(dd->div1_mask);
Richard Woodruff358965d2010-02-22 22:09:08 -0700339
Jon Huntera36795c2010-12-21 21:31:43 -0700340 /* Configure dco and sd_div for dplls that have these fields */
341 if (dd->dco_mask) {
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700342 _lookup_dco(clk, &dco, dd->last_rounded_m, dd->last_rounded_n);
Jon Huntera36795c2010-12-21 21:31:43 -0700343 v &= ~(dd->dco_mask);
344 v |= dco << __ffs(dd->dco_mask);
345 }
346 if (dd->sddiv_mask) {
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700347 _lookup_sddiv(clk, &sd_div, dd->last_rounded_m,
348 dd->last_rounded_n);
Jon Huntera36795c2010-12-21 21:31:43 -0700349 v &= ~(dd->sddiv_mask);
350 v |= sd_div << __ffs(dd->sddiv_mask);
Richard Woodruff358965d2010-02-22 22:09:08 -0700351 }
352
Tero Kristo0565fb12015-03-03 13:27:48 +0200353 ti_clk_ll_ops->clk_writel(v, dd->mult_div1_reg);
Paul Walmsley60c3f652010-01-26 20:13:11 -0700354
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700355 /* Set 4X multiplier and low-power mode */
356 if (dd->m4xen_mask || dd->lpmode_mask) {
Tero Kristo0565fb12015-03-03 13:27:48 +0200357 v = ti_clk_ll_ops->clk_readl(dd->control_reg);
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700358
359 if (dd->m4xen_mask) {
360 if (dd->last_rounded_m4xen)
361 v |= dd->m4xen_mask;
362 else
363 v &= ~dd->m4xen_mask;
364 }
365
366 if (dd->lpmode_mask) {
367 if (dd->last_rounded_lpmode)
368 v |= dd->lpmode_mask;
369 else
370 v &= ~dd->lpmode_mask;
371 }
372
Tero Kristo0565fb12015-03-03 13:27:48 +0200373 ti_clk_ll_ops->clk_writel(v, dd->control_reg);
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700374 }
375
Paul Walmsley60c3f652010-01-26 20:13:11 -0700376 /* We let the clock framework set the other output dividers later */
377
378 /* REVISIT: Set ramp-up delay? */
379
380 _omap3_noncore_dpll_lock(clk);
381
382 return 0;
383}
384
385/* Public functions */
386
387/**
388 * omap3_dpll_recalc - recalculate DPLL rate
389 * @clk: DPLL struct clk
390 *
391 * Recalculate and propagate the DPLL rate.
392 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700393unsigned long omap3_dpll_recalc(struct clk_hw *hw, unsigned long parent_rate)
394{
395 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
Paul Walmsley455db9c2012-11-10 19:32:46 -0700396
Paul Walmsley60c3f652010-01-26 20:13:11 -0700397 return omap2_get_dpll_rate(clk);
398}
399
400/* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */
401
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700402/**
403 * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
404 * @clk: pointer to a DPLL struct clk
405 *
406 * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
407 * The choice of modes depends on the DPLL's programmed rate: if it is
408 * the same as the DPLL's parent clock, it will enter bypass;
409 * otherwise, it will enter lock. This code will wait for the DPLL to
410 * indicate readiness before returning, unless the DPLL takes too long
411 * to enter the target state. Intended to be used as the struct clk's
412 * enable function. If DPLL3 was passed in, or the DPLL does not
413 * support low-power stop, or if the DPLL took too long to enter
414 * bypass or lock, return -EINVAL; otherwise, return 0.
415 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700416int omap3_noncore_dpll_enable(struct clk_hw *hw)
417{
418 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700419 int r;
420 struct dpll_data *dd;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100421 struct clk_hw *parent;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700422
423 dd = clk->dpll_data;
424 if (!dd)
425 return -EINVAL;
426
Mike Turquette32cc0022012-11-10 16:58:41 -0700427 if (clk->clkdm) {
Tero Kristo0565fb12015-03-03 13:27:48 +0200428 r = ti_clk_ll_ops->clkdm_clk_enable(clk->clkdm, hw->clk);
Mike Turquette32cc0022012-11-10 16:58:41 -0700429 if (r) {
430 WARN(1,
431 "%s: could not enable %s's clockdomain %s: %d\n",
432 __func__, __clk_get_name(hw->clk),
Tero Kristo0565fb12015-03-03 13:27:48 +0200433 clk->clkdm_name, r);
Mike Turquette32cc0022012-11-10 16:58:41 -0700434 return r;
435 }
436 }
437
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100438 parent = __clk_get_hw(__clk_get_parent(hw->clk));
Mike Turquette32cc0022012-11-10 16:58:41 -0700439
440 if (__clk_get_rate(hw->clk) == __clk_get_rate(dd->clk_bypass)) {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100441 WARN_ON(parent != __clk_get_hw(dd->clk_bypass));
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700442 r = _omap3_noncore_dpll_bypass(clk);
443 } else {
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100444 WARN_ON(parent != __clk_get_hw(dd->clk_ref));
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700445 r = _omap3_noncore_dpll_lock(clk);
446 }
Mike Turquette32cc0022012-11-10 16:58:41 -0700447
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700448 return r;
449}
450
451/**
452 * omap3_noncore_dpll_disable - instruct a DPLL to enter low-power stop
453 * @clk: pointer to a DPLL struct clk
454 *
455 * Instructs a non-CORE DPLL to enter low-power stop. This function is
456 * intended for use in struct clkops. No return value.
457 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700458void omap3_noncore_dpll_disable(struct clk_hw *hw)
459{
460 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
461
462 _omap3_noncore_dpll_stop(clk);
463 if (clk->clkdm)
Tero Kristo0565fb12015-03-03 13:27:48 +0200464 ti_clk_ll_ops->clkdm_clk_disable(clk->clkdm, hw->clk);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700465}
466
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700467/* Non-CORE DPLL rate set code */
468
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700469/**
Tero Kristod539efa2014-10-03 16:57:11 +0300470 * omap3_noncore_dpll_determine_rate - determine rate for a DPLL
471 * @hw: pointer to the clock to determine rate for
472 * @rate: target rate for the DPLL
473 * @best_parent_rate: pointer for returning best parent rate
474 * @best_parent_clk: pointer for returning best parent clock
475 *
476 * Determines which DPLL mode to use for reaching a desired target rate.
477 * Checks whether the DPLL shall be in bypass or locked mode, and if
478 * locked, calculates the M,N values for the DPLL via round-rate.
479 * Returns a positive clock rate with success, negative error value
480 * in failure.
481 */
482long omap3_noncore_dpll_determine_rate(struct clk_hw *hw, unsigned long rate,
Tomeu Vizoso1c8e6002015-01-23 12:03:31 +0100483 unsigned long min_rate,
484 unsigned long max_rate,
Tero Kristod539efa2014-10-03 16:57:11 +0300485 unsigned long *best_parent_rate,
Tero Kristo6f8e8532014-12-12 15:22:00 +0200486 struct clk_hw **best_parent_clk)
Tero Kristod539efa2014-10-03 16:57:11 +0300487{
488 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
489 struct dpll_data *dd;
490
491 if (!hw || !rate)
492 return -EINVAL;
493
494 dd = clk->dpll_data;
495 if (!dd)
496 return -EINVAL;
497
498 if (__clk_get_rate(dd->clk_bypass) == rate &&
499 (dd->modes & (1 << DPLL_LOW_POWER_BYPASS))) {
Tero Kristo6f8e8532014-12-12 15:22:00 +0200500 *best_parent_clk = __clk_get_hw(dd->clk_bypass);
Tero Kristod539efa2014-10-03 16:57:11 +0300501 } else {
502 rate = omap2_dpll_round_rate(hw, rate, best_parent_rate);
Tero Kristo6f8e8532014-12-12 15:22:00 +0200503 *best_parent_clk = __clk_get_hw(dd->clk_ref);
Tero Kristod539efa2014-10-03 16:57:11 +0300504 }
505
506 *best_parent_rate = rate;
507
508 return rate;
509}
510
511/**
512 * omap3_noncore_dpll_set_parent - set parent for a DPLL clock
513 * @hw: pointer to the clock to set parent for
514 * @index: parent index to select
515 *
516 * Sets parent for a DPLL clock. This sets the DPLL into bypass or
517 * locked mode. Returns 0 with success, negative error value otherwise.
518 */
519int omap3_noncore_dpll_set_parent(struct clk_hw *hw, u8 index)
520{
521 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
522 int ret;
523
524 if (!hw)
525 return -EINVAL;
526
527 if (index)
528 ret = _omap3_noncore_dpll_bypass(clk);
529 else
530 ret = _omap3_noncore_dpll_lock(clk);
531
532 return ret;
533}
534
535/**
Tero Kristo2e1a7b02014-10-03 16:57:14 +0300536 * omap3_noncore_dpll_set_rate - set rate for a DPLL clock
Tero Kristod539efa2014-10-03 16:57:11 +0300537 * @hw: pointer to the clock to set parent for
538 * @rate: target rate for the clock
539 * @parent_rate: rate of the parent clock
540 *
541 * Sets rate for a DPLL clock. First checks if the clock parent is
542 * reference clock (in bypass mode, the rate of the clock can't be
543 * changed) and proceeds with the rate change operation. Returns 0
544 * with success, negative error value otherwise.
545 */
Tero Kristo2e1a7b02014-10-03 16:57:14 +0300546int omap3_noncore_dpll_set_rate(struct clk_hw *hw, unsigned long rate,
547 unsigned long parent_rate)
Tero Kristod539efa2014-10-03 16:57:11 +0300548{
549 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
550 struct dpll_data *dd;
551 u16 freqsel = 0;
552 int ret;
553
554 if (!hw || !rate)
555 return -EINVAL;
556
557 dd = clk->dpll_data;
558 if (!dd)
559 return -EINVAL;
560
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100561 if (__clk_get_hw(__clk_get_parent(hw->clk)) !=
562 __clk_get_hw(dd->clk_ref))
Tero Kristod539efa2014-10-03 16:57:11 +0300563 return -EINVAL;
564
565 if (dd->last_rounded_rate == 0)
566 return -EINVAL;
567
568 /* Freqsel is available only on OMAP343X devices */
Tero Kristof3b19aa2015-02-27 17:54:14 +0200569 if (ti_clk_get_features()->flags & TI_CLK_DPLL_HAS_FREQSEL) {
Tero Kristod539efa2014-10-03 16:57:11 +0300570 freqsel = _omap3_dpll_compute_freqsel(clk, dd->last_rounded_n);
571 WARN_ON(!freqsel);
572 }
573
574 pr_debug("%s: %s: set rate: locking rate to %lu.\n", __func__,
575 __clk_get_name(hw->clk), rate);
576
577 ret = omap3_noncore_dpll_program(clk, freqsel);
578
579 return ret;
580}
581
582/**
583 * omap3_noncore_dpll_set_rate_and_parent - set rate and parent for a DPLL clock
584 * @hw: pointer to the clock to set rate and parent for
585 * @rate: target rate for the DPLL
586 * @parent_rate: clock rate of the DPLL parent
587 * @index: new parent index for the DPLL, 0 - reference, 1 - bypass
588 *
589 * Sets rate and parent for a DPLL clock. If new parent is the bypass
590 * clock, only selects the parent. Otherwise proceeds with a rate
591 * change, as this will effectively also change the parent as the
592 * DPLL is put into locked mode. Returns 0 with success, negative error
593 * value otherwise.
594 */
595int omap3_noncore_dpll_set_rate_and_parent(struct clk_hw *hw,
596 unsigned long rate,
597 unsigned long parent_rate,
598 u8 index)
599{
600 int ret;
601
602 if (!hw || !rate)
603 return -EINVAL;
604
605 /*
606 * clk-ref at index[0], in which case we only need to set rate,
607 * the parent will be changed automatically with the lock sequence.
608 * With clk-bypass case we only need to change parent.
609 */
610 if (index)
611 ret = omap3_noncore_dpll_set_parent(hw, index);
612 else
Tero Kristo2e1a7b02014-10-03 16:57:14 +0300613 ret = omap3_noncore_dpll_set_rate(hw, rate, parent_rate);
Tero Kristod539efa2014-10-03 16:57:11 +0300614
615 return ret;
616}
617
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700618/* DPLL autoidle read/set code */
619
620/**
621 * omap3_dpll_autoidle_read - read a DPLL's autoidle bits
622 * @clk: struct clk * of the DPLL to read
623 *
624 * Return the DPLL's autoidle bits, shifted down to bit 0. Returns
625 * -EINVAL if passed a null pointer or if the struct clk does not
626 * appear to refer to a DPLL.
627 */
Tero Kristo0565fb12015-03-03 13:27:48 +0200628static u32 omap3_dpll_autoidle_read(struct clk_hw_omap *clk)
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700629{
630 const struct dpll_data *dd;
631 u32 v;
632
633 if (!clk || !clk->dpll_data)
634 return -EINVAL;
635
636 dd = clk->dpll_data;
637
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600638 if (!dd->autoidle_reg)
639 return -EINVAL;
640
Tero Kristo0565fb12015-03-03 13:27:48 +0200641 v = ti_clk_ll_ops->clk_readl(dd->autoidle_reg);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700642 v &= dd->autoidle_mask;
643 v >>= __ffs(dd->autoidle_mask);
644
645 return v;
646}
647
648/**
649 * omap3_dpll_allow_idle - enable DPLL autoidle bits
650 * @clk: struct clk * of the DPLL to operate on
651 *
652 * Enable DPLL automatic idle control. This automatic idle mode
653 * switching takes effect only when the DPLL is locked, at least on
654 * OMAP3430. The DPLL will enter low-power stop when its downstream
655 * clocks are gated. No return value.
656 */
Tero Kristo0565fb12015-03-03 13:27:48 +0200657static void omap3_dpll_allow_idle(struct clk_hw_omap *clk)
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700658{
659 const struct dpll_data *dd;
660 u32 v;
661
662 if (!clk || !clk->dpll_data)
663 return;
664
665 dd = clk->dpll_data;
666
Paul Walmsley455db9c2012-11-10 19:32:46 -0700667 if (!dd->autoidle_reg)
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600668 return;
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600669
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700670 /*
671 * REVISIT: CORE DPLL can optionally enter low-power bypass
672 * by writing 0x5 instead of 0x1. Add some mechanism to
673 * optionally enter this mode.
674 */
Tero Kristo0565fb12015-03-03 13:27:48 +0200675 v = ti_clk_ll_ops->clk_readl(dd->autoidle_reg);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700676 v &= ~dd->autoidle_mask;
677 v |= DPLL_AUTOIDLE_LOW_POWER_STOP << __ffs(dd->autoidle_mask);
Tero Kristo0565fb12015-03-03 13:27:48 +0200678 ti_clk_ll_ops->clk_writel(v, dd->autoidle_reg);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700679}
680
681/**
682 * omap3_dpll_deny_idle - prevent DPLL from automatically idling
683 * @clk: struct clk * of the DPLL to operate on
684 *
685 * Disable DPLL automatic idle control. No return value.
686 */
Tero Kristo0565fb12015-03-03 13:27:48 +0200687static void omap3_dpll_deny_idle(struct clk_hw_omap *clk)
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700688{
689 const struct dpll_data *dd;
690 u32 v;
691
692 if (!clk || !clk->dpll_data)
693 return;
694
695 dd = clk->dpll_data;
696
Paul Walmsley455db9c2012-11-10 19:32:46 -0700697 if (!dd->autoidle_reg)
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600698 return;
Vaibhav Bediad76316f2012-05-07 23:55:30 -0600699
Tero Kristo0565fb12015-03-03 13:27:48 +0200700 v = ti_clk_ll_ops->clk_readl(dd->autoidle_reg);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700701 v &= ~dd->autoidle_mask;
702 v |= DPLL_AUTOIDLE_DISABLE << __ffs(dd->autoidle_mask);
Tero Kristo0565fb12015-03-03 13:27:48 +0200703 ti_clk_ll_ops->clk_writel(v, dd->autoidle_reg);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700704}
705
706/* Clock control for DPLL outputs */
707
Tomi Valkeinen994c41e2014-01-30 13:17:20 +0200708/* Find the parent DPLL for the given clkoutx2 clock */
709static struct clk_hw_omap *omap3_find_clkoutx2_dpll(struct clk_hw *hw)
Mike Turquette32cc0022012-11-10 16:58:41 -0700710{
Mike Turquette32cc0022012-11-10 16:58:41 -0700711 struct clk_hw_omap *pclk = NULL;
712 struct clk *parent;
713
714 /* Walk up the parents of clk, looking for a DPLL */
715 do {
716 do {
717 parent = __clk_get_parent(hw->clk);
718 hw = __clk_get_hw(parent);
719 } while (hw && (__clk_get_flags(hw->clk) & CLK_IS_BASIC));
720 if (!hw)
721 break;
722 pclk = to_clk_hw_omap(hw);
723 } while (pclk && !pclk->dpll_data);
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700724
Paul Walmsleya032d332012-08-03 09:21:10 -0600725 /* clk does not have a DPLL as a parent? error in the clock data */
726 if (!pclk) {
727 WARN_ON(1);
Tomi Valkeinen994c41e2014-01-30 13:17:20 +0200728 return NULL;
Paul Walmsleya032d332012-08-03 09:21:10 -0600729 }
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700730
Tomi Valkeinen994c41e2014-01-30 13:17:20 +0200731 return pclk;
732}
733
734/**
735 * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate
736 * @clk: DPLL output struct clk
737 *
738 * Using parent clock DPLL data, look up DPLL state. If locked, set our
739 * rate to the dpll_clk * 2; otherwise, just use dpll_clk.
740 */
741unsigned long omap3_clkoutx2_recalc(struct clk_hw *hw,
742 unsigned long parent_rate)
743{
744 const struct dpll_data *dd;
745 unsigned long rate;
746 u32 v;
747 struct clk_hw_omap *pclk = NULL;
748
749 if (!parent_rate)
750 return 0;
751
752 pclk = omap3_find_clkoutx2_dpll(hw);
753
754 if (!pclk)
755 return 0;
756
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700757 dd = pclk->dpll_data;
758
759 WARN_ON(!dd->enable_mask);
760
Tero Kristo0565fb12015-03-03 13:27:48 +0200761 v = ti_clk_ll_ops->clk_readl(dd->control_reg) & dd->enable_mask;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700762 v >>= __ffs(dd->enable_mask);
Richard Woodruff358965d2010-02-22 22:09:08 -0700763 if ((v != OMAP3XXX_EN_DPLL_LOCKED) || (dd->flags & DPLL_J_TYPE))
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600764 rate = parent_rate;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700765 else
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600766 rate = parent_rate * 2;
Rajendra Nayaka1391d22009-12-08 18:47:16 -0700767 return rate;
768}
Vaibhav Hiremath353cec42012-07-05 08:05:15 -0700769
770/* OMAP3/4 non-CORE DPLL clkops */
Mike Turquette32cc0022012-11-10 16:58:41 -0700771const struct clk_hw_omap_ops clkhwops_omap3_dpll = {
772 .allow_idle = omap3_dpll_allow_idle,
773 .deny_idle = omap3_dpll_deny_idle,
774};
Tero Kristo0565fb12015-03-03 13:27:48 +0200775
776/**
777 * omap3_dpll4_set_rate - set rate for omap3 per-dpll
778 * @hw: clock to change
779 * @rate: target rate for clock
780 * @parent_rate: rate of the parent clock
781 *
782 * Check if the current SoC supports the per-dpll reprogram operation
783 * or not, and then do the rate change if supported. Returns -EINVAL
784 * if not supported, 0 for success, and potential error codes from the
785 * clock rate change.
786 */
787int omap3_dpll4_set_rate(struct clk_hw *hw, unsigned long rate,
788 unsigned long parent_rate)
789{
790 /*
791 * According to the 12-5 CDP code from TI, "Limitation 2.5"
792 * on 3430ES1 prevents us from changing DPLL multipliers or dividers
793 * on DPLL4.
794 */
795 if (ti_clk_get_features()->flags & TI_CLK_DPLL4_DENY_REPROGRAM) {
796 pr_err("clock: DPLL4 cannot change rate due to silicon 'Limitation 2.5' on 3430ES1.\n");
797 return -EINVAL;
798 }
799
800 return omap3_noncore_dpll_set_rate(hw, rate, parent_rate);
801}
802
803/**
804 * omap3_dpll4_set_rate_and_parent - set rate and parent for omap3 per-dpll
805 * @hw: clock to change
806 * @rate: target rate for clock
807 * @parent_rate: rate of the parent clock
808 * @index: parent index, 0 - reference clock, 1 - bypass clock
809 *
810 * Check if the current SoC support the per-dpll reprogram operation
811 * or not, and then do the rate + parent change if supported. Returns
812 * -EINVAL if not supported, 0 for success, and potential error codes
813 * from the clock rate change.
814 */
815int omap3_dpll4_set_rate_and_parent(struct clk_hw *hw, unsigned long rate,
816 unsigned long parent_rate, u8 index)
817{
818 if (ti_clk_get_features()->flags & TI_CLK_DPLL4_DENY_REPROGRAM) {
819 pr_err("clock: DPLL4 cannot change rate due to silicon 'Limitation 2.5' on 3430ES1.\n");
820 return -EINVAL;
821 }
822
823 return omap3_noncore_dpll_set_rate_and_parent(hw, rate, parent_rate,
824 index);
825}