1 1.9 thorpej /* $NetBSD: etphy.c,v 1.9 2020/03/15 23:04:50 thorpej Exp $ */ 2 1.1 jnemeth /* $OpenBSD: etphy.c,v 1.4 2008/04/02 20:12:58 brad Exp $ */ 3 1.1 jnemeth 4 1.1 jnemeth /* 5 1.1 jnemeth * Copyright (c) 2007 The DragonFly Project. All rights reserved. 6 1.1 jnemeth * 7 1.1 jnemeth * This code is derived from software contributed to The DragonFly Project 8 1.1 jnemeth * by Sepherosa Ziehau <sepherosa (at) gmail.com> 9 1.1 jnemeth * 10 1.1 jnemeth * Redistribution and use in source and binary forms, with or without 11 1.1 jnemeth * modification, are permitted provided that the following conditions 12 1.1 jnemeth * are met: 13 1.1 jnemeth * 14 1.1 jnemeth * 1. Redistributions of source code must retain the above copyright 15 1.1 jnemeth * notice, this list of conditions and the following disclaimer. 16 1.1 jnemeth * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 jnemeth * notice, this list of conditions and the following disclaimer in 18 1.1 jnemeth * the documentation and/or other materials provided with the 19 1.1 jnemeth * distribution. 20 1.1 jnemeth * 3. Neither the name of The DragonFly Project nor the names of its 21 1.1 jnemeth * contributors may be used to endorse or promote products derived 22 1.1 jnemeth * from this software without specific, prior written permission. 23 1.1 jnemeth * 24 1.1 jnemeth * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 1.1 jnemeth * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 1.1 jnemeth * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 27 1.1 jnemeth * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 28 1.1 jnemeth * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 29 1.1 jnemeth * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 30 1.1 jnemeth * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 31 1.1 jnemeth * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 32 1.1 jnemeth * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 33 1.1 jnemeth * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 34 1.1 jnemeth * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 1.1 jnemeth * SUCH DAMAGE. 36 1.1 jnemeth * 37 1.1 jnemeth * $DragonFly: src/sys/dev/netif/mii_layer/truephy.c,v 1.1 2007/10/12 14:12:42 sephe Exp $ 38 1.1 jnemeth */ 39 1.1 jnemeth 40 1.1 jnemeth #include <sys/cdefs.h> 41 1.9 thorpej __KERNEL_RCSID(0, "$NetBSD: etphy.c,v 1.9 2020/03/15 23:04:50 thorpej Exp $"); 42 1.1 jnemeth 43 1.1 jnemeth #include <sys/param.h> 44 1.1 jnemeth #include <sys/systm.h> 45 1.1 jnemeth #include <sys/kernel.h> 46 1.1 jnemeth #include <sys/device.h> 47 1.1 jnemeth #include <sys/socket.h> 48 1.1 jnemeth 49 1.1 jnemeth #include <net/if.h> 50 1.1 jnemeth #include <net/if_media.h> 51 1.1 jnemeth 52 1.1 jnemeth #include <dev/mii/mii.h> 53 1.1 jnemeth #include <dev/mii/miivar.h> 54 1.1 jnemeth #include <dev/mii/miidevs.h> 55 1.1 jnemeth 56 1.1 jnemeth #define ETPHY_INDEX 0x10 /* XXX reserved in DS */ 57 1.1 jnemeth #define ETPHY_INDEX_MAGIC 0x402 58 1.1 jnemeth #define ETPHY_DATA 0x11 /* XXX reserved in DS */ 59 1.1 jnemeth 60 1.1 jnemeth #define ETPHY_CTRL 0x12 61 1.1 jnemeth #define ETPHY_CTRL_DIAG 0x0004 62 1.1 jnemeth #define ETPHY_CTRL_RSV1 0x0002 /* XXX reserved */ 63 1.1 jnemeth #define ETPHY_CTRL_RSV0 0x0001 /* XXX reserved */ 64 1.1 jnemeth 65 1.1 jnemeth #define ETPHY_CONF 0x16 66 1.1 jnemeth #define ETPHY_CONF_TXFIFO_MASK 0x3000 67 1.1 jnemeth #define ETPHY_CONF_TXFIFO_8 0x0000 68 1.1 jnemeth #define ETPHY_CONF_TXFIFO_16 0x1000 69 1.1 jnemeth #define ETPHY_CONF_TXFIFO_24 0x2000 70 1.1 jnemeth #define ETPHY_CONF_TXFIFO_32 0x3000 71 1.1 jnemeth 72 1.1 jnemeth #define ETPHY_SR 0x1a 73 1.1 jnemeth #define ETPHY_SR_SPD_MASK 0x0300 74 1.1 jnemeth #define ETPHY_SR_SPD_1000T 0x0200 75 1.1 jnemeth #define ETPHY_SR_SPD_100TX 0x0100 76 1.1 jnemeth #define ETPHY_SR_SPD_10T 0x0000 77 1.1 jnemeth #define ETPHY_SR_FDX 0x0080 78 1.1 jnemeth 79 1.1 jnemeth 80 1.5 msaitoh static int etphy_service(struct mii_softc *, struct mii_data *, int); 81 1.5 msaitoh static void etphy_attach(device_t, device_t, void *); 82 1.5 msaitoh static int etphy_match(device_t, cfdata_t, void *); 83 1.5 msaitoh static void etphy_reset(struct mii_softc *); 84 1.5 msaitoh static void etphy_status(struct mii_softc *); 85 1.1 jnemeth 86 1.5 msaitoh static const struct mii_phy_funcs etphy_funcs = { 87 1.1 jnemeth etphy_service, etphy_status, etphy_reset, 88 1.1 jnemeth }; 89 1.1 jnemeth 90 1.1 jnemeth static const struct mii_phydesc etphys[] = { 91 1.3 christos MII_PHY_DESC(AGERE, ET1011), 92 1.6 msaitoh MII_PHY_DESC(AGERE, ET1011C), 93 1.3 christos MII_PHY_END, 94 1.1 jnemeth }; 95 1.1 jnemeth 96 1.1 jnemeth CFATTACH_DECL_NEW(etphy, sizeof(struct mii_softc), 97 1.1 jnemeth etphy_match, etphy_attach, mii_phy_detach, mii_phy_activate); 98 1.1 jnemeth 99 1.1 jnemeth static const struct etphy_dsp { 100 1.1 jnemeth uint16_t index; 101 1.1 jnemeth uint16_t data; 102 1.1 jnemeth } etphy_dspcode[] = { 103 1.1 jnemeth { 0x880b, 0x0926 }, /* AfeIfCreg4B1000Msbs */ 104 1.1 jnemeth { 0x880c, 0x0926 }, /* AfeIfCreg4B100Msbs */ 105 1.1 jnemeth { 0x880d, 0x0926 }, /* AfeIfCreg4B10Msbs */ 106 1.1 jnemeth 107 1.1 jnemeth { 0x880e, 0xb4d3 }, /* AfeIfCreg4B1000Lsbs */ 108 1.1 jnemeth { 0x880f, 0xb4d3 }, /* AfeIfCreg4B100Lsbs */ 109 1.1 jnemeth { 0x8810, 0xb4d3 }, /* AfeIfCreg4B10Lsbs */ 110 1.1 jnemeth 111 1.1 jnemeth { 0x8805, 0xb03e }, /* AfeIfCreg3B1000Msbs */ 112 1.1 jnemeth { 0x8806, 0xb03e }, /* AfeIfCreg3B100Msbs */ 113 1.1 jnemeth { 0x8807, 0xff00 }, /* AfeIfCreg3B10Msbs */ 114 1.1 jnemeth 115 1.1 jnemeth { 0x8808, 0xe090 }, /* AfeIfCreg3B1000Lsbs */ 116 1.1 jnemeth { 0x8809, 0xe110 }, /* AfeIfCreg3B100Lsbs */ 117 1.1 jnemeth { 0x880a, 0x0000 }, /* AfeIfCreg3B10Lsbs */ 118 1.1 jnemeth 119 1.1 jnemeth { 0x300d, 1 }, /* DisableNorm */ 120 1.1 jnemeth 121 1.1 jnemeth { 0x280c, 0x0180 }, /* LinkHoldEnd */ 122 1.1 jnemeth 123 1.1 jnemeth { 0x1c21, 0x0002 }, /* AlphaM */ 124 1.1 jnemeth 125 1.1 jnemeth { 0x3821, 6 }, /* FfeLkgTx0 */ 126 1.1 jnemeth { 0x381d, 1 }, /* FfeLkg1g4 */ 127 1.1 jnemeth { 0x381e, 1 }, /* FfeLkg1g5 */ 128 1.1 jnemeth { 0x381f, 1 }, /* FfeLkg1g6 */ 129 1.1 jnemeth { 0x3820, 1 }, /* FfeLkg1g7 */ 130 1.1 jnemeth 131 1.1 jnemeth { 0x8402, 0x01f0 }, /* Btinact */ 132 1.1 jnemeth { 0x800e, 20 }, /* LftrainTime */ 133 1.1 jnemeth { 0x800f, 24 }, /* DvguardTime */ 134 1.1 jnemeth { 0x8010, 46 } /* IdlguardTime */ 135 1.1 jnemeth }; 136 1.1 jnemeth 137 1.5 msaitoh static int 138 1.1 jnemeth etphy_match(device_t parent, cfdata_t match, void *aux) 139 1.1 jnemeth { 140 1.1 jnemeth struct mii_attach_args *ma = aux; 141 1.1 jnemeth 142 1.1 jnemeth if (mii_phy_match(ma, etphys) != NULL) 143 1.1 jnemeth return 10; 144 1.1 jnemeth 145 1.1 jnemeth return 0; 146 1.1 jnemeth } 147 1.1 jnemeth 148 1.5 msaitoh static void 149 1.1 jnemeth etphy_attach(device_t parent, device_t self, void *aux) 150 1.1 jnemeth { 151 1.1 jnemeth struct mii_softc *sc = device_private(self); 152 1.1 jnemeth struct mii_attach_args *ma = aux; 153 1.1 jnemeth struct mii_data *mii = ma->mii_data; 154 1.1 jnemeth const struct mii_phydesc *mpd; 155 1.1 jnemeth 156 1.1 jnemeth mpd = mii_phy_match(ma, etphys); 157 1.1 jnemeth aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 158 1.1 jnemeth 159 1.1 jnemeth sc->mii_dev = self; 160 1.1 jnemeth sc->mii_inst = mii->mii_instance; 161 1.1 jnemeth sc->mii_phy = ma->mii_phyno; 162 1.1 jnemeth sc->mii_funcs = &etphy_funcs; 163 1.1 jnemeth sc->mii_mpd_model = MII_MODEL(ma->mii_id2); 164 1.1 jnemeth sc->mii_pdata = mii; 165 1.1 jnemeth sc->mii_flags = ma->mii_flags; 166 1.1 jnemeth 167 1.1 jnemeth sc->mii_flags |= MIIF_NOISOLATE | MIIF_NOLOOP; 168 1.1 jnemeth 169 1.9 thorpej mii_lock(mii); 170 1.9 thorpej 171 1.1 jnemeth PHY_RESET(sc); 172 1.1 jnemeth 173 1.2 msaitoh PHY_READ(sc, MII_BMSR, &sc->mii_capabilities); 174 1.2 msaitoh sc->mii_capabilities &= ma->mii_capmask; 175 1.1 jnemeth if (sc->mii_capabilities & BMSR_EXTSTAT) { 176 1.2 msaitoh PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities); 177 1.1 jnemeth /* No 1000baseT half-duplex support */ 178 1.1 jnemeth sc->mii_extcapabilities &= ~EXTSR_1000THDX; 179 1.1 jnemeth } 180 1.1 jnemeth 181 1.9 thorpej mii_unlock(mii); 182 1.9 thorpej 183 1.7 msaitoh mii_phy_add_media(sc); 184 1.1 jnemeth } 185 1.1 jnemeth 186 1.5 msaitoh static int 187 1.1 jnemeth etphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 188 1.1 jnemeth { 189 1.1 jnemeth struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 190 1.2 msaitoh uint16_t bmcr; 191 1.1 jnemeth 192 1.9 thorpej KASSERT(mii_locked(mii)); 193 1.9 thorpej 194 1.1 jnemeth switch (cmd) { 195 1.1 jnemeth case MII_POLLSTAT: 196 1.4 msaitoh /* If we're not polling our PHY instance, just return. */ 197 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst) 198 1.1 jnemeth return 0; 199 1.1 jnemeth break; 200 1.1 jnemeth 201 1.1 jnemeth case MII_MEDIACHG: 202 1.1 jnemeth /* 203 1.1 jnemeth * If the media indicates a different PHY instance, 204 1.1 jnemeth * isolate ourselves. 205 1.1 jnemeth */ 206 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 207 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr); 208 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO); 209 1.1 jnemeth return 0; 210 1.1 jnemeth } 211 1.1 jnemeth 212 1.4 msaitoh /* If the interface is not up, don't do anything. */ 213 1.1 jnemeth if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 214 1.1 jnemeth break; 215 1.1 jnemeth 216 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) { 217 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr); 218 1.2 msaitoh bmcr &= ~BMCR_AUTOEN; 219 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr); 220 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_PDOWN); 221 1.8 msaitoh } else { 222 1.8 msaitoh /* 223 1.8 msaitoh * Issue reset before configuring autonego. 224 1.8 msaitoh * XXX Is this required? 225 1.8 msaitoh */ 226 1.8 msaitoh PHY_RESET(sc); 227 1.1 jnemeth } 228 1.1 jnemeth 229 1.1 jnemeth mii_phy_setmedia(sc); 230 1.1 jnemeth 231 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) { 232 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr); 233 1.2 msaitoh bmcr &= ~BMCR_PDOWN; 234 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr); 235 1.1 jnemeth 236 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) { 237 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, 238 1.5 msaitoh bmcr | BMCR_AUTOEN | BMCR_STARTNEG); 239 1.1 jnemeth } 240 1.1 jnemeth } 241 1.1 jnemeth break; 242 1.1 jnemeth 243 1.1 jnemeth case MII_TICK: 244 1.4 msaitoh /* If we're not currently selected, just return. */ 245 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst) 246 1.1 jnemeth return 0; 247 1.1 jnemeth 248 1.1 jnemeth if (mii_phy_tick(sc) == EJUSTRETURN) 249 1.1 jnemeth return 0; 250 1.1 jnemeth break; 251 1.1 jnemeth } 252 1.1 jnemeth 253 1.1 jnemeth /* Update the media status. */ 254 1.1 jnemeth mii_phy_status(sc); 255 1.1 jnemeth 256 1.1 jnemeth /* Callback if something changed. */ 257 1.1 jnemeth mii_phy_update(sc, cmd); 258 1.1 jnemeth return 0; 259 1.1 jnemeth } 260 1.1 jnemeth 261 1.5 msaitoh static void 262 1.1 jnemeth etphy_reset(struct mii_softc *sc) 263 1.1 jnemeth { 264 1.2 msaitoh uint16_t reg; 265 1.1 jnemeth int i; 266 1.1 jnemeth 267 1.9 thorpej KASSERT(mii_locked(sc->mii_pdata)); 268 1.9 thorpej 269 1.6 msaitoh if (sc->mii_mpd_model == MII_MODEL_AGERE_ET1011) { 270 1.6 msaitoh mii_phy_reset(sc); 271 1.6 msaitoh return; 272 1.6 msaitoh } 273 1.6 msaitoh 274 1.1 jnemeth for (i = 0; i < 2; ++i) { 275 1.2 msaitoh PHY_READ(sc, MII_PHYIDR1, ®); 276 1.2 msaitoh PHY_READ(sc, MII_PHYIDR2, ®); 277 1.1 jnemeth 278 1.2 msaitoh PHY_READ(sc, ETPHY_CTRL, ®); 279 1.5 msaitoh PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_DIAG | ETPHY_CTRL_RSV1); 280 1.1 jnemeth 281 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, ETPHY_INDEX_MAGIC); 282 1.2 msaitoh PHY_READ(sc, ETPHY_DATA, ®); 283 1.1 jnemeth 284 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_RSV1); 285 1.1 jnemeth } 286 1.1 jnemeth 287 1.2 msaitoh PHY_READ(sc, MII_BMCR, ®); 288 1.2 msaitoh PHY_READ(sc, ETPHY_CTRL, ®); 289 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_PDOWN | BMCR_S1000); 290 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL, 291 1.1 jnemeth ETPHY_CTRL_DIAG | ETPHY_CTRL_RSV1 | ETPHY_CTRL_RSV0); 292 1.1 jnemeth 293 1.1 jnemeth #define N(arr) (int)(sizeof(arr) / sizeof(arr[0])) 294 1.1 jnemeth 295 1.1 jnemeth for (i = 0; i < N(etphy_dspcode); ++i) { 296 1.1 jnemeth const struct etphy_dsp *dsp = &etphy_dspcode[i]; 297 1.1 jnemeth 298 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, dsp->index); 299 1.1 jnemeth PHY_WRITE(sc, ETPHY_DATA, dsp->data); 300 1.1 jnemeth 301 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, dsp->index); 302 1.2 msaitoh PHY_READ(sc, ETPHY_DATA, ®); 303 1.1 jnemeth } 304 1.1 jnemeth 305 1.1 jnemeth #undef N 306 1.1 jnemeth 307 1.2 msaitoh PHY_READ(sc, MII_BMCR, ®); 308 1.2 msaitoh PHY_READ(sc, ETPHY_CTRL, ®); 309 1.5 msaitoh PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_S1000); 310 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_RSV1); 311 1.1 jnemeth 312 1.1 jnemeth mii_phy_reset(sc); 313 1.1 jnemeth } 314 1.1 jnemeth 315 1.5 msaitoh static void 316 1.1 jnemeth etphy_status(struct mii_softc *sc) 317 1.1 jnemeth { 318 1.1 jnemeth struct mii_data *mii = sc->mii_pdata; 319 1.2 msaitoh uint16_t bmsr, bmcr, sr; 320 1.1 jnemeth 321 1.9 thorpej KASSERT(mii_locked(mii)); 322 1.9 thorpej 323 1.1 jnemeth mii->mii_media_status = IFM_AVALID; 324 1.1 jnemeth mii->mii_media_active = IFM_ETHER; 325 1.1 jnemeth 326 1.2 msaitoh PHY_READ(sc, ETPHY_SR, &sr); 327 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr); 328 1.1 jnemeth 329 1.2 msaitoh PHY_READ(sc, MII_BMSR, &bmsr); 330 1.2 msaitoh PHY_READ(sc, MII_BMSR, &bmsr); 331 1.1 jnemeth if (bmsr & BMSR_LINK) 332 1.1 jnemeth mii->mii_media_status |= IFM_ACTIVE; 333 1.1 jnemeth 334 1.1 jnemeth if (bmcr & BMCR_AUTOEN) { 335 1.1 jnemeth if ((bmsr & BMSR_ACOMP) == 0) { 336 1.1 jnemeth mii->mii_media_active |= IFM_NONE; 337 1.1 jnemeth return; 338 1.1 jnemeth } 339 1.1 jnemeth } 340 1.1 jnemeth 341 1.1 jnemeth switch (sr & ETPHY_SR_SPD_MASK) { 342 1.1 jnemeth case ETPHY_SR_SPD_1000T: 343 1.1 jnemeth mii->mii_media_active |= IFM_1000_T; 344 1.1 jnemeth break; 345 1.1 jnemeth case ETPHY_SR_SPD_100TX: 346 1.1 jnemeth mii->mii_media_active |= IFM_100_TX; 347 1.1 jnemeth break; 348 1.1 jnemeth case ETPHY_SR_SPD_10T: 349 1.1 jnemeth mii->mii_media_active |= IFM_10_T; 350 1.1 jnemeth break; 351 1.1 jnemeth default: 352 1.1 jnemeth mii->mii_media_active |= IFM_NONE; 353 1.1 jnemeth return; 354 1.1 jnemeth } 355 1.1 jnemeth 356 1.1 jnemeth if (sr & ETPHY_SR_FDX) 357 1.6 msaitoh mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc); 358 1.1 jnemeth else 359 1.1 jnemeth mii->mii_media_active |= IFM_HDX; 360 1.1 jnemeth } 361