]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/ethernet/marvell/mvmdio.c
fe6072aae0a67ba2c91cdc6571285fcd35b3db17
[mirror_ubuntu-artful-kernel.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
8 * the MDIO bus). This driver is currently used by the mvneta and
9 * mv643xx_eth drivers.
10 *
11 * Copyright (C) 2012 Marvell
12 *
13 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
14 *
15 * This file is licensed under the terms of the GNU General Public
16 * License version 2. This program is licensed "as is" without any
17 * warranty of any kind, whether express or implied.
18 */
19
20 #include <linux/clk.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/of_mdio.h>
27 #include <linux/phy.h>
28 #include <linux/platform_device.h>
29 #include <linux/sched.h>
30 #include <linux/wait.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 #define MVMDIO_ERR_INT_CAUSE 0x007C
40 #define MVMDIO_ERR_INT_SMI_DONE 0x00000010
41 #define MVMDIO_ERR_INT_MASK 0x0080
42
43 /*
44 * SMI Timeout measurements:
45 * - Kirkwood 88F6281 (Globalscale Dreamplug): 45us to 95us (Interrupt)
46 * - Armada 370 (Globalscale Mirabox): 41us to 43us (Polled)
47 */
48 #define MVMDIO_SMI_TIMEOUT 1000 /* 1000us = 1ms */
49 #define MVMDIO_SMI_POLL_INTERVAL_MIN 45
50 #define MVMDIO_SMI_POLL_INTERVAL_MAX 55
51
52 struct orion_mdio_dev {
53 void __iomem *regs;
54 struct clk *clk[3];
55 /*
56 * If we have access to the error interrupt pin (which is
57 * somewhat misnamed as it not only reflects internal errors
58 * but also reflects SMI completion), use that to wait for
59 * SMI access completion instead of polling the SMI busy bit.
60 */
61 int err_interrupt;
62 wait_queue_head_t smi_busy_wait;
63 };
64
65 struct orion_mdio_ops {
66 int (*is_done)(struct orion_mdio_dev *);
67 unsigned int poll_interval_min;
68 unsigned int poll_interval_max;
69 };
70
71 /* Wait for the SMI unit to be ready for another operation
72 */
73 static int orion_mdio_wait_ready(const struct orion_mdio_ops *ops,
74 struct mii_bus *bus)
75 {
76 struct orion_mdio_dev *dev = bus->priv;
77 unsigned long timeout = usecs_to_jiffies(MVMDIO_SMI_TIMEOUT);
78 unsigned long end = jiffies + timeout;
79 int timedout = 0;
80
81 while (1) {
82 if (ops->is_done(dev))
83 return 0;
84 else if (timedout)
85 break;
86
87 if (dev->err_interrupt <= 0) {
88 usleep_range(ops->poll_interval_min,
89 ops->poll_interval_max);
90
91 if (time_is_before_jiffies(end))
92 ++timedout;
93 } else {
94 /* wait_event_timeout does not guarantee a delay of at
95 * least one whole jiffie, so timeout must be no less
96 * than two.
97 */
98 if (timeout < 2)
99 timeout = 2;
100 wait_event_timeout(dev->smi_busy_wait,
101 ops->is_done(dev), timeout);
102
103 ++timedout;
104 }
105 }
106
107 dev_err(bus->parent, "Timeout: SMI busy for too long\n");
108 return -ETIMEDOUT;
109 }
110
111 static int orion_mdio_smi_is_done(struct orion_mdio_dev *dev)
112 {
113 return !(readl(dev->regs) & MVMDIO_SMI_BUSY);
114 }
115
116 static const struct orion_mdio_ops orion_mdio_smi_ops = {
117 .is_done = orion_mdio_smi_is_done,
118 .poll_interval_min = MVMDIO_SMI_POLL_INTERVAL_MIN,
119 .poll_interval_max = MVMDIO_SMI_POLL_INTERVAL_MAX,
120 };
121
122 static int orion_mdio_read(struct mii_bus *bus, int mii_id,
123 int regnum)
124 {
125 struct orion_mdio_dev *dev = bus->priv;
126 u32 val;
127 int ret;
128
129 if (regnum & MII_ADDR_C45)
130 return -EOPNOTSUPP;
131
132 ret = orion_mdio_wait_ready(&orion_mdio_smi_ops, bus);
133 if (ret < 0)
134 goto out;
135
136 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
137 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
138 MVMDIO_SMI_READ_OPERATION),
139 dev->regs);
140
141 ret = orion_mdio_wait_ready(&orion_mdio_smi_ops, bus);
142 if (ret < 0)
143 goto out;
144
145 val = readl(dev->regs);
146 if (!(val & MVMDIO_SMI_READ_VALID)) {
147 dev_err(bus->parent, "SMI bus read not valid\n");
148 ret = -ENODEV;
149 goto out;
150 }
151
152 ret = val & GENMASK(15, 0);
153 out:
154 return ret;
155 }
156
157 static int orion_mdio_write(struct mii_bus *bus, int mii_id,
158 int regnum, u16 value)
159 {
160 struct orion_mdio_dev *dev = bus->priv;
161 int ret;
162
163 if (regnum & MII_ADDR_C45)
164 return -EOPNOTSUPP;
165
166 ret = orion_mdio_wait_ready(&orion_mdio_smi_ops, bus);
167 if (ret < 0)
168 goto out;
169
170 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
171 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
172 MVMDIO_SMI_WRITE_OPERATION |
173 (value << MVMDIO_SMI_DATA_SHIFT)),
174 dev->regs);
175
176 out:
177 return ret;
178 }
179
180 static irqreturn_t orion_mdio_err_irq(int irq, void *dev_id)
181 {
182 struct orion_mdio_dev *dev = dev_id;
183
184 if (readl(dev->regs + MVMDIO_ERR_INT_CAUSE) &
185 MVMDIO_ERR_INT_SMI_DONE) {
186 writel(~MVMDIO_ERR_INT_SMI_DONE,
187 dev->regs + MVMDIO_ERR_INT_CAUSE);
188 wake_up(&dev->smi_busy_wait);
189 return IRQ_HANDLED;
190 }
191
192 return IRQ_NONE;
193 }
194
195 static int orion_mdio_probe(struct platform_device *pdev)
196 {
197 struct resource *r;
198 struct mii_bus *bus;
199 struct orion_mdio_dev *dev;
200 int i, ret;
201
202 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
203 if (!r) {
204 dev_err(&pdev->dev, "No SMI register address given\n");
205 return -ENODEV;
206 }
207
208 bus = devm_mdiobus_alloc_size(&pdev->dev,
209 sizeof(struct orion_mdio_dev));
210 if (!bus)
211 return -ENOMEM;
212
213 bus->name = "orion_mdio_bus";
214 bus->read = orion_mdio_read;
215 bus->write = orion_mdio_write;
216 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
217 dev_name(&pdev->dev));
218 bus->parent = &pdev->dev;
219
220 dev = bus->priv;
221 dev->regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
222 if (!dev->regs) {
223 dev_err(&pdev->dev, "Unable to remap SMI register\n");
224 return -ENODEV;
225 }
226
227 init_waitqueue_head(&dev->smi_busy_wait);
228
229 for (i = 0; i < ARRAY_SIZE(dev->clk); i++) {
230 dev->clk[i] = of_clk_get(pdev->dev.of_node, i);
231 if (IS_ERR(dev->clk[i]))
232 break;
233 clk_prepare_enable(dev->clk[i]);
234 }
235
236 dev->err_interrupt = platform_get_irq(pdev, 0);
237 if (dev->err_interrupt > 0 &&
238 resource_size(r) < MVMDIO_ERR_INT_MASK + 4) {
239 dev_err(&pdev->dev,
240 "disabling interrupt, resource size is too small\n");
241 dev->err_interrupt = 0;
242 }
243 if (dev->err_interrupt > 0) {
244 ret = devm_request_irq(&pdev->dev, dev->err_interrupt,
245 orion_mdio_err_irq,
246 IRQF_SHARED, pdev->name, dev);
247 if (ret)
248 goto out_mdio;
249
250 writel(MVMDIO_ERR_INT_SMI_DONE,
251 dev->regs + MVMDIO_ERR_INT_MASK);
252
253 } else if (dev->err_interrupt == -EPROBE_DEFER) {
254 return -EPROBE_DEFER;
255 }
256
257 if (pdev->dev.of_node)
258 ret = of_mdiobus_register(bus, pdev->dev.of_node);
259 else
260 ret = mdiobus_register(bus);
261 if (ret < 0) {
262 dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
263 goto out_mdio;
264 }
265
266 platform_set_drvdata(pdev, bus);
267
268 return 0;
269
270 out_mdio:
271 if (dev->err_interrupt > 0)
272 writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
273
274 for (i = 0; i < ARRAY_SIZE(dev->clk); i++) {
275 if (IS_ERR(dev->clk[i]))
276 break;
277 clk_disable_unprepare(dev->clk[i]);
278 clk_put(dev->clk[i]);
279 }
280
281 return ret;
282 }
283
284 static int orion_mdio_remove(struct platform_device *pdev)
285 {
286 struct mii_bus *bus = platform_get_drvdata(pdev);
287 struct orion_mdio_dev *dev = bus->priv;
288 int i;
289
290 if (dev->err_interrupt > 0)
291 writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
292 mdiobus_unregister(bus);
293
294 for (i = 0; i < ARRAY_SIZE(dev->clk); i++) {
295 if (IS_ERR(dev->clk[i]))
296 break;
297 clk_disable_unprepare(dev->clk[i]);
298 clk_put(dev->clk[i]);
299 }
300
301 return 0;
302 }
303
304 static const struct of_device_id orion_mdio_match[] = {
305 { .compatible = "marvell,orion-mdio" },
306 { }
307 };
308 MODULE_DEVICE_TABLE(of, orion_mdio_match);
309
310 static struct platform_driver orion_mdio_driver = {
311 .probe = orion_mdio_probe,
312 .remove = orion_mdio_remove,
313 .driver = {
314 .name = "orion-mdio",
315 .of_match_table = orion_mdio_match,
316 },
317 };
318
319 module_platform_driver(orion_mdio_driver);
320
321 MODULE_DESCRIPTION("Marvell MDIO interface driver");
322 MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
323 MODULE_LICENSE("GPL");
324 MODULE_ALIAS("platform:orion-mdio");