xref: /netbsd-src/sys/dev/isa/com_isa.c (revision 10ad5ffa714ce1a679dcc9dd8159648df2d67b5a)
1 /*	$NetBSD: com_isa.c,v 1.35 2009/04/02 00:09:33 dyoung 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 Charles M. Hannum.
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) 1991 The Regents of the University of California.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)com.c	7.5 (Berkeley) 5/16/91
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: com_isa.c,v 1.35 2009/04/02 00:09:33 dyoung Exp $");
65 
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/ioctl.h>
69 #include <sys/select.h>
70 #include <sys/tty.h>
71 #include <sys/proc.h>
72 #include <sys/user.h>
73 #include <sys/file.h>
74 #include <sys/uio.h>
75 #include <sys/kernel.h>
76 #include <sys/syslog.h>
77 #include <sys/device.h>
78 
79 #include <sys/intr.h>
80 #include <sys/bus.h>
81 
82 #include <dev/ic/comreg.h>
83 #include <dev/ic/comvar.h>
84 #ifdef COM_HAYESP
85 #include <dev/ic/hayespreg.h>
86 #endif
87 
88 #include <dev/isa/isavar.h>
89 
90 struct com_isa_softc {
91 	struct	com_softc sc_com;	/* real "com" softc */
92 
93 	/* ISA-specific goo. */
94 	isa_chipset_tag_t sc_ic;
95 	void	*sc_ih;			/* interrupt handler */
96 	int	sc_irq;
97 };
98 
99 static bool com_isa_suspend(device_t PMF_FN_PROTO);
100 static bool com_isa_resume(device_t PMF_FN_PROTO);
101 
102 int com_isa_probe(device_t, cfdata_t , void *);
103 void com_isa_attach(device_t, device_t, void *);
104 static int com_isa_detach(device_t, int);
105 #ifdef COM_HAYESP
106 int com_isa_isHAYESP(bus_space_handle_t, struct com_softc *);
107 #endif
108 
109 
110 CFATTACH_DECL3_NEW(com_isa, sizeof(struct com_isa_softc),
111     com_isa_probe, com_isa_attach, com_isa_detach, com_activate,
112     NULL, NULL, DVF_DETACH_SHUTDOWN);
113 
114 int
115 com_isa_probe(device_t parent, cfdata_t match, void *aux)
116 {
117 	bus_space_tag_t iot;
118 	bus_space_handle_t ioh;
119 	int iobase;
120 	int rv = 1;
121 	struct isa_attach_args *ia = aux;
122 
123 	if (ia->ia_nio < 1)
124 		return (0);
125 	if (ia->ia_nirq < 1)
126 		return (0);
127 
128 	if (ISA_DIRECT_CONFIG(ia))
129 		return (0);
130 
131 	/* Disallow wildcarded i/o address. */
132 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
133 		return (0);
134 
135 	/* Don't allow wildcarded IRQ. */
136 	if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
137 		return (0);
138 
139 	iot = ia->ia_iot;
140 	iobase = ia->ia_io[0].ir_addr;
141 
142 	/* if it's in use as console, it's there. */
143 	if (!com_is_console(iot, iobase, 0)) {
144 		if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
145 			return 0;
146 		}
147 		rv = comprobe1(iot, ioh);
148 		bus_space_unmap(iot, ioh, COM_NPORTS);
149 	}
150 
151 	if (rv) {
152 		ia->ia_nio = 1;
153 		ia->ia_io[0].ir_size = COM_NPORTS;
154 
155 		ia->ia_nirq = 1;
156 
157 		ia->ia_niomem = 0;
158 		ia->ia_ndrq = 0;
159 	}
160 	return (rv);
161 }
162 
163 void
164 com_isa_attach(device_t parent, device_t self, void *aux)
165 {
166 	struct com_isa_softc *isc = device_private(self);
167 	struct com_softc *sc = &isc->sc_com;
168 	int iobase, irq;
169 	bus_space_tag_t iot;
170 	bus_space_handle_t ioh;
171 	struct isa_attach_args *ia = aux;
172 #ifdef COM_HAYESP
173 	int	hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
174 	int	*hayespp;
175 #endif
176 
177 	/*
178 	 * We're living on an isa.
179 	 */
180 	iobase = ia->ia_io[0].ir_addr;
181 	iot = ia->ia_iot;
182 
183 	if (!com_is_console(iot, iobase, &ioh) &&
184 	    bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
185 		printf(": can't map i/o space\n");
186 		return;
187 	}
188 
189 	sc->sc_dev = self;
190 
191 	COM_INIT_REGS(sc->sc_regs, iot, ioh, iobase);
192 
193 	sc->sc_frequency = COM_FREQ;
194 	irq = ia->ia_irq[0].ir_irq;
195 
196 #ifdef COM_HAYESP
197 	for (hayespp = hayesp_ports; *hayespp != 0; hayespp++) {
198 		bus_space_handle_t	hayespioh;
199 #define	HAYESP_NPORTS	8
200 		if (bus_space_map(iot, *hayespp, HAYESP_NPORTS, 0, &hayespioh))
201 			continue;
202 		if (com_isa_isHAYESP(hayespioh, sc)) {
203 			break;
204 		}
205 		bus_space_unmap(iot, hayespioh, HAYESP_NPORTS);
206 	}
207 #endif
208 
209 	com_attach_subr(sc);
210 
211 	if (!pmf_device_register1(self, com_isa_suspend, com_isa_resume,
212 	    com_cleanup))
213 		aprint_error_dev(self, "couldn't establish power handler\n");
214 
215 	isc->sc_ic = ia->ia_ic;
216 	isc->sc_irq = irq;
217 	isc->sc_ih = isa_intr_establish(ia->ia_ic, irq, IST_EDGE, IPL_SERIAL,
218 	    comintr, sc);
219 }
220 
221 static bool
222 com_isa_suspend(device_t self PMF_FN_ARGS)
223 {
224 	struct com_isa_softc *isc = device_private(self);
225 
226 	if (!com_suspend(self PMF_FN_CALL))
227 		return false;
228 
229 	isa_intr_disestablish(isc->sc_ic, isc->sc_ih);
230 	isc->sc_ih = NULL;
231 
232 	return true;
233 }
234 
235 static bool
236 com_isa_resume(device_t self PMF_FN_ARGS)
237 {
238 	struct com_isa_softc *isc = device_private(self);
239 	struct com_softc *sc = &isc->sc_com;
240 
241 	isc->sc_ih = isa_intr_establish(isc->sc_ic, isc->sc_irq, IST_EDGE,
242 	    IPL_SERIAL, comintr, sc);
243 
244 	return com_resume(self PMF_FN_CALL);
245 }
246 
247 static int
248 com_isa_detach(device_t self, int flags)
249 {
250 	struct com_isa_softc *isc = device_private(self);
251 	struct com_softc *sc = &isc->sc_com;
252 	const struct com_regs *cr = &sc->sc_regs;
253 	int rc;
254 
255 	if (isc->sc_ih != NULL)
256 		isa_intr_disestablish(isc->sc_ic, isc->sc_ih);
257 
258 	pmf_device_deregister(self);
259 
260 	if ((rc = com_detach(self, flags)) != 0)
261 		return rc;
262 
263 	com_cleanup(self, 0);
264 
265 #ifdef COM_HAYESP
266 	if (sc->sc_type == COM_TYPE_HAYESP)
267 		bus_space_unmap(cr->cr_iot, sc->sc_hayespioh, HAYESP_NPORTS);
268 #endif
269 	bus_space_unmap(cr->cr_iot, cr->cr_ioh, COM_NPORTS);
270 
271 	return 0;
272 }
273 
274 #ifdef COM_HAYESP
275 int
276 com_isa_isHAYESP(bus_space_handle_t hayespioh, struct com_softc *sc)
277 {
278 	char	val, dips;
279 	int	combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
280 	bus_space_tag_t iot = sc->sc_regs.cr_iot;
281 
282 	/*
283 	 * Hayes ESP cards have two iobases.  One is for compatibility with
284 	 * 16550 serial chips, and at the same ISA PC base addresses.  The
285 	 * other is for ESP-specific enhanced features, and lies at a
286 	 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
287 	 */
288 
289 	/* Test for ESP signature */
290 	if ((bus_space_read_1(iot, hayespioh, 0) & 0xf3) == 0)
291 		return (0);
292 
293 	/*
294 	 * ESP is present at ESP enhanced base address; unknown com port
295 	 */
296 
297 	/* Get the dip-switch configurations */
298 	bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETDIPS);
299 	dips = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1);
300 
301 	/* Determine which com port this ESP card services: bits 0,1 of  */
302 	/*  dips is the port # (0-3); combaselist[val] is the com_iobase */
303 	if (sc->sc_regs.cr_iobase != combaselist[dips & 0x03])
304 		return (0);
305 
306 	printf(": ESP");
307 
308  	/* Check ESP Self Test bits. */
309 	/* Check for ESP version 2.0: bits 4,5,6 == 010 */
310 	bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETTEST);
311 	val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1); /* Clear reg1 */
312 	val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS2);
313 	if ((val & 0x70) < 0x20) {
314 		printf("-old (%o)", val & 0x70);
315 		/* we do not support the necessary features */
316 		return (0);
317 	}
318 
319 	/* Check for ability to emulate 16550: bit 8 == 1 */
320 	if ((dips & 0x80) == 0) {
321 		printf(" slave");
322 		/* XXX Does slave really mean no 16550 support?? */
323 		return (0);
324 	}
325 
326 	/*
327 	 * If we made it this far, we are a full-featured ESP v2.0 (or
328 	 * better), at the correct com port address.
329 	 */
330 	sc->sc_type = COM_TYPE_HAYESP;
331 	sc->sc_hayespioh = hayespioh;
332 	sc->sc_fifolen = 1024;
333 	sc->sc_prescaler = 0;			/* set prescaler to x1. */
334 	printf(", 1024 byte fifo\n");
335 	return (1);
336 }
337 #endif
338