rtfps.c revision 1.37
1/*	$NetBSD: rtfps.c,v 1.37 1998/01/14 12:14:46 drochner 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
52struct 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#ifdef __BROKEN_INDIRECT_CONFIG
67int rtfpsprobe __P((struct device *, void *, void *));
68#else
69int rtfpsprobe __P((struct device *, struct cfdata *, void *));
70#endif
71void rtfpsattach __P((struct device *, struct device *, void *));
72int rtfpsintr __P((void *));
73int rtfpsprint __P((void *, const char *));
74
75struct cfattach rtfps_ca = {
76	sizeof(struct rtfps_softc), rtfpsprobe, rtfpsattach
77};
78
79int
80rtfpsprobe(parent, self, aux)
81	struct device *parent;
82#ifdef __BROKEN_INDIRECT_CONFIG
83	void *self;
84#else
85	struct cfdata *self;
86#endif
87	void *aux;
88{
89	struct isa_attach_args *ia = aux;
90	int iobase = ia->ia_iobase;
91	bus_space_tag_t iot = ia->ia_iot;
92	bus_space_handle_t ioh;
93	int i, rv = 1;
94
95	/*
96	 * Do the normal com probe for the first UART and assume
97	 * its presence, and the ability to map the other UARTS,
98	 * means there is a multiport board there.
99	 * XXX Needs more robustness.
100	 */
101
102	/* Disallow wildcarded i/o address. */
103	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
104		return (0);
105
106	/* if the first port is in use as console, then it. */
107	if (com_is_console(iot, iobase, 0))
108		goto checkmappings;
109
110	if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
111		rv = 0;
112		goto out;
113	}
114	rv = comprobe1(iot, ioh, iobase);
115	bus_space_unmap(iot, ioh, COM_NPORTS);
116	if (rv == 0)
117		goto out;
118
119checkmappings:
120	for (i = 1; i < NSLAVES; i++) {
121		iobase += COM_NPORTS;
122
123		if (com_is_console(iot, iobase, 0))
124			continue;
125
126		if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
127			rv = 0;
128			goto out;
129		}
130		bus_space_unmap(iot, ioh, COM_NPORTS);
131	}
132
133out:
134	if (rv)
135		ia->ia_iosize = NSLAVES * COM_NPORTS;
136	return (rv);
137}
138
139int
140rtfpsprint(aux, pnp)
141	void *aux;
142	const char *pnp;
143{
144	struct commulti_attach_args *ca = aux;
145
146	if (pnp)
147		printf("com at %s", pnp);
148	printf(" slave %d", ca->ca_slave);
149	return (UNCONF);
150}
151
152void
153rtfpsattach(parent, self, aux)
154	struct device *parent, *self;
155	void *aux;
156{
157	struct rtfps_softc *sc = (void *)self;
158	struct isa_attach_args *ia = aux;
159	struct commulti_attach_args ca;
160	static int irqport[] = {
161		IOBASEUNK, IOBASEUNK, IOBASEUNK, IOBASEUNK,
162		IOBASEUNK, IOBASEUNK, IOBASEUNK, IOBASEUNK,
163		IOBASEUNK,     0x2f2,     0x6f2,     0x6f3,
164		IOBASEUNK, IOBASEUNK, IOBASEUNK, IOBASEUNK
165	};
166	bus_space_tag_t iot = ia->ia_iot;
167	int i, iobase;
168
169	printf("\n");
170
171	sc->sc_iot = ia->ia_iot;
172	sc->sc_iobase = ia->ia_iobase;
173
174	if (ia->ia_irq >= 16 || irqport[ia->ia_irq] == IOBASEUNK) {
175		printf("%s: invalid irq\n", sc->sc_dev.dv_xname);
176		return;
177	}
178	sc->sc_irqport = irqport[ia->ia_irq];
179
180	for (i = 0; i < NSLAVES; i++) {
181		iobase = sc->sc_iobase + i * COM_NPORTS;
182		if (!com_is_console(iot, iobase, &sc->sc_slaveioh[i]) &&
183		    bus_space_map(iot, iobase, COM_NPORTS, 0,
184			&sc->sc_slaveioh[i])) {
185			printf("%s: can't map i/o space for slave %d\n",
186			    sc->sc_dev.dv_xname, i);
187			return;
188		}
189	}
190	if (bus_space_map(iot, sc->sc_irqport, 1, 0, &sc->sc_irqioh)) {
191		printf("%s: can't map irq port at 0x%x\n", sc->sc_dev.dv_xname,
192		    sc->sc_irqport);
193		return;
194	}
195
196	bus_space_write_1(iot, sc->sc_irqioh, 0, 0);
197
198	for (i = 0; i < NSLAVES; i++) {
199		ca.ca_slave = i;
200		ca.ca_iot = sc->sc_iot;
201		ca.ca_ioh = sc->sc_slaveioh[i];
202		ca.ca_iobase = sc->sc_iobase + i * COM_NPORTS;
203		ca.ca_noien = 0;
204
205		sc->sc_slaves[i] = config_found(self, &ca, rtfpsprint);
206		if (sc->sc_slaves[i] != NULL)
207			sc->sc_alive |= 1 << i;
208	}
209
210	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
211	    IPL_SERIAL, rtfpsintr, sc);
212}
213
214int
215rtfpsintr(arg)
216	void *arg;
217{
218	struct rtfps_softc *sc = arg;
219	bus_space_tag_t iot = sc->sc_iot;
220	int alive = sc->sc_alive;
221
222	bus_space_write_1(iot, sc->sc_irqioh, 0, 0);
223
224#define	TRY(n) \
225	if (alive & (1 << (n))) \
226		comintr(sc->sc_slaves[n]);
227	TRY(0);
228	TRY(1);
229	TRY(2);
230	TRY(3);
231#undef TRY
232
233	return (1);
234}
235