xref: /netbsd/sys/arch/arc/jazz/lpt_jazzio.c (revision c4a72b64)
1 /*	$NetBSD: lpt_jazzio.c,v 1.3 2002/10/02 04:59:49 thorpej Exp $	*/
2 /*	$OpenBSD: lpt_lbus.c,v 1.3 1997/04/10 16:29:17 pefo Exp $	*/
3 
4 /*
5  * Copyright (c) 1993, 1994 Charles M. Hannum.
6  * Copyright (c) 1990 William F. Jolitz, TeleMuse
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This software is a component of "386BSD" developed by
20  *	William F. Jolitz, TeleMuse.
21  * 4. Neither the name of the developer nor the name "386BSD"
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
26  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
27  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
28  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
29  * NOT MAKE USE OF THIS WORK.
30  *
31  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
32  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
33  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
34  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
35  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
36  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
37  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
38  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  */
52 
53 /*
54  * Device Driver for AT parallel printer port
55  */
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/device.h>
60 
61 #include <machine/autoconf.h>
62 #include <machine/bus.h>
63 #include <machine/intr.h>
64 
65 #include <dev/ic/lptreg.h>
66 #include <dev/ic/lptvar.h>
67 
68 #include <arc/jazz/jazziovar.h>
69 
70 int lpt_jazzio_probe __P((struct device *, struct cfdata *, void *));
71 void lpt_jazzio_attach __P((struct device *, struct device *, void *));
72 
73 CFATTACH_DECL(lpt_jazzio, sizeof(struct lpt_softc),
74     lpt_jazzio_probe, lpt_jazzio_attach, NULL, NULL);
75 
76 /*
77  * XXX - copied from lpt_isa.c
78  *	sys/arch/arm32/mainbus/lpt_pioc.c also copies this.
79  *	sys/arch/amiga/dev/lpt_supio.c doesn't.
80  */
81 static int lpt_port_test __P((bus_space_tag_t, bus_space_handle_t, bus_addr_t,
82 		bus_size_t, u_char, u_char));
83 /*
84  * Internal routine to lptprobe to do port tests of one byte value.
85  */
86 static int
87 lpt_port_test(iot, ioh, base, off, data, mask)
88 	bus_space_tag_t iot;
89 	bus_space_handle_t ioh;
90 	bus_addr_t base;
91 	bus_size_t off;
92 	u_char data, mask;
93 {
94 	int timeout;
95 	u_char temp;
96 
97 	data &= mask;
98 	bus_space_write_1(iot, ioh, off, data);
99 	timeout = 1000;
100 	do {
101 		delay(10);
102 		temp = bus_space_read_1(iot, ioh, off) & mask;
103 	} while (temp != data && --timeout);
104 	return (temp == data);
105 }
106 
107 int
108 lpt_jazzio_probe(parent, match, aux)
109 	struct device *parent;
110 	struct cfdata *match;
111 	void *aux;
112 {
113 	struct jazzio_attach_args *ja = aux;
114 	bus_space_tag_t iot;
115 	bus_space_handle_t ioh;
116 	bus_addr_t base;
117 	u_int8_t mask, data;
118 	int i, rv;
119 
120 #ifdef DEBUG
121 #define	ABORT								     \
122 	do {								     \
123 		printf("lpt_jazzio_probe: mask %x data %x failed\n", mask,   \
124 		    data);						     \
125 		goto out;						     \
126 	} while (0)
127 #else
128 #define	ABORT	goto out
129 #endif
130 
131 	if(strcmp(ja->ja_name, "lpt") != 0)
132 		 return (0);
133 
134 	iot = ja->ja_bust;
135 	base = ja->ja_addr;
136 	if (bus_space_map(iot, base, LPT_NPORTS, 0, &ioh))
137 		return 0;
138 
139 	rv = 0;
140 	mask = 0xff;
141 
142 	data = 0x55;				/* Alternating zeros */
143 	if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
144 		ABORT;
145 
146 	data = 0xaa;				/* Alternating ones */
147 	if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
148 		ABORT;
149 
150 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
151 		data = ~(1 << i);
152 		if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
153 			ABORT;
154 	}
155 
156 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
157 		data = (1 << i);
158 		if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
159 			ABORT;
160 	}
161 
162 	bus_space_write_1(iot, ioh, lpt_data, 0);
163 	bus_space_write_1(iot, ioh, lpt_control, 0);
164 
165 	rv = 1;
166 
167 out:
168 	bus_space_unmap(iot, ioh, LPT_NPORTS);
169 	return rv;
170 }
171 
172 void
173 lpt_jazzio_attach(parent, self, aux)
174 	struct device *parent, *self;
175 	void *aux;
176 {
177 	struct lpt_softc *sc = (void *)self;
178 	struct jazzio_attach_args *ja = aux;
179 	bus_space_tag_t iot;
180 	bus_space_handle_t ioh;
181 
182 	printf("\n");
183 
184 	sc->sc_state = 0;
185 	iot = sc->sc_iot = ja->ja_bust;
186 	if (bus_space_map(iot, ja->ja_addr, LPT_NPORTS, 0, &ioh)) {
187 		printf("%s: can't map i/o space\n", self->dv_xname);
188 		return;
189 	}
190 	sc->sc_ioh = ioh;
191 
192 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
193 
194 	jazzio_intr_establish(ja->ja_intr, lptintr, sc);
195 }
196