sdhc_acpi.c revision 1.2.2.2 1 /* $NetBSD: sdhc_acpi.c,v 1.2.2.2 2016/07/09 20:25:01 skrll Exp $ */
2
3 /*
4 * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) NetBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: sdhc_acpi.c,v 1.2.2.2 2016/07/09 20:25:01 skrll Exp $");
30
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/systm.h>
34 #include <sys/kmem.h>
35
36 #include <dev/acpi/acpireg.h>
37 #include <dev/acpi/acpivar.h>
38
39 #include <dev/sdmmc/sdhcreg.h>
40 #include <dev/sdmmc/sdhcvar.h>
41 #include <dev/sdmmc/sdmmcvar.h>
42
43 #define _COMPONENT ACPI_RESOURCE_COMPONENT
44 ACPI_MODULE_NAME ("sdhc_acpi")
45
46 static int sdhc_acpi_match(device_t, cfdata_t, void *);
47 static void sdhc_acpi_attach(device_t, device_t, void *);
48 static int sdhc_acpi_detach(device_t, int);
49 static bool sdhc_acpi_resume(device_t, const pmf_qual_t *);
50
51 struct sdhc_acpi_softc {
52 struct sdhc_softc sc;
53 int sc_irq;
54
55 ACPI_HANDLE sc_crs, sc_srs;
56 ACPI_BUFFER sc_crs_buffer;
57 };
58
59 CFATTACH_DECL_NEW(sdhc_acpi, sizeof(struct sdhc_acpi_softc),
60 sdhc_acpi_match, sdhc_acpi_attach, sdhc_acpi_detach, NULL);
61
62 static uint32_t sdhc_acpi_intr(void *);
63
64 static const char * const sdhc_acpi_ids[] = {
65 "80860F14",
66 "80860F16",
67 NULL
68 };
69
70 static int
71 sdhc_acpi_match(device_t parent, cfdata_t match, void *opaque)
72 {
73 struct acpi_attach_args *aa = opaque;
74
75 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
76 return 0;
77
78 return acpi_match_hid(aa->aa_node->ad_devinfo, sdhc_acpi_ids);
79 }
80
81 static void
82 sdhc_acpi_attach(device_t parent, device_t self, void *opaque)
83 {
84 struct sdhc_acpi_softc *sc = device_private(self);
85 struct acpi_attach_args *aa = opaque;
86 struct acpi_resources res;
87 struct acpi_mem *mem;
88 struct acpi_irq *irq;
89 bus_space_handle_t memh;
90 ACPI_STATUS rv;
91
92 sc->sc.sc_dev = self;
93 sc->sc.sc_dmat = aa->aa_dmat;
94 sc->sc.sc_host = NULL;
95 sc->sc_irq = -1;
96
97 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
98 &res, &acpi_resource_parse_ops_default);
99 if (ACPI_FAILURE(rv))
100 return;
101
102 AcpiGetHandle(aa->aa_node->ad_handle, "_CRS", &sc->sc_crs);
103 AcpiGetHandle(aa->aa_node->ad_handle, "_SRS", &sc->sc_srs);
104 if (sc->sc_crs && sc->sc_srs) {
105 /* XXX Why need this? */
106 sc->sc_crs_buffer.Pointer = NULL;
107 sc->sc_crs_buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
108 rv = AcpiGetCurrentResources(sc->sc_crs, &sc->sc_crs_buffer);
109 if (ACPI_FAILURE(rv))
110 sc->sc_crs = sc->sc_srs = NULL;
111 }
112
113 mem = acpi_res_mem(&res, 0);
114 irq = acpi_res_irq(&res, 0);
115 if (mem == NULL || irq == NULL) {
116 aprint_error_dev(self, "incomplete resources\n");
117 goto cleanup;
118 }
119
120 if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0,
121 &memh)) {
122 aprint_error_dev(self, "couldn't map registers\n");
123 goto cleanup;
124 }
125
126 /* XXX acpi_intr_establish? */
127 rv = AcpiOsInstallInterruptHandler(irq->ar_irq, sdhc_acpi_intr, sc);
128 if (ACPI_FAILURE(rv)) {
129 aprint_error_dev(self,
130 "couldn't establish interrupt handler\n");
131 goto cleanup;
132 }
133 sc->sc_irq = irq->ar_irq;
134
135 sc->sc.sc_host = kmem_zalloc(sizeof(struct sdhc_host *), KM_NOSLEEP);
136 if (sc->sc.sc_host == NULL) {
137 aprint_error_dev(self, "couldn't alloc memory\n");
138 goto cleanup;
139 }
140
141 if (sdhc_host_found(&sc->sc, aa->aa_memt, memh, mem->ar_length) != 0) {
142 aprint_error_dev(self, "couldn't initialize host\n");
143 goto cleanup;
144 }
145
146 if (!pmf_device_register1(self, sdhc_suspend, sdhc_acpi_resume,
147 sdhc_shutdown)) {
148 aprint_error_dev(self, "couldn't establish powerhook\n");
149 }
150
151 acpi_resource_cleanup(&res);
152 return;
153
154 cleanup:
155 acpi_resource_cleanup(&res);
156 if (sc->sc_crs_buffer.Pointer)
157 ACPI_FREE(sc->sc_crs_buffer.Pointer);
158 if (sc->sc_irq >= 0)
159 /* XXX acpi_intr_disestablish? */
160 AcpiOsRemoveInterruptHandler(sc->sc_irq, sdhc_acpi_intr);
161 if (sc->sc.sc_host != NULL)
162 kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *));
163 }
164
165 static int
166 sdhc_acpi_detach(device_t self, int flags)
167 {
168 struct sdhc_acpi_softc *sc = device_private(self);
169 int rv;
170
171 pmf_device_deregister(self);
172
173 if (sc->sc_crs_buffer.Pointer)
174 ACPI_FREE(sc->sc_crs_buffer.Pointer);
175
176 rv = sdhc_detach(&sc->sc, flags);
177 if (rv)
178 return rv;
179
180 if (sc->sc_irq >= 0)
181 /* XXX acpi_intr_disestablish? */
182 AcpiOsRemoveInterruptHandler(sc->sc_irq, sdhc_acpi_intr);
183
184 if (sc->sc.sc_host != NULL)
185 kmem_free(sc->sc.sc_host, sizeof(struct sdhc_host *));
186
187 return 0;
188 }
189
190 static bool
191 sdhc_acpi_resume(device_t self, const pmf_qual_t *qual)
192 {
193 struct sdhc_acpi_softc *sc = device_private(self);
194 ACPI_STATUS rv;
195
196 if (sc->sc_crs && sc->sc_srs) {
197 rv = AcpiSetCurrentResources(sc->sc_srs, &sc->sc_crs_buffer);
198 if (ACPI_FAILURE(rv))
199 printf("%s: _SRS failed: %s\n",
200 device_xname(self), AcpiFormatException(rv));
201 }
202
203 return sdhc_resume(self, qual);
204 }
205
206 static uint32_t
207 sdhc_acpi_intr(void *context)
208 {
209 struct sdhc_acpi_softc *sc = context;
210
211 if (!sdhc_intr(&sc->sc))
212 return ACPI_INTERRUPT_NOT_HANDLED;
213 return ACPI_INTERRUPT_HANDLED;
214 }
215