Home | History | Annotate | Line # | Download | only in pcmcia
pcmcom.c revision 1.19
      1 /*	$NetBSD: pcmcom.c,v 1.19 2004/08/10 21:14:50 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2000, 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by RedBack Networks, Inc.
      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  * Device driver for multi-port PCMCIA serial cards, written by
     41  * Jason R. Thorpe for RedBack Networks, Inc.
     42  *
     43  * Most of these cards are simply multiple UARTs sharing a single interrupt
     44  * line, and rely on the fact that PCMCIA level-triggered interrupts can
     45  * be shared.  There are no special interrupt registers on them, as there
     46  * are on most ISA multi-port serial cards.
     47  *
     48  * If there are other cards that have interrupt registers, they should not
     49  * be glued into this driver.  Rather, separate drivers should be written
     50  * for those devices, as we have in the ISA multi-port serial card case.
     51  */
     52 
     53 #include <sys/cdefs.h>
     54 __KERNEL_RCSID(0, "$NetBSD: pcmcom.c,v 1.19 2004/08/10 21:14:50 mycroft Exp $");
     55 
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/device.h>
     59 #include <sys/termios.h>
     60 #include <sys/malloc.h>
     61 
     62 #include <machine/bus.h>
     63 #include <machine/intr.h>
     64 
     65 #include <dev/ic/comreg.h>
     66 #include <dev/ic/comvar.h>
     67 
     68 #include <dev/pcmcia/pcmciavar.h>
     69 #include <dev/pcmcia/pcmciareg.h>
     70 #include <dev/pcmcia/pcmciadevs.h>
     71 
     72 #include "com.h"
     73 #include "pcmcom.h"
     74 
     75 #include "locators.h"
     76 
     77 struct pcmcom_softc {
     78 	struct device sc_dev;			/* generic device glue */
     79 
     80 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     81 	void *sc_ih;				/* interrupt handle */
     82 	int sc_enabled_count;			/* enabled count */
     83 
     84 #define	NSLAVES			8
     85 	struct device *sc_slaves[NSLAVES];	/* slave info */
     86 	int sc_nslaves;				/* slave count */
     87 
     88 	int sc_state;
     89 #define	PCMCOM_ATTACHED		3
     90 };
     91 
     92 struct pcmcom_attach_args {
     93 	bus_space_tag_t pca_iot;		/* I/O tag */
     94 	bus_space_handle_t pca_ioh;		/* I/O handle */
     95 	int pca_slave;				/* slave # */
     96 };
     97 
     98 int	pcmcom_match __P((struct device *, struct cfdata *, void *));
     99 int	pcmcom_validate_config __P((struct pcmcia_config_entry *));
    100 void	pcmcom_attach __P((struct device *, struct device *, void *));
    101 int	pcmcom_detach __P((struct device *, int));
    102 int	pcmcom_activate __P((struct device *, enum devact));
    103 
    104 CFATTACH_DECL(pcmcom, sizeof(struct pcmcom_softc),
    105     pcmcom_match, pcmcom_attach, pcmcom_detach, pcmcom_activate);
    106 
    107 const struct pcmcia_product pcmcom_products[] = {
    108 	{ PCMCIA_VENDOR_SOCKET, PCMCIA_PRODUCT_SOCKET_DUAL_RS232,
    109 	  PCMCIA_CIS_INVALID },
    110 };
    111 const size_t pcmcom_nproducts =
    112     sizeof(pcmcom_products) / sizeof(pcmcom_products[0]);
    113 
    114 int	pcmcom_print __P((void *, const char *));
    115 int	pcmcom_submatch __P((struct device *, struct cfdata *, void *));
    116 
    117 int	pcmcom_enable __P((struct pcmcom_softc *));
    118 void	pcmcom_disable __P((struct pcmcom_softc *));
    119 
    120 int	pcmcom_intr __P((void *));
    121 
    122 int
    123 pcmcom_match(parent, cf, aux)
    124 	struct device *parent;
    125 	struct cfdata *cf;
    126 	void *aux;
    127 {
    128 	struct pcmcia_attach_args *pa = aux;
    129 
    130 	if (pcmcia_product_lookup(pa, pcmcom_products, pcmcom_nproducts,
    131 	    sizeof(pcmcom_products[0]), NULL))
    132 		return (2);	/* beat com_pcmcia */
    133 	return (0);
    134 }
    135 
    136 int
    137 pcmcom_validate_config(cfe)
    138 	struct pcmcia_config_entry *cfe;
    139 {
    140 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    141 	    cfe->num_iospace < 1 || cfe->num_iospace > NSLAVES)
    142 		return (EINVAL);
    143 	return (0);
    144 }
    145 
    146 void
    147 pcmcom_attach(parent, self, aux)
    148 	struct device *parent, *self;
    149 	void *aux;
    150 {
    151 	struct pcmcom_softc *sc = (void *)self;
    152 	struct pcmcia_attach_args *pa = aux;
    153 	struct pcmcia_config_entry *cfe;
    154 	int slave;
    155 	int error;
    156 
    157 	sc->sc_pf = pa->pf;
    158 
    159 	error = pcmcia_function_configure(pa->pf, pcmcom_validate_config);
    160 	if (error) {
    161 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    162 		    error);
    163 		return;
    164 	}
    165 
    166 	cfe = pa->pf->cfe;
    167 	sc->sc_nslaves = cfe->num_iospace;
    168 
    169 	error = pcmcom_enable(sc);
    170 	if (error)
    171 		goto fail;
    172 
    173 	/* Attach the children. */
    174 	for (slave = 0; slave < sc->sc_nslaves; slave++) {
    175 		struct pcmcom_attach_args pca;
    176 
    177 		printf("%s: slave %d\n", self->dv_xname, slave);
    178 
    179 		pca.pca_iot = cfe->iospace[slave].handle.iot;
    180 		pca.pca_ioh = cfe->iospace[slave].handle.ioh;
    181 		pca.pca_slave = slave;
    182 
    183 		sc->sc_slaves[slave] = config_found_sm(&sc->sc_dev, &pca,
    184 		    pcmcom_print, pcmcom_submatch);
    185 	}
    186 
    187 	pcmcom_disable(sc);
    188 	sc->sc_state = PCMCOM_ATTACHED;
    189 	return;
    190 
    191 fail:
    192 	pcmcia_function_unconfigure(pa->pf);
    193 }
    194 
    195 int
    196 pcmcom_detach(self, flags)
    197 	struct device *self;
    198 	int flags;
    199 {
    200 	struct pcmcom_softc *sc = (void *)self;
    201 	int slave, error;
    202 
    203 	if (sc->sc_state != PCMCOM_ATTACHED)
    204 		return (0);
    205 
    206 	for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
    207 		if (sc->sc_slaves[slave]) {
    208 			/* Detach the child. */
    209 			error = config_detach(sc->sc_slaves[slave], flags);
    210 			if (error)
    211 				return (error);
    212 			sc->sc_slaves[slave] = 0;
    213 		}
    214 	}
    215 
    216 	pcmcia_function_unconfigure(sc->sc_pf);
    217 
    218 	return (0);
    219 }
    220 
    221 int
    222 pcmcom_activate(self, act)
    223 	struct device *self;
    224 	enum devact act;
    225 {
    226 	struct pcmcom_softc *sc = (void *)self;
    227 	int slave, error = 0, s;
    228 
    229 	s = splserial();
    230 	switch (act) {
    231 	case DVACT_ACTIVATE:
    232 		error = EOPNOTSUPP;
    233 		break;
    234 
    235 	case DVACT_DEACTIVATE:
    236 		for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
    237 			if (sc->sc_slaves[slave]) {
    238 				/*
    239 				 * Deactivate the child.  Doing so will cause
    240 				 * our own enabled count to drop to 0, once all
    241 				 * children are deactivated.
    242 				 */
    243 				error = config_deactivate(sc->sc_slaves[slave]);
    244 				if (error)
    245 					break;
    246 			}
    247 		}
    248 		break;
    249 	}
    250 	splx(s);
    251 	return (error);
    252 }
    253 
    254 int
    255 pcmcom_print(aux, pnp)
    256 	void *aux;
    257 	const char *pnp;
    258 {
    259 	struct pcmcom_attach_args *pca = aux;
    260 
    261 	/* only com's can attach to pcmcom's; easy... */
    262 	if (pnp)
    263 		aprint_normal("com at %s", pnp);
    264 
    265 	aprint_normal(" slave %d", pca->pca_slave);
    266 
    267 	return (UNCONF);
    268 }
    269 
    270 int
    271 pcmcom_submatch(parent, cf, aux)
    272 	struct device *parent;
    273 	struct cfdata *cf;
    274 	void *aux;
    275 {
    276 	struct pcmcom_attach_args *pca = aux;
    277 
    278 	if (cf->cf_loc[PCMCOMCF_SLAVE] != pca->pca_slave &&
    279 	    cf->cf_loc[PCMCOMCF_SLAVE] != PCMCOMCF_SLAVE_DEFAULT)
    280 		return (0);
    281 
    282 	return (config_match(parent, cf, aux));
    283 }
    284 
    285 int
    286 pcmcom_intr(arg)
    287 	void *arg;
    288 {
    289 #if NCOM > 0
    290 	struct pcmcom_softc *sc = arg;
    291 	int i, rval = 0;
    292 
    293 	if (sc->sc_enabled_count == 0)
    294 		return (0);
    295 
    296 	for (i = 0; i < sc->sc_nslaves; i++) {
    297 		if (sc->sc_slaves[i])
    298 			rval |= comintr(sc->sc_slaves[i]);
    299 	}
    300 
    301 	return (rval);
    302 #else
    303 	return (0);
    304 #endif /* NCOM > 0 */
    305 }
    306 
    307 int
    308 pcmcom_enable(sc)
    309 	struct pcmcom_softc *sc;
    310 {
    311 	int error;
    312 
    313 	if (sc->sc_enabled_count++ != 0)
    314 		return (0);
    315 
    316 	/* Establish the interrupt. */
    317 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_SERIAL,
    318 	    pcmcom_intr, sc);
    319 	if (!sc->sc_ih)
    320 		return (EIO);
    321 
    322 	error = pcmcia_function_enable(sc->sc_pf);
    323 	if (error) {
    324 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    325 		sc->sc_ih = 0;
    326 	}
    327 
    328 	return (error);
    329 }
    330 
    331 void
    332 pcmcom_disable(sc)
    333 	struct pcmcom_softc *sc;
    334 {
    335 
    336 	if (--sc->sc_enabled_count != 0)
    337 		return;
    338 
    339 	pcmcia_function_disable(sc->sc_pf);
    340 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    341 	sc->sc_ih = 0;
    342 }
    343 
    344 /****** Here begins the com attachment code. ******/
    345 
    346 #if NCOM_PCMCOM > 0
    347 int	com_pcmcom_match __P((struct device *, struct cfdata *, void *));
    348 void	com_pcmcom_attach __P((struct device *, struct device *, void *));
    349 
    350 /* No pcmcom-specific goo in the softc; it's all in the parent. */
    351 CFATTACH_DECL(com_pcmcom, sizeof(struct com_softc),
    352     com_pcmcom_match, com_pcmcom_attach, com_detach, com_activate);
    353 
    354 int	com_pcmcom_enable __P((struct com_softc *));
    355 void	com_pcmcom_disable __P((struct com_softc *));
    356 
    357 int
    358 com_pcmcom_match(parent, cf, aux)
    359 	struct device *parent;
    360 	struct cfdata *cf;
    361 	void *aux;
    362 {
    363 
    364 	/* Device is always present. */
    365 	return (1);
    366 }
    367 
    368 void
    369 com_pcmcom_attach(parent, self, aux)
    370 	struct device *parent, *self;
    371 	void *aux;
    372 {
    373 	struct com_softc *sc = (struct com_softc *)self;
    374 	struct pcmcom_attach_args *pca = aux;
    375 
    376 	sc->sc_iot = pca->pca_iot;
    377 	sc->sc_ioh = pca->pca_ioh;
    378 
    379 	sc->enabled = 1;
    380 
    381 	sc->sc_iobase = -1;
    382 	sc->sc_frequency = COM_FREQ;
    383 
    384 	sc->enable = com_pcmcom_enable;
    385 	sc->disable = com_pcmcom_disable;
    386 
    387 	com_attach_subr(sc);
    388 
    389 	sc->enabled = 0;
    390 }
    391 
    392 int
    393 com_pcmcom_enable(sc)
    394 	struct com_softc *sc;
    395 {
    396 
    397 	return (pcmcom_enable((struct pcmcom_softc *)sc->sc_dev.dv_parent));
    398 }
    399 
    400 void
    401 com_pcmcom_disable(sc)
    402 	struct com_softc *sc;
    403 {
    404 
    405 	pcmcom_disable((struct pcmcom_softc *)sc->sc_dev.dv_parent);
    406 }
    407 #endif /* NCOM_PCMCOM > 0 */
    408