Home | History | Annotate | Line # | Download | only in ic
dl10019.c revision 1.8.8.1
      1 /*	$NetBSD: dl10019.c,v 1.8.8.1 2008/01/20 17:51:31 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 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.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: dl10019.c,v 1.8.8.1 2008/01/20 17:51:31 bouyer Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/syslog.h>
     46 #include <sys/socket.h>
     47 #include <sys/device.h>
     48 
     49 #include <net/if.h>
     50 #include <net/if_ether.h>
     51 #include <net/if_media.h>
     52 
     53 #include <sys/bus.h>
     54 #include <sys/intr.h>
     55 
     56 #include <dev/mii/miivar.h>
     57 #include <dev/mii/mii.h>
     58 #include <dev/mii/mii_bitbang.h>
     59 
     60 #include <dev/ic/dp8390reg.h>
     61 #include <dev/ic/dp8390var.h>
     62 
     63 #include <dev/ic/ne2000reg.h>
     64 #include <dev/ic/ne2000var.h>
     65 
     66 #include <dev/ic/dl10019reg.h>
     67 #include <dev/ic/dl10019var.h>
     68 
     69 int	dl10019_mii_readreg(struct device *, int, int);
     70 void	dl10019_mii_writereg(struct device *, int, int, int);
     71 void	dl10019_mii_statchg(struct device *);
     72 
     73 /*
     74  * MII bit-bang glue.
     75  */
     76 u_int32_t dl10019_mii_bitbang_read(struct device *);
     77 void dl10019_mii_bitbang_write(struct device *, u_int32_t);
     78 
     79 const struct mii_bitbang_ops dl10019_mii_bitbang_ops = {
     80 	dl10019_mii_bitbang_read,
     81 	dl10019_mii_bitbang_write,
     82 	{
     83 		DL0_GPIO_MII_DATAOUT,	/* MII_BIT_MDO */
     84 		DL0_GPIO_MII_DATAIN,	/* MII_BIT_MDI */
     85 		DL0_GPIO_MII_CLK,	/* MII_BIT_MDC */
     86 		DL0_19_GPIO_MII_DIROUT,	/* MII_BIT_DIR_HOST_PHY */
     87 		0,			/* MII_BIT_DIR_PHY_HOST */
     88 	}
     89 };
     90 
     91 const struct mii_bitbang_ops dl10022_mii_bitbang_ops = {
     92 	dl10019_mii_bitbang_read,
     93 	dl10019_mii_bitbang_write,
     94 	{
     95 		DL0_GPIO_MII_DATAOUT,	/* MII_BIT_MDO */
     96 		DL0_GPIO_MII_DATAIN,	/* MII_BIT_MDI */
     97 		DL0_GPIO_MII_CLK,	/* MII_BIT_MDC */
     98 		DL0_22_GPIO_MII_DIROUT,	/* MII_BIT_DIR_HOST_PHY */
     99 		0,			/* MII_BIT_DIR_PHY_HOST */
    100 	}
    101 };
    102 
    103 static void
    104 dl10019_mii_reset(struct dp8390_softc *sc)
    105 {
    106 	struct ne2000_softc *nsc = (void *) sc;
    107 	int i;
    108 
    109 	if (nsc->sc_type != NE2000_TYPE_DL10022)
    110 		return;
    111 
    112 	for (i = 0; i < 2; i++) {
    113 		bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
    114 		    0x08);
    115 		delay(1);
    116 		bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
    117 		    0x0c);
    118 		delay(1);
    119 	}
    120 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO, 0x00);
    121 }
    122 
    123 void
    124 dl10019_media_init(struct dp8390_softc *sc)
    125 {
    126 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    127 
    128 	sc->sc_mii.mii_ifp = ifp;
    129 	sc->sc_mii.mii_readreg = dl10019_mii_readreg;
    130 	sc->sc_mii.mii_writereg = dl10019_mii_writereg;
    131 	sc->sc_mii.mii_statchg = dl10019_mii_statchg;
    132 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, dp8390_mediachange,
    133 	    dp8390_mediastatus);
    134 
    135 	dl10019_mii_reset(sc);
    136 
    137 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    138 	    MII_OFFSET_ANY, 0);
    139 
    140 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    141 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    142 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    143 	} else
    144 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    145 }
    146 
    147 void
    148 dl10019_media_fini(struct dp8390_softc *sc)
    149 {
    150 
    151 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    152 }
    153 
    154 int
    155 dl10019_mediachange(struct dp8390_softc *sc)
    156 {
    157 	int rc;
    158 
    159 	if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
    160 		return 0;
    161 	return rc;
    162 }
    163 
    164 void
    165 dl10019_mediastatus(struct dp8390_softc *sc, struct ifmediareq *ifmr)
    166 {
    167 
    168 	mii_pollstat(&sc->sc_mii);
    169 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
    170 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    171 }
    172 
    173 void
    174 dl10019_init_card(struct dp8390_softc *sc)
    175 {
    176 
    177 	dl10019_mii_reset(sc);
    178 	mii_mediachg(&sc->sc_mii);
    179 }
    180 
    181 void
    182 dl10019_stop_card(struct dp8390_softc *sc)
    183 {
    184 
    185 	mii_down(&sc->sc_mii);
    186 }
    187 
    188 u_int32_t
    189 dl10019_mii_bitbang_read(struct device *self)
    190 {
    191 	struct dp8390_softc *sc = (void *) self;
    192 
    193 	/* We're already in Page 0. */
    194 	return (bus_space_read_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO) &
    195 	    ~DL0_GPIO_PRESERVE);
    196 }
    197 
    198 void
    199 dl10019_mii_bitbang_write(struct device *self, u_int32_t val)
    200 {
    201 	struct dp8390_softc *sc = (void *) self;
    202 	u_int8_t gpio;
    203 
    204 	/* We're already in Page 0. */
    205 	gpio = bus_space_read_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO);
    206 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
    207 	    (val & ~DL0_GPIO_PRESERVE) | (gpio & DL0_GPIO_PRESERVE));
    208 }
    209 
    210 int
    211 dl10019_mii_readreg(struct device *self, int phy, int reg)
    212 {
    213 	struct ne2000_softc *nsc = (void *) self;
    214 	const struct mii_bitbang_ops *ops;
    215 	int val;
    216 
    217 	ops = (nsc->sc_type == NE2000_TYPE_DL10022) ?
    218 	    &dl10022_mii_bitbang_ops : &dl10019_mii_bitbang_ops;
    219 
    220 	val = mii_bitbang_readreg(self, ops, phy, reg);
    221 
    222 	return (val);
    223 }
    224 
    225 void
    226 dl10019_mii_writereg(struct device *self, int phy, int reg, int val)
    227 {
    228 	struct ne2000_softc *nsc = (void *) self;
    229 	const struct mii_bitbang_ops *ops;
    230 
    231 	ops = (nsc->sc_type == NE2000_TYPE_DL10022) ?
    232 	    &dl10022_mii_bitbang_ops : &dl10019_mii_bitbang_ops;
    233 
    234 	mii_bitbang_writereg(self, ops, phy, reg, val);
    235 }
    236 
    237 void
    238 dl10019_mii_statchg(struct device *self)
    239 {
    240 	struct dp8390_softc *sc = (void *) self;
    241 	struct ne2000_softc *nsc = (void *) self;
    242 
    243 	/*
    244 	 * Disable collision detection on the DL10022 if
    245 	 * we are on a full-duplex link.
    246 	 */
    247 	if (nsc->sc_type == NE2000_TYPE_DL10022) {
    248 		u_int8_t diag;
    249 
    250 		if (sc->sc_mii.mii_media_active & IFM_FDX)
    251 			diag = DL0_DIAG_NOCOLLDETECT;
    252 		else
    253 			diag = 0;
    254 		bus_space_write_1(sc->sc_regt, sc->sc_regh,
    255 		    NEDL_DL0_DIAG, diag);
    256 	}
    257 }
    258