Home | History | Annotate | Line # | Download | only in isa
moxa_isa.c revision 1.1
      1 /*	$NetBSD: moxa_isa.c,v 1.1 2000/11/20 19:24:39 jdolecek Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
      5  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
      6  *
      7  * This code is derived from public-domain software written by
      8  * Roland McGrath.
      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 Charles M. Hannum.
     21  * 4. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/device.h>
     39 #include <sys/termios.h>
     40 
     41 #include <machine/bus.h>
     42 #include <machine/intr.h>
     43 
     44 #include <dev/ic/comreg.h>
     45 #include <dev/ic/comvar.h>
     46 
     47 #include <dev/isa/isavar.h>
     48 #include <dev/isa/com_multi.h>
     49 
     50 #define	NSLAVES	8
     51 
     52 struct moxa_isa_softc {
     53 	struct device sc_dev;
     54 	void *sc_ih;
     55 
     56 	bus_space_tag_t sc_iot;
     57 	int sc_iobase;
     58 
     59 	int sc_alive;			/* mask of slave units attached */
     60 	void *sc_slaves[NSLAVES];	/* com device unit numbers */
     61 	bus_space_handle_t sc_slaveioh[NSLAVES];
     62 };
     63 
     64 int moxa_isaprobe __P((struct device *, struct cfdata *, void *));
     65 void moxa_isaattach __P((struct device *, struct device *, void *));
     66 int moxa_isaintr __P((void *));
     67 int moxa_isaprint __P((void *, const char *));
     68 
     69 struct cfattach moxa_isa_ca = {
     70 	sizeof(struct moxa_isa_softc), moxa_isaprobe, moxa_isaattach
     71 };
     72 
     73 int
     74 moxa_isaprobe(parent, self, aux)
     75 	struct device *parent;
     76 	struct cfdata *self;
     77 	void *aux;
     78 {
     79 	struct isa_attach_args *ia = aux;
     80 	int iobase = ia->ia_iobase;
     81 	bus_space_tag_t iot = ia->ia_iot;
     82 	bus_space_handle_t ioh;
     83 	int i, rv = 1;
     84 
     85 	/*
     86 	 * Do the normal com probe for the first UART and assume
     87 	 * its presence, and the ability to map the other UARTS,
     88 	 * means there is a multiport board there.
     89 	 * XXX Needs more robustness.
     90 	 */
     91 
     92 	/* Disallow wildcarded i/o address. */
     93 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
     94 		return (0);
     95 
     96 	/* if the first port is in use as console, then it. */
     97 	if (com_is_console(iot, iobase, 0))
     98 		goto checkmappings;
     99 
    100 	if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
    101 		rv = 0;
    102 		goto out;
    103 	}
    104 	rv = comprobe1(iot, ioh);
    105 	bus_space_unmap(iot, ioh, COM_NPORTS);
    106 	if (rv == 0)
    107 		goto out;
    108 
    109 checkmappings:
    110 	for (i = 1; i < NSLAVES; i++) {
    111 		iobase += COM_NPORTS;
    112 
    113 		if (com_is_console(iot, iobase, 0))
    114 			continue;
    115 
    116 		if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
    117 			rv = 0;
    118 			goto out;
    119 		}
    120 		bus_space_unmap(iot, ioh, COM_NPORTS);
    121 	}
    122 
    123 out:
    124 	if (rv)
    125 		ia->ia_iosize = NSLAVES * COM_NPORTS;
    126 	return (rv);
    127 }
    128 
    129 int
    130 moxa_isaprint(aux, pnp)
    131 	void *aux;
    132 	const char *pnp;
    133 {
    134 	struct commulti_attach_args *ca = aux;
    135 
    136 	if (pnp)
    137 		printf("com at %s", pnp);
    138 	printf(" slave %d", ca->ca_slave);
    139 	return (UNCONF);
    140 }
    141 
    142 void
    143 moxa_isaattach(parent, self, aux)
    144 	struct device *parent, *self;
    145 	void *aux;
    146 {
    147 	struct moxa_isa_softc *sc = (void *)self;
    148 	struct isa_attach_args *ia = aux;
    149 	struct commulti_attach_args ca;
    150 	bus_space_tag_t iot = ia->ia_iot;
    151 	int i, iobase;
    152 
    153 	printf("\n");
    154 
    155 	sc->sc_iot = ia->ia_iot;
    156 	sc->sc_iobase = ia->ia_iobase;
    157 
    158 	for (i = 0; i < NSLAVES; i++) {
    159 		iobase = sc->sc_iobase + i * COM_NPORTS;
    160 		if (!com_is_console(iot, iobase, &sc->sc_slaveioh[i]) &&
    161 		    bus_space_map(iot, iobase, COM_NPORTS, 0,
    162 			&sc->sc_slaveioh[i])) {
    163 			printf("%s: can't map i/o space for slave %d\n",
    164 			    sc->sc_dev.dv_xname, i);
    165 			return;
    166 		}
    167 	}
    168 
    169 	for (i = 0; i < NSLAVES; i++) {
    170 		ca.ca_slave = i;
    171 		ca.ca_iot = sc->sc_iot;
    172 		ca.ca_ioh = sc->sc_slaveioh[i];
    173 		ca.ca_iobase = sc->sc_iobase + i * COM_NPORTS;
    174 		ca.ca_noien = 1;
    175 
    176 		sc->sc_slaves[i] = config_found(self, &ca, moxa_isaprint);
    177 		if (sc->sc_slaves[i] != NULL)
    178 			sc->sc_alive |= 1 << i;
    179 	}
    180 
    181 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    182 	    IPL_SERIAL, moxa_isaintr, sc);
    183 }
    184 
    185 int
    186 moxa_isaintr(arg)
    187 	void *arg;
    188 {
    189 	struct moxa_isa_softc *sc = arg;
    190 	int bits;
    191 	int rv;
    192 
    193 	bits = sc->sc_alive;
    194 	if (bits == 0)
    195 		return (0);
    196 
    197 	for (;;rv = 0) {
    198 #define	TRY(n) \
    199 		if (bits & (1 << (n))) \
    200 			rv += comintr(sc->sc_slaves[n]);
    201 		TRY(0);
    202 		TRY(1);
    203 		TRY(2);
    204 		TRY(3);
    205 		TRY(4);
    206 		TRY(5);
    207 		TRY(6);
    208 		TRY(7);
    209 #undef TRY
    210 		if (rv == 0)
    211 			return (1);
    212  	}
    213 }
    214