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