1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 #include <linux/kernel.h>
8 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/clk-provider.h>
15 #define SUPER_STATE_IDLE 0
16 #define SUPER_STATE_RUN 1
17 #define SUPER_STATE_IRQ 2
18 #define SUPER_STATE_FIQ 3
20 #define SUPER_STATE_SHIFT 28
21 #define SUPER_STATE_MASK ((BIT(SUPER_STATE_IDLE) | BIT(SUPER_STATE_RUN) | \
22 BIT(SUPER_STATE_IRQ) | BIT(SUPER_STATE_FIQ)) \
25 #define SUPER_LP_DIV2_BYPASS (1 << 16)
27 #define super_state(s) (BIT(s) << SUPER_STATE_SHIFT)
28 #define super_state_to_src_shift(m, s) ((m->width * s))
29 #define super_state_to_src_mask(m) (((1 << m->width) - 1))
31 #define CCLK_SRC_PLLP_OUT0 4
32 #define CCLK_SRC_PLLP_OUT4 5
34 static u8
clk_super_get_parent(struct clk_hw
*hw
)
36 struct tegra_clk_super_mux
*mux
= to_clk_super_mux(hw
);
40 val
= readl_relaxed(mux
->reg
);
42 state
= val
& SUPER_STATE_MASK
;
44 BUG_ON((state
!= super_state(SUPER_STATE_RUN
)) &&
45 (state
!= super_state(SUPER_STATE_IDLE
)));
46 shift
= (state
== super_state(SUPER_STATE_IDLE
)) ?
47 super_state_to_src_shift(mux
, SUPER_STATE_IDLE
) :
48 super_state_to_src_shift(mux
, SUPER_STATE_RUN
);
50 source
= (val
>> shift
) & super_state_to_src_mask(mux
);
53 * If LP_DIV2_BYPASS is not set and PLLX is current parent then
54 * PLLX/2 is the input source to CCLKLP.
56 if ((mux
->flags
& TEGRA_DIVIDER_2
) && !(val
& SUPER_LP_DIV2_BYPASS
) &&
57 (source
== mux
->pllx_index
))
58 source
= mux
->div2_index
;
63 static int clk_super_set_parent(struct clk_hw
*hw
, u8 index
)
65 struct tegra_clk_super_mux
*mux
= to_clk_super_mux(hw
);
68 u8 parent_index
, shift
;
69 unsigned long flags
= 0;
72 spin_lock_irqsave(mux
->lock
, flags
);
74 val
= readl_relaxed(mux
->reg
);
75 state
= val
& SUPER_STATE_MASK
;
76 BUG_ON((state
!= super_state(SUPER_STATE_RUN
)) &&
77 (state
!= super_state(SUPER_STATE_IDLE
)));
78 shift
= (state
== super_state(SUPER_STATE_IDLE
)) ?
79 super_state_to_src_shift(mux
, SUPER_STATE_IDLE
) :
80 super_state_to_src_shift(mux
, SUPER_STATE_RUN
);
83 * For LP mode super-clock switch between PLLX direct
84 * and divided-by-2 outputs is allowed only when other
85 * than PLLX clock source is current parent.
87 if ((mux
->flags
& TEGRA_DIVIDER_2
) && ((index
== mux
->div2_index
) ||
88 (index
== mux
->pllx_index
))) {
89 parent_index
= clk_super_get_parent(hw
);
90 if ((parent_index
== mux
->div2_index
) ||
91 (parent_index
== mux
->pllx_index
)) {
96 val
^= SUPER_LP_DIV2_BYPASS
;
97 writel_relaxed(val
, mux
->reg
);
100 if (index
== mux
->div2_index
)
101 index
= mux
->pllx_index
;
104 /* enable PLLP branches to CPU before selecting PLLP source */
105 if ((mux
->flags
& TEGRA210_CPU_CLK
) &&
106 (index
== CCLK_SRC_PLLP_OUT0
|| index
== CCLK_SRC_PLLP_OUT4
))
107 tegra_clk_set_pllp_out_cpu(true);
109 val
&= ~((super_state_to_src_mask(mux
)) << shift
);
110 val
|= (index
& (super_state_to_src_mask(mux
))) << shift
;
112 writel_relaxed(val
, mux
->reg
);
115 /* disable PLLP branches to CPU if not used */
116 if ((mux
->flags
& TEGRA210_CPU_CLK
) &&
117 index
!= CCLK_SRC_PLLP_OUT0
&& index
!= CCLK_SRC_PLLP_OUT4
)
118 tegra_clk_set_pllp_out_cpu(false);
122 spin_unlock_irqrestore(mux
->lock
, flags
);
127 static void clk_super_mux_restore_context(struct clk_hw
*hw
)
131 parent_id
= clk_hw_get_parent_index(hw
);
132 if (WARN_ON(parent_id
< 0))
135 clk_super_set_parent(hw
, parent_id
);
138 static const struct clk_ops tegra_clk_super_mux_ops
= {
139 .get_parent
= clk_super_get_parent
,
140 .set_parent
= clk_super_set_parent
,
141 .restore_context
= clk_super_mux_restore_context
,
144 static long clk_super_round_rate(struct clk_hw
*hw
, unsigned long rate
,
145 unsigned long *parent_rate
)
147 struct tegra_clk_super_mux
*super
= to_clk_super_mux(hw
);
148 struct clk_hw
*div_hw
= &super
->frac_div
.hw
;
150 __clk_hw_set_clk(div_hw
, hw
);
152 return super
->div_ops
->round_rate(div_hw
, rate
, parent_rate
);
155 static unsigned long clk_super_recalc_rate(struct clk_hw
*hw
,
156 unsigned long parent_rate
)
158 struct tegra_clk_super_mux
*super
= to_clk_super_mux(hw
);
159 struct clk_hw
*div_hw
= &super
->frac_div
.hw
;
161 __clk_hw_set_clk(div_hw
, hw
);
163 return super
->div_ops
->recalc_rate(div_hw
, parent_rate
);
166 static int clk_super_set_rate(struct clk_hw
*hw
, unsigned long rate
,
167 unsigned long parent_rate
)
169 struct tegra_clk_super_mux
*super
= to_clk_super_mux(hw
);
170 struct clk_hw
*div_hw
= &super
->frac_div
.hw
;
172 __clk_hw_set_clk(div_hw
, hw
);
174 return super
->div_ops
->set_rate(div_hw
, rate
, parent_rate
);
177 static void clk_super_restore_context(struct clk_hw
*hw
)
179 struct tegra_clk_super_mux
*super
= to_clk_super_mux(hw
);
180 struct clk_hw
*div_hw
= &super
->frac_div
.hw
;
183 parent_id
= clk_hw_get_parent_index(hw
);
184 if (WARN_ON(parent_id
< 0))
187 super
->div_ops
->restore_context(div_hw
);
188 clk_super_set_parent(hw
, parent_id
);
191 const struct clk_ops tegra_clk_super_ops
= {
192 .get_parent
= clk_super_get_parent
,
193 .set_parent
= clk_super_set_parent
,
194 .set_rate
= clk_super_set_rate
,
195 .round_rate
= clk_super_round_rate
,
196 .recalc_rate
= clk_super_recalc_rate
,
197 .restore_context
= clk_super_restore_context
,
200 struct clk
*tegra_clk_register_super_mux(const char *name
,
201 const char **parent_names
, u8 num_parents
,
202 unsigned long flags
, void __iomem
*reg
, u8 clk_super_flags
,
203 u8 width
, u8 pllx_index
, u8 div2_index
, spinlock_t
*lock
)
205 struct tegra_clk_super_mux
*super
;
207 struct clk_init_data init
;
209 super
= kzalloc(sizeof(*super
), GFP_KERNEL
);
211 return ERR_PTR(-ENOMEM
);
214 init
.ops
= &tegra_clk_super_mux_ops
;
216 init
.parent_names
= parent_names
;
217 init
.num_parents
= num_parents
;
220 super
->pllx_index
= pllx_index
;
221 super
->div2_index
= div2_index
;
223 super
->width
= width
;
224 super
->flags
= clk_super_flags
;
226 /* Data in .init is copied by clk_register(), so stack variable OK */
227 super
->hw
.init
= &init
;
229 clk
= clk_register(NULL
, &super
->hw
);
236 struct clk
*tegra_clk_register_super_clk(const char *name
,
237 const char * const *parent_names
, u8 num_parents
,
238 unsigned long flags
, void __iomem
*reg
, u8 clk_super_flags
,
241 struct tegra_clk_super_mux
*super
;
243 struct clk_init_data init
;
245 super
= kzalloc(sizeof(*super
), GFP_KERNEL
);
247 return ERR_PTR(-ENOMEM
);
250 init
.ops
= &tegra_clk_super_ops
;
252 init
.parent_names
= parent_names
;
253 init
.num_parents
= num_parents
;
258 super
->flags
= clk_super_flags
;
259 super
->frac_div
.reg
= reg
+ 4;
260 super
->frac_div
.shift
= 16;
261 super
->frac_div
.width
= 8;
262 super
->frac_div
.frac_width
= 1;
263 super
->frac_div
.lock
= lock
;
264 super
->div_ops
= &tegra_clk_frac_div_ops
;
266 /* Data in .init is copied by clk_register(), so stack variable OK */
267 super
->hw
.init
= &init
;
269 clk
= clk_register(NULL
, &super
->hw
);