1 /* $NetBSD: lpt_pioc.c,v 1.9 2008/03/07 17:15:51 cube Exp $ */
4 * Copyright (c) 1997 Mark Brinicombe
5 * Copyright (c) 1997 Causality Limited
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 * Device Driver for AT parallel printer port
37 #include <sys/param.h>
39 __KERNEL_RCSID(0, "$NetBSD: lpt_pioc.c,v 1.9 2008/03/07 17:15:51 cube Exp $");
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>
52 /* Prototypes for functions */
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 *);
60 /* device attach structure */
62 CFATTACH_DECL_NEW(lpt_pioc
, sizeof(struct lpt_softc
),
63 lpt_pioc_probe
, lpt_pioc_attach
, NULL
, NULL
);
66 * Internal routine to lptprobe to do port tests of one byte value.
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
)
76 bus_space_write_1(iot
, ioh
, off
, data
);
80 temp
= bus_space_read_1(iot
, ioh
, off
) & mask
;
81 } while (temp
!= data
&& --timeout
);
82 return (temp
== data
);
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.
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 manauls 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.
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.
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.
104 * 3) Set the data and control ports to a value of 0
107 lptprobe(bus_space_tag_t iot
, u_int iobase
)
109 bus_space_handle_t ioh
;
113 #define ABORT do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
116 #define ABORT goto out
119 if (bus_space_map(iot
, iobase
, LPT_NPORTS
, 0, &ioh
))
124 data
= 0x55; /* Alternating zeros */
125 if (!lpt_port_test(iot
, ioh
, iobase
, lpt_data
, data
, mask
))
128 data
= 0xaa; /* Alternating ones */
129 if (!lpt_port_test(iot
, ioh
, iobase
, lpt_data
, data
, mask
))
132 for (i
= 0; i
< CHAR_BIT
; i
++) { /* Walking zero */
134 if (!lpt_port_test(iot
, ioh
, iobase
, lpt_data
, data
, mask
))
138 for (i
= 0; i
< CHAR_BIT
; i
++) { /* Walking one */
140 if (!lpt_port_test(iot
, ioh
, iobase
, lpt_data
, data
, mask
))
144 bus_space_write_1(iot
, ioh
, lpt_data
, 0);
145 bus_space_write_1(iot
, ioh
, lpt_control
, 0);
150 bus_space_unmap(iot
, ioh
, LPT_NPORTS
);
155 * int lpt_pioc_probe(struct device *parent, struct cfdata *cf, void *aux)
157 * Make sure we are trying to attach a lpt device and then
162 lpt_pioc_probe(device_t parent
, cfdata_t match
, void *aux
)
164 struct pioc_attach_args
*pa
= aux
;
167 if (pa
->pa_name
&& strcmp(pa
->pa_name
, "lpt") != 0)
170 /* We need an offset */
171 if (pa
->pa_offset
== PIOCCF_OFFSET_DEFAULT
)
174 rv
= lptprobe(pa
->pa_iot
, pa
->pa_iobase
+ pa
->pa_offset
);
184 * void lpt_pioc_attach(struct device *parent, struct device *self, void *aux)
186 * attach the lpt device
190 lpt_pioc_attach(device_t parent
, device_t self
, void *aux
)
192 struct lpt_softc
*sc
= device_private(self
);
193 struct pioc_attach_args
*pa
= aux
;
195 bus_space_handle_t ioh
;
199 if (pa
->pa_irq
!= MAINBUSCF_IRQ_DEFAULT
)
202 aprint_normal(": polled\n");
204 iobase
= pa
->pa_iobase
+ pa
->pa_offset
;
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");
215 if (pa
->pa_irq
!= MAINBUSCF_IRQ_DEFAULT
)
216 sc
->sc_ih
= intr_claim(pa
->pa_irq
, IPL_TTY
, "lpt",
220 /* End of lpt_pioc.c */