Home | History | Annotate | Line # | Download | only in pcmcia
pcmcom.c revision 1.41.14.1
      1 /*	$NetBSD: pcmcom.c,v 1.41.14.1 2021/03/22 02:01:01 thorpej 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Device driver for multi-port PCMCIA serial cards, written by
     34  * Jason R. Thorpe for RedBack Networks, Inc.
     35  *
     36  * Most of these cards are simply multiple UARTs sharing a single interrupt
     37  * line, and rely on the fact that PCMCIA level-triggered interrupts can
     38  * be shared.  There are no special interrupt registers on them, as there
     39  * are on most ISA multi-port serial cards.
     40  *
     41  * If there are other cards that have interrupt registers, they should not
     42  * be glued into this driver.  Rather, separate drivers should be written
     43  * for those devices, as we have in the ISA multi-port serial card case.
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: pcmcom.c,v 1.41.14.1 2021/03/22 02:01:01 thorpej Exp $");
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/device.h>
     52 #include <sys/termios.h>
     53 #include <sys/malloc.h>
     54 
     55 #include <sys/bus.h>
     56 #include <sys/intr.h>
     57 
     58 #include <dev/ic/comreg.h>
     59 #include <dev/ic/comvar.h>
     60 
     61 #include <dev/pcmcia/pcmciavar.h>
     62 #include <dev/pcmcia/pcmciareg.h>
     63 #include <dev/pcmcia/pcmciadevs.h>
     64 
     65 #include "com.h"
     66 #include "pcmcom.h"
     67 
     68 #include "locators.h"
     69 
     70 struct pcmcom_softc {
     71 	device_t sc_dev;			/* generic device glue */
     72 
     73 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     74 	void *sc_ih;				/* interrupt handle */
     75 	int sc_enabled_count;			/* enabled count */
     76 
     77 #define	NSLAVES			8
     78 	device_t sc_slaves[NSLAVES];	/* slave info */
     79 	int sc_nslaves;				/* slave count */
     80 
     81 	int sc_state;
     82 #define	PCMCOM_ATTACHED		3
     83 };
     84 
     85 struct pcmcom_attach_args {
     86 	bus_space_tag_t pca_iot;		/* I/O tag */
     87 	bus_space_handle_t pca_ioh;		/* I/O handle */
     88 	int pca_slave;				/* slave # */
     89 };
     90 
     91 int	pcmcom_match(device_t, cfdata_t, void *);
     92 int	pcmcom_validate_config(struct pcmcia_config_entry *);
     93 void	pcmcom_attach(device_t, device_t, void *);
     94 int	pcmcom_detach(device_t, int);
     95 void	pcmcom_childdet(device_t, device_t);
     96 
     97 CFATTACH_DECL_NEW(pcmcom, sizeof(struct pcmcom_softc),
     98     pcmcom_match, pcmcom_attach, pcmcom_detach, NULL);
     99 
    100 const struct pcmcia_product pcmcom_products[] = {
    101 	{ PCMCIA_VENDOR_SOCKET, PCMCIA_PRODUCT_SOCKET_DUAL_RS232,
    102 	  PCMCIA_CIS_INVALID },
    103 #if 0	/* does not work */
    104 	{ PCMCIA_VENDOR_SOCKET, PCMCIA_PRODUCT_SOCKET_DUAL_RS232_A,
    105 	  PCMCIA_CIS_INVALID },
    106 #endif
    107 };
    108 const size_t pcmcom_nproducts = __arraycount(pcmcom_products);
    109 
    110 int	pcmcom_print(void *, const char *);
    111 
    112 int	pcmcom_enable(struct pcmcom_softc *);
    113 void	pcmcom_disable(struct pcmcom_softc *);
    114 
    115 int	pcmcom_intr(void *);
    116 
    117 int
    118 pcmcom_match(device_t parent, cfdata_t cf, void *aux)
    119 {
    120 	struct pcmcia_attach_args *pa = aux;
    121 
    122 	if (pcmcia_product_lookup(pa, pcmcom_products, pcmcom_nproducts,
    123 	    sizeof(pcmcom_products[0]), NULL))
    124 		return (2);	/* beat com_pcmcia */
    125 	return (0);
    126 }
    127 
    128 int
    129 pcmcom_validate_config(struct pcmcia_config_entry *cfe)
    130 {
    131 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    132 	    cfe->num_iospace < 1 || cfe->num_iospace > NSLAVES)
    133 		return (EINVAL);
    134 	return (0);
    135 }
    136 
    137 void
    138 pcmcom_attach(device_t parent, device_t self, void *aux)
    139 {
    140 	struct pcmcom_softc *sc = device_private(self);
    141 	struct pcmcia_attach_args *pa = aux;
    142 	struct pcmcia_config_entry *cfe;
    143 	int slave;
    144 	int error;
    145 	int locs[PCMCOMCF_NLOCS];
    146 
    147 	sc->sc_dev = self;
    148 
    149 	sc->sc_pf = pa->pf;
    150 
    151 	error = pcmcia_function_configure(pa->pf, pcmcom_validate_config);
    152 	if (error) {
    153 		aprint_error_dev(self, "configure failed, error=%d\n", error);
    154 		return;
    155 	}
    156 
    157 	cfe = pa->pf->cfe;
    158 	sc->sc_nslaves = cfe->num_iospace;
    159 
    160 	error = pcmcom_enable(sc);
    161 	if (error)
    162 		goto fail;
    163 
    164 	/* Attach the children. */
    165 	for (slave = 0; slave < sc->sc_nslaves; slave++) {
    166 		struct pcmcom_attach_args pca;
    167 
    168 		printf("%s: slave %d\n", device_xname(self), slave);
    169 
    170 		pca.pca_iot = cfe->iospace[slave].handle.iot;
    171 		pca.pca_ioh = cfe->iospace[slave].handle.ioh;
    172 		pca.pca_slave = slave;
    173 
    174 		locs[PCMCOMCF_SLAVE] = slave;
    175 
    176 		sc->sc_slaves[slave] =
    177 		    config_found(sc->sc_dev, &pca, pcmcom_print,
    178 				 CFARG_SUBMATCH, config_stdsubmatch,
    179 				 CFARG_IATTR, "pcmcom",
    180 				 CFARG_LOCATORS, locs,
    181 				 CFARG_EOL);
    182 	}
    183 
    184 	pcmcom_disable(sc);
    185 	sc->sc_state = PCMCOM_ATTACHED;
    186 	return;
    187 
    188 fail:
    189 	pcmcia_function_unconfigure(pa->pf);
    190 }
    191 
    192 void
    193 pcmcom_childdet(device_t self, device_t child)
    194 {
    195 	struct pcmcom_softc *sc = device_private(self);
    196 	int slave;
    197 
    198 	for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
    199 		if (sc->sc_slaves[slave] == child)
    200 			sc->sc_slaves[slave] = NULL;
    201 	}
    202 }
    203 
    204 int
    205 pcmcom_detach(device_t self, int flags)
    206 {
    207 	struct pcmcom_softc *sc = device_private(self);
    208 	int slave, error;
    209 
    210 	if (sc->sc_state != PCMCOM_ATTACHED)
    211 		return (0);
    212 
    213 	for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
    214 		if (sc->sc_slaves[slave]) {
    215 			/* Detach the child. */
    216 			error = config_detach(sc->sc_slaves[slave], flags);
    217 			if (error)
    218 				return (error);
    219 		}
    220 	}
    221 
    222 	pcmcia_function_unconfigure(sc->sc_pf);
    223 
    224 	return (0);
    225 }
    226 
    227 int
    228 pcmcom_print(void *aux, const char *pnp)
    229 {
    230 	struct pcmcom_attach_args *pca = aux;
    231 
    232 	/* only com's can attach to pcmcom's; easy... */
    233 	if (pnp)
    234 		aprint_normal("com at %s", pnp);
    235 
    236 	aprint_normal(" slave %d", pca->pca_slave);
    237 
    238 	return (UNCONF);
    239 }
    240 
    241 int
    242 pcmcom_intr(void *arg)
    243 {
    244 #if NCOM > 0
    245 	struct pcmcom_softc *sc = arg;
    246 	int i, rval = 0;
    247 
    248 	if (sc->sc_enabled_count == 0)
    249 		return (0);
    250 
    251 	for (i = 0; i < sc->sc_nslaves; i++) {
    252 		if (sc->sc_slaves[i])
    253 			rval |= comintr(sc->sc_slaves[i]);
    254 	}
    255 
    256 	return (rval);
    257 #else
    258 	return (0);
    259 #endif /* NCOM > 0 */
    260 }
    261 
    262 int
    263 pcmcom_enable(struct pcmcom_softc *sc)
    264 {
    265 	int error;
    266 
    267 	if (sc->sc_enabled_count++ != 0)
    268 		return (0);
    269 
    270 	/* Establish the interrupt. */
    271 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_SERIAL,
    272 	    pcmcom_intr, sc);
    273 	if (!sc->sc_ih)
    274 		return (EIO);
    275 
    276 	error = pcmcia_function_enable(sc->sc_pf);
    277 	if (error) {
    278 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    279 		sc->sc_ih = 0;
    280 	}
    281 
    282 	return (error);
    283 }
    284 
    285 void
    286 pcmcom_disable(struct pcmcom_softc *sc)
    287 {
    288 
    289 	if (--sc->sc_enabled_count != 0)
    290 		return;
    291 
    292 	pcmcia_function_disable(sc->sc_pf);
    293 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    294 	sc->sc_ih = 0;
    295 }
    296 
    297 /****** Here begins the com attachment code. ******/
    298 
    299 #if NCOM_PCMCOM > 0
    300 int	com_pcmcom_match(device_t, cfdata_t , void *);
    301 void	com_pcmcom_attach(device_t, device_t, void *);
    302 
    303 /* No pcmcom-specific goo in the softc; it's all in the parent. */
    304 CFATTACH_DECL_NEW(com_pcmcom, sizeof(struct com_softc),
    305     com_pcmcom_match, com_pcmcom_attach, com_detach, NULL);
    306 
    307 int	com_pcmcom_enable(struct com_softc *);
    308 void	com_pcmcom_disable(struct com_softc *);
    309 
    310 int
    311 com_pcmcom_match(device_t parent, cfdata_t cf, void *aux)
    312 {
    313 
    314 	/* Device is always present. */
    315 	return (1);
    316 }
    317 
    318 void
    319 com_pcmcom_attach(device_t parent, device_t self, void *aux)
    320 {
    321 	struct com_softc *sc = device_private(self);
    322 	struct pcmcom_attach_args *pca = aux;
    323 
    324 	sc->sc_dev = self;
    325 	com_init_regs(&sc->sc_regs, pca->pca_iot, pca->pca_ioh, -1);
    326 	sc->enabled = 1;
    327 
    328 	sc->sc_frequency = COM_FREQ;
    329 
    330 	sc->enable = com_pcmcom_enable;
    331 	sc->disable = com_pcmcom_disable;
    332 
    333 	com_attach_subr(sc);
    334 
    335 	sc->enabled = 0;
    336 }
    337 
    338 int
    339 com_pcmcom_enable(struct com_softc *sc)
    340 {
    341 	return pcmcom_enable(device_private(device_parent(sc->sc_dev)));
    342 }
    343 
    344 void
    345 com_pcmcom_disable(struct com_softc *sc)
    346 {
    347 
    348 	pcmcom_disable(device_private(device_parent(sc->sc_dev)));
    349 }
    350 #endif /* NCOM_PCMCOM > 0 */
    351