1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/delay.h>
4 #include <linux/clk-provider.h>
6 #include <linux/platform_device.h>
7 #include <linux/property.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_EN0 BIT(23)
14 #define REG_PCI_CONTROL_REFCLK_EN1 BIT(22)
15 #define REG_PCI_CONTROL_PERSTOUT2 BIT(16)
16 #define REG_GSW_CLK_DIV_SEL 0x1b4
17 #define REG_EMI_CLK_DIV_SEL 0x1b8
18 #define REG_BUS_CLK_DIV_SEL 0x1bc
19 #define REG_SPI_CLK_DIV_SEL 0x1c4
20 #define REG_SPI_CLK_FREQ_SEL 0x1c8
21 #define REG_NPU_CLK_DIV_SEL 0x1fc
22 #define REG_CRYPTO_CLKSRC 0x200
23 #define REG_RESET_CONTROL2 0x830
24 #define REG_RESET2_CONTROL_PCIE2 BIT(27)
25 #define REG_RESET_CONTROL1 0x834
26 #define REG_RESET_CONTROL_PCIEHB BIT(29)
27 #define REG_RESET_CONTROL_PCIE1 BIT(27)
28 #define REG_RESET_CONTROL_PCIE2 BIT(26)
30 #define REG_PCIE0_MEM 0x00
31 #define REG_PCIE0_MEM_MASK 0x04
32 #define REG_PCIE1_MEM 0x08
33 #define REG_PCIE1_MEM_MASK 0x0c
34 #define REG_PCIE2_MEM 0x10
35 #define REG_PCIE2_MEM_MASK 0x14
36 #define REG_PCIE_RESET_OPEN_DRAIN 0x018c
37 #define REG_PCIE_RESET_OPEN_DRAIN_MASK GENMASK(2, 0)
38 #define REG_NP_SCU_PCIC 0x88
39 #define REG_NP_SCU_SSTR 0x9c
40 #define REG_PCIE_XSI0_SEL_MASK GENMASK(14, 13)
41 #define REG_PCIE_XSI1_SEL_MASK GENMASK(12, 11)
50 const unsigned int *base_values;
51 unsigned int base_value;
67 struct en_clk_soc_data {
68 const struct clk_ops pcie_ops;
69 int (*hw_init)(struct platform_device *pdev, void __iomem *base,
70 void __iomem *np_base);
73 static const u32 gsw_base[] = { 400000000, 500000000 };
74 static const u32 emi_base[] = { 333000000, 400000000 };
75 static const u32 bus_base[] = { 500000000, 540000000 };
76 static const u32 slic_base[] = { 100000000, 3125000 };
77 static const u32 npu_base[] = { 333000000, 400000000, 500000000 };
79 static const struct en_clk_desc en7523_base_clks[] = {
84 .base_reg = REG_GSW_CLK_DIV_SEL,
87 .base_values = gsw_base,
88 .n_base_values = ARRAY_SIZE(gsw_base),
97 .base_reg = REG_EMI_CLK_DIV_SEL,
100 .base_values = emi_base,
101 .n_base_values = ARRAY_SIZE(emi_base),
107 .id = EN7523_CLK_BUS,
110 .base_reg = REG_BUS_CLK_DIV_SEL,
113 .base_values = bus_base,
114 .n_base_values = ARRAY_SIZE(bus_base),
120 .id = EN7523_CLK_SLIC,
123 .base_reg = REG_SPI_CLK_FREQ_SEL,
126 .base_values = slic_base,
127 .n_base_values = ARRAY_SIZE(slic_base),
129 .div_reg = REG_SPI_CLK_DIV_SEL,
135 .id = EN7523_CLK_SPI,
138 .base_reg = REG_SPI_CLK_DIV_SEL,
140 .base_value = 400000000,
147 .id = EN7523_CLK_NPU,
150 .base_reg = REG_NPU_CLK_DIV_SEL,
153 .base_values = npu_base,
154 .n_base_values = ARRAY_SIZE(npu_base),
160 .id = EN7523_CLK_CRYPTO,
163 .base_reg = REG_CRYPTO_CLKSRC,
166 .base_values = emi_base,
167 .n_base_values = ARRAY_SIZE(emi_base),
171 static unsigned int en7523_get_base_rate(void __iomem *base, unsigned int i)
173 const struct en_clk_desc *desc = &en7523_base_clks[i];
176 if (!desc->base_bits)
177 return desc->base_value;
179 val = readl(base + desc->base_reg);
180 val >>= desc->base_shift;
181 val &= (1 << desc->base_bits) - 1;
183 if (val >= desc->n_base_values)
186 return desc->base_values[val];
189 static u32 en7523_get_div(void __iomem *base, int i)
191 const struct en_clk_desc *desc = &en7523_base_clks[i];
197 reg = desc->div_reg ? desc->div_reg : desc->base_reg;
198 val = readl(base + reg);
199 val >>= desc->div_shift;
200 val &= (1 << desc->div_bits) - 1;
202 if (!val && desc->div_val0)
203 return desc->div_val0;
205 return (val + 1) * desc->div_step;
208 static int en7523_pci_is_enabled(struct clk_hw *hw)
210 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
212 return !!(readl(cg->base + REG_PCI_CONTROL) & REG_PCI_CONTROL_REFCLK_EN1);
215 static int en7523_pci_prepare(struct clk_hw *hw)
217 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
218 void __iomem *np_base = cg->base;
221 /* Need to pull device low before reset */
222 val = readl(np_base + REG_PCI_CONTROL);
223 val &= ~(REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT);
224 writel(val, np_base + REG_PCI_CONTROL);
225 usleep_range(1000, 2000);
227 /* Enable PCIe port 1 */
228 val |= REG_PCI_CONTROL_REFCLK_EN1;
229 writel(val, np_base + REG_PCI_CONTROL);
230 usleep_range(1000, 2000);
232 /* Reset to default */
233 val = readl(np_base + REG_RESET_CONTROL1);
234 mask = REG_RESET_CONTROL_PCIE1 | REG_RESET_CONTROL_PCIE2 |
235 REG_RESET_CONTROL_PCIEHB;
236 writel(val & ~mask, np_base + REG_RESET_CONTROL1);
237 usleep_range(1000, 2000);
238 writel(val | mask, np_base + REG_RESET_CONTROL1);
240 writel(val & ~mask, np_base + REG_RESET_CONTROL1);
241 usleep_range(5000, 10000);
244 mask = REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT;
245 val = readl(np_base + REG_PCI_CONTROL);
246 writel(val & ~mask, np_base + REG_PCI_CONTROL);
247 usleep_range(1000, 2000);
248 writel(val | mask, np_base + REG_PCI_CONTROL);
254 static void en7523_pci_unprepare(struct clk_hw *hw)
256 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
257 void __iomem *np_base = cg->base;
260 val = readl(np_base + REG_PCI_CONTROL);
261 val &= ~REG_PCI_CONTROL_REFCLK_EN1;
262 writel(val, np_base + REG_PCI_CONTROL);
265 static struct clk_hw *en7523_register_pcie_clk(struct device *dev,
266 void __iomem *np_base)
268 const struct en_clk_soc_data *soc_data = device_get_match_data(dev);
269 struct clk_init_data init = {
271 .ops = &soc_data->pcie_ops,
273 struct en_clk_gate *cg;
275 cg = devm_kzalloc(dev, sizeof(*cg), GFP_KERNEL);
282 if (init.ops->disable)
283 init.ops->disable(&cg->hw);
284 init.ops->unprepare(&cg->hw);
286 if (clk_hw_register(dev, &cg->hw))
292 static int en7581_pci_is_enabled(struct clk_hw *hw)
294 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
297 mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1;
298 val = readl(cg->base + REG_PCI_CONTROL);
299 return (val & mask) == mask;
302 static int en7581_pci_prepare(struct clk_hw *hw)
304 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
305 void __iomem *np_base = cg->base;
308 mask = REG_RESET_CONTROL_PCIE1 | REG_RESET_CONTROL_PCIE2 |
309 REG_RESET_CONTROL_PCIEHB;
310 val = readl(np_base + REG_RESET_CONTROL1);
311 writel(val & ~mask, np_base + REG_RESET_CONTROL1);
312 val = readl(np_base + REG_RESET_CONTROL2);
313 writel(val & ~REG_RESET2_CONTROL_PCIE2, np_base + REG_RESET_CONTROL2);
314 usleep_range(5000, 10000);
319 static int en7581_pci_enable(struct clk_hw *hw)
321 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
322 void __iomem *np_base = cg->base;
325 mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1 |
326 REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT2 |
327 REG_PCI_CONTROL_PERSTOUT;
328 val = readl(np_base + REG_PCI_CONTROL);
329 writel(val | mask, np_base + REG_PCI_CONTROL);
335 static void en7581_pci_unprepare(struct clk_hw *hw)
337 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
338 void __iomem *np_base = cg->base;
341 mask = REG_RESET_CONTROL_PCIE1 | REG_RESET_CONTROL_PCIE2 |
342 REG_RESET_CONTROL_PCIEHB;
343 val = readl(np_base + REG_RESET_CONTROL1);
344 writel(val | mask, np_base + REG_RESET_CONTROL1);
345 mask = REG_RESET_CONTROL_PCIE1 | REG_RESET_CONTROL_PCIE2;
346 writel(val | mask, np_base + REG_RESET_CONTROL1);
347 val = readl(np_base + REG_RESET_CONTROL2);
348 writel(val | REG_RESET_CONTROL_PCIE2, np_base + REG_RESET_CONTROL2);
352 static void en7581_pci_disable(struct clk_hw *hw)
354 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
355 void __iomem *np_base = cg->base;
358 mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1 |
359 REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT2 |
360 REG_PCI_CONTROL_PERSTOUT;
361 val = readl(np_base + REG_PCI_CONTROL);
362 writel(val & ~mask, np_base + REG_PCI_CONTROL);
363 usleep_range(1000, 2000);
366 static int en7581_clk_hw_init(struct platform_device *pdev,
368 void __iomem *np_base)
370 void __iomem *pb_base;
373 pb_base = devm_platform_ioremap_resource(pdev, 2);
375 return PTR_ERR(pb_base);
377 val = readl(np_base + REG_NP_SCU_SSTR);
378 val &= ~(REG_PCIE_XSI0_SEL_MASK | REG_PCIE_XSI1_SEL_MASK);
379 writel(val, np_base + REG_NP_SCU_SSTR);
380 val = readl(np_base + REG_NP_SCU_PCIC);
381 writel(val | 3, np_base + REG_NP_SCU_PCIC);
383 writel(0x20000000, pb_base + REG_PCIE0_MEM);
384 writel(0xfc000000, pb_base + REG_PCIE0_MEM_MASK);
385 writel(0x24000000, pb_base + REG_PCIE1_MEM);
386 writel(0xfc000000, pb_base + REG_PCIE1_MEM_MASK);
387 writel(0x28000000, pb_base + REG_PCIE2_MEM);
388 writel(0xfc000000, pb_base + REG_PCIE2_MEM_MASK);
390 val = readl(base + REG_PCIE_RESET_OPEN_DRAIN);
391 writel(val | REG_PCIE_RESET_OPEN_DRAIN_MASK,
392 base + REG_PCIE_RESET_OPEN_DRAIN);
397 static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_data *clk_data,
398 void __iomem *base, void __iomem *np_base)
404 for (i = 0; i < ARRAY_SIZE(en7523_base_clks); i++) {
405 const struct en_clk_desc *desc = &en7523_base_clks[i];
407 rate = en7523_get_base_rate(base, i);
408 rate /= en7523_get_div(base, i);
410 hw = clk_hw_register_fixed_rate(dev, desc->name, NULL, 0, rate);
412 pr_err("Failed to register clk %s: %ld\n",
413 desc->name, PTR_ERR(hw));
417 clk_data->hws[desc->id] = hw;
420 hw = en7523_register_pcie_clk(dev, np_base);
421 clk_data->hws[EN7523_CLK_PCIE] = hw;
423 clk_data->num = EN7523_NUM_CLOCKS;
426 static int en7523_clk_probe(struct platform_device *pdev)
428 struct device_node *node = pdev->dev.of_node;
429 const struct en_clk_soc_data *soc_data;
430 struct clk_hw_onecell_data *clk_data;
431 void __iomem *base, *np_base;
434 base = devm_platform_ioremap_resource(pdev, 0);
436 return PTR_ERR(base);
438 np_base = devm_platform_ioremap_resource(pdev, 1);
440 return PTR_ERR(np_base);
442 soc_data = device_get_match_data(&pdev->dev);
443 if (soc_data->hw_init) {
444 r = soc_data->hw_init(pdev, base, np_base);
449 clk_data = devm_kzalloc(&pdev->dev,
450 struct_size(clk_data, hws, EN7523_NUM_CLOCKS),
455 en7523_register_clocks(&pdev->dev, clk_data, base, np_base);
457 r = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
460 "could not register clock provider: %s: %d\n",
466 static const struct en_clk_soc_data en7523_data = {
468 .is_enabled = en7523_pci_is_enabled,
469 .prepare = en7523_pci_prepare,
470 .unprepare = en7523_pci_unprepare,
474 static const struct en_clk_soc_data en7581_data = {
476 .is_enabled = en7581_pci_is_enabled,
477 .prepare = en7581_pci_prepare,
478 .enable = en7581_pci_enable,
479 .unprepare = en7581_pci_unprepare,
480 .disable = en7581_pci_disable,
482 .hw_init = en7581_clk_hw_init,
485 static const struct of_device_id of_match_clk_en7523[] = {
486 { .compatible = "airoha,en7523-scu", .data = &en7523_data },
487 { .compatible = "airoha,en7581-scu", .data = &en7581_data },
491 static struct platform_driver clk_en7523_drv = {
492 .probe = en7523_clk_probe,
494 .name = "clk-en7523",
495 .of_match_table = of_match_clk_en7523,
496 .suppress_bind_attrs = true,
500 static int __init clk_en7523_init(void)
502 return platform_driver_register(&clk_en7523_drv);
505 arch_initcall(clk_en7523_init);