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