xref: /linux/drivers/net/mdio/mdio-mux-meson-g12a.c (revision 7681a4f58fb9c338d6dfe1181607f84c793d77de)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Baylibre, SAS.
3  * Author: Jerome Brunet <jbrunet@baylibre.com>
4  */
5 
6 #include <linux/bitfield.h>
7 #include <linux/clk.h>
8 #include <linux/clk-provider.h>
9 #include <linux/device.h>
10 #include <linux/io.h>
11 #include <linux/iopoll.h>
12 #include <linux/mdio-mux.h>
13 #include <linux/module.h>
14 #include <linux/phy.h>
15 #include <linux/platform_device.h>
16 
17 #define ETH_PLL_STS		0x40
18 #define ETH_PLL_CTL0		0x44
19 #define  PLL_CTL0_LOCK_DIG	BIT(30)
20 #define  PLL_CTL0_RST		BIT(29)
21 #define  PLL_CTL0_EN		BIT(28)
22 #define  PLL_CTL0_SEL		BIT(23)
23 #define  PLL_CTL0_N		GENMASK(14, 10)
24 #define  PLL_CTL0_M		GENMASK(8, 0)
25 #define  PLL_LOCK_TIMEOUT	1000000
26 #define  PLL_MUX_NUM_PARENT	2
27 #define ETH_PLL_CTL1		0x48
28 #define ETH_PLL_CTL2		0x4c
29 #define ETH_PLL_CTL3		0x50
30 #define ETH_PLL_CTL4		0x54
31 #define ETH_PLL_CTL5		0x58
32 #define ETH_PLL_CTL6		0x5c
33 #define ETH_PLL_CTL7		0x60
34 
35 #define ETH_PHY_CNTL0		0x80
36 #define   EPHY_G12A_ID		0x33010180
37 #define ETH_PHY_CNTL1		0x84
38 #define  PHY_CNTL1_ST_MODE	GENMASK(2, 0)
39 #define  PHY_CNTL1_ST_PHYADD	GENMASK(7, 3)
40 #define   EPHY_DFLT_ADD		8
41 #define  PHY_CNTL1_MII_MODE	GENMASK(15, 14)
42 #define   EPHY_MODE_RMII	0x1
43 #define  PHY_CNTL1_CLK_EN	BIT(16)
44 #define  PHY_CNTL1_CLKFREQ	BIT(17)
45 #define  PHY_CNTL1_PHY_ENB	BIT(18)
46 #define ETH_PHY_CNTL2		0x88
47 #define  PHY_CNTL2_USE_INTERNAL	BIT(5)
48 #define  PHY_CNTL2_SMI_SRC_MAC	BIT(6)
49 #define  PHY_CNTL2_RX_CLK_EPHY	BIT(9)
50 
51 #define MESON_G12A_MDIO_EXTERNAL_ID 0
52 #define MESON_G12A_MDIO_INTERNAL_ID 1
53 
54 struct g12a_mdio_mux {
55 	bool pll_is_enabled;
56 	void __iomem *regs;
57 	void *mux_handle;
58 	struct clk *pll;
59 };
60 
61 struct g12a_ephy_pll {
62 	void __iomem *base;
63 	struct clk_hw hw;
64 };
65 
66 #define g12a_ephy_pll_to_dev(_hw)			\
67 	container_of(_hw, struct g12a_ephy_pll, hw)
68 
69 static unsigned long g12a_ephy_pll_recalc_rate(struct clk_hw *hw,
70 					       unsigned long parent_rate)
71 {
72 	struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
73 	u32 val, m, n;
74 
75 	val = readl(pll->base + ETH_PLL_CTL0);
76 	m = FIELD_GET(PLL_CTL0_M, val);
77 	n = FIELD_GET(PLL_CTL0_N, val);
78 
79 	return parent_rate * m / n;
80 }
81 
82 static int g12a_ephy_pll_enable(struct clk_hw *hw)
83 {
84 	struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
85 	u32 val = readl(pll->base + ETH_PLL_CTL0);
86 
87 	/* Apply both enable an reset */
88 	val |= PLL_CTL0_RST | PLL_CTL0_EN;
89 	writel(val, pll->base + ETH_PLL_CTL0);
90 
91 	/* Clear the reset to let PLL lock */
92 	val &= ~PLL_CTL0_RST;
93 	writel(val, pll->base + ETH_PLL_CTL0);
94 
95 	/* Poll on the digital lock instead of the usual analog lock
96 	 * This is done because bit 31 is unreliable on some SoC. Bit
97 	 * 31 may indicate that the PLL is not lock even though the clock
98 	 * is actually running
99 	 */
100 	return readl_poll_timeout(pll->base + ETH_PLL_CTL0, val,
101 				  val & PLL_CTL0_LOCK_DIG, 0, PLL_LOCK_TIMEOUT);
102 }
103 
104 static void g12a_ephy_pll_disable(struct clk_hw *hw)
105 {
106 	struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
107 	u32 val;
108 
109 	val = readl(pll->base + ETH_PLL_CTL0);
110 	val &= ~PLL_CTL0_EN;
111 	val |= PLL_CTL0_RST;
112 	writel(val, pll->base + ETH_PLL_CTL0);
113 }
114 
115 static int g12a_ephy_pll_is_enabled(struct clk_hw *hw)
116 {
117 	struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
118 	unsigned int val;
119 
120 	val = readl(pll->base + ETH_PLL_CTL0);
121 
122 	return (val & PLL_CTL0_LOCK_DIG) ? 1 : 0;
123 }
124 
125 static int g12a_ephy_pll_init(struct clk_hw *hw)
126 {
127 	struct g12a_ephy_pll *pll = g12a_ephy_pll_to_dev(hw);
128 
129 	/* Apply PLL HW settings */
130 	writel(0x29c0040a, pll->base + ETH_PLL_CTL0);
131 	writel(0x927e0000, pll->base + ETH_PLL_CTL1);
132 	writel(0xac5f49e5, pll->base + ETH_PLL_CTL2);
133 	writel(0x00000000, pll->base + ETH_PLL_CTL3);
134 	writel(0x00000000, pll->base + ETH_PLL_CTL4);
135 	writel(0x20200000, pll->base + ETH_PLL_CTL5);
136 	writel(0x0000c002, pll->base + ETH_PLL_CTL6);
137 	writel(0x00000023, pll->base + ETH_PLL_CTL7);
138 
139 	return 0;
140 }
141 
142 static const struct clk_ops g12a_ephy_pll_ops = {
143 	.recalc_rate	= g12a_ephy_pll_recalc_rate,
144 	.is_enabled	= g12a_ephy_pll_is_enabled,
145 	.enable		= g12a_ephy_pll_enable,
146 	.disable	= g12a_ephy_pll_disable,
147 	.init		= g12a_ephy_pll_init,
148 };
149 
150 static int g12a_enable_internal_mdio(struct g12a_mdio_mux *priv)
151 {
152 	int ret;
153 
154 	/* Enable the phy clock */
155 	if (!priv->pll_is_enabled) {
156 		ret = clk_prepare_enable(priv->pll);
157 		if (ret)
158 			return ret;
159 	}
160 
161 	priv->pll_is_enabled = true;
162 
163 	/* Initialize ephy control */
164 	writel(EPHY_G12A_ID, priv->regs + ETH_PHY_CNTL0);
165 	writel(FIELD_PREP(PHY_CNTL1_ST_MODE, 3) |
166 	       FIELD_PREP(PHY_CNTL1_ST_PHYADD, EPHY_DFLT_ADD) |
167 	       FIELD_PREP(PHY_CNTL1_MII_MODE, EPHY_MODE_RMII) |
168 	       PHY_CNTL1_CLK_EN |
169 	       PHY_CNTL1_CLKFREQ |
170 	       PHY_CNTL1_PHY_ENB,
171 	       priv->regs + ETH_PHY_CNTL1);
172 	writel(PHY_CNTL2_USE_INTERNAL |
173 	       PHY_CNTL2_SMI_SRC_MAC |
174 	       PHY_CNTL2_RX_CLK_EPHY,
175 	       priv->regs + ETH_PHY_CNTL2);
176 
177 	return 0;
178 }
179 
180 static int g12a_enable_external_mdio(struct g12a_mdio_mux *priv)
181 {
182 	/* Reset the mdio bus mux */
183 	writel_relaxed(0x0, priv->regs + ETH_PHY_CNTL2);
184 
185 	/* Disable the phy clock if enabled */
186 	if (priv->pll_is_enabled) {
187 		clk_disable_unprepare(priv->pll);
188 		priv->pll_is_enabled = false;
189 	}
190 
191 	return 0;
192 }
193 
194 static int g12a_mdio_switch_fn(int current_child, int desired_child,
195 			       void *data)
196 {
197 	struct g12a_mdio_mux *priv = dev_get_drvdata(data);
198 
199 	if (current_child == desired_child)
200 		return 0;
201 
202 	switch (desired_child) {
203 	case MESON_G12A_MDIO_EXTERNAL_ID:
204 		return g12a_enable_external_mdio(priv);
205 	case MESON_G12A_MDIO_INTERNAL_ID:
206 		return g12a_enable_internal_mdio(priv);
207 	default:
208 		return -EINVAL;
209 	}
210 }
211 
212 static const struct of_device_id g12a_mdio_mux_match[] = {
213 	{ .compatible = "amlogic,g12a-mdio-mux", },
214 	{},
215 };
216 MODULE_DEVICE_TABLE(of, g12a_mdio_mux_match);
217 
218 static int g12a_ephy_glue_clk_register(struct device *dev)
219 {
220 	struct g12a_mdio_mux *priv = dev_get_drvdata(dev);
221 	const char *parent_names[PLL_MUX_NUM_PARENT];
222 	struct clk_init_data init;
223 	struct g12a_ephy_pll *pll;
224 	struct clk_mux *mux;
225 	struct clk *clk;
226 	char *name;
227 	int i;
228 
229 	/* get the mux parents */
230 	for (i = 0; i < PLL_MUX_NUM_PARENT; i++) {
231 		char in_name[8];
232 
233 		snprintf(in_name, sizeof(in_name), "clkin%d", i);
234 		clk = devm_clk_get(dev, in_name);
235 		if (IS_ERR(clk))
236 			return dev_err_probe(dev, PTR_ERR(clk),
237 					     "Missing clock %s\n", in_name);
238 
239 		parent_names[i] = __clk_get_name(clk);
240 	}
241 
242 	/* create the input mux */
243 	mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
244 	if (!mux)
245 		return -ENOMEM;
246 
247 	name = kasprintf(GFP_KERNEL, "%s#mux", dev_name(dev));
248 	if (!name)
249 		return -ENOMEM;
250 
251 	init.name = name;
252 	init.ops = &clk_mux_ro_ops;
253 	init.flags = 0;
254 	init.parent_names = parent_names;
255 	init.num_parents = PLL_MUX_NUM_PARENT;
256 
257 	mux->reg = priv->regs + ETH_PLL_CTL0;
258 	mux->shift = __ffs(PLL_CTL0_SEL);
259 	mux->mask = PLL_CTL0_SEL >> mux->shift;
260 	mux->hw.init = &init;
261 
262 	clk = devm_clk_register(dev, &mux->hw);
263 	kfree(name);
264 	if (IS_ERR(clk)) {
265 		dev_err(dev, "failed to register input mux\n");
266 		return PTR_ERR(clk);
267 	}
268 
269 	/* create the pll */
270 	pll = devm_kzalloc(dev, sizeof(*pll), GFP_KERNEL);
271 	if (!pll)
272 		return -ENOMEM;
273 
274 	name = kasprintf(GFP_KERNEL, "%s#pll", dev_name(dev));
275 	if (!name)
276 		return -ENOMEM;
277 
278 	init.name = name;
279 	init.ops = &g12a_ephy_pll_ops;
280 	init.flags = 0;
281 	parent_names[0] = __clk_get_name(clk);
282 	init.parent_names = parent_names;
283 	init.num_parents = 1;
284 
285 	pll->base = priv->regs;
286 	pll->hw.init = &init;
287 
288 	clk = devm_clk_register(dev, &pll->hw);
289 	kfree(name);
290 	if (IS_ERR(clk)) {
291 		dev_err(dev, "failed to register input mux\n");
292 		return PTR_ERR(clk);
293 	}
294 
295 	priv->pll = clk;
296 
297 	return 0;
298 }
299 
300 static int g12a_mdio_mux_probe(struct platform_device *pdev)
301 {
302 	struct device *dev = &pdev->dev;
303 	struct g12a_mdio_mux *priv;
304 	struct clk *pclk;
305 	int ret;
306 
307 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
308 	if (!priv)
309 		return -ENOMEM;
310 
311 	platform_set_drvdata(pdev, priv);
312 
313 	priv->regs = devm_platform_ioremap_resource(pdev, 0);
314 	if (IS_ERR(priv->regs))
315 		return PTR_ERR(priv->regs);
316 
317 	pclk = devm_clk_get_enabled(dev, "pclk");
318 	if (IS_ERR(pclk))
319 		return dev_err_probe(dev, PTR_ERR(pclk),
320 				     "failed to get peripheral clock\n");
321 
322 	/* Register PLL in CCF */
323 	ret = g12a_ephy_glue_clk_register(dev);
324 	if (ret)
325 		return ret;
326 
327 	ret = mdio_mux_init(dev, dev->of_node, g12a_mdio_switch_fn,
328 			    &priv->mux_handle, dev, NULL);
329 	if (ret)
330 		dev_err_probe(dev, ret, "mdio multiplexer init failed\n");
331 
332 	return ret;
333 }
334 
335 static int g12a_mdio_mux_remove(struct platform_device *pdev)
336 {
337 	struct g12a_mdio_mux *priv = platform_get_drvdata(pdev);
338 
339 	mdio_mux_uninit(priv->mux_handle);
340 
341 	if (priv->pll_is_enabled)
342 		clk_disable_unprepare(priv->pll);
343 
344 	return 0;
345 }
346 
347 static struct platform_driver g12a_mdio_mux_driver = {
348 	.probe		= g12a_mdio_mux_probe,
349 	.remove		= g12a_mdio_mux_remove,
350 	.driver		= {
351 		.name	= "g12a-mdio_mux",
352 		.of_match_table = g12a_mdio_mux_match,
353 	},
354 };
355 module_platform_driver(g12a_mdio_mux_driver);
356 
357 MODULE_DESCRIPTION("Amlogic G12a MDIO multiplexer driver");
358 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
359 MODULE_LICENSE("GPL v2");
360