xref: /netbsd-src/sys/dev/isa/lpt_isa.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: lpt_isa.c,v 1.47 1997/10/20 18:43:16 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1993, 1994 Charles Hannum.
5  * Copyright (c) 1990 William F. Jolitz, TeleMuse
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This software is a component of "386BSD" developed by
19  *	William F. Jolitz, TeleMuse.
20  * 4. Neither the name of the developer nor the name "386BSD"
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
25  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
26  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
27  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
28  * NOT MAKE USE OF THIS WORK.
29  *
30  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
31  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
32  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
33  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
34  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
35  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
36  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
37  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
40  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
43  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  */
51 
52 /*
53  * Device Driver for AT parallel printer port
54  */
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/proc.h>
59 #include <sys/user.h>
60 #include <sys/buf.h>
61 #include <sys/kernel.h>
62 #include <sys/ioctl.h>
63 #include <sys/uio.h>
64 #include <sys/device.h>
65 #include <sys/conf.h>
66 #include <sys/syslog.h>
67 
68 #include <machine/bus.h>
69 #include <machine/intr.h>
70 
71 #include <dev/isa/isavar.h>
72 #include <dev/ic/lptreg.h>
73 #include <dev/ic/lptvar.h>
74 
75 #define	LPTPRI		(PZERO+8)
76 #define	LPT_BSIZE	1024
77 
78 #ifndef LPTDEBUG
79 #define LPRINTF(a)
80 #else
81 #define LPRINTF(a)	if (lpt_isa_debug) printf a
82 int lpt_isa_debug = 0;
83 #endif
84 
85 struct lpt_isa_softc {
86 	struct lpt_softc sc_lpt;
87 	int sc_irq;
88 
89 };
90 
91 #ifdef __BROKEN_INDIRECT_CONFIG
92 int lpt_isa_probe __P((struct device *, void *, void *));
93 #else
94 int lpt_isa_probe __P((struct device *, struct cfdata *, void *));
95 #endif
96 void lpt_isa_attach __P((struct device *, struct device *, void *));
97 
98 struct cfattach lpt_isa_ca = {
99 	sizeof(struct lpt_isa_softc), lpt_isa_probe, lpt_isa_attach
100 };
101 
102 int	lpt_port_test __P((bus_space_tag_t, bus_space_handle_t, bus_addr_t,
103 	    bus_size_t, u_char, u_char));
104 
105 /*
106  * Internal routine to lptprobe to do port tests of one byte value.
107  */
108 int
109 lpt_port_test(iot, ioh, base, off, data, mask)
110 	bus_space_tag_t iot;
111 	bus_space_handle_t ioh;
112 	bus_addr_t base;
113 	bus_size_t off;
114 	u_char data, mask;
115 {
116 	int timeout;
117 	u_char temp;
118 
119 	data &= mask;
120 	bus_space_write_1(iot, ioh, off, data);
121 	timeout = 1000;
122 	do {
123 		delay(10);
124 		temp = bus_space_read_1(iot, ioh, off) & mask;
125 	} while (temp != data && --timeout);
126 	LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n",
127 	    (unsigned)(base + off), (unsigned)data, (unsigned)temp, timeout));
128 	return (temp == data);
129 }
130 
131 /*
132  * Logic:
133  *	1) You should be able to write to and read back the same value
134  *	   to the data port.  Do an alternating zeros, alternating ones,
135  *	   walking zero, and walking one test to check for stuck bits.
136  *
137  *	2) You should be able to write to and read back the same value
138  *	   to the control port lower 5 bits, the upper 3 bits are reserved
139  *	   per the IBM PC technical reference manauls and different boards
140  *	   do different things with them.  Do an alternating zeros, alternating
141  *	   ones, walking zero, and walking one test to check for stuck bits.
142  *
143  *	   Some printers drag the strobe line down when the are powered off
144  * 	   so this bit has been masked out of the control port test.
145  *
146  *	   XXX Some printers may not like a fast pulse on init or strobe, I
147  *	   don't know at this point, if that becomes a problem these bits
148  *	   should be turned off in the mask byte for the control port test.
149  *
150  *	3) Set the data and control ports to a value of 0
151  */
152 int
153 lpt_isa_probe(parent, match, aux)
154 	struct device *parent;
155 #ifdef __BROKEN_INDIRECT_CONFIG
156 	void *match;
157 #else
158 	struct cfdata *match;
159 #endif
160 	void *aux;
161 {
162 	struct isa_attach_args *ia = aux;
163 	bus_space_tag_t iot;
164 	bus_space_handle_t ioh;
165 	u_long base;
166 	u_char mask, data;
167 	int i, rv;
168 
169 #ifdef DEBUG
170 #define	ABORT	do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
171 		    goto out;} while (0)
172 #else
173 #define	ABORT	goto out
174 #endif
175 
176 	/* Disallow wildcarded i/o address. */
177 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
178 		return (0);
179 
180 	iot = ia->ia_iot;
181 	base = ia->ia_iobase;
182 	if (bus_space_map(iot, base, LPT_NPORTS, 0, &ioh))
183 		return 0;
184 
185 	rv = 0;
186 	mask = 0xff;
187 
188 	data = 0x55;				/* Alternating zeros */
189 	if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
190 		ABORT;
191 
192 	data = 0xaa;				/* Alternating ones */
193 	if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
194 		ABORT;
195 
196 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
197 		data = ~(1 << i);
198 		if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
199 			ABORT;
200 	}
201 
202 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
203 		data = (1 << i);
204 		if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
205 			ABORT;
206 	}
207 
208 	bus_space_write_1(iot, ioh, lpt_data, 0);
209 	bus_space_write_1(iot, ioh, lpt_control, 0);
210 
211 	ia->ia_iosize = LPT_NPORTS;
212 	ia->ia_msize = 0;
213 
214 	rv = 1;
215 
216 out:
217 	bus_space_unmap(iot, ioh, LPT_NPORTS);
218 	return rv;
219 }
220 
221 void
222 lpt_isa_attach(parent, self, aux)
223 	struct device *parent, *self;
224 	void *aux;
225 {
226 	struct lpt_isa_softc *sc = (void *)self;
227 	struct lpt_softc *lsc = &sc->sc_lpt;
228 	struct isa_attach_args *ia = aux;
229 	bus_space_tag_t iot;
230 	bus_space_handle_t ioh;
231 
232 	if (ia->ia_irq != IRQUNK)
233 		printf("\n");
234 	else
235 		printf(": polled\n");
236 
237 	sc->sc_irq = ia->ia_irq;
238 
239 	iot = lsc->sc_iot = ia->ia_iot;
240 	if (bus_space_map(iot, ia->ia_iobase, LPT_NPORTS, 0, &ioh)) {
241 		printf("%s: can't map i/o space\n", self->dv_xname);
242 		return;
243 	}
244 	lsc->sc_ioh = ioh;
245 
246 	lpt_attach_subr(lsc);
247 
248 	if (ia->ia_irq != IRQUNK)
249 		lsc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
250 		    IPL_TTY, lptintr, lsc);
251 }
252 
253