Home | History | Annotate | Line # | Download | only in mii
tlphy.c revision 1.5
      1 /*	$NetBSD: tlphy.c,v 1.5 1998/01/12 09:28:54 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32  /* Driver for Texas Instruments's ThunderLAN internal PHY */
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/kernel.h>
     37 #include <sys/device.h>
     38 #include <sys/malloc.h>
     39 #include <sys/proc.h>
     40 #include <sys/socket.h>
     41 #include <net/if.h>
     42 #include <net/if_media.h>
     43 
     44 
     45 #include <dev/mii/mii_adapter.h>
     46 #include <dev/mii/mii_adapters_id.h>
     47 #include <dev/mii/mii_phy.h>
     48 #include <dev/mii/generic_phy.h>
     49 #include <dev/mii/tlphy.h>
     50 
     51 static int tlphy_up __P((struct phy_softc *sc));
     52 void	tlphy_pdown __P((void *v));
     53 int	tlphy_media_set __P((int, void *));
     54 int	tlphy_media_set_aui __P((struct phy_softc *));
     55 int	tlphy_status __P((int, void*));
     56 
     57 #ifdef __BROKEN_INDIRECT_CONFIG
     58 int	tlphymatch __P((struct device *, void *, void *));
     59 #else
     60 int	tlphymatch __P((struct device *, struct cfdata *, void *));
     61 #endif
     62 void	tlphyattach __P((struct device *, struct device *, void *));
     63 
     64 struct cfattach tlphy_ca = {
     65 	sizeof(struct phy_softc), tlphymatch, tlphyattach
     66 };
     67 
     68 int
     69 tlphymatch(parent, match, aux)
     70 	struct device *parent;
     71 #ifdef __BROKEN_INDIRECT_CONFIG
     72 	void *match;
     73 #else
     74 	struct cfdata *match;
     75 #endif
     76 	void *aux;
     77 {
     78 	mii_phy_t *phy = aux;
     79 
     80 	if (phy->phy_id == 0x40005013 || phy->phy_id == 0x40005014 ||
     81 	    phy->phy_id == 0x40005015)
     82 		return (1);
     83 	return (0);
     84 }
     85 
     86 void
     87 tlphyattach(parent, self, aux)
     88 	struct device *parent, *self;
     89 	void *aux;
     90 {
     91 	struct phy_softc *sc = (struct phy_softc *)self;
     92 
     93 	sc->phy_link = aux;
     94 	sc->phy_link->phy_softc = sc;
     95 	sc->phy_link->phy_media_set = tlphy_media_set;
     96 	sc->phy_link->phy_status = tlphy_status;
     97 	sc->phy_link->phy_pdown = tlphy_pdown;
     98 
     99 	phy_reset(sc);
    100 
    101 	switch (sc->phy_link->adapter_id) {
    102 	case COMPAQ_INT_NETLIGENT_10_100:
    103 		sc->phy_link->phy_media = PHY_BNC;
    104 		break;
    105 	case COMPAQ_NETLIGENT_10_100:
    106 		sc->phy_link->phy_media = 0;
    107 		break;
    108 	case COMPAQ_INT_NETFLEX:
    109 	case COMPAQ_NETFLEX_BNC:
    110 		sc->phy_link->phy_media = PHY_BNC | PHY_10baseT;
    111 		break;
    112 	case TI_TLAN:
    113 		sc->phy_link->phy_media = PHY_10baseT;
    114 		break;
    115 	default:
    116 		sc->phy_link->phy_media = PHY_AUI;
    117 		if (phy_media_probe(sc) != 0) {
    118 			printf(" (autoconfig failed)");
    119 			break;
    120 		}
    121 	}
    122 
    123 	if (sc->phy_link->phy_media) {
    124 		printf(": ");
    125 		phy_media_print(sc->phy_link->phy_media);
    126 	}
    127 	printf("\n");
    128 }
    129 
    130 void
    131 tlphy_pdown(v)
    132 	void *v;
    133 {
    134 	struct phy_softc *sc = v;
    135 	mii_phy_t *phy_link = sc->phy_link;
    136 
    137 	mii_writereg(phy_link->mii_softc, phy_link->dev, PHY_CONTROL,
    138 	    CTRL_ISO | CTRL_PDOWN);
    139 }
    140 
    141 static int
    142 tlphy_up(sc)
    143 	struct phy_softc *sc;
    144 {
    145 	int s;
    146 	mii_phy_t *phy_link = sc->phy_link;
    147 	int control, i;
    148 
    149 	s = splnet();
    150 
    151 	/* set control registers to a reasonable value, wait for power_up */
    152 	mii_writereg(phy_link->mii_softc, phy_link->dev, PHY_CONTROL,
    153 	    CTRL_LOOPBK);
    154 	mii_writereg(phy_link->mii_softc, phy_link->dev, PHY_TL_CTRL, 0);
    155 	i = 0;
    156 	do {
    157 		DELAY(100000);
    158 		control = mii_readreg(phy_link->mii_softc, phy_link->dev,
    159 		    PHY_TL_ST);
    160 	} while ((control < 0 || (control & TL_ST_PHOK) == 0) && ++i < 10);
    161 	if (control < 0 || (control & TL_ST_PHOK) == 0) {
    162 		splx(s);
    163 		printf("%s: power-up failed\n", sc->sc_dev.dv_xname);
    164 		return (0);
    165 	}
    166 	if (phy_reset(sc) == 0) {
    167 		splx(s);
    168 		return (0);
    169 	}
    170 	splx(s);
    171 	return (1);
    172 }
    173 
    174 int
    175 tlphy_media_set(media, v)
    176 	int media;
    177 	void *v;
    178 {
    179 	struct phy_softc *sc = v;
    180 	int subtype;
    181 
    182 	if (IFM_TYPE(media) != IFM_ETHER)
    183 		return (EINVAL);
    184 
    185 	if (tlphy_up(sc) == 0)
    186 		return (EIO);
    187 
    188 	subtype = IFM_SUBTYPE(media);
    189 	switch (subtype) {
    190 	case IFM_10_2:
    191 	case IFM_10_5:
    192 		if ((subtype == IFM_10_2 &&
    193 		     (sc->phy_link->phy_media & PHY_BNC)) ||
    194 		    (subtype == IFM_10_5 &&
    195 		     (sc->phy_link->phy_media & PHY_AUI)))
    196 			return (tlphy_media_set_aui(sc));
    197 		else
    198 			return (EINVAL);
    199 	case IFM_10_T:
    200 		return (phy_media_set_10_100(sc, media));
    201 	case IFM_AUTO:
    202 		return (ENODEV);
    203 	default:
    204 		return (EINVAL);
    205 	}
    206 }
    207 
    208 int
    209 tlphy_media_set_aui(sc)
    210 	struct phy_softc *sc;
    211 {
    212 	mii_phy_t *phy_link = sc->phy_link;
    213 
    214 	mii_writereg(phy_link->mii_softc, phy_link->dev,
    215 	    PHY_CONTROL, 0);
    216 	mii_writereg(phy_link->mii_softc, phy_link->dev,
    217 	    PHY_TL_CTRL, TL_CTRL_AUISEL);
    218 	DELAY(100000);
    219 	return (0);
    220 }
    221 
    222 int
    223 tlphy_status(media, v)
    224 	int media;
    225 	void *v;
    226 {
    227 	struct phy_softc *sc = v;
    228 	int reg;
    229 
    230 	if (IFM_SUBTYPE(media) == IFM_10_T)
    231 		return (phy_status(media, sc));
    232 	reg = mii_readreg(sc->phy_link->mii_softc, sc->phy_link->dev,
    233 	    PHY_TL_ST);
    234 	if ((reg & PHY_TL_ST) == 0)
    235 		return (ENETDOWN);
    236 	return (0);
    237 }
    238