Home | History | Annotate | Line # | Download | only in pcmcia
if_ne_pcmcia.c revision 1.4.2.3
      1 /*	$NetBSD: if_ne_pcmcia.c,v 1.4.2.3 1997/12/18 22:31:04 perry 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 
     49 #include <dev/ic/dp8390reg.h>
     50 #include <dev/ic/dp8390var.h>
     51 
     52 #include <dev/ic/ne2000reg.h>
     53 #include <dev/ic/ne2000var.h>
     54 
     55 #ifdef __BROKEN_INDIRECT_CONFIG
     56 int ne_pcmcia_match __P((struct device *, void *, void *));
     57 #else
     58 int ne_pcmcia_match __P((struct device *, struct cfdata *, void *));
     59 #endif
     60 void ne_pcmcia_attach __P((struct device *, struct device *, void *));
     61 
     62 int	ne_pcmcia_enable __P((struct dp8390_softc *));
     63 void	ne_pcmcia_disable __P((struct dp8390_softc *));
     64 
     65 struct ne_pcmcia_softc {
     66 	struct ne2000_softc sc_ne2000;		/* real "ne2000" softc */
     67 
     68 	/* PCMCIA-specific goo */
     69 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o information */
     70 	int sc_asic_io_window;			/* i/o window for ASIC */
     71 	int sc_nic_io_window;			/* i/o window for NIC */
     72 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     73 	void *sc_ih;				/* interrupt handle */
     74 };
     75 
     76 struct cfattach ne_pcmcia_ca = {
     77 	sizeof(struct ne_pcmcia_softc), ne_pcmcia_match, ne_pcmcia_attach
     78 };
     79 
     80 struct ne2000dev {
     81     char *name;
     82     int32_t manufacturer;
     83     int32_t product;
     84     char *cis1_info0;
     85     char *cis1_info1;
     86     int function;
     87     int enet_maddr;
     88     unsigned char enet_vendor[3];
     89 } ne2000devs[] = {
     90     { "PreMax PE-200",
     91       0xffff, 0xffff, "PMX   ", "PE-200", 0,
     92       0x07f0, { 0x00, 0x20, 0xe0 } },
     93     { "National Semiconductor InfoMover",
     94       0x00a4, 0x0002, NULL, NULL, 0,
     95       0x0ff0, { 0x08, 0x00, 0x5a } },
     96     { "DEC DEPCM-BA",
     97       0x0000, 0x0000, "DIGITAL", "DEPCM-XX", 0,
     98       0x0ff0, { 0x00, 0x00, 0xe8 } },
     99     /* this card might need pcic_alloc_iobase < 0x400, and/or
    100        CIRRUS_PD672X on TI TravelMate 5000 needs it: */
    101     { "Linksys EthernetCard",
    102       0x0149, 0x0265, "LINKSYS", "E-CARD",
    103       0, -1, { 0x00, 0x80, 0xc8 } },
    104     { "Linksys Combo EthernetCard",
    105       0x0149, 0xc1ab, NULL, NULL,
    106       0, -1, { 0x00, 0x80, 0xc8 } },
    107     { "Planet SmartCOM 2000",
    108       /* This card doesn't have manufacturer and product id in CIS.  */
    109       PCMCIA_MANUFACTURER_INVALID, PCMCIA_PRODUCT_INVALID,
    110       "PCMCIA", "UE2212", 0,
    111       0xff0, { 0x00, 0x00, 0xe8 } },
    112     { "D-Link DE-650",
    113       PCMCIA_MANUFACTURER_INVALID, PCMCIA_PRODUCT_INVALID,
    114       "D-Link", "DE-650", 0,
    115       0x0040, { 0x00, 0x80, 0xc8 } },
    116 #if 0
    117     /* the rest of these are stolen from the linux pcnet pcmcia device
    118        driver.  Since I don't know the manfid or cis info strings for
    119        any of them, they're not compiled in until I do. */
    120 
    121     { "Accton EN2212",
    122       0x0000, 0x0000, NULL, NULL, 0,
    123       0x0ff0, { 0x00, 0x00, 0xe8 } },
    124     { "Allied Telesis LA-PCM",
    125       0x0000, 0x0000, NULL, NULL, 0,
    126       0x0ff0, { 0x00, 0x00, 0xf4 } },
    127     { "APEX MultiCard",
    128       0x0000, 0x0000, NULL, NULL, 0,
    129       0x03f4, { 0x00, 0x20, 0xe5 } },
    130     { "ASANTE FriendlyNet",
    131       0x0000, 0x0000, NULL, NULL, 0,
    132       0x4910, { 0x00, 0x00, 0x94 } },
    133     { "Danpex EN-6200P2",
    134       0x0000, 0x0000, NULL, NULL, 0,
    135       0x0110, { 0x00, 0x40, 0xc7 } },
    136     { "DataTrek NetCard",
    137       0x0000, 0x0000, NULL, NULL, 0,
    138       0x0ff0, { 0x00, 0x20, 0xe8 } },
    139     { "Dayna CommuniCard E",
    140       0x0000, 0x0000, NULL, NULL, 0,
    141       0x0110, { 0x00, 0x80, 0x19 } },
    142     { "EP-210 Ethernet",
    143       0x0000, 0x0000, NULL, NULL, 0,
    144       0x0110, { 0x00, 0x40, 0x33 } },
    145     { "Epson EEN10B",
    146       0x0000, 0x0000, NULL, NULL, 0,
    147       0x0ff0, { 0x00, 0x00, 0x48 } },
    148     { "ELECOM Laneed LD-CDWA",
    149       0x0000, 0x0000, NULL, NULL, 0,
    150       0x00b8, { 0x08, 0x00, 0x42 } },
    151     { "Grey Cell GCS2220",
    152       0x0000, 0x0000, NULL, NULL, 0,
    153       0x0000, { 0x00, 0x47, 0x43 } },
    154     { "Hypertec Ethernet",
    155       0x0000, 0x0000, NULL, NULL, 0,
    156       0x01c0, { 0x00, 0x40, 0x4c } },
    157     { "IBM CCAE",
    158       0x0000, 0x0000, NULL, NULL, 0,
    159       0x0ff0, { 0x08, 0x00, 0x5a } },
    160     { "IBM CCAE",
    161       0x0000, 0x0000, NULL, NULL, 0,
    162       0x0ff0, { 0x00, 0x04, 0xac } },
    163     { "IBM CCAE",
    164       0x0000, 0x0000, NULL, NULL, 0,
    165       0x0ff0, { 0x00, 0x06, 0x29 } },
    166     { "IBM FME",
    167       0x0000, 0x0000, NULL, NULL, 0,
    168       0x0374, { 0x00, 0x04, 0xac } },
    169     { "IBM FME",
    170       0x0000, 0x0000, NULL, NULL, 0,
    171       0x0374, { 0x08, 0x00, 0x5a } },
    172     { "I-O DATA PCLA/T",
    173       0x0000, 0x0000, NULL, NULL, 0,
    174       0x0ff0, { 0x00, 0xa0, 0xb0 } },
    175     { "Katron PE-520",
    176       0x0000, 0x0000, NULL, NULL, 0,
    177       0x0110, { 0x00, 0x40, 0xf6 } },
    178     { "Kingston KNE-PCM/x",
    179       0x0000, 0x0000, NULL, NULL, 0,
    180       0x0ff0, { 0x00, 0xc0, 0xf0 } },
    181     { "Kingston KNE-PCM/x",
    182       0x0000, 0x0000, NULL, NULL, 0,
    183       0x0ff0, { 0xe2, 0x0c, 0x0f } },
    184     { "Kingston KNE-PC2",
    185       0x0000, 0x0000, NULL, NULL, 0,
    186       0x0180, { 0x00, 0xc0, 0xf0 } },
    187     { "Longshine LCS-8534",
    188       0x0000, 0x0000, NULL, NULL, 0,
    189       0x0000, { 0x08, 0x00, 0x00 } },
    190     { "Maxtech PCN2000",
    191       0x0000, 0x0000, NULL, NULL, 0,
    192       0x5000, { 0x00, 0x00, 0xe8 } },
    193     { "NDC Instant-Link",
    194       0x0000, 0x0000, NULL, NULL, 0,
    195       0x003a, { 0x00, 0x80, 0xc6 } },
    196     { "NE2000 Compatible",
    197       0x0000, 0x0000, NULL, NULL, 0,
    198       0x0ff0, { 0x00, 0xa0, 0x0c } },
    199     { "Network General Sniffer",
    200       0x0000, 0x0000, NULL, NULL, 0,
    201       0x0ff0, { 0x00, 0x00, 0x65 } },
    202     { "Panasonic VEL211",
    203       0x0000, 0x0000, NULL, NULL, 0,
    204       0x0ff0, { 0x00, 0x80, 0x45 } },
    205     { "RPTI EP400",
    206       0x0000, 0x0000, NULL, NULL, 0,
    207       0x0110, { 0x00, 0x40, 0x95 } },
    208     { "SCM Ethernet",
    209       0x0000, 0x0000, NULL, NULL, 0,
    210       0x0ff0, { 0x00, 0x20, 0xcb } },
    211     { "Socket EA",
    212       0x0000, 0x0000, NULL, NULL, 0,
    213       0x4000, { 0x00, 0xc0, 0x1b } },
    214     { "Volktek NPL-402CT",
    215       0x0000, 0x0000, NULL, NULL, 0,
    216       0x0060, { 0x00, 0x40, 0x05 } },
    217 #endif
    218 };
    219 
    220 #define	NE2000_NDEVS	(sizeof(ne2000devs) / sizeof(ne2000devs[0]))
    221 
    222 #define ne2000_match(card, fct, n) \
    223 ((((((card)->manufacturer != PCMCIA_MANUFACTURER_INVALID) && \
    224     ((card)->manufacturer == ne2000devs[(n)].manufacturer) && \
    225     ((card)->product != PCMCIA_PRODUCT_INVALID) && \
    226     ((card)->product == ne2000devs[(n)].product)) || \
    227    ((ne2000devs[(n)].cis1_info0) && (ne2000devs[(n)].cis1_info1) && \
    228     (strcmp((card)->cis1_info[0], ne2000devs[(n)].cis1_info0) == 0) && \
    229     (strcmp((card)->cis1_info[1], ne2000devs[(n)].cis1_info1) == 0))) && \
    230   ((fct) == ne2000devs[(n)].function))? \
    231  &ne2000devs[(n)]:NULL)
    232 
    233 int
    234 ne_pcmcia_match(parent, match, aux)
    235 	struct device *parent;
    236 #ifdef __BROKEN_INDIRECT_CONFIG
    237 	void *match;
    238 #else
    239 	struct cfdata *cf;
    240 #endif
    241 	void *aux;
    242 {
    243 	struct pcmcia_attach_args *pa = aux;
    244 	int i;
    245 
    246 	for (i = 0; i < NE2000_NDEVS; i++) {
    247 		if (ne2000_match(pa->card, pa->pf->number, i))
    248 			return (1);
    249 	}
    250 
    251 	return (0);
    252 }
    253 
    254 void
    255 ne_pcmcia_attach(parent, self, aux)
    256 	struct device *parent, *self;
    257 	void *aux;
    258 {
    259 	struct ne_pcmcia_softc *psc = (void *) self;
    260 	struct ne2000_softc *nsc = &psc->sc_ne2000;
    261 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
    262 	struct pcmcia_attach_args *pa = aux;
    263 	struct pcmcia_config_entry *cfe;
    264 	struct ne2000dev *ne_dev;
    265 	struct pcmcia_mem_handle pcmh;
    266 	bus_addr_t offset;
    267 	int i, j, mwindow;
    268 	u_int8_t myea[6], *enaddr = NULL;
    269 
    270 	psc->sc_pf = pa->pf;
    271 	cfe = pa->pf->cfe_head.sqh_first;
    272 
    273 #if 0
    274 	/*
    275 	 * Some ne2000 driver's claim to have memory; others don't.
    276 	 * Since I don't care, I don't check.
    277 	 */
    278 
    279 	if (cfe->num_memspace != 1) {
    280 		printf(": unexpected number of memory spaces "
    281 		    " %d should be 1\n", cfe->num_memspace);
    282 		return;
    283 	}
    284 #endif
    285 
    286 	if (cfe->num_iospace == 1) {
    287 		if (cfe->iospace[0].length != NE2000_NPORTS) {
    288 			printf(": unexpected I/O space configuration\n");
    289 			return;
    290 		}
    291 	} else if (cfe->num_iospace == 2) {
    292 		/*
    293 		 * Some cards report a separate space for NIC and ASIC.
    294 		 * This make some sense, but we must allocate a single
    295 		 * NE2000_NPORTS-sized chunk, due to brain damaged
    296 		 * address decoders on some of these cards.
    297 		 */
    298 		if ((cfe->iospace[0].length + cfe->iospace[1].length) !=
    299 		    NE2000_NPORTS) {
    300 			printf(": unexpected I/O space configuration\n");
    301 			return;
    302 		}
    303 	} else {
    304 		printf(": unexpected number of i/o spaces %d"
    305 		    " should be 1 or 2\n", cfe->num_iospace);
    306 	}
    307 
    308 	if (pcmcia_io_alloc(pa->pf, 0, NE2000_NPORTS, NE2000_NPORTS,
    309 	    &psc->sc_pcioh)) {
    310 		printf(": can't alloc i/o space\n");
    311 		return;
    312 	}
    313 
    314 	dsc->sc_regt = psc->sc_pcioh.iot;
    315 	dsc->sc_regh = psc->sc_pcioh.ioh;
    316 
    317 	nsc->sc_asict = psc->sc_pcioh.iot;
    318 	if (bus_space_subregion(dsc->sc_regt, dsc->sc_regh,
    319 	    NE2000_ASIC_OFFSET, NE2000_ASIC_NPORTS,
    320 	    &nsc->sc_asich)) {
    321 		printf(": can't get subregion for asic\n");
    322 		return;
    323 	}
    324 
    325 	/* Set up power management hooks. */
    326 	dsc->sc_enable = ne_pcmcia_enable;
    327 	dsc->sc_disable = ne_pcmcia_disable;
    328 
    329 	/* Enable the card. */
    330 	pcmcia_function_init(pa->pf, cfe);
    331 	if (pcmcia_function_enable(pa->pf)) {
    332 		printf(": function enable failed\n");
    333 		return;
    334 	}
    335 
    336 	/* some cards claim to be io16, but they're lying. */
    337 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO8,
    338 	    NE2000_NIC_OFFSET, NE2000_NIC_NPORTS,
    339 	    &psc->sc_pcioh, &psc->sc_nic_io_window)) {
    340 		printf(": can't map NIC i/o space\n");
    341 		return;
    342 	}
    343 
    344 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16,
    345 	    NE2000_ASIC_OFFSET, NE2000_ASIC_NPORTS,
    346 	    &psc->sc_pcioh, &psc->sc_asic_io_window)) {
    347 		printf(": can't map ASIC i/o space\n");
    348 		return;
    349 	}
    350 
    351 	printf("\n");
    352 
    353 	/*
    354 	 * Read the station address from the board.
    355 	 */
    356 	for (i = 0; i < NE2000_NDEVS; i++) {
    357 		if ((ne_dev = ne2000_match(pa->card, pa->pf->number, i))
    358 		    != NULL) {
    359 			if (ne_dev->enet_maddr >= 0) {
    360 				if (pcmcia_mem_alloc(pa->pf,
    361 				    ETHER_ADDR_LEN * 2, &pcmh)) {
    362 					printf("%s: can't alloc mem for"
    363 					    " enet addr\n",
    364 					    dsc->sc_dev.dv_xname);
    365 					return;
    366 				}
    367 				if (pcmcia_mem_map(pa->pf, PCMCIA_MEM_ATTR,
    368 				    ne_dev->enet_maddr, ETHER_ADDR_LEN * 2,
    369 				    &pcmh, &offset, &mwindow)) {
    370 					printf("%s: can't map mem for"
    371 					    " enet addr\n",
    372 					    dsc->sc_dev.dv_xname);
    373 					return;
    374 				}
    375 				for (j = 0; j < ETHER_ADDR_LEN; j++)
    376 					myea[j] = bus_space_read_1(pcmh.memt,
    377 					    pcmh.memh, offset + (j * 2));
    378 				pcmcia_mem_unmap(pa->pf, mwindow);
    379 				pcmcia_mem_free(pa->pf, &pcmh);
    380 				enaddr = myea;
    381 			}
    382 			break;
    383 		}
    384 	}
    385 
    386 	if (enaddr != NULL) {
    387 		/*
    388 		 * Make sure this is what we expect.
    389 		 */
    390 		if (enaddr[0] != ne_dev->enet_vendor[0] ||
    391 		    enaddr[1] != ne_dev->enet_vendor[1] ||
    392 		    enaddr[2] != ne_dev->enet_vendor[2]) {
    393 			printf("\n%s: enet addr has incorrect vendor code\n",
    394 			    dsc->sc_dev.dv_xname);
    395 			printf("%s: (%02x:%02x:%02x should be "
    396 			    "%02x:%02x:%02x)\n", dsc->sc_dev.dv_xname,
    397 			    enaddr[0], enaddr[1], enaddr[2],
    398 			    ne_dev->enet_vendor[0],
    399 			    ne_dev->enet_vendor[1],
    400 			    ne_dev->enet_vendor[2]);
    401 			return;
    402 		}
    403 	}
    404 
    405 	printf("%s: %s Ethernet\n", dsc->sc_dev.dv_xname, ne_dev->name);
    406 
    407 	ne2000_attach(nsc, enaddr);
    408 
    409 	pcmcia_function_disable(pa->pf);
    410 }
    411 
    412 int
    413 ne_pcmcia_enable(dsc)
    414 	struct dp8390_softc *dsc;
    415 {
    416 	struct ne_pcmcia_softc *psc = (struct ne_pcmcia_softc *)dsc;
    417 
    418 	/* set up the interrupt */
    419 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, dp8390_intr,
    420 	    dsc);
    421 	if (psc->sc_ih == NULL) {
    422 		printf("%s: couldn't establish interrupt\n",
    423 		    dsc->sc_dev.dv_xname);
    424 		return (1);
    425 	}
    426 
    427 	return (pcmcia_function_enable(psc->sc_pf));
    428 }
    429 
    430 void
    431 ne_pcmcia_disable(dsc)
    432 	struct dp8390_softc *dsc;
    433 {
    434 	struct ne_pcmcia_softc *psc = (struct ne_pcmcia_softc *)dsc;
    435 
    436 	pcmcia_function_disable(psc->sc_pf);
    437 
    438 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    439 }
    440