lpt_isa.c revision 1.45 1 1.45 is /* $NetBSD: lpt_isa.c,v 1.45 1997/10/14 15:50:21 is Exp $ */
2 1.23 cgd
3 1.1 cgd /*
4 1.11 mycroft * Copyright (c) 1993, 1994 Charles 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.1 cgd
56 1.9 mycroft #include <sys/param.h>
57 1.9 mycroft #include <sys/systm.h>
58 1.9 mycroft #include <sys/proc.h>
59 1.9 mycroft #include <sys/user.h>
60 1.9 mycroft #include <sys/buf.h>
61 1.9 mycroft #include <sys/kernel.h>
62 1.9 mycroft #include <sys/ioctl.h>
63 1.9 mycroft #include <sys/uio.h>
64 1.11 mycroft #include <sys/device.h>
65 1.38 christos #include <sys/conf.h>
66 1.11 mycroft #include <sys/syslog.h>
67 1.1 cgd
68 1.39 mycroft #include <machine/bus.h>
69 1.37 cgd #include <machine/intr.h>
70 1.9 mycroft
71 1.30 cgd #include <dev/isa/isavar.h>
72 1.45 is #include <dev/ic/lptreg.h>
73 1.45 is #include <dev/ic/lptvar.h>
74 1.11 mycroft
75 1.11 mycroft #define LPTPRI (PZERO+8)
76 1.11 mycroft #define LPT_BSIZE 1024
77 1.1 cgd
78 1.44 mikel #ifndef LPTDEBUG
79 1.38 christos #define LPRINTF(a)
80 1.1 cgd #else
81 1.45 is #define LPRINTF(a) if (lpt_isa_debug) printf a
82 1.45 is int lpt_isa_debug = 0;
83 1.1 cgd #endif
84 1.1 cgd
85 1.45 is struct lpt_isa_softc {
86 1.45 is struct lpt_softc sc_lpt;
87 1.25 mycroft int sc_irq;
88 1.45 is
89 1.15 mycroft };
90 1.11 mycroft
91 1.43 cgd #ifdef __BROKEN_INDIRECT_CONFIG
92 1.45 is int lpt_isa_probe __P((struct device *, void *, void *));
93 1.43 cgd #else
94 1.45 is int lpt_isa_probe __P((struct device *, struct cfdata *, void *));
95 1.43 cgd #endif
96 1.45 is void lpt_isa_attach __P((struct device *, struct device *, void *));
97 1.33 thorpej
98 1.45 is struct cfattach lpt_isa_ca = {
99 1.45 is sizeof(struct lpt_isa_softc), lpt_isa_probe, lpt_isa_attach
100 1.1 cgd };
101 1.1 cgd
102 1.42 thorpej int lpt_port_test __P((bus_space_tag_t, bus_space_handle_t, bus_addr_t,
103 1.42 thorpej bus_size_t, u_char, u_char));
104 1.34 cgd
105 1.2 cgd /*
106 1.11 mycroft * Internal routine to lptprobe to do port tests of one byte value.
107 1.2 cgd */
108 1.2 cgd int
109 1.42 thorpej lpt_port_test(iot, ioh, base, off, data, mask)
110 1.42 thorpej bus_space_tag_t iot;
111 1.42 thorpej bus_space_handle_t ioh;
112 1.42 thorpej bus_addr_t base;
113 1.42 thorpej bus_size_t off;
114 1.11 mycroft u_char data, mask;
115 1.11 mycroft {
116 1.11 mycroft int timeout;
117 1.11 mycroft u_char temp;
118 1.2 cgd
119 1.11 mycroft data &= mask;
120 1.42 thorpej bus_space_write_1(iot, ioh, off, data);
121 1.11 mycroft timeout = 1000;
122 1.11 mycroft do {
123 1.14 mycroft delay(10);
124 1.42 thorpej temp = bus_space_read_1(iot, ioh, off) & mask;
125 1.11 mycroft } while (temp != data && --timeout);
126 1.44 mikel LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n",
127 1.44 mikel (unsigned)(base + off), (unsigned)data, (unsigned)temp, timeout));
128 1.2 cgd return (temp == data);
129 1.11 mycroft }
130 1.2 cgd
131 1.2 cgd /*
132 1.2 cgd * Logic:
133 1.2 cgd * 1) You should be able to write to and read back the same value
134 1.2 cgd * to the data port. Do an alternating zeros, alternating ones,
135 1.2 cgd * walking zero, and walking one test to check for stuck bits.
136 1.2 cgd *
137 1.2 cgd * 2) You should be able to write to and read back the same value
138 1.2 cgd * to the control port lower 5 bits, the upper 3 bits are reserved
139 1.2 cgd * per the IBM PC technical reference manauls and different boards
140 1.2 cgd * do different things with them. Do an alternating zeros, alternating
141 1.2 cgd * ones, walking zero, and walking one test to check for stuck bits.
142 1.2 cgd *
143 1.2 cgd * Some printers drag the strobe line down when the are powered off
144 1.2 cgd * so this bit has been masked out of the control port test.
145 1.2 cgd *
146 1.2 cgd * XXX Some printers may not like a fast pulse on init or strobe, I
147 1.2 cgd * don't know at this point, if that becomes a problem these bits
148 1.2 cgd * should be turned off in the mask byte for the control port test.
149 1.2 cgd *
150 1.2 cgd * 3) Set the data and control ports to a value of 0
151 1.2 cgd */
152 1.2 cgd int
153 1.45 is lpt_isa_probe(parent, match, aux)
154 1.25 mycroft struct device *parent;
155 1.43 cgd #ifdef __BROKEN_INDIRECT_CONFIG
156 1.43 cgd void *match;
157 1.43 cgd #else
158 1.43 cgd struct cfdata *match;
159 1.43 cgd #endif
160 1.43 cgd void *aux;
161 1.11 mycroft {
162 1.15 mycroft struct isa_attach_args *ia = aux;
163 1.42 thorpej bus_space_tag_t iot;
164 1.42 thorpej bus_space_handle_t ioh;
165 1.32 cgd u_long base;
166 1.11 mycroft u_char mask, data;
167 1.32 cgd int i, rv;
168 1.2 cgd
169 1.11 mycroft #ifdef DEBUG
170 1.41 christos #define ABORT do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
171 1.32 cgd goto out;} while (0)
172 1.11 mycroft #else
173 1.32 cgd #define ABORT goto out
174 1.11 mycroft #endif
175 1.2 cgd
176 1.42 thorpej iot = ia->ia_iot;
177 1.32 cgd base = ia->ia_iobase;
178 1.42 thorpej if (bus_space_map(iot, base, LPT_NPORTS, 0, &ioh))
179 1.32 cgd return 0;
180 1.32 cgd
181 1.32 cgd rv = 0;
182 1.2 cgd mask = 0xff;
183 1.11 mycroft
184 1.28 mycroft data = 0x55; /* Alternating zeros */
185 1.42 thorpej if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
186 1.28 mycroft ABORT;
187 1.28 mycroft
188 1.28 mycroft data = 0xaa; /* Alternating ones */
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 for (i = 0; i < CHAR_BIT; i++) { /* Walking zero */
193 1.28 mycroft data = ~(1 << i);
194 1.42 thorpej if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
195 1.11 mycroft ABORT;
196 1.28 mycroft }
197 1.2 cgd
198 1.28 mycroft for (i = 0; i < CHAR_BIT; i++) { /* Walking one */
199 1.28 mycroft data = (1 << i);
200 1.42 thorpej if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
201 1.11 mycroft ABORT;
202 1.28 mycroft }
203 1.2 cgd
204 1.42 thorpej bus_space_write_1(iot, ioh, lpt_data, 0);
205 1.42 thorpej bus_space_write_1(iot, ioh, lpt_control, 0);
206 1.11 mycroft
207 1.15 mycroft ia->ia_iosize = LPT_NPORTS;
208 1.15 mycroft ia->ia_msize = 0;
209 1.32 cgd
210 1.32 cgd rv = 1;
211 1.32 cgd
212 1.32 cgd out:
213 1.42 thorpej bus_space_unmap(iot, ioh, LPT_NPORTS);
214 1.32 cgd return rv;
215 1.11 mycroft }
216 1.1 cgd
217 1.15 mycroft void
218 1.45 is lpt_isa_attach(parent, self, aux)
219 1.15 mycroft struct device *parent, *self;
220 1.15 mycroft void *aux;
221 1.1 cgd {
222 1.45 is struct lpt_isa_softc *sc = (void *)self;
223 1.45 is struct lpt_softc *lsc = &sc->sc_lpt;
224 1.15 mycroft struct isa_attach_args *ia = aux;
225 1.42 thorpej bus_space_tag_t iot;
226 1.42 thorpej bus_space_handle_t ioh;
227 1.15 mycroft
228 1.25 mycroft if (ia->ia_irq != IRQUNK)
229 1.41 christos printf("\n");
230 1.15 mycroft else
231 1.41 christos printf(": polled\n");
232 1.1 cgd
233 1.16 mycroft sc->sc_irq = ia->ia_irq;
234 1.32 cgd
235 1.45 is iot = lsc->sc_iot = ia->ia_iot;
236 1.45 is if (bus_space_map(iot, ia->ia_iobase, LPT_NPORTS, 0, &ioh))
237 1.45 is panic("lpt_isa_attach: couldn't map I/O ports");
238 1.45 is lsc->sc_ioh = ioh;
239 1.32 cgd
240 1.45 is lpt_attach_subr(lsc);
241 1.16 mycroft
242 1.30 cgd if (ia->ia_irq != IRQUNK)
243 1.45 is lsc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
244 1.45 is IPL_TTY, lptintr, lsc);
245 1.1 cgd }
246 1.1 cgd
247