Home | History | Annotate | Line # | Download | only in mii
brgphy.c revision 1.6.4.2
      1 /*	$NetBSD: brgphy.c,v 1.6.4.2 2003/01/28 06:25:41 jmc Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
      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.
     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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by Manuel Bouyer.
     54  * 4. The name of the author may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * driver for the Broadcom BCM5400 Gig-E PHY.
     71  *
     72  * Programming information for this PHY was gleaned from FreeBSD
     73  * (they were apparently able to get a datasheet from Broadcom).
     74  */
     75 
     76 #include <sys/cdefs.h>
     77 __KERNEL_RCSID(0, "$NetBSD: brgphy.c,v 1.6.4.2 2003/01/28 06:25:41 jmc Exp $");
     78 
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/kernel.h>
     82 #include <sys/device.h>
     83 #include <sys/malloc.h>
     84 #include <sys/socket.h>
     85 #include <sys/errno.h>
     86 
     87 #include <net/if.h>
     88 #include <net/if_media.h>
     89 
     90 #include <dev/mii/mii.h>
     91 #include <dev/mii/miivar.h>
     92 #include <dev/mii/miidevs.h>
     93 
     94 #include <dev/mii/brgphyreg.h>
     95 
     96 int	brgphymatch(struct device *, struct cfdata *, void *);
     97 void	brgphyattach(struct device *, struct device *, void *);
     98 
     99 struct cfattach brgphy_ca = {
    100 	sizeof(struct mii_softc), brgphymatch, brgphyattach,
    101 	    mii_phy_detach, mii_phy_activate
    102 };
    103 
    104 int	brgphy_service(struct mii_softc *, struct mii_data *, int);
    105 void	brgphy_status(struct mii_softc *);
    106 
    107 void	brgphy_5401_reset(struct mii_softc *);
    108 void	brgphy_5411_reset(struct mii_softc *);
    109 void	brgphy_5703_reset(struct mii_softc *);
    110 void	brgphy_5704_reset(struct mii_softc *);
    111 
    112 const struct mii_phy_funcs brgphy_funcs = {
    113 	brgphy_service, brgphy_status, mii_phy_reset,
    114 };
    115 
    116 const struct mii_phy_funcs brgphy_5401_funcs = {
    117 	brgphy_service, brgphy_status, brgphy_5401_reset,
    118 };
    119 
    120 const struct mii_phy_funcs brgphy_5411_funcs = {
    121 	brgphy_service, brgphy_status, brgphy_5411_reset,
    122 };
    123 
    124 const struct mii_phy_funcs brgphy_5703_funcs = {
    125 	brgphy_service, brgphy_status, brgphy_5703_reset,
    126 };
    127 
    128 const struct mii_phy_funcs brgphy_5704_funcs = {
    129 	brgphy_service, brgphy_status, brgphy_5704_reset,
    130 };
    131 
    132 
    133 const struct mii_phydesc brgphys[] = {
    134 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM5400,
    135 	  MII_STR_BROADCOM_BCM5400 },
    136 
    137 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM5401,
    138 	  MII_STR_BROADCOM_BCM5401 },
    139 
    140 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM5411,
    141 	  MII_STR_BROADCOM_BCM5411 },
    142 
    143 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM5421,
    144 	  MII_STR_BROADCOM_BCM5421 },
    145 
    146 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM5701,
    147 	  MII_STR_BROADCOM_BCM5701 },
    148 
    149 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM5703,
    150 	  MII_STR_BROADCOM_BCM5703 },
    151 
    152 	{ MII_OUI_BROADCOM,		MII_MODEL_BROADCOM_BCM5704,
    153 	  MII_STR_BROADCOM_BCM5704 },
    154 
    155 	{ 0,				0,
    156 	  NULL },
    157 };
    158 
    159 static void bcm5401_load_dspcode(struct mii_softc *);
    160 static void bcm5411_load_dspcode(struct mii_softc *);
    161 static void bcm5703_load_dspcode(struct mii_softc *);
    162 static void bcm5704_load_dspcode(struct mii_softc *);
    163 
    164 int
    165 brgphymatch(struct device *parent, struct cfdata *match, void *aux)
    166 {
    167 	struct mii_attach_args *ma = aux;
    168 
    169 	if (mii_phy_match(ma, brgphys) != NULL)
    170 		return (10);
    171 
    172 	return (0);
    173 }
    174 
    175 void
    176 brgphyattach(struct device *parent, struct device *self, void *aux)
    177 {
    178 	struct mii_softc *sc = (struct mii_softc *)self;
    179 	struct mii_attach_args *ma = aux;
    180 	struct mii_data *mii = ma->mii_data;
    181 	const struct mii_phydesc *mpd;
    182 
    183 	mpd = mii_phy_match(ma, brgphys);
    184 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    185 
    186 	sc->mii_inst = mii->mii_instance;
    187 	sc->mii_phy = ma->mii_phyno;
    188 	sc->mii_pdata = mii;
    189 	sc->mii_flags = ma->mii_flags;
    190 	sc->mii_anegticks = 5;
    191 
    192 	switch (MII_MODEL(ma->mii_id2)) {
    193 	case MII_MODEL_BROADCOM_BCM5400:
    194 		sc->mii_funcs = &brgphy_5401_funcs;
    195 		printf("%s: using BCM5401 DSP patch\n", sc->mii_dev.dv_xname);
    196 		break;
    197 
    198 	case MII_MODEL_BROADCOM_BCM5401:
    199 		if (MII_REV(ma->mii_id2) == 1 || MII_REV(ma->mii_id2) == 3) {
    200 			sc->mii_funcs = &brgphy_5401_funcs;
    201 			printf("%s: using BCM5401 DSP patch\n",
    202 			    sc->mii_dev.dv_xname);
    203 		} else
    204 			sc->mii_funcs = &brgphy_funcs;
    205 		break;
    206 
    207 	case MII_MODEL_BROADCOM_BCM5411:
    208 		sc->mii_funcs = &brgphy_5411_funcs;
    209 		printf("%s: using BCM5411 DSP patch\n", sc->mii_dev.dv_xname);
    210 		break;
    211 
    212 #ifdef notyet /* unverified, untested */
    213 	case MII_MODEL_BROADCOM_BCM5703:
    214 		sc->mii_funcs = &brgphy_5703_funcs;
    215 		printf("%s: using BCM5703 DSP patch\n", sc->mii_dev.dv_xname);
    216 		break;
    217 #endif
    218 
    219 	case MII_MODEL_BROADCOM_BCM5704:
    220 		sc->mii_funcs = &brgphy_5704_funcs;
    221 		printf("%s: using BCM5704 DSP patch\n", sc->mii_dev.dv_xname);
    222 		break;
    223 
    224 	default:
    225 		sc->mii_funcs = &brgphy_funcs;
    226 		break;
    227 	}
    228 
    229 	PHY_RESET(sc);
    230 
    231 	sc->mii_capabilities =
    232 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    233 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    234 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
    235 
    236 	printf("%s: ", sc->mii_dev.dv_xname);
    237 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
    238 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
    239 		printf("no media present");
    240 	else
    241 		mii_phy_add_media(sc);
    242 	printf("\n");
    243 }
    244 
    245 int
    246 brgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    247 {
    248 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    249 	int reg;
    250 
    251 	switch (cmd) {
    252 	case MII_POLLSTAT:
    253 		/*
    254 		 * If we're not polling our PHY instance, just return.
    255 		 */
    256 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    257 			return (0);
    258 		break;
    259 
    260 	case MII_MEDIACHG:
    261 		/*
    262 		 * If the media indicates a different PHY instance,
    263 		 * isolate ourselves.
    264 		 */
    265 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    266 			reg = PHY_READ(sc, MII_BMCR);
    267 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    268 			return (0);
    269 		}
    270 
    271 		/*
    272 		 * If the interface is not up, don't do anything.
    273 		 */
    274 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    275 			break;
    276 
    277 		mii_phy_reset(sc);	/* XXX hardware bug work-around */
    278 		mii_phy_setmedia(sc);
    279 		break;
    280 
    281 	case MII_TICK:
    282 		/*
    283 		 * If we're not currently selected, just return.
    284 		 */
    285 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    286 			return (0);
    287 
    288 		if (mii_phy_tick(sc) == EJUSTRETURN)
    289 			return (0);
    290 		break;
    291 
    292 	case MII_DOWN:
    293 		mii_phy_down(sc);
    294 		return (0);
    295 	}
    296 
    297 	/* Update the media status. */
    298 	mii_phy_status(sc);
    299 
    300 	/*
    301 	 * Callback if something changed.  Note that we need to poke
    302 	 * the DSP on the Broadcom PHYs if the media changes.
    303 	 */
    304 	if (sc->mii_media_active != mii->mii_media_active ||
    305 	    sc->mii_media_status != mii->mii_media_status ||
    306 	    cmd == MII_MEDIACHG) {
    307 		mii_phy_update(sc, cmd);
    308 		if (sc->mii_funcs == &brgphy_5401_funcs)
    309 			bcm5401_load_dspcode(sc);
    310 		else if (sc->mii_funcs == &brgphy_5411_funcs)
    311 			bcm5411_load_dspcode(sc);
    312 	}
    313 	return (0);
    314 }
    315 
    316 void
    317 brgphy_status(struct mii_softc *sc)
    318 {
    319 	struct mii_data *mii = sc->mii_pdata;
    320 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    321 	int bmcr, auxsts, gtsr;
    322 
    323 	mii->mii_media_status = IFM_AVALID;
    324 	mii->mii_media_active = IFM_ETHER;
    325 
    326 	auxsts = PHY_READ(sc, BRGPHY_MII_AUXSTS);
    327 
    328 	if (auxsts & BRGPHY_AUXSTS_LINK)
    329 		mii->mii_media_status |= IFM_ACTIVE;
    330 
    331 	bmcr = PHY_READ(sc, MII_BMCR);
    332 	if (bmcr & BMCR_ISO) {
    333 		mii->mii_media_active |= IFM_NONE;
    334 		mii->mii_media_status = 0;
    335 		return;
    336 	}
    337 
    338 	if (bmcr & BMCR_LOOP)
    339 		mii->mii_media_active |= IFM_LOOP;
    340 
    341 	if (bmcr & BMCR_AUTOEN) {
    342 		/*
    343 		 * The media status bits are only valid of autonegotiation
    344 		 * has completed (or it's disabled).
    345 		 */
    346 		if ((auxsts & BRGPHY_AUXSTS_ACOMP) == 0) {
    347 			/* Erg, still trying, I guess... */
    348 			mii->mii_media_active |= IFM_NONE;
    349 			return;
    350 		}
    351 
    352 		switch (auxsts & BRGPHY_AUXSTS_AN_RES) {
    353 		case BRGPHY_RES_1000FD:
    354 			mii->mii_media_active |= IFM_1000_T|IFM_FDX;
    355 			gtsr = PHY_READ(sc, MII_100T2SR);
    356 			if (gtsr & GTSR_MS_RES)
    357 				mii->mii_media_active |= IFM_ETH_MASTER;
    358 			break;
    359 
    360 		case BRGPHY_RES_1000HD:
    361 			mii->mii_media_active |= IFM_1000_T;
    362 			gtsr = PHY_READ(sc, MII_100T2SR);
    363 			if (gtsr & GTSR_MS_RES)
    364 				mii->mii_media_active |= IFM_ETH_MASTER;
    365 			break;
    366 
    367 		case BRGPHY_RES_100FD:
    368 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
    369 			break;
    370 
    371 		case BRGPHY_RES_100T4:
    372 			mii->mii_media_active |= IFM_100_T4;
    373 			break;
    374 
    375 		case BRGPHY_RES_100HD:
    376 			mii->mii_media_active |= IFM_100_TX;
    377 			break;
    378 
    379 		case BRGPHY_RES_10FD:
    380 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
    381 			break;
    382 
    383 		case BRGPHY_RES_10HD:
    384 			mii->mii_media_active |= IFM_10_T;
    385 			break;
    386 
    387 		default:
    388 			mii->mii_media_active |= IFM_NONE;
    389 			mii->mii_media_status = 0;
    390 		}
    391 	} else
    392 		mii->mii_media_active = ife->ifm_media;
    393 }
    394 
    395 void
    396 brgphy_5401_reset(struct mii_softc *sc)
    397 {
    398 
    399 	mii_phy_reset(sc);
    400 	bcm5401_load_dspcode(sc);
    401 }
    402 
    403 void
    404 brgphy_5411_reset(struct mii_softc *sc)
    405 {
    406 
    407 	mii_phy_reset(sc);
    408 	bcm5411_load_dspcode(sc);
    409 }
    410 
    411 
    412 void
    413 brgphy_5703_reset(struct mii_softc *sc)
    414 {
    415 
    416 	mii_phy_reset(sc);
    417 	bcm5703_load_dspcode(sc);
    418 }
    419 
    420 void
    421 brgphy_5704_reset(struct mii_softc *sc)
    422 {
    423 
    424 	mii_phy_reset(sc);
    425 	bcm5704_load_dspcode(sc);
    426 }
    427 
    428 /* Turn off tap power management on 5401. */
    429 static void
    430 bcm5401_load_dspcode(struct mii_softc *sc)
    431 {
    432 	static const struct {
    433 		int		reg;
    434 		uint16_t	val;
    435 	} dspcode[] = {
    436 		{ BRGPHY_MII_AUXCTL,		0x0c20 },
    437 		{ BRGPHY_MII_DSP_ADDR_REG,	0x0012 },
    438 		{ BRGPHY_MII_DSP_RW_PORT,	0x1804 },
    439 		{ BRGPHY_MII_DSP_ADDR_REG,	0x0013 },
    440 		{ BRGPHY_MII_DSP_RW_PORT,	0x1204 },
    441 		{ BRGPHY_MII_DSP_ADDR_REG,	0x8006 },
    442 		{ BRGPHY_MII_DSP_RW_PORT,	0x0132 },
    443 		{ BRGPHY_MII_DSP_ADDR_REG,	0x8006 },
    444 		{ BRGPHY_MII_DSP_RW_PORT,	0x0232 },
    445 		{ BRGPHY_MII_DSP_ADDR_REG,	0x201f },
    446 		{ BRGPHY_MII_DSP_RW_PORT,	0x0a20 },
    447 		{ 0,				0 },
    448 	};
    449 	int i;
    450 
    451 	for (i = 0; dspcode[i].reg != 0; i++)
    452 		PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
    453     delay(40);
    454 }
    455 
    456 static void
    457 bcm5411_load_dspcode(struct mii_softc *sc)
    458 {
    459 	static const struct {
    460 		int		reg;
    461 		uint16_t	val;
    462 	} dspcode[] = {
    463 		{ 0x1c,				0x8c23 },
    464 		{ 0x1c,				0x8ca3 },
    465 		{ 0x1c,				0x8c23 },
    466 		{ 0,				0 },
    467 	};
    468 	int i;
    469 
    470 	for (i = 0; dspcode[i].reg != 0; i++)
    471 		PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
    472 }
    473 
    474 static void
    475 bcm5703_load_dspcode(struct mii_softc *sc)
    476 {
    477 	static const struct {
    478 		int		reg;
    479 		uint16_t	val;
    480 	} dspcode[] = {
    481 		{ BRGPHY_MII_AUXCTL,		0x0c00 },
    482 		{ BRGPHY_MII_DSP_ADDR_REG,	0x201f },
    483 		{ BRGPHY_MII_DSP_RW_PORT,	0x2aaa },
    484 		{ 0,				0 },
    485 	};
    486 	int i;
    487 
    488 	for (i = 0; dspcode[i].reg != 0; i++)
    489 		PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
    490 }
    491 
    492 static void
    493 bcm5704_load_dspcode(struct mii_softc *sc)
    494 {
    495 	static const struct {
    496 		int		reg;
    497 		uint16_t	val;
    498 	} dspcode[] = {
    499 		{ 0x1c,				0x8d68 },
    500    		{ 0x1c,				0x8d68 },
    501 		{ 0,				0 },
    502 	};
    503 	int i;
    504 
    505 	for (i = 0; dspcode[i].reg != 0; i++)
    506 		PHY_WRITE(sc, dspcode[i].reg, dspcode[i].val);
    507 }
    508