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