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