1 /* $NetBSD: lpt_pioc.c,v 1.13 2019/12/27 09:28:41 msaitoh Exp $ */
2
3 /*
4 * Copyright (c) 1997 Mark Brinicombe
5 * Copyright (c) 1997 Causality Limited
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. The name of the company nor the name of the author may be used to
17 * endorse or promote products derived from this software without specific
18 * prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTERS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Device Driver for AT parallel printer port
35 */
36
37 #include <sys/param.h>
38
39 __KERNEL_RCSID(0, "$NetBSD: lpt_pioc.c,v 1.13 2019/12/27 09:28:41 msaitoh Exp $");
40
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/bus.h>
45 #include <machine/intr.h>
46 #include <acorn32/mainbus/piocvar.h>
47 #include <dev/ic/lptreg.h>
48 #include <dev/ic/lptvar.h>
49
50 #include "locators.h"
51
52 /* Prototypes for functions */
53
54 static int lpt_port_test (bus_space_tag_t, bus_space_handle_t, bus_addr_t,
55 bus_size_t, u_char, u_char);
56 static int lptprobe (bus_space_tag_t, u_int);
57 static int lpt_pioc_probe (device_t, cfdata_t , void *);
58 static void lpt_pioc_attach (device_t, device_t, void *);
59
60 /* device attach structure */
61
62 CFATTACH_DECL_NEW(lpt_pioc, sizeof(struct lpt_softc),
63 lpt_pioc_probe, lpt_pioc_attach, NULL, NULL);
64
65 /*
66 * Internal routine to lptprobe to do port tests of one byte value.
67 */
68 static int
lpt_port_test(bus_space_tag_t iot,bus_space_handle_t ioh,bus_addr_t base,bus_size_t off,u_char data,u_char mask)69 lpt_port_test(bus_space_tag_t iot, bus_space_handle_t ioh,
70 bus_addr_t base, bus_size_t off, u_char data, u_char mask)
71 {
72 int timeout;
73 u_char temp;
74
75 data &= mask;
76 bus_space_write_1(iot, ioh, off, data);
77 timeout = 1000;
78 do {
79 delay(10);
80 temp = bus_space_read_1(iot, ioh, off) & mask;
81 } while (temp != data && --timeout);
82 return (temp == data);
83 }
84
85 /*
86 * Logic:
87 * 1) You should be able to write to and read back the same value
88 * to the data port. Do an alternating zeros, alternating ones,
89 * walking zero, and walking one test to check for stuck bits.
90 *
91 * 2) You should be able to write to and read back the same value
92 * to the control port lower 5 bits, the upper 3 bits are reserved
93 * per the IBM PC technical reference manuals and different boards
94 * do different things with them. Do an alternating zeros, alternating
95 * ones, walking zero, and walking one test to check for stuck bits.
96 *
97 * Some printers drag the strobe line down when the are powered off
98 * so this bit has been masked out of the control port test.
99 *
100 * XXX Some printers may not like a fast pulse on init or strobe, I
101 * don't know at this point, if that becomes a problem these bits
102 * should be turned off in the mask byte for the control port test.
103 *
104 * 3) Set the data and control ports to a value of 0
105 */
106 static int
lptprobe(bus_space_tag_t iot,u_int iobase)107 lptprobe(bus_space_tag_t iot, u_int iobase)
108 {
109 bus_space_handle_t ioh;
110 u_char mask, data;
111 int i, rv;
112 #ifdef DEBUG
113 #define ABORT do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
114 goto out;} while (0)
115 #else
116 #define ABORT goto out
117 #endif
118
119 if (bus_space_map(iot, iobase, LPT_NPORTS, 0, &ioh))
120 return 0;
121 rv = 0;
122 mask = 0xff;
123
124 data = 0x55; /* Alternating zeros */
125 if (!lpt_port_test(iot, ioh, iobase, lpt_data, data, mask))
126 ABORT;
127
128 data = 0xaa; /* Alternating ones */
129 if (!lpt_port_test(iot, ioh, iobase, lpt_data, data, mask))
130 ABORT;
131
132 for (i = 0; i < CHAR_BIT; i++) { /* Walking zero */
133 data = ~(1 << i);
134 if (!lpt_port_test(iot, ioh, iobase, lpt_data, data, mask))
135 ABORT;
136 }
137
138 for (i = 0; i < CHAR_BIT; i++) { /* Walking one */
139 data = (1 << i);
140 if (!lpt_port_test(iot, ioh, iobase, lpt_data, data, mask))
141 ABORT;
142 }
143
144 bus_space_write_1(iot, ioh, lpt_data, 0);
145 bus_space_write_1(iot, ioh, lpt_control, 0);
146
147 rv = LPT_NPORTS;
148
149 out:
150 bus_space_unmap(iot, ioh, LPT_NPORTS);
151 return rv;
152 }
153
154 /*
155 * int lpt_pioc_probe(device_t parent, cfdata_t cf, void *aux)
156 *
157 * Make sure we are trying to attach a lpt device and then
158 * probe for one.
159 */
160
161 static int
lpt_pioc_probe(device_t parent,cfdata_t match,void * aux)162 lpt_pioc_probe(device_t parent, cfdata_t match, void *aux)
163 {
164 struct pioc_attach_args *pa = aux;
165 int rv;
166
167 if (pa->pa_name && strcmp(pa->pa_name, "lpt") != 0)
168 return(0);
169
170 /* We need an offset */
171 if (pa->pa_offset == PIOCCF_OFFSET_DEFAULT)
172 return(0);
173
174 rv = lptprobe(pa->pa_iot, pa->pa_iobase + pa->pa_offset);
175
176 if (rv) {
177 pa->pa_iosize = rv;
178 return(1);
179 }
180 return(0);
181 }
182
183 /*
184 * void lpt_pioc_attach(device_t parent, device_t self, void *aux)
185 *
186 * attach the lpt device
187 */
188
189 static void
lpt_pioc_attach(device_t parent,device_t self,void * aux)190 lpt_pioc_attach(device_t parent, device_t self, void *aux)
191 {
192 struct lpt_softc *sc = device_private(self);
193 struct pioc_attach_args *pa = aux;
194 bus_space_tag_t iot;
195 bus_space_handle_t ioh;
196 u_int iobase;
197
198 sc->sc_dev = self;
199 if (pa->pa_irq != MAINBUSCF_IRQ_DEFAULT)
200 aprint_normal("\n");
201 else
202 aprint_normal(": polled\n");
203
204 iobase = pa->pa_iobase + pa->pa_offset;
205
206 iot = sc->sc_iot = pa->pa_iot;
207 if (bus_space_map(iot, iobase, LPT_NPORTS, 0, &ioh)) {
208 aprint_error_dev(self, "couldn't map I/O ports");
209 return;
210 }
211 sc->sc_ioh = ioh;
212
213 lpt_attach_subr(sc);
214
215 if (pa->pa_irq != MAINBUSCF_IRQ_DEFAULT)
216 sc->sc_ih = intr_claim(pa->pa_irq, IPL_TTY, "lpt",
217 lptintr, sc);
218 }
219
220 /* End of lpt_pioc.c */
221