1 // SPDX-License-Identifier: GPL-2.0+
3 * Marvell Armada 37xx SoC Time Base Generator clocks
5 * Marek Behun <marek.behun@nic.cz>
7 * Based on Linux driver by:
8 * Gregory CLEMENT <gregory.clement@free-electrons.com>
12 #include <clk-uclass.h>
16 #include <asm/arch/cpu.h>
22 #define TBG_CTRL7 0x20
23 #define TBG_CTRL8 0x30
25 #define TBG_DIV_MASK 0x1FF
27 #define TBG_A_REFDIV 0
28 #define TBG_B_REFDIV 16
31 #define TBG_B_FBDIV 18
33 #define TBG_A_VCODIV_SE 0
34 #define TBG_B_VCODIV_SE 16
36 #define TBG_A_VCODIV_DIFF 1
37 #define TBG_B_VCODIV_DIFF 17
47 static const struct tbg_def tbg[NUM_TBG] = {
48 {"TBG-A-P", TBG_A_REFDIV, TBG_A_FBDIV, TBG_CTRL8, TBG_A_VCODIV_DIFF},
49 {"TBG-B-P", TBG_B_REFDIV, TBG_B_FBDIV, TBG_CTRL8, TBG_B_VCODIV_DIFF},
50 {"TBG-A-S", TBG_A_REFDIV, TBG_A_FBDIV, TBG_CTRL1, TBG_A_VCODIV_SE},
51 {"TBG-B-S", TBG_B_REFDIV, TBG_B_FBDIV, TBG_CTRL1, TBG_B_VCODIV_SE},
56 unsigned int mult[NUM_TBG];
57 unsigned int div[NUM_TBG];
60 static unsigned int tbg_get_mult(void __iomem *reg, const struct tbg_def *ptbg)
64 val = readl(reg + TBG_CTRL0);
66 return ((val >> ptbg->fbdiv_offset) & TBG_DIV_MASK) << 2;
69 static unsigned int tbg_get_div(void __iomem *reg, const struct tbg_def *ptbg)
74 val = readl(reg + TBG_CTRL7);
76 div = (val >> ptbg->refdiv_offset) & TBG_DIV_MASK;
79 val = readl(reg + ptbg->vcodiv_reg);
81 div *= 1 << ((val >> ptbg->vcodiv_offset) & TBG_DIV_MASK);
86 static ulong armada_37xx_tbg_clk_get_rate(struct clk *clk)
88 struct a37xx_tbgclk *priv = dev_get_priv(clk->dev);
90 if (clk->id >= NUM_TBG)
93 return priv->rates[clk->id];
96 #if defined(CONFIG_CMD_CLK) && defined(CONFIG_CLK_ARMADA_3720)
97 int armada_37xx_tbg_clk_dump(struct udevice *dev)
99 struct a37xx_tbgclk *priv = dev_get_priv(dev);
102 for (i = 0; i < NUM_TBG; ++i)
103 printf(" %s at %lu Hz\n", tbg[i].name,
111 static int armada_37xx_tbg_clk_probe(struct udevice *dev)
113 struct a37xx_tbgclk *priv = dev_get_priv(dev);
118 reg = dev_read_addr_ptr(dev);
120 dev_err(dev, "no io address\n");
124 xtal = (ulong)get_ref_clk() * 1000000;
126 for (i = 0; i < NUM_TBG; ++i) {
127 unsigned int mult, div;
129 mult = tbg_get_mult(reg, &tbg[i]);
130 div = tbg_get_div(reg, &tbg[i]);
132 priv->rates[i] = (xtal * mult) / div;
138 static const struct clk_ops armada_37xx_tbg_clk_ops = {
139 .get_rate = armada_37xx_tbg_clk_get_rate,
142 static const struct udevice_id armada_37xx_tbg_clk_ids[] = {
143 { .compatible = "marvell,armada-3700-tbg-clock" },
147 U_BOOT_DRIVER(armada_37xx_tbg_clk) = {
148 .name = "armada_37xx_tbg_clk",
150 .of_match = armada_37xx_tbg_clk_ids,
151 .ops = &armada_37xx_tbg_clk_ops,
152 .priv_auto_alloc_size = sizeof(struct a37xx_tbgclk),
153 .probe = armada_37xx_tbg_clk_probe,