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