net: mvmdio: use <linux/delay.h> instead of <asm/delay.h>
[deliverable/linux.git] / drivers / net / ethernet / marvell / mvmdio.c
1 /*
2 * Driver for the MDIO interface of Marvell network interfaces.
3 *
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.
12 *
13 * Copyright (C) 2012 Marvell
14 *
15 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
16 *
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.
20 */
21
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>
31
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)
39
40 struct orion_mdio_dev {
41 struct mutex lock;
42 void __iomem *smireg;
43 };
44
45 /*
46 * Wait for the SMI unit to be ready for another operation
47 */
48 static int orion_mdio_wait_ready(struct mii_bus *bus)
49 {
50 struct orion_mdio_dev *dev = bus->priv;
51 int count;
52 u32 val;
53
54 count = 0;
55 while (1) {
56 val = readl(dev->smireg);
57 if (!(val & MVMDIO_SMI_BUSY))
58 break;
59
60 if (count > 100) {
61 dev_err(bus->parent, "Timeout: SMI busy for too long\n");
62 return -ETIMEDOUT;
63 }
64
65 udelay(10);
66 count++;
67 }
68
69 return 0;
70 }
71
72 static int orion_mdio_read(struct mii_bus *bus, int mii_id,
73 int regnum)
74 {
75 struct orion_mdio_dev *dev = bus->priv;
76 int count;
77 u32 val;
78 int ret;
79
80 mutex_lock(&dev->lock);
81
82 ret = orion_mdio_wait_ready(bus);
83 if (ret < 0) {
84 mutex_unlock(&dev->lock);
85 return ret;
86 }
87
88 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
89 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
90 MVMDIO_SMI_READ_OPERATION),
91 dev->smireg);
92
93 /* Wait for the value to become available */
94 count = 0;
95 while (1) {
96 val = readl(dev->smireg);
97 if (val & MVMDIO_SMI_READ_VALID)
98 break;
99
100 if (count > 100) {
101 dev_err(bus->parent, "Timeout when reading PHY\n");
102 mutex_unlock(&dev->lock);
103 return -ETIMEDOUT;
104 }
105
106 udelay(10);
107 count++;
108 }
109
110 mutex_unlock(&dev->lock);
111
112 return val & 0xFFFF;
113 }
114
115 static int orion_mdio_write(struct mii_bus *bus, int mii_id,
116 int regnum, u16 value)
117 {
118 struct orion_mdio_dev *dev = bus->priv;
119 int ret;
120
121 mutex_lock(&dev->lock);
122
123 ret = orion_mdio_wait_ready(bus);
124 if (ret < 0) {
125 mutex_unlock(&dev->lock);
126 return ret;
127 }
128
129 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
130 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
131 MVMDIO_SMI_WRITE_OPERATION |
132 (value << MVMDIO_SMI_DATA_SHIFT)),
133 dev->smireg);
134
135 mutex_unlock(&dev->lock);
136
137 return 0;
138 }
139
140 static int orion_mdio_reset(struct mii_bus *bus)
141 {
142 return 0;
143 }
144
145 static int __devinit orion_mdio_probe(struct platform_device *pdev)
146 {
147 struct device_node *np = pdev->dev.of_node;
148 struct mii_bus *bus;
149 struct orion_mdio_dev *dev;
150 int i, ret;
151
152 bus = mdiobus_alloc_size(sizeof(struct orion_mdio_dev));
153 if (!bus) {
154 dev_err(&pdev->dev, "Cannot allocate MDIO bus\n");
155 return -ENOMEM;
156 }
157
158 bus->name = "orion_mdio_bus";
159 bus->read = orion_mdio_read;
160 bus->write = orion_mdio_write;
161 bus->reset = orion_mdio_reset;
162 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
163 dev_name(&pdev->dev));
164 bus->parent = &pdev->dev;
165
166 bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
167 if (!bus->irq) {
168 dev_err(&pdev->dev, "Cannot allocate PHY IRQ array\n");
169 mdiobus_free(bus);
170 return -ENOMEM;
171 }
172
173 for (i = 0; i < PHY_MAX_ADDR; i++)
174 bus->irq[i] = PHY_POLL;
175
176 dev = bus->priv;
177 dev->smireg = of_iomap(pdev->dev.of_node, 0);
178 if (!dev->smireg) {
179 dev_err(&pdev->dev, "No SMI register address given in DT\n");
180 kfree(bus->irq);
181 mdiobus_free(bus);
182 return -ENODEV;
183 }
184
185 mutex_init(&dev->lock);
186
187 ret = of_mdiobus_register(bus, np);
188 if (ret < 0) {
189 dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
190 iounmap(dev->smireg);
191 kfree(bus->irq);
192 mdiobus_free(bus);
193 return ret;
194 }
195
196 platform_set_drvdata(pdev, bus);
197
198 return 0;
199 }
200
201 static int __devexit orion_mdio_remove(struct platform_device *pdev)
202 {
203 struct mii_bus *bus = platform_get_drvdata(pdev);
204 mdiobus_unregister(bus);
205 kfree(bus->irq);
206 mdiobus_free(bus);
207 return 0;
208 }
209
210 static const struct of_device_id orion_mdio_match[] = {
211 { .compatible = "marvell,orion-mdio" },
212 { }
213 };
214 MODULE_DEVICE_TABLE(of, orion_mdio_match);
215
216 static struct platform_driver orion_mdio_driver = {
217 .probe = orion_mdio_probe,
218 .remove = __devexit_p(orion_mdio_remove),
219 .driver = {
220 .name = "orion-mdio",
221 .of_match_table = orion_mdio_match,
222 },
223 };
224
225 module_platform_driver(orion_mdio_driver);
226
227 MODULE_DESCRIPTION("Marvell MDIO interface driver");
228 MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
229 MODULE_LICENSE("GPL");
This page took 0.035724 seconds and 5 git commands to generate.