1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Marek Vasut <marex@denx.de>
8 #include <clk-uclass.h>
10 #include <dm/devres.h>
14 #include <asm/arch/clock_manager.h>
16 enum socfpga_a10_clk_type {
17 SOCFPGA_A10_CLK_MAIN_PLL,
18 SOCFPGA_A10_CLK_PER_PLL,
19 SOCFPGA_A10_CLK_PERIP_CLK,
20 SOCFPGA_A10_CLK_GATE_CLK,
21 SOCFPGA_A10_CLK_UNKNOWN_CLK,
24 struct socfpga_a10_clk_platdata {
25 enum socfpga_a10_clk_type type;
30 /* Control register */
32 /* Divider register */
36 /* Clock gating register */
41 static int socfpga_a10_clk_get_upstream(struct clk *clk, struct clk **upclk)
43 struct socfpga_a10_clk_platdata *plat = dev_get_platdata(clk->dev);
46 if (plat->clks.count == 0)
49 if (plat->clks.count == 1) {
50 *upclk = &plat->clks.clks[0];
55 dev_err(clk->dev, "Invalid control register\n");
59 reg = readl(plat->regs + plat->ctl_reg);
61 /* Assume PLLs are ON for now */
62 if (plat->type == SOCFPGA_A10_CLK_MAIN_PLL) {
63 reg = (reg >> 8) & 0x3;
65 } else if (plat->type == SOCFPGA_A10_CLK_PER_PLL) {
66 reg = (reg >> 8) & 0x3;
69 reg = (reg >> 16) & 0x7;
74 dev_err(clk->dev, "Invalid clock source\n");
78 *upclk = &plat->clks.clks[reg];
82 static int socfpga_a10_clk_endisable(struct clk *clk, bool enable)
84 struct socfpga_a10_clk_platdata *plat = dev_get_platdata(clk->dev);
85 struct clk *upclk = NULL;
88 if (!enable && plat->gate_reg)
89 clrbits_le32(plat->regs + plat->gate_reg, BIT(plat->gate_bit));
91 ret = socfpga_a10_clk_get_upstream(clk, &upclk);
102 if (enable && plat->gate_reg)
103 setbits_le32(plat->regs + plat->gate_reg, BIT(plat->gate_bit));
108 static int socfpga_a10_clk_enable(struct clk *clk)
110 return socfpga_a10_clk_endisable(clk, true);
113 static int socfpga_a10_clk_disable(struct clk *clk)
115 return socfpga_a10_clk_endisable(clk, false);
118 static ulong socfpga_a10_clk_get_rate(struct clk *clk)
120 struct socfpga_a10_clk_platdata *plat = dev_get_platdata(clk->dev);
121 struct clk *upclk = NULL;
122 ulong rate = 0, reg, numer, denom;
125 ret = socfpga_a10_clk_get_upstream(clk, &upclk);
129 rate = clk_get_rate(upclk);
131 if (plat->type == SOCFPGA_A10_CLK_MAIN_PLL) {
132 reg = readl(plat->regs + plat->ctl_reg + 4); /* VCO1 */
133 numer = reg & CLKMGR_MAINPLL_VCO1_NUMER_MSK;
134 denom = (reg >> CLKMGR_MAINPLL_VCO1_DENOM_LSB) &
135 CLKMGR_MAINPLL_VCO1_DENOM_MSK;
139 } else if (plat->type == SOCFPGA_A10_CLK_PER_PLL) {
140 reg = readl(plat->regs + plat->ctl_reg + 4); /* VCO1 */
141 numer = reg & CLKMGR_PERPLL_VCO1_NUMER_MSK;
142 denom = (reg >> CLKMGR_PERPLL_VCO1_DENOM_LSB) &
143 CLKMGR_PERPLL_VCO1_DENOM_MSK;
148 rate /= plat->fix_div;
150 if (plat->fix_div == 1 && plat->ctl_reg) {
151 reg = readl(plat->regs + plat->ctl_reg);
157 reg = readl(plat->regs + plat->div_reg);
158 reg >>= plat->div_off;
159 reg &= (1 << plat->div_len) - 1;
160 if (plat->type == SOCFPGA_A10_CLK_PERIP_CLK)
162 if (plat->type == SOCFPGA_A10_CLK_GATE_CLK)
170 static struct clk_ops socfpga_a10_clk_ops = {
171 .enable = socfpga_a10_clk_enable,
172 .disable = socfpga_a10_clk_disable,
173 .get_rate = socfpga_a10_clk_get_rate,
177 * This workaround tries to fix the massively broken generated "handoff" DT,
178 * which contains duplicate clock nodes without any connection to the clock
179 * manager DT node. Yet, those "handoff" DT nodes contain configuration of
180 * the fixed input clock of the Arria10 which are missing from the base DT
183 * This workaround sets up upstream clock for the fixed input clocks of the
184 * A10 described in the base DT such that they map to the fixed clock from
185 * the "handoff" DT. This does not fully match how the clock look on the
186 * A10, but it is the least intrusive way to fix this mess.
188 static void socfpga_a10_handoff_workaround(struct udevice *dev)
190 struct socfpga_a10_clk_platdata *plat = dev_get_platdata(dev);
191 const void *fdt = gd->fdt_blob;
192 struct clk_bulk *bulk = &plat->clks;
193 int i, ret, offset = dev_of_offset(dev);
194 static const char * const socfpga_a10_fixedclk_map[] = {
195 "osc1", "altera_arria10_hps_eosc1",
196 "cb_intosc_ls_clk", "altera_arria10_hps_cb_intosc_ls",
197 "f2s_free_clk", "altera_arria10_hps_f2h_free",
200 if (fdt_node_check_compatible(fdt, offset, "fixed-clock"))
203 for (i = 0; i < ARRAY_SIZE(socfpga_a10_fixedclk_map); i += 2)
204 if (!strcmp(dev->name, socfpga_a10_fixedclk_map[i]))
207 if (i == ARRAY_SIZE(socfpga_a10_fixedclk_map))
210 ret = uclass_get_device_by_name(UCLASS_CLK,
211 socfpga_a10_fixedclk_map[i + 1], &dev);
216 bulk->clks = devm_kcalloc(dev, bulk->count,
217 sizeof(struct clk), GFP_KERNEL);
221 ret = clk_request(dev, &bulk->clks[0]);
226 static int socfpga_a10_clk_bind(struct udevice *dev)
228 const void *fdt = gd->fdt_blob;
229 int offset = dev_of_offset(dev);
230 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
234 for (offset = fdt_first_subnode(fdt, offset);
236 offset = fdt_next_subnode(fdt, offset)) {
237 name = fdt_get_name(fdt, offset, NULL);
241 if (!strcmp(name, "clocks")) {
242 offset = fdt_first_subnode(fdt, offset);
243 name = fdt_get_name(fdt, offset, NULL);
248 /* Filter out supported sub-clock */
249 if (fdt_node_check_compatible(fdt, offset,
250 "altr,socfpga-a10-pll-clock") &&
251 fdt_node_check_compatible(fdt, offset,
252 "altr,socfpga-a10-perip-clk") &&
253 fdt_node_check_compatible(fdt, offset,
254 "altr,socfpga-a10-gate-clk") &&
255 fdt_node_check_compatible(fdt, offset, "fixed-clock"))
258 if (pre_reloc_only &&
259 !dm_ofnode_pre_reloc(offset_to_ofnode(offset)))
262 ret = device_bind_driver_to_node(dev, "clk-a10", name,
263 offset_to_ofnode(offset),
272 static int socfpga_a10_clk_probe(struct udevice *dev)
274 struct socfpga_a10_clk_platdata *plat = dev_get_platdata(dev);
275 const void *fdt = gd->fdt_blob;
276 int offset = dev_of_offset(dev);
278 clk_get_bulk(dev, &plat->clks);
280 socfpga_a10_handoff_workaround(dev);
282 if (!fdt_node_check_compatible(fdt, offset,
283 "altr,socfpga-a10-pll-clock")) {
284 /* Main PLL has 3 upstream clock */
285 if (plat->clks.count == 3)
286 plat->type = SOCFPGA_A10_CLK_MAIN_PLL;
288 plat->type = SOCFPGA_A10_CLK_PER_PLL;
289 } else if (!fdt_node_check_compatible(fdt, offset,
290 "altr,socfpga-a10-perip-clk")) {
291 plat->type = SOCFPGA_A10_CLK_PERIP_CLK;
292 } else if (!fdt_node_check_compatible(fdt, offset,
293 "altr,socfpga-a10-gate-clk")) {
294 plat->type = SOCFPGA_A10_CLK_GATE_CLK;
296 plat->type = SOCFPGA_A10_CLK_UNKNOWN_CLK;
302 static int socfpga_a10_ofdata_to_platdata(struct udevice *dev)
304 struct socfpga_a10_clk_platdata *plat = dev_get_platdata(dev);
305 struct socfpga_a10_clk_platdata *pplat;
306 struct udevice *pdev;
307 const void *fdt = gd->fdt_blob;
308 unsigned int divreg[3], gatereg[2];
309 int ret, offset = dev_of_offset(dev);
312 regs = dev_read_u32_default(dev, "reg", 0x0);
314 if (!fdt_node_check_compatible(fdt, offset, "altr,clk-mgr")) {
315 plat->regs = devfdt_get_addr(dev);
317 pdev = dev_get_parent(dev);
321 pplat = dev_get_platdata(pdev);
325 plat->ctl_reg = regs;
326 plat->regs = pplat->regs;
329 plat->type = SOCFPGA_A10_CLK_UNKNOWN_CLK;
331 plat->fix_div = dev_read_u32_default(dev, "fixed-divider", 1);
333 ret = dev_read_u32_array(dev, "div-reg", divreg, ARRAY_SIZE(divreg));
335 plat->div_reg = divreg[0];
336 plat->div_len = divreg[2];
337 plat->div_off = divreg[1];
340 ret = dev_read_u32_array(dev, "clk-gate", gatereg, ARRAY_SIZE(gatereg));
342 plat->gate_reg = gatereg[0];
343 plat->gate_bit = gatereg[1];
349 static const struct udevice_id socfpga_a10_clk_match[] = {
350 { .compatible = "altr,clk-mgr" },
354 U_BOOT_DRIVER(socfpga_a10_clk) = {
357 .of_match = socfpga_a10_clk_match,
358 .ops = &socfpga_a10_clk_ops,
359 .bind = socfpga_a10_clk_bind,
360 .probe = socfpga_a10_clk_probe,
361 .ofdata_to_platdata = socfpga_a10_ofdata_to_platdata,
363 .platdata_auto_alloc_size = sizeof(struct socfpga_a10_clk_platdata),