boca.c revision 1.39 1 /* $NetBSD: boca.c,v 1.39 2002/10/02 03:10:46 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, and information provided by David Muir Sharnoff.
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: boca.c,v 1.39 2002/10/02 03:10:46 thorpej Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/termios.h>
43 #include <sys/kernel.h>
44 #include <sys/callout.h>
45
46 #include <machine/bus.h>
47 #include <machine/intr.h>
48
49 #include <dev/ic/comreg.h>
50 #include <dev/ic/comvar.h>
51
52 #include <dev/isa/isavar.h>
53 #include <dev/isa/com_multi.h>
54
55 #define NSLAVES 8
56
57 struct boca_softc {
58 struct device sc_dev;
59 void *sc_ih;
60
61 bus_space_tag_t sc_iot;
62 int sc_iobase;
63
64 int sc_alive; /* mask of slave units attached */
65 void *sc_slaves[NSLAVES]; /* com device unit numbers */
66 bus_space_handle_t sc_slaveioh[NSLAVES];
67 struct callout fixup;
68 };
69
70 int bocaprobe __P((struct device *, struct cfdata *, void *));
71 void bocaattach __P((struct device *, struct device *, void *));
72 int bocaintr __P((void *));
73 void boca_fixup __P((void *));
74 int bocaprint __P((void *, const char *));
75
76 CFATTACH_DECL(boca, sizeof(struct boca_softc),
77 bocaprobe, bocaattach, NULL, NULL);
78
79 int
80 bocaprobe(parent, self, aux)
81 struct device *parent;
82 struct cfdata *self;
83 void *aux;
84 {
85 struct isa_attach_args *ia = aux;
86 bus_space_tag_t iot = ia->ia_iot;
87 bus_space_handle_t ioh;
88 int i, iobase, rv = 1;
89
90 /*
91 * Do the normal com probe for the first UART and assume
92 * its presence, and the ability to map the other UARTS,
93 * means there is a multiport board there.
94 * XXX Needs more robustness.
95 */
96
97 if (ia->ia_nio < 1)
98 return (0);
99 if (ia->ia_nirq < 1)
100 return (0);
101
102 if (ISA_DIRECT_CONFIG(ia))
103 return (0);
104
105 /* Disallow wildcarded i/o address. */
106 if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
107 return (0);
108 if (ia->ia_irq[0].ir_irq == ISACF_IRQ_DEFAULT)
109 return (0);
110
111 /* if the first port is in use as console, then it. */
112 if (com_is_console(iot, iobase, 0))
113 goto checkmappings;
114
115 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
116 rv = 0;
117 goto out;
118 }
119 rv = comprobe1(iot, ioh);
120 bus_space_unmap(iot, ioh, COM_NPORTS);
121 if (rv == 0)
122 goto out;
123
124 checkmappings:
125 for (i = 1, iobase = ia->ia_io[0].ir_addr; i < NSLAVES; i++) {
126 iobase += COM_NPORTS;
127
128 if (com_is_console(iot, iobase, 0))
129 continue;
130
131 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
132 rv = 0;
133 goto out;
134 }
135 bus_space_unmap(iot, ioh, COM_NPORTS);
136 }
137
138 out:
139 if (rv) {
140 ia->ia_nio = 1;
141 ia->ia_io[0].ir_size = NSLAVES * COM_NPORTS;
142
143 ia->ia_nirq = 1;
144
145 ia->ia_niomem = 0;
146 ia->ia_ndrq = 0;
147 }
148 return (rv);
149 }
150
151 int
152 bocaprint(aux, pnp)
153 void *aux;
154 const char *pnp;
155 {
156 struct commulti_attach_args *ca = aux;
157
158 if (pnp)
159 printf("com at %s", pnp);
160 printf(" slave %d", ca->ca_slave);
161 return (UNCONF);
162 }
163
164 void
165 bocaattach(parent, self, aux)
166 struct device *parent, *self;
167 void *aux;
168 {
169 struct boca_softc *sc = (void *)self;
170 struct isa_attach_args *ia = aux;
171 struct commulti_attach_args ca;
172 bus_space_tag_t iot = ia->ia_iot;
173 int i, iobase;
174
175 printf("\n");
176
177 sc->sc_iot = ia->ia_iot;
178 sc->sc_iobase = ia->ia_io[0].ir_addr;
179
180 for (i = 0; i < NSLAVES; i++) {
181 iobase = sc->sc_iobase + i * COM_NPORTS;
182 if (!com_is_console(iot, iobase, &sc->sc_slaveioh[i]) &&
183 bus_space_map(iot, iobase, COM_NPORTS, 0,
184 &sc->sc_slaveioh[i])) {
185 printf("%s: can't map i/o space for slave %d\n",
186 sc->sc_dev.dv_xname, i);
187 return;
188 }
189 }
190
191 for (i = 0; i < NSLAVES; i++) {
192
193 ca.ca_slave = i;
194 ca.ca_iot = sc->sc_iot;
195 ca.ca_ioh = sc->sc_slaveioh[i];
196 ca.ca_iobase = sc->sc_iobase + i * COM_NPORTS;
197 ca.ca_noien = 0;
198
199 sc->sc_slaves[i] = config_found(self, &ca, bocaprint);
200 if (sc->sc_slaves[i] != NULL)
201 sc->sc_alive |= 1 << i;
202 }
203
204 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
205 IST_EDGE, IPL_SERIAL, bocaintr, sc);
206 callout_init(&sc->fixup);
207 callout_reset(&sc->fixup, hz/10, boca_fixup, sc);
208 }
209
210 int
211 bocaintr(arg)
212 void *arg;
213 {
214 struct boca_softc *sc = arg;
215 bus_space_tag_t iot = sc->sc_iot;
216 int alive = sc->sc_alive;
217 int bits;
218
219 bits = bus_space_read_1(iot, sc->sc_slaveioh[0], com_scratch) & alive;
220 if (bits == 0)
221 return (0);
222
223 for (;;) {
224 #define TRY(n) \
225 if (bits & (1 << (n))) { \
226 if (comintr(sc->sc_slaves[n]) == 0) { \
227 printf("%s: bogus intr for port %d\n", \
228 sc->sc_dev.dv_xname, n); \
229 } \
230 }
231 TRY(0);
232 TRY(1);
233 TRY(2);
234 TRY(3);
235 TRY(4);
236 TRY(5);
237 TRY(6);
238 TRY(7);
239 #undef TRY
240 bits = bus_space_read_1(iot, sc->sc_slaveioh[0],
241 com_scratch) & alive;
242 if (bits == 0) {
243 return (1);
244 }
245 }
246 }
247
248 void
249 boca_fixup(v)
250 void *v;
251 {
252 struct boca_softc *sc = v;
253 int alive = sc->sc_alive;
254 int i, s;
255
256 s = splserial();
257
258 for (i = 0; i < 8; i++) {
259 if (alive & (1 << (i)))
260 comintr(sc->sc_slaves[i]);
261 }
262 callout_reset(&sc->fixup, hz/10, boca_fixup, sc);
263 splx(s);
264 }
265