blob: 971a4c1bbbaa721cb43b5178fc9963cabc177c80 [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
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>
Florian Fainelli2ec98522013-03-22 03:39:27 +000027#include <linux/interrupt.h>
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010028#include <linux/platform_device.h>
Thomas Petazzonid98a80f2012-11-19 11:39:42 +010029#include <linux/delay.h>
Florian Fainelli7111b712013-03-22 03:39:25 +000030#include <linux/io.h>
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +000031#include <linux/clk.h>
Florian Fainelli7111b712013-03-22 03:39:25 +000032#include <linux/of_mdio.h>
Florian Fainelli2ec98522013-03-22 03:39:27 +000033#include <linux/sched.h>
34#include <linux/wait.h>
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010035
36#define MVMDIO_SMI_DATA_SHIFT 0
37#define MVMDIO_SMI_PHY_ADDR_SHIFT 16
38#define MVMDIO_SMI_PHY_REG_SHIFT 21
39#define MVMDIO_SMI_READ_OPERATION BIT(26)
40#define MVMDIO_SMI_WRITE_OPERATION 0
41#define MVMDIO_SMI_READ_VALID BIT(27)
42#define MVMDIO_SMI_BUSY BIT(28)
Florian Fainelli2ec98522013-03-22 03:39:27 +000043#define MVMDIO_ERR_INT_CAUSE 0x007C
44#define MVMDIO_ERR_INT_SMI_DONE 0x00000010
45#define MVMDIO_ERR_INT_MASK 0x0080
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010046
Leigh Brownb70cd1c2013-10-29 09:33:31 +000047/*
48 * SMI Timeout measurements:
49 * - Kirkwood 88F6281 (Globalscale Dreamplug): 45us to 95us (Interrupt)
50 * - Armada 370 (Globalscale Mirabox): 41us to 43us (Polled)
51 */
52#define MVMDIO_SMI_TIMEOUT 1000 /* 1000us = 1ms */
53#define MVMDIO_SMI_POLL_INTERVAL_MIN 45
54#define MVMDIO_SMI_POLL_INTERVAL_MAX 55
55
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010056struct orion_mdio_dev {
57 struct mutex lock;
Florian Fainelli3712b712013-03-22 03:39:26 +000058 void __iomem *regs;
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +000059 struct clk *clk;
Florian Fainelli2ec98522013-03-22 03:39:27 +000060 /*
61 * If we have access to the error interrupt pin (which is
62 * somewhat misnamed as it not only reflects internal errors
63 * but also reflects SMI completion), use that to wait for
64 * SMI access completion instead of polling the SMI busy bit.
65 */
66 int err_interrupt;
67 wait_queue_head_t smi_busy_wait;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010068};
69
Florian Fainelli2ec98522013-03-22 03:39:27 +000070static int orion_mdio_smi_is_done(struct orion_mdio_dev *dev)
71{
72 return !(readl(dev->regs) & MVMDIO_SMI_BUSY);
73}
74
Thomas Petazzonib07812f2012-11-19 11:40:15 +010075/* Wait for the SMI unit to be ready for another operation
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010076 */
77static int orion_mdio_wait_ready(struct mii_bus *bus)
78{
79 struct orion_mdio_dev *dev = bus->priv;
Leigh Brownb70cd1c2013-10-29 09:33:31 +000080 unsigned long timeout = usecs_to_jiffies(MVMDIO_SMI_TIMEOUT);
81 unsigned long end = jiffies + timeout;
82 int timedout = 0;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010083
Leigh Brownb70cd1c2013-10-29 09:33:31 +000084 while (1) {
85 if (orion_mdio_smi_is_done(dev))
86 return 0;
87 else if (timedout)
88 break;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010089
Leigh Brownb70cd1c2013-10-29 09:33:31 +000090 if (dev->err_interrupt <= 0) {
91 usleep_range(MVMDIO_SMI_POLL_INTERVAL_MIN,
92 MVMDIO_SMI_POLL_INTERVAL_MAX);
Florian Fainelli2ec98522013-03-22 03:39:27 +000093
Leigh Brownb70cd1c2013-10-29 09:33:31 +000094 if (time_is_before_jiffies(end))
95 ++timedout;
96 } else {
Florian Fainelli2ec98522013-03-22 03:39:27 +000097 wait_event_timeout(dev->smi_busy_wait,
Leigh Brownb70cd1c2013-10-29 09:33:31 +000098 orion_mdio_smi_is_done(dev),
99 timeout);
100
101 ++timedout;
102 }
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100103 }
104
Leigh Brownb70cd1c2013-10-29 09:33:31 +0000105 dev_err(bus->parent, "Timeout: SMI busy for too long\n");
106 return -ETIMEDOUT;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100107}
108
109static int orion_mdio_read(struct mii_bus *bus, int mii_id,
110 int regnum)
111{
112 struct orion_mdio_dev *dev = bus->priv;
113 int count;
114 u32 val;
115 int ret;
116
117 mutex_lock(&dev->lock);
118
119 ret = orion_mdio_wait_ready(bus);
120 if (ret < 0) {
121 mutex_unlock(&dev->lock);
122 return ret;
123 }
124
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
130 /* Wait for the value to become available */
131 count = 0;
132 while (1) {
Florian Fainelli3712b712013-03-22 03:39:26 +0000133 val = readl(dev->regs);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100134 if (val & MVMDIO_SMI_READ_VALID)
135 break;
136
137 if (count > 100) {
138 dev_err(bus->parent, "Timeout when reading PHY\n");
139 mutex_unlock(&dev->lock);
140 return -ETIMEDOUT;
141 }
142
143 udelay(10);
144 count++;
145 }
146
147 mutex_unlock(&dev->lock);
148
149 return val & 0xFFFF;
150}
151
152static int orion_mdio_write(struct mii_bus *bus, int mii_id,
153 int regnum, u16 value)
154{
155 struct orion_mdio_dev *dev = bus->priv;
156 int ret;
157
158 mutex_lock(&dev->lock);
159
160 ret = orion_mdio_wait_ready(bus);
161 if (ret < 0) {
162 mutex_unlock(&dev->lock);
163 return ret;
164 }
165
166 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
167 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
168 MVMDIO_SMI_WRITE_OPERATION |
169 (value << MVMDIO_SMI_DATA_SHIFT)),
Florian Fainelli3712b712013-03-22 03:39:26 +0000170 dev->regs);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100171
172 mutex_unlock(&dev->lock);
173
174 return 0;
175}
176
177static int orion_mdio_reset(struct mii_bus *bus)
178{
179 return 0;
180}
181
Florian Fainelli2ec98522013-03-22 03:39:27 +0000182static irqreturn_t orion_mdio_err_irq(int irq, void *dev_id)
183{
184 struct orion_mdio_dev *dev = dev_id;
185
186 if (readl(dev->regs + MVMDIO_ERR_INT_CAUSE) &
187 MVMDIO_ERR_INT_SMI_DONE) {
188 writel(~MVMDIO_ERR_INT_SMI_DONE,
189 dev->regs + MVMDIO_ERR_INT_CAUSE);
190 wake_up(&dev->smi_busy_wait);
191 return IRQ_HANDLED;
192 }
193
194 return IRQ_NONE;
195}
196
Greg KH03ce7582012-12-21 13:42:15 +0000197static int orion_mdio_probe(struct platform_device *pdev)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100198{
Florian Fainelli7111b712013-03-22 03:39:25 +0000199 struct resource *r;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100200 struct mii_bus *bus;
201 struct orion_mdio_dev *dev;
202 int i, ret;
203
Florian Fainelli7111b712013-03-22 03:39:25 +0000204 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
205 if (!r) {
206 dev_err(&pdev->dev, "No SMI register address given\n");
207 return -ENODEV;
208 }
209
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100210 bus = mdiobus_alloc_size(sizeof(struct orion_mdio_dev));
211 if (!bus) {
212 dev_err(&pdev->dev, "Cannot allocate MDIO bus\n");
213 return -ENOMEM;
214 }
215
216 bus->name = "orion_mdio_bus";
217 bus->read = orion_mdio_read;
218 bus->write = orion_mdio_write;
219 bus->reset = orion_mdio_reset;
220 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
221 dev_name(&pdev->dev));
222 bus->parent = &pdev->dev;
223
224 bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
225 if (!bus->irq) {
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100226 mdiobus_free(bus);
227 return -ENOMEM;
228 }
229
230 for (i = 0; i < PHY_MAX_ADDR; i++)
231 bus->irq[i] = PHY_POLL;
232
233 dev = bus->priv;
Florian Fainelli3712b712013-03-22 03:39:26 +0000234 dev->regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
235 if (!dev->regs) {
Florian Fainelli7111b712013-03-22 03:39:25 +0000236 dev_err(&pdev->dev, "Unable to remap SMI register\n");
Florian Fainelli2ec98522013-03-22 03:39:27 +0000237 ret = -ENODEV;
238 goto out_mdio;
239 }
240
241 init_waitqueue_head(&dev->smi_busy_wait);
242
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +0000243 dev->clk = devm_clk_get(&pdev->dev, NULL);
244 if (!IS_ERR(dev->clk))
245 clk_prepare_enable(dev->clk);
246
Florian Fainelli2ec98522013-03-22 03:39:27 +0000247 dev->err_interrupt = platform_get_irq(pdev, 0);
248 if (dev->err_interrupt != -ENXIO) {
249 ret = devm_request_irq(&pdev->dev, dev->err_interrupt,
250 orion_mdio_err_irq,
251 IRQF_SHARED, pdev->name, dev);
252 if (ret)
253 goto out_mdio;
254
255 writel(MVMDIO_ERR_INT_SMI_DONE,
256 dev->regs + MVMDIO_ERR_INT_MASK);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100257 }
258
259 mutex_init(&dev->lock);
260
Florian Fainelli7111b712013-03-22 03:39:25 +0000261 if (pdev->dev.of_node)
262 ret = of_mdiobus_register(bus, pdev->dev.of_node);
263 else
264 ret = mdiobus_register(bus);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100265 if (ret < 0) {
266 dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
Florian Fainelli2ec98522013-03-22 03:39:27 +0000267 goto out_mdio;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100268 }
269
270 platform_set_drvdata(pdev, bus);
271
272 return 0;
Florian Fainelli2ec98522013-03-22 03:39:27 +0000273
274out_mdio:
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +0000275 if (!IS_ERR(dev->clk))
276 clk_disable_unprepare(dev->clk);
Florian Fainelli2ec98522013-03-22 03:39:27 +0000277 kfree(bus->irq);
278 mdiobus_free(bus);
279 return ret;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100280}
281
Greg KH03ce7582012-12-21 13:42:15 +0000282static int orion_mdio_remove(struct platform_device *pdev)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100283{
284 struct mii_bus *bus = platform_get_drvdata(pdev);
Florian Fainelli2ec98522013-03-22 03:39:27 +0000285 struct orion_mdio_dev *dev = bus->priv;
286
287 writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100288 mdiobus_unregister(bus);
289 kfree(bus->irq);
290 mdiobus_free(bus);
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +0000291 if (!IS_ERR(dev->clk))
292 clk_disable_unprepare(dev->clk);
293
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100294 return 0;
295}
296
297static const struct of_device_id orion_mdio_match[] = {
298 { .compatible = "marvell,orion-mdio" },
299 { }
300};
301MODULE_DEVICE_TABLE(of, orion_mdio_match);
302
303static struct platform_driver orion_mdio_driver = {
304 .probe = orion_mdio_probe,
Greg KH03ce7582012-12-21 13:42:15 +0000305 .remove = orion_mdio_remove,
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100306 .driver = {
307 .name = "orion-mdio",
308 .of_match_table = orion_mdio_match,
309 },
310};
311
312module_platform_driver(orion_mdio_driver);
313
314MODULE_DESCRIPTION("Marvell MDIO interface driver");
315MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
316MODULE_LICENSE("GPL");
Simon Baatz404b8be2013-03-24 10:33:59 +0000317MODULE_ALIAS("platform:orion-mdio");