1 /* MOXA ART Ethernet (RTL8201CP) MDIO interface driver
3 * Copyright (C) 2013 Jonas Jensen <jonas.jensen@gmail.com>
5 * This file is licensed under the terms of the GNU General Public
6 * License version 2. This program is licensed "as is" without any
7 * warranty of any kind, whether express or implied.
10 #include <linux/delay.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/of_address.h>
16 #include <linux/of_mdio.h>
17 #include <linux/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/regulator/consumer.h>
21 #define REG_PHY_CTRL 0
22 #define REG_PHY_WRITE_DATA 4
25 #define MIIWR BIT(27) /* init write sequence (auto cleared)*/
27 #define REGAD_MASK 0x3e00000
28 #define PHYAD_MASK 0x1f0000
29 #define MIIRDATA_MASK 0xffff
31 /* REG_PHY_WRITE_DATA */
32 #define MIIWDATA_MASK 0xffff
34 struct moxart_mdio_data {
38 static int moxart_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
40 struct moxart_mdio_data *data = bus->priv;
42 unsigned int count = 5;
44 dev_dbg(&bus->dev, "%s\n", __func__);
46 ctrl |= MIIRD | ((mii_id << 16) & PHYAD_MASK) |
47 ((regnum << 21) & REGAD_MASK);
49 writel(ctrl, data->base + REG_PHY_CTRL);
52 ctrl = readl(data->base + REG_PHY_CTRL);
55 return ctrl & MIIRDATA_MASK;
61 dev_dbg(&bus->dev, "%s timed out\n", __func__);
66 static int moxart_mdio_write(struct mii_bus *bus, int mii_id,
67 int regnum, u16 value)
69 struct moxart_mdio_data *data = bus->priv;
71 unsigned int count = 5;
73 dev_dbg(&bus->dev, "%s\n", __func__);
75 ctrl |= MIIWR | ((mii_id << 16) & PHYAD_MASK) |
76 ((regnum << 21) & REGAD_MASK);
78 value &= MIIWDATA_MASK;
80 writel(value, data->base + REG_PHY_WRITE_DATA);
81 writel(ctrl, data->base + REG_PHY_CTRL);
84 ctrl = readl(data->base + REG_PHY_CTRL);
93 dev_dbg(&bus->dev, "%s timed out\n", __func__);
98 static int moxart_mdio_reset(struct mii_bus *bus)
102 for (i = 0; i < PHY_MAX_ADDR; i++) {
103 data = moxart_mdio_read(bus, i, MII_BMCR);
108 if (moxart_mdio_write(bus, i, MII_BMCR, data) < 0)
115 static int moxart_mdio_probe(struct platform_device *pdev)
117 struct device_node *np = pdev->dev.of_node;
119 struct moxart_mdio_data *data;
120 struct resource *res;
123 bus = mdiobus_alloc_size(sizeof(*data));
127 bus->name = "MOXA ART Ethernet MII";
128 bus->read = &moxart_mdio_read;
129 bus->write = &moxart_mdio_write;
130 bus->reset = &moxart_mdio_reset;
131 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d-mii", pdev->name, pdev->id);
132 bus->parent = &pdev->dev;
134 bus->irq = devm_kzalloc(&pdev->dev, sizeof(int) * PHY_MAX_ADDR,
138 goto err_out_free_mdiobus;
141 /* Setting PHY_IGNORE_INTERRUPT here even if it has no effect,
142 * of_mdiobus_register() sets these PHY_POLL.
143 * Ideally, the interrupt from MAC controller could be used to
144 * detect link state changes, not polling, i.e. if there was
145 * a way phy_driver could set PHY_HAS_INTERRUPT but have that
146 * interrupt handled in ethernet drivercode.
148 for (i = 0; i < PHY_MAX_ADDR; i++)
149 bus->irq[i] = PHY_IGNORE_INTERRUPT;
152 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
153 data->base = devm_ioremap_resource(&pdev->dev, res);
154 if (IS_ERR(data->base)) {
155 ret = PTR_ERR(data->base);
156 goto err_out_free_mdiobus;
159 ret = of_mdiobus_register(bus, np);
161 goto err_out_free_mdiobus;
163 platform_set_drvdata(pdev, bus);
167 err_out_free_mdiobus:
172 static int moxart_mdio_remove(struct platform_device *pdev)
174 struct mii_bus *bus = platform_get_drvdata(pdev);
176 mdiobus_unregister(bus);
182 static const struct of_device_id moxart_mdio_dt_ids[] = {
183 { .compatible = "moxa,moxart-mdio" },
186 MODULE_DEVICE_TABLE(of, moxart_mdio_dt_ids);
188 static struct platform_driver moxart_mdio_driver = {
189 .probe = moxart_mdio_probe,
190 .remove = moxart_mdio_remove,
192 .name = "moxart-mdio",
193 .of_match_table = moxart_mdio_dt_ids,
197 module_platform_driver(moxart_mdio_driver);
199 MODULE_DESCRIPTION("MOXA ART MDIO interface driver");
200 MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");
201 MODULE_LICENSE("GPL");