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>
14 #include <dm/device_compat.h>
18 #include <asm/arch/clk.h>
19 #include <asm/arch/hardware.h>
20 #include <asm/arch/sys_proto.h>
22 /* Register bitfield defines */
23 #define PLLCTRL_FBDIV_MASK 0x7f000
24 #define PLLCTRL_FBDIV_SHIFT 12
25 #define PLLCTRL_BPFORCE_MASK (1 << 4)
26 #define PLLCTRL_PWRDWN_MASK 2
27 #define PLLCTRL_PWRDWN_SHIFT 1
28 #define PLLCTRL_RESET_MASK 1
29 #define PLLCTRL_RESET_SHIFT 0
31 #define ZYNQ_CLK_MAXDIV 0x3f
32 #define CLK_CTRL_DIV1_SHIFT 20
33 #define CLK_CTRL_DIV1_MASK (ZYNQ_CLK_MAXDIV << CLK_CTRL_DIV1_SHIFT)
34 #define CLK_CTRL_DIV0_SHIFT 8
35 #define CLK_CTRL_DIV0_MASK (ZYNQ_CLK_MAXDIV << CLK_CTRL_DIV0_SHIFT)
36 #define CLK_CTRL_SRCSEL_SHIFT 4
37 #define CLK_CTRL_SRCSEL_MASK (0x3 << CLK_CTRL_SRCSEL_SHIFT)
39 #define CLK_CTRL_DIV2X_SHIFT 26
40 #define CLK_CTRL_DIV2X_MASK (ZYNQ_CLK_MAXDIV << CLK_CTRL_DIV2X_SHIFT)
41 #define CLK_CTRL_DIV3X_SHIFT 20
42 #define CLK_CTRL_DIV3X_MASK (ZYNQ_CLK_MAXDIV << CLK_CTRL_DIV3X_SHIFT)
44 DECLARE_GLOBAL_DATA_PTR;
46 #ifndef CONFIG_SPL_BUILD
47 enum zynq_clk_rclk {mio_clk, emio_clk};
50 struct zynq_clk_priv {
52 #ifndef CONFIG_SPL_BUILD
53 struct clk gem_emio_clk[2];
57 static void *zynq_clk_get_register(enum zynq_clk id)
61 return &slcr_base->arm_pll_ctrl;
63 return &slcr_base->ddr_pll_ctrl;
65 return &slcr_base->io_pll_ctrl;
67 return &slcr_base->lqspi_clk_ctrl;
69 return &slcr_base->smc_clk_ctrl;
71 return &slcr_base->pcap_clk_ctrl;
72 case sdio0_clk ... sdio1_clk:
73 return &slcr_base->sdio_clk_ctrl;
74 case uart0_clk ... uart1_clk:
75 return &slcr_base->uart_clk_ctrl;
76 case spi0_clk ... spi1_clk:
77 return &slcr_base->spi_clk_ctrl;
78 #ifndef CONFIG_SPL_BUILD
80 return &slcr_base->dci_clk_ctrl;
82 return &slcr_base->gem0_clk_ctrl;
84 return &slcr_base->gem1_clk_ctrl;
86 return &slcr_base->fpga0_clk_ctrl;
88 return &slcr_base->fpga1_clk_ctrl;
90 return &slcr_base->fpga2_clk_ctrl;
92 return &slcr_base->fpga3_clk_ctrl;
93 case can0_clk ... can1_clk:
94 return &slcr_base->can_clk_ctrl;
95 case dbg_trc_clk ... dbg_apb_clk:
99 return &slcr_base->dbg_clk_ctrl;
103 static enum zynq_clk zynq_clk_get_cpu_pll(u32 clk_ctrl)
105 u32 srcsel = (clk_ctrl & CLK_CTRL_SRCSEL_MASK) >> CLK_CTRL_SRCSEL_SHIFT;
118 static enum zynq_clk zynq_clk_get_peripheral_pll(u32 clk_ctrl)
120 u32 srcsel = (clk_ctrl & CLK_CTRL_SRCSEL_MASK) >> CLK_CTRL_SRCSEL_SHIFT;
133 static ulong zynq_clk_get_pll_rate(struct zynq_clk_priv *priv, enum zynq_clk id)
135 u32 clk_ctrl, reset, pwrdwn, mul, bypass;
137 clk_ctrl = readl(zynq_clk_get_register(id));
139 reset = (clk_ctrl & PLLCTRL_RESET_MASK) >> PLLCTRL_RESET_SHIFT;
140 pwrdwn = (clk_ctrl & PLLCTRL_PWRDWN_MASK) >> PLLCTRL_PWRDWN_SHIFT;
144 bypass = clk_ctrl & PLLCTRL_BPFORCE_MASK;
148 mul = (clk_ctrl & PLLCTRL_FBDIV_MASK) >> PLLCTRL_FBDIV_SHIFT;
150 return priv->ps_clk_freq * mul;
153 #ifndef CONFIG_SPL_BUILD
154 static enum zynq_clk_rclk zynq_clk_get_gem_rclk(enum zynq_clk id)
156 u32 clk_ctrl, srcsel;
159 clk_ctrl = readl(&slcr_base->gem0_rclk_ctrl);
161 clk_ctrl = readl(&slcr_base->gem1_rclk_ctrl);
163 srcsel = (clk_ctrl & CLK_CTRL_SRCSEL_MASK) >> CLK_CTRL_SRCSEL_SHIFT;
171 static ulong zynq_clk_get_cpu_rate(struct zynq_clk_priv *priv, enum zynq_clk id)
173 u32 clk_621, clk_ctrl, div;
176 clk_ctrl = readl(&slcr_base->arm_clk_ctrl);
178 div = (clk_ctrl & CLK_CTRL_DIV0_MASK) >> CLK_CTRL_DIV0_SHIFT;
185 clk_621 = readl(&slcr_base->clk_621_true) & 1;
197 pll = zynq_clk_get_cpu_pll(clk_ctrl);
199 return DIV_ROUND_CLOSEST(zynq_clk_get_pll_rate(priv, pll), div);
202 #ifndef CONFIG_SPL_BUILD
203 static ulong zynq_clk_get_ddr2x_rate(struct zynq_clk_priv *priv)
207 clk_ctrl = readl(&slcr_base->ddr_clk_ctrl);
209 div = (clk_ctrl & CLK_CTRL_DIV2X_MASK) >> CLK_CTRL_DIV2X_SHIFT;
211 return DIV_ROUND_CLOSEST(zynq_clk_get_pll_rate(priv, ddrpll_clk), div);
215 static ulong zynq_clk_get_ddr3x_rate(struct zynq_clk_priv *priv)
219 clk_ctrl = readl(&slcr_base->ddr_clk_ctrl);
221 div = (clk_ctrl & CLK_CTRL_DIV3X_MASK) >> CLK_CTRL_DIV3X_SHIFT;
223 return DIV_ROUND_CLOSEST(zynq_clk_get_pll_rate(priv, ddrpll_clk), div);
226 #ifndef CONFIG_SPL_BUILD
227 static ulong zynq_clk_get_dci_rate(struct zynq_clk_priv *priv)
229 u32 clk_ctrl, div0, div1;
231 clk_ctrl = readl(&slcr_base->dci_clk_ctrl);
233 div0 = (clk_ctrl & CLK_CTRL_DIV0_MASK) >> CLK_CTRL_DIV0_SHIFT;
234 div1 = (clk_ctrl & CLK_CTRL_DIV1_MASK) >> CLK_CTRL_DIV1_SHIFT;
236 return DIV_ROUND_CLOSEST(DIV_ROUND_CLOSEST(
237 zynq_clk_get_pll_rate(priv, ddrpll_clk), div0), div1);
241 static ulong zynq_clk_get_peripheral_rate(struct zynq_clk_priv *priv,
242 enum zynq_clk id, bool two_divs)
248 clk_ctrl = readl(zynq_clk_get_register(id));
250 div0 = (clk_ctrl & CLK_CTRL_DIV0_MASK) >> CLK_CTRL_DIV0_SHIFT;
254 #ifndef CONFIG_SPL_BUILD
256 div1 = (clk_ctrl & CLK_CTRL_DIV1_MASK) >> CLK_CTRL_DIV1_SHIFT;
262 pll = zynq_clk_get_peripheral_pll(clk_ctrl);
267 zynq_clk_get_pll_rate(priv, pll), div0),
271 #ifndef CONFIG_SPL_BUILD
272 static ulong zynq_clk_get_gem_rate(struct zynq_clk_priv *priv, enum zynq_clk id)
276 if (zynq_clk_get_gem_rclk(id) == mio_clk)
277 return zynq_clk_get_peripheral_rate(priv, id, true);
279 parent = &priv->gem_emio_clk[id - gem0_clk];
281 return clk_get_rate(parent);
283 debug("%s: gem%d emio rx clock source unknown\n", __func__,
289 static unsigned long zynq_clk_calc_peripheral_two_divs(ulong rate,
291 u32 *div0, u32 *div1)
293 long new_err, best_err = (long)(~0UL >> 1);
294 ulong new_rate, best_rate = 0;
297 for (d0 = 1; d0 <= ZYNQ_CLK_MAXDIV; d0++) {
298 for (d1 = 1; d1 <= ZYNQ_CLK_MAXDIV >> 1; d1++) {
299 new_rate = DIV_ROUND_CLOSEST(
300 DIV_ROUND_CLOSEST(pll_rate, d0), d1);
301 new_err = abs(new_rate - rate);
303 if (new_err < best_err) {
307 best_rate = new_rate;
315 static ulong zynq_clk_set_peripheral_rate(struct zynq_clk_priv *priv,
316 enum zynq_clk id, ulong rate,
320 u32 clk_ctrl, div0 = 0, div1 = 0;
321 ulong pll_rate, new_rate;
324 reg = zynq_clk_get_register(id);
325 clk_ctrl = readl(reg);
327 pll = zynq_clk_get_peripheral_pll(clk_ctrl);
328 pll_rate = zynq_clk_get_pll_rate(priv, pll);
329 clk_ctrl &= ~CLK_CTRL_DIV0_MASK;
331 clk_ctrl &= ~CLK_CTRL_DIV1_MASK;
332 new_rate = zynq_clk_calc_peripheral_two_divs(rate, pll_rate,
334 clk_ctrl |= div1 << CLK_CTRL_DIV1_SHIFT;
336 div0 = DIV_ROUND_CLOSEST(pll_rate, rate);
337 if (div0 > ZYNQ_CLK_MAXDIV)
338 div0 = ZYNQ_CLK_MAXDIV;
339 new_rate = DIV_ROUND_CLOSEST(rate, div0);
341 clk_ctrl |= div0 << CLK_CTRL_DIV0_SHIFT;
344 writel(clk_ctrl, reg);
350 static ulong zynq_clk_set_gem_rate(struct zynq_clk_priv *priv, enum zynq_clk id,
355 if (zynq_clk_get_gem_rclk(id) == mio_clk)
356 return zynq_clk_set_peripheral_rate(priv, id, rate, true);
358 parent = &priv->gem_emio_clk[id - gem0_clk];
360 return clk_set_rate(parent, rate);
362 debug("%s: gem%d emio rx clock source unknown\n", __func__,
369 #ifndef CONFIG_SPL_BUILD
370 static ulong zynq_clk_get_rate(struct clk *clk)
372 struct zynq_clk_priv *priv = dev_get_priv(clk->dev);
373 enum zynq_clk id = clk->id;
374 bool two_divs = false;
377 case armpll_clk ... iopll_clk:
378 return zynq_clk_get_pll_rate(priv, id);
379 case cpu_6or4x_clk ... cpu_1x_clk:
380 return zynq_clk_get_cpu_rate(priv, id);
382 return zynq_clk_get_ddr2x_rate(priv);
384 return zynq_clk_get_ddr3x_rate(priv);
386 return zynq_clk_get_dci_rate(priv);
387 case gem0_clk ... gem1_clk:
388 return zynq_clk_get_gem_rate(priv, id);
389 case fclk0_clk ... can1_clk:
392 case dbg_trc_clk ... dbg_apb_clk:
393 case lqspi_clk ... pcap_clk:
394 case sdio0_clk ... spi1_clk:
395 return zynq_clk_get_peripheral_rate(priv, id, two_divs);
397 return zynq_clk_get_cpu_rate(priv, cpu_2x_clk);
398 case usb0_aper_clk ... swdt_clk:
399 return zynq_clk_get_cpu_rate(priv, cpu_1x_clk);
405 static ulong zynq_clk_set_rate(struct clk *clk, ulong rate)
407 struct zynq_clk_priv *priv = dev_get_priv(clk->dev);
408 enum zynq_clk id = clk->id;
409 bool two_divs = false;
412 case gem0_clk ... gem1_clk:
413 return zynq_clk_set_gem_rate(priv, id, rate);
414 case fclk0_clk ... can1_clk:
417 case lqspi_clk ... pcap_clk:
418 case sdio0_clk ... spi1_clk:
419 case dbg_trc_clk ... dbg_apb_clk:
420 return zynq_clk_set_peripheral_rate(priv, id, rate, two_divs);
426 static ulong zynq_clk_get_rate(struct clk *clk)
428 struct zynq_clk_priv *priv = dev_get_priv(clk->dev);
429 enum zynq_clk id = clk->id;
432 case cpu_6or4x_clk ... cpu_1x_clk:
433 return zynq_clk_get_cpu_rate(priv, id);
435 return zynq_clk_get_ddr3x_rate(priv);
436 case lqspi_clk ... pcap_clk:
437 case sdio0_clk ... spi1_clk:
438 return zynq_clk_get_peripheral_rate(priv, id, 0);
439 case i2c0_aper_clk ... i2c1_aper_clk:
440 return zynq_clk_get_cpu_rate(priv, cpu_1x_clk);
447 static struct clk_ops zynq_clk_ops = {
448 .get_rate = zynq_clk_get_rate,
449 #ifndef CONFIG_SPL_BUILD
450 .set_rate = zynq_clk_set_rate,
454 static int zynq_clk_probe(struct udevice *dev)
456 struct zynq_clk_priv *priv = dev_get_priv(dev);
457 #ifndef CONFIG_SPL_BUILD
462 for (i = 0; i < 2; i++) {
463 sprintf(name, "gem%d_emio_clk", i);
464 ret = clk_get_by_name(dev, name, &priv->gem_emio_clk[i]);
465 if (ret < 0 && ret != -ENODATA) {
466 dev_err(dev, "failed to get %s clock\n", name);
472 priv->ps_clk_freq = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
473 "ps-clk-frequency", 33333333UL);
478 static const struct udevice_id zynq_clk_ids[] = {
479 { .compatible = "xlnx,ps7-clkc"},
483 U_BOOT_DRIVER(zynq_clk) = {
486 .of_match = zynq_clk_ids,
487 .ops = &zynq_clk_ops,
488 .priv_auto_alloc_size = sizeof(struct zynq_clk_priv),
489 .probe = zynq_clk_probe,