Home | History | Annotate | Line # | Download | only in mca
if_ep_mca.c revision 1.5
      1 /*	$NetBSD: if_ep_mca.c,v 1.5 2001/08/18 00:41:38 chs Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jaromir Dolecek.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1997 Jonathan Stone <jonathan (at) NetBSD.org>
     41  * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) beer.org>
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *      This product includes software developed by Herb Peyerl.
     55  * 4. The name of Herb Peyerl may not be used to endorse or promote products
     56  *    derived from this software without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     61  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     62  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     63  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     64  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     65  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     66  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     67  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     68  */
     69 
     70 /*
     71  * Driver for 3Com 3c529 cards.
     72  *
     73  * If you encouter sucky performance, try kernel without DEBUG/DIAGNOSTIC.
     74  * This helped on my test machine to change the performance of the card
     75  * from like 5KB/s to like 800 KB/s.
     76  */
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/mbuf.h>
     81 #include <sys/socket.h>
     82 #include <sys/ioctl.h>
     83 #include <sys/errno.h>
     84 #include <sys/device.h>
     85 
     86 #include <net/if.h>
     87 #include <net/if_ether.h>
     88 #include <net/if_media.h>
     89 
     90 #include <machine/bus.h>
     91 
     92 #include <dev/mii/miivar.h>
     93 
     94 #include <dev/ic/elink3var.h>
     95 #include <dev/ic/elink3reg.h>
     96 
     97 #include <dev/mca/mcavar.h>
     98 #include <dev/mca/mcadevs.h>
     99 
    100 /*
    101  * MCA constants.
    102  */
    103 #define MCA_CBIO		0x200	/* Configuration Base IO Address */
    104 #define MCA_IOSZ		0x10	/* I/O space size */
    105 
    106 int ep_mca_match __P((struct device *, struct cfdata *, void *));
    107 void ep_mca_attach __P((struct device *, struct device *, void *));
    108 
    109 struct cfattach ep_mca_ca = {
    110 	sizeof(struct ep_softc), ep_mca_match, ep_mca_attach
    111 };
    112 
    113 const struct ep_mca_product {
    114 	u_int32_t	epp_prodid;	/* MCA product ID */
    115 	const char	*epp_name;	/* device name */
    116 } ep_mca_products[] = {
    117 	{ MCA_PRODUCT_3C529_TP,	"3c529-TP Ethernet Adapter" },
    118 	{ MCA_PRODUCT_3C529_TM, "3c529 Ethernet Adapter (test mode)" },
    119 	{ MCA_PRODUCT_3C529_2T, "3c529 Ethernet Adapter (10base2/T)" },
    120 	{ MCA_PRODUCT_3C529_T,	"3c529 Ethernet Adapter (10baseT)"   },
    121 
    122 	{ 0,			NULL },
    123 };
    124 
    125 static const struct ep_mca_product *ep_mca_lookup
    126     __P((const struct mca_attach_args *));
    127 
    128 static const struct ep_mca_product *
    129 ep_mca_lookup(ma)
    130 	const struct mca_attach_args *ma;
    131 {
    132 	const struct ep_mca_product *epp;
    133 
    134 	for (epp = ep_mca_products; epp->epp_name != NULL; epp++)
    135 		if (ma->ma_id == epp->epp_prodid)
    136 			return (epp);
    137 
    138 	return (NULL);
    139 }
    140 
    141 int
    142 ep_mca_match(parent, match, aux)
    143 	struct device *parent;
    144 	struct cfdata *match;
    145 	void *aux;
    146 {
    147 	struct mca_attach_args *ma = (struct mca_attach_args *) aux;
    148 
    149 	if (ep_mca_lookup(ma) != NULL)
    150 		return (1);
    151 
    152 	return (0);
    153 }
    154 
    155 void
    156 ep_mca_attach(parent, self, aux)
    157 	struct device *parent, *self;
    158 	void *aux;
    159 {
    160 	struct ep_softc *sc = (void *)self;
    161 	struct mca_attach_args *ma = aux;
    162 	bus_space_handle_t ioh;
    163 	int pos4, pos5, iobase, irq, media;
    164 	const struct ep_mca_product *epp;
    165 
    166 	pos4 = mca_conf_read(ma->ma_mc, ma->ma_slot, 4);
    167 	pos5 = mca_conf_read(ma->ma_mc, ma->ma_slot, 5);
    168 
    169 	/*
    170 	 * POS register 2: (adf pos0)
    171 	 * 7 6 5 4 3 2 1 0
    172 	 *               \__ enable: 0=adapter disabled, 1=adapter enabled
    173 	 *
    174 	 * POS register 3: (adf pos1)
    175 	 *
    176 	 * 7 6 5 4 3 2 1 0
    177 	 * \________/
    178 	 *          \_______ Boot ROM Address Range: 0=disabled
    179 	 *                     X=0xc2000-0xc3fff + (x * 0x2000)
    180 	 *
    181 	 * POS register 4: (adf pos2)
    182 	 *
    183 	 * 7 6 5 4 3 2 1 0
    184 	 * \________/  \_/
    185 	 *          \    \__ Transceiver Type: 00=on-board (RJ45), 01=ext(AUI)
    186 	 *           \______ I/O Address Range: 0x200-0x20f + ((x>>2) * 0x400)
    187 	 *
    188 	 * POS register 5: (adf pos3)
    189 	 *
    190 	 * 7 6 5 4 3 2 1 0
    191 	 *          \____/
    192 	 *               \__ Interrupt level
    193 	 */
    194 
    195 	iobase = MCA_CBIO + (((pos4 & 0xfc) >> 2) * 0x400);
    196 	irq = (pos5 & 0x0f);
    197 
    198 	/* map the pio registers */
    199 	if (bus_space_map(ma->ma_iot, iobase, MCA_IOSZ, 0, &ioh)) {
    200 		printf("%s: unable to map i/o space\n", sc->sc_dev.dv_xname);
    201 		return;
    202 	}
    203 
    204 	sc->sc_iot = ma->ma_iot;
    205 	sc->sc_ioh = ioh;
    206 
    207 	epp = ep_mca_lookup(ma);
    208 	if (epp == NULL) {
    209 		printf("\n");
    210 		panic("ep_mca_attach: impossible");
    211 	}
    212 
    213 	printf(" slot %d irq %d: 3Com %s\n", ma->ma_slot + 1,
    214 		irq, epp->epp_name);
    215 
    216 	sc->enable = NULL;
    217 	sc->disable = NULL;
    218 	sc->enabled = 1;
    219 
    220 	sc->bustype = ELINK_BUS_MCA;
    221 	sc->ep_flags = 0;
    222 
    223 	if (epconfig(sc, ELINK_CHIPSET_3C509, NULL))
    224 		return;
    225 
    226 	/* Map and establish the interrupt. */
    227 	sc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_NET, epintr, sc);
    228 	if (sc->sc_ih == NULL) {
    229 		printf("%s: couldn't establish interrupt handler\n",
    230 		    sc->sc_dev.dv_xname);
    231 		return;
    232 	}
    233 
    234 	/*
    235 	 * Set default media to be same as the one selected in POS.
    236 	 */
    237 	switch (pos4 & 0x03) {
    238 	case 0x00:
    239 		/* on-board RJ-45 */
    240 		media = IFM_10_T;
    241 		break;
    242 	case 0x01:
    243 		/* external AUI */
    244 		media = IFM_10_5;
    245 		break;
    246 	case 0x02:
    247 		/* undefined, should never be set */
    248 		media = IFM_NONE;
    249 		break;
    250 	case 0x03:
    251 		/* BNC */
    252 		media = IFM_10_2;
    253 		break;
    254 	}
    255 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|media);
    256 }
    257