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