1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/delay.h>
4 #include <linux/clk-provider.h>
7 #include <linux/platform_device.h>
8 #include <dt-bindings/clock/en7523-clk.h>
10 #define REG_PCI_CONTROL 0x88
11 #define REG_PCI_CONTROL_PERSTOUT BIT(29)
12 #define REG_PCI_CONTROL_PERSTOUT1 BIT(26)
13 #define REG_PCI_CONTROL_REFCLK_EN1 BIT(22)
14 #define REG_GSW_CLK_DIV_SEL 0x1b4
15 #define REG_EMI_CLK_DIV_SEL 0x1b8
16 #define REG_BUS_CLK_DIV_SEL 0x1bc
17 #define REG_SPI_CLK_DIV_SEL 0x1c4
18 #define REG_SPI_CLK_FREQ_SEL 0x1c8
19 #define REG_NPU_CLK_DIV_SEL 0x1fc
20 #define REG_CRYPTO_CLKSRC 0x200
21 #define REG_RESET_CONTROL 0x834
22 #define REG_RESET_CONTROL_PCIEHB BIT(29)
23 #define REG_RESET_CONTROL_PCIE1 BIT(27)
24 #define REG_RESET_CONTROL_PCIE2 BIT(26)
33 const unsigned int *base_values;
34 unsigned int base_value;
50 static const u32 gsw_base[] = { 400000000, 500000000 };
51 static const u32 emi_base[] = { 333000000, 400000000 };
52 static const u32 bus_base[] = { 500000000, 540000000 };
53 static const u32 slic_base[] = { 100000000, 3125000 };
54 static const u32 npu_base[] = { 333000000, 400000000, 500000000 };
56 static const struct en_clk_desc en7523_base_clks[] = {
61 .base_reg = REG_GSW_CLK_DIV_SEL,
64 .base_values = gsw_base,
65 .n_base_values = ARRAY_SIZE(gsw_base),
74 .base_reg = REG_EMI_CLK_DIV_SEL,
77 .base_values = emi_base,
78 .n_base_values = ARRAY_SIZE(emi_base),
87 .base_reg = REG_BUS_CLK_DIV_SEL,
90 .base_values = bus_base,
91 .n_base_values = ARRAY_SIZE(bus_base),
97 .id = EN7523_CLK_SLIC,
100 .base_reg = REG_SPI_CLK_FREQ_SEL,
103 .base_values = slic_base,
104 .n_base_values = ARRAY_SIZE(slic_base),
106 .div_reg = REG_SPI_CLK_DIV_SEL,
112 .id = EN7523_CLK_SPI,
115 .base_reg = REG_SPI_CLK_DIV_SEL,
117 .base_value = 400000000,
124 .id = EN7523_CLK_NPU,
127 .base_reg = REG_NPU_CLK_DIV_SEL,
130 .base_values = npu_base,
131 .n_base_values = ARRAY_SIZE(npu_base),
137 .id = EN7523_CLK_CRYPTO,
140 .base_reg = REG_CRYPTO_CLKSRC,
143 .base_values = emi_base,
144 .n_base_values = ARRAY_SIZE(emi_base),
148 static const struct of_device_id of_match_clk_en7523[] = {
149 { .compatible = "airoha,en7523-scu", },
153 static unsigned int en7523_get_base_rate(void __iomem *base, unsigned int i)
155 const struct en_clk_desc *desc = &en7523_base_clks[i];
158 if (!desc->base_bits)
159 return desc->base_value;
161 val = readl(base + desc->base_reg);
162 val >>= desc->base_shift;
163 val &= (1 << desc->base_bits) - 1;
165 if (val >= desc->n_base_values)
168 return desc->base_values[val];
171 static u32 en7523_get_div(void __iomem *base, int i)
173 const struct en_clk_desc *desc = &en7523_base_clks[i];
179 reg = desc->div_reg ? desc->div_reg : desc->base_reg;
180 val = readl(base + reg);
181 val >>= desc->div_shift;
182 val &= (1 << desc->div_bits) - 1;
184 if (!val && desc->div_val0)
185 return desc->div_val0;
187 return (val + 1) * desc->div_step;
190 static int en7523_pci_is_enabled(struct clk_hw *hw)
192 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
194 return !!(readl(cg->base + REG_PCI_CONTROL) & REG_PCI_CONTROL_REFCLK_EN1);
197 static int en7523_pci_prepare(struct clk_hw *hw)
199 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
200 void __iomem *np_base = cg->base;
203 /* Need to pull device low before reset */
204 val = readl(np_base + REG_PCI_CONTROL);
205 val &= ~(REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT);
206 writel(val, np_base + REG_PCI_CONTROL);
207 usleep_range(1000, 2000);
209 /* Enable PCIe port 1 */
210 val |= REG_PCI_CONTROL_REFCLK_EN1;
211 writel(val, np_base + REG_PCI_CONTROL);
212 usleep_range(1000, 2000);
214 /* Reset to default */
215 val = readl(np_base + REG_RESET_CONTROL);
216 mask = REG_RESET_CONTROL_PCIE1 | REG_RESET_CONTROL_PCIE2 |
217 REG_RESET_CONTROL_PCIEHB;
218 writel(val & ~mask, np_base + REG_RESET_CONTROL);
219 usleep_range(1000, 2000);
220 writel(val | mask, np_base + REG_RESET_CONTROL);
222 writel(val & ~mask, np_base + REG_RESET_CONTROL);
223 usleep_range(5000, 10000);
226 mask = REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT;
227 val = readl(np_base + REG_PCI_CONTROL);
228 writel(val & ~mask, np_base + REG_PCI_CONTROL);
229 usleep_range(1000, 2000);
230 writel(val | mask, np_base + REG_PCI_CONTROL);
236 static void en7523_pci_unprepare(struct clk_hw *hw)
238 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
239 void __iomem *np_base = cg->base;
242 val = readl(np_base + REG_PCI_CONTROL);
243 val &= ~REG_PCI_CONTROL_REFCLK_EN1;
244 writel(val, np_base + REG_PCI_CONTROL);
247 static struct clk_hw *en7523_register_pcie_clk(struct device *dev,
248 void __iomem *np_base)
250 static const struct clk_ops pcie_gate_ops = {
251 .is_enabled = en7523_pci_is_enabled,
252 .prepare = en7523_pci_prepare,
253 .unprepare = en7523_pci_unprepare,
255 struct clk_init_data init = {
257 .ops = &pcie_gate_ops,
259 struct en_clk_gate *cg;
261 cg = devm_kzalloc(dev, sizeof(*cg), GFP_KERNEL);
267 en7523_pci_unprepare(&cg->hw);
269 if (clk_hw_register(dev, &cg->hw))
275 static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_data *clk_data,
276 void __iomem *base, void __iomem *np_base)
282 for (i = 0; i < ARRAY_SIZE(en7523_base_clks); i++) {
283 const struct en_clk_desc *desc = &en7523_base_clks[i];
285 rate = en7523_get_base_rate(base, i);
286 rate /= en7523_get_div(base, i);
288 hw = clk_hw_register_fixed_rate(dev, desc->name, NULL, 0, rate);
290 pr_err("Failed to register clk %s: %ld\n",
291 desc->name, PTR_ERR(hw));
295 clk_data->hws[desc->id] = hw;
298 hw = en7523_register_pcie_clk(dev, np_base);
299 clk_data->hws[EN7523_CLK_PCIE] = hw;
301 clk_data->num = EN7523_NUM_CLOCKS;
304 static int en7523_clk_probe(struct platform_device *pdev)
306 struct device_node *node = pdev->dev.of_node;
307 struct clk_hw_onecell_data *clk_data;
308 void __iomem *base, *np_base;
311 base = devm_platform_ioremap_resource(pdev, 0);
313 return PTR_ERR(base);
315 np_base = devm_platform_ioremap_resource(pdev, 1);
317 return PTR_ERR(np_base);
319 clk_data = devm_kzalloc(&pdev->dev,
320 struct_size(clk_data, hws, EN7523_NUM_CLOCKS),
325 en7523_register_clocks(&pdev->dev, clk_data, base, np_base);
327 r = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
330 "could not register clock provider: %s: %d\n",
336 static struct platform_driver clk_en7523_drv = {
337 .probe = en7523_clk_probe,
339 .name = "clk-en7523",
340 .of_match_table = of_match_clk_en7523,
341 .suppress_bind_attrs = true,
345 static int __init clk_en7523_init(void)
347 return platform_driver_register(&clk_en7523_drv);
350 arch_initcall(clk_en7523_init);