1 // SPDX-License-Identifier: GPL-2.0+
2 /* Realtek MDIO interface driver
4 * ASICs we intend to support with this driver:
6 * RTL8366 - The original version, apparently
7 * RTL8369 - Similar enough to have the same datsheet as RTL8366
8 * RTL8366RB - Probably reads out "RTL8366 revision B", has a quite
9 * different register layout from the other two
10 * RTL8366S - Is this "RTL8366 super"?
11 * RTL8367 - Has an OpenWRT driver as well
12 * RTL8368S - Seems to be an alternative name for RTL8366RB
13 * RTL8370 - Also uses SMI
22 #include <linux/module.h>
24 #include <linux/overflow.h>
25 #include <linux/regmap.h>
28 #include "realtek-mdio.h"
31 /* Read/write via mdiobus */
32 #define REALTEK_MDIO_CTRL0_REG 31
33 #define REALTEK_MDIO_START_REG 29
34 #define REALTEK_MDIO_CTRL1_REG 21
35 #define REALTEK_MDIO_ADDRESS_REG 23
36 #define REALTEK_MDIO_DATA_WRITE_REG 24
37 #define REALTEK_MDIO_DATA_READ_REG 25
39 #define REALTEK_MDIO_START_OP 0xFFFF
40 #define REALTEK_MDIO_ADDR_OP 0x000E
41 #define REALTEK_MDIO_READ_OP 0x0001
42 #define REALTEK_MDIO_WRITE_OP 0x0003
44 static int realtek_mdio_write(void *ctx, u32 reg, u32 val)
46 struct realtek_priv *priv = ctx;
47 struct mii_bus *bus = priv->bus;
50 mutex_lock(&bus->mdio_lock);
52 ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_CTRL0_REG, REALTEK_MDIO_ADDR_OP);
56 ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_ADDRESS_REG, reg);
60 ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_DATA_WRITE_REG, val);
64 ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_CTRL1_REG, REALTEK_MDIO_WRITE_OP);
67 mutex_unlock(&bus->mdio_lock);
72 static int realtek_mdio_read(void *ctx, u32 reg, u32 *val)
74 struct realtek_priv *priv = ctx;
75 struct mii_bus *bus = priv->bus;
78 mutex_lock(&bus->mdio_lock);
80 ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_CTRL0_REG, REALTEK_MDIO_ADDR_OP);
84 ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_ADDRESS_REG, reg);
88 ret = bus->write(bus, priv->mdio_addr, REALTEK_MDIO_CTRL1_REG, REALTEK_MDIO_READ_OP);
92 ret = bus->read(bus, priv->mdio_addr, REALTEK_MDIO_DATA_READ_REG);
99 mutex_unlock(&bus->mdio_lock);
104 static const struct realtek_interface_info realtek_mdio_info = {
105 .reg_read = realtek_mdio_read,
106 .reg_write = realtek_mdio_write,
110 * realtek_mdio_probe() - Probe a platform device for an MDIO-connected switch
111 * @mdiodev: mdio_device to probe on.
113 * This function should be used as the .probe in an mdio_driver. After
114 * calling the common probe function for both interfaces, it initializes the
115 * values specific for MDIO-connected devices. Finally, it calls a common
116 * function to register the DSA switch.
118 * Context: Can sleep. Takes and releases priv->map_lock.
119 * Return: Returns 0 on success, a negative error on failure.
121 int realtek_mdio_probe(struct mdio_device *mdiodev)
123 struct device *dev = &mdiodev->dev;
124 struct realtek_priv *priv;
127 priv = rtl83xx_probe(dev, &realtek_mdio_info);
129 return PTR_ERR(priv);
131 priv->bus = mdiodev->bus;
132 priv->mdio_addr = mdiodev->addr;
133 priv->write_reg_noack = realtek_mdio_write;
135 ret = rtl83xx_register_switch(priv);
137 rtl83xx_remove(priv);
143 EXPORT_SYMBOL_NS_GPL(realtek_mdio_probe, "REALTEK_DSA");
146 * realtek_mdio_remove() - Remove the driver of an MDIO-connected switch
147 * @mdiodev: mdio_device to be removed.
149 * This function should be used as the .remove in an mdio_driver. First
150 * it unregisters the DSA switch and then it calls the common remove function.
152 * Context: Can sleep.
155 void realtek_mdio_remove(struct mdio_device *mdiodev)
157 struct realtek_priv *priv = dev_get_drvdata(&mdiodev->dev);
162 rtl83xx_unregister_switch(priv);
164 rtl83xx_remove(priv);
166 EXPORT_SYMBOL_NS_GPL(realtek_mdio_remove, "REALTEK_DSA");
169 * realtek_mdio_shutdown() - Shutdown the driver of a MDIO-connected switch
170 * @mdiodev: mdio_device shutting down.
172 * This function should be used as the .shutdown in a platform_driver. It calls
173 * the common shutdown function.
175 * Context: Can sleep.
178 void realtek_mdio_shutdown(struct mdio_device *mdiodev)
180 struct realtek_priv *priv = dev_get_drvdata(&mdiodev->dev);
185 rtl83xx_shutdown(priv);
187 EXPORT_SYMBOL_NS_GPL(realtek_mdio_shutdown, "REALTEK_DSA");