Home | History | Annotate | Line # | Download | only in pcmcia
com_pcmcia.c revision 1.52
      1 /*	$NetBSD: com_pcmcia.c,v 1.52 2006/07/13 22:56:02 gdamore Exp $	 */
      2 
      3 /*-
      4  * Copyright (c) 1998, 2004 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. Neither the name of the University nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: com_pcmcia.c,v 1.52 2006/07/13 22:56:02 gdamore Exp $");
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/ioctl.h>
     76 #include <sys/select.h>
     77 #include <sys/tty.h>
     78 #include <sys/proc.h>
     79 #include <sys/user.h>
     80 #include <sys/conf.h>
     81 #include <sys/file.h>
     82 #include <sys/uio.h>
     83 #include <sys/kernel.h>
     84 #include <sys/syslog.h>
     85 #include <sys/device.h>
     86 
     87 #include <machine/intr.h>
     88 #include <machine/bus.h>
     89 
     90 #include <dev/pcmcia/pcmciavar.h>
     91 #include <dev/pcmcia/pcmciareg.h>
     92 #include <dev/pcmcia/pcmciadevs.h>
     93 
     94 #include <dev/ic/comreg.h>
     95 #include <dev/ic/comvar.h>
     96 
     97 #include <dev/isa/isareg.h>
     98 
     99 int com_pcmcia_match(struct device *, struct cfdata *, void *);
    100 int com_pcmcia_validate_config(struct pcmcia_config_entry *);
    101 void com_pcmcia_attach(struct device *, struct device *, void *);
    102 int com_pcmcia_detach(struct device *, int);
    103 
    104 int com_pcmcia_enable(struct com_softc *);
    105 void com_pcmcia_disable(struct com_softc *);
    106 
    107 struct com_pcmcia_softc {
    108 	struct com_softc sc_com;		/* real "com" softc */
    109 
    110 	/* PCMCIA-specific goo */
    111 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    112 	void *sc_ih;				/* interrupt handler */
    113 	int sc_attached;
    114 };
    115 
    116 CFATTACH_DECL(com_pcmcia, sizeof(struct com_pcmcia_softc),
    117     com_pcmcia_match, com_pcmcia_attach, com_pcmcia_detach, com_activate);
    118 
    119 static const struct pcmcia_product com_pcmcia_products[] = {
    120 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
    121 	  PCMCIA_CIS_MEGAHERTZ_XJ2288 },
    122 };
    123 static const size_t com_pcmcia_nproducts =
    124     sizeof(com_pcmcia_products) / sizeof(com_pcmcia_products[0]);
    125 
    126 int
    127 com_pcmcia_match(parent, match, aux)
    128 	struct device *parent;
    129 	struct cfdata *match;
    130 	void *aux;
    131 {
    132 	int comportmask;
    133 	struct pcmcia_attach_args *pa = aux;
    134 	struct pcmcia_config_entry *cfe;
    135 
    136 	/* 1. Does it claim to be a serial device? */
    137 	if (pa->pf->function == PCMCIA_FUNCTION_SERIAL)
    138 		return 1;
    139 
    140 	/* 2. Does it have all four 'standard' port ranges? */
    141 	comportmask = 0;
    142 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    143 		switch (cfe->iospace[0].start) {
    144 		case IO_COM1:
    145 			comportmask |= 1;
    146 			break;
    147 		case IO_COM2:
    148 			comportmask |= 2;
    149 			break;
    150 		case IO_COM3:
    151 			comportmask |= 4;
    152 			break;
    153 		case IO_COM4:
    154 			comportmask |= 8;
    155 			break;
    156 		}
    157 	}
    158 
    159 	if (comportmask == 15)
    160 		return 1;
    161 
    162 	/* 3. Is this a card we know about? */
    163 	if (pcmcia_product_lookup(pa, com_pcmcia_products, com_pcmcia_nproducts,
    164 	    sizeof(com_pcmcia_products[0]), NULL))
    165 		return 1;
    166 
    167 	return 0;
    168 }
    169 
    170 int
    171 com_pcmcia_validate_config(cfe)
    172 	struct pcmcia_config_entry *cfe;
    173 {
    174 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    175 	    cfe->num_iospace != 1)
    176 		return (EINVAL);
    177 	/* Some cards have a memory space, but we don't use it. */
    178 	cfe->num_memspace = 0;
    179 	return (0);
    180 }
    181 
    182 void
    183 com_pcmcia_attach(parent, self, aux)
    184 	struct device  *parent, *self;
    185 	void *aux;
    186 {
    187 	struct com_pcmcia_softc *psc = (void *) self;
    188 	struct com_softc *sc = &psc->sc_com;
    189 	struct pcmcia_attach_args *pa = aux;
    190 	struct pcmcia_config_entry *cfe;
    191 	int error;
    192 
    193 	psc->sc_pf = pa->pf;
    194 
    195 	error = pcmcia_function_configure(pa->pf, com_pcmcia_validate_config);
    196 	if (error) {
    197 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    198 		    error);
    199 		return;
    200 	}
    201 
    202 	cfe = pa->pf->cfe;
    203 	COM_INIT_REGS(sc->sc_regs, cfe->iospace[0].handle.iot,
    204 	    cfe->iospace[0].handle.ioh, -1);
    205 
    206 	error = com_pcmcia_enable(sc);
    207 	if (error)
    208 		goto fail;
    209 
    210 	sc->enabled = 1;
    211 
    212 	sc->sc_frequency = COM_FREQ;
    213 
    214 	sc->enable = com_pcmcia_enable;
    215 	sc->disable = com_pcmcia_disable;
    216 
    217 	aprint_normal("%s", self->dv_xname);
    218 
    219 	com_attach_subr(sc);
    220 
    221 	sc->enabled = 0;
    222 
    223 	psc->sc_attached = 1;
    224 	com_pcmcia_disable(sc);
    225 	return;
    226 
    227 fail:
    228 	pcmcia_function_unconfigure(pa->pf);
    229 }
    230 
    231 int
    232 com_pcmcia_detach(self, flags)
    233 	struct device  *self;
    234 	int flags;
    235 {
    236 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) self;
    237 	int error;
    238 
    239 	if (!psc->sc_attached)
    240 		return (0);
    241 
    242 	if ((error = com_detach(self, flags)) != 0)
    243 		return error;
    244 
    245 	pcmcia_function_unconfigure(psc->sc_pf);
    246 	return (0);
    247 }
    248 
    249 int
    250 com_pcmcia_enable(sc)
    251 	struct com_softc *sc;
    252 {
    253 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    254 	struct pcmcia_function *pf = psc->sc_pf;
    255 	int error;
    256 
    257 	/* establish the interrupt. */
    258 	psc->sc_ih = pcmcia_intr_establish(pf, IPL_SERIAL, comintr, sc);
    259 	if (psc->sc_ih == NULL) {
    260 		printf("%s: couldn't establish interrupt\n",
    261 		    sc->sc_dev.dv_xname);
    262 		return (1);
    263 	}
    264 
    265 	if ((error = pcmcia_function_enable(pf)) != 0) {
    266 		pcmcia_intr_disestablish(pf, psc->sc_ih);
    267 		psc->sc_ih = 0;
    268 		return (error);
    269 	}
    270 
    271 	if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
    272 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
    273 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
    274 		int reg;
    275 
    276 		/* turn off the ethernet-disable bit */
    277 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    278 		if (reg & 0x08) {
    279 			reg &= ~0x08;
    280 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    281 		}
    282 	}
    283 
    284 	return (0);
    285 }
    286 
    287 void
    288 com_pcmcia_disable(sc)
    289 	struct com_softc *sc;
    290 {
    291 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
    292 
    293 	pcmcia_function_disable(psc->sc_pf);
    294 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    295 	psc->sc_ih = 0;
    296 }
    297