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