Home | History | Annotate | Line # | Download | only in mii
micphy.c revision 1.1.6.2
      1  1.1.6.2  yamt /*	$NetBSD: micphy.c,v 1.1.6.2 2014/05/22 11:40:23 yamt Exp $	*/
      2  1.1.6.2  yamt 
      3  1.1.6.2  yamt /*-
      4  1.1.6.2  yamt  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
      5  1.1.6.2  yamt  * All rights reserved.
      6  1.1.6.2  yamt  *
      7  1.1.6.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.6.2  yamt  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1.6.2  yamt  * NASA Ames Research Center, and by Frank van der Linden.
     10  1.1.6.2  yamt  *
     11  1.1.6.2  yamt  * Redistribution and use in source and binary forms, with or without
     12  1.1.6.2  yamt  * modification, are permitted provided that the following conditions
     13  1.1.6.2  yamt  * are met:
     14  1.1.6.2  yamt  * 1. Redistributions of source code must retain the above copyright
     15  1.1.6.2  yamt  *    notice, this list of conditions and the following disclaimer.
     16  1.1.6.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1.6.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     18  1.1.6.2  yamt  *    documentation and/or other materials provided with the distribution.
     19  1.1.6.2  yamt  *
     20  1.1.6.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1.6.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1.6.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1.6.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1.6.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1.6.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1.6.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1.6.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1.6.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1.6.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1.6.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1.6.2  yamt  */
     32  1.1.6.2  yamt 
     33  1.1.6.2  yamt /*
     34  1.1.6.2  yamt  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     35  1.1.6.2  yamt  *
     36  1.1.6.2  yamt  * Redistribution and use in source and binary forms, with or without
     37  1.1.6.2  yamt  * modification, are permitted provided that the following conditions
     38  1.1.6.2  yamt  * are met:
     39  1.1.6.2  yamt  * 1. Redistributions of source code must retain the above copyright
     40  1.1.6.2  yamt  *    notice, this list of conditions and the following disclaimer.
     41  1.1.6.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     42  1.1.6.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     43  1.1.6.2  yamt  *    documentation and/or other materials provided with the distribution.
     44  1.1.6.2  yamt  *
     45  1.1.6.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     46  1.1.6.2  yamt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     47  1.1.6.2  yamt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     48  1.1.6.2  yamt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     49  1.1.6.2  yamt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     50  1.1.6.2  yamt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     51  1.1.6.2  yamt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     52  1.1.6.2  yamt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     53  1.1.6.2  yamt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     54  1.1.6.2  yamt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     55  1.1.6.2  yamt  */
     56  1.1.6.2  yamt 
     57  1.1.6.2  yamt /*
     58  1.1.6.2  yamt  * Driver for Micrel KSZ9021RN PHYs
     59  1.1.6.2  yamt  */
     60  1.1.6.2  yamt 
     61  1.1.6.2  yamt #include <sys/cdefs.h>
     62  1.1.6.2  yamt __KERNEL_RCSID(0, "$NetBSD: micphy.c,v 1.1.6.2 2014/05/22 11:40:23 yamt Exp $");
     63  1.1.6.2  yamt 
     64  1.1.6.2  yamt #include "opt_mii.h"
     65  1.1.6.2  yamt 
     66  1.1.6.2  yamt #include <sys/param.h>
     67  1.1.6.2  yamt #include <sys/systm.h>
     68  1.1.6.2  yamt #include <sys/kernel.h>
     69  1.1.6.2  yamt #include <sys/device.h>
     70  1.1.6.2  yamt #include <sys/socket.h>
     71  1.1.6.2  yamt #include <sys/errno.h>
     72  1.1.6.2  yamt 
     73  1.1.6.2  yamt #include <net/if.h>
     74  1.1.6.2  yamt #include <net/if_media.h>
     75  1.1.6.2  yamt 
     76  1.1.6.2  yamt #include <dev/mii/mii.h>
     77  1.1.6.2  yamt #include <dev/mii/miivar.h>
     78  1.1.6.2  yamt #include <dev/mii/miidevs.h>
     79  1.1.6.2  yamt 
     80  1.1.6.2  yamt static int	micphymatch(device_t, cfdata_t, void *);
     81  1.1.6.2  yamt static void	micphyattach(device_t, device_t, void *);
     82  1.1.6.2  yamt 
     83  1.1.6.2  yamt CFATTACH_DECL3_NEW(micphy, sizeof(struct mii_softc),
     84  1.1.6.2  yamt     micphymatch, micphyattach, mii_phy_detach, mii_phy_activate, NULL, NULL,
     85  1.1.6.2  yamt     DVF_DETACH_SHUTDOWN);
     86  1.1.6.2  yamt 
     87  1.1.6.2  yamt static int	micphy_service(struct mii_softc *, struct mii_data *, int);
     88  1.1.6.2  yamt static void	micphy_fixup(struct mii_softc *, int, int, device_t);
     89  1.1.6.2  yamt 
     90  1.1.6.2  yamt static const struct mii_phy_funcs micphy_funcs = {
     91  1.1.6.2  yamt 	micphy_service, ukphy_status, mii_phy_reset,
     92  1.1.6.2  yamt };
     93  1.1.6.2  yamt 
     94  1.1.6.2  yamt static const struct mii_phydesc micphys[] = {
     95  1.1.6.2  yamt 	{ MII_OUI_MICREL,		MII_MODEL_MICREL_KSZ9021RNI,
     96  1.1.6.2  yamt 	  MII_STR_MICREL_KSZ9021RNI },
     97  1.1.6.2  yamt 
     98  1.1.6.2  yamt 	{ 0,				0,
     99  1.1.6.2  yamt 	  NULL },
    100  1.1.6.2  yamt };
    101  1.1.6.2  yamt 
    102  1.1.6.2  yamt static int
    103  1.1.6.2  yamt micphymatch(device_t parent, cfdata_t match, void *aux)
    104  1.1.6.2  yamt {
    105  1.1.6.2  yamt 	struct mii_attach_args *ma = aux;
    106  1.1.6.2  yamt 
    107  1.1.6.2  yamt 	if (mii_phy_match(ma, micphys) != NULL)
    108  1.1.6.2  yamt 		return 10;
    109  1.1.6.2  yamt 
    110  1.1.6.2  yamt 	return 1;
    111  1.1.6.2  yamt }
    112  1.1.6.2  yamt 
    113  1.1.6.2  yamt static void
    114  1.1.6.2  yamt micphyattach(device_t parent, device_t self, void *aux)
    115  1.1.6.2  yamt {
    116  1.1.6.2  yamt 	struct mii_softc *sc = device_private(self);
    117  1.1.6.2  yamt 	struct mii_attach_args *ma = aux;
    118  1.1.6.2  yamt 	struct mii_data *mii = ma->mii_data;
    119  1.1.6.2  yamt 	int model = MII_MODEL(ma->mii_id2);
    120  1.1.6.2  yamt 	int rev = MII_REV(ma->mii_id2);
    121  1.1.6.2  yamt 	const struct mii_phydesc *mpd;
    122  1.1.6.2  yamt 
    123  1.1.6.2  yamt 	mpd = mii_phy_match(ma, micphys);
    124  1.1.6.2  yamt 	aprint_naive(": Media interface\n");
    125  1.1.6.2  yamt 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, rev);
    126  1.1.6.2  yamt 
    127  1.1.6.2  yamt 	sc->mii_dev = self;
    128  1.1.6.2  yamt 	sc->mii_inst = mii->mii_instance;
    129  1.1.6.2  yamt 	sc->mii_phy = ma->mii_phyno;
    130  1.1.6.2  yamt 	sc->mii_funcs = &micphy_funcs;
    131  1.1.6.2  yamt 	sc->mii_pdata = mii;
    132  1.1.6.2  yamt 	sc->mii_flags = ma->mii_flags;
    133  1.1.6.2  yamt 	sc->mii_anegticks = MII_ANEGTICKS;
    134  1.1.6.2  yamt 
    135  1.1.6.2  yamt 	PHY_RESET(sc);
    136  1.1.6.2  yamt 
    137  1.1.6.2  yamt 	micphy_fixup(sc, model, rev, parent);
    138  1.1.6.2  yamt 
    139  1.1.6.2  yamt 	sc->mii_capabilities =
    140  1.1.6.2  yamt 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
    141  1.1.6.2  yamt 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    142  1.1.6.2  yamt 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
    143  1.1.6.2  yamt 	aprint_normal_dev(self, "");
    144  1.1.6.2  yamt 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
    145  1.1.6.2  yamt 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
    146  1.1.6.2  yamt 		aprint_error("no media present");
    147  1.1.6.2  yamt 	else
    148  1.1.6.2  yamt 		mii_phy_add_media(sc);
    149  1.1.6.2  yamt 	aprint_normal("\n");
    150  1.1.6.2  yamt }
    151  1.1.6.2  yamt 
    152  1.1.6.2  yamt static int
    153  1.1.6.2  yamt micphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    154  1.1.6.2  yamt {
    155  1.1.6.2  yamt 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    156  1.1.6.2  yamt 	int reg;
    157  1.1.6.2  yamt 
    158  1.1.6.2  yamt 	switch (cmd) {
    159  1.1.6.2  yamt 	case MII_POLLSTAT:
    160  1.1.6.2  yamt 		/*
    161  1.1.6.2  yamt 		 * If we're not polling our PHY instance, just return.
    162  1.1.6.2  yamt 		 */
    163  1.1.6.2  yamt 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    164  1.1.6.2  yamt 			return 0;
    165  1.1.6.2  yamt 		break;
    166  1.1.6.2  yamt 
    167  1.1.6.2  yamt 	case MII_MEDIACHG:
    168  1.1.6.2  yamt 		/*
    169  1.1.6.2  yamt 		 * If the media indicates a different PHY instance,
    170  1.1.6.2  yamt 		 * isolate ourselves.
    171  1.1.6.2  yamt 		 */
    172  1.1.6.2  yamt 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    173  1.1.6.2  yamt 			reg = PHY_READ(sc, MII_BMCR);
    174  1.1.6.2  yamt 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    175  1.1.6.2  yamt 			return 0;
    176  1.1.6.2  yamt 		}
    177  1.1.6.2  yamt 
    178  1.1.6.2  yamt 		/*
    179  1.1.6.2  yamt 		 * If the interface is not up, don't do anything.
    180  1.1.6.2  yamt 		 */
    181  1.1.6.2  yamt 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    182  1.1.6.2  yamt 			break;
    183  1.1.6.2  yamt 
    184  1.1.6.2  yamt 		mii_phy_setmedia(sc);
    185  1.1.6.2  yamt 		break;
    186  1.1.6.2  yamt 
    187  1.1.6.2  yamt 	case MII_TICK:
    188  1.1.6.2  yamt 		/*
    189  1.1.6.2  yamt 		 * If we're not currently selected, just return.
    190  1.1.6.2  yamt 		 */
    191  1.1.6.2  yamt 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    192  1.1.6.2  yamt 			return 0;
    193  1.1.6.2  yamt 
    194  1.1.6.2  yamt 		if (mii_phy_tick(sc) == EJUSTRETURN)
    195  1.1.6.2  yamt 			return 0;
    196  1.1.6.2  yamt 		break;
    197  1.1.6.2  yamt 
    198  1.1.6.2  yamt 	case MII_DOWN:
    199  1.1.6.2  yamt 		mii_phy_down(sc);
    200  1.1.6.2  yamt 		return 0;
    201  1.1.6.2  yamt 	}
    202  1.1.6.2  yamt 
    203  1.1.6.2  yamt 	/* Update the media status. */
    204  1.1.6.2  yamt 	mii_phy_status(sc);
    205  1.1.6.2  yamt 
    206  1.1.6.2  yamt 	/* Callback if something changed. */
    207  1.1.6.2  yamt 	mii_phy_update(sc, cmd);
    208  1.1.6.2  yamt 	return 0;
    209  1.1.6.2  yamt }
    210  1.1.6.2  yamt 
    211  1.1.6.2  yamt #define XREG_CONTROL	0x0b
    212  1.1.6.2  yamt #define XREG_WRITE	0x0c
    213  1.1.6.2  yamt #define XREG_READ	0x0d
    214  1.1.6.2  yamt #define XREG_CTL_SEL_READ	0x0000
    215  1.1.6.2  yamt #define XREG_CTL_SEL_WRITE	0x8000
    216  1.1.6.2  yamt 
    217  1.1.6.2  yamt #define REG_RGMII_CLOCK_AND_CONTROL	0x104
    218  1.1.6.2  yamt #define REG_RGMII_RX_DATA		0x105
    219  1.1.6.2  yamt 
    220  1.1.6.2  yamt static void micphy_writexreg(struct mii_softc *sc, uint32_t reg, uint32_t wval)
    221  1.1.6.2  yamt {
    222  1.1.6.2  yamt 	int rval;
    223  1.1.6.2  yamt 
    224  1.1.6.2  yamt 	PHY_WRITE(sc, XREG_CONTROL, XREG_CTL_SEL_WRITE | reg);
    225  1.1.6.2  yamt 	PHY_WRITE(sc, XREG_WRITE, wval);
    226  1.1.6.2  yamt 	PHY_WRITE(sc, XREG_CONTROL, XREG_CTL_SEL_READ | reg);
    227  1.1.6.2  yamt 	rval = PHY_READ(sc, XREG_READ);
    228  1.1.6.2  yamt 	KDASSERT(wval == rval);
    229  1.1.6.2  yamt }
    230  1.1.6.2  yamt 
    231  1.1.6.2  yamt static void
    232  1.1.6.2  yamt micphy_fixup(struct mii_softc *sc, int model, int rev, device_t parent)
    233  1.1.6.2  yamt {
    234  1.1.6.2  yamt 	switch (model) {
    235  1.1.6.2  yamt 	case MII_MODEL_MICREL_KSZ9021RNI:
    236  1.1.6.2  yamt 		if (!device_is_a(parent, "cpsw"))
    237  1.1.6.2  yamt 			break;
    238  1.1.6.2  yamt 
    239  1.1.6.2  yamt 		aprint_normal_dev(sc->mii_dev, "adjusting RGMII signal timing for cpsw\n");
    240  1.1.6.2  yamt 
    241  1.1.6.2  yamt 		// RGMII RX Data Pad Skew
    242  1.1.6.2  yamt 		micphy_writexreg(sc, REG_RGMII_RX_DATA, 0x0000);
    243  1.1.6.2  yamt 
    244  1.1.6.2  yamt 		// RGMII Clock and Control Pad Skew
    245  1.1.6.2  yamt 		micphy_writexreg(sc, REG_RGMII_CLOCK_AND_CONTROL, 0x9090);
    246  1.1.6.2  yamt 
    247  1.1.6.2  yamt 		break;
    248  1.1.6.2  yamt 	}
    249  1.1.6.2  yamt 
    250  1.1.6.2  yamt 	return;
    251  1.1.6.2  yamt }
    252