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