1 1.17 thorpej /* $NetBSD: atppc_isa.c,v 1.17 2022/09/25 17:09:36 thorpej Exp $ */ 2 1.2 bjh21 3 1.1 jdolecek /*- 4 1.1 jdolecek * Copyright (c) 2001 Alcove - Nicolas Souchu 5 1.1 jdolecek * All rights reserved. 6 1.1 jdolecek * 7 1.1 jdolecek * Redistribution and use in source and binary forms, with or without 8 1.1 jdolecek * modification, are permitted provided that the following conditions 9 1.1 jdolecek * are met: 10 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright 11 1.1 jdolecek * notice, this list of conditions and the following disclaimer. 12 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the 14 1.1 jdolecek * documentation and/or other materials provided with the distribution. 15 1.1 jdolecek * 16 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 1.1 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 1.1 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 jdolecek * SUCH DAMAGE. 27 1.1 jdolecek * 28 1.3 bjh21 * FreeBSD: src/sys/isa/ppc.c,v 1.26.2.5 2001/10/02 05:21:45 nsouch Exp 29 1.1 jdolecek * 30 1.1 jdolecek */ 31 1.1 jdolecek 32 1.3 bjh21 #include <sys/cdefs.h> 33 1.17 thorpej __KERNEL_RCSID(0, "$NetBSD: atppc_isa.c,v 1.17 2022/09/25 17:09:36 thorpej Exp $"); 34 1.3 bjh21 35 1.1 jdolecek #include "opt_atppc.h" 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/kernel.h> 40 1.1 jdolecek #include <sys/device.h> 41 1.1 jdolecek 42 1.11 ad #include <sys/intr.h> 43 1.11 ad #include <sys/bus.h> 44 1.1 jdolecek 45 1.1 jdolecek #include <dev/isa/isareg.h> 46 1.1 jdolecek #include <dev/isa/isavar.h> 47 1.1 jdolecek #include <dev/isa/isadmavar.h> 48 1.1 jdolecek 49 1.1 jdolecek #include <dev/ic/atppcreg.h> 50 1.1 jdolecek #include <dev/ic/atppcvar.h> 51 1.4 jdolecek #include <dev/isa/atppc_isadma.h> 52 1.1 jdolecek 53 1.1 jdolecek /* 54 1.1 jdolecek * ISA bus attach code for atppc driver. 55 1.16 andvar * Note on capabilities: capabilities may exist in the chipset but may not 56 1.5 drochner * necessarily be useable. I.e. you may specify an IRQ in the autoconfig, but 57 1.1 jdolecek * will the port actually have an IRQ assigned to it at the hardware level? 58 1.16 andvar * How can you test if the capabilities can be used? For interrupts, see if a 59 1.5 drochner * handler exists (sc_intr != NULL). For DMA, see if the sc_dma_start() and 60 1.1 jdolecek * sc_dma_finish() function pointers are not NULL. 61 1.1 jdolecek */ 62 1.1 jdolecek 63 1.4 jdolecek /* Configuration data for atppc on isa bus. */ 64 1.4 jdolecek struct atppc_isa_softc { 65 1.4 jdolecek /* Machine independent device data */ 66 1.4 jdolecek struct atppc_softc sc_atppc; 67 1.4 jdolecek 68 1.4 jdolecek /* IRQ/DRQ/IO Port assignments on ISA bus */ 69 1.4 jdolecek int sc_irq; 70 1.4 jdolecek int sc_drq; 71 1.4 jdolecek int sc_iobase; 72 1.4 jdolecek 73 1.4 jdolecek /* ISA chipset tag */ 74 1.4 jdolecek isa_chipset_tag_t sc_ic; 75 1.4 jdolecek }; 76 1.4 jdolecek 77 1.4 jdolecek /* Probe and attach functions for a atppc device on the ISA bus. */ 78 1.14 cegger static int atppc_isa_probe(device_t, cfdata_t, void *); 79 1.14 cegger static void atppc_isa_attach(device_t, device_t, void *); 80 1.4 jdolecek 81 1.5 drochner static int atppc_isa_dma_start(struct atppc_softc *, void *, u_int, 82 1.4 jdolecek u_int8_t); 83 1.4 jdolecek static int atppc_isa_dma_finish(struct atppc_softc *); 84 1.4 jdolecek static int atppc_isa_dma_abort(struct atppc_softc *); 85 1.14 cegger static int atppc_isa_dma_malloc(device_t, void **, bus_addr_t *, 86 1.4 jdolecek bus_size_t); 87 1.14 cegger static void atppc_isa_dma_free(device_t, void **, bus_addr_t *, 88 1.4 jdolecek bus_size_t); 89 1.1 jdolecek 90 1.13 cegger CFATTACH_DECL_NEW(atppc_isa, sizeof(struct atppc_isa_softc), atppc_isa_probe, 91 1.4 jdolecek atppc_isa_attach, NULL, NULL); 92 1.1 jdolecek 93 1.5 drochner /* 94 1.5 drochner * Probe function: find parallel port controller on isa bus. Combined from 95 1.5 drochner * lpt_isa_probe() in lpt.c and atppc_detect_port() from FreeBSD's ppc.c. 96 1.1 jdolecek */ 97 1.1 jdolecek static int 98 1.14 cegger atppc_isa_probe(device_t parent, cfdata_t cf, void *aux) 99 1.1 jdolecek { 100 1.1 jdolecek bus_space_handle_t ioh; 101 1.5 drochner struct isa_attach_args *ia = aux; 102 1.1 jdolecek bus_space_tag_t iot = ia->ia_iot; 103 1.5 drochner int rval = 0; 104 1.1 jdolecek 105 1.4 jdolecek if (ia->ia_nio < 1) 106 1.4 jdolecek return (0); 107 1.1 jdolecek 108 1.4 jdolecek /* Disallow wildcarded i/o address */ 109 1.6 drochner if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT) 110 1.4 jdolecek return (0); 111 1.4 jdolecek 112 1.4 jdolecek if (bus_space_map(iot, ia->ia_io[0].ir_addr, IO_LPTSIZE, 0, &ioh)) 113 1.4 jdolecek return (0); 114 1.4 jdolecek 115 1.5 drochner if (atppc_detect_port(iot, ioh) == 0) 116 1.5 drochner rval = 1; 117 1.4 jdolecek 118 1.5 drochner bus_space_unmap(iot, ioh, IO_LPTSIZE); 119 1.1 jdolecek 120 1.5 drochner if (rval) { 121 1.5 drochner ia->ia_nio = 1; 122 1.5 drochner ia->ia_io[0].ir_size = IO_LPTSIZE; 123 1.5 drochner ia->ia_nirq = 1; 124 1.5 drochner ia->ia_ndrq = 1; 125 1.5 drochner ia->ia_niomem = 0; 126 1.5 drochner } 127 1.5 drochner return (rval); 128 1.1 jdolecek } 129 1.1 jdolecek 130 1.1 jdolecek /* Attach function: attach and configure parallel port controller on isa bus. */ 131 1.5 drochner static void 132 1.14 cegger atppc_isa_attach(device_t parent, device_t self, void *aux) 133 1.1 jdolecek { 134 1.14 cegger struct atppc_isa_softc *sc = device_private(self); 135 1.5 drochner struct atppc_softc *lsc = &sc->sc_atppc; 136 1.5 drochner struct isa_attach_args *ia = aux; 137 1.1 jdolecek 138 1.4 jdolecek printf(": AT Parallel Port\n"); 139 1.4 jdolecek 140 1.1 jdolecek lsc->sc_iot = ia->ia_iot; 141 1.5 drochner lsc->sc_dmat = ia->ia_dmat; 142 1.1 jdolecek lsc->sc_has = 0; 143 1.1 jdolecek sc->sc_ic = ia->ia_ic; 144 1.1 jdolecek sc->sc_iobase = ia->ia_io->ir_addr; 145 1.1 jdolecek 146 1.5 drochner if (bus_space_map(lsc->sc_iot, sc->sc_iobase, IO_LPTSIZE, 0, 147 1.1 jdolecek &lsc->sc_ioh) != 0) { 148 1.12 cegger aprint_error_dev(self, "attempt to map bus space failed, device not " 149 1.12 cegger "properly attached.\n"); 150 1.1 jdolecek lsc->sc_dev_ok = ATPPC_NOATTACH; 151 1.1 jdolecek return; 152 1.1 jdolecek } 153 1.4 jdolecek 154 1.14 cegger lsc->sc_dev = self; 155 1.4 jdolecek lsc->sc_dev_ok = ATPPC_ATTACHED; 156 1.1 jdolecek 157 1.1 jdolecek /* Assign interrupt handler */ 158 1.9 thorpej if (!(device_cfdata(self)->cf_flags & ATPPC_FLAG_DISABLE_INTR) 159 1.6 drochner && ia->ia_irq->ir_irq != ISA_UNKNOWN_IRQ 160 1.4 jdolecek && ia->ia_nirq >= 1) { 161 1.4 jdolecek sc->sc_irq = ia->ia_irq[0].ir_irq; 162 1.4 jdolecek } else 163 1.4 jdolecek sc->sc_irq = -1; 164 1.4 jdolecek 165 1.4 jdolecek if (sc->sc_irq > 0) { 166 1.4 jdolecek /* Establish interrupt handler. */ 167 1.4 jdolecek lsc->sc_ieh = isa_intr_establish(sc->sc_ic, sc->sc_irq, 168 1.15 jakllsch IST_EDGE, IPL_TTY, atppcintr, lsc->sc_dev); 169 1.4 jdolecek 170 1.4 jdolecek lsc->sc_has |= ATPPC_HAS_INTR; 171 1.1 jdolecek } 172 1.1 jdolecek 173 1.1 jdolecek /* Configure DMA */ 174 1.9 thorpej if (!(device_cfdata(self)->cf_flags & ATPPC_FLAG_DISABLE_DMA) 175 1.6 drochner && ia->ia_drq->ir_drq != ISA_UNKNOWN_DRQ 176 1.4 jdolecek && ia->ia_ndrq >= 1) 177 1.4 jdolecek sc->sc_drq = ia->ia_drq[0].ir_drq; 178 1.4 jdolecek else 179 1.4 jdolecek sc->sc_drq = -1; 180 1.4 jdolecek 181 1.4 jdolecek if (sc->sc_drq != -1 182 1.4 jdolecek && atppc_isadma_setup(lsc, sc->sc_ic, sc->sc_drq) == 0) { 183 1.4 jdolecek lsc->sc_has |= ATPPC_HAS_DMA; 184 1.4 jdolecek 185 1.4 jdolecek /* setup DMA hooks */ 186 1.4 jdolecek lsc->sc_dma_start = atppc_isa_dma_start; 187 1.4 jdolecek lsc->sc_dma_finish = atppc_isa_dma_finish; 188 1.4 jdolecek lsc->sc_dma_abort = atppc_isa_dma_abort; 189 1.4 jdolecek lsc->sc_dma_malloc = atppc_isa_dma_malloc; 190 1.4 jdolecek lsc->sc_dma_free = atppc_isa_dma_free; 191 1.1 jdolecek } 192 1.1 jdolecek 193 1.1 jdolecek /* Run soft configuration attach */ 194 1.1 jdolecek atppc_sc_attach(lsc); 195 1.1 jdolecek 196 1.1 jdolecek return; 197 1.1 jdolecek } 198 1.1 jdolecek 199 1.4 jdolecek /* Start DMA operation over ISA bus */ 200 1.5 drochner static int 201 1.4 jdolecek atppc_isa_dma_start(struct atppc_softc *lsc, void *buf, u_int nbytes, 202 1.4 jdolecek u_int8_t mode) 203 1.4 jdolecek { 204 1.14 cegger struct atppc_isa_softc *sc = (struct atppc_isa_softc *) lsc; 205 1.5 drochner 206 1.4 jdolecek return atppc_isadma_start(sc->sc_ic, sc->sc_drq, buf, nbytes, mode); 207 1.4 jdolecek } 208 1.4 jdolecek 209 1.4 jdolecek /* Stop DMA operation over ISA bus */ 210 1.5 drochner static int 211 1.5 drochner atppc_isa_dma_finish(struct atppc_softc *lsc) 212 1.1 jdolecek { 213 1.14 cegger struct atppc_isa_softc *sc = (struct atppc_isa_softc *) lsc; 214 1.5 drochner 215 1.4 jdolecek return atppc_isadma_finish(sc->sc_ic, sc->sc_drq); 216 1.4 jdolecek } 217 1.1 jdolecek 218 1.4 jdolecek /* Abort DMA operation over ISA bus */ 219 1.5 drochner static int 220 1.5 drochner atppc_isa_dma_abort(struct atppc_softc *lsc) 221 1.4 jdolecek { 222 1.14 cegger struct atppc_isa_softc *sc = (struct atppc_isa_softc *) lsc; 223 1.5 drochner 224 1.4 jdolecek return atppc_isadma_abort(sc->sc_ic, sc->sc_drq); 225 1.4 jdolecek } 226 1.1 jdolecek 227 1.5 drochner /* Allocate memory for DMA over ISA bus */ 228 1.4 jdolecek static int 229 1.14 cegger atppc_isa_dma_malloc(device_t dev, void **buf, bus_addr_t *bus_addr, 230 1.4 jdolecek bus_size_t size) 231 1.4 jdolecek { 232 1.14 cegger struct atppc_isa_softc *sc = device_private(dev); 233 1.1 jdolecek 234 1.4 jdolecek return atppc_isadma_malloc(sc->sc_ic, sc->sc_drq, buf, bus_addr, size); 235 1.4 jdolecek } 236 1.1 jdolecek 237 1.5 drochner /* Free memory allocated by atppc_isa_dma_malloc() */ 238 1.5 drochner static void 239 1.14 cegger atppc_isa_dma_free(device_t dev, void **buf, bus_addr_t *bus_addr, 240 1.4 jdolecek bus_size_t size) 241 1.4 jdolecek { 242 1.14 cegger struct atppc_isa_softc *sc = device_private(dev); 243 1.1 jdolecek 244 1.4 jdolecek return atppc_isadma_free(sc->sc_ic, sc->sc_drq, buf, bus_addr, size); 245 1.1 jdolecek } 246