Home | History | Annotate | Line # | Download | only in isa
lpt_isa.c revision 1.63
      1  1.63  christos /*	$NetBSD: lpt_isa.c,v 1.63 2006/11/16 01:33:00 christos 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.60     perry  *	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.60     perry  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
     25  1.60     perry  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
     26  1.60     perry  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
     27  1.60     perry  * 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.60     perry  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
     32  1.60     perry  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES
     33  1.60     perry  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
     34  1.60     perry  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
     35  1.60     perry  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
     36  1.60     perry  * 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.63  christos __KERNEL_RCSID(0, "$NetBSD: lpt_isa.c,v 1.63 2006/11/16 01:33:00 christos 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.59     perry int lpt_isa_probe(struct device *, struct cfdata *, void *);
     94  1.59     perry void lpt_isa_attach(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.59     perry int lpt_port_test(bus_space_tag_t, bus_space_handle_t, bus_addr_t,
    100  1.59     perry 	    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.62  christos lpt_port_test(bus_space_tag_t iot, bus_space_handle_t ioh,
    107  1.63  christos     bus_addr_t base, bus_size_t off, u_char data, u_char mask)
    108  1.11   mycroft {
    109  1.11   mycroft 	int timeout;
    110  1.11   mycroft 	u_char temp;
    111   1.2       cgd 
    112  1.11   mycroft 	data &= mask;
    113  1.42   thorpej 	bus_space_write_1(iot, ioh, off, data);
    114  1.11   mycroft 	timeout = 1000;
    115  1.11   mycroft 	do {
    116  1.14   mycroft 		delay(10);
    117  1.42   thorpej 		temp = bus_space_read_1(iot, ioh, off) & mask;
    118  1.11   mycroft 	} while (temp != data && --timeout);
    119  1.44     mikel 	LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n",
    120  1.44     mikel 	    (unsigned)(base + off), (unsigned)data, (unsigned)temp, timeout));
    121   1.2       cgd 	return (temp == data);
    122  1.11   mycroft }
    123   1.2       cgd 
    124   1.2       cgd /*
    125   1.2       cgd  * Logic:
    126   1.2       cgd  *	1) You should be able to write to and read back the same value
    127   1.2       cgd  *	   to the data port.  Do an alternating zeros, alternating ones,
    128   1.2       cgd  *	   walking zero, and walking one test to check for stuck bits.
    129   1.2       cgd  *
    130   1.2       cgd  *	2) You should be able to write to and read back the same value
    131   1.2       cgd  *	   to the control port lower 5 bits, the upper 3 bits are reserved
    132   1.2       cgd  *	   per the IBM PC technical reference manauls and different boards
    133   1.2       cgd  *	   do different things with them.  Do an alternating zeros, alternating
    134   1.2       cgd  *	   ones, walking zero, and walking one test to check for stuck bits.
    135   1.2       cgd  *
    136   1.2       cgd  *	   Some printers drag the strobe line down when the are powered off
    137   1.2       cgd  * 	   so this bit has been masked out of the control port test.
    138   1.2       cgd  *
    139   1.2       cgd  *	   XXX Some printers may not like a fast pulse on init or strobe, I
    140   1.2       cgd  *	   don't know at this point, if that becomes a problem these bits
    141   1.2       cgd  *	   should be turned off in the mask byte for the control port test.
    142   1.2       cgd  *
    143   1.2       cgd  *	3) Set the data and control ports to a value of 0
    144   1.2       cgd  */
    145   1.2       cgd int
    146  1.63  christos lpt_isa_probe(struct device *parent, struct cfdata *match,
    147  1.62  christos     void *aux)
    148  1.11   mycroft {
    149  1.15   mycroft 	struct isa_attach_args *ia = aux;
    150  1.42   thorpej 	bus_space_tag_t iot;
    151  1.42   thorpej 	bus_space_handle_t ioh;
    152  1.32       cgd 	u_long base;
    153  1.11   mycroft 	u_char mask, data;
    154  1.32       cgd 	int i, rv;
    155   1.2       cgd 
    156  1.57  christos #ifdef LPT_DEBUG
    157  1.41  christos #define	ABORT	do {printf("lptprobe: mask %x data %x failed\n", mask, data); \
    158  1.32       cgd 		    goto out;} while (0)
    159  1.11   mycroft #else
    160  1.32       cgd #define	ABORT	goto out
    161  1.11   mycroft #endif
    162  1.46   thorpej 
    163  1.52   thorpej 	if (ia->ia_nio < 1)
    164  1.52   thorpej 		return (0);
    165  1.52   thorpej 
    166  1.52   thorpej 	if (ISA_DIRECT_CONFIG(ia))
    167  1.52   thorpej 		return (0);
    168  1.52   thorpej 
    169  1.46   thorpej 	/* Disallow wildcarded i/o address. */
    170  1.58  drochner 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    171  1.46   thorpej 		return (0);
    172   1.2       cgd 
    173  1.42   thorpej 	iot = ia->ia_iot;
    174  1.56    simonb 	base = ia->ia_io[0].ir_addr;
    175  1.42   thorpej 	if (bus_space_map(iot, base, LPT_NPORTS, 0, &ioh))
    176  1.32       cgd 		return 0;
    177  1.32       cgd 
    178  1.32       cgd 	rv = 0;
    179   1.2       cgd 	mask = 0xff;
    180  1.11   mycroft 
    181  1.28   mycroft 	data = 0x55;				/* Alternating zeros */
    182  1.42   thorpej 	if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
    183  1.28   mycroft 		ABORT;
    184  1.28   mycroft 
    185  1.28   mycroft 	data = 0xaa;				/* Alternating ones */
    186  1.42   thorpej 	if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
    187  1.28   mycroft 		ABORT;
    188  1.28   mycroft 
    189  1.28   mycroft 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking zero */
    190  1.28   mycroft 		data = ~(1 << i);
    191  1.42   thorpej 		if (!lpt_port_test(iot, ioh, base, lpt_data, data, mask))
    192  1.11   mycroft 			ABORT;
    193  1.28   mycroft 	}
    194   1.2       cgd 
    195  1.28   mycroft 	for (i = 0; i < CHAR_BIT; i++) {	/* Walking one */
    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.42   thorpej 	bus_space_write_1(iot, ioh, lpt_data, 0);
    202  1.42   thorpej 	bus_space_write_1(iot, ioh, lpt_control, 0);
    203  1.11   mycroft 
    204  1.52   thorpej 	ia->ia_io[0].ir_size = LPT_NPORTS;
    205  1.52   thorpej 
    206  1.52   thorpej 	ia->ia_niomem = 0;
    207  1.52   thorpej 	ia->ia_ndrq = 0;
    208  1.32       cgd 
    209  1.32       cgd 	rv = 1;
    210  1.32       cgd 
    211  1.32       cgd out:
    212  1.42   thorpej 	bus_space_unmap(iot, ioh, LPT_NPORTS);
    213  1.32       cgd 	return rv;
    214  1.11   mycroft }
    215   1.1       cgd 
    216  1.15   mycroft void
    217  1.63  christos lpt_isa_attach(struct device *parent, struct device *self, void *aux)
    218   1.1       cgd {
    219  1.45        is 	struct lpt_isa_softc *sc = (void *)self;
    220  1.45        is 	struct lpt_softc *lsc = &sc->sc_lpt;
    221  1.15   mycroft 	struct isa_attach_args *ia = aux;
    222  1.42   thorpej 	bus_space_tag_t iot;
    223  1.42   thorpej 	bus_space_handle_t ioh;
    224  1.15   mycroft 
    225  1.52   thorpej 	if (ia->ia_nirq < 1 ||
    226  1.58  drochner 	    ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) {
    227  1.52   thorpej 		sc->sc_irq = -1;
    228  1.52   thorpej 		printf(": polled\n");
    229  1.52   thorpej 	} else {
    230  1.52   thorpej 		sc->sc_irq = ia->ia_irq[0].ir_irq;
    231  1.41  christos 		printf("\n");
    232  1.52   thorpej 	}
    233  1.32       cgd 
    234  1.45        is 	iot = lsc->sc_iot = ia->ia_iot;
    235  1.52   thorpej 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, LPT_NPORTS, 0, &ioh)) {
    236  1.47   thorpej 		printf("%s: can't map i/o space\n", self->dv_xname);
    237  1.47   thorpej 		return;
    238  1.47   thorpej 	}
    239  1.45        is 	lsc->sc_ioh = ioh;
    240  1.32       cgd 
    241  1.45        is 	lpt_attach_subr(lsc);
    242  1.16   mycroft 
    243  1.52   thorpej 	if (sc->sc_irq != -1)
    244  1.52   thorpej 		lsc->sc_ih = isa_intr_establish(ia->ia_ic, sc->sc_irq,
    245  1.52   thorpej 		    IST_EDGE, IPL_TTY, lptintr, lsc);
    246   1.1       cgd }
    247