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