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