Home | History | Annotate | Line # | Download | only in pcmcia
com_pcmcia.c revision 1.11
      1 /*	$NetBSD: com_pcmcia.c,v 1.11 1998/08/13 15:08:54 nathanw 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 (cfe->num_memspace != 0)
    234 			continue;
    235 
    236 		if (cfe->num_iospace != 1)
    237 			continue;
    238 
    239 		if (cfe->iomask == 3) {
    240 			if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    241 			    cfe->iospace[0].length, &psc->sc_pcioh)) {
    242 				continue;
    243 			}
    244 		} else {
    245 			if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    246 			    cfe->iospace[0].length, 0, &psc->sc_pcioh)) {
    247 				continue;
    248 			}
    249 		}
    250 
    251 		break;
    252 	}
    253 
    254 	if (!cfe) {
    255 		printf(": can't allocate i/o space\n");
    256 		return;
    257 	}
    258 
    259 	sc->sc_iot = psc->sc_pcioh.iot;
    260 	sc->sc_ioh = psc->sc_pcioh.ioh;
    261 
    262 	/* Enable the card. */
    263 	pcmcia_function_init(pa->pf, cfe);
    264 	if (com_pcmcia_enable1(sc))
    265 		printf(": function enable failed\n");
    266 
    267 	sc->enabled = 1;
    268 
    269 	/* map in the io space */
    270 
    271 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    272 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, psc->sc_pcioh.size,
    273 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    274 		printf(": can't map i/o space\n");
    275 		return;
    276 	}
    277 
    278 	sc->sc_iobase = -1;
    279 	sc->sc_frequency = COM_FREQ;
    280 
    281 	sc->enable = com_pcmcia_enable;
    282 	sc->disable = com_pcmcia_disable;
    283 
    284 	if ((comdev = com_dev_match(pa->card)) != NULL)
    285 		printf(": %s", comdev->name);
    286 	else
    287 		printf(": Generic serial device");
    288 
    289 	com_attach_subr(sc);
    290 
    291 	sc->enabled = 0;
    292 
    293 	com_pcmcia_disable1(sc);
    294 }
    295 
    296 int
    297 com_pcmcia_enable(sc)
    298 	struct com_softc *sc;
    299 {
    300 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    301 	struct pcmcia_function *pf = psc->sc_pf;
    302 
    303 	/* establish the interrupt. */
    304 	psc->sc_ih = pcmcia_intr_establish(pf, IPL_SERIAL, comintr, sc);
    305 	if (psc->sc_ih == NULL) {
    306 		printf("%s: couldn't establish interrupt\n",
    307 		       sc->sc_dev.dv_xname);
    308 		return (1);
    309 	}
    310 	return com_pcmcia_enable1(sc);
    311 }
    312 
    313 int
    314 com_pcmcia_enable1(sc)
    315 	struct com_softc *sc;
    316 {
    317 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    318 	struct pcmcia_function *pf = psc->sc_pf;
    319 	int ret;
    320 
    321 	if ((ret = pcmcia_function_enable(pf)))
    322 	    return(ret);
    323 
    324 	if (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) {
    325 		int reg;
    326 
    327 		/* turn off the ethernet-disable bit */
    328 
    329 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    330 		if (reg & 0x08) {
    331 		    reg &= ~0x08;
    332 		    pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    333 		}
    334 	}
    335 
    336 	return(ret);
    337 }
    338 
    339 void
    340 com_pcmcia_disable(sc)
    341 	struct com_softc *sc;
    342 {
    343 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    344 
    345 	com_pcmcia_disable1(sc);
    346 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    347 }
    348 
    349 void
    350 com_pcmcia_disable1(sc)
    351 	struct com_softc *sc;
    352 {
    353 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    354 
    355 	pcmcia_function_disable(psc->sc_pf);
    356 }
    357