Home | History | Annotate | Line # | Download | only in pcmcia
com_pcmcia.c revision 1.32
      1 /*	$NetBSD: com_pcmcia.c,v 1.32 2002/10/02 16:52:05 thorpej Exp $	 */
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1991 The Regents of the University of California.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     72  */
     73 
     74 #include <sys/cdefs.h>
     75 __KERNEL_RCSID(0, "$NetBSD: com_pcmcia.c,v 1.32 2002/10/02 16:52:05 thorpej Exp $");
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/ioctl.h>
     80 #include <sys/select.h>
     81 #include <sys/tty.h>
     82 #include <sys/proc.h>
     83 #include <sys/user.h>
     84 #include <sys/conf.h>
     85 #include <sys/file.h>
     86 #include <sys/uio.h>
     87 #include <sys/kernel.h>
     88 #include <sys/syslog.h>
     89 #include <sys/device.h>
     90 
     91 #include <machine/intr.h>
     92 #include <machine/bus.h>
     93 
     94 #include <dev/pcmcia/pcmciavar.h>
     95 #include <dev/pcmcia/pcmciareg.h>
     96 #include <dev/pcmcia/pcmciadevs.h>
     97 
     98 #include <dev/ic/comreg.h>
     99 #include <dev/ic/comvar.h>
    100 
    101 #include <dev/isa/isareg.h>
    102 
    103 struct com_dev {
    104 	char	*name;
    105 	char	*cis1_info[4];
    106 };
    107 
    108 /* Devices that we need to match by CIS strings */
    109 static struct com_dev com_devs[] = {
    110 	{ PCMCIA_STR_MEGAHERTZ_XJ2288, PCMCIA_CIS_MEGAHERTZ_XJ2288 },
    111 };
    112 
    113 
    114 static int com_devs_size = sizeof(com_devs) / sizeof(com_devs[0]);
    115 static struct com_dev *com_dev_match __P((struct pcmcia_card *));
    116 
    117 int com_pcmcia_match __P((struct device *, struct cfdata *, void *));
    118 void com_pcmcia_attach __P((struct device *, struct device *, void *));
    119 int com_pcmcia_detach __P((struct device *, int));
    120 void com_pcmcia_cleanup __P((void *));
    121 
    122 int com_pcmcia_enable __P((struct com_softc *));
    123 void com_pcmcia_disable __P((struct com_softc *));
    124 int com_pcmcia_enable1 __P((struct com_softc *));
    125 void com_pcmcia_disable1 __P((struct com_softc *));
    126 
    127 struct com_pcmcia_softc {
    128 	struct com_softc sc_com;		/* real "com" softc */
    129 
    130 	/* PCMCIA-specific goo */
    131 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
    132 	int sc_io_window;			/* our i/o window */
    133 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    134 	void *sc_ih;				/* interrupt handler */
    135 };
    136 
    137 CFATTACH_DECL(com_pcmcia, sizeof(struct com_pcmcia_softc),
    138     com_pcmcia_match, com_pcmcia_attach, com_pcmcia_detach, com_activate);
    139 
    140 /* Look for pcmcia cards with particular CIS strings */
    141 static struct com_dev *
    142 com_dev_match(card)
    143 	struct pcmcia_card *card;
    144 {
    145 	int i, j;
    146 
    147 	for (i = 0; i < com_devs_size; i++) {
    148 		for (j = 0; j < 4; j++)
    149 			if (com_devs[i].cis1_info[j] &&
    150 			    strcmp(com_devs[i].cis1_info[j],
    151 			    card->cis1_info[j]) != 0)
    152 				break;
    153 		if (j == 4)
    154 			return &com_devs[i];
    155 	}
    156 
    157 	return NULL;
    158 }
    159 
    160 
    161 int
    162 com_pcmcia_match(parent, match, aux)
    163 	struct device *parent;
    164 	struct cfdata *match;
    165 	void *aux;
    166 {
    167 	int comportmask;
    168 	struct pcmcia_attach_args *pa = aux;
    169 	struct pcmcia_config_entry *cfe;
    170 
    171 	/* 1. Does it claim to be a serial device? */
    172 	if (pa->pf->function == PCMCIA_FUNCTION_SERIAL)
    173 		return 1;
    174 
    175 	/* 2. Does it have all four 'standard' port ranges? */
    176 	comportmask = 0;
    177 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    178 		switch (cfe->iospace[0].start) {
    179 		case IO_COM1:
    180 			comportmask |= 1;
    181 			break;
    182 		case IO_COM2:
    183 			comportmask |= 2;
    184 			break;
    185 		case IO_COM3:
    186 			comportmask |= 4;
    187 			break;
    188 		case IO_COM4:
    189 			comportmask |= 8;
    190 			break;
    191 		}
    192 	}
    193 
    194 	if (comportmask == 15)
    195 		return 1;
    196 
    197 	/* 3. Is this a card we know about? */
    198 	if (com_dev_match(pa->card) != NULL)
    199 		return 1;
    200 
    201 	return 0;
    202 }
    203 
    204 void
    205 com_pcmcia_attach(parent, self, aux)
    206 	struct device  *parent, *self;
    207 	void *aux;
    208 {
    209 	struct com_pcmcia_softc *psc = (void *) self;
    210 	struct com_softc *sc = &psc->sc_com;
    211 	struct pcmcia_attach_args *pa = aux;
    212 	struct pcmcia_config_entry *cfe;
    213 	int             autoalloc = 0;
    214 
    215 	psc->sc_pf = pa->pf;
    216 
    217 	psc->sc_io_window = -1;
    218 
    219 retry:
    220 	/* find a cfe we can use */
    221 
    222 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    223 #if 0
    224 		/*
    225 		 * Some modem cards (e.g. Xircom CM33) also have
    226 		 * mem space.  Don't bother with this check.
    227 		 */
    228 		if (cfe->num_memspace != 0)
    229 			continue;
    230 #endif
    231 
    232 		if (cfe->num_iospace != 1)
    233 			continue;
    234 
    235 		if (autoalloc == 0) {
    236 			/*
    237 			 * cfe->iomask == 3 is our test for the "generic"
    238 			 * config table entry, which we want to avoid on the
    239 			 * first pass and use exclusively on the second pass.
    240 			 */
    241 			if ((cfe->iomask != 3) &&
    242 			    (cfe->iospace[0].start != 0)) {
    243 				if (!pcmcia_io_alloc(pa->pf,
    244 				    cfe->iospace[0].start,
    245 				    cfe->iospace[0].length, 0,
    246 				    &psc->sc_pcioh)) {
    247 					goto found;
    248 				}
    249 			}
    250 		} else {
    251 			if (cfe->iomask == 3) {
    252 				if (!pcmcia_io_alloc(pa->pf, 0,
    253 				    cfe->iospace[0].length,
    254 				    cfe->iospace[0].length, &psc->sc_pcioh)) {
    255 					goto found;
    256 				}
    257 			}
    258 		}
    259 	}
    260 	if (autoalloc == 0) {
    261 		autoalloc = 1;
    262 		goto retry;
    263 	} else if (!cfe) {
    264 		printf(": can't allocate i/o space\n");
    265 		return;
    266 	}
    267 found:
    268 	/* Enable the card. */
    269 	pcmcia_function_init(pa->pf, cfe);
    270 	if (com_pcmcia_enable1(sc))
    271 		printf(": function enable failed\n");
    272 
    273 	sc->enabled = 1;
    274 
    275 	/* map in the io space */
    276 
    277 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    278 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, psc->sc_pcioh.size,
    279 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    280 		printf(": can't map i/o space\n");
    281 		return;
    282 	}
    283 	sc->sc_iot = psc->sc_pcioh.iot;
    284 	sc->sc_ioh = psc->sc_pcioh.ioh;
    285 
    286 	sc->sc_iobase = -1;
    287 	sc->sc_frequency = COM_FREQ;
    288 
    289 	sc->enable = com_pcmcia_enable;
    290 	sc->disable = com_pcmcia_disable;
    291 
    292 	printf(": serial device\n%s", sc->sc_dev.dv_xname);
    293 
    294 	com_attach_subr(sc);
    295 
    296 	sc->enabled = 0;
    297 
    298 	com_pcmcia_disable1(sc);
    299 }
    300 
    301 int
    302 com_pcmcia_detach(self, flags)
    303 	struct device  *self;
    304 	int flags;
    305 {
    306 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) self;
    307 	int error;
    308 
    309 	/* Unmap our i/o window. */
    310 	if (psc->sc_io_window == -1) {
    311 		printf("%s: I/O window not allocated.",
    312 		    psc->sc_com.sc_dev.dv_xname);
    313 		return 0;
    314 	}
    315 
    316 	if ((error = com_detach(self, flags)) != 0)
    317 		return error;
    318 
    319 	/* Unmap our i/o window. */
    320 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    321 
    322 	/* Free our i/o space. */
    323 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    324 
    325 	return 0;
    326 }
    327 
    328 int
    329 com_pcmcia_enable(sc)
    330 	struct com_softc *sc;
    331 {
    332 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    333 	struct pcmcia_function *pf = psc->sc_pf;
    334 	int error;
    335 
    336 	if ((error = com_pcmcia_enable1(sc)) != 0)
    337 		return error;
    338 
    339 	/* establish the interrupt. */
    340 	psc->sc_ih = pcmcia_intr_establish(pf, IPL_SERIAL, comintr, sc);
    341 	if (psc->sc_ih == NULL) {
    342 		printf("%s: couldn't establish interrupt\n",
    343 		    sc->sc_dev.dv_xname);
    344 		com_pcmcia_disable1(sc);
    345 		return 1;
    346 	}
    347 	return 0;
    348 }
    349 
    350 int
    351 com_pcmcia_enable1(sc)
    352 	struct com_softc *sc;
    353 {
    354 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    355 	struct pcmcia_function *pf = psc->sc_pf;
    356 	int ret;
    357 
    358 	if ((ret = pcmcia_function_enable(pf)) != 0)
    359 		return ret;
    360 
    361 	if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
    362 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
    363 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
    364 		int reg;
    365 
    366 		/* turn off the ethernet-disable bit */
    367 
    368 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    369 		if (reg & 0x08) {
    370 			reg &= ~0x08;
    371 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    372 		}
    373 	}
    374 	return ret;
    375 }
    376 
    377 void
    378 com_pcmcia_disable(sc)
    379 	struct com_softc *sc;
    380 {
    381 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    382 
    383 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    384 	com_pcmcia_disable1(sc);
    385 }
    386 
    387 void
    388 com_pcmcia_disable1(sc)
    389 	struct com_softc *sc;
    390 {
    391 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    392 
    393 	pcmcia_function_disable(psc->sc_pf);
    394 }
    395