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