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