xref: /linux/drivers/clk/starfive/clk-starfive-jh7110-aon.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1b2ab3c94SEmil Renner Berthing // SPDX-License-Identifier: GPL-2.0
2b2ab3c94SEmil Renner Berthing /*
3b2ab3c94SEmil Renner Berthing  * StarFive JH7110 Always-On Clock Driver
4b2ab3c94SEmil Renner Berthing  *
5b2ab3c94SEmil Renner Berthing  * Copyright (C) 2022 Emil Renner Berthing <kernel@esmil.dk>
6b2ab3c94SEmil Renner Berthing  * Copyright (C) 2022 StarFive Technology Co., Ltd.
7b2ab3c94SEmil Renner Berthing  */
8b2ab3c94SEmil Renner Berthing 
9b2ab3c94SEmil Renner Berthing #include <linux/clk-provider.h>
10b2ab3c94SEmil Renner Berthing #include <linux/io.h>
11b2ab3c94SEmil Renner Berthing #include <linux/platform_device.h>
12b2ab3c94SEmil Renner Berthing 
13b2ab3c94SEmil Renner Berthing #include <dt-bindings/clock/starfive,jh7110-crg.h>
14b2ab3c94SEmil Renner Berthing 
15b2ab3c94SEmil Renner Berthing #include "clk-starfive-jh7110.h"
16b2ab3c94SEmil Renner Berthing 
17b2ab3c94SEmil Renner Berthing /* external clocks */
18b2ab3c94SEmil Renner Berthing #define JH7110_AONCLK_OSC		(JH7110_AONCLK_END + 0)
19b2ab3c94SEmil Renner Berthing #define JH7110_AONCLK_GMAC0_RMII_REFIN	(JH7110_AONCLK_END + 1)
20b2ab3c94SEmil Renner Berthing #define JH7110_AONCLK_GMAC0_RGMII_RXIN	(JH7110_AONCLK_END + 2)
21b2ab3c94SEmil Renner Berthing #define JH7110_AONCLK_STG_AXIAHB	(JH7110_AONCLK_END + 3)
22b2ab3c94SEmil Renner Berthing #define JH7110_AONCLK_APB_BUS		(JH7110_AONCLK_END + 4)
23b2ab3c94SEmil Renner Berthing #define JH7110_AONCLK_GMAC0_GTXCLK	(JH7110_AONCLK_END + 5)
24b2ab3c94SEmil Renner Berthing #define JH7110_AONCLK_RTC_OSC		(JH7110_AONCLK_END + 6)
25b2ab3c94SEmil Renner Berthing 
26b2ab3c94SEmil Renner Berthing static const struct jh71x0_clk_data jh7110_aonclk_data[] = {
27b2ab3c94SEmil Renner Berthing 	/* source */
28b2ab3c94SEmil Renner Berthing 	JH71X0__DIV(JH7110_AONCLK_OSC_DIV4, "osc_div4", 4, JH7110_AONCLK_OSC),
29*a242b205SEmil Renner Berthing 	JH71X0__MUX(JH7110_AONCLK_APB_FUNC, "apb_func", 0, 2,
30b2ab3c94SEmil Renner Berthing 		    JH7110_AONCLK_OSC_DIV4,
31b2ab3c94SEmil Renner Berthing 		    JH7110_AONCLK_OSC),
32b2ab3c94SEmil Renner Berthing 	/* gmac0 */
33b2ab3c94SEmil Renner Berthing 	JH71X0_GATE(JH7110_AONCLK_GMAC0_AHB, "gmac0_ahb", 0, JH7110_AONCLK_STG_AXIAHB),
34b2ab3c94SEmil Renner Berthing 	JH71X0_GATE(JH7110_AONCLK_GMAC0_AXI, "gmac0_axi", 0, JH7110_AONCLK_STG_AXIAHB),
35b2ab3c94SEmil Renner Berthing 	JH71X0__DIV(JH7110_AONCLK_GMAC0_RMII_RTX, "gmac0_rmii_rtx", 30,
36b2ab3c94SEmil Renner Berthing 		    JH7110_AONCLK_GMAC0_RMII_REFIN),
37b2ab3c94SEmil Renner Berthing 	JH71X0_GMUX(JH7110_AONCLK_GMAC0_TX, "gmac0_tx",
38b2ab3c94SEmil Renner Berthing 		    CLK_SET_RATE_PARENT | CLK_SET_RATE_NO_REPARENT, 2,
39b2ab3c94SEmil Renner Berthing 		    JH7110_AONCLK_GMAC0_GTXCLK,
40b2ab3c94SEmil Renner Berthing 		    JH7110_AONCLK_GMAC0_RMII_RTX),
41b2ab3c94SEmil Renner Berthing 	JH71X0__INV(JH7110_AONCLK_GMAC0_TX_INV, "gmac0_tx_inv", JH7110_AONCLK_GMAC0_TX),
42*a242b205SEmil Renner Berthing 	JH71X0__MUX(JH7110_AONCLK_GMAC0_RX, "gmac0_rx", 0, 2,
43b2ab3c94SEmil Renner Berthing 		    JH7110_AONCLK_GMAC0_RGMII_RXIN,
44b2ab3c94SEmil Renner Berthing 		    JH7110_AONCLK_GMAC0_RMII_RTX),
45b2ab3c94SEmil Renner Berthing 	JH71X0__INV(JH7110_AONCLK_GMAC0_RX_INV, "gmac0_rx_inv", JH7110_AONCLK_GMAC0_RX),
46b2ab3c94SEmil Renner Berthing 	/* otpc */
47b2ab3c94SEmil Renner Berthing 	JH71X0_GATE(JH7110_AONCLK_OTPC_APB, "otpc_apb", 0, JH7110_AONCLK_APB_BUS),
48b2ab3c94SEmil Renner Berthing 	/* rtc */
49b2ab3c94SEmil Renner Berthing 	JH71X0_GATE(JH7110_AONCLK_RTC_APB, "rtc_apb", 0, JH7110_AONCLK_APB_BUS),
50b2ab3c94SEmil Renner Berthing 	JH71X0__DIV(JH7110_AONCLK_RTC_INTERNAL, "rtc_internal", 1022, JH7110_AONCLK_OSC),
51*a242b205SEmil Renner Berthing 	JH71X0__MUX(JH7110_AONCLK_RTC_32K, "rtc_32k", 0, 2,
52b2ab3c94SEmil Renner Berthing 		    JH7110_AONCLK_RTC_OSC,
53b2ab3c94SEmil Renner Berthing 		    JH7110_AONCLK_RTC_INTERNAL),
54b2ab3c94SEmil Renner Berthing 	JH71X0_GATE(JH7110_AONCLK_RTC_CAL, "rtc_cal", 0, JH7110_AONCLK_OSC),
55b2ab3c94SEmil Renner Berthing };
56b2ab3c94SEmil Renner Berthing 
jh7110_aonclk_get(struct of_phandle_args * clkspec,void * data)57b2ab3c94SEmil Renner Berthing static struct clk_hw *jh7110_aonclk_get(struct of_phandle_args *clkspec, void *data)
58b2ab3c94SEmil Renner Berthing {
59b2ab3c94SEmil Renner Berthing 	struct jh71x0_clk_priv *priv = data;
60b2ab3c94SEmil Renner Berthing 	unsigned int idx = clkspec->args[0];
61b2ab3c94SEmil Renner Berthing 
62b2ab3c94SEmil Renner Berthing 	if (idx < JH7110_AONCLK_END)
63b2ab3c94SEmil Renner Berthing 		return &priv->reg[idx].hw;
64b2ab3c94SEmil Renner Berthing 
65b2ab3c94SEmil Renner Berthing 	return ERR_PTR(-EINVAL);
66b2ab3c94SEmil Renner Berthing }
67b2ab3c94SEmil Renner Berthing 
jh7110_aoncrg_probe(struct platform_device * pdev)68b2ab3c94SEmil Renner Berthing static int jh7110_aoncrg_probe(struct platform_device *pdev)
69b2ab3c94SEmil Renner Berthing {
70b2ab3c94SEmil Renner Berthing 	struct jh71x0_clk_priv *priv;
71b2ab3c94SEmil Renner Berthing 	unsigned int idx;
72b2ab3c94SEmil Renner Berthing 	int ret;
73b2ab3c94SEmil Renner Berthing 
74b2ab3c94SEmil Renner Berthing 	priv = devm_kzalloc(&pdev->dev,
75b2ab3c94SEmil Renner Berthing 			    struct_size(priv, reg, JH7110_AONCLK_END),
76b2ab3c94SEmil Renner Berthing 			    GFP_KERNEL);
77b2ab3c94SEmil Renner Berthing 	if (!priv)
78b2ab3c94SEmil Renner Berthing 		return -ENOMEM;
79b2ab3c94SEmil Renner Berthing 
80b2ab3c94SEmil Renner Berthing 	spin_lock_init(&priv->rmw_lock);
81b2ab3c94SEmil Renner Berthing 	priv->dev = &pdev->dev;
82b2ab3c94SEmil Renner Berthing 	priv->base = devm_platform_ioremap_resource(pdev, 0);
83b2ab3c94SEmil Renner Berthing 	if (IS_ERR(priv->base))
84b2ab3c94SEmil Renner Berthing 		return PTR_ERR(priv->base);
85b2ab3c94SEmil Renner Berthing 
86b2ab3c94SEmil Renner Berthing 	for (idx = 0; idx < JH7110_AONCLK_END; idx++) {
87b2ab3c94SEmil Renner Berthing 		u32 max = jh7110_aonclk_data[idx].max;
88b2ab3c94SEmil Renner Berthing 		struct clk_parent_data parents[4] = {};
89b2ab3c94SEmil Renner Berthing 		struct clk_init_data init = {
90b2ab3c94SEmil Renner Berthing 			.name = jh7110_aonclk_data[idx].name,
91b2ab3c94SEmil Renner Berthing 			.ops = starfive_jh71x0_clk_ops(max),
92b2ab3c94SEmil Renner Berthing 			.parent_data = parents,
93b2ab3c94SEmil Renner Berthing 			.num_parents =
94b2ab3c94SEmil Renner Berthing 				((max & JH71X0_CLK_MUX_MASK) >> JH71X0_CLK_MUX_SHIFT) + 1,
95b2ab3c94SEmil Renner Berthing 			.flags = jh7110_aonclk_data[idx].flags,
96b2ab3c94SEmil Renner Berthing 		};
97b2ab3c94SEmil Renner Berthing 		struct jh71x0_clk *clk = &priv->reg[idx];
98b2ab3c94SEmil Renner Berthing 		unsigned int i;
99b2ab3c94SEmil Renner Berthing 
100b2ab3c94SEmil Renner Berthing 		for (i = 0; i < init.num_parents; i++) {
101b2ab3c94SEmil Renner Berthing 			unsigned int pidx = jh7110_aonclk_data[idx].parents[i];
102b2ab3c94SEmil Renner Berthing 
103b2ab3c94SEmil Renner Berthing 			if (pidx < JH7110_AONCLK_END)
104b2ab3c94SEmil Renner Berthing 				parents[i].hw = &priv->reg[pidx].hw;
105b2ab3c94SEmil Renner Berthing 			else if (pidx == JH7110_AONCLK_OSC)
106b2ab3c94SEmil Renner Berthing 				parents[i].fw_name = "osc";
107b2ab3c94SEmil Renner Berthing 			else if (pidx == JH7110_AONCLK_GMAC0_RMII_REFIN)
108b2ab3c94SEmil Renner Berthing 				parents[i].fw_name = "gmac0_rmii_refin";
109b2ab3c94SEmil Renner Berthing 			else if (pidx == JH7110_AONCLK_GMAC0_RGMII_RXIN)
110b2ab3c94SEmil Renner Berthing 				parents[i].fw_name = "gmac0_rgmii_rxin";
111b2ab3c94SEmil Renner Berthing 			else if (pidx == JH7110_AONCLK_STG_AXIAHB)
112b2ab3c94SEmil Renner Berthing 				parents[i].fw_name = "stg_axiahb";
113b2ab3c94SEmil Renner Berthing 			else if (pidx == JH7110_AONCLK_APB_BUS)
114b2ab3c94SEmil Renner Berthing 				parents[i].fw_name = "apb_bus";
115b2ab3c94SEmil Renner Berthing 			else if (pidx == JH7110_AONCLK_GMAC0_GTXCLK)
116b2ab3c94SEmil Renner Berthing 				parents[i].fw_name = "gmac0_gtxclk";
117b2ab3c94SEmil Renner Berthing 			else if (pidx == JH7110_AONCLK_RTC_OSC)
118b2ab3c94SEmil Renner Berthing 				parents[i].fw_name = "rtc_osc";
119b2ab3c94SEmil Renner Berthing 		}
120b2ab3c94SEmil Renner Berthing 
121b2ab3c94SEmil Renner Berthing 		clk->hw.init = &init;
122b2ab3c94SEmil Renner Berthing 		clk->idx = idx;
123b2ab3c94SEmil Renner Berthing 		clk->max_div = max & JH71X0_CLK_DIV_MASK;
124b2ab3c94SEmil Renner Berthing 
125b2ab3c94SEmil Renner Berthing 		ret = devm_clk_hw_register(&pdev->dev, &clk->hw);
126b2ab3c94SEmil Renner Berthing 		if (ret)
127b2ab3c94SEmil Renner Berthing 			return ret;
128b2ab3c94SEmil Renner Berthing 	}
129b2ab3c94SEmil Renner Berthing 
130b2ab3c94SEmil Renner Berthing 	ret = devm_of_clk_add_hw_provider(&pdev->dev, jh7110_aonclk_get, priv);
131b2ab3c94SEmil Renner Berthing 	if (ret)
132b2ab3c94SEmil Renner Berthing 		return ret;
133b2ab3c94SEmil Renner Berthing 
134b2ab3c94SEmil Renner Berthing 	return jh7110_reset_controller_register(priv, "rst-aon", 1);
135b2ab3c94SEmil Renner Berthing }
136b2ab3c94SEmil Renner Berthing 
137b2ab3c94SEmil Renner Berthing static const struct of_device_id jh7110_aoncrg_match[] = {
138b2ab3c94SEmil Renner Berthing 	{ .compatible = "starfive,jh7110-aoncrg" },
139b2ab3c94SEmil Renner Berthing 	{ /* sentinel */ }
140b2ab3c94SEmil Renner Berthing };
141b2ab3c94SEmil Renner Berthing MODULE_DEVICE_TABLE(of, jh7110_aoncrg_match);
142b2ab3c94SEmil Renner Berthing 
143b2ab3c94SEmil Renner Berthing static struct platform_driver jh7110_aoncrg_driver = {
144b2ab3c94SEmil Renner Berthing 	.probe = jh7110_aoncrg_probe,
145b2ab3c94SEmil Renner Berthing 	.driver = {
146b2ab3c94SEmil Renner Berthing 		.name = "clk-starfive-jh7110-aon",
147b2ab3c94SEmil Renner Berthing 		.of_match_table = jh7110_aoncrg_match,
148b2ab3c94SEmil Renner Berthing 	},
149b2ab3c94SEmil Renner Berthing };
150b2ab3c94SEmil Renner Berthing module_platform_driver(jh7110_aoncrg_driver);
151b2ab3c94SEmil Renner Berthing 
152b2ab3c94SEmil Renner Berthing MODULE_AUTHOR("Emil Renner Berthing");
153b2ab3c94SEmil Renner Berthing MODULE_DESCRIPTION("StarFive JH7110 always-on clock driver");
154b2ab3c94SEmil Renner Berthing MODULE_LICENSE("GPL");
155