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