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