Home | History | Annotate | Line # | Download | only in mii
ihphy.c revision 1.11
      1  1.11   msaitoh /*	$NetBSD: ihphy.c,v 1.11 2019/01/22 03:42:27 msaitoh Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*-
      4   1.1  christos  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
      5   1.1  christos  * All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  christos  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1  christos  * NASA Ames Research Center, and by Frank van der Linden.
     10   1.1  christos  *
     11   1.1  christos  * Redistribution and use in source and binary forms, with or without
     12   1.1  christos  * modification, are permitted provided that the following conditions
     13   1.1  christos  * are met:
     14   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     16   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  christos  *    documentation and/or other materials provided with the distribution.
     19   1.1  christos  *
     20   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1  christos  */
     32   1.1  christos 
     33   1.1  christos /*
     34   1.1  christos  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     35   1.1  christos  *
     36   1.1  christos  * Redistribution and use in source and binary forms, with or without
     37   1.1  christos  * modification, are permitted provided that the following conditions
     38   1.1  christos  * are met:
     39   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     40   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     41   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     43   1.1  christos  *    documentation and/or other materials provided with the distribution.
     44   1.1  christos  *
     45   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     46   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     47   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     48   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     49   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     50   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     51   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     52   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     53   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     54   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     55   1.1  christos  */
     56   1.1  christos 
     57   1.1  christos /*
     58   1.1  christos  * Driver for Intel's 82577 (Hanksville) Ethernet 10/100/1000 PHY
     59   1.1  christos  * Data Sheet: http://download.intel.com/design/network/datashts/319439.pdf
     60   1.1  christos  */
     61   1.1  christos 
     62   1.1  christos #include <sys/cdefs.h>
     63  1.11   msaitoh __KERNEL_RCSID(0, "$NetBSD: ihphy.c,v 1.11 2019/01/22 03:42:27 msaitoh Exp $");
     64   1.1  christos 
     65   1.1  christos #include <sys/param.h>
     66   1.1  christos #include <sys/systm.h>
     67   1.1  christos #include <sys/kernel.h>
     68   1.1  christos #include <sys/device.h>
     69   1.1  christos #include <sys/socket.h>
     70   1.1  christos #include <sys/errno.h>
     71   1.1  christos 
     72   1.1  christos #include <net/if.h>
     73   1.1  christos #include <net/if_media.h>
     74   1.1  christos 
     75   1.1  christos #include <dev/mii/mii.h>
     76   1.1  christos #include <dev/mii/miivar.h>
     77   1.1  christos #include <dev/mii/miidevs.h>
     78   1.1  christos 
     79   1.1  christos #include <dev/mii/ihphyreg.h>
     80   1.1  christos 
     81   1.1  christos static int	ihphymatch(device_t, cfdata_t, void *);
     82   1.1  christos static void	ihphyattach(device_t, device_t, void *);
     83   1.1  christos 
     84   1.1  christos CFATTACH_DECL3_NEW(ihphy, sizeof(struct mii_softc),
     85   1.1  christos     ihphymatch, ihphyattach, mii_phy_detach, mii_phy_activate, NULL, NULL,
     86   1.1  christos     DVF_DETACH_SHUTDOWN);
     87   1.1  christos 
     88   1.1  christos static int	ihphy_service(struct mii_softc *, struct mii_data *, int);
     89   1.1  christos static void	ihphy_status(struct mii_softc *);
     90   1.1  christos static void	ihphy_reset(struct mii_softc *);
     91   1.1  christos 
     92   1.1  christos static const struct mii_phy_funcs ihphy_funcs = {
     93   1.1  christos 	ihphy_service, ihphy_status, ihphy_reset,
     94   1.1  christos };
     95   1.1  christos 
     96   1.1  christos static const struct mii_phydesc ihphys[] = {
     97   1.1  christos 	{ MII_OUI_INTEL,		MII_MODEL_INTEL_I82577,
     98   1.1  christos 	  MII_STR_INTEL_I82577 },
     99   1.2   msaitoh 	{ MII_OUI_INTEL,		MII_MODEL_INTEL_I82579,
    100   1.2   msaitoh 	  MII_STR_INTEL_I82579 },
    101   1.7   msaitoh 	{ MII_OUI_INTEL,		MII_MODEL_INTEL_I217,
    102   1.7   msaitoh 	  MII_STR_INTEL_I217 },
    103   1.1  christos 
    104   1.1  christos 	{ 0,				0,
    105   1.1  christos 	  NULL },
    106   1.1  christos };
    107   1.1  christos 
    108   1.1  christos static int
    109   1.1  christos ihphymatch(device_t parent, cfdata_t match, void *aux)
    110   1.1  christos {
    111   1.1  christos 	struct mii_attach_args *ma = aux;
    112   1.1  christos 
    113   1.1  christos 	if (mii_phy_match(ma, ihphys) != NULL)
    114   1.1  christos 		return 10;
    115   1.1  christos 
    116   1.1  christos 	return 0;
    117   1.1  christos }
    118   1.1  christos 
    119   1.1  christos static void
    120   1.1  christos ihphyattach(device_t parent, device_t self, void *aux)
    121   1.1  christos {
    122   1.1  christos 	struct mii_softc *sc = device_private(self);
    123   1.1  christos 	struct mii_attach_args *ma = aux;
    124   1.1  christos 	struct mii_data *mii = ma->mii_data;
    125   1.1  christos 	const struct mii_phydesc *mpd;
    126  1.11   msaitoh 	uint16_t reg;
    127   1.1  christos 
    128   1.1  christos 	mpd = mii_phy_match(ma, ihphys);
    129   1.1  christos 	aprint_naive(": Media interface\n");
    130   1.1  christos 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
    131   1.1  christos 
    132   1.1  christos 	sc->mii_dev = self;
    133   1.1  christos 	sc->mii_inst = mii->mii_instance;
    134   1.1  christos 	sc->mii_phy = ma->mii_phyno;
    135  1.10   msaitoh 	sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
    136  1.10   msaitoh 	sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
    137  1.10   msaitoh 	sc->mii_mpd_rev = MII_REV(ma->mii_id2);
    138   1.1  christos 	sc->mii_funcs = &ihphy_funcs;
    139   1.1  christos 	sc->mii_pdata = mii;
    140   1.1  christos 	sc->mii_flags = ma->mii_flags;
    141   1.1  christos 	sc->mii_anegticks = MII_ANEGTICKS;
    142   1.1  christos 
    143   1.1  christos 	PHY_RESET(sc);
    144   1.1  christos 
    145  1.11   msaitoh 	PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
    146  1.11   msaitoh 	sc->mii_capabilities &= ma->mii_capmask;
    147   1.1  christos 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    148  1.11   msaitoh 		PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
    149   1.1  christos 	aprint_normal_dev(self, "");
    150   1.1  christos 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
    151   1.1  christos 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
    152   1.1  christos 		aprint_error("no media present");
    153   1.1  christos 	else
    154   1.1  christos 		mii_phy_add_media(sc);
    155   1.1  christos 	aprint_normal("\n");
    156   1.1  christos 
    157   1.1  christos 	/*
    158   1.1  christos 	 * Link setup (as done by Intel's Linux driver for the 82577).
    159   1.1  christos 	 */
    160  1.11   msaitoh 	PHY_READ(sc, IHPHY_MII_CFG, &reg);
    161   1.1  christos 	reg |= IHPHY_CFG_TX_CRS;
    162   1.1  christos 	reg |= IHPHY_CFG_DOWN_SHIFT;
    163   1.1  christos 	PHY_WRITE(sc, IHPHY_MII_CFG, reg);
    164   1.1  christos }
    165   1.1  christos 
    166   1.1  christos static int
    167   1.1  christos ihphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    168   1.1  christos {
    169   1.1  christos 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    170  1.11   msaitoh 	uint16_t reg;
    171   1.1  christos 
    172   1.1  christos 	switch (cmd) {
    173   1.1  christos 	case MII_POLLSTAT:
    174   1.1  christos 		/*
    175   1.1  christos 		 * If we're not polling our PHY instance, just return.
    176   1.1  christos 		 */
    177   1.1  christos 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    178   1.1  christos 			return 0;
    179   1.1  christos 		break;
    180   1.1  christos 
    181   1.1  christos 	case MII_MEDIACHG:
    182   1.1  christos 		/*
    183   1.1  christos 		 * If the media indicates a different PHY instance,
    184   1.1  christos 		 * isolate ourselves.
    185   1.1  christos 		 */
    186   1.1  christos 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    187  1.11   msaitoh 			PHY_READ(sc, MII_BMCR, &reg);
    188   1.1  christos 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    189   1.1  christos 			return 0;
    190   1.1  christos 		}
    191   1.1  christos 
    192   1.1  christos 		/*
    193   1.1  christos 		 * If the interface is not up, don't do anything.
    194   1.1  christos 		 */
    195   1.1  christos 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    196   1.1  christos 			break;
    197   1.1  christos 
    198   1.1  christos 		/*
    199   1.1  christos 		 * If media is deselected, disable link (standby).
    200   1.1  christos 		 */
    201  1.11   msaitoh 		PHY_READ(sc, IHPHY_MII_ECR, &reg);
    202   1.1  christos 		if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE)
    203   1.1  christos 			reg &= ~IHPHY_ECR_LNK_EN;
    204   1.1  christos 		else
    205   1.1  christos 			reg |= IHPHY_ECR_LNK_EN;
    206   1.1  christos 		PHY_WRITE(sc, IHPHY_MII_ECR, reg);
    207   1.1  christos 
    208   1.1  christos 		/*
    209   1.1  christos 		 * XXX Adjust MDI/MDIX configuration?  Other settings?
    210   1.1  christos 		 */
    211   1.1  christos 
    212   1.1  christos 		mii_phy_setmedia(sc);
    213   1.1  christos 		break;
    214   1.1  christos 
    215   1.1  christos 	case MII_TICK:
    216   1.1  christos 		/*
    217   1.1  christos 		 * If we're not currently selected, just return.
    218   1.1  christos 		 */
    219   1.1  christos 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    220   1.1  christos 			return 0;
    221   1.1  christos 
    222   1.1  christos 		if (mii_phy_tick(sc) == EJUSTRETURN)
    223   1.1  christos 			return 0;
    224   1.1  christos 		break;
    225   1.1  christos 
    226   1.1  christos 	case MII_DOWN:
    227   1.1  christos 		mii_phy_down(sc);
    228   1.1  christos 		PHY_WRITE(sc, MII_BMCR, BMCR_PDOWN);
    229   1.1  christos 		return 0;
    230   1.1  christos 	}
    231   1.1  christos 
    232   1.1  christos 	/* Update the media status. */
    233   1.1  christos 	mii_phy_status(sc);
    234   1.1  christos 
    235   1.1  christos 	/* Callback if something changed. */
    236   1.1  christos 	mii_phy_update(sc, cmd);
    237   1.1  christos 	return 0;
    238   1.1  christos }
    239   1.1  christos 
    240   1.1  christos static void
    241   1.1  christos ihphy_status(struct mii_softc *sc)
    242   1.1  christos {
    243   1.1  christos 	struct mii_data *mii = sc->mii_pdata;
    244  1.11   msaitoh 	uint16_t esr, bmcr, gtsr;
    245   1.1  christos 
    246   1.1  christos 	mii->mii_media_status = IFM_AVALID;
    247   1.1  christos 	mii->mii_media_active = IFM_ETHER;
    248   1.1  christos 
    249  1.11   msaitoh 	PHY_READ(sc, IHPHY_MII_ESR, &esr);
    250   1.1  christos 
    251   1.1  christos 	if (esr & IHPHY_ESR_LINK)
    252   1.1  christos 		mii->mii_media_status |= IFM_ACTIVE;
    253   1.1  christos 
    254  1.11   msaitoh 	PHY_READ(sc, MII_BMCR, &bmcr);
    255   1.1  christos 	if (bmcr & (BMCR_ISO | BMCR_PDOWN)) {
    256   1.1  christos 		mii->mii_media_active |= IFM_NONE;
    257   1.1  christos 		mii->mii_media_status = 0;
    258   1.1  christos 		return;
    259   1.1  christos 	}
    260   1.1  christos 
    261   1.1  christos 	if (bmcr & BMCR_LOOP)
    262   1.1  christos 		mii->mii_media_active |= IFM_LOOP;
    263   1.1  christos 
    264   1.1  christos 	if (bmcr & BMCR_AUTOEN) {
    265   1.1  christos 		if ((esr & IHPHY_ESR_ANEG_STAT) == 0) {
    266   1.1  christos 			/* Erg, still trying, I guess... */
    267   1.1  christos 			mii->mii_media_active |= IFM_NONE;
    268   1.1  christos 			return;
    269   1.1  christos 		}
    270   1.1  christos 	}
    271   1.1  christos 
    272   1.1  christos 	switch (esr & IHPHY_ESR_SPEED) {
    273   1.1  christos 	case IHPHY_SPEED_1000:
    274   1.1  christos 		mii->mii_media_active |= IFM_1000_T;
    275  1.11   msaitoh 		PHY_READ(sc, MII_100T2SR, &gtsr);
    276   1.1  christos 		if (gtsr & GTSR_MS_RES)
    277   1.1  christos 			mii->mii_media_active |= IFM_ETH_MASTER;
    278   1.1  christos 		break;
    279   1.1  christos 
    280   1.1  christos 	case IHPHY_SPEED_100:
    281   1.1  christos 		/* 100BASE-T2 and 100BASE-T4 are not supported. */
    282   1.1  christos 		mii->mii_media_active |= IFM_100_TX;
    283   1.1  christos 		break;
    284   1.1  christos 
    285   1.1  christos 	case IHPHY_SPEED_10:
    286   1.1  christos 		mii->mii_media_active |= IFM_10_T;
    287   1.1  christos 		break;
    288   1.1  christos 
    289   1.1  christos 	default:
    290   1.1  christos 		mii->mii_media_active |= IFM_NONE;
    291   1.1  christos 		mii->mii_media_status = 0;
    292   1.1  christos 		return;
    293   1.1  christos 	}
    294   1.1  christos 
    295   1.1  christos 	if (esr & IHPHY_ESR_DUPLEX)
    296   1.8   msaitoh 		mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
    297   1.8   msaitoh 	else
    298   1.8   msaitoh 		mii->mii_media_active |= IFM_HDX;
    299   1.1  christos }
    300   1.1  christos 
    301   1.1  christos static void
    302   1.1  christos ihphy_reset(struct mii_softc *sc)
    303   1.1  christos {
    304  1.11   msaitoh 	int i;
    305  1.11   msaitoh 	uint16_t reg;
    306   1.1  christos 
    307   1.1  christos 	PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_ISO);
    308   1.1  christos 
    309   1.1  christos 	/*
    310   1.1  christos 	 * Regarding reset, the data sheet specifies (page 55):
    311   1.1  christos 	 *
    312   1.1  christos 	 * "After PHY reset, a delay of 10 ms is required before
    313   1.1  christos 	 *  any register access using MDIO."
    314   1.1  christos 	 */
    315   1.1  christos 	delay(10000);
    316   1.1  christos 
    317   1.1  christos 	/* Wait another 100ms for it to complete. */
    318   1.1  christos 	for (i = 0; i < 100; i++) {
    319  1.11   msaitoh 		PHY_READ(sc, MII_BMCR, &reg);
    320   1.1  christos 		if ((reg & BMCR_RESET) == 0)
    321   1.1  christos 			break;
    322   1.1  christos 		delay(1000);
    323   1.1  christos 	}
    324   1.1  christos 
    325   1.1  christos 	if (sc->mii_inst != 0)
    326   1.1  christos 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    327   1.1  christos }
    328