blob: c4eeb69a5beee6f4e4214746b9c958c614e64beb [file] [log] [blame]
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +01001/*
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
Leigh Brownd4a0acb2013-10-29 09:33:34 +00007 * 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.
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010010 *
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/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/mutex.h>
24#include <linux/phy.h>
Florian Fainelli2ec98522013-03-22 03:39:27 +000025#include <linux/interrupt.h>
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010026#include <linux/platform_device.h>
Thomas Petazzonid98a80f2012-11-19 11:39:42 +010027#include <linux/delay.h>
Florian Fainelli7111b712013-03-22 03:39:25 +000028#include <linux/io.h>
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +000029#include <linux/clk.h>
Florian Fainelli7111b712013-03-22 03:39:25 +000030#include <linux/of_mdio.h>
Florian Fainelli2ec98522013-03-22 03:39:27 +000031#include <linux/sched.h>
32#include <linux/wait.h>
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010033
34#define MVMDIO_SMI_DATA_SHIFT 0
35#define MVMDIO_SMI_PHY_ADDR_SHIFT 16
36#define MVMDIO_SMI_PHY_REG_SHIFT 21
37#define MVMDIO_SMI_READ_OPERATION BIT(26)
38#define MVMDIO_SMI_WRITE_OPERATION 0
39#define MVMDIO_SMI_READ_VALID BIT(27)
40#define MVMDIO_SMI_BUSY BIT(28)
Florian Fainelli2ec98522013-03-22 03:39:27 +000041#define MVMDIO_ERR_INT_CAUSE 0x007C
42#define MVMDIO_ERR_INT_SMI_DONE 0x00000010
43#define MVMDIO_ERR_INT_MASK 0x0080
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010044
Leigh Brownb70cd1c2013-10-29 09:33:31 +000045/*
46 * SMI Timeout measurements:
47 * - Kirkwood 88F6281 (Globalscale Dreamplug): 45us to 95us (Interrupt)
48 * - Armada 370 (Globalscale Mirabox): 41us to 43us (Polled)
49 */
50#define MVMDIO_SMI_TIMEOUT 1000 /* 1000us = 1ms */
51#define MVMDIO_SMI_POLL_INTERVAL_MIN 45
52#define MVMDIO_SMI_POLL_INTERVAL_MAX 55
53
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010054struct orion_mdio_dev {
55 struct mutex lock;
Florian Fainelli3712b712013-03-22 03:39:26 +000056 void __iomem *regs;
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +000057 struct clk *clk;
Florian Fainelli2ec98522013-03-22 03:39:27 +000058 /*
59 * If we have access to the error interrupt pin (which is
60 * somewhat misnamed as it not only reflects internal errors
61 * but also reflects SMI completion), use that to wait for
62 * SMI access completion instead of polling the SMI busy bit.
63 */
64 int err_interrupt;
65 wait_queue_head_t smi_busy_wait;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010066};
67
Florian Fainelli2ec98522013-03-22 03:39:27 +000068static int orion_mdio_smi_is_done(struct orion_mdio_dev *dev)
69{
70 return !(readl(dev->regs) & MVMDIO_SMI_BUSY);
71}
72
Thomas Petazzonib07812f2012-11-19 11:40:15 +010073/* Wait for the SMI unit to be ready for another operation
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010074 */
75static int orion_mdio_wait_ready(struct mii_bus *bus)
76{
77 struct orion_mdio_dev *dev = bus->priv;
Leigh Brownb70cd1c2013-10-29 09:33:31 +000078 unsigned long timeout = usecs_to_jiffies(MVMDIO_SMI_TIMEOUT);
79 unsigned long end = jiffies + timeout;
80 int timedout = 0;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010081
Leigh Brownb70cd1c2013-10-29 09:33:31 +000082 while (1) {
83 if (orion_mdio_smi_is_done(dev))
84 return 0;
85 else if (timedout)
86 break;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010087
Leigh Brownb70cd1c2013-10-29 09:33:31 +000088 if (dev->err_interrupt <= 0) {
89 usleep_range(MVMDIO_SMI_POLL_INTERVAL_MIN,
90 MVMDIO_SMI_POLL_INTERVAL_MAX);
Florian Fainelli2ec98522013-03-22 03:39:27 +000091
Leigh Brownb70cd1c2013-10-29 09:33:31 +000092 if (time_is_before_jiffies(end))
93 ++timedout;
94 } else {
Leigh Brown1a1f20b2013-12-19 13:09:48 +000095 /* wait_event_timeout does not guarantee a delay of at
96 * least one whole jiffie, so timeout must be no less
97 * than two.
98 */
99 if (timeout < 2)
100 timeout = 2;
Florian Fainelli2ec98522013-03-22 03:39:27 +0000101 wait_event_timeout(dev->smi_busy_wait,
Leigh Brownb70cd1c2013-10-29 09:33:31 +0000102 orion_mdio_smi_is_done(dev),
103 timeout);
104
105 ++timedout;
106 }
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100107 }
108
Leigh Brownb70cd1c2013-10-29 09:33:31 +0000109 dev_err(bus->parent, "Timeout: SMI busy for too long\n");
110 return -ETIMEDOUT;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100111}
112
113static int orion_mdio_read(struct mii_bus *bus, int mii_id,
114 int regnum)
115{
116 struct orion_mdio_dev *dev = bus->priv;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100117 u32 val;
118 int ret;
119
120 mutex_lock(&dev->lock);
121
122 ret = orion_mdio_wait_ready(bus);
Leigh Brown839f46b2013-10-29 09:33:32 +0000123 if (ret < 0)
124 goto out;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100125
126 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
127 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
128 MVMDIO_SMI_READ_OPERATION),
Florian Fainelli3712b712013-03-22 03:39:26 +0000129 dev->regs);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100130
Leigh Brown839f46b2013-10-29 09:33:32 +0000131 ret = orion_mdio_wait_ready(bus);
132 if (ret < 0)
133 goto out;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100134
Leigh Brown839f46b2013-10-29 09:33:32 +0000135 val = readl(dev->regs);
136 if (!(val & MVMDIO_SMI_READ_VALID)) {
137 dev_err(bus->parent, "SMI bus read not valid\n");
138 ret = -ENODEV;
139 goto out;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100140 }
141
Leigh Brown839f46b2013-10-29 09:33:32 +0000142 ret = val & 0xFFFF;
143out:
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100144 mutex_unlock(&dev->lock);
Leigh Brown839f46b2013-10-29 09:33:32 +0000145 return ret;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100146}
147
148static int orion_mdio_write(struct mii_bus *bus, int mii_id,
149 int regnum, u16 value)
150{
151 struct orion_mdio_dev *dev = bus->priv;
152 int ret;
153
154 mutex_lock(&dev->lock);
155
156 ret = orion_mdio_wait_ready(bus);
Leigh Brown526edcf2013-10-29 09:33:33 +0000157 if (ret < 0)
158 goto out;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100159
160 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
161 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
162 MVMDIO_SMI_WRITE_OPERATION |
163 (value << MVMDIO_SMI_DATA_SHIFT)),
Florian Fainelli3712b712013-03-22 03:39:26 +0000164 dev->regs);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100165
Leigh Brown526edcf2013-10-29 09:33:33 +0000166out:
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100167 mutex_unlock(&dev->lock);
Leigh Brown526edcf2013-10-29 09:33:33 +0000168 return ret;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100169}
170
171static int orion_mdio_reset(struct mii_bus *bus)
172{
173 return 0;
174}
175
Florian Fainelli2ec98522013-03-22 03:39:27 +0000176static irqreturn_t orion_mdio_err_irq(int irq, void *dev_id)
177{
178 struct orion_mdio_dev *dev = dev_id;
179
180 if (readl(dev->regs + MVMDIO_ERR_INT_CAUSE) &
181 MVMDIO_ERR_INT_SMI_DONE) {
182 writel(~MVMDIO_ERR_INT_SMI_DONE,
183 dev->regs + MVMDIO_ERR_INT_CAUSE);
184 wake_up(&dev->smi_busy_wait);
185 return IRQ_HANDLED;
186 }
187
188 return IRQ_NONE;
189}
190
Greg KH03ce7582012-12-21 13:42:15 +0000191static int orion_mdio_probe(struct platform_device *pdev)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100192{
Florian Fainelli7111b712013-03-22 03:39:25 +0000193 struct resource *r;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100194 struct mii_bus *bus;
195 struct orion_mdio_dev *dev;
196 int i, ret;
197
Florian Fainelli7111b712013-03-22 03:39:25 +0000198 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
199 if (!r) {
200 dev_err(&pdev->dev, "No SMI register address given\n");
201 return -ENODEV;
202 }
203
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100204 bus = mdiobus_alloc_size(sizeof(struct orion_mdio_dev));
205 if (!bus) {
206 dev_err(&pdev->dev, "Cannot allocate MDIO bus\n");
207 return -ENOMEM;
208 }
209
210 bus->name = "orion_mdio_bus";
211 bus->read = orion_mdio_read;
212 bus->write = orion_mdio_write;
213 bus->reset = orion_mdio_reset;
214 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
215 dev_name(&pdev->dev));
216 bus->parent = &pdev->dev;
217
218 bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
219 if (!bus->irq) {
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100220 mdiobus_free(bus);
221 return -ENOMEM;
222 }
223
224 for (i = 0; i < PHY_MAX_ADDR; i++)
225 bus->irq[i] = PHY_POLL;
226
227 dev = bus->priv;
Florian Fainelli3712b712013-03-22 03:39:26 +0000228 dev->regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
229 if (!dev->regs) {
Florian Fainelli7111b712013-03-22 03:39:25 +0000230 dev_err(&pdev->dev, "Unable to remap SMI register\n");
Florian Fainelli2ec98522013-03-22 03:39:27 +0000231 ret = -ENODEV;
232 goto out_mdio;
233 }
234
235 init_waitqueue_head(&dev->smi_busy_wait);
236
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +0000237 dev->clk = devm_clk_get(&pdev->dev, NULL);
238 if (!IS_ERR(dev->clk))
239 clk_prepare_enable(dev->clk);
240
Florian Fainelli2ec98522013-03-22 03:39:27 +0000241 dev->err_interrupt = platform_get_irq(pdev, 0);
242 if (dev->err_interrupt != -ENXIO) {
243 ret = devm_request_irq(&pdev->dev, dev->err_interrupt,
244 orion_mdio_err_irq,
245 IRQF_SHARED, pdev->name, dev);
246 if (ret)
247 goto out_mdio;
248
249 writel(MVMDIO_ERR_INT_SMI_DONE,
250 dev->regs + MVMDIO_ERR_INT_MASK);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100251 }
252
253 mutex_init(&dev->lock);
254
Florian Fainelli7111b712013-03-22 03:39:25 +0000255 if (pdev->dev.of_node)
256 ret = of_mdiobus_register(bus, pdev->dev.of_node);
257 else
258 ret = mdiobus_register(bus);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100259 if (ret < 0) {
260 dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
Florian Fainelli2ec98522013-03-22 03:39:27 +0000261 goto out_mdio;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100262 }
263
264 platform_set_drvdata(pdev, bus);
265
266 return 0;
Florian Fainelli2ec98522013-03-22 03:39:27 +0000267
268out_mdio:
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +0000269 if (!IS_ERR(dev->clk))
270 clk_disable_unprepare(dev->clk);
Florian Fainelli2ec98522013-03-22 03:39:27 +0000271 kfree(bus->irq);
272 mdiobus_free(bus);
273 return ret;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100274}
275
Greg KH03ce7582012-12-21 13:42:15 +0000276static int orion_mdio_remove(struct platform_device *pdev)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100277{
278 struct mii_bus *bus = platform_get_drvdata(pdev);
Florian Fainelli2ec98522013-03-22 03:39:27 +0000279 struct orion_mdio_dev *dev = bus->priv;
280
281 writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100282 mdiobus_unregister(bus);
283 kfree(bus->irq);
284 mdiobus_free(bus);
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +0000285 if (!IS_ERR(dev->clk))
286 clk_disable_unprepare(dev->clk);
287
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100288 return 0;
289}
290
291static const struct of_device_id orion_mdio_match[] = {
292 { .compatible = "marvell,orion-mdio" },
293 { }
294};
295MODULE_DEVICE_TABLE(of, orion_mdio_match);
296
297static struct platform_driver orion_mdio_driver = {
298 .probe = orion_mdio_probe,
Greg KH03ce7582012-12-21 13:42:15 +0000299 .remove = orion_mdio_remove,
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100300 .driver = {
301 .name = "orion-mdio",
302 .of_match_table = orion_mdio_match,
303 },
304};
305
306module_platform_driver(orion_mdio_driver);
307
308MODULE_DESCRIPTION("Marvell MDIO interface driver");
309MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
310MODULE_LICENSE("GPL");
Simon Baatz404b8be2013-03-24 10:33:59 +0000311MODULE_ALIAS("platform:orion-mdio");