Home | History | Annotate | Line # | Download | only in mainbus
atppc_pioc.c revision 1.2.8.1
      1 /* $NetBSD: atppc_pioc.c,v 1.2.8.1 2006/04/01 12:06:06 yamt Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2001 Alcove - Nicolas Souchu
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  *
     28  * FreeBSD: src/sys/isa/ppc.c,v 1.26.2.5 2001/10/02 05:21:45 nsouch Exp
     29  *
     30  */
     31 
     32 #include "opt_atppc.h"
     33 
     34 #include <sys/param.h>
     35 __KERNEL_RCSID(0, "$NetBSD: atppc_pioc.c,v 1.2.8.1 2006/04/01 12:06:06 yamt Exp $");
     36 
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/malloc.h>
     40 #include <sys/device.h>
     41 
     42 #include <machine/intr.h>
     43 #include <machine/bus.h>
     44 
     45 #include <arch/acorn32/mainbus/piocvar.h>
     46 
     47 #include <dev/ic/atppcreg.h>
     48 #include <dev/ic/atppcvar.h>
     49 
     50 #include "locators.h"
     51 
     52 /* Probe and attach functions for a atppc device on the PIOC. */
     53 static int atppc_pioc_probe(struct device *, struct cfdata *, void *);
     54 static void atppc_pioc_attach(struct device *, struct device *, void *);
     55 
     56 CFATTACH_DECL(atppc_pioc, sizeof(struct atppc_softc), atppc_pioc_probe,
     57 	atppc_pioc_attach, NULL, NULL);
     58 
     59 #define IO_LPTSIZE 8
     60 
     61 /*
     62  * Probe function: find parallel port controller on isa bus. Combined from
     63  * lpt_isa_probe() in lpt.c and atppc_detect_port() from FreeBSD's ppc.c.
     64  */
     65 static int
     66 atppc_pioc_probe(struct device *parent, struct cfdata *cf, void *aux)
     67 {
     68 	bus_space_handle_t ioh;
     69 	struct pioc_attach_args *pa = aux;
     70 	bus_space_tag_t iot = pa->pa_iot;
     71 	int addr = pa->pa_iobase + pa->pa_offset;
     72 	int rval = 0;
     73 
     74 	if (pa->pa_name && strcmp(pa->pa_name, "lpt") != 0)
     75 		return 0;
     76 
     77 	if (pa->pa_offset == PIOCCF_OFFSET_DEFAULT) {
     78 		aprint_error("%s(%s): io port unknown.\n", __func__,
     79 			parent->dv_xname);
     80 	} else if (bus_space_map(iot, addr, IO_LPTSIZE, 0, &ioh) == 0) {
     81 		if (atppc_detect_port(iot, ioh) == 0)
     82 			rval = 1;
     83 		else
     84 			aprint_error("%s(%s): unable to write/read I/O "
     85 			    "port.\n", __func__, parent->dv_xname);
     86 		bus_space_unmap(iot, ioh, IO_LPTSIZE);
     87 	} else {
     88 		aprint_error("%s(%s): attempt to map bus space failed.\n",
     89 		    __func__, parent->dv_xname);
     90 	}
     91 
     92 	return rval;
     93 }
     94 
     95 /* Attach function: attach and configure parallel port controller on isa bus. */
     96 static void
     97 atppc_pioc_attach(struct device *parent, struct device *self, void *aux)
     98 {
     99 	struct atppc_softc *sc = (struct atppc_softc *)self;
    100 	struct pioc_attach_args *pa = aux;
    101 	bus_addr_t iobase;
    102 
    103 	sc->sc_iot = pa->pa_iot;
    104 	sc->sc_has = 0;
    105 	iobase = pa->pa_iobase + pa->pa_offset;
    106 
    107 	if (bus_space_map(sc->sc_iot, iobase, IO_LPTSIZE, 0,
    108 		&sc->sc_ioh) != 0) {
    109 		aprint_error(": attempt to map bus space failed, device not "
    110 			"properly attached.\n");
    111 		sc->sc_dev_ok = ATPPC_NOATTACH;
    112 		return;
    113 	}
    114 	sc->sc_dev_ok = ATPPC_ATTACHED;
    115 
    116 	/* Assign interrupt handler */
    117 	if (!(device_cfdata(self)->cf_flags & ATPPC_FLAG_DISABLE_INTR)) {
    118 		if (pa->pa_irq != MAINBUSCF_IRQ_DEFAULT) {
    119 			/* Establish interrupt handler. */
    120 			sc->sc_ieh = intr_claim(pa->pa_irq, IPL_TTY, "atppc",
    121 			    atppcintr, sc);
    122 			sc->sc_has |= ATPPC_HAS_INTR;
    123 		} else
    124 			ATPPC_DPRINTF(("%s: IRQ not assigned or bad number of "
    125 				"IRQs.\n", self->dv_xname));
    126 	} else
    127 		ATPPC_VPRINTF(("%s: interrupts not configured due to flags.\n",
    128 			self->dv_xname));
    129 
    130 	/* Run soft configuration attach */
    131 	atppc_sc_attach(sc);
    132 }
    133