Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2012 Freescale Semiconductor, Inc. |
Andy Fleming | b21f87a3 | 2014-07-25 17:39:08 -0500 | [diff] [blame] | 4 | * Andy Fleming <afleming@gmail.com> |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 5 | * Roy Zang <tie-fei.zang@freescale.com> |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 6 | * Some part is taken from tsec.c |
| 7 | */ |
| 8 | #include <common.h> |
| 9 | #include <miiphy.h> |
| 10 | #include <phy.h> |
| 11 | #include <asm/io.h> |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 12 | #include <fsl_memac.h> |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 13 | #include <fm_eth.h> |
| 14 | |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 15 | #ifdef CONFIG_SYS_MEMAC_LITTLE_ENDIAN |
| 16 | #define memac_out_32(a, v) out_le32(a, v) |
| 17 | #define memac_clrbits_32(a, v) clrbits_le32(a, v) |
| 18 | #define memac_setbits_32(a, v) setbits_le32(a, v) |
| 19 | #else |
| 20 | #define memac_out_32(a, v) out_be32(a, v) |
| 21 | #define memac_clrbits_32(a, v) clrbits_be32(a, v) |
| 22 | #define memac_setbits_32(a, v) setbits_be32(a, v) |
| 23 | #endif |
| 24 | |
Madalin Bucur | 20e0f62 | 2020-04-23 16:25:18 +0300 | [diff] [blame] | 25 | struct fm_mdio_priv { |
| 26 | struct memac_mdio_controller *regs; |
| 27 | }; |
Madalin Bucur | 20e0f62 | 2020-04-23 16:25:18 +0300 | [diff] [blame] | 28 | |
Ioana Ciornei | 64a0fb4 | 2020-12-09 13:31:58 +0200 | [diff] [blame] | 29 | #define MAX_NUM_RETRIES 1000 |
| 30 | |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 31 | static u32 memac_in_32(u32 *reg) |
| 32 | { |
| 33 | #ifdef CONFIG_SYS_MEMAC_LITTLE_ENDIAN |
| 34 | return in_le32(reg); |
| 35 | #else |
| 36 | return in_be32(reg); |
| 37 | #endif |
| 38 | } |
| 39 | |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 40 | /* |
Ioana Ciornei | 64a0fb4 | 2020-12-09 13:31:58 +0200 | [diff] [blame] | 41 | * Wait until the MDIO bus is free |
| 42 | */ |
| 43 | static int memac_wait_until_free(struct memac_mdio_controller *regs) |
| 44 | { |
| 45 | unsigned int timeout = MAX_NUM_RETRIES; |
| 46 | |
| 47 | while ((memac_in_32(®s->mdio_stat) & MDIO_STAT_BSY) && timeout--) |
| 48 | ; |
| 49 | |
| 50 | if (!timeout) { |
| 51 | printf("timeout waiting for MDIO bus to be free\n"); |
| 52 | return -ETIMEDOUT; |
| 53 | } |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Wait till the MDIO read or write operation is complete |
| 60 | */ |
| 61 | static int memac_wait_until_done(struct memac_mdio_controller *regs) |
| 62 | { |
| 63 | unsigned int timeout = MAX_NUM_RETRIES; |
| 64 | |
Markus Koch | eab18b3 | 2022-01-11 19:22:54 +0100 | [diff] [blame] | 65 | while ((memac_in_32(®s->mdio_stat) & MDIO_STAT_BSY) && timeout--) |
Ioana Ciornei | 64a0fb4 | 2020-12-09 13:31:58 +0200 | [diff] [blame] | 66 | ; |
| 67 | |
| 68 | if (!timeout) { |
| 69 | printf("timeout waiting for MDIO operation to complete\n"); |
| 70 | return -ETIMEDOUT; |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 77 | * Write value to the PHY for this device to the register at regnum, waiting |
| 78 | * until the write is done before it returns. All PHY configuration has to be |
| 79 | * done through the TSEC1 MIIM regs |
| 80 | */ |
| 81 | int memac_mdio_write(struct mii_dev *bus, int port_addr, int dev_addr, |
| 82 | int regnum, u16 value) |
| 83 | { |
Madalin Bucur | 20e0f62 | 2020-04-23 16:25:18 +0300 | [diff] [blame] | 84 | struct memac_mdio_controller *regs; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 85 | u32 mdio_ctl; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 86 | u32 c45 = 1; /* Default to 10G interface */ |
Ioana Ciornei | 64a0fb4 | 2020-12-09 13:31:58 +0200 | [diff] [blame] | 87 | int err; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 88 | |
Madalin Bucur | 20e0f62 | 2020-04-23 16:25:18 +0300 | [diff] [blame] | 89 | struct fm_mdio_priv *priv; |
| 90 | |
| 91 | if (!bus->priv) |
| 92 | return -EINVAL; |
| 93 | priv = dev_get_priv(bus->priv); |
| 94 | regs = priv->regs; |
| 95 | debug("memac_mdio_write(regs %p, port %d, dev %d, reg %d, val %#x)\n", |
| 96 | regs, port_addr, dev_addr, regnum, value); |
Madalin Bucur | 20e0f62 | 2020-04-23 16:25:18 +0300 | [diff] [blame] | 97 | |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 98 | if (dev_addr == MDIO_DEVAD_NONE) { |
| 99 | c45 = 0; /* clause 22 */ |
| 100 | dev_addr = regnum & 0x1f; |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 101 | memac_clrbits_32(®s->mdio_stat, MDIO_STAT_ENC); |
Shaohui Xie | 3a7ed5a | 2014-04-22 18:21:37 +0800 | [diff] [blame] | 102 | } else |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 103 | memac_setbits_32(®s->mdio_stat, MDIO_STAT_ENC); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 104 | |
Ioana Ciornei | 64a0fb4 | 2020-12-09 13:31:58 +0200 | [diff] [blame] | 105 | err = memac_wait_until_free(regs); |
| 106 | if (err) |
| 107 | return err; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 108 | |
| 109 | /* Set the port and dev addr */ |
| 110 | mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr); |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 111 | memac_out_32(®s->mdio_ctl, mdio_ctl); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 112 | |
| 113 | /* Set the register address */ |
| 114 | if (c45) |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 115 | memac_out_32(®s->mdio_addr, regnum & 0xffff); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 116 | |
Ioana Ciornei | 64a0fb4 | 2020-12-09 13:31:58 +0200 | [diff] [blame] | 117 | err = memac_wait_until_free(regs); |
| 118 | if (err) |
| 119 | return err; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 120 | |
| 121 | /* Write the value to the register */ |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 122 | memac_out_32(®s->mdio_data, MDIO_DATA(value)); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 123 | |
Ioana Ciornei | 64a0fb4 | 2020-12-09 13:31:58 +0200 | [diff] [blame] | 124 | err = memac_wait_until_done(regs); |
| 125 | if (err) |
| 126 | return err; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Reads from register regnum in the PHY for device dev, returning the value. |
| 133 | * Clears miimcom first. All PHY configuration has to be done through the |
| 134 | * TSEC1 MIIM regs |
| 135 | */ |
| 136 | int memac_mdio_read(struct mii_dev *bus, int port_addr, int dev_addr, |
| 137 | int regnum) |
| 138 | { |
Madalin Bucur | 20e0f62 | 2020-04-23 16:25:18 +0300 | [diff] [blame] | 139 | struct memac_mdio_controller *regs; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 140 | u32 mdio_ctl; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 141 | u32 c45 = 1; |
Ioana Ciornei | 64a0fb4 | 2020-12-09 13:31:58 +0200 | [diff] [blame] | 142 | int err; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 143 | |
Madalin Bucur | 20e0f62 | 2020-04-23 16:25:18 +0300 | [diff] [blame] | 144 | struct fm_mdio_priv *priv; |
| 145 | |
| 146 | if (!bus->priv) |
| 147 | return -EINVAL; |
| 148 | priv = dev_get_priv(bus->priv); |
| 149 | regs = priv->regs; |
Madalin Bucur | 20e0f62 | 2020-04-23 16:25:18 +0300 | [diff] [blame] | 150 | |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 151 | if (dev_addr == MDIO_DEVAD_NONE) { |
| 152 | c45 = 0; /* clause 22 */ |
| 153 | dev_addr = regnum & 0x1f; |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 154 | memac_clrbits_32(®s->mdio_stat, MDIO_STAT_ENC); |
Shaohui Xie | 3a7ed5a | 2014-04-22 18:21:37 +0800 | [diff] [blame] | 155 | } else |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 156 | memac_setbits_32(®s->mdio_stat, MDIO_STAT_ENC); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 157 | |
Ioana Ciornei | 64a0fb4 | 2020-12-09 13:31:58 +0200 | [diff] [blame] | 158 | err = memac_wait_until_free(regs); |
| 159 | if (err) |
| 160 | return err; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 161 | |
| 162 | /* Set the Port and Device Addrs */ |
| 163 | mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr); |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 164 | memac_out_32(®s->mdio_ctl, mdio_ctl); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 165 | |
| 166 | /* Set the register address */ |
| 167 | if (c45) |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 168 | memac_out_32(®s->mdio_addr, regnum & 0xffff); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 169 | |
Ioana Ciornei | 64a0fb4 | 2020-12-09 13:31:58 +0200 | [diff] [blame] | 170 | err = memac_wait_until_free(regs); |
| 171 | if (err) |
| 172 | return err; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 173 | |
| 174 | /* Initiate the read */ |
| 175 | mdio_ctl |= MDIO_CTL_READ; |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 176 | memac_out_32(®s->mdio_ctl, mdio_ctl); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 177 | |
Ioana Ciornei | 64a0fb4 | 2020-12-09 13:31:58 +0200 | [diff] [blame] | 178 | err = memac_wait_until_done(regs); |
| 179 | if (err) |
| 180 | return err; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 181 | |
| 182 | /* Return all Fs if nothing was there */ |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 183 | if (memac_in_32(®s->mdio_stat) & MDIO_STAT_RD_ER) |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 184 | return 0xffff; |
| 185 | |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 186 | return memac_in_32(®s->mdio_data) & 0xffff; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | int memac_mdio_reset(struct mii_dev *bus) |
| 190 | { |
| 191 | return 0; |
| 192 | } |
| 193 | |
Madalin Bucur | 20e0f62 | 2020-04-23 16:25:18 +0300 | [diff] [blame] | 194 | #if defined(CONFIG_PHYLIB) && defined(CONFIG_DM_MDIO) |
| 195 | static int fm_mdio_read(struct udevice *dev, int addr, int devad, int reg) |
| 196 | { |
| 197 | struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) : |
| 198 | NULL; |
| 199 | |
| 200 | if (pdata && pdata->mii_bus) |
| 201 | return memac_mdio_read(pdata->mii_bus, addr, devad, reg); |
| 202 | |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | static int fm_mdio_write(struct udevice *dev, int addr, int devad, int reg, |
| 207 | u16 val) |
| 208 | { |
| 209 | struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) : |
| 210 | NULL; |
| 211 | |
| 212 | if (pdata && pdata->mii_bus) |
| 213 | return memac_mdio_write(pdata->mii_bus, addr, devad, reg, val); |
| 214 | |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | static int fm_mdio_reset(struct udevice *dev) |
| 219 | { |
| 220 | struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) : |
| 221 | NULL; |
| 222 | |
| 223 | if (pdata && pdata->mii_bus) |
| 224 | return memac_mdio_reset(pdata->mii_bus); |
| 225 | |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | static const struct mdio_ops fm_mdio_ops = { |
| 230 | .read = fm_mdio_read, |
| 231 | .write = fm_mdio_write, |
| 232 | .reset = fm_mdio_reset, |
| 233 | }; |
| 234 | |
| 235 | static const struct udevice_id fm_mdio_ids[] = { |
| 236 | { .compatible = "fsl,fman-memac-mdio" }, |
| 237 | {} |
| 238 | }; |
| 239 | |
| 240 | static int fm_mdio_probe(struct udevice *dev) |
| 241 | { |
| 242 | struct fm_mdio_priv *priv = (dev) ? dev_get_priv(dev) : NULL; |
| 243 | struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) : |
| 244 | NULL; |
| 245 | |
| 246 | if (!dev) { |
| 247 | printf("%s dev = NULL\n", __func__); |
| 248 | return -1; |
| 249 | } |
| 250 | if (!priv) { |
| 251 | printf("dev_get_priv(dev %p) = NULL\n", dev); |
| 252 | return -1; |
| 253 | } |
| 254 | priv->regs = (void *)(uintptr_t)dev_read_addr(dev); |
| 255 | debug("%s priv %p @ regs %p, pdata %p\n", __func__, |
| 256 | priv, priv->regs, pdata); |
| 257 | |
| 258 | /* |
| 259 | * On some platforms like B4860, default value of MDIO_CLK_DIV bits |
| 260 | * in mdio_stat(mdio_cfg) register generates MDIO clock too high |
| 261 | * (much higher than 2.5MHz), violating the IEEE specs. |
| 262 | * On other platforms like T1040, default value of MDIO_CLK_DIV bits |
| 263 | * is zero, so MDIO clock is disabled. |
| 264 | * So, for proper functioning of MDIO, MDIO_CLK_DIV bits needs to |
| 265 | * be properly initialized. |
| 266 | * The default NEG bit should be '1' as per FMANv3 RM, but on platforms |
| 267 | * like T2080QDS, this bit default is '0', which leads to MDIO failure |
| 268 | * on XAUI PHY, so set this bit definitely. |
| 269 | */ |
| 270 | if (priv && priv->regs && priv->regs->mdio_stat) |
| 271 | memac_setbits_32(&priv->regs->mdio_stat, |
| 272 | MDIO_STAT_CLKDIV(258) | MDIO_STAT_NEG); |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static int fm_mdio_remove(struct udevice *dev) |
| 278 | { |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | U_BOOT_DRIVER(fman_mdio) = { |
| 283 | .name = "fman_mdio", |
| 284 | .id = UCLASS_MDIO, |
| 285 | .of_match = fm_mdio_ids, |
| 286 | .probe = fm_mdio_probe, |
| 287 | .remove = fm_mdio_remove, |
| 288 | .ops = &fm_mdio_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 289 | .priv_auto = sizeof(struct fm_mdio_priv), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 290 | .plat_auto = sizeof(struct mdio_perdev_priv), |
Madalin Bucur | 20e0f62 | 2020-04-23 16:25:18 +0300 | [diff] [blame] | 291 | }; |
| 292 | #endif /* CONFIG_PHYLIB && CONFIG_DM_MDIO */ |