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