Home | History | Annotate | Line # | Download | only in pcmcia
pcmcom.c revision 1.29
      1 /*	$NetBSD: pcmcom.c,v 1.29 2006/11/16 00:53:16 christos 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.29 2006/11/16 00:53:16 christos 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 #if 0	/* does not work */
    111 	{ PCMCIA_VENDOR_SOCKET, PCMCIA_PRODUCT_SOCKET_DUAL_RS232_A,
    112 	  PCMCIA_CIS_INVALID },
    113 #endif
    114 };
    115 const size_t pcmcom_nproducts =
    116     sizeof(pcmcom_products) / sizeof(pcmcom_products[0]);
    117 
    118 int	pcmcom_print(void *, const char *);
    119 
    120 int	pcmcom_enable(struct pcmcom_softc *);
    121 void	pcmcom_disable(struct pcmcom_softc *);
    122 
    123 int	pcmcom_intr(void *);
    124 
    125 int
    126 pcmcom_match(struct device *parent __unused, struct cfdata *cf __unused,
    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(struct device *parent __unused, struct device *self, void *aux)
    149 {
    150 	struct pcmcom_softc *sc = (void *)self;
    151 	struct pcmcia_attach_args *pa = aux;
    152 	struct pcmcia_config_entry *cfe;
    153 	int slave;
    154 	int error;
    155 	int locs[PCMCOMCF_NLOCS];
    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 		locs[PCMCOMCF_SLAVE] = slave;
    184 
    185 		sc->sc_slaves[slave] = config_found_sm_loc(&sc->sc_dev,
    186 			"pcmcom", locs,
    187 			&pca, pcmcom_print, config_stdsubmatch);
    188 	}
    189 
    190 	pcmcom_disable(sc);
    191 	sc->sc_state = PCMCOM_ATTACHED;
    192 	return;
    193 
    194 fail:
    195 	pcmcia_function_unconfigure(pa->pf);
    196 }
    197 
    198 int
    199 pcmcom_detach(self, flags)
    200 	struct device *self;
    201 	int flags;
    202 {
    203 	struct pcmcom_softc *sc = (void *)self;
    204 	int slave, error;
    205 
    206 	if (sc->sc_state != PCMCOM_ATTACHED)
    207 		return (0);
    208 
    209 	for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
    210 		if (sc->sc_slaves[slave]) {
    211 			/* Detach the child. */
    212 			error = config_detach(sc->sc_slaves[slave], flags);
    213 			if (error)
    214 				return (error);
    215 			sc->sc_slaves[slave] = 0;
    216 		}
    217 	}
    218 
    219 	pcmcia_function_unconfigure(sc->sc_pf);
    220 
    221 	return (0);
    222 }
    223 
    224 int
    225 pcmcom_activate(self, act)
    226 	struct device *self;
    227 	enum devact act;
    228 {
    229 	struct pcmcom_softc *sc = (void *)self;
    230 	int slave, error = 0, s;
    231 
    232 	s = splserial();
    233 	switch (act) {
    234 	case DVACT_ACTIVATE:
    235 		error = EOPNOTSUPP;
    236 		break;
    237 
    238 	case DVACT_DEACTIVATE:
    239 		for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
    240 			if (sc->sc_slaves[slave]) {
    241 				/*
    242 				 * Deactivate the child.  Doing so will cause
    243 				 * our own enabled count to drop to 0, once all
    244 				 * children are deactivated.
    245 				 */
    246 				error = config_deactivate(sc->sc_slaves[slave]);
    247 				if (error)
    248 					break;
    249 			}
    250 		}
    251 		break;
    252 	}
    253 	splx(s);
    254 	return (error);
    255 }
    256 
    257 int
    258 pcmcom_print(aux, pnp)
    259 	void *aux;
    260 	const char *pnp;
    261 {
    262 	struct pcmcom_attach_args *pca = aux;
    263 
    264 	/* only com's can attach to pcmcom's; easy... */
    265 	if (pnp)
    266 		aprint_normal("com at %s", pnp);
    267 
    268 	aprint_normal(" slave %d", pca->pca_slave);
    269 
    270 	return (UNCONF);
    271 }
    272 
    273 int
    274 pcmcom_intr(arg)
    275 	void *arg;
    276 {
    277 #if NCOM > 0
    278 	struct pcmcom_softc *sc = arg;
    279 	int i, rval = 0;
    280 
    281 	if (sc->sc_enabled_count == 0)
    282 		return (0);
    283 
    284 	for (i = 0; i < sc->sc_nslaves; i++) {
    285 		if (sc->sc_slaves[i])
    286 			rval |= comintr(sc->sc_slaves[i]);
    287 	}
    288 
    289 	return (rval);
    290 #else
    291 	return (0);
    292 #endif /* NCOM > 0 */
    293 }
    294 
    295 int
    296 pcmcom_enable(sc)
    297 	struct pcmcom_softc *sc;
    298 {
    299 	int error;
    300 
    301 	if (sc->sc_enabled_count++ != 0)
    302 		return (0);
    303 
    304 	/* Establish the interrupt. */
    305 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_SERIAL,
    306 	    pcmcom_intr, sc);
    307 	if (!sc->sc_ih)
    308 		return (EIO);
    309 
    310 	error = pcmcia_function_enable(sc->sc_pf);
    311 	if (error) {
    312 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    313 		sc->sc_ih = 0;
    314 	}
    315 
    316 	return (error);
    317 }
    318 
    319 void
    320 pcmcom_disable(sc)
    321 	struct pcmcom_softc *sc;
    322 {
    323 
    324 	if (--sc->sc_enabled_count != 0)
    325 		return;
    326 
    327 	pcmcia_function_disable(sc->sc_pf);
    328 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    329 	sc->sc_ih = 0;
    330 }
    331 
    332 /****** Here begins the com attachment code. ******/
    333 
    334 #if NCOM_PCMCOM > 0
    335 int	com_pcmcom_match(struct device *, struct cfdata *, void *);
    336 void	com_pcmcom_attach(struct device *, struct device *, void *);
    337 
    338 /* No pcmcom-specific goo in the softc; it's all in the parent. */
    339 CFATTACH_DECL(com_pcmcom, sizeof(struct com_softc),
    340     com_pcmcom_match, com_pcmcom_attach, com_detach, com_activate);
    341 
    342 int	com_pcmcom_enable(struct com_softc *);
    343 void	com_pcmcom_disable(struct com_softc *);
    344 
    345 int
    346 com_pcmcom_match(struct device *parent __unused, struct cfdata *cf __unused,
    347     void *aux __unused)
    348 {
    349 
    350 	/* Device is always present. */
    351 	return (1);
    352 }
    353 
    354 void
    355 com_pcmcom_attach(struct device *parent __unused, struct device *self,
    356     void *aux)
    357 {
    358 	struct com_softc *sc = (struct com_softc *)self;
    359 	struct pcmcom_attach_args *pca = aux;
    360 
    361 	COM_INIT_REGS(sc->sc_regs, pca->pca_iot, pca->pca_ioh, -1);
    362 	sc->enabled = 1;
    363 
    364 	sc->sc_frequency = COM_FREQ;
    365 
    366 	sc->enable = com_pcmcom_enable;
    367 	sc->disable = com_pcmcom_disable;
    368 
    369 	com_attach_subr(sc);
    370 
    371 	sc->enabled = 0;
    372 }
    373 
    374 int
    375 com_pcmcom_enable(sc)
    376 	struct com_softc *sc;
    377 {
    378 
    379 	return (pcmcom_enable((struct pcmcom_softc *)device_parent(&sc->sc_dev)));
    380 }
    381 
    382 void
    383 com_pcmcom_disable(sc)
    384 	struct com_softc *sc;
    385 {
    386 
    387 	pcmcom_disable((struct pcmcom_softc *)device_parent(&sc->sc_dev));
    388 }
    389 #endif /* NCOM_PCMCOM > 0 */
    390