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