xref: /netbsd-src/sys/dev/mii/bmtphy.c (revision 7a9a30c5e763bb556f73924ae04973e33cc385da)
1*7a9a30c5Sthorpej /*	$NetBSD: bmtphy.c,v 1.37 2020/03/15 23:04:50 thorpej Exp $	*/
29987ee52Sthorpej 
39987ee52Sthorpej /*-
49987ee52Sthorpej  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
59987ee52Sthorpej  * All rights reserved.
69987ee52Sthorpej  *
79987ee52Sthorpej  * This code is derived from software contributed to The NetBSD Foundation
89987ee52Sthorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
99987ee52Sthorpej  * NASA Ames Research Center.
109987ee52Sthorpej  *
119987ee52Sthorpej  * Redistribution and use in source and binary forms, with or without
129987ee52Sthorpej  * modification, are permitted provided that the following conditions
139987ee52Sthorpej  * are met:
149987ee52Sthorpej  * 1. Redistributions of source code must retain the above copyright
159987ee52Sthorpej  *    notice, this list of conditions and the following disclaimer.
169987ee52Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
179987ee52Sthorpej  *    notice, this list of conditions and the following disclaimer in the
189987ee52Sthorpej  *    documentation and/or other materials provided with the distribution.
199987ee52Sthorpej  *
209987ee52Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
219987ee52Sthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
229987ee52Sthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
239987ee52Sthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
249987ee52Sthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
259987ee52Sthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
269987ee52Sthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
279987ee52Sthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
289987ee52Sthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
299987ee52Sthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
309987ee52Sthorpej  * POSSIBILITY OF SUCH DAMAGE.
319987ee52Sthorpej  */
329987ee52Sthorpej 
339987ee52Sthorpej /*
349987ee52Sthorpej  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
359987ee52Sthorpej  *
369987ee52Sthorpej  * Redistribution and use in source and binary forms, with or without
379987ee52Sthorpej  * modification, are permitted provided that the following conditions
389987ee52Sthorpej  * are met:
399987ee52Sthorpej  * 1. Redistributions of source code must retain the above copyright
409987ee52Sthorpej  *    notice, this list of conditions and the following disclaimer.
419987ee52Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
429987ee52Sthorpej  *    notice, this list of conditions and the following disclaimer in the
439987ee52Sthorpej  *    documentation and/or other materials provided with the distribution.
449987ee52Sthorpej  *
459987ee52Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
469987ee52Sthorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
479987ee52Sthorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
489987ee52Sthorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
499987ee52Sthorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
509987ee52Sthorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
519987ee52Sthorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
529987ee52Sthorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
539987ee52Sthorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
549987ee52Sthorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
559987ee52Sthorpej  */
569987ee52Sthorpej 
579987ee52Sthorpej /*
589987ee52Sthorpej  * Driver for the Broadcom BCM5201/BCM5202 "Mini-Theta" PHYs.  This also
599987ee52Sthorpej  * drives the PHY on the 3Com 3c905C.  The 3c905C's PHY is described in
609987ee52Sthorpej  * the 3c905C data sheet.
619987ee52Sthorpej  */
629987ee52Sthorpej 
638b7bb912Slukem #include <sys/cdefs.h>
64*7a9a30c5Sthorpej __KERNEL_RCSID(0, "$NetBSD: bmtphy.c,v 1.37 2020/03/15 23:04:50 thorpej Exp $");
658b7bb912Slukem 
669987ee52Sthorpej #include <sys/param.h>
679987ee52Sthorpej #include <sys/systm.h>
689987ee52Sthorpej #include <sys/kernel.h>
699987ee52Sthorpej #include <sys/device.h>
709987ee52Sthorpej #include <sys/socket.h>
719987ee52Sthorpej #include <sys/errno.h>
729987ee52Sthorpej 
739987ee52Sthorpej #include <net/if.h>
749987ee52Sthorpej #include <net/if_media.h>
759987ee52Sthorpej 
769987ee52Sthorpej #include <dev/mii/mii.h>
779987ee52Sthorpej #include <dev/mii/miivar.h>
789987ee52Sthorpej #include <dev/mii/miidevs.h>
799987ee52Sthorpej 
809987ee52Sthorpej #include <dev/mii/bmtphyreg.h>
819987ee52Sthorpej 
827db0e577Sxtraeme static int	bmtphymatch(device_t, cfdata_t, void *);
837db0e577Sxtraeme static void	bmtphyattach(device_t, device_t, void *);
849987ee52Sthorpej 
857db0e577Sxtraeme CFATTACH_DECL_NEW(bmtphy, sizeof(struct mii_softc),
86c9b3657cSthorpej     bmtphymatch, bmtphyattach, mii_phy_detach, mii_phy_activate);
879987ee52Sthorpej 
881efb3da0Sthorpej static int	bmtphy_service(struct mii_softc *, struct mii_data *, int);
891efb3da0Sthorpej static void	bmtphy_status(struct mii_softc *);
90bb4a0543Sjdc static void	bmtphy_reset(struct mii_softc *);
919987ee52Sthorpej 
921efb3da0Sthorpej static const struct mii_phy_funcs bmtphy_funcs = {
93bb4a0543Sjdc 	bmtphy_service, bmtphy_status, bmtphy_reset,
949987ee52Sthorpej };
959987ee52Sthorpej 
961efb3da0Sthorpej static const struct mii_phydesc bmtphys[] = {
977b43da1bSchristos 	MII_PHY_DESC(xxBROADCOM, 3C905B),
987b43da1bSchristos 	MII_PHY_DESC(xxBROADCOM, 3C905C),
997b43da1bSchristos 	MII_PHY_DESC(xxBROADCOM, BCM5201),
1007b43da1bSchristos 	MII_PHY_DESC(xxBROADCOM, BCM5214),
1017b43da1bSchristos 	MII_PHY_DESC(xxBROADCOM, BCM5221),
1027b43da1bSchristos 	MII_PHY_DESC(xxBROADCOM, BCM5222),
1037b43da1bSchristos 	MII_PHY_DESC(xxBROADCOM, BCM4401),
1047b43da1bSchristos 	MII_PHY_END,
1059987ee52Sthorpej };
1069987ee52Sthorpej 
1071efb3da0Sthorpej static int
bmtphymatch(device_t parent,cfdata_t match,void * aux)1087db0e577Sxtraeme bmtphymatch(device_t parent, cfdata_t match, void *aux)
1099987ee52Sthorpej {
1109987ee52Sthorpej 	struct mii_attach_args *ma = aux;
1119987ee52Sthorpej 
1129987ee52Sthorpej 	if (mii_phy_match(ma, bmtphys) != NULL)
1138e65e831Smsaitoh 		return 10;
1149987ee52Sthorpej 
1158e65e831Smsaitoh 	return 0;
1169987ee52Sthorpej }
1179987ee52Sthorpej 
1181efb3da0Sthorpej static void
bmtphyattach(device_t parent,device_t self,void * aux)1197db0e577Sxtraeme bmtphyattach(device_t parent, device_t self, void *aux)
1209987ee52Sthorpej {
121838ee1e0Sthorpej 	struct mii_softc *sc = device_private(self);
1229987ee52Sthorpej 	struct mii_attach_args *ma = aux;
1239987ee52Sthorpej 	struct mii_data *mii = ma->mii_data;
1249987ee52Sthorpej 	const struct mii_phydesc *mpd;
1259987ee52Sthorpej 
1269987ee52Sthorpej 	mpd = mii_phy_match(ma, bmtphys);
127c31f87a5Sthorpej 	aprint_naive(": Media interface\n");
128c31f87a5Sthorpej 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
1299987ee52Sthorpej 
1307db0e577Sxtraeme 	sc->mii_dev = self;
131bb4a0543Sjdc 	sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
1329987ee52Sthorpej 	sc->mii_inst = mii->mii_instance;
1339987ee52Sthorpej 	sc->mii_phy = ma->mii_phyno;
1349987ee52Sthorpej 	sc->mii_funcs = &bmtphy_funcs;
1359987ee52Sthorpej 	sc->mii_pdata = mii;
136b0178985Sthorpej 	sc->mii_flags = ma->mii_flags;
1379987ee52Sthorpej 
138*7a9a30c5Sthorpej 	mii_lock(mii);
139*7a9a30c5Sthorpej 
1409987ee52Sthorpej 	PHY_RESET(sc);
1419987ee52Sthorpej 
1428e65e831Smsaitoh 	/* XXX Check AUX_STS_FX_MODE to set MIIF_HAVE_FIBER? */
1439987ee52Sthorpej 
144a5cdd4b4Smsaitoh 	PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
145a5cdd4b4Smsaitoh 	sc->mii_capabilities &= ma->mii_capmask;
146509697f3Smsaitoh 
147*7a9a30c5Sthorpej 	mii_unlock(mii);
148*7a9a30c5Sthorpej 
1499987ee52Sthorpej 	mii_phy_add_media(sc);
1509987ee52Sthorpej }
1519987ee52Sthorpej 
1521efb3da0Sthorpej static int
bmtphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)15389893e42Sthorpej bmtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
1549987ee52Sthorpej {
1559987ee52Sthorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
156a5cdd4b4Smsaitoh 	uint16_t reg;
1579987ee52Sthorpej 
158*7a9a30c5Sthorpej 	KASSERT(mii_locked(mii));
159*7a9a30c5Sthorpej 
1609987ee52Sthorpej 	switch (cmd) {
1619987ee52Sthorpej 	case MII_POLLSTAT:
1628e65e831Smsaitoh 		/* If we're not polling our PHY instance, just return. */
1639987ee52Sthorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
1648e65e831Smsaitoh 			return 0;
1659987ee52Sthorpej 		break;
1669987ee52Sthorpej 
1679987ee52Sthorpej 	case MII_MEDIACHG:
1689987ee52Sthorpej 		/*
1699987ee52Sthorpej 		 * If the media indicates a different PHY instance,
1709987ee52Sthorpej 		 * isolate ourselves.
1719987ee52Sthorpej 		 */
1729987ee52Sthorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
173a5cdd4b4Smsaitoh 			PHY_READ(sc, MII_BMCR, &reg);
1749987ee52Sthorpej 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
1758e65e831Smsaitoh 			return 0;
1769987ee52Sthorpej 		}
1779987ee52Sthorpej 
1788e65e831Smsaitoh 		/* If the interface is not up, don't do anything. */
1799987ee52Sthorpej 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
1809987ee52Sthorpej 			break;
1819987ee52Sthorpej 
1829987ee52Sthorpej 		mii_phy_setmedia(sc);
1839987ee52Sthorpej 		break;
1849987ee52Sthorpej 
1859987ee52Sthorpej 	case MII_TICK:
1868e65e831Smsaitoh 		/* If we're not currently selected, just return. */
1879987ee52Sthorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
1888e65e831Smsaitoh 			return 0;
1899987ee52Sthorpej 
1909987ee52Sthorpej 		if (mii_phy_tick(sc) == EJUSTRETURN)
1918e65e831Smsaitoh 			return 0;
1929987ee52Sthorpej 		break;
1939987ee52Sthorpej 
1949987ee52Sthorpej 	case MII_DOWN:
1959987ee52Sthorpej 		mii_phy_down(sc);
1968e65e831Smsaitoh 		return 0;
1979987ee52Sthorpej 	}
1989987ee52Sthorpej 
1999987ee52Sthorpej 	/* Update the media status. */
2009987ee52Sthorpej 	mii_phy_status(sc);
2019987ee52Sthorpej 
2029987ee52Sthorpej 	/* Callback if something changed. */
2039987ee52Sthorpej 	mii_phy_update(sc, cmd);
2048e65e831Smsaitoh 	return 0;
2059987ee52Sthorpej }
2069987ee52Sthorpej 
2071efb3da0Sthorpej static void
bmtphy_status(struct mii_softc * sc)20889893e42Sthorpej bmtphy_status(struct mii_softc *sc)
2099987ee52Sthorpej {
2109987ee52Sthorpej 	struct mii_data *mii = sc->mii_pdata;
2119987ee52Sthorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
212a5cdd4b4Smsaitoh 	uint16_t bmsr, bmcr, aux_csr;
2139987ee52Sthorpej 
214*7a9a30c5Sthorpej 	KASSERT(mii_locked(mii));
215*7a9a30c5Sthorpej 
2169987ee52Sthorpej 	mii->mii_media_status = IFM_AVALID;
2179987ee52Sthorpej 	mii->mii_media_active = IFM_ETHER;
2189987ee52Sthorpej 
219a5cdd4b4Smsaitoh 	PHY_READ(sc, MII_BMSR, &bmsr);
220a5cdd4b4Smsaitoh 	PHY_READ(sc, MII_BMSR, &bmsr);
2219987ee52Sthorpej 
2229987ee52Sthorpej 	if (bmsr & BMSR_LINK)
2239987ee52Sthorpej 		mii->mii_media_status |= IFM_ACTIVE;
2249987ee52Sthorpej 
225a5cdd4b4Smsaitoh 	PHY_READ(sc, MII_BMCR, &bmcr);
2269987ee52Sthorpej 	if (bmcr & BMCR_ISO) {
2279987ee52Sthorpej 		mii->mii_media_active |= IFM_NONE;
2289987ee52Sthorpej 		mii->mii_media_status = 0;
2299987ee52Sthorpej 		return;
2309987ee52Sthorpej 	}
2319987ee52Sthorpej 
2329987ee52Sthorpej 	if (bmcr & BMCR_LOOP)
2339987ee52Sthorpej 		mii->mii_media_active |= IFM_LOOP;
2349987ee52Sthorpej 
2359987ee52Sthorpej 	if (bmcr & BMCR_AUTOEN) {
2369987ee52Sthorpej 		/*
2370be714dcSmsaitoh 		 * The media status bits are only valid if autonegotiation
2389987ee52Sthorpej 		 * has completed (or it's disabled).
2399987ee52Sthorpej 		 */
2409987ee52Sthorpej 		if ((bmsr & BMSR_ACOMP) == 0) {
2419987ee52Sthorpej 			/* Erg, still trying, I guess... */
2429987ee52Sthorpej 			mii->mii_media_active |= IFM_NONE;
2439987ee52Sthorpej 			return;
2449987ee52Sthorpej 		}
2459987ee52Sthorpej 
246a5cdd4b4Smsaitoh 		PHY_READ(sc, MII_BMTPHY_AUX_CSR, &aux_csr);
2479987ee52Sthorpej 		if (aux_csr & AUX_CSR_SPEED)
2489987ee52Sthorpej 			mii->mii_media_active |= IFM_100_TX;
2499987ee52Sthorpej 		else
2509987ee52Sthorpej 			mii->mii_media_active |= IFM_10_T;
2519987ee52Sthorpej 		if (aux_csr & AUX_CSR_FDX)
2529987ee52Sthorpej 			mii->mii_media_active |= IFM_FDX;
253bb4a0543Sjdc 		else
254bb4a0543Sjdc 			mii->mii_media_active |= IFM_HDX;
255dff35a24Smrg 
256dff35a24Smrg 		if (mii->mii_media_active & IFM_FDX)
257dff35a24Smrg 			mii->mii_media_active |= mii_phy_flowstatus(sc);
2589987ee52Sthorpej 	} else
2599987ee52Sthorpej 		mii->mii_media_active = ife->ifm_media;
2609987ee52Sthorpej }
261bb4a0543Sjdc 
262bb4a0543Sjdc static void
bmtphy_reset(struct mii_softc * sc)263bb4a0543Sjdc bmtphy_reset(struct mii_softc *sc)
264bb4a0543Sjdc {
265a5cdd4b4Smsaitoh 	uint16_t data;
266bb4a0543Sjdc 
267*7a9a30c5Sthorpej 	KASSERT(mii_locked(sc->mii_pdata));
268*7a9a30c5Sthorpej 
269bb4a0543Sjdc 	mii_phy_reset(sc);
270bb4a0543Sjdc 
271bb4a0543Sjdc 	if (sc->mii_mpd_model == MII_MODEL_xxBROADCOM_BCM5221) {
272bb4a0543Sjdc 		/* Enable shadow register mode */
273a5cdd4b4Smsaitoh 		PHY_READ(sc, 0x1f, &data);
274bb4a0543Sjdc 		PHY_WRITE(sc, 0x1f, data | 0x0080);
275bb4a0543Sjdc 
276bb4a0543Sjdc 		/* Enable APD (Auto PowerDetect) */
277a5cdd4b4Smsaitoh 		PHY_READ(sc, MII_BMTPHY_AUX2, &data);
278bb4a0543Sjdc 		PHY_WRITE(sc, MII_BMTPHY_AUX2, data | 0x0020);
279bb4a0543Sjdc 
2800be714dcSmsaitoh 		/* Enable clocks across APD for Auto-MDIX functionality */
281a5cdd4b4Smsaitoh 		PHY_READ(sc, MII_BMTPHY_INTR, &data);
282bb4a0543Sjdc 		PHY_WRITE(sc, MII_BMTPHY_INTR, data | 0x0004);
283bb4a0543Sjdc 
284bb4a0543Sjdc 		/* Disable shadow register mode */
285a5cdd4b4Smsaitoh 		PHY_READ(sc, 0x1f, &data);
286bb4a0543Sjdc 		PHY_WRITE(sc, 0x1f, data & ~0x0080);
287bb4a0543Sjdc 	}
288bb4a0543Sjdc }
289