blob: e4aa8e2d2e8aa06d61b23edf59dae091fcf98e01 [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
Antoine Ténart14ef8b32017-06-15 16:43:16 +020020#include <linux/clk.h>
21#include <linux/delay.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010024#include <linux/kernel.h>
25#include <linux/module.h>
Florian Fainelli7111b712013-03-22 03:39:25 +000026#include <linux/of_mdio.h>
Antoine Ténart14ef8b32017-06-15 16:43:16 +020027#include <linux/phy.h>
28#include <linux/platform_device.h>
Florian Fainelli2ec98522013-03-22 03:39:27 +000029#include <linux/sched.h>
30#include <linux/wait.h>
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010031
Antoine Ténart2040ef22017-06-15 16:43:17 +020032#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
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010042
Leigh Brownb70cd1c2013-10-29 09:33:31 +000043/*
44 * SMI Timeout measurements:
45 * - Kirkwood 88F6281 (Globalscale Dreamplug): 45us to 95us (Interrupt)
46 * - Armada 370 (Globalscale Mirabox): 41us to 43us (Polled)
47 */
Antoine Ténart2040ef22017-06-15 16:43:17 +020048#define MVMDIO_SMI_TIMEOUT 1000 /* 1000us = 1ms */
49#define MVMDIO_SMI_POLL_INTERVAL_MIN 45
50#define MVMDIO_SMI_POLL_INTERVAL_MAX 55
Leigh Brownb70cd1c2013-10-29 09:33:31 +000051
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010052struct orion_mdio_dev {
Florian Fainelli3712b712013-03-22 03:39:26 +000053 void __iomem *regs;
Russell King96cb4342017-04-10 16:28:31 +010054 struct clk *clk[3];
Florian Fainelli2ec98522013-03-22 03:39:27 +000055 /*
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;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010063};
64
Antoine Ténartb0b7fa42017-06-15 16:43:20 +020065struct orion_mdio_ops {
66 int (*is_done)(struct orion_mdio_dev *);
Antoine Ténart19557962017-06-15 16:43:21 +020067 unsigned int poll_interval_min;
68 unsigned int poll_interval_max;
Antoine Ténartb0b7fa42017-06-15 16:43:20 +020069};
Florian Fainelli2ec98522013-03-22 03:39:27 +000070
Thomas Petazzonib07812f2012-11-19 11:40:15 +010071/* Wait for the SMI unit to be ready for another operation
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010072 */
Antoine Ténartb0b7fa42017-06-15 16:43:20 +020073static int orion_mdio_wait_ready(const struct orion_mdio_ops *ops,
74 struct mii_bus *bus)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010075{
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) {
Antoine Ténartb0b7fa42017-06-15 16:43:20 +020082 if (ops->is_done(dev))
Leigh Brownb70cd1c2013-10-29 09:33:31 +000083 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) {
Antoine Ténart19557962017-06-15 16:43:21 +020088 usleep_range(ops->poll_interval_min,
89 ops->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,
Antoine Ténartb0b7fa42017-06-15 16:43:20 +0200101 ops->is_done(dev), timeout);
Leigh Brownb70cd1c2013-10-29 09:33:31 +0000102
103 ++timedout;
104 }
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100105 }
106
Leigh Brownb70cd1c2013-10-29 09:33:31 +0000107 dev_err(bus->parent, "Timeout: SMI busy for too long\n");
108 return -ETIMEDOUT;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100109}
110
Antoine Ténartb0b7fa42017-06-15 16:43:20 +0200111static int orion_mdio_smi_is_done(struct orion_mdio_dev *dev)
112{
113 return !(readl(dev->regs) & MVMDIO_SMI_BUSY);
114}
115
116static const struct orion_mdio_ops orion_mdio_smi_ops = {
117 .is_done = orion_mdio_smi_is_done,
Antoine Ténart19557962017-06-15 16:43:21 +0200118 .poll_interval_min = MVMDIO_SMI_POLL_INTERVAL_MIN,
119 .poll_interval_max = MVMDIO_SMI_POLL_INTERVAL_MAX,
Antoine Ténartb0b7fa42017-06-15 16:43:20 +0200120};
121
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100122static int orion_mdio_read(struct mii_bus *bus, int mii_id,
123 int regnum)
124{
125 struct orion_mdio_dev *dev = bus->priv;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100126 u32 val;
127 int ret;
128
Antoine Ténartb0b7fa42017-06-15 16:43:20 +0200129 ret = orion_mdio_wait_ready(&orion_mdio_smi_ops, bus);
Leigh Brown839f46b2013-10-29 09:33:32 +0000130 if (ret < 0)
131 goto out;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100132
133 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
134 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
135 MVMDIO_SMI_READ_OPERATION),
Florian Fainelli3712b712013-03-22 03:39:26 +0000136 dev->regs);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100137
Antoine Ténartb0b7fa42017-06-15 16:43:20 +0200138 ret = orion_mdio_wait_ready(&orion_mdio_smi_ops, bus);
Leigh Brown839f46b2013-10-29 09:33:32 +0000139 if (ret < 0)
140 goto out;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100141
Leigh Brown839f46b2013-10-29 09:33:32 +0000142 val = readl(dev->regs);
143 if (!(val & MVMDIO_SMI_READ_VALID)) {
144 dev_err(bus->parent, "SMI bus read not valid\n");
145 ret = -ENODEV;
146 goto out;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100147 }
148
Antoine Ténartfd3ebd82017-06-15 16:43:18 +0200149 ret = val & GENMASK(15, 0);
Leigh Brown839f46b2013-10-29 09:33:32 +0000150out:
Leigh Brown839f46b2013-10-29 09:33:32 +0000151 return ret;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100152}
153
154static int orion_mdio_write(struct mii_bus *bus, int mii_id,
155 int regnum, u16 value)
156{
157 struct orion_mdio_dev *dev = bus->priv;
158 int ret;
159
Antoine Ténartb0b7fa42017-06-15 16:43:20 +0200160 ret = orion_mdio_wait_ready(&orion_mdio_smi_ops, bus);
Leigh Brown526edcf2013-10-29 09:33:33 +0000161 if (ret < 0)
162 goto out;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100163
164 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
165 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
166 MVMDIO_SMI_WRITE_OPERATION |
167 (value << MVMDIO_SMI_DATA_SHIFT)),
Florian Fainelli3712b712013-03-22 03:39:26 +0000168 dev->regs);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100169
Leigh Brown526edcf2013-10-29 09:33:33 +0000170out:
Leigh Brown526edcf2013-10-29 09:33:33 +0000171 return ret;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100172}
173
Florian Fainelli2ec98522013-03-22 03:39:27 +0000174static irqreturn_t orion_mdio_err_irq(int irq, void *dev_id)
175{
176 struct orion_mdio_dev *dev = dev_id;
177
178 if (readl(dev->regs + MVMDIO_ERR_INT_CAUSE) &
179 MVMDIO_ERR_INT_SMI_DONE) {
180 writel(~MVMDIO_ERR_INT_SMI_DONE,
181 dev->regs + MVMDIO_ERR_INT_CAUSE);
182 wake_up(&dev->smi_busy_wait);
183 return IRQ_HANDLED;
184 }
185
186 return IRQ_NONE;
187}
188
Greg KH03ce7582012-12-21 13:42:15 +0000189static int orion_mdio_probe(struct platform_device *pdev)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100190{
Florian Fainelli7111b712013-03-22 03:39:25 +0000191 struct resource *r;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100192 struct mii_bus *bus;
193 struct orion_mdio_dev *dev;
Russell King96cb4342017-04-10 16:28:31 +0100194 int i, ret;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100195
Florian Fainelli7111b712013-03-22 03:39:25 +0000196 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
197 if (!r) {
198 dev_err(&pdev->dev, "No SMI register address given\n");
199 return -ENODEV;
200 }
201
Ezequiel Garcia56ecd2c2014-05-22 20:07:02 -0300202 bus = devm_mdiobus_alloc_size(&pdev->dev,
203 sizeof(struct orion_mdio_dev));
204 if (!bus)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100205 return -ENOMEM;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100206
207 bus->name = "orion_mdio_bus";
208 bus->read = orion_mdio_read;
209 bus->write = orion_mdio_write;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100210 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
211 dev_name(&pdev->dev));
212 bus->parent = &pdev->dev;
213
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100214 dev = bus->priv;
Florian Fainelli3712b712013-03-22 03:39:26 +0000215 dev->regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
216 if (!dev->regs) {
Florian Fainelli7111b712013-03-22 03:39:25 +0000217 dev_err(&pdev->dev, "Unable to remap SMI register\n");
Alexey Khoroshilovf814bfd2016-10-01 00:56:37 +0300218 return -ENODEV;
Florian Fainelli2ec98522013-03-22 03:39:27 +0000219 }
220
221 init_waitqueue_head(&dev->smi_busy_wait);
222
Russell King96cb4342017-04-10 16:28:31 +0100223 for (i = 0; i < ARRAY_SIZE(dev->clk); i++) {
224 dev->clk[i] = of_clk_get(pdev->dev.of_node, i);
225 if (IS_ERR(dev->clk[i]))
226 break;
227 clk_prepare_enable(dev->clk[i]);
228 }
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +0000229
Florian Fainelli2ec98522013-03-22 03:39:27 +0000230 dev->err_interrupt = platform_get_irq(pdev, 0);
Russell Kinga51e2c92017-04-10 16:28:20 +0100231 if (dev->err_interrupt > 0 &&
232 resource_size(r) < MVMDIO_ERR_INT_MASK + 4) {
233 dev_err(&pdev->dev,
234 "disabling interrupt, resource size is too small\n");
235 dev->err_interrupt = 0;
236 }
Ezequiel Garcia39076b02014-04-30 13:28:51 -0300237 if (dev->err_interrupt > 0) {
Florian Fainelli2ec98522013-03-22 03:39:27 +0000238 ret = devm_request_irq(&pdev->dev, dev->err_interrupt,
239 orion_mdio_err_irq,
240 IRQF_SHARED, pdev->name, dev);
241 if (ret)
242 goto out_mdio;
243
244 writel(MVMDIO_ERR_INT_SMI_DONE,
245 dev->regs + MVMDIO_ERR_INT_MASK);
Ezequiel Garcia39076b02014-04-30 13:28:51 -0300246
247 } else if (dev->err_interrupt == -EPROBE_DEFER) {
248 return -EPROBE_DEFER;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100249 }
250
Florian Fainelli7111b712013-03-22 03:39:25 +0000251 if (pdev->dev.of_node)
252 ret = of_mdiobus_register(bus, pdev->dev.of_node);
253 else
254 ret = mdiobus_register(bus);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100255 if (ret < 0) {
256 dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
Florian Fainelli2ec98522013-03-22 03:39:27 +0000257 goto out_mdio;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100258 }
259
260 platform_set_drvdata(pdev, bus);
261
262 return 0;
Florian Fainelli2ec98522013-03-22 03:39:27 +0000263
264out_mdio:
Russell King37282482017-04-10 16:28:04 +0100265 if (dev->err_interrupt > 0)
266 writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
Russell King96cb4342017-04-10 16:28:31 +0100267
268 for (i = 0; i < ARRAY_SIZE(dev->clk); i++) {
269 if (IS_ERR(dev->clk[i]))
270 break;
271 clk_disable_unprepare(dev->clk[i]);
272 clk_put(dev->clk[i]);
273 }
274
Florian Fainelli2ec98522013-03-22 03:39:27 +0000275 return ret;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100276}
277
Greg KH03ce7582012-12-21 13:42:15 +0000278static int orion_mdio_remove(struct platform_device *pdev)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100279{
280 struct mii_bus *bus = platform_get_drvdata(pdev);
Florian Fainelli2ec98522013-03-22 03:39:27 +0000281 struct orion_mdio_dev *dev = bus->priv;
Russell King96cb4342017-04-10 16:28:31 +0100282 int i;
Florian Fainelli2ec98522013-03-22 03:39:27 +0000283
Russell King7093a972017-04-10 16:28:09 +0100284 if (dev->err_interrupt > 0)
285 writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100286 mdiobus_unregister(bus);
Russell King96cb4342017-04-10 16:28:31 +0100287
288 for (i = 0; i < ARRAY_SIZE(dev->clk); i++) {
289 if (IS_ERR(dev->clk[i]))
290 break;
291 clk_disable_unprepare(dev->clk[i]);
292 clk_put(dev->clk[i]);
293 }
Sebastian Hesselbarth3d604da2013-04-07 01:09:47 +0000294
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100295 return 0;
296}
297
298static const struct of_device_id orion_mdio_match[] = {
299 { .compatible = "marvell,orion-mdio" },
300 { }
301};
302MODULE_DEVICE_TABLE(of, orion_mdio_match);
303
304static struct platform_driver orion_mdio_driver = {
305 .probe = orion_mdio_probe,
Greg KH03ce7582012-12-21 13:42:15 +0000306 .remove = orion_mdio_remove,
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100307 .driver = {
308 .name = "orion-mdio",
309 .of_match_table = orion_mdio_match,
310 },
311};
312
313module_platform_driver(orion_mdio_driver);
314
315MODULE_DESCRIPTION("Marvell MDIO interface driver");
316MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
317MODULE_LICENSE("GPL");
Simon Baatz404b8be2013-03-24 10:33:59 +0000318MODULE_ALIAS("platform:orion-mdio");