Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 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 |
Leigh Brown | d4a0acb | 2013-10-29 09:33:34 +0000 | [diff] [blame] | 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. |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 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/init.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/phy.h> |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Thomas Petazzoni | d98a80f | 2012-11-19 11:39:42 +0100 | [diff] [blame] | 27 | #include <linux/delay.h> |
Florian Fainelli | 7111b71 | 2013-03-22 03:39:25 +0000 | [diff] [blame] | 28 | #include <linux/io.h> |
Sebastian Hesselbarth | 3d604da | 2013-04-07 01:09:47 +0000 | [diff] [blame] | 29 | #include <linux/clk.h> |
Florian Fainelli | 7111b71 | 2013-03-22 03:39:25 +0000 | [diff] [blame] | 30 | #include <linux/of_mdio.h> |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 31 | #include <linux/sched.h> |
| 32 | #include <linux/wait.h> |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 33 | |
| 34 | #define MVMDIO_SMI_DATA_SHIFT 0 |
| 35 | #define MVMDIO_SMI_PHY_ADDR_SHIFT 16 |
| 36 | #define MVMDIO_SMI_PHY_REG_SHIFT 21 |
| 37 | #define MVMDIO_SMI_READ_OPERATION BIT(26) |
| 38 | #define MVMDIO_SMI_WRITE_OPERATION 0 |
| 39 | #define MVMDIO_SMI_READ_VALID BIT(27) |
| 40 | #define MVMDIO_SMI_BUSY BIT(28) |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 41 | #define MVMDIO_ERR_INT_CAUSE 0x007C |
| 42 | #define MVMDIO_ERR_INT_SMI_DONE 0x00000010 |
| 43 | #define MVMDIO_ERR_INT_MASK 0x0080 |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 44 | |
Leigh Brown | b70cd1c | 2013-10-29 09:33:31 +0000 | [diff] [blame] | 45 | /* |
| 46 | * SMI Timeout measurements: |
| 47 | * - Kirkwood 88F6281 (Globalscale Dreamplug): 45us to 95us (Interrupt) |
| 48 | * - Armada 370 (Globalscale Mirabox): 41us to 43us (Polled) |
| 49 | */ |
| 50 | #define MVMDIO_SMI_TIMEOUT 1000 /* 1000us = 1ms */ |
| 51 | #define MVMDIO_SMI_POLL_INTERVAL_MIN 45 |
| 52 | #define MVMDIO_SMI_POLL_INTERVAL_MAX 55 |
| 53 | |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 54 | struct orion_mdio_dev { |
| 55 | struct mutex lock; |
Florian Fainelli | 3712b71 | 2013-03-22 03:39:26 +0000 | [diff] [blame] | 56 | void __iomem *regs; |
Sebastian Hesselbarth | 3d604da | 2013-04-07 01:09:47 +0000 | [diff] [blame] | 57 | struct clk *clk; |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 58 | /* |
| 59 | * If we have access to the error interrupt pin (which is |
| 60 | * somewhat misnamed as it not only reflects internal errors |
| 61 | * but also reflects SMI completion), use that to wait for |
| 62 | * SMI access completion instead of polling the SMI busy bit. |
| 63 | */ |
| 64 | int err_interrupt; |
| 65 | wait_queue_head_t smi_busy_wait; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 68 | static int orion_mdio_smi_is_done(struct orion_mdio_dev *dev) |
| 69 | { |
| 70 | return !(readl(dev->regs) & MVMDIO_SMI_BUSY); |
| 71 | } |
| 72 | |
Thomas Petazzoni | b07812f | 2012-11-19 11:40:15 +0100 | [diff] [blame] | 73 | /* Wait for the SMI unit to be ready for another operation |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 74 | */ |
| 75 | static int orion_mdio_wait_ready(struct mii_bus *bus) |
| 76 | { |
| 77 | struct orion_mdio_dev *dev = bus->priv; |
Leigh Brown | b70cd1c | 2013-10-29 09:33:31 +0000 | [diff] [blame] | 78 | unsigned long timeout = usecs_to_jiffies(MVMDIO_SMI_TIMEOUT); |
| 79 | unsigned long end = jiffies + timeout; |
| 80 | int timedout = 0; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 81 | |
Leigh Brown | b70cd1c | 2013-10-29 09:33:31 +0000 | [diff] [blame] | 82 | while (1) { |
| 83 | if (orion_mdio_smi_is_done(dev)) |
| 84 | return 0; |
| 85 | else if (timedout) |
| 86 | break; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 87 | |
Leigh Brown | b70cd1c | 2013-10-29 09:33:31 +0000 | [diff] [blame] | 88 | if (dev->err_interrupt <= 0) { |
| 89 | usleep_range(MVMDIO_SMI_POLL_INTERVAL_MIN, |
| 90 | MVMDIO_SMI_POLL_INTERVAL_MAX); |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 91 | |
Leigh Brown | b70cd1c | 2013-10-29 09:33:31 +0000 | [diff] [blame] | 92 | if (time_is_before_jiffies(end)) |
| 93 | ++timedout; |
| 94 | } else { |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 95 | wait_event_timeout(dev->smi_busy_wait, |
Leigh Brown | b70cd1c | 2013-10-29 09:33:31 +0000 | [diff] [blame] | 96 | orion_mdio_smi_is_done(dev), |
| 97 | timeout); |
| 98 | |
| 99 | ++timedout; |
| 100 | } |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Leigh Brown | b70cd1c | 2013-10-29 09:33:31 +0000 | [diff] [blame] | 103 | dev_err(bus->parent, "Timeout: SMI busy for too long\n"); |
| 104 | return -ETIMEDOUT; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static int orion_mdio_read(struct mii_bus *bus, int mii_id, |
| 108 | int regnum) |
| 109 | { |
| 110 | struct orion_mdio_dev *dev = bus->priv; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 111 | u32 val; |
| 112 | int ret; |
| 113 | |
| 114 | mutex_lock(&dev->lock); |
| 115 | |
| 116 | ret = orion_mdio_wait_ready(bus); |
Leigh Brown | 839f46b | 2013-10-29 09:33:32 +0000 | [diff] [blame] | 117 | if (ret < 0) |
| 118 | goto out; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 119 | |
| 120 | writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) | |
| 121 | (regnum << MVMDIO_SMI_PHY_REG_SHIFT) | |
| 122 | MVMDIO_SMI_READ_OPERATION), |
Florian Fainelli | 3712b71 | 2013-03-22 03:39:26 +0000 | [diff] [blame] | 123 | dev->regs); |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 124 | |
Leigh Brown | 839f46b | 2013-10-29 09:33:32 +0000 | [diff] [blame] | 125 | ret = orion_mdio_wait_ready(bus); |
| 126 | if (ret < 0) |
| 127 | goto out; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 128 | |
Leigh Brown | 839f46b | 2013-10-29 09:33:32 +0000 | [diff] [blame] | 129 | val = readl(dev->regs); |
| 130 | if (!(val & MVMDIO_SMI_READ_VALID)) { |
| 131 | dev_err(bus->parent, "SMI bus read not valid\n"); |
| 132 | ret = -ENODEV; |
| 133 | goto out; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Leigh Brown | 839f46b | 2013-10-29 09:33:32 +0000 | [diff] [blame] | 136 | ret = val & 0xFFFF; |
| 137 | out: |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 138 | mutex_unlock(&dev->lock); |
Leigh Brown | 839f46b | 2013-10-29 09:33:32 +0000 | [diff] [blame] | 139 | return ret; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static int orion_mdio_write(struct mii_bus *bus, int mii_id, |
| 143 | int regnum, u16 value) |
| 144 | { |
| 145 | struct orion_mdio_dev *dev = bus->priv; |
| 146 | int ret; |
| 147 | |
| 148 | mutex_lock(&dev->lock); |
| 149 | |
| 150 | ret = orion_mdio_wait_ready(bus); |
Leigh Brown | 526edcf | 2013-10-29 09:33:33 +0000 | [diff] [blame] | 151 | if (ret < 0) |
| 152 | goto out; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 153 | |
| 154 | writel(((mii_id << MVMDIO_SMI_PHY_ADDR_SHIFT) | |
| 155 | (regnum << MVMDIO_SMI_PHY_REG_SHIFT) | |
| 156 | MVMDIO_SMI_WRITE_OPERATION | |
| 157 | (value << MVMDIO_SMI_DATA_SHIFT)), |
Florian Fainelli | 3712b71 | 2013-03-22 03:39:26 +0000 | [diff] [blame] | 158 | dev->regs); |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 159 | |
Leigh Brown | 526edcf | 2013-10-29 09:33:33 +0000 | [diff] [blame] | 160 | out: |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 161 | mutex_unlock(&dev->lock); |
Leigh Brown | 526edcf | 2013-10-29 09:33:33 +0000 | [diff] [blame] | 162 | return ret; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static int orion_mdio_reset(struct mii_bus *bus) |
| 166 | { |
| 167 | return 0; |
| 168 | } |
| 169 | |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 170 | static 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 KH | 03ce758 | 2012-12-21 13:42:15 +0000 | [diff] [blame] | 185 | static int orion_mdio_probe(struct platform_device *pdev) |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 186 | { |
Florian Fainelli | 7111b71 | 2013-03-22 03:39:25 +0000 | [diff] [blame] | 187 | struct resource *r; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 188 | struct mii_bus *bus; |
| 189 | struct orion_mdio_dev *dev; |
| 190 | int i, ret; |
| 191 | |
Florian Fainelli | 7111b71 | 2013-03-22 03:39:25 +0000 | [diff] [blame] | 192 | 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 | |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 198 | bus = mdiobus_alloc_size(sizeof(struct orion_mdio_dev)); |
| 199 | if (!bus) { |
| 200 | dev_err(&pdev->dev, "Cannot allocate MDIO bus\n"); |
| 201 | return -ENOMEM; |
| 202 | } |
| 203 | |
| 204 | bus->name = "orion_mdio_bus"; |
| 205 | bus->read = orion_mdio_read; |
| 206 | bus->write = orion_mdio_write; |
| 207 | bus->reset = orion_mdio_reset; |
| 208 | snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", |
| 209 | dev_name(&pdev->dev)); |
| 210 | bus->parent = &pdev->dev; |
| 211 | |
| 212 | bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); |
| 213 | if (!bus->irq) { |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 214 | mdiobus_free(bus); |
| 215 | return -ENOMEM; |
| 216 | } |
| 217 | |
| 218 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 219 | bus->irq[i] = PHY_POLL; |
| 220 | |
| 221 | dev = bus->priv; |
Florian Fainelli | 3712b71 | 2013-03-22 03:39:26 +0000 | [diff] [blame] | 222 | dev->regs = devm_ioremap(&pdev->dev, r->start, resource_size(r)); |
| 223 | if (!dev->regs) { |
Florian Fainelli | 7111b71 | 2013-03-22 03:39:25 +0000 | [diff] [blame] | 224 | dev_err(&pdev->dev, "Unable to remap SMI register\n"); |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 225 | ret = -ENODEV; |
| 226 | goto out_mdio; |
| 227 | } |
| 228 | |
| 229 | init_waitqueue_head(&dev->smi_busy_wait); |
| 230 | |
Sebastian Hesselbarth | 3d604da | 2013-04-07 01:09:47 +0000 | [diff] [blame] | 231 | dev->clk = devm_clk_get(&pdev->dev, NULL); |
| 232 | if (!IS_ERR(dev->clk)) |
| 233 | clk_prepare_enable(dev->clk); |
| 234 | |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 235 | dev->err_interrupt = platform_get_irq(pdev, 0); |
| 236 | if (dev->err_interrupt != -ENXIO) { |
| 237 | ret = devm_request_irq(&pdev->dev, dev->err_interrupt, |
| 238 | orion_mdio_err_irq, |
| 239 | IRQF_SHARED, pdev->name, dev); |
| 240 | if (ret) |
| 241 | goto out_mdio; |
| 242 | |
| 243 | writel(MVMDIO_ERR_INT_SMI_DONE, |
| 244 | dev->regs + MVMDIO_ERR_INT_MASK); |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | mutex_init(&dev->lock); |
| 248 | |
Florian Fainelli | 7111b71 | 2013-03-22 03:39:25 +0000 | [diff] [blame] | 249 | if (pdev->dev.of_node) |
| 250 | ret = of_mdiobus_register(bus, pdev->dev.of_node); |
| 251 | else |
| 252 | ret = mdiobus_register(bus); |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 253 | if (ret < 0) { |
| 254 | dev_err(&pdev->dev, "Cannot register MDIO bus (%d)\n", ret); |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 255 | goto out_mdio; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | platform_set_drvdata(pdev, bus); |
| 259 | |
| 260 | return 0; |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 261 | |
| 262 | out_mdio: |
Sebastian Hesselbarth | 3d604da | 2013-04-07 01:09:47 +0000 | [diff] [blame] | 263 | if (!IS_ERR(dev->clk)) |
| 264 | clk_disable_unprepare(dev->clk); |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 265 | kfree(bus->irq); |
| 266 | mdiobus_free(bus); |
| 267 | return ret; |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Greg KH | 03ce758 | 2012-12-21 13:42:15 +0000 | [diff] [blame] | 270 | static int orion_mdio_remove(struct platform_device *pdev) |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 271 | { |
| 272 | struct mii_bus *bus = platform_get_drvdata(pdev); |
Florian Fainelli | 2ec9852 | 2013-03-22 03:39:27 +0000 | [diff] [blame] | 273 | struct orion_mdio_dev *dev = bus->priv; |
| 274 | |
| 275 | writel(0, dev->regs + MVMDIO_ERR_INT_MASK); |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 276 | mdiobus_unregister(bus); |
| 277 | kfree(bus->irq); |
| 278 | mdiobus_free(bus); |
Sebastian Hesselbarth | 3d604da | 2013-04-07 01:09:47 +0000 | [diff] [blame] | 279 | if (!IS_ERR(dev->clk)) |
| 280 | clk_disable_unprepare(dev->clk); |
| 281 | |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static const struct of_device_id orion_mdio_match[] = { |
| 286 | { .compatible = "marvell,orion-mdio" }, |
| 287 | { } |
| 288 | }; |
| 289 | MODULE_DEVICE_TABLE(of, orion_mdio_match); |
| 290 | |
| 291 | static struct platform_driver orion_mdio_driver = { |
| 292 | .probe = orion_mdio_probe, |
Greg KH | 03ce758 | 2012-12-21 13:42:15 +0000 | [diff] [blame] | 293 | .remove = orion_mdio_remove, |
Thomas Petazzoni | fc8f5ad | 2012-11-12 17:03:47 +0100 | [diff] [blame] | 294 | .driver = { |
| 295 | .name = "orion-mdio", |
| 296 | .of_match_table = orion_mdio_match, |
| 297 | }, |
| 298 | }; |
| 299 | |
| 300 | module_platform_driver(orion_mdio_driver); |
| 301 | |
| 302 | MODULE_DESCRIPTION("Marvell MDIO interface driver"); |
| 303 | MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>"); |
| 304 | MODULE_LICENSE("GPL"); |
Simon Baatz | 404b8be | 2013-03-24 10:33:59 +0000 | [diff] [blame] | 305 | MODULE_ALIAS("platform:orion-mdio"); |