com_isa.c revision 1.32 1 /* $NetBSD: com_isa.c,v 1.32 2008/02/29 07:02:05 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.32 2008/02/29 07:02:05 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 isa_chipset_tag_t sc_ic;
102 void *sc_ih; /* interrupt handler */
103 int sc_irq;
104 };
105
106 static bool com_isa_suspend(device_t PMF_FN_PROTO);
107 static bool com_isa_resume(device_t PMF_FN_PROTO);
108
109 int com_isa_probe(device_t, struct cfdata *, void *);
110 void com_isa_attach(device_t, device_t, void *);
111 static int com_isa_detach(device_t, int);
112 #ifdef COM_HAYESP
113 int com_isa_isHAYESP(bus_space_handle_t, struct com_softc *);
114 #endif
115
116
117 CFATTACH_DECL(com_isa, sizeof(struct com_isa_softc),
118 com_isa_probe, com_isa_attach, com_isa_detach, com_activate);
119
120 int
121 com_isa_probe(device_t parent, struct cfdata *match, void *aux)
122 {
123 bus_space_tag_t iot;
124 bus_space_handle_t ioh;
125 int iobase;
126 int rv = 1;
127 struct isa_attach_args *ia = aux;
128
129 if (ia->ia_nio < 1)
130 return (0);
131 if (ia->ia_nirq < 1)
132 return (0);
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
141 /* Don't allow wildcarded IRQ. */
142 if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
143 return (0);
144
145 iot = ia->ia_iot;
146 iobase = ia->ia_io[0].ir_addr;
147
148 /* if it's in use as console, it's there. */
149 if (!com_is_console(iot, iobase, 0)) {
150 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
151 return 0;
152 }
153 rv = comprobe1(iot, ioh);
154 bus_space_unmap(iot, ioh, COM_NPORTS);
155 }
156
157 if (rv) {
158 ia->ia_nio = 1;
159 ia->ia_io[0].ir_size = COM_NPORTS;
160
161 ia->ia_nirq = 1;
162
163 ia->ia_niomem = 0;
164 ia->ia_ndrq = 0;
165 }
166 return (rv);
167 }
168
169 void
170 com_isa_attach(device_t parent, device_t self, void *aux)
171 {
172 struct com_isa_softc *isc = device_private(self);
173 struct com_softc *sc = &isc->sc_com;
174 int iobase, irq;
175 bus_space_tag_t iot;
176 bus_space_handle_t ioh;
177 struct isa_attach_args *ia = aux;
178 #ifdef COM_HAYESP
179 int hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
180 int *hayespp;
181 #endif
182
183 /*
184 * We're living on an isa.
185 */
186 iobase = ia->ia_io[0].ir_addr;
187 iot = ia->ia_iot;
188
189 if (!com_is_console(iot, iobase, &ioh) &&
190 bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
191 printf(": can't map i/o space\n");
192 return;
193 }
194
195 COM_INIT_REGS(sc->sc_regs, iot, ioh, iobase);
196
197 sc->sc_frequency = COM_FREQ;
198 irq = ia->ia_irq[0].ir_irq;
199
200 #ifdef COM_HAYESP
201 for (hayespp = hayesp_ports; *hayespp != 0; hayespp++) {
202 bus_space_handle_t hayespioh;
203 #define HAYESP_NPORTS 8
204 if (bus_space_map(iot, *hayespp, HAYESP_NPORTS, 0, &hayespioh))
205 continue;
206 if (com_isa_isHAYESP(hayespioh, sc)) {
207 break;
208 }
209 bus_space_unmap(iot, hayespioh, HAYESP_NPORTS);
210 }
211 #endif
212
213 com_attach_subr(sc);
214
215 if (!pmf_device_register1(self, com_isa_suspend, com_isa_resume,
216 com_cleanup))
217 aprint_error_dev(self, "couldn't establish power handler\n");
218
219 isc->sc_ic = ia->ia_ic;
220 isc->sc_irq = irq;
221 isc->sc_ih = isa_intr_establish(ia->ia_ic, irq, IST_EDGE, IPL_SERIAL,
222 comintr, sc);
223 }
224
225 static bool
226 com_isa_suspend(device_t self PMF_FN_ARGS)
227 {
228 struct com_isa_softc *isc = device_private(self);
229
230 if (!com_suspend(self PMF_FN_CALL))
231 return false;
232
233 isa_intr_disestablish(isc->sc_ic, isc->sc_ih);
234 isc->sc_ih = NULL;
235
236 return true;
237 }
238
239 static bool
240 com_isa_resume(device_t self PMF_FN_ARGS)
241 {
242 struct com_isa_softc *isc = device_private(self);
243 struct com_softc *sc = &isc->sc_com;
244
245 isc->sc_ih = isa_intr_establish(isc->sc_ic, isc->sc_irq, IST_EDGE,
246 IPL_SERIAL, comintr, sc);
247
248 return com_resume(self PMF_FN_CALL);
249 }
250
251 static int
252 com_isa_detach(device_t self, int flags)
253 {
254 struct com_isa_softc *isc = device_private(self);
255 struct com_softc *sc = &isc->sc_com;
256 const struct com_regs *cr = &sc->sc_regs;
257 int rc;
258
259 if (isc->sc_ih != NULL)
260 isa_intr_disestablish(isc->sc_ic, isc->sc_ih);
261
262 pmf_device_deregister(self);
263
264 if ((rc = com_detach(self, flags)) != 0)
265 return rc;
266
267 com_cleanup(self, 0);
268
269 #ifdef COM_HAYESP
270 if (sc->sc_type == COM_TYPE_HAYESP)
271 bus_space_unmap(cr->cr_iot, sc->sc_hayespioh, HAYESP_NPORTS);
272 #endif
273 bus_space_unmap(cr->cr_iot, cr->cr_ioh, COM_NPORTS);
274
275 return 0;
276 }
277
278 #ifdef COM_HAYESP
279 int
280 com_isa_isHAYESP(bus_space_handle_t hayespioh, struct com_softc *sc)
281 {
282 char val, dips;
283 int combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
284 bus_space_tag_t iot = sc->sc_regs.cr_iot;
285
286 /*
287 * Hayes ESP cards have two iobases. One is for compatibility with
288 * 16550 serial chips, and at the same ISA PC base addresses. The
289 * other is for ESP-specific enhanced features, and lies at a
290 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
291 */
292
293 /* Test for ESP signature */
294 if ((bus_space_read_1(iot, hayespioh, 0) & 0xf3) == 0)
295 return (0);
296
297 /*
298 * ESP is present at ESP enhanced base address; unknown com port
299 */
300
301 /* Get the dip-switch configurations */
302 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETDIPS);
303 dips = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1);
304
305 /* Determine which com port this ESP card services: bits 0,1 of */
306 /* dips is the port # (0-3); combaselist[val] is the com_iobase */
307 if (sc->sc_regs.cr_iobase != combaselist[dips & 0x03])
308 return (0);
309
310 printf(": ESP");
311
312 /* Check ESP Self Test bits. */
313 /* Check for ESP version 2.0: bits 4,5,6 == 010 */
314 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETTEST);
315 val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1); /* Clear reg1 */
316 val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS2);
317 if ((val & 0x70) < 0x20) {
318 printf("-old (%o)", val & 0x70);
319 /* we do not support the necessary features */
320 return (0);
321 }
322
323 /* Check for ability to emulate 16550: bit 8 == 1 */
324 if ((dips & 0x80) == 0) {
325 printf(" slave");
326 /* XXX Does slave really mean no 16550 support?? */
327 return (0);
328 }
329
330 /*
331 * If we made it this far, we are a full-featured ESP v2.0 (or
332 * better), at the correct com port address.
333 */
334 sc->sc_type = COM_TYPE_HAYESP;
335 sc->sc_hayespioh = hayespioh;
336 sc->sc_fifolen = 1024;
337 sc->sc_prescaler = 0; /* set prescaler to x1. */
338 printf(", 1024 byte fifo\n");
339 return (1);
340 }
341 #endif
342