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