xref: /openbsd-src/sys/dev/isa/ast.c (revision 7a1e2957ddfe3b414f2f7af668511d06c4c23193)
1 /*	$OpenBSD: ast.c,v 1.10 1996/05/10 12:35:41 deraadt Exp $	*/
2 /*	$NetBSD: ast.c,v 1.27 1996/05/05 19:49:54 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
6  * Copyright (c) 1995 Charles Hannum.  All rights reserved.
7  *
8  * This code is derived from public-domain software written by
9  * Roland McGrath.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by Charles Hannum.
22  * 4. The name of the author may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/termios.h>
41 
42 #ifdef i386							/* XXX */
43 #include <machine/cpu.h>					/* XXX */
44 #else								/* XXX */
45 #include <machine/intr.h>
46 #endif								/* XXX */
47 #include <machine/bus.h>
48 
49 #include <dev/isa/isavar.h>
50 #include <dev/isa/comreg.h>
51 #include <dev/isa/comvar.h>
52 
53 #define	NSLAVES	4
54 
55 struct ast_softc {
56 	struct device sc_dev;
57 	void *sc_ih;
58 
59 	bus_chipset_tag_t sc_bc;
60 	int sc_iobase;
61 
62 	int sc_alive;			/* mask of slave units attached */
63 	void *sc_slaves[NSLAVES];	/* com device unit numbers */
64 	bus_io_handle_t sc_slaveioh[NSLAVES];
65 };
66 
67 int astprobe __P((struct device *, void *, void *));
68 void astattach __P((struct device *, struct device *, void *));
69 int astintr __P((void *));
70 int astprint __P((void *, char *));
71 
72 struct cfattach ast_ca = {
73 	sizeof(struct ast_softc), astprobe, astattach
74 };
75 
76 struct cfdriver ast_cd = {
77 	NULL, "ast", DV_TTY
78 };
79 
80 int
81 astprobe(parent, self, aux)
82 	struct device *parent;
83 	void *self;
84 	void *aux;
85 {
86 	struct isa_attach_args *ia = aux;
87 	int iobase = ia->ia_iobase;
88 	bus_chipset_tag_t bc = ia->ia_bc;
89 	bus_io_handle_t ioh;
90 	int i, rv = 1;
91 
92 	/*
93 	 * Do the normal com probe for the first UART and assume
94 	 * its presence, and the ability to map the other UARTS,
95 	 * means there is a multiport board there.
96 	 * XXX Needs more robustness.
97 	 */
98 
99 	/* if the first port is in use as console, then it. */
100 	if (iobase == comconsaddr && !comconsattached)
101 		goto checkmappings;
102 
103 	if (bus_io_map(bc, iobase, COM_NPORTS, &ioh)) {
104 		rv = 0;
105 		goto out;
106 	}
107 	rv = comprobe1(bc, ioh, iobase);
108 	bus_io_unmap(bc, ioh, COM_NPORTS);
109 	if (rv == 0)
110 		goto out;
111 
112 checkmappings:
113 	for (i = 1; i < NSLAVES; i++) {
114 		iobase += COM_NPORTS;
115 
116 		if (iobase == comconsaddr && !comconsattached)
117 			continue;
118 
119 		if (bus_io_map(bc, iobase, COM_NPORTS, &ioh)) {
120 			rv = 0;
121 			goto out;
122 		}
123 		bus_io_unmap(bc, ioh, COM_NPORTS);
124 	}
125 
126 out:
127 	if (rv)
128 		ia->ia_iosize = NSLAVES * COM_NPORTS;
129 	return (rv);
130 }
131 
132 int
133 astprint(aux, pnp)
134 	void *aux;
135 	char *pnp;
136 {
137 	struct commulti_attach_args *ca = aux;
138 
139 	if (pnp)
140 		printf("com at %s", pnp);
141 	printf(" slave %d", ca->ca_slave);
142 	return (UNCONF);
143 }
144 
145 void
146 astattach(parent, self, aux)
147 	struct device *parent, *self;
148 	void *aux;
149 {
150 	struct ast_softc *sc = (void *)self;
151 	struct isa_attach_args *ia = aux;
152 	struct commulti_attach_args ca;
153 	bus_chipset_tag_t bc = ia->ia_bc;
154 	int i;
155 
156 	sc->sc_bc = ia->ia_bc;
157 	sc->sc_iobase = ia->ia_iobase;
158 
159 	for (i = 0; i < NSLAVES; i++)
160 		if (bus_io_map(sc->sc_bc, sc->sc_iobase + i * COM_NPORTS,
161 		    COM_NPORTS, &sc->sc_slaveioh[i]))
162 			panic("astattach: couldn't map slave %d", i);
163 
164 	/*
165 	 * Enable the master interrupt.
166 	 */
167 	bus_io_write_1(sc->sc_bc, sc->sc_slaveioh[3], 7, 0x80);
168 
169 	printf("\n");
170 
171 	for (i = 0; i < NSLAVES; i++) {
172 		ca.ca_slave = i;
173 		ca.ca_bc = sc->sc_bc;
174 		ca.ca_ioh = sc->sc_slaveioh[i];
175 		ca.ca_iobase = sc->sc_iobase + i * COM_NPORTS;
176 		ca.ca_noien = 1;
177 
178 		sc->sc_slaves[i] = config_found(self, &ca, astprint);
179 		if (sc->sc_slaves[i] != NULL)
180 			sc->sc_alive |= 1 << i;
181 	}
182 
183 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
184 	    IPL_TTY, astintr, sc, sc->sc_dev.dv_xname);
185 }
186 
187 int
188 astintr(arg)
189 	void *arg;
190 {
191 	struct ast_softc *sc = arg;
192 	bus_chipset_tag_t bc = sc->sc_bc;
193 	int alive = sc->sc_alive;
194 	int bits;
195 
196 	bits = ~bus_io_read_1(bc, sc->sc_slaveioh[3], 7) & alive;
197 	if (bits == 0)
198 		return (0);
199 
200 	for (;;) {
201 #define	TRY(n) \
202 		if (bits & (1 << (n))) \
203 			comintr(sc->sc_slaves[n]);
204 		TRY(0);
205 		TRY(1);
206 		TRY(2);
207 		TRY(3);
208 #undef TRY
209 		bits = ~bus_io_read_1(bc, sc->sc_slaveioh[3], 7) & alive;
210 		if (bits == 0)
211 			return (1);
212  	}
213 }
214