xref: /netbsd/sys/dev/isa/boca.c (revision c4a72b64)
1 /*	$NetBSD: boca.c,v 1.40 2002/11/17 22:49:56 chs 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.40 2002/11/17 22:49:56 chs 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 	iobase = ia->ia_io[0].ir_addr;
112 
113 	/* if the first port is in use as console, then it. */
114 	if (com_is_console(iot, iobase, 0))
115 		goto checkmappings;
116 
117 	if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
118 		rv = 0;
119 		goto out;
120 	}
121 	rv = comprobe1(iot, ioh);
122 	bus_space_unmap(iot, ioh, COM_NPORTS);
123 	if (rv == 0)
124 		goto out;
125 
126 checkmappings:
127 	for (i = 1; i < NSLAVES; i++) {
128 		iobase += COM_NPORTS;
129 
130 		if (com_is_console(iot, iobase, 0))
131 			continue;
132 
133 		if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
134 			rv = 0;
135 			goto out;
136 		}
137 		bus_space_unmap(iot, ioh, COM_NPORTS);
138 	}
139 
140 out:
141 	if (rv) {
142 		ia->ia_nio = 1;
143 		ia->ia_io[0].ir_size = NSLAVES * COM_NPORTS;
144 
145 		ia->ia_nirq = 1;
146 
147 		ia->ia_niomem = 0;
148 		ia->ia_ndrq = 0;
149 	}
150 	return (rv);
151 }
152 
153 int
154 bocaprint(aux, pnp)
155 	void *aux;
156 	const char *pnp;
157 {
158 	struct commulti_attach_args *ca = aux;
159 
160 	if (pnp)
161 		printf("com at %s", pnp);
162 	printf(" slave %d", ca->ca_slave);
163 	return (UNCONF);
164 }
165 
166 void
167 bocaattach(parent, self, aux)
168 	struct device *parent, *self;
169 	void *aux;
170 {
171 	struct boca_softc *sc = (void *)self;
172 	struct isa_attach_args *ia = aux;
173 	struct commulti_attach_args ca;
174 	bus_space_tag_t iot = ia->ia_iot;
175 	int i, iobase;
176 
177 	printf("\n");
178 
179 	sc->sc_iot = ia->ia_iot;
180 	sc->sc_iobase = ia->ia_io[0].ir_addr;
181 
182 	for (i = 0; i < NSLAVES; i++) {
183 		iobase = sc->sc_iobase + i * COM_NPORTS;
184 		if (!com_is_console(iot, iobase, &sc->sc_slaveioh[i]) &&
185 		    bus_space_map(iot, iobase, COM_NPORTS, 0,
186 			&sc->sc_slaveioh[i])) {
187 			printf("%s: can't map i/o space for slave %d\n",
188 			     sc->sc_dev.dv_xname, i);
189 			return;
190 		}
191 	}
192 
193 	for (i = 0; i < NSLAVES; i++) {
194 
195 		ca.ca_slave = i;
196 		ca.ca_iot = sc->sc_iot;
197 		ca.ca_ioh = sc->sc_slaveioh[i];
198 		ca.ca_iobase = sc->sc_iobase + i * COM_NPORTS;
199 		ca.ca_noien = 0;
200 
201 		sc->sc_slaves[i] = config_found(self, &ca, bocaprint);
202 		if (sc->sc_slaves[i] != NULL)
203 			sc->sc_alive |= 1 << i;
204 	}
205 
206 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
207 	    IST_EDGE, IPL_SERIAL, bocaintr, sc);
208 	callout_init(&sc->fixup);
209 	callout_reset(&sc->fixup, hz/10, boca_fixup, sc);
210 }
211 
212 int
213 bocaintr(arg)
214 	void *arg;
215 {
216 	struct boca_softc *sc = arg;
217 	bus_space_tag_t iot = sc->sc_iot;
218 	int alive = sc->sc_alive;
219 	int bits;
220 
221 	bits = bus_space_read_1(iot, sc->sc_slaveioh[0], com_scratch) & alive;
222 	if (bits == 0)
223 		return (0);
224 
225 	for (;;) {
226 #define	TRY(n) \
227 		if (bits & (1 << (n))) { \
228 			if (comintr(sc->sc_slaves[n]) == 0) { \
229 				printf("%s: bogus intr for port %d\n", \
230 				    sc->sc_dev.dv_xname, n); \
231 			} \
232 		}
233 		TRY(0);
234 		TRY(1);
235 		TRY(2);
236 		TRY(3);
237 		TRY(4);
238 		TRY(5);
239 		TRY(6);
240 		TRY(7);
241 #undef TRY
242 		bits = bus_space_read_1(iot, sc->sc_slaveioh[0],
243 		    com_scratch) & alive;
244 		if (bits == 0) {
245 			return (1);
246 		}
247  	}
248 }
249 
250 void
251 boca_fixup(v)
252 	void *v;
253 {
254 	struct boca_softc *sc = v;
255 	int alive = sc->sc_alive;
256 	int i, s;
257 
258 	s = splserial();
259 
260 	for (i = 0; i < 8; i++) {
261 		if (alive & (1 << (i)))
262 			comintr(sc->sc_slaves[i]);
263 	}
264 	callout_reset(&sc->fixup, hz/10, boca_fixup, sc);
265 	splx(s);
266 }
267