Home | History | Annotate | Line # | Download | only in isa
lpt_isa.c revision 1.70
      1  1.70   msaitoh /*	$NetBSD: lpt_isa.c,v 1.70 2019/12/27 09:28:41 msaitoh 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.70   msaitoh __KERNEL_RCSID(0, "$NetBSD: lpt_isa.c,v 1.70 2019/12/27 09:28:41 msaitoh 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/buf.h>
     63   1.9   mycroft #include <sys/kernel.h>
     64   1.9   mycroft #include <sys/ioctl.h>
     65   1.9   mycroft #include <sys/uio.h>
     66  1.11   mycroft #include <sys/device.h>
     67  1.11   mycroft #include <sys/syslog.h>
     68   1.1       cgd 
     69  1.64        ad #include <sys/bus.h>
     70  1.64        ad #include <sys/intr.h>
     71   1.9   mycroft 
     72  1.30       cgd #include <dev/isa/isavar.h>
     73  1.45        is #include <dev/ic/lptreg.h>
     74  1.45        is #include <dev/ic/lptvar.h>
     75  1.11   mycroft 
     76  1.11   mycroft #define	LPTPRI		(PZERO+8)
     77  1.11   mycroft #define	LPT_BSIZE	1024
     78   1.1       cgd 
     79  1.44     mikel #ifndef LPTDEBUG
     80  1.38  christos #define LPRINTF(a)
     81   1.1       cgd #else
     82  1.45        is #define LPRINTF(a)	if (lpt_isa_debug) printf a
     83  1.45        is int lpt_isa_debug = 0;
     84   1.1       cgd #endif
     85   1.1       cgd 
     86  1.45        is struct lpt_isa_softc {
     87  1.45        is 	struct lpt_softc sc_lpt;
     88  1.25   mycroft 	int sc_irq;
     89  1.65    dyoung 	isa_chipset_tag_t sc_ic;
     90  1.45        is 
     91  1.15   mycroft };
     92  1.11   mycroft 
     93  1.66      cube int lpt_isa_probe(device_t, cfdata_t, void *);
     94  1.66      cube static void lpt_isa_attach(device_t, device_t, void *);
     95  1.65    dyoung static int lpt_isa_detach(device_t, int);
     96  1.33   thorpej 
     97  1.66      cube CFATTACH_DECL_NEW(lpt_isa, sizeof(struct lpt_isa_softc),
     98  1.65    dyoung     lpt_isa_probe, lpt_isa_attach, lpt_isa_detach, NULL);
     99   1.1       cgd 
    100  1.59     perry int lpt_port_test(bus_space_tag_t, bus_space_handle_t, bus_addr_t,
    101  1.59     perry 	    bus_size_t, u_char, u_char);
    102  1.34       cgd 
    103   1.2       cgd /*
    104  1.11   mycroft  * Internal routine to lptprobe to do port tests of one byte value.
    105   1.2       cgd  */
    106   1.2       cgd int
    107  1.62  christos lpt_port_test(bus_space_tag_t iot, bus_space_handle_t ioh,
    108  1.63  christos     bus_addr_t base, bus_size_t off, u_char data, u_char mask)
    109  1.11   mycroft {
    110  1.11   mycroft 	int timeout;
    111  1.11   mycroft 	u_char temp;
    112   1.2       cgd 
    113  1.11   mycroft 	data &= mask;
    114  1.42   thorpej 	bus_space_write_1(iot, ioh, off, data);
    115  1.11   mycroft 	timeout = 1000;
    116  1.11   mycroft 	do {
    117  1.14   mycroft 		delay(10);
    118  1.42   thorpej 		temp = bus_space_read_1(iot, ioh, off) & mask;
    119  1.11   mycroft 	} while (temp != data && --timeout);
    120  1.44     mikel 	LPRINTF(("lpt: port=0x%x out=0x%x in=0x%x timeout=%d\n",
    121  1.44     mikel 	    (unsigned)(base + off), (unsigned)data, (unsigned)temp, timeout));
    122   1.2       cgd 	return (temp == data);
    123  1.11   mycroft }
    124   1.2       cgd 
    125   1.2       cgd /*
    126   1.2       cgd  * Logic:
    127   1.2       cgd  *	1) You should be able to write to and read back the same value
    128   1.2       cgd  *	   to the data port.  Do an alternating zeros, alternating ones,
    129   1.2       cgd  *	   walking zero, and walking one test to check for stuck bits.
    130   1.2       cgd  *
    131   1.2       cgd  *	2) You should be able to write to and read back the same value
    132   1.2       cgd  *	   to the control port lower 5 bits, the upper 3 bits are reserved
    133  1.70   msaitoh  *	   per the IBM PC technical reference manuals and different boards
    134   1.2       cgd  *	   do different things with them.  Do an alternating zeros, alternating
    135   1.2       cgd  *	   ones, walking zero, and walking one test to check for stuck bits.
    136   1.2       cgd  *
    137   1.2       cgd  *	   Some printers drag the strobe line down when the are powered off
    138   1.2       cgd  * 	   so this bit has been masked out of the control port test.
    139   1.2       cgd  *
    140   1.2       cgd  *	   XXX Some printers may not like a fast pulse on init or strobe, I
    141   1.2       cgd  *	   don't know at this point, if that becomes a problem these bits
    142   1.2       cgd  *	   should be turned off in the mask byte for the control port test.
    143   1.2       cgd  *
    144   1.2       cgd  *	3) Set the data and control ports to a value of 0
    145   1.2       cgd  */
    146   1.2       cgd int
    147  1.66      cube lpt_isa_probe(device_t parent, cfdata_t match, 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.66      cube lpt_isa_attach(device_t parent, device_t self, void *aux)
    218   1.1       cgd {
    219  1.66      cube 	struct lpt_isa_softc *sc = device_private(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.66      cube 	lsc->sc_dev = self;
    226  1.66      cube 
    227  1.52   thorpej 	if (ia->ia_nirq < 1 ||
    228  1.58  drochner 	    ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) {
    229  1.52   thorpej 		sc->sc_irq = -1;
    230  1.66      cube 		aprint_normal(": polled\n");
    231  1.52   thorpej 	} else {
    232  1.52   thorpej 		sc->sc_irq = ia->ia_irq[0].ir_irq;
    233  1.66      cube 		aprint_normal("\n");
    234  1.52   thorpej 	}
    235  1.32       cgd 
    236  1.67  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    237  1.67  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    238  1.67  jmcneill 
    239  1.45        is 	iot = lsc->sc_iot = ia->ia_iot;
    240  1.52   thorpej 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, LPT_NPORTS, 0, &ioh)) {
    241  1.66      cube 		aprint_normal_dev(self, "can't map i/o space\n");
    242  1.47   thorpej 		return;
    243  1.47   thorpej 	}
    244  1.45        is 	lsc->sc_ioh = ioh;
    245  1.32       cgd 
    246  1.45        is 	lpt_attach_subr(lsc);
    247  1.16   mycroft 
    248  1.65    dyoung 	sc->sc_ic = ia->ia_ic;
    249  1.52   thorpej 	if (sc->sc_irq != -1)
    250  1.69  jdolecek 		lsc->sc_ih = isa_intr_establish_xname(sc->sc_ic, sc->sc_irq,
    251  1.69  jdolecek 		    IST_EDGE, IPL_TTY, lptintr, lsc, device_xname(lsc->sc_dev));
    252   1.1       cgd }
    253  1.65    dyoung 
    254  1.65    dyoung static int
    255  1.65    dyoung lpt_isa_detach(device_t self, int flags)
    256  1.65    dyoung {
    257  1.65    dyoung 	int rc;
    258  1.65    dyoung 	struct lpt_isa_softc *sc = device_private(self);
    259  1.65    dyoung 	struct lpt_softc *lsc = &sc->sc_lpt;
    260  1.65    dyoung 
    261  1.65    dyoung 	if ((rc = lpt_detach_subr(self, flags)) != 0)
    262  1.65    dyoung 		return rc;
    263  1.65    dyoung 
    264  1.65    dyoung 	if (sc->sc_irq != -1)
    265  1.65    dyoung 		isa_intr_disestablish(sc->sc_ic, lsc->sc_ih);
    266  1.65    dyoung 
    267  1.65    dyoung 	bus_space_unmap(lsc->sc_iot, lsc->sc_ioh, LPT_NPORTS);
    268  1.65    dyoung 	return 0;
    269  1.65    dyoung }
    270