Home | History | Annotate | Line # | Download | only in mca
if_we_mca.c revision 1.19.12.1
      1 /*	$NetBSD: if_we_mca.c,v 1.19.12.1 2008/03/24 07:15:30 keiichi Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 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 of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, and Jaromir Dolecek.
     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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
     42  * adapters.
     43  *
     44  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
     45  *
     46  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
     47  * copied, distributed, and sold, in both source and binary form provided that
     48  * the above copyright and these terms are retained.  Under no circumstances is
     49  * the author responsible for the proper functioning of this software, nor does
     50  * the author assume any responsibility for damages incurred with its use.
     51  */
     52 
     53 /*
     54  * Device driver for the Western Digital/SMC 8003 and 8013 series,
     55  * and the SMC Elite Ultra (8216).
     56  *
     57  * Currently only tested with WD8003W/A and EtherCard PLUS Elite 10T/A
     58  * (8013WP/A). Other WD8003 and SMC Elite based cards should work
     59  * without problems too.
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: if_we_mca.c,v 1.19.12.1 2008/03/24 07:15:30 keiichi Exp $");
     64 
     65 #include <sys/param.h>
     66 #include <sys/systm.h>
     67 #include <sys/device.h>
     68 #include <sys/socket.h>
     69 #include <sys/mbuf.h>
     70 
     71 #include <net/if.h>
     72 #include <net/if_types.h>
     73 #include <net/if_media.h>
     74 #include <net/if_ether.h>
     75 
     76 #include <sys/bus.h>
     77 
     78 #include <dev/mca/mcareg.h>
     79 #include <dev/mca/mcavar.h>
     80 #include <dev/mca/mcadevs.h>
     81 
     82 #include <dev/ic/dp8390reg.h>
     83 #include <dev/ic/dp8390var.h>
     84 #include <dev/ic/wereg.h>
     85 #include <dev/ic/wevar.h>
     86 
     87 #define WD_8003		0x01
     88 #define WD_ELITE	0x02
     89 
     90 int we_mca_probe(device_t, cfdata_t , void *);
     91 void we_mca_attach(device_t, device_t, void *);
     92 void we_mca_init_hook(struct we_softc *);
     93 
     94 CFATTACH_DECL_NEW(we_mca, sizeof(struct we_softc),
     95     we_mca_probe, we_mca_attach, NULL, NULL);
     96 
     97 /*
     98  * The types for some cards may not be correct; hopefully it's close
     99  * enough.
    100  */
    101 static const struct we_mca_product {
    102 	u_int32_t we_id;
    103 	const char *we_name;
    104 	int we_flag;
    105 	int we_type;
    106 	const char *we_typestr;
    107 } we_mca_products[] = {
    108 	{ MCA_PRODUCT_WD_8013EP, "EtherCard PLUS Elite/A (8013EP/A)",
    109 		WD_ELITE,	WE_TYPE_WD8013EP, "WD8013EP/A" },
    110 	{ MCA_PRODUCT_WD_8013WP, "EtherCard PLUS Elite 10T/A (8013WP/A)",
    111 		WD_ELITE,	WE_TYPE_WD8013EP, "WD8013WP/A" },
    112 	{ MCA_PRODUCT_IBM_WD_2,"IBM PS/2 Adapter/A for Ethernet Networks (UTP)",
    113 		WD_ELITE, WE_TYPE_WD8013EP, "WD8013WP/A"},
    114 	{ MCA_PRODUCT_IBM_WD_T,"IBM PS/2 Adapter/A for Ethernet Networks (BNC)",
    115 		WD_ELITE, WE_TYPE_WD8013EP, "WD8013WP/A"},
    116 	{ MCA_PRODUCT_WD_8003E,	"WD EtherCard PLUS/A (WD8003E/A or WD8003ET/A)",
    117 		WD_8003,	WE_TYPE_WD8003E, "WD8003E/A or WD8003ET/A" },
    118 	{ MCA_PRODUCT_WD_8003ST,"WD StarCard PLUS/A (WD8003ST/A)",
    119 		WD_8003,	WE_TYPE_WD8003W, "WD8003ST/A" }, /* XXX */
    120 	{ MCA_PRODUCT_WD_8003W,	"WD EtherCard PLUS 10T/A (WD8003W/A)",
    121 		WD_8003,	WE_TYPE_WD8003W, "WD8003W/A" },
    122 	{ MCA_PRODUCT_IBM_WD_O, "IBM PS/2 Adapter/A for Ethernet Networks",
    123 		WD_8003,	WE_TYPE_WD8003W, "IBM PS/2 Adapter/A" },
    124 	{ 0x0000,		NULL,
    125 		0,		0,		 NULL },
    126 };
    127 
    128 /* see POS description in we_mca_attach() */
    129 static const int we_mca_irq[] = {
    130 	3, 4, 10, 15,
    131 };
    132 
    133 static const struct we_mca_product *we_mca_lookup(int);
    134 
    135 static const struct we_mca_product *
    136 we_mca_lookup(int id)
    137 {
    138 	const struct we_mca_product *wep;
    139 
    140 	for(wep = we_mca_products; wep->we_name; wep++)
    141 		if (wep->we_id == id)
    142 			return (wep);
    143 
    144 	return (NULL);
    145 }
    146 
    147 int
    148 we_mca_probe(device_t parent, cfdata_t cf, void *aux)
    149 {
    150 	struct mca_attach_args *ma = aux;
    151 
    152 	return (we_mca_lookup(ma->ma_id) != NULL);
    153 }
    154 
    155 void
    156 we_mca_attach(device_t parent, device_t self, void *aux)
    157 {
    158 	struct we_softc *wsc = device_private(self);
    159 	struct dp8390_softc *sc = &wsc->sc_dp8390;
    160 	struct mca_attach_args *ma = aux;
    161 	const struct we_mca_product *wep;
    162 	bus_space_tag_t nict, asict, memt;
    163 	bus_space_handle_t nich, asich, memh;
    164 	const char *typestr;
    165 	int irq, iobase, maddr;
    166 	int pos2, pos3, pos5;
    167 
    168 	sc->sc_dev = self;
    169 
    170 	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
    171 	pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
    172 	pos5 = mca_conf_read(ma->ma_mc, ma->ma_slot, 5);
    173 
    174 	/*
    175 	 * POS registers differ a lot between 8003 and 8013, so they are
    176 	 * divided to two sections.
    177 	 *
    178 	 * 8003: POS register 2: (adf pos0)
    179 	 * 7 6 5 4 3 2 1 0
    180 	 * 0 0 1 \_____/ \__ enable: 0=adapter disabled, 1=adapter enabled
    181 	 *             \____ Adapter I/O Space: 0x200-0x21F + XX*0x20
    182 	 *
    183 	 * 8003: POS register 3: (adf pos1)
    184 	 * 7 6 5 4 3 2 1 0
    185 	 * 1 1 0 \___/ 1 X
    186 	 *           \______ Shared Ram Space (16K Bytes):
    187 	 *                     0xC0000-0xC3FFF + XX * 0x4000
    188 	 *
    189 	 * 8003: POS register 5: (adf pos3)
    190 	 * 7 6 5 4 3 2 1 0
    191 	 *             \_/
    192 	 *               \__ Interrupt Level: 00=3 01=4 10=10 11=15
    193 	 *
    194 	 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    195 	 *
    196 	 * 8013: POS register 2: (adf pos0)
    197 	 * 7 6 5 4 3 2 1 0
    198 	 * \____/        \__ enable: 0=adapter disabled, 1=adapter enabled
    199 	 *      \___________ Adapter I/O Space: 0x0800-0x081F + XX * 0x1000
    200 	 *
    201 	 * 8013: POS register 3: (adf pos1)
    202 	 * 7 6 5 4 3 2 1 0
    203 	 * | X 0 | \_____/
    204 	 * |     |       \__ Shared RAM Base Address
    205 	 * |     |                0xc0000 + ((xx & 0x0f) * 0x2000)
    206 	 * |      \____________ Ram size: 0 = 8k, 1 = 16k
    207 	 * |                      some size and address combinations are
    208 	 * |                        not supported, varies by card :(
    209 	 *  \__________________ If set, add 0xf00000 to shared RAM base addr
    210 	 *                      puts shared RAM near top of 16MB address space
    211 	 *
    212 	 * 8013: POS register 5: (adf pos3)
    213 	 * 7 6 5 4 3 2 1 0
    214 	 *         \_/ \_/
    215 	 *           \   \__ Media Select: 00=TwPr (no link) 10=BNC
    216 	 *            \          01=AUI|AUI or 10BaseT
    217 	 *             \____ Interrupt Level: 00=3 01=4 10=10 11=14
    218 	 */
    219 
    220 	wep = we_mca_lookup(ma->ma_id);
    221 #ifdef DIAGNOSTIC
    222 	if (wep == NULL) {
    223 		aprint_error("\n%s: where did the card go?\n",
    224 		    device_xname(self));
    225 		return;
    226 	}
    227 #endif
    228 
    229 	if (wep->we_flag & WD_8003) {
    230 		/* WD8003 based */
    231 		iobase = 0x200 + (((pos2 & 0x0e) >> 1) * 0x020);
    232 		maddr  = 0xC0000 + (((pos3 & 0x1c) >> 2) * 0x04000);
    233 		irq = we_mca_irq[(pos5 & 0x03)];
    234 		sc->mem_size = 16384;
    235 	} else {
    236 		/* SMC Elite */
    237 		iobase = 0x800 + (((pos2 & 0xf0) >> 4) * 0x1000);
    238 		maddr = 0xC0000 + ((pos3 & 0x0f) * 0x2000)
    239 		    + ((pos3 & 0x80) ? 0xF00000 : 0);
    240 		irq = we_mca_irq[(pos5 & 0x0c) >> 2];
    241 		sc->mem_size = (pos3 & 0x10) ? 16384 : 8192;
    242 	}
    243 
    244 	nict = asict = ma->ma_iot;
    245 	memt = ma->ma_memt;
    246 
    247 	aprint_normal(" slot %d port %#x-%#x mem %#x-%#x irq %d: %s\n",
    248 		ma->ma_slot + 1,
    249 		iobase, iobase + WE_NPORTS - 1,
    250 		maddr, maddr + sc->mem_size - 1,
    251 		irq, wep->we_name);
    252 
    253 	/* Map the device. */
    254 	if (bus_space_map(asict, iobase, WE_NPORTS, 0, &asich)) {
    255 		aprint_error_dev(self, "can't map nic i/o space\n");
    256 		return;
    257 	}
    258 
    259 	if (bus_space_subregion(asict, asich, WE_NIC_OFFSET, WE_NIC_NPORTS,
    260 	    &nich)) {
    261 		aprint_error_dev(self, "can't subregion i/o space\n");
    262 		return;
    263 	}
    264 
    265 	wsc->sc_type = wep->we_type;
    266 	/* all cards we support are 16bit native (no need for reset) */
    267 	wsc->sc_flags = WE_16BIT_ENABLE|WE_16BIT_NOTOGGLE;
    268 	sc->is790 = 0;
    269 	typestr = wep->we_typestr;
    270 
    271 	/*
    272 	 * Map memory space.
    273 	 */
    274 	if (bus_space_map(memt, maddr, sc->mem_size, 0, &memh)) {
    275 		aprint_error_dev(self, "can't map shared memory %#x-%#x\n",
    276 		    maddr, maddr + sc->mem_size - 1);
    277 		return;
    278 	}
    279 
    280 	wsc->sc_asict = asict;
    281 	wsc->sc_asich = asich;
    282 
    283 	sc->sc_regt = nict;
    284 	sc->sc_regh = nich;
    285 
    286 	sc->sc_buft = memt;
    287 	sc->sc_bufh = memh;
    288 
    289 	wsc->sc_maddr = maddr;
    290 
    291 	/* Interface is always enabled. */
    292 	sc->sc_enabled = 1;
    293 
    294 	wsc->sc_init_hook = we_mca_init_hook;
    295 
    296 	if (we_config(self, wsc, typestr))
    297 		return;
    298 
    299 	/* Map and establish the interrupt. */
    300 	wsc->sc_ih = mca_intr_establish(ma->ma_mc, irq,
    301 			    IPL_NET, dp8390_intr, sc);
    302 	if (wsc->sc_ih == NULL) {
    303 		aprint_error_dev(self, "can't establish interrupt\n");
    304 		return;
    305 	}
    306 }
    307 
    308 void
    309 we_mca_init_hook(struct we_softc *wsc)
    310 {
    311 	/*
    312 	 * This quirk really needs to be here, at least for WD8003W/A. Without
    313 	 * this, the card doesn't send any interrupts in 16bit mode. The quirk
    314 	 * was taken from Linux smc-mca.c driver.
    315 	 * I do not know why it's necessary. I don't want to know. It works
    316 	 * and that is enough for me.
    317 	 */
    318 	bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_LAAR, 0x04);
    319 	wsc->sc_laar_proto |= 0x04;
    320 }
    321