1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Default clock type
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2005-2008, 2015 Texas Instruments, Inc.
5*4882a593Smuzhiyun * Copyright (C) 2004-2010 Nokia Corporation
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Contacts:
8*4882a593Smuzhiyun * Richard Woodruff <r-woodruff2@ti.com>
9*4882a593Smuzhiyun * Paul Walmsley
10*4882a593Smuzhiyun * Tero Kristo <t-kristo@ti.com>
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
13*4882a593Smuzhiyun * it under the terms of the GNU General Public License version 2 as
14*4882a593Smuzhiyun * published by the Free Software Foundation.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * This program is distributed "as is" WITHOUT ANY WARRANTY of any
17*4882a593Smuzhiyun * kind, whether express or implied; without even the implied warranty
18*4882a593Smuzhiyun * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19*4882a593Smuzhiyun * GNU General Public License for more details.
20*4882a593Smuzhiyun */
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include <linux/kernel.h>
23*4882a593Smuzhiyun #include <linux/errno.h>
24*4882a593Smuzhiyun #include <linux/clk-provider.h>
25*4882a593Smuzhiyun #include <linux/io.h>
26*4882a593Smuzhiyun #include <linux/clk/ti.h>
27*4882a593Smuzhiyun #include <linux/delay.h>
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "clock.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun * MAX_MODULE_ENABLE_WAIT: maximum of number of microseconds to wait
33*4882a593Smuzhiyun * for a module to indicate that it is no longer in idle
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun #define MAX_MODULE_ENABLE_WAIT 100000
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun * CM module register offsets, used for calculating the companion
39*4882a593Smuzhiyun * register addresses.
40*4882a593Smuzhiyun */
41*4882a593Smuzhiyun #define CM_FCLKEN 0x0000
42*4882a593Smuzhiyun #define CM_ICLKEN 0x0010
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /**
45*4882a593Smuzhiyun * _wait_idlest_generic - wait for a module to leave the idle state
46*4882a593Smuzhiyun * @clk: module clock to wait for (needed for register offsets)
47*4882a593Smuzhiyun * @reg: virtual address of module IDLEST register
48*4882a593Smuzhiyun * @mask: value to mask against to determine if the module is active
49*4882a593Smuzhiyun * @idlest: idle state indicator (0 or 1) for the clock
50*4882a593Smuzhiyun * @name: name of the clock (for printk)
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun * Wait for a module to leave idle, where its idle-status register is
53*4882a593Smuzhiyun * not inside the CM module. Returns 1 if the module left idle
54*4882a593Smuzhiyun * promptly, or 0 if the module did not leave idle before the timeout
55*4882a593Smuzhiyun * elapsed. XXX Deprecated - should be moved into drivers for the
56*4882a593Smuzhiyun * individual IP block that the IDLEST register exists in.
57*4882a593Smuzhiyun */
_wait_idlest_generic(struct clk_hw_omap * clk,struct clk_omap_reg * reg,u32 mask,u8 idlest,const char * name)58*4882a593Smuzhiyun static int _wait_idlest_generic(struct clk_hw_omap *clk,
59*4882a593Smuzhiyun struct clk_omap_reg *reg,
60*4882a593Smuzhiyun u32 mask, u8 idlest, const char *name)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun int i = 0, ena = 0;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun ena = (idlest) ? 0 : mask;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /* Wait until module enters enabled state */
67*4882a593Smuzhiyun for (i = 0; i < MAX_MODULE_ENABLE_WAIT; i++) {
68*4882a593Smuzhiyun if ((ti_clk_ll_ops->clk_readl(reg) & mask) == ena)
69*4882a593Smuzhiyun break;
70*4882a593Smuzhiyun udelay(1);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun if (i < MAX_MODULE_ENABLE_WAIT)
74*4882a593Smuzhiyun pr_debug("omap clock: module associated with clock %s ready after %d loops\n",
75*4882a593Smuzhiyun name, i);
76*4882a593Smuzhiyun else
77*4882a593Smuzhiyun pr_err("omap clock: module associated with clock %s didn't enable in %d tries\n",
78*4882a593Smuzhiyun name, MAX_MODULE_ENABLE_WAIT);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun return (i < MAX_MODULE_ENABLE_WAIT) ? 1 : 0;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun * _omap2_module_wait_ready - wait for an OMAP module to leave IDLE
85*4882a593Smuzhiyun * @clk: struct clk * belonging to the module
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * If the necessary clocks for the OMAP hardware IP block that
88*4882a593Smuzhiyun * corresponds to clock @clk are enabled, then wait for the module to
89*4882a593Smuzhiyun * indicate readiness (i.e., to leave IDLE). This code does not
90*4882a593Smuzhiyun * belong in the clock code and will be moved in the medium term to
91*4882a593Smuzhiyun * module-dependent code. No return value.
92*4882a593Smuzhiyun */
_omap2_module_wait_ready(struct clk_hw_omap * clk)93*4882a593Smuzhiyun static void _omap2_module_wait_ready(struct clk_hw_omap *clk)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun struct clk_omap_reg companion_reg, idlest_reg;
96*4882a593Smuzhiyun u8 other_bit, idlest_bit, idlest_val, idlest_reg_id;
97*4882a593Smuzhiyun s16 prcm_mod;
98*4882a593Smuzhiyun int r;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /* Not all modules have multiple clocks that their IDLEST depends on */
101*4882a593Smuzhiyun if (clk->ops->find_companion) {
102*4882a593Smuzhiyun clk->ops->find_companion(clk, &companion_reg, &other_bit);
103*4882a593Smuzhiyun if (!(ti_clk_ll_ops->clk_readl(&companion_reg) &
104*4882a593Smuzhiyun (1 << other_bit)))
105*4882a593Smuzhiyun return;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun clk->ops->find_idlest(clk, &idlest_reg, &idlest_bit, &idlest_val);
109*4882a593Smuzhiyun r = ti_clk_ll_ops->cm_split_idlest_reg(&idlest_reg, &prcm_mod,
110*4882a593Smuzhiyun &idlest_reg_id);
111*4882a593Smuzhiyun if (r) {
112*4882a593Smuzhiyun /* IDLEST register not in the CM module */
113*4882a593Smuzhiyun _wait_idlest_generic(clk, &idlest_reg, (1 << idlest_bit),
114*4882a593Smuzhiyun idlest_val, clk_hw_get_name(&clk->hw));
115*4882a593Smuzhiyun } else {
116*4882a593Smuzhiyun ti_clk_ll_ops->cm_wait_module_ready(0, prcm_mod, idlest_reg_id,
117*4882a593Smuzhiyun idlest_bit);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /**
122*4882a593Smuzhiyun * omap2_clk_dflt_find_companion - find companion clock to @clk
123*4882a593Smuzhiyun * @clk: struct clk * to find the companion clock of
124*4882a593Smuzhiyun * @other_reg: void __iomem ** to return the companion clock CM_*CLKEN va in
125*4882a593Smuzhiyun * @other_bit: u8 ** to return the companion clock bit shift in
126*4882a593Smuzhiyun *
127*4882a593Smuzhiyun * Note: We don't need special code here for INVERT_ENABLE for the
128*4882a593Smuzhiyun * time being since INVERT_ENABLE only applies to clocks enabled by
129*4882a593Smuzhiyun * CM_CLKEN_PLL
130*4882a593Smuzhiyun *
131*4882a593Smuzhiyun * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes it's
132*4882a593Smuzhiyun * just a matter of XORing the bits.
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * Some clocks don't have companion clocks. For example, modules with
135*4882a593Smuzhiyun * only an interface clock (such as MAILBOXES) don't have a companion
136*4882a593Smuzhiyun * clock. Right now, this code relies on the hardware exporting a bit
137*4882a593Smuzhiyun * in the correct companion register that indicates that the
138*4882a593Smuzhiyun * nonexistent 'companion clock' is active. Future patches will
139*4882a593Smuzhiyun * associate this type of code with per-module data structures to
140*4882a593Smuzhiyun * avoid this issue, and remove the casts. No return value.
141*4882a593Smuzhiyun */
omap2_clk_dflt_find_companion(struct clk_hw_omap * clk,struct clk_omap_reg * other_reg,u8 * other_bit)142*4882a593Smuzhiyun void omap2_clk_dflt_find_companion(struct clk_hw_omap *clk,
143*4882a593Smuzhiyun struct clk_omap_reg *other_reg,
144*4882a593Smuzhiyun u8 *other_bit)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun memcpy(other_reg, &clk->enable_reg, sizeof(*other_reg));
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /*
149*4882a593Smuzhiyun * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
150*4882a593Smuzhiyun * it's just a matter of XORing the bits.
151*4882a593Smuzhiyun */
152*4882a593Smuzhiyun other_reg->offset ^= (CM_FCLKEN ^ CM_ICLKEN);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun *other_bit = clk->enable_bit;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /**
158*4882a593Smuzhiyun * omap2_clk_dflt_find_idlest - find CM_IDLEST reg va, bit shift for @clk
159*4882a593Smuzhiyun * @clk: struct clk * to find IDLEST info for
160*4882a593Smuzhiyun * @idlest_reg: void __iomem ** to return the CM_IDLEST va in
161*4882a593Smuzhiyun * @idlest_bit: u8 * to return the CM_IDLEST bit shift in
162*4882a593Smuzhiyun * @idlest_val: u8 * to return the idle status indicator
163*4882a593Smuzhiyun *
164*4882a593Smuzhiyun * Return the CM_IDLEST register address and bit shift corresponding
165*4882a593Smuzhiyun * to the module that "owns" this clock. This default code assumes
166*4882a593Smuzhiyun * that the CM_IDLEST bit shift is the CM_*CLKEN bit shift, and that
167*4882a593Smuzhiyun * the IDLEST register address ID corresponds to the CM_*CLKEN
168*4882a593Smuzhiyun * register address ID (e.g., that CM_FCLKEN2 corresponds to
169*4882a593Smuzhiyun * CM_IDLEST2). This is not true for all modules. No return value.
170*4882a593Smuzhiyun */
omap2_clk_dflt_find_idlest(struct clk_hw_omap * clk,struct clk_omap_reg * idlest_reg,u8 * idlest_bit,u8 * idlest_val)171*4882a593Smuzhiyun void omap2_clk_dflt_find_idlest(struct clk_hw_omap *clk,
172*4882a593Smuzhiyun struct clk_omap_reg *idlest_reg, u8 *idlest_bit,
173*4882a593Smuzhiyun u8 *idlest_val)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun memcpy(idlest_reg, &clk->enable_reg, sizeof(*idlest_reg));
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun idlest_reg->offset &= ~0xf0;
178*4882a593Smuzhiyun idlest_reg->offset |= 0x20;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun *idlest_bit = clk->enable_bit;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /*
183*4882a593Smuzhiyun * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
184*4882a593Smuzhiyun * 34xx reverses this, just to keep us on our toes
185*4882a593Smuzhiyun * AM35xx uses both, depending on the module.
186*4882a593Smuzhiyun */
187*4882a593Smuzhiyun *idlest_val = ti_clk_get_features()->cm_idlest_val;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /**
191*4882a593Smuzhiyun * omap2_dflt_clk_enable - enable a clock in the hardware
192*4882a593Smuzhiyun * @hw: struct clk_hw * of the clock to enable
193*4882a593Smuzhiyun *
194*4882a593Smuzhiyun * Enable the clock @hw in the hardware. We first call into the OMAP
195*4882a593Smuzhiyun * clockdomain code to "enable" the corresponding clockdomain if this
196*4882a593Smuzhiyun * is the first enabled user of the clockdomain. Then program the
197*4882a593Smuzhiyun * hardware to enable the clock. Then wait for the IP block that uses
198*4882a593Smuzhiyun * this clock to leave idle (if applicable). Returns the error value
199*4882a593Smuzhiyun * from clkdm_clk_enable() if it terminated with an error, or -EINVAL
200*4882a593Smuzhiyun * if @hw has a null clock enable_reg, or zero upon success.
201*4882a593Smuzhiyun */
omap2_dflt_clk_enable(struct clk_hw * hw)202*4882a593Smuzhiyun int omap2_dflt_clk_enable(struct clk_hw *hw)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun struct clk_hw_omap *clk;
205*4882a593Smuzhiyun u32 v;
206*4882a593Smuzhiyun int ret = 0;
207*4882a593Smuzhiyun bool clkdm_control;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (ti_clk_get_features()->flags & TI_CLK_DISABLE_CLKDM_CONTROL)
210*4882a593Smuzhiyun clkdm_control = false;
211*4882a593Smuzhiyun else
212*4882a593Smuzhiyun clkdm_control = true;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun clk = to_clk_hw_omap(hw);
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun if (clkdm_control && clk->clkdm) {
217*4882a593Smuzhiyun ret = ti_clk_ll_ops->clkdm_clk_enable(clk->clkdm, hw->clk);
218*4882a593Smuzhiyun if (ret) {
219*4882a593Smuzhiyun WARN(1,
220*4882a593Smuzhiyun "%s: could not enable %s's clockdomain %s: %d\n",
221*4882a593Smuzhiyun __func__, clk_hw_get_name(hw),
222*4882a593Smuzhiyun clk->clkdm_name, ret);
223*4882a593Smuzhiyun return ret;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun /* FIXME should not have INVERT_ENABLE bit here */
228*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&clk->enable_reg);
229*4882a593Smuzhiyun if (clk->flags & INVERT_ENABLE)
230*4882a593Smuzhiyun v &= ~(1 << clk->enable_bit);
231*4882a593Smuzhiyun else
232*4882a593Smuzhiyun v |= (1 << clk->enable_bit);
233*4882a593Smuzhiyun ti_clk_ll_ops->clk_writel(v, &clk->enable_reg);
234*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&clk->enable_reg); /* OCP barrier */
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun if (clk->ops && clk->ops->find_idlest)
237*4882a593Smuzhiyun _omap2_module_wait_ready(clk);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun return 0;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /**
243*4882a593Smuzhiyun * omap2_dflt_clk_disable - disable a clock in the hardware
244*4882a593Smuzhiyun * @hw: struct clk_hw * of the clock to disable
245*4882a593Smuzhiyun *
246*4882a593Smuzhiyun * Disable the clock @hw in the hardware, and call into the OMAP
247*4882a593Smuzhiyun * clockdomain code to "disable" the corresponding clockdomain if all
248*4882a593Smuzhiyun * clocks/hwmods in that clockdomain are now disabled. No return
249*4882a593Smuzhiyun * value.
250*4882a593Smuzhiyun */
omap2_dflt_clk_disable(struct clk_hw * hw)251*4882a593Smuzhiyun void omap2_dflt_clk_disable(struct clk_hw *hw)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun struct clk_hw_omap *clk;
254*4882a593Smuzhiyun u32 v;
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun clk = to_clk_hw_omap(hw);
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&clk->enable_reg);
259*4882a593Smuzhiyun if (clk->flags & INVERT_ENABLE)
260*4882a593Smuzhiyun v |= (1 << clk->enable_bit);
261*4882a593Smuzhiyun else
262*4882a593Smuzhiyun v &= ~(1 << clk->enable_bit);
263*4882a593Smuzhiyun ti_clk_ll_ops->clk_writel(v, &clk->enable_reg);
264*4882a593Smuzhiyun /* No OCP barrier needed here since it is a disable operation */
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun if (!(ti_clk_get_features()->flags & TI_CLK_DISABLE_CLKDM_CONTROL) &&
267*4882a593Smuzhiyun clk->clkdm)
268*4882a593Smuzhiyun ti_clk_ll_ops->clkdm_clk_disable(clk->clkdm, hw->clk);
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /**
272*4882a593Smuzhiyun * omap2_dflt_clk_is_enabled - is clock enabled in the hardware?
273*4882a593Smuzhiyun * @hw: struct clk_hw * to check
274*4882a593Smuzhiyun *
275*4882a593Smuzhiyun * Return 1 if the clock represented by @hw is enabled in the
276*4882a593Smuzhiyun * hardware, or 0 otherwise. Intended for use in the struct
277*4882a593Smuzhiyun * clk_ops.is_enabled function pointer.
278*4882a593Smuzhiyun */
omap2_dflt_clk_is_enabled(struct clk_hw * hw)279*4882a593Smuzhiyun int omap2_dflt_clk_is_enabled(struct clk_hw *hw)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun struct clk_hw_omap *clk = to_clk_hw_omap(hw);
282*4882a593Smuzhiyun u32 v;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun v = ti_clk_ll_ops->clk_readl(&clk->enable_reg);
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun if (clk->flags & INVERT_ENABLE)
287*4882a593Smuzhiyun v ^= BIT(clk->enable_bit);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun v &= BIT(clk->enable_bit);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun return v ? 1 : 0;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun const struct clk_hw_omap_ops clkhwops_wait = {
295*4882a593Smuzhiyun .find_idlest = omap2_clk_dflt_find_idlest,
296*4882a593Smuzhiyun .find_companion = omap2_clk_dflt_find_companion,
297*4882a593Smuzhiyun };
298