acpi_ec.c revision 1.41.6.5 1 1.41.6.5 joerg /* $NetBSD: acpi_ec.c,v 1.41.6.5 2007/10/02 21:44:11 joerg Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.41.6.5 joerg * Copyright (c) 2007 Joerg Sonnenberger <joerg (at) NetBSD.org>.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.41.6.5 joerg *
11 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
12 1.1 thorpej * notice, this list of conditions and the following disclaimer.
13 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
14 1.41.6.5 joerg * notice, this list of conditions and the following disclaimer in
15 1.41.6.5 joerg * the documentation and/or other materials provided with the
16 1.41.6.5 joerg * distribution.
17 1.41.6.5 joerg *
18 1.41.6.5 joerg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 1.41.6.5 joerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 1.41.6.5 joerg * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 1.41.6.5 joerg * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 1.41.6.5 joerg * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.41.6.5 joerg * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 1.41.6.5 joerg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.41.6.5 joerg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 1.41.6.5 joerg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 1.41.6.5 joerg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 1.41.6.5 joerg * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 thorpej * SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.3 lukem #include <sys/cdefs.h>
33 1.41.6.5 joerg __KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.41.6.5 2007/10/02 21:44:11 joerg Exp $");
34 1.1 thorpej
35 1.1 thorpej #include <sys/param.h>
36 1.1 thorpej #include <sys/systm.h>
37 1.41.6.5 joerg #include <sys/condvar.h>
38 1.1 thorpej #include <sys/device.h>
39 1.1 thorpej #include <sys/kernel.h>
40 1.41.6.5 joerg #include <sys/kthread.h>
41 1.41.6.5 joerg #include <sys/mutex.h>
42 1.1 thorpej
43 1.1 thorpej #include <machine/bus.h>
44 1.1 thorpej
45 1.1 thorpej #include <dev/acpi/acpivar.h>
46 1.1 thorpej
47 1.41.6.5 joerg #define EC_LOCK_TIMEOUT 5
48 1.23 kochi
49 1.41.6.5 joerg /* From ACPI 3.0b, chapter 12.3 */
50 1.41.6.5 joerg #define EC_COMMAND_READ 0x80
51 1.41.6.5 joerg #define EC_COMMAND_WRITE 0x81
52 1.41.6.5 joerg #define EC_COMMAND_BURST_EN 0x82
53 1.41.6.5 joerg #define EC_COMMAND_BURST_DIS 0x83
54 1.41.6.5 joerg #define EC_COMMAND_QUERY 0x84
55 1.41.6.5 joerg
56 1.41.6.5 joerg /* From ACPI 3.0b, chapter 12.2.1 */
57 1.41.6.5 joerg #define EC_STATUS_OBF 0x01
58 1.41.6.5 joerg #define EC_STATUS_IBF 0x02
59 1.41.6.5 joerg #define EC_STATUS_CMD 0x08
60 1.41.6.5 joerg #define EC_STATUS_BURST 0x10
61 1.41.6.5 joerg #define EC_STATUS_SCI 0x20
62 1.41.6.5 joerg #define EC_STATUS_SMI 0x40
63 1.1 thorpej
64 1.41.6.5 joerg static const char *ec_hid[] = {
65 1.41.6.5 joerg "PNP0C09",
66 1.41.6.5 joerg NULL,
67 1.41.6.5 joerg };
68 1.1 thorpej
69 1.41.6.5 joerg enum ec_state_t {
70 1.41.6.5 joerg EC_STATE_QUERY,
71 1.41.6.5 joerg EC_STATE_READ,
72 1.41.6.5 joerg EC_STATE_READ_VAL,
73 1.41.6.5 joerg EC_STATE_READ_WAIT,
74 1.41.6.5 joerg EC_STATE_WRITE,
75 1.41.6.5 joerg EC_STATE_WRITE_VAL,
76 1.41.6.5 joerg EC_STATE_WRITE_WAIT,
77 1.41.6.5 joerg EC_STATE_FREE
78 1.41.6.5 joerg };
79 1.1 thorpej
80 1.41.6.5 joerg struct acpiec_softc {
81 1.41.6.5 joerg ACPI_HANDLE sc_ech;
82 1.1 thorpej
83 1.41.6.5 joerg ACPI_HANDLE sc_gpeh;
84 1.41.6.5 joerg UINT8 sc_gpebit;
85 1.1 thorpej
86 1.41.6.5 joerg bus_space_tag_t sc_data_st;
87 1.41.6.5 joerg bus_space_handle_t sc_data_sh;
88 1.1 thorpej
89 1.41.6.5 joerg bus_space_tag_t sc_csr_st;
90 1.41.6.5 joerg bus_space_handle_t sc_csr_sh;
91 1.17 mycroft
92 1.41.6.5 joerg bool sc_need_global_lock;
93 1.41.6.5 joerg UINT32 sc_global_lock;
94 1.1 thorpej
95 1.41.6.5 joerg kmutex_t sc_mtx, sc_access_mtx;
96 1.41.6.5 joerg kcondvar_t sc_cv, sc_cv_sci;
97 1.41.6.5 joerg enum ec_state_t sc_state;
98 1.41.6.5 joerg bool sc_got_sci;
99 1.41.6.5 joerg
100 1.41.6.5 joerg uint8_t sc_cur_addr, sc_cur_val;
101 1.16 kochi };
102 1.16 kochi
103 1.41.6.5 joerg static int acpiecdt_match(device_t, struct cfdata *, void *);
104 1.41.6.5 joerg static void acpiecdt_attach(device_t, device_t, void *);
105 1.1 thorpej
106 1.41.6.5 joerg static int acpiec_match(device_t, struct cfdata *, void *);
107 1.41.6.5 joerg static void acpiec_attach(device_t, device_t, void *);
108 1.41.6.5 joerg
109 1.41.6.5 joerg static void acpiec_common_attach(device_t, device_t, ACPI_HANDLE,
110 1.41.6.5 joerg bus_addr_t, bus_addr_t, ACPI_HANDLE, uint8_t);
111 1.41.6.5 joerg
112 1.41.6.5 joerg static pnp_status_t acpiec_power(device_t, pnp_request_t, void *);
113 1.17 mycroft
114 1.41.6.5 joerg static bool acpiec_parse_gpe_package(device_t, ACPI_HANDLE,
115 1.41.6.5 joerg ACPI_HANDLE *, uint8_t *);
116 1.20 yamt
117 1.41.6.5 joerg static void acpiec_gpe_query(void *);
118 1.41.6.5 joerg static UINT32 acpiec_gpe_handler(void *);
119 1.41.6.5 joerg static ACPI_STATUS acpiec_space_setup(ACPI_HANDLE, UINT32, void *, void **);
120 1.41.6.5 joerg static ACPI_STATUS acpiec_space_handler(UINT32, ACPI_PHYSICAL_ADDRESS,
121 1.41.6.5 joerg UINT32, ACPI_INTEGER *, void *, void *);
122 1.20 yamt
123 1.41.6.5 joerg static void acpiec_gpe_state_maschine(device_t);
124 1.20 yamt
125 1.41.6.5 joerg CFATTACH_DECL_NEW(acpiec, sizeof(struct acpiec_softc),
126 1.20 yamt acpiec_match, acpiec_attach, NULL, NULL);
127 1.20 yamt
128 1.41.6.5 joerg CFATTACH_DECL_NEW(acpiecdt, sizeof(struct acpiec_softc),
129 1.41.6.5 joerg acpiecdt_match, acpiecdt_attach, NULL, NULL);
130 1.23 kochi
131 1.41.6.5 joerg static device_t ec_singleton = NULL;
132 1.41.6.5 joerg static bool acpiec_cold = false;
133 1.41.6.5 joerg
134 1.41.6.5 joerg static bool
135 1.41.6.5 joerg acpiecdt_find(device_t parent, ACPI_HANDLE *ec_handle,
136 1.41.6.5 joerg bus_addr_t *cmd_reg, bus_addr_t *data_reg, uint8_t *gpebit)
137 1.9 tshiozak {
138 1.41.6.5 joerg EC_BOOT_RESOURCES *ec_boot;
139 1.41.6.5 joerg ACPI_STATUS rv;
140 1.41.6.5 joerg
141 1.41.6.5 joerg rv = AcpiGetFirmwareTable("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
142 1.41.6.5 joerg (void *)&ec_boot);
143 1.41.6.5 joerg if (rv != AE_OK)
144 1.41.6.5 joerg return false;
145 1.41.6.5 joerg
146 1.41.6.5 joerg if (ec_boot->EcControl.RegisterBitWidth != 8 ||
147 1.41.6.5 joerg ec_boot->EcData.RegisterBitWidth != 8) {
148 1.41.6.5 joerg aprint_error_dev(parent,
149 1.41.6.5 joerg "ECDT register width invalid (%d/%d)\n",
150 1.41.6.5 joerg ec_boot->EcControl.RegisterBitWidth,
151 1.41.6.5 joerg ec_boot->EcData.RegisterBitWidth);
152 1.41.6.5 joerg return false;
153 1.41.6.5 joerg }
154 1.41.6.5 joerg
155 1.41.6.5 joerg rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ec_boot->EcId, ec_handle);
156 1.41.6.5 joerg if (rv != AE_OK) {
157 1.41.6.5 joerg aprint_error_dev(parent,
158 1.41.6.5 joerg "failed to look up EC object %s: %s\n",
159 1.41.6.5 joerg ec_boot->EcId, AcpiFormatException(rv));
160 1.41.6.5 joerg return false;
161 1.41.6.5 joerg }
162 1.41.6.5 joerg
163 1.41.6.5 joerg *cmd_reg = ec_boot->EcControl.Address;
164 1.41.6.5 joerg *data_reg = ec_boot->EcData.Address;
165 1.41.6.5 joerg *gpebit = ec_boot->GpeBit;
166 1.24 kochi
167 1.41.6.5 joerg return true;
168 1.9 tshiozak }
169 1.4 thorpej
170 1.41.6.5 joerg static int
171 1.41.6.5 joerg acpiecdt_match(device_t parent, struct cfdata *match, void *aux)
172 1.1 thorpej {
173 1.41.6.5 joerg ACPI_HANDLE ec_handle;
174 1.41.6.5 joerg bus_addr_t cmd_reg, data_reg;
175 1.41.6.5 joerg uint8_t gpebit;
176 1.20 yamt
177 1.41.6.5 joerg if (acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
178 1.41.6.5 joerg return 1;
179 1.41.6.5 joerg else
180 1.41.6.5 joerg return 0;
181 1.1 thorpej }
182 1.1 thorpej
183 1.41.6.5 joerg static void
184 1.41.6.5 joerg acpiecdt_attach(device_t parent, device_t self, void *aux)
185 1.1 thorpej {
186 1.41.6.5 joerg ACPI_HANDLE ec_handle;
187 1.41.6.5 joerg bus_addr_t cmd_reg, data_reg;
188 1.41.6.5 joerg uint8_t gpebit;
189 1.20 yamt
190 1.41.6.5 joerg if (!acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
191 1.41.6.5 joerg panic("ECDT disappeared");
192 1.41.6.5 joerg
193 1.41.6.5 joerg aprint_naive(": ACPI Embedded Controller via ECDT\n");
194 1.41.6.5 joerg aprint_normal(": ACPI Embedded Controller via ECDT\n");
195 1.41.6.5 joerg
196 1.41.6.5 joerg acpiec_common_attach(parent, self, ec_handle, cmd_reg, data_reg,
197 1.41.6.5 joerg NULL, gpebit);
198 1.1 thorpej }
199 1.1 thorpej
200 1.31 kochi static int
201 1.41.6.5 joerg acpiec_match(device_t parent, struct cfdata *match, void *aux)
202 1.1 thorpej {
203 1.1 thorpej struct acpi_attach_args *aa = aux;
204 1.1 thorpej
205 1.1 thorpej if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
206 1.25 kochi return 0;
207 1.1 thorpej
208 1.25 kochi return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid);
209 1.1 thorpej }
210 1.1 thorpej
211 1.41.6.5 joerg static void
212 1.41.6.5 joerg acpiec_attach(device_t parent, device_t self, void *aux)
213 1.17 mycroft {
214 1.41.6.5 joerg struct acpi_attach_args *aa = aux;
215 1.41.6.5 joerg struct acpi_resources ec_res;
216 1.41.6.5 joerg struct acpi_io *io0, *io1;
217 1.41.6.5 joerg ACPI_HANDLE gpe_handle;
218 1.41.6.5 joerg uint8_t gpebit;
219 1.23 kochi ACPI_STATUS rv;
220 1.17 mycroft
221 1.41.6.5 joerg if (ec_singleton != NULL) {
222 1.41.6.5 joerg aprint_naive(": ACPI Embedded Controller (disabled)\n");
223 1.41.6.5 joerg aprint_normal(": ACPI Embedded Controller (disabled)\n");
224 1.41.6.5 joerg pnp_register(self, pnp_generic_power);
225 1.23 kochi return;
226 1.23 kochi }
227 1.23 kochi
228 1.41.6.5 joerg aprint_naive(": ACPI Embedded Controller\n");
229 1.41.6.5 joerg aprint_normal(": ACPI Embedded Controller\n");
230 1.23 kochi
231 1.41.6.5 joerg if (!acpiec_parse_gpe_package(self, aa->aa_node->ad_handle,
232 1.41.6.5 joerg &gpe_handle, &gpebit))
233 1.41.6.5 joerg return;
234 1.23 kochi
235 1.41.6.5 joerg rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
236 1.41.6.5 joerg &ec_res, &acpi_resource_parse_ops_default);
237 1.41.6.5 joerg if (rv != AE_OK) {
238 1.41.6.5 joerg aprint_error_dev(self, "resource parsing failed: %s\n",
239 1.23 kochi AcpiFormatException(rv));
240 1.41.6.5 joerg return;
241 1.17 mycroft }
242 1.23 kochi
243 1.41.6.5 joerg if ((io0 = acpi_res_io(&ec_res, 0)) == NULL) {
244 1.41.6.5 joerg aprint_error_dev(self, "no data register resource\n");
245 1.41.6.5 joerg goto free_res;
246 1.41.6.5 joerg }
247 1.41.6.5 joerg if ((io1 = acpi_res_io(&ec_res, 1)) == NULL) {
248 1.41.6.5 joerg aprint_error_dev(self, "no CSR register resource\n");
249 1.41.6.5 joerg goto free_res;
250 1.41.6.5 joerg }
251 1.23 kochi
252 1.41.6.5 joerg acpiec_common_attach(parent, self, aa->aa_node->ad_handle,
253 1.41.6.5 joerg io1->ar_base, io0->ar_base, gpe_handle, gpebit);
254 1.23 kochi
255 1.41.6.5 joerg free_res:
256 1.41.6.5 joerg acpi_resource_cleanup(&ec_res);
257 1.17 mycroft }
258 1.17 mycroft
259 1.31 kochi static void
260 1.41.6.5 joerg acpiec_common_attach(device_t parent, device_t self,
261 1.41.6.5 joerg ACPI_HANDLE ec_handle, bus_addr_t cmd_reg, bus_addr_t data_reg,
262 1.41.6.5 joerg ACPI_HANDLE gpe_handle, uint8_t gpebit)
263 1.1 thorpej {
264 1.41.6.5 joerg struct acpiec_softc *sc = device_private(self);
265 1.1 thorpej ACPI_STATUS rv;
266 1.41.6.5 joerg ACPI_INTEGER val;
267 1.1 thorpej
268 1.41.6.5 joerg sc->sc_ech = ec_handle;
269 1.41.6.5 joerg sc->sc_gpeh = gpe_handle;
270 1.41.6.5 joerg sc->sc_gpebit = gpebit;
271 1.41.6.5 joerg
272 1.41.6.5 joerg sc->sc_state = EC_STATE_FREE;
273 1.41.6.5 joerg mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_TTY);
274 1.41.6.5 joerg mutex_init(&sc->sc_access_mtx, MUTEX_DEFAULT, IPL_NONE);
275 1.41.6.5 joerg cv_init(&sc->sc_cv, "eccv");
276 1.41.6.5 joerg cv_init(&sc->sc_cv_sci, "ecsci");
277 1.41.6.5 joerg
278 1.41.6.5 joerg if (bus_space_map(sc->sc_data_st, data_reg, 1, 0,
279 1.41.6.5 joerg &sc->sc_data_sh) != 0) {
280 1.41.6.5 joerg aprint_error_dev(self, "unable to map data register\n");
281 1.41.6.5 joerg return;
282 1.41.6.5 joerg }
283 1.1 thorpej
284 1.41.6.5 joerg if (bus_space_map(sc->sc_csr_st, cmd_reg, 1, 0, &sc->sc_csr_sh) != 0) {
285 1.41.6.5 joerg aprint_error_dev(self, "unable to map CSR register\n");
286 1.41.6.5 joerg goto post_data_map;
287 1.41.6.5 joerg }
288 1.17 mycroft
289 1.41.6.5 joerg rv = acpi_eval_integer(sc->sc_ech, "_GLK", &val);
290 1.41.6.5 joerg if (rv == AE_OK) {
291 1.41.6.5 joerg sc->sc_need_global_lock = val != 0;
292 1.41.6.5 joerg } else if (rv != AE_NOT_FOUND) {
293 1.41.6.5 joerg aprint_error_dev(self, "unable to evaluate _GLK: %s\n",
294 1.41.6.5 joerg AcpiFormatException(rv));
295 1.41.6.5 joerg goto post_csr_map;
296 1.41.6.5 joerg } else {
297 1.41.6.5 joerg sc->sc_need_global_lock = false;
298 1.41.6.5 joerg }
299 1.41.6.5 joerg if (sc->sc_need_global_lock)
300 1.41.6.5 joerg aprint_normal_dev(self, "using global ACPI lock\n");
301 1.1 thorpej
302 1.41.6.5 joerg rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC,
303 1.41.6.5 joerg acpiec_space_handler, acpiec_space_setup, self);
304 1.41.6.5 joerg if (rv != AE_OK) {
305 1.41.6.5 joerg aprint_error_dev(self,
306 1.41.6.5 joerg "unable to install address space handler: %s\n",
307 1.41.6.5 joerg AcpiFormatException(rv));
308 1.41.6.5 joerg goto post_csr_map;
309 1.41.6.5 joerg }
310 1.1 thorpej
311 1.41.6.5 joerg rv = AcpiInstallGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
312 1.41.6.5 joerg ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, self);
313 1.41.6.5 joerg if (rv != AE_OK) {
314 1.41.6.5 joerg aprint_error_dev(self, "unable to install GPE handler: %s\n",
315 1.41.6.5 joerg AcpiFormatException(rv));
316 1.41.6.5 joerg goto post_csr_map;
317 1.41.6.5 joerg }
318 1.23 kochi
319 1.41.6.5 joerg rv = AcpiSetGpeType(sc->sc_gpeh, sc->sc_gpebit, ACPI_GPE_TYPE_RUNTIME);
320 1.41.6.5 joerg if (rv != AE_OK) {
321 1.41.6.5 joerg aprint_error_dev(self, "unable to set GPE type: %s\n",
322 1.41.6.5 joerg AcpiFormatException(rv));
323 1.41.6.5 joerg goto post_csr_map;
324 1.1 thorpej }
325 1.1 thorpej
326 1.41.6.5 joerg rv = AcpiEnableGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
327 1.41.6.5 joerg if (rv != AE_OK) {
328 1.41.6.5 joerg aprint_error_dev(self, "unable to enable GPE: %s\n",
329 1.41.6.5 joerg AcpiFormatException(rv));
330 1.41.6.5 joerg goto post_csr_map;
331 1.41.6.5 joerg }
332 1.23 kochi
333 1.41.6.5 joerg if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, acpiec_gpe_query,
334 1.41.6.5 joerg self, NULL, "acpiec sci thread")) {
335 1.41.6.5 joerg aprint_error_dev(self, "unable to create query kthread\n");
336 1.41.6.5 joerg goto post_csr_map;
337 1.17 mycroft }
338 1.17 mycroft
339 1.41.6.5 joerg ec_singleton = self;
340 1.1 thorpej
341 1.41.6.5 joerg pnp_register(self, acpiec_power);
342 1.1 thorpej
343 1.41.6.5 joerg return;
344 1.1 thorpej
345 1.41.6.5 joerg post_csr_map:
346 1.41.6.5 joerg (void)AcpiRemoveGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
347 1.41.6.5 joerg acpiec_gpe_handler);
348 1.41.6.5 joerg (void)AcpiRemoveAddressSpaceHandler(sc->sc_ech,
349 1.41.6.5 joerg ACPI_ADR_SPACE_EC, acpiec_space_handler);
350 1.41.6.5 joerg bus_space_unmap(sc->sc_csr_st, sc->sc_csr_sh, 1);
351 1.41.6.5 joerg post_data_map:
352 1.41.6.5 joerg bus_space_unmap(sc->sc_data_st, sc->sc_data_sh, 1);
353 1.41.6.5 joerg }
354 1.41.6.5 joerg
355 1.41.6.5 joerg static pnp_status_t
356 1.41.6.5 joerg acpiec_power(device_t dv, pnp_request_t req, void *opaque)
357 1.41.6.5 joerg {
358 1.41.6.5 joerg pnp_capabilities_t *pcaps;
359 1.41.6.5 joerg pnp_state_t *pstate;
360 1.41.6.5 joerg
361 1.41.6.5 joerg switch (req) {
362 1.41.6.5 joerg case PNP_REQUEST_GET_CAPABILITIES:
363 1.41.6.5 joerg pcaps = opaque;
364 1.41.6.5 joerg pcaps->state = PNP_STATE_D0 | PNP_STATE_D3;
365 1.41.6.5 joerg break;
366 1.41.6.5 joerg case PNP_REQUEST_GET_STATE:
367 1.41.6.5 joerg pstate = opaque;
368 1.41.6.5 joerg if (acpiec_cold)
369 1.41.6.5 joerg *pstate = PNP_STATE_D0;
370 1.41.6.5 joerg else
371 1.41.6.5 joerg *pstate = PNP_STATE_D3;
372 1.41.6.5 joerg break;
373 1.41.6.5 joerg case PNP_REQUEST_SET_STATE:
374 1.41.6.5 joerg pstate = opaque;
375 1.41.6.5 joerg switch (*pstate) {
376 1.41.6.5 joerg case PNP_STATE_D0:
377 1.41.6.5 joerg acpiec_cold = false;
378 1.1 thorpej break;
379 1.41.6.5 joerg case PNP_STATE_D3:
380 1.41.6.5 joerg acpiec_cold = true;
381 1.1 thorpej break;
382 1.41.6.5 joerg default:
383 1.41.6.5 joerg return PNP_STATUS_UNSUPPORTED;
384 1.1 thorpej }
385 1.41.6.5 joerg break;
386 1.41.6.5 joerg default:
387 1.41.6.5 joerg return PNP_STATUS_UNSUPPORTED;
388 1.1 thorpej }
389 1.1 thorpej
390 1.41.6.5 joerg return PNP_STATUS_SUCCESS;
391 1.1 thorpej }
392 1.1 thorpej
393 1.41.6.5 joerg static bool
394 1.41.6.5 joerg acpiec_parse_gpe_package(device_t self, ACPI_HANDLE ec_handle,
395 1.41.6.5 joerg ACPI_HANDLE *gpe_handle, uint8_t *gpebit)
396 1.1 thorpej {
397 1.41.6.5 joerg ACPI_BUFFER buf;
398 1.41.6.5 joerg ACPI_OBJECT *p, *c;
399 1.25 kochi ACPI_STATUS rv;
400 1.1 thorpej
401 1.41.6.5 joerg rv = acpi_eval_struct(ec_handle, "_GPE", &buf);
402 1.41.6.5 joerg if (rv != AE_OK) {
403 1.41.6.5 joerg aprint_error_dev(self, "unable to evaluate _GPE: %s\n",
404 1.41.6.5 joerg AcpiFormatException(rv));
405 1.41.6.5 joerg return false;
406 1.41.6.5 joerg }
407 1.41.6.5 joerg
408 1.41.6.5 joerg p = buf.Pointer;
409 1.41.6.5 joerg
410 1.41.6.5 joerg if (p->Type == ACPI_TYPE_INTEGER) {
411 1.41.6.5 joerg *gpe_handle = NULL;
412 1.41.6.5 joerg *gpebit = p->Integer.Value;
413 1.41.6.5 joerg AcpiOsFree(p);
414 1.41.6.5 joerg return true;
415 1.41.6.5 joerg }
416 1.41.6.5 joerg
417 1.41.6.5 joerg if (p->Type != ACPI_TYPE_PACKAGE) {
418 1.41.6.5 joerg aprint_error_dev(self, "_GPE is neither integer nor package\n");
419 1.41.6.5 joerg AcpiOsFree(p);
420 1.41.6.5 joerg return false;
421 1.41.6.5 joerg }
422 1.41.6.5 joerg
423 1.41.6.5 joerg if (p->Package.Count != 2) {
424 1.41.6.5 joerg aprint_error_dev(self, "_GPE package does not contain 2 elements\n");
425 1.41.6.5 joerg AcpiOsFree(p);
426 1.41.6.5 joerg return false;
427 1.1 thorpej }
428 1.33 kochi
429 1.41.6.5 joerg c = &p->Package.Elements[0];
430 1.41.6.5 joerg switch (c->Type) {
431 1.41.6.5 joerg case ACPI_TYPE_LOCAL_REFERENCE:
432 1.41.6.5 joerg case ACPI_TYPE_ANY:
433 1.41.6.5 joerg *gpe_handle = c->Reference.Handle;
434 1.41.6.5 joerg break;
435 1.41.6.5 joerg case ACPI_TYPE_STRING:
436 1.41.6.5 joerg /* XXX should be using real scope here */
437 1.41.6.5 joerg rv = AcpiGetHandle(NULL, p->String.Pointer, gpe_handle);
438 1.41.6.5 joerg if (rv != AE_OK) {
439 1.41.6.5 joerg aprint_error_dev(self,
440 1.41.6.5 joerg "_GPE device reference unresolvable\n");
441 1.41.6.5 joerg AcpiOsFree(p);
442 1.41.6.5 joerg return false;
443 1.41.6.5 joerg }
444 1.41.6.5 joerg break;
445 1.41.6.5 joerg default:
446 1.41.6.5 joerg aprint_error_dev(self, "_GPE device reference incorrect\n");
447 1.41.6.5 joerg AcpiOsFree(p);
448 1.41.6.5 joerg return false;
449 1.41.6.5 joerg }
450 1.41.6.5 joerg c = &p->Package.Elements[1];
451 1.41.6.5 joerg if (c->Type != ACPI_TYPE_INTEGER) {
452 1.41.6.5 joerg aprint_error_dev(self,
453 1.41.6.5 joerg "_GPE package needs integer as 2nd field\n");
454 1.41.6.5 joerg AcpiOsFree(p);
455 1.41.6.5 joerg return false;
456 1.41.6.5 joerg }
457 1.41.6.5 joerg *gpebit = c->Integer.Value;
458 1.41.6.5 joerg AcpiOsFree(p);
459 1.41.6.5 joerg return true;
460 1.1 thorpej }
461 1.1 thorpej
462 1.41.6.5 joerg static uint8_t
463 1.41.6.5 joerg acpiec_read_data(struct acpiec_softc *sc)
464 1.1 thorpej {
465 1.41.6.5 joerg return bus_space_read_1(sc->sc_data_st, sc->sc_data_sh, 0);
466 1.41.6.5 joerg }
467 1.1 thorpej
468 1.41.6.5 joerg static void
469 1.41.6.5 joerg acpiec_write_data(struct acpiec_softc *sc, uint8_t val)
470 1.41.6.5 joerg {
471 1.41.6.5 joerg bus_space_write_1(sc->sc_data_st, sc->sc_data_sh, 0, val);
472 1.41.6.5 joerg }
473 1.1 thorpej
474 1.41.6.5 joerg static uint8_t
475 1.41.6.5 joerg acpiec_read_status(struct acpiec_softc *sc)
476 1.41.6.5 joerg {
477 1.41.6.5 joerg return bus_space_read_1(sc->sc_csr_st, sc->sc_csr_sh, 0);
478 1.41.6.5 joerg }
479 1.1 thorpej
480 1.41.6.5 joerg static void
481 1.41.6.5 joerg acpiec_write_command(struct acpiec_softc *sc, uint8_t cmd)
482 1.41.6.5 joerg {
483 1.41.6.5 joerg bus_space_write_1(sc->sc_csr_st, sc->sc_csr_sh, 0, cmd);
484 1.1 thorpej }
485 1.1 thorpej
486 1.1 thorpej static ACPI_STATUS
487 1.41.6.5 joerg acpiec_space_setup(ACPI_HANDLE region, UINT32 func, void *arg,
488 1.41.6.5 joerg void **region_arg)
489 1.1 thorpej {
490 1.41.6.5 joerg if (func == ACPI_REGION_DEACTIVATE)
491 1.41.6.5 joerg *region_arg = NULL;
492 1.41.6.5 joerg else
493 1.41.6.5 joerg *region_arg = arg;
494 1.1 thorpej
495 1.41.6.5 joerg return AE_OK;
496 1.41.6.5 joerg }
497 1.1 thorpej
498 1.41.6.5 joerg static void
499 1.41.6.5 joerg acpiec_lock(device_t dv)
500 1.41.6.5 joerg {
501 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
502 1.41.6.5 joerg ACPI_STATUS rv;
503 1.1 thorpej
504 1.41.6.5 joerg mutex_enter(&sc->sc_access_mtx);
505 1.1 thorpej
506 1.41.6.5 joerg if (sc->sc_need_global_lock) {
507 1.41.6.5 joerg rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->sc_global_lock);
508 1.41.6.5 joerg if (rv != AE_OK) {
509 1.41.6.5 joerg aprint_error("%s: failed to acquire global lock (continuing)\n",
510 1.41.6.5 joerg device_xname(dv));
511 1.41.6.5 joerg return;
512 1.41.6.5 joerg }
513 1.41.6.5 joerg }
514 1.41.6.5 joerg }
515 1.1 thorpej
516 1.41.6.5 joerg static void
517 1.41.6.5 joerg acpiec_unlock(device_t dv)
518 1.41.6.5 joerg {
519 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
520 1.41.6.5 joerg ACPI_STATUS rv;
521 1.41.6.5 joerg
522 1.41.6.5 joerg if (sc->sc_need_global_lock) {
523 1.41.6.5 joerg rv = AcpiReleaseGlobalLock(sc->sc_global_lock);
524 1.41.6.5 joerg if (rv != AE_OK) {
525 1.41.6.5 joerg aprint_error("%s: failed to release global lock (continuing)\n",
526 1.41.6.5 joerg device_xname(dv));
527 1.41.6.5 joerg }
528 1.1 thorpej }
529 1.41.6.5 joerg mutex_exit(&sc->sc_access_mtx);
530 1.41.6.5 joerg }
531 1.1 thorpej
532 1.41.6.5 joerg static ACPI_STATUS
533 1.41.6.5 joerg acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
534 1.41.6.5 joerg {
535 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
536 1.41.6.5 joerg int timeouts = 0;
537 1.1 thorpej
538 1.41.6.5 joerg acpiec_lock(dv);
539 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
540 1.1 thorpej
541 1.41.6.5 joerg retry:
542 1.41.6.5 joerg sc->sc_cur_addr = addr;
543 1.41.6.5 joerg sc->sc_state = EC_STATE_READ;
544 1.41.6.5 joerg
545 1.41.6.5 joerg acpiec_write_command(sc, EC_COMMAND_READ);
546 1.41.6.5 joerg if (cold || acpiec_cold) {
547 1.41.6.5 joerg for (delay(1); sc->sc_state != EC_STATE_READ_WAIT; delay(1))
548 1.41.6.5 joerg acpiec_gpe_state_maschine(dv);
549 1.41.6.5 joerg } else while (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz)) {
550 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
551 1.41.6.5 joerg AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
552 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
553 1.41.6.5 joerg if (++timeouts < 5)
554 1.41.6.5 joerg goto retry;
555 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
556 1.41.6.5 joerg acpiec_unlock(dv);
557 1.41.6.5 joerg aprint_error_dev(dv, "command takes over 5sec...\n");
558 1.41.6.5 joerg return AE_ERROR;
559 1.41.6.5 joerg }
560 1.41.6.5 joerg *val = acpiec_read_data(sc);
561 1.1 thorpej
562 1.41.6.5 joerg sc->sc_state = EC_STATE_FREE;
563 1.41.6.5 joerg
564 1.41.6.5 joerg if (sc->sc_got_sci)
565 1.41.6.5 joerg cv_signal(&sc->sc_cv_sci);
566 1.41.6.5 joerg
567 1.41.6.5 joerg
568 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
569 1.41.6.5 joerg acpiec_unlock(dv);
570 1.41.6.5 joerg return AE_OK;
571 1.1 thorpej }
572 1.1 thorpej
573 1.1 thorpej static ACPI_STATUS
574 1.41.6.5 joerg acpiec_write(device_t dv, uint8_t addr, uint8_t val)
575 1.1 thorpej {
576 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
577 1.41.6.5 joerg int timeouts = 0;
578 1.1 thorpej
579 1.41.6.5 joerg acpiec_lock(dv);
580 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
581 1.1 thorpej
582 1.41.6.5 joerg retry:
583 1.41.6.5 joerg sc->sc_cur_addr = addr;
584 1.41.6.5 joerg sc->sc_cur_val = val;
585 1.41.6.5 joerg sc->sc_state = EC_STATE_WRITE;
586 1.41.6.5 joerg
587 1.41.6.5 joerg acpiec_write_command(sc, EC_COMMAND_WRITE);
588 1.41.6.5 joerg if (cold || acpiec_cold) {
589 1.41.6.5 joerg for (delay(1); sc->sc_state != EC_STATE_WRITE_WAIT; delay(1))
590 1.41.6.5 joerg acpiec_gpe_state_maschine(dv);
591 1.41.6.5 joerg } else while (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz)) {
592 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
593 1.41.6.5 joerg AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
594 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
595 1.41.6.5 joerg if (++timeouts < 5)
596 1.41.6.5 joerg goto retry;
597 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
598 1.41.6.5 joerg acpiec_unlock(dv);
599 1.41.6.5 joerg aprint_error_dev(dv, "command takes over 5sec...\n");
600 1.41.6.5 joerg return AE_ERROR;
601 1.41.6.4 joerg }
602 1.41.6.4 joerg
603 1.41.6.5 joerg sc->sc_state = EC_STATE_FREE;
604 1.1 thorpej
605 1.41.6.5 joerg if (sc->sc_got_sci)
606 1.41.6.5 joerg cv_signal(&sc->sc_cv_sci);
607 1.41.6.5 joerg
608 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
609 1.41.6.5 joerg acpiec_unlock(dv);
610 1.41.6.5 joerg return AE_OK;
611 1.1 thorpej }
612 1.1 thorpej
613 1.1 thorpej static ACPI_STATUS
614 1.41.6.5 joerg acpiec_space_handler(UINT32 func, ACPI_PHYSICAL_ADDRESS paddr,
615 1.41.6.5 joerg UINT32 width, ACPI_INTEGER *value, void *arg, void *region_arg)
616 1.1 thorpej {
617 1.41.6.5 joerg device_t dv;
618 1.41.6.5 joerg struct acpiec_softc *sc;
619 1.25 kochi ACPI_STATUS rv;
620 1.41.6.5 joerg uint8_t addr, reg;
621 1.41.6.5 joerg unsigned int i;
622 1.41.6.5 joerg
623 1.41.6.5 joerg if (paddr > 0xff || width % 8 != 0 || value == NULL || arg == NULL ||
624 1.41.6.5 joerg paddr + width / 8 > 0xff)
625 1.41.6.5 joerg return AE_BAD_PARAMETER;
626 1.1 thorpej
627 1.41.6.5 joerg addr = paddr;
628 1.41.6.5 joerg dv = arg;
629 1.41.6.5 joerg sc = device_private(dv);
630 1.41.6.5 joerg
631 1.41.6.5 joerg rv = AE_OK;
632 1.41.6.5 joerg
633 1.41.6.5 joerg switch (func) {
634 1.41.6.5 joerg case ACPI_READ:
635 1.41.6.5 joerg for (i = 0; i < width; i += 8, ++addr) {
636 1.41.6.5 joerg rv = acpiec_read(dv, addr, ®);
637 1.41.6.5 joerg if (rv != AE_OK)
638 1.41.6.5 joerg break;
639 1.41.6.5 joerg *value |= (ACPI_INTEGER)reg << i;
640 1.41.6.5 joerg }
641 1.41.6.5 joerg break;
642 1.41.6.5 joerg case ACPI_WRITE:
643 1.41.6.5 joerg for (i = 0; i < width; i += 8, ++addr) {
644 1.41.6.5 joerg reg = (*value >>i) & 0xff;
645 1.41.6.5 joerg rv = acpiec_write(dv, addr, reg);
646 1.41.6.5 joerg if (rv != AE_OK)
647 1.41.6.5 joerg break;
648 1.41.6.5 joerg }
649 1.41.6.5 joerg break;
650 1.41.6.5 joerg default:
651 1.41.6.5 joerg aprint_error("%s: invalid Address Space function called: %x\n",
652 1.41.6.5 joerg device_xname(dv), (unsigned int)func);
653 1.41.6.5 joerg return AE_BAD_PARAMETER;
654 1.41.6.5 joerg }
655 1.1 thorpej
656 1.25 kochi return rv;
657 1.24 kochi }
658 1.1 thorpej
659 1.41.6.5 joerg static void
660 1.41.6.5 joerg acpiec_gpe_query(void *arg)
661 1.1 thorpej {
662 1.41.6.5 joerg device_t dv = arg;
663 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
664 1.41.6.5 joerg uint8_t reg;
665 1.41.6.5 joerg char qxx[5];
666 1.25 kochi ACPI_STATUS rv;
667 1.1 thorpej
668 1.41.6.5 joerg loop:
669 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
670 1.1 thorpej
671 1.41.6.5 joerg if (sc->sc_got_sci == false)
672 1.41.6.5 joerg cv_wait(&sc->sc_cv_sci, &sc->sc_mtx);
673 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
674 1.1 thorpej
675 1.41.6.5 joerg acpiec_lock(dv);
676 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
677 1.1 thorpej
678 1.41.6.5 joerg acpiec_write_command(sc, EC_COMMAND_QUERY);
679 1.41.6.5 joerg sc->sc_state = EC_STATE_QUERY;
680 1.1 thorpej
681 1.41.6.5 joerg cv_wait(&sc->sc_cv, &sc->sc_mtx);
682 1.1 thorpej
683 1.41.6.5 joerg reg = acpiec_read_data(sc);
684 1.41.6.5 joerg sc->sc_state = EC_STATE_FREE;
685 1.41.6.5 joerg sc->sc_got_sci = false;
686 1.1 thorpej
687 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
688 1.41.6.5 joerg acpiec_unlock(dv);
689 1.41.6.5 joerg
690 1.41.6.5 joerg if (reg == 0)
691 1.41.6.5 joerg goto loop; /* Spurious query result */
692 1.41.6.5 joerg /*
693 1.41.6.5 joerg * Evaluate _Qxx to respond to the controller.
694 1.41.6.5 joerg */
695 1.41.6.5 joerg snprintf(qxx, sizeof(qxx), "_Q%02X", (unsigned int)reg);
696 1.41.6.5 joerg rv = AcpiEvaluateObject(sc->sc_ech, qxx, NULL, NULL);
697 1.41.6.5 joerg if (rv != AE_OK && rv != AE_NOT_FOUND) {
698 1.41.6.5 joerg aprint_error("%s: GPE query method %s failed: %s",
699 1.41.6.5 joerg device_xname(dv), qxx, AcpiFormatException(rv));
700 1.41.6.5 joerg }
701 1.41.6.5 joerg
702 1.41.6.5 joerg goto loop;
703 1.24 kochi }
704 1.1 thorpej
705 1.41.6.5 joerg static void
706 1.41.6.5 joerg acpiec_gpe_state_maschine(device_t dv)
707 1.1 thorpej {
708 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
709 1.41.6.5 joerg uint8_t reg;
710 1.1 thorpej
711 1.41.6.5 joerg reg = acpiec_read_status(sc);
712 1.1 thorpej
713 1.41.6.5 joerg if (reg & EC_STATUS_SCI)
714 1.41.6.5 joerg sc->sc_got_sci = true;
715 1.1 thorpej
716 1.41.6.5 joerg switch (sc->sc_state) {
717 1.41.6.5 joerg case EC_STATE_QUERY:
718 1.41.6.5 joerg if ((reg & EC_STATUS_OBF) == 0)
719 1.41.6.5 joerg break; /* Nothing of interest here. */
720 1.1 thorpej
721 1.41.6.5 joerg cv_signal(&sc->sc_cv);
722 1.41.6.5 joerg break;
723 1.1 thorpej
724 1.41.6.5 joerg case EC_STATE_READ:
725 1.41.6.5 joerg if ((reg & EC_STATUS_IBF) != 0)
726 1.41.6.5 joerg break; /* Nothing of interest here. */
727 1.1 thorpej
728 1.41.6.5 joerg acpiec_write_data(sc, sc->sc_cur_addr);
729 1.41.6.5 joerg sc->sc_state = EC_STATE_READ_VAL;
730 1.41.6.5 joerg break;
731 1.1 thorpej
732 1.41.6.5 joerg case EC_STATE_READ_VAL:
733 1.41.6.5 joerg if ((reg & EC_STATUS_OBF) == 0)
734 1.41.6.5 joerg break; /* Nothing of interest here. */
735 1.41.6.5 joerg cv_signal(&sc->sc_cv);
736 1.41.6.5 joerg sc->sc_state = EC_STATE_READ_WAIT;
737 1.41.6.5 joerg break;
738 1.41.6.3 joerg
739 1.41.6.5 joerg case EC_STATE_READ_WAIT:
740 1.41.6.5 joerg break; /* Nothing of interest here. */
741 1.41.6.3 joerg
742 1.41.6.5 joerg case EC_STATE_WRITE:
743 1.41.6.5 joerg if ((reg & EC_STATUS_IBF) != 0)
744 1.41.6.5 joerg break; /* Nothing of interest here. */
745 1.41.6.5 joerg acpiec_write_data(sc, sc->sc_cur_addr);
746 1.41.6.5 joerg sc->sc_state = EC_STATE_WRITE_VAL;
747 1.41.6.5 joerg break;
748 1.41.6.3 joerg
749 1.41.6.5 joerg case EC_STATE_WRITE_VAL:
750 1.41.6.5 joerg if ((reg & EC_STATUS_IBF) != 0)
751 1.41.6.5 joerg break; /* Nothing of interest here. */
752 1.41.6.5 joerg cv_signal(&sc->sc_cv);
753 1.41.6.5 joerg acpiec_write_data(sc, sc->sc_cur_val);
754 1.41.6.5 joerg sc->sc_state = EC_STATE_WRITE_WAIT;
755 1.41.6.3 joerg break;
756 1.41.6.3 joerg
757 1.41.6.5 joerg case EC_STATE_WRITE_WAIT:
758 1.41.6.3 joerg break;
759 1.41.6.3 joerg
760 1.41.6.5 joerg case EC_STATE_FREE:
761 1.41.6.5 joerg if (sc->sc_got_sci)
762 1.41.6.5 joerg cv_signal(&sc->sc_cv_sci);
763 1.41.6.3 joerg break;
764 1.41.6.5 joerg default:
765 1.41.6.5 joerg panic("invalid state");
766 1.41.6.3 joerg }
767 1.41.6.5 joerg }
768 1.41.6.3 joerg
769 1.41.6.5 joerg static UINT32
770 1.41.6.5 joerg acpiec_gpe_handler(void *arg)
771 1.41.6.5 joerg {
772 1.41.6.5 joerg device_t dv = arg;
773 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
774 1.41.6.3 joerg
775 1.41.6.5 joerg AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
776 1.41.6.5 joerg
777 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
778 1.41.6.5 joerg acpiec_gpe_state_maschine(dv);
779 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
780 1.41.6.5 joerg
781 1.41.6.5 joerg return 0;
782 1.41.6.3 joerg }
783