com_isa.c revision 1.33 1 /* $NetBSD: com_isa.c,v 1.33 2008/03/14 15:09:11 cube 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.33 2008/03/14 15:09:11 cube 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, cfdata_t , 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_NEW(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, cfdata_t 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 sc->sc_dev = self;
196
197 COM_INIT_REGS(sc->sc_regs, iot, ioh, iobase);
198
199 sc->sc_frequency = COM_FREQ;
200 irq = ia->ia_irq[0].ir_irq;
201
202 #ifdef COM_HAYESP
203 for (hayespp = hayesp_ports; *hayespp != 0; hayespp++) {
204 bus_space_handle_t hayespioh;
205 #define HAYESP_NPORTS 8
206 if (bus_space_map(iot, *hayespp, HAYESP_NPORTS, 0, &hayespioh))
207 continue;
208 if (com_isa_isHAYESP(hayespioh, sc)) {
209 break;
210 }
211 bus_space_unmap(iot, hayespioh, HAYESP_NPORTS);
212 }
213 #endif
214
215 com_attach_subr(sc);
216
217 if (!pmf_device_register1(self, com_isa_suspend, com_isa_resume,
218 com_cleanup))
219 aprint_error_dev(self, "couldn't establish power handler\n");
220
221 isc->sc_ic = ia->ia_ic;
222 isc->sc_irq = irq;
223 isc->sc_ih = isa_intr_establish(ia->ia_ic, irq, IST_EDGE, IPL_SERIAL,
224 comintr, sc);
225 }
226
227 static bool
228 com_isa_suspend(device_t self PMF_FN_ARGS)
229 {
230 struct com_isa_softc *isc = device_private(self);
231
232 if (!com_suspend(self PMF_FN_CALL))
233 return false;
234
235 isa_intr_disestablish(isc->sc_ic, isc->sc_ih);
236 isc->sc_ih = NULL;
237
238 return true;
239 }
240
241 static bool
242 com_isa_resume(device_t self PMF_FN_ARGS)
243 {
244 struct com_isa_softc *isc = device_private(self);
245 struct com_softc *sc = &isc->sc_com;
246
247 isc->sc_ih = isa_intr_establish(isc->sc_ic, isc->sc_irq, IST_EDGE,
248 IPL_SERIAL, comintr, sc);
249
250 return com_resume(self PMF_FN_CALL);
251 }
252
253 static int
254 com_isa_detach(device_t self, int flags)
255 {
256 struct com_isa_softc *isc = device_private(self);
257 struct com_softc *sc = &isc->sc_com;
258 const struct com_regs *cr = &sc->sc_regs;
259 int rc;
260
261 if (isc->sc_ih != NULL)
262 isa_intr_disestablish(isc->sc_ic, isc->sc_ih);
263
264 pmf_device_deregister(self);
265
266 if ((rc = com_detach(self, flags)) != 0)
267 return rc;
268
269 com_cleanup(self, 0);
270
271 #ifdef COM_HAYESP
272 if (sc->sc_type == COM_TYPE_HAYESP)
273 bus_space_unmap(cr->cr_iot, sc->sc_hayespioh, HAYESP_NPORTS);
274 #endif
275 bus_space_unmap(cr->cr_iot, cr->cr_ioh, COM_NPORTS);
276
277 return 0;
278 }
279
280 #ifdef COM_HAYESP
281 int
282 com_isa_isHAYESP(bus_space_handle_t hayespioh, struct com_softc *sc)
283 {
284 char val, dips;
285 int combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
286 bus_space_tag_t iot = sc->sc_regs.cr_iot;
287
288 /*
289 * Hayes ESP cards have two iobases. One is for compatibility with
290 * 16550 serial chips, and at the same ISA PC base addresses. The
291 * other is for ESP-specific enhanced features, and lies at a
292 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
293 */
294
295 /* Test for ESP signature */
296 if ((bus_space_read_1(iot, hayespioh, 0) & 0xf3) == 0)
297 return (0);
298
299 /*
300 * ESP is present at ESP enhanced base address; unknown com port
301 */
302
303 /* Get the dip-switch configurations */
304 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETDIPS);
305 dips = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1);
306
307 /* Determine which com port this ESP card services: bits 0,1 of */
308 /* dips is the port # (0-3); combaselist[val] is the com_iobase */
309 if (sc->sc_regs.cr_iobase != combaselist[dips & 0x03])
310 return (0);
311
312 printf(": ESP");
313
314 /* Check ESP Self Test bits. */
315 /* Check for ESP version 2.0: bits 4,5,6 == 010 */
316 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETTEST);
317 val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1); /* Clear reg1 */
318 val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS2);
319 if ((val & 0x70) < 0x20) {
320 printf("-old (%o)", val & 0x70);
321 /* we do not support the necessary features */
322 return (0);
323 }
324
325 /* Check for ability to emulate 16550: bit 8 == 1 */
326 if ((dips & 0x80) == 0) {
327 printf(" slave");
328 /* XXX Does slave really mean no 16550 support?? */
329 return (0);
330 }
331
332 /*
333 * If we made it this far, we are a full-featured ESP v2.0 (or
334 * better), at the correct com port address.
335 */
336 sc->sc_type = COM_TYPE_HAYESP;
337 sc->sc_hayespioh = hayespioh;
338 sc->sc_fifolen = 1024;
339 sc->sc_prescaler = 0; /* set prescaler to x1. */
340 printf(", 1024 byte fifo\n");
341 return (1);
342 }
343 #endif
344