blob: a0d1b084ecec9820aead869a281289a2d4249d9c [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
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010020#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/mutex.h>
23#include <linux/phy.h>
Florian Fainelli2ec98522013-03-22 03:39:27 +000024#include <linux/interrupt.h>
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010025#include <linux/platform_device.h>
Thomas Petazzonid98a80f2012-11-19 11:39:42 +010026#include <linux/delay.h>
Florian Fainelli7111b712013-03-22 03:39:25 +000027#include <linux/io.h>
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +000028#include <linux/clk.h>
Florian Fainelli7111b712013-03-22 03:39:25 +000029#include <linux/of_mdio.h>
Florian Fainelli2ec98522013-03-22 03:39:27 +000030#include <linux/sched.h>
31#include <linux/wait.h>
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010032
33#define MVMDIO_SMI_DATA_SHIFT 0
34#define MVMDIO_SMI_PHY_ADDR_SHIFT 16
35#define MVMDIO_SMI_PHY_REG_SHIFT 21
36#define MVMDIO_SMI_READ_OPERATION BIT(26)
37#define MVMDIO_SMI_WRITE_OPERATION 0
38#define MVMDIO_SMI_READ_VALID BIT(27)
39#define MVMDIO_SMI_BUSY BIT(28)
Florian Fainelli2ec98522013-03-22 03:39:27 +000040#define MVMDIO_ERR_INT_CAUSE 0x007C
41#define MVMDIO_ERR_INT_SMI_DONE 0x00000010
42#define MVMDIO_ERR_INT_MASK 0x0080
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010043
Leigh Brownb70cd1c2013-10-29 09:33:31 +000044/*
45 * SMI Timeout measurements:
46 * - Kirkwood 88F6281 (Globalscale Dreamplug): 45us to 95us (Interrupt)
47 * - Armada 370 (Globalscale Mirabox): 41us to 43us (Polled)
48 */
49#define MVMDIO_SMI_TIMEOUT 1000 /* 1000us = 1ms */
50#define MVMDIO_SMI_POLL_INTERVAL_MIN 45
51#define MVMDIO_SMI_POLL_INTERVAL_MAX 55
52
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010053struct orion_mdio_dev {
54 struct mutex lock;
Florian Fainelli3712b712013-03-22 03:39:26 +000055 void __iomem *regs;
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +000056 struct clk *clk;
Florian Fainelli2ec98522013-03-22 03:39:27 +000057 /*
58 * If we have access to the error interrupt pin (which is
59 * somewhat misnamed as it not only reflects internal errors
60 * but also reflects SMI completion), use that to wait for
61 * SMI access completion instead of polling the SMI busy bit.
62 */
63 int err_interrupt;
64 wait_queue_head_t smi_busy_wait;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010065};
66
Florian Fainelli2ec98522013-03-22 03:39:27 +000067static int orion_mdio_smi_is_done(struct orion_mdio_dev *dev)
68{
69 return !(readl(dev->regs) & MVMDIO_SMI_BUSY);
70}
71
Thomas Petazzonib07812f2012-11-19 11:40:15 +010072/* Wait for the SMI unit to be ready for another operation
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010073 */
74static int orion_mdio_wait_ready(struct mii_bus *bus)
75{
76 struct orion_mdio_dev *dev = bus->priv;
Leigh Brownb70cd1c2013-10-29 09:33:31 +000077 unsigned long timeout = usecs_to_jiffies(MVMDIO_SMI_TIMEOUT);
78 unsigned long end = jiffies + timeout;
79 int timedout = 0;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010080
Leigh Brownb70cd1c2013-10-29 09:33:31 +000081 while (1) {
82 if (orion_mdio_smi_is_done(dev))
83 return 0;
84 else if (timedout)
85 break;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010086
Leigh Brownb70cd1c2013-10-29 09:33:31 +000087 if (dev->err_interrupt <= 0) {
88 usleep_range(MVMDIO_SMI_POLL_INTERVAL_MIN,
89 MVMDIO_SMI_POLL_INTERVAL_MAX);
Florian Fainelli2ec98522013-03-22 03:39:27 +000090
Leigh Brownb70cd1c2013-10-29 09:33:31 +000091 if (time_is_before_jiffies(end))
92 ++timedout;
93 } else {
Leigh Brown1a1f20b2013-12-19 13:09:48 +000094 /* 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;
Florian Fainelli2ec98522013-03-22 03:39:27 +0000100 wait_event_timeout(dev->smi_busy_wait,
Leigh Brownb70cd1c2013-10-29 09:33:31 +0000101 orion_mdio_smi_is_done(dev),
102 timeout);
103
104 ++timedout;
105 }
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100106 }
107
Leigh Brownb70cd1c2013-10-29 09:33:31 +0000108 dev_err(bus->parent, "Timeout: SMI busy for too long\n");
109 return -ETIMEDOUT;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100110}
111
112static int orion_mdio_read(struct mii_bus *bus, int mii_id,
113 int regnum)
114{
115 struct orion_mdio_dev *dev = bus->priv;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100116 u32 val;
117 int ret;
118
119 mutex_lock(&dev->lock);
120
121 ret = orion_mdio_wait_ready(bus);
Leigh Brown839f46b2013-10-29 09:33:32 +0000122 if (ret < 0)
123 goto out;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100124
125 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
126 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
127 MVMDIO_SMI_READ_OPERATION),
Florian Fainelli3712b712013-03-22 03:39:26 +0000128 dev->regs);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100129
Leigh Brown839f46b2013-10-29 09:33:32 +0000130 ret = orion_mdio_wait_ready(bus);
131 if (ret < 0)
132 goto out;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100133
Leigh Brown839f46b2013-10-29 09:33:32 +0000134 val = readl(dev->regs);
135 if (!(val & MVMDIO_SMI_READ_VALID)) {
136 dev_err(bus->parent, "SMI bus read not valid\n");
137 ret = -ENODEV;
138 goto out;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100139 }
140
Leigh Brown839f46b2013-10-29 09:33:32 +0000141 ret = val & 0xFFFF;
142out:
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100143 mutex_unlock(&dev->lock);
Leigh Brown839f46b2013-10-29 09:33:32 +0000144 return ret;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100145}
146
147static int orion_mdio_write(struct mii_bus *bus, int mii_id,
148 int regnum, u16 value)
149{
150 struct orion_mdio_dev *dev = bus->priv;
151 int ret;
152
153 mutex_lock(&dev->lock);
154
155 ret = orion_mdio_wait_ready(bus);
Leigh Brown526edcf2013-10-29 09:33:33 +0000156 if (ret < 0)
157 goto out;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100158
159 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
160 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
161 MVMDIO_SMI_WRITE_OPERATION |
162 (value << MVMDIO_SMI_DATA_SHIFT)),
Florian Fainelli3712b712013-03-22 03:39:26 +0000163 dev->regs);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100164
Leigh Brown526edcf2013-10-29 09:33:33 +0000165out:
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100166 mutex_unlock(&dev->lock);
Leigh Brown526edcf2013-10-29 09:33:33 +0000167 return ret;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100168}
169
Florian Fainelli2ec98522013-03-22 03:39:27 +0000170static irqreturn_t orion_mdio_err_irq(int irq, void *dev_id)
171{
172 struct orion_mdio_dev *dev = dev_id;
173
174 if (readl(dev->regs + MVMDIO_ERR_INT_CAUSE) &
175 MVMDIO_ERR_INT_SMI_DONE) {
176 writel(~MVMDIO_ERR_INT_SMI_DONE,
177 dev->regs + MVMDIO_ERR_INT_CAUSE);
178 wake_up(&dev->smi_busy_wait);
179 return IRQ_HANDLED;
180 }
181
182 return IRQ_NONE;
183}
184
Greg KH03ce7582012-12-21 13:42:15 +0000185static int orion_mdio_probe(struct platform_device *pdev)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100186{
Florian Fainelli7111b712013-03-22 03:39:25 +0000187 struct resource *r;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100188 struct mii_bus *bus;
189 struct orion_mdio_dev *dev;
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100190 int ret;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100191
Florian Fainelli7111b712013-03-22 03:39:25 +0000192 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193 if (!r) {
194 dev_err(&pdev->dev, "No SMI register address given\n");
195 return -ENODEV;
196 }
197
Ezequiel Garcia56ecd2c2014-05-22 20:07:02 -0300198 bus = devm_mdiobus_alloc_size(&pdev->dev,
199 sizeof(struct orion_mdio_dev));
200 if (!bus)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100201 return -ENOMEM;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100202
203 bus->name = "orion_mdio_bus";
204 bus->read = orion_mdio_read;
205 bus->write = orion_mdio_write;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100206 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
207 dev_name(&pdev->dev));
208 bus->parent = &pdev->dev;
209
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100210 dev = bus->priv;
Florian Fainelli3712b712013-03-22 03:39:26 +0000211 dev->regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
212 if (!dev->regs) {
Florian Fainelli7111b712013-03-22 03:39:25 +0000213 dev_err(&pdev->dev, "Unable to remap SMI register\n");
Alexey Khoroshilovf814bfd2016-10-01 00:56:37 +0300214 return -ENODEV;
Florian Fainelli2ec98522013-03-22 03:39:27 +0000215 }
216
217 init_waitqueue_head(&dev->smi_busy_wait);
218
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +0000219 dev->clk = devm_clk_get(&pdev->dev, NULL);
220 if (!IS_ERR(dev->clk))
221 clk_prepare_enable(dev->clk);
222
Florian Fainelli2ec98522013-03-22 03:39:27 +0000223 dev->err_interrupt = platform_get_irq(pdev, 0);
Ezequiel Garcia39076b02014-04-30 13:28:51 -0300224 if (dev->err_interrupt > 0) {
Florian Fainelli2ec98522013-03-22 03:39:27 +0000225 ret = devm_request_irq(&pdev->dev, dev->err_interrupt,
226 orion_mdio_err_irq,
227 IRQF_SHARED, pdev->name, dev);
228 if (ret)
229 goto out_mdio;
230
231 writel(MVMDIO_ERR_INT_SMI_DONE,
232 dev->regs + MVMDIO_ERR_INT_MASK);
Ezequiel Garcia39076b02014-04-30 13:28:51 -0300233
234 } else if (dev->err_interrupt == -EPROBE_DEFER) {
235 return -EPROBE_DEFER;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100236 }
237
238 mutex_init(&dev->lock);
239
Florian Fainelli7111b712013-03-22 03:39:25 +0000240 if (pdev->dev.of_node)
241 ret = of_mdiobus_register(bus, pdev->dev.of_node);
242 else
243 ret = mdiobus_register(bus);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100244 if (ret < 0) {
245 dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
Florian Fainelli2ec98522013-03-22 03:39:27 +0000246 goto out_mdio;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100247 }
248
249 platform_set_drvdata(pdev, bus);
250
251 return 0;
Florian Fainelli2ec98522013-03-22 03:39:27 +0000252
253out_mdio:
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +0000254 if (!IS_ERR(dev->clk))
255 clk_disable_unprepare(dev->clk);
Florian Fainelli2ec98522013-03-22 03:39:27 +0000256 return ret;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100257}
258
Greg KH03ce7582012-12-21 13:42:15 +0000259static int orion_mdio_remove(struct platform_device *pdev)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100260{
261 struct mii_bus *bus = platform_get_drvdata(pdev);
Florian Fainelli2ec98522013-03-22 03:39:27 +0000262 struct orion_mdio_dev *dev = bus->priv;
263
264 writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100265 mdiobus_unregister(bus);
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +0000266 if (!IS_ERR(dev->clk))
267 clk_disable_unprepare(dev->clk);
268
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100269 return 0;
270}
271
272static const struct of_device_id orion_mdio_match[] = {
273 { .compatible = "marvell,orion-mdio" },
274 { }
275};
276MODULE_DEVICE_TABLE(of, orion_mdio_match);
277
278static struct platform_driver orion_mdio_driver = {
279 .probe = orion_mdio_probe,
Greg KH03ce7582012-12-21 13:42:15 +0000280 .remove = orion_mdio_remove,
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100281 .driver = {
282 .name = "orion-mdio",
283 .of_match_table = orion_mdio_match,
284 },
285};
286
287module_platform_driver(orion_mdio_driver);
288
289MODULE_DESCRIPTION("Marvell MDIO interface driver");
290MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
291MODULE_LICENSE("GPL");
Simon Baatz404b8be2013-03-24 10:33:59 +0000292MODULE_ALIAS("platform:orion-mdio");