Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Micrel PHY drivers |
| 4 | * |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 5 | * Copyright 2010-2011 Freescale Semiconductor, Inc. |
| 6 | * author Andy Fleming |
David Andrey | f0d83c4 | 2013-02-06 22:18:37 +0100 | [diff] [blame] | 7 | * (C) 2012 NetModule AG, David Andrey, added KSZ9031 |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 8 | * (C) Copyright 2017 Adaptrum, Inc. |
| 9 | * Written by Alexandru Gagniuc <alex.g@adaptrum.com> for Adaptrum, Inc. |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 10 | */ |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 11 | #include <common.h> |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 13 | #include <env.h> |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 14 | #include <errno.h> |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 15 | #include <micrel.h> |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 16 | #include <phy.h> |
| 17 | |
Pavel Machek | 5f02211 | 2014-09-09 14:26:51 +0200 | [diff] [blame] | 18 | /* |
David Andrey | f0d83c4 | 2013-02-06 22:18:37 +0100 | [diff] [blame] | 19 | * KSZ9021 - KSZ9031 common |
| 20 | */ |
| 21 | |
| 22 | #define MII_KSZ90xx_PHY_CTL 0x1f |
| 23 | #define MIIM_KSZ90xx_PHYCTL_1000 (1 << 6) |
| 24 | #define MIIM_KSZ90xx_PHYCTL_100 (1 << 5) |
| 25 | #define MIIM_KSZ90xx_PHYCTL_10 (1 << 4) |
| 26 | #define MIIM_KSZ90xx_PHYCTL_DUPLEX (1 << 3) |
| 27 | |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 28 | /* KSZ9021 PHY Registers */ |
| 29 | #define MII_KSZ9021_EXTENDED_CTRL 0x0b |
| 30 | #define MII_KSZ9021_EXTENDED_DATAW 0x0c |
| 31 | #define MII_KSZ9021_EXTENDED_DATAR 0x0d |
| 32 | |
| 33 | #define CTRL1000_PREFER_MASTER (1 << 10) |
| 34 | #define CTRL1000_CONFIG_MASTER (1 << 11) |
| 35 | #define CTRL1000_MANUAL_CONFIG (1 << 12) |
| 36 | |
James Byrne | 457107f | 2019-03-04 17:40:33 +0000 | [diff] [blame] | 37 | #define KSZ9021_PS_TO_REG 120 |
| 38 | |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 39 | /* KSZ9031 PHY Registers */ |
| 40 | #define MII_KSZ9031_MMD_ACCES_CTRL 0x0d |
| 41 | #define MII_KSZ9031_MMD_REG_DATA 0x0e |
| 42 | |
James Byrne | 457107f | 2019-03-04 17:40:33 +0000 | [diff] [blame] | 43 | #define KSZ9031_PS_TO_REG 60 |
| 44 | |
David Andrey | f0d83c4 | 2013-02-06 22:18:37 +0100 | [diff] [blame] | 45 | static int ksz90xx_startup(struct phy_device *phydev) |
| 46 | { |
| 47 | unsigned phy_ctl; |
Michal Simek | 5ff8966 | 2016-05-18 12:46:12 +0200 | [diff] [blame] | 48 | int ret; |
| 49 | |
| 50 | ret = genphy_update_link(phydev); |
| 51 | if (ret) |
| 52 | return ret; |
| 53 | |
David Andrey | f0d83c4 | 2013-02-06 22:18:37 +0100 | [diff] [blame] | 54 | phy_ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_KSZ90xx_PHY_CTL); |
| 55 | |
| 56 | if (phy_ctl & MIIM_KSZ90xx_PHYCTL_DUPLEX) |
| 57 | phydev->duplex = DUPLEX_FULL; |
| 58 | else |
| 59 | phydev->duplex = DUPLEX_HALF; |
| 60 | |
| 61 | if (phy_ctl & MIIM_KSZ90xx_PHYCTL_1000) |
| 62 | phydev->speed = SPEED_1000; |
| 63 | else if (phy_ctl & MIIM_KSZ90xx_PHYCTL_100) |
| 64 | phydev->speed = SPEED_100; |
| 65 | else if (phy_ctl & MIIM_KSZ90xx_PHYCTL_10) |
| 66 | phydev->speed = SPEED_10; |
| 67 | return 0; |
| 68 | } |
David Andrey | f0d83c4 | 2013-02-06 22:18:37 +0100 | [diff] [blame] | 69 | |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 70 | /* Common OF config bits for KSZ9021 and KSZ9031 */ |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 71 | struct ksz90x1_reg_field { |
| 72 | const char *name; |
| 73 | const u8 size; /* Size of the bitfield, in bits */ |
| 74 | const u8 off; /* Offset from bit 0 */ |
| 75 | const u8 dflt; /* Default value */ |
| 76 | }; |
| 77 | |
| 78 | struct ksz90x1_ofcfg { |
| 79 | const u16 reg; |
| 80 | const u16 devad; |
| 81 | const struct ksz90x1_reg_field *grp; |
| 82 | const u16 grpsz; |
| 83 | }; |
| 84 | |
| 85 | static const struct ksz90x1_reg_field ksz90x1_rxd_grp[] = { |
| 86 | { "rxd0-skew-ps", 4, 0, 0x7 }, { "rxd1-skew-ps", 4, 4, 0x7 }, |
| 87 | { "rxd2-skew-ps", 4, 8, 0x7 }, { "rxd3-skew-ps", 4, 12, 0x7 } |
| 88 | }; |
| 89 | |
| 90 | static const struct ksz90x1_reg_field ksz90x1_txd_grp[] = { |
| 91 | { "txd0-skew-ps", 4, 0, 0x7 }, { "txd1-skew-ps", 4, 4, 0x7 }, |
| 92 | { "txd2-skew-ps", 4, 8, 0x7 }, { "txd3-skew-ps", 4, 12, 0x7 }, |
| 93 | }; |
| 94 | |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 95 | static const struct ksz90x1_reg_field ksz9021_clk_grp[] = { |
| 96 | { "txen-skew-ps", 4, 0, 0x7 }, { "txc-skew-ps", 4, 4, 0x7 }, |
| 97 | { "rxdv-skew-ps", 4, 8, 0x7 }, { "rxc-skew-ps", 4, 12, 0x7 }, |
| 98 | }; |
| 99 | |
| 100 | static const struct ksz90x1_reg_field ksz9031_ctl_grp[] = { |
| 101 | { "txen-skew-ps", 4, 0, 0x7 }, { "rxdv-skew-ps", 4, 4, 0x7 } |
| 102 | }; |
| 103 | |
| 104 | static const struct ksz90x1_reg_field ksz9031_clk_grp[] = { |
| 105 | { "rxc-skew-ps", 5, 0, 0xf }, { "txc-skew-ps", 5, 5, 0xf } |
| 106 | }; |
| 107 | |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 108 | static int ksz90x1_of_config_group(struct phy_device *phydev, |
James Byrne | 457107f | 2019-03-04 17:40:33 +0000 | [diff] [blame] | 109 | struct ksz90x1_ofcfg *ofcfg, |
| 110 | int ps_to_regval) |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 111 | { |
| 112 | struct udevice *dev = phydev->dev; |
| 113 | struct phy_driver *drv = phydev->drv; |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 114 | int val[4]; |
| 115 | int i, changed = 0, offset, max; |
| 116 | u16 regval = 0; |
James Byrne | 9e79144 | 2019-03-04 17:40:34 +0000 | [diff] [blame] | 117 | ofnode node; |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 118 | |
| 119 | if (!drv || !drv->writeext) |
| 120 | return -EOPNOTSUPP; |
| 121 | |
Marek Vasut | 7bbc25b | 2021-01-17 00:16:16 +0100 | [diff] [blame] | 122 | node = phydev->node; |
| 123 | |
| 124 | if (!ofnode_valid(node)) { |
| 125 | /* Look for a PHY node under the Ethernet node */ |
| 126 | node = dev_read_subnode(dev, "ethernet-phy"); |
| 127 | } |
| 128 | |
James Byrne | 9e79144 | 2019-03-04 17:40:34 +0000 | [diff] [blame] | 129 | if (!ofnode_valid(node)) { |
| 130 | /* No node found, look in the Ethernet node */ |
| 131 | node = dev_ofnode(dev); |
| 132 | } |
| 133 | |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 134 | for (i = 0; i < ofcfg->grpsz; i++) { |
James Byrne | 9e79144 | 2019-03-04 17:40:34 +0000 | [diff] [blame] | 135 | val[i] = ofnode_read_u32_default(node, ofcfg->grp[i].name, ~0); |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 136 | offset = ofcfg->grp[i].off; |
| 137 | if (val[i] == -1) { |
| 138 | /* Default register value for KSZ9021 */ |
| 139 | regval |= ofcfg->grp[i].dflt << offset; |
| 140 | } else { |
| 141 | changed = 1; /* Value was changed in OF */ |
| 142 | /* Calculate the register value and fix corner cases */ |
Andreas Pretzsch | f7c689d | 2018-11-29 20:04:53 +0100 | [diff] [blame] | 143 | max = (1 << ofcfg->grp[i].size) - 1; |
| 144 | if (val[i] > ps_to_regval * max) { |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 145 | regval |= max << offset; |
| 146 | } else { |
| 147 | regval |= (val[i] / ps_to_regval) << offset; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (!changed) |
| 153 | return 0; |
| 154 | |
| 155 | return drv->writeext(phydev, 0, ofcfg->devad, ofcfg->reg, regval); |
| 156 | } |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 157 | |
| 158 | static int ksz9021_of_config(struct phy_device *phydev) |
| 159 | { |
| 160 | struct ksz90x1_ofcfg ofcfg[] = { |
| 161 | { MII_KSZ9021_EXT_RGMII_RX_DATA_SKEW, 0, ksz90x1_rxd_grp, 4 }, |
| 162 | { MII_KSZ9021_EXT_RGMII_TX_DATA_SKEW, 0, ksz90x1_txd_grp, 4 }, |
| 163 | { MII_KSZ9021_EXT_RGMII_CLOCK_SKEW, 0, ksz9021_clk_grp, 4 }, |
| 164 | }; |
| 165 | int i, ret = 0; |
| 166 | |
Marek Vasut | 0c76630 | 2016-11-14 15:08:42 +0100 | [diff] [blame] | 167 | for (i = 0; i < ARRAY_SIZE(ofcfg); i++) { |
James Byrne | 457107f | 2019-03-04 17:40:33 +0000 | [diff] [blame] | 168 | ret = ksz90x1_of_config_group(phydev, &ofcfg[i], |
| 169 | KSZ9021_PS_TO_REG); |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 170 | if (ret) |
| 171 | return ret; |
Marek Vasut | 0c76630 | 2016-11-14 15:08:42 +0100 | [diff] [blame] | 172 | } |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 173 | |
| 174 | return 0; |
| 175 | } |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 176 | |
| 177 | static int ksz9031_of_config(struct phy_device *phydev) |
| 178 | { |
| 179 | struct ksz90x1_ofcfg ofcfg[] = { |
| 180 | { MII_KSZ9031_EXT_RGMII_CTRL_SIG_SKEW, 2, ksz9031_ctl_grp, 2 }, |
| 181 | { MII_KSZ9031_EXT_RGMII_RX_DATA_SKEW, 2, ksz90x1_rxd_grp, 4 }, |
| 182 | { MII_KSZ9031_EXT_RGMII_TX_DATA_SKEW, 2, ksz90x1_txd_grp, 4 }, |
| 183 | { MII_KSZ9031_EXT_RGMII_CLOCK_SKEW, 2, ksz9031_clk_grp, 2 }, |
| 184 | }; |
| 185 | int i, ret = 0; |
| 186 | |
| 187 | for (i = 0; i < ARRAY_SIZE(ofcfg); i++) { |
James Byrne | 457107f | 2019-03-04 17:40:33 +0000 | [diff] [blame] | 188 | ret = ksz90x1_of_config_group(phydev, &ofcfg[i], |
| 189 | KSZ9031_PS_TO_REG); |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 190 | if (ret) |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int ksz9031_center_flp_timing(struct phy_device *phydev) |
| 198 | { |
| 199 | struct phy_driver *drv = phydev->drv; |
| 200 | int ret = 0; |
| 201 | |
| 202 | if (!drv || !drv->writeext) |
| 203 | return -EOPNOTSUPP; |
| 204 | |
| 205 | ret = drv->writeext(phydev, 0, 0, MII_KSZ9031_FLP_BURST_TX_LO, 0x1A80); |
| 206 | if (ret) |
| 207 | return ret; |
| 208 | |
| 209 | ret = drv->writeext(phydev, 0, 0, MII_KSZ9031_FLP_BURST_TX_HI, 0x6); |
| 210 | return ret; |
| 211 | } |
| 212 | |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 213 | /* |
| 214 | * KSZ9021 |
| 215 | */ |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 216 | int ksz9021_phy_extended_write(struct phy_device *phydev, int regnum, u16 val) |
| 217 | { |
| 218 | /* extended registers */ |
| 219 | phy_write(phydev, MDIO_DEVAD_NONE, |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 220 | MII_KSZ9021_EXTENDED_CTRL, regnum | 0x8000); |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 221 | return phy_write(phydev, MDIO_DEVAD_NONE, |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 222 | MII_KSZ9021_EXTENDED_DATAW, val); |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | int ksz9021_phy_extended_read(struct phy_device *phydev, int regnum) |
| 226 | { |
| 227 | /* extended registers */ |
| 228 | phy_write(phydev, MDIO_DEVAD_NONE, MII_KSZ9021_EXTENDED_CTRL, regnum); |
| 229 | return phy_read(phydev, MDIO_DEVAD_NONE, MII_KSZ9021_EXTENDED_DATAR); |
| 230 | } |
| 231 | |
Stefano Babic | a8aa299 | 2013-09-02 15:42:31 +0200 | [diff] [blame] | 232 | |
| 233 | static int ksz9021_phy_extread(struct phy_device *phydev, int addr, int devaddr, |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 234 | int regnum) |
Stefano Babic | a8aa299 | 2013-09-02 15:42:31 +0200 | [diff] [blame] | 235 | { |
| 236 | return ksz9021_phy_extended_read(phydev, regnum); |
| 237 | } |
| 238 | |
| 239 | static int ksz9021_phy_extwrite(struct phy_device *phydev, int addr, |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 240 | int devaddr, int regnum, u16 val) |
Stefano Babic | a8aa299 | 2013-09-02 15:42:31 +0200 | [diff] [blame] | 241 | { |
| 242 | return ksz9021_phy_extended_write(phydev, regnum, val); |
| 243 | } |
| 244 | |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 245 | static int ksz9021_config(struct phy_device *phydev) |
| 246 | { |
| 247 | unsigned ctrl1000 = 0; |
| 248 | const unsigned master = CTRL1000_PREFER_MASTER | |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 249 | CTRL1000_CONFIG_MASTER | CTRL1000_MANUAL_CONFIG; |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 250 | unsigned features = phydev->drv->features; |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 251 | int ret; |
| 252 | |
| 253 | ret = ksz9021_of_config(phydev); |
| 254 | if (ret) |
| 255 | return ret; |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 256 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 257 | if (env_get("disable_giga")) |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 258 | features &= ~(SUPPORTED_1000baseT_Half | |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 259 | SUPPORTED_1000baseT_Full); |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 260 | /* force master mode for 1000BaseT due to chip errata */ |
| 261 | if (features & SUPPORTED_1000baseT_Half) |
| 262 | ctrl1000 |= ADVERTISE_1000HALF | master; |
| 263 | if (features & SUPPORTED_1000baseT_Full) |
| 264 | ctrl1000 |= ADVERTISE_1000FULL | master; |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 265 | phydev->advertising = features; |
| 266 | phydev->supported = features; |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 267 | phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, ctrl1000); |
| 268 | genphy_config_aneg(phydev); |
| 269 | genphy_restart_aneg(phydev); |
| 270 | return 0; |
| 271 | } |
| 272 | |
Marek Vasut | ce250e9 | 2023-03-19 18:02:56 +0100 | [diff] [blame] | 273 | U_BOOT_PHY_DRIVER(ksz9021) = { |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 274 | .name = "Micrel ksz9021", |
| 275 | .uid = 0x221610, |
James Byrne | bc292c2 | 2019-03-06 12:48:27 +0000 | [diff] [blame] | 276 | .mask = 0xfffffe, |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 277 | .features = PHY_GBIT_FEATURES, |
| 278 | .config = &ksz9021_config, |
David Andrey | f0d83c4 | 2013-02-06 22:18:37 +0100 | [diff] [blame] | 279 | .startup = &ksz90xx_startup, |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 280 | .shutdown = &genphy_shutdown, |
Stefano Babic | a8aa299 | 2013-09-02 15:42:31 +0200 | [diff] [blame] | 281 | .writeext = &ksz9021_phy_extwrite, |
| 282 | .readext = &ksz9021_phy_extread, |
Troy Kisky | 80b6b09 | 2012-02-07 14:08:48 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 285 | /* |
David Andrey | f0d83c4 | 2013-02-06 22:18:37 +0100 | [diff] [blame] | 286 | * KSZ9031 |
| 287 | */ |
SARTRE Leo | eaf68ac | 2013-04-30 16:57:25 +0200 | [diff] [blame] | 288 | int ksz9031_phy_extended_write(struct phy_device *phydev, |
| 289 | int devaddr, int regnum, u16 mode, u16 val) |
| 290 | { |
| 291 | /*select register addr for mmd*/ |
| 292 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 293 | MII_KSZ9031_MMD_ACCES_CTRL, devaddr); |
| 294 | /*select register for mmd*/ |
| 295 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 296 | MII_KSZ9031_MMD_REG_DATA, regnum); |
| 297 | /*setup mode*/ |
| 298 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 299 | MII_KSZ9031_MMD_ACCES_CTRL, (mode | devaddr)); |
| 300 | /*write the value*/ |
| 301 | return phy_write(phydev, MDIO_DEVAD_NONE, |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 302 | MII_KSZ9031_MMD_REG_DATA, val); |
SARTRE Leo | eaf68ac | 2013-04-30 16:57:25 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | int ksz9031_phy_extended_read(struct phy_device *phydev, int devaddr, |
| 306 | int regnum, u16 mode) |
| 307 | { |
| 308 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 309 | MII_KSZ9031_MMD_ACCES_CTRL, devaddr); |
| 310 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 311 | MII_KSZ9031_MMD_REG_DATA, regnum); |
| 312 | phy_write(phydev, MDIO_DEVAD_NONE, |
| 313 | MII_KSZ9031_MMD_ACCES_CTRL, (devaddr | mode)); |
| 314 | return phy_read(phydev, MDIO_DEVAD_NONE, MII_KSZ9031_MMD_REG_DATA); |
| 315 | } |
| 316 | |
Stefano Babic | a8aa299 | 2013-09-02 15:42:31 +0200 | [diff] [blame] | 317 | static int ksz9031_phy_extread(struct phy_device *phydev, int addr, int devaddr, |
| 318 | int regnum) |
| 319 | { |
| 320 | return ksz9031_phy_extended_read(phydev, devaddr, regnum, |
| 321 | MII_KSZ9031_MOD_DATA_NO_POST_INC); |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 322 | } |
Stefano Babic | a8aa299 | 2013-09-02 15:42:31 +0200 | [diff] [blame] | 323 | |
| 324 | static int ksz9031_phy_extwrite(struct phy_device *phydev, int addr, |
| 325 | int devaddr, int regnum, u16 val) |
| 326 | { |
| 327 | return ksz9031_phy_extended_write(phydev, devaddr, regnum, |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 328 | MII_KSZ9031_MOD_DATA_POST_INC_RW, val); |
| 329 | } |
Stefano Babic | a8aa299 | 2013-09-02 15:42:31 +0200 | [diff] [blame] | 330 | |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 331 | static int ksz9031_config(struct phy_device *phydev) |
| 332 | { |
| 333 | int ret; |
Sebastien Bourdelin | 3a6e033 | 2017-07-28 15:59:22 -0400 | [diff] [blame] | 334 | |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 335 | ret = ksz9031_of_config(phydev); |
| 336 | if (ret) |
| 337 | return ret; |
Ash Charles | 3f55bb6 | 2016-10-21 17:31:33 -0400 | [diff] [blame] | 338 | ret = ksz9031_center_flp_timing(phydev); |
| 339 | if (ret) |
| 340 | return ret; |
Sebastien Bourdelin | 3a6e033 | 2017-07-28 15:59:22 -0400 | [diff] [blame] | 341 | |
| 342 | /* add an option to disable the gigabit feature of this PHY */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 343 | if (env_get("disable_giga")) { |
Sebastien Bourdelin | 3a6e033 | 2017-07-28 15:59:22 -0400 | [diff] [blame] | 344 | unsigned features; |
| 345 | unsigned bmcr; |
| 346 | |
| 347 | /* disable speed 1000 in features supported by the PHY */ |
| 348 | features = phydev->drv->features; |
| 349 | features &= ~(SUPPORTED_1000baseT_Half | |
| 350 | SUPPORTED_1000baseT_Full); |
| 351 | phydev->advertising = phydev->supported = features; |
| 352 | |
| 353 | /* disable speed 1000 in Basic Control Register */ |
| 354 | bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); |
| 355 | bmcr &= ~(1 << 6); |
| 356 | phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, bmcr); |
| 357 | |
| 358 | /* disable speed 1000 in 1000Base-T Control Register */ |
| 359 | phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, 0); |
| 360 | |
| 361 | /* start autoneg */ |
| 362 | genphy_config_aneg(phydev); |
| 363 | genphy_restart_aneg(phydev); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 368 | return genphy_config(phydev); |
| 369 | } |
Stefano Babic | a8aa299 | 2013-09-02 15:42:31 +0200 | [diff] [blame] | 370 | |
Marek Vasut | ce250e9 | 2023-03-19 18:02:56 +0100 | [diff] [blame] | 371 | U_BOOT_PHY_DRIVER(ksz9031) = { |
David Andrey | f0d83c4 | 2013-02-06 22:18:37 +0100 | [diff] [blame] | 372 | .name = "Micrel ksz9031", |
Philippe Schenker | be98837 | 2020-03-11 11:59:22 +0100 | [diff] [blame] | 373 | .uid = PHY_ID_KSZ9031, |
| 374 | .mask = MII_KSZ9x31_SILICON_REV_MASK, |
David Andrey | f0d83c4 | 2013-02-06 22:18:37 +0100 | [diff] [blame] | 375 | .features = PHY_GBIT_FEATURES, |
Marek Vasut | 1005ce5 | 2015-12-05 17:41:58 +0100 | [diff] [blame] | 376 | .config = &ksz9031_config, |
David Andrey | f0d83c4 | 2013-02-06 22:18:37 +0100 | [diff] [blame] | 377 | .startup = &ksz90xx_startup, |
| 378 | .shutdown = &genphy_shutdown, |
Stefano Babic | a8aa299 | 2013-09-02 15:42:31 +0200 | [diff] [blame] | 379 | .writeext = &ksz9031_phy_extwrite, |
| 380 | .readext = &ksz9031_phy_extread, |
David Andrey | f0d83c4 | 2013-02-06 22:18:37 +0100 | [diff] [blame] | 381 | }; |
| 382 | |
Philippe Schenker | 3060ecc | 2020-03-11 11:59:23 +0100 | [diff] [blame] | 383 | /* |
| 384 | * KSZ9131 |
| 385 | */ |
Claudiu Beznea | 840a6b0 | 2020-12-03 11:18:30 +0200 | [diff] [blame] | 386 | |
| 387 | #define KSZ9131RN_MMD_COMMON_CTRL_REG 2 |
| 388 | #define KSZ9131RN_RXC_DLL_CTRL 76 |
| 389 | #define KSZ9131RN_TXC_DLL_CTRL 77 |
| 390 | #define KSZ9131RN_DLL_CTRL_BYPASS BIT_MASK(12) |
| 391 | #define KSZ9131RN_DLL_ENABLE_DELAY 0 |
| 392 | #define KSZ9131RN_DLL_DISABLE_DELAY BIT(12) |
| 393 | |
| 394 | static int ksz9131_config_rgmii_delay(struct phy_device *phydev) |
| 395 | { |
| 396 | struct phy_driver *drv = phydev->drv; |
| 397 | u16 rxcdll_val, txcdll_val, val; |
| 398 | int ret; |
| 399 | |
| 400 | switch (phydev->interface) { |
| 401 | case PHY_INTERFACE_MODE_RGMII: |
| 402 | rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY; |
| 403 | txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY; |
| 404 | break; |
| 405 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 406 | rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; |
| 407 | txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; |
| 408 | break; |
| 409 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 410 | rxcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; |
| 411 | txcdll_val = KSZ9131RN_DLL_DISABLE_DELAY; |
| 412 | break; |
| 413 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 414 | rxcdll_val = KSZ9131RN_DLL_DISABLE_DELAY; |
| 415 | txcdll_val = KSZ9131RN_DLL_ENABLE_DELAY; |
| 416 | break; |
| 417 | default: |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | val = drv->readext(phydev, 0, KSZ9131RN_MMD_COMMON_CTRL_REG, |
| 422 | KSZ9131RN_RXC_DLL_CTRL); |
| 423 | val &= ~KSZ9131RN_DLL_CTRL_BYPASS; |
| 424 | val |= rxcdll_val; |
| 425 | ret = drv->writeext(phydev, 0, KSZ9131RN_MMD_COMMON_CTRL_REG, |
| 426 | KSZ9131RN_RXC_DLL_CTRL, val); |
| 427 | if (ret) |
| 428 | return ret; |
| 429 | |
| 430 | val = drv->readext(phydev, 0, KSZ9131RN_MMD_COMMON_CTRL_REG, |
| 431 | KSZ9131RN_TXC_DLL_CTRL); |
| 432 | |
| 433 | val &= ~KSZ9131RN_DLL_CTRL_BYPASS; |
| 434 | val |= txcdll_val; |
| 435 | ret = drv->writeext(phydev, 0, KSZ9131RN_MMD_COMMON_CTRL_REG, |
| 436 | KSZ9131RN_TXC_DLL_CTRL, val); |
| 437 | |
| 438 | return ret; |
| 439 | } |
| 440 | |
Philippe Schenker | 3060ecc | 2020-03-11 11:59:23 +0100 | [diff] [blame] | 441 | static int ksz9131_config(struct phy_device *phydev) |
| 442 | { |
Claudiu Beznea | 840a6b0 | 2020-12-03 11:18:30 +0200 | [diff] [blame] | 443 | int ret; |
| 444 | |
| 445 | if (phy_interface_is_rgmii(phydev)) { |
| 446 | ret = ksz9131_config_rgmii_delay(phydev); |
| 447 | if (ret) |
| 448 | return ret; |
| 449 | } |
Philippe Schenker | 3060ecc | 2020-03-11 11:59:23 +0100 | [diff] [blame] | 450 | |
| 451 | /* add an option to disable the gigabit feature of this PHY */ |
| 452 | if (env_get("disable_giga")) { |
| 453 | unsigned features; |
| 454 | unsigned bmcr; |
| 455 | |
| 456 | /* disable speed 1000 in features supported by the PHY */ |
| 457 | features = phydev->drv->features; |
| 458 | features &= ~(SUPPORTED_1000baseT_Half | |
| 459 | SUPPORTED_1000baseT_Full); |
| 460 | phydev->advertising = phydev->supported = features; |
| 461 | |
| 462 | /* disable speed 1000 in Basic Control Register */ |
| 463 | bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); |
| 464 | bmcr &= ~(1 << 6); |
| 465 | phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, bmcr); |
| 466 | |
| 467 | /* disable speed 1000 in 1000Base-T Control Register */ |
| 468 | phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, 0); |
| 469 | |
| 470 | /* start autoneg */ |
| 471 | genphy_config_aneg(phydev); |
| 472 | genphy_restart_aneg(phydev); |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | return genphy_config(phydev); |
| 478 | } |
| 479 | |
Marek Vasut | ce250e9 | 2023-03-19 18:02:56 +0100 | [diff] [blame] | 480 | U_BOOT_PHY_DRIVER(ksz9131) = { |
Claudiu Beznea | 3f1606f | 2020-12-03 11:18:31 +0200 | [diff] [blame] | 481 | .name = "Micrel ksz9131", |
Philippe Schenker | 3060ecc | 2020-03-11 11:59:23 +0100 | [diff] [blame] | 482 | .uid = PHY_ID_KSZ9131, |
| 483 | .mask = MII_KSZ9x31_SILICON_REV_MASK, |
| 484 | .features = PHY_GBIT_FEATURES, |
| 485 | .config = &ksz9131_config, |
| 486 | .startup = &ksz90xx_startup, |
| 487 | .shutdown = &genphy_shutdown, |
| 488 | .writeext = &ksz9031_phy_extwrite, |
| 489 | .readext = &ksz9031_phy_extread, |
| 490 | }; |
| 491 | |
| 492 | int ksz9xx1_phy_get_id(struct phy_device *phydev) |
| 493 | { |
| 494 | unsigned int phyid; |
| 495 | |
| 496 | get_phy_id(phydev->bus, phydev->addr, MDIO_DEVAD_NONE, &phyid); |
| 497 | |
| 498 | return phyid; |
| 499 | } |