Home | History | Annotate | Line # | Download | only in pcmcia
pcmcom.c revision 1.23
      1 /*	$NetBSD: pcmcom.c,v 1.23 2005/08/25 18:35:39 drochner 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.23 2005/08/25 18:35:39 drochner 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(struct device *, struct cfdata *, void *);
     99 int	pcmcom_validate_config(struct pcmcia_config_entry *);
    100 void	pcmcom_attach(struct device *, struct device *, void *);
    101 int	pcmcom_detach(struct device *, int);
    102 int	pcmcom_activate(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(void *, const char *);
    115 int	pcmcom_submatch(struct device *, struct cfdata *,
    116 			     const locdesc_t *, void *);
    117 
    118 int	pcmcom_enable(struct pcmcom_softc *);
    119 void	pcmcom_disable(struct pcmcom_softc *);
    120 
    121 int	pcmcom_intr(void *);
    122 
    123 int
    124 pcmcom_match(parent, cf, aux)
    125 	struct device *parent;
    126 	struct cfdata *cf;
    127 	void *aux;
    128 {
    129 	struct pcmcia_attach_args *pa = aux;
    130 
    131 	if (pcmcia_product_lookup(pa, pcmcom_products, pcmcom_nproducts,
    132 	    sizeof(pcmcom_products[0]), NULL))
    133 		return (2);	/* beat com_pcmcia */
    134 	return (0);
    135 }
    136 
    137 int
    138 pcmcom_validate_config(cfe)
    139 	struct pcmcia_config_entry *cfe;
    140 {
    141 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    142 	    cfe->num_iospace < 1 || cfe->num_iospace > NSLAVES)
    143 		return (EINVAL);
    144 	return (0);
    145 }
    146 
    147 void
    148 pcmcom_attach(parent, self, aux)
    149 	struct device *parent, *self;
    150 	void *aux;
    151 {
    152 	struct pcmcom_softc *sc = (void *)self;
    153 	struct pcmcia_attach_args *pa = aux;
    154 	struct pcmcia_config_entry *cfe;
    155 	int slave;
    156 	int error;
    157 	int locs[PCMCOMCF_NLOCS];
    158 
    159 	sc->sc_pf = pa->pf;
    160 
    161 	error = pcmcia_function_configure(pa->pf, pcmcom_validate_config);
    162 	if (error) {
    163 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    164 		    error);
    165 		return;
    166 	}
    167 
    168 	cfe = pa->pf->cfe;
    169 	sc->sc_nslaves = cfe->num_iospace;
    170 
    171 	error = pcmcom_enable(sc);
    172 	if (error)
    173 		goto fail;
    174 
    175 	/* Attach the children. */
    176 	for (slave = 0; slave < sc->sc_nslaves; slave++) {
    177 		struct pcmcom_attach_args pca;
    178 
    179 		printf("%s: slave %d\n", self->dv_xname, slave);
    180 
    181 		pca.pca_iot = cfe->iospace[slave].handle.iot;
    182 		pca.pca_ioh = cfe->iospace[slave].handle.ioh;
    183 		pca.pca_slave = slave;
    184 
    185 		locs[PCMCOMCF_SLAVE] = slave;
    186 
    187 		sc->sc_slaves[slave] = config_found_sm_loc(&sc->sc_dev,
    188 			"pcmcom", locs,
    189 			&pca, pcmcom_print, pcmcom_submatch);
    190 	}
    191 
    192 	pcmcom_disable(sc);
    193 	sc->sc_state = PCMCOM_ATTACHED;
    194 	return;
    195 
    196 fail:
    197 	pcmcia_function_unconfigure(pa->pf);
    198 }
    199 
    200 int
    201 pcmcom_detach(self, flags)
    202 	struct device *self;
    203 	int flags;
    204 {
    205 	struct pcmcom_softc *sc = (void *)self;
    206 	int slave, error;
    207 
    208 	if (sc->sc_state != PCMCOM_ATTACHED)
    209 		return (0);
    210 
    211 	for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
    212 		if (sc->sc_slaves[slave]) {
    213 			/* Detach the child. */
    214 			error = config_detach(sc->sc_slaves[slave], flags);
    215 			if (error)
    216 				return (error);
    217 			sc->sc_slaves[slave] = 0;
    218 		}
    219 	}
    220 
    221 	pcmcia_function_unconfigure(sc->sc_pf);
    222 
    223 	return (0);
    224 }
    225 
    226 int
    227 pcmcom_activate(self, act)
    228 	struct device *self;
    229 	enum devact act;
    230 {
    231 	struct pcmcom_softc *sc = (void *)self;
    232 	int slave, error = 0, s;
    233 
    234 	s = splserial();
    235 	switch (act) {
    236 	case DVACT_ACTIVATE:
    237 		error = EOPNOTSUPP;
    238 		break;
    239 
    240 	case DVACT_DEACTIVATE:
    241 		for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
    242 			if (sc->sc_slaves[slave]) {
    243 				/*
    244 				 * Deactivate the child.  Doing so will cause
    245 				 * our own enabled count to drop to 0, once all
    246 				 * children are deactivated.
    247 				 */
    248 				error = config_deactivate(sc->sc_slaves[slave]);
    249 				if (error)
    250 					break;
    251 			}
    252 		}
    253 		break;
    254 	}
    255 	splx(s);
    256 	return (error);
    257 }
    258 
    259 int
    260 pcmcom_print(aux, pnp)
    261 	void *aux;
    262 	const char *pnp;
    263 {
    264 	struct pcmcom_attach_args *pca = aux;
    265 
    266 	/* only com's can attach to pcmcom's; easy... */
    267 	if (pnp)
    268 		aprint_normal("com at %s", pnp);
    269 
    270 	aprint_normal(" slave %d", pca->pca_slave);
    271 
    272 	return (UNCONF);
    273 }
    274 
    275 int
    276 pcmcom_submatch(parent, cf, locs, aux)
    277 	struct device *parent;
    278 	struct cfdata *cf;
    279 	const locdesc_t *locs;
    280 	void *aux;
    281 {
    282 
    283 	if (cf->cf_loc[PCMCOMCF_SLAVE] != PCMCOMCF_SLAVE_DEFAULT &&
    284 	    cf->cf_loc[PCMCOMCF_SLAVE] != locs[PCMCOMCF_SLAVE]);
    285 		return (0);
    286 
    287 	return (config_match(parent, cf, aux));
    288 }
    289 
    290 int
    291 pcmcom_intr(arg)
    292 	void *arg;
    293 {
    294 #if NCOM > 0
    295 	struct pcmcom_softc *sc = arg;
    296 	int i, rval = 0;
    297 
    298 	if (sc->sc_enabled_count == 0)
    299 		return (0);
    300 
    301 	for (i = 0; i < sc->sc_nslaves; i++) {
    302 		if (sc->sc_slaves[i])
    303 			rval |= comintr(sc->sc_slaves[i]);
    304 	}
    305 
    306 	return (rval);
    307 #else
    308 	return (0);
    309 #endif /* NCOM > 0 */
    310 }
    311 
    312 int
    313 pcmcom_enable(sc)
    314 	struct pcmcom_softc *sc;
    315 {
    316 	int error;
    317 
    318 	if (sc->sc_enabled_count++ != 0)
    319 		return (0);
    320 
    321 	/* Establish the interrupt. */
    322 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_SERIAL,
    323 	    pcmcom_intr, sc);
    324 	if (!sc->sc_ih)
    325 		return (EIO);
    326 
    327 	error = pcmcia_function_enable(sc->sc_pf);
    328 	if (error) {
    329 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    330 		sc->sc_ih = 0;
    331 	}
    332 
    333 	return (error);
    334 }
    335 
    336 void
    337 pcmcom_disable(sc)
    338 	struct pcmcom_softc *sc;
    339 {
    340 
    341 	if (--sc->sc_enabled_count != 0)
    342 		return;
    343 
    344 	pcmcia_function_disable(sc->sc_pf);
    345 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    346 	sc->sc_ih = 0;
    347 }
    348 
    349 /****** Here begins the com attachment code. ******/
    350 
    351 #if NCOM_PCMCOM > 0
    352 int	com_pcmcom_match(struct device *, struct cfdata *, void *);
    353 void	com_pcmcom_attach(struct device *, struct device *, void *);
    354 
    355 /* No pcmcom-specific goo in the softc; it's all in the parent. */
    356 CFATTACH_DECL(com_pcmcom, sizeof(struct com_softc),
    357     com_pcmcom_match, com_pcmcom_attach, com_detach, com_activate);
    358 
    359 int	com_pcmcom_enable(struct com_softc *);
    360 void	com_pcmcom_disable(struct com_softc *);
    361 
    362 int
    363 com_pcmcom_match(parent, cf, aux)
    364 	struct device *parent;
    365 	struct cfdata *cf;
    366 	void *aux;
    367 {
    368 
    369 	/* Device is always present. */
    370 	return (1);
    371 }
    372 
    373 void
    374 com_pcmcom_attach(parent, self, aux)
    375 	struct device *parent, *self;
    376 	void *aux;
    377 {
    378 	struct com_softc *sc = (struct com_softc *)self;
    379 	struct pcmcom_attach_args *pca = aux;
    380 
    381 	sc->sc_iot = pca->pca_iot;
    382 	sc->sc_ioh = pca->pca_ioh;
    383 
    384 	sc->enabled = 1;
    385 
    386 	sc->sc_iobase = -1;
    387 	sc->sc_frequency = COM_FREQ;
    388 
    389 	sc->enable = com_pcmcom_enable;
    390 	sc->disable = com_pcmcom_disable;
    391 
    392 	com_attach_subr(sc);
    393 
    394 	sc->enabled = 0;
    395 }
    396 
    397 int
    398 com_pcmcom_enable(sc)
    399 	struct com_softc *sc;
    400 {
    401 
    402 	return (pcmcom_enable((struct pcmcom_softc *)sc->sc_dev.dv_parent));
    403 }
    404 
    405 void
    406 com_pcmcom_disable(sc)
    407 	struct com_softc *sc;
    408 {
    409 
    410 	pcmcom_disable((struct pcmcom_softc *)sc->sc_dev.dv_parent);
    411 }
    412 #endif /* NCOM_PCMCOM > 0 */
    413