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 <linux/reset-controller.h>
9 #include <dt-bindings/clock/en7523-clk.h>
10 #include <dt-bindings/reset/airoha,en7581-reset.h>
12 #define RST_NR_PER_BANK 32
14 #define REG_PCI_CONTROL 0x88
15 #define REG_PCI_CONTROL_PERSTOUT BIT(29)
16 #define REG_PCI_CONTROL_PERSTOUT1 BIT(26)
17 #define REG_PCI_CONTROL_REFCLK_EN0 BIT(23)
18 #define REG_PCI_CONTROL_REFCLK_EN1 BIT(22)
19 #define REG_PCI_CONTROL_PERSTOUT2 BIT(16)
20 #define REG_GSW_CLK_DIV_SEL 0x1b4
21 #define REG_EMI_CLK_DIV_SEL 0x1b8
22 #define REG_BUS_CLK_DIV_SEL 0x1bc
23 #define REG_SPI_CLK_DIV_SEL 0x1c4
24 #define REG_SPI_CLK_FREQ_SEL 0x1c8
25 #define REG_NPU_CLK_DIV_SEL 0x1fc
26 #define REG_CRYPTO_CLKSRC 0x200
27 #define REG_RESET_CONTROL2 0x830
28 #define REG_RESET2_CONTROL_PCIE2 BIT(27)
29 #define REG_RESET_CONTROL1 0x834
30 #define REG_RESET_CONTROL_PCIEHB BIT(29)
31 #define REG_RESET_CONTROL_PCIE1 BIT(27)
32 #define REG_RESET_CONTROL_PCIE2 BIT(26)
34 #define REG_PCIE0_MEM 0x00
35 #define REG_PCIE0_MEM_MASK 0x04
36 #define REG_PCIE1_MEM 0x08
37 #define REG_PCIE1_MEM_MASK 0x0c
38 #define REG_PCIE2_MEM 0x10
39 #define REG_PCIE2_MEM_MASK 0x14
40 #define REG_NP_SCU_PCIC 0x88
41 #define REG_NP_SCU_SSTR 0x9c
42 #define REG_PCIE_XSI0_SEL_MASK GENMASK(14, 13)
43 #define REG_PCIE_XSI1_SEL_MASK GENMASK(12, 11)
45 #define REG_RST_CTRL2 0x00
46 #define REG_RST_CTRL1 0x04
55 const unsigned int *base_values;
56 unsigned int base_value;
77 struct reset_controller_dev rcdev;
80 struct en_clk_soc_data {
81 const struct clk_ops pcie_ops;
87 int (*hw_init)(struct platform_device *pdev, void __iomem *np_base);
90 static const u32 gsw_base[] = { 400000000, 500000000 };
91 static const u32 emi_base[] = { 333000000, 400000000 };
92 static const u32 bus_base[] = { 500000000, 540000000 };
93 static const u32 slic_base[] = { 100000000, 3125000 };
94 static const u32 npu_base[] = { 333000000, 400000000, 500000000 };
96 static const struct en_clk_desc en7523_base_clks[] = {
101 .base_reg = REG_GSW_CLK_DIV_SEL,
104 .base_values = gsw_base,
105 .n_base_values = ARRAY_SIZE(gsw_base),
112 .id = EN7523_CLK_EMI,
115 .base_reg = REG_EMI_CLK_DIV_SEL,
118 .base_values = emi_base,
119 .n_base_values = ARRAY_SIZE(emi_base),
126 .id = EN7523_CLK_BUS,
129 .base_reg = REG_BUS_CLK_DIV_SEL,
132 .base_values = bus_base,
133 .n_base_values = ARRAY_SIZE(bus_base),
140 .id = EN7523_CLK_SLIC,
143 .base_reg = REG_SPI_CLK_FREQ_SEL,
146 .base_values = slic_base,
147 .n_base_values = ARRAY_SIZE(slic_base),
149 .div_reg = REG_SPI_CLK_DIV_SEL,
155 .id = EN7523_CLK_SPI,
158 .base_reg = REG_SPI_CLK_DIV_SEL,
160 .base_value = 400000000,
167 .id = EN7523_CLK_NPU,
170 .base_reg = REG_NPU_CLK_DIV_SEL,
173 .base_values = npu_base,
174 .n_base_values = ARRAY_SIZE(npu_base),
181 .id = EN7523_CLK_CRYPTO,
184 .base_reg = REG_CRYPTO_CLKSRC,
187 .base_values = emi_base,
188 .n_base_values = ARRAY_SIZE(emi_base),
192 static const u16 en7581_rst_ofs[] = {
197 static const u16 en7581_rst_map[] = {
199 [EN7581_XPON_PHY_RST] = 0,
200 [EN7581_CPU_TIMER2_RST] = 2,
201 [EN7581_HSUART_RST] = 3,
202 [EN7581_UART4_RST] = 4,
203 [EN7581_UART5_RST] = 5,
204 [EN7581_I2C2_RST] = 6,
205 [EN7581_XSI_MAC_RST] = 7,
206 [EN7581_XSI_PHY_RST] = 8,
207 [EN7581_NPU_RST] = 9,
208 [EN7581_I2S_RST] = 10,
209 [EN7581_TRNG_RST] = 11,
210 [EN7581_TRNG_MSTART_RST] = 12,
211 [EN7581_DUAL_HSI0_RST] = 13,
212 [EN7581_DUAL_HSI1_RST] = 14,
213 [EN7581_HSI_RST] = 15,
214 [EN7581_DUAL_HSI0_MAC_RST] = 16,
215 [EN7581_DUAL_HSI1_MAC_RST] = 17,
216 [EN7581_HSI_MAC_RST] = 18,
217 [EN7581_WDMA_RST] = 19,
218 [EN7581_WOE0_RST] = 20,
219 [EN7581_WOE1_RST] = 21,
220 [EN7581_HSDMA_RST] = 22,
221 [EN7581_TDMA_RST] = 24,
222 [EN7581_EMMC_RST] = 25,
223 [EN7581_SOE_RST] = 26,
224 [EN7581_PCIE2_RST] = 27,
225 [EN7581_XFP_MAC_RST] = 28,
226 [EN7581_USB_HOST_P1_RST] = 29,
227 [EN7581_USB_HOST_P1_U3_PHY_RST] = 30,
229 [EN7581_PCM1_ZSI_ISI_RST] = RST_NR_PER_BANK + 0,
230 [EN7581_FE_PDMA_RST] = RST_NR_PER_BANK + 1,
231 [EN7581_FE_QDMA_RST] = RST_NR_PER_BANK + 2,
232 [EN7581_PCM_SPIWP_RST] = RST_NR_PER_BANK + 4,
233 [EN7581_CRYPTO_RST] = RST_NR_PER_BANK + 6,
234 [EN7581_TIMER_RST] = RST_NR_PER_BANK + 8,
235 [EN7581_PCM1_RST] = RST_NR_PER_BANK + 11,
236 [EN7581_UART_RST] = RST_NR_PER_BANK + 12,
237 [EN7581_GPIO_RST] = RST_NR_PER_BANK + 13,
238 [EN7581_GDMA_RST] = RST_NR_PER_BANK + 14,
239 [EN7581_I2C_MASTER_RST] = RST_NR_PER_BANK + 16,
240 [EN7581_PCM2_ZSI_ISI_RST] = RST_NR_PER_BANK + 17,
241 [EN7581_SFC_RST] = RST_NR_PER_BANK + 18,
242 [EN7581_UART2_RST] = RST_NR_PER_BANK + 19,
243 [EN7581_GDMP_RST] = RST_NR_PER_BANK + 20,
244 [EN7581_FE_RST] = RST_NR_PER_BANK + 21,
245 [EN7581_USB_HOST_P0_RST] = RST_NR_PER_BANK + 22,
246 [EN7581_GSW_RST] = RST_NR_PER_BANK + 23,
247 [EN7581_SFC2_PCM_RST] = RST_NR_PER_BANK + 25,
248 [EN7581_PCIE0_RST] = RST_NR_PER_BANK + 26,
249 [EN7581_PCIE1_RST] = RST_NR_PER_BANK + 27,
250 [EN7581_CPU_TIMER_RST] = RST_NR_PER_BANK + 28,
251 [EN7581_PCIE_HB_RST] = RST_NR_PER_BANK + 29,
252 [EN7581_XPON_MAC_RST] = RST_NR_PER_BANK + 31,
255 static unsigned int en7523_get_base_rate(void __iomem *base, unsigned int i)
257 const struct en_clk_desc *desc = &en7523_base_clks[i];
260 if (!desc->base_bits)
261 return desc->base_value;
263 val = readl(base + desc->base_reg);
264 val >>= desc->base_shift;
265 val &= (1 << desc->base_bits) - 1;
267 if (val >= desc->n_base_values)
270 return desc->base_values[val];
273 static u32 en7523_get_div(void __iomem *base, int i)
275 const struct en_clk_desc *desc = &en7523_base_clks[i];
281 reg = desc->div_reg ? desc->div_reg : desc->base_reg;
282 val = readl(base + reg);
283 val >>= desc->div_shift;
284 val &= (1 << desc->div_bits) - 1;
286 if (!val && desc->div_val0)
287 return desc->div_val0;
289 return (val + desc->div_offset) * desc->div_step;
292 static int en7523_pci_is_enabled(struct clk_hw *hw)
294 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
296 return !!(readl(cg->base + REG_PCI_CONTROL) & REG_PCI_CONTROL_REFCLK_EN1);
299 static int en7523_pci_prepare(struct clk_hw *hw)
301 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
302 void __iomem *np_base = cg->base;
305 /* Need to pull device low before reset */
306 val = readl(np_base + REG_PCI_CONTROL);
307 val &= ~(REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT);
308 writel(val, np_base + REG_PCI_CONTROL);
309 usleep_range(1000, 2000);
311 /* Enable PCIe port 1 */
312 val |= REG_PCI_CONTROL_REFCLK_EN1;
313 writel(val, np_base + REG_PCI_CONTROL);
314 usleep_range(1000, 2000);
316 /* Reset to default */
317 val = readl(np_base + REG_RESET_CONTROL1);
318 mask = REG_RESET_CONTROL_PCIE1 | REG_RESET_CONTROL_PCIE2 |
319 REG_RESET_CONTROL_PCIEHB;
320 writel(val & ~mask, np_base + REG_RESET_CONTROL1);
321 usleep_range(1000, 2000);
322 writel(val | mask, np_base + REG_RESET_CONTROL1);
324 writel(val & ~mask, np_base + REG_RESET_CONTROL1);
325 usleep_range(5000, 10000);
328 mask = REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT;
329 val = readl(np_base + REG_PCI_CONTROL);
330 writel(val & ~mask, np_base + REG_PCI_CONTROL);
331 usleep_range(1000, 2000);
332 writel(val | mask, np_base + REG_PCI_CONTROL);
338 static void en7523_pci_unprepare(struct clk_hw *hw)
340 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
341 void __iomem *np_base = cg->base;
344 val = readl(np_base + REG_PCI_CONTROL);
345 val &= ~REG_PCI_CONTROL_REFCLK_EN1;
346 writel(val, np_base + REG_PCI_CONTROL);
349 static struct clk_hw *en7523_register_pcie_clk(struct device *dev,
350 void __iomem *np_base)
352 const struct en_clk_soc_data *soc_data = device_get_match_data(dev);
353 struct clk_init_data init = {
355 .ops = &soc_data->pcie_ops,
357 struct en_clk_gate *cg;
359 cg = devm_kzalloc(dev, sizeof(*cg), GFP_KERNEL);
366 if (init.ops->unprepare)
367 init.ops->unprepare(&cg->hw);
369 if (clk_hw_register(dev, &cg->hw))
375 static int en7581_pci_is_enabled(struct clk_hw *hw)
377 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
380 mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1;
381 val = readl(cg->base + REG_PCI_CONTROL);
382 return (val & mask) == mask;
385 static int en7581_pci_enable(struct clk_hw *hw)
387 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
388 void __iomem *np_base = cg->base;
391 mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1 |
392 REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT2 |
393 REG_PCI_CONTROL_PERSTOUT;
394 val = readl(np_base + REG_PCI_CONTROL);
395 writel(val | mask, np_base + REG_PCI_CONTROL);
401 static void en7581_pci_disable(struct clk_hw *hw)
403 struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
404 void __iomem *np_base = cg->base;
407 mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1 |
408 REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT2 |
409 REG_PCI_CONTROL_PERSTOUT;
410 val = readl(np_base + REG_PCI_CONTROL);
411 writel(val & ~mask, np_base + REG_PCI_CONTROL);
412 usleep_range(1000, 2000);
415 static int en7581_clk_hw_init(struct platform_device *pdev,
416 void __iomem *np_base)
418 void __iomem *pb_base;
421 pb_base = devm_platform_ioremap_resource(pdev, 3);
423 return PTR_ERR(pb_base);
425 val = readl(np_base + REG_NP_SCU_SSTR);
426 val &= ~(REG_PCIE_XSI0_SEL_MASK | REG_PCIE_XSI1_SEL_MASK);
427 writel(val, np_base + REG_NP_SCU_SSTR);
428 val = readl(np_base + REG_NP_SCU_PCIC);
429 writel(val | 3, np_base + REG_NP_SCU_PCIC);
431 writel(0x20000000, pb_base + REG_PCIE0_MEM);
432 writel(0xfc000000, pb_base + REG_PCIE0_MEM_MASK);
433 writel(0x24000000, pb_base + REG_PCIE1_MEM);
434 writel(0xfc000000, pb_base + REG_PCIE1_MEM_MASK);
435 writel(0x28000000, pb_base + REG_PCIE2_MEM);
436 writel(0xfc000000, pb_base + REG_PCIE2_MEM_MASK);
441 static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_data *clk_data,
442 void __iomem *base, void __iomem *np_base)
448 for (i = 0; i < ARRAY_SIZE(en7523_base_clks); i++) {
449 const struct en_clk_desc *desc = &en7523_base_clks[i];
451 rate = en7523_get_base_rate(base, i);
452 rate /= en7523_get_div(base, i);
454 hw = clk_hw_register_fixed_rate(dev, desc->name, NULL, 0, rate);
456 pr_err("Failed to register clk %s: %ld\n",
457 desc->name, PTR_ERR(hw));
461 clk_data->hws[desc->id] = hw;
464 hw = en7523_register_pcie_clk(dev, np_base);
465 clk_data->hws[EN7523_CLK_PCIE] = hw;
467 clk_data->num = EN7523_NUM_CLOCKS;
470 static int en7523_reset_update(struct reset_controller_dev *rcdev,
471 unsigned long id, bool assert)
473 struct en_rst_data *rst_data = container_of(rcdev, struct en_rst_data, rcdev);
474 void __iomem *addr = rst_data->base + rst_data->bank_ofs[id / RST_NR_PER_BANK];
479 val |= BIT(id % RST_NR_PER_BANK);
481 val &= ~BIT(id % RST_NR_PER_BANK);
487 static int en7523_reset_assert(struct reset_controller_dev *rcdev,
490 return en7523_reset_update(rcdev, id, true);
493 static int en7523_reset_deassert(struct reset_controller_dev *rcdev,
496 return en7523_reset_update(rcdev, id, false);
499 static int en7523_reset_status(struct reset_controller_dev *rcdev,
502 struct en_rst_data *rst_data = container_of(rcdev, struct en_rst_data, rcdev);
503 void __iomem *addr = rst_data->base + rst_data->bank_ofs[id / RST_NR_PER_BANK];
505 return !!(readl(addr) & BIT(id % RST_NR_PER_BANK));
508 static int en7523_reset_xlate(struct reset_controller_dev *rcdev,
509 const struct of_phandle_args *reset_spec)
511 struct en_rst_data *rst_data = container_of(rcdev, struct en_rst_data, rcdev);
513 if (reset_spec->args[0] >= rcdev->nr_resets)
516 return rst_data->idx_map[reset_spec->args[0]];
519 static const struct reset_control_ops en7523_reset_ops = {
520 .assert = en7523_reset_assert,
521 .deassert = en7523_reset_deassert,
522 .status = en7523_reset_status,
525 static int en7523_reset_register(struct platform_device *pdev,
526 const struct en_clk_soc_data *soc_data)
528 struct device *dev = &pdev->dev;
529 struct en_rst_data *rst_data;
532 /* no reset lines available */
533 if (!soc_data->reset.idx_map_nr)
536 base = devm_platform_ioremap_resource(pdev, 2);
538 return PTR_ERR(base);
540 rst_data = devm_kzalloc(dev, sizeof(*rst_data), GFP_KERNEL);
544 rst_data->bank_ofs = soc_data->reset.bank_ofs;
545 rst_data->idx_map = soc_data->reset.idx_map;
546 rst_data->base = base;
548 rst_data->rcdev.nr_resets = soc_data->reset.idx_map_nr;
549 rst_data->rcdev.of_xlate = en7523_reset_xlate;
550 rst_data->rcdev.ops = &en7523_reset_ops;
551 rst_data->rcdev.of_node = dev->of_node;
552 rst_data->rcdev.of_reset_n_cells = 1;
553 rst_data->rcdev.owner = THIS_MODULE;
554 rst_data->rcdev.dev = dev;
556 return devm_reset_controller_register(dev, &rst_data->rcdev);
559 static int en7523_clk_probe(struct platform_device *pdev)
561 struct device_node *node = pdev->dev.of_node;
562 const struct en_clk_soc_data *soc_data;
563 struct clk_hw_onecell_data *clk_data;
564 void __iomem *base, *np_base;
567 base = devm_platform_ioremap_resource(pdev, 0);
569 return PTR_ERR(base);
571 np_base = devm_platform_ioremap_resource(pdev, 1);
573 return PTR_ERR(np_base);
575 soc_data = device_get_match_data(&pdev->dev);
576 if (soc_data->hw_init) {
577 r = soc_data->hw_init(pdev, np_base);
582 clk_data = devm_kzalloc(&pdev->dev,
583 struct_size(clk_data, hws, EN7523_NUM_CLOCKS),
588 en7523_register_clocks(&pdev->dev, clk_data, base, np_base);
590 r = of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
592 return dev_err_probe(&pdev->dev, r, "Could not register clock provider: %s\n",
595 r = en7523_reset_register(pdev, soc_data);
597 of_clk_del_provider(node);
598 return dev_err_probe(&pdev->dev, r, "Could not register reset controller: %s\n",
605 static const struct en_clk_soc_data en7523_data = {
607 .is_enabled = en7523_pci_is_enabled,
608 .prepare = en7523_pci_prepare,
609 .unprepare = en7523_pci_unprepare,
613 static const struct en_clk_soc_data en7581_data = {
615 .is_enabled = en7581_pci_is_enabled,
616 .enable = en7581_pci_enable,
617 .disable = en7581_pci_disable,
620 .bank_ofs = en7581_rst_ofs,
621 .idx_map = en7581_rst_map,
622 .idx_map_nr = ARRAY_SIZE(en7581_rst_map),
624 .hw_init = en7581_clk_hw_init,
627 static const struct of_device_id of_match_clk_en7523[] = {
628 { .compatible = "airoha,en7523-scu", .data = &en7523_data },
629 { .compatible = "airoha,en7581-scu", .data = &en7581_data },
633 static struct platform_driver clk_en7523_drv = {
634 .probe = en7523_clk_probe,
636 .name = "clk-en7523",
637 .of_match_table = of_match_clk_en7523,
638 .suppress_bind_attrs = true,
642 static int __init clk_en7523_init(void)
644 return platform_driver_register(&clk_en7523_drv);
647 arch_initcall(clk_en7523_init);