xref: /freebsd/sys/dev/mii/acphy.c (revision aad970f1fee9a2a3e5a0f880be9b87c6193b3bd1)
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 
38aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
39aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
409526a692SSemen Ustimenko 
419526a692SSemen Ustimenko /*
429526a692SSemen Ustimenko  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
439526a692SSemen Ustimenko  *
449526a692SSemen Ustimenko  * Redistribution and use in source and binary forms, with or without
459526a692SSemen Ustimenko  * modification, are permitted provided that the following conditions
469526a692SSemen Ustimenko  * are met:
479526a692SSemen Ustimenko  * 1. Redistributions of source code must retain the above copyright
489526a692SSemen Ustimenko  *    notice, this list of conditions and the following disclaimer.
499526a692SSemen Ustimenko  * 2. Redistributions in binary form must reproduce the above copyright
509526a692SSemen Ustimenko  *    notice, this list of conditions and the following disclaimer in the
519526a692SSemen Ustimenko  *    documentation and/or other materials provided with the distribution.
529526a692SSemen Ustimenko  * 3. All advertising materials mentioning features or use of this software
539526a692SSemen Ustimenko  *    must display the following acknowledgement:
549526a692SSemen Ustimenko  *	This product includes software developed by Manuel Bouyer.
559526a692SSemen Ustimenko  * 4. The name of the author may not be used to endorse or promote products
569526a692SSemen Ustimenko  *    derived from this software without specific prior written permission.
579526a692SSemen Ustimenko  *
589526a692SSemen Ustimenko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
599526a692SSemen Ustimenko  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
609526a692SSemen Ustimenko  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
619526a692SSemen Ustimenko  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
629526a692SSemen Ustimenko  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
639526a692SSemen Ustimenko  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
649526a692SSemen Ustimenko  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
659526a692SSemen Ustimenko  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
669526a692SSemen Ustimenko  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
679526a692SSemen Ustimenko  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
689526a692SSemen Ustimenko  */
699526a692SSemen Ustimenko 
709526a692SSemen Ustimenko /*
719526a692SSemen Ustimenko  * Driver for Altima AC101 10/100 PHY
729526a692SSemen Ustimenko  */
739526a692SSemen Ustimenko 
748368cf8fSDavid E. O'Brien #include <sys/cdefs.h>
758368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$");
768368cf8fSDavid E. O'Brien 
779526a692SSemen Ustimenko #include <sys/param.h>
789526a692SSemen Ustimenko #include <sys/systm.h>
799526a692SSemen Ustimenko #include <sys/kernel.h>
809526a692SSemen Ustimenko #include <sys/socket.h>
819526a692SSemen Ustimenko #include <sys/errno.h>
829526a692SSemen Ustimenko #include <sys/module.h>
839526a692SSemen Ustimenko #include <sys/bus.h>
849526a692SSemen Ustimenko 
859526a692SSemen Ustimenko #include <net/if.h>
869526a692SSemen Ustimenko #include <net/if_media.h>
879526a692SSemen Ustimenko 
889526a692SSemen Ustimenko #include <dev/mii/mii.h>
899526a692SSemen Ustimenko #include <dev/mii/miivar.h>
902d3ce713SDavid E. O'Brien #include "miidevs.h"
919526a692SSemen Ustimenko 
929526a692SSemen Ustimenko #include <dev/mii/acphyreg.h>
939526a692SSemen Ustimenko 
949526a692SSemen Ustimenko #include "miibus_if.h"
959526a692SSemen Ustimenko 
96e51a25f8SAlfred Perlstein static int acphy_probe(device_t);
97e51a25f8SAlfred Perlstein static int acphy_attach(device_t);
989526a692SSemen Ustimenko 
999526a692SSemen Ustimenko static device_method_t acphy_methods[] = {
1009526a692SSemen Ustimenko 	/* device interface */
1019526a692SSemen Ustimenko 	DEVMETHOD(device_probe,		acphy_probe),
1029526a692SSemen Ustimenko 	DEVMETHOD(device_attach,	acphy_attach),
103279fe8d1SPoul-Henning Kamp 	DEVMETHOD(device_detach,	mii_phy_detach),
1049526a692SSemen Ustimenko 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1059526a692SSemen Ustimenko 	{ 0, 0 }
1069526a692SSemen Ustimenko };
1079526a692SSemen Ustimenko 
1089526a692SSemen Ustimenko static devclass_t acphy_devclass;
1099526a692SSemen Ustimenko 
1109526a692SSemen Ustimenko static driver_t acphy_driver = {
1119526a692SSemen Ustimenko 	"acphy",
1129526a692SSemen Ustimenko 	acphy_methods,
1139526a692SSemen Ustimenko 	sizeof(struct mii_softc)
1149526a692SSemen Ustimenko };
1159526a692SSemen Ustimenko 
1169526a692SSemen Ustimenko DRIVER_MODULE(acphy, miibus, acphy_driver, acphy_devclass, 0, 0);
1179526a692SSemen Ustimenko 
118e51a25f8SAlfred Perlstein static int	acphy_service(struct mii_softc *, struct mii_data *, int);
119e51a25f8SAlfred Perlstein static void	acphy_reset(struct mii_softc *);
120e51a25f8SAlfred Perlstein static void	acphy_status(struct mii_softc *);
1219526a692SSemen Ustimenko 
1229c1c2e99SAlfred Perlstein static int
1239c1c2e99SAlfred Perlstein acphy_probe(dev)
1249526a692SSemen Ustimenko 	device_t		dev;
1259526a692SSemen Ustimenko {
1269526a692SSemen Ustimenko 	struct mii_attach_args *ma;
1279526a692SSemen Ustimenko 
1289526a692SSemen Ustimenko 	ma = device_get_ivars(dev);
1299526a692SSemen Ustimenko 
1309526a692SSemen Ustimenko 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxALTIMA &&
1319526a692SSemen Ustimenko 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxALTIMA_AC101) {
1329526a692SSemen Ustimenko 		device_set_desc(dev, MII_STR_xxALTIMA_AC101);
133f9b77424SDavid E. O'Brien 	} else if(MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxALTIMA &&
134f9b77424SDavid E. O'Brien 	          MII_MODEL(ma->mii_id2) == MII_MODEL_xxALTIMA_AC101L) {
135f9b77424SDavid E. O'Brien 		device_set_desc(dev, MII_STR_xxALTIMA_AC101L);
1369526a692SSemen Ustimenko 	} else
1379526a692SSemen Ustimenko 		return (ENXIO);
1389526a692SSemen Ustimenko 
1399526a692SSemen Ustimenko 	return (0);
1409526a692SSemen Ustimenko }
1419526a692SSemen Ustimenko 
1429c1c2e99SAlfred Perlstein static int
1439c1c2e99SAlfred Perlstein acphy_attach(dev)
1449526a692SSemen Ustimenko 	device_t		dev;
1459526a692SSemen Ustimenko {
1469526a692SSemen Ustimenko 	struct mii_softc *sc;
1479526a692SSemen Ustimenko 	struct mii_attach_args *ma;
1489526a692SSemen Ustimenko 	struct mii_data *mii;
1499526a692SSemen Ustimenko 
1509526a692SSemen Ustimenko 	sc = device_get_softc(dev);
1519526a692SSemen Ustimenko 	ma = device_get_ivars(dev);
1529526a692SSemen Ustimenko 	sc->mii_dev = device_get_parent(dev);
1539526a692SSemen Ustimenko 	mii = device_get_softc(sc->mii_dev);
1549526a692SSemen Ustimenko 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
1559526a692SSemen Ustimenko 
1569526a692SSemen Ustimenko 	sc->mii_inst = mii->mii_instance;
1579526a692SSemen Ustimenko 	sc->mii_phy = ma->mii_phyno;
1589526a692SSemen Ustimenko 	sc->mii_service = acphy_service;
1599526a692SSemen Ustimenko 	sc->mii_pdata = mii;
1609526a692SSemen Ustimenko 	sc->mii_flags |= MIIF_NOISOLATE;
1619526a692SSemen Ustimenko 
1629526a692SSemen Ustimenko 	acphy_reset(sc);
1639526a692SSemen Ustimenko 
1649526a692SSemen Ustimenko 	mii->mii_instance++;
1659526a692SSemen Ustimenko 
1669526a692SSemen Ustimenko 	sc->mii_capabilities =
1679526a692SSemen Ustimenko 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
1689526a692SSemen Ustimenko 	device_printf(dev, " ");
16907dd9383SPoul-Henning Kamp 	mii_add_media(sc);
1709526a692SSemen Ustimenko 	printf("\n");
1719526a692SSemen Ustimenko 
1729526a692SSemen Ustimenko 	MIIBUS_MEDIAINIT(sc->mii_dev);
1739526a692SSemen Ustimenko 	return (0);
1749526a692SSemen Ustimenko }
1759526a692SSemen Ustimenko 
176d9730b8bSJonathan Lemon static int
1779526a692SSemen Ustimenko acphy_service(sc, mii, cmd)
1789526a692SSemen Ustimenko 	struct mii_softc *sc;
1799526a692SSemen Ustimenko 	struct mii_data *mii;
1809526a692SSemen Ustimenko 	int cmd;
1819526a692SSemen Ustimenko {
1829526a692SSemen Ustimenko 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
1839526a692SSemen Ustimenko 	int reg;
1849526a692SSemen Ustimenko 
1859526a692SSemen Ustimenko 	/*
1864592db46SSemen Ustimenko 	 * If we're not selected, then do nothing, just isolate and power
1874592db46SSemen Ustimenko 	 * down, if changing media.
1889526a692SSemen Ustimenko 	 */
1899526a692SSemen Ustimenko 	if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
1904592db46SSemen Ustimenko 		if (cmd == MII_MEDIACHG) {
1919526a692SSemen Ustimenko 			reg = PHY_READ(sc, MII_BMCR);
1929526a692SSemen Ustimenko 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO | BMCR_PDOWN);
1934592db46SSemen Ustimenko 		}
1944592db46SSemen Ustimenko 
1959526a692SSemen Ustimenko 		return (0);
1969526a692SSemen Ustimenko 	}
1979526a692SSemen Ustimenko 
1984592db46SSemen Ustimenko 	switch (cmd) {
1994592db46SSemen Ustimenko 	case MII_POLLSTAT:
2004592db46SSemen Ustimenko 		break;
2014592db46SSemen Ustimenko 
2024592db46SSemen Ustimenko 	case MII_MEDIACHG:
2039526a692SSemen Ustimenko 		/*
2049526a692SSemen Ustimenko 		 * If the interface is not up, don't do anything.
2059526a692SSemen Ustimenko 		 */
2069526a692SSemen Ustimenko 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
2079526a692SSemen Ustimenko 			break;
2089526a692SSemen Ustimenko 
2094592db46SSemen Ustimenko 		/* Wake & deisolate up if necessary */
2109526a692SSemen Ustimenko 		reg = PHY_READ(sc, MII_BMCR);
2119526a692SSemen Ustimenko 		if (reg & (BMCR_ISO | BMCR_PDOWN))
2129526a692SSemen Ustimenko 			PHY_WRITE(sc, MII_BMCR, reg & ~(BMCR_ISO | BMCR_PDOWN));
2139526a692SSemen Ustimenko 
2149526a692SSemen Ustimenko 		switch (IFM_SUBTYPE(ife->ifm_media)) {
2159526a692SSemen Ustimenko 		case IFM_AUTO:
2169526a692SSemen Ustimenko 			/*
2179526a692SSemen Ustimenko 			 * If we're already in auto mode, just return.
2189526a692SSemen Ustimenko 			 */
2199526a692SSemen Ustimenko 			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
2209526a692SSemen Ustimenko 				return (0);
2219526a692SSemen Ustimenko 
222fd94424cSPoul-Henning Kamp 			(void) mii_phy_auto(sc);
2239526a692SSemen Ustimenko 			break;
2249526a692SSemen Ustimenko 
2259526a692SSemen Ustimenko 		default:
2269526a692SSemen Ustimenko 			/*
2279526a692SSemen Ustimenko 			 * BMCR data is stored in the ifmedia entry.
2289526a692SSemen Ustimenko 			 */
2299526a692SSemen Ustimenko 			PHY_WRITE(sc, MII_ANAR,
2309526a692SSemen Ustimenko 			    mii_anar(ife->ifm_media));
2319526a692SSemen Ustimenko 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
2329526a692SSemen Ustimenko 		}
2339526a692SSemen Ustimenko 		break;
2349526a692SSemen Ustimenko 
2359526a692SSemen Ustimenko 	case MII_TICK:
2369526a692SSemen Ustimenko 		/*
2379526a692SSemen Ustimenko 		 * Is the interface even up?
2389526a692SSemen Ustimenko 		 */
2399526a692SSemen Ustimenko 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
2409526a692SSemen Ustimenko 			return (0);
2419526a692SSemen Ustimenko 
2429526a692SSemen Ustimenko 		/*
243d9730b8bSJonathan Lemon 		 * Only used for autonegotiation.
2449526a692SSemen Ustimenko 		 */
245d9730b8bSJonathan Lemon 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
246d9730b8bSJonathan Lemon 			break;
247d9730b8bSJonathan Lemon 
248d9730b8bSJonathan Lemon 		/*
2494592db46SSemen Ustimenko 		 * This PHY's autonegotiation doesn't need to be kicked.
250d9730b8bSJonathan Lemon 		 */
2519526a692SSemen Ustimenko 		break;
2529526a692SSemen Ustimenko 	}
2539526a692SSemen Ustimenko 
2549526a692SSemen Ustimenko 	/* Update the media status. */
2559526a692SSemen Ustimenko 	acphy_status(sc);
2569526a692SSemen Ustimenko 
2579526a692SSemen Ustimenko 	/* Callback if something changed. */
258d9730b8bSJonathan Lemon 	mii_phy_update(sc, cmd);
2599526a692SSemen Ustimenko 	return (0);
2609526a692SSemen Ustimenko }
2619526a692SSemen Ustimenko 
262d9730b8bSJonathan Lemon static void
2639526a692SSemen Ustimenko acphy_status(sc)
2649526a692SSemen Ustimenko 	struct mii_softc *sc;
2659526a692SSemen Ustimenko {
2669526a692SSemen Ustimenko 	struct mii_data *mii = sc->mii_pdata;
2679526a692SSemen Ustimenko 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
2689526a692SSemen Ustimenko 	int bmsr, bmcr, diag;
2699526a692SSemen Ustimenko 
2709526a692SSemen Ustimenko 	mii->mii_media_status = IFM_AVALID;
2719526a692SSemen Ustimenko 	mii->mii_media_active = IFM_ETHER;
2729526a692SSemen Ustimenko 
2739526a692SSemen Ustimenko 	bmsr = PHY_READ(sc, MII_BMSR) |
2749526a692SSemen Ustimenko 	    PHY_READ(sc, MII_BMSR);
2759526a692SSemen Ustimenko 	if (bmsr & BMSR_LINK)
2769526a692SSemen Ustimenko 		mii->mii_media_status |= IFM_ACTIVE;
2779526a692SSemen Ustimenko 
2789526a692SSemen Ustimenko 	bmcr = PHY_READ(sc, MII_BMCR);
2799526a692SSemen Ustimenko 	if (bmcr & BMCR_ISO) {
2809526a692SSemen Ustimenko 		mii->mii_media_active |= IFM_NONE;
2819526a692SSemen Ustimenko 		mii->mii_media_status = 0;
2829526a692SSemen Ustimenko 		return;
2839526a692SSemen Ustimenko 	}
2849526a692SSemen Ustimenko 
2859526a692SSemen Ustimenko 	if (bmcr & BMCR_LOOP)
2869526a692SSemen Ustimenko 		mii->mii_media_active |= IFM_LOOP;
2879526a692SSemen Ustimenko 
2889526a692SSemen Ustimenko 	if (bmcr & BMCR_AUTOEN) {
2899526a692SSemen Ustimenko 		if ((bmsr & BMSR_ACOMP) == 0) {
2909526a692SSemen Ustimenko 			/* Erg, still trying, I guess... */
2919526a692SSemen Ustimenko 			mii->mii_media_active |= IFM_NONE;
2929526a692SSemen Ustimenko 			return;
2939526a692SSemen Ustimenko 		}
2944592db46SSemen Ustimenko 		diag = PHY_READ(sc, MII_ACPHY_DIAG);
2959526a692SSemen Ustimenko 		if (diag & AC_DIAG_SPEED)
2969526a692SSemen Ustimenko 			mii->mii_media_active |= IFM_100_TX;
2979526a692SSemen Ustimenko 		else
2989526a692SSemen Ustimenko 			mii->mii_media_active |= IFM_10_T;
2999526a692SSemen Ustimenko 
3009526a692SSemen Ustimenko 		if (diag & AC_DIAG_DUPLEX)
3019526a692SSemen Ustimenko 			mii->mii_media_active |= IFM_FDX;
3029526a692SSemen Ustimenko 	} else
3039526a692SSemen Ustimenko 		mii->mii_media_active = ife->ifm_media;
3049526a692SSemen Ustimenko }
3059526a692SSemen Ustimenko 
306d9730b8bSJonathan Lemon static void
3079526a692SSemen Ustimenko acphy_reset(sc)
3089526a692SSemen Ustimenko 	struct mii_softc *sc;
3099526a692SSemen Ustimenko {
3109526a692SSemen Ustimenko 
3119526a692SSemen Ustimenko 	mii_phy_reset(sc);
3129526a692SSemen Ustimenko 	PHY_WRITE(sc, MII_ACPHY_INT, 0);
3139526a692SSemen Ustimenko }
314