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