lpt_isa.c revision 1.65 1 /* $NetBSD: lpt_isa.c,v 1.65 2008/02/22 20:53:58 dyoung Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1994 Charles M. 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/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: lpt_isa.c,v 1.65 2008/02/22 20:53:58 dyoung Exp $");
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/proc.h>
62 #include <sys/user.h>
63 #include <sys/buf.h>
64 #include <sys/kernel.h>
65 #include <sys/ioctl.h>
66 #include <sys/uio.h>
67 #include <sys/device.h>
68 #include <sys/syslog.h>
69
70 #include <sys/bus.h>
71 #include <sys/intr.h>
72
73 #include <dev/isa/isavar.h>
74 #include <dev/ic/lptreg.h>
75 #include <dev/ic/lptvar.h>
76
77 #define LPTPRI (PZERO+8)
78 #define LPT_BSIZE 1024
79
80 #ifndef LPTDEBUG
81 #define LPRINTF(a)
82 #else
83 #define LPRINTF(a) if (lpt_isa_debug) printf a
84 int lpt_isa_debug = 0;
85 #endif
86
87 struct lpt_isa_softc {
88 struct lpt_softc sc_lpt;
89 int sc_irq;
90 isa_chipset_tag_t sc_ic;
91
92 };
93
94 int lpt_isa_probe(struct device *, struct cfdata *, void *);
95 static void lpt_isa_attach(struct device *, struct device *, void *);
96 static int lpt_isa_detach(device_t, int);
97
98 CFATTACH_DECL(lpt_isa, sizeof(struct lpt_isa_softc),
99 lpt_isa_probe, lpt_isa_attach, lpt_isa_detach, NULL);
100
101 int lpt_port_test(bus_space_tag_t, bus_space_handle_t, bus_addr_t,
102 bus_size_t, u_char, u_char);
103
104 /*
105 * Internal routine to lptprobe to do port tests of one byte value.
106 */
107 int
108 lpt_port_test(bus_space_tag_t iot, bus_space_handle_t ioh,
109 bus_addr_t base, bus_size_t off, u_char data, u_char mask)
110 {
111 int timeout;
112 u_char temp;
113
114 data &= mask;
115 bus_space_write_1(iot, ioh, off, data);
116 timeout = 1000;
117 do {
118 delay(10);
119 temp = bus_space_read_1(iot, ioh, off) & mask;
120 } while (temp != data && --timeout);
121 LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n",
122 (unsigned)(base + off), (unsigned)data, (unsigned)temp, timeout));
123 return (temp == data);
124 }
125
126 /*
127 * Logic:
128 * 1) You should be able to write to and read back the same value
129 * to the data port. Do an alternating zeros, alternating ones,
130 * walking zero, and walking one test to check for stuck bits.
131 *
132 * 2) You should be able to write to and read back the same value
133 * to the control port lower 5 bits, the upper 3 bits are reserved
134 * per the IBM PC technical reference manauls and different boards
135 * do different things with them. Do an alternating zeros, alternating
136 * ones, walking zero, and walking one test to check for stuck bits.
137 *
138 * Some printers drag the strobe line down when the are powered off
139 * so this bit has been masked out of the control port test.
140 *
141 * XXX Some printers may not like a fast pulse on init or strobe, I
142 * don't know at this point, if that becomes a problem these bits
143 * should be turned off in the mask byte for the control port test.
144 *
145 * 3) Set the data and control ports to a value of 0
146 */
147 int
148 lpt_isa_probe(struct device *parent, struct cfdata *match,
149 void *aux)
150 {
151 struct isa_attach_args *ia = aux;
152 bus_space_tag_t iot;
153 bus_space_handle_t ioh;
154 u_long base;
155 u_char mask, data;
156 int i, rv;
157
158 #ifdef LPT_DEBUG
159 #define ABORT do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
160 goto out;} while (0)
161 #else
162 #define ABORT goto out
163 #endif
164
165 if (ia->ia_nio < 1)
166 return (0);
167
168 if (ISA_DIRECT_CONFIG(ia))
169 return (0);
170
171 /* Disallow wildcarded i/o address. */
172 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
173 return (0);
174
175 iot = ia->ia_iot;
176 base = ia->ia_io[0].ir_addr;
177 if (bus_space_map(iot, base, LPT_NPORTS, 0, &ioh))
178 return 0;
179
180 rv = 0;
181 mask = 0xff;
182
183 data = 0x55; /* Alternating zeros */
184 if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
185 ABORT;
186
187 data = 0xaa; /* Alternating ones */
188 if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
189 ABORT;
190
191 for (i = 0; i < CHAR_BIT; i++) { /* Walking zero */
192 data = ~(1 << i);
193 if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
194 ABORT;
195 }
196
197 for (i = 0; i < CHAR_BIT; i++) { /* Walking one */
198 data = (1 << i);
199 if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
200 ABORT;
201 }
202
203 bus_space_write_1(iot, ioh, lpt_data, 0);
204 bus_space_write_1(iot, ioh, lpt_control, 0);
205
206 ia->ia_io[0].ir_size = LPT_NPORTS;
207
208 ia->ia_niomem = 0;
209 ia->ia_ndrq = 0;
210
211 rv = 1;
212
213 out:
214 bus_space_unmap(iot, ioh, LPT_NPORTS);
215 return rv;
216 }
217
218 void
219 lpt_isa_attach(struct device *parent, struct device *self, void *aux)
220 {
221 struct lpt_isa_softc *sc = (void *)self;
222 struct lpt_softc *lsc = &sc->sc_lpt;
223 struct isa_attach_args *ia = aux;
224 bus_space_tag_t iot;
225 bus_space_handle_t ioh;
226
227 if (ia->ia_nirq < 1 ||
228 ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) {
229 sc->sc_irq = -1;
230 printf(": polled\n");
231 } else {
232 sc->sc_irq = ia->ia_irq[0].ir_irq;
233 printf("\n");
234 }
235
236 iot = lsc->sc_iot = ia->ia_iot;
237 if (bus_space_map(iot, ia->ia_io[0].ir_addr, LPT_NPORTS, 0, &ioh)) {
238 printf("%s: can't map i/o space\n", self->dv_xname);
239 return;
240 }
241 lsc->sc_ioh = ioh;
242
243 lpt_attach_subr(lsc);
244
245 sc->sc_ic = ia->ia_ic;
246 if (sc->sc_irq != -1)
247 lsc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq,
248 IST_EDGE, IPL_TTY, lptintr, lsc);
249 }
250
251 static int
252 lpt_isa_detach(device_t self, int flags)
253 {
254 int rc;
255 struct lpt_isa_softc *sc = device_private(self);
256 struct lpt_softc *lsc = &sc->sc_lpt;
257
258 if ((rc = lpt_detach_subr(self, flags)) != 0)
259 return rc;
260
261 if (sc->sc_irq != -1)
262 isa_intr_disestablish(sc->sc_ic, lsc->sc_ih);
263
264 bus_space_unmap(lsc->sc_iot, lsc->sc_ioh, LPT_NPORTS);
265 return 0;
266 }
267