1 /*	$OpenBSD: rtfps.c,v 1.19 2002/03/14 01:26:56 millert Exp $       */
2 /*	$NetBSD: rtfps.c,v 1.27 1996/10/21 22:41:18 thorpej 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 #include <machine/bus.h>
43 #include <machine/intr.h>
44 
45 #include <dev/isa/isavar.h>
46 #include <dev/ic/comreg.h>
47 #include <dev/ic/comvar.h>
48 
49 #define	NSLAVES	4
50 
51 struct rtfps_softc {
52 	struct device sc_dev;
53 	void *sc_ih;
54 
55 	bus_space_tag_t sc_iot;
56 	int sc_iobase;
57 	int sc_irqport;
58 	bus_space_handle_t sc_irqioh;
59 
60 	int sc_alive;			/* mask of slave units attached */
61 	void *sc_slaves[NSLAVES];	/* com device unit numbers */
62 	bus_space_handle_t sc_slaveioh[NSLAVES];
63 };
64 
65 int rtfpsprobe(struct device *, void *, void *);
66 void rtfpsattach(struct device *, struct device *, void *);
67 int rtfpsintr(void *);
68 int rtfpsprint(void *, const char *);
69 
70 struct cfattach rtfps_ca = {
71 	sizeof(struct rtfps_softc), rtfpsprobe, rtfpsattach
72 };
73 
74 struct cfdriver rtfps_cd = {
75 	NULL, "rtfps", DV_TTY
76 };
77 
78 int
rtfpsprobe(parent,self,aux)79 rtfpsprobe(parent, self, aux)
80 	struct device *parent;
81 	void *self;
82 	void *aux;
83 {
84 	struct isa_attach_args *ia = aux;
85 	int iobase = ia->ia_iobase;
86 	bus_space_tag_t iot = ia->ia_iot;
87 	bus_space_handle_t ioh;
88 	int i, 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 the first port is in use as console, then it. */
98 	if (iobase == comconsaddr && !comconsattached)
99 		goto checkmappings;
100 
101 	if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
102 		rv = 0;
103 		goto out;
104 	}
105 	rv = comprobe1(iot, ioh);
106 	bus_space_unmap(iot, ioh, COM_NPORTS);
107 	if (rv == 0)
108 		goto out;
109 
110 checkmappings:
111 	for (i = 1; i < NSLAVES; i++) {
112 		iobase += COM_NPORTS;
113 
114 		if (iobase == comconsaddr && !comconsattached)
115 			continue;
116 
117 		if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
118 			rv = 0;
119 			goto out;
120 		}
121 		bus_space_unmap(iot, ioh, COM_NPORTS);
122 	}
123 
124 out:
125 	if (rv)
126 		ia->ia_iosize = NSLAVES * COM_NPORTS;
127 	return (rv);
128 }
129 
130 int
rtfpsprint(aux,pnp)131 rtfpsprint(aux, pnp)
132 	void *aux;
133 	const char *pnp;
134 {
135 	struct commulti_attach_args *ca = aux;
136 
137 	if (pnp)
138 		printf("com at %s", pnp);
139 	printf(" slave %d", ca->ca_slave);
140 	return (UNCONF);
141 }
142 
143 void
rtfpsattach(parent,self,aux)144 rtfpsattach(parent, self, aux)
145 	struct device *parent, *self;
146 	void *aux;
147 {
148 	struct rtfps_softc *sc = (void *)self;
149 	struct isa_attach_args *ia = aux;
150 	struct commulti_attach_args ca;
151 	static int irqport[] = {
152 		IOBASEUNK, IOBASEUNK, IOBASEUNK, IOBASEUNK,
153 		IOBASEUNK, IOBASEUNK, IOBASEUNK, IOBASEUNK,
154 		IOBASEUNK,     0x2f2,     0x6f2,     0x6f3,
155 		IOBASEUNK, IOBASEUNK, IOBASEUNK, IOBASEUNK
156 	};
157 	bus_space_tag_t iot = ia->ia_iot;
158 	int i;
159 
160 	sc->sc_iot = ia->ia_iot;
161 	sc->sc_iobase = ia->ia_iobase;
162 
163 	if (ia->ia_irq >= 16 || irqport[ia->ia_irq] == IOBASEUNK)
164 		panic("rtfpsattach: invalid irq");
165 	sc->sc_irqport = irqport[ia->ia_irq];
166 
167 	for (i = 0; i < NSLAVES; i++)
168 		if (bus_space_map(iot, sc->sc_iobase + i * COM_NPORTS,
169 		    COM_NPORTS, 0, &sc->sc_slaveioh[i]))
170 			panic("rtfpsattach: couldn't map slave %d", i);
171 	if (bus_space_map(iot, sc->sc_irqport, 1, 0, &sc->sc_irqioh))
172 		panic("rtfpsattach: couldn't map irq port at 0x%x",
173 		    sc->sc_irqport);
174 
175 	bus_space_write_1(iot, sc->sc_irqioh, 0, 0);
176 
177 	printf("\n");
178 
179 	for (i = 0; i < NSLAVES; i++) {
180 		ca.ca_slave = i;
181 		ca.ca_iot = sc->sc_iot;
182 		ca.ca_ioh = sc->sc_slaveioh[i];
183 		ca.ca_iobase = sc->sc_iobase + i * COM_NPORTS;
184 		ca.ca_noien = 0;
185 
186 		sc->sc_slaves[i] = config_found(self, &ca, rtfpsprint);
187 		if (sc->sc_slaves[i] != NULL)
188 			sc->sc_alive |= 1 << i;
189 	}
190 
191 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
192 	    IPL_TTY, rtfpsintr, sc, sc->sc_dev.dv_xname);
193 }
194 
195 int
rtfpsintr(arg)196 rtfpsintr(arg)
197 	void *arg;
198 {
199 	struct rtfps_softc *sc = arg;
200 	bus_space_tag_t iot = sc->sc_iot;
201 	int alive = sc->sc_alive;
202 
203 	bus_space_write_1(iot, sc->sc_irqioh, 0, 0);
204 
205 #define	TRY(n) \
206 	if (alive & (1 << (n))) \
207 		comintr(sc->sc_slaves[n]);
208 	TRY(0);
209 	TRY(1);
210 	TRY(2);
211 	TRY(3);
212 #undef TRY
213 
214 	return (1);
215 }
216