1/* $NetBSD: atppc_ofisa.c,v 1.9 2008/04/16 09:39:01 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 * 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 <sys/cdefs.h> 33__KERNEL_RCSID(0, "$NetBSD: atppc_ofisa.c,v 1.9 2008/04/16 09:39:01 cegger Exp $"); 34 35#include "opt_atppc.h" 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/ofw/openfirm.h> 49#include <dev/isa/isavar.h> 50#include <dev/ofisa/ofisavar.h> 51 52#include <dev/ic/atppcvar.h> 53#include <dev/isa/atppc_isadma.h> 54 55static int atppc_ofisa_match(device_t, cfdata_t, void *); 56static void atppc_ofisa_attach(device_t, device_t, void *); 57 58struct atppc_ofisa_softc { 59 struct atppc_softc sc_atppc; 60 61 isa_chipset_tag_t sc_ic; 62 int sc_drq; 63}; 64 65CFATTACH_DECL_NEW(atppc_ofisa, sizeof(struct atppc_ofisa_softc), atppc_ofisa_match, 66 atppc_ofisa_attach, NULL, NULL); 67 68static int atppc_ofisa_dma_start(struct atppc_softc *, void *, u_int, 69 u_int8_t); 70static int atppc_ofisa_dma_finish(struct atppc_softc *); 71static int atppc_ofisa_dma_abort(struct atppc_softc *); 72static int atppc_ofisa_dma_malloc(device_t, void **, bus_addr_t *, 73 bus_size_t); 74static void atppc_ofisa_dma_free(device_t, void **, bus_addr_t *, 75 bus_size_t); 76/* 77 * atppc_ofisa_match: autoconf(9) match routine 78 */ 79static int 80atppc_ofisa_match(device_t parent, cfdata_t match, void *aux) 81{ 82 struct ofisa_attach_args *aa = aux; 83 static const char *const compatible_strings[] = { "pnpPNP,401", NULL }; 84 int rv = 0; 85 86 if (of_compatible(aa->oba.oba_phandle, compatible_strings) != -1) 87 rv = 5; 88#ifdef _LPT_OFISA_MD_MATCH 89 if (!rv) 90 rv = lpt_ofisa_md_match(parent, match, aux); 91#endif 92 return (rv); 93} 94 95static void 96atppc_ofisa_attach(device_t parent, device_t self, void *aux) 97{ 98 struct atppc_softc *sc = device_private(self); 99 struct atppc_ofisa_softc *asc = device_private(self); 100 struct ofisa_attach_args *aa = aux; 101 struct ofisa_reg_desc reg; 102 struct ofisa_intr_desc intr; 103 struct ofisa_dma_desc dma; 104 int n; 105 106 sc->sc_dev_ok = ATPPC_NOATTACH; 107 108 printf(": AT Parallel Port\n"); 109 110 /* find our I/O space */ 111 n = ofisa_reg_get(aa->oba.oba_phandle, ®, 1); 112#ifdef _LPT_OFISA_MD_REG_FIXUP 113 n = lpt_ofisa_md_reg_fixup(parent, self, aux, ®, 1, n); 114#endif 115 if (n != 1) { 116 aprint_error_dev(sc->sc_dev, "unable to find i/o register resource\n"); 117 return; 118 } 119 120 /* find our IRQ */ 121 n = ofisa_intr_get(aa->oba.oba_phandle, &intr, 1); 122#ifdef _LPT_OFISA_MD_INTR_FIXUP 123 n = lpt_ofisa_md_intr_fixup(parent, self, aux, &intr, 1, n); 124#endif 125 if (n != 1) { 126 aprint_error_dev(sc->sc_dev, "unable to find irq resource\n"); 127 return; 128 } 129 130 /* find our DRQ */ 131 if (ofisa_dma_get(aa->oba.oba_phandle, &dma, 1) != 1) { 132 aprint_error_dev(sc->sc_dev, "unable to find DMA data\n"); 133 return; 134 } 135 asc->sc_drq = dma.drq; 136 137 /* Attach */ 138 sc->sc_dev = self; 139 sc->sc_iot = (reg.type == OFISA_REG_TYPE_IO) ? aa->iot : aa->memt; 140 sc->sc_has = 0; 141 asc->sc_ic = aa->ic; 142 143 sc->sc_dev_ok = ATPPC_ATTACHED; 144 145 if (bus_space_map(sc->sc_iot, reg.addr, reg.len, 0, 146 &sc->sc_ioh) != 0) { 147 aprint_error_dev(self, "attempt to map bus space failed, device not " 148 "properly attached.\n"); 149 return; 150 } 151 152 sc->sc_ieh = isa_intr_establish(aa->ic, intr.irq, intr.share, 153 IPL_TTY, atppcintr, sc); 154 sc->sc_has |= ATPPC_HAS_INTR; 155 156 /* setup DMA hooks */ 157 if (atppc_isadma_setup(sc, asc->sc_ic, asc->sc_drq) == 0) { 158 sc->sc_has |= ATPPC_HAS_DMA; 159 sc->sc_dma_start = atppc_ofisa_dma_start; 160 sc->sc_dma_finish = atppc_ofisa_dma_finish; 161 sc->sc_dma_abort = atppc_ofisa_dma_abort; 162 sc->sc_dma_malloc = atppc_ofisa_dma_malloc; 163 sc->sc_dma_free = atppc_ofisa_dma_free; 164 } 165 166 /* Run soft configuration attach */ 167 atppc_sc_attach(sc); 168} 169 170/* Start DMA operation over ISA bus */ 171static int 172atppc_ofisa_dma_start(struct atppc_softc *lsc, void *buf, u_int nbytes, 173 u_int8_t mode) 174{ 175 struct atppc_ofisa_softc * sc = (struct atppc_ofisa_softc *) lsc; 176 177 return atppc_isadma_start(sc->sc_ic, sc->sc_drq, buf, nbytes, mode); 178} 179 180/* Stop DMA operation over ISA bus */ 181static int 182atppc_ofisa_dma_finish(struct atppc_softc * lsc) 183{ 184 struct atppc_ofisa_softc * sc = (struct atppc_ofisa_softc *) lsc; 185 186 return atppc_isadma_finish(sc->sc_ic, sc->sc_drq); 187} 188 189/* Abort DMA operation over ISA bus */ 190int 191atppc_ofisa_dma_abort(struct atppc_softc * lsc) 192{ 193 struct atppc_ofisa_softc * sc = (struct atppc_ofisa_softc *) lsc; 194 195 return atppc_isadma_abort(sc->sc_ic, sc->sc_drq); 196} 197 198/* Allocate memory for DMA over ISA bus */ 199int 200atppc_ofisa_dma_malloc(device_t dev, void ** buf, bus_addr_t * bus_addr, 201 bus_size_t size) 202{ 203 struct atppc_ofisa_softc * sc = device_private(dev); 204 205 return atppc_isadma_malloc(sc->sc_ic, sc->sc_drq, buf, bus_addr, size); 206} 207 208/* Free memory allocated by atppc_isa_dma_malloc() */ 209void 210atppc_ofisa_dma_free(device_t dev, void ** buf, bus_addr_t * bus_addr, 211 bus_size_t size) 212{ 213 struct atppc_ofisa_softc * sc = device_private(dev); 214 215 return atppc_isadma_free(sc->sc_ic, sc->sc_drq, buf, bus_addr, size); 216} 217