1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/delay.h>
4 #include <linux/clk-provider.h>
6 #include <linux/mfd/syscon.h>
7 #include <linux/platform_device.h>
8 #include <linux/property.h>
9 #include <linux/regmap.h>
10 #include <linux/reset-controller.h>
11 #include <dt-bindings/clock/en7523-clk.h>
12 #include <dt-bindings/reset/airoha,en7581-reset.h>
14 #define RST_NR_PER_BANK 32
16 #define REG_PCI_CONTROL 0x88
17 #define REG_PCI_CONTROL_PERSTOUT BIT(29)
18 #define REG_PCI_CONTROL_PERSTOUT1 BIT(26)
19 #define REG_PCI_CONTROL_REFCLK_EN0 BIT(23)
20 #define REG_PCI_CONTROL_REFCLK_EN1 BIT(22)
21 #define REG_PCI_CONTROL_PERSTOUT2 BIT(16)
22 #define REG_GSW_CLK_DIV_SEL 0x1b4
23 #define REG_EMI_CLK_DIV_SEL 0x1b8
24 #define REG_BUS_CLK_DIV_SEL 0x1bc
25 #define REG_SPI_CLK_DIV_SEL 0x1c4
26 #define REG_SPI_CLK_FREQ_SEL 0x1c8
27 #define REG_NPU_CLK_DIV_SEL 0x1fc
28 #define REG_CRYPTO_CLKSRC 0x200
29 #define REG_RESET_CONTROL2 0x830
30 #define REG_RESET2_CONTROL_PCIE2 BIT(27)
31 #define REG_RESET_CONTROL1 0x834
32 #define REG_RESET_CONTROL_PCIEHB BIT(29)
33 #define REG_RESET_CONTROL_PCIE1 BIT(27)
34 #define REG_RESET_CONTROL_PCIE2 BIT(26)
36 #define REG_NP_SCU_PCIC 0x88
37 #define REG_NP_SCU_SSTR 0x9c
38 #define REG_PCIE_XSI0_SEL_MASK GENMASK(14, 13)
39 #define REG_PCIE_XSI1_SEL_MASK GENMASK(12, 11)
40 #define REG_CRYPTO_CLKSRC2 0x20c
42 #define REG_RST_CTRL2 0x830
43 #define REG_RST_CTRL1 0x834
52 const unsigned int *base_values;
53 unsigned int base_value;
74 struct reset_controller_dev rcdev;
77 struct en_clk_soc_data {
78 const struct clk_ops pcie_ops;
79 int (*hw_init)(struct platform_device *pdev,
80 struct clk_hw_onecell_data *clk_data);
83 static const u32 gsw_base[] = { 400000000, 500000000 };
84 static const u32 emi_base[] = { 333000000, 400000000 };
85 static const u32 bus_base[] = { 500000000, 540000000 };
86 static const u32 slic_base[] = { 100000000, 3125000 };
87 static const u32 npu_base[] = { 333000000, 400000000, 500000000 };
89 static const u32 emi7581_base[] = { 540000000, 480000000, 400000000, 300000000 };
90 static const u32 bus7581_base[] = { 600000000, 540000000 };
91 static const u32 npu7581_base[] = { 800000000, 750000000, 720000000, 600000000 };
92 static const u32 crypto_base[] = { 540000000, 480000000 };
94 static const struct en_clk_desc en7523_base_clks[] = {
99 .base_reg = REG_GSW_CLK_DIV_SEL,
102 .base_values = gsw_base,
103 .n_base_values = ARRAY_SIZE(gsw_base),
110 .id = EN7523_CLK_EMI,
113 .base_reg = REG_EMI_CLK_DIV_SEL,
116 .base_values = emi_base,
117 .n_base_values = ARRAY_SIZE(emi_base),
124 .id = EN7523_CLK_BUS,
127 .base_reg = REG_BUS_CLK_DIV_SEL,
130 .base_values = bus_base,
131 .n_base_values = ARRAY_SIZE(bus_base),
138 .id = EN7523_CLK_SLIC,
141 .base_reg = REG_SPI_CLK_FREQ_SEL,
144 .base_values = slic_base,
145 .n_base_values = ARRAY_SIZE(slic_base),
147 .div_reg = REG_SPI_CLK_DIV_SEL,
153 .id = EN7523_CLK_SPI,
156 .base_reg = REG_SPI_CLK_DIV_SEL,
158 .base_value = 400000000,
165 .id = EN7523_CLK_NPU,
168 .base_reg = REG_NPU_CLK_DIV_SEL,
171 .base_values = npu_base,
172 .n_base_values = ARRAY_SIZE(npu_base),
179 .id = EN7523_CLK_CRYPTO,
182 .base_reg = REG_CRYPTO_CLKSRC,
185 .base_values = emi_base,
186 .n_base_values = ARRAY_SIZE(emi_base),
190 static const struct en_clk_desc en7581_base_clks[] = {
192 .id = EN7523_CLK_GSW,
195 .base_reg = REG_GSW_CLK_DIV_SEL,
198 .base_values = gsw_base,
199 .n_base_values = ARRAY_SIZE(gsw_base),
206 .id = EN7523_CLK_EMI,
209 .base_reg = REG_EMI_CLK_DIV_SEL,
212 .base_values = emi7581_base,
213 .n_base_values = ARRAY_SIZE(emi7581_base),
220 .id = EN7523_CLK_BUS,
223 .base_reg = REG_BUS_CLK_DIV_SEL,
226 .base_values = bus7581_base,
227 .n_base_values = ARRAY_SIZE(bus7581_base),
234 .id = EN7523_CLK_SLIC,
237 .base_reg = REG_SPI_CLK_FREQ_SEL,
240 .base_values = slic_base,
241 .n_base_values = ARRAY_SIZE(slic_base),
243 .div_reg = REG_SPI_CLK_DIV_SEL,
249 .id = EN7523_CLK_SPI,
252 .base_reg = REG_SPI_CLK_DIV_SEL,
254 .base_value = 400000000,
261 .id = EN7523_CLK_NPU,
264 .base_reg = REG_NPU_CLK_DIV_SEL,
267 .base_values = npu7581_base,
268 .n_base_values = ARRAY_SIZE(npu7581_base),
275 .id = EN7523_CLK_CRYPTO,
278 .base_reg = REG_CRYPTO_CLKSRC2,
281 .base_values = crypto_base,
282 .n_base_values = ARRAY_SIZE(crypto_base),
286 static const u16 en7581_rst_ofs[] = {
291 static const u16 en7581_rst_map[] = {
293 [EN7581_XPON_PHY_RST] = 0,
294 [EN7581_CPU_TIMER2_RST] = 2,
295 [EN7581_HSUART_RST] = 3,
296 [EN7581_UART4_RST] = 4,
297 [EN7581_UART5_RST] = 5,
298 [EN7581_I2C2_RST] = 6,
299 [EN7581_XSI_MAC_RST] = 7,
300 [EN7581_XSI_PHY_RST] = 8,
301 [EN7581_NPU_RST] = 9,
302 [EN7581_I2S_RST] = 10,
303 [EN7581_TRNG_RST] = 11,
304 [EN7581_TRNG_MSTART_RST] = 12,
305 [EN7581_DUAL_HSI0_RST] = 13,
306 [EN7581_DUAL_HSI1_RST] = 14,
307 [EN7581_HSI_RST] = 15,
308 [EN7581_DUAL_HSI0_MAC_RST] = 16,
309 [EN7581_DUAL_HSI1_MAC_RST] = 17,
310 [EN7581_HSI_MAC_RST] = 18,
311 [EN7581_WDMA_RST] = 19,
312 [EN7581_WOE0_RST] = 20,
313 [EN7581_WOE1_RST] = 21,
314 [EN7581_HSDMA_RST] = 22,
315 [EN7581_TDMA_RST] = 24,
316 [EN7581_EMMC_RST] = 25,
317 [EN7581_SOE_RST] = 26,
318 [EN7581_PCIE2_RST] = 27,
319 [EN7581_XFP_MAC_RST] = 28,
320 [EN7581_USB_HOST_P1_RST] = 29,
321 [EN7581_USB_HOST_P1_U3_PHY_RST] = 30,
323 [EN7581_PCM1_ZSI_ISI_RST] = RST_NR_PER_BANK + 0,
324 [EN7581_FE_PDMA_RST] = RST_NR_PER_BANK + 1,
325 [EN7581_FE_QDMA_RST] = RST_NR_PER_BANK + 2,
326 [EN7581_PCM_SPIWP_RST] = RST_NR_PER_BANK + 4,
327 [EN7581_CRYPTO_RST] = RST_NR_PER_BANK + 6,
328 [EN7581_TIMER_RST] = RST_NR_PER_BANK + 8,
329 [EN7581_PCM1_RST] = RST_NR_PER_BANK + 11,
330 [EN7581_UART_RST] = RST_NR_PER_BANK + 12,
331 [EN7581_GPIO_RST] = RST_NR_PER_BANK + 13,
332 [EN7581_GDMA_RST] = RST_NR_PER_BANK + 14,
333 [EN7581_I2C_MASTER_RST] = RST_NR_PER_BANK + 16,
334 [EN7581_PCM2_ZSI_ISI_RST] = RST_NR_PER_BANK + 17,
335 [EN7581_SFC_RST] = RST_NR_PER_BANK + 18,
336 [EN7581_UART2_RST] = RST_NR_PER_BANK + 19,
337 [EN7581_GDMP_RST] = RST_NR_PER_BANK + 20,
338 [EN7581_FE_RST] = RST_NR_PER_BANK + 21,
339 [EN7581_USB_HOST_P0_RST] = RST_NR_PER_BANK + 22,
340 [EN7581_GSW_RST] = RST_NR_PER_BANK + 23,
341 [EN7581_SFC2_PCM_RST] = RST_NR_PER_BANK + 25,
342 [EN7581_PCIE0_RST] = RST_NR_PER_BANK + 26,
343 [EN7581_PCIE1_RST] = RST_NR_PER_BANK + 27,
344 [EN7581_CPU_TIMER_RST] = RST_NR_PER_BANK + 28,
345 [EN7581_PCIE_HB_RST] = RST_NR_PER_BANK + 29,
346 [EN7581_XPON_MAC_RST] = RST_NR_PER_BANK + 31,
349 static u32 en7523_get_base_rate(const struct en_clk_desc *desc, u32 val)
351 if (!desc->base_bits)
352 return desc->base_value;
354 val >>= desc->base_shift;
355 val &= (1 << desc->base_bits) - 1;
357 if (val >= desc->n_base_values)
360 return desc->base_values[val];
363 static u32 en7523_get_div(const struct en_clk_desc *desc, u32 val)
368 val >>= desc->div_shift;
369 val &= (1 << desc->div_bits) - 1;
371 if (!val && desc->div_val0)
372 return desc->div_val0;
374 return (val + desc->div_offset) * desc->div_step;
377 static int en7523_pci_is_enabled(struct clk_hw *hw)
379 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
381 return !!(readl(cg->base + REG_PCI_CONTROL) & REG_PCI_CONTROL_REFCLK_EN1);
384 static int en7523_pci_prepare(struct clk_hw *hw)
386 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
387 void __iomem *np_base = cg->base;
390 /* Need to pull device low before reset */
391 val = readl(np_base + REG_PCI_CONTROL);
392 val &= ~(REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT);
393 writel(val, np_base + REG_PCI_CONTROL);
394 usleep_range(1000, 2000);
396 /* Enable PCIe port 1 */
397 val |= REG_PCI_CONTROL_REFCLK_EN1;
398 writel(val, np_base + REG_PCI_CONTROL);
399 usleep_range(1000, 2000);
401 /* Reset to default */
402 val = readl(np_base + REG_RESET_CONTROL1);
403 mask = REG_RESET_CONTROL_PCIE1 | REG_RESET_CONTROL_PCIE2 |
404 REG_RESET_CONTROL_PCIEHB;
405 writel(val & ~mask, np_base + REG_RESET_CONTROL1);
406 usleep_range(1000, 2000);
407 writel(val | mask, np_base + REG_RESET_CONTROL1);
409 writel(val & ~mask, np_base + REG_RESET_CONTROL1);
410 usleep_range(5000, 10000);
413 mask = REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT;
414 val = readl(np_base + REG_PCI_CONTROL);
415 writel(val & ~mask, np_base + REG_PCI_CONTROL);
416 usleep_range(1000, 2000);
417 writel(val | mask, np_base + REG_PCI_CONTROL);
423 static void en7523_pci_unprepare(struct clk_hw *hw)
425 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
426 void __iomem *np_base = cg->base;
429 val = readl(np_base + REG_PCI_CONTROL);
430 val &= ~REG_PCI_CONTROL_REFCLK_EN1;
431 writel(val, np_base + REG_PCI_CONTROL);
434 static struct clk_hw *en7523_register_pcie_clk(struct device *dev,
435 void __iomem *np_base)
437 const struct en_clk_soc_data *soc_data = device_get_match_data(dev);
438 struct clk_init_data init = {
440 .ops = &soc_data->pcie_ops,
442 struct en_clk_gate *cg;
444 cg = devm_kzalloc(dev, sizeof(*cg), GFP_KERNEL);
451 if (init.ops->unprepare)
452 init.ops->unprepare(&cg->hw);
454 if (clk_hw_register(dev, &cg->hw))
460 static int en7581_pci_is_enabled(struct clk_hw *hw)
462 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
465 mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1;
466 val = readl(cg->base + REG_PCI_CONTROL);
467 return (val & mask) == mask;
470 static int en7581_pci_enable(struct clk_hw *hw)
472 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
473 void __iomem *np_base = cg->base;
476 mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1 |
477 REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT2 |
478 REG_PCI_CONTROL_PERSTOUT;
479 val = readl(np_base + REG_PCI_CONTROL);
480 writel(val | mask, np_base + REG_PCI_CONTROL);
486 static void en7581_pci_disable(struct clk_hw *hw)
488 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
489 void __iomem *np_base = cg->base;
492 mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1 |
493 REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT2 |
494 REG_PCI_CONTROL_PERSTOUT;
495 val = readl(np_base + REG_PCI_CONTROL);
496 writel(val & ~mask, np_base + REG_PCI_CONTROL);
497 usleep_range(1000, 2000);
500 static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_data *clk_data,
501 void __iomem *base, void __iomem *np_base)
507 clk_data->num = EN7523_NUM_CLOCKS;
509 for (i = 0; i < ARRAY_SIZE(en7523_base_clks); i++) {
510 const struct en_clk_desc *desc = &en7523_base_clks[i];
511 u32 reg = desc->div_reg ? desc->div_reg : desc->base_reg;
512 u32 val = readl(base + desc->base_reg);
514 rate = en7523_get_base_rate(desc, val);
515 val = readl(base + reg);
516 rate /= en7523_get_div(desc, val);
518 hw = clk_hw_register_fixed_rate(dev, desc->name, NULL, 0, rate);
520 pr_err("Failed to register clk %s: %ld\n",
521 desc->name, PTR_ERR(hw));
525 clk_data->hws[desc->id] = hw;
528 hw = en7523_register_pcie_clk(dev, np_base);
529 clk_data->hws[EN7523_CLK_PCIE] = hw;
532 static int en7523_clk_hw_init(struct platform_device *pdev,
533 struct clk_hw_onecell_data *clk_data)
535 void __iomem *base, *np_base;
537 base = devm_platform_ioremap_resource(pdev, 0);
539 return PTR_ERR(base);
541 np_base = devm_platform_ioremap_resource(pdev, 1);
543 return PTR_ERR(np_base);
545 en7523_register_clocks(&pdev->dev, clk_data, base, np_base);
550 static void en7581_register_clocks(struct device *dev, struct clk_hw_onecell_data *clk_data,
551 struct regmap *map, void __iomem *base)
557 for (i = 0; i < ARRAY_SIZE(en7581_base_clks); i++) {
558 const struct en_clk_desc *desc = &en7581_base_clks[i];
559 u32 val, reg = desc->div_reg ? desc->div_reg : desc->base_reg;
562 err = regmap_read(map, desc->base_reg, &val);
564 pr_err("Failed reading fixed clk rate %s: %d\n",
568 rate = en7523_get_base_rate(desc, val);
570 err = regmap_read(map, reg, &val);
572 pr_err("Failed reading fixed clk div %s: %d\n",
576 rate /= en7523_get_div(desc, val);
578 hw = clk_hw_register_fixed_rate(dev, desc->name, NULL, 0, rate);
580 pr_err("Failed to register clk %s: %ld\n",
581 desc->name, PTR_ERR(hw));
585 clk_data->hws[desc->id] = hw;
588 hw = en7523_register_pcie_clk(dev, base);
589 clk_data->hws[EN7523_CLK_PCIE] = hw;
591 clk_data->num = EN7523_NUM_CLOCKS;
594 static int en7523_reset_update(struct reset_controller_dev *rcdev,
595 unsigned long id, bool assert)
597 struct en_rst_data *rst_data = container_of(rcdev, struct en_rst_data, rcdev);
598 void __iomem *addr = rst_data->base + rst_data->bank_ofs[id / RST_NR_PER_BANK];
603 val |= BIT(id % RST_NR_PER_BANK);
605 val &= ~BIT(id % RST_NR_PER_BANK);
611 static int en7523_reset_assert(struct reset_controller_dev *rcdev,
614 return en7523_reset_update(rcdev, id, true);
617 static int en7523_reset_deassert(struct reset_controller_dev *rcdev,
620 return en7523_reset_update(rcdev, id, false);
623 static int en7523_reset_status(struct reset_controller_dev *rcdev,
626 struct en_rst_data *rst_data = container_of(rcdev, struct en_rst_data, rcdev);
627 void __iomem *addr = rst_data->base + rst_data->bank_ofs[id / RST_NR_PER_BANK];
629 return !!(readl(addr) & BIT(id % RST_NR_PER_BANK));
632 static int en7523_reset_xlate(struct reset_controller_dev *rcdev,
633 const struct of_phandle_args *reset_spec)
635 struct en_rst_data *rst_data = container_of(rcdev, struct en_rst_data, rcdev);
637 if (reset_spec->args[0] >= rcdev->nr_resets)
640 return rst_data->idx_map[reset_spec->args[0]];
643 static const struct reset_control_ops en7581_reset_ops = {
644 .assert = en7523_reset_assert,
645 .deassert = en7523_reset_deassert,
646 .status = en7523_reset_status,
649 static int en7581_reset_register(struct device *dev, void __iomem *base)
651 struct en_rst_data *rst_data;
653 rst_data = devm_kzalloc(dev, sizeof(*rst_data), GFP_KERNEL);
657 rst_data->bank_ofs = en7581_rst_ofs;
658 rst_data->idx_map = en7581_rst_map;
659 rst_data->base = base;
661 rst_data->rcdev.nr_resets = ARRAY_SIZE(en7581_rst_map);
662 rst_data->rcdev.of_xlate = en7523_reset_xlate;
663 rst_data->rcdev.ops = &en7581_reset_ops;
664 rst_data->rcdev.of_node = dev->of_node;
665 rst_data->rcdev.of_reset_n_cells = 1;
666 rst_data->rcdev.owner = THIS_MODULE;
667 rst_data->rcdev.dev = dev;
669 return devm_reset_controller_register(dev, &rst_data->rcdev);
672 static int en7581_clk_hw_init(struct platform_device *pdev,
673 struct clk_hw_onecell_data *clk_data)
679 map = syscon_regmap_lookup_by_compatible("airoha,en7581-chip-scu");
683 base = devm_platform_ioremap_resource(pdev, 0);
685 return PTR_ERR(base);
687 en7581_register_clocks(&pdev->dev, clk_data, map, base);
689 val = readl(base + REG_NP_SCU_SSTR);
690 val &= ~(REG_PCIE_XSI0_SEL_MASK | REG_PCIE_XSI1_SEL_MASK);
691 writel(val, base + REG_NP_SCU_SSTR);
692 val = readl(base + REG_NP_SCU_PCIC);
693 writel(val | 3, base + REG_NP_SCU_PCIC);
695 return en7581_reset_register(&pdev->dev, base);
698 static int en7523_clk_probe(struct platform_device *pdev)
700 struct device_node *node = pdev->dev.of_node;
701 const struct en_clk_soc_data *soc_data;
702 struct clk_hw_onecell_data *clk_data;
705 clk_data = devm_kzalloc(&pdev->dev,
706 struct_size(clk_data, hws, EN7523_NUM_CLOCKS),
711 soc_data = device_get_match_data(&pdev->dev);
712 r = soc_data->hw_init(pdev, clk_data);
716 return of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
719 static const struct en_clk_soc_data en7523_data = {
721 .is_enabled = en7523_pci_is_enabled,
722 .prepare = en7523_pci_prepare,
723 .unprepare = en7523_pci_unprepare,
725 .hw_init = en7523_clk_hw_init,
728 static const struct en_clk_soc_data en7581_data = {
730 .is_enabled = en7581_pci_is_enabled,
731 .enable = en7581_pci_enable,
732 .disable = en7581_pci_disable,
734 .hw_init = en7581_clk_hw_init,
737 static const struct of_device_id of_match_clk_en7523[] = {
738 { .compatible = "airoha,en7523-scu", .data = &en7523_data },
739 { .compatible = "airoha,en7581-scu", .data = &en7581_data },
743 static struct platform_driver clk_en7523_drv = {
744 .probe = en7523_clk_probe,
746 .name = "clk-en7523",
747 .of_match_table = of_match_clk_en7523,
748 .suppress_bind_attrs = true,
752 static int __init clk_en7523_init(void)
754 return platform_driver_register(&clk_en7523_drv);
757 arch_initcall(clk_en7523_init);