1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright 2014 Google, Inc
4*4882a593Smuzhiyun * Author: Alexandru M Stan <amstan@chromium.org>
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/slab.h>
8*4882a593Smuzhiyun #include <linux/clk.h>
9*4882a593Smuzhiyun #include <linux/clk-provider.h>
10*4882a593Smuzhiyun #include <linux/io.h>
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include "clk.h"
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun struct rockchip_mmc_clock {
15*4882a593Smuzhiyun struct clk_hw hw;
16*4882a593Smuzhiyun void __iomem *reg;
17*4882a593Smuzhiyun int id;
18*4882a593Smuzhiyun int shift;
19*4882a593Smuzhiyun int cached_phase;
20*4882a593Smuzhiyun struct notifier_block clk_rate_change_nb;
21*4882a593Smuzhiyun };
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #define to_mmc_clock(_hw) container_of(_hw, struct rockchip_mmc_clock, hw)
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #define RK3288_MMC_CLKGEN_DIV 2
26*4882a593Smuzhiyun
rockchip_mmc_recalc(struct clk_hw * hw,unsigned long parent_rate)27*4882a593Smuzhiyun static unsigned long rockchip_mmc_recalc(struct clk_hw *hw,
28*4882a593Smuzhiyun unsigned long parent_rate)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun return parent_rate / RK3288_MMC_CLKGEN_DIV;
31*4882a593Smuzhiyun }
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define ROCKCHIP_MMC_DELAY_SEL BIT(10)
34*4882a593Smuzhiyun #define ROCKCHIP_MMC_DEGREE_MASK 0x3
35*4882a593Smuzhiyun #define ROCKCHIP_MMC_DELAYNUM_OFFSET 2
36*4882a593Smuzhiyun #define ROCKCHIP_MMC_DELAYNUM_MASK (0xff << ROCKCHIP_MMC_DELAYNUM_OFFSET)
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #define PSECS_PER_SEC 1000000000000LL
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /*
41*4882a593Smuzhiyun * Each fine delay is between 44ps-77ps. Assume each fine delay is 60ps to
42*4882a593Smuzhiyun * simplify calculations. So 45degs could be anywhere between 33deg and 57.8deg.
43*4882a593Smuzhiyun */
44*4882a593Smuzhiyun #define ROCKCHIP_MMC_DELAY_ELEMENT_PSEC 60
45*4882a593Smuzhiyun
rockchip_mmc_get_phase(struct clk_hw * hw)46*4882a593Smuzhiyun static int rockchip_mmc_get_phase(struct clk_hw *hw)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun struct rockchip_mmc_clock *mmc_clock = to_mmc_clock(hw);
49*4882a593Smuzhiyun unsigned long rate = clk_hw_get_rate(hw);
50*4882a593Smuzhiyun u32 raw_value;
51*4882a593Smuzhiyun u16 degrees;
52*4882a593Smuzhiyun u32 delay_num = 0;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* Constant signal, no measurable phase shift */
55*4882a593Smuzhiyun if (!rate)
56*4882a593Smuzhiyun return 0;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun raw_value = readl(mmc_clock->reg) >> (mmc_clock->shift);
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun degrees = (raw_value & ROCKCHIP_MMC_DEGREE_MASK) * 90;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun if (raw_value & ROCKCHIP_MMC_DELAY_SEL) {
63*4882a593Smuzhiyun /* degrees/delaynum * 1000000 */
64*4882a593Smuzhiyun unsigned long factor = (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC / 10) *
65*4882a593Smuzhiyun 36 * (rate / 10000);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun delay_num = (raw_value & ROCKCHIP_MMC_DELAYNUM_MASK);
68*4882a593Smuzhiyun delay_num >>= ROCKCHIP_MMC_DELAYNUM_OFFSET;
69*4882a593Smuzhiyun degrees += DIV_ROUND_CLOSEST(delay_num * factor, 1000000);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun return degrees % 360;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun
rockchip_mmc_set_phase(struct clk_hw * hw,int degrees)75*4882a593Smuzhiyun static int rockchip_mmc_set_phase(struct clk_hw *hw, int degrees)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun struct rockchip_mmc_clock *mmc_clock = to_mmc_clock(hw);
78*4882a593Smuzhiyun unsigned long rate = clk_hw_get_rate(hw);
79*4882a593Smuzhiyun u8 nineties, remainder;
80*4882a593Smuzhiyun u8 delay_num;
81*4882a593Smuzhiyun u32 raw_value;
82*4882a593Smuzhiyun u32 delay;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun * The below calculation is based on the output clock from
86*4882a593Smuzhiyun * MMC host to the card, which expects the phase clock inherits
87*4882a593Smuzhiyun * the clock rate from its parent, namely the output clock
88*4882a593Smuzhiyun * provider of MMC host. However, things may go wrong if
89*4882a593Smuzhiyun * (1) It is orphan.
90*4882a593Smuzhiyun * (2) It is assigned to the wrong parent.
91*4882a593Smuzhiyun *
92*4882a593Smuzhiyun * This check help debug the case (1), which seems to be the
93*4882a593Smuzhiyun * most likely problem we often face and which makes it difficult
94*4882a593Smuzhiyun * for people to debug unstable mmc tuning results.
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun if (!rate) {
97*4882a593Smuzhiyun pr_err("%s: invalid clk rate\n", __func__);
98*4882a593Smuzhiyun return -EINVAL;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun nineties = degrees / 90;
102*4882a593Smuzhiyun remainder = (degrees % 90);
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /*
105*4882a593Smuzhiyun * Due to the inexact nature of the "fine" delay, we might
106*4882a593Smuzhiyun * actually go non-monotonic. We don't go _too_ monotonic
107*4882a593Smuzhiyun * though, so we should be OK. Here are options of how we may
108*4882a593Smuzhiyun * work:
109*4882a593Smuzhiyun *
110*4882a593Smuzhiyun * Ideally we end up with:
111*4882a593Smuzhiyun * 1.0, 2.0, ..., 69.0, 70.0, ..., 89.0, 90.0
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * On one extreme (if delay is actually 44ps):
114*4882a593Smuzhiyun * .73, 1.5, ..., 50.6, 51.3, ..., 65.3, 90.0
115*4882a593Smuzhiyun * The other (if delay is actually 77ps):
116*4882a593Smuzhiyun * 1.3, 2.6, ..., 88.6. 89.8, ..., 114.0, 90
117*4882a593Smuzhiyun *
118*4882a593Smuzhiyun * It's possible we might make a delay that is up to 25
119*4882a593Smuzhiyun * degrees off from what we think we're making. That's OK
120*4882a593Smuzhiyun * though because we should be REALLY far from any bad range.
121*4882a593Smuzhiyun */
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /*
124*4882a593Smuzhiyun * Convert to delay; do a little extra work to make sure we
125*4882a593Smuzhiyun * don't overflow 32-bit / 64-bit numbers.
126*4882a593Smuzhiyun */
127*4882a593Smuzhiyun delay = 10000000; /* PSECS_PER_SEC / 10000 / 10 */
128*4882a593Smuzhiyun delay *= remainder;
129*4882a593Smuzhiyun delay = DIV_ROUND_CLOSEST(delay,
130*4882a593Smuzhiyun (rate / 1000) * 36 *
131*4882a593Smuzhiyun (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC / 10));
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun delay_num = (u8) min_t(u32, delay, 255);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun raw_value = delay_num ? ROCKCHIP_MMC_DELAY_SEL : 0;
136*4882a593Smuzhiyun raw_value |= delay_num << ROCKCHIP_MMC_DELAYNUM_OFFSET;
137*4882a593Smuzhiyun raw_value |= nineties;
138*4882a593Smuzhiyun writel(HIWORD_UPDATE(raw_value, 0x07ff, mmc_clock->shift),
139*4882a593Smuzhiyun mmc_clock->reg);
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun pr_debug("%s->set_phase(%d) delay_nums=%u reg[0x%p]=0x%03x actual_degrees=%d\n",
142*4882a593Smuzhiyun clk_hw_get_name(hw), degrees, delay_num,
143*4882a593Smuzhiyun mmc_clock->reg, raw_value>>(mmc_clock->shift),
144*4882a593Smuzhiyun rockchip_mmc_get_phase(hw)
145*4882a593Smuzhiyun );
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun return 0;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun static const struct clk_ops rockchip_mmc_clk_ops = {
151*4882a593Smuzhiyun .recalc_rate = rockchip_mmc_recalc,
152*4882a593Smuzhiyun .get_phase = rockchip_mmc_get_phase,
153*4882a593Smuzhiyun .set_phase = rockchip_mmc_set_phase,
154*4882a593Smuzhiyun };
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun #define to_rockchip_mmc_clock(x) \
157*4882a593Smuzhiyun container_of(x, struct rockchip_mmc_clock, clk_rate_change_nb)
rockchip_mmc_clk_rate_notify(struct notifier_block * nb,unsigned long event,void * data)158*4882a593Smuzhiyun static int rockchip_mmc_clk_rate_notify(struct notifier_block *nb,
159*4882a593Smuzhiyun unsigned long event, void *data)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun struct rockchip_mmc_clock *mmc_clock = to_rockchip_mmc_clock(nb);
162*4882a593Smuzhiyun struct clk_notifier_data *ndata = data;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /*
165*4882a593Smuzhiyun * rockchip_mmc_clk is mostly used by mmc controllers to sample
166*4882a593Smuzhiyun * the intput data, which expects the fixed phase after the tuning
167*4882a593Smuzhiyun * process. However if the clock rate is changed, the phase is stale
168*4882a593Smuzhiyun * and may break the data sampling. So here we try to restore the phase
169*4882a593Smuzhiyun * for that case, except that
170*4882a593Smuzhiyun * (1) cached_phase is invaild since we inevitably cached it when the
171*4882a593Smuzhiyun * clock provider be reparented from orphan to its real parent in the
172*4882a593Smuzhiyun * first place. Otherwise we may mess up the initialization of MMC cards
173*4882a593Smuzhiyun * since we only set the default sample phase and drive phase later on.
174*4882a593Smuzhiyun * (2) the new coming rate is higher than the older one since mmc driver
175*4882a593Smuzhiyun * set the max-frequency to match the boards' ability but we can't go
176*4882a593Smuzhiyun * over the heads of that, otherwise the tests smoke out the issue.
177*4882a593Smuzhiyun */
178*4882a593Smuzhiyun if (ndata->old_rate <= ndata->new_rate)
179*4882a593Smuzhiyun return NOTIFY_DONE;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun if (event == PRE_RATE_CHANGE)
182*4882a593Smuzhiyun mmc_clock->cached_phase =
183*4882a593Smuzhiyun rockchip_mmc_get_phase(&mmc_clock->hw);
184*4882a593Smuzhiyun else if (mmc_clock->cached_phase != -EINVAL &&
185*4882a593Smuzhiyun event == POST_RATE_CHANGE)
186*4882a593Smuzhiyun rockchip_mmc_set_phase(&mmc_clock->hw, mmc_clock->cached_phase);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun return NOTIFY_DONE;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
rockchip_clk_register_mmc(const char * name,const char * const * parent_names,u8 num_parents,void __iomem * reg,int shift)191*4882a593Smuzhiyun struct clk *rockchip_clk_register_mmc(const char *name,
192*4882a593Smuzhiyun const char *const *parent_names, u8 num_parents,
193*4882a593Smuzhiyun void __iomem *reg, int shift)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun struct clk_init_data init;
196*4882a593Smuzhiyun struct rockchip_mmc_clock *mmc_clock;
197*4882a593Smuzhiyun struct clk *clk;
198*4882a593Smuzhiyun int ret;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun mmc_clock = kmalloc(sizeof(*mmc_clock), GFP_KERNEL);
201*4882a593Smuzhiyun if (!mmc_clock)
202*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun init.name = name;
205*4882a593Smuzhiyun init.flags = 0;
206*4882a593Smuzhiyun init.num_parents = num_parents;
207*4882a593Smuzhiyun init.parent_names = parent_names;
208*4882a593Smuzhiyun init.ops = &rockchip_mmc_clk_ops;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun mmc_clock->hw.init = &init;
211*4882a593Smuzhiyun mmc_clock->reg = reg;
212*4882a593Smuzhiyun mmc_clock->shift = shift;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun clk = clk_register(NULL, &mmc_clock->hw);
215*4882a593Smuzhiyun if (IS_ERR(clk)) {
216*4882a593Smuzhiyun ret = PTR_ERR(clk);
217*4882a593Smuzhiyun goto err_register;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun mmc_clock->clk_rate_change_nb.notifier_call =
221*4882a593Smuzhiyun &rockchip_mmc_clk_rate_notify;
222*4882a593Smuzhiyun ret = clk_notifier_register(clk, &mmc_clock->clk_rate_change_nb);
223*4882a593Smuzhiyun if (ret)
224*4882a593Smuzhiyun goto err_notifier;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun return clk;
227*4882a593Smuzhiyun err_notifier:
228*4882a593Smuzhiyun clk_unregister(clk);
229*4882a593Smuzhiyun err_register:
230*4882a593Smuzhiyun kfree(mmc_clock);
231*4882a593Smuzhiyun return ERR_PTR(ret);
232*4882a593Smuzhiyun }
233