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