Home | History | Annotate | Line # | Download | only in isa
if_ne_isa.c revision 1.25.2.1
      1 /*	$NetBSD: if_ne_isa.c,v 1.25.2.1 2008/05/18 12:34:03 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 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 of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: if_ne_isa.c,v 1.25.2.1 2008/05/18 12:34:03 yamt Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/socket.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/errno.h>
     42 #include <sys/syslog.h>
     43 #include <sys/select.h>
     44 #include <sys/device.h>
     45 
     46 #include <net/if.h>
     47 #include <net/if_dl.h>
     48 #include <net/if_ether.h>
     49 #include <net/if_media.h>
     50 
     51 #include <sys/intr.h>
     52 #include <sys/bus.h>
     53 
     54 #include <dev/ic/dp8390reg.h>
     55 #include <dev/ic/dp8390var.h>
     56 
     57 #include <dev/ic/ne2000reg.h>
     58 #include <dev/ic/ne2000var.h>
     59 
     60 #include <dev/ic/rtl80x9reg.h>
     61 #include <dev/ic/rtl80x9var.h>
     62 
     63 #include <dev/isa/isavar.h>
     64 
     65 int	ne_isa_match(device_t, cfdata_t, void *);
     66 void	ne_isa_attach(device_t, device_t, void *);
     67 
     68 struct ne_isa_softc {
     69 	struct	ne2000_softc sc_ne2000;		/* real "ne2000" softc */
     70 
     71 	/* ISA-specific goo. */
     72 	void	*sc_ih;				/* interrupt cookie */
     73 };
     74 
     75 CFATTACH_DECL_NEW(ne_isa, sizeof(struct ne_isa_softc),
     76     ne_isa_match, ne_isa_attach, NULL, NULL);
     77 
     78 int
     79 ne_isa_match(device_t parent, cfdata_t match, void *aux)
     80 {
     81 	struct isa_attach_args *ia = aux;
     82 	bus_space_tag_t nict = ia->ia_iot;
     83 	bus_space_handle_t nich;
     84 	bus_space_tag_t asict;
     85 	bus_space_handle_t asich;
     86 	int rv = 0;
     87 
     88 	if (ia->ia_nio < 1)
     89 		return (0);
     90 	if (ia->ia_nirq < 1)
     91 		return (0);
     92 
     93 	if (ISA_DIRECT_CONFIG(ia))
     94 		return (0);
     95 
     96 	/* Disallow wildcarded values. */
     97 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
     98 		return (0);
     99 	if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
    100 		return (0);
    101 
    102 	/* Make sure this is a valid NE[12]000 i/o address. */
    103 	if ((ia->ia_io[0].ir_addr & 0x1f) != 0)
    104 		return (0);
    105 
    106 	/* Map i/o space. */
    107 	if (bus_space_map(nict, ia->ia_io[0].ir_addr, NE2000_NPORTS, 0, &nich))
    108 		return (0);
    109 
    110 	asict = nict;
    111 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
    112 	    NE2000_ASIC_NPORTS, &asich))
    113 		goto out;
    114 
    115 	/* Look for an NE2000-compatible card. */
    116 	rv = ne2000_detect(nict, nich, asict, asich);
    117 
    118 	if (rv) {
    119 		ia->ia_nio = 1;
    120 		ia->ia_io[0].ir_size = NE2000_NPORTS;
    121 
    122 		ia->ia_nirq = 1;
    123 
    124 		ia->ia_niomem = 0;
    125 		ia->ia_ndrq = 0;
    126 	}
    127 
    128  out:
    129 	bus_space_unmap(nict, nich, NE2000_NPORTS);
    130 	return (rv);
    131 }
    132 
    133 void
    134 ne_isa_attach(device_t parent, device_t self, void *aux)
    135 {
    136 	struct ne_isa_softc *isc = device_private(self);
    137 	struct ne2000_softc *nsc = &isc->sc_ne2000;
    138 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
    139 	struct isa_attach_args *ia = aux;
    140 	bus_space_tag_t nict = ia->ia_iot;
    141 	bus_space_handle_t nich;
    142 	bus_space_tag_t asict = nict;
    143 	bus_space_handle_t asich;
    144 	const char *typestr;
    145 	int netype;
    146 
    147 	dsc->sc_dev = self;
    148 	aprint_normal("\n");
    149 
    150 	/* Map i/o space. */
    151 	if (bus_space_map(nict, ia->ia_io[0].ir_addr, NE2000_NPORTS,
    152 	    0, &nich)) {
    153 		aprint_error_dev(self, "can't map i/o space\n");
    154 		return;
    155 	}
    156 
    157 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
    158 	    NE2000_ASIC_NPORTS, &asich)) {
    159 		aprint_error_dev(self, "can't subregion i/o space\n");
    160 		return;
    161 	}
    162 
    163 	dsc->sc_regt = nict;
    164 	dsc->sc_regh = nich;
    165 
    166 	nsc->sc_asict = asict;
    167 	nsc->sc_asich = asich;
    168 
    169 	/*
    170 	 * Detect it again, so we can print some information about the
    171 	 * interface.
    172 	 */
    173 	netype = ne2000_detect(nict, nich, asict, asich);
    174 	switch (netype) {
    175 	case NE2000_TYPE_NE1000:
    176 		typestr = "NE1000";
    177 		break;
    178 
    179 	case NE2000_TYPE_NE2000:
    180 		typestr = "NE2000";
    181 		/*
    182 		 * Check for a Realtek 8019.
    183 		 */
    184 		bus_space_write_1(nict, nich, ED_P0_CR,
    185 		    ED_CR_PAGE_0 | ED_CR_STP);
    186 		if (bus_space_read_1(nict, nich, NERTL_RTL0_8019ID0) ==
    187 								RTL0_8019ID0 &&
    188 		    bus_space_read_1(nict, nich, NERTL_RTL0_8019ID1) ==
    189 								RTL0_8019ID1) {
    190 			typestr = "NE2000 (RTL8019)";
    191 			dsc->sc_mediachange = rtl80x9_mediachange;
    192 			dsc->sc_mediastatus = rtl80x9_mediastatus;
    193 			dsc->init_card = rtl80x9_init_card;
    194 			dsc->sc_media_init = rtl80x9_media_init;
    195 		}
    196 		break;
    197 
    198 	default:
    199 		aprint_error_dev(self, "where did the card go?!\n");
    200 		return;
    201 	}
    202 
    203 	aprint_normal_dev(self, "%s Ethernet\n", typestr);
    204 
    205 	/* This interface is always enabled. */
    206 	dsc->sc_enabled = 1;
    207 
    208 	/*
    209 	 * Do generic NE2000 attach.  This will read the station address
    210 	 * from the EEPROM.
    211 	 */
    212 	ne2000_attach(nsc, NULL);
    213 
    214 	/* Establish the interrupt handler. */
    215 	isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
    216 	    IST_EDGE, IPL_NET, dp8390_intr, dsc);
    217 	if (isc->sc_ih == NULL)
    218 		aprint_error_dev(self,
    219 		    "couldn't establish interrupt handler\n");
    220 }
    221