xref: /netbsd-src/sys/dev/isa/rtfps.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: rtfps.c,v 1.35 1997/10/20 18:43:19 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
5  * Copyright (c) 1995 Charles 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 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/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/termios.h>
40 
41 #include <machine/bus.h>
42 #include <machine/intr.h>
43 
44 #include <dev/ic/comreg.h>
45 #include <dev/ic/comvar.h>
46 
47 #include <dev/isa/isavar.h>
48 #include <dev/isa/com_multi.h>
49 
50 #define	NSLAVES	4
51 
52 struct rtfps_softc {
53 	struct device sc_dev;
54 	void *sc_ih;
55 
56 	bus_space_tag_t sc_iot;
57 	int sc_iobase;
58 	int sc_irqport;
59 	bus_space_handle_t sc_irqioh;
60 
61 	int sc_alive;			/* mask of slave units attached */
62 	void *sc_slaves[NSLAVES];	/* com device unit numbers */
63 	bus_space_handle_t sc_slaveioh[NSLAVES];
64 };
65 
66 int rtfpsprobe __P((struct device *, void *, void *));
67 void rtfpsattach __P((struct device *, struct device *, void *));
68 int rtfpsintr __P((void *));
69 int rtfpsprint __P((void *, const char *));
70 
71 struct cfattach rtfps_ca = {
72 	sizeof(struct rtfps_softc), rtfpsprobe, rtfpsattach
73 };
74 
75 struct cfdriver rtfps_cd = {
76 	NULL, "rtfps", DV_TTY
77 };
78 
79 int
80 rtfpsprobe(parent, self, aux)
81 	struct device *parent;
82 	void *self;
83 	void *aux;
84 {
85 	struct isa_attach_args *ia = aux;
86 	int iobase = ia->ia_iobase;
87 	bus_space_tag_t iot = ia->ia_iot;
88 	bus_space_handle_t ioh;
89 	int i, rv = 1;
90 
91 	/*
92 	 * Do the normal com probe for the first UART and assume
93 	 * its presence, and the ability to map the other UARTS,
94 	 * means there is a multiport board there.
95 	 * XXX Needs more robustness.
96 	 */
97 
98 	/* Disallow wildcarded i/o address. */
99 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
100 		return (0);
101 
102 	/* if the first port is in use as console, then it. */
103 	if (com_is_console(iot, iobase, 0))
104 		goto checkmappings;
105 
106 	if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
107 		rv = 0;
108 		goto out;
109 	}
110 	rv = comprobe1(iot, ioh, iobase);
111 	bus_space_unmap(iot, ioh, COM_NPORTS);
112 	if (rv == 0)
113 		goto out;
114 
115 checkmappings:
116 	for (i = 1; i < NSLAVES; i++) {
117 		iobase += COM_NPORTS;
118 
119 		if (com_is_console(iot, iobase, 0))
120 			continue;
121 
122 		if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
123 			rv = 0;
124 			goto out;
125 		}
126 		bus_space_unmap(iot, ioh, COM_NPORTS);
127 	}
128 
129 out:
130 	if (rv)
131 		ia->ia_iosize = NSLAVES * COM_NPORTS;
132 	return (rv);
133 }
134 
135 int
136 rtfpsprint(aux, pnp)
137 	void *aux;
138 	const char *pnp;
139 {
140 	struct commulti_attach_args *ca = aux;
141 
142 	if (pnp)
143 		printf("com at %s", pnp);
144 	printf(" slave %d", ca->ca_slave);
145 	return (UNCONF);
146 }
147 
148 void
149 rtfpsattach(parent, self, aux)
150 	struct device *parent, *self;
151 	void *aux;
152 {
153 	struct rtfps_softc *sc = (void *)self;
154 	struct isa_attach_args *ia = aux;
155 	struct commulti_attach_args ca;
156 	static int irqport[] = {
157 		IOBASEUNK, IOBASEUNK, IOBASEUNK, IOBASEUNK,
158 		IOBASEUNK, IOBASEUNK, IOBASEUNK, IOBASEUNK,
159 		IOBASEUNK,     0x2f2,     0x6f2,     0x6f3,
160 		IOBASEUNK, IOBASEUNK, IOBASEUNK, IOBASEUNK
161 	};
162 	bus_space_tag_t iot = ia->ia_iot;
163 	int i, iobase;
164 
165 	printf("\n");
166 
167 	sc->sc_iot = ia->ia_iot;
168 	sc->sc_iobase = ia->ia_iobase;
169 
170 	if (ia->ia_irq >= 16 || irqport[ia->ia_irq] == IOBASEUNK) {
171 		printf("%s: invalid irq\n", sc->sc_dev.dv_xname);
172 		return;
173 	}
174 	sc->sc_irqport = irqport[ia->ia_irq];
175 
176 	for (i = 0; i < NSLAVES; i++) {
177 		iobase = sc->sc_iobase + i * COM_NPORTS;
178 		if (!com_is_console(iot, iobase, &sc->sc_slaveioh[i]) &&
179 		    bus_space_map(iot, iobase, COM_NPORTS, 0,
180 			&sc->sc_slaveioh[i])) {
181 			printf("%s: can't map i/o space for slave %d\n",
182 			    sc->sc_dev.dv_xname, i);
183 			return;
184 		}
185 	}
186 	if (bus_space_map(iot, sc->sc_irqport, 1, 0, &sc->sc_irqioh)) {
187 		printf("%s: can't map irq port at 0x%x\n", sc->sc_dev.dv_xname,
188 		    sc->sc_irqport);
189 		return;
190 	}
191 
192 	bus_space_write_1(iot, sc->sc_irqioh, 0, 0);
193 
194 	for (i = 0; i < NSLAVES; i++) {
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, rtfpsprint);
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, IST_EDGE,
207 	    IPL_SERIAL, rtfpsintr, sc);
208 }
209 
210 int
211 rtfpsintr(arg)
212 	void *arg;
213 {
214 	struct rtfps_softc *sc = arg;
215 	bus_space_tag_t iot = sc->sc_iot;
216 	int alive = sc->sc_alive;
217 
218 	bus_space_write_1(iot, sc->sc_irqioh, 0, 0);
219 
220 #define	TRY(n) \
221 	if (alive & (1 << (n))) \
222 		comintr(sc->sc_slaves[n]);
223 	TRY(0);
224 	TRY(1);
225 	TRY(2);
226 	TRY(3);
227 #undef TRY
228 
229 	return (1);
230 }
231