dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / net / phy / mdio-moxart.c
blobaf3910fe8ec7d9bfb222b25c13fccea325729a0d
1 // SPDX-License-Identifier: GPL-2.0
2 /* MOXA ART Ethernet (RTL8201CP) MDIO interface driver
4 * Copyright (C) 2013 Jonas Jensen <jonas.jensen@gmail.com>
5 */
7 #include <linux/delay.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/of_address.h>
12 #include <linux/of_mdio.h>
13 #include <linux/phy.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/consumer.h>
17 #define REG_PHY_CTRL 0
18 #define REG_PHY_WRITE_DATA 4
20 /* REG_PHY_CTRL */
21 #define MIIWR BIT(27) /* init write sequence (auto cleared)*/
22 #define MIIRD BIT(26)
23 #define REGAD_MASK 0x3e00000
24 #define PHYAD_MASK 0x1f0000
25 #define MIIRDATA_MASK 0xffff
27 /* REG_PHY_WRITE_DATA */
28 #define MIIWDATA_MASK 0xffff
30 struct moxart_mdio_data {
31 void __iomem *base;
34 static int moxart_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
36 struct moxart_mdio_data *data = bus->priv;
37 u32 ctrl = 0;
38 unsigned int count = 5;
40 dev_dbg(&bus->dev, "%s\n", __func__);
42 ctrl |= MIIRD | ((mii_id << 16) & PHYAD_MASK) |
43 ((regnum << 21) & REGAD_MASK);
45 writel(ctrl, data->base + REG_PHY_CTRL);
47 do {
48 ctrl = readl(data->base + REG_PHY_CTRL);
50 if (!(ctrl & MIIRD))
51 return ctrl & MIIRDATA_MASK;
53 mdelay(10);
54 count--;
55 } while (count > 0);
57 dev_dbg(&bus->dev, "%s timed out\n", __func__);
59 return -ETIMEDOUT;
62 static int moxart_mdio_write(struct mii_bus *bus, int mii_id,
63 int regnum, u16 value)
65 struct moxart_mdio_data *data = bus->priv;
66 u32 ctrl = 0;
67 unsigned int count = 5;
69 dev_dbg(&bus->dev, "%s\n", __func__);
71 ctrl |= MIIWR | ((mii_id << 16) & PHYAD_MASK) |
72 ((regnum << 21) & REGAD_MASK);
74 value &= MIIWDATA_MASK;
76 writel(value, data->base + REG_PHY_WRITE_DATA);
77 writel(ctrl, data->base + REG_PHY_CTRL);
79 do {
80 ctrl = readl(data->base + REG_PHY_CTRL);
82 if (!(ctrl & MIIWR))
83 return 0;
85 mdelay(10);
86 count--;
87 } while (count > 0);
89 dev_dbg(&bus->dev, "%s timed out\n", __func__);
91 return -ETIMEDOUT;
94 static int moxart_mdio_reset(struct mii_bus *bus)
96 int data, i;
98 for (i = 0; i < PHY_MAX_ADDR; i++) {
99 data = moxart_mdio_read(bus, i, MII_BMCR);
100 if (data < 0)
101 continue;
103 data |= BMCR_RESET;
104 if (moxart_mdio_write(bus, i, MII_BMCR, data) < 0)
105 continue;
108 return 0;
111 static int moxart_mdio_probe(struct platform_device *pdev)
113 struct device_node *np = pdev->dev.of_node;
114 struct mii_bus *bus;
115 struct moxart_mdio_data *data;
116 struct resource *res;
117 int ret, i;
119 bus = mdiobus_alloc_size(sizeof(*data));
120 if (!bus)
121 return -ENOMEM;
123 bus->name = "MOXA ART Ethernet MII";
124 bus->read = &moxart_mdio_read;
125 bus->write = &moxart_mdio_write;
126 bus->reset = &moxart_mdio_reset;
127 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d-mii", pdev->name, pdev->id);
128 bus->parent = &pdev->dev;
130 /* Setting PHY_IGNORE_INTERRUPT here even if it has no effect,
131 * of_mdiobus_register() sets these PHY_POLL.
132 * Ideally, the interrupt from MAC controller could be used to
133 * detect link state changes, not polling, i.e. if there was
134 * a way phy_driver could set PHY_HAS_INTERRUPT but have that
135 * interrupt handled in ethernet drivercode.
137 for (i = 0; i < PHY_MAX_ADDR; i++)
138 bus->irq[i] = PHY_IGNORE_INTERRUPT;
140 data = bus->priv;
141 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
142 data->base = devm_ioremap_resource(&pdev->dev, res);
143 if (IS_ERR(data->base)) {
144 ret = PTR_ERR(data->base);
145 goto err_out_free_mdiobus;
148 ret = of_mdiobus_register(bus, np);
149 if (ret < 0)
150 goto err_out_free_mdiobus;
152 platform_set_drvdata(pdev, bus);
154 return 0;
156 err_out_free_mdiobus:
157 mdiobus_free(bus);
158 return ret;
161 static int moxart_mdio_remove(struct platform_device *pdev)
163 struct mii_bus *bus = platform_get_drvdata(pdev);
165 mdiobus_unregister(bus);
166 mdiobus_free(bus);
168 return 0;
171 static const struct of_device_id moxart_mdio_dt_ids[] = {
172 { .compatible = "moxa,moxart-mdio" },
175 MODULE_DEVICE_TABLE(of, moxart_mdio_dt_ids);
177 static struct platform_driver moxart_mdio_driver = {
178 .probe = moxart_mdio_probe,
179 .remove = moxart_mdio_remove,
180 .driver = {
181 .name = "moxart-mdio",
182 .of_match_table = moxart_mdio_dt_ids,
186 module_platform_driver(moxart_mdio_driver);
188 MODULE_DESCRIPTION("MOXA ART MDIO interface driver");
189 MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");
190 MODULE_LICENSE("GPL v2");