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