blob: b75f52d203b23b600b53ddbdf0fe5d2ef38c01a6 [file] [log] [blame]
Teik Heng Chong8ab95782022-06-29 13:51:50 +08001// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
Siew Chin Lim5e317de2021-08-10 11:26:32 +08002/*
Teik Heng Chong8ab95782022-06-29 13:51:50 +08003 * Copyright (C) 2020-2022 Intel Corporation <www.intel.com>
Siew Chin Lim5e317de2021-08-10 11:26:32 +08004 */
5
Siew Chin Lim5e317de2021-08-10 11:26:32 +08006#include <asm/arch/clock_manager.h>
7#include <asm/global_data.h>
8#include <asm/io.h>
9#include "clk-mem-n5x.h"
10#include <clk-uclass.h>
11#include <dm.h>
12#include <dm/lists.h>
13#include <dm/util.h>
14#include <dt-bindings/clock/n5x-clock.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18struct socfpga_mem_clk_plat {
19 void __iomem *regs;
20};
21
22void clk_mem_wait_for_lock(struct socfpga_mem_clk_plat *plat, u32 mask)
23{
24 u32 inter_val;
25 u32 retry = 0;
26
27 do {
28 inter_val = CM_REG_READL(plat, MEMCLKMGR_STAT) & mask;
29
30 /* Wait for stable lock */
31 if (inter_val == mask)
32 retry++;
33 else
34 retry = 0;
35
36 if (retry >= 10)
37 return;
38 } while (1);
39}
40
41/*
42 * function to write the bypass register which requires a poll of the
43 * busy bit
44 */
45void clk_mem_write_bypass_mempll(struct socfpga_mem_clk_plat *plat, u32 val)
46{
47 CM_REG_WRITEL(plat, val, MEMCLKMGR_MEMPLL_BYPASS);
48}
49
50/*
51 * Setup clocks while making no assumptions about previous state of the clocks.
52 */
53static void clk_mem_basic_init(struct udevice *dev,
54 const struct cm_config * const cfg)
55{
56 struct socfpga_mem_clk_plat *plat = dev_get_plat(dev);
57
58 if (!cfg)
59 return;
60
61 /* Put PLLs in bypass */
62 clk_mem_write_bypass_mempll(plat, MEMCLKMGR_BYPASS_MEMPLL_ALL);
63
64 /* Put PLLs in Reset */
65 CM_REG_SETBITS(plat, MEMCLKMGR_MEMPLL_PLLCTRL,
66 MEMCLKMGR_PLLCTRL_BYPASS_MASK);
67
68 /* setup mem PLL */
69 CM_REG_WRITEL(plat, cfg->mem_memdiv, MEMCLKMGR_MEMPLL_MEMDIV);
70 CM_REG_WRITEL(plat, cfg->mem_pllglob, MEMCLKMGR_MEMPLL_PLLGLOB);
71 CM_REG_WRITEL(plat, cfg->mem_plldiv, MEMCLKMGR_MEMPLL_PLLDIV);
72 CM_REG_WRITEL(plat, cfg->mem_plloutdiv, MEMCLKMGR_MEMPLL_PLLOUTDIV);
73
74 /* Take PLL out of reset and power up */
75 CM_REG_CLRBITS(plat, MEMCLKMGR_MEMPLL_PLLCTRL,
76 MEMCLKMGR_PLLCTRL_BYPASS_MASK);
77}
78
79static int socfpga_mem_clk_enable(struct clk *clk)
80{
81 const struct cm_config *cm_default_cfg = cm_get_default_config();
82 struct socfpga_mem_clk_plat *plat = dev_get_plat(clk->dev);
83
84 clk_mem_basic_init(clk->dev, cm_default_cfg);
85
86 clk_mem_wait_for_lock(plat, MEMCLKMGR_STAT_ALLPLL_LOCKED_MASK);
87
88 CM_REG_WRITEL(plat, CM_REG_READL(plat, MEMCLKMGR_MEMPLL_PLLGLOB) |
89 MEMCLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK,
90 MEMCLKMGR_MEMPLL_PLLGLOB);
91
92 /* Take all PLLs out of bypass */
93 clk_mem_write_bypass_mempll(plat, 0);
94
95 /* Clear the loss of lock bits (write 1 to clear) */
96 CM_REG_CLRBITS(plat, MEMCLKMGR_INTRCLR,
97 MEMCLKMGR_INTER_MEMPLLLOST_MASK);
98
99 /* Take all ping pong counters out of reset */
100 CM_REG_CLRBITS(plat, MEMCLKMGR_MEMPLL_EXTCNTRST,
101 MEMCLKMGR_EXTCNTRST_ALLCNTRST);
102
103 return 0;
104}
105
106static int socfpga_mem_clk_of_to_plat(struct udevice *dev)
107{
108 struct socfpga_mem_clk_plat *plat = dev_get_plat(dev);
109 fdt_addr_t addr;
110
111 addr = devfdt_get_addr(dev);
112 if (addr == FDT_ADDR_T_NONE)
113 return -EINVAL;
114 plat->regs = (void __iomem *)addr;
115
116 return 0;
117}
118
119static struct clk_ops socfpga_mem_clk_ops = {
120 .enable = socfpga_mem_clk_enable
121};
122
123static const struct udevice_id socfpga_mem_clk_match[] = {
124 { .compatible = "intel,n5x-mem-clkmgr" },
125 {}
126};
127
128U_BOOT_DRIVER(socfpga_n5x_mem_clk) = {
129 .name = "mem-clk-n5x",
130 .id = UCLASS_CLK,
131 .of_match = socfpga_mem_clk_match,
132 .ops = &socfpga_mem_clk_ops,
133 .of_to_plat = socfpga_mem_clk_of_to_plat,
134 .plat_auto = sizeof(struct socfpga_mem_clk_plat),
135};