Home | History | Annotate | Line # | Download | only in pcmcia
if_ne_pcmcia.c revision 1.19
      1 /*	$NetBSD: if_ne_pcmcia.c,v 1.19 1998/09/23 21:22:41 veego 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_DIGITAL_DEPCMXX,
     92       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
     93       PCMCIA_CIS_DIGITAL_DEPCMXX,
     94       0, 0x0ff0, { 0x00, 0x00, 0xe8 } },
     95 
     96     { PCMCIA_STR_PLANET_SMARTCOM2000,
     97       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
     98       PCMCIA_CIS_PLANET_SMARTCOM2000,
     99       0, 0xff0, { 0x00, 0x00, 0xe8 } },
    100 
    101     { PCMCIA_STR_DLINK_DE650,
    102       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    103       PCMCIA_CIS_DLINK_DE650,
    104       0, 0x0040, { 0x00, 0x80, 0xc8 } },
    105 
    106     { PCMCIA_STR_DLINK_DE660,
    107       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    108       PCMCIA_CIS_DLINK_DE660,
    109       0, -1, { 0x00, 0x80, 0xc8 } },
    110 
    111     { PCMCIA_STR_RPTI_EP401,
    112       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    113       PCMCIA_CIS_RPTI_EP401,
    114       0, -1, { 0x00, 0x40, 0x95 } },
    115 
    116     { PCMCIA_STR_ACCTON_EN2212,
    117       PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    118       PCMCIA_CIS_ACCTON_EN2212,
    119       0, 0x0ff0, { 0x00, 0x00, 0xe8 } },
    120 
    121     /*
    122      * You have to add new entries which contains
    123      * PCMCIA_VENDOR_INVALID and/or PCMCIA_PRODUCT_INVALID
    124      * in front of this comment.
    125      *
    126      * There are cards which use a generic vendor and product id but needs
    127      * a different handling depending on the cis_info, so ne2000_match
    128      * needs a table where the exceptions comes first and then the normal
    129      * product and vendor entries.
    130      */
    131 
    132     { PCMCIA_STR_IBM_INFOMOVER,
    133       PCMCIA_VENDOR_IBM, PCMCIA_PRODUCT_IBM_INFOMOVER,
    134       PCMCIA_CIS_IBM_INFOMOVER,
    135       0, 0x0ff0, { 0x08, 0x00, 0x5a } },
    136 
    137     { PCMCIA_STR_LINKSYS_ECARD,
    138       PCMCIA_VENDOR_LINKSYS, PCMCIA_PRODUCT_LINKSYS_ECARD,
    139       PCMCIA_CIS_LINKSYS_ECARD,
    140       0, -1, { 0x00, 0x80, 0xc8 } },
    141 
    142     { PCMCIA_STR_LINKSYS_COMBO_ECARD,
    143       PCMCIA_VENDOR_LINKSYS, PCMCIA_PRODUCT_LINKSYS_COMBO_ECARD,
    144       PCMCIA_CIS_LINKSYS_COMBO_ECARD,
    145       0, -1, { 0x00, 0x80, 0xc8 } },
    146 
    147     { PCMCIA_STR_LINKSYS_TRUST_COMBO_ECARD,
    148       PCMCIA_VENDOR_LINKSYS, PCMCIA_PRODUCT_LINKSYS_TRUST_COMBO_ECARD,
    149       PCMCIA_CIS_LINKSYS_TRUST_COMBO_ECARD,
    150       0, 0x0120, { 0x20, 0x04, 0x49 } },
    151 
    152     { PCMCIA_STR_IODATA_PCLAT,
    153       PCMCIA_VENDOR_IODATA, PCMCIA_PRODUCT_IODATA_PCLAT,
    154       PCMCIA_CIS_IODATA_PCLAT,
    155       /* two possible location, 0x01c0 or 0x0ff0 */
    156       0, -1, { 0x00, 0xa0, 0xb0 } },
    157 
    158     { PCMCIA_STR_DAYNA_COMMUNICARD_E_1,
    159       PCMCIA_VENDOR_DAYNA, PCMCIA_PRODUCT_DAYNA_COMMUNICARD_E_1,
    160       PCMCIA_CIS_DAYNA_COMMUNICARD_E_1,
    161       0, 0x0110, { 0x00, 0x80, 0x19 } },
    162 
    163     { PCMCIA_STR_DAYNA_COMMUNICARD_E_2,
    164       PCMCIA_VENDOR_DAYNA, PCMCIA_PRODUCT_DAYNA_COMMUNICARD_E_2,
    165       PCMCIA_CIS_DAYNA_COMMUNICARD_E_2,
    166       0, -1, { 0x00, 0x80, 0x19 } },
    167 #if 0
    168     /* the rest of these are stolen from the linux pcnet pcmcia device
    169        driver.  Since I don't know the manfid or cis info strings for
    170        any of them, they're not compiled in until I do. */
    171     { "Allied Telesis LA-PCM",
    172       0x0000, 0x0000, NULL, NULL, 0,
    173       0x0ff0, { 0x00, 0x00, 0xf4 } },
    174     { "APEX MultiCard",
    175       0x0000, 0x0000, NULL, NULL, 0,
    176       0x03f4, { 0x00, 0x20, 0xe5 } },
    177     { "ASANTE FriendlyNet",
    178       0x0000, 0x0000, NULL, NULL, 0,
    179       0x4910, { 0x00, 0x00, 0x94 } },
    180     { "Danpex EN-6200P2",
    181       0x0000, 0x0000, NULL, NULL, 0,
    182       0x0110, { 0x00, 0x40, 0xc7 } },
    183     { "DataTrek NetCard",
    184       0x0000, 0x0000, NULL, NULL, 0,
    185       0x0ff0, { 0x00, 0x20, 0xe8 } },
    186     { "Dayna CommuniCard E",
    187       0x0000, 0x0000, NULL, NULL, 0,
    188       0x0110, { 0x00, 0x80, 0x19 } },
    189     { "EP-210 Ethernet",
    190       0x0000, 0x0000, NULL, NULL, 0,
    191       0x0110, { 0x00, 0x40, 0x33 } },
    192     { "Epson EEN10B",
    193       0x0000, 0x0000, NULL, NULL, 0,
    194       0x0ff0, { 0x00, 0x00, 0x48 } },
    195     { "ELECOM Laneed LD-CDWA",
    196       0x0000, 0x0000, NULL, NULL, 0,
    197       0x00b8, { 0x08, 0x00, 0x42 } },
    198     { "Grey Cell GCS2220",
    199       0x0000, 0x0000, NULL, NULL, 0,
    200       0x0000, { 0x00, 0x47, 0x43 } },
    201     { "Hypertec Ethernet",
    202       0x0000, 0x0000, NULL, NULL, 0,
    203       0x01c0, { 0x00, 0x40, 0x4c } },
    204     { "IBM CCAE",
    205       0x0000, 0x0000, NULL, NULL, 0,
    206       0x0ff0, { 0x08, 0x00, 0x5a } },
    207     { "IBM CCAE",
    208       0x0000, 0x0000, NULL, NULL, 0,
    209       0x0ff0, { 0x00, 0x04, 0xac } },
    210     { "IBM CCAE",
    211       0x0000, 0x0000, NULL, NULL, 0,
    212       0x0ff0, { 0x00, 0x06, 0x29 } },
    213     { "IBM FME",
    214       0x0000, 0x0000, NULL, NULL, 0,
    215       0x0374, { 0x00, 0x04, 0xac } },
    216     { "IBM FME",
    217       0x0000, 0x0000, NULL, NULL, 0,
    218       0x0374, { 0x08, 0x00, 0x5a } },
    219     { "Katron PE-520",
    220       0x0000, 0x0000, NULL, NULL, 0,
    221       0x0110, { 0x00, 0x40, 0xf6 } },
    222     { "Kingston KNE-PCM/x",
    223       0x0000, 0x0000, NULL, NULL, 0,
    224       0x0ff0, { 0x00, 0xc0, 0xf0 } },
    225     { "Kingston KNE-PCM/x",
    226       0x0000, 0x0000, NULL, NULL, 0,
    227       0x0ff0, { 0xe2, 0x0c, 0x0f } },
    228     { "Kingston KNE-PC2",
    229       0x0000, 0x0000, NULL, NULL, 0,
    230       0x0180, { 0x00, 0xc0, 0xf0 } },
    231     { "Longshine LCS-8534",
    232       0x0000, 0x0000, NULL, NULL, 0,
    233       0x0000, { 0x08, 0x00, 0x00 } },
    234     { "Maxtech PCN2000",
    235       0x0000, 0x0000, NULL, NULL, 0,
    236       0x5000, { 0x00, 0x00, 0xe8 } },
    237     { "NDC Instant-Link",
    238       0x0000, 0x0000, NULL, NULL, 0,
    239       0x003a, { 0x00, 0x80, 0xc6 } },
    240     { "NE2000 Compatible",
    241       0x0000, 0x0000, NULL, NULL, 0,
    242       0x0ff0, { 0x00, 0xa0, 0x0c } },
    243     { "Network General Sniffer",
    244       0x0000, 0x0000, NULL, NULL, 0,
    245       0x0ff0, { 0x00, 0x00, 0x65 } },
    246     { "Panasonic VEL211",
    247       0x0000, 0x0000, NULL, NULL, 0,
    248       0x0ff0, { 0x00, 0x80, 0x45 } },
    249     { "SCM Ethernet",
    250       0x0000, 0x0000, NULL, NULL, 0,
    251       0x0ff0, { 0x00, 0x20, 0xcb } },
    252     { "Socket EA",
    253       0x0000, 0x0000, NULL, NULL, 0,
    254       0x4000, { 0x00, 0xc0, 0x1b } },
    255     { "Volktek NPL-402CT",
    256       0x0000, 0x0000, NULL, NULL, 0,
    257       0x0060, { 0x00, 0x40, 0x05 } },
    258 #endif
    259 };
    260 
    261 #define	NE2000_NDEVS	(sizeof(ne2000devs) / sizeof(ne2000devs[0]))
    262 
    263 #define ne2000_match(card, fct, n) \
    264 ((((((card)->manufacturer != PCMCIA_VENDOR_INVALID) && \
    265     ((card)->manufacturer == ne2000devs[(n)].manufacturer) && \
    266     ((card)->product != PCMCIA_PRODUCT_INVALID) && \
    267     ((card)->product == ne2000devs[(n)].product)) || \
    268    ((ne2000devs[(n)].cis_info[0]) && (ne2000devs[(n)].cis_info[1]) && \
    269     (strcmp((card)->cis1_info[0], ne2000devs[(n)].cis_info[0]) == 0) && \
    270     (strcmp((card)->cis1_info[1], ne2000devs[(n)].cis_info[1]) == 0))) && \
    271   ((fct) == ne2000devs[(n)].function))? \
    272  &ne2000devs[(n)]:NULL)
    273 
    274 int
    275 ne_pcmcia_match(parent, match, aux)
    276 	struct device *parent;
    277 	struct cfdata *match;
    278 	void *aux;
    279 {
    280 	struct pcmcia_attach_args *pa = aux;
    281 	int i;
    282 
    283 	for (i = 0; i < NE2000_NDEVS; i++) {
    284 		if (ne2000_match(pa->card, pa->pf->number, i))
    285 			return (1);
    286 	}
    287 
    288 	return (0);
    289 }
    290 
    291 void
    292 ne_pcmcia_attach(parent, self, aux)
    293 	struct device *parent, *self;
    294 	void *aux;
    295 {
    296 	struct ne_pcmcia_softc *psc = (void *) self;
    297 	struct ne2000_softc *nsc = &psc->sc_ne2000;
    298 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
    299 	struct pcmcia_attach_args *pa = aux;
    300 	struct pcmcia_config_entry *cfe;
    301 	struct ne2000dev *ne_dev;
    302 	struct pcmcia_mem_handle pcmh;
    303 	bus_addr_t offset;
    304 	int i, j, mwindow;
    305 	u_int8_t myea[6], *enaddr = NULL;
    306 
    307 	psc->sc_pf = pa->pf;
    308 	cfe = pa->pf->cfe_head.sqh_first;
    309 
    310 #if 0
    311 	/*
    312 	 * Some ne2000 driver's claim to have memory; others don't.
    313 	 * Since I don't care, I don't check.
    314 	 */
    315 
    316 	if (cfe->num_memspace != 1) {
    317 		printf(": unexpected number of memory spaces "
    318 		    " %d should be 1\n", cfe->num_memspace);
    319 		return;
    320 	}
    321 #endif
    322 
    323 	if (cfe->num_iospace == 1) {
    324 		if (cfe->iospace[0].length != NE2000_NPORTS) {
    325 			printf(": unexpected I/O space configuration\n");
    326 			return;
    327 		}
    328 	} else if (cfe->num_iospace == 2) {
    329 		/*
    330 		 * Some cards report a separate space for NIC and ASIC.
    331 		 * This make some sense, but we must allocate a single
    332 		 * NE2000_NPORTS-sized chunk, due to brain damaged
    333 		 * address decoders on some of these cards.
    334 		 */
    335 		if ((cfe->iospace[0].length + cfe->iospace[1].length) !=
    336 		    NE2000_NPORTS) {
    337 			printf(": unexpected I/O space configuration\n");
    338 			return;
    339 		}
    340 	} else {
    341 		printf(": unexpected number of i/o spaces %d"
    342 		    " should be 1 or 2\n", cfe->num_iospace);
    343 	}
    344 
    345 	if (pcmcia_io_alloc(pa->pf, 0, NE2000_NPORTS, NE2000_NPORTS,
    346 	    &psc->sc_pcioh)) {
    347 		printf(": can't alloc i/o space\n");
    348 		return;
    349 	}
    350 
    351 	dsc->sc_regt = psc->sc_pcioh.iot;
    352 	dsc->sc_regh = psc->sc_pcioh.ioh;
    353 
    354 	nsc->sc_asict = psc->sc_pcioh.iot;
    355 	if (bus_space_subregion(dsc->sc_regt, dsc->sc_regh,
    356 	    NE2000_ASIC_OFFSET, NE2000_ASIC_NPORTS,
    357 	    &nsc->sc_asich)) {
    358 		printf(": can't get subregion for asic\n");
    359 		return;
    360 	}
    361 
    362 	/* Set up power management hooks. */
    363 	dsc->sc_enable = ne_pcmcia_enable;
    364 	dsc->sc_disable = ne_pcmcia_disable;
    365 
    366 	/* Enable the card. */
    367 	pcmcia_function_init(pa->pf, cfe);
    368 	if (pcmcia_function_enable(pa->pf)) {
    369 		printf(": function enable failed\n");
    370 		return;
    371 	}
    372 
    373 	/* some cards claim to be io16, but they're lying. */
    374 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO8,
    375 	    NE2000_NIC_OFFSET, NE2000_NIC_NPORTS,
    376 	    &psc->sc_pcioh, &psc->sc_nic_io_window)) {
    377 		printf(": can't map NIC i/o space\n");
    378 		return;
    379 	}
    380 
    381 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16,
    382 	    NE2000_ASIC_OFFSET, NE2000_ASIC_NPORTS,
    383 	    &psc->sc_pcioh, &psc->sc_asic_io_window)) {
    384 		printf(": can't map ASIC i/o space\n");
    385 		return;
    386 	}
    387 
    388 	printf("\n");
    389 
    390 	/*
    391 	 * Read the station address from the board.
    392 	 */
    393 	for (i = 0; i < NE2000_NDEVS; i++) {
    394 		if ((ne_dev = ne2000_match(pa->card, pa->pf->number, i))
    395 		    != NULL) {
    396 			if (ne_dev->enet_maddr >= 0) {
    397 				if (pcmcia_mem_alloc(pa->pf,
    398 				    ETHER_ADDR_LEN * 2, &pcmh)) {
    399 					printf("%s: can't alloc mem for"
    400 					    " enet addr\n",
    401 					    dsc->sc_dev.dv_xname);
    402 					return;
    403 				}
    404 				if (pcmcia_mem_map(pa->pf, PCMCIA_MEM_ATTR,
    405 				    ne_dev->enet_maddr, ETHER_ADDR_LEN * 2,
    406 				    &pcmh, &offset, &mwindow)) {
    407 					printf("%s: can't map mem for"
    408 					    " enet addr\n",
    409 					    dsc->sc_dev.dv_xname);
    410 					return;
    411 				}
    412 				for (j = 0; j < ETHER_ADDR_LEN; j++)
    413 					myea[j] = bus_space_read_1(pcmh.memt,
    414 					    pcmh.memh, offset + (j * 2));
    415 				pcmcia_mem_unmap(pa->pf, mwindow);
    416 				pcmcia_mem_free(pa->pf, &pcmh);
    417 				enaddr = myea;
    418 			}
    419 			break;
    420 		}
    421 	}
    422 
    423 	if (enaddr != NULL) {
    424 		/*
    425 		 * Make sure this is what we expect.
    426 		 */
    427 		if (enaddr[0] != ne_dev->enet_vendor[0] ||
    428 		    enaddr[1] != ne_dev->enet_vendor[1] ||
    429 		    enaddr[2] != ne_dev->enet_vendor[2]) {
    430 			printf("%s: enet addr has incorrect vendor code\n",
    431 			    dsc->sc_dev.dv_xname);
    432 			printf("%s: (%02x:%02x:%02x should be "
    433 			    "%02x:%02x:%02x)\n", dsc->sc_dev.dv_xname,
    434 			    enaddr[0], enaddr[1], enaddr[2],
    435 			    ne_dev->enet_vendor[0],
    436 			    ne_dev->enet_vendor[1],
    437 			    ne_dev->enet_vendor[2]);
    438 			return;
    439 		}
    440 	}
    441 
    442 	printf("%s: %s Ethernet\n", dsc->sc_dev.dv_xname, ne_dev->name);
    443 
    444 	ne2000_attach(nsc, enaddr);
    445 
    446 	pcmcia_function_disable(pa->pf);
    447 }
    448 
    449 int
    450 ne_pcmcia_enable(dsc)
    451 	struct dp8390_softc *dsc;
    452 {
    453 	struct ne_pcmcia_softc *psc = (struct ne_pcmcia_softc *)dsc;
    454 
    455 	/* set up the interrupt */
    456 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, dp8390_intr,
    457 	    dsc);
    458 	if (psc->sc_ih == NULL) {
    459 		printf("%s: couldn't establish interrupt\n",
    460 		    dsc->sc_dev.dv_xname);
    461 		return (1);
    462 	}
    463 
    464 	return (pcmcia_function_enable(psc->sc_pf));
    465 }
    466 
    467 void
    468 ne_pcmcia_disable(dsc)
    469 	struct dp8390_softc *dsc;
    470 {
    471 	struct ne_pcmcia_softc *psc = (struct ne_pcmcia_softc *)dsc;
    472 
    473 	pcmcia_function_disable(psc->sc_pf);
    474 
    475 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    476 }
    477