xref: /netbsd-src/sys/arch/arc/jazz/lpt_jazzio.c (revision cf10107d5d3746e98e37d403b996f1965f67f255)
1*cf10107dSdyoung /*	$NetBSD: lpt_jazzio.c,v 1.10 2011/07/01 19:25:42 dyoung Exp $	*/
2459f2585Sur /*	$OpenBSD: lpt_lbus.c,v 1.3 1997/04/10 16:29:17 pefo Exp $	*/
3459f2585Sur 
4459f2585Sur /*
5459f2585Sur  * Copyright (c) 1993, 1994 Charles M. Hannum.
6459f2585Sur  * Copyright (c) 1990 William F. Jolitz, TeleMuse
7459f2585Sur  * All rights reserved.
8459f2585Sur  *
9459f2585Sur  * Redistribution and use in source and binary forms, with or without
10459f2585Sur  * modification, are permitted provided that the following conditions
11459f2585Sur  * are met:
12459f2585Sur  * 1. Redistributions of source code must retain the above copyright
13459f2585Sur  *    notice, this list of conditions and the following disclaimer.
14459f2585Sur  * 2. Redistributions in binary form must reproduce the above copyright
15459f2585Sur  *    notice, this list of conditions and the following disclaimer in the
16459f2585Sur  *    documentation and/or other materials provided with the distribution.
17459f2585Sur  * 3. All advertising materials mentioning features or use of this software
18459f2585Sur  *    must display the following acknowledgement:
19459f2585Sur  *	This software is a component of "386BSD" developed by
20459f2585Sur  *	William F. Jolitz, TeleMuse.
21459f2585Sur  * 4. Neither the name of the developer nor the name "386BSD"
22459f2585Sur  *    may be used to endorse or promote products derived from this software
23459f2585Sur  *    without specific prior written permission.
24459f2585Sur  *
25459f2585Sur  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
26459f2585Sur  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
27459f2585Sur  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
28459f2585Sur  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
29459f2585Sur  * NOT MAKE USE OF THIS WORK.
30459f2585Sur  *
31459f2585Sur  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
32459f2585Sur  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
33459f2585Sur  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
34459f2585Sur  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
35459f2585Sur  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
36459f2585Sur  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
37459f2585Sur  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
38459f2585Sur  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
39459f2585Sur  *
40459f2585Sur  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
41459f2585Sur  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42459f2585Sur  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43459f2585Sur  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
44459f2585Sur  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45459f2585Sur  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46459f2585Sur  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47459f2585Sur  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48459f2585Sur  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49459f2585Sur  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50459f2585Sur  * SUCH DAMAGE.
51459f2585Sur  */
52459f2585Sur 
53459f2585Sur /*
54459f2585Sur  * Device Driver for AT parallel printer port
55459f2585Sur  */
56459f2585Sur 
57a4183603Slukem #include <sys/cdefs.h>
58*cf10107dSdyoung __KERNEL_RCSID(0, "$NetBSD: lpt_jazzio.c,v 1.10 2011/07/01 19:25:42 dyoung Exp $");
59a4183603Slukem 
60459f2585Sur #include <sys/param.h>
61459f2585Sur #include <sys/systm.h>
62459f2585Sur #include <sys/device.h>
63459f2585Sur 
64459f2585Sur #include <machine/autoconf.h>
65*cf10107dSdyoung #include <sys/bus.h>
66459f2585Sur #include <machine/intr.h>
67459f2585Sur 
68459f2585Sur #include <dev/ic/lptreg.h>
69459f2585Sur #include <dev/ic/lptvar.h>
70459f2585Sur 
71459f2585Sur #include <arc/jazz/jazziovar.h>
72459f2585Sur 
738ecf8999Scube int lpt_jazzio_probe(device_t, cfdata_t , void *);
748ecf8999Scube void lpt_jazzio_attach(device_t, device_t, void *);
75459f2585Sur 
768ecf8999Scube CFATTACH_DECL_NEW(lpt_jazzio, sizeof(struct lpt_softc),
77c5e91d44Sthorpej     lpt_jazzio_probe, lpt_jazzio_attach, NULL, NULL);
78459f2585Sur 
79459f2585Sur /*
80459f2585Sur  * XXX - copied from lpt_isa.c
81459f2585Sur  *	sys/arch/arm32/mainbus/lpt_pioc.c also copies this.
82459f2585Sur  *	sys/arch/amiga/dev/lpt_supio.c doesn't.
83459f2585Sur  */
847fe2a5a0Stsutsui static int lpt_port_test(bus_space_tag_t, bus_space_handle_t, bus_addr_t,
857fe2a5a0Stsutsui 		bus_size_t, u_char, u_char);
86459f2585Sur /*
87459f2585Sur  * Internal routine to lptprobe to do port tests of one byte value.
88459f2585Sur  */
89459f2585Sur 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)907fe2a5a0Stsutsui lpt_port_test(bus_space_tag_t iot, bus_space_handle_t ioh, bus_addr_t base,
917fe2a5a0Stsutsui     bus_size_t off, u_char data, u_char mask)
92459f2585Sur {
93459f2585Sur 	int timeout;
94459f2585Sur 	u_char temp;
95459f2585Sur 
96459f2585Sur 	data &= mask;
97459f2585Sur 	bus_space_write_1(iot, ioh, off, data);
98459f2585Sur 	timeout = 1000;
99459f2585Sur 	do {
100459f2585Sur 		delay(10);
101459f2585Sur 		temp = bus_space_read_1(iot, ioh, off) & mask;
102459f2585Sur 	} while (temp != data && --timeout);
1037fe2a5a0Stsutsui 	return temp == data;
104459f2585Sur }
105459f2585Sur 
106459f2585Sur int
lpt_jazzio_probe(device_t parent,cfdata_t match,void * aux)1078ecf8999Scube lpt_jazzio_probe(device_t parent, cfdata_t match, void *aux)
108459f2585Sur {
109459f2585Sur 	struct jazzio_attach_args *ja = aux;
110459f2585Sur 	bus_space_tag_t iot;
111459f2585Sur 	bus_space_handle_t ioh;
112459f2585Sur 	bus_addr_t base;
1137fe2a5a0Stsutsui 	uint8_t mask, data;
114459f2585Sur 	int i, rv;
115459f2585Sur 
116459f2585Sur #ifdef DEBUG
117459f2585Sur #define	ABORT								     \
118459f2585Sur 	do {								     \
119459f2585Sur 		printf("lpt_jazzio_probe: mask %x data %x failed\n", mask,   \
120459f2585Sur 		    data);						     \
121459f2585Sur 		goto out;						     \
122459f2585Sur 	} while (0)
123459f2585Sur #else
124459f2585Sur #define	ABORT	goto out
125459f2585Sur #endif
126459f2585Sur 
127da446ea2Stsutsui 	if (strcmp(ja->ja_name, "LPT1") != 0)
128459f2585Sur 		 return (0);
129459f2585Sur 
130459f2585Sur 	iot = ja->ja_bust;
131459f2585Sur 	base = ja->ja_addr;
132459f2585Sur 	if (bus_space_map(iot, base, LPT_NPORTS, 0, &ioh))
133459f2585Sur 		return 0;
134459f2585Sur 
135459f2585Sur 	rv = 0;
136459f2585Sur 	mask = 0xff;
137459f2585Sur 
138459f2585Sur 	data = 0x55;				/* Alternating zeros */
139459f2585Sur 	if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
140459f2585Sur 		ABORT;
141459f2585Sur 
142459f2585Sur 	data = 0xaa;				/* Alternating ones */
143459f2585Sur 	if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
144459f2585Sur 		ABORT;
145459f2585Sur 
146459f2585Sur 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
147459f2585Sur 		data = ~(1 << i);
148459f2585Sur 		if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
149459f2585Sur 			ABORT;
150459f2585Sur 	}
151459f2585Sur 
152459f2585Sur 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
153459f2585Sur 		data = (1 << i);
154459f2585Sur 		if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
155459f2585Sur 			ABORT;
156459f2585Sur 	}
157459f2585Sur 
158459f2585Sur 	bus_space_write_1(iot, ioh, lpt_data, 0);
159459f2585Sur 	bus_space_write_1(iot, ioh, lpt_control, 0);
160459f2585Sur 
161459f2585Sur 	rv = 1;
162459f2585Sur 
163459f2585Sur out:
164459f2585Sur 	bus_space_unmap(iot, ioh, LPT_NPORTS);
165459f2585Sur 	return rv;
166459f2585Sur }
167459f2585Sur 
168459f2585Sur void
lpt_jazzio_attach(device_t parent,device_t self,void * aux)1698ecf8999Scube lpt_jazzio_attach(device_t parent, device_t self, void *aux)
170459f2585Sur {
1718ecf8999Scube 	struct lpt_softc *sc = device_private(self);
172459f2585Sur 	struct jazzio_attach_args *ja = aux;
173459f2585Sur 	bus_space_tag_t iot;
174459f2585Sur 	bus_space_handle_t ioh;
175459f2585Sur 
1768ecf8999Scube 	aprint_normal("\n");
177459f2585Sur 
1788ecf8999Scube 	sc->sc_dev = self;
179459f2585Sur 	sc->sc_state = 0;
180459f2585Sur 	iot = sc->sc_iot = ja->ja_bust;
181459f2585Sur 	if (bus_space_map(iot, ja->ja_addr, LPT_NPORTS, 0, &ioh)) {
1828ecf8999Scube 		aprint_error_dev(self, "can't map i/o space\n");
183459f2585Sur 		return;
184459f2585Sur 	}
185459f2585Sur 	sc->sc_ioh = ioh;
186459f2585Sur 
187459f2585Sur 	bus_space_write_1(iot, ioh, lpt_control, LPC_NINIT);
188459f2585Sur 
189459f2585Sur 	jazzio_intr_establish(ja->ja_intr, lptintr, sc);
190459f2585Sur }
191