1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2015, The Linux Foundation. All rights reserved. */
3 /* Copyright (c) 2020 Sartura Ltd. */
5 #include <linux/delay.h>
7 #include <linux/iopoll.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/of_address.h>
11 #include <linux/of_mdio.h>
12 #include <linux/phy.h>
13 #include <linux/platform_device.h>
14 #include <linux/clk.h>
16 #define MDIO_MODE_REG 0x40
17 #define MDIO_ADDR_REG 0x44
18 #define MDIO_DATA_WRITE_REG 0x48
19 #define MDIO_DATA_READ_REG 0x4c
20 #define MDIO_CMD_REG 0x50
21 #define MDIO_CMD_ACCESS_BUSY BIT(16)
22 #define MDIO_CMD_ACCESS_START BIT(8)
23 #define MDIO_CMD_ACCESS_CODE_READ 0
24 #define MDIO_CMD_ACCESS_CODE_WRITE 1
25 #define MDIO_CMD_ACCESS_CODE_C45_ADDR 0
26 #define MDIO_CMD_ACCESS_CODE_C45_WRITE 1
27 #define MDIO_CMD_ACCESS_CODE_C45_READ 2
29 /* 0 = Clause 22, 1 = Clause 45 */
30 #define MDIO_MODE_C45 BIT(8)
32 #define IPQ4019_MDIO_TIMEOUT 10000
33 #define IPQ4019_MDIO_SLEEP 10
35 /* MDIO clock source frequency is fixed to 100M */
36 #define IPQ_MDIO_CLK_RATE 100000000
38 #define IPQ_PHY_SET_DELAY_US 100000
40 struct ipq4019_mdio_data {
41 void __iomem *membase;
42 void __iomem *eth_ldo_rdy;
46 static int ipq4019_mdio_wait_busy(struct mii_bus *bus)
48 struct ipq4019_mdio_data *priv = bus->priv;
51 return readl_poll_timeout(priv->membase + MDIO_CMD_REG, busy,
52 (busy & MDIO_CMD_ACCESS_BUSY) == 0,
53 IPQ4019_MDIO_SLEEP, IPQ4019_MDIO_TIMEOUT);
56 static int ipq4019_mdio_read_c45(struct mii_bus *bus, int mii_id, int mmd,
59 struct ipq4019_mdio_data *priv = bus->priv;
63 if (ipq4019_mdio_wait_busy(bus))
66 data = readl(priv->membase + MDIO_MODE_REG);
68 data |= MDIO_MODE_C45;
70 writel(data, priv->membase + MDIO_MODE_REG);
72 /* issue the phy address and mmd */
73 writel((mii_id << 8) | mmd, priv->membase + MDIO_ADDR_REG);
76 writel(reg, priv->membase + MDIO_DATA_WRITE_REG);
78 cmd = MDIO_CMD_ACCESS_START | MDIO_CMD_ACCESS_CODE_C45_ADDR;
80 /* issue read command */
81 writel(cmd, priv->membase + MDIO_CMD_REG);
83 /* Wait read complete */
84 if (ipq4019_mdio_wait_busy(bus))
87 cmd = MDIO_CMD_ACCESS_START | MDIO_CMD_ACCESS_CODE_C45_READ;
89 writel(cmd, priv->membase + MDIO_CMD_REG);
91 if (ipq4019_mdio_wait_busy(bus))
94 /* Read and return data */
95 return readl(priv->membase + MDIO_DATA_READ_REG);
98 static int ipq4019_mdio_read_c22(struct mii_bus *bus, int mii_id, int regnum)
100 struct ipq4019_mdio_data *priv = bus->priv;
104 if (ipq4019_mdio_wait_busy(bus))
107 data = readl(priv->membase + MDIO_MODE_REG);
109 data &= ~MDIO_MODE_C45;
111 writel(data, priv->membase + MDIO_MODE_REG);
113 /* issue the phy address and reg */
114 writel((mii_id << 8) | regnum, priv->membase + MDIO_ADDR_REG);
116 cmd = MDIO_CMD_ACCESS_START | MDIO_CMD_ACCESS_CODE_READ;
118 /* issue read command */
119 writel(cmd, priv->membase + MDIO_CMD_REG);
121 /* Wait read complete */
122 if (ipq4019_mdio_wait_busy(bus))
125 /* Read and return data */
126 return readl(priv->membase + MDIO_DATA_READ_REG);
129 static int ipq4019_mdio_write_c45(struct mii_bus *bus, int mii_id, int mmd,
132 struct ipq4019_mdio_data *priv = bus->priv;
136 if (ipq4019_mdio_wait_busy(bus))
139 data = readl(priv->membase + MDIO_MODE_REG);
141 data |= MDIO_MODE_C45;
143 writel(data, priv->membase + MDIO_MODE_REG);
145 /* issue the phy address and mmd */
146 writel((mii_id << 8) | mmd, priv->membase + MDIO_ADDR_REG);
149 writel(reg, priv->membase + MDIO_DATA_WRITE_REG);
151 cmd = MDIO_CMD_ACCESS_START | MDIO_CMD_ACCESS_CODE_C45_ADDR;
153 writel(cmd, priv->membase + MDIO_CMD_REG);
155 if (ipq4019_mdio_wait_busy(bus))
158 /* issue write data */
159 writel(value, priv->membase + MDIO_DATA_WRITE_REG);
161 cmd = MDIO_CMD_ACCESS_START | MDIO_CMD_ACCESS_CODE_C45_WRITE;
162 writel(cmd, priv->membase + MDIO_CMD_REG);
164 /* Wait write complete */
165 if (ipq4019_mdio_wait_busy(bus))
171 static int ipq4019_mdio_write_c22(struct mii_bus *bus, int mii_id, int regnum,
174 struct ipq4019_mdio_data *priv = bus->priv;
178 if (ipq4019_mdio_wait_busy(bus))
181 /* Enter Clause 22 mode */
182 data = readl(priv->membase + MDIO_MODE_REG);
184 data &= ~MDIO_MODE_C45;
186 writel(data, priv->membase + MDIO_MODE_REG);
188 /* issue the phy address and reg */
189 writel((mii_id << 8) | regnum, priv->membase + MDIO_ADDR_REG);
191 /* issue write data */
192 writel(value, priv->membase + MDIO_DATA_WRITE_REG);
194 /* issue write command */
195 cmd = MDIO_CMD_ACCESS_START | MDIO_CMD_ACCESS_CODE_WRITE;
197 writel(cmd, priv->membase + MDIO_CMD_REG);
199 /* Wait write complete */
200 if (ipq4019_mdio_wait_busy(bus))
206 static int ipq_mdio_reset(struct mii_bus *bus)
208 struct ipq4019_mdio_data *priv = bus->priv;
212 /* To indicate CMN_PLL that ethernet_ldo has been ready if platform resource 1
213 * is specified in the device tree.
215 if (priv->eth_ldo_rdy) {
216 val = readl(priv->eth_ldo_rdy);
218 writel(val, priv->eth_ldo_rdy);
219 fsleep(IPQ_PHY_SET_DELAY_US);
222 /* Configure MDIO clock source frequency if clock is specified in the device tree */
223 ret = clk_set_rate(priv->mdio_clk, IPQ_MDIO_CLK_RATE);
227 ret = clk_prepare_enable(priv->mdio_clk);
234 static int ipq4019_mdio_probe(struct platform_device *pdev)
236 struct ipq4019_mdio_data *priv;
238 struct resource *res;
241 bus = devm_mdiobus_alloc_size(&pdev->dev, sizeof(*priv));
247 priv->membase = devm_platform_ioremap_resource(pdev, 0);
248 if (IS_ERR(priv->membase))
249 return PTR_ERR(priv->membase);
251 priv->mdio_clk = devm_clk_get_optional(&pdev->dev, "gcc_mdio_ahb_clk");
252 if (IS_ERR(priv->mdio_clk))
253 return PTR_ERR(priv->mdio_clk);
255 /* The platform resource is provided on the chipset IPQ5018 */
256 /* This resource is optional */
257 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
259 priv->eth_ldo_rdy = devm_ioremap_resource(&pdev->dev, res);
261 bus->name = "ipq4019_mdio";
262 bus->read = ipq4019_mdio_read_c22;
263 bus->write = ipq4019_mdio_write_c22;
264 bus->read_c45 = ipq4019_mdio_read_c45;
265 bus->write_c45 = ipq4019_mdio_write_c45;
266 bus->reset = ipq_mdio_reset;
267 bus->parent = &pdev->dev;
268 snprintf(bus->id, MII_BUS_ID_SIZE, "%s%d", pdev->name, pdev->id);
270 ret = of_mdiobus_register(bus, pdev->dev.of_node);
272 dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
276 platform_set_drvdata(pdev, bus);
281 static int ipq4019_mdio_remove(struct platform_device *pdev)
283 struct mii_bus *bus = platform_get_drvdata(pdev);
285 mdiobus_unregister(bus);
290 static const struct of_device_id ipq4019_mdio_dt_ids[] = {
291 { .compatible = "qcom,ipq4019-mdio" },
292 { .compatible = "qcom,ipq5018-mdio" },
295 MODULE_DEVICE_TABLE(of, ipq4019_mdio_dt_ids);
297 static struct platform_driver ipq4019_mdio_driver = {
298 .probe = ipq4019_mdio_probe,
299 .remove = ipq4019_mdio_remove,
301 .name = "ipq4019-mdio",
302 .of_match_table = ipq4019_mdio_dt_ids,
306 module_platform_driver(ipq4019_mdio_driver);
308 MODULE_DESCRIPTION("ipq4019 MDIO interface driver");
309 MODULE_AUTHOR("Qualcomm Atheros");
310 MODULE_LICENSE("Dual BSD/GPL");