Home | History | Annotate | Line # | Download | only in ic
dl10019.c revision 1.2
      1 /*	$NetBSD: dl10019.c,v 1.2 2001/06/03 04:39:52 thorpej 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 "opt_inet.h"
     40 #include "bpfilter.h"
     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 #ifdef INET
     54 #include <netinet/in.h>
     55 #include <netinet/if_inarp.h>
     56 #endif
     57 
     58 #include <machine/bus.h>
     59 #include <machine/intr.h>
     60 
     61 #include <dev/mii/miivar.h>
     62 #include <dev/mii/mii.h>
     63 #include <dev/mii/mii_bitbang.h>
     64 
     65 #include <dev/ic/dp8390reg.h>
     66 #include <dev/ic/dp8390var.h>
     67 
     68 #include <dev/ic/ne2000reg.h>
     69 #include <dev/ic/ne2000var.h>
     70 
     71 #include <dev/ic/dl10019reg.h>
     72 #include <dev/ic/dl10019var.h>
     73 
     74 int	dl10019_mii_readreg(struct device *, int, int);
     75 void	dl10019_mii_writereg(struct device *, int, int, int);
     76 void	dl10019_mii_statchg(struct device *);
     77 
     78 /*
     79  * MII bit-bang glue.
     80  */
     81 u_int32_t dl10019_mii_bitbang_read(struct device *);
     82 void dl10019_mii_bitbang_write(struct device *, u_int32_t);
     83 
     84 const struct mii_bitbang_ops dl10019_mii_bitbang_ops = {
     85 	dl10019_mii_bitbang_read,
     86 	dl10019_mii_bitbang_write,
     87 	{
     88 		DL0_GPIO_MII_DATAOUT,	/* MII_BIT_MDO */
     89 		DL0_GPIO_MII_DATAIN,	/* MII_BIT_MDI */
     90 		DL0_GPIO_MII_CLK,	/* MII_BIT_MDC */
     91 		DL0_19_GPIO_MII_DIROUT,	/* MII_BIT_DIR_HOST_PHY */
     92 		0,			/* MII_BIT_DIR_PHY_HOST */
     93 	}
     94 };
     95 
     96 const struct mii_bitbang_ops dl10022_mii_bitbang_ops = {
     97 	dl10019_mii_bitbang_read,
     98 	dl10019_mii_bitbang_write,
     99 	{
    100 		DL0_GPIO_MII_DATAOUT,	/* MII_BIT_MDO */
    101 		DL0_GPIO_MII_DATAIN,	/* MII_BIT_MDI */
    102 		DL0_GPIO_MII_CLK,	/* MII_BIT_MDC */
    103 		DL0_22_GPIO_MII_DIROUT,	/* MII_BIT_DIR_HOST_PHY */
    104 		0,			/* MII_BIT_DIR_PHY_HOST */
    105 	}
    106 };
    107 
    108 static void
    109 dl10019_mii_reset(struct dp8390_softc *sc)
    110 {
    111 	struct ne2000_softc *nsc = (void *) sc;
    112 	int i;
    113 
    114 	if (nsc->sc_type != NE2000_TYPE_DL10022)
    115 		return;
    116 
    117 	for (i = 0; i < 2; i++) {
    118 		bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
    119 		    0x08);
    120 		delay(1);
    121 		bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
    122 		    0x0c);
    123 		delay(1);
    124 	}
    125 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO, 0x00);
    126 }
    127 
    128 void
    129 dl10019_media_init(struct dp8390_softc *sc)
    130 {
    131 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    132 
    133 	sc->sc_mii.mii_ifp = ifp;
    134 	sc->sc_mii.mii_readreg = dl10019_mii_readreg;
    135 	sc->sc_mii.mii_writereg = dl10019_mii_writereg;
    136 	sc->sc_mii.mii_statchg = dl10019_mii_statchg;
    137 	ifmedia_init(&sc->sc_mii.mii_media, 0, dp8390_mediachange,
    138 	    dp8390_mediastatus);
    139 
    140 	dl10019_mii_reset(sc);
    141 
    142 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    143 	    MII_OFFSET_ANY, 0);
    144 
    145 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    146 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    147 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    148 	} else
    149 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    150 }
    151 
    152 void
    153 dl10019_media_fini(struct dp8390_softc *sc)
    154 {
    155 
    156 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    157 }
    158 
    159 int
    160 dl10019_mediachange(struct dp8390_softc *sc)
    161 {
    162 
    163 	mii_mediachg(&sc->sc_mii);
    164 	return (0);
    165 }
    166 
    167 void
    168 dl10019_mediastatus(struct dp8390_softc *sc, struct ifmediareq *ifmr)
    169 {
    170 
    171 	mii_pollstat(&sc->sc_mii);
    172 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
    173 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    174 }
    175 
    176 void
    177 dl10019_init_card(struct dp8390_softc *sc)
    178 {
    179 
    180 	dl10019_mii_reset(sc);
    181 	mii_mediachg(&sc->sc_mii);
    182 }
    183 
    184 void
    185 dl10019_stop_card(struct dp8390_softc *sc)
    186 {
    187 
    188 	mii_down(&sc->sc_mii);
    189 }
    190 
    191 u_int32_t
    192 dl10019_mii_bitbang_read(struct device *self)
    193 {
    194 	struct dp8390_softc *sc = (void *) self;
    195 
    196 	/* We're already in Page 0. */
    197 	return (bus_space_read_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO) &
    198 	    ~DL0_GPIO_PRESERVE);
    199 }
    200 
    201 void
    202 dl10019_mii_bitbang_write(struct device *self, u_int32_t val)
    203 {
    204 	struct dp8390_softc *sc = (void *) self;
    205 	u_int8_t gpio;
    206 
    207 	/* We're already in Page 0. */
    208 	gpio = bus_space_read_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO);
    209 	bus_space_write_1(sc->sc_regt, sc->sc_regh, NEDL_DL0_GPIO,
    210 	    (val & ~DL0_GPIO_PRESERVE) | (gpio & DL0_GPIO_PRESERVE));
    211 }
    212 
    213 int
    214 dl10019_mii_readreg(struct device *self, int phy, int reg)
    215 {
    216 	struct ne2000_softc *nsc = (void *) self;
    217 	const struct mii_bitbang_ops *ops;
    218 	int val;
    219 
    220 	ops = (nsc->sc_type == NE2000_TYPE_DL10022) ?
    221 	    &dl10022_mii_bitbang_ops : &dl10019_mii_bitbang_ops;
    222 
    223 	val = mii_bitbang_readreg(self, &dl10019_mii_bitbang_ops, phy, reg);
    224 
    225 	return (val);
    226 }
    227 
    228 void
    229 dl10019_mii_writereg(struct device *self, int phy, int reg, int val)
    230 {
    231 	struct ne2000_softc *nsc = (void *) self;
    232 	const struct mii_bitbang_ops *ops;
    233 
    234 	ops = (nsc->sc_type == NE2000_TYPE_DL10022) ?
    235 	    &dl10022_mii_bitbang_ops : &dl10019_mii_bitbang_ops;
    236 
    237 	mii_bitbang_writereg(self, &dl10019_mii_bitbang_ops, phy, reg, val);
    238 }
    239 
    240 void
    241 dl10019_mii_statchg(struct device *self)
    242 {
    243 	struct dp8390_softc *sc = (void *) self;
    244 	struct ne2000_softc *nsc = (void *) self;
    245 
    246 	/*
    247 	 * Disable collision detection on the DL10022 if
    248 	 * we are on a full-duplex link.
    249 	 */
    250 	if (nsc->sc_type == NE2000_TYPE_DL10022) {
    251 		u_int8_t diag;
    252 
    253 		if (sc->sc_mii.mii_media_active & IFM_FDX)
    254 			diag = DL0_DIAG_NOCOLLDETECT;
    255 		else
    256 			diag = 0;
    257 		bus_space_write_1(sc->sc_regt, sc->sc_regh,
    258 		    NEDL_DL0_DIAG, diag);
    259 	}
    260 }
    261