Home | History | Annotate | Line # | Download | only in isa
lpt_isa.c revision 1.56
      1  1.56    simonb /*	$NetBSD: lpt_isa.c,v 1.56 2003/01/20 05:30:06 simonb Exp $	*/
      2  1.23       cgd 
      3   1.1       cgd /*
      4  1.49   mycroft  * Copyright (c) 1993, 1994 Charles M. Hannum.
      5   1.1       cgd  * Copyright (c) 1990 William F. Jolitz, TeleMuse
      6   1.1       cgd  * All rights reserved.
      7   1.1       cgd  *
      8   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9   1.1       cgd  * modification, are permitted provided that the following conditions
     10   1.1       cgd  * are met:
     11   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17   1.1       cgd  *    must display the following acknowledgement:
     18   1.1       cgd  *	This software is a component of "386BSD" developed by
     19   1.1       cgd  *	William F. Jolitz, TeleMuse.
     20   1.1       cgd  * 4. Neither the name of the developer nor the name "386BSD"
     21   1.1       cgd  *    may be used to endorse or promote products derived from this software
     22   1.1       cgd  *    without specific prior written permission.
     23   1.1       cgd  *
     24   1.1       cgd  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
     25   1.1       cgd  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
     26   1.1       cgd  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
     27   1.1       cgd  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
     28   1.1       cgd  * NOT MAKE USE OF THIS WORK.
     29   1.1       cgd  *
     30   1.1       cgd  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
     31   1.1       cgd  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
     32   1.1       cgd  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
     33   1.1       cgd  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
     34   1.1       cgd  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
     35   1.1       cgd  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
     36   1.1       cgd  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
     37   1.1       cgd  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
     38   1.1       cgd  *
     39   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
     40   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     41   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     42   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
     43   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     44   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     45   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     46   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     47   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     48   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     49   1.1       cgd  * SUCH DAMAGE.
     50   1.1       cgd  */
     51   1.1       cgd 
     52   1.1       cgd /*
     53   1.1       cgd  * Device Driver for AT parallel printer port
     54   1.1       cgd  */
     55  1.51     lukem 
     56  1.51     lukem #include <sys/cdefs.h>
     57  1.56    simonb __KERNEL_RCSID(0, "$NetBSD: lpt_isa.c,v 1.56 2003/01/20 05:30:06 simonb Exp $");
     58   1.1       cgd 
     59   1.9   mycroft #include <sys/param.h>
     60   1.9   mycroft #include <sys/systm.h>
     61   1.9   mycroft #include <sys/proc.h>
     62   1.9   mycroft #include <sys/user.h>
     63   1.9   mycroft #include <sys/buf.h>
     64   1.9   mycroft #include <sys/kernel.h>
     65   1.9   mycroft #include <sys/ioctl.h>
     66   1.9   mycroft #include <sys/uio.h>
     67  1.11   mycroft #include <sys/device.h>
     68  1.11   mycroft #include <sys/syslog.h>
     69   1.1       cgd 
     70  1.39   mycroft #include <machine/bus.h>
     71  1.37       cgd #include <machine/intr.h>
     72   1.9   mycroft 
     73  1.30       cgd #include <dev/isa/isavar.h>
     74  1.45        is #include <dev/ic/lptreg.h>
     75  1.45        is #include <dev/ic/lptvar.h>
     76  1.11   mycroft 
     77  1.11   mycroft #define	LPTPRI		(PZERO+8)
     78  1.11   mycroft #define	LPT_BSIZE	1024
     79   1.1       cgd 
     80  1.44     mikel #ifndef LPTDEBUG
     81  1.38  christos #define LPRINTF(a)
     82   1.1       cgd #else
     83  1.45        is #define LPRINTF(a)	if (lpt_isa_debug) printf a
     84  1.45        is int lpt_isa_debug = 0;
     85   1.1       cgd #endif
     86   1.1       cgd 
     87  1.45        is struct lpt_isa_softc {
     88  1.45        is 	struct lpt_softc sc_lpt;
     89  1.25   mycroft 	int sc_irq;
     90  1.45        is 
     91  1.15   mycroft };
     92  1.11   mycroft 
     93  1.45        is int lpt_isa_probe __P((struct device *, struct cfdata *, void *));
     94  1.45        is void lpt_isa_attach __P((struct device *, struct device *, void *));
     95  1.33   thorpej 
     96  1.54   thorpej CFATTACH_DECL(lpt_isa, sizeof(struct lpt_isa_softc),
     97  1.55   thorpej     lpt_isa_probe, lpt_isa_attach, NULL, NULL);
     98   1.1       cgd 
     99  1.42   thorpej int	lpt_port_test __P((bus_space_tag_t, bus_space_handle_t, bus_addr_t,
    100  1.42   thorpej 	    bus_size_t, u_char, u_char));
    101  1.34       cgd 
    102   1.2       cgd /*
    103  1.11   mycroft  * Internal routine to lptprobe to do port tests of one byte value.
    104   1.2       cgd  */
    105   1.2       cgd int
    106  1.42   thorpej lpt_port_test(iot, ioh, base, off, data, mask)
    107  1.42   thorpej 	bus_space_tag_t iot;
    108  1.42   thorpej 	bus_space_handle_t ioh;
    109  1.42   thorpej 	bus_addr_t base;
    110  1.42   thorpej 	bus_size_t off;
    111  1.11   mycroft 	u_char data, mask;
    112  1.11   mycroft {
    113  1.11   mycroft 	int timeout;
    114  1.11   mycroft 	u_char temp;
    115   1.2       cgd 
    116  1.11   mycroft 	data &= mask;
    117  1.42   thorpej 	bus_space_write_1(iot, ioh, off, data);
    118  1.11   mycroft 	timeout = 1000;
    119  1.11   mycroft 	do {
    120  1.14   mycroft 		delay(10);
    121  1.42   thorpej 		temp = bus_space_read_1(iot, ioh, off) & mask;
    122  1.11   mycroft 	} while (temp != data && --timeout);
    123  1.44     mikel 	LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n",
    124  1.44     mikel 	    (unsigned)(base + off), (unsigned)data, (unsigned)temp, timeout));
    125   1.2       cgd 	return (temp == data);
    126  1.11   mycroft }
    127   1.2       cgd 
    128   1.2       cgd /*
    129   1.2       cgd  * Logic:
    130   1.2       cgd  *	1) You should be able to write to and read back the same value
    131   1.2       cgd  *	   to the data port.  Do an alternating zeros, alternating ones,
    132   1.2       cgd  *	   walking zero, and walking one test to check for stuck bits.
    133   1.2       cgd  *
    134   1.2       cgd  *	2) You should be able to write to and read back the same value
    135   1.2       cgd  *	   to the control port lower 5 bits, the upper 3 bits are reserved
    136   1.2       cgd  *	   per the IBM PC technical reference manauls and different boards
    137   1.2       cgd  *	   do different things with them.  Do an alternating zeros, alternating
    138   1.2       cgd  *	   ones, walking zero, and walking one test to check for stuck bits.
    139   1.2       cgd  *
    140   1.2       cgd  *	   Some printers drag the strobe line down when the are powered off
    141   1.2       cgd  * 	   so this bit has been masked out of the control port test.
    142   1.2       cgd  *
    143   1.2       cgd  *	   XXX Some printers may not like a fast pulse on init or strobe, I
    144   1.2       cgd  *	   don't know at this point, if that becomes a problem these bits
    145   1.2       cgd  *	   should be turned off in the mask byte for the control port test.
    146   1.2       cgd  *
    147   1.2       cgd  *	3) Set the data and control ports to a value of 0
    148   1.2       cgd  */
    149   1.2       cgd int
    150  1.45        is lpt_isa_probe(parent, match, aux)
    151  1.25   mycroft 	struct device *parent;
    152  1.43       cgd 	struct cfdata *match;
    153  1.43       cgd 	void *aux;
    154  1.11   mycroft {
    155  1.15   mycroft 	struct isa_attach_args *ia = aux;
    156  1.42   thorpej 	bus_space_tag_t iot;
    157  1.42   thorpej 	bus_space_handle_t ioh;
    158  1.32       cgd 	u_long base;
    159  1.11   mycroft 	u_char mask, data;
    160  1.32       cgd 	int i, rv;
    161   1.2       cgd 
    162  1.11   mycroft #ifdef DEBUG
    163  1.41  christos #define	ABORT	do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
    164  1.32       cgd 		    goto out;} while (0)
    165  1.11   mycroft #else
    166  1.32       cgd #define	ABORT	goto out
    167  1.11   mycroft #endif
    168  1.46   thorpej 
    169  1.52   thorpej 	if (ia->ia_nio < 1)
    170  1.52   thorpej 		return (0);
    171  1.52   thorpej 
    172  1.52   thorpej 	if (ISA_DIRECT_CONFIG(ia))
    173  1.52   thorpej 		return (0);
    174  1.52   thorpej 
    175  1.46   thorpej 	/* Disallow wildcarded i/o address. */
    176  1.52   thorpej 	if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
    177  1.46   thorpej 		return (0);
    178   1.2       cgd 
    179  1.42   thorpej 	iot = ia->ia_iot;
    180  1.56    simonb 	base = ia->ia_io[0].ir_addr;
    181  1.42   thorpej 	if (bus_space_map(iot, base, LPT_NPORTS, 0, &ioh))
    182  1.32       cgd 		return 0;
    183  1.32       cgd 
    184  1.32       cgd 	rv = 0;
    185   1.2       cgd 	mask = 0xff;
    186  1.11   mycroft 
    187  1.28   mycroft 	data = 0x55;				/* Alternating zeros */
    188  1.42   thorpej 	if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
    189  1.28   mycroft 		ABORT;
    190  1.28   mycroft 
    191  1.28   mycroft 	data = 0xaa;				/* Alternating ones */
    192  1.42   thorpej 	if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
    193  1.28   mycroft 		ABORT;
    194  1.28   mycroft 
    195  1.28   mycroft 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
    196  1.28   mycroft 		data = ~(1 << i);
    197  1.42   thorpej 		if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
    198  1.11   mycroft 			ABORT;
    199  1.28   mycroft 	}
    200   1.2       cgd 
    201  1.28   mycroft 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
    202  1.28   mycroft 		data = (1 << i);
    203  1.42   thorpej 		if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
    204  1.11   mycroft 			ABORT;
    205  1.28   mycroft 	}
    206   1.2       cgd 
    207  1.42   thorpej 	bus_space_write_1(iot, ioh, lpt_data, 0);
    208  1.42   thorpej 	bus_space_write_1(iot, ioh, lpt_control, 0);
    209  1.11   mycroft 
    210  1.52   thorpej 	ia->ia_io[0].ir_size = LPT_NPORTS;
    211  1.52   thorpej 
    212  1.52   thorpej 	ia->ia_niomem = 0;
    213  1.52   thorpej 	ia->ia_ndrq = 0;
    214  1.32       cgd 
    215  1.32       cgd 	rv = 1;
    216  1.32       cgd 
    217  1.32       cgd out:
    218  1.42   thorpej 	bus_space_unmap(iot, ioh, LPT_NPORTS);
    219  1.32       cgd 	return rv;
    220  1.11   mycroft }
    221   1.1       cgd 
    222  1.15   mycroft void
    223  1.45        is lpt_isa_attach(parent, self, aux)
    224  1.15   mycroft 	struct device *parent, *self;
    225  1.15   mycroft 	void *aux;
    226   1.1       cgd {
    227  1.45        is 	struct lpt_isa_softc *sc = (void *)self;
    228  1.45        is 	struct lpt_softc *lsc = &sc->sc_lpt;
    229  1.15   mycroft 	struct isa_attach_args *ia = aux;
    230  1.42   thorpej 	bus_space_tag_t iot;
    231  1.42   thorpej 	bus_space_handle_t ioh;
    232  1.15   mycroft 
    233  1.52   thorpej 	if (ia->ia_nirq < 1 ||
    234  1.52   thorpej 	    ia->ia_irq[0].ir_irq == ISACF_IRQ_DEFAULT) {
    235  1.52   thorpej 		sc->sc_irq = -1;
    236  1.52   thorpej 		printf(": polled\n");
    237  1.52   thorpej 	} else {
    238  1.52   thorpej 		sc->sc_irq = ia->ia_irq[0].ir_irq;
    239  1.41  christos 		printf("\n");
    240  1.52   thorpej 	}
    241  1.32       cgd 
    242  1.45        is 	iot = lsc->sc_iot = ia->ia_iot;
    243  1.52   thorpej 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, LPT_NPORTS, 0, &ioh)) {
    244  1.47   thorpej 		printf("%s: can't map i/o space\n", self->dv_xname);
    245  1.47   thorpej 		return;
    246  1.47   thorpej 	}
    247  1.45        is 	lsc->sc_ioh = ioh;
    248  1.32       cgd 
    249  1.45        is 	lpt_attach_subr(lsc);
    250  1.16   mycroft 
    251  1.52   thorpej 	if (sc->sc_irq != -1)
    252  1.52   thorpej 		lsc->sc_ih = isa_intr_establish(ia->ia_ic, sc->sc_irq,
    253  1.52   thorpej 		    IST_EDGE, IPL_TTY, lptintr, lsc);
    254   1.1       cgd }
    255