Home | History | Annotate | Line # | Download | only in isa
atppc_isa.c revision 1.4
      1 /* $NetBSD: atppc_isa.c,v 1.4 2004/01/25 11:35:46 jdolecek 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.4 2004/01/25 11:35:46 jdolecek 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 
     50 #include <dev/ic/atppcreg.h>
     51 #include <dev/ic/atppcvar.h>
     52 #include <dev/isa/atppc_isadma.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 /* Configuration data for atppc on isa bus. */
     65 struct atppc_isa_softc {
     66 	/* Machine independent device data */
     67 	struct atppc_softc sc_atppc;
     68 
     69 	/* IRQ/DRQ/IO Port assignments on ISA bus */
     70 	int sc_irq;
     71 	int sc_drq;
     72 	int sc_iobase;
     73 
     74 	/* ISA chipset tag */
     75 	isa_chipset_tag_t sc_ic;
     76 };
     77 
     78 /* Probe and attach functions for a atppc device on the ISA bus. */
     79 static int atppc_isa_probe __P((struct device *, struct cfdata *, void *));
     80 static void atppc_isa_attach __P((struct device *, struct device *, void *));
     81 
     82 static int atppc_isa_dma_start(struct atppc_softc *, void *, u_int,
     83 	u_int8_t);
     84 static int atppc_isa_dma_finish(struct atppc_softc *);
     85 static int atppc_isa_dma_abort(struct atppc_softc *);
     86 static int atppc_isa_dma_malloc(struct device *, caddr_t *, bus_addr_t *,
     87 	bus_size_t);
     88 static void atppc_isa_dma_free(struct device *, caddr_t *, bus_addr_t *,
     89 	bus_size_t);
     90 
     91 CFATTACH_DECL(atppc_isa, sizeof(struct atppc_isa_softc), atppc_isa_probe,
     92 	atppc_isa_attach, NULL, NULL);
     93 
     94 /*
     95  * Probe function: find parallel port controller on isa bus. Combined from
     96  * lpt_isa_probe() in lpt.c and atppc_detect_port() from FreeBSD's ppc.c.
     97  */
     98 static int
     99 atppc_isa_probe(struct device * parent, struct cfdata * cf, void * aux)
    100 {
    101 	bus_space_handle_t ioh;
    102 	struct isa_attach_args * ia = aux;
    103 	bus_space_tag_t iot = ia->ia_iot;
    104 
    105 	if (ia->ia_nio < 1)
    106 		return (0);
    107 
    108 	/* Disallow wildcarded i/o address */
    109 	if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
    110 		return (0);
    111 
    112 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, IO_LPTSIZE, 0, &ioh))
    113 		return (0);
    114 
    115 	if (atppc_detect_port(iot, ioh) != 0)
    116 		return (0);
    117 
    118 	ia->ia_nio = 1;
    119 	ia->ia_io[0].ir_size = IO_LPTSIZE;
    120 	ia->ia_nirq = 1;
    121 	ia->ia_ndrq = 1;
    122 	ia->ia_niomem = 0;
    123 
    124 	return (1);
    125 }
    126 
    127 /* Attach function: attach and configure parallel port controller on isa bus. */
    128 static void
    129 atppc_isa_attach(struct device *parent, struct device *self, void *aux)
    130 {
    131 	struct atppc_isa_softc * sc = (struct atppc_isa_softc *)self;
    132 	struct atppc_softc * lsc = (struct atppc_softc *)self;
    133 	struct isa_attach_args * ia = aux;
    134 
    135 	printf(": AT Parallel Port\n");
    136 
    137 	lsc->sc_iot = ia->ia_iot;
    138 	lsc->sc_dmat = ia->ia_dmat;
    139 	lsc->sc_has = 0;
    140 	sc->sc_ic = ia->ia_ic;
    141 	sc->sc_iobase = ia->ia_io->ir_addr;
    142 
    143 	if (bus_space_map(lsc->sc_iot, sc->sc_iobase, IO_LPTSIZE, 0,
    144 		&lsc->sc_ioh) != 0) {
    145 		printf("%s: attempt to map bus space failed, device not "
    146 			"properly attached.\n", self->dv_xname);
    147 		lsc->sc_dev_ok = ATPPC_NOATTACH;
    148 		return;
    149 	}
    150 
    151 	lsc->sc_dev_ok = ATPPC_ATTACHED;
    152 
    153 	/* Assign interrupt handler */
    154 	if (!(self->dv_cfdata->cf_flags & ATPPC_FLAG_DISABLE_INTR)
    155 	   && ia->ia_irq->ir_irq != ISACF_IRQ_DEFAULT
    156 	   && ia->ia_nirq >= 1) {
    157 		sc->sc_irq = ia->ia_irq[0].ir_irq;
    158 	} else
    159 		sc->sc_irq = -1;
    160 
    161 	if (sc->sc_irq > 0) {
    162 		/* Establish interrupt handler. */
    163 		lsc->sc_ieh = isa_intr_establish(sc->sc_ic, sc->sc_irq,
    164 			IST_EDGE, IPL_ATPPC, atppcintr, &lsc->sc_dev);
    165 
    166 		lsc->sc_has |= ATPPC_HAS_INTR;
    167 	}
    168 
    169 	/* Configure DMA */
    170 	if (!(self->dv_cfdata->cf_flags & ATPPC_FLAG_DISABLE_DMA)
    171 	    && ia->ia_drq->ir_drq != ISACF_DRQ_DEFAULT
    172 	    && ia->ia_ndrq >= 1)
    173 		sc->sc_drq = ia->ia_drq[0].ir_drq;
    174 	else
    175 		sc->sc_drq = -1;
    176 
    177 	if (sc->sc_drq != -1
    178 	    && atppc_isadma_setup(lsc, sc->sc_ic, sc->sc_drq) == 0) {
    179 		lsc->sc_has |= ATPPC_HAS_DMA;
    180 
    181 		/* setup DMA hooks */
    182 		lsc->sc_dma_start = atppc_isa_dma_start;
    183 		lsc->sc_dma_finish = atppc_isa_dma_finish;
    184 		lsc->sc_dma_abort = atppc_isa_dma_abort;
    185 		lsc->sc_dma_malloc = atppc_isa_dma_malloc;
    186 		lsc->sc_dma_free = atppc_isa_dma_free;
    187 	}
    188 
    189 	/* Run soft configuration attach */
    190 	atppc_sc_attach(lsc);
    191 
    192 	return;
    193 }
    194 
    195 /* Start DMA operation over ISA bus */
    196 static int
    197 atppc_isa_dma_start(struct atppc_softc *lsc, void *buf, u_int nbytes,
    198 	u_int8_t mode)
    199 {
    200 	struct atppc_isa_softc * sc = (struct atppc_isa_softc *) lsc;
    201 
    202 	return atppc_isadma_start(sc->sc_ic, sc->sc_drq, buf, nbytes, mode);
    203 }
    204 
    205 /* Stop DMA operation over ISA bus */
    206 static int
    207 atppc_isa_dma_finish(struct atppc_softc * lsc)
    208 {
    209 	struct atppc_isa_softc * sc = (struct atppc_isa_softc *) lsc;
    210 
    211 	return atppc_isadma_finish(sc->sc_ic, sc->sc_drq);
    212 }
    213 
    214 /* Abort DMA operation over ISA bus */
    215 static int
    216 atppc_isa_dma_abort(struct atppc_softc * lsc)
    217 {
    218 	struct atppc_isa_softc * sc = (struct atppc_isa_softc *) lsc;
    219 
    220 	return atppc_isadma_abort(sc->sc_ic, sc->sc_drq);
    221 }
    222 
    223 /* Allocate memory for DMA over ISA bus */
    224 static int
    225 atppc_isa_dma_malloc(struct device * dev, caddr_t * buf, bus_addr_t * bus_addr,
    226 	bus_size_t size)
    227 {
    228 	struct atppc_isa_softc * sc = (struct atppc_isa_softc *) dev;
    229 
    230 	return atppc_isadma_malloc(sc->sc_ic, sc->sc_drq, buf, bus_addr, size);
    231 }
    232 
    233 /* Free memory allocated by atppc_isa_dma_malloc() */
    234 static void
    235 atppc_isa_dma_free(struct device * dev, caddr_t * buf, bus_addr_t * bus_addr,
    236 	bus_size_t size)
    237 {
    238 	struct atppc_isa_softc * sc = (struct atppc_isa_softc *) dev;
    239 
    240 	return atppc_isadma_free(sc->sc_ic, sc->sc_drq, buf, bus_addr, size);
    241 }
    242