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