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