Home | History | Annotate | Line # | Download | only in pcmcia
if_ne_pcmcia.c revision 1.18
      1 /*	$NetBSD: if_ne_pcmcia.c,v 1.18 1998/09/20 19:19:53 dbj Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Marc Horowitz.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/select.h>
     35 #include <sys/device.h>
     36 #include <sys/socket.h>
     37 
     38 #include <net/if_types.h>
     39 #include <net/if.h>
     40 #include <net/if_media.h>
     41 #include <net/if_ether.h>
     42 
     43 #include <machine/bus.h>
     44 #include <machine/intr.h>
     45 
     46 #include <dev/pcmcia/pcmciareg.h>
     47 #include <dev/pcmcia/pcmciavar.h>
     48 #include <dev/pcmcia/pcmciadevs.h>
     49 
     50 #include <dev/ic/dp8390reg.h>
     51 #include <dev/ic/dp8390var.h>
     52 
     53 #include <dev/ic/ne2000reg.h>
     54 #include <dev/ic/ne2000var.h>
     55 
     56 int ne_pcmcia_match __P((struct device *, struct cfdata *, void *));
     57 void ne_pcmcia_attach __P((struct device *, struct device *, void *));
     58 
     59 int	ne_pcmcia_enable __P((struct dp8390_softc *));
     60 void	ne_pcmcia_disable __P((struct dp8390_softc *));
     61 
     62 struct ne_pcmcia_softc {
     63 	struct ne2000_softc sc_ne2000;		/* real "ne2000" softc */
     64 
     65 	/* PCMCIA-specific goo */
     66 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o information */
     67 	int sc_asic_io_window;			/* i/o window for ASIC */
     68 	int sc_nic_io_window;			/* i/o window for NIC */
     69 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     70 	void *sc_ih;				/* interrupt handle */
     71 };
     72 
     73 struct cfattach ne_pcmcia_ca = {
     74 	sizeof(struct ne_pcmcia_softc), ne_pcmcia_match, ne_pcmcia_attach
     75 };
     76 
     77 struct ne2000dev {
     78     char *name;
     79     int32_t manufacturer;
     80     int32_t product;
     81     char *cis_info[4];
     82     int function;
     83     int enet_maddr;
     84     unsigned char enet_vendor[3];
     85 } ne2000devs[] = {
     86     { PCMCIA_STR_PREMAX_PE200,
     87       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
     88       PCMCIA_CIS_PREMAX_PE200,
     89       0, 0x07f0, { 0x00, 0x20, 0xe0 } },
     90 
     91     { PCMCIA_STR_IBM_INFOMOVER,
     92       PCMCIA_VENDOR_IBM, PCMCIA_PRODUCT_IBM_INFOMOVER,
     93       PCMCIA_CIS_IBM_INFOMOVER,
     94       0, 0x0ff0, { 0x08, 0x00, 0x5a } },
     95 
     96     { PCMCIA_STR_DIGITAL_DEPCMXX,
     97       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
     98       PCMCIA_CIS_DIGITAL_DEPCMXX,
     99       0, 0x0ff0, { 0x00, 0x00, 0xe8 } },
    100 
    101     { PCMCIA_STR_LINKSYS_ECARD,
    102       PCMCIA_VENDOR_LINKSYS, PCMCIA_PRODUCT_LINKSYS_ECARD,
    103       PCMCIA_CIS_LINKSYS_ECARD,
    104       0, -1, { 0x00, 0x80, 0xc8 } },
    105 
    106     { PCMCIA_STR_LINKSYS_COMBO_ECARD,
    107       PCMCIA_VENDOR_LINKSYS, PCMCIA_PRODUCT_LINKSYS_COMBO_ECARD,
    108       PCMCIA_CIS_LINKSYS_COMBO_ECARD,
    109       0, -1, { 0x00, 0x80, 0xc8 } },
    110 
    111     { PCMCIA_STR_LINKSYS_TRUST_COMBO_ECARD,
    112       PCMCIA_VENDOR_LINKSYS, PCMCIA_PRODUCT_LINKSYS_TRUST_COMBO_ECARD,
    113       PCMCIA_CIS_LINKSYS_TRUST_COMBO_ECARD,
    114       0, 0x0120, { 0x20, 0x04, 0x49 } },
    115 
    116     { PCMCIA_STR_PLANET_SMARTCOM2000,
    117       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    118       PCMCIA_CIS_PLANET_SMARTCOM2000,
    119       0, 0xff0, { 0x00, 0x00, 0xe8 } },
    120 
    121     { PCMCIA_STR_DLINK_DE650,
    122       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    123       PCMCIA_CIS_DLINK_DE650,
    124       0, 0x0040, { 0x00, 0x80, 0xc8 } },
    125 
    126     { PCMCIA_STR_DLINK_DE660,
    127       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    128       PCMCIA_CIS_DLINK_DE660,
    129       0, -1, { 0x00, 0x80, 0xc8 } },
    130 
    131     { PCMCIA_STR_IODATA_PCLAT,
    132       PCMCIA_VENDOR_IODATA, PCMCIA_PRODUCT_IODATA_PCLAT,
    133       PCMCIA_CIS_IODATA_PCLAT,
    134       /* two possible location, 0x01c0 or 0x0ff0 */
    135       0, -1, { 0x00, 0xa0, 0xb0 } },
    136 
    137     { PCMCIA_STR_DAYNA_COMMUNICARD_E_1,
    138       PCMCIA_VENDOR_DAYNA, PCMCIA_PRODUCT_DAYNA_COMMUNICARD_E_1,
    139       PCMCIA_CIS_DAYNA_COMMUNICARD_E_1,
    140       0, 0x0110, { 0x00, 0x80, 0x19 } },
    141 
    142     { PCMCIA_STR_DAYNA_COMMUNICARD_E_2,
    143       PCMCIA_VENDOR_DAYNA, PCMCIA_PRODUCT_DAYNA_COMMUNICARD_E_2,
    144       PCMCIA_CIS_DAYNA_COMMUNICARD_E_2,
    145       0, -1, { 0x00, 0x80, 0x19 } },
    146 
    147     { PCMCIA_STR_RPTI_EP401,
    148       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    149       PCMCIA_CIS_RPTI_EP401,
    150       0, -1, { 0x00, 0x40, 0x95 } },
    151 
    152     { PCMCIA_STR_ACCTON_EN2212,
    153       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    154       PCMCIA_CIS_ACCTON_EN2212,
    155       0, 0x0ff0, { 0x00, 0x00, 0xe8 } },
    156 #if 0
    157     /* the rest of these are stolen from the linux pcnet pcmcia device
    158        driver.  Since I don't know the manfid or cis info strings for
    159        any of them, they're not compiled in until I do. */
    160     { "Allied Telesis LA-PCM",
    161       0x0000, 0x0000, NULL, NULL, 0,
    162       0x0ff0, { 0x00, 0x00, 0xf4 } },
    163     { "APEX MultiCard",
    164       0x0000, 0x0000, NULL, NULL, 0,
    165       0x03f4, { 0x00, 0x20, 0xe5 } },
    166     { "ASANTE FriendlyNet",
    167       0x0000, 0x0000, NULL, NULL, 0,
    168       0x4910, { 0x00, 0x00, 0x94 } },
    169     { "Danpex EN-6200P2",
    170       0x0000, 0x0000, NULL, NULL, 0,
    171       0x0110, { 0x00, 0x40, 0xc7 } },
    172     { "DataTrek NetCard",
    173       0x0000, 0x0000, NULL, NULL, 0,
    174       0x0ff0, { 0x00, 0x20, 0xe8 } },
    175     { "Dayna CommuniCard E",
    176       0x0000, 0x0000, NULL, NULL, 0,
    177       0x0110, { 0x00, 0x80, 0x19 } },
    178     { "EP-210 Ethernet",
    179       0x0000, 0x0000, NULL, NULL, 0,
    180       0x0110, { 0x00, 0x40, 0x33 } },
    181     { "Epson EEN10B",
    182       0x0000, 0x0000, NULL, NULL, 0,
    183       0x0ff0, { 0x00, 0x00, 0x48 } },
    184     { "ELECOM Laneed LD-CDWA",
    185       0x0000, 0x0000, NULL, NULL, 0,
    186       0x00b8, { 0x08, 0x00, 0x42 } },
    187     { "Grey Cell GCS2220",
    188       0x0000, 0x0000, NULL, NULL, 0,
    189       0x0000, { 0x00, 0x47, 0x43 } },
    190     { "Hypertec Ethernet",
    191       0x0000, 0x0000, NULL, NULL, 0,
    192       0x01c0, { 0x00, 0x40, 0x4c } },
    193     { "IBM CCAE",
    194       0x0000, 0x0000, NULL, NULL, 0,
    195       0x0ff0, { 0x08, 0x00, 0x5a } },
    196     { "IBM CCAE",
    197       0x0000, 0x0000, NULL, NULL, 0,
    198       0x0ff0, { 0x00, 0x04, 0xac } },
    199     { "IBM CCAE",
    200       0x0000, 0x0000, NULL, NULL, 0,
    201       0x0ff0, { 0x00, 0x06, 0x29 } },
    202     { "IBM FME",
    203       0x0000, 0x0000, NULL, NULL, 0,
    204       0x0374, { 0x00, 0x04, 0xac } },
    205     { "IBM FME",
    206       0x0000, 0x0000, NULL, NULL, 0,
    207       0x0374, { 0x08, 0x00, 0x5a } },
    208     { "Katron PE-520",
    209       0x0000, 0x0000, NULL, NULL, 0,
    210       0x0110, { 0x00, 0x40, 0xf6 } },
    211     { "Kingston KNE-PCM/x",
    212       0x0000, 0x0000, NULL, NULL, 0,
    213       0x0ff0, { 0x00, 0xc0, 0xf0 } },
    214     { "Kingston KNE-PCM/x",
    215       0x0000, 0x0000, NULL, NULL, 0,
    216       0x0ff0, { 0xe2, 0x0c, 0x0f } },
    217     { "Kingston KNE-PC2",
    218       0x0000, 0x0000, NULL, NULL, 0,
    219       0x0180, { 0x00, 0xc0, 0xf0 } },
    220     { "Longshine LCS-8534",
    221       0x0000, 0x0000, NULL, NULL, 0,
    222       0x0000, { 0x08, 0x00, 0x00 } },
    223     { "Maxtech PCN2000",
    224       0x0000, 0x0000, NULL, NULL, 0,
    225       0x5000, { 0x00, 0x00, 0xe8 } },
    226     { "NDC Instant-Link",
    227       0x0000, 0x0000, NULL, NULL, 0,
    228       0x003a, { 0x00, 0x80, 0xc6 } },
    229     { "NE2000 Compatible",
    230       0x0000, 0x0000, NULL, NULL, 0,
    231       0x0ff0, { 0x00, 0xa0, 0x0c } },
    232     { "Network General Sniffer",
    233       0x0000, 0x0000, NULL, NULL, 0,
    234       0x0ff0, { 0x00, 0x00, 0x65 } },
    235     { "Panasonic VEL211",
    236       0x0000, 0x0000, NULL, NULL, 0,
    237       0x0ff0, { 0x00, 0x80, 0x45 } },
    238     { "SCM Ethernet",
    239       0x0000, 0x0000, NULL, NULL, 0,
    240       0x0ff0, { 0x00, 0x20, 0xcb } },
    241     { "Socket EA",
    242       0x0000, 0x0000, NULL, NULL, 0,
    243       0x4000, { 0x00, 0xc0, 0x1b } },
    244     { "Volktek NPL-402CT",
    245       0x0000, 0x0000, NULL, NULL, 0,
    246       0x0060, { 0x00, 0x40, 0x05 } },
    247 #endif
    248 };
    249 
    250 #define	NE2000_NDEVS	(sizeof(ne2000devs) / sizeof(ne2000devs[0]))
    251 
    252 #define ne2000_match(card, fct, n) \
    253 ((((((card)->manufacturer != PCMCIA_VENDOR_INVALID) && \
    254     ((card)->manufacturer == ne2000devs[(n)].manufacturer) && \
    255     ((card)->product != PCMCIA_PRODUCT_INVALID) && \
    256     ((card)->product == ne2000devs[(n)].product)) || \
    257    ((ne2000devs[(n)].cis_info[0]) && (ne2000devs[(n)].cis_info[1]) && \
    258     (strcmp((card)->cis1_info[0], ne2000devs[(n)].cis_info[0]) == 0) && \
    259     (strcmp((card)->cis1_info[1], ne2000devs[(n)].cis_info[1]) == 0))) && \
    260   ((fct) == ne2000devs[(n)].function))? \
    261  &ne2000devs[(n)]:NULL)
    262 
    263 int
    264 ne_pcmcia_match(parent, match, aux)
    265 	struct device *parent;
    266 	struct cfdata *match;
    267 	void *aux;
    268 {
    269 	struct pcmcia_attach_args *pa = aux;
    270 	int i;
    271 
    272 	for (i = 0; i < NE2000_NDEVS; i++) {
    273 		if (ne2000_match(pa->card, pa->pf->number, i))
    274 			return (1);
    275 	}
    276 
    277 	return (0);
    278 }
    279 
    280 void
    281 ne_pcmcia_attach(parent, self, aux)
    282 	struct device *parent, *self;
    283 	void *aux;
    284 {
    285 	struct ne_pcmcia_softc *psc = (void *) self;
    286 	struct ne2000_softc *nsc = &psc->sc_ne2000;
    287 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
    288 	struct pcmcia_attach_args *pa = aux;
    289 	struct pcmcia_config_entry *cfe;
    290 	struct ne2000dev *ne_dev;
    291 	struct pcmcia_mem_handle pcmh;
    292 	bus_addr_t offset;
    293 	int i, j, mwindow;
    294 	u_int8_t myea[6], *enaddr = NULL;
    295 
    296 	psc->sc_pf = pa->pf;
    297 	cfe = pa->pf->cfe_head.sqh_first;
    298 
    299 #if 0
    300 	/*
    301 	 * Some ne2000 driver's claim to have memory; others don't.
    302 	 * Since I don't care, I don't check.
    303 	 */
    304 
    305 	if (cfe->num_memspace != 1) {
    306 		printf(": unexpected number of memory spaces "
    307 		    " %d should be 1\n", cfe->num_memspace);
    308 		return;
    309 	}
    310 #endif
    311 
    312 	if (cfe->num_iospace == 1) {
    313 		if (cfe->iospace[0].length != NE2000_NPORTS) {
    314 			printf(": unexpected I/O space configuration\n");
    315 			return;
    316 		}
    317 	} else if (cfe->num_iospace == 2) {
    318 		/*
    319 		 * Some cards report a separate space for NIC and ASIC.
    320 		 * This make some sense, but we must allocate a single
    321 		 * NE2000_NPORTS-sized chunk, due to brain damaged
    322 		 * address decoders on some of these cards.
    323 		 */
    324 		if ((cfe->iospace[0].length + cfe->iospace[1].length) !=
    325 		    NE2000_NPORTS) {
    326 			printf(": unexpected I/O space configuration\n");
    327 			return;
    328 		}
    329 	} else {
    330 		printf(": unexpected number of i/o spaces %d"
    331 		    " should be 1 or 2\n", cfe->num_iospace);
    332 	}
    333 
    334 	if (pcmcia_io_alloc(pa->pf, 0, NE2000_NPORTS, NE2000_NPORTS,
    335 	    &psc->sc_pcioh)) {
    336 		printf(": can't alloc i/o space\n");
    337 		return;
    338 	}
    339 
    340 	dsc->sc_regt = psc->sc_pcioh.iot;
    341 	dsc->sc_regh = psc->sc_pcioh.ioh;
    342 
    343 	nsc->sc_asict = psc->sc_pcioh.iot;
    344 	if (bus_space_subregion(dsc->sc_regt, dsc->sc_regh,
    345 	    NE2000_ASIC_OFFSET, NE2000_ASIC_NPORTS,
    346 	    &nsc->sc_asich)) {
    347 		printf(": can't get subregion for asic\n");
    348 		return;
    349 	}
    350 
    351 	/* Set up power management hooks. */
    352 	dsc->sc_enable = ne_pcmcia_enable;
    353 	dsc->sc_disable = ne_pcmcia_disable;
    354 
    355 	/* Enable the card. */
    356 	pcmcia_function_init(pa->pf, cfe);
    357 	if (pcmcia_function_enable(pa->pf)) {
    358 		printf(": function enable failed\n");
    359 		return;
    360 	}
    361 
    362 	/* some cards claim to be io16, but they're lying. */
    363 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO8,
    364 	    NE2000_NIC_OFFSET, NE2000_NIC_NPORTS,
    365 	    &psc->sc_pcioh, &psc->sc_nic_io_window)) {
    366 		printf(": can't map NIC i/o space\n");
    367 		return;
    368 	}
    369 
    370 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16,
    371 	    NE2000_ASIC_OFFSET, NE2000_ASIC_NPORTS,
    372 	    &psc->sc_pcioh, &psc->sc_asic_io_window)) {
    373 		printf(": can't map ASIC i/o space\n");
    374 		return;
    375 	}
    376 
    377 	printf("\n");
    378 
    379 	/*
    380 	 * Read the station address from the board.
    381 	 */
    382 	for (i = 0; i < NE2000_NDEVS; i++) {
    383 		if ((ne_dev = ne2000_match(pa->card, pa->pf->number, i))
    384 		    != NULL) {
    385 			if (ne_dev->enet_maddr >= 0) {
    386 				if (pcmcia_mem_alloc(pa->pf,
    387 				    ETHER_ADDR_LEN * 2, &pcmh)) {
    388 					printf("%s: can't alloc mem for"
    389 					    " enet addr\n",
    390 					    dsc->sc_dev.dv_xname);
    391 					return;
    392 				}
    393 				if (pcmcia_mem_map(pa->pf, PCMCIA_MEM_ATTR,
    394 				    ne_dev->enet_maddr, ETHER_ADDR_LEN * 2,
    395 				    &pcmh, &offset, &mwindow)) {
    396 					printf("%s: can't map mem for"
    397 					    " enet addr\n",
    398 					    dsc->sc_dev.dv_xname);
    399 					return;
    400 				}
    401 				for (j = 0; j < ETHER_ADDR_LEN; j++)
    402 					myea[j] = bus_space_read_1(pcmh.memt,
    403 					    pcmh.memh, offset + (j * 2));
    404 				pcmcia_mem_unmap(pa->pf, mwindow);
    405 				pcmcia_mem_free(pa->pf, &pcmh);
    406 				enaddr = myea;
    407 			}
    408 			break;
    409 		}
    410 	}
    411 
    412 	if (enaddr != NULL) {
    413 		/*
    414 		 * Make sure this is what we expect.
    415 		 */
    416 		if (enaddr[0] != ne_dev->enet_vendor[0] ||
    417 		    enaddr[1] != ne_dev->enet_vendor[1] ||
    418 		    enaddr[2] != ne_dev->enet_vendor[2]) {
    419 			printf("%s: enet addr has incorrect vendor code\n",
    420 			    dsc->sc_dev.dv_xname);
    421 			printf("%s: (%02x:%02x:%02x should be "
    422 			    "%02x:%02x:%02x)\n", dsc->sc_dev.dv_xname,
    423 			    enaddr[0], enaddr[1], enaddr[2],
    424 			    ne_dev->enet_vendor[0],
    425 			    ne_dev->enet_vendor[1],
    426 			    ne_dev->enet_vendor[2]);
    427 			return;
    428 		}
    429 	}
    430 
    431 	printf("%s: %s Ethernet\n", dsc->sc_dev.dv_xname, ne_dev->name);
    432 
    433 	ne2000_attach(nsc, enaddr);
    434 
    435 	pcmcia_function_disable(pa->pf);
    436 }
    437 
    438 int
    439 ne_pcmcia_enable(dsc)
    440 	struct dp8390_softc *dsc;
    441 {
    442 	struct ne_pcmcia_softc *psc = (struct ne_pcmcia_softc *)dsc;
    443 
    444 	/* set up the interrupt */
    445 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, dp8390_intr,
    446 	    dsc);
    447 	if (psc->sc_ih == NULL) {
    448 		printf("%s: couldn't establish interrupt\n",
    449 		    dsc->sc_dev.dv_xname);
    450 		return (1);
    451 	}
    452 
    453 	return (pcmcia_function_enable(psc->sc_pf));
    454 }
    455 
    456 void
    457 ne_pcmcia_disable(dsc)
    458 	struct dp8390_softc *dsc;
    459 {
    460 	struct ne_pcmcia_softc *psc = (struct ne_pcmcia_softc *)dsc;
    461 
    462 	pcmcia_function_disable(psc->sc_pf);
    463 
    464 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    465 }
    466