Home | History | Annotate | Line # | Download | only in mii
ipgphy.c revision 1.6
      1 /*	$NetBSD: ipgphy.c,v 1.6 2019/11/21 09:48:57 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 (at) 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.6 2019/11/21 09:48:57 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 
     45 #include <net/if.h>
     46 #include <net/if_media.h>
     47 
     48 #include <dev/mii/mii.h>
     49 #include <dev/mii/miivar.h>
     50 #include <dev/mii/miidevs.h>
     51 
     52 #include <dev/mii/ipgphyreg.h>
     53 
     54 #include <dev/pci/if_stgereg.h>
     55 
     56 static int ipgphy_match(device_t, cfdata_t, void *);
     57 static void ipgphy_attach(device_t, device_t, void *);
     58 
     59 CFATTACH_DECL_NEW(ipgphy, sizeof(struct mii_softc),
     60     ipgphy_match, ipgphy_attach, mii_phy_detach, mii_phy_activate);
     61 
     62 static int	ipgphy_service(struct mii_softc *, struct mii_data *, int);
     63 static void	ipgphy_status(struct mii_softc *);
     64 static int	ipgphy_mii_phy_auto(struct mii_softc *, u_int);
     65 static void	ipgphy_load_dspcode(struct mii_softc *);
     66 static void	ipgphy_reset(struct mii_softc *);
     67 
     68 static const struct mii_phy_funcs ipgphy_funcs = {
     69 	ipgphy_service, ipgphy_status, ipgphy_reset,
     70 };
     71 
     72 static const struct mii_phydesc ipgphys[] = {
     73 	MII_PHY_DESC(xxICPLUS, IP1000A),
     74 	MII_PHY_DESC(xxICPLUS, IP1001),
     75 	MII_PHY_END,
     76 };
     77 
     78 static int
     79 ipgphy_match(device_t parent, cfdata_t match, void *aux)
     80 {
     81 	struct mii_attach_args *ma = aux;
     82 
     83 	if (mii_phy_match(ma, ipgphys) != NULL)
     84 		return 10;
     85 
     86 	return 0;
     87 }
     88 
     89 static void
     90 ipgphy_attach(device_t parent, device_t self, void *aux)
     91 {
     92 	struct mii_softc *sc = device_private(self);
     93 	struct mii_attach_args *ma = aux;
     94 	struct mii_data *mii = ma->mii_data;
     95 	const struct mii_phydesc *mpd;
     96 
     97 	mpd = mii_phy_match(ma, ipgphys);
     98 	aprint_naive(": Media interface\n");
     99 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    100 
    101 	sc->mii_dev = self;
    102 	sc->mii_inst = mii->mii_instance;
    103 	sc->mii_phy = ma->mii_phyno;
    104 	sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
    105 	sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
    106 	sc->mii_mpd_rev = MII_REV(ma->mii_id2);
    107 	sc->mii_funcs = &ipgphy_funcs;
    108 	sc->mii_pdata = mii;
    109 	sc->mii_flags = ma->mii_flags;
    110 
    111 	sc->mii_flags |= MIIF_NOISOLATE;
    112 	sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    113 
    114 	PHY_RESET(sc);
    115 
    116 	PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
    117 	sc->mii_capabilities &= ma->mii_capmask;
    118 	//sc->mii_capabilities &= ~BMSR_ANEG;
    119 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    120 		PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
    121 
    122 	mii_phy_add_media(sc);
    123 	aprint_normal("\n");
    124 }
    125 
    126 static int
    127 ipgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    128 {
    129 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    130 	uint16_t reg, speed;
    131 
    132 	switch (cmd) {
    133 	case MII_POLLSTAT:
    134 		/* If we're not polling our PHY instance, just return. */
    135 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    136 			return 0;
    137 		break;
    138 
    139 	case MII_MEDIACHG:
    140 		/*
    141 		 * If the media indicates a different PHY instance,
    142 		 * isolate ourselves.
    143 		 */
    144 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    145 			PHY_READ(sc, MII_BMCR, &reg);
    146 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    147 			return 0;
    148 		}
    149 
    150 		/* If the interface is not up, don't do anything. */
    151 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    152 			break;
    153 
    154 		PHY_RESET(sc);
    155 
    156 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    157 		case IFM_AUTO:
    158 		case IFM_1000_T:
    159 			/*
    160 			 * This device is required to do auto negotiation
    161 			 * on 1000BASE-T.
    162 			 */
    163 			(void)ipgphy_mii_phy_auto(sc, ife->ifm_media);
    164 			goto done;
    165 			break;
    166 
    167 		case IFM_100_TX:
    168 			speed = BMCR_S100;
    169 			break;
    170 
    171 		case IFM_10_T:
    172 			speed = BMCR_S10;
    173 			break;
    174 
    175 		default:
    176 			return EINVAL;
    177 		}
    178 
    179 		if ((ife->ifm_media & IFM_FDX) != 0)
    180 			speed |= BMCR_FDX;
    181 
    182 		PHY_WRITE(sc, MII_100T2CR, 0);
    183 		PHY_WRITE(sc, MII_BMCR, speed);
    184 done:
    185 		break;
    186 
    187 	case MII_TICK:
    188 		/* If we're not currently selected, just return. */
    189 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    190 			return 0;
    191 
    192 		/* Is the interface even up? */
    193 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    194 			return 0;
    195 
    196 		/* Only used for autonegotiation. */
    197 		if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) &&
    198 		    (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) {
    199 			sc->mii_ticks = 0;
    200 			break;
    201 		}
    202 
    203 		/*
    204 		 * Check to see if we have link.  If we do, we don't
    205 		 * need to restart the autonegotiation process.  Read
    206 		 * the BMSR twice in case it's latched.
    207 		 */
    208 		PHY_READ(sc, MII_BMSR, &reg);
    209 		PHY_READ(sc, MII_BMSR, &reg);
    210 		if (reg & BMSR_LINK) {
    211 			/*
    212 			 * Reset autonegotiation timer to 0 in case the link
    213 			 * goes down in the next tick.
    214 			 */
    215 			sc->mii_ticks = 0;
    216 			/* See above. */
    217 			break;
    218 		}
    219 
    220 		/* Announce link loss right after it happens */
    221 		if (sc->mii_ticks++ == 0)
    222 			break;
    223 
    224 		/* Only retry autonegotiation every mii_anegticks seconds. */
    225 		if (sc->mii_ticks <= sc->mii_anegticks)
    226 			break;
    227 
    228 		sc->mii_ticks = 0;
    229 		ipgphy_mii_phy_auto(sc, ife->ifm_media);
    230 		break;
    231 	}
    232 
    233 	/* Update the media status. */
    234 	ipgphy_status(sc);
    235 
    236 	/* Callback if something changed. */
    237 	mii_phy_update(sc, cmd);
    238 	return 0;
    239 }
    240 
    241 static void
    242 ipgphy_status(struct mii_softc *sc)
    243 {
    244 	struct mii_data *mii = sc->mii_pdata;
    245 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    246 	uint16_t bmsr, bmcr, stat, gtsr;
    247 
    248 	/* For IP1000A, use generic way */
    249 	if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1000A) {
    250 		ukphy_status(sc);
    251 		return;
    252 	}
    253 
    254 	mii->mii_media_status = IFM_AVALID;
    255 	mii->mii_media_active = IFM_ETHER;
    256 
    257 	PHY_READ(sc, MII_BMSR, &bmsr);
    258 	PHY_READ(sc, MII_BMSR, &bmsr);
    259 	if (bmsr & BMSR_LINK)
    260 		mii->mii_media_status |= IFM_ACTIVE;
    261 
    262 	PHY_READ(sc, MII_BMCR, &bmcr);
    263 	if (bmcr & BMCR_LOOP)
    264 		mii->mii_media_active |= IFM_LOOP;
    265 
    266 	if (bmcr & BMCR_AUTOEN) {
    267 		if ((bmsr & BMSR_ACOMP) == 0) {
    268 			/* Erg, still trying, I guess... */
    269 			mii->mii_media_active |= IFM_NONE;
    270 			return;
    271 		}
    272 
    273 		PHY_READ(sc, IPGPHY_LSR, &stat);
    274 		switch (stat & IPGPHY_LSR_SPEED_MASK) {
    275 		case IPGPHY_LSR_SPEED_10:
    276 			mii->mii_media_active |= IFM_10_T;
    277 			break;
    278 		case IPGPHY_LSR_SPEED_100:
    279 			mii->mii_media_active |= IFM_100_TX;
    280 			break;
    281 		case IPGPHY_LSR_SPEED_1000:
    282 			mii->mii_media_active |= IFM_1000_T;
    283 			break;
    284 		default:
    285 			mii->mii_media_active |= IFM_NONE;
    286 			return;
    287 		}
    288 
    289 		if (stat & IPGPHY_LSR_FULL_DUPLEX)
    290 			mii->mii_media_active |= IFM_FDX;
    291 		else
    292 			mii->mii_media_active |= IFM_HDX;
    293 
    294 		if (mii->mii_media_active & IFM_FDX)
    295 			mii->mii_media_active |= mii_phy_flowstatus(sc);
    296 
    297 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
    298 			PHY_READ(sc, MII_100T2SR, &gtsr);
    299 			if (gtsr & GTSR_MS_RES)
    300 				mii->mii_media_active |= IFM_ETH_MASTER;
    301 		}
    302 	} else
    303 		mii->mii_media_active = ife->ifm_media;
    304 }
    305 
    306 static int
    307 ipgphy_mii_phy_auto(struct mii_softc *sc, u_int media)
    308 {
    309 	uint16_t reg = 0;
    310 	u_int subtype = IFM_SUBTYPE(media);
    311 
    312 	/* XXX Is it requreid ? */
    313 	if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1001) {
    314 		PHY_READ(sc, MII_ANAR, &reg);
    315 		reg &= ~(ANAR_PAUSE_SYM | ANAR_PAUSE_ASYM);
    316 		reg |= ANAR_NP;
    317 	}
    318 
    319 	if (subtype == IFM_AUTO)
    320 		reg |= ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD;
    321 
    322 	if (sc->mii_flags & MIIF_DOPAUSE)
    323 		reg |= ANAR_PAUSE_SYM | ANAR_PAUSE_ASYM;
    324 
    325 	PHY_WRITE(sc, MII_ANAR, reg | ANAR_CSMA);
    326 
    327 	if (subtype == IFM_AUTO)
    328 		reg = GTCR_ADV_1000TFDX | GTCR_ADV_1000THDX;
    329 	else if (subtype == IFM_1000_T) {
    330 		if ((media & IFM_FDX) != 0)
    331 			reg = GTCR_ADV_1000TFDX;
    332 		else
    333 			reg = GTCR_ADV_1000THDX;
    334 	} else
    335 		reg = 0;
    336 
    337 	PHY_WRITE(sc, MII_100T2CR, reg);
    338 
    339 	PHY_WRITE(sc, MII_BMCR, BMCR_FDX | BMCR_AUTOEN | BMCR_STARTNEG);
    340 
    341 	return EJUSTRETURN;
    342 }
    343 
    344 static void
    345 ipgphy_load_dspcode(struct mii_softc *sc)
    346 {
    347 	PHY_WRITE(sc, 31, 0x0001);
    348 	PHY_WRITE(sc, 27, 0x01e0);
    349 	PHY_WRITE(sc, 31, 0x0002);
    350 	PHY_WRITE(sc, 27, 0xeb8e);
    351 	PHY_WRITE(sc, 31, 0x0000);
    352 	PHY_WRITE(sc, 30, 0x005e);
    353 	PHY_WRITE(sc, 9, 0x0700);
    354 
    355 	DELAY(50);
    356 }
    357 
    358 static void
    359 ipgphy_reset(struct mii_softc *sc)
    360 {
    361 	struct ifnet *ifp = sc->mii_pdata->mii_ifp;
    362 	uint16_t reg;
    363 
    364 	mii_phy_reset(sc);
    365 
    366 	/* Clear autoneg/full-duplex as we don't want it after reset */
    367 	PHY_READ(sc, MII_BMCR, &reg);
    368 	reg &= ~(BMCR_AUTOEN | BMCR_FDX);
    369 	PHY_WRITE(sc, MII_BMCR, reg);
    370 
    371 	if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1000A &&
    372 	    strcmp(ifp->if_xname, "stge") == 0) {
    373 		struct stge_softc *stge_sc = ifp->if_softc;
    374 		if (stge_sc->sc_rev >= 0x40 && stge_sc->sc_rev <= 0x4e)
    375 			ipgphy_load_dspcode(sc);
    376 	}
    377 }
    378