1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2016 Rockchip Electronics Co., Ltd
4 * Author: Andy Yan <andy.yan@rock-chips.com>
9 #include <clk-uclass.h>
14 #include <asm/arch/clock.h>
15 #include <asm/arch/cru_rv1108.h>
16 #include <asm/arch/hardware.h>
18 #include <dt-bindings/clock/rv1108-cru.h>
21 VCO_MAX_HZ = 2400U * 1000000,
22 VCO_MIN_HZ = 600 * 1000000,
23 OUTPUT_MAX_HZ = 2400U * 1000000,
24 OUTPUT_MIN_HZ = 24 * 1000000,
27 #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
29 #define PLL_DIVISORS(hz, _refdiv, _postdiv1, _postdiv2) {\
31 .fbdiv = (u32)((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ),\
32 .postdiv1 = _postdiv1, .postdiv2 = _postdiv2};\
33 _Static_assert(((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ) *\
34 OSC_HZ / (_refdiv * _postdiv1 * _postdiv2) == hz,\
35 #hz "Hz cannot be hit with PLL "\
36 "divisors on line " __stringify(__LINE__));
38 /* use integer mode */
39 static inline int rv1108_pll_id(enum rk_clk_id clk_id)
52 printf("invalid pll id:%d\n", clk_id);
60 static uint32_t rkclk_pll_get_rate(struct rv1108_cru *cru,
61 enum rk_clk_id clk_id)
63 uint32_t refdiv, fbdiv, postdiv1, postdiv2;
64 uint32_t con0, con1, con3;
65 int pll_id = rv1108_pll_id(clk_id);
66 struct rv1108_pll *pll = &cru->pll[pll_id];
69 con3 = readl(&pll->con3);
71 if (con3 & WORK_MODE_MASK) {
72 con0 = readl(&pll->con0);
73 con1 = readl(&pll->con1);
74 fbdiv = (con0 >> FBDIV_SHIFT) & FBDIV_MASK;
75 postdiv1 = (con1 & POSTDIV1_MASK) >> POSTDIV1_SHIFT;
76 postdiv2 = (con1 & POSTDIV2_MASK) >> POSTDIV2_SHIFT;
77 refdiv = (con1 & REFDIV_MASK) >> REFDIV_SHIFT;
78 freq = (24 * fbdiv / (refdiv * postdiv1 * postdiv2)) * 1000000;
86 static int rv1108_mac_set_clk(struct rv1108_cru *cru, ulong rate)
88 uint32_t con = readl(&cru->clksel_con[24]);
92 if ((con >> MAC_PLL_SEL_SHIFT) & MAC_PLL_SEL_GPLL)
93 pll_rate = rkclk_pll_get_rate(cru, CLK_GENERAL);
95 pll_rate = rkclk_pll_get_rate(cru, CLK_ARM);
97 /*default set 50MHZ for gmac*/
101 div = DIV_ROUND_UP(pll_rate, rate) - 1;
103 rk_clrsetreg(&cru->clksel_con[24], MAC_CLK_DIV_MASK,
104 div << MAC_CLK_DIV_SHIFT);
106 debug("Unsupported div for gmac:%d\n", div);
108 return DIV_TO_RATE(pll_rate, div);
111 static int rv1108_sfc_set_clk(struct rv1108_cru *cru, uint rate)
113 u32 con = readl(&cru->clksel_con[27]);
117 if ((con >> SFC_PLL_SEL_SHIFT) && SFC_PLL_SEL_GPLL)
118 pll_rate = rkclk_pll_get_rate(cru, CLK_GENERAL);
120 pll_rate = rkclk_pll_get_rate(cru, CLK_DDR);
122 div = DIV_ROUND_UP(pll_rate, rate) - 1;
124 rk_clrsetreg(&cru->clksel_con[27], SFC_CLK_DIV_MASK,
125 div << SFC_CLK_DIV_SHIFT);
127 debug("Unsupported sfc clk rate:%d\n", rate);
129 return DIV_TO_RATE(pll_rate, div);
132 static ulong rv1108_saradc_get_clk(struct rv1108_cru *cru)
136 val = readl(&cru->clksel_con[22]);
137 div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
138 CLK_SARADC_DIV_CON_WIDTH);
140 return DIV_TO_RATE(OSC_HZ, div);
143 static ulong rv1108_saradc_set_clk(struct rv1108_cru *cru, uint hz)
147 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
148 assert(src_clk_div < 128);
150 rk_clrsetreg(&cru->clksel_con[22],
151 CLK_SARADC_DIV_CON_MASK,
152 src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
154 return rv1108_saradc_get_clk(cru);
157 static ulong rv1108_clk_get_rate(struct clk *clk)
159 struct rv1108_clk_priv *priv = dev_get_priv(clk->dev);
163 return rkclk_pll_get_rate(priv->cru, clk->id);
165 return rv1108_saradc_get_clk(priv->cru);
171 static ulong rv1108_clk_set_rate(struct clk *clk, ulong rate)
173 struct rv1108_clk_priv *priv = dev_get_priv(clk->dev);
178 new_rate = rv1108_mac_set_clk(priv->cru, rate);
181 new_rate = rv1108_sfc_set_clk(priv->cru, rate);
184 new_rate = rv1108_saradc_set_clk(priv->cru, rate);
193 static const struct clk_ops rv1108_clk_ops = {
194 .get_rate = rv1108_clk_get_rate,
195 .set_rate = rv1108_clk_set_rate,
198 static void rkclk_init(struct rv1108_cru *cru)
200 unsigned int apll = rkclk_pll_get_rate(cru, CLK_ARM);
201 unsigned int dpll = rkclk_pll_get_rate(cru, CLK_DDR);
202 unsigned int gpll = rkclk_pll_get_rate(cru, CLK_GENERAL);
204 rk_clrsetreg(&cru->clksel_con[0], CORE_CLK_DIV_MASK,
205 0 << MAC_CLK_DIV_SHIFT);
207 printf("APLL: %d DPLL:%d GPLL:%d\n", apll, dpll, gpll);
210 static int rv1108_clk_ofdata_to_platdata(struct udevice *dev)
212 struct rv1108_clk_priv *priv = dev_get_priv(dev);
214 priv->cru = dev_read_addr_ptr(dev);
219 static int rv1108_clk_probe(struct udevice *dev)
221 struct rv1108_clk_priv *priv = dev_get_priv(dev);
223 rkclk_init(priv->cru);
228 static int rv1108_clk_bind(struct udevice *dev)
231 struct udevice *sys_child;
232 struct sysreset_reg *priv;
234 /* The reset driver does not have a device node, so bind it here */
235 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
238 debug("Warning: No sysreset driver: ret=%d\n", ret);
240 priv = malloc(sizeof(struct sysreset_reg));
241 priv->glb_srst_fst_value = offsetof(struct rv1108_cru,
243 priv->glb_srst_snd_value = offsetof(struct rv1108_cru,
245 sys_child->priv = priv;
248 #if CONFIG_IS_ENABLED(CONFIG_RESET_ROCKCHIP)
249 ret = offsetof(struct rk3368_cru, softrst_con[0]);
250 ret = rockchip_reset_bind(dev, ret, 13);
252 debug("Warning: software reset driver bind faile\n");
258 static const struct udevice_id rv1108_clk_ids[] = {
259 { .compatible = "rockchip,rv1108-cru" },
263 U_BOOT_DRIVER(clk_rv1108) = {
264 .name = "clk_rv1108",
266 .of_match = rv1108_clk_ids,
267 .priv_auto_alloc_size = sizeof(struct rv1108_clk_priv),
268 .ofdata_to_platdata = rv1108_clk_ofdata_to_platdata,
269 .ops = &rv1108_clk_ops,
270 .bind = rv1108_clk_bind,
271 .probe = rv1108_clk_probe,