Home | History | Annotate | Line # | Download | only in mii
jmphy.c revision 1.2
      1  1.2  msaitoh /*	$NetBSD: jmphy.c,v 1.2 2019/11/26 08:19:51 msaitoh Exp $ */
      2  1.1  msaitoh /*	$OpenBSD: jmphy.c,v 1.6 2015/03/14 03:38:48 jsg Exp $	*/
      3  1.1  msaitoh /*-
      4  1.1  msaitoh  * Copyright (c) 2008, Pyun YongHyeon <yongari (at) FreeBSD.org>
      5  1.1  msaitoh  * All rights reserved.
      6  1.1  msaitoh  *
      7  1.1  msaitoh  * Redistribution and use in source and binary forms, with or without
      8  1.1  msaitoh  * modification, are permitted provided that the following conditions
      9  1.1  msaitoh  * are met:
     10  1.1  msaitoh  * 1. Redistributions of source code must retain the above copyright
     11  1.1  msaitoh  *    notice unmodified, this list of conditions, and the following
     12  1.1  msaitoh  *    disclaimer.
     13  1.1  msaitoh  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  msaitoh  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  msaitoh  *    documentation and/or other materials provided with the distribution.
     16  1.1  msaitoh  *
     17  1.1  msaitoh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  1.1  msaitoh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  1.1  msaitoh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  1.1  msaitoh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  1.1  msaitoh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1  msaitoh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  1.1  msaitoh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.1  msaitoh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.1  msaitoh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.1  msaitoh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.1  msaitoh  * SUCH DAMAGE.
     28  1.1  msaitoh  *
     29  1.1  msaitoh  * $FreeBSD: src/sys/dev/mii/jmphy.c,v 1.1 2008/05/27 01:16:40 yongari Exp $
     30  1.1  msaitoh  * $DragonFly: src/sys/dev/netif/mii_layer/jmphy.c,v 1.1 2008/07/22 11:28:49 sephe Exp $
     31  1.1  msaitoh  */
     32  1.1  msaitoh 
     33  1.1  msaitoh /*
     34  1.1  msaitoh  * Driver for the JMicron JMP211 10/100/1000, JMP202 10/100 PHY.
     35  1.1  msaitoh  */
     36  1.1  msaitoh 
     37  1.1  msaitoh #include <sys/param.h>
     38  1.1  msaitoh #include <sys/systm.h>
     39  1.1  msaitoh #include <sys/device.h>
     40  1.1  msaitoh #include <sys/socket.h>
     41  1.1  msaitoh 
     42  1.1  msaitoh #include <net/if.h>
     43  1.1  msaitoh #include <net/if_media.h>
     44  1.1  msaitoh 
     45  1.1  msaitoh #include <dev/mii/mii.h>
     46  1.1  msaitoh #include <dev/mii/miivar.h>
     47  1.1  msaitoh #include <dev/mii/miidevs.h>
     48  1.1  msaitoh #include <dev/mii/jmphyreg.h>
     49  1.1  msaitoh 
     50  1.1  msaitoh static int	jmphy_service(struct mii_softc *, struct mii_data *, int);
     51  1.1  msaitoh static void	jmphy_status(struct mii_softc *);
     52  1.1  msaitoh static int	jmphy_match(device_t, cfdata_t, void *);
     53  1.1  msaitoh static void	jmphy_attach(device_t, device_t, void *);
     54  1.1  msaitoh static void	jmphy_reset(struct mii_softc *);
     55  1.1  msaitoh static uint16_t	jmphy_anar(struct ifmedia_entry *);
     56  1.1  msaitoh static int	jmphy_auto(struct mii_softc *, struct ifmedia_entry *);
     57  1.1  msaitoh 
     58  1.1  msaitoh static const struct mii_phy_funcs jmphy_funcs = {
     59  1.1  msaitoh 	jmphy_service, jmphy_status, jmphy_reset,
     60  1.1  msaitoh };
     61  1.1  msaitoh 
     62  1.1  msaitoh CFATTACH_DECL_NEW(jmphy, sizeof (struct mii_softc),
     63  1.1  msaitoh     jmphy_match, jmphy_attach, mii_phy_detach, mii_phy_activate);
     64  1.1  msaitoh 
     65  1.1  msaitoh static const struct mii_phydesc jmphys[] = {
     66  1.1  msaitoh 	MII_PHY_DESC(JMICRON, JMP202),
     67  1.1  msaitoh 	MII_PHY_DESC(JMICRON, JMP211),
     68  1.1  msaitoh 	MII_PHY_END,
     69  1.1  msaitoh };
     70  1.1  msaitoh 
     71  1.1  msaitoh static int
     72  1.1  msaitoh jmphy_match(device_t parent, cfdata_t match, void *aux)
     73  1.1  msaitoh {
     74  1.1  msaitoh 	struct mii_attach_args *ma = aux;
     75  1.1  msaitoh 
     76  1.1  msaitoh 	if (mii_phy_match(ma, jmphys) != NULL)
     77  1.1  msaitoh 		return 10;
     78  1.1  msaitoh 
     79  1.1  msaitoh 	return 0;
     80  1.1  msaitoh }
     81  1.1  msaitoh 
     82  1.1  msaitoh static void
     83  1.1  msaitoh jmphy_attach(device_t parent, device_t self, void *aux)
     84  1.1  msaitoh {
     85  1.1  msaitoh 	struct mii_softc *sc = device_private(self);
     86  1.1  msaitoh 	struct mii_attach_args *ma = aux;
     87  1.1  msaitoh 	struct mii_data *mii = ma->mii_data;
     88  1.1  msaitoh 	const struct mii_phydesc *mpd;
     89  1.1  msaitoh 
     90  1.1  msaitoh 	mpd = mii_phy_match(ma, jmphys);
     91  1.1  msaitoh 	aprint_naive(": Media interface\n");
     92  1.1  msaitoh 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
     93  1.1  msaitoh 
     94  1.1  msaitoh 	sc->mii_dev = self;
     95  1.1  msaitoh 	sc->mii_inst = mii->mii_instance;
     96  1.1  msaitoh 	sc->mii_phy = ma->mii_phyno;
     97  1.1  msaitoh 	sc->mii_funcs = &jmphy_funcs;
     98  1.1  msaitoh 	sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
     99  1.1  msaitoh 	sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
    100  1.1  msaitoh 	sc->mii_mpd_rev = MII_REV(ma->mii_id2);
    101  1.1  msaitoh 	sc->mii_pdata = mii;
    102  1.1  msaitoh 	sc->mii_flags = ma->mii_flags;
    103  1.1  msaitoh 
    104  1.1  msaitoh 	sc->mii_flags |= MIIF_NOISOLATE | MIIF_NOLOOP;
    105  1.1  msaitoh 
    106  1.1  msaitoh 	PHY_RESET(sc);
    107  1.1  msaitoh 
    108  1.1  msaitoh 	PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
    109  1.1  msaitoh 	sc->mii_capabilities &= ma->mii_capmask;
    110  1.1  msaitoh 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    111  1.1  msaitoh 		PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
    112  1.1  msaitoh 
    113  1.1  msaitoh 	aprint_normal_dev(self, "");
    114  1.1  msaitoh 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
    115  1.1  msaitoh 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
    116  1.1  msaitoh 		aprint_error("no media present");
    117  1.1  msaitoh 	else
    118  1.1  msaitoh 		mii_phy_add_media(sc);
    119  1.1  msaitoh 	aprint_normal("\n");
    120  1.1  msaitoh }
    121  1.1  msaitoh 
    122  1.1  msaitoh static int
    123  1.1  msaitoh jmphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    124  1.1  msaitoh {
    125  1.1  msaitoh 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    126  1.1  msaitoh 	uint16_t bmcr, ssr;
    127  1.1  msaitoh 
    128  1.1  msaitoh 	switch (cmd) {
    129  1.1  msaitoh 	case MII_POLLSTAT:
    130  1.2  msaitoh 		/* If we're not polling our PHY instance, just return. */
    131  1.1  msaitoh 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    132  1.1  msaitoh 			return 0;
    133  1.1  msaitoh 		break;
    134  1.1  msaitoh 
    135  1.1  msaitoh 	case MII_MEDIACHG:
    136  1.1  msaitoh 		/*
    137  1.1  msaitoh 		 * If the media indicates a different PHY instance,
    138  1.1  msaitoh 		 * isolate ourselves.
    139  1.1  msaitoh 		 */
    140  1.1  msaitoh 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    141  1.1  msaitoh 			PHY_READ(sc, MII_BMCR, &bmcr);
    142  1.1  msaitoh 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
    143  1.1  msaitoh 			return 0;
    144  1.1  msaitoh 		}
    145  1.1  msaitoh 
    146  1.2  msaitoh 		/* If the interface is not up, don't do anything. */
    147  1.1  msaitoh 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    148  1.1  msaitoh 			break;
    149  1.1  msaitoh 
    150  1.1  msaitoh 		if (jmphy_auto(sc, ife) != EJUSTRETURN)
    151  1.1  msaitoh 			return EINVAL;
    152  1.1  msaitoh 		break;
    153  1.1  msaitoh 
    154  1.1  msaitoh 	case MII_TICK:
    155  1.2  msaitoh 		/* If we're not currently selected, just return. */
    156  1.1  msaitoh 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    157  1.1  msaitoh 			return 0;
    158  1.1  msaitoh 
    159  1.2  msaitoh 		/* Is the interface even up? */
    160  1.1  msaitoh 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    161  1.1  msaitoh 			return 0;
    162  1.1  msaitoh 
    163  1.2  msaitoh 		/* Only used for autonegotiation. */
    164  1.1  msaitoh 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    165  1.1  msaitoh 			break;
    166  1.1  msaitoh 
    167  1.1  msaitoh 		/* Check for link. */
    168  1.1  msaitoh 		PHY_READ(sc, JMPHY_SSR, &ssr);
    169  1.1  msaitoh 		if (ssr & JMPHY_SSR_LINK_UP) {
    170  1.1  msaitoh 			sc->mii_ticks = 0;
    171  1.1  msaitoh 			break;
    172  1.1  msaitoh 		}
    173  1.1  msaitoh 
    174  1.1  msaitoh 		/* Announce link loss right after it happens. */
    175  1.1  msaitoh 		if (sc->mii_ticks++ == 0)
    176  1.1  msaitoh 			break;
    177  1.1  msaitoh 		if (sc->mii_ticks <= sc->mii_anegticks)
    178  1.1  msaitoh 			return 0;
    179  1.1  msaitoh 
    180  1.1  msaitoh 		sc->mii_ticks = 0;
    181  1.1  msaitoh 		jmphy_auto(sc, ife);
    182  1.1  msaitoh 		break;
    183  1.1  msaitoh 	}
    184  1.1  msaitoh 
    185  1.1  msaitoh 	/* Update the media status. */
    186  1.1  msaitoh 	jmphy_status(sc);
    187  1.1  msaitoh 
    188  1.1  msaitoh 	/* Callback if something changed. */
    189  1.1  msaitoh 	mii_phy_update(sc, cmd);
    190  1.1  msaitoh 	return 0;
    191  1.1  msaitoh }
    192  1.1  msaitoh 
    193  1.1  msaitoh static void
    194  1.1  msaitoh jmphy_status(struct mii_softc *sc)
    195  1.1  msaitoh {
    196  1.1  msaitoh 	struct mii_data *mii = sc->mii_pdata;
    197  1.1  msaitoh 	uint16_t bmcr, ssr, gtsr;
    198  1.1  msaitoh 
    199  1.1  msaitoh 	mii->mii_media_status = IFM_AVALID;
    200  1.1  msaitoh 	mii->mii_media_active = IFM_ETHER;
    201  1.1  msaitoh 
    202  1.1  msaitoh 	PHY_READ(sc, JMPHY_SSR, &ssr);
    203  1.1  msaitoh 	if ((ssr & JMPHY_SSR_LINK_UP) != 0)
    204  1.1  msaitoh 		mii->mii_media_status |= IFM_ACTIVE;
    205  1.1  msaitoh 
    206  1.1  msaitoh 	PHY_READ(sc, MII_BMCR, &bmcr);
    207  1.1  msaitoh 	if ((bmcr & BMCR_ISO) != 0) {
    208  1.1  msaitoh 		mii->mii_media_active |= IFM_NONE;
    209  1.1  msaitoh 		mii->mii_media_status = 0;
    210  1.1  msaitoh 		return;
    211  1.1  msaitoh 	}
    212  1.1  msaitoh 
    213  1.1  msaitoh 	if ((bmcr & BMCR_LOOP) != 0)
    214  1.1  msaitoh 		mii->mii_media_active |= IFM_LOOP;
    215  1.1  msaitoh 
    216  1.1  msaitoh 	if ((ssr & JMPHY_SSR_SPD_DPLX_RESOLVED) == 0) {
    217  1.1  msaitoh 		/* Erg, still trying, I guess... */
    218  1.1  msaitoh 		mii->mii_media_active |= IFM_NONE;
    219  1.1  msaitoh 		return;
    220  1.1  msaitoh 	}
    221  1.1  msaitoh 
    222  1.1  msaitoh 	switch ((ssr & JMPHY_SSR_SPEED_MASK)) {
    223  1.1  msaitoh 	case JMPHY_SSR_SPEED_1000:
    224  1.1  msaitoh 		mii->mii_media_active |= IFM_1000_T;
    225  1.1  msaitoh 		/*
    226  1.1  msaitoh 		 * jmphy(4) got a valid link so reset mii_ticks.
    227  1.1  msaitoh 		 * Resetting mii_ticks is needed in order to
    228  1.1  msaitoh 		 * detect link loss after auto-negotiation.
    229  1.1  msaitoh 		 */
    230  1.1  msaitoh 		sc->mii_ticks = 0;
    231  1.1  msaitoh 		break;
    232  1.1  msaitoh 	case JMPHY_SSR_SPEED_100:
    233  1.1  msaitoh 		mii->mii_media_active |= IFM_100_TX;
    234  1.1  msaitoh 		sc->mii_ticks = 0;
    235  1.1  msaitoh 		break;
    236  1.1  msaitoh 	case JMPHY_SSR_SPEED_10:
    237  1.1  msaitoh 		mii->mii_media_active |= IFM_10_T;
    238  1.1  msaitoh 		sc->mii_ticks = 0;
    239  1.1  msaitoh 		break;
    240  1.1  msaitoh 	default:
    241  1.1  msaitoh 		mii->mii_media_active |= IFM_NONE;
    242  1.1  msaitoh 		return;
    243  1.1  msaitoh 	}
    244  1.1  msaitoh 
    245  1.1  msaitoh 	if ((ssr & JMPHY_SSR_DUPLEX) != 0)
    246  1.1  msaitoh 		mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
    247  1.1  msaitoh 	else
    248  1.1  msaitoh 		mii->mii_media_active |= IFM_HDX;
    249  1.1  msaitoh 
    250  1.1  msaitoh 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
    251  1.1  msaitoh 		PHY_READ(sc, MII_GTSR, &gtsr);
    252  1.1  msaitoh 		if ((gtsr & GTSR_MS_RES) != 0)
    253  1.1  msaitoh 			mii->mii_media_active |= IFM_ETH_MASTER;
    254  1.1  msaitoh 	}
    255  1.1  msaitoh }
    256  1.1  msaitoh 
    257  1.1  msaitoh static void
    258  1.1  msaitoh jmphy_reset(struct mii_softc *sc)
    259  1.1  msaitoh {
    260  1.1  msaitoh 	int i;
    261  1.1  msaitoh 	uint16_t val;
    262  1.1  msaitoh 
    263  1.1  msaitoh 	/* Disable sleep mode. */
    264  1.1  msaitoh 	PHY_READ(sc, JMPHY_TMCTL, &val);
    265  1.1  msaitoh 	PHY_WRITE(sc, JMPHY_TMCTL, val & ~JMPHY_TMCTL_SLEEP_ENB);
    266  1.1  msaitoh 
    267  1.1  msaitoh 	PHY_READ(sc, MII_BMCR, &val);
    268  1.1  msaitoh 	PHY_WRITE(sc, MII_BMCR, val | BMCR_RESET);
    269  1.1  msaitoh 
    270  1.1  msaitoh 	for (i = 0; i < 1000; i++) {
    271  1.1  msaitoh 		DELAY(1);
    272  1.1  msaitoh 		PHY_READ(sc, MII_BMCR, &val);
    273  1.1  msaitoh 		if ((val & BMCR_RESET) == 0)
    274  1.1  msaitoh 			break;
    275  1.1  msaitoh 	}
    276  1.1  msaitoh }
    277  1.1  msaitoh 
    278  1.1  msaitoh static uint16_t
    279  1.1  msaitoh jmphy_anar(struct ifmedia_entry *ife)
    280  1.1  msaitoh {
    281  1.1  msaitoh 	uint16_t anar;
    282  1.1  msaitoh 
    283  1.1  msaitoh 	anar = 0;
    284  1.1  msaitoh 	switch (IFM_SUBTYPE(ife->ifm_media)) {
    285  1.1  msaitoh 	case IFM_AUTO:
    286  1.1  msaitoh 		anar |= ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10;
    287  1.1  msaitoh 		break;
    288  1.1  msaitoh 	case IFM_1000_T:
    289  1.1  msaitoh 		break;
    290  1.1  msaitoh 	case IFM_100_TX:
    291  1.1  msaitoh 		anar |= ANAR_TX | ANAR_TX_FD;
    292  1.1  msaitoh 		break;
    293  1.1  msaitoh 	case IFM_10_T:
    294  1.1  msaitoh 		anar |= ANAR_10 | ANAR_10_FD;
    295  1.1  msaitoh 		break;
    296  1.1  msaitoh 	default:
    297  1.1  msaitoh 		break;
    298  1.1  msaitoh 	}
    299  1.1  msaitoh 
    300  1.1  msaitoh 	return anar;
    301  1.1  msaitoh }
    302  1.1  msaitoh 
    303  1.1  msaitoh static int
    304  1.1  msaitoh jmphy_auto(struct mii_softc *sc, struct ifmedia_entry *ife)
    305  1.1  msaitoh {
    306  1.1  msaitoh 	uint16_t anar, bmcr, gig;
    307  1.1  msaitoh 
    308  1.1  msaitoh 	gig = 0;
    309  1.1  msaitoh 	PHY_READ(sc, MII_BMCR, &bmcr);
    310  1.1  msaitoh 	switch (IFM_SUBTYPE(ife->ifm_media)) {
    311  1.1  msaitoh 	case IFM_AUTO:
    312  1.1  msaitoh 		gig |= GTCR_ADV_1000TFDX | GTCR_ADV_1000THDX;
    313  1.1  msaitoh 		break;
    314  1.1  msaitoh 	case IFM_1000_T:
    315  1.1  msaitoh 		gig |= GTCR_ADV_1000TFDX | GTCR_ADV_1000THDX;
    316  1.1  msaitoh 		break;
    317  1.1  msaitoh 	case IFM_100_TX:
    318  1.1  msaitoh 	case IFM_10_T:
    319  1.1  msaitoh 		break;
    320  1.1  msaitoh 	case IFM_NONE:
    321  1.1  msaitoh 		PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO | BMCR_PDOWN);
    322  1.1  msaitoh 		return EJUSTRETURN;
    323  1.1  msaitoh 	default:
    324  1.1  msaitoh 		return EINVAL;
    325  1.1  msaitoh 	}
    326  1.1  msaitoh 
    327  1.1  msaitoh 	if ((ife->ifm_media & IFM_LOOP) != 0)
    328  1.1  msaitoh 		bmcr |= BMCR_LOOP;
    329  1.1  msaitoh 
    330  1.1  msaitoh 	anar = jmphy_anar(ife);
    331  1.1  msaitoh 	if (sc->mii_flags & MIIF_DOPAUSE)
    332  1.1  msaitoh 		anar |= ANAR_PAUSE_TOWARDS;
    333  1.1  msaitoh 
    334  1.1  msaitoh 	if ((sc->mii_flags & MIIF_HAVE_GTCR) != 0) {
    335  1.1  msaitoh #ifdef notyet
    336  1.1  msaitoh 		struct mii_data *mii;
    337  1.1  msaitoh 
    338  1.1  msaitoh 		mii = sc->mii_pdata;
    339  1.1  msaitoh 		if ((mii->mii_media.ifm_media & IFM_ETH_MASTER) != 0)
    340  1.1  msaitoh 			gig |= GTCR_MAN_MS | GTCR_MAN_ADV;
    341  1.1  msaitoh #endif
    342  1.1  msaitoh 		PHY_WRITE(sc, MII_100T2CR, gig);
    343  1.1  msaitoh 	}
    344  1.1  msaitoh 	PHY_WRITE(sc, MII_ANAR, anar | ANAR_CSMA);
    345  1.1  msaitoh 	PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_AUTOEN | BMCR_STARTNEG);
    346  1.1  msaitoh 
    347  1.1  msaitoh 	return EJUSTRETURN;
    348  1.1  msaitoh }
    349