1 // SPDX-License-Identifier: GPL-2.0+
3 * Qualcomm IPQ4019 MDIO driver
5 * Copyright (c) 2020 Sartura Ltd.
7 * Author: Luka Kovacic <luka.kovacic@sartura.hr>
8 * Author: Robert Marko <robert.marko@sartura.hr>
10 * Based on Linux driver
17 #include <linux/bitops.h>
18 #include <linux/iopoll.h>
22 #define MDIO_MODE_REG 0x40
23 #define MDIO_ADDR_REG 0x44
24 #define MDIO_DATA_WRITE_REG 0x48
25 #define MDIO_DATA_READ_REG 0x4c
26 #define MDIO_CMD_REG 0x50
27 #define MDIO_CMD_ACCESS_BUSY BIT(16)
28 #define MDIO_CMD_ACCESS_START BIT(8)
29 #define MDIO_CMD_ACCESS_CODE_READ 0
30 #define MDIO_CMD_ACCESS_CODE_WRITE 1
32 /* 0 = Clause 22, 1 = Clause 45 */
33 #define MDIO_MODE_BIT BIT(8)
35 #define IPQ4019_MDIO_TIMEOUT 10000
36 #define IPQ4019_MDIO_SLEEP 10
38 struct ipq4019_mdio_priv {
39 phys_addr_t mdio_base;
42 static int ipq4019_mdio_wait_busy(struct ipq4019_mdio_priv *priv)
46 return readl_poll_sleep_timeout(priv->mdio_base + MDIO_CMD_REG, busy,
47 (busy & MDIO_CMD_ACCESS_BUSY) == 0, IPQ4019_MDIO_SLEEP,
48 IPQ4019_MDIO_TIMEOUT);
51 int ipq4019_mdio_read(struct udevice *dev, int addr, int devad, int reg)
53 struct ipq4019_mdio_priv *priv = dev_get_priv(dev);
56 if (ipq4019_mdio_wait_busy(priv))
59 /* Issue the phy address and reg */
60 writel((addr << 8) | reg, priv->mdio_base + MDIO_ADDR_REG);
62 cmd = MDIO_CMD_ACCESS_START | MDIO_CMD_ACCESS_CODE_READ;
64 /* Issue read command */
65 writel(cmd, priv->mdio_base + MDIO_CMD_REG);
67 /* Wait read complete */
68 if (ipq4019_mdio_wait_busy(priv))
71 /* Read and return data */
72 return readl(priv->mdio_base + MDIO_DATA_READ_REG);
75 int ipq4019_mdio_write(struct udevice *dev, int addr, int devad,
78 struct ipq4019_mdio_priv *priv = dev_get_priv(dev);
81 if (ipq4019_mdio_wait_busy(priv))
84 /* Issue the phy addreass and reg */
85 writel((addr << 8) | reg, priv->mdio_base + MDIO_ADDR_REG);
87 /* Issue write data */
88 writel(val, priv->mdio_base + MDIO_DATA_WRITE_REG);
90 cmd = MDIO_CMD_ACCESS_START | MDIO_CMD_ACCESS_CODE_WRITE;
92 /* Issue write command */
93 writel(cmd, priv->mdio_base + MDIO_CMD_REG);
95 /* Wait for write complete */
97 if (ipq4019_mdio_wait_busy(priv))
103 static const struct mdio_ops ipq4019_mdio_ops = {
104 .read = ipq4019_mdio_read,
105 .write = ipq4019_mdio_write,
108 static int ipq4019_mdio_bind(struct udevice *dev)
110 if (ofnode_valid(dev->node))
111 device_set_name(dev, ofnode_get_name(dev->node));
116 static int ipq4019_mdio_probe(struct udevice *dev)
118 struct ipq4019_mdio_priv *priv = dev_get_priv(dev);
121 priv->mdio_base = dev_read_addr(dev);
122 if (priv->mdio_base == FDT_ADDR_T_NONE)
125 /* Enter Clause 22 mode */
126 data = readl(priv->mdio_base + MDIO_MODE_REG);
127 data &= ~MDIO_MODE_BIT;
128 writel(data, priv->mdio_base + MDIO_MODE_REG);
133 static const struct udevice_id ipq4019_mdio_ids[] = {
134 { .compatible = "qcom,ipq4019-mdio", },
138 U_BOOT_DRIVER(ipq4019_mdio) = {
139 .name = "ipq4019_mdio",
141 .of_match = ipq4019_mdio_ids,
142 .bind = ipq4019_mdio_bind,
143 .probe = ipq4019_mdio_probe,
144 .ops = &ipq4019_mdio_ops,
145 .priv_auto_alloc_size = sizeof(struct ipq4019_mdio_priv),