xref: /openbsd-src/sys/dev/isa/ast.c (revision d724e01ae4dac35949585b9083e28ff2ba35b0b5)
1 /*	$OpenBSD: ast.c,v 1.8 1996/04/21 22:22:48 deraadt Exp $	*/
2 /*	$NetBSD: ast.c,v 1.26 1996/04/15 18:55:23 cgd 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/device.h>
39 #include <sys/termios.h>
40 
41 #ifdef i386							/* XXX */
42 #include <machine/cpu.h>					/* XXX */
43 #else								/* XXX */
44 #include <machine/intr.h>
45 #endif								/* XXX */
46 #include <machine/bus.h>
47 
48 #include <dev/isa/isavar.h>
49 #include <dev/isa/comreg.h>
50 #include <dev/isa/comvar.h>
51 
52 #define	NSLAVES	4
53 
54 struct ast_softc {
55 	struct device sc_dev;
56 	void *sc_ih;
57 
58 	bus_chipset_tag_t sc_bc;
59 	int sc_iobase;
60 
61 	int sc_alive;			/* mask of slave units attached */
62 	void *sc_slaves[NSLAVES];	/* com device unit numbers */
63 	bus_io_handle_t sc_slaveioh[NSLAVES];
64 };
65 
66 int astprobe();
67 void astattach();
68 int astintr __P((void *));
69 
70 struct cfattach ast_ca = {
71 	sizeof(struct ast_softc), astprobe, astattach
72 };
73 
74 struct cfdriver ast_cd = {
75 	NULL, "ast", DV_TTY
76 };
77 
78 int
79 astprobe(parent, self, aux)
80 	struct device *parent, *self;
81 	void *aux;
82 {
83 	struct isa_attach_args *ia = aux;
84 	int iobase = ia->ia_iobase;
85 	bus_chipset_tag_t bc = ia->ia_bc;
86 	bus_io_handle_t ioh;
87 	int i, rv = 1;
88 
89 	/*
90 	 * Do the normal com probe for the first UART and assume
91 	 * its presence, and the ability to map the other UARTS,
92 	 * means there is a multiport board there.
93 	 * XXX Needs more robustness.
94 	 */
95 
96 	/* if the first port is in use as console, then it. */
97 	if (iobase == comconsaddr && !comconsattached)
98 		goto checkmappings;
99 
100 	if (bus_io_map(bc, iobase, COM_NPORTS, &ioh)) {
101 		rv = 0;
102 		goto out;
103 	}
104 	rv = comprobe1(bc, ioh, iobase);
105 	bus_io_unmap(bc, ioh, COM_NPORTS);
106 	if (rv == 0)
107 		goto out;
108 
109 checkmappings:
110 	for (i = 1; i < NSLAVES; i++) {
111 		iobase += COM_NPORTS;
112 
113 		if (iobase == comconsaddr && !comconsattached)
114 			continue;
115 
116 		if (bus_io_map(bc, iobase, COM_NPORTS, &ioh)) {
117 			rv = 0;
118 			goto out;
119 		}
120 		bus_io_unmap(bc, ioh, COM_NPORTS);
121 	}
122 
123 out:
124 	if (rv)
125 		ia->ia_iosize = NSLAVES * COM_NPORTS;
126 	return (rv);
127 }
128 
129 int
130 astprint(aux, pnp)
131 	void *aux;
132 	char *pnp;
133 {
134 	struct commulti_attach_args *ca = aux;
135 
136 	if (pnp)
137 		printf("com at %s", pnp);
138 	printf(" slave %d", ca->ca_slave);
139 	return (UNCONF);
140 }
141 
142 void
143 astattach(parent, self, aux)
144 	struct device *parent, *self;
145 	void *aux;
146 {
147 	struct ast_softc *sc = (void *)self;
148 	struct isa_attach_args *ia = aux;
149 	struct commulti_attach_args ca;
150 	int i, subunit;
151 
152 	sc->sc_bc = ia->ia_bc;
153 	sc->sc_iobase = ia->ia_iobase;
154 
155 	for (i = 0; i < NSLAVES; i++)
156 		if (bus_io_map(bc, sc->sc_iobase + i * COM_NPORTS, COM_NPORTS,
157 		    &sc->sc_slaveioh[i]))
158 			panic("astattach: couldn't map slave %d", i);
159 
160 	/*
161 	 * Enable the master interrupt.
162 	 */
163 	bus_io_write_1(bc, sc->sc_slaveioh[3], 7, 0x80);
164 
165 	printf("\n");
166 
167 	for (i = 0; i < NSLAVES; i++) {
168 		struct cfdata *match;
169 
170 		ca.ca_slave = i;
171 		ca.ca_bc = sc->sc_bc;
172 		ca.ca_ioh = sc->sc_slaveioh[i];
173 		ca.ca_iobase = sc->sc_iobase + i * COM_NPORTS;
174 		ca.ca_noien = 1;
175 
176 		sc->sc_slaves[i] = config_found(self, &ca, astprint);
177 		if (sc->sc_slaves[i] != NULL)
178 			sc->sc_alive |= 1 << i;
179 	}
180 
181 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
182 	    IPL_TTY, astintr, sc, sc->sc_dev.dv_xname);
183 }
184 
185 int
186 astintr(arg)
187 	void *arg;
188 {
189 	struct ast_softc *sc = arg;
190 	bus_chipset_tag_t bc = sc->sc_bc;
191 	int alive = sc->sc_alive;
192 	int bits;
193 
194 	bits = ~bus_io_read_1(bc, sc->sc_slaveioh[3], 7) & alive;
195 	if (bits == 0)
196 		return (0);
197 
198 	for (;;) {
199 #define	TRY(n) \
200 		if (bits & (1 << (n))) \
201 			comintr(sc->sc_slaves[n]);
202 		TRY(0);
203 		TRY(1);
204 		TRY(2);
205 		TRY(3);
206 #undef TRY
207 		bits = ~bus_io_read_1(bc, sc->sc_slaveioh[3], 7) & alive;
208 		if (bits == 0)
209 			return (1);
210  	}
211 }
212