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