Home | History | Annotate | Line # | Download | only in pcmcia
com_pcmcia.c revision 1.22
      1 /*	$NetBSD: com_pcmcia.c,v 1.22 2001/11/13 07:26:32 lukem 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.22 2001/11/13 07:26:32 lukem 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/types.h>
     90 #include <sys/device.h>
     91 
     92 #include <machine/intr.h>
     93 #include <machine/bus.h>
     94 
     95 #include <dev/pcmcia/pcmciavar.h>
     96 #include <dev/pcmcia/pcmciareg.h>
     97 #include <dev/pcmcia/pcmciadevs.h>
     98 
     99 #include <dev/ic/comreg.h>
    100 #include <dev/ic/comvar.h>
    101 
    102 #include <dev/isa/isareg.h>
    103 
    104 struct com_dev {
    105 	char	*name;
    106 	char	*cis1_info[4];
    107 };
    108 
    109 /* Devices that we need to match by CIS strings */
    110 static struct com_dev com_devs[] = {
    111 	{ PCMCIA_STR_MEGAHERTZ_XJ2288, PCMCIA_CIS_MEGAHERTZ_XJ2288 },
    112 };
    113 
    114 
    115 static int com_devs_size = sizeof(com_devs) / sizeof(com_devs[0]);
    116 static struct com_dev *com_dev_match __P((struct pcmcia_card *));
    117 
    118 int com_pcmcia_match __P((struct device *, struct cfdata *, void *));
    119 void com_pcmcia_attach __P((struct device *, struct device *, void *));
    120 int com_pcmcia_detach __P((struct device *, int));
    121 void com_pcmcia_cleanup __P((void *));
    122 
    123 int com_pcmcia_enable __P((struct com_softc *));
    124 void com_pcmcia_disable __P((struct com_softc *));
    125 int com_pcmcia_enable1 __P((struct com_softc *));
    126 void com_pcmcia_disable1 __P((struct com_softc *));
    127 
    128 struct com_pcmcia_softc {
    129 	struct com_softc sc_com;		/* real "com" softc */
    130 
    131 	/* PCMCIA-specific goo */
    132 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
    133 	int sc_io_window;			/* our i/o window */
    134 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    135 	void *sc_ih;				/* interrupt handler */
    136 };
    137 
    138 struct cfattach com_pcmcia_ca = {
    139 	sizeof(struct com_pcmcia_softc), com_pcmcia_match, com_pcmcia_attach,
    140 	com_pcmcia_detach, com_activate
    141 };
    142 
    143 /* Look for pcmcia cards with particular CIS strings */
    144 static struct com_dev *
    145 com_dev_match(card)
    146 	struct pcmcia_card *card;
    147 {
    148 	int i, j;
    149 
    150 	for (i = 0; i < com_devs_size; i++) {
    151 		for (j = 0; j < 4; j++)
    152 			if (com_devs[i].cis1_info[j] &&
    153 			    strcmp(com_devs[i].cis1_info[j],
    154 			    card->cis1_info[j]) != 0)
    155 				break;
    156 		if (j == 4)
    157 			return &com_devs[i];
    158 	}
    159 
    160 	return NULL;
    161 }
    162 
    163 
    164 int
    165 com_pcmcia_match(parent, match, aux)
    166 	struct device *parent;
    167 	struct cfdata *match;
    168 	void *aux;
    169 {
    170 	int comportmask;
    171 	struct pcmcia_attach_args *pa = aux;
    172 	struct pcmcia_config_entry *cfe;
    173 
    174 	/* 1. Does it claim to be a serial device? */
    175 	if (pa->pf->function == PCMCIA_FUNCTION_SERIAL)
    176 		return 1;
    177 
    178 	/* 2. Does it have all four 'standard' port ranges? */
    179 	comportmask = 0;
    180 	for (cfe = pa->pf->cfe_head.sqh_first; cfe;
    181 	    cfe = cfe->cfe_list.sqe_next) {
    182 		switch (cfe->iospace[0].start) {
    183 		case IO_COM1:
    184 			comportmask |= 1;
    185 			break;
    186 		case IO_COM2:
    187 			comportmask |= 2;
    188 			break;
    189 		case IO_COM3:
    190 			comportmask |= 4;
    191 			break;
    192 		case IO_COM4:
    193 			comportmask |= 8;
    194 			break;
    195 		}
    196 	}
    197 
    198 	if (comportmask == 15)
    199 		return 1;
    200 
    201 	/* 3. Is this a card we know about? */
    202 	if (com_dev_match(pa->card) != NULL)
    203 		return 1;
    204 
    205 	return 0;
    206 }
    207 
    208 void
    209 com_pcmcia_attach(parent, self, aux)
    210 	struct device  *parent, *self;
    211 	void *aux;
    212 {
    213 	struct com_pcmcia_softc *psc = (void *) self;
    214 	struct com_softc *sc = &psc->sc_com;
    215 	struct pcmcia_attach_args *pa = aux;
    216 	struct pcmcia_config_entry *cfe;
    217 	int             autoalloc = 0;
    218 
    219 	psc->sc_pf = pa->pf;
    220 
    221 retry:
    222 	/* find a cfe we can use */
    223 
    224 	for (cfe = pa->pf->cfe_head.sqh_first; cfe;
    225 	    cfe = cfe->cfe_list.sqe_next) {
    226 #if 0
    227 		/*
    228 		 * Some modem cards (e.g. Xircom CM33) also have
    229 		 * mem space.  Don't bother with this check.
    230 		 */
    231 		if (cfe->num_memspace != 0)
    232 			continue;
    233 #endif
    234 
    235 		if (cfe->num_iospace != 1)
    236 			continue;
    237 
    238 		if (autoalloc == 0) {
    239 			/*
    240 			 * cfe->iomask == 3 is our test for the "generic"
    241 			 * config table entry, which we want to avoid on the
    242 			 * first pass and use exclusively on the second pass.
    243 			 */
    244 			if ((cfe->iomask != 3) &&
    245 			    (cfe->iospace[0].start != 0)) {
    246 				if (!pcmcia_io_alloc(pa->pf,
    247 				    cfe->iospace[0].start,
    248 				    cfe->iospace[0].length, 0,
    249 				    &psc->sc_pcioh)) {
    250 					goto found;
    251 				}
    252 			}
    253 		} else {
    254 			if (cfe->iomask == 3) {
    255 				if (!pcmcia_io_alloc(pa->pf, 0,
    256 				    cfe->iospace[0].length,
    257 				    cfe->iospace[0].length, &psc->sc_pcioh)) {
    258 					goto found;
    259 				}
    260 			}
    261 		}
    262 	}
    263 	if (autoalloc == 0) {
    264 		autoalloc = 1;
    265 		goto retry;
    266 	} else if (!cfe) {
    267 		printf(": can't allocate i/o space\n");
    268 		return;
    269 	}
    270 found:
    271 	sc->sc_iot = psc->sc_pcioh.iot;
    272 	sc->sc_ioh = psc->sc_pcioh.ioh;
    273 
    274 	/* Enable the card. */
    275 	pcmcia_function_init(pa->pf, cfe);
    276 	if (com_pcmcia_enable1(sc))
    277 		printf(": function enable failed\n");
    278 
    279 	sc->enabled = 1;
    280 
    281 	/* map in the io space */
    282 
    283 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    284 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, psc->sc_pcioh.size,
    285 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    286 		printf(": can't map i/o space\n");
    287 		return;
    288 	}
    289 	sc->sc_iobase = -1;
    290 	sc->sc_frequency = COM_FREQ;
    291 
    292 	sc->enable = com_pcmcia_enable;
    293 	sc->disable = com_pcmcia_disable;
    294 
    295 	printf(": serial device\n%s", sc->sc_dev.dv_xname);
    296 
    297 	com_attach_subr(sc);
    298 
    299 	sc->enabled = 0;
    300 
    301 	com_pcmcia_disable1(sc);
    302 }
    303 
    304 int
    305 com_pcmcia_detach(self, flags)
    306 	struct device  *self;
    307 	int flags;
    308 {
    309 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) self;
    310 	int error;
    311 
    312 	if ((error = com_detach(self, flags)) != 0)
    313 		return error;
    314 
    315 	/* Unmap our i/o window. */
    316 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    317 
    318 	/* Free our i/o space. */
    319 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    320 
    321 	return 0;
    322 }
    323 
    324 int
    325 com_pcmcia_enable(sc)
    326 	struct com_softc *sc;
    327 {
    328 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    329 	struct pcmcia_function *pf = psc->sc_pf;
    330 
    331 	/* establish the interrupt. */
    332 	psc->sc_ih = pcmcia_intr_establish(pf, IPL_SERIAL, comintr, sc);
    333 	if (psc->sc_ih == NULL) {
    334 		printf("%s: couldn't establish interrupt\n",
    335 		    sc->sc_dev.dv_xname);
    336 		return 1;
    337 	}
    338 	return com_pcmcia_enable1(sc);
    339 }
    340 
    341 int
    342 com_pcmcia_enable1(sc)
    343 	struct com_softc *sc;
    344 {
    345 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    346 	struct pcmcia_function *pf = psc->sc_pf;
    347 	int ret;
    348 
    349 	if ((ret = pcmcia_function_enable(pf)) != 0)
    350 		return ret;
    351 
    352 	if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
    353 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
    354 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
    355 		int reg;
    356 
    357 		/* turn off the ethernet-disable bit */
    358 
    359 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    360 		if (reg & 0x08) {
    361 			reg &= ~0x08;
    362 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    363 		}
    364 	}
    365 	return ret;
    366 }
    367 
    368 void
    369 com_pcmcia_disable(sc)
    370 	struct com_softc *sc;
    371 {
    372 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    373 
    374 	com_pcmcia_disable1(sc);
    375 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    376 }
    377 
    378 void
    379 com_pcmcia_disable1(sc)
    380 	struct com_softc *sc;
    381 {
    382 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    383 
    384 	pcmcia_function_disable(psc->sc_pf);
    385 }
    386