1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2013 Broadcom Corporation
3*4882a593Smuzhiyun * Copyright 2013 Linaro Limited
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or
6*4882a593Smuzhiyun * modify it under the terms of the GNU General Public License as
7*4882a593Smuzhiyun * published by the Free Software Foundation version 2.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * This program is distributed "as is" WITHOUT ANY WARRANTY of any
10*4882a593Smuzhiyun * kind, whether express or implied; without even the implied warranty
11*4882a593Smuzhiyun * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*4882a593Smuzhiyun * GNU General Public License for more details.
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include "clk-kona.h"
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <linux/delay.h>
18*4882a593Smuzhiyun #include <linux/io.h>
19*4882a593Smuzhiyun #include <linux/kernel.h>
20*4882a593Smuzhiyun #include <linux/clk-provider.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun * "Policies" affect the frequencies of bus clocks provided by a
24*4882a593Smuzhiyun * CCU. (I believe these polices are named "Deep Sleep", "Economy",
25*4882a593Smuzhiyun * "Normal", and "Turbo".) A lower policy number has lower power
26*4882a593Smuzhiyun * consumption, and policy 2 is the default.
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun #define CCU_POLICY_COUNT 4
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #define CCU_ACCESS_PASSWORD 0xA5A500
31*4882a593Smuzhiyun #define CLK_GATE_DELAY_LOOP 2000
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /* Bitfield operations */
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /* Produces a mask of set bits covering a range of a 32-bit value */
bitfield_mask(u32 shift,u32 width)36*4882a593Smuzhiyun static inline u32 bitfield_mask(u32 shift, u32 width)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun return ((1 << width) - 1) << shift;
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /* Extract the value of a bitfield found within a given register value */
bitfield_extract(u32 reg_val,u32 shift,u32 width)42*4882a593Smuzhiyun static inline u32 bitfield_extract(u32 reg_val, u32 shift, u32 width)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun return (reg_val & bitfield_mask(shift, width)) >> shift;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /* Replace the value of a bitfield found within a given register value */
bitfield_replace(u32 reg_val,u32 shift,u32 width,u32 val)48*4882a593Smuzhiyun static inline u32 bitfield_replace(u32 reg_val, u32 shift, u32 width, u32 val)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun u32 mask = bitfield_mask(shift, width);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun return (reg_val & ~mask) | (val << shift);
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /* Divider and scaling helpers */
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /* Convert a divider into the scaled divisor value it represents. */
scaled_div_value(struct bcm_clk_div * div,u32 reg_div)58*4882a593Smuzhiyun static inline u64 scaled_div_value(struct bcm_clk_div *div, u32 reg_div)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun return (u64)reg_div + ((u64)1 << div->u.s.frac_width);
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /*
64*4882a593Smuzhiyun * Build a scaled divider value as close as possible to the
65*4882a593Smuzhiyun * given whole part (div_value) and fractional part (expressed
66*4882a593Smuzhiyun * in billionths).
67*4882a593Smuzhiyun */
scaled_div_build(struct bcm_clk_div * div,u32 div_value,u32 billionths)68*4882a593Smuzhiyun u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value, u32 billionths)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun u64 combined;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun BUG_ON(!div_value);
73*4882a593Smuzhiyun BUG_ON(billionths >= BILLION);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun combined = (u64)div_value * BILLION + billionths;
76*4882a593Smuzhiyun combined <<= div->u.s.frac_width;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun return DIV_ROUND_CLOSEST_ULL(combined, BILLION);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /* The scaled minimum divisor representable by a divider */
82*4882a593Smuzhiyun static inline u64
scaled_div_min(struct bcm_clk_div * div)83*4882a593Smuzhiyun scaled_div_min(struct bcm_clk_div *div)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun if (divider_is_fixed(div))
86*4882a593Smuzhiyun return (u64)div->u.fixed;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun return scaled_div_value(div, 0);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* The scaled maximum divisor representable by a divider */
scaled_div_max(struct bcm_clk_div * div)92*4882a593Smuzhiyun u64 scaled_div_max(struct bcm_clk_div *div)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun u32 reg_div;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun if (divider_is_fixed(div))
97*4882a593Smuzhiyun return (u64)div->u.fixed;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun reg_div = ((u32)1 << div->u.s.width) - 1;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun return scaled_div_value(div, reg_div);
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /*
105*4882a593Smuzhiyun * Convert a scaled divisor into its divider representation as
106*4882a593Smuzhiyun * stored in a divider register field.
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun static inline u32
divider(struct bcm_clk_div * div,u64 scaled_div)109*4882a593Smuzhiyun divider(struct bcm_clk_div *div, u64 scaled_div)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun BUG_ON(scaled_div < scaled_div_min(div));
112*4882a593Smuzhiyun BUG_ON(scaled_div > scaled_div_max(div));
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun return (u32)(scaled_div - ((u64)1 << div->u.s.frac_width));
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* Return a rate scaled for use when dividing by a scaled divisor. */
118*4882a593Smuzhiyun static inline u64
scale_rate(struct bcm_clk_div * div,u32 rate)119*4882a593Smuzhiyun scale_rate(struct bcm_clk_div *div, u32 rate)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun if (divider_is_fixed(div))
122*4882a593Smuzhiyun return (u64)rate;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun return (u64)rate << div->u.s.frac_width;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /* CCU access */
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /* Read a 32-bit register value from a CCU's address space. */
__ccu_read(struct ccu_data * ccu,u32 reg_offset)130*4882a593Smuzhiyun static inline u32 __ccu_read(struct ccu_data *ccu, u32 reg_offset)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun return readl(ccu->base + reg_offset);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun /* Write a 32-bit register value into a CCU's address space. */
136*4882a593Smuzhiyun static inline void
__ccu_write(struct ccu_data * ccu,u32 reg_offset,u32 reg_val)137*4882a593Smuzhiyun __ccu_write(struct ccu_data *ccu, u32 reg_offset, u32 reg_val)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun writel(reg_val, ccu->base + reg_offset);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
ccu_lock(struct ccu_data * ccu)142*4882a593Smuzhiyun static inline unsigned long ccu_lock(struct ccu_data *ccu)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun unsigned long flags;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun spin_lock_irqsave(&ccu->lock, flags);
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun return flags;
149*4882a593Smuzhiyun }
ccu_unlock(struct ccu_data * ccu,unsigned long flags)150*4882a593Smuzhiyun static inline void ccu_unlock(struct ccu_data *ccu, unsigned long flags)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun spin_unlock_irqrestore(&ccu->lock, flags);
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun * Enable/disable write access to CCU protected registers. The
157*4882a593Smuzhiyun * WR_ACCESS register for all CCUs is at offset 0.
158*4882a593Smuzhiyun */
__ccu_write_enable(struct ccu_data * ccu)159*4882a593Smuzhiyun static inline void __ccu_write_enable(struct ccu_data *ccu)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun if (ccu->write_enabled) {
162*4882a593Smuzhiyun pr_err("%s: access already enabled for %s\n", __func__,
163*4882a593Smuzhiyun ccu->name);
164*4882a593Smuzhiyun return;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun ccu->write_enabled = true;
167*4882a593Smuzhiyun __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD | 1);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
__ccu_write_disable(struct ccu_data * ccu)170*4882a593Smuzhiyun static inline void __ccu_write_disable(struct ccu_data *ccu)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun if (!ccu->write_enabled) {
173*4882a593Smuzhiyun pr_err("%s: access wasn't enabled for %s\n", __func__,
174*4882a593Smuzhiyun ccu->name);
175*4882a593Smuzhiyun return;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD);
179*4882a593Smuzhiyun ccu->write_enabled = false;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /*
183*4882a593Smuzhiyun * Poll a register in a CCU's address space, returning when the
184*4882a593Smuzhiyun * specified bit in that register's value is set (or clear). Delay
185*4882a593Smuzhiyun * a microsecond after each read of the register. Returns true if
186*4882a593Smuzhiyun * successful, or false if we gave up trying.
187*4882a593Smuzhiyun *
188*4882a593Smuzhiyun * Caller must ensure the CCU lock is held.
189*4882a593Smuzhiyun */
190*4882a593Smuzhiyun static inline bool
__ccu_wait_bit(struct ccu_data * ccu,u32 reg_offset,u32 bit,bool want)191*4882a593Smuzhiyun __ccu_wait_bit(struct ccu_data *ccu, u32 reg_offset, u32 bit, bool want)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun unsigned int tries;
194*4882a593Smuzhiyun u32 bit_mask = 1 << bit;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun for (tries = 0; tries < CLK_GATE_DELAY_LOOP; tries++) {
197*4882a593Smuzhiyun u32 val;
198*4882a593Smuzhiyun bool bit_val;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun val = __ccu_read(ccu, reg_offset);
201*4882a593Smuzhiyun bit_val = (val & bit_mask) != 0;
202*4882a593Smuzhiyun if (bit_val == want)
203*4882a593Smuzhiyun return true;
204*4882a593Smuzhiyun udelay(1);
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun pr_warn("%s: %s/0x%04x bit %u was never %s\n", __func__,
207*4882a593Smuzhiyun ccu->name, reg_offset, bit, want ? "set" : "clear");
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun return false;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun /* Policy operations */
213*4882a593Smuzhiyun
__ccu_policy_engine_start(struct ccu_data * ccu,bool sync)214*4882a593Smuzhiyun static bool __ccu_policy_engine_start(struct ccu_data *ccu, bool sync)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun struct bcm_policy_ctl *control = &ccu->policy.control;
217*4882a593Smuzhiyun u32 offset;
218*4882a593Smuzhiyun u32 go_bit;
219*4882a593Smuzhiyun u32 mask;
220*4882a593Smuzhiyun bool ret;
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* If we don't need to control policy for this CCU, we're done. */
223*4882a593Smuzhiyun if (!policy_ctl_exists(control))
224*4882a593Smuzhiyun return true;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun offset = control->offset;
227*4882a593Smuzhiyun go_bit = control->go_bit;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun /* Ensure we're not busy before we start */
230*4882a593Smuzhiyun ret = __ccu_wait_bit(ccu, offset, go_bit, false);
231*4882a593Smuzhiyun if (!ret) {
232*4882a593Smuzhiyun pr_err("%s: ccu %s policy engine wouldn't go idle\n",
233*4882a593Smuzhiyun __func__, ccu->name);
234*4882a593Smuzhiyun return false;
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun /*
238*4882a593Smuzhiyun * If it's a synchronous request, we'll wait for the voltage
239*4882a593Smuzhiyun * and frequency of the active load to stabilize before
240*4882a593Smuzhiyun * returning. To do this we select the active load by
241*4882a593Smuzhiyun * setting the ATL bit.
242*4882a593Smuzhiyun *
243*4882a593Smuzhiyun * An asynchronous request instead ramps the voltage in the
244*4882a593Smuzhiyun * background, and when that process stabilizes, the target
245*4882a593Smuzhiyun * load is copied to the active load and the CCU frequency
246*4882a593Smuzhiyun * is switched. We do this by selecting the target load
247*4882a593Smuzhiyun * (ATL bit clear) and setting the request auto-copy (AC bit
248*4882a593Smuzhiyun * set).
249*4882a593Smuzhiyun *
250*4882a593Smuzhiyun * Note, we do NOT read-modify-write this register.
251*4882a593Smuzhiyun */
252*4882a593Smuzhiyun mask = (u32)1 << go_bit;
253*4882a593Smuzhiyun if (sync)
254*4882a593Smuzhiyun mask |= 1 << control->atl_bit;
255*4882a593Smuzhiyun else
256*4882a593Smuzhiyun mask |= 1 << control->ac_bit;
257*4882a593Smuzhiyun __ccu_write(ccu, offset, mask);
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun /* Wait for indication that operation is complete. */
260*4882a593Smuzhiyun ret = __ccu_wait_bit(ccu, offset, go_bit, false);
261*4882a593Smuzhiyun if (!ret)
262*4882a593Smuzhiyun pr_err("%s: ccu %s policy engine never started\n",
263*4882a593Smuzhiyun __func__, ccu->name);
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun return ret;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
__ccu_policy_engine_stop(struct ccu_data * ccu)268*4882a593Smuzhiyun static bool __ccu_policy_engine_stop(struct ccu_data *ccu)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun struct bcm_lvm_en *enable = &ccu->policy.enable;
271*4882a593Smuzhiyun u32 offset;
272*4882a593Smuzhiyun u32 enable_bit;
273*4882a593Smuzhiyun bool ret;
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun /* If we don't need to control policy for this CCU, we're done. */
276*4882a593Smuzhiyun if (!policy_lvm_en_exists(enable))
277*4882a593Smuzhiyun return true;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /* Ensure we're not busy before we start */
280*4882a593Smuzhiyun offset = enable->offset;
281*4882a593Smuzhiyun enable_bit = enable->bit;
282*4882a593Smuzhiyun ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
283*4882a593Smuzhiyun if (!ret) {
284*4882a593Smuzhiyun pr_err("%s: ccu %s policy engine already stopped\n",
285*4882a593Smuzhiyun __func__, ccu->name);
286*4882a593Smuzhiyun return false;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /* Now set the bit to stop the engine (NO read-modify-write) */
290*4882a593Smuzhiyun __ccu_write(ccu, offset, (u32)1 << enable_bit);
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun /* Wait for indication that it has stopped. */
293*4882a593Smuzhiyun ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
294*4882a593Smuzhiyun if (!ret)
295*4882a593Smuzhiyun pr_err("%s: ccu %s policy engine never stopped\n",
296*4882a593Smuzhiyun __func__, ccu->name);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun return ret;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun /*
302*4882a593Smuzhiyun * A CCU has four operating conditions ("policies"), and some clocks
303*4882a593Smuzhiyun * can be disabled or enabled based on which policy is currently in
304*4882a593Smuzhiyun * effect. Such clocks have a bit in a "policy mask" register for
305*4882a593Smuzhiyun * each policy indicating whether the clock is enabled for that
306*4882a593Smuzhiyun * policy or not. The bit position for a clock is the same for all
307*4882a593Smuzhiyun * four registers, and the 32-bit registers are at consecutive
308*4882a593Smuzhiyun * addresses.
309*4882a593Smuzhiyun */
policy_init(struct ccu_data * ccu,struct bcm_clk_policy * policy)310*4882a593Smuzhiyun static bool policy_init(struct ccu_data *ccu, struct bcm_clk_policy *policy)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun u32 offset;
313*4882a593Smuzhiyun u32 mask;
314*4882a593Smuzhiyun int i;
315*4882a593Smuzhiyun bool ret;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun if (!policy_exists(policy))
318*4882a593Smuzhiyun return true;
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun /*
321*4882a593Smuzhiyun * We need to stop the CCU policy engine to allow update
322*4882a593Smuzhiyun * of our policy bits.
323*4882a593Smuzhiyun */
324*4882a593Smuzhiyun if (!__ccu_policy_engine_stop(ccu)) {
325*4882a593Smuzhiyun pr_err("%s: unable to stop CCU %s policy engine\n",
326*4882a593Smuzhiyun __func__, ccu->name);
327*4882a593Smuzhiyun return false;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun /*
331*4882a593Smuzhiyun * For now, if a clock defines its policy bit we just mark
332*4882a593Smuzhiyun * it "enabled" for all four policies.
333*4882a593Smuzhiyun */
334*4882a593Smuzhiyun offset = policy->offset;
335*4882a593Smuzhiyun mask = (u32)1 << policy->bit;
336*4882a593Smuzhiyun for (i = 0; i < CCU_POLICY_COUNT; i++) {
337*4882a593Smuzhiyun u32 reg_val;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun reg_val = __ccu_read(ccu, offset);
340*4882a593Smuzhiyun reg_val |= mask;
341*4882a593Smuzhiyun __ccu_write(ccu, offset, reg_val);
342*4882a593Smuzhiyun offset += sizeof(u32);
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun /* We're done updating; fire up the policy engine again. */
346*4882a593Smuzhiyun ret = __ccu_policy_engine_start(ccu, true);
347*4882a593Smuzhiyun if (!ret)
348*4882a593Smuzhiyun pr_err("%s: unable to restart CCU %s policy engine\n",
349*4882a593Smuzhiyun __func__, ccu->name);
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun return ret;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun /* Gate operations */
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun /* Determine whether a clock is gated. CCU lock must be held. */
357*4882a593Smuzhiyun static bool
__is_clk_gate_enabled(struct ccu_data * ccu,struct bcm_clk_gate * gate)358*4882a593Smuzhiyun __is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun u32 bit_mask;
361*4882a593Smuzhiyun u32 reg_val;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun /* If there is no gate we can assume it's enabled. */
364*4882a593Smuzhiyun if (!gate_exists(gate))
365*4882a593Smuzhiyun return true;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun bit_mask = 1 << gate->status_bit;
368*4882a593Smuzhiyun reg_val = __ccu_read(ccu, gate->offset);
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun return (reg_val & bit_mask) != 0;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun /* Determine whether a clock is gated. */
374*4882a593Smuzhiyun static bool
is_clk_gate_enabled(struct ccu_data * ccu,struct bcm_clk_gate * gate)375*4882a593Smuzhiyun is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun long flags;
378*4882a593Smuzhiyun bool ret;
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun /* Avoid taking the lock if we can */
381*4882a593Smuzhiyun if (!gate_exists(gate))
382*4882a593Smuzhiyun return true;
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun flags = ccu_lock(ccu);
385*4882a593Smuzhiyun ret = __is_clk_gate_enabled(ccu, gate);
386*4882a593Smuzhiyun ccu_unlock(ccu, flags);
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun return ret;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun /*
392*4882a593Smuzhiyun * Commit our desired gate state to the hardware.
393*4882a593Smuzhiyun * Returns true if successful, false otherwise.
394*4882a593Smuzhiyun */
395*4882a593Smuzhiyun static bool
__gate_commit(struct ccu_data * ccu,struct bcm_clk_gate * gate)396*4882a593Smuzhiyun __gate_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun u32 reg_val;
399*4882a593Smuzhiyun u32 mask;
400*4882a593Smuzhiyun bool enabled = false;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun BUG_ON(!gate_exists(gate));
403*4882a593Smuzhiyun if (!gate_is_sw_controllable(gate))
404*4882a593Smuzhiyun return true; /* Nothing we can change */
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun reg_val = __ccu_read(ccu, gate->offset);
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun /* For a hardware/software gate, set which is in control */
409*4882a593Smuzhiyun if (gate_is_hw_controllable(gate)) {
410*4882a593Smuzhiyun mask = (u32)1 << gate->hw_sw_sel_bit;
411*4882a593Smuzhiyun if (gate_is_sw_managed(gate))
412*4882a593Smuzhiyun reg_val |= mask;
413*4882a593Smuzhiyun else
414*4882a593Smuzhiyun reg_val &= ~mask;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun /*
418*4882a593Smuzhiyun * If software is in control, enable or disable the gate.
419*4882a593Smuzhiyun * If hardware is, clear the enabled bit for good measure.
420*4882a593Smuzhiyun * If a software controlled gate can't be disabled, we're
421*4882a593Smuzhiyun * required to write a 0 into the enable bit (but the gate
422*4882a593Smuzhiyun * will be enabled).
423*4882a593Smuzhiyun */
424*4882a593Smuzhiyun mask = (u32)1 << gate->en_bit;
425*4882a593Smuzhiyun if (gate_is_sw_managed(gate) && (enabled = gate_is_enabled(gate)) &&
426*4882a593Smuzhiyun !gate_is_no_disable(gate))
427*4882a593Smuzhiyun reg_val |= mask;
428*4882a593Smuzhiyun else
429*4882a593Smuzhiyun reg_val &= ~mask;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun __ccu_write(ccu, gate->offset, reg_val);
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun /* For a hardware controlled gate, we're done */
434*4882a593Smuzhiyun if (!gate_is_sw_managed(gate))
435*4882a593Smuzhiyun return true;
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun /* Otherwise wait for the gate to be in desired state */
438*4882a593Smuzhiyun return __ccu_wait_bit(ccu, gate->offset, gate->status_bit, enabled);
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /*
442*4882a593Smuzhiyun * Initialize a gate. Our desired state (hardware/software select,
443*4882a593Smuzhiyun * and if software, its enable state) is committed to hardware
444*4882a593Smuzhiyun * without the usual checks to see if it's already set up that way.
445*4882a593Smuzhiyun * Returns true if successful, false otherwise.
446*4882a593Smuzhiyun */
gate_init(struct ccu_data * ccu,struct bcm_clk_gate * gate)447*4882a593Smuzhiyun static bool gate_init(struct ccu_data *ccu, struct bcm_clk_gate *gate)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun if (!gate_exists(gate))
450*4882a593Smuzhiyun return true;
451*4882a593Smuzhiyun return __gate_commit(ccu, gate);
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun /*
455*4882a593Smuzhiyun * Set a gate to enabled or disabled state. Does nothing if the
456*4882a593Smuzhiyun * gate is not currently under software control, or if it is already
457*4882a593Smuzhiyun * in the requested state. Returns true if successful, false
458*4882a593Smuzhiyun * otherwise. CCU lock must be held.
459*4882a593Smuzhiyun */
460*4882a593Smuzhiyun static bool
__clk_gate(struct ccu_data * ccu,struct bcm_clk_gate * gate,bool enable)461*4882a593Smuzhiyun __clk_gate(struct ccu_data *ccu, struct bcm_clk_gate *gate, bool enable)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun bool ret;
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun if (!gate_exists(gate) || !gate_is_sw_managed(gate))
466*4882a593Smuzhiyun return true; /* Nothing to do */
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun if (!enable && gate_is_no_disable(gate)) {
469*4882a593Smuzhiyun pr_warn("%s: invalid gate disable request (ignoring)\n",
470*4882a593Smuzhiyun __func__);
471*4882a593Smuzhiyun return true;
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun if (enable == gate_is_enabled(gate))
475*4882a593Smuzhiyun return true; /* No change */
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun gate_flip_enabled(gate);
478*4882a593Smuzhiyun ret = __gate_commit(ccu, gate);
479*4882a593Smuzhiyun if (!ret)
480*4882a593Smuzhiyun gate_flip_enabled(gate); /* Revert the change */
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun return ret;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun /* Enable or disable a gate. Returns 0 if successful, -EIO otherwise */
clk_gate(struct ccu_data * ccu,const char * name,struct bcm_clk_gate * gate,bool enable)486*4882a593Smuzhiyun static int clk_gate(struct ccu_data *ccu, const char *name,
487*4882a593Smuzhiyun struct bcm_clk_gate *gate, bool enable)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun unsigned long flags;
490*4882a593Smuzhiyun bool success;
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun /*
493*4882a593Smuzhiyun * Avoid taking the lock if we can. We quietly ignore
494*4882a593Smuzhiyun * requests to change state that don't make sense.
495*4882a593Smuzhiyun */
496*4882a593Smuzhiyun if (!gate_exists(gate) || !gate_is_sw_managed(gate))
497*4882a593Smuzhiyun return 0;
498*4882a593Smuzhiyun if (!enable && gate_is_no_disable(gate))
499*4882a593Smuzhiyun return 0;
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun flags = ccu_lock(ccu);
502*4882a593Smuzhiyun __ccu_write_enable(ccu);
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun success = __clk_gate(ccu, gate, enable);
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun __ccu_write_disable(ccu);
507*4882a593Smuzhiyun ccu_unlock(ccu, flags);
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun if (success)
510*4882a593Smuzhiyun return 0;
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun pr_err("%s: failed to %s gate for %s\n", __func__,
513*4882a593Smuzhiyun enable ? "enable" : "disable", name);
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun return -EIO;
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun /* Hysteresis operations */
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun /*
521*4882a593Smuzhiyun * If a clock gate requires a turn-off delay it will have
522*4882a593Smuzhiyun * "hysteresis" register bits defined. The first, if set, enables
523*4882a593Smuzhiyun * the delay; and if enabled, the second bit determines whether the
524*4882a593Smuzhiyun * delay is "low" or "high" (1 means high). For now, if it's
525*4882a593Smuzhiyun * defined for a clock, we set it.
526*4882a593Smuzhiyun */
hyst_init(struct ccu_data * ccu,struct bcm_clk_hyst * hyst)527*4882a593Smuzhiyun static bool hyst_init(struct ccu_data *ccu, struct bcm_clk_hyst *hyst)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun u32 offset;
530*4882a593Smuzhiyun u32 reg_val;
531*4882a593Smuzhiyun u32 mask;
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun if (!hyst_exists(hyst))
534*4882a593Smuzhiyun return true;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun offset = hyst->offset;
537*4882a593Smuzhiyun mask = (u32)1 << hyst->en_bit;
538*4882a593Smuzhiyun mask |= (u32)1 << hyst->val_bit;
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun reg_val = __ccu_read(ccu, offset);
541*4882a593Smuzhiyun reg_val |= mask;
542*4882a593Smuzhiyun __ccu_write(ccu, offset, reg_val);
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun return true;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun /* Trigger operations */
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun /*
550*4882a593Smuzhiyun * Caller must ensure CCU lock is held and access is enabled.
551*4882a593Smuzhiyun * Returns true if successful, false otherwise.
552*4882a593Smuzhiyun */
__clk_trigger(struct ccu_data * ccu,struct bcm_clk_trig * trig)553*4882a593Smuzhiyun static bool __clk_trigger(struct ccu_data *ccu, struct bcm_clk_trig *trig)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun /* Trigger the clock and wait for it to finish */
556*4882a593Smuzhiyun __ccu_write(ccu, trig->offset, 1 << trig->bit);
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun return __ccu_wait_bit(ccu, trig->offset, trig->bit, false);
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun /* Divider operations */
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun /* Read a divider value and return the scaled divisor it represents. */
divider_read_scaled(struct ccu_data * ccu,struct bcm_clk_div * div)564*4882a593Smuzhiyun static u64 divider_read_scaled(struct ccu_data *ccu, struct bcm_clk_div *div)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun unsigned long flags;
567*4882a593Smuzhiyun u32 reg_val;
568*4882a593Smuzhiyun u32 reg_div;
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun if (divider_is_fixed(div))
571*4882a593Smuzhiyun return (u64)div->u.fixed;
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun flags = ccu_lock(ccu);
574*4882a593Smuzhiyun reg_val = __ccu_read(ccu, div->u.s.offset);
575*4882a593Smuzhiyun ccu_unlock(ccu, flags);
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun /* Extract the full divider field from the register value */
578*4882a593Smuzhiyun reg_div = bitfield_extract(reg_val, div->u.s.shift, div->u.s.width);
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun /* Return the scaled divisor value it represents */
581*4882a593Smuzhiyun return scaled_div_value(div, reg_div);
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun /*
585*4882a593Smuzhiyun * Convert a divider's scaled divisor value into its recorded form
586*4882a593Smuzhiyun * and commit it into the hardware divider register.
587*4882a593Smuzhiyun *
588*4882a593Smuzhiyun * Returns 0 on success. Returns -EINVAL for invalid arguments.
589*4882a593Smuzhiyun * Returns -ENXIO if gating failed, and -EIO if a trigger failed.
590*4882a593Smuzhiyun */
__div_commit(struct ccu_data * ccu,struct bcm_clk_gate * gate,struct bcm_clk_div * div,struct bcm_clk_trig * trig)591*4882a593Smuzhiyun static int __div_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate,
592*4882a593Smuzhiyun struct bcm_clk_div *div, struct bcm_clk_trig *trig)
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun bool enabled;
595*4882a593Smuzhiyun u32 reg_div;
596*4882a593Smuzhiyun u32 reg_val;
597*4882a593Smuzhiyun int ret = 0;
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun BUG_ON(divider_is_fixed(div));
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun /*
602*4882a593Smuzhiyun * If we're just initializing the divider, and no initial
603*4882a593Smuzhiyun * state was defined in the device tree, we just find out
604*4882a593Smuzhiyun * what its current value is rather than updating it.
605*4882a593Smuzhiyun */
606*4882a593Smuzhiyun if (div->u.s.scaled_div == BAD_SCALED_DIV_VALUE) {
607*4882a593Smuzhiyun reg_val = __ccu_read(ccu, div->u.s.offset);
608*4882a593Smuzhiyun reg_div = bitfield_extract(reg_val, div->u.s.shift,
609*4882a593Smuzhiyun div->u.s.width);
610*4882a593Smuzhiyun div->u.s.scaled_div = scaled_div_value(div, reg_div);
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun return 0;
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun /* Convert the scaled divisor to the value we need to record */
616*4882a593Smuzhiyun reg_div = divider(div, div->u.s.scaled_div);
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun /* Clock needs to be enabled before changing the rate */
619*4882a593Smuzhiyun enabled = __is_clk_gate_enabled(ccu, gate);
620*4882a593Smuzhiyun if (!enabled && !__clk_gate(ccu, gate, true)) {
621*4882a593Smuzhiyun ret = -ENXIO;
622*4882a593Smuzhiyun goto out;
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun
625*4882a593Smuzhiyun /* Replace the divider value and record the result */
626*4882a593Smuzhiyun reg_val = __ccu_read(ccu, div->u.s.offset);
627*4882a593Smuzhiyun reg_val = bitfield_replace(reg_val, div->u.s.shift, div->u.s.width,
628*4882a593Smuzhiyun reg_div);
629*4882a593Smuzhiyun __ccu_write(ccu, div->u.s.offset, reg_val);
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun /* If the trigger fails we still want to disable the gate */
632*4882a593Smuzhiyun if (!__clk_trigger(ccu, trig))
633*4882a593Smuzhiyun ret = -EIO;
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun /* Disable the clock again if it was disabled to begin with */
636*4882a593Smuzhiyun if (!enabled && !__clk_gate(ccu, gate, false))
637*4882a593Smuzhiyun ret = ret ? ret : -ENXIO; /* return first error */
638*4882a593Smuzhiyun out:
639*4882a593Smuzhiyun return ret;
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun /*
643*4882a593Smuzhiyun * Initialize a divider by committing our desired state to hardware
644*4882a593Smuzhiyun * without the usual checks to see if it's already set up that way.
645*4882a593Smuzhiyun * Returns true if successful, false otherwise.
646*4882a593Smuzhiyun */
div_init(struct ccu_data * ccu,struct bcm_clk_gate * gate,struct bcm_clk_div * div,struct bcm_clk_trig * trig)647*4882a593Smuzhiyun static bool div_init(struct ccu_data *ccu, struct bcm_clk_gate *gate,
648*4882a593Smuzhiyun struct bcm_clk_div *div, struct bcm_clk_trig *trig)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun if (!divider_exists(div) || divider_is_fixed(div))
651*4882a593Smuzhiyun return true;
652*4882a593Smuzhiyun return !__div_commit(ccu, gate, div, trig);
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun
divider_write(struct ccu_data * ccu,struct bcm_clk_gate * gate,struct bcm_clk_div * div,struct bcm_clk_trig * trig,u64 scaled_div)655*4882a593Smuzhiyun static int divider_write(struct ccu_data *ccu, struct bcm_clk_gate *gate,
656*4882a593Smuzhiyun struct bcm_clk_div *div, struct bcm_clk_trig *trig,
657*4882a593Smuzhiyun u64 scaled_div)
658*4882a593Smuzhiyun {
659*4882a593Smuzhiyun unsigned long flags;
660*4882a593Smuzhiyun u64 previous;
661*4882a593Smuzhiyun int ret;
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun BUG_ON(divider_is_fixed(div));
664*4882a593Smuzhiyun
665*4882a593Smuzhiyun previous = div->u.s.scaled_div;
666*4882a593Smuzhiyun if (previous == scaled_div)
667*4882a593Smuzhiyun return 0; /* No change */
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun div->u.s.scaled_div = scaled_div;
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun flags = ccu_lock(ccu);
672*4882a593Smuzhiyun __ccu_write_enable(ccu);
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun ret = __div_commit(ccu, gate, div, trig);
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun __ccu_write_disable(ccu);
677*4882a593Smuzhiyun ccu_unlock(ccu, flags);
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun if (ret)
680*4882a593Smuzhiyun div->u.s.scaled_div = previous; /* Revert the change */
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun return ret;
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun /* Common clock rate helpers */
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun /*
689*4882a593Smuzhiyun * Implement the common clock framework recalc_rate method, taking
690*4882a593Smuzhiyun * into account a divider and an optional pre-divider. The
691*4882a593Smuzhiyun * pre-divider register pointer may be NULL.
692*4882a593Smuzhiyun */
clk_recalc_rate(struct ccu_data * ccu,struct bcm_clk_div * div,struct bcm_clk_div * pre_div,unsigned long parent_rate)693*4882a593Smuzhiyun static unsigned long clk_recalc_rate(struct ccu_data *ccu,
694*4882a593Smuzhiyun struct bcm_clk_div *div, struct bcm_clk_div *pre_div,
695*4882a593Smuzhiyun unsigned long parent_rate)
696*4882a593Smuzhiyun {
697*4882a593Smuzhiyun u64 scaled_parent_rate;
698*4882a593Smuzhiyun u64 scaled_div;
699*4882a593Smuzhiyun u64 result;
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun if (!divider_exists(div))
702*4882a593Smuzhiyun return parent_rate;
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun if (parent_rate > (unsigned long)LONG_MAX)
705*4882a593Smuzhiyun return 0; /* actually this would be a caller bug */
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun /*
708*4882a593Smuzhiyun * If there is a pre-divider, divide the scaled parent rate
709*4882a593Smuzhiyun * by the pre-divider value first. In this case--to improve
710*4882a593Smuzhiyun * accuracy--scale the parent rate by *both* the pre-divider
711*4882a593Smuzhiyun * value and the divider before actually computing the
712*4882a593Smuzhiyun * result of the pre-divider.
713*4882a593Smuzhiyun *
714*4882a593Smuzhiyun * If there's only one divider, just scale the parent rate.
715*4882a593Smuzhiyun */
716*4882a593Smuzhiyun if (pre_div && divider_exists(pre_div)) {
717*4882a593Smuzhiyun u64 scaled_rate;
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun scaled_rate = scale_rate(pre_div, parent_rate);
720*4882a593Smuzhiyun scaled_rate = scale_rate(div, scaled_rate);
721*4882a593Smuzhiyun scaled_div = divider_read_scaled(ccu, pre_div);
722*4882a593Smuzhiyun scaled_parent_rate = DIV_ROUND_CLOSEST_ULL(scaled_rate,
723*4882a593Smuzhiyun scaled_div);
724*4882a593Smuzhiyun } else {
725*4882a593Smuzhiyun scaled_parent_rate = scale_rate(div, parent_rate);
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun /*
729*4882a593Smuzhiyun * Get the scaled divisor value, and divide the scaled
730*4882a593Smuzhiyun * parent rate by that to determine this clock's resulting
731*4882a593Smuzhiyun * rate.
732*4882a593Smuzhiyun */
733*4882a593Smuzhiyun scaled_div = divider_read_scaled(ccu, div);
734*4882a593Smuzhiyun result = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, scaled_div);
735*4882a593Smuzhiyun
736*4882a593Smuzhiyun return (unsigned long)result;
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun /*
740*4882a593Smuzhiyun * Compute the output rate produced when a given parent rate is fed
741*4882a593Smuzhiyun * into two dividers. The pre-divider can be NULL, and even if it's
742*4882a593Smuzhiyun * non-null it may be nonexistent. It's also OK for the divider to
743*4882a593Smuzhiyun * be nonexistent, and in that case the pre-divider is also ignored.
744*4882a593Smuzhiyun *
745*4882a593Smuzhiyun * If scaled_div is non-null, it is used to return the scaled divisor
746*4882a593Smuzhiyun * value used by the (downstream) divider to produce that rate.
747*4882a593Smuzhiyun */
round_rate(struct ccu_data * ccu,struct bcm_clk_div * div,struct bcm_clk_div * pre_div,unsigned long rate,unsigned long parent_rate,u64 * scaled_div)748*4882a593Smuzhiyun static long round_rate(struct ccu_data *ccu, struct bcm_clk_div *div,
749*4882a593Smuzhiyun struct bcm_clk_div *pre_div,
750*4882a593Smuzhiyun unsigned long rate, unsigned long parent_rate,
751*4882a593Smuzhiyun u64 *scaled_div)
752*4882a593Smuzhiyun {
753*4882a593Smuzhiyun u64 scaled_parent_rate;
754*4882a593Smuzhiyun u64 min_scaled_div;
755*4882a593Smuzhiyun u64 max_scaled_div;
756*4882a593Smuzhiyun u64 best_scaled_div;
757*4882a593Smuzhiyun u64 result;
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun BUG_ON(!divider_exists(div));
760*4882a593Smuzhiyun BUG_ON(!rate);
761*4882a593Smuzhiyun BUG_ON(parent_rate > (u64)LONG_MAX);
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun /*
764*4882a593Smuzhiyun * If there is a pre-divider, divide the scaled parent rate
765*4882a593Smuzhiyun * by the pre-divider value first. In this case--to improve
766*4882a593Smuzhiyun * accuracy--scale the parent rate by *both* the pre-divider
767*4882a593Smuzhiyun * value and the divider before actually computing the
768*4882a593Smuzhiyun * result of the pre-divider.
769*4882a593Smuzhiyun *
770*4882a593Smuzhiyun * If there's only one divider, just scale the parent rate.
771*4882a593Smuzhiyun *
772*4882a593Smuzhiyun * For simplicity we treat the pre-divider as fixed (for now).
773*4882a593Smuzhiyun */
774*4882a593Smuzhiyun if (divider_exists(pre_div)) {
775*4882a593Smuzhiyun u64 scaled_rate;
776*4882a593Smuzhiyun u64 scaled_pre_div;
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun scaled_rate = scale_rate(pre_div, parent_rate);
779*4882a593Smuzhiyun scaled_rate = scale_rate(div, scaled_rate);
780*4882a593Smuzhiyun scaled_pre_div = divider_read_scaled(ccu, pre_div);
781*4882a593Smuzhiyun scaled_parent_rate = DIV_ROUND_CLOSEST_ULL(scaled_rate,
782*4882a593Smuzhiyun scaled_pre_div);
783*4882a593Smuzhiyun } else {
784*4882a593Smuzhiyun scaled_parent_rate = scale_rate(div, parent_rate);
785*4882a593Smuzhiyun }
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun /*
788*4882a593Smuzhiyun * Compute the best possible divider and ensure it is in
789*4882a593Smuzhiyun * range. A fixed divider can't be changed, so just report
790*4882a593Smuzhiyun * the best we can do.
791*4882a593Smuzhiyun */
792*4882a593Smuzhiyun if (!divider_is_fixed(div)) {
793*4882a593Smuzhiyun best_scaled_div = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate,
794*4882a593Smuzhiyun rate);
795*4882a593Smuzhiyun min_scaled_div = scaled_div_min(div);
796*4882a593Smuzhiyun max_scaled_div = scaled_div_max(div);
797*4882a593Smuzhiyun if (best_scaled_div > max_scaled_div)
798*4882a593Smuzhiyun best_scaled_div = max_scaled_div;
799*4882a593Smuzhiyun else if (best_scaled_div < min_scaled_div)
800*4882a593Smuzhiyun best_scaled_div = min_scaled_div;
801*4882a593Smuzhiyun } else {
802*4882a593Smuzhiyun best_scaled_div = divider_read_scaled(ccu, div);
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun
805*4882a593Smuzhiyun /* OK, figure out the resulting rate */
806*4882a593Smuzhiyun result = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, best_scaled_div);
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun if (scaled_div)
809*4882a593Smuzhiyun *scaled_div = best_scaled_div;
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun return (long)result;
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun /* Common clock parent helpers */
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun /*
817*4882a593Smuzhiyun * For a given parent selector (register field) value, find the
818*4882a593Smuzhiyun * index into a selector's parent_sel array that contains it.
819*4882a593Smuzhiyun * Returns the index, or BAD_CLK_INDEX if it's not found.
820*4882a593Smuzhiyun */
parent_index(struct bcm_clk_sel * sel,u8 parent_sel)821*4882a593Smuzhiyun static u8 parent_index(struct bcm_clk_sel *sel, u8 parent_sel)
822*4882a593Smuzhiyun {
823*4882a593Smuzhiyun u8 i;
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun BUG_ON(sel->parent_count > (u32)U8_MAX);
826*4882a593Smuzhiyun for (i = 0; i < sel->parent_count; i++)
827*4882a593Smuzhiyun if (sel->parent_sel[i] == parent_sel)
828*4882a593Smuzhiyun return i;
829*4882a593Smuzhiyun return BAD_CLK_INDEX;
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun
832*4882a593Smuzhiyun /*
833*4882a593Smuzhiyun * Fetch the current value of the selector, and translate that into
834*4882a593Smuzhiyun * its corresponding index in the parent array we registered with
835*4882a593Smuzhiyun * the clock framework.
836*4882a593Smuzhiyun *
837*4882a593Smuzhiyun * Returns parent array index that corresponds with the value found,
838*4882a593Smuzhiyun * or BAD_CLK_INDEX if the found value is out of range.
839*4882a593Smuzhiyun */
selector_read_index(struct ccu_data * ccu,struct bcm_clk_sel * sel)840*4882a593Smuzhiyun static u8 selector_read_index(struct ccu_data *ccu, struct bcm_clk_sel *sel)
841*4882a593Smuzhiyun {
842*4882a593Smuzhiyun unsigned long flags;
843*4882a593Smuzhiyun u32 reg_val;
844*4882a593Smuzhiyun u32 parent_sel;
845*4882a593Smuzhiyun u8 index;
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun /* If there's no selector, there's only one parent */
848*4882a593Smuzhiyun if (!selector_exists(sel))
849*4882a593Smuzhiyun return 0;
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun /* Get the value in the selector register */
852*4882a593Smuzhiyun flags = ccu_lock(ccu);
853*4882a593Smuzhiyun reg_val = __ccu_read(ccu, sel->offset);
854*4882a593Smuzhiyun ccu_unlock(ccu, flags);
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun /* Look up that selector's parent array index and return it */
859*4882a593Smuzhiyun index = parent_index(sel, parent_sel);
860*4882a593Smuzhiyun if (index == BAD_CLK_INDEX)
861*4882a593Smuzhiyun pr_err("%s: out-of-range parent selector %u (%s 0x%04x)\n",
862*4882a593Smuzhiyun __func__, parent_sel, ccu->name, sel->offset);
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun return index;
865*4882a593Smuzhiyun }
866*4882a593Smuzhiyun
867*4882a593Smuzhiyun /*
868*4882a593Smuzhiyun * Commit our desired selector value to the hardware.
869*4882a593Smuzhiyun *
870*4882a593Smuzhiyun * Returns 0 on success. Returns -EINVAL for invalid arguments.
871*4882a593Smuzhiyun * Returns -ENXIO if gating failed, and -EIO if a trigger failed.
872*4882a593Smuzhiyun */
873*4882a593Smuzhiyun static int
__sel_commit(struct ccu_data * ccu,struct bcm_clk_gate * gate,struct bcm_clk_sel * sel,struct bcm_clk_trig * trig)874*4882a593Smuzhiyun __sel_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate,
875*4882a593Smuzhiyun struct bcm_clk_sel *sel, struct bcm_clk_trig *trig)
876*4882a593Smuzhiyun {
877*4882a593Smuzhiyun u32 parent_sel;
878*4882a593Smuzhiyun u32 reg_val;
879*4882a593Smuzhiyun bool enabled;
880*4882a593Smuzhiyun int ret = 0;
881*4882a593Smuzhiyun
882*4882a593Smuzhiyun BUG_ON(!selector_exists(sel));
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun /*
885*4882a593Smuzhiyun * If we're just initializing the selector, and no initial
886*4882a593Smuzhiyun * state was defined in the device tree, we just find out
887*4882a593Smuzhiyun * what its current value is rather than updating it.
888*4882a593Smuzhiyun */
889*4882a593Smuzhiyun if (sel->clk_index == BAD_CLK_INDEX) {
890*4882a593Smuzhiyun u8 index;
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun reg_val = __ccu_read(ccu, sel->offset);
893*4882a593Smuzhiyun parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
894*4882a593Smuzhiyun index = parent_index(sel, parent_sel);
895*4882a593Smuzhiyun if (index == BAD_CLK_INDEX)
896*4882a593Smuzhiyun return -EINVAL;
897*4882a593Smuzhiyun sel->clk_index = index;
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun return 0;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun BUG_ON((u32)sel->clk_index >= sel->parent_count);
903*4882a593Smuzhiyun parent_sel = sel->parent_sel[sel->clk_index];
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun /* Clock needs to be enabled before changing the parent */
906*4882a593Smuzhiyun enabled = __is_clk_gate_enabled(ccu, gate);
907*4882a593Smuzhiyun if (!enabled && !__clk_gate(ccu, gate, true))
908*4882a593Smuzhiyun return -ENXIO;
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun /* Replace the selector value and record the result */
911*4882a593Smuzhiyun reg_val = __ccu_read(ccu, sel->offset);
912*4882a593Smuzhiyun reg_val = bitfield_replace(reg_val, sel->shift, sel->width, parent_sel);
913*4882a593Smuzhiyun __ccu_write(ccu, sel->offset, reg_val);
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun /* If the trigger fails we still want to disable the gate */
916*4882a593Smuzhiyun if (!__clk_trigger(ccu, trig))
917*4882a593Smuzhiyun ret = -EIO;
918*4882a593Smuzhiyun
919*4882a593Smuzhiyun /* Disable the clock again if it was disabled to begin with */
920*4882a593Smuzhiyun if (!enabled && !__clk_gate(ccu, gate, false))
921*4882a593Smuzhiyun ret = ret ? ret : -ENXIO; /* return first error */
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun return ret;
924*4882a593Smuzhiyun }
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun /*
927*4882a593Smuzhiyun * Initialize a selector by committing our desired state to hardware
928*4882a593Smuzhiyun * without the usual checks to see if it's already set up that way.
929*4882a593Smuzhiyun * Returns true if successful, false otherwise.
930*4882a593Smuzhiyun */
sel_init(struct ccu_data * ccu,struct bcm_clk_gate * gate,struct bcm_clk_sel * sel,struct bcm_clk_trig * trig)931*4882a593Smuzhiyun static bool sel_init(struct ccu_data *ccu, struct bcm_clk_gate *gate,
932*4882a593Smuzhiyun struct bcm_clk_sel *sel, struct bcm_clk_trig *trig)
933*4882a593Smuzhiyun {
934*4882a593Smuzhiyun if (!selector_exists(sel))
935*4882a593Smuzhiyun return true;
936*4882a593Smuzhiyun return !__sel_commit(ccu, gate, sel, trig);
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun
939*4882a593Smuzhiyun /*
940*4882a593Smuzhiyun * Write a new value into a selector register to switch to a
941*4882a593Smuzhiyun * different parent clock. Returns 0 on success, or an error code
942*4882a593Smuzhiyun * (from __sel_commit()) otherwise.
943*4882a593Smuzhiyun */
selector_write(struct ccu_data * ccu,struct bcm_clk_gate * gate,struct bcm_clk_sel * sel,struct bcm_clk_trig * trig,u8 index)944*4882a593Smuzhiyun static int selector_write(struct ccu_data *ccu, struct bcm_clk_gate *gate,
945*4882a593Smuzhiyun struct bcm_clk_sel *sel, struct bcm_clk_trig *trig,
946*4882a593Smuzhiyun u8 index)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun unsigned long flags;
949*4882a593Smuzhiyun u8 previous;
950*4882a593Smuzhiyun int ret;
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun previous = sel->clk_index;
953*4882a593Smuzhiyun if (previous == index)
954*4882a593Smuzhiyun return 0; /* No change */
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun sel->clk_index = index;
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun flags = ccu_lock(ccu);
959*4882a593Smuzhiyun __ccu_write_enable(ccu);
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun ret = __sel_commit(ccu, gate, sel, trig);
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun __ccu_write_disable(ccu);
964*4882a593Smuzhiyun ccu_unlock(ccu, flags);
965*4882a593Smuzhiyun
966*4882a593Smuzhiyun if (ret)
967*4882a593Smuzhiyun sel->clk_index = previous; /* Revert the change */
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun return ret;
970*4882a593Smuzhiyun }
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun /* Clock operations */
973*4882a593Smuzhiyun
kona_peri_clk_enable(struct clk_hw * hw)974*4882a593Smuzhiyun static int kona_peri_clk_enable(struct clk_hw *hw)
975*4882a593Smuzhiyun {
976*4882a593Smuzhiyun struct kona_clk *bcm_clk = to_kona_clk(hw);
977*4882a593Smuzhiyun struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun return clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, true);
980*4882a593Smuzhiyun }
981*4882a593Smuzhiyun
kona_peri_clk_disable(struct clk_hw * hw)982*4882a593Smuzhiyun static void kona_peri_clk_disable(struct clk_hw *hw)
983*4882a593Smuzhiyun {
984*4882a593Smuzhiyun struct kona_clk *bcm_clk = to_kona_clk(hw);
985*4882a593Smuzhiyun struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
986*4882a593Smuzhiyun
987*4882a593Smuzhiyun (void)clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, false);
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun
kona_peri_clk_is_enabled(struct clk_hw * hw)990*4882a593Smuzhiyun static int kona_peri_clk_is_enabled(struct clk_hw *hw)
991*4882a593Smuzhiyun {
992*4882a593Smuzhiyun struct kona_clk *bcm_clk = to_kona_clk(hw);
993*4882a593Smuzhiyun struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
994*4882a593Smuzhiyun
995*4882a593Smuzhiyun return is_clk_gate_enabled(bcm_clk->ccu, gate) ? 1 : 0;
996*4882a593Smuzhiyun }
997*4882a593Smuzhiyun
kona_peri_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)998*4882a593Smuzhiyun static unsigned long kona_peri_clk_recalc_rate(struct clk_hw *hw,
999*4882a593Smuzhiyun unsigned long parent_rate)
1000*4882a593Smuzhiyun {
1001*4882a593Smuzhiyun struct kona_clk *bcm_clk = to_kona_clk(hw);
1002*4882a593Smuzhiyun struct peri_clk_data *data = bcm_clk->u.peri;
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun return clk_recalc_rate(bcm_clk->ccu, &data->div, &data->pre_div,
1005*4882a593Smuzhiyun parent_rate);
1006*4882a593Smuzhiyun }
1007*4882a593Smuzhiyun
kona_peri_clk_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)1008*4882a593Smuzhiyun static long kona_peri_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1009*4882a593Smuzhiyun unsigned long *parent_rate)
1010*4882a593Smuzhiyun {
1011*4882a593Smuzhiyun struct kona_clk *bcm_clk = to_kona_clk(hw);
1012*4882a593Smuzhiyun struct bcm_clk_div *div = &bcm_clk->u.peri->div;
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun if (!divider_exists(div))
1015*4882a593Smuzhiyun return clk_hw_get_rate(hw);
1016*4882a593Smuzhiyun
1017*4882a593Smuzhiyun /* Quietly avoid a zero rate */
1018*4882a593Smuzhiyun return round_rate(bcm_clk->ccu, div, &bcm_clk->u.peri->pre_div,
1019*4882a593Smuzhiyun rate ? rate : 1, *parent_rate, NULL);
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun
kona_peri_clk_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)1022*4882a593Smuzhiyun static int kona_peri_clk_determine_rate(struct clk_hw *hw,
1023*4882a593Smuzhiyun struct clk_rate_request *req)
1024*4882a593Smuzhiyun {
1025*4882a593Smuzhiyun struct kona_clk *bcm_clk = to_kona_clk(hw);
1026*4882a593Smuzhiyun struct clk_hw *current_parent;
1027*4882a593Smuzhiyun unsigned long parent_rate;
1028*4882a593Smuzhiyun unsigned long best_delta;
1029*4882a593Smuzhiyun unsigned long best_rate;
1030*4882a593Smuzhiyun u32 parent_count;
1031*4882a593Smuzhiyun long rate;
1032*4882a593Smuzhiyun u32 which;
1033*4882a593Smuzhiyun
1034*4882a593Smuzhiyun /*
1035*4882a593Smuzhiyun * If there is no other parent to choose, use the current one.
1036*4882a593Smuzhiyun * Note: We don't honor (or use) CLK_SET_RATE_NO_REPARENT.
1037*4882a593Smuzhiyun */
1038*4882a593Smuzhiyun WARN_ON_ONCE(bcm_clk->init_data.flags & CLK_SET_RATE_NO_REPARENT);
1039*4882a593Smuzhiyun parent_count = (u32)bcm_clk->init_data.num_parents;
1040*4882a593Smuzhiyun if (parent_count < 2) {
1041*4882a593Smuzhiyun rate = kona_peri_clk_round_rate(hw, req->rate,
1042*4882a593Smuzhiyun &req->best_parent_rate);
1043*4882a593Smuzhiyun if (rate < 0)
1044*4882a593Smuzhiyun return rate;
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun req->rate = rate;
1047*4882a593Smuzhiyun return 0;
1048*4882a593Smuzhiyun }
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun /* Unless we can do better, stick with current parent */
1051*4882a593Smuzhiyun current_parent = clk_hw_get_parent(hw);
1052*4882a593Smuzhiyun parent_rate = clk_hw_get_rate(current_parent);
1053*4882a593Smuzhiyun best_rate = kona_peri_clk_round_rate(hw, req->rate, &parent_rate);
1054*4882a593Smuzhiyun best_delta = abs(best_rate - req->rate);
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun /* Check whether any other parent clock can produce a better result */
1057*4882a593Smuzhiyun for (which = 0; which < parent_count; which++) {
1058*4882a593Smuzhiyun struct clk_hw *parent = clk_hw_get_parent_by_index(hw, which);
1059*4882a593Smuzhiyun unsigned long delta;
1060*4882a593Smuzhiyun unsigned long other_rate;
1061*4882a593Smuzhiyun
1062*4882a593Smuzhiyun BUG_ON(!parent);
1063*4882a593Smuzhiyun if (parent == current_parent)
1064*4882a593Smuzhiyun continue;
1065*4882a593Smuzhiyun
1066*4882a593Smuzhiyun /* We don't support CLK_SET_RATE_PARENT */
1067*4882a593Smuzhiyun parent_rate = clk_hw_get_rate(parent);
1068*4882a593Smuzhiyun other_rate = kona_peri_clk_round_rate(hw, req->rate,
1069*4882a593Smuzhiyun &parent_rate);
1070*4882a593Smuzhiyun delta = abs(other_rate - req->rate);
1071*4882a593Smuzhiyun if (delta < best_delta) {
1072*4882a593Smuzhiyun best_delta = delta;
1073*4882a593Smuzhiyun best_rate = other_rate;
1074*4882a593Smuzhiyun req->best_parent_hw = parent;
1075*4882a593Smuzhiyun req->best_parent_rate = parent_rate;
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun req->rate = best_rate;
1080*4882a593Smuzhiyun return 0;
1081*4882a593Smuzhiyun }
1082*4882a593Smuzhiyun
kona_peri_clk_set_parent(struct clk_hw * hw,u8 index)1083*4882a593Smuzhiyun static int kona_peri_clk_set_parent(struct clk_hw *hw, u8 index)
1084*4882a593Smuzhiyun {
1085*4882a593Smuzhiyun struct kona_clk *bcm_clk = to_kona_clk(hw);
1086*4882a593Smuzhiyun struct peri_clk_data *data = bcm_clk->u.peri;
1087*4882a593Smuzhiyun struct bcm_clk_sel *sel = &data->sel;
1088*4882a593Smuzhiyun struct bcm_clk_trig *trig;
1089*4882a593Smuzhiyun int ret;
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun BUG_ON(index >= sel->parent_count);
1092*4882a593Smuzhiyun
1093*4882a593Smuzhiyun /* If there's only one parent we don't require a selector */
1094*4882a593Smuzhiyun if (!selector_exists(sel))
1095*4882a593Smuzhiyun return 0;
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun /*
1098*4882a593Smuzhiyun * The regular trigger is used by default, but if there's a
1099*4882a593Smuzhiyun * pre-trigger we want to use that instead.
1100*4882a593Smuzhiyun */
1101*4882a593Smuzhiyun trig = trigger_exists(&data->pre_trig) ? &data->pre_trig
1102*4882a593Smuzhiyun : &data->trig;
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun ret = selector_write(bcm_clk->ccu, &data->gate, sel, trig, index);
1105*4882a593Smuzhiyun if (ret == -ENXIO) {
1106*4882a593Smuzhiyun pr_err("%s: gating failure for %s\n", __func__,
1107*4882a593Smuzhiyun bcm_clk->init_data.name);
1108*4882a593Smuzhiyun ret = -EIO; /* Don't proliferate weird errors */
1109*4882a593Smuzhiyun } else if (ret == -EIO) {
1110*4882a593Smuzhiyun pr_err("%s: %strigger failed for %s\n", __func__,
1111*4882a593Smuzhiyun trig == &data->pre_trig ? "pre-" : "",
1112*4882a593Smuzhiyun bcm_clk->init_data.name);
1113*4882a593Smuzhiyun }
1114*4882a593Smuzhiyun
1115*4882a593Smuzhiyun return ret;
1116*4882a593Smuzhiyun }
1117*4882a593Smuzhiyun
kona_peri_clk_get_parent(struct clk_hw * hw)1118*4882a593Smuzhiyun static u8 kona_peri_clk_get_parent(struct clk_hw *hw)
1119*4882a593Smuzhiyun {
1120*4882a593Smuzhiyun struct kona_clk *bcm_clk = to_kona_clk(hw);
1121*4882a593Smuzhiyun struct peri_clk_data *data = bcm_clk->u.peri;
1122*4882a593Smuzhiyun u8 index;
1123*4882a593Smuzhiyun
1124*4882a593Smuzhiyun index = selector_read_index(bcm_clk->ccu, &data->sel);
1125*4882a593Smuzhiyun
1126*4882a593Smuzhiyun /* Not all callers would handle an out-of-range value gracefully */
1127*4882a593Smuzhiyun return index == BAD_CLK_INDEX ? 0 : index;
1128*4882a593Smuzhiyun }
1129*4882a593Smuzhiyun
kona_peri_clk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)1130*4882a593Smuzhiyun static int kona_peri_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1131*4882a593Smuzhiyun unsigned long parent_rate)
1132*4882a593Smuzhiyun {
1133*4882a593Smuzhiyun struct kona_clk *bcm_clk = to_kona_clk(hw);
1134*4882a593Smuzhiyun struct peri_clk_data *data = bcm_clk->u.peri;
1135*4882a593Smuzhiyun struct bcm_clk_div *div = &data->div;
1136*4882a593Smuzhiyun u64 scaled_div = 0;
1137*4882a593Smuzhiyun int ret;
1138*4882a593Smuzhiyun
1139*4882a593Smuzhiyun if (parent_rate > (unsigned long)LONG_MAX)
1140*4882a593Smuzhiyun return -EINVAL;
1141*4882a593Smuzhiyun
1142*4882a593Smuzhiyun if (rate == clk_hw_get_rate(hw))
1143*4882a593Smuzhiyun return 0;
1144*4882a593Smuzhiyun
1145*4882a593Smuzhiyun if (!divider_exists(div))
1146*4882a593Smuzhiyun return rate == parent_rate ? 0 : -EINVAL;
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun /*
1149*4882a593Smuzhiyun * A fixed divider can't be changed. (Nor can a fixed
1150*4882a593Smuzhiyun * pre-divider be, but for now we never actually try to
1151*4882a593Smuzhiyun * change that.) Tolerate a request for a no-op change.
1152*4882a593Smuzhiyun */
1153*4882a593Smuzhiyun if (divider_is_fixed(&data->div))
1154*4882a593Smuzhiyun return rate == parent_rate ? 0 : -EINVAL;
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun /*
1157*4882a593Smuzhiyun * Get the scaled divisor value needed to achieve a clock
1158*4882a593Smuzhiyun * rate as close as possible to what was requested, given
1159*4882a593Smuzhiyun * the parent clock rate supplied.
1160*4882a593Smuzhiyun */
1161*4882a593Smuzhiyun (void)round_rate(bcm_clk->ccu, div, &data->pre_div,
1162*4882a593Smuzhiyun rate ? rate : 1, parent_rate, &scaled_div);
1163*4882a593Smuzhiyun
1164*4882a593Smuzhiyun /*
1165*4882a593Smuzhiyun * We aren't updating any pre-divider at this point, so
1166*4882a593Smuzhiyun * we'll use the regular trigger.
1167*4882a593Smuzhiyun */
1168*4882a593Smuzhiyun ret = divider_write(bcm_clk->ccu, &data->gate, &data->div,
1169*4882a593Smuzhiyun &data->trig, scaled_div);
1170*4882a593Smuzhiyun if (ret == -ENXIO) {
1171*4882a593Smuzhiyun pr_err("%s: gating failure for %s\n", __func__,
1172*4882a593Smuzhiyun bcm_clk->init_data.name);
1173*4882a593Smuzhiyun ret = -EIO; /* Don't proliferate weird errors */
1174*4882a593Smuzhiyun } else if (ret == -EIO) {
1175*4882a593Smuzhiyun pr_err("%s: trigger failed for %s\n", __func__,
1176*4882a593Smuzhiyun bcm_clk->init_data.name);
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun return ret;
1180*4882a593Smuzhiyun }
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun struct clk_ops kona_peri_clk_ops = {
1183*4882a593Smuzhiyun .enable = kona_peri_clk_enable,
1184*4882a593Smuzhiyun .disable = kona_peri_clk_disable,
1185*4882a593Smuzhiyun .is_enabled = kona_peri_clk_is_enabled,
1186*4882a593Smuzhiyun .recalc_rate = kona_peri_clk_recalc_rate,
1187*4882a593Smuzhiyun .determine_rate = kona_peri_clk_determine_rate,
1188*4882a593Smuzhiyun .set_parent = kona_peri_clk_set_parent,
1189*4882a593Smuzhiyun .get_parent = kona_peri_clk_get_parent,
1190*4882a593Smuzhiyun .set_rate = kona_peri_clk_set_rate,
1191*4882a593Smuzhiyun };
1192*4882a593Smuzhiyun
1193*4882a593Smuzhiyun /* Put a peripheral clock into its initial state */
__peri_clk_init(struct kona_clk * bcm_clk)1194*4882a593Smuzhiyun static bool __peri_clk_init(struct kona_clk *bcm_clk)
1195*4882a593Smuzhiyun {
1196*4882a593Smuzhiyun struct ccu_data *ccu = bcm_clk->ccu;
1197*4882a593Smuzhiyun struct peri_clk_data *peri = bcm_clk->u.peri;
1198*4882a593Smuzhiyun const char *name = bcm_clk->init_data.name;
1199*4882a593Smuzhiyun struct bcm_clk_trig *trig;
1200*4882a593Smuzhiyun
1201*4882a593Smuzhiyun BUG_ON(bcm_clk->type != bcm_clk_peri);
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun if (!policy_init(ccu, &peri->policy)) {
1204*4882a593Smuzhiyun pr_err("%s: error initializing policy for %s\n",
1205*4882a593Smuzhiyun __func__, name);
1206*4882a593Smuzhiyun return false;
1207*4882a593Smuzhiyun }
1208*4882a593Smuzhiyun if (!gate_init(ccu, &peri->gate)) {
1209*4882a593Smuzhiyun pr_err("%s: error initializing gate for %s\n", __func__, name);
1210*4882a593Smuzhiyun return false;
1211*4882a593Smuzhiyun }
1212*4882a593Smuzhiyun if (!hyst_init(ccu, &peri->hyst)) {
1213*4882a593Smuzhiyun pr_err("%s: error initializing hyst for %s\n", __func__, name);
1214*4882a593Smuzhiyun return false;
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun if (!div_init(ccu, &peri->gate, &peri->div, &peri->trig)) {
1217*4882a593Smuzhiyun pr_err("%s: error initializing divider for %s\n", __func__,
1218*4882a593Smuzhiyun name);
1219*4882a593Smuzhiyun return false;
1220*4882a593Smuzhiyun }
1221*4882a593Smuzhiyun
1222*4882a593Smuzhiyun /*
1223*4882a593Smuzhiyun * For the pre-divider and selector, the pre-trigger is used
1224*4882a593Smuzhiyun * if it's present, otherwise we just use the regular trigger.
1225*4882a593Smuzhiyun */
1226*4882a593Smuzhiyun trig = trigger_exists(&peri->pre_trig) ? &peri->pre_trig
1227*4882a593Smuzhiyun : &peri->trig;
1228*4882a593Smuzhiyun
1229*4882a593Smuzhiyun if (!div_init(ccu, &peri->gate, &peri->pre_div, trig)) {
1230*4882a593Smuzhiyun pr_err("%s: error initializing pre-divider for %s\n", __func__,
1231*4882a593Smuzhiyun name);
1232*4882a593Smuzhiyun return false;
1233*4882a593Smuzhiyun }
1234*4882a593Smuzhiyun
1235*4882a593Smuzhiyun if (!sel_init(ccu, &peri->gate, &peri->sel, trig)) {
1236*4882a593Smuzhiyun pr_err("%s: error initializing selector for %s\n", __func__,
1237*4882a593Smuzhiyun name);
1238*4882a593Smuzhiyun return false;
1239*4882a593Smuzhiyun }
1240*4882a593Smuzhiyun
1241*4882a593Smuzhiyun return true;
1242*4882a593Smuzhiyun }
1243*4882a593Smuzhiyun
__kona_clk_init(struct kona_clk * bcm_clk)1244*4882a593Smuzhiyun static bool __kona_clk_init(struct kona_clk *bcm_clk)
1245*4882a593Smuzhiyun {
1246*4882a593Smuzhiyun switch (bcm_clk->type) {
1247*4882a593Smuzhiyun case bcm_clk_peri:
1248*4882a593Smuzhiyun return __peri_clk_init(bcm_clk);
1249*4882a593Smuzhiyun default:
1250*4882a593Smuzhiyun BUG();
1251*4882a593Smuzhiyun }
1252*4882a593Smuzhiyun return false;
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun /* Set a CCU and all its clocks into their desired initial state */
kona_ccu_init(struct ccu_data * ccu)1256*4882a593Smuzhiyun bool __init kona_ccu_init(struct ccu_data *ccu)
1257*4882a593Smuzhiyun {
1258*4882a593Smuzhiyun unsigned long flags;
1259*4882a593Smuzhiyun unsigned int which;
1260*4882a593Smuzhiyun struct kona_clk *kona_clks = ccu->kona_clks;
1261*4882a593Smuzhiyun bool success = true;
1262*4882a593Smuzhiyun
1263*4882a593Smuzhiyun flags = ccu_lock(ccu);
1264*4882a593Smuzhiyun __ccu_write_enable(ccu);
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun for (which = 0; which < ccu->clk_num; which++) {
1267*4882a593Smuzhiyun struct kona_clk *bcm_clk = &kona_clks[which];
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun if (!bcm_clk->ccu)
1270*4882a593Smuzhiyun continue;
1271*4882a593Smuzhiyun
1272*4882a593Smuzhiyun success &= __kona_clk_init(bcm_clk);
1273*4882a593Smuzhiyun }
1274*4882a593Smuzhiyun
1275*4882a593Smuzhiyun __ccu_write_disable(ccu);
1276*4882a593Smuzhiyun ccu_unlock(ccu, flags);
1277*4882a593Smuzhiyun return success;
1278*4882a593Smuzhiyun }
1279