1 /*        $NetBSD: ast.c,v 1.68 2021/08/07 16:19:12 thorpej 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/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ast.c,v 1.68 2021/08/07 16:19:12 thorpej Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/termios.h>
43 
44 #include <sys/bus.h>
45 #include <sys/intr.h>
46 
47 #include <dev/ic/comreg.h>
48 #include <dev/ic/comvar.h>
49 
50 #include <dev/isa/isavar.h>
51 #include <dev/isa/com_multi.h>
52 
53 #define   NSLAVES   4
54 
55 struct ast_softc {
56           void *sc_ih;
57 
58           bus_space_tag_t sc_iot;
59           int sc_iobase;
60 
61           int sc_alive;                           /* mask of slave units attached */
62           void *sc_slaves[NSLAVES];     /* com device softc pointers */
63           bus_space_handle_t sc_slaveioh[NSLAVES];
64 };
65 
66 int astprobe(device_t, cfdata_t, void *);
67 void astattach(device_t, device_t, void *);
68 int astintr(void *);
69 
70 CFATTACH_DECL_NEW(ast, sizeof(struct ast_softc),
71     astprobe, astattach, NULL, NULL);
72 
73 int
astprobe(device_t parent,cfdata_t self,void * aux)74 astprobe(device_t parent, cfdata_t self, void *aux)
75 {
76           struct isa_attach_args *ia = aux;
77           bus_space_tag_t iot = ia->ia_iot;
78           bus_space_handle_t ioh;
79           int i, iobase, rv = 1;
80 
81           /*
82            * Do the normal com probe for the first UART and assume
83            * its presence, and the ability to map the other UARTS,
84            * means there is a multiport board there.
85            * XXX Needs more robustness.
86            */
87 
88           if (ia->ia_nio < 1)
89                     return (0);
90           if (ia->ia_nirq < 1)
91                     return (0);
92 
93           if (ISA_DIRECT_CONFIG(ia))
94                     return (0);
95 
96           /* Disallow wildcarded i/o address. */
97           if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
98                     return (0);
99 
100           if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
101                     return (0);
102 
103           /* if the first port is in use as console, then it. */
104           if (com_is_console(iot, ia->ia_io[0].ir_addr, 0))
105                     goto checkmappings;
106 
107           if (bus_space_map(iot, ia->ia_io[0].ir_addr, COM_NPORTS, 0, &ioh)) {
108                     rv = 0;
109                     goto out;
110           }
111           rv = comprobe1(iot, ioh);
112           bus_space_unmap(iot, ioh, COM_NPORTS);
113           if (rv == 0)
114                     goto out;
115 
116 checkmappings:
117           for (i = 1, iobase = ia->ia_io[0].ir_addr; i < NSLAVES; i++) {
118                     iobase += COM_NPORTS;
119 
120                     if (com_is_console(iot, iobase, 0))
121                               continue;
122 
123                     if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
124                               rv = 0;
125                               goto out;
126                     }
127                     bus_space_unmap(iot, ioh, COM_NPORTS);
128           }
129 
130 out:
131           if (rv) {
132                     ia->ia_nio = 1;
133                     ia->ia_io[0].ir_size = NSLAVES * COM_NPORTS;
134 
135                     ia->ia_nirq = 1;
136 
137                     ia->ia_niomem = 0;
138                     ia->ia_ndrq = 0;
139           }
140           return (rv);
141 }
142 
143 void
astattach(device_t parent,device_t self,void * aux)144 astattach(device_t parent, device_t self, void *aux)
145 {
146           struct ast_softc *sc = device_private(self);
147           struct isa_attach_args *ia = aux;
148           struct commulti_attach_args ca;
149           bus_space_tag_t iot = ia->ia_iot;
150           int i, iobase;
151           device_t slave;
152 
153           printf("\n");
154 
155           sc->sc_iot = ia->ia_iot;
156           sc->sc_iobase = ia->ia_io[0].ir_addr;
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                               aprint_error_dev(self,
164                                   "can't map i/o space for slave %d\n", i);
165                               return;
166                     }
167           }
168 
169           /*
170            * Enable the master interrupt.
171            */
172           bus_space_write_1(iot, sc->sc_slaveioh[3], com_scratch, 0x80);
173 
174           for (i = 0; i < NSLAVES; i++) {
175                     ca.ca_slave = i;
176                     ca.ca_iot = sc->sc_iot;
177                     ca.ca_ioh = sc->sc_slaveioh[i];
178                     ca.ca_iobase = sc->sc_iobase + i * COM_NPORTS;
179                     ca.ca_noien = 1;
180 
181                     slave = config_found(self, &ca, commultiprint, CFARGS_NONE);
182                     if (slave != NULL) {
183                               sc->sc_alive |= 1 << i;
184                               sc->sc_slaves[i] = device_private(slave);
185                     }
186           }
187 
188           sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
189               IST_EDGE, IPL_SERIAL, astintr, sc);
190 }
191 
192 int
astintr(void * arg)193 astintr(void *arg)
194 {
195           struct ast_softc *sc = arg;
196           bus_space_tag_t iot = sc->sc_iot;
197           int alive = sc->sc_alive;
198           int bits;
199 
200           bits = ~bus_space_read_1(iot, sc->sc_slaveioh[3], com_scratch) & alive;
201           if (bits == 0)
202                     return (0);
203 
204           for (;;) {
205 #define   TRY(n) \
206                     if (bits & (1 << (n))) \
207                               comintr(sc->sc_slaves[n]);
208                     TRY(0);
209                     TRY(1);
210                     TRY(2);
211                     TRY(3);
212 #undef TRY
213                     bits = ~bus_space_read_1(iot, sc->sc_slaveioh[3],
214                         com_scratch) & alive;
215                     if (bits == 0)
216                               return (1);
217           }
218 }
219