Home | History | Annotate | Line # | Download | only in pci
atppc_puc.c revision 1.9
      1  1.9    cegger /* $NetBSD: atppc_puc.c,v 1.9 2008/04/16 09:39:01 cegger Exp $ */
      2  1.1  jdolecek 
      3  1.1  jdolecek /*-
      4  1.1  jdolecek  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5  1.1  jdolecek  * All rights reserved.
      6  1.1  jdolecek  *
      7  1.1  jdolecek  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  jdolecek  * by Jaromir Dolecek.
      9  1.1  jdolecek  *
     10  1.1  jdolecek  * Redistribution and use in source and binary forms, with or without
     11  1.1  jdolecek  * modification, are permitted provided that the following conditions
     12  1.1  jdolecek  * are met:
     13  1.1  jdolecek  * 1. Redistributions of source code must retain the above copyright
     14  1.1  jdolecek  *    notice, this list of conditions and the following disclaimer.
     15  1.1  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  jdolecek  *    documentation and/or other materials provided with the distribution.
     18  1.1  jdolecek  * 3. All advertising materials mentioning features or use of this software
     19  1.1  jdolecek  *    must display the following acknowledgement:
     20  1.1  jdolecek  *        This product includes software developed by the NetBSD
     21  1.1  jdolecek  *        Foundation, Inc. and its contributors.
     22  1.1  jdolecek  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  jdolecek  *    contributors may be used to endorse or promote products derived
     24  1.1  jdolecek  *    from this software without specific prior written permission.
     25  1.1  jdolecek  *
     26  1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  jdolecek  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  jdolecek  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  jdolecek  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  jdolecek  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  jdolecek  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  jdolecek  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  jdolecek  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  jdolecek  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  jdolecek  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  jdolecek  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  jdolecek  */
     38  1.1  jdolecek 
     39  1.1  jdolecek #include "opt_atppc.h"
     40  1.1  jdolecek 
     41  1.1  jdolecek #include <sys/cdefs.h>
     42  1.9    cegger __KERNEL_RCSID(0, "$NetBSD: atppc_puc.c,v 1.9 2008/04/16 09:39:01 cegger Exp $");
     43  1.1  jdolecek 
     44  1.1  jdolecek #include <sys/param.h>
     45  1.1  jdolecek #include <sys/systm.h>
     46  1.1  jdolecek #include <sys/errno.h>
     47  1.1  jdolecek #include <sys/ioctl.h>
     48  1.1  jdolecek #include <sys/syslog.h>
     49  1.1  jdolecek #include <sys/device.h>
     50  1.1  jdolecek #include <sys/proc.h>
     51  1.1  jdolecek #include <sys/termios.h>
     52  1.1  jdolecek 
     53  1.6        ad #include <sys/bus.h>
     54  1.2  jdolecek #include <uvm/uvm_extern.h>
     55  1.1  jdolecek 
     56  1.1  jdolecek #include <dev/pci/pcivar.h>
     57  1.1  jdolecek #include <dev/pci/pucvar.h>
     58  1.1  jdolecek 
     59  1.1  jdolecek #include <dev/ic/atppcvar.h>
     60  1.1  jdolecek 
     61  1.9    cegger static int	atppc_puc_match(device_t, cfdata_t, void *);
     62  1.9    cegger static void	atppc_puc_attach(device_t, device_t, void *);
     63  1.1  jdolecek 
     64  1.2  jdolecek struct atppc_puc_softc {
     65  1.2  jdolecek 	/* Machine independent device data */
     66  1.2  jdolecek 	struct atppc_softc sc_atppc;
     67  1.2  jdolecek 
     68  1.2  jdolecek 	bus_dmamap_t sc_dmamap;
     69  1.2  jdolecek };
     70  1.3     perry 
     71  1.8    cegger CFATTACH_DECL_NEW(atppc_puc, sizeof(struct atppc_puc_softc), atppc_puc_match,
     72  1.1  jdolecek     atppc_puc_attach, NULL, NULL);
     73  1.1  jdolecek 
     74  1.2  jdolecek static int atppc_puc_dma_setup(struct atppc_puc_softc *);
     75  1.3     perry static int atppc_puc_dma_start(struct atppc_softc *, void *, u_int,
     76  1.1  jdolecek 	u_int8_t);
     77  1.1  jdolecek static int atppc_puc_dma_finish(struct atppc_softc *);
     78  1.1  jdolecek static int atppc_puc_dma_abort(struct atppc_softc *);
     79  1.9    cegger static int atppc_puc_dma_malloc(device_t, void **, bus_addr_t *,
     80  1.1  jdolecek 	bus_size_t);
     81  1.9    cegger static void atppc_puc_dma_free(device_t, void **, bus_addr_t *,
     82  1.1  jdolecek 	bus_size_t);
     83  1.1  jdolecek 
     84  1.1  jdolecek /*
     85  1.1  jdolecek  * atppc_acpi_match: autoconf(9) match routine
     86  1.1  jdolecek  */
     87  1.1  jdolecek static int
     88  1.9    cegger atppc_puc_match(device_t parent, cfdata_t match, void *aux)
     89  1.1  jdolecek {
     90  1.1  jdolecek 	struct puc_attach_args *aa = aux;
     91  1.1  jdolecek 
     92  1.1  jdolecek 	/*
     93  1.1  jdolecek 	 * Locators already matched, just check the type.
     94  1.1  jdolecek 	 */
     95  1.1  jdolecek 	if (aa->type != PUC_PORT_TYPE_LPT)
     96  1.1  jdolecek 		return (0);
     97  1.1  jdolecek 
     98  1.1  jdolecek 	return (1);
     99  1.1  jdolecek }
    100  1.1  jdolecek 
    101  1.1  jdolecek static void
    102  1.9    cegger atppc_puc_attach(device_t parent, device_t self, void *aux)
    103  1.1  jdolecek {
    104  1.9    cegger 	struct atppc_softc *sc = device_private(self);
    105  1.9    cegger 	struct atppc_puc_softc *psc = device_private(self);
    106  1.1  jdolecek 	struct puc_attach_args *aa = aux;
    107  1.1  jdolecek 	const char *intrstr;
    108  1.1  jdolecek 
    109  1.1  jdolecek 	sc->sc_dev_ok = ATPPC_NOATTACH;
    110  1.1  jdolecek 
    111  1.1  jdolecek 	printf(": AT Parallel Port\n");
    112  1.1  jdolecek 
    113  1.1  jdolecek 	/* Attach */
    114  1.9    cegger 	sc->sc_dev = self;
    115  1.1  jdolecek 	sc->sc_iot = aa->t;
    116  1.1  jdolecek 	sc->sc_ioh = aa->h;
    117  1.1  jdolecek 	sc->sc_dmat = aa->dmat;
    118  1.1  jdolecek 	sc->sc_has = 0;
    119  1.1  jdolecek 
    120  1.1  jdolecek 	intrstr = pci_intr_string(aa->pc, aa->intrhandle);
    121  1.1  jdolecek 	sc->sc_ieh = pci_intr_establish(aa->pc, aa->intrhandle, IPL_TTY,
    122  1.1  jdolecek 	    atppcintr, sc);
    123  1.1  jdolecek 	if (sc->sc_ieh == NULL) {
    124  1.8    cegger 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
    125  1.1  jdolecek 		if (intrstr != NULL)
    126  1.1  jdolecek 			printf(" at %s", intrstr);
    127  1.1  jdolecek 		printf("\n");
    128  1.1  jdolecek 		return;
    129  1.1  jdolecek 	}
    130  1.8    cegger 	printf("%s: interrupting at %s\n", device_xname(sc->sc_dev), intrstr);
    131  1.1  jdolecek 	sc->sc_has |= ATPPC_HAS_INTR;
    132  1.1  jdolecek 
    133  1.1  jdolecek 	/* setup DMA hooks */
    134  1.2  jdolecek 	if (atppc_puc_dma_setup(psc) == 0) {
    135  1.2  jdolecek 		sc->sc_has |= ATPPC_HAS_DMA;
    136  1.2  jdolecek 		sc->sc_dma_start = atppc_puc_dma_start;
    137  1.2  jdolecek 		sc->sc_dma_finish = atppc_puc_dma_finish;
    138  1.2  jdolecek 		sc->sc_dma_abort = atppc_puc_dma_abort;
    139  1.2  jdolecek 		sc->sc_dma_malloc = atppc_puc_dma_malloc;
    140  1.2  jdolecek 		sc->sc_dma_free = atppc_puc_dma_free;
    141  1.2  jdolecek 	}
    142  1.1  jdolecek 
    143  1.1  jdolecek 	/* Finished attach */
    144  1.1  jdolecek 	sc->sc_dev_ok = ATPPC_ATTACHED;
    145  1.1  jdolecek 
    146  1.1  jdolecek 	/* Run soft configuration attach */
    147  1.1  jdolecek 	atppc_sc_attach(sc);
    148  1.1  jdolecek }
    149  1.1  jdolecek 
    150  1.2  jdolecek /* Setup DMA structures */
    151  1.2  jdolecek static int
    152  1.2  jdolecek atppc_puc_dma_setup(struct atppc_puc_softc *psc)
    153  1.2  jdolecek {
    154  1.2  jdolecek 	return EOPNOTSUPP;	/* XXX DMA not tested yet */
    155  1.2  jdolecek #if 0
    156  1.2  jdolecek 	struct atppc_softc *sc = (struct atppc_softc *)psc;
    157  1.2  jdolecek 	int error;
    158  1.2  jdolecek 
    159  1.2  jdolecek #define BUFSIZE	PAGE_SIZE	/* XXX see lptvar.h */
    160  1.2  jdolecek 	if ((error = bus_dmamap_create(sc->sc_dmat, BUFSIZE, 1, BUFSIZE, 0,
    161  1.2  jdolecek 	    BUS_DMA_NOWAIT, &psc->sc_dmamap)))
    162  1.2  jdolecek 		return error;
    163  1.3     perry 
    164  1.2  jdolecek 	return (0);
    165  1.2  jdolecek #endif
    166  1.2  jdolecek }
    167  1.2  jdolecek 
    168  1.1  jdolecek /* Start DMA operation over PCI bus */
    169  1.3     perry static int
    170  1.2  jdolecek atppc_puc_dma_start(struct atppc_softc *dev, void *buf, u_int nbytes,
    171  1.1  jdolecek 	u_int8_t mode)
    172  1.1  jdolecek {
    173  1.2  jdolecek 	struct atppc_puc_softc *psc = (struct atppc_puc_softc *) dev;
    174  1.2  jdolecek 	struct atppc_softc *sc = &psc->sc_atppc;
    175  1.2  jdolecek 
    176  1.2  jdolecek 	bus_dmamap_sync(sc->sc_dmat, psc->sc_dmamap, 0, nbytes,
    177  1.2  jdolecek 	    (mode == ATPPC_DMA_MODE_WRITE) ? BUS_DMASYNC_PREWRITE
    178  1.2  jdolecek 			: BUS_DMASYNC_PREREAD);
    179  1.1  jdolecek 
    180  1.1  jdolecek 	return (0);
    181  1.1  jdolecek }
    182  1.1  jdolecek 
    183  1.1  jdolecek /* Stop DMA operation over PCI bus */
    184  1.3     perry static int
    185  1.2  jdolecek atppc_puc_dma_finish(struct atppc_softc *dev)
    186  1.1  jdolecek {
    187  1.1  jdolecek 
    188  1.2  jdolecek 	struct atppc_puc_softc *psc = (struct atppc_puc_softc *) dev;
    189  1.2  jdolecek 	struct atppc_softc *sc = &psc->sc_atppc;
    190  1.2  jdolecek 
    191  1.2  jdolecek 	/*
    192  1.2  jdolecek 	 * We don't know direction of DMA, so sync both. We can safely
    193  1.2  jdolecek 	 *  assume the dma map is loaded.
    194  1.2  jdolecek 	 */
    195  1.2  jdolecek 	bus_dmamap_sync(sc->sc_dmat, psc->sc_dmamap, 0,
    196  1.2  jdolecek 	    psc->sc_dmamap->dm_segs[0].ds_len,
    197  1.2  jdolecek 	    BUS_DMASYNC_POSTWRITE|BUS_DMASYNC_POSTREAD);
    198  1.2  jdolecek 
    199  1.1  jdolecek 	return (0);
    200  1.1  jdolecek }
    201  1.1  jdolecek 
    202  1.1  jdolecek /* Abort DMA operation over PCI bus */
    203  1.3     perry int
    204  1.1  jdolecek atppc_puc_dma_abort(struct atppc_softc * lsc)
    205  1.1  jdolecek {
    206  1.1  jdolecek 
    207  1.2  jdolecek 	/* Nothing to do - we do not need to sync, op is aborted */
    208  1.1  jdolecek 	return (0);
    209  1.1  jdolecek }
    210  1.1  jdolecek 
    211  1.3     perry /* Allocate memory for DMA over PCI bus */
    212  1.1  jdolecek int
    213  1.9    cegger atppc_puc_dma_malloc(device_t dev, void **buf, bus_addr_t *bus_addr,
    214  1.1  jdolecek 	bus_size_t size)
    215  1.1  jdolecek {
    216  1.9    cegger 	struct atppc_puc_softc *psc = device_private(dev);
    217  1.2  jdolecek 	struct atppc_softc *sc = &psc->sc_atppc;
    218  1.2  jdolecek 	int error;
    219  1.2  jdolecek 
    220  1.2  jdolecek 	error = bus_dmamap_load(sc->sc_dmat, psc->sc_dmamap, *buf, size,
    221  1.2  jdolecek 	    NULL /* kernel address */, BUS_DMA_WAITOK|BUS_DMA_STREAMING);
    222  1.2  jdolecek 	if (error)
    223  1.2  jdolecek 		return (error);
    224  1.1  jdolecek 
    225  1.2  jdolecek 	*bus_addr = psc->sc_dmamap->dm_segs[0].ds_addr;
    226  1.2  jdolecek 	return (0);
    227  1.1  jdolecek }
    228  1.1  jdolecek 
    229  1.3     perry /* Free memory allocated by atppc_isa_dma_malloc() */
    230  1.3     perry void
    231  1.9    cegger atppc_puc_dma_free(device_t dev, void **buf, bus_addr_t *bus_addr,
    232  1.1  jdolecek 	bus_size_t size)
    233  1.1  jdolecek {
    234  1.9    cegger 	struct atppc_puc_softc *psc = device_private(dev);
    235  1.2  jdolecek 	struct atppc_softc *sc = &psc->sc_atppc;
    236  1.1  jdolecek 
    237  1.2  jdolecek 	return (bus_dmamap_unload(sc->sc_dmat, psc->sc_dmamap));
    238  1.1  jdolecek }
    239