xref: /freebsd/sys/dev/mii/acphy.c (revision a35b933378bb0fb0df1cec1d85587b5db66e8bf9)
19526a692SSemen Ustimenko /*-
29526a692SSemen Ustimenko  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
39526a692SSemen Ustimenko  * All rights reserved.
49526a692SSemen Ustimenko  *
59526a692SSemen Ustimenko  * This code is derived from software contributed to The NetBSD Foundation
69526a692SSemen Ustimenko  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
79526a692SSemen Ustimenko  * NASA Ames Research Center.
89526a692SSemen Ustimenko  *
99526a692SSemen Ustimenko  * Redistribution and use in source and binary forms, with or without
109526a692SSemen Ustimenko  * modification, are permitted provided that the following conditions
119526a692SSemen Ustimenko  * are met:
129526a692SSemen Ustimenko  * 1. Redistributions of source code must retain the above copyright
139526a692SSemen Ustimenko  *    notice, this list of conditions and the following disclaimer.
149526a692SSemen Ustimenko  * 2. Redistributions in binary form must reproduce the above copyright
159526a692SSemen Ustimenko  *    notice, this list of conditions and the following disclaimer in the
169526a692SSemen Ustimenko  *    documentation and/or other materials provided with the distribution.
179526a692SSemen Ustimenko  * 3. All advertising materials mentioning features or use of this software
189526a692SSemen Ustimenko  *    must display the following acknowledgement:
199526a692SSemen Ustimenko  *	This product includes software developed by the NetBSD
209526a692SSemen Ustimenko  *	Foundation, Inc. and its contributors.
219526a692SSemen Ustimenko  * 4. Neither the name of The NetBSD Foundation nor the names of its
229526a692SSemen Ustimenko  *    contributors may be used to endorse or promote products derived
239526a692SSemen Ustimenko  *    from this software without specific prior written permission.
249526a692SSemen Ustimenko  *
259526a692SSemen Ustimenko  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
269526a692SSemen Ustimenko  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
279526a692SSemen Ustimenko  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
289526a692SSemen Ustimenko  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
299526a692SSemen Ustimenko  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
309526a692SSemen Ustimenko  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
319526a692SSemen Ustimenko  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
329526a692SSemen Ustimenko  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
339526a692SSemen Ustimenko  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
349526a692SSemen Ustimenko  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
359526a692SSemen Ustimenko  * POSSIBILITY OF SUCH DAMAGE.
369526a692SSemen Ustimenko  */
37aad970f1SDavid E. O'Brien 
38098ca2bdSWarner Losh /*-
399526a692SSemen Ustimenko  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
409526a692SSemen Ustimenko  *
419526a692SSemen Ustimenko  * Redistribution and use in source and binary forms, with or without
429526a692SSemen Ustimenko  * modification, are permitted provided that the following conditions
439526a692SSemen Ustimenko  * are met:
449526a692SSemen Ustimenko  * 1. Redistributions of source code must retain the above copyright
459526a692SSemen Ustimenko  *    notice, this list of conditions and the following disclaimer.
469526a692SSemen Ustimenko  * 2. Redistributions in binary form must reproduce the above copyright
479526a692SSemen Ustimenko  *    notice, this list of conditions and the following disclaimer in the
489526a692SSemen Ustimenko  *    documentation and/or other materials provided with the distribution.
499526a692SSemen Ustimenko  * 3. All advertising materials mentioning features or use of this software
509526a692SSemen Ustimenko  *    must display the following acknowledgement:
519526a692SSemen Ustimenko  *	This product includes software developed by Manuel Bouyer.
529526a692SSemen Ustimenko  * 4. The name of the author may not be used to endorse or promote products
539526a692SSemen Ustimenko  *    derived from this software without specific prior written permission.
549526a692SSemen Ustimenko  *
559526a692SSemen Ustimenko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
569526a692SSemen Ustimenko  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
579526a692SSemen Ustimenko  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
589526a692SSemen Ustimenko  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
599526a692SSemen Ustimenko  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
609526a692SSemen Ustimenko  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
619526a692SSemen Ustimenko  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
629526a692SSemen Ustimenko  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
639526a692SSemen Ustimenko  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
649526a692SSemen Ustimenko  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
659526a692SSemen Ustimenko  */
669526a692SSemen Ustimenko 
6750aa1061SMarius Strobl #include <sys/cdefs.h>
6850aa1061SMarius Strobl __FBSDID("$FreeBSD$");
6950aa1061SMarius Strobl 
709526a692SSemen Ustimenko /*
719526a692SSemen Ustimenko  * Driver for Altima AC101 10/100 PHY
729526a692SSemen Ustimenko  */
739526a692SSemen Ustimenko 
749526a692SSemen Ustimenko #include <sys/param.h>
759526a692SSemen Ustimenko #include <sys/systm.h>
769526a692SSemen Ustimenko #include <sys/kernel.h>
779526a692SSemen Ustimenko #include <sys/socket.h>
789526a692SSemen Ustimenko #include <sys/errno.h>
799526a692SSemen Ustimenko #include <sys/module.h>
809526a692SSemen Ustimenko #include <sys/bus.h>
819526a692SSemen Ustimenko 
829526a692SSemen Ustimenko #include <net/if.h>
839526a692SSemen Ustimenko #include <net/if_media.h>
849526a692SSemen Ustimenko 
859526a692SSemen Ustimenko #include <dev/mii/mii.h>
869526a692SSemen Ustimenko #include <dev/mii/miivar.h>
872d3ce713SDavid E. O'Brien #include "miidevs.h"
889526a692SSemen Ustimenko 
899526a692SSemen Ustimenko #include <dev/mii/acphyreg.h>
909526a692SSemen Ustimenko 
919526a692SSemen Ustimenko #include "miibus_if.h"
929526a692SSemen Ustimenko 
93e51a25f8SAlfred Perlstein static int acphy_probe(device_t);
94e51a25f8SAlfred Perlstein static int acphy_attach(device_t);
959526a692SSemen Ustimenko 
969526a692SSemen Ustimenko static device_method_t acphy_methods[] = {
979526a692SSemen Ustimenko 	/* device interface */
989526a692SSemen Ustimenko 	DEVMETHOD(device_probe,		acphy_probe),
999526a692SSemen Ustimenko 	DEVMETHOD(device_attach,	acphy_attach),
100279fe8d1SPoul-Henning Kamp 	DEVMETHOD(device_detach,	mii_phy_detach),
1019526a692SSemen Ustimenko 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1029526a692SSemen Ustimenko 	{ 0, 0 }
1039526a692SSemen Ustimenko };
1049526a692SSemen Ustimenko 
1059526a692SSemen Ustimenko static devclass_t acphy_devclass;
1069526a692SSemen Ustimenko 
1079526a692SSemen Ustimenko static driver_t acphy_driver = {
1089526a692SSemen Ustimenko 	"acphy",
1099526a692SSemen Ustimenko 	acphy_methods,
1109526a692SSemen Ustimenko 	sizeof(struct mii_softc)
1119526a692SSemen Ustimenko };
1129526a692SSemen Ustimenko 
1139526a692SSemen Ustimenko DRIVER_MODULE(acphy, miibus, acphy_driver, acphy_devclass, 0, 0);
1149526a692SSemen Ustimenko 
115e51a25f8SAlfred Perlstein static int	acphy_service(struct mii_softc *, struct mii_data *, int);
116e51a25f8SAlfred Perlstein static void	acphy_reset(struct mii_softc *);
117e51a25f8SAlfred Perlstein static void	acphy_status(struct mii_softc *);
1189526a692SSemen Ustimenko 
119a35b9333SMarius Strobl static const struct mii_phydesc acphys[] = {
120a35b9333SMarius Strobl 	MII_PHY_DESC(xxALTIMA, AC101),
121a35b9333SMarius Strobl 	MII_PHY_DESC(xxALTIMA, AC101L),
122a35b9333SMarius Strobl 	MII_PHY_END
123a35b9333SMarius Strobl };
124a35b9333SMarius Strobl 
1259c1c2e99SAlfred Perlstein static int
1267d830ac9SWarner Losh acphy_probe(device_t dev)
1279526a692SSemen Ustimenko {
1289526a692SSemen Ustimenko 
129a35b9333SMarius Strobl 	return (mii_phy_dev_probe(dev, acphys, BUS_PROBE_DEFAULT));
1309526a692SSemen Ustimenko }
1319526a692SSemen Ustimenko 
1329c1c2e99SAlfred Perlstein static int
1337d830ac9SWarner Losh acphy_attach(device_t dev)
1349526a692SSemen Ustimenko {
1359526a692SSemen Ustimenko 	struct mii_softc *sc;
1369526a692SSemen Ustimenko 	struct mii_attach_args *ma;
1379526a692SSemen Ustimenko 	struct mii_data *mii;
1389526a692SSemen Ustimenko 
1399526a692SSemen Ustimenko 	sc = device_get_softc(dev);
1409526a692SSemen Ustimenko 	ma = device_get_ivars(dev);
1419526a692SSemen Ustimenko 	sc->mii_dev = device_get_parent(dev);
1429526a692SSemen Ustimenko 	mii = device_get_softc(sc->mii_dev);
1439526a692SSemen Ustimenko 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
1449526a692SSemen Ustimenko 
1459526a692SSemen Ustimenko 	sc->mii_inst = mii->mii_instance;
1469526a692SSemen Ustimenko 	sc->mii_phy = ma->mii_phyno;
1479526a692SSemen Ustimenko 	sc->mii_service = acphy_service;
1489526a692SSemen Ustimenko 	sc->mii_pdata = mii;
1499526a692SSemen Ustimenko 	sc->mii_flags |= MIIF_NOISOLATE;
1509526a692SSemen Ustimenko 
1519526a692SSemen Ustimenko 	acphy_reset(sc);
1529526a692SSemen Ustimenko 
1539526a692SSemen Ustimenko 	mii->mii_instance++;
1549526a692SSemen Ustimenko 
1559526a692SSemen Ustimenko 	sc->mii_capabilities =
1569526a692SSemen Ustimenko 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
1579526a692SSemen Ustimenko 	device_printf(dev, " ");
15807dd9383SPoul-Henning Kamp 	mii_add_media(sc);
1599526a692SSemen Ustimenko 	printf("\n");
1609526a692SSemen Ustimenko 
1619526a692SSemen Ustimenko 	MIIBUS_MEDIAINIT(sc->mii_dev);
1629526a692SSemen Ustimenko 	return (0);
1639526a692SSemen Ustimenko }
1649526a692SSemen Ustimenko 
165d9730b8bSJonathan Lemon static int
1667d830ac9SWarner Losh acphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
1679526a692SSemen Ustimenko {
1689526a692SSemen Ustimenko 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
1699526a692SSemen Ustimenko 	int reg;
1709526a692SSemen Ustimenko 
1719526a692SSemen Ustimenko 	/*
1724592db46SSemen Ustimenko 	 * If we're not selected, then do nothing, just isolate and power
1734592db46SSemen Ustimenko 	 * down, if changing media.
1749526a692SSemen Ustimenko 	 */
1759526a692SSemen Ustimenko 	if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
1764592db46SSemen Ustimenko 		if (cmd == MII_MEDIACHG) {
1779526a692SSemen Ustimenko 			reg = PHY_READ(sc, MII_BMCR);
1789526a692SSemen Ustimenko 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO | BMCR_PDOWN);
1794592db46SSemen Ustimenko 		}
1804592db46SSemen Ustimenko 
1819526a692SSemen Ustimenko 		return (0);
1829526a692SSemen Ustimenko 	}
1839526a692SSemen Ustimenko 
1844592db46SSemen Ustimenko 	switch (cmd) {
1854592db46SSemen Ustimenko 	case MII_POLLSTAT:
1864592db46SSemen Ustimenko 		break;
1874592db46SSemen Ustimenko 
1884592db46SSemen Ustimenko 	case MII_MEDIACHG:
1899526a692SSemen Ustimenko 		/*
1909526a692SSemen Ustimenko 		 * If the interface is not up, don't do anything.
1919526a692SSemen Ustimenko 		 */
1929526a692SSemen Ustimenko 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
1939526a692SSemen Ustimenko 			break;
1949526a692SSemen Ustimenko 
1954592db46SSemen Ustimenko 		/* Wake & deisolate up if necessary */
1969526a692SSemen Ustimenko 		reg = PHY_READ(sc, MII_BMCR);
1979526a692SSemen Ustimenko 		if (reg & (BMCR_ISO | BMCR_PDOWN))
1989526a692SSemen Ustimenko 			PHY_WRITE(sc, MII_BMCR, reg & ~(BMCR_ISO | BMCR_PDOWN));
1999526a692SSemen Ustimenko 
2009526a692SSemen Ustimenko 		switch (IFM_SUBTYPE(ife->ifm_media)) {
2019526a692SSemen Ustimenko 		case IFM_AUTO:
2029526a692SSemen Ustimenko 			/*
2039526a692SSemen Ustimenko 			 * If we're already in auto mode, just return.
2049526a692SSemen Ustimenko 			 */
2059526a692SSemen Ustimenko 			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
2069526a692SSemen Ustimenko 				return (0);
2079526a692SSemen Ustimenko 
208fd94424cSPoul-Henning Kamp 			(void) mii_phy_auto(sc);
2099526a692SSemen Ustimenko 			break;
2109526a692SSemen Ustimenko 
2119526a692SSemen Ustimenko 		default:
2129526a692SSemen Ustimenko 			/*
2139526a692SSemen Ustimenko 			 * BMCR data is stored in the ifmedia entry.
2149526a692SSemen Ustimenko 			 */
2159526a692SSemen Ustimenko 			PHY_WRITE(sc, MII_ANAR,
2169526a692SSemen Ustimenko 			    mii_anar(ife->ifm_media));
2179526a692SSemen Ustimenko 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
2189526a692SSemen Ustimenko 		}
2199526a692SSemen Ustimenko 		break;
2209526a692SSemen Ustimenko 
2219526a692SSemen Ustimenko 	case MII_TICK:
2229526a692SSemen Ustimenko 		/*
2239526a692SSemen Ustimenko 		 * Is the interface even up?
2249526a692SSemen Ustimenko 		 */
2259526a692SSemen Ustimenko 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
2269526a692SSemen Ustimenko 			return (0);
2279526a692SSemen Ustimenko 
2289526a692SSemen Ustimenko 		/*
229d9730b8bSJonathan Lemon 		 * Only used for autonegotiation.
2309526a692SSemen Ustimenko 		 */
231d9730b8bSJonathan Lemon 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
232d9730b8bSJonathan Lemon 			break;
233d9730b8bSJonathan Lemon 
234d9730b8bSJonathan Lemon 		/*
2354592db46SSemen Ustimenko 		 * This PHY's autonegotiation doesn't need to be kicked.
236d9730b8bSJonathan Lemon 		 */
2379526a692SSemen Ustimenko 		break;
2389526a692SSemen Ustimenko 	}
2399526a692SSemen Ustimenko 
2409526a692SSemen Ustimenko 	/* Update the media status. */
2419526a692SSemen Ustimenko 	acphy_status(sc);
2429526a692SSemen Ustimenko 
2439526a692SSemen Ustimenko 	/* Callback if something changed. */
244d9730b8bSJonathan Lemon 	mii_phy_update(sc, cmd);
2459526a692SSemen Ustimenko 	return (0);
2469526a692SSemen Ustimenko }
2479526a692SSemen Ustimenko 
248d9730b8bSJonathan Lemon static void
2497d830ac9SWarner Losh acphy_status(struct mii_softc *sc)
2509526a692SSemen Ustimenko {
2519526a692SSemen Ustimenko 	struct mii_data *mii = sc->mii_pdata;
2529526a692SSemen Ustimenko 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
2539526a692SSemen Ustimenko 	int bmsr, bmcr, diag;
2549526a692SSemen Ustimenko 
2559526a692SSemen Ustimenko 	mii->mii_media_status = IFM_AVALID;
2569526a692SSemen Ustimenko 	mii->mii_media_active = IFM_ETHER;
2579526a692SSemen Ustimenko 
2589526a692SSemen Ustimenko 	bmsr = PHY_READ(sc, MII_BMSR) |
2599526a692SSemen Ustimenko 	    PHY_READ(sc, MII_BMSR);
2609526a692SSemen Ustimenko 	if (bmsr & BMSR_LINK)
2619526a692SSemen Ustimenko 		mii->mii_media_status |= IFM_ACTIVE;
2629526a692SSemen Ustimenko 
2639526a692SSemen Ustimenko 	bmcr = PHY_READ(sc, MII_BMCR);
2649526a692SSemen Ustimenko 	if (bmcr & BMCR_ISO) {
2659526a692SSemen Ustimenko 		mii->mii_media_active |= IFM_NONE;
2669526a692SSemen Ustimenko 		mii->mii_media_status = 0;
2679526a692SSemen Ustimenko 		return;
2689526a692SSemen Ustimenko 	}
2699526a692SSemen Ustimenko 
2709526a692SSemen Ustimenko 	if (bmcr & BMCR_LOOP)
2719526a692SSemen Ustimenko 		mii->mii_media_active |= IFM_LOOP;
2729526a692SSemen Ustimenko 
2739526a692SSemen Ustimenko 	if (bmcr & BMCR_AUTOEN) {
2749526a692SSemen Ustimenko 		if ((bmsr & BMSR_ACOMP) == 0) {
2759526a692SSemen Ustimenko 			/* Erg, still trying, I guess... */
2769526a692SSemen Ustimenko 			mii->mii_media_active |= IFM_NONE;
2779526a692SSemen Ustimenko 			return;
2789526a692SSemen Ustimenko 		}
2794592db46SSemen Ustimenko 		diag = PHY_READ(sc, MII_ACPHY_DIAG);
2809526a692SSemen Ustimenko 		if (diag & AC_DIAG_SPEED)
2819526a692SSemen Ustimenko 			mii->mii_media_active |= IFM_100_TX;
2829526a692SSemen Ustimenko 		else
2839526a692SSemen Ustimenko 			mii->mii_media_active |= IFM_10_T;
2849526a692SSemen Ustimenko 
2859526a692SSemen Ustimenko 		if (diag & AC_DIAG_DUPLEX)
2869526a692SSemen Ustimenko 			mii->mii_media_active |= IFM_FDX;
2879526a692SSemen Ustimenko 	} else
2889526a692SSemen Ustimenko 		mii->mii_media_active = ife->ifm_media;
2899526a692SSemen Ustimenko }
2909526a692SSemen Ustimenko 
291d9730b8bSJonathan Lemon static void
2927d830ac9SWarner Losh acphy_reset(struct mii_softc *sc)
2939526a692SSemen Ustimenko {
2949526a692SSemen Ustimenko 
2959526a692SSemen Ustimenko 	mii_phy_reset(sc);
2969526a692SSemen Ustimenko 	PHY_WRITE(sc, MII_ACPHY_INT, 0);
2979526a692SSemen Ustimenko }
298