Home | History | Annotate | Line # | Download | only in ic
rtl80x9.c revision 1.12.16.1
      1  1.12.16.1      mjf /*	$NetBSD: rtl80x9.c,v 1.12.16.1 2008/04/03 12:42:42 mjf Exp $	*/
      2        1.1  thorpej 
      3        1.1  thorpej /*-
      4        1.1  thorpej  * Copyright (c) 1998 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 of the Numerical Aerospace Simulation Facility,
      9        1.1  thorpej  * NASA Ames Research Center.
     10        1.1  thorpej  *
     11        1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12        1.1  thorpej  * modification, are permitted provided that the following conditions
     13        1.1  thorpej  * are met:
     14        1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15        1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16        1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18        1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19        1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     20        1.1  thorpej  *    must display the following acknowledgement:
     21        1.1  thorpej  *	This product includes software developed by the NetBSD
     22        1.1  thorpej  *	Foundation, Inc. and its contributors.
     23        1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24        1.1  thorpej  *    contributors may be used to endorse or promote products derived
     25        1.1  thorpej  *    from this software without specific prior written permission.
     26        1.1  thorpej  *
     27        1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28        1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29        1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30        1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31        1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32        1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33        1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34        1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35        1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36        1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37        1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38        1.1  thorpej  */
     39        1.8    lukem 
     40        1.8    lukem #include <sys/cdefs.h>
     41  1.12.16.1      mjf __KERNEL_RCSID(0, "$NetBSD: rtl80x9.c,v 1.12.16.1 2008/04/03 12:42:42 mjf Exp $");
     42        1.1  thorpej 
     43        1.1  thorpej #include <sys/param.h>
     44        1.1  thorpej #include <sys/systm.h>
     45        1.1  thorpej #include <sys/mbuf.h>
     46        1.1  thorpej #include <sys/syslog.h>
     47        1.1  thorpej #include <sys/socket.h>
     48        1.1  thorpej #include <sys/device.h>
     49        1.1  thorpej 
     50        1.1  thorpej #include <net/if.h>
     51        1.1  thorpej #include <net/if_ether.h>
     52        1.1  thorpej #include <net/if_media.h>
     53        1.1  thorpej 
     54       1.12       ad #include <sys/bus.h>
     55       1.12       ad #include <sys/intr.h>
     56        1.1  thorpej 
     57        1.1  thorpej #include <dev/ic/dp8390reg.h>
     58        1.1  thorpej #include <dev/ic/dp8390var.h>
     59        1.1  thorpej 
     60        1.1  thorpej #include <dev/ic/ne2000reg.h>
     61        1.1  thorpej #include <dev/ic/ne2000var.h>
     62        1.1  thorpej 
     63        1.1  thorpej #include <dev/ic/rtl80x9reg.h>
     64        1.1  thorpej #include <dev/ic/rtl80x9var.h>
     65        1.1  thorpej 
     66        1.1  thorpej int
     67        1.1  thorpej rtl80x9_mediachange(dsc)
     68        1.1  thorpej 	struct dp8390_softc *dsc;
     69        1.1  thorpej {
     70        1.1  thorpej 
     71        1.1  thorpej 	/*
     72        1.1  thorpej 	 * Current media is already set up.  Just reset the interface
     73        1.1  thorpej 	 * to let the new value take hold.  The new media will be
     74        1.1  thorpej 	 * set up in ne_pci_rtl8029_init_card() called via dp8390_init().
     75        1.1  thorpej 	 */
     76        1.1  thorpej 	dp8390_reset(dsc);
     77        1.1  thorpej 	return (0);
     78        1.1  thorpej }
     79        1.1  thorpej 
     80        1.1  thorpej void
     81        1.1  thorpej rtl80x9_mediastatus(sc, ifmr)
     82        1.1  thorpej 	struct dp8390_softc *sc;
     83        1.1  thorpej 	struct ifmediareq *ifmr;
     84        1.1  thorpej {
     85        1.1  thorpej 	struct ifnet *ifp = &sc->sc_ec.ec_if;
     86        1.1  thorpej 	u_int8_t cr_proto = sc->cr_proto |
     87        1.1  thorpej 	    ((ifp->if_flags & IFF_RUNNING) ? ED_CR_STA : ED_CR_STP);
     88        1.1  thorpej 
     89        1.1  thorpej 	/*
     90        1.1  thorpej 	 * Sigh, can detect which media is being used, but can't
     91        1.1  thorpej 	 * detect if we have link or not.
     92        1.1  thorpej 	 */
     93        1.1  thorpej 
     94        1.1  thorpej 	/* Set NIC to page 3 registers. */
     95        1.1  thorpej 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_3);
     96        1.1  thorpej 
     97        1.1  thorpej 	if (NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG0) &
     98        1.1  thorpej 	    RTL3_CONFIG0_BNC)
     99        1.1  thorpej 		ifmr->ifm_active = IFM_ETHER|IFM_10_2;
    100        1.1  thorpej 	else {
    101        1.1  thorpej 		ifmr->ifm_active = IFM_ETHER|IFM_10_T;
    102        1.1  thorpej 		if (NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3) &
    103        1.1  thorpej 		    RTL3_CONFIG3_FUDUP)
    104        1.1  thorpej 			ifmr->ifm_active |= IFM_FDX;
    105        1.1  thorpej 	}
    106        1.1  thorpej 
    107        1.1  thorpej 	/* Set NIC to page 0 registers. */
    108        1.1  thorpej 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_0);
    109        1.1  thorpej }
    110        1.1  thorpej 
    111        1.1  thorpej void
    112        1.1  thorpej rtl80x9_init_card(sc)
    113        1.1  thorpej 	struct dp8390_softc *sc;
    114        1.1  thorpej {
    115        1.1  thorpej 	struct ifmedia *ifm = &sc->sc_media;
    116        1.1  thorpej 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    117        1.1  thorpej 	u_int8_t cr_proto = sc->cr_proto |
    118        1.1  thorpej 	    ((ifp->if_flags & IFF_RUNNING) ? ED_CR_STA : ED_CR_STP);
    119        1.1  thorpej 	u_int8_t reg;
    120        1.1  thorpej 
    121        1.1  thorpej 	/* Set NIC to page 3 registers. */
    122        1.1  thorpej 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_3);
    123        1.1  thorpej 
    124        1.3       is 	/* write enable config1-3. */
    125        1.3       is 	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_EECR,
    126        1.3       is 	    RTL3_EECR_EEM1|RTL3_EECR_EEM0);
    127        1.3       is 
    128        1.1  thorpej 	/* First, set basic media type. */
    129        1.1  thorpej 	reg = NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG2);
    130        1.1  thorpej 	reg &= ~(RTL3_CONFIG2_PL1|RTL3_CONFIG2_PL0);
    131        1.1  thorpej 	switch (IFM_SUBTYPE(ifm->ifm_cur->ifm_media)) {
    132        1.1  thorpej 	case IFM_AUTO:
    133        1.1  thorpej 		/* Nothing to do; both bits clear == auto-detect. */
    134        1.1  thorpej 		break;
    135        1.1  thorpej 
    136        1.1  thorpej 	case IFM_10_T:
    137        1.4       is 		/*
    138        1.4       is 		 * According to docs, this should be:
    139        1.4       is 		 * reg |= RTL3_CONFIG2_PL0;
    140        1.4       is 		 * but this doesn't work, so make it the same as AUTO.
    141        1.4       is 		 */
    142        1.1  thorpej 		break;
    143        1.1  thorpej 
    144        1.1  thorpej 	case IFM_10_2:
    145        1.1  thorpej 		reg |= RTL3_CONFIG2_PL1|RTL3_CONFIG2_PL0;
    146        1.1  thorpej 		break;
    147        1.1  thorpej 	}
    148        1.1  thorpej 	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG2, reg);
    149        1.1  thorpej 
    150        1.1  thorpej 	/* Now, set duplex mode. */
    151        1.1  thorpej 	reg = NIC_GET(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3);
    152        1.1  thorpej 	if (ifm->ifm_cur->ifm_media & IFM_FDX)
    153        1.1  thorpej 		reg |= RTL3_CONFIG3_FUDUP;
    154        1.1  thorpej 	else
    155        1.1  thorpej 		reg &= ~RTL3_CONFIG3_FUDUP;
    156        1.1  thorpej 	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3, reg);
    157        1.1  thorpej 
    158        1.3       is 	/* write disable config1-3 */
    159        1.3       is 	NIC_PUT(sc->sc_regt, sc->sc_regh, NERTL_RTL3_EECR, 0);
    160        1.3       is 
    161        1.1  thorpej 	/* Set NIC to page 0 registers. */
    162        1.1  thorpej 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_CR, cr_proto | ED_CR_PAGE_0);
    163        1.1  thorpej }
    164        1.1  thorpej 
    165        1.1  thorpej void
    166        1.5  thorpej rtl80x9_media_init(sc)
    167        1.1  thorpej 	struct dp8390_softc *sc;
    168        1.1  thorpej {
    169        1.1  thorpej 	static int rtl80x9_media[] = {
    170        1.1  thorpej 		IFM_ETHER|IFM_AUTO,
    171        1.1  thorpej 		IFM_ETHER|IFM_10_T,
    172        1.1  thorpej 		IFM_ETHER|IFM_10_T|IFM_FDX,
    173        1.1  thorpej 		IFM_ETHER|IFM_10_2,
    174        1.1  thorpej 	};
    175        1.5  thorpej 	static const int rtl80x9_nmedia =
    176        1.5  thorpej 	    sizeof(rtl80x9_media) / sizeof(rtl80x9_media[0]);
    177        1.1  thorpej 
    178        1.5  thorpej 	int i, defmedia;
    179        1.3       is 	u_int8_t conf2, conf3;
    180        1.1  thorpej 
    181  1.12.16.1      mjf 	aprint_normal_dev(sc->sc_dev,
    182  1.12.16.1      mjf 	    "10base2, 10baseT, 10baseT-FDX, auto, default ");
    183        1.3       is 
    184        1.3       is 	bus_space_write_1(sc->sc_regt, sc->sc_regh, ED_P0_CR, ED_CR_PAGE_3);
    185        1.3       is 
    186        1.3       is 	conf2 = bus_space_read_1(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG2);
    187        1.3       is 	conf3 = bus_space_read_1(sc->sc_regt, sc->sc_regh, NERTL_RTL3_CONFIG3);
    188        1.3       is 	printf("[0x%02x 0x%02x] ", conf2, conf3);
    189        1.3       is 
    190        1.3       is 	conf2 &= RTL3_CONFIG2_PL1|RTL3_CONFIG2_PL0;
    191        1.3       is 
    192        1.3       is 	switch (conf2) {
    193       1.10  mycroft 	default:
    194        1.5  thorpej 		defmedia = IFM_ETHER|IFM_AUTO;
    195        1.3       is 		printf("auto\n");
    196        1.3       is 		break;
    197        1.3       is 
    198        1.3       is 	case RTL3_CONFIG2_PL1|RTL3_CONFIG2_PL0:
    199        1.3       is 	case RTL3_CONFIG2_PL1:	/* XXX rtl docs sys 10base5, but chip cant do */
    200        1.5  thorpej 		defmedia = IFM_ETHER|IFM_10_2;
    201        1.3       is 		printf("10base2\n");
    202        1.3       is 		break;
    203        1.3       is 
    204        1.3       is 	case RTL3_CONFIG2_PL0:
    205        1.3       is 		if (conf3 & RTL3_CONFIG3_FUDUP) {
    206        1.5  thorpej 			defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
    207        1.3       is 			printf("10baseT-FDX\n");
    208        1.3       is 		} else {
    209        1.5  thorpej 			defmedia = IFM_ETHER|IFM_10_T;
    210        1.3       is 			printf("10baseT\n");
    211        1.3       is 		}
    212        1.3       is 		break;
    213        1.3       is 	}
    214        1.3       is 
    215        1.3       is 	bus_space_write_1(sc->sc_regt, sc->sc_regh, ED_P0_CR, ED_CR_PAGE_0);
    216        1.5  thorpej 
    217        1.5  thorpej 	ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
    218        1.5  thorpej 	for (i = 0; i < rtl80x9_nmedia; i++)
    219        1.5  thorpej 		ifmedia_add(&sc->sc_media, rtl80x9_media[i], 0, NULL);
    220        1.5  thorpej 	ifmedia_set(&sc->sc_media, defmedia);
    221        1.1  thorpej }
    222