xref: /linux/drivers/net/ethernet/marvell/mvmdio.c (revision d91517839e5d95adc0cf4b28caa7af62a71de526)
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/kernel.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/phy.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
26 #include <linux/delay.h>
27 #include <linux/io.h>
28 #include <linux/clk.h>
29 #include <linux/of_mdio.h>
30 #include <linux/sched.h>
31 #include <linux/wait.h>
32 
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)
40 #define MVMDIO_ERR_INT_CAUSE		   0x007C
41 #define  MVMDIO_ERR_INT_SMI_DONE	   0x00000010
42 #define MVMDIO_ERR_INT_MASK		   0x0080
43 
44 /*
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 
53 struct orion_mdio_dev {
54 	struct mutex lock;
55 	void __iomem *regs;
56 	struct clk *clk;
57 	/*
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;
65 };
66 
67 static int orion_mdio_smi_is_done(struct orion_mdio_dev *dev)
68 {
69 	return !(readl(dev->regs) & MVMDIO_SMI_BUSY);
70 }
71 
72 /* Wait for the SMI unit to be ready for another operation
73  */
74 static int orion_mdio_wait_ready(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 (orion_mdio_smi_is_done(dev))
83 			return 0;
84 	        else if (timedout)
85 			break;
86 
87 	        if (dev->err_interrupt <= 0) {
88 			usleep_range(MVMDIO_SMI_POLL_INTERVAL_MIN,
89 				     MVMDIO_SMI_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 				           orion_mdio_smi_is_done(dev),
102 				           timeout);
103 
104 			++timedout;
105 	        }
106 	}
107 
108 	dev_err(bus->parent, "Timeout: SMI busy for too long\n");
109 	return  -ETIMEDOUT;
110 }
111 
112 static int orion_mdio_read(struct mii_bus *bus, int mii_id,
113 			   int regnum)
114 {
115 	struct orion_mdio_dev *dev = bus->priv;
116 	u32 val;
117 	int ret;
118 
119 	mutex_lock(&dev->lock);
120 
121 	ret = orion_mdio_wait_ready(bus);
122 	if (ret < 0)
123 		goto out;
124 
125 	writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
126 		(regnum << MVMDIO_SMI_PHY_REG_SHIFT)  |
127 		MVMDIO_SMI_READ_OPERATION),
128 	       dev->regs);
129 
130 	ret = orion_mdio_wait_ready(bus);
131 	if (ret < 0)
132 		goto out;
133 
134 	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;
139 	}
140 
141 	ret = val & 0xFFFF;
142 out:
143 	mutex_unlock(&dev->lock);
144 	return ret;
145 }
146 
147 static 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);
156 	if (ret < 0)
157 		goto out;
158 
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)),
163 	       dev->regs);
164 
165 out:
166 	mutex_unlock(&dev->lock);
167 	return ret;
168 }
169 
170 static int orion_mdio_reset(struct mii_bus *bus)
171 {
172 	return 0;
173 }
174 
175 static irqreturn_t orion_mdio_err_irq(int irq, void *dev_id)
176 {
177 	struct orion_mdio_dev *dev = dev_id;
178 
179 	if (readl(dev->regs + MVMDIO_ERR_INT_CAUSE) &
180 			MVMDIO_ERR_INT_SMI_DONE) {
181 		writel(~MVMDIO_ERR_INT_SMI_DONE,
182 				dev->regs + MVMDIO_ERR_INT_CAUSE);
183 		wake_up(&dev->smi_busy_wait);
184 		return IRQ_HANDLED;
185 	}
186 
187 	return IRQ_NONE;
188 }
189 
190 static int orion_mdio_probe(struct platform_device *pdev)
191 {
192 	struct resource *r;
193 	struct mii_bus *bus;
194 	struct orion_mdio_dev *dev;
195 	int i, ret;
196 
197 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
198 	if (!r) {
199 		dev_err(&pdev->dev, "No SMI register address given\n");
200 		return -ENODEV;
201 	}
202 
203 	bus = mdiobus_alloc_size(sizeof(struct orion_mdio_dev));
204 	if (!bus) {
205 		dev_err(&pdev->dev, "Cannot allocate MDIO bus\n");
206 		return -ENOMEM;
207 	}
208 
209 	bus->name = "orion_mdio_bus";
210 	bus->read = orion_mdio_read;
211 	bus->write = orion_mdio_write;
212 	bus->reset = orion_mdio_reset;
213 	snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
214 		 dev_name(&pdev->dev));
215 	bus->parent = &pdev->dev;
216 
217 	bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
218 	if (!bus->irq) {
219 		mdiobus_free(bus);
220 		return -ENOMEM;
221 	}
222 
223 	for (i = 0; i < PHY_MAX_ADDR; i++)
224 		bus->irq[i] = PHY_POLL;
225 
226 	dev = bus->priv;
227 	dev->regs = devm_ioremap(&pdev->dev, r->start, resource_size(r));
228 	if (!dev->regs) {
229 		dev_err(&pdev->dev, "Unable to remap SMI register\n");
230 		ret = -ENODEV;
231 		goto out_mdio;
232 	}
233 
234 	init_waitqueue_head(&dev->smi_busy_wait);
235 
236 	dev->clk = devm_clk_get(&pdev->dev, NULL);
237 	if (!IS_ERR(dev->clk))
238 		clk_prepare_enable(dev->clk);
239 
240 	dev->err_interrupt = platform_get_irq(pdev, 0);
241 	if (dev->err_interrupt != -ENXIO) {
242 		ret = devm_request_irq(&pdev->dev, dev->err_interrupt,
243 					orion_mdio_err_irq,
244 					IRQF_SHARED, pdev->name, dev);
245 		if (ret)
246 			goto out_mdio;
247 
248 		writel(MVMDIO_ERR_INT_SMI_DONE,
249 			dev->regs + MVMDIO_ERR_INT_MASK);
250 	}
251 
252 	mutex_init(&dev->lock);
253 
254 	if (pdev->dev.of_node)
255 		ret = of_mdiobus_register(bus, pdev->dev.of_node);
256 	else
257 		ret = mdiobus_register(bus);
258 	if (ret < 0) {
259 		dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
260 		goto out_mdio;
261 	}
262 
263 	platform_set_drvdata(pdev, bus);
264 
265 	return 0;
266 
267 out_mdio:
268 	if (!IS_ERR(dev->clk))
269 		clk_disable_unprepare(dev->clk);
270 	kfree(bus->irq);
271 	mdiobus_free(bus);
272 	return ret;
273 }
274 
275 static int orion_mdio_remove(struct platform_device *pdev)
276 {
277 	struct mii_bus *bus = platform_get_drvdata(pdev);
278 	struct orion_mdio_dev *dev = bus->priv;
279 
280 	writel(0, dev->regs + MVMDIO_ERR_INT_MASK);
281 	mdiobus_unregister(bus);
282 	kfree(bus->irq);
283 	mdiobus_free(bus);
284 	if (!IS_ERR(dev->clk))
285 		clk_disable_unprepare(dev->clk);
286 
287 	return 0;
288 }
289 
290 static const struct of_device_id orion_mdio_match[] = {
291 	{ .compatible = "marvell,orion-mdio" },
292 	{ }
293 };
294 MODULE_DEVICE_TABLE(of, orion_mdio_match);
295 
296 static struct platform_driver orion_mdio_driver = {
297 	.probe = orion_mdio_probe,
298 	.remove = orion_mdio_remove,
299 	.driver = {
300 		.name = "orion-mdio",
301 		.of_match_table = orion_mdio_match,
302 	},
303 };
304 
305 module_platform_driver(orion_mdio_driver);
306 
307 MODULE_DESCRIPTION("Marvell MDIO interface driver");
308 MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
309 MODULE_LICENSE("GPL");
310 MODULE_ALIAS("platform:orion-mdio");
311