2 * Driver for the MDIO interface of Marvell network interfaces.
4 * Since the MDIO interface of Marvell network interfaces is shared
5 * between all network interfaces, having a single driver allows to
6 * handle concurrent accesses properly (you may have four Ethernet
7 * ports, but they in fact share the same SMI interface to access the
8 * MDIO bus). Moreover, this MDIO interface code is similar between
9 * the mv643xx_eth driver and the mvneta driver. For now, it is only
10 * used by the mvneta driver, but it could later be used by the
11 * mv643xx_eth driver as well.
13 * Copyright (C) 2012 Marvell
15 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
17 * This file is licensed under the terms of the GNU General Public
18 * License version 2. This program is licensed "as is" without any
19 * warranty of any kind, whether express or implied.
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/phy.h>
27 #include <linux/of_address.h>
28 #include <linux/of_mdio.h>
29 #include <linux/platform_device.h>
30 #include <linux/delay.h>
32 #define MVMDIO_SMI_DATA_SHIFT 0
33 #define MVMDIO_SMI_PHY_ADDR_SHIFT 16
34 #define MVMDIO_SMI_PHY_REG_SHIFT 21
35 #define MVMDIO_SMI_READ_OPERATION BIT(26)
36 #define MVMDIO_SMI_WRITE_OPERATION 0
37 #define MVMDIO_SMI_READ_VALID BIT(27)
38 #define MVMDIO_SMI_BUSY BIT(28)
40 struct orion_mdio_dev {
45 /* Wait for the SMI unit to be ready for another operation
47 static int orion_mdio_wait_ready(struct mii_bus *bus)
49 struct orion_mdio_dev *dev = bus->priv;
55 val = readl(dev->smireg);
56 if (!(val & MVMDIO_SMI_BUSY))
60 dev_err(bus->parent, "Timeout: SMI busy for too long\n");
71 static int orion_mdio_read(struct mii_bus *bus, int mii_id,
74 struct orion_mdio_dev *dev = bus->priv;
79 mutex_lock(&dev->lock);
81 ret = orion_mdio_wait_ready(bus);
83 mutex_unlock(&dev->lock);
87 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
88 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
89 MVMDIO_SMI_READ_OPERATION),
92 /* Wait for the value to become available */
95 val = readl(dev->smireg);
96 if (val & MVMDIO_SMI_READ_VALID)
100 dev_err(bus->parent, "Timeout when reading PHY\n");
101 mutex_unlock(&dev->lock);
109 mutex_unlock(&dev->lock);
114 static int orion_mdio_write(struct mii_bus *bus, int mii_id,
115 int regnum, u16 value)
117 struct orion_mdio_dev *dev = bus->priv;
120 mutex_lock(&dev->lock);
122 ret = orion_mdio_wait_ready(bus);
124 mutex_unlock(&dev->lock);
128 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
129 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
130 MVMDIO_SMI_WRITE_OPERATION |
131 (value << MVMDIO_SMI_DATA_SHIFT)),
134 mutex_unlock(&dev->lock);
139 static int orion_mdio_reset(struct mii_bus *bus)
144 static int orion_mdio_probe(struct platform_device *pdev)
146 struct device_node *np = pdev->dev.of_node;
148 struct orion_mdio_dev *dev;
151 bus = mdiobus_alloc_size(sizeof(struct orion_mdio_dev));
153 dev_err(&pdev->dev, "Cannot allocate MDIO bus\n");
157 bus->name = "orion_mdio_bus";
158 bus->read = orion_mdio_read;
159 bus->write = orion_mdio_write;
160 bus->reset = orion_mdio_reset;
161 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
162 dev_name(&pdev->dev));
163 bus->parent = &pdev->dev;
165 bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
171 for (i = 0; i < PHY_MAX_ADDR; i++)
172 bus->irq[i] = PHY_POLL;
175 dev->smireg = of_iomap(pdev->dev.of_node, 0);
177 dev_err(&pdev->dev, "No SMI register address given in DT\n");
183 mutex_init(&dev->lock);
185 ret = of_mdiobus_register(bus, np);
187 dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
188 iounmap(dev->smireg);
194 platform_set_drvdata(pdev, bus);
199 static int orion_mdio_remove(struct platform_device *pdev)
201 struct mii_bus *bus = platform_get_drvdata(pdev);
202 mdiobus_unregister(bus);
208 static const struct of_device_id orion_mdio_match[] = {
209 { .compatible = "marvell,orion-mdio" },
212 MODULE_DEVICE_TABLE(of, orion_mdio_match);
214 static struct platform_driver orion_mdio_driver = {
215 .probe = orion_mdio_probe,
216 .remove = orion_mdio_remove,
218 .name = "orion-mdio",
219 .of_match_table = orion_mdio_match,
223 module_platform_driver(orion_mdio_driver);
225 MODULE_DESCRIPTION("Marvell MDIO interface driver");
226 MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
227 MODULE_LICENSE("GPL");