xref: /netbsd/sys/arch/acorn32/mainbus/lpt_pioc.c (revision bf9ec67e)
1 /*	$NetBSD: lpt_pioc.c,v 1.5 2002/03/10 15:47:44 bjh21 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.5 2002/03/10 15:47:44 bjh21 Exp $");
40 
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <machine/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 __P((bus_space_tag_t, bus_space_handle_t, bus_addr_t,
55     bus_size_t,	u_char, u_char));
56 static int lptprobe __P((bus_space_tag_t, u_int));
57 static int  lpt_pioc_probe  __P((struct device *, struct cfdata *, void *));
58 static void lpt_pioc_attach __P((struct device *, struct device *, void *));
59 
60 /* device attach structure */
61 
62 struct cfattach lpt_pioc_ca = {
63 	sizeof(struct lpt_softc), lpt_pioc_probe, lpt_pioc_attach
64 };
65 
66 /*
67  * Internal routine to lptprobe to do port tests of one byte value.
68  */
69 static int
70 lpt_port_test(iot, ioh, base, off, data, mask)
71 	bus_space_tag_t iot;
72 	bus_space_handle_t ioh;
73 	bus_addr_t base;
74 	bus_size_t off;
75 	u_char data, mask;
76 {
77 	int timeout;
78 	u_char temp;
79 
80 	data &= mask;
81 	bus_space_write_1(iot, ioh, off, data);
82 	timeout = 1000;
83 	do {
84 		delay(10);
85 		temp = bus_space_read_1(iot, ioh, off) & mask;
86 	} while (temp != data && --timeout);
87 	return (temp == data);
88 }
89 
90 /*
91  * Logic:
92  *	1) You should be able to write to and read back the same value
93  *	   to the data port.  Do an alternating zeros, alternating ones,
94  *	   walking zero, and walking one test to check for stuck bits.
95  *
96  *	2) You should be able to write to and read back the same value
97  *	   to the control port lower 5 bits, the upper 3 bits are reserved
98  *	   per the IBM PC technical reference manauls and different boards
99  *	   do different things with them.  Do an alternating zeros, alternating
100  *	   ones, walking zero, and walking one test to check for stuck bits.
101  *
102  *	   Some printers drag the strobe line down when the are powered off
103  * 	   so this bit has been masked out of the control port test.
104  *
105  *	   XXX Some printers may not like a fast pulse on init or strobe, I
106  *	   don't know at this point, if that becomes a problem these bits
107  *	   should be turned off in the mask byte for the control port test.
108  *
109  *	3) Set the data and control ports to a value of 0
110  */
111 static int
112 lptprobe(iot, iobase)
113 	bus_space_tag_t iot;
114 	u_int iobase;
115 {
116 	bus_space_handle_t ioh;
117 	u_char mask, data;
118 	int i, rv;
119 #ifdef DEBUG
120 #define	ABORT	do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
121 		    goto out;} while (0)
122 #else
123 #define	ABORT	goto out
124 #endif
125 
126 	if (bus_space_map(iot, iobase, LPT_NPORTS, 0, &ioh))
127 		return 0;
128 	rv = 0;
129 	mask = 0xff;
130 
131 	data = 0x55;				/* Alternating zeros */
132 	if (!lpt_port_test(iot, ioh, iobase, lpt_data, data, mask))
133 		ABORT;
134 
135 	data = 0xaa;				/* Alternating ones */
136 	if (!lpt_port_test(iot, ioh, iobase, lpt_data, data, mask))
137 		ABORT;
138 
139 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
140 		data = ~(1 << i);
141 		if (!lpt_port_test(iot, ioh, iobase, lpt_data, data, mask))
142 			ABORT;
143 	}
144 
145 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
146 		data = (1 << i);
147 		if (!lpt_port_test(iot, ioh, iobase, lpt_data, data, mask))
148 			ABORT;
149 	}
150 
151 	bus_space_write_1(iot, ioh, lpt_data, 0);
152 	bus_space_write_1(iot, ioh, lpt_control, 0);
153 
154 	rv = LPT_NPORTS;
155 
156 out:
157 	bus_space_unmap(iot, ioh, LPT_NPORTS);
158 	return rv;
159 }
160 
161 /*
162  * int lpt_pioc_probe(struct device *parent, struct cfdata *cf, void *aux)
163  *
164  * Make sure we are trying to attach a lpt device and then
165  * probe for one.
166  */
167 
168 static int
169 lpt_pioc_probe(parent, match, aux)
170 	struct device *parent;
171 	struct cfdata *match;
172 	void *aux;
173 {
174 	struct pioc_attach_args *pa = aux;
175 	int rv;
176 
177 	if (pa->pa_name && strcmp(pa->pa_name, "lpt") != 0)
178 		return(0);
179 
180 	/* We need an offset */
181 	if (pa->pa_offset == PIOCCF_OFFSET_DEFAULT)
182 		return(0);
183 
184 	rv = lptprobe(pa->pa_iot, pa->pa_iobase + pa->pa_offset);
185 
186 	if (rv) {
187 		pa->pa_iosize = rv;
188 		return(1);
189 	}
190 	return(0);
191 }
192 
193 /*
194  * void lpt_pioc_attach(struct device *parent, struct device *self, void *aux)
195  *
196  * attach the lpt device
197  */
198 
199 static void
200 lpt_pioc_attach(parent, self, aux)
201 	struct device *parent, *self;
202 	void *aux;
203 {
204 	struct lpt_softc *sc = (void *)self;
205 	struct pioc_attach_args *pa = aux;
206 	bus_space_tag_t iot;
207 	bus_space_handle_t ioh;
208 	u_int iobase;
209 
210 	if (pa->pa_irq != MAINBUSCF_IRQ_DEFAULT)
211 		printf("\n");
212 	else
213 		printf(": polled\n");
214 
215 	iobase = pa->pa_iobase + pa->pa_offset;
216 
217 	iot = sc->sc_iot = pa->pa_iot;
218 	if (bus_space_map(iot, iobase, LPT_NPORTS, 0, &ioh))
219 		panic("lptattach: couldn't map I/O ports");
220 	sc->sc_ioh = ioh;
221 
222 	lpt_attach_subr(sc);
223 
224 	if (pa->pa_irq != MAINBUSCF_IRQ_DEFAULT)
225 		sc->sc_ih = intr_claim(pa->pa_irq, IPL_TTY, "lpt",
226 		    lptintr, sc);
227 }
228 
229 /* End of lpt_pioc.c */
230