1 /* $NetBSD: ipgphy.c,v 1.11 2023/02/22 08:09:09 msaitoh Exp $ */ 2 /* $OpenBSD: ipgphy.c,v 1.19 2015/07/19 06:28:12 yuo Exp $ */ 3 4 /*- 5 * Copyright (c) 2006, Pyun YongHyeon <yongari@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 /* 33 * Driver for the IC Plus IP1000A/IP1001 10/100/1000 PHY. 34 */ 35 #include <sys/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: ipgphy.c,v 1.11 2023/02/22 08:09:09 msaitoh Exp $"); 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/device.h> 42 #include <sys/socket.h> 43 #include <sys/errno.h> 44 #include <prop/proplib.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/miidevs.h> 52 53 #include <dev/mii/ipgphyreg.h> 54 55 static int ipgphy_match(device_t, cfdata_t, void *); 56 static void ipgphy_attach(device_t, device_t, void *); 57 58 struct ipgphy_softc { 59 struct mii_softc sc_mii; 60 bool need_loaddspcode; 61 }; 62 63 CFATTACH_DECL_NEW(ipgphy, sizeof(struct ipgphy_softc), 64 ipgphy_match, ipgphy_attach, mii_phy_detach, mii_phy_activate); 65 66 static int ipgphy_service(struct mii_softc *, struct mii_data *, int); 67 static void ipgphy_status(struct mii_softc *); 68 static int ipgphy_mii_phy_auto(struct mii_softc *, u_int); 69 static void ipgphy_load_dspcode(struct mii_softc *); 70 static void ipgphy_reset(struct mii_softc *); 71 72 static const struct mii_phy_funcs ipgphy_funcs = { 73 ipgphy_service, ipgphy_status, ipgphy_reset, 74 }; 75 76 static const struct mii_phydesc ipgphys[] = { 77 MII_PHY_DESC(xxICPLUS, IP1000A), 78 MII_PHY_DESC(xxICPLUS, IP1001), 79 MII_PHY_END, 80 }; 81 82 static int 83 ipgphy_match(device_t parent, cfdata_t match, void *aux) 84 { 85 struct mii_attach_args *ma = aux; 86 87 if (mii_phy_match(ma, ipgphys) != NULL) 88 return 10; 89 90 return 0; 91 } 92 93 static void 94 ipgphy_attach(device_t parent, device_t self, void *aux) 95 { 96 struct ipgphy_softc *isc = device_private(self); 97 struct mii_softc *sc = &isc->sc_mii; 98 struct mii_attach_args *ma = aux; 99 struct mii_data *mii = ma->mii_data; 100 const struct mii_phydesc *mpd; 101 prop_dictionary_t dict; 102 103 mpd = mii_phy_match(ma, ipgphys); 104 aprint_naive(": Media interface\n"); 105 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2)); 106 107 sc->mii_dev = self; 108 sc->mii_inst = mii->mii_instance; 109 sc->mii_phy = ma->mii_phyno; 110 sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2); 111 sc->mii_mpd_model = MII_MODEL(ma->mii_id2); 112 sc->mii_mpd_rev = MII_REV(ma->mii_id2); 113 sc->mii_funcs = &ipgphy_funcs; 114 sc->mii_pdata = mii; 115 sc->mii_flags = ma->mii_flags; 116 sc->mii_flags |= MIIF_NOISOLATE; 117 118 if (device_is_a(parent, "stge")) { 119 dict = device_properties(parent); 120 prop_dictionary_get_bool(dict, "need_loaddspcode", 121 &isc->need_loaddspcode); 122 } 123 124 mii_lock(mii); 125 126 PHY_RESET(sc); 127 128 PHY_READ(sc, MII_BMSR, &sc->mii_capabilities); 129 sc->mii_capabilities &= ma->mii_capmask; 130 //sc->mii_capabilities &= ~BMSR_ANEG; 131 if (sc->mii_capabilities & BMSR_EXTSTAT) 132 PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities); 133 134 mii_unlock(mii); 135 136 mii_phy_add_media(sc); 137 } 138 139 static int 140 ipgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 141 { 142 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 143 uint16_t reg, speed; 144 145 KASSERT(mii_locked(mii)); 146 147 switch (cmd) { 148 case MII_POLLSTAT: 149 /* If we're not polling our PHY instance, just return. */ 150 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 151 return 0; 152 break; 153 154 case MII_MEDIACHG: 155 /* 156 * If the media indicates a different PHY instance, 157 * isolate ourselves. 158 */ 159 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 160 PHY_READ(sc, MII_BMCR, ®); 161 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 162 return 0; 163 } 164 165 /* If the interface is not up, don't do anything. */ 166 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 167 break; 168 169 PHY_RESET(sc); 170 171 switch (IFM_SUBTYPE(ife->ifm_media)) { 172 case IFM_AUTO: 173 case IFM_1000_T: 174 /* 175 * This device is required to do auto negotiation 176 * on 1000BASE-T. 177 */ 178 (void)ipgphy_mii_phy_auto(sc, ife->ifm_media); 179 goto done; 180 break; 181 182 case IFM_100_TX: 183 speed = BMCR_S100; 184 break; 185 186 case IFM_10_T: 187 speed = BMCR_S10; 188 break; 189 190 default: 191 return EINVAL; 192 } 193 194 if ((ife->ifm_media & IFM_FDX) != 0) 195 speed |= BMCR_FDX; 196 197 PHY_WRITE(sc, MII_100T2CR, 0); 198 PHY_WRITE(sc, MII_BMCR, speed); 199 done: 200 break; 201 202 case MII_TICK: 203 /* If we're not currently selected, just return. */ 204 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 205 return 0; 206 207 /* Is the interface even up? */ 208 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 209 return 0; 210 211 /* Only used for autonegotiation. */ 212 if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) && 213 (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) { 214 sc->mii_ticks = 0; 215 break; 216 } 217 218 /* 219 * Check to see if we have link. If we do, we don't 220 * need to restart the autonegotiation process. Read 221 * the BMSR twice in case it's latched. 222 */ 223 PHY_READ(sc, MII_BMSR, ®); 224 PHY_READ(sc, MII_BMSR, ®); 225 if (reg & BMSR_LINK) { 226 /* 227 * Reset autonegotiation timer to 0 in case the link 228 * goes down in the next tick. 229 */ 230 sc->mii_ticks = 0; 231 /* See above. */ 232 break; 233 } 234 235 /* Announce link loss right after it happens */ 236 if (sc->mii_ticks++ == 0) 237 break; 238 239 /* Only retry autonegotiation every mii_anegticks seconds. */ 240 if (sc->mii_ticks < sc->mii_anegticks) 241 break; 242 243 sc->mii_ticks = 0; 244 ipgphy_mii_phy_auto(sc, ife->ifm_media); 245 break; 246 } 247 248 /* Update the media status. */ 249 ipgphy_status(sc); 250 251 /* Callback if something changed. */ 252 mii_phy_update(sc, cmd); 253 return 0; 254 } 255 256 static void 257 ipgphy_status(struct mii_softc *sc) 258 { 259 struct mii_data *mii = sc->mii_pdata; 260 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 261 uint16_t bmsr, bmcr, stat, gtsr; 262 263 KASSERT(mii_locked(mii)); 264 265 /* For IP1000A, use generic way */ 266 if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1000A) { 267 ukphy_status(sc); 268 return; 269 } 270 271 mii->mii_media_status = IFM_AVALID; 272 mii->mii_media_active = IFM_ETHER; 273 274 PHY_READ(sc, MII_BMSR, &bmsr); 275 PHY_READ(sc, MII_BMSR, &bmsr); 276 if (bmsr & BMSR_LINK) 277 mii->mii_media_status |= IFM_ACTIVE; 278 279 PHY_READ(sc, MII_BMCR, &bmcr); 280 if (bmcr & BMCR_LOOP) 281 mii->mii_media_active |= IFM_LOOP; 282 283 if (bmcr & BMCR_AUTOEN) { 284 if ((bmsr & BMSR_ACOMP) == 0) { 285 /* Erg, still trying, I guess... */ 286 mii->mii_media_active |= IFM_NONE; 287 return; 288 } 289 290 PHY_READ(sc, IPGPHY_LSR, &stat); 291 switch (stat & IPGPHY_LSR_SPEED_MASK) { 292 case IPGPHY_LSR_SPEED_10: 293 mii->mii_media_active |= IFM_10_T; 294 break; 295 case IPGPHY_LSR_SPEED_100: 296 mii->mii_media_active |= IFM_100_TX; 297 break; 298 case IPGPHY_LSR_SPEED_1000: 299 mii->mii_media_active |= IFM_1000_T; 300 break; 301 default: 302 mii->mii_media_active |= IFM_NONE; 303 return; 304 } 305 306 if (stat & IPGPHY_LSR_FULL_DUPLEX) 307 mii->mii_media_active |= IFM_FDX; 308 else 309 mii->mii_media_active |= IFM_HDX; 310 311 if (mii->mii_media_active & IFM_FDX) 312 mii->mii_media_active |= mii_phy_flowstatus(sc); 313 314 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) { 315 PHY_READ(sc, MII_100T2SR, >sr); 316 if (gtsr & GTSR_MS_RES) 317 mii->mii_media_active |= IFM_ETH_MASTER; 318 } 319 } else 320 mii->mii_media_active = ife->ifm_media; 321 } 322 323 static int 324 ipgphy_mii_phy_auto(struct mii_softc *sc, u_int media) 325 { 326 uint16_t reg = 0; 327 u_int subtype = IFM_SUBTYPE(media); 328 329 KASSERT(mii_locked(sc->mii_pdata)); 330 331 /* XXX Is it requreid ? */ 332 if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1001) { 333 PHY_READ(sc, MII_ANAR, ®); 334 reg &= ~(ANAR_PAUSE_SYM | ANAR_PAUSE_ASYM); 335 reg |= ANAR_NP; 336 } 337 338 if (subtype == IFM_AUTO) 339 reg |= ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD; 340 341 if (sc->mii_flags & MIIF_DOPAUSE) 342 reg |= ANAR_PAUSE_SYM | ANAR_PAUSE_ASYM; 343 344 PHY_WRITE(sc, MII_ANAR, reg | ANAR_CSMA); 345 346 if (subtype == IFM_AUTO) 347 reg = GTCR_ADV_1000TFDX | GTCR_ADV_1000THDX; 348 else if (subtype == IFM_1000_T) { 349 if ((media & IFM_FDX) != 0) 350 reg = GTCR_ADV_1000TFDX; 351 else 352 reg = GTCR_ADV_1000THDX; 353 } else 354 reg = 0; 355 356 PHY_WRITE(sc, MII_100T2CR, reg); 357 358 PHY_WRITE(sc, MII_BMCR, BMCR_FDX | BMCR_AUTOEN | BMCR_STARTNEG); 359 360 return EJUSTRETURN; 361 } 362 363 static void 364 ipgphy_load_dspcode(struct mii_softc *sc) 365 { 366 367 PHY_WRITE(sc, 31, 0x0001); 368 PHY_WRITE(sc, 27, 0x01e0); 369 PHY_WRITE(sc, 31, 0x0002); 370 PHY_WRITE(sc, 27, 0xeb8e); 371 PHY_WRITE(sc, 31, 0x0000); 372 PHY_WRITE(sc, 30, 0x005e); 373 PHY_WRITE(sc, 9, 0x0700); 374 375 DELAY(50); 376 } 377 378 static void 379 ipgphy_reset(struct mii_softc *sc) 380 { 381 struct ipgphy_softc *isc = device_private(sc->mii_dev); 382 uint16_t reg; 383 384 KASSERT(mii_locked(sc->mii_pdata)); 385 386 mii_phy_reset(sc); 387 388 /* Clear autoneg/full-duplex as we don't want it after reset */ 389 PHY_READ(sc, MII_BMCR, ®); 390 reg &= ~(BMCR_AUTOEN | BMCR_FDX); 391 PHY_WRITE(sc, MII_BMCR, reg); 392 393 if (isc->need_loaddspcode) 394 ipgphy_load_dspcode(sc); 395 } 396