1 /* $NetBSD: atppc_acpi.c,v 1.17 2010/03/05 14:00:17 jruoho 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_acpi.c,v 1.17 2010/03/05 14:00:17 jruoho Exp $"); 34 35 #include "opt_atppc.h" 36 37 #include <sys/param.h> 38 #include <sys/device.h> 39 #include <sys/systm.h> 40 #include <sys/termios.h> 41 42 #include <dev/acpi/acpivar.h> 43 44 #include <dev/ic/atppcvar.h> 45 46 #include <dev/isa/isadmavar.h> 47 #include <dev/isa/atppc_isadma.h> 48 49 static int atppc_acpi_match(device_t, cfdata_t, void *); 50 static void atppc_acpi_attach(device_t, device_t, void *); 51 52 struct atppc_acpi_softc { 53 struct atppc_softc sc_atppc; 54 55 isa_chipset_tag_t sc_ic; 56 int sc_drq; 57 }; 58 59 CFATTACH_DECL_NEW(atppc_acpi, sizeof(struct atppc_acpi_softc), atppc_acpi_match, 60 atppc_acpi_attach, NULL, NULL); 61 62 /* 63 * Supported device IDs 64 */ 65 66 static const char * const atppc_acpi_ids[] = { 67 "PNP04??", /* Standard AT printer port */ 68 NULL 69 }; 70 71 static int atppc_acpi_dma_start(struct atppc_softc *, void *, u_int, 72 u_int8_t); 73 static int atppc_acpi_dma_finish(struct atppc_softc *); 74 static int atppc_acpi_dma_abort(struct atppc_softc *); 75 static int atppc_acpi_dma_malloc(device_t, void **, bus_addr_t *, 76 bus_size_t); 77 static void atppc_acpi_dma_free(device_t, void **, bus_addr_t *, 78 bus_size_t); 79 /* 80 * atppc_acpi_match: autoconf(9) match routine 81 */ 82 static int 83 atppc_acpi_match(device_t parent, cfdata_t match, void *aux) 84 { 85 struct acpi_attach_args *aa = aux; 86 87 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 88 return 0; 89 90 return acpi_match_hid(aa->aa_node->ad_devinfo, atppc_acpi_ids); 91 } 92 93 static void 94 atppc_acpi_attach(device_t parent, device_t self, void *aux) 95 { 96 struct atppc_softc *sc = device_private(self); 97 struct atppc_acpi_softc *asc = device_private(self); 98 struct acpi_attach_args *aa = aux; 99 struct acpi_resources res; 100 struct acpi_io *io; 101 struct acpi_irq *irq; 102 struct acpi_drq *drq; 103 ACPI_STATUS rv; 104 int nirq; 105 106 sc->sc_dev_ok = ATPPC_NOATTACH; 107 108 sc->sc_dev = self; 109 110 /* parse resources */ 111 rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS", 112 &res, &acpi_resource_parse_ops_default); 113 if (ACPI_FAILURE(rv)) 114 return; 115 116 /* find our i/o registers */ 117 io = acpi_res_io(&res, 0); 118 if (io == NULL) { 119 aprint_error_dev(sc->sc_dev, "unable to find i/o register resource\n"); 120 goto out; 121 } 122 123 /* find our IRQ */ 124 irq = acpi_res_irq(&res, 0); 125 if (irq == NULL) { 126 aprint_error_dev(sc->sc_dev, "unable to find irq resource\n"); 127 goto out; 128 } 129 nirq = irq->ar_irq; 130 131 /* find our DRQ */ 132 drq = acpi_res_drq(&res, 0); 133 if (drq == NULL) { 134 aprint_error_dev(sc->sc_dev, "unable to find drq resource\n"); 135 goto out; 136 } 137 asc->sc_drq = drq->ar_drq; 138 139 /* Attach */ 140 sc->sc_iot = aa->aa_iot; 141 sc->sc_has = 0; 142 asc->sc_ic = aa->aa_ic; 143 144 sc->sc_dev_ok = ATPPC_ATTACHED; 145 146 if (bus_space_map(sc->sc_iot, io->ar_base, io->ar_length, 0, 147 &sc->sc_ioh) != 0) { 148 aprint_error_dev(self, "attempt to map bus space failed, device not " 149 "properly attached.\n"); 150 goto out; 151 } 152 153 sc->sc_ieh = isa_intr_establish(aa->aa_ic, nirq, 154 (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL, 155 IPL_TTY, atppcintr, sc->sc_dev); 156 157 /* setup DMA hooks */ 158 if (atppc_isadma_setup(sc, asc->sc_ic, asc->sc_drq) == 0) { 159 sc->sc_has |= ATPPC_HAS_DMA; 160 sc->sc_dma_start = atppc_acpi_dma_start; 161 sc->sc_dma_finish = atppc_acpi_dma_finish; 162 sc->sc_dma_abort = atppc_acpi_dma_abort; 163 sc->sc_dma_malloc = atppc_acpi_dma_malloc; 164 sc->sc_dma_free = atppc_acpi_dma_free; 165 } 166 167 sc->sc_has |= ATPPC_HAS_INTR; 168 169 /* Run soft configuration attach */ 170 atppc_sc_attach(sc); 171 out: 172 acpi_resource_cleanup(&res); 173 } 174 175 /* Start DMA operation over ISA bus */ 176 static int 177 atppc_acpi_dma_start(struct atppc_softc *lsc, void *buf, u_int nbytes, 178 u_int8_t mode) 179 { 180 struct atppc_acpi_softc * sc = (struct atppc_acpi_softc *) lsc; 181 182 return atppc_isadma_start(sc->sc_ic, sc->sc_drq, buf, nbytes, mode); 183 } 184 185 /* Stop DMA operation over ISA bus */ 186 static int 187 atppc_acpi_dma_finish(struct atppc_softc * lsc) 188 { 189 struct atppc_acpi_softc * sc = (struct atppc_acpi_softc *) lsc; 190 191 return atppc_isadma_finish(sc->sc_ic, sc->sc_drq); 192 } 193 194 /* Abort DMA operation over ISA bus */ 195 int 196 atppc_acpi_dma_abort(struct atppc_softc * lsc) 197 { 198 struct atppc_acpi_softc * sc = (struct atppc_acpi_softc *) lsc; 199 200 return atppc_isadma_abort(sc->sc_ic, sc->sc_drq); 201 } 202 203 /* Allocate memory for DMA over ISA bus */ 204 static int 205 atppc_acpi_dma_malloc(device_t dev, void ** buf, bus_addr_t * bus_addr, 206 bus_size_t size) 207 { 208 struct atppc_acpi_softc * sc = device_private(dev); 209 210 return atppc_isadma_malloc(sc->sc_ic, sc->sc_drq, buf, bus_addr, size); 211 } 212 213 /* Free memory allocated by atppc_isa_dma_malloc() */ 214 static void 215 atppc_acpi_dma_free(device_t dev, void ** buf, bus_addr_t * bus_addr, 216 bus_size_t size) 217 { 218 struct atppc_acpi_softc * sc = device_private(dev); 219 220 return atppc_isadma_free(sc->sc_ic, sc->sc_drq, buf, bus_addr, size); 221 } 222