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. |
Calvin Johnson | 15e7064 | 2021-06-11 13:53:58 +0300 | [diff] [blame] | 5 | * Copyright 2021 NXP |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 6 | * |
| 7 | * Authors: Andy Fleming <afleming@freescale.com> |
| 8 | * Timur Tabi <timur@freescale.com> |
| 9 | * |
| 10 | * This file is licensed under the terms of the GNU General Public License |
| 11 | * version 2. This program is licensed "as is" without any warranty of any |
| 12 | * kind, whether express or implied. |
| 13 | */ |
| 14 | |
Calvin Johnson | 15e7064 | 2021-06-11 13:53:58 +0300 | [diff] [blame] | 15 | #include <linux/acpi.h> |
Marcin Wojtas | ac53c26 | 2021-06-25 12:38:53 +0200 | [diff] [blame] | 16 | #include <linux/acpi_mdio.h> |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
Calvin Johnson | 15e7064 | 2021-06-11 13:53:58 +0300 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 19 | #include <linux/mdio.h> |
Calvin Johnson | 15e7064 | 2021-06-11 13:53:58 +0300 | [diff] [blame] | 20 | #include <linux/module.h> |
Rob Herring | 5af5073 | 2013-09-17 14:28:33 -0500 | [diff] [blame] | 21 | #include <linux/of_address.h> |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 22 | #include <linux/of_mdio.h> |
Calvin Johnson | 15e7064 | 2021-06-11 13:53:58 +0300 | [diff] [blame] | 23 | #include <linux/of_platform.h> |
| 24 | #include <linux/phy.h> |
| 25 | #include <linux/slab.h> |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 26 | |
| 27 | /* Number of microseconds to wait for a register to respond */ |
| 28 | #define TIMEOUT 1000 |
| 29 | |
| 30 | struct tgec_mdio_controller { |
| 31 | __be32 reserved[12]; |
| 32 | __be32 mdio_stat; /* MDIO configuration and status */ |
| 33 | __be32 mdio_ctl; /* MDIO control */ |
| 34 | __be32 mdio_data; /* MDIO data */ |
| 35 | __be32 mdio_addr; /* MDIO address */ |
| 36 | } __packed; |
| 37 | |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 38 | #define MDIO_STAT_ENC BIT(6) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 39 | #define MDIO_STAT_CLKDIV(x) (((x>>1) & 0xff) << 8) |
Shaohui Xie | 49ff2d3 | 2015-01-13 10:30:59 +0800 | [diff] [blame] | 40 | #define MDIO_STAT_BSY BIT(0) |
| 41 | #define MDIO_STAT_RD_ER BIT(1) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 42 | #define MDIO_CTL_DEV_ADDR(x) (x & 0x1f) |
| 43 | #define MDIO_CTL_PORT_ADDR(x) ((x & 0x1f) << 5) |
Shaohui Xie | 49ff2d3 | 2015-01-13 10:30:59 +0800 | [diff] [blame] | 44 | #define MDIO_CTL_PRE_DIS BIT(10) |
| 45 | #define MDIO_CTL_SCAN_EN BIT(11) |
| 46 | #define MDIO_CTL_POST_INC BIT(14) |
| 47 | #define MDIO_CTL_READ BIT(15) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 48 | |
| 49 | #define MDIO_DATA(x) (x & 0xffff) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 50 | |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 51 | struct mdio_fsl_priv { |
| 52 | struct tgec_mdio_controller __iomem *mdio_base; |
| 53 | bool is_little_endian; |
Tobias Waldekranz | 6198c72 | 2022-01-18 22:50:50 +0100 | [diff] [blame] | 54 | bool has_a009885; |
Madalin Bucur | 1d3ca68 | 2020-01-22 15:20:29 +0200 | [diff] [blame] | 55 | bool has_a011043; |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | static u32 xgmac_read32(void __iomem *regs, |
| 59 | bool is_little_endian) |
| 60 | { |
| 61 | if (is_little_endian) |
| 62 | return ioread32(regs); |
| 63 | else |
| 64 | return ioread32be(regs); |
| 65 | } |
| 66 | |
| 67 | static void xgmac_write32(u32 value, |
| 68 | void __iomem *regs, |
| 69 | bool is_little_endian) |
| 70 | { |
| 71 | if (is_little_endian) |
| 72 | iowrite32(value, regs); |
| 73 | else |
| 74 | iowrite32be(value, regs); |
| 75 | } |
| 76 | |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 77 | /* |
Madalin Bucur | c1543d3 | 2014-07-29 14:47:25 -0500 | [diff] [blame] | 78 | * Wait until the MDIO bus is free |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 79 | */ |
| 80 | static int xgmac_wait_until_free(struct device *dev, |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 81 | struct tgec_mdio_controller __iomem *regs, |
| 82 | bool is_little_endian) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 83 | { |
Shaohui Xie | 22f6bba | 2015-01-21 19:08:32 +0800 | [diff] [blame] | 84 | unsigned int timeout; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 85 | |
| 86 | /* Wait till the bus is free */ |
Shaohui Xie | 22f6bba | 2015-01-21 19:08:32 +0800 | [diff] [blame] | 87 | timeout = TIMEOUT; |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 88 | while ((xgmac_read32(®s->mdio_stat, is_little_endian) & |
| 89 | MDIO_STAT_BSY) && timeout) { |
Shaohui Xie | 22f6bba | 2015-01-21 19:08:32 +0800 | [diff] [blame] | 90 | cpu_relax(); |
| 91 | timeout--; |
| 92 | } |
| 93 | |
| 94 | if (!timeout) { |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 95 | dev_err(dev, "timeout waiting for bus to be free\n"); |
| 96 | return -ETIMEDOUT; |
| 97 | } |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Wait till the MDIO read or write operation is complete |
| 104 | */ |
| 105 | static int xgmac_wait_until_done(struct device *dev, |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 106 | struct tgec_mdio_controller __iomem *regs, |
| 107 | bool is_little_endian) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 108 | { |
Shaohui Xie | 22f6bba | 2015-01-21 19:08:32 +0800 | [diff] [blame] | 109 | unsigned int timeout; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 110 | |
| 111 | /* Wait till the MDIO write is complete */ |
Shaohui Xie | 22f6bba | 2015-01-21 19:08:32 +0800 | [diff] [blame] | 112 | timeout = TIMEOUT; |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 113 | while ((xgmac_read32(®s->mdio_stat, is_little_endian) & |
| 114 | MDIO_STAT_BSY) && timeout) { |
Shaohui Xie | 22f6bba | 2015-01-21 19:08:32 +0800 | [diff] [blame] | 115 | cpu_relax(); |
| 116 | timeout--; |
| 117 | } |
| 118 | |
| 119 | if (!timeout) { |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 120 | dev_err(dev, "timeout waiting for operation to complete\n"); |
| 121 | return -ETIMEDOUT; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Write value to the PHY for this device to the register at regnum,waiting |
| 129 | * until the write is done before it returns. All PHY configuration has to be |
| 130 | * done through the TSEC1 MIIM regs. |
| 131 | */ |
| 132 | static int xgmac_mdio_write(struct mii_bus *bus, int phy_id, int regnum, u16 value) |
| 133 | { |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 134 | struct mdio_fsl_priv *priv = (struct mdio_fsl_priv *)bus->priv; |
| 135 | struct tgec_mdio_controller __iomem *regs = priv->mdio_base; |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 136 | uint16_t dev_addr; |
| 137 | u32 mdio_ctl, mdio_stat; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 138 | int ret; |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 139 | bool endian = priv->is_little_endian; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 140 | |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 141 | mdio_stat = xgmac_read32(®s->mdio_stat, endian); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 142 | if (regnum & MII_ADDR_C45) { |
| 143 | /* Clause 45 (ie 10G) */ |
| 144 | dev_addr = (regnum >> 16) & 0x1f; |
| 145 | mdio_stat |= MDIO_STAT_ENC; |
| 146 | } else { |
| 147 | /* Clause 22 (ie 1G) */ |
| 148 | dev_addr = regnum & 0x1f; |
| 149 | mdio_stat &= ~MDIO_STAT_ENC; |
| 150 | } |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 151 | |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 152 | xgmac_write32(mdio_stat, ®s->mdio_stat, endian); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 153 | |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 154 | ret = xgmac_wait_until_free(&bus->dev, regs, endian); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 155 | if (ret) |
| 156 | return ret; |
| 157 | |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 158 | /* Set the port and dev addr */ |
| 159 | mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr); |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 160 | xgmac_write32(mdio_ctl, ®s->mdio_ctl, endian); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 161 | |
| 162 | /* Set the register address */ |
| 163 | if (regnum & MII_ADDR_C45) { |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 164 | xgmac_write32(regnum & 0xffff, ®s->mdio_addr, endian); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 165 | |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 166 | ret = xgmac_wait_until_free(&bus->dev, regs, endian); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 167 | if (ret) |
| 168 | return ret; |
| 169 | } |
| 170 | |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 171 | /* Write the value to the register */ |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 172 | xgmac_write32(MDIO_DATA(value), ®s->mdio_data, endian); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 173 | |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 174 | ret = xgmac_wait_until_done(&bus->dev, regs, endian); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 175 | if (ret) |
| 176 | return ret; |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Reads from register regnum in the PHY for device dev, returning the value. |
| 183 | * Clears miimcom first. All PHY configuration has to be done through the |
| 184 | * TSEC1 MIIM regs. |
| 185 | */ |
| 186 | static int xgmac_mdio_read(struct mii_bus *bus, int phy_id, int regnum) |
| 187 | { |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 188 | struct mdio_fsl_priv *priv = (struct mdio_fsl_priv *)bus->priv; |
| 189 | struct tgec_mdio_controller __iomem *regs = priv->mdio_base; |
Tobias Waldekranz | 6198c72 | 2022-01-18 22:50:50 +0100 | [diff] [blame] | 190 | unsigned long flags; |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 191 | uint16_t dev_addr; |
| 192 | uint32_t mdio_stat; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 193 | uint32_t mdio_ctl; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 194 | int ret; |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 195 | bool endian = priv->is_little_endian; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 196 | |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 197 | mdio_stat = xgmac_read32(®s->mdio_stat, endian); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 198 | if (regnum & MII_ADDR_C45) { |
| 199 | dev_addr = (regnum >> 16) & 0x1f; |
| 200 | mdio_stat |= MDIO_STAT_ENC; |
| 201 | } else { |
| 202 | dev_addr = regnum & 0x1f; |
Shaohui Xie | e54bfe9 | 2015-01-13 10:30:31 +0800 | [diff] [blame] | 203 | mdio_stat &= ~MDIO_STAT_ENC; |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 204 | } |
| 205 | |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 206 | xgmac_write32(mdio_stat, ®s->mdio_stat, endian); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 207 | |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 208 | ret = xgmac_wait_until_free(&bus->dev, regs, endian); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 209 | if (ret) |
| 210 | return ret; |
| 211 | |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 212 | /* Set the Port and Device Addrs */ |
| 213 | mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr); |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 214 | xgmac_write32(mdio_ctl, ®s->mdio_ctl, endian); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 215 | |
| 216 | /* Set the register address */ |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 217 | if (regnum & MII_ADDR_C45) { |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 218 | xgmac_write32(regnum & 0xffff, ®s->mdio_addr, endian); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 219 | |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 220 | ret = xgmac_wait_until_free(&bus->dev, regs, endian); |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 221 | if (ret) |
| 222 | return ret; |
| 223 | } |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 224 | |
Tobias Waldekranz | 6198c72 | 2022-01-18 22:50:50 +0100 | [diff] [blame] | 225 | if (priv->has_a009885) |
| 226 | /* Once the operation completes, i.e. MDIO_STAT_BSY clears, we |
| 227 | * must read back the data register within 16 MDC cycles. |
| 228 | */ |
| 229 | local_irq_save(flags); |
| 230 | |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 231 | /* Initiate the read */ |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 232 | xgmac_write32(mdio_ctl | MDIO_CTL_READ, ®s->mdio_ctl, endian); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 233 | |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 234 | ret = xgmac_wait_until_done(&bus->dev, regs, endian); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 235 | if (ret) |
Tobias Waldekranz | 6198c72 | 2022-01-18 22:50:50 +0100 | [diff] [blame] | 236 | goto irq_restore; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 237 | |
| 238 | /* Return all Fs if nothing was there */ |
Madalin Bucur | 1d3ca68 | 2020-01-22 15:20:29 +0200 | [diff] [blame] | 239 | if ((xgmac_read32(®s->mdio_stat, endian) & MDIO_STAT_RD_ER) && |
| 240 | !priv->has_a011043) { |
Jamie Iles | 1ec8e74 | 2020-09-24 15:56:45 +0100 | [diff] [blame] | 241 | dev_dbg(&bus->dev, |
Shruti Kanetkar | 9e6492e | 2014-07-29 14:53:03 -0500 | [diff] [blame] | 242 | "Error while reading PHY%d reg at %d.%hhu\n", |
Shruti Kanetkar | 55fd364 | 2014-06-11 13:41:40 -0500 | [diff] [blame] | 243 | phy_id, dev_addr, regnum); |
Tobias Waldekranz | 6198c72 | 2022-01-18 22:50:50 +0100 | [diff] [blame] | 244 | ret = 0xffff; |
| 245 | } else { |
| 246 | ret = xgmac_read32(®s->mdio_data, endian) & 0xffff; |
| 247 | dev_dbg(&bus->dev, "read %04x\n", ret); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Tobias Waldekranz | 6198c72 | 2022-01-18 22:50:50 +0100 | [diff] [blame] | 250 | irq_restore: |
| 251 | if (priv->has_a009885) |
| 252 | local_irq_restore(flags); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 253 | |
Tobias Waldekranz | 6198c72 | 2022-01-18 22:50:50 +0100 | [diff] [blame] | 254 | return ret; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Bill Pemberton | 33897cc | 2012-12-03 09:23:58 -0500 | [diff] [blame] | 257 | static int xgmac_mdio_probe(struct platform_device *pdev) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 258 | { |
Marcin Wojtas | ac53c26 | 2021-06-25 12:38:53 +0200 | [diff] [blame] | 259 | struct fwnode_handle *fwnode; |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 260 | struct mdio_fsl_priv *priv; |
Calvin Johnson | 15e7064 | 2021-06-11 13:53:58 +0300 | [diff] [blame] | 261 | struct resource *res; |
| 262 | struct mii_bus *bus; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 263 | int ret; |
| 264 | |
Calvin Johnson | 229f4bb | 2020-06-22 20:35:33 +0530 | [diff] [blame] | 265 | /* In DPAA-1, MDIO is one of the many FMan sub-devices. The FMan |
| 266 | * defines a register space that spans a large area, covering all the |
| 267 | * subdevice areas. Therefore, MDIO cannot claim exclusive access to |
| 268 | * this register area. |
| 269 | */ |
| 270 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 271 | if (!res) { |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 272 | dev_err(&pdev->dev, "could not obtain address\n"); |
Calvin Johnson | 229f4bb | 2020-06-22 20:35:33 +0530 | [diff] [blame] | 273 | return -EINVAL; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 276 | bus = mdiobus_alloc_size(sizeof(struct mdio_fsl_priv)); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 277 | if (!bus) |
| 278 | return -ENOMEM; |
| 279 | |
| 280 | bus->name = "Freescale XGMAC MDIO Bus"; |
| 281 | bus->read = xgmac_mdio_read; |
| 282 | bus->write = xgmac_mdio_write; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 283 | bus->parent = &pdev->dev; |
Jeremy Linton | 0f183fd | 2020-06-22 20:35:34 +0530 | [diff] [blame] | 284 | bus->probe_capabilities = MDIOBUS_C22_C45; |
Calvin Johnson | 229f4bb | 2020-06-22 20:35:33 +0530 | [diff] [blame] | 285 | snprintf(bus->id, MII_BUS_ID_SIZE, "%pa", &res->start); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 286 | |
| 287 | /* Set the PHY base address */ |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 288 | priv = bus->priv; |
Calvin Johnson | 229f4bb | 2020-06-22 20:35:33 +0530 | [diff] [blame] | 289 | priv->mdio_base = ioremap(res->start, resource_size(res)); |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 290 | if (!priv->mdio_base) { |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 291 | ret = -ENOMEM; |
| 292 | goto err_ioremap; |
| 293 | } |
| 294 | |
Calvin Johnson | 15e7064 | 2021-06-11 13:53:58 +0300 | [diff] [blame] | 295 | /* For both ACPI and DT cases, endianness of MDIO controller |
| 296 | * needs to be specified using "little-endian" property. |
| 297 | */ |
Calvin Johnson | 229f4bb | 2020-06-22 20:35:33 +0530 | [diff] [blame] | 298 | priv->is_little_endian = device_property_read_bool(&pdev->dev, |
| 299 | "little-endian"); |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 300 | |
Tobias Waldekranz | 6198c72 | 2022-01-18 22:50:50 +0100 | [diff] [blame] | 301 | priv->has_a009885 = device_property_read_bool(&pdev->dev, |
| 302 | "fsl,erratum-a009885"); |
Calvin Johnson | 229f4bb | 2020-06-22 20:35:33 +0530 | [diff] [blame] | 303 | priv->has_a011043 = device_property_read_bool(&pdev->dev, |
| 304 | "fsl,erratum-a011043"); |
Madalin Bucur | 1d3ca68 | 2020-01-22 15:20:29 +0200 | [diff] [blame] | 305 | |
Marcin Wojtas | ac53c26 | 2021-06-25 12:38:53 +0200 | [diff] [blame] | 306 | fwnode = pdev->dev.fwnode; |
| 307 | if (is_of_node(fwnode)) |
| 308 | ret = of_mdiobus_register(bus, to_of_node(fwnode)); |
| 309 | else if (is_acpi_node(fwnode)) |
| 310 | ret = acpi_mdiobus_register(bus, fwnode); |
| 311 | else |
| 312 | ret = -EINVAL; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 313 | if (ret) { |
| 314 | dev_err(&pdev->dev, "cannot register MDIO bus\n"); |
| 315 | goto err_registration; |
| 316 | } |
| 317 | |
Jingoo Han | 8513fbd | 2013-05-23 00:52:31 +0000 | [diff] [blame] | 318 | platform_set_drvdata(pdev, bus); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 319 | |
| 320 | return 0; |
| 321 | |
| 322 | err_registration: |
Shaohui Xie | 73ee544 | 2015-03-16 18:56:29 +0800 | [diff] [blame] | 323 | iounmap(priv->mdio_base); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 324 | |
| 325 | err_ioremap: |
| 326 | mdiobus_free(bus); |
| 327 | |
| 328 | return ret; |
| 329 | } |
| 330 | |
Bill Pemberton | 33897cc | 2012-12-03 09:23:58 -0500 | [diff] [blame] | 331 | static int xgmac_mdio_remove(struct platform_device *pdev) |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 332 | { |
Jingoo Han | 8513fbd | 2013-05-23 00:52:31 +0000 | [diff] [blame] | 333 | struct mii_bus *bus = platform_get_drvdata(pdev); |
Tobias Waldekranz | 3f7c239 | 2022-01-18 22:50:53 +0100 | [diff] [blame] | 334 | struct mdio_fsl_priv *priv = bus->priv; |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 335 | |
| 336 | mdiobus_unregister(bus); |
Tobias Waldekranz | 3f7c239 | 2022-01-18 22:50:53 +0100 | [diff] [blame] | 337 | iounmap(priv->mdio_base); |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 338 | mdiobus_free(bus); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Fabian Frederick | 94e5a2a | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 343 | static const struct of_device_id xgmac_mdio_match[] = { |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 344 | { |
| 345 | .compatible = "fsl,fman-xmdio", |
| 346 | }, |
Andy Fleming | 1fcf77c | 2015-01-04 17:36:02 +0800 | [diff] [blame] | 347 | { |
| 348 | .compatible = "fsl,fman-memac-mdio", |
| 349 | }, |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 350 | {}, |
| 351 | }; |
| 352 | MODULE_DEVICE_TABLE(of, xgmac_mdio_match); |
| 353 | |
Calvin Johnson | 229f4bb | 2020-06-22 20:35:33 +0530 | [diff] [blame] | 354 | static const struct acpi_device_id xgmac_acpi_match[] = { |
| 355 | { "NXP0006" }, |
| 356 | { } |
| 357 | }; |
| 358 | MODULE_DEVICE_TABLE(acpi, xgmac_acpi_match); |
| 359 | |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 360 | static struct platform_driver xgmac_mdio_driver = { |
| 361 | .driver = { |
| 362 | .name = "fsl-fman_xmdio", |
| 363 | .of_match_table = xgmac_mdio_match, |
Calvin Johnson | 229f4bb | 2020-06-22 20:35:33 +0530 | [diff] [blame] | 364 | .acpi_match_table = xgmac_acpi_match, |
Timur Tabi | 9f35a73 | 2012-08-20 09:26:39 +0000 | [diff] [blame] | 365 | }, |
| 366 | .probe = xgmac_mdio_probe, |
| 367 | .remove = xgmac_mdio_remove, |
| 368 | }; |
| 369 | |
| 370 | module_platform_driver(xgmac_mdio_driver); |
| 371 | |
| 372 | MODULE_DESCRIPTION("Freescale QorIQ 10G MDIO Controller"); |
| 373 | MODULE_LICENSE("GPL v2"); |