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