xref: /freebsd/sys/dev/dc/dcphy.c (revision 91cc2adb2e5642eeaf419f5d41bd61b5a1d17191)
196f2e892SBill Paul /*
296f2e892SBill Paul  * Copyright (c) 1997, 1998, 1999
396f2e892SBill Paul  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
496f2e892SBill Paul  *
596f2e892SBill Paul  * Redistribution and use in source and binary forms, with or without
696f2e892SBill Paul  * modification, are permitted provided that the following conditions
796f2e892SBill Paul  * are met:
896f2e892SBill Paul  * 1. Redistributions of source code must retain the above copyright
996f2e892SBill Paul  *    notice, this list of conditions and the following disclaimer.
1096f2e892SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1196f2e892SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1296f2e892SBill Paul  *    documentation and/or other materials provided with the distribution.
1396f2e892SBill Paul  * 3. All advertising materials mentioning features or use of this software
1496f2e892SBill Paul  *    must display the following acknowledgement:
1596f2e892SBill Paul  *	This product includes software developed by Bill Paul.
1696f2e892SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1796f2e892SBill Paul  *    may be used to endorse or promote products derived from this software
1896f2e892SBill Paul  *    without specific prior written permission.
1996f2e892SBill Paul  *
2096f2e892SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2196f2e892SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2296f2e892SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2396f2e892SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2496f2e892SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2596f2e892SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2696f2e892SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2796f2e892SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2896f2e892SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2996f2e892SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3096f2e892SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3196f2e892SBill Paul  *
3296f2e892SBill Paul  * $FreeBSD$
3396f2e892SBill Paul  */
3496f2e892SBill Paul 
3596f2e892SBill Paul /*
3696f2e892SBill Paul  * Pseudo-driver for internal NWAY support on DEC 21143 and workalike
3796f2e892SBill Paul  * controllers. Technically we're abusing the miibus code to handle
3896f2e892SBill Paul  * media selection and NWAY support here since there is no MII
3996f2e892SBill Paul  * interface. However the logical operations are roughly the same,
4096f2e892SBill Paul  * and the alternative is to create a fake MII interface in the driver,
4196f2e892SBill Paul  * which is harder to do.
4296f2e892SBill Paul  */
4396f2e892SBill Paul 
4496f2e892SBill Paul #include <sys/param.h>
4596f2e892SBill Paul #include <sys/systm.h>
4696f2e892SBill Paul #include <sys/kernel.h>
4796f2e892SBill Paul #include <sys/malloc.h>
4896f2e892SBill Paul #include <sys/socket.h>
4996f2e892SBill Paul #include <sys/errno.h>
5096f2e892SBill Paul #include <sys/module.h>
5196f2e892SBill Paul #include <sys/bus.h>
5296f2e892SBill Paul 
5396f2e892SBill Paul #include <net/if.h>
5496f2e892SBill Paul #include <net/if_arp.h>
5596f2e892SBill Paul #include <net/if_media.h>
5696f2e892SBill Paul 
5796f2e892SBill Paul #include <dev/mii/mii.h>
5896f2e892SBill Paul #include <dev/mii/miivar.h>
5996f2e892SBill Paul #include <dev/mii/miidevs.h>
6096f2e892SBill Paul 
6196f2e892SBill Paul #include <machine/clock.h>
6296f2e892SBill Paul #include <machine/bus_pio.h>
6396f2e892SBill Paul #include <machine/bus_memio.h>
6496f2e892SBill Paul #include <machine/bus.h>
6596f2e892SBill Paul #include <machine/resource.h>
6696f2e892SBill Paul #include <sys/bus.h>
6796f2e892SBill Paul 
6896f2e892SBill Paul #include <pci/pcivar.h>
6996f2e892SBill Paul 
7096f2e892SBill Paul #include <pci/if_dcreg.h>
7196f2e892SBill Paul 
7296f2e892SBill Paul #include "miibus_if.h"
7396f2e892SBill Paul 
7496f2e892SBill Paul #if !defined(lint)
7596f2e892SBill Paul static const char rcsid[] =
7696f2e892SBill Paul   "$FreeBSD$";
7796f2e892SBill Paul #endif
7896f2e892SBill Paul 
7996f2e892SBill Paul #define DC_SETBIT(sc, reg, x)                           \
8096f2e892SBill Paul         CSR_WRITE_4(sc, reg,                            \
8196f2e892SBill Paul                 CSR_READ_4(sc, reg) | x)
8296f2e892SBill Paul 
8396f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)                           \
8496f2e892SBill Paul         CSR_WRITE_4(sc, reg,                            \
8596f2e892SBill Paul                 CSR_READ_4(sc, reg) & ~x)
8696f2e892SBill Paul 
8796f2e892SBill Paul #define MIIF_AUTOTIMEOUT	0x0004
8896f2e892SBill Paul 
8991cc2adbSBill Paul /*
9091cc2adbSBill Paul  * This is the subsystem ID for the built-in 21143 ethernet
9191cc2adbSBill Paul  * in several Compaq Presario systems. Apparently these are
9291cc2adbSBill Paul  * 10Mbps only, so we need to treat them specially.
9391cc2adbSBill Paul  */
9491cc2adbSBill Paul #define COMPAQ_PRESARIO_ID	0xb0bb0e11
9591cc2adbSBill Paul 
9696f2e892SBill Paul static int dcphy_probe		__P((device_t));
9796f2e892SBill Paul static int dcphy_attach		__P((device_t));
9896f2e892SBill Paul static int dcphy_detach		__P((device_t));
9996f2e892SBill Paul 
10096f2e892SBill Paul static device_method_t dcphy_methods[] = {
10196f2e892SBill Paul 	/* device interface */
10296f2e892SBill Paul 	DEVMETHOD(device_probe,		dcphy_probe),
10396f2e892SBill Paul 	DEVMETHOD(device_attach,	dcphy_attach),
10496f2e892SBill Paul 	DEVMETHOD(device_detach,	dcphy_detach),
10596f2e892SBill Paul 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
10696f2e892SBill Paul 	{ 0, 0 }
10796f2e892SBill Paul };
10896f2e892SBill Paul 
10996f2e892SBill Paul static devclass_t dcphy_devclass;
11096f2e892SBill Paul 
11196f2e892SBill Paul static driver_t dcphy_driver = {
11296f2e892SBill Paul 	"dcphy",
11396f2e892SBill Paul 	dcphy_methods,
11496f2e892SBill Paul 	sizeof(struct mii_softc)
11596f2e892SBill Paul };
11696f2e892SBill Paul 
11796f2e892SBill Paul DRIVER_MODULE(dcphy, miibus, dcphy_driver, dcphy_devclass, 0, 0);
11896f2e892SBill Paul 
11996f2e892SBill Paul int	dcphy_service __P((struct mii_softc *, struct mii_data *, int));
12096f2e892SBill Paul void	dcphy_status __P((struct mii_softc *));
12196f2e892SBill Paul static int dcphy_auto		__P((struct mii_softc *, int));
12296f2e892SBill Paul static void dcphy_reset		__P((struct mii_softc *));
12396f2e892SBill Paul 
12496f2e892SBill Paul static int dcphy_probe(dev)
12596f2e892SBill Paul 	device_t		dev;
12696f2e892SBill Paul {
12796f2e892SBill Paul 	struct mii_attach_args *ma;
12896f2e892SBill Paul 
12996f2e892SBill Paul 	ma = device_get_ivars(dev);
13096f2e892SBill Paul 
13196f2e892SBill Paul 	/*
13296f2e892SBill Paul 	 * The dc driver will report the 21143 vendor and device
13396f2e892SBill Paul 	 * ID to let us know that it wants us to attach.
13496f2e892SBill Paul 	 */
13596f2e892SBill Paul 	if (ma->mii_id1 != DC_VENDORID_DEC ||
13696f2e892SBill Paul 	    ma->mii_id2 != DC_DEVICEID_21143)
13796f2e892SBill Paul 		return(ENXIO);
13896f2e892SBill Paul 
13996f2e892SBill Paul 	device_set_desc(dev, "Intel 21143 NWAY media interface");
14096f2e892SBill Paul 
14196f2e892SBill Paul 	return (0);
14296f2e892SBill Paul }
14396f2e892SBill Paul 
14496f2e892SBill Paul static int dcphy_attach(dev)
14596f2e892SBill Paul 	device_t		dev;
14696f2e892SBill Paul {
14796f2e892SBill Paul 	struct mii_softc *sc;
14896f2e892SBill Paul 	struct mii_attach_args *ma;
14996f2e892SBill Paul 	struct mii_data *mii;
15096f2e892SBill Paul 	struct dc_softc		*dc_sc;
15196f2e892SBill Paul 
15296f2e892SBill Paul 	sc = device_get_softc(dev);
15396f2e892SBill Paul 	ma = device_get_ivars(dev);
15496f2e892SBill Paul 	sc->mii_dev = device_get_parent(dev);
15596f2e892SBill Paul 	mii = device_get_softc(sc->mii_dev);
15696f2e892SBill Paul 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
15796f2e892SBill Paul 
15896f2e892SBill Paul 	sc->mii_inst = mii->mii_instance;
15996f2e892SBill Paul 	sc->mii_phy = ma->mii_phyno;
16096f2e892SBill Paul 	sc->mii_service = dcphy_service;
16196f2e892SBill Paul 	sc->mii_pdata = mii;
16296f2e892SBill Paul 
16396f2e892SBill Paul 	sc->mii_flags |= MIIF_NOISOLATE;
16496f2e892SBill Paul 	mii->mii_instance++;
16596f2e892SBill Paul 
16696f2e892SBill Paul #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
16796f2e892SBill Paul 
16896f2e892SBill Paul 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
16996f2e892SBill Paul 	    BMCR_ISO);
17096f2e892SBill Paul 
17196f2e892SBill Paul 	/*dcphy_reset(sc);*/
17296f2e892SBill Paul 	dc_sc = mii->mii_ifp->if_softc;
17396f2e892SBill Paul 	CSR_WRITE_4(dc_sc, DC_10BTSTAT, 0);
17496f2e892SBill Paul 	CSR_WRITE_4(dc_sc, DC_10BTCTRL, 0);
17596f2e892SBill Paul 
17696f2e892SBill Paul 	switch(pci_read_config(device_get_parent(sc->mii_dev),
17796f2e892SBill Paul 	    DC_PCI_CSID, 4)) {
17891cc2adbSBill Paul 	case COMPAQ_PRESARIO_ID:
17996f2e892SBill Paul 		/* Example of how to only allow 10Mbps modes. */
18091cc2adbSBill Paul 		sc->mii_capabilities = BMSR_ANEG|BMSR_10TFDX|BMSR_10THDX;
18196f2e892SBill Paul 		break;
18296f2e892SBill Paul 	default:
18391cc2adbSBill Paul 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
18491cc2adbSBill Paul 		    sc->mii_inst), BMCR_LOOP|BMCR_S100);
18591cc2adbSBill Paul 
18696f2e892SBill Paul 		sc->mii_capabilities =
18796f2e892SBill Paul 		    BMSR_ANEG|BMSR_100TXFDX|BMSR_100TXHDX|
18896f2e892SBill Paul 		    BMSR_10TFDX|BMSR_10THDX;
18996f2e892SBill Paul 		break;
19096f2e892SBill Paul 	}
19196f2e892SBill Paul 
19296f2e892SBill Paul 	sc->mii_capabilities &= ma->mii_capmask;
19396f2e892SBill Paul 	device_printf(dev, " ");
19496f2e892SBill Paul 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
19596f2e892SBill Paul 		printf("no media present");
19696f2e892SBill Paul 	else
19796f2e892SBill Paul 		mii_add_media(mii, sc->mii_capabilities, sc->mii_inst);
19896f2e892SBill Paul 	printf("\n");
19996f2e892SBill Paul #undef ADD
20096f2e892SBill Paul 
20196f2e892SBill Paul 	MIIBUS_MEDIAINIT(sc->mii_dev);
20296f2e892SBill Paul 	return(0);
20396f2e892SBill Paul }
20496f2e892SBill Paul 
20596f2e892SBill Paul static int dcphy_detach(dev)
20696f2e892SBill Paul 	device_t		dev;
20796f2e892SBill Paul {
20896f2e892SBill Paul 	struct mii_softc *sc;
20996f2e892SBill Paul 	struct mii_data *mii;
21096f2e892SBill Paul 
21196f2e892SBill Paul 	sc = device_get_softc(dev);
21296f2e892SBill Paul 	mii = device_get_softc(device_get_parent(dev));
21396f2e892SBill Paul 	sc->mii_dev = NULL;
21496f2e892SBill Paul 	LIST_REMOVE(sc, mii_list);
21596f2e892SBill Paul 
21696f2e892SBill Paul 	return(0);
21796f2e892SBill Paul }
21896f2e892SBill Paul 
21996f2e892SBill Paul int
22096f2e892SBill Paul dcphy_service(sc, mii, cmd)
22196f2e892SBill Paul 	struct mii_softc *sc;
22296f2e892SBill Paul 	struct mii_data *mii;
22396f2e892SBill Paul 	int cmd;
22496f2e892SBill Paul {
22596f2e892SBill Paul 	struct dc_softc		*dc_sc;
22696f2e892SBill Paul 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
22796f2e892SBill Paul 	int reg;
22896f2e892SBill Paul 	u_int32_t		mode;
22996f2e892SBill Paul 
23096f2e892SBill Paul 	dc_sc = mii->mii_ifp->if_softc;
23196f2e892SBill Paul 
23296f2e892SBill Paul 	switch (cmd) {
23396f2e892SBill Paul 	case MII_POLLSTAT:
23496f2e892SBill Paul 		/*
23596f2e892SBill Paul 		 * If we're not polling our PHY instance, just return.
23696f2e892SBill Paul 		 */
23796f2e892SBill Paul 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
23896f2e892SBill Paul 			return (0);
23996f2e892SBill Paul 		}
24096f2e892SBill Paul 		break;
24196f2e892SBill Paul 
24296f2e892SBill Paul 	case MII_MEDIACHG:
24396f2e892SBill Paul 		/*
24496f2e892SBill Paul 		 * If the media indicates a different PHY instance,
24596f2e892SBill Paul 		 * isolate ourselves.
24696f2e892SBill Paul 		 */
24796f2e892SBill Paul 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
24896f2e892SBill Paul 			return (0);
24996f2e892SBill Paul 		}
25096f2e892SBill Paul 
25196f2e892SBill Paul 		/*
25296f2e892SBill Paul 		 * If the interface is not up, don't do anything.
25396f2e892SBill Paul 		 */
25496f2e892SBill Paul 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
25596f2e892SBill Paul 			break;
25696f2e892SBill Paul 
25796f2e892SBill Paul 		sc->mii_flags = 0;
25896f2e892SBill Paul 		mii->mii_media_active = IFM_NONE;
25996f2e892SBill Paul 		mode = CSR_READ_4(dc_sc, DC_NETCFG);
26096f2e892SBill Paul 		mode &= ~(DC_NETCFG_FULLDUPLEX|DC_NETCFG_PORTSEL|
26196f2e892SBill Paul 		    DC_NETCFG_PCS|DC_NETCFG_SCRAMBLER|DC_NETCFG_SPEEDSEL);
26296f2e892SBill Paul 
26396f2e892SBill Paul 		switch (IFM_SUBTYPE(ife->ifm_media)) {
26496f2e892SBill Paul 		case IFM_AUTO:
26596f2e892SBill Paul 			/*dcphy_reset(sc);*/
26696f2e892SBill Paul 			(void) dcphy_auto(sc, 0);
26796f2e892SBill Paul 			break;
26896f2e892SBill Paul 		case IFM_100_T4:
26996f2e892SBill Paul 			/*
27096f2e892SBill Paul 			 * XXX Not supported as a manual setting right now.
27196f2e892SBill Paul 			 */
27296f2e892SBill Paul 			return (EINVAL);
27396f2e892SBill Paul 		case IFM_100_TX:
27496f2e892SBill Paul 			dcphy_reset(sc);
27596f2e892SBill Paul 			DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
27696f2e892SBill Paul 			mode |= DC_NETCFG_PORTSEL|DC_NETCFG_PCS|
27796f2e892SBill Paul 			    DC_NETCFG_SCRAMBLER;
27896f2e892SBill Paul 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
27996f2e892SBill Paul 				mode |= DC_NETCFG_FULLDUPLEX;
28096f2e892SBill Paul 			else
28196f2e892SBill Paul 				mode &= ~DC_NETCFG_FULLDUPLEX;
28296f2e892SBill Paul 			CSR_WRITE_4(dc_sc, DC_NETCFG, mode);
28396f2e892SBill Paul 			break;
28496f2e892SBill Paul 		case IFM_10_T:
28596f2e892SBill Paul 			DC_CLRBIT(dc_sc, DC_SIARESET, DC_SIA_RESET);
28696f2e892SBill Paul 			DC_CLRBIT(dc_sc, DC_10BTCTRL, 0xFFFF);
28796f2e892SBill Paul 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
28896f2e892SBill Paul 				DC_SETBIT(dc_sc, DC_10BTCTRL, 0x7F3D);
28996f2e892SBill Paul 			else
29096f2e892SBill Paul 				DC_SETBIT(dc_sc, DC_10BTCTRL, 0x7F3F);
29196f2e892SBill Paul 			DC_SETBIT(dc_sc, DC_SIARESET, DC_SIA_RESET);
29296f2e892SBill Paul 			DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
29396f2e892SBill Paul 			mode &= ~DC_NETCFG_PORTSEL;
29496f2e892SBill Paul 			mode |= DC_NETCFG_SPEEDSEL;
29596f2e892SBill Paul 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
29696f2e892SBill Paul 				mode |= DC_NETCFG_FULLDUPLEX;
29796f2e892SBill Paul 			else
29896f2e892SBill Paul 				mode &= ~DC_NETCFG_FULLDUPLEX;
29996f2e892SBill Paul 			CSR_WRITE_4(dc_sc, DC_NETCFG, mode);
30096f2e892SBill Paul 			break;
30196f2e892SBill Paul 		default:
30296f2e892SBill Paul 			return(EINVAL);
30396f2e892SBill Paul 			break;
30496f2e892SBill Paul 		}
30596f2e892SBill Paul 		break;
30696f2e892SBill Paul 
30796f2e892SBill Paul 	case MII_TICK:
30896f2e892SBill Paul 		/*
30996f2e892SBill Paul 		 * If we're not currently selected, just return.
31096f2e892SBill Paul 		 */
31196f2e892SBill Paul 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
31296f2e892SBill Paul 			return (0);
31396f2e892SBill Paul 
31496f2e892SBill Paul 		/*
31596f2e892SBill Paul 		 * Only used for autonegotiation.
31696f2e892SBill Paul 		 */
31796f2e892SBill Paul 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
31896f2e892SBill Paul 			return (0);
31996f2e892SBill Paul 
32096f2e892SBill Paul 		/*
32196f2e892SBill Paul 		 * Is the interface even up?
32296f2e892SBill Paul 		 */
32396f2e892SBill Paul 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
32496f2e892SBill Paul 			return (0);
32596f2e892SBill Paul 
32696f2e892SBill Paul 		if (sc->mii_flags & MIIF_DOINGAUTO) {
32796f2e892SBill Paul 			if (++sc->mii_ticks != 5)
32896f2e892SBill Paul 				return(0);
32996f2e892SBill Paul 			else {
33096f2e892SBill Paul 				sc->mii_ticks = 0;
33196f2e892SBill Paul 				sc->mii_flags &= ~MIIF_DOINGAUTO;
33296f2e892SBill Paul 				sc->mii_flags |= MIIF_AUTOTIMEOUT;
33396f2e892SBill Paul 			}
33496f2e892SBill Paul 		}
33596f2e892SBill Paul 
33696f2e892SBill Paul 		sc->mii_flags &= ~MIIF_DOINGAUTO;
33796f2e892SBill Paul 
33896f2e892SBill Paul 		/*
33996f2e892SBill Paul 		 * Check to see if we have link.  If we do, we don't
34096f2e892SBill Paul 		 * need to restart the autonegotiation process.  Read
34196f2e892SBill Paul 		 * the BMSR twice in case it's latched.
34296f2e892SBill Paul 		 */
34396f2e892SBill Paul 		reg = CSR_READ_4(dc_sc, DC_10BTSTAT) &
34496f2e892SBill Paul 		    (DC_TSTAT_LS10|DC_TSTAT_LS100);
34596f2e892SBill Paul 
34696f2e892SBill Paul 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX &&
34796f2e892SBill Paul 		    !(reg & DC_TSTAT_LS100)) {
34896f2e892SBill Paul 			if (sc->mii_flags & MIIF_AUTOTIMEOUT) {
34996f2e892SBill Paul 				sc->mii_flags &= ~MIIF_AUTOTIMEOUT;
35096f2e892SBill Paul 				break;
35196f2e892SBill Paul 			} else
35296f2e892SBill Paul 				return(0);
35396f2e892SBill Paul 		} else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T &&
35496f2e892SBill Paul 		    !(reg & DC_TSTAT_LS10)) {
35596f2e892SBill Paul 			if (sc->mii_flags & MIIF_AUTOTIMEOUT) {
35696f2e892SBill Paul 				sc->mii_flags &= ~MIIF_AUTOTIMEOUT;
35796f2e892SBill Paul 				break;
35896f2e892SBill Paul 			} else
35996f2e892SBill Paul 				return(0);
36096f2e892SBill Paul 		} else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE &&
36196f2e892SBill Paul 		    (!(reg & DC_TSTAT_LS10) || !(reg & DC_TSTAT_LS100))) {
36296f2e892SBill Paul 			if (sc->mii_flags & MIIF_AUTOTIMEOUT) {
36396f2e892SBill Paul 				sc->mii_flags &= ~MIIF_AUTOTIMEOUT;
36496f2e892SBill Paul 				break;
36596f2e892SBill Paul 			} else
36696f2e892SBill Paul 				return(0);
36796f2e892SBill Paul 		} else if (CSR_READ_4(dc_sc, DC_ISR) & DC_ISR_LINKGOOD) {
36896f2e892SBill Paul 			if (sc->mii_flags & MIIF_AUTOTIMEOUT) {
36996f2e892SBill Paul 				sc->mii_flags &= ~MIIF_AUTOTIMEOUT;
37096f2e892SBill Paul 				break;
37196f2e892SBill Paul 			} else
37296f2e892SBill Paul 				return(0);
37396f2e892SBill Paul 		}
37496f2e892SBill Paul 
37596f2e892SBill Paul 		sc->mii_ticks = 0;
37696f2e892SBill Paul 		/*dcphy_reset(sc);*/
37796f2e892SBill Paul 		dcphy_auto(sc, 0);
37896f2e892SBill Paul 
37996f2e892SBill Paul 		break;
38096f2e892SBill Paul 	}
38196f2e892SBill Paul 
38296f2e892SBill Paul 	/* Update the media status. */
38396f2e892SBill Paul 	dcphy_status(sc);
38496f2e892SBill Paul 
38596f2e892SBill Paul 	/* Callback if something changed. */
38696f2e892SBill Paul 	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
38796f2e892SBill Paul 		MIIBUS_STATCHG(sc->mii_dev);
38896f2e892SBill Paul 		sc->mii_active = mii->mii_media_active;
38996f2e892SBill Paul 	}
39096f2e892SBill Paul 	return (0);
39196f2e892SBill Paul }
39296f2e892SBill Paul 
39396f2e892SBill Paul void
39496f2e892SBill Paul dcphy_status(sc)
39596f2e892SBill Paul 	struct mii_softc *sc;
39696f2e892SBill Paul {
39796f2e892SBill Paul 	struct mii_data *mii = sc->mii_pdata;
39896f2e892SBill Paul 	int reg, anlpar;
39996f2e892SBill Paul 	struct dc_softc		*dc_sc;
40096f2e892SBill Paul 
40196f2e892SBill Paul 	dc_sc = mii->mii_ifp->if_softc;
40296f2e892SBill Paul 
40396f2e892SBill Paul 	mii->mii_media_status = IFM_AVALID;
40496f2e892SBill Paul 	mii->mii_media_active = IFM_ETHER;
40596f2e892SBill Paul 
40696f2e892SBill Paul 	reg = CSR_READ_4(dc_sc, DC_10BTSTAT) &
40796f2e892SBill Paul 	     (DC_TSTAT_LS10|DC_TSTAT_LS100);
40896f2e892SBill Paul 
40996f2e892SBill Paul 	if (!(reg & DC_TSTAT_LS10) || !(reg & DC_TSTAT_LS100))
41096f2e892SBill Paul 		mii->mii_media_status |= IFM_ACTIVE;
41196f2e892SBill Paul 
41296f2e892SBill Paul 	if (sc->mii_flags & MIIF_DOINGAUTO) {
41396f2e892SBill Paul 		mii->mii_media_active |= IFM_NONE;
41496f2e892SBill Paul 		return;
41596f2e892SBill Paul 	}
41696f2e892SBill Paul 
41796f2e892SBill Paul 	if (CSR_READ_4(dc_sc, DC_10BTCTRL) & DC_TCTL_AUTONEGENBL &&
41896f2e892SBill Paul 	    CSR_READ_4(dc_sc, DC_10BTSTAT) & DC_TSTAT_ANEGSTAT) {
41996f2e892SBill Paul 		/* Erg, still trying, I guess... */
42096f2e892SBill Paul 		if ((CSR_READ_4(dc_sc, DC_10BTSTAT) &
42196f2e892SBill Paul 		    DC_ASTAT_AUTONEGCMP) != DC_ASTAT_AUTONEGCMP) {
42296f2e892SBill Paul 			mii->mii_media_active |= IFM_NONE;
42396f2e892SBill Paul 			return;
42496f2e892SBill Paul 		}
42596f2e892SBill Paul 
42696f2e892SBill Paul 		if (CSR_READ_4(dc_sc, DC_10BTSTAT) & DC_TSTAT_LP_CAN_NWAY) {
42796f2e892SBill Paul 			anlpar = CSR_READ_4(dc_sc, DC_10BTSTAT) >> 16;
42891cc2adbSBill Paul 			if (anlpar & ANLPAR_T4 &&
42991cc2adbSBill Paul 			    sc->mii_capabilities & BMSR_100TXHDX)
43096f2e892SBill Paul 				mii->mii_media_active |= IFM_100_T4;
43191cc2adbSBill Paul 			else if (anlpar & ANLPAR_TX_FD &&
43291cc2adbSBill Paul 			    sc->mii_capabilities & BMSR_100TXHDX)
43396f2e892SBill Paul 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
43491cc2adbSBill Paul 			else if (anlpar & ANLPAR_TX &&
43591cc2adbSBill Paul 			    sc->mii_capabilities & BMSR_100TXHDX)
43696f2e892SBill Paul 				mii->mii_media_active |= IFM_100_TX;
43796f2e892SBill Paul 			else if (anlpar & ANLPAR_10_FD)
43896f2e892SBill Paul 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
43996f2e892SBill Paul 			else if (anlpar & ANLPAR_10)
44096f2e892SBill Paul 				mii->mii_media_active |= IFM_10_T;
44196f2e892SBill Paul 			else
44296f2e892SBill Paul 				mii->mii_media_active |= IFM_NONE;
44396f2e892SBill Paul 			if (DC_IS_INTEL(dc_sc))
44496f2e892SBill Paul 				DC_CLRBIT(dc_sc, DC_10BTCTRL,
44596f2e892SBill Paul 				    DC_TCTL_AUTONEGENBL);
44696f2e892SBill Paul 			return;
44796f2e892SBill Paul 		}
44896f2e892SBill Paul 		/*
44996f2e892SBill Paul 		 * If the other side doesn't support NWAY, then the
45096f2e892SBill Paul 		 * best we can do is determine if we have a 10Mbps or
45196f2e892SBill Paul 		 * 100Mbps link. There's no way to know if the link
45296f2e892SBill Paul 		 * is full or half duplex, so we default to half duplex
45396f2e892SBill Paul 		 * and hope that the user is clever enough to manually
45496f2e892SBill Paul 		 * change the media settings if we're wrong.
45596f2e892SBill Paul 		 */
45696f2e892SBill Paul 		if (!(reg & DC_TSTAT_LS100))
45796f2e892SBill Paul 			mii->mii_media_active |= IFM_100_TX;
45896f2e892SBill Paul 		else if (!(reg & DC_TSTAT_LS10))
45996f2e892SBill Paul 			mii->mii_media_active |= IFM_10_T;
46096f2e892SBill Paul 		else
46196f2e892SBill Paul 			mii->mii_media_active |= IFM_NONE;
46296f2e892SBill Paul 		if (DC_IS_INTEL(dc_sc))
46396f2e892SBill Paul 			DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
46496f2e892SBill Paul 		return;
46596f2e892SBill Paul 	}
46696f2e892SBill Paul 
46796f2e892SBill Paul 	if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_SCRAMBLER)
46896f2e892SBill Paul 		mii->mii_media_active |= IFM_100_TX;
46996f2e892SBill Paul 	else
47096f2e892SBill Paul 		mii->mii_media_active |= IFM_10_T;
47196f2e892SBill Paul 	if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_FULLDUPLEX)
47296f2e892SBill Paul 		mii->mii_media_active |= IFM_FDX;
47396f2e892SBill Paul 
47496f2e892SBill Paul 	return;
47596f2e892SBill Paul }
47696f2e892SBill Paul 
47796f2e892SBill Paul static int
47896f2e892SBill Paul dcphy_auto(mii, waitfor)
47996f2e892SBill Paul 	struct mii_softc	*mii;
48096f2e892SBill Paul 	int			waitfor;
48196f2e892SBill Paul {
48296f2e892SBill Paul 	int			i;
48396f2e892SBill Paul 	struct dc_softc		*sc;
48496f2e892SBill Paul 
48596f2e892SBill Paul 	sc = mii->mii_pdata->mii_ifp->if_softc;
48696f2e892SBill Paul 
48796f2e892SBill Paul 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
48896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
48996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
49096f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
49191cc2adbSBill Paul 		if (mii->mii_capabilities & BMSR_100TXHDX)
49296f2e892SBill Paul 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x3FFFF);
49391cc2adbSBill Paul 		else
49491cc2adbSBill Paul 			CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFF);
49596f2e892SBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
49696f2e892SBill Paul 		DC_SETBIT(sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
49796f2e892SBill Paul 		DC_SETBIT(sc, DC_10BTSTAT, DC_ASTAT_TXDISABLE);
49896f2e892SBill Paul 	}
49996f2e892SBill Paul 
50096f2e892SBill Paul 	if (waitfor) {
50196f2e892SBill Paul 		/* Wait 500ms for it to complete. */
50296f2e892SBill Paul 		for (i = 0; i < 500; i++) {
50396f2e892SBill Paul 			if ((CSR_READ_4(sc, DC_10BTSTAT) & DC_TSTAT_ANEGSTAT)
50496f2e892SBill Paul 			    == DC_ASTAT_AUTONEGCMP)
50596f2e892SBill Paul 				return(0);
50696f2e892SBill Paul 			DELAY(1000);
50796f2e892SBill Paul 		}
50896f2e892SBill Paul 		/*
50996f2e892SBill Paul 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
51096f2e892SBill Paul 		 * If that's set, a timeout is pending, and it will
51196f2e892SBill Paul 		 * clear the flag.
51296f2e892SBill Paul 		 */
51396f2e892SBill Paul 		return(EIO);
51496f2e892SBill Paul 	}
51596f2e892SBill Paul 
51696f2e892SBill Paul 	/*
51796f2e892SBill Paul 	 * Just let it finish asynchronously.  This is for the benefit of
51896f2e892SBill Paul 	 * the tick handler driving autonegotiation.  Don't want 500ms
51996f2e892SBill Paul 	 * delays all the time while the system is running!
52096f2e892SBill Paul 	 */
52196f2e892SBill Paul 	if ((mii->mii_flags & MIIF_DOINGAUTO) == 0)
52296f2e892SBill Paul 		mii->mii_flags |= MIIF_DOINGAUTO;
52396f2e892SBill Paul 
52496f2e892SBill Paul 	return(EJUSTRETURN);
52596f2e892SBill Paul }
52696f2e892SBill Paul 
52796f2e892SBill Paul static void
52896f2e892SBill Paul dcphy_reset(mii)
52996f2e892SBill Paul 	struct mii_softc	*mii;
53096f2e892SBill Paul {
53196f2e892SBill Paul 	struct dc_softc		*sc;
53296f2e892SBill Paul 
53396f2e892SBill Paul 	sc = mii->mii_pdata->mii_ifp->if_softc;
53496f2e892SBill Paul 
53596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
53696f2e892SBill Paul 	DELAY(1000);
53796f2e892SBill Paul 	DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
53896f2e892SBill Paul 
53996f2e892SBill Paul 	return;
54096f2e892SBill Paul }
54196f2e892SBill Paul 
542