xref: /netbsd-src/sys/dev/mca/if_we_mca.c (revision 5a996cdd32543ed8a722c3fc1cf4c0d5b21907cb)
1*5a996cddSjdolecek /*	$NetBSD: if_we_mca.c,v 1.22 2021/04/08 17:36:33 jdolecek Exp $	*/
275b7bea5Sjdolecek 
375b7bea5Sjdolecek /*-
4ba9261baSjdolecek  * Copyright (c) 1997, 1998, 2001 The NetBSD Foundation, Inc.
575b7bea5Sjdolecek  * All rights reserved.
675b7bea5Sjdolecek  *
775b7bea5Sjdolecek  * This code is derived from software contributed to The NetBSD Foundation
875b7bea5Sjdolecek  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
975b7bea5Sjdolecek  * NASA Ames Research Center, and Jaromir Dolecek.
1075b7bea5Sjdolecek  *
1175b7bea5Sjdolecek  * Redistribution and use in source and binary forms, with or without
1275b7bea5Sjdolecek  * modification, are permitted provided that the following conditions
1375b7bea5Sjdolecek  * are met:
1475b7bea5Sjdolecek  * 1. Redistributions of source code must retain the above copyright
1575b7bea5Sjdolecek  *    notice, this list of conditions and the following disclaimer.
1675b7bea5Sjdolecek  * 2. Redistributions in binary form must reproduce the above copyright
1775b7bea5Sjdolecek  *    notice, this list of conditions and the following disclaimer in the
1875b7bea5Sjdolecek  *    documentation and/or other materials provided with the distribution.
1975b7bea5Sjdolecek  *
2075b7bea5Sjdolecek  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2175b7bea5Sjdolecek  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2275b7bea5Sjdolecek  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2375b7bea5Sjdolecek  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2475b7bea5Sjdolecek  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2575b7bea5Sjdolecek  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2675b7bea5Sjdolecek  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2775b7bea5Sjdolecek  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2875b7bea5Sjdolecek  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2975b7bea5Sjdolecek  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3075b7bea5Sjdolecek  * POSSIBILITY OF SUCH DAMAGE.
3175b7bea5Sjdolecek  */
3275b7bea5Sjdolecek 
3375b7bea5Sjdolecek /*
3475b7bea5Sjdolecek  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
3575b7bea5Sjdolecek  * adapters.
3675b7bea5Sjdolecek  *
3775b7bea5Sjdolecek  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
3875b7bea5Sjdolecek  *
3975b7bea5Sjdolecek  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
4075b7bea5Sjdolecek  * copied, distributed, and sold, in both source and binary form provided that
4175b7bea5Sjdolecek  * the above copyright and these terms are retained.  Under no circumstances is
4275b7bea5Sjdolecek  * the author responsible for the proper functioning of this software, nor does
4375b7bea5Sjdolecek  * the author assume any responsibility for damages incurred with its use.
4475b7bea5Sjdolecek  */
4575b7bea5Sjdolecek 
4675b7bea5Sjdolecek /*
4775b7bea5Sjdolecek  * Device driver for the Western Digital/SMC 8003 and 8013 series,
4875b7bea5Sjdolecek  * and the SMC Elite Ultra (8216).
4975b7bea5Sjdolecek  *
50f5f3fd55Sjdolecek  * Currently only tested with WD8003W/A and EtherCard PLUS Elite 10T/A
51f5f3fd55Sjdolecek  * (8013WP/A). Other WD8003 and SMC Elite based cards should work
52f5f3fd55Sjdolecek  * without problems too.
5375b7bea5Sjdolecek  */
5475b7bea5Sjdolecek 
558b7bb912Slukem #include <sys/cdefs.h>
56*5a996cddSjdolecek __KERNEL_RCSID(0, "$NetBSD: if_we_mca.c,v 1.22 2021/04/08 17:36:33 jdolecek Exp $");
578b7bb912Slukem 
5875b7bea5Sjdolecek #include <sys/param.h>
5975b7bea5Sjdolecek #include <sys/systm.h>
6075b7bea5Sjdolecek #include <sys/device.h>
6175b7bea5Sjdolecek #include <sys/socket.h>
6275b7bea5Sjdolecek #include <sys/mbuf.h>
6375b7bea5Sjdolecek 
6475b7bea5Sjdolecek #include <net/if.h>
6575b7bea5Sjdolecek #include <net/if_types.h>
6675b7bea5Sjdolecek #include <net/if_media.h>
6775b7bea5Sjdolecek #include <net/if_ether.h>
6875b7bea5Sjdolecek 
69a2a38285Sad #include <sys/bus.h>
7075b7bea5Sjdolecek 
7175b7bea5Sjdolecek #include <dev/mca/mcareg.h>
7275b7bea5Sjdolecek #include <dev/mca/mcavar.h>
7375b7bea5Sjdolecek #include <dev/mca/mcadevs.h>
7475b7bea5Sjdolecek 
7575b7bea5Sjdolecek #include <dev/ic/dp8390reg.h>
7675b7bea5Sjdolecek #include <dev/ic/dp8390var.h>
7775b7bea5Sjdolecek #include <dev/ic/wereg.h>
7875b7bea5Sjdolecek #include <dev/ic/wevar.h>
7975b7bea5Sjdolecek 
8075b7bea5Sjdolecek #define WD_8003		0x01
8175b7bea5Sjdolecek #define WD_ELITE	0x02
8275b7bea5Sjdolecek 
83cf80f73dScube int we_mca_probe(device_t, cfdata_t , void *);
84cf80f73dScube void we_mca_attach(device_t, device_t, void *);
8518db93c7Sperry void we_mca_init_hook(struct we_softc *);
8675b7bea5Sjdolecek 
87cf80f73dScube CFATTACH_DECL_NEW(we_mca, sizeof(struct we_softc),
88c9b3657cSthorpej     we_mca_probe, we_mca_attach, NULL, NULL);
8975b7bea5Sjdolecek 
9075b7bea5Sjdolecek /*
9175b7bea5Sjdolecek  * The types for some cards may not be correct; hopefully it's close
9275b7bea5Sjdolecek  * enough.
9375b7bea5Sjdolecek  */
9475b7bea5Sjdolecek static const struct we_mca_product {
9575b7bea5Sjdolecek 	u_int32_t we_id;
9675b7bea5Sjdolecek 	const char *we_name;
9775b7bea5Sjdolecek 	int we_flag;
9875b7bea5Sjdolecek 	int we_type;
9975b7bea5Sjdolecek 	const char *we_typestr;
10075b7bea5Sjdolecek } we_mca_products[] = {
10175b7bea5Sjdolecek 	{ MCA_PRODUCT_WD_8013EP, "EtherCard PLUS Elite/A (8013EP/A)",
10275b7bea5Sjdolecek 		WD_ELITE,	WE_TYPE_WD8013EP, "WD8013EP/A" },
10375b7bea5Sjdolecek 	{ MCA_PRODUCT_WD_8013WP, "EtherCard PLUS Elite 10T/A (8013WP/A)",
104f5f3fd55Sjdolecek 		WD_ELITE,	WE_TYPE_WD8013EP, "WD8013WP/A" },
10575b7bea5Sjdolecek 	{ MCA_PRODUCT_IBM_WD_2,"IBM PS/2 Adapter/A for Ethernet Networks (UTP)",
106f2d3769fSjdolecek 		WD_ELITE, WE_TYPE_WD8013EP, "WD8013WP/A"},
10775b7bea5Sjdolecek 	{ MCA_PRODUCT_IBM_WD_T,"IBM PS/2 Adapter/A for Ethernet Networks (BNC)",
108f2d3769fSjdolecek 		WD_ELITE, WE_TYPE_WD8013EP, "WD8013WP/A"},
10975b7bea5Sjdolecek 	{ MCA_PRODUCT_WD_8003E,	"WD EtherCard PLUS/A (WD8003E/A or WD8003ET/A)",
11075b7bea5Sjdolecek 		WD_8003,	WE_TYPE_WD8003E, "WD8003E/A or WD8003ET/A" },
11175b7bea5Sjdolecek 	{ MCA_PRODUCT_WD_8003ST,"WD StarCard PLUS/A (WD8003ST/A)",
11275b7bea5Sjdolecek 		WD_8003,	WE_TYPE_WD8003W, "WD8003ST/A" }, /* XXX */
11375b7bea5Sjdolecek 	{ MCA_PRODUCT_WD_8003W,	"WD EtherCard PLUS 10T/A (WD8003W/A)",
11475b7bea5Sjdolecek 		WD_8003,	WE_TYPE_WD8003W, "WD8003W/A" },
11575b7bea5Sjdolecek 	{ MCA_PRODUCT_IBM_WD_O, "IBM PS/2 Adapter/A for Ethernet Networks",
116f2d3769fSjdolecek 		WD_8003,	WE_TYPE_WD8003W, "IBM PS/2 Adapter/A" },
117514b3fccSchristos 	{ 0x0000,		NULL,
118514b3fccSchristos 		0,		0,		 NULL },
11975b7bea5Sjdolecek };
12075b7bea5Sjdolecek 
12175b7bea5Sjdolecek /* see POS description in we_mca_attach() */
12275b7bea5Sjdolecek static const int we_mca_irq[] = {
12375b7bea5Sjdolecek 	3, 4, 10, 15,
12475b7bea5Sjdolecek };
12575b7bea5Sjdolecek 
12618db93c7Sperry static const struct we_mca_product *we_mca_lookup(int);
12775b7bea5Sjdolecek 
12875b7bea5Sjdolecek static const struct we_mca_product *
we_mca_lookup(int id)129cf80f73dScube we_mca_lookup(int id)
13075b7bea5Sjdolecek {
13175b7bea5Sjdolecek 	const struct we_mca_product *wep;
13275b7bea5Sjdolecek 
13375b7bea5Sjdolecek 	for(wep = we_mca_products; wep->we_name; wep++)
13475b7bea5Sjdolecek 		if (wep->we_id == id)
13575b7bea5Sjdolecek 			return (wep);
13675b7bea5Sjdolecek 
13775b7bea5Sjdolecek 	return (NULL);
13875b7bea5Sjdolecek }
13975b7bea5Sjdolecek 
14075b7bea5Sjdolecek int
we_mca_probe(device_t parent,cfdata_t cf,void * aux)141cf80f73dScube we_mca_probe(device_t parent, cfdata_t cf, void *aux)
14275b7bea5Sjdolecek {
14375b7bea5Sjdolecek 	struct mca_attach_args *ma = aux;
14475b7bea5Sjdolecek 
14575b7bea5Sjdolecek 	return (we_mca_lookup(ma->ma_id) != NULL);
14675b7bea5Sjdolecek }
14775b7bea5Sjdolecek 
14875b7bea5Sjdolecek void
we_mca_attach(device_t parent,device_t self,void * aux)149cf80f73dScube we_mca_attach(device_t parent, device_t self, void *aux)
15075b7bea5Sjdolecek {
151838ee1e0Sthorpej 	struct we_softc *wsc = device_private(self);
15275b7bea5Sjdolecek 	struct dp8390_softc *sc = &wsc->sc_dp8390;
15375b7bea5Sjdolecek 	struct mca_attach_args *ma = aux;
15475b7bea5Sjdolecek 	const struct we_mca_product *wep;
15575b7bea5Sjdolecek 	bus_space_tag_t nict, asict, memt;
15675b7bea5Sjdolecek 	bus_space_handle_t nich, asich, memh;
15775b7bea5Sjdolecek 	const char *typestr;
15875b7bea5Sjdolecek 	int irq, iobase, maddr;
15975b7bea5Sjdolecek 	int pos2, pos3, pos5;
16075b7bea5Sjdolecek 
161cf80f73dScube 	sc->sc_dev = self;
162cf80f73dScube 
16375b7bea5Sjdolecek 	pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
16475b7bea5Sjdolecek 	pos3 = mca_conf_read(ma->ma_mc, ma->ma_slot, 3);
16575b7bea5Sjdolecek 	pos5 = mca_conf_read(ma->ma_mc, ma->ma_slot, 5);
16675b7bea5Sjdolecek 
16775b7bea5Sjdolecek 	/*
168f5f3fd55Sjdolecek 	 * POS registers differ a lot between 8003 and 8013, so they are
16975b7bea5Sjdolecek 	 * divided to two sections.
17075b7bea5Sjdolecek 	 *
17175b7bea5Sjdolecek 	 * 8003: POS register 2: (adf pos0)
17275b7bea5Sjdolecek 	 * 7 6 5 4 3 2 1 0
173eefdd936Sjdolecek 	 * 0 0 1 \_____/ \__ enable: 0=adapter disabled, 1=adapter enabled
174eefdd936Sjdolecek 	 *             \____ Adapter I/O Space: 0x200-0x21F + XX*0x20
17575b7bea5Sjdolecek 	 *
17675b7bea5Sjdolecek 	 * 8003: POS register 3: (adf pos1)
17775b7bea5Sjdolecek 	 * 7 6 5 4 3 2 1 0
178eefdd936Sjdolecek 	 * 1 1 0 \___/ 1 X
17975b7bea5Sjdolecek 	 *           \______ Shared Ram Space (16K Bytes):
18075b7bea5Sjdolecek 	 *                     0xC0000-0xC3FFF + XX * 0x4000
18175b7bea5Sjdolecek 	 *
18275b7bea5Sjdolecek 	 * 8003: POS register 5: (adf pos3)
18375b7bea5Sjdolecek 	 * 7 6 5 4 3 2 1 0
18475b7bea5Sjdolecek 	 *             \_/
18575b7bea5Sjdolecek 	 *               \__ Interrupt Level: 00=3 01=4 10=10 11=15
18675b7bea5Sjdolecek 	 *
18775b7bea5Sjdolecek 	 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
18875b7bea5Sjdolecek 	 *
18975b7bea5Sjdolecek 	 * 8013: POS register 2: (adf pos0)
19075b7bea5Sjdolecek 	 * 7 6 5 4 3 2 1 0
19175b7bea5Sjdolecek 	 * \____/        \__ enable: 0=adapter disabled, 1=adapter enabled
19275b7bea5Sjdolecek 	 *      \___________ Adapter I/O Space: 0x0800-0x081F + XX * 0x1000
19375b7bea5Sjdolecek 	 *
19475b7bea5Sjdolecek 	 * 8013: POS register 3: (adf pos1)
19575b7bea5Sjdolecek 	 * 7 6 5 4 3 2 1 0
196f2d3769fSjdolecek 	 * | X 0 | \_____/
197f2d3769fSjdolecek 	 * |     |       \__ Shared RAM Base Address
198f2d3769fSjdolecek 	 * |     |                0xc0000 + ((xx & 0x0f) * 0x2000)
199f2d3769fSjdolecek 	 * |      \____________ Ram size: 0 = 8k, 1 = 16k
200f2d3769fSjdolecek 	 * |                      some size and address combinations are
201f2d3769fSjdolecek 	 * |                        not supported, varies by card :(
202f2d3769fSjdolecek 	 *  \__________________ If set, add 0xf00000 to shared RAM base addr
203f2d3769fSjdolecek 	 *                      puts shared RAM near top of 16MB address space
20475b7bea5Sjdolecek 	 *
20575b7bea5Sjdolecek 	 * 8013: POS register 5: (adf pos3)
20675b7bea5Sjdolecek 	 * 7 6 5 4 3 2 1 0
20775b7bea5Sjdolecek 	 *         \_/ \_/
20875b7bea5Sjdolecek 	 *           \   \__ Media Select: 00=TwPr (no link) 10=BNC
20975b7bea5Sjdolecek 	 *            \          01=AUI|AUI or 10BaseT
21075b7bea5Sjdolecek 	 *             \____ Interrupt Level: 00=3 01=4 10=10 11=14
21175b7bea5Sjdolecek 	 */
21275b7bea5Sjdolecek 
21375b7bea5Sjdolecek 	wep = we_mca_lookup(ma->ma_id);
21475b7bea5Sjdolecek #ifdef DIAGNOSTIC
21575b7bea5Sjdolecek 	if (wep == NULL) {
216cf80f73dScube 		aprint_error("\n%s: where did the card go?\n",
217cf80f73dScube 		    device_xname(self));
21875b7bea5Sjdolecek 		return;
21975b7bea5Sjdolecek 	}
22075b7bea5Sjdolecek #endif
22175b7bea5Sjdolecek 
22275b7bea5Sjdolecek 	if (wep->we_flag & WD_8003) {
22375b7bea5Sjdolecek 		/* WD8003 based */
22475b7bea5Sjdolecek 		iobase = 0x200 + (((pos2 & 0x0e) >> 1) * 0x020);
22575b7bea5Sjdolecek 		maddr  = 0xC0000 + (((pos3 & 0x1c) >> 2) * 0x04000);
22675b7bea5Sjdolecek 		irq = we_mca_irq[(pos5 & 0x03)];
227f2d3769fSjdolecek 		sc->mem_size = 16384;
22875b7bea5Sjdolecek 	} else {
22975b7bea5Sjdolecek 		/* SMC Elite */
23075b7bea5Sjdolecek 		iobase = 0x800 + (((pos2 & 0xf0) >> 4) * 0x1000);
231f2d3769fSjdolecek 		maddr = 0xC0000 + ((pos3 & 0x0f) * 0x2000)
232f2d3769fSjdolecek 		    + ((pos3 & 0x80) ? 0xF00000 : 0);
233f5f3fd55Sjdolecek 		irq = we_mca_irq[(pos5 & 0x0c) >> 2];
234f2d3769fSjdolecek 		sc->mem_size = (pos3 & 0x10) ? 16384 : 8192;
23575b7bea5Sjdolecek 	}
23675b7bea5Sjdolecek 
23775b7bea5Sjdolecek 	nict = asict = ma->ma_iot;
23875b7bea5Sjdolecek 	memt = ma->ma_memt;
23975b7bea5Sjdolecek 
240cf80f73dScube 	aprint_normal(" slot %d port %#x-%#x mem %#x-%#x irq %d: %s\n",
241f5f3fd55Sjdolecek 		ma->ma_slot + 1,
242f5f3fd55Sjdolecek 		iobase, iobase + WE_NPORTS - 1,
243f5f3fd55Sjdolecek 		maddr, maddr + sc->mem_size - 1,
244f5f3fd55Sjdolecek 		irq, wep->we_name);
24575b7bea5Sjdolecek 
24675b7bea5Sjdolecek 	/* Map the device. */
24775b7bea5Sjdolecek 	if (bus_space_map(asict, iobase, WE_NPORTS, 0, &asich)) {
248cf80f73dScube 		aprint_error_dev(self, "can't map nic i/o space\n");
24975b7bea5Sjdolecek 		return;
25075b7bea5Sjdolecek 	}
25175b7bea5Sjdolecek 
25275b7bea5Sjdolecek 	if (bus_space_subregion(asict, asich, WE_NIC_OFFSET, WE_NIC_NPORTS,
25375b7bea5Sjdolecek 	    &nich)) {
254cf80f73dScube 		aprint_error_dev(self, "can't subregion i/o space\n");
25575b7bea5Sjdolecek 		return;
25675b7bea5Sjdolecek 	}
25775b7bea5Sjdolecek 
25875b7bea5Sjdolecek 	wsc->sc_type = wep->we_type;
259fc38d3f3Sjdolecek 	/* all cards we support are 16bit native (no need for reset) */
260fc38d3f3Sjdolecek 	wsc->sc_flags = WE_16BIT_ENABLE|WE_16BIT_NOTOGGLE;
26175b7bea5Sjdolecek 	sc->is790 = 0;
26275b7bea5Sjdolecek 	typestr = wep->we_typestr;
26375b7bea5Sjdolecek 
26475b7bea5Sjdolecek 	/*
26575b7bea5Sjdolecek 	 * Map memory space.
26675b7bea5Sjdolecek 	 */
26775b7bea5Sjdolecek 	if (bus_space_map(memt, maddr, sc->mem_size, 0, &memh)) {
268cf80f73dScube 		aprint_error_dev(self, "can't map shared memory %#x-%#x\n",
269f5f3fd55Sjdolecek 		    maddr, maddr + sc->mem_size - 1);
27075b7bea5Sjdolecek 		return;
27175b7bea5Sjdolecek 	}
27275b7bea5Sjdolecek 
27375b7bea5Sjdolecek 	wsc->sc_asict = asict;
27475b7bea5Sjdolecek 	wsc->sc_asich = asich;
27575b7bea5Sjdolecek 
27675b7bea5Sjdolecek 	sc->sc_regt = nict;
27775b7bea5Sjdolecek 	sc->sc_regh = nich;
27875b7bea5Sjdolecek 
27975b7bea5Sjdolecek 	sc->sc_buft = memt;
28075b7bea5Sjdolecek 	sc->sc_bufh = memh;
28175b7bea5Sjdolecek 
28275b7bea5Sjdolecek 	wsc->sc_maddr = maddr;
28375b7bea5Sjdolecek 
28475b7bea5Sjdolecek 	/* Interface is always enabled. */
28575b7bea5Sjdolecek 	sc->sc_enabled = 1;
28675b7bea5Sjdolecek 
28775b7bea5Sjdolecek 	wsc->sc_init_hook = we_mca_init_hook;
28875b7bea5Sjdolecek 
28975b7bea5Sjdolecek 	if (we_config(self, wsc, typestr))
29075b7bea5Sjdolecek 		return;
29175b7bea5Sjdolecek 
29275b7bea5Sjdolecek 	/* Map and establish the interrupt. */
29375b7bea5Sjdolecek 	wsc->sc_ih = mca_intr_establish(ma->ma_mc, irq,
29475b7bea5Sjdolecek 			    IPL_NET, dp8390_intr, sc);
29575b7bea5Sjdolecek 	if (wsc->sc_ih == NULL) {
296cf80f73dScube 		aprint_error_dev(self, "can't establish interrupt\n");
29775b7bea5Sjdolecek 		return;
29875b7bea5Sjdolecek 	}
29975b7bea5Sjdolecek }
30075b7bea5Sjdolecek 
30175b7bea5Sjdolecek void
we_mca_init_hook(struct we_softc * wsc)302cf80f73dScube we_mca_init_hook(struct we_softc *wsc)
30375b7bea5Sjdolecek {
30475b7bea5Sjdolecek 	/*
305*5a996cddSjdolecek 	 * Enable interrupts. This is required for at least WD8003W/A, without
306*5a996cddSjdolecek 	 * this, the card doesn't send any interrupts in 16bit mode. The code
30775b7bea5Sjdolecek 	 * was taken from Linux smc-mca.c driver.
30875b7bea5Sjdolecek 	 */
30975b7bea5Sjdolecek 	bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_LAAR, 0x04);
31075b7bea5Sjdolecek 	wsc->sc_laar_proto |= 0x04;
31175b7bea5Sjdolecek }
312