Home | History | Annotate | Line # | Download | only in pcmcia
if_ep_pcmcia.c revision 1.10
      1 /*	$NetBSD: if_ep_pcmcia.c,v 1.10 1998/07/05 06:49:16 jonathan 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 "opt_inet.h"
     33 #include "opt_ns.h"
     34 #include "bpfilter.h"
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/socket.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/errno.h>
     42 #include <sys/syslog.h>
     43 #include <sys/select.h>
     44 #include <sys/device.h>
     45 
     46 #include <net/if.h>
     47 #include <net/if_dl.h>
     48 #include <net/if_ether.h>
     49 #include <net/if_media.h>
     50 
     51 #ifdef INET
     52 #include <netinet/in.h>
     53 #include <netinet/in_systm.h>
     54 #include <netinet/in_var.h>
     55 #include <netinet/ip.h>
     56 #include <netinet/if_inarp.h>
     57 #endif
     58 
     59 #ifdef NS
     60 #include <netns/ns.h>
     61 #include <netns/ns_if.h>
     62 #endif
     63 
     64 #if NBPFILTER > 0
     65 #include <net/bpf.h>
     66 #include <net/bpfdesc.h>
     67 #endif
     68 
     69 #include <machine/cpu.h>
     70 #include <machine/bus.h>
     71 #include <machine/intr.h>
     72 
     73 #include <dev/ic/elink3var.h>
     74 #include <dev/ic/elink3reg.h>
     75 
     76 #include <dev/pcmcia/pcmciareg.h>
     77 #include <dev/pcmcia/pcmciavar.h>
     78 
     79 #define	PCMCIA_MANUFACTURER_3COM	0x101
     80 #define	PCMCIA_PRODUCT_3COM_3C562	0x562
     81 #define	PCMCIA_PRODUCT_3COM_3C589	0x589
     82 
     83 int	ep_pcmcia_match __P((struct device *, struct cfdata *, void *));
     84 void	ep_pcmcia_attach __P((struct device *, struct device *, void *));
     85 
     86 int	ep_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
     87 int	ep_pcmcia_enable __P((struct ep_softc *));
     88 void	ep_pcmcia_disable __P((struct ep_softc *));
     89 
     90 int	ep_pcmcia_enable1 __P((struct ep_softc *));
     91 void	ep_pcmcia_disable1 __P((struct ep_softc *));
     92 
     93 struct ep_pcmcia_softc {
     94 	struct ep_softc sc_ep;			/* real "ep" softc */
     95 
     96 	/* PCMCIA-specific goo */
     97 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     98 	int sc_io_window;			/* our i/o window */
     99 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    100 };
    101 
    102 struct cfattach ep_pcmcia_ca = {
    103 	sizeof(struct ep_pcmcia_softc), ep_pcmcia_match, ep_pcmcia_attach
    104 };
    105 
    106 int
    107 ep_pcmcia_match(parent, match, aux)
    108 	struct device *parent;
    109 	struct cfdata *match;
    110 	void *aux;
    111 {
    112 	struct pcmcia_attach_args *pa = aux;
    113 
    114 	if (pa->manufacturer == PCMCIA_MANUFACTURER_3COM) {
    115 		switch (pa->product) {
    116 		case PCMCIA_PRODUCT_3COM_3C562:
    117 		case PCMCIA_PRODUCT_3COM_3C589:
    118 			if (pa->pf->number == 0)
    119 				return (1);
    120 		}
    121 	}
    122 
    123 	return (0);
    124 }
    125 
    126 int
    127 ep_pcmcia_enable(sc)
    128 	struct ep_softc *sc;
    129 {
    130 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    131 	struct pcmcia_function *pf = psc->sc_pf;
    132 
    133 	/* establish the interrupt. */
    134 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, epintr, sc);
    135 	if (sc->sc_ih == NULL) {
    136 		printf("%s: couldn't establish interrupt\n",
    137 		    sc->sc_dev.dv_xname);
    138 		return (1);
    139 	}
    140 
    141 	return (ep_pcmcia_enable1(sc));
    142 }
    143 
    144 int
    145 ep_pcmcia_enable1(sc)
    146 	struct ep_softc *sc;
    147 {
    148 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    149 	struct pcmcia_function *pf = psc->sc_pf;
    150 	int ret;
    151 
    152 	if ((ret = pcmcia_function_enable(pf)))
    153 		return (ret);
    154 
    155 	if (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) {
    156 		int reg;
    157 
    158 		/* turn off the serial-disable bit */
    159 
    160 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    161 		if (reg & 0x08) {
    162 			reg &= ~0x08;
    163 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    164 		}
    165 
    166 	}
    167 
    168 	return (ret);
    169 }
    170 
    171 void
    172 ep_pcmcia_disable(sc)
    173 	struct ep_softc *sc;
    174 {
    175 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    176 
    177 	ep_pcmcia_disable1(sc);
    178 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    179 }
    180 
    181 void
    182 ep_pcmcia_disable1(sc)
    183 	struct ep_softc *sc;
    184 {
    185 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    186 
    187 	pcmcia_function_disable(psc->sc_pf);
    188 }
    189 
    190 void
    191 ep_pcmcia_attach(parent, self, aux)
    192 	struct device  *parent, *self;
    193 	void           *aux;
    194 {
    195 	struct ep_pcmcia_softc *psc = (void *) self;
    196 	struct ep_softc *sc = &psc->sc_ep;
    197 	struct pcmcia_attach_args *pa = aux;
    198 	struct pcmcia_config_entry *cfe;
    199 	int i;
    200 	u_int8_t myla[ETHER_ADDR_LEN];
    201 	u_int8_t *enaddr;
    202 	char *model;
    203 
    204 	psc->sc_pf = pa->pf;
    205 	cfe = pa->pf->cfe_head.sqh_first;
    206 
    207 	/* Enable the card. */
    208 	pcmcia_function_init(pa->pf, cfe);
    209 	if (ep_pcmcia_enable1(sc))
    210 		printf(": function enable failed\n");
    211 
    212 	sc->enabled = 1;
    213 
    214 	if (cfe->num_memspace != 0)
    215 		printf(": unexpected number of memory spaces %d should be 0\n",
    216 		    cfe->num_memspace);
    217 
    218 	if (cfe->num_iospace != 1)
    219 		printf(": unexpected number of I/O spaces %d should be 1\n",
    220 		    cfe->num_iospace);
    221 
    222 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    223 		bus_addr_t maxaddr = (pa->pf->sc->iobase + pa->pf->sc->iosize);
    224 
    225 		for (i = pa->pf->sc->iobase; i < maxaddr; i += 0x10) {
    226 			/*
    227 			 * the 3c562 can only use 0x??00-0x??7f
    228 			 * according to the Linux driver
    229 			 */
    230 			if (i & 0x80)
    231 				continue;
    232 			if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
    233 			    0, &psc->sc_pcioh) == 0)
    234 				break;
    235 		}
    236 		if (i >= maxaddr) {
    237 			printf(": can't allocate i/o space\n");
    238 			return;
    239 		}
    240 	} else {
    241 		if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    242 		    cfe->iospace[0].length, &psc->sc_pcioh))
    243 			printf(": can't allocate i/o space\n");
    244 	}
    245 
    246 	sc->sc_iot = psc->sc_pcioh.iot;
    247 	sc->sc_ioh = psc->sc_pcioh.ioh;
    248 
    249 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    250 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
    251 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    252 		printf(": can't map i/o space\n");
    253 		return;
    254 	}
    255 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    256 		/*
    257 		 * 3c562a-c use this; 3c562d does it in the regular way.
    258 		 * we might want to check the revision and produce a warning
    259 		 * in the future.
    260 		 */
    261 		if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla) != 1)
    262 			enaddr = NULL;
    263 		else
    264 			enaddr = myla;
    265 	} else {
    266 		enaddr = NULL;
    267 	}
    268 
    269 	sc->bustype = EP_BUS_PCMCIA;
    270 
    271 	switch (pa->product) {
    272 	case PCMCIA_PRODUCT_3COM_3C589:
    273 		model = "3Com 3C589 Ethernet";
    274 		break;
    275 	case PCMCIA_PRODUCT_3COM_3C562:
    276 		model = "3Com 3C562 Ethernet";
    277 		break;
    278 	default:
    279 		model = "3Com Ethernet, model unknown";
    280 		break;
    281 	}
    282 
    283 	printf(": %s\n", model);
    284 
    285 	sc->enable = ep_pcmcia_enable;
    286 	sc->disable = ep_pcmcia_disable;
    287 
    288 	epconfig(sc, EP_CHIPSET_3C509, enaddr);
    289 
    290 	sc->enabled = 0;
    291 
    292 	ep_pcmcia_disable1(sc);
    293 }
    294 
    295 int
    296 ep_pcmcia_get_enaddr(tuple, arg)
    297 	struct pcmcia_tuple *tuple;
    298 	void *arg;
    299 {
    300 	u_int8_t *myla = arg;
    301 	int i;
    302 
    303 	/* this is 3c562a-c magic */
    304 	if (tuple->code == 0x88) {
    305 		if (tuple->length < ETHER_ADDR_LEN)
    306 			return (0);
    307 
    308 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    309 			myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
    310 			myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
    311 		}
    312 
    313 		return (1);
    314 	}
    315 	return (0);
    316 }
    317