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