1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Baylibre, SAS.
6 #include <linux/bitfield.h>
7 #include <linux/delay.h>
10 #include <linux/mdio-mux.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
15 #define REG2_PHYID GENMASK(21, 0)
16 #define EPHY_GXL_ID 0x110181
17 #define REG2_LEDACT GENMASK(23, 22)
18 #define REG2_LEDLINK GENMASK(25, 24)
19 #define REG2_DIV4SEL BIT(27)
20 #define REG2_ADCBYPASS BIT(30)
21 #define REG2_CLKINSEL BIT(31)
23 #define REG3_ENH BIT(3)
24 #define REG3_CFGMODE GENMASK(6, 4)
25 #define REG3_AUTOMDIX BIT(7)
26 #define REG3_PHYADDR GENMASK(12, 8)
27 #define REG3_PWRUPRST BIT(21)
28 #define REG3_PWRDOWN BIT(22)
29 #define REG3_LEDPOL BIT(23)
30 #define REG3_PHYMDI BIT(26)
31 #define REG3_CLKINEN BIT(29)
32 #define REG3_PHYIP BIT(30)
33 #define REG3_PHYEN BIT(31)
35 #define REG4_PWRUPRSTSIG BIT(0)
37 #define MESON_GXL_MDIO_EXTERNAL_ID 0
38 #define MESON_GXL_MDIO_INTERNAL_ID 1
45 static void gxl_enable_internal_mdio(struct gxl_mdio_mux *priv)
49 /* Setup the internal phy */
51 FIELD_PREP(REG3_CFGMODE, 0x7) |
53 FIELD_PREP(REG3_PHYADDR, 8) |
59 writel(REG4_PWRUPRSTSIG, priv->regs + ETH_REG4);
60 writel(val, priv->regs + ETH_REG3);
63 /* NOTE: The HW kept the phy id configurable at runtime.
64 * The id below is arbitrary. It is the one used in the vendor code.
65 * The only constraint is that it must match the one in
66 * drivers/net/phy/meson-gxl.c to properly match the PHY.
68 writel(FIELD_PREP(REG2_PHYID, EPHY_GXL_ID),
69 priv->regs + ETH_REG2);
71 /* Enable the internal phy */
73 writel(val, priv->regs + ETH_REG3);
74 writel(0, priv->regs + ETH_REG4);
76 /* The phy needs a bit of time to power up */
80 static void gxl_enable_external_mdio(struct gxl_mdio_mux *priv)
82 /* Reset the mdio bus mux to the external phy */
83 writel(0, priv->regs + ETH_REG3);
86 static int gxl_mdio_switch_fn(int current_child, int desired_child,
89 struct gxl_mdio_mux *priv = dev_get_drvdata(data);
91 if (current_child == desired_child)
94 switch (desired_child) {
95 case MESON_GXL_MDIO_EXTERNAL_ID:
96 gxl_enable_external_mdio(priv);
98 case MESON_GXL_MDIO_INTERNAL_ID:
99 gxl_enable_internal_mdio(priv);
108 static const struct of_device_id gxl_mdio_mux_match[] = {
109 { .compatible = "amlogic,gxl-mdio-mux", },
112 MODULE_DEVICE_TABLE(of, gxl_mdio_mux_match);
114 static int gxl_mdio_mux_probe(struct platform_device *pdev)
116 struct device *dev = &pdev->dev;
117 struct gxl_mdio_mux *priv;
121 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
124 platform_set_drvdata(pdev, priv);
126 priv->regs = devm_platform_ioremap_resource(pdev, 0);
127 if (IS_ERR(priv->regs))
128 return PTR_ERR(priv->regs);
130 rclk = devm_clk_get_enabled(dev, "ref");
132 return dev_err_probe(dev, PTR_ERR(rclk),
133 "failed to get reference clock\n");
135 ret = mdio_mux_init(dev, dev->of_node, gxl_mdio_switch_fn,
136 &priv->mux_handle, dev, NULL);
138 dev_err_probe(dev, ret, "mdio multiplexer init failed\n");
143 static void gxl_mdio_mux_remove(struct platform_device *pdev)
145 struct gxl_mdio_mux *priv = platform_get_drvdata(pdev);
147 mdio_mux_uninit(priv->mux_handle);
150 static struct platform_driver gxl_mdio_mux_driver = {
151 .probe = gxl_mdio_mux_probe,
152 .remove = gxl_mdio_mux_remove,
154 .name = "gxl-mdio-mux",
155 .of_match_table = gxl_mdio_mux_match,
158 module_platform_driver(gxl_mdio_mux_driver);
160 MODULE_DESCRIPTION("Amlogic GXL MDIO multiplexer driver");
162 MODULE_LICENSE("GPL");