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