Home | History | Annotate | Line # | Download | only in dev
lpt_pcc.c revision 1.7
      1  1.7  thorpej /*	$NetBSD: lpt_pcc.c,v 1.7 2002/10/02 05:28:13 thorpej Exp $ */
      2  1.2      scw 
      3  1.2      scw /*-
      4  1.2      scw  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.2      scw  * All rights reserved.
      6  1.2      scw  *
      7  1.2      scw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2      scw  * by Steve C. Woodford.
      9  1.2      scw  *
     10  1.2      scw  * Redistribution and use in source and binary forms, with or without
     11  1.2      scw  * modification, are permitted provided that the following conditions
     12  1.2      scw  * are met:
     13  1.2      scw  * 1. Redistributions of source code must retain the above copyright
     14  1.2      scw  *    notice, this list of conditions and the following disclaimer.
     15  1.2      scw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2      scw  *    notice, this list of conditions and the following disclaimer in the
     17  1.2      scw  *    documentation and/or other materials provided with the distribution.
     18  1.2      scw  * 3. All advertising materials mentioning features or use of this software
     19  1.2      scw  *    must display the following acknowledgement:
     20  1.2      scw  *	This product includes software developed by the NetBSD
     21  1.2      scw  *	Foundation, Inc. and its contributors.
     22  1.2      scw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.2      scw  *    contributors may be used to endorse or promote products derived
     24  1.2      scw  *    from this software without specific prior written permission.
     25  1.2      scw  *
     26  1.2      scw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.2      scw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.2      scw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.2      scw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.2      scw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.2      scw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.2      scw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.2      scw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.2      scw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.2      scw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.2      scw  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2      scw  */
     38  1.2      scw 
     39  1.2      scw /*
     40  1.2      scw  * Device Driver back-end for the MVME147's parallel printer port
     41  1.2      scw  */
     42  1.2      scw 
     43  1.2      scw #include <sys/param.h>
     44  1.2      scw #include <sys/systm.h>
     45  1.2      scw #include <sys/kernel.h>
     46  1.2      scw #include <sys/device.h>
     47  1.2      scw #include <sys/syslog.h>
     48  1.2      scw 
     49  1.3      scw #include <machine/bus.h>
     50  1.2      scw 
     51  1.5      scw #include <dev/mvme/lptvar.h>
     52  1.5      scw 
     53  1.2      scw #include <mvme68k/dev/lpt_pccreg.h>
     54  1.2      scw #include <mvme68k/dev/pccreg.h>
     55  1.2      scw #include <mvme68k/dev/pccvar.h>
     56  1.2      scw 
     57  1.2      scw 
     58  1.2      scw 
     59  1.3      scw static int lpt_pcc_intr __P((void *));
     60  1.3      scw static void lpt_pcc_open __P((struct lpt_softc *, int));
     61  1.3      scw static void lpt_pcc_close __P((struct lpt_softc *));
     62  1.3      scw static void lpt_pcc_iprime __P((struct lpt_softc *));
     63  1.3      scw static void lpt_pcc_speed __P((struct lpt_softc *, int));
     64  1.3      scw static int lpt_pcc_notrdy __P((struct lpt_softc *, int));
     65  1.3      scw static void lpt_pcc_wr_data __P((struct lpt_softc *, u_char));
     66  1.2      scw 
     67  1.2      scw struct lpt_funcs lpt_pcc_funcs = {
     68  1.2      scw 	lpt_pcc_open,
     69  1.2      scw 	lpt_pcc_close,
     70  1.2      scw 	lpt_pcc_iprime,
     71  1.2      scw 	lpt_pcc_speed,
     72  1.2      scw 	lpt_pcc_notrdy,
     73  1.2      scw 	lpt_pcc_wr_data
     74  1.2      scw };
     75  1.2      scw 
     76  1.2      scw /*
     77  1.2      scw  * Autoconfig stuff
     78  1.2      scw  */
     79  1.3      scw static int lpt_pcc_match __P((struct device *, struct cfdata *, void *));
     80  1.2      scw static void lpt_pcc_attach __P((struct device *, struct device *, void *));
     81  1.2      scw 
     82  1.7  thorpej CFATTACH_DECL(lpt_pcc, sizeof(struct lpt_softc),
     83  1.7  thorpej     lpt_pcc_match, lpt_pcc_attach, NULL, NULL);
     84  1.2      scw 
     85  1.2      scw extern struct cfdriver lpt_cd;
     86  1.2      scw 
     87  1.2      scw 
     88  1.2      scw /*ARGSUSED*/
     89  1.3      scw static int
     90  1.2      scw lpt_pcc_match(parent, cf, args)
     91  1.2      scw 	struct device *parent;
     92  1.2      scw 	struct cfdata *cf;
     93  1.2      scw 	void *args;
     94  1.2      scw {
     95  1.3      scw 	struct pcc_attach_args *pa;
     96  1.3      scw 
     97  1.3      scw 	pa = args;
     98  1.2      scw 
     99  1.2      scw 	if (strcmp(pa->pa_name, lpt_cd.cd_name))
    100  1.2      scw 		return (0);
    101  1.2      scw 
    102  1.2      scw 	pa->pa_ipl = cf->pcccf_ipl;
    103  1.2      scw 	return (1);
    104  1.2      scw }
    105  1.2      scw 
    106  1.2      scw /*ARGSUSED*/
    107  1.2      scw static void
    108  1.2      scw lpt_pcc_attach(parent, self, args)
    109  1.3      scw 	struct device *parent, *self;
    110  1.3      scw 	void *args;
    111  1.2      scw {
    112  1.3      scw 	struct lpt_softc *sc;
    113  1.3      scw 	struct pcc_attach_args *pa;
    114  1.3      scw 
    115  1.3      scw 	sc = (struct lpt_softc *) self;
    116  1.3      scw 	pa = args;
    117  1.3      scw 
    118  1.3      scw 	sc->sc_bust = pa->pa_bust;
    119  1.3      scw 	bus_space_map(pa->pa_bust, pa->pa_offset, LPREG_SIZE, 0, &sc->sc_bush);
    120  1.2      scw 
    121  1.2      scw 	sc->sc_ipl = pa->pa_ipl & PCC_IMASK;
    122  1.3      scw 	sc->sc_funcs = &lpt_pcc_funcs;
    123  1.2      scw 	sc->sc_laststatus = 0;
    124  1.2      scw 
    125  1.2      scw 	printf(": PCC Parallel Printer\n");
    126  1.2      scw 
    127  1.2      scw 	/*
    128  1.2      scw 	 * Disable interrupts until device is opened
    129  1.2      scw 	 */
    130  1.3      scw 	pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 0);
    131  1.2      scw 
    132  1.2      scw 	/*
    133  1.2      scw 	 * Main attachment code
    134  1.2      scw 	 */
    135  1.2      scw 	lpt_attach_subr(sc);
    136  1.2      scw 
    137  1.4      scw 	/* Register the event counter */
    138  1.4      scw 	evcnt_attach_dynamic(&sc->sc_evcnt, EVCNT_TYPE_INTR,
    139  1.4      scw 	    pccintr_evcnt(sc->sc_ipl), "printer", sc->sc_dev.dv_xname);
    140  1.4      scw 
    141  1.2      scw 	/*
    142  1.2      scw 	 * Hook into the printer interrupt
    143  1.2      scw 	 */
    144  1.4      scw 	pccintr_establish(PCCV_PRINTER, lpt_pcc_intr, sc->sc_ipl, sc,
    145  1.4      scw 	    &sc->sc_evcnt);
    146  1.2      scw }
    147  1.2      scw 
    148  1.2      scw /*
    149  1.2      scw  * Handle printer interrupts which occur when the printer is ready to accept
    150  1.2      scw  * another char.
    151  1.2      scw  */
    152  1.2      scw int
    153  1.2      scw lpt_pcc_intr(arg)
    154  1.2      scw 	void *arg;
    155  1.2      scw {
    156  1.3      scw 	struct lpt_softc *sc;
    157  1.2      scw 	int i;
    158  1.2      scw 
    159  1.3      scw 	sc = arg;
    160  1.3      scw 
    161  1.2      scw 	/* is printer online and ready for output */
    162  1.2      scw 	if (lpt_pcc_notrdy(sc, 0) && lpt_pcc_notrdy(sc, 1))
    163  1.2      scw 		return 0;
    164  1.2      scw 
    165  1.2      scw 	i = lpt_intr(sc);
    166  1.2      scw 
    167  1.3      scw 	if (pcc_reg_read(sys_pcc, PCCREG_PRNT_INTR_CTRL) & LPI_ACKINT) {
    168  1.3      scw 		pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
    169  1.3      scw 		    sc->sc_icr | LPI_ACKINT);
    170  1.3      scw 	}
    171  1.2      scw 
    172  1.3      scw 	return (i);
    173  1.2      scw }
    174  1.2      scw 
    175  1.2      scw 
    176  1.2      scw static void
    177  1.2      scw lpt_pcc_open(sc, int_ena)
    178  1.2      scw 	struct lpt_softc *sc;
    179  1.2      scw 	int int_ena;
    180  1.2      scw {
    181  1.2      scw 	int sps;
    182  1.2      scw 
    183  1.3      scw 	pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
    184  1.3      scw 	    LPI_ACKINT | LPI_FAULTINT);
    185  1.2      scw 
    186  1.3      scw 	if (int_ena == 0) {
    187  1.2      scw 		sps = splhigh();
    188  1.2      scw 		sc->sc_icr = sc->sc_ipl | LPI_ENABLE;
    189  1.3      scw 		pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, sc->sc_icr);
    190  1.2      scw 		splx(sps);
    191  1.2      scw 	}
    192  1.2      scw }
    193  1.2      scw 
    194  1.2      scw static void
    195  1.2      scw lpt_pcc_close(sc)
    196  1.2      scw 	struct lpt_softc *sc;
    197  1.2      scw {
    198  1.3      scw 
    199  1.3      scw 	pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 0);
    200  1.2      scw 	sc->sc_icr = sc->sc_ipl;
    201  1.3      scw 	pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, sc->sc_icr);
    202  1.2      scw }
    203  1.2      scw 
    204  1.3      scw /* ARGSUSED */
    205  1.2      scw static void
    206  1.2      scw lpt_pcc_iprime(sc)
    207  1.2      scw 	struct lpt_softc *sc;
    208  1.2      scw {
    209  1.3      scw 
    210  1.3      scw 	lpt_control_write(LPC_INPUT_PRIME);
    211  1.2      scw 	delay(100);
    212  1.2      scw }
    213  1.2      scw 
    214  1.3      scw /* ARGSUSED */
    215  1.2      scw static void
    216  1.2      scw lpt_pcc_speed(sc, speed)
    217  1.2      scw 	struct lpt_softc *sc;
    218  1.2      scw 	int speed;
    219  1.2      scw {
    220  1.3      scw 
    221  1.3      scw 	if (speed == LPT_STROBE_FAST)
    222  1.3      scw 		lpt_control_write(LPC_FAST_STROBE);
    223  1.2      scw 	else
    224  1.3      scw 		lpt_control_write(0);
    225  1.2      scw }
    226  1.2      scw 
    227  1.2      scw static int
    228  1.2      scw lpt_pcc_notrdy(sc, err)
    229  1.2      scw 	struct lpt_softc *sc;
    230  1.2      scw 	int err;
    231  1.2      scw {
    232  1.2      scw 	u_char status;
    233  1.2      scw 	u_char new;
    234  1.2      scw 
    235  1.2      scw #define	LPS_INVERT	(LPS_SELECT)
    236  1.2      scw #define	LPS_MASK	(LPS_SELECT|LPS_FAULT|LPS_BUSY|LPS_PAPER_EMPTY)
    237  1.2      scw 
    238  1.3      scw 	status = (lpt_status_read(sc) ^ LPS_INVERT) & LPS_MASK;
    239  1.2      scw 
    240  1.3      scw 	if (err) {
    241  1.2      scw 		new = status & ~sc->sc_laststatus;
    242  1.2      scw 		sc->sc_laststatus = status;
    243  1.2      scw 
    244  1.2      scw 		if (new & LPS_SELECT)
    245  1.2      scw 			log(LOG_NOTICE, "%s: offline\n",
    246  1.3      scw 			    sc->sc_dev.dv_xname);
    247  1.2      scw 		else if (new & LPS_PAPER_EMPTY)
    248  1.2      scw 			log(LOG_NOTICE, "%s: out of paper\n",
    249  1.3      scw 			    sc->sc_dev.dv_xname);
    250  1.2      scw 		else if (new & LPS_FAULT)
    251  1.2      scw 			log(LOG_NOTICE, "%s: output error\n",
    252  1.3      scw 			    sc->sc_dev.dv_xname);
    253  1.2      scw 	}
    254  1.2      scw 
    255  1.3      scw 	pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
    256  1.3      scw 	    sc->sc_icr | LPI_FAULTINT);
    257  1.2      scw 
    258  1.3      scw 	return (status);
    259  1.2      scw }
    260  1.2      scw 
    261  1.2      scw static void
    262  1.2      scw lpt_pcc_wr_data(sc, data)
    263  1.2      scw 	struct lpt_softc *sc;
    264  1.2      scw 	u_char data;
    265  1.2      scw {
    266  1.2      scw 
    267  1.3      scw 	lpt_data_write(sc, data);
    268  1.2      scw }
    269