1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Weidmüller Interface GmbH & Co. KG
4 * Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
6 * Copyright (C) 2013 Soren Brinkmann <soren.brinkmann@xilinx.com>
7 * Copyright (C) 2013 Xilinx, Inc. All rights reserved.
11 #include <clk-uclass.h>
16 #include <asm/arch/clk.h>
17 #include <asm/arch/hardware.h>
18 #include <asm/arch/sys_proto.h>
20 /* Register bitfield defines */
21 #define PLLCTRL_FBDIV_MASK 0x7f000
22 #define PLLCTRL_FBDIV_SHIFT 12
23 #define PLLCTRL_BPFORCE_MASK (1 << 4)
24 #define PLLCTRL_PWRDWN_MASK 2
25 #define PLLCTRL_PWRDWN_SHIFT 1
26 #define PLLCTRL_RESET_MASK 1
27 #define PLLCTRL_RESET_SHIFT 0
29 #define ZYNQ_CLK_MAXDIV 0x3f
30 #define CLK_CTRL_DIV1_SHIFT 20
31 #define CLK_CTRL_DIV1_MASK (ZYNQ_CLK_MAXDIV << CLK_CTRL_DIV1_SHIFT)
32 #define CLK_CTRL_DIV0_SHIFT 8
33 #define CLK_CTRL_DIV0_MASK (ZYNQ_CLK_MAXDIV << CLK_CTRL_DIV0_SHIFT)
34 #define CLK_CTRL_SRCSEL_SHIFT 4
35 #define CLK_CTRL_SRCSEL_MASK (0x3 << CLK_CTRL_SRCSEL_SHIFT)
37 #define CLK_CTRL_DIV2X_SHIFT 26
38 #define CLK_CTRL_DIV2X_MASK (ZYNQ_CLK_MAXDIV << CLK_CTRL_DIV2X_SHIFT)
39 #define CLK_CTRL_DIV3X_SHIFT 20
40 #define CLK_CTRL_DIV3X_MASK (ZYNQ_CLK_MAXDIV << CLK_CTRL_DIV3X_SHIFT)
42 DECLARE_GLOBAL_DATA_PTR;
44 #ifndef CONFIG_SPL_BUILD
45 enum zynq_clk_rclk {mio_clk, emio_clk};
48 struct zynq_clk_priv {
50 #ifndef CONFIG_SPL_BUILD
51 struct clk gem_emio_clk[2];
55 static void *zynq_clk_get_register(enum zynq_clk id)
59 return &slcr_base->arm_pll_ctrl;
61 return &slcr_base->ddr_pll_ctrl;
63 return &slcr_base->io_pll_ctrl;
65 return &slcr_base->lqspi_clk_ctrl;
67 return &slcr_base->smc_clk_ctrl;
69 return &slcr_base->pcap_clk_ctrl;
70 case sdio0_clk ... sdio1_clk:
71 return &slcr_base->sdio_clk_ctrl;
72 case uart0_clk ... uart1_clk:
73 return &slcr_base->uart_clk_ctrl;
74 case spi0_clk ... spi1_clk:
75 return &slcr_base->spi_clk_ctrl;
76 #ifndef CONFIG_SPL_BUILD
78 return &slcr_base->dci_clk_ctrl;
80 return &slcr_base->gem0_clk_ctrl;
82 return &slcr_base->gem1_clk_ctrl;
84 return &slcr_base->fpga0_clk_ctrl;
86 return &slcr_base->fpga1_clk_ctrl;
88 return &slcr_base->fpga2_clk_ctrl;
90 return &slcr_base->fpga3_clk_ctrl;
91 case can0_clk ... can1_clk:
92 return &slcr_base->can_clk_ctrl;
93 case dbg_trc_clk ... dbg_apb_clk:
97 return &slcr_base->dbg_clk_ctrl;
101 static enum zynq_clk zynq_clk_get_cpu_pll(u32 clk_ctrl)
103 u32 srcsel = (clk_ctrl & CLK_CTRL_SRCSEL_MASK) >> CLK_CTRL_SRCSEL_SHIFT;
116 static enum zynq_clk zynq_clk_get_peripheral_pll(u32 clk_ctrl)
118 u32 srcsel = (clk_ctrl & CLK_CTRL_SRCSEL_MASK) >> CLK_CTRL_SRCSEL_SHIFT;
131 static ulong zynq_clk_get_pll_rate(struct zynq_clk_priv *priv, enum zynq_clk id)
133 u32 clk_ctrl, reset, pwrdwn, mul, bypass;
135 clk_ctrl = readl(zynq_clk_get_register(id));
137 reset = (clk_ctrl & PLLCTRL_RESET_MASK) >> PLLCTRL_RESET_SHIFT;
138 pwrdwn = (clk_ctrl & PLLCTRL_PWRDWN_MASK) >> PLLCTRL_PWRDWN_SHIFT;
142 bypass = clk_ctrl & PLLCTRL_BPFORCE_MASK;
146 mul = (clk_ctrl & PLLCTRL_FBDIV_MASK) >> PLLCTRL_FBDIV_SHIFT;
148 return priv->ps_clk_freq * mul;
151 #ifndef CONFIG_SPL_BUILD
152 static enum zynq_clk_rclk zynq_clk_get_gem_rclk(enum zynq_clk id)
154 u32 clk_ctrl, srcsel;
157 clk_ctrl = readl(&slcr_base->gem0_rclk_ctrl);
159 clk_ctrl = readl(&slcr_base->gem1_rclk_ctrl);
161 srcsel = (clk_ctrl & CLK_CTRL_SRCSEL_MASK) >> CLK_CTRL_SRCSEL_SHIFT;
169 static ulong zynq_clk_get_cpu_rate(struct zynq_clk_priv *priv, enum zynq_clk id)
171 u32 clk_621, clk_ctrl, div;
174 clk_ctrl = readl(&slcr_base->arm_clk_ctrl);
176 div = (clk_ctrl & CLK_CTRL_DIV0_MASK) >> CLK_CTRL_DIV0_SHIFT;
183 clk_621 = readl(&slcr_base->clk_621_true) & 1;
195 pll = zynq_clk_get_cpu_pll(clk_ctrl);
197 return DIV_ROUND_CLOSEST(zynq_clk_get_pll_rate(priv, pll), div);
200 #ifndef CONFIG_SPL_BUILD
201 static ulong zynq_clk_get_ddr2x_rate(struct zynq_clk_priv *priv)
205 clk_ctrl = readl(&slcr_base->ddr_clk_ctrl);
207 div = (clk_ctrl & CLK_CTRL_DIV2X_MASK) >> CLK_CTRL_DIV2X_SHIFT;
209 return DIV_ROUND_CLOSEST(zynq_clk_get_pll_rate(priv, ddrpll_clk), div);
213 static ulong zynq_clk_get_ddr3x_rate(struct zynq_clk_priv *priv)
217 clk_ctrl = readl(&slcr_base->ddr_clk_ctrl);
219 div = (clk_ctrl & CLK_CTRL_DIV3X_MASK) >> CLK_CTRL_DIV3X_SHIFT;
221 return DIV_ROUND_CLOSEST(zynq_clk_get_pll_rate(priv, ddrpll_clk), div);
224 #ifndef CONFIG_SPL_BUILD
225 static ulong zynq_clk_get_dci_rate(struct zynq_clk_priv *priv)
227 u32 clk_ctrl, div0, div1;
229 clk_ctrl = readl(&slcr_base->dci_clk_ctrl);
231 div0 = (clk_ctrl & CLK_CTRL_DIV0_MASK) >> CLK_CTRL_DIV0_SHIFT;
232 div1 = (clk_ctrl & CLK_CTRL_DIV1_MASK) >> CLK_CTRL_DIV1_SHIFT;
234 return DIV_ROUND_CLOSEST(DIV_ROUND_CLOSEST(
235 zynq_clk_get_pll_rate(priv, ddrpll_clk), div0), div1);
239 static ulong zynq_clk_get_peripheral_rate(struct zynq_clk_priv *priv,
240 enum zynq_clk id, bool two_divs)
246 clk_ctrl = readl(zynq_clk_get_register(id));
248 div0 = (clk_ctrl & CLK_CTRL_DIV0_MASK) >> CLK_CTRL_DIV0_SHIFT;
252 #ifndef CONFIG_SPL_BUILD
254 div1 = (clk_ctrl & CLK_CTRL_DIV1_MASK) >> CLK_CTRL_DIV1_SHIFT;
260 pll = zynq_clk_get_peripheral_pll(clk_ctrl);
265 zynq_clk_get_pll_rate(priv, pll), div0),
269 #ifndef CONFIG_SPL_BUILD
270 static ulong zynq_clk_get_gem_rate(struct zynq_clk_priv *priv, enum zynq_clk id)
274 if (zynq_clk_get_gem_rclk(id) == mio_clk)
275 return zynq_clk_get_peripheral_rate(priv, id, true);
277 parent = &priv->gem_emio_clk[id - gem0_clk];
279 return clk_get_rate(parent);
281 debug("%s: gem%d emio rx clock source unknown\n", __func__,
287 static unsigned long zynq_clk_calc_peripheral_two_divs(ulong rate,
289 u32 *div0, u32 *div1)
291 long new_err, best_err = (long)(~0UL >> 1);
292 ulong new_rate, best_rate = 0;
295 for (d0 = 1; d0 <= ZYNQ_CLK_MAXDIV; d0++) {
296 for (d1 = 1; d1 <= ZYNQ_CLK_MAXDIV >> 1; d1++) {
297 new_rate = DIV_ROUND_CLOSEST(
298 DIV_ROUND_CLOSEST(pll_rate, d0), d1);
299 new_err = abs(new_rate - rate);
301 if (new_err < best_err) {
305 best_rate = new_rate;
313 static ulong zynq_clk_set_peripheral_rate(struct zynq_clk_priv *priv,
314 enum zynq_clk id, ulong rate,
318 u32 clk_ctrl, div0 = 0, div1 = 0;
319 ulong pll_rate, new_rate;
322 reg = zynq_clk_get_register(id);
323 clk_ctrl = readl(reg);
325 pll = zynq_clk_get_peripheral_pll(clk_ctrl);
326 pll_rate = zynq_clk_get_pll_rate(priv, pll);
327 clk_ctrl &= ~CLK_CTRL_DIV0_MASK;
329 clk_ctrl &= ~CLK_CTRL_DIV1_MASK;
330 new_rate = zynq_clk_calc_peripheral_two_divs(rate, pll_rate,
332 clk_ctrl |= div1 << CLK_CTRL_DIV1_SHIFT;
334 div0 = DIV_ROUND_CLOSEST(pll_rate, rate);
335 if (div0 > ZYNQ_CLK_MAXDIV)
336 div0 = ZYNQ_CLK_MAXDIV;
337 new_rate = DIV_ROUND_CLOSEST(rate, div0);
339 clk_ctrl |= div0 << CLK_CTRL_DIV0_SHIFT;
342 writel(clk_ctrl, reg);
348 static ulong zynq_clk_set_gem_rate(struct zynq_clk_priv *priv, enum zynq_clk id,
353 if (zynq_clk_get_gem_rclk(id) == mio_clk)
354 return zynq_clk_set_peripheral_rate(priv, id, rate, true);
356 parent = &priv->gem_emio_clk[id - gem0_clk];
358 return clk_set_rate(parent, rate);
360 debug("%s: gem%d emio rx clock source unknown\n", __func__,
367 #ifndef CONFIG_SPL_BUILD
368 static ulong zynq_clk_get_rate(struct clk *clk)
370 struct zynq_clk_priv *priv = dev_get_priv(clk->dev);
371 enum zynq_clk id = clk->id;
372 bool two_divs = false;
375 case armpll_clk ... iopll_clk:
376 return zynq_clk_get_pll_rate(priv, id);
377 case cpu_6or4x_clk ... cpu_1x_clk:
378 return zynq_clk_get_cpu_rate(priv, id);
380 return zynq_clk_get_ddr2x_rate(priv);
382 return zynq_clk_get_ddr3x_rate(priv);
384 return zynq_clk_get_dci_rate(priv);
385 case gem0_clk ... gem1_clk:
386 return zynq_clk_get_gem_rate(priv, id);
387 case fclk0_clk ... can1_clk:
390 case dbg_trc_clk ... dbg_apb_clk:
391 case lqspi_clk ... pcap_clk:
392 case sdio0_clk ... spi1_clk:
393 return zynq_clk_get_peripheral_rate(priv, id, two_divs);
395 return zynq_clk_get_cpu_rate(priv, cpu_2x_clk);
396 case usb0_aper_clk ... swdt_clk:
397 return zynq_clk_get_cpu_rate(priv, cpu_1x_clk);
403 static ulong zynq_clk_set_rate(struct clk *clk, ulong rate)
405 struct zynq_clk_priv *priv = dev_get_priv(clk->dev);
406 enum zynq_clk id = clk->id;
407 bool two_divs = false;
410 case gem0_clk ... gem1_clk:
411 return zynq_clk_set_gem_rate(priv, id, rate);
412 case fclk0_clk ... can1_clk:
415 case lqspi_clk ... pcap_clk:
416 case sdio0_clk ... spi1_clk:
417 case dbg_trc_clk ... dbg_apb_clk:
418 return zynq_clk_set_peripheral_rate(priv, id, rate, two_divs);
424 static ulong zynq_clk_get_rate(struct clk *clk)
426 struct zynq_clk_priv *priv = dev_get_priv(clk->dev);
427 enum zynq_clk id = clk->id;
430 case cpu_6or4x_clk ... cpu_1x_clk:
431 return zynq_clk_get_cpu_rate(priv, id);
433 return zynq_clk_get_ddr3x_rate(priv);
434 case lqspi_clk ... pcap_clk:
435 case sdio0_clk ... spi1_clk:
436 return zynq_clk_get_peripheral_rate(priv, id, 0);
437 case i2c0_aper_clk ... i2c1_aper_clk:
438 return zynq_clk_get_cpu_rate(priv, cpu_1x_clk);
445 static struct clk_ops zynq_clk_ops = {
446 .get_rate = zynq_clk_get_rate,
447 #ifndef CONFIG_SPL_BUILD
448 .set_rate = zynq_clk_set_rate,
452 static int zynq_clk_probe(struct udevice *dev)
454 struct zynq_clk_priv *priv = dev_get_priv(dev);
455 #ifndef CONFIG_SPL_BUILD
460 for (i = 0; i < 2; i++) {
461 sprintf(name, "gem%d_emio_clk", i);
462 ret = clk_get_by_name(dev, name, &priv->gem_emio_clk[i]);
463 if (ret < 0 && ret != -ENODATA) {
464 dev_err(dev, "failed to get %s clock\n", name);
470 priv->ps_clk_freq = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
471 "ps-clk-frequency", 33333333UL);
476 static const struct udevice_id zynq_clk_ids[] = {
477 { .compatible = "xlnx,ps7-clkc"},
481 U_BOOT_DRIVER(zynq_clk) = {
484 .of_match = zynq_clk_ids,
485 .ops = &zynq_clk_ops,
486 .priv_auto_alloc_size = sizeof(struct zynq_clk_priv),
487 .probe = zynq_clk_probe,