xref: /netbsd-src/sys/dev/isa/tcom.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /*	$NetBSD: tcom.c,v 1.16 2008/04/28 20:23:52 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Eric S. Hvozda.
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1998 Jukka Marin.  All rights reserved.
34  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
35  * Copyright (c) 1995 Charles Hannum.  All rights reserved.
36  *
37  * This code is derived from public-domain software written by
38  * Roland McGrath.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *	This product includes software developed by Charles Hannum.
51  * 4. The name of the author may not be used to endorse or promote products
52  *    derived from this software without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 /*
67  * NOTE: This driver has been tested in TCOM "UNIX mode" with base address
68  * set to 0x100 or 0x200 and baud rate jumpers to "normal".  This code
69  * does NOT know about the 2x and 4x baud rates available on TCOM cards.
70  */
71 
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: tcom.c,v 1.16 2008/04/28 20:23:52 martin Exp $");
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/device.h>
78 #include <sys/termios.h>
79 
80 #include <sys/bus.h>
81 #include <sys/intr.h>
82 
83 #include <dev/ic/comreg.h>
84 #include <dev/ic/comvar.h>
85 
86 #include <dev/isa/isavar.h>
87 #include <dev/isa/com_multi.h>
88 
89 #define	NSLAVES	8			/* TCOM cards have 4 or 8 UARTs */
90 #define STATUS_OFFSET	0x40		/* offset from board base address */
91 #define	STATUS_SIZE	8		/* 8 bytes reserved for irq status */
92 
93 struct tcom_softc {
94 	struct device sc_dev;
95 	void *sc_ih;
96 
97 	bus_space_tag_t sc_iot;
98 	int sc_iobase;
99 
100 	int sc_alive;			/* mask of slave units attached */
101 	void *sc_slaves[NSLAVES];	/* com device unit numbers */
102 	bus_space_handle_t sc_slaveioh[NSLAVES];
103 	bus_space_handle_t sc_statusioh;
104 };
105 
106 int tcomprobe(struct device *, struct cfdata *, void *);
107 void tcomattach(struct device *, struct device *, void *);
108 int tcomintr(void *);
109 
110 CFATTACH_DECL(tcom, sizeof(struct tcom_softc),
111     tcomprobe, tcomattach, NULL, NULL);
112 
113 int
114 tcomprobe(struct device *parent, struct cfdata *self,
115     void *aux)
116 {
117 	struct isa_attach_args *ia = aux;
118 	bus_space_tag_t iot = ia->ia_iot;
119 	bus_space_handle_t ioh;
120 	int i, iobase, rv = 1;
121 
122 	/*
123 	 * Do the normal com probe for the first UART and assume
124 	 * its presence, and the ability to map the other UARTS,
125 	 * means there is a multiport board there.
126 	 * XXX Needs more robustness.
127 	 */
128 
129 	if (ia->ia_nio < 1)
130 		return (0);
131 	if (ia->ia_nirq < 1)
132 		return (1);
133 
134 	if (ISA_DIRECT_CONFIG(ia))
135 		return (0);
136 
137 	/* Disallow wildcarded i/o address. */
138 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
139 		return (0);
140 	if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
141 		return (0);
142 
143 	/* if the first port is in use as console, then it. */
144 	if (com_is_console(iot, ia->ia_io[0].ir_addr, 0))
145 		goto checkmappings;
146 
147 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, COM_NPORTS, 0, &ioh)) {
148 		rv = 0;
149 		goto out;
150 	}
151 	rv = comprobe1(iot, ioh);
152 	bus_space_unmap(iot, ioh, COM_NPORTS);
153 	if (rv == 0)
154 		goto out;
155 
156 checkmappings:
157 	for (i = 1, iobase = ia->ia_io[0].ir_addr; i < NSLAVES; i++) {
158 		iobase += COM_NPORTS;
159 
160 		if (com_is_console(iot, iobase, 0))
161 			continue;
162 
163 		if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
164 			rv = 0;
165 			goto out;
166 		}
167 		bus_space_unmap(iot, ioh, COM_NPORTS);
168 	}
169 
170 out:
171 	if (rv) {
172 		ia->ia_nio = 1;
173 		ia->ia_io[0].ir_size = NSLAVES * COM_NPORTS;
174 
175 		ia->ia_nirq = 1;
176 
177 		ia->ia_niomem = 0;
178 		ia->ia_ndrq = 0;
179 	}
180 	return (rv);
181 }
182 
183 void
184 tcomattach(struct device *parent, struct device *self, void *aux)
185 {
186 	struct tcom_softc *sc = (void *)self;
187 	struct isa_attach_args *ia = aux;
188 	struct commulti_attach_args ca;
189 	bus_space_tag_t iot = ia->ia_iot;
190 	int i, iobase;
191 
192 	printf("\n");
193 
194 	sc->sc_iot = ia->ia_iot;
195 	sc->sc_iobase = ia->ia_io[0].ir_addr;
196 
197 	for (i = 0; i < NSLAVES; i++) {
198 		iobase = sc->sc_iobase + i * COM_NPORTS;
199 		if (!com_is_console(iot, iobase, &sc->sc_slaveioh[i]) &&
200 		    bus_space_map(iot, iobase, COM_NPORTS, 0,
201 			&sc->sc_slaveioh[i])) {
202 			aprint_error_dev(&sc->sc_dev, "can't map i/o space for slave %d\n", i);
203 			return;
204 		}
205 	}
206 
207 	if (bus_space_map(iot, sc->sc_iobase + STATUS_OFFSET, STATUS_SIZE, 0,
208 	    &sc->sc_statusioh)) {
209 		aprint_error_dev(&sc->sc_dev, "can't map status space\n");
210 		return;
211 	}
212 
213 	for (i = 0; i < NSLAVES; i++) {
214 		ca.ca_slave = i;
215 		ca.ca_iot = sc->sc_iot;
216 		ca.ca_ioh = sc->sc_slaveioh[i];
217 		ca.ca_iobase = sc->sc_iobase + i * COM_NPORTS;
218 		ca.ca_noien = 0;
219 
220 		sc->sc_slaves[i] = config_found(self, &ca, commultiprint);
221 		if (sc->sc_slaves[i] != NULL)
222 			sc->sc_alive |= 1 << i;
223 	}
224 
225 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
226 	    IST_EDGE, IPL_SERIAL, tcomintr, sc);
227 }
228 
229 int
230 tcomintr(arg)
231 	void *arg;
232 {
233 	struct tcom_softc *sc = arg;
234 	bus_space_tag_t iot = sc->sc_iot;
235 	int alive = sc->sc_alive;
236 	int bits;
237 
238 	bits = ~bus_space_read_1(iot, sc->sc_statusioh, 0) & alive;
239 	if (bits == 0)
240 		return (0);
241 
242 	for (;;) {
243 #define	TRY(n) \
244 		if (bits & (1 << (n))) \
245 			comintr(sc->sc_slaves[n]);
246 		TRY(0);
247 		TRY(1);
248 		TRY(2);
249 		TRY(3);
250 		TRY(4);
251 		TRY(5);
252 		TRY(6);
253 		TRY(7);
254 #undef TRY
255 		bits = ~bus_space_read_1(iot, sc->sc_statusioh, 0) & alive;
256 		if (bits == 0)
257 			return (1);
258  	}
259 }
260