1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-NetBSD 3 * 4 * Copyright (c) 2019 Rubicon Communications, LLC (Netgate) 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center, and by Frank van der Linden. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 /* Driver for the Marvell 88e151x gigabit ethernet PHY. */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/socket.h> 42 #include <sys/errno.h> 43 #include <sys/module.h> 44 #include <sys/bus.h> 45 46 #include <net/if.h> 47 #include <net/if_media.h> 48 49 #include <dev/mii/mii.h> 50 #include <dev/mii/miivar.h> 51 #include <dev/mii/mv88e151xreg.h> 52 #include "miidevs.h" 53 54 #include "miibus_if.h" 55 56 static int mv88e151x_probe(device_t); 57 static int mv88e151x_attach(device_t); 58 59 static device_method_t mv88e151x_methods[] = { 60 /* device interface */ 61 DEVMETHOD(device_probe, mv88e151x_probe), 62 DEVMETHOD(device_attach, mv88e151x_attach), 63 DEVMETHOD(device_detach, mii_phy_detach), 64 DEVMETHOD(device_shutdown, bus_generic_shutdown), 65 DEVMETHOD_END 66 }; 67 68 static devclass_t mv88e151x_devclass; 69 70 static driver_t mv88e151x_driver = { 71 "mv88e151x", 72 mv88e151x_methods, 73 sizeof(struct mii_softc) 74 }; 75 76 DRIVER_MODULE(mv88e151x, miibus, mv88e151x_driver, mv88e151x_devclass, 0, 0); 77 78 static int mv88e151x_service(struct mii_softc *, struct mii_data *, int); 79 static void mv88e151x_status(struct mii_softc *); 80 81 static const struct mii_phydesc mv88e151xphys[] = { 82 MII_PHY_DESC(xxMARVELL, E1512), 83 MII_PHY_END 84 }; 85 86 static const struct mii_phy_funcs mv88e151x_funcs = { 87 mv88e151x_service, 88 mv88e151x_status, 89 mii_phy_reset 90 }; 91 92 static int 93 mv88e151x_probe(device_t dev) 94 { 95 96 return (mii_phy_dev_probe(dev, mv88e151xphys, BUS_PROBE_DEFAULT)); 97 } 98 99 static int 100 mv88e151x_attach(device_t dev) 101 { 102 const struct mii_attach_args *ma; 103 struct mii_softc *sc; 104 uint32_t cop_cap, cop_extcap; 105 106 sc = device_get_softc(dev); 107 ma = device_get_ivars(dev); 108 109 mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &mv88e151x_funcs, 0); 110 111 PHY_RESET(sc); 112 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & sc->mii_capmask; 113 cop_cap = sc->mii_capabilities; 114 if (sc->mii_capabilities & BMSR_EXTSTAT) { 115 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR); 116 cop_extcap = sc->mii_extcapabilities; 117 } 118 device_printf(dev, " "); 119 if (MII_MODEL(ma->mii_id2) == MII_MODEL_xxMARVELL_E1512) 120 sc->mii_capabilities &= ~BMSR_ANEG; 121 mii_phy_add_media(sc); 122 if (MII_MODEL(ma->mii_id2) == MII_MODEL_xxMARVELL_E1512) { 123 /* Switch the fiber PHY and add the supported media. */ 124 PHY_WRITE(sc, MV88E151X_PAGE, MV88E151X_PAGE_FIBER); 125 PHY_RESET(sc); 126 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & sc->mii_capmask; 127 if (sc->mii_capabilities & BMSR_EXTSTAT) 128 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR); 129 sc->mii_flags |= MIIF_NOISOLATE; 130 printf(", "); 131 mii_phy_add_media(sc); 132 133 /* Switch back to copper PHY. */ 134 PHY_WRITE(sc, MV88E151X_PAGE, MV88E151X_PAGE_COPPER); 135 sc->mii_flags &= ~MIIF_NOISOLATE; 136 sc->mii_capabilities = cop_cap; 137 sc->mii_extcapabilities = cop_extcap; 138 } 139 printf("\n"); 140 141 MIIBUS_MEDIAINIT(sc->mii_dev); 142 143 mii_phy_setmedia(sc); 144 145 /* Enable the fiber PHY auto negotiation. */ 146 if (MII_MODEL(ma->mii_id2) == MII_MODEL_xxMARVELL_E1512) { 147 PHY_WRITE(sc, MV88E151X_PAGE, MV88E151X_PAGE_FIBER); 148 PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_AUTOEN | 149 BMCR_STARTNEG | BMCR_FDX | BMCR_S1000); 150 PHY_WRITE(sc, MV88E151X_PAGE, MV88E151X_PAGE_COPPER); 151 } 152 153 return (0); 154 } 155 156 static int 157 mv88e151x_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 158 { 159 switch (cmd) { 160 case MII_POLLSTAT: 161 break; 162 163 case MII_MEDIACHG: 164 mii_phy_setmedia(sc); 165 break; 166 167 case MII_TICK: 168 if (mii_phy_tick(sc) == EJUSTRETURN) 169 return (0); 170 break; 171 } 172 173 /* Update the media status. */ 174 PHY_STATUS(sc); 175 176 /* Callback if something changed. */ 177 mii_phy_update(sc, cmd); 178 return (0); 179 } 180 181 static void 182 mv88e151x_fiber_status(struct mii_softc *phy) 183 { 184 struct mii_data *mii = phy->mii_pdata; 185 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 186 int bmsr, bmcr, anlpar; //, gtcr, gtsr; 187 uint32_t reg; 188 189 mii->mii_media_status = IFM_AVALID; 190 mii->mii_media_active = IFM_ETHER; 191 192 /* Switch to the fiber PHY. */ 193 PHY_WRITE(phy, MV88E151X_PAGE, MV88E151X_PAGE_FIBER); 194 195 reg = PHY_READ(phy, MV88E151X_FIBER_STATUS); 196 bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR); 197 if (reg & MV88E151X_STATUS_LINK) 198 mii->mii_media_status |= IFM_ACTIVE; 199 bmcr = PHY_READ(phy, MII_BMCR); 200 if (bmcr & BMCR_ISO) { 201 mii->mii_media_active |= IFM_NONE; 202 mii->mii_media_status = 0; 203 PHY_WRITE(phy, MV88E151X_PAGE, MV88E151X_PAGE_COPPER); 204 return; 205 } 206 207 if (bmcr & BMCR_LOOP) 208 mii->mii_media_active |= IFM_LOOP; 209 210 if (bmcr & BMCR_AUTOEN) { 211 /* 212 * NWay autonegotiation takes the highest-order common 213 * bit of the ANAR and ANLPAR (i.e. best media advertised 214 * both by us and our link partner). 215 */ 216 if ((bmsr & BMSR_ACOMP) == 0) { 217 /* Erg, still trying, I guess... */ 218 mii->mii_media_active |= IFM_NONE; 219 PHY_WRITE(phy, MV88E151X_PAGE, MV88E151X_PAGE_COPPER); 220 return; 221 } 222 223 anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR); 224 if (anlpar & ANLPAR_X_FD) 225 mii->mii_media_active |= IFM_1000_SX | IFM_FDX; 226 else if (anlpar & ANLPAR_X_HD) 227 mii->mii_media_active |= IFM_1000_SX | IFM_HDX; 228 else if (reg & MV88E151X_STATUS_LINK && 229 reg & MV88E151X_STATUS_SYNC && 230 (reg & MV88E151X_STATUS_ENERGY) == 0) { 231 if ((reg & MV88E151X_STATUS_SPEED_MASK) == 232 MV88E151X_STATUS_SPEED_1000) 233 mii->mii_media_active |= IFM_1000_SX; 234 else if ((reg & MV88E151X_STATUS_SPEED_MASK) == 235 MV88E151X_STATUS_SPEED_100) 236 mii->mii_media_active |= IFM_100_FX; 237 else 238 mii->mii_media_active |= IFM_NONE; 239 if ((reg & MV88E151X_STATUS_SPEED_MASK) != 0 && 240 (reg & MV88E151X_STATUS_FDX)) 241 mii->mii_media_active |= IFM_FDX; 242 } else 243 mii->mii_media_active |= IFM_NONE; 244 245 if ((mii->mii_media_active & IFM_NONE) == 0) 246 phy->mii_flags |= MIIF_IS_1000X; 247 if ((mii->mii_media_active & IFM_FDX) != 0) 248 mii->mii_media_active |= mii_phy_flowstatus(phy); 249 } else 250 mii->mii_media_active = ife->ifm_media; 251 252 if ((mii->mii_media_status & IFM_ACTIVE) == 0) 253 phy->mii_flags &= ~MIIF_IS_1000X; 254 255 /* Switch back to copper PHY. */ 256 PHY_WRITE(phy, MV88E151X_PAGE, MV88E151X_PAGE_COPPER); 257 } 258 259 static void 260 mv88e151x_status(struct mii_softc *phy) 261 { 262 struct mii_data *mii = phy->mii_pdata; 263 264 mv88e151x_fiber_status(phy); 265 if (mii->mii_media_status & IFM_ACTIVE) 266 return; 267 ukphy_status(phy); 268 } 269