1 /* $NetBSD: wb_acpi.c,v 1.7 2020/12/07 10:02:51 jmcneill Exp $ */
2
3 /*
4 * Copyright (c) 2009 Jared D. McNeill <jmcneill@invisible.ca>
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: wb_acpi.c,v 1.7 2020/12/07 10:02:51 jmcneill Exp $");
30
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/systm.h>
34
35 #include <dev/acpi/acpireg.h>
36 #include <dev/acpi/acpivar.h>
37 #include <dev/acpi/acpi_intr.h>
38
39 #include <dev/ic/w83l518dvar.h>
40 #include <dev/ic/w83l518dreg.h>
41
42 #include <dev/isa/isadmavar.h>
43
44 #include <dev/sdmmc/sdmmcvar.h>
45
46 #define _COMPONENT ACPI_RESOURCE_COMPONENT
47 ACPI_MODULE_NAME ("wb_acpi")
48
49 static int wb_acpi_match(device_t, cfdata_t, void *);
50 static void wb_acpi_attach(device_t, device_t, void *);
51 static int wb_acpi_detach(device_t, int);
52 static bool wb_acpi_suspend(device_t, const pmf_qual_t *);
53 static bool wb_acpi_resume(device_t, const pmf_qual_t *);
54
55 struct wb_acpi_softc {
56 struct wb_softc sc_wb;
57 isa_chipset_tag_t sc_ic;
58 void *sc_ih;
59 int sc_ioh_length;
60
61 ACPI_HANDLE sc_crs, sc_srs;
62 ACPI_BUFFER sc_crs_buffer;
63 };
64
65 CFATTACH_DECL_NEW(wb_acpi, sizeof(struct wb_acpi_softc),
66 wb_acpi_match,
67 wb_acpi_attach,
68 wb_acpi_detach,
69 NULL
70 );
71
72 static const char * const wb_acpi_ids[] = {
73 #if notyet
74 "WEC0515", /* Memory Stick interface */
75 #endif
76 "WEC0517", /* SD Memory Card interface */
77 NULL
78 };
79
80 static int
wb_acpi_match(device_t parent,cfdata_t match,void * opaque)81 wb_acpi_match(device_t parent, cfdata_t match, void *opaque)
82 {
83 struct acpi_attach_args *aa = opaque;
84
85 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
86 return 0;
87
88 return acpi_match_hid(aa->aa_node->ad_devinfo, wb_acpi_ids);
89 }
90
91 static void
wb_acpi_attach(device_t parent,device_t self,void * opaque)92 wb_acpi_attach(device_t parent, device_t self, void *opaque)
93 {
94 struct wb_acpi_softc *sc = device_private(self);
95 struct acpi_attach_args *aa = opaque;
96 struct acpi_resources res;
97 struct acpi_io *io;
98 struct acpi_irq *irq;
99 bus_space_handle_t ioh;
100 ACPI_STATUS rv;
101
102 sc->sc_ic = aa->aa_ic;
103
104 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
105 &res, &acpi_resource_parse_ops_default);
106 if (ACPI_FAILURE(rv))
107 return;
108
109 AcpiGetHandle(aa->aa_node->ad_handle, "_CRS", &sc->sc_crs);
110 AcpiGetHandle(aa->aa_node->ad_handle, "_SRS", &sc->sc_srs);
111 if (sc->sc_crs && sc->sc_srs) {
112 sc->sc_crs_buffer.Pointer = NULL;
113 sc->sc_crs_buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
114 rv = AcpiGetCurrentResources(sc->sc_crs, &sc->sc_crs_buffer);
115 if (ACPI_FAILURE(rv))
116 sc->sc_crs = sc->sc_srs = NULL;
117 }
118
119 io = acpi_res_io(&res, 0);
120 irq = acpi_res_irq(&res, 0);
121 if (io == NULL || irq == NULL) {
122 aprint_error_dev(self, "incomplete resources\n");
123 goto cleanup;
124 }
125
126 if (bus_space_map(aa->aa_iot, io->ar_base, io->ar_length, 0, &ioh)) {
127 aprint_error_dev(self, "couldn't map registers\n");
128 goto cleanup;
129 }
130 sc->sc_ioh_length = io->ar_length;
131
132 sc->sc_ih = acpi_intr_establish(self,
133 (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
134 IPL_SDMMC, false, wb_intr, &sc->sc_wb, device_xname(self));
135 if (sc->sc_ih == NULL) {
136 aprint_error_dev(self,
137 "couldn't establish interrupt handler\n");
138 goto cleanup;
139 }
140
141 sc->sc_wb.wb_dev = self;
142 sc->sc_wb.wb_type = WB_DEVNO_SD;
143 sc->sc_wb.wb_iot = aa->aa_iot;
144 sc->sc_wb.wb_ioh = ioh;
145 sc->sc_wb.wb_irq = irq->ar_irq;
146 sc->sc_wb.wb_base = io->ar_base;
147 wb_attach(&sc->sc_wb);
148
149 pmf_device_register(self, wb_acpi_suspend, wb_acpi_resume);
150
151 cleanup:
152 acpi_resource_cleanup(&res);
153 }
154
155 static int
wb_acpi_detach(device_t self,int flags)156 wb_acpi_detach(device_t self, int flags)
157 {
158 struct wb_acpi_softc *sc = device_private(self);
159 int rv;
160
161 pmf_device_deregister(self);
162
163 if (sc->sc_crs_buffer.Pointer)
164 ACPI_FREE(sc->sc_crs_buffer.Pointer);
165
166 rv = wb_detach(&sc->sc_wb, flags);
167 if (rv)
168 return rv;
169
170 if (sc->sc_ih)
171 acpi_intr_disestablish(sc->sc_ih);
172
173 if (sc->sc_ioh_length > 0)
174 bus_space_unmap(sc->sc_wb.wb_iot, sc->sc_wb.wb_ioh,
175 sc->sc_ioh_length);
176
177 return 0;
178 }
179
180 static bool
wb_acpi_suspend(device_t self,const pmf_qual_t * qual)181 wb_acpi_suspend(device_t self, const pmf_qual_t *qual)
182 {
183 struct wb_acpi_softc *sc = device_private(self);
184
185 return wb_suspend(&sc->sc_wb);
186 }
187
188 static bool
wb_acpi_resume(device_t self,const pmf_qual_t * qual)189 wb_acpi_resume(device_t self, const pmf_qual_t *qual)
190 {
191 struct wb_acpi_softc *sc = device_private(self);
192 ACPI_STATUS rv;
193
194 if (sc->sc_crs && sc->sc_srs) {
195 rv = AcpiSetCurrentResources(sc->sc_srs, &sc->sc_crs_buffer);
196 if (ACPI_FAILURE(rv))
197 printf("%s: _SRS failed: %s\n",
198 device_xname(self), AcpiFormatException(rv));
199 }
200
201 return wb_resume(&sc->sc_wb);
202 }
203