Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QorIQ 10G MDIO Controller |
| 3 | * |
| 4 | * Copyright 2012 Freescale Semiconductor, Inc. |
| 5 | * |
| 6 | * Authors: Andy Fleming <afleming@freescale.com> |
| 7 | * Timur Tabi <timur@freescale.com> |
| 8 | * |
| 9 | * This file is licensed under the terms of the GNU General Public License |
| 10 | * version 2. This program is licensed "as is" without any warranty of any |
| 11 | * kind, whether express or implied. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/phy.h> |
| 19 | #include <linux/mdio.h> |
Rob Herring | 5af5073 | 2013-09-17 14:28:33 -0500 | [diff] [blame] | 20 | #include <linux/of_address.h> |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 21 | #include <linux/of_platform.h> |
| 22 | #include <linux/of_mdio.h> |
| 23 | |
| 24 | /* Number of microseconds to wait for a register to respond */ |
| 25 | #define TIMEOUT 1000 |
| 26 | |
| 27 | struct tgec_mdio_controller { |
| 28 | __be32 reserved[12]; |
| 29 | __be32 mdio_stat; /* MDIO configuration and status */ |
| 30 | __be32 mdio_ctl; /* MDIO control */ |
| 31 | __be32 mdio_data; /* MDIO data */ |
| 32 | __be32 mdio_addr; /* MDIO address */ |
| 33 | } __packed; |
| 34 | |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 35 | #define MDIO_STAT_ENC BIT(6) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 36 | #define MDIO_STAT_CLKDIV(x) (((x>>1) & 0xff) << 8) |
Shaohui Xie | 49ff2d3 | 2015-01-13 10:30:59 +0800 | [diff] [blame] | 37 | #define MDIO_STAT_BSY BIT(0) |
| 38 | #define MDIO_STAT_RD_ER BIT(1) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 39 | #define MDIO_CTL_DEV_ADDR(x) (x & 0x1f) |
| 40 | #define MDIO_CTL_PORT_ADDR(x) ((x & 0x1f) << 5) |
Shaohui Xie | 49ff2d3 | 2015-01-13 10:30:59 +0800 | [diff] [blame] | 41 | #define MDIO_CTL_PRE_DIS BIT(10) |
| 42 | #define MDIO_CTL_SCAN_EN BIT(11) |
| 43 | #define MDIO_CTL_POST_INC BIT(14) |
| 44 | #define MDIO_CTL_READ BIT(15) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 45 | |
| 46 | #define MDIO_DATA(x) (x & 0xffff) |
Shaohui Xie | 49ff2d3 | 2015-01-13 10:30:59 +0800 | [diff] [blame] | 47 | #define MDIO_DATA_BSY BIT(31) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
Madalin Bucur | c1543d3 | 2014-07-29 14:47:25 -0500 | [diff] [blame] | 50 | * Wait until the MDIO bus is free |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 51 | */ |
| 52 | static int xgmac_wait_until_free(struct device *dev, |
| 53 | struct tgec_mdio_controller __iomem *regs) |
| 54 | { |
Shaohui Xie | 22f6bba | 2015-01-21 19:08:32 +0800 | [diff] [blame^] | 55 | unsigned int timeout; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 56 | |
| 57 | /* Wait till the bus is free */ |
Shaohui Xie | 22f6bba | 2015-01-21 19:08:32 +0800 | [diff] [blame^] | 58 | timeout = TIMEOUT; |
| 59 | while ((ioread32be(®s->mdio_stat) & MDIO_STAT_BSY) && timeout) { |
| 60 | cpu_relax(); |
| 61 | timeout--; |
| 62 | } |
| 63 | |
| 64 | if (!timeout) { |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 65 | dev_err(dev, "timeout waiting for bus to be free\n"); |
| 66 | return -ETIMEDOUT; |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Wait till the MDIO read or write operation is complete |
| 74 | */ |
| 75 | static int xgmac_wait_until_done(struct device *dev, |
| 76 | struct tgec_mdio_controller __iomem *regs) |
| 77 | { |
Shaohui Xie | 22f6bba | 2015-01-21 19:08:32 +0800 | [diff] [blame^] | 78 | unsigned int timeout; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 79 | |
| 80 | /* Wait till the MDIO write is complete */ |
Shaohui Xie | 22f6bba | 2015-01-21 19:08:32 +0800 | [diff] [blame^] | 81 | timeout = TIMEOUT; |
| 82 | while ((ioread32be(®s->mdio_data) & MDIO_DATA_BSY) && timeout) { |
| 83 | cpu_relax(); |
| 84 | timeout--; |
| 85 | } |
| 86 | |
| 87 | if (!timeout) { |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 88 | dev_err(dev, "timeout waiting for operation to complete\n"); |
| 89 | return -ETIMEDOUT; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Write value to the PHY for this device to the register at regnum,waiting |
| 97 | * until the write is done before it returns. All PHY configuration has to be |
| 98 | * done through the TSEC1 MIIM regs. |
| 99 | */ |
| 100 | static int xgmac_mdio_write(struct mii_bus *bus, int phy_id, int regnum, u16 value) |
| 101 | { |
| 102 | struct tgec_mdio_controller __iomem *regs = bus->priv; |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 103 | uint16_t dev_addr; |
| 104 | u32 mdio_ctl, mdio_stat; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 105 | int ret; |
| 106 | |
Shaohui Xie | ca43e58 | 2015-01-21 19:07:49 +0800 | [diff] [blame] | 107 | mdio_stat = ioread32be(®s->mdio_stat); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 108 | if (regnum & MII_ADDR_C45) { |
| 109 | /* Clause 45 (ie 10G) */ |
| 110 | dev_addr = (regnum >> 16) & 0x1f; |
| 111 | mdio_stat |= MDIO_STAT_ENC; |
| 112 | } else { |
| 113 | /* Clause 22 (ie 1G) */ |
| 114 | dev_addr = regnum & 0x1f; |
| 115 | mdio_stat &= ~MDIO_STAT_ENC; |
| 116 | } |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 117 | |
Shaohui Xie | ca43e58 | 2015-01-21 19:07:49 +0800 | [diff] [blame] | 118 | iowrite32be(mdio_stat, ®s->mdio_stat); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 119 | |
| 120 | ret = xgmac_wait_until_free(&bus->dev, regs); |
| 121 | if (ret) |
| 122 | return ret; |
| 123 | |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 124 | /* Set the port and dev addr */ |
| 125 | mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr); |
Shaohui Xie | ca43e58 | 2015-01-21 19:07:49 +0800 | [diff] [blame] | 126 | iowrite32be(mdio_ctl, ®s->mdio_ctl); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 127 | |
| 128 | /* Set the register address */ |
| 129 | if (regnum & MII_ADDR_C45) { |
Shaohui Xie | ca43e58 | 2015-01-21 19:07:49 +0800 | [diff] [blame] | 130 | iowrite32be(regnum & 0xffff, ®s->mdio_addr); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 131 | |
| 132 | ret = xgmac_wait_until_free(&bus->dev, regs); |
| 133 | if (ret) |
| 134 | return ret; |
| 135 | } |
| 136 | |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 137 | /* Write the value to the register */ |
Shaohui Xie | ca43e58 | 2015-01-21 19:07:49 +0800 | [diff] [blame] | 138 | iowrite32be(MDIO_DATA(value), ®s->mdio_data); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 139 | |
| 140 | ret = xgmac_wait_until_done(&bus->dev, regs); |
| 141 | if (ret) |
| 142 | return ret; |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Reads from register regnum in the PHY for device dev, returning the value. |
| 149 | * Clears miimcom first. All PHY configuration has to be done through the |
| 150 | * TSEC1 MIIM regs. |
| 151 | */ |
| 152 | static int xgmac_mdio_read(struct mii_bus *bus, int phy_id, int regnum) |
| 153 | { |
| 154 | struct tgec_mdio_controller __iomem *regs = bus->priv; |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 155 | uint16_t dev_addr; |
| 156 | uint32_t mdio_stat; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 157 | uint32_t mdio_ctl; |
| 158 | uint16_t value; |
| 159 | int ret; |
| 160 | |
Shaohui Xie | ca43e58 | 2015-01-21 19:07:49 +0800 | [diff] [blame] | 161 | mdio_stat = ioread32be(®s->mdio_stat); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 162 | if (regnum & MII_ADDR_C45) { |
| 163 | dev_addr = (regnum >> 16) & 0x1f; |
| 164 | mdio_stat |= MDIO_STAT_ENC; |
| 165 | } else { |
| 166 | dev_addr = regnum & 0x1f; |
Shaohui Xie | e54bfe9 | 2015-01-13 10:30:31 +0800 | [diff] [blame] | 167 | mdio_stat &= ~MDIO_STAT_ENC; |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 168 | } |
| 169 | |
Shaohui Xie | ca43e58 | 2015-01-21 19:07:49 +0800 | [diff] [blame] | 170 | iowrite32be(mdio_stat, ®s->mdio_stat); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 171 | |
| 172 | ret = xgmac_wait_until_free(&bus->dev, regs); |
| 173 | if (ret) |
| 174 | return ret; |
| 175 | |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 176 | /* Set the Port and Device Addrs */ |
| 177 | mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr); |
Shaohui Xie | ca43e58 | 2015-01-21 19:07:49 +0800 | [diff] [blame] | 178 | iowrite32be(mdio_ctl, ®s->mdio_ctl); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 179 | |
| 180 | /* Set the register address */ |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 181 | if (regnum & MII_ADDR_C45) { |
Shaohui Xie | ca43e58 | 2015-01-21 19:07:49 +0800 | [diff] [blame] | 182 | iowrite32be(regnum & 0xffff, ®s->mdio_addr); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 183 | |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 184 | ret = xgmac_wait_until_free(&bus->dev, regs); |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | } |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 188 | |
| 189 | /* Initiate the read */ |
Shaohui Xie | ca43e58 | 2015-01-21 19:07:49 +0800 | [diff] [blame] | 190 | iowrite32be(mdio_ctl | MDIO_CTL_READ, ®s->mdio_ctl); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 191 | |
| 192 | ret = xgmac_wait_until_done(&bus->dev, regs); |
| 193 | if (ret) |
| 194 | return ret; |
| 195 | |
| 196 | /* Return all Fs if nothing was there */ |
Shaohui Xie | ca43e58 | 2015-01-21 19:07:49 +0800 | [diff] [blame] | 197 | if (ioread32be(®s->mdio_stat) & MDIO_STAT_RD_ER) { |
Shruti Kanetkar | 55fd364 | 2014-06-11 13:41:40 -0500 | [diff] [blame] | 198 | dev_err(&bus->dev, |
Shruti Kanetkar | 9e6492e | 2014-07-29 14:53:03 -0500 | [diff] [blame] | 199 | "Error while reading PHY%d reg at %d.%hhu\n", |
Shruti Kanetkar | 55fd364 | 2014-06-11 13:41:40 -0500 | [diff] [blame] | 200 | phy_id, dev_addr, regnum); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 201 | return 0xffff; |
| 202 | } |
| 203 | |
Shaohui Xie | ca43e58 | 2015-01-21 19:07:49 +0800 | [diff] [blame] | 204 | value = ioread32be(®s->mdio_data) & 0xffff; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 205 | dev_dbg(&bus->dev, "read %04x\n", value); |
| 206 | |
| 207 | return value; |
| 208 | } |
| 209 | |
Bill Pemberton | 33897cc | 2012-12-03 09:23:58 -0500 | [diff] [blame] | 210 | static int xgmac_mdio_probe(struct platform_device *pdev) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 211 | { |
| 212 | struct device_node *np = pdev->dev.of_node; |
| 213 | struct mii_bus *bus; |
| 214 | struct resource res; |
| 215 | int ret; |
| 216 | |
| 217 | ret = of_address_to_resource(np, 0, &res); |
| 218 | if (ret) { |
| 219 | dev_err(&pdev->dev, "could not obtain address\n"); |
| 220 | return ret; |
| 221 | } |
| 222 | |
Shaohui Xie | aa84247 | 2014-12-30 16:28:00 +0800 | [diff] [blame] | 223 | bus = mdiobus_alloc(); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 224 | if (!bus) |
| 225 | return -ENOMEM; |
| 226 | |
| 227 | bus->name = "Freescale XGMAC MDIO Bus"; |
| 228 | bus->read = xgmac_mdio_read; |
| 229 | bus->write = xgmac_mdio_write; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 230 | bus->parent = &pdev->dev; |
| 231 | snprintf(bus->id, MII_BUS_ID_SIZE, "%llx", (unsigned long long)res.start); |
| 232 | |
| 233 | /* Set the PHY base address */ |
| 234 | bus->priv = of_iomap(np, 0); |
| 235 | if (!bus->priv) { |
| 236 | ret = -ENOMEM; |
| 237 | goto err_ioremap; |
| 238 | } |
| 239 | |
| 240 | ret = of_mdiobus_register(bus, np); |
| 241 | if (ret) { |
| 242 | dev_err(&pdev->dev, "cannot register MDIO bus\n"); |
| 243 | goto err_registration; |
| 244 | } |
| 245 | |
Jingoo Han | 8513fbd | 2013-05-23 00:52:31 +0000 | [diff] [blame] | 246 | platform_set_drvdata(pdev, bus); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 247 | |
| 248 | return 0; |
| 249 | |
| 250 | err_registration: |
| 251 | iounmap(bus->priv); |
| 252 | |
| 253 | err_ioremap: |
| 254 | mdiobus_free(bus); |
| 255 | |
| 256 | return ret; |
| 257 | } |
| 258 | |
Bill Pemberton | 33897cc | 2012-12-03 09:23:58 -0500 | [diff] [blame] | 259 | static int xgmac_mdio_remove(struct platform_device *pdev) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 260 | { |
Jingoo Han | 8513fbd | 2013-05-23 00:52:31 +0000 | [diff] [blame] | 261 | struct mii_bus *bus = platform_get_drvdata(pdev); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 262 | |
| 263 | mdiobus_unregister(bus); |
| 264 | iounmap(bus->priv); |
| 265 | mdiobus_free(bus); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static struct of_device_id xgmac_mdio_match[] = { |
| 271 | { |
| 272 | .compatible = "fsl,fman-xmdio", |
| 273 | }, |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 274 | { |
| 275 | .compatible = "fsl,fman-memac-mdio", |
| 276 | }, |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 277 | {}, |
| 278 | }; |
| 279 | MODULE_DEVICE_TABLE(of, xgmac_mdio_match); |
| 280 | |
| 281 | static struct platform_driver xgmac_mdio_driver = { |
| 282 | .driver = { |
| 283 | .name = "fsl-fman_xmdio", |
| 284 | .of_match_table = xgmac_mdio_match, |
| 285 | }, |
| 286 | .probe = xgmac_mdio_probe, |
| 287 | .remove = xgmac_mdio_remove, |
| 288 | }; |
| 289 | |
| 290 | module_platform_driver(xgmac_mdio_driver); |
| 291 | |
| 292 | MODULE_DESCRIPTION("Freescale QorIQ 10G MDIO Controller"); |
| 293 | MODULE_LICENSE("GPL v2"); |