xref: /netbsd-src/sys/dev/acpi/atppc_acpi.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: atppc_acpi.c,v 1.10 2007/10/19 11:59:35 ad 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: atppc_acpi.c,v 1.10 2007/10/19 11:59:35 ad Exp $");
41 
42 #include "opt_atppc.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/ioctl.h>
48 #include <sys/syslog.h>
49 #include <sys/device.h>
50 #include <sys/proc.h>
51 #include <sys/termios.h>
52 
53 #include <sys/bus.h>
54 
55 #include <dev/isa/isavar.h>
56 #include <dev/isa/isadmavar.h>
57 
58 #include <dev/acpi/acpica.h>
59 #include <dev/acpi/acpireg.h>
60 #include <dev/acpi/acpivar.h>
61 
62 #include <dev/ic/atppcvar.h>
63 #include <dev/isa/atppc_isadma.h>
64 
65 static int	atppc_acpi_match(struct device *, struct cfdata *, void *);
66 static void	atppc_acpi_attach(struct device *, struct device *, void *);
67 
68 struct atppc_acpi_softc {
69 	struct atppc_softc sc_atppc;
70 
71 	isa_chipset_tag_t sc_ic;
72 	int sc_drq;
73 };
74 
75 CFATTACH_DECL(atppc_acpi, sizeof(struct atppc_acpi_softc), atppc_acpi_match,
76     atppc_acpi_attach, NULL, NULL);
77 
78 /*
79  * Supported device IDs
80  */
81 
82 static const char * const atppc_acpi_ids[] = {
83 	"PNP04??",	/* Standard AT printer port */
84 	NULL
85 };
86 
87 static int atppc_acpi_dma_start(struct atppc_softc *, void *, u_int,
88 	u_int8_t);
89 static int atppc_acpi_dma_finish(struct atppc_softc *);
90 static int atppc_acpi_dma_abort(struct atppc_softc *);
91 static int atppc_acpi_dma_malloc(struct device *, void **, bus_addr_t *,
92 	bus_size_t);
93 static void atppc_acpi_dma_free(struct device *, void **, bus_addr_t *,
94 	bus_size_t);
95 /*
96  * atppc_acpi_match: autoconf(9) match routine
97  */
98 static int
99 atppc_acpi_match(struct device *parent, struct cfdata *match, void *aux)
100 {
101 	struct acpi_attach_args *aa = aux;
102 
103 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
104 		return 0;
105 
106 	return acpi_match_hid(aa->aa_node->ad_devinfo, atppc_acpi_ids);
107 }
108 
109 static void
110 atppc_acpi_attach(struct device *parent, struct device *self, void *aux)
111 {
112 	struct atppc_softc *sc = (struct atppc_softc *) self;
113 	struct atppc_acpi_softc *asc = (struct atppc_acpi_softc *)self;
114 	struct acpi_attach_args *aa = aux;
115 	struct acpi_resources res;
116 	struct acpi_io *io;
117 	struct acpi_irq *irq;
118 	struct acpi_drq *drq;
119 	ACPI_STATUS rv;
120 	int nirq;
121 
122 	sc->sc_dev_ok = ATPPC_NOATTACH;
123 
124 	aprint_naive(": AT Parallel Port\n");
125 	aprint_normal(": AT Parallel Port\n");
126 
127 	/* parse resources */
128 	rv = acpi_resource_parse(&sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
129 				 &res, &acpi_resource_parse_ops_default);
130 	if (ACPI_FAILURE(rv))
131 		return;
132 
133 	/* find our i/o registers */
134 	io = acpi_res_io(&res, 0);
135 	if (io == NULL) {
136 		aprint_error("%s: unable to find i/o register resource\n",
137 		    sc->sc_dev.dv_xname);
138 		goto out;
139 	}
140 
141 	/* find our IRQ */
142 	irq = acpi_res_irq(&res, 0);
143 	if (irq == NULL) {
144 		aprint_error("%s: unable to find irq resource\n",
145 		    sc->sc_dev.dv_xname);
146 		goto out;
147 	}
148 	nirq = irq->ar_irq;
149 
150 	/* find our DRQ */
151 	drq = acpi_res_drq(&res, 0);
152 	if (drq == NULL) {
153 		aprint_error("%s: unable to find drq resource\n",
154 		    sc->sc_dev.dv_xname);
155 		goto out;
156 	}
157 	asc->sc_drq = drq->ar_drq;
158 
159 	/* Attach */
160 	sc->sc_iot = aa->aa_iot;
161 	sc->sc_has = 0;
162 	asc->sc_ic = aa->aa_ic;
163 
164 	sc->sc_dev_ok = ATPPC_ATTACHED;
165 
166 	if (bus_space_map(sc->sc_iot, io->ar_base, io->ar_length, 0,
167 		&sc->sc_ioh) != 0) {
168 		aprint_error("%s: attempt to map bus space failed, device not "
169 			"properly attached.\n", self->dv_xname);
170 		goto out;
171 	}
172 
173 	sc->sc_ieh = isa_intr_establish(aa->aa_ic, nirq,
174 	    (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL,
175 	    IPL_TTY, atppcintr, sc);
176 
177 	/* setup DMA hooks */
178 	if (atppc_isadma_setup(sc, asc->sc_ic, asc->sc_drq) == 0) {
179 		sc->sc_has |= ATPPC_HAS_DMA;
180 		sc->sc_dma_start = atppc_acpi_dma_start;
181 		sc->sc_dma_finish = atppc_acpi_dma_finish;
182 		sc->sc_dma_abort = atppc_acpi_dma_abort;
183 		sc->sc_dma_malloc = atppc_acpi_dma_malloc;
184 		sc->sc_dma_free = atppc_acpi_dma_free;
185 	}
186 
187 	sc->sc_has |= ATPPC_HAS_INTR;
188 
189 	/* Run soft configuration attach */
190 	atppc_sc_attach(sc);
191  out:
192 	acpi_resource_cleanup(&res);
193 }
194 
195 /* Start DMA operation over ISA bus */
196 static int
197 atppc_acpi_dma_start(struct atppc_softc *lsc, void *buf, u_int nbytes,
198 	u_int8_t mode)
199 {
200 	struct atppc_acpi_softc * sc = (struct atppc_acpi_softc *) lsc;
201 
202 	return atppc_isadma_start(sc->sc_ic, sc->sc_drq, buf, nbytes, mode);
203 }
204 
205 /* Stop DMA operation over ISA bus */
206 static int
207 atppc_acpi_dma_finish(struct atppc_softc * lsc)
208 {
209 	struct atppc_acpi_softc * sc = (struct atppc_acpi_softc *) lsc;
210 
211 	return atppc_isadma_finish(sc->sc_ic, sc->sc_drq);
212 }
213 
214 /* Abort DMA operation over ISA bus */
215 int
216 atppc_acpi_dma_abort(struct atppc_softc * lsc)
217 {
218 	struct atppc_acpi_softc * sc = (struct atppc_acpi_softc *) lsc;
219 
220 	return atppc_isadma_abort(sc->sc_ic, sc->sc_drq);
221 }
222 
223 /* Allocate memory for DMA over ISA bus */
224 static int
225 atppc_acpi_dma_malloc(struct device * dev, void ** buf, bus_addr_t * bus_addr,
226 	bus_size_t size)
227 {
228 	struct atppc_acpi_softc * sc = (struct atppc_acpi_softc *) dev;
229 
230 	return atppc_isadma_malloc(sc->sc_ic, sc->sc_drq, buf, bus_addr, size);
231 }
232 
233 /* Free memory allocated by atppc_isa_dma_malloc() */
234 static void
235 atppc_acpi_dma_free(struct device * dev, void ** buf, bus_addr_t * bus_addr,
236 	bus_size_t size)
237 {
238 	struct atppc_acpi_softc * sc = (struct atppc_acpi_softc *) dev;
239 
240 	return atppc_isadma_free(sc->sc_ic, sc->sc_drq, buf, bus_addr, size);
241 }
242