Home | History | Annotate | Line # | Download | only in isa
atppc_isa.c revision 1.3
      1 /* $NetBSD: atppc_isa.c,v 1.3 2004/01/25 00:28:01 bjh21 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 <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: atppc_isa.c,v 1.3 2004/01/25 00:28:01 bjh21 Exp $");
     34 
     35 #include "opt_atppc.h"
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/malloc.h>
     41 #include <sys/device.h>
     42 
     43 #include <machine/intr.h>
     44 #include <machine/bus.h>
     45 
     46 #include <dev/isa/isareg.h>
     47 #include <dev/isa/isavar.h>
     48 #include <dev/isa/isadmavar.h>
     49 #include <dev/isa/atppc_isa_subr.h>
     50 
     51 #include <dev/ic/atppcreg.h>
     52 #include <dev/ic/atppcvar.h>
     53 
     54 /*
     55  * ISA bus attach code for atppc driver.
     56  * Note on capabilities: capabilites may exist in the chipset but may not
     57  * necessarily be useable. I.e. you may specify an IRQ in the autoconfig, but
     58  * will the port actually have an IRQ assigned to it at the hardware level?
     59  * How can you test if the capabilites can be used? For interrupts, see if a
     60  * handler exists (sc_intr != NULL). For DMA, see if the sc_dma_start() and
     61  * sc_dma_finish() function pointers are not NULL.
     62  */
     63 
     64 /* Probe, attach, and detach functions for a atppc device on the ISA bus. */
     65 static int atppc_isa_probe __P((struct device *, struct cfdata *, void *));
     66 static void atppc_isa_attach __P((struct device *, struct device *, void *));
     67 static int atppc_isa_detach __P((struct device *, int));
     68 
     69 CFATTACH_DECL(atppc_isa, sizeof(struct atppc_isa_softc), atppc_isa_probe,
     70 	atppc_isa_attach, atppc_isa_detach, NULL);
     71 
     72 /*
     73  * Probe function: find parallel port controller on isa bus. Combined from
     74  * lpt_isa_probe() in lpt.c and atppc_detect_port() from FreeBSD's ppc.c.
     75  */
     76 static int
     77 atppc_isa_probe(struct device * parent, struct cfdata * cf, void * aux)
     78 {
     79 	bus_space_handle_t ioh;
     80 	struct isa_attach_args * ia = aux;
     81 	bus_space_tag_t iot = ia->ia_iot;
     82 	int addr = ia->ia_io->ir_addr;
     83 	int rval = 0;
     84 
     85 	if(ia->ia_nio < 1 || addr == ISACF_PORT_DEFAULT) {
     86 		printf("%s(%s): io port unknown.\n", __func__,
     87 			parent->dv_xname);
     88 	}
     89 	else if(bus_space_map(iot, addr, IO_LPTSIZE, 0, &ioh) == 0) {
     90 		if (atppc_detect_port(iot, ioh) == 0)
     91 			rval = 1;
     92 		else
     93 			printf("%s(%s): unable to write/read I/O port.\n",
     94 				__func__, parent->dv_xname);
     95 		bus_space_unmap(iot, ioh, IO_LPTSIZE);
     96 	}
     97 	else {
     98 		printf("%s(%s): attempt to map bus space failed.\n", __func__,
     99 			parent->dv_xname);
    100 	}
    101 
    102 	if(rval) {
    103 		ia->ia_nio = 1;
    104 		ia->ia_io[0].ir_size = IO_LPTSIZE;
    105 		ia->ia_nirq = 1;
    106 		ia->ia_ndrq = 1;
    107 		ia->ia_niomem = 0;
    108 	}
    109 
    110 	return rval;
    111 }
    112 
    113 /* Attach function: attach and configure parallel port controller on isa bus. */
    114 static void
    115 atppc_isa_attach(struct device * parent, struct device * self, void * aux)
    116 {
    117 	struct atppc_isa_softc * sc = (struct atppc_isa_softc *)self;
    118 	struct atppc_softc * lsc = (struct atppc_softc *)self;
    119 	struct isa_attach_args * ia = aux;
    120 
    121 	lsc->sc_iot = ia->ia_iot;
    122 	lsc->sc_dmat = ia->ia_dmat;
    123 	lsc->sc_has = 0;
    124 	sc->sc_ic = ia->ia_ic;
    125 	sc->sc_iobase = ia->ia_io->ir_addr;
    126 
    127 	if(bus_space_map(lsc->sc_iot, sc->sc_iobase, IO_LPTSIZE, 0,
    128 		&lsc->sc_ioh) != 0) {
    129 		printf("%s: attempt to map bus space failed, device not "
    130 			"properly attached.\n", self->dv_xname);
    131 		lsc->sc_dev_ok = ATPPC_NOATTACH;
    132 		return;
    133 	}
    134 	else {
    135 		lsc->sc_dev_ok = ATPPC_ATTACHED;
    136 	}
    137 
    138 	/* Assign interrupt handler */
    139 	if(!(self->dv_cfdata->cf_flags & ATPPC_FLAG_DISABLE_INTR)) {
    140 		if((ia->ia_irq->ir_irq != ISACF_IRQ_DEFAULT) &&
    141 			(ia->ia_nirq > 0)) {
    142 
    143 			sc->sc_irq = ia->ia_irq->ir_irq;
    144 			/* Establish interrupt handler. */
    145 			if(!(atppc_isa_intr_establish(lsc))) {
    146 				lsc->sc_has |= ATPPC_HAS_INTR;
    147 			}
    148 		}
    149 		else {
    150 			ATPPC_DPRINTF(("%s: IRQ not assigned or bad number of "
    151 				"IRQs.\n", self->dv_xname));
    152 		}
    153 	}
    154 	else {
    155 		ATPPC_VPRINTF(("%s: interrupts not configured due to flags.\n",
    156 			self->dv_xname));
    157 	}
    158 
    159 	/* Configure DMA */
    160 	if(!(self->dv_cfdata->cf_flags & ATPPC_FLAG_DISABLE_DMA)) {
    161 		if((ia->ia_drq->ir_drq != ISACF_DRQ_DEFAULT) &&
    162 			(ia->ia_ndrq > 0)) {
    163 
    164 			sc->sc_drq = ia->ia_drq->ir_drq;
    165 			if(!(atppc_isa_dma_setup(lsc))) {
    166 				lsc->sc_has |= ATPPC_HAS_DMA;
    167 			}
    168 		}
    169 		else {
    170 			ATPPC_DPRINTF(("%s: DRQ not assigned or bad number of "
    171 				"DRQs.\n", self->dv_xname));
    172 		}
    173 	}
    174 	else {
    175 		ATPPC_VPRINTF(("%s: dma not configured due to flags.\n",
    176 			self->dv_xname));
    177 	}
    178 
    179 	/* Run soft configuration attach */
    180 	atppc_sc_attach(lsc);
    181 
    182 	return;
    183 }
    184 
    185 /* Detach function: used to detach atppc driver at run time. */
    186 static int
    187 atppc_isa_detach(struct device * dev, int flag)
    188 {
    189 	struct atppc_softc * lsc = (struct atppc_softc *) dev;
    190 	int rval;
    191 
    192 	if(lsc->sc_dev_ok == ATPPC_ATTACHED) {
    193 		/* Run soft configuration detach first */
    194 		rval = atppc_sc_detach(lsc, flag);
    195 
    196 		/* Disable DMA */
    197 		atppc_isa_dma_remove(lsc);
    198 
    199 		/* Disestablish interrupt handler */
    200 		atppc_isa_intr_disestablish(lsc);
    201 
    202 		/* Unmap bus space */
    203 		bus_space_unmap(lsc->sc_iot, lsc->sc_ioh, IO_LPTSIZE);
    204 
    205 		/* Mark config data */
    206 		lsc->sc_dev_ok = ATPPC_NOATTACH;
    207 	}
    208 	else {
    209 		rval = 0;
    210 		if(!(flag & DETACH_QUIET)) {
    211 			printf("%s not properly attached! Detach routines "
    212 				"skipped.\n", dev->dv_xname);
    213 		}
    214 	}
    215 
    216 	if(!(flag & DETACH_QUIET)) {
    217 		printf("%s: detached.", dev->dv_xname);
    218 	}
    219 
    220 	return rval;
    221 }
    222