blob: bbc5fdedd13b548d5c89f819fe3d1c082adad044 [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>
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010027#include <linux/platform_device.h>
Thomas Petazzonid98a80f2012-11-19 11:39:42 +010028#include <linux/delay.h>
Florian Fainelli7111b712013-03-22 03:39:25 +000029#include <linux/io.h>
30#include <linux/of_mdio.h>
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010031
32#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
40struct orion_mdio_dev {
41 struct mutex lock;
42 void __iomem *smireg;
43};
44
Thomas Petazzonib07812f2012-11-19 11:40:15 +010045/* Wait for the SMI unit to be ready for another operation
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +010046 */
47static int orion_mdio_wait_ready(struct mii_bus *bus)
48{
49 struct orion_mdio_dev *dev = bus->priv;
50 int count;
51 u32 val;
52
53 count = 0;
54 while (1) {
55 val = readl(dev->smireg);
56 if (!(val & MVMDIO_SMI_BUSY))
57 break;
58
59 if (count > 100) {
60 dev_err(bus->parent, "Timeout: SMI busy for too long\n");
61 return -ETIMEDOUT;
62 }
63
64 udelay(10);
65 count++;
66 }
67
68 return 0;
69}
70
71static int orion_mdio_read(struct mii_bus *bus, int mii_id,
72 int regnum)
73{
74 struct orion_mdio_dev *dev = bus->priv;
75 int count;
76 u32 val;
77 int ret;
78
79 mutex_lock(&dev->lock);
80
81 ret = orion_mdio_wait_ready(bus);
82 if (ret < 0) {
83 mutex_unlock(&dev->lock);
84 return ret;
85 }
86
87 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
88 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
89 MVMDIO_SMI_READ_OPERATION),
90 dev->smireg);
91
92 /* Wait for the value to become available */
93 count = 0;
94 while (1) {
95 val = readl(dev->smireg);
96 if (val & MVMDIO_SMI_READ_VALID)
97 break;
98
99 if (count > 100) {
100 dev_err(bus->parent, "Timeout when reading PHY\n");
101 mutex_unlock(&dev->lock);
102 return -ETIMEDOUT;
103 }
104
105 udelay(10);
106 count++;
107 }
108
109 mutex_unlock(&dev->lock);
110
111 return val & 0xFFFF;
112}
113
114static int orion_mdio_write(struct mii_bus *bus, int mii_id,
115 int regnum, u16 value)
116{
117 struct orion_mdio_dev *dev = bus->priv;
118 int ret;
119
120 mutex_lock(&dev->lock);
121
122 ret = orion_mdio_wait_ready(bus);
123 if (ret < 0) {
124 mutex_unlock(&dev->lock);
125 return ret;
126 }
127
128 writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) |
129 (regnum << MVMDIO_SMI_PHY_REG_SHIFT) |
130 MVMDIO_SMI_WRITE_OPERATION |
131 (value << MVMDIO_SMI_DATA_SHIFT)),
132 dev->smireg);
133
134 mutex_unlock(&dev->lock);
135
136 return 0;
137}
138
139static int orion_mdio_reset(struct mii_bus *bus)
140{
141 return 0;
142}
143
Greg KH03ce7582012-12-21 13:42:15 +0000144static int orion_mdio_probe(struct platform_device *pdev)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100145{
Florian Fainelli7111b712013-03-22 03:39:25 +0000146 struct resource *r;
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100147 struct mii_bus *bus;
148 struct orion_mdio_dev *dev;
149 int i, ret;
150
Florian Fainelli7111b712013-03-22 03:39:25 +0000151 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
152 if (!r) {
153 dev_err(&pdev->dev, "No SMI register address given\n");
154 return -ENODEV;
155 }
156
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100157 bus = mdiobus_alloc_size(sizeof(struct orion_mdio_dev));
158 if (!bus) {
159 dev_err(&pdev->dev, "Cannot allocate MDIO bus\n");
160 return -ENOMEM;
161 }
162
163 bus->name = "orion_mdio_bus";
164 bus->read = orion_mdio_read;
165 bus->write = orion_mdio_write;
166 bus->reset = orion_mdio_reset;
167 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii",
168 dev_name(&pdev->dev));
169 bus->parent = &pdev->dev;
170
171 bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
172 if (!bus->irq) {
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100173 mdiobus_free(bus);
174 return -ENOMEM;
175 }
176
177 for (i = 0; i < PHY_MAX_ADDR; i++)
178 bus->irq[i] = PHY_POLL;
179
180 dev = bus->priv;
Florian Fainelli7111b712013-03-22 03:39:25 +0000181 dev->smireg = devm_ioremap(&pdev->dev, r->start, resource_size(r));
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100182 if (!dev->smireg) {
Florian Fainelli7111b712013-03-22 03:39:25 +0000183 dev_err(&pdev->dev, "Unable to remap SMI register\n");
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100184 kfree(bus->irq);
185 mdiobus_free(bus);
186 return -ENODEV;
187 }
188
189 mutex_init(&dev->lock);
190
Florian Fainelli7111b712013-03-22 03:39:25 +0000191 if (pdev->dev.of_node)
192 ret = of_mdiobus_register(bus, pdev->dev.of_node);
193 else
194 ret = mdiobus_register(bus);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100195 if (ret < 0) {
196 dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret);
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100197 kfree(bus->irq);
198 mdiobus_free(bus);
199 return ret;
200 }
201
202 platform_set_drvdata(pdev, bus);
203
204 return 0;
205}
206
Greg KH03ce7582012-12-21 13:42:15 +0000207static int orion_mdio_remove(struct platform_device *pdev)
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100208{
209 struct mii_bus *bus = platform_get_drvdata(pdev);
210 mdiobus_unregister(bus);
211 kfree(bus->irq);
212 mdiobus_free(bus);
213 return 0;
214}
215
216static const struct of_device_id orion_mdio_match[] = {
217 { .compatible = "marvell,orion-mdio" },
218 { }
219};
220MODULE_DEVICE_TABLE(of, orion_mdio_match);
221
222static struct platform_driver orion_mdio_driver = {
223 .probe = orion_mdio_probe,
Greg KH03ce7582012-12-21 13:42:15 +0000224 .remove = orion_mdio_remove,
Thomas Petazzonifc8f5ad2012-11-12 17:03:47 +0100225 .driver = {
226 .name = "orion-mdio",
227 .of_match_table = orion_mdio_match,
228 },
229};
230
231module_platform_driver(orion_mdio_driver);
232
233MODULE_DESCRIPTION("Marvell MDIO interface driver");
234MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
235MODULE_LICENSE("GPL");