acpi_ec.c revision 1.33.2.6 1 1.33.2.6 yamt /* $NetBSD: acpi_ec.c,v 1.33.2.6 2008/01/21 09:42:29 yamt Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.33.2.6 yamt * 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.33.2.6 yamt *
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.33.2.6 yamt * notice, this list of conditions and the following disclaimer in
15 1.33.2.6 yamt * the documentation and/or other materials provided with the
16 1.33.2.6 yamt * distribution.
17 1.33.2.6 yamt *
18 1.33.2.6 yamt * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 1.33.2.6 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 1.33.2.6 yamt * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 1.33.2.6 yamt * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 1.33.2.6 yamt * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.33.2.6 yamt * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 1.33.2.6 yamt * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.33.2.6 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 1.33.2.6 yamt * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 1.33.2.6 yamt * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 1.33.2.6 yamt * 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.33.2.6 yamt /*
33 1.33.2.6 yamt * The ACPI Embedded Controller (EC) driver serves two different purposes:
34 1.33.2.6 yamt * - read and write access from ASL, e.g. to read battery state
35 1.33.2.6 yamt * - notification of ASL of System Control Interrupts.
36 1.33.2.6 yamt *
37 1.33.2.6 yamt * Access to the EC is serialised by sc_access_mtx and optionally the
38 1.33.2.6 yamt * ACPI global mutex. Both locks are held until the request is fulfilled.
39 1.33.2.6 yamt * All access to the softc has to hold sc_mtx to serialise against the GPE
40 1.33.2.6 yamt * handler and the callout. sc_mtx is also used for wakeup conditions.
41 1.33.2.6 yamt *
42 1.33.2.6 yamt * SCIs are processed in a kernel thread. Handling gets a bit complicated
43 1.33.2.6 yamt * by the lock order (sc_mtx must be acquired after sc_access_mtx and the
44 1.33.2.6 yamt * ACPI global mutex).
45 1.33.2.6 yamt *
46 1.33.2.6 yamt * Read and write requests spin around for a short time as many requests
47 1.33.2.6 yamt * can be handled instantly by the EC. During normal processing interrupt
48 1.33.2.6 yamt * mode is used exclusively. At boot and resume time interrupts are not
49 1.33.2.6 yamt * working and the handlers just busy loop.
50 1.33.2.6 yamt *
51 1.33.2.6 yamt * A callout is scheduled to compensate for missing interrupts on some
52 1.33.2.6 yamt * hardware. If the EC doesn't process a request for 5s, it is most likely
53 1.33.2.6 yamt * in a wedged state. No method to reset the EC is currently known.
54 1.33.2.6 yamt *
55 1.33.2.6 yamt * Special care has to be taken to not poll the EC in a busy loop without
56 1.33.2.6 yamt * delay. This can prevent processing of Power Button events. At least some
57 1.33.2.6 yamt * Lenovo Thinkpads seem to be implement the Power Button Override in the EC
58 1.33.2.6 yamt * and the only option to recover on those models is to cut off all power.
59 1.33.2.6 yamt */
60 1.3 lukem
61 1.3 lukem #include <sys/cdefs.h>
62 1.33.2.6 yamt __KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.33.2.6 2008/01/21 09:42:29 yamt Exp $");
63 1.1 thorpej
64 1.1 thorpej #include <sys/param.h>
65 1.1 thorpej #include <sys/systm.h>
66 1.33.2.6 yamt #include <sys/condvar.h>
67 1.1 thorpej #include <sys/device.h>
68 1.1 thorpej #include <sys/kernel.h>
69 1.33.2.6 yamt #include <sys/kthread.h>
70 1.33.2.6 yamt #include <sys/mutex.h>
71 1.1 thorpej
72 1.33.2.4 yamt #include <sys/bus.h>
73 1.1 thorpej
74 1.1 thorpej #include <dev/acpi/acpivar.h>
75 1.33.2.6 yamt #include <dev/acpi/acpi_ecvar.h>
76 1.1 thorpej
77 1.33.2.6 yamt /* Maximum time to wait for global ACPI lock in ms */
78 1.33.2.6 yamt #define EC_LOCK_TIMEOUT 5
79 1.23 kochi
80 1.33.2.6 yamt /* Maximum time to poll for completion of a command in ms */
81 1.33.2.6 yamt #define EC_POLL_TIMEOUT 5
82 1.1 thorpej
83 1.33.2.6 yamt /* Maximum time to give a single EC command in s */
84 1.33.2.6 yamt #define EC_CMD_TIMEOUT 10
85 1.33.2.6 yamt
86 1.33.2.6 yamt /* From ACPI 3.0b, chapter 12.3 */
87 1.33.2.6 yamt #define EC_COMMAND_READ 0x80
88 1.33.2.6 yamt #define EC_COMMAND_WRITE 0x81
89 1.33.2.6 yamt #define EC_COMMAND_BURST_EN 0x82
90 1.33.2.6 yamt #define EC_COMMAND_BURST_DIS 0x83
91 1.33.2.6 yamt #define EC_COMMAND_QUERY 0x84
92 1.33.2.6 yamt
93 1.33.2.6 yamt /* From ACPI 3.0b, chapter 12.2.1 */
94 1.33.2.6 yamt #define EC_STATUS_OBF 0x01
95 1.33.2.6 yamt #define EC_STATUS_IBF 0x02
96 1.33.2.6 yamt #define EC_STATUS_CMD 0x08
97 1.33.2.6 yamt #define EC_STATUS_BURST 0x10
98 1.33.2.6 yamt #define EC_STATUS_SCI 0x20
99 1.33.2.6 yamt #define EC_STATUS_SMI 0x40
100 1.1 thorpej
101 1.33.2.6 yamt static const char *ec_hid[] = {
102 1.33.2.6 yamt "PNP0C09",
103 1.33.2.6 yamt NULL,
104 1.33.2.6 yamt };
105 1.1 thorpej
106 1.33.2.6 yamt enum ec_state_t {
107 1.33.2.6 yamt EC_STATE_QUERY,
108 1.33.2.6 yamt EC_STATE_QUERY_VAL,
109 1.33.2.6 yamt EC_STATE_READ,
110 1.33.2.6 yamt EC_STATE_READ_ADDR,
111 1.33.2.6 yamt EC_STATE_READ_VAL,
112 1.33.2.6 yamt EC_STATE_WRITE,
113 1.33.2.6 yamt EC_STATE_WRITE_ADDR,
114 1.33.2.6 yamt EC_STATE_WRITE_VAL,
115 1.33.2.6 yamt EC_STATE_FREE
116 1.33.2.6 yamt };
117 1.1 thorpej
118 1.33.2.6 yamt struct acpiec_softc {
119 1.33.2.6 yamt ACPI_HANDLE sc_ech;
120 1.1 thorpej
121 1.33.2.6 yamt ACPI_HANDLE sc_gpeh;
122 1.33.2.6 yamt UINT8 sc_gpebit;
123 1.1 thorpej
124 1.33.2.6 yamt bus_space_tag_t sc_data_st;
125 1.33.2.6 yamt bus_space_handle_t sc_data_sh;
126 1.17 mycroft
127 1.33.2.6 yamt bus_space_tag_t sc_csr_st;
128 1.33.2.6 yamt bus_space_handle_t sc_csr_sh;
129 1.1 thorpej
130 1.33.2.6 yamt bool sc_need_global_lock;
131 1.33.2.6 yamt UINT32 sc_global_lock;
132 1.33.2.6 yamt
133 1.33.2.6 yamt kmutex_t sc_mtx, sc_access_mtx;
134 1.33.2.6 yamt kcondvar_t sc_cv, sc_cv_sci;
135 1.33.2.6 yamt enum ec_state_t sc_state;
136 1.33.2.6 yamt bool sc_got_sci;
137 1.33.2.6 yamt callout_t sc_pseudo_intr;
138 1.33.2.6 yamt
139 1.33.2.6 yamt uint8_t sc_cur_addr, sc_cur_val;
140 1.16 kochi };
141 1.16 kochi
142 1.33.2.6 yamt static int acpiecdt_match(device_t, struct cfdata *, void *);
143 1.33.2.6 yamt static void acpiecdt_attach(device_t, device_t, void *);
144 1.1 thorpej
145 1.33.2.6 yamt static int acpiec_match(device_t, struct cfdata *, void *);
146 1.33.2.6 yamt static void acpiec_attach(device_t, device_t, void *);
147 1.17 mycroft
148 1.33.2.6 yamt static void acpiec_common_attach(device_t, device_t, ACPI_HANDLE,
149 1.33.2.6 yamt bus_addr_t, bus_addr_t, ACPI_HANDLE, uint8_t);
150 1.20 yamt
151 1.33.2.6 yamt static bool acpiec_resume(device_t);
152 1.33.2.6 yamt static bool acpiec_suspend(device_t);
153 1.20 yamt
154 1.33.2.6 yamt static bool acpiec_parse_gpe_package(device_t, ACPI_HANDLE,
155 1.33.2.6 yamt ACPI_HANDLE *, uint8_t *);
156 1.20 yamt
157 1.33.2.6 yamt static void acpiec_callout(void *);
158 1.33.2.6 yamt static void acpiec_gpe_query(void *);
159 1.33.2.6 yamt static UINT32 acpiec_gpe_handler(void *);
160 1.33.2.6 yamt static ACPI_STATUS acpiec_space_setup(ACPI_HANDLE, UINT32, void *, void **);
161 1.33.2.6 yamt static ACPI_STATUS acpiec_space_handler(UINT32, ACPI_PHYSICAL_ADDRESS,
162 1.33.2.6 yamt UINT32, ACPI_INTEGER *, void *, void *);
163 1.20 yamt
164 1.33.2.6 yamt static void acpiec_gpe_state_machine(device_t);
165 1.23 kochi
166 1.33.2.6 yamt CFATTACH_DECL_NEW(acpiec, sizeof(struct acpiec_softc),
167 1.33.2.6 yamt acpiec_match, acpiec_attach, NULL, NULL);
168 1.24 kochi
169 1.33.2.6 yamt CFATTACH_DECL_NEW(acpiecdt, sizeof(struct acpiec_softc),
170 1.33.2.6 yamt acpiecdt_match, acpiecdt_attach, NULL, NULL);
171 1.33.2.6 yamt
172 1.33.2.6 yamt static device_t ec_singleton = NULL;
173 1.33.2.6 yamt static bool acpiec_cold = false;
174 1.4 thorpej
175 1.33.2.6 yamt static bool
176 1.33.2.6 yamt acpiecdt_find(device_t parent, ACPI_HANDLE *ec_handle,
177 1.33.2.6 yamt bus_addr_t *cmd_reg, bus_addr_t *data_reg, uint8_t *gpebit)
178 1.1 thorpej {
179 1.33.2.6 yamt ACPI_TABLE_ECDT *ecdt;
180 1.25 kochi ACPI_STATUS rv;
181 1.1 thorpej
182 1.33.2.6 yamt rv = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
183 1.33.2.6 yamt if (ACPI_FAILURE(rv))
184 1.33.2.6 yamt return false;
185 1.33.2.6 yamt
186 1.33.2.6 yamt if (ecdt->Control.BitWidth != 8 || ecdt->Data.BitWidth != 8) {
187 1.33.2.6 yamt aprint_error_dev(parent,
188 1.33.2.6 yamt "ECDT register width invalid (%d/%d)\n",
189 1.33.2.6 yamt ecdt->Control.BitWidth, ecdt->Data.BitWidth);
190 1.33.2.6 yamt return false;
191 1.17 mycroft }
192 1.20 yamt
193 1.33.2.6 yamt rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ecdt->Id, ec_handle);
194 1.33.2.6 yamt if (ACPI_FAILURE(rv)) {
195 1.33.2.6 yamt aprint_error_dev(parent,
196 1.33.2.6 yamt "failed to look up EC object %s: %s\n",
197 1.33.2.6 yamt ecdt->Id, AcpiFormatException(rv));
198 1.33.2.6 yamt return false;
199 1.33.2.6 yamt }
200 1.33.2.6 yamt
201 1.33.2.6 yamt *cmd_reg = ecdt->Control.Address;
202 1.33.2.6 yamt *data_reg = ecdt->Data.Address;
203 1.33.2.6 yamt *gpebit = ecdt->Gpe;
204 1.33.2.6 yamt
205 1.33.2.6 yamt return true;
206 1.1 thorpej }
207 1.1 thorpej
208 1.33.2.6 yamt static int
209 1.33.2.6 yamt acpiecdt_match(device_t parent, struct cfdata *match, void *aux)
210 1.1 thorpej {
211 1.33.2.6 yamt ACPI_HANDLE ec_handle;
212 1.33.2.6 yamt bus_addr_t cmd_reg, data_reg;
213 1.33.2.6 yamt uint8_t gpebit;
214 1.20 yamt
215 1.33.2.6 yamt if (acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
216 1.33.2.6 yamt return 1;
217 1.33.2.6 yamt else
218 1.33.2.6 yamt return 0;
219 1.33.2.6 yamt }
220 1.33.2.6 yamt
221 1.33.2.6 yamt static void
222 1.33.2.6 yamt acpiecdt_attach(device_t parent, device_t self, void *aux)
223 1.33.2.6 yamt {
224 1.33.2.6 yamt ACPI_HANDLE ec_handle;
225 1.33.2.6 yamt bus_addr_t cmd_reg, data_reg;
226 1.33.2.6 yamt uint8_t gpebit;
227 1.33.2.6 yamt
228 1.33.2.6 yamt if (!acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
229 1.33.2.6 yamt panic("ECDT disappeared");
230 1.33.2.6 yamt
231 1.33.2.6 yamt aprint_naive(": ACPI Embedded Controller via ECDT\n");
232 1.33.2.6 yamt aprint_normal(": ACPI Embedded Controller via ECDT\n");
233 1.33.2.6 yamt
234 1.33.2.6 yamt acpiec_common_attach(parent, self, ec_handle, cmd_reg, data_reg,
235 1.33.2.6 yamt NULL, gpebit);
236 1.1 thorpej }
237 1.1 thorpej
238 1.31 kochi static int
239 1.33.2.6 yamt acpiec_match(device_t parent, struct cfdata *match, void *aux)
240 1.1 thorpej {
241 1.1 thorpej struct acpi_attach_args *aa = aux;
242 1.1 thorpej
243 1.1 thorpej if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
244 1.25 kochi return 0;
245 1.1 thorpej
246 1.25 kochi return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid);
247 1.1 thorpej }
248 1.1 thorpej
249 1.33.2.6 yamt static void
250 1.33.2.6 yamt acpiec_attach(device_t parent, device_t self, void *aux)
251 1.17 mycroft {
252 1.33.2.6 yamt struct acpi_attach_args *aa = aux;
253 1.33.2.6 yamt struct acpi_resources ec_res;
254 1.33.2.6 yamt struct acpi_io *io0, *io1;
255 1.33.2.6 yamt ACPI_HANDLE gpe_handle;
256 1.33.2.6 yamt uint8_t gpebit;
257 1.23 kochi ACPI_STATUS rv;
258 1.17 mycroft
259 1.33.2.6 yamt if (ec_singleton != NULL) {
260 1.33.2.6 yamt aprint_naive(": ACPI Embedded Controller (disabled)\n");
261 1.33.2.6 yamt aprint_normal(": ACPI Embedded Controller (disabled)\n");
262 1.33.2.6 yamt if (!pmf_device_register(self, NULL, NULL))
263 1.33.2.6 yamt aprint_error_dev(self, "couldn't establish power handler\n");
264 1.17 mycroft return;
265 1.33.2.6 yamt }
266 1.17 mycroft
267 1.33.2.6 yamt aprint_naive(": ACPI Embedded Controller\n");
268 1.33.2.6 yamt aprint_normal(": ACPI Embedded Controller\n");
269 1.33.2.6 yamt
270 1.33.2.6 yamt if (!acpiec_parse_gpe_package(self, aa->aa_node->ad_handle,
271 1.33.2.6 yamt &gpe_handle, &gpebit))
272 1.17 mycroft return;
273 1.17 mycroft
274 1.33.2.6 yamt rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
275 1.33.2.6 yamt &ec_res, &acpi_resource_parse_ops_default);
276 1.33.2.6 yamt if (rv != AE_OK) {
277 1.33.2.6 yamt aprint_error_dev(self, "resource parsing failed: %s\n",
278 1.33.2.6 yamt AcpiFormatException(rv));
279 1.23 kochi return;
280 1.23 kochi }
281 1.23 kochi
282 1.33.2.6 yamt if ((io0 = acpi_res_io(&ec_res, 0)) == NULL) {
283 1.33.2.6 yamt aprint_error_dev(self, "no data register resource\n");
284 1.33.2.6 yamt goto free_res;
285 1.33.2.6 yamt }
286 1.33.2.6 yamt if ((io1 = acpi_res_io(&ec_res, 1)) == NULL) {
287 1.33.2.6 yamt aprint_error_dev(self, "no CSR register resource\n");
288 1.33.2.6 yamt goto free_res;
289 1.33.2.6 yamt }
290 1.23 kochi
291 1.33.2.6 yamt acpiec_common_attach(parent, self, aa->aa_node->ad_handle,
292 1.33.2.6 yamt io1->ar_base, io0->ar_base, gpe_handle, gpebit);
293 1.23 kochi
294 1.33.2.6 yamt free_res:
295 1.33.2.6 yamt acpi_resource_cleanup(&ec_res);
296 1.33.2.6 yamt }
297 1.23 kochi
298 1.33.2.6 yamt static void
299 1.33.2.6 yamt acpiec_common_attach(device_t parent, device_t self,
300 1.33.2.6 yamt ACPI_HANDLE ec_handle, bus_addr_t cmd_reg, bus_addr_t data_reg,
301 1.33.2.6 yamt ACPI_HANDLE gpe_handle, uint8_t gpebit)
302 1.33.2.6 yamt {
303 1.33.2.6 yamt struct acpiec_softc *sc = device_private(self);
304 1.33.2.6 yamt ACPI_STATUS rv;
305 1.33.2.6 yamt ACPI_INTEGER val;
306 1.23 kochi
307 1.33.2.6 yamt sc->sc_ech = ec_handle;
308 1.33.2.6 yamt sc->sc_gpeh = gpe_handle;
309 1.33.2.6 yamt sc->sc_gpebit = gpebit;
310 1.33.2.6 yamt
311 1.33.2.6 yamt sc->sc_state = EC_STATE_FREE;
312 1.33.2.6 yamt mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_TTY);
313 1.33.2.6 yamt mutex_init(&sc->sc_access_mtx, MUTEX_DEFAULT, IPL_NONE);
314 1.33.2.6 yamt cv_init(&sc->sc_cv, "eccv");
315 1.33.2.6 yamt cv_init(&sc->sc_cv_sci, "ecsci");
316 1.33.2.6 yamt
317 1.33.2.6 yamt if (bus_space_map(sc->sc_data_st, data_reg, 1, 0,
318 1.33.2.6 yamt &sc->sc_data_sh) != 0) {
319 1.33.2.6 yamt aprint_error_dev(self, "unable to map data register\n");
320 1.33.2.6 yamt return;
321 1.23 kochi }
322 1.23 kochi
323 1.33.2.6 yamt if (bus_space_map(sc->sc_csr_st, cmd_reg, 1, 0, &sc->sc_csr_sh) != 0) {
324 1.33.2.6 yamt aprint_error_dev(self, "unable to map CSR register\n");
325 1.33.2.6 yamt goto post_data_map;
326 1.33 kochi }
327 1.33 kochi
328 1.33.2.6 yamt rv = acpi_eval_integer(sc->sc_ech, "_GLK", &val);
329 1.33.2.6 yamt if (rv == AE_OK) {
330 1.33.2.6 yamt sc->sc_need_global_lock = val != 0;
331 1.33.2.6 yamt } else if (rv != AE_NOT_FOUND) {
332 1.33.2.6 yamt aprint_error_dev(self, "unable to evaluate _GLK: %s\n",
333 1.33.2.6 yamt AcpiFormatException(rv));
334 1.33.2.6 yamt goto post_csr_map;
335 1.33.2.6 yamt } else {
336 1.33.2.6 yamt sc->sc_need_global_lock = false;
337 1.33 kochi }
338 1.33.2.6 yamt if (sc->sc_need_global_lock)
339 1.33.2.6 yamt aprint_normal_dev(self, "using global ACPI lock\n");
340 1.33 kochi
341 1.33.2.6 yamt callout_init(&sc->sc_pseudo_intr, CALLOUT_MPSAFE);
342 1.33.2.6 yamt callout_setfunc(&sc->sc_pseudo_intr, acpiec_callout, self);
343 1.33.2.6 yamt
344 1.33.2.6 yamt rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC,
345 1.33.2.6 yamt acpiec_space_handler, acpiec_space_setup, self);
346 1.33.2.6 yamt if (rv != AE_OK) {
347 1.33.2.6 yamt aprint_error_dev(self,
348 1.33.2.6 yamt "unable to install address space handler: %s\n",
349 1.23 kochi AcpiFormatException(rv));
350 1.33.2.6 yamt goto post_csr_map;
351 1.17 mycroft }
352 1.23 kochi
353 1.33.2.6 yamt rv = AcpiInstallGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
354 1.33.2.6 yamt ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, self);
355 1.33.2.6 yamt if (rv != AE_OK) {
356 1.33.2.6 yamt aprint_error_dev(self, "unable to install GPE handler: %s\n",
357 1.33.2.6 yamt AcpiFormatException(rv));
358 1.33.2.6 yamt goto post_csr_map;
359 1.33.2.6 yamt }
360 1.23 kochi
361 1.33.2.6 yamt rv = AcpiSetGpeType(sc->sc_gpeh, sc->sc_gpebit, ACPI_GPE_TYPE_RUNTIME);
362 1.33.2.6 yamt if (rv != AE_OK) {
363 1.33.2.6 yamt aprint_error_dev(self, "unable to set GPE type: %s\n",
364 1.33.2.6 yamt AcpiFormatException(rv));
365 1.33.2.6 yamt goto post_csr_map;
366 1.33.2.6 yamt }
367 1.23 kochi
368 1.33.2.6 yamt rv = AcpiEnableGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
369 1.33.2.6 yamt if (rv != AE_OK) {
370 1.33.2.6 yamt aprint_error_dev(self, "unable to enable GPE: %s\n",
371 1.33.2.6 yamt AcpiFormatException(rv));
372 1.33.2.6 yamt goto post_csr_map;
373 1.33.2.6 yamt }
374 1.33.2.6 yamt
375 1.33.2.6 yamt if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, acpiec_gpe_query,
376 1.33.2.6 yamt self, NULL, "acpiec sci thread")) {
377 1.33.2.6 yamt aprint_error_dev(self, "unable to create query kthread\n");
378 1.33.2.6 yamt goto post_csr_map;
379 1.33.2.6 yamt }
380 1.23 kochi
381 1.33.2.6 yamt ec_singleton = self;
382 1.23 kochi
383 1.33.2.6 yamt if (!pmf_device_register(self, acpiec_suspend, acpiec_resume))
384 1.33.2.6 yamt aprint_error_dev(self, "couldn't establish power handler\n");
385 1.23 kochi
386 1.23 kochi return;
387 1.33.2.6 yamt
388 1.33.2.6 yamt post_csr_map:
389 1.33.2.6 yamt (void)AcpiRemoveGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
390 1.33.2.6 yamt acpiec_gpe_handler);
391 1.33.2.6 yamt (void)AcpiRemoveAddressSpaceHandler(sc->sc_ech,
392 1.33.2.6 yamt ACPI_ADR_SPACE_EC, acpiec_space_handler);
393 1.33.2.6 yamt bus_space_unmap(sc->sc_csr_st, sc->sc_csr_sh, 1);
394 1.33.2.6 yamt post_data_map:
395 1.33.2.6 yamt bus_space_unmap(sc->sc_data_st, sc->sc_data_sh, 1);
396 1.17 mycroft }
397 1.17 mycroft
398 1.33.2.6 yamt static bool
399 1.33.2.6 yamt acpiec_suspend(device_t dv)
400 1.1 thorpej {
401 1.33.2.6 yamt acpiec_cold = true;
402 1.1 thorpej
403 1.33.2.6 yamt return true;
404 1.33.2.6 yamt }
405 1.1 thorpej
406 1.33.2.6 yamt static bool
407 1.33.2.6 yamt acpiec_resume(device_t dv)
408 1.33.2.6 yamt {
409 1.33.2.6 yamt acpiec_cold = false;
410 1.1 thorpej
411 1.33.2.6 yamt return true;
412 1.33.2.6 yamt }
413 1.17 mycroft
414 1.33.2.6 yamt static bool
415 1.33.2.6 yamt acpiec_parse_gpe_package(device_t self, ACPI_HANDLE ec_handle,
416 1.33.2.6 yamt ACPI_HANDLE *gpe_handle, uint8_t *gpebit)
417 1.33.2.6 yamt {
418 1.33.2.6 yamt ACPI_BUFFER buf;
419 1.33.2.6 yamt ACPI_OBJECT *p, *c;
420 1.33.2.6 yamt ACPI_STATUS rv;
421 1.1 thorpej
422 1.33.2.6 yamt rv = acpi_eval_struct(ec_handle, "_GPE", &buf);
423 1.33.2.6 yamt if (rv != AE_OK) {
424 1.33.2.6 yamt aprint_error_dev(self, "unable to evaluate _GPE: %s\n",
425 1.33.2.6 yamt AcpiFormatException(rv));
426 1.33.2.6 yamt return false;
427 1.33.2.6 yamt }
428 1.1 thorpej
429 1.33.2.6 yamt p = buf.Pointer;
430 1.23 kochi
431 1.33.2.6 yamt if (p->Type == ACPI_TYPE_INTEGER) {
432 1.33.2.6 yamt *gpe_handle = NULL;
433 1.33.2.6 yamt *gpebit = p->Integer.Value;
434 1.33.2.6 yamt AcpiOsFree(p);
435 1.33.2.6 yamt return true;
436 1.33.2.6 yamt }
437 1.23 kochi
438 1.33.2.6 yamt if (p->Type != ACPI_TYPE_PACKAGE) {
439 1.33.2.6 yamt aprint_error_dev(self, "_GPE is neither integer nor package\n");
440 1.33.2.6 yamt AcpiOsFree(p);
441 1.33.2.6 yamt return false;
442 1.33.2.6 yamt }
443 1.33.2.6 yamt
444 1.33.2.6 yamt if (p->Package.Count != 2) {
445 1.33.2.6 yamt aprint_error_dev(self, "_GPE package does not contain 2 elements\n");
446 1.33.2.6 yamt AcpiOsFree(p);
447 1.33.2.6 yamt return false;
448 1.1 thorpej }
449 1.1 thorpej
450 1.33.2.6 yamt c = &p->Package.Elements[0];
451 1.33.2.6 yamt switch (c->Type) {
452 1.33.2.6 yamt case ACPI_TYPE_LOCAL_REFERENCE:
453 1.33.2.6 yamt case ACPI_TYPE_ANY:
454 1.33.2.6 yamt *gpe_handle = c->Reference.Handle;
455 1.33.2.6 yamt break;
456 1.33.2.6 yamt case ACPI_TYPE_STRING:
457 1.33.2.6 yamt /* XXX should be using real scope here */
458 1.33.2.6 yamt rv = AcpiGetHandle(NULL, p->String.Pointer, gpe_handle);
459 1.33.2.6 yamt if (rv != AE_OK) {
460 1.33.2.6 yamt aprint_error_dev(self,
461 1.33.2.6 yamt "_GPE device reference unresolvable\n");
462 1.33.2.6 yamt AcpiOsFree(p);
463 1.33.2.6 yamt return false;
464 1.33.2.6 yamt }
465 1.33.2.6 yamt break;
466 1.33.2.6 yamt default:
467 1.33.2.6 yamt aprint_error_dev(self, "_GPE device reference incorrect\n");
468 1.33.2.6 yamt AcpiOsFree(p);
469 1.33.2.6 yamt return false;
470 1.33.2.6 yamt }
471 1.33.2.6 yamt c = &p->Package.Elements[1];
472 1.33.2.6 yamt if (c->Type != ACPI_TYPE_INTEGER) {
473 1.33.2.6 yamt aprint_error_dev(self,
474 1.33.2.6 yamt "_GPE package needs integer as 2nd field\n");
475 1.33.2.6 yamt AcpiOsFree(p);
476 1.33.2.6 yamt return false;
477 1.33.2.6 yamt }
478 1.33.2.6 yamt *gpebit = c->Integer.Value;
479 1.33.2.6 yamt AcpiOsFree(p);
480 1.33.2.6 yamt return true;
481 1.33.2.6 yamt }
482 1.23 kochi
483 1.33.2.6 yamt static uint8_t
484 1.33.2.6 yamt acpiec_read_data(struct acpiec_softc *sc)
485 1.33.2.6 yamt {
486 1.33.2.6 yamt return bus_space_read_1(sc->sc_data_st, sc->sc_data_sh, 0);
487 1.33.2.6 yamt }
488 1.17 mycroft
489 1.33.2.6 yamt static void
490 1.33.2.6 yamt acpiec_write_data(struct acpiec_softc *sc, uint8_t val)
491 1.33.2.6 yamt {
492 1.33.2.6 yamt bus_space_write_1(sc->sc_data_st, sc->sc_data_sh, 0, val);
493 1.33.2.6 yamt }
494 1.1 thorpej
495 1.33.2.6 yamt static uint8_t
496 1.33.2.6 yamt acpiec_read_status(struct acpiec_softc *sc)
497 1.33.2.6 yamt {
498 1.33.2.6 yamt return bus_space_read_1(sc->sc_csr_st, sc->sc_csr_sh, 0);
499 1.33.2.6 yamt }
500 1.33 kochi
501 1.33.2.6 yamt static void
502 1.33.2.6 yamt acpiec_write_command(struct acpiec_softc *sc, uint8_t cmd)
503 1.33.2.6 yamt {
504 1.33.2.6 yamt bus_space_write_1(sc->sc_csr_st, sc->sc_csr_sh, 0, cmd);
505 1.33.2.6 yamt }
506 1.33 kochi
507 1.33.2.6 yamt static ACPI_STATUS
508 1.33.2.6 yamt acpiec_space_setup(ACPI_HANDLE region, UINT32 func, void *arg,
509 1.33.2.6 yamt void **region_arg)
510 1.33.2.6 yamt {
511 1.33.2.6 yamt if (func == ACPI_REGION_DEACTIVATE)
512 1.33.2.6 yamt *region_arg = NULL;
513 1.33.2.6 yamt else
514 1.33.2.6 yamt *region_arg = arg;
515 1.1 thorpej
516 1.33.2.6 yamt return AE_OK;
517 1.1 thorpej }
518 1.1 thorpej
519 1.1 thorpej static void
520 1.33.2.6 yamt acpiec_lock(device_t dv)
521 1.1 thorpej {
522 1.33.2.6 yamt struct acpiec_softc *sc = device_private(dv);
523 1.25 kochi ACPI_STATUS rv;
524 1.1 thorpej
525 1.33.2.6 yamt mutex_enter(&sc->sc_access_mtx);
526 1.1 thorpej
527 1.33.2.6 yamt if (sc->sc_need_global_lock) {
528 1.33.2.6 yamt rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->sc_global_lock);
529 1.33.2.6 yamt if (rv != AE_OK) {
530 1.33.2.6 yamt aprint_error_dev(dv, "failed to acquire global lock: %s\n",
531 1.25 kochi AcpiFormatException(rv));
532 1.33.2.6 yamt return;
533 1.1 thorpej }
534 1.1 thorpej }
535 1.33.2.6 yamt }
536 1.1 thorpej
537 1.33.2.6 yamt static void
538 1.33.2.6 yamt acpiec_unlock(device_t dv)
539 1.33.2.6 yamt {
540 1.33.2.6 yamt struct acpiec_softc *sc = device_private(dv);
541 1.33.2.6 yamt ACPI_STATUS rv;
542 1.1 thorpej
543 1.33.2.6 yamt if (sc->sc_need_global_lock) {
544 1.33.2.6 yamt rv = AcpiReleaseGlobalLock(sc->sc_global_lock);
545 1.33.2.6 yamt if (rv != AE_OK) {
546 1.33.2.6 yamt aprint_error_dev(dv, "failed to release global lock: %s\n",
547 1.33.2.6 yamt AcpiFormatException(rv));
548 1.33.2.6 yamt }
549 1.33.2.6 yamt }
550 1.33.2.6 yamt mutex_exit(&sc->sc_access_mtx);
551 1.1 thorpej }
552 1.1 thorpej
553 1.33.2.6 yamt static ACPI_STATUS
554 1.33.2.6 yamt acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
555 1.1 thorpej {
556 1.33.2.6 yamt struct acpiec_softc *sc = device_private(dv);
557 1.33.2.6 yamt int i, timeo = 1000 * EC_CMD_TIMEOUT;
558 1.1 thorpej
559 1.33.2.6 yamt acpiec_lock(dv);
560 1.33.2.6 yamt mutex_enter(&sc->sc_mtx);
561 1.33.2.6 yamt
562 1.33.2.6 yamt sc->sc_cur_addr = addr;
563 1.33.2.6 yamt sc->sc_state = EC_STATE_READ;
564 1.33.2.6 yamt
565 1.33.2.6 yamt for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
566 1.33.2.6 yamt acpiec_gpe_state_machine(dv);
567 1.33.2.6 yamt if (sc->sc_state == EC_STATE_FREE)
568 1.33.2.6 yamt goto done;
569 1.33.2.6 yamt delay(1);
570 1.33.2.6 yamt }
571 1.33.2.6 yamt
572 1.33.2.6 yamt if (cold || acpiec_cold) {
573 1.33.2.6 yamt while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
574 1.33.2.6 yamt delay(1000);
575 1.33.2.6 yamt acpiec_gpe_state_machine(dv);
576 1.1 thorpej }
577 1.33.2.6 yamt if (sc->sc_state != EC_STATE_FREE) {
578 1.33.2.6 yamt mutex_exit(&sc->sc_mtx);
579 1.33.2.6 yamt AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
580 1.33.2.6 yamt acpiec_unlock(dv);
581 1.33.2.6 yamt aprint_error_dev(dv, "command timed out, state %d\n",
582 1.33.2.6 yamt sc->sc_state);
583 1.33.2.6 yamt return AE_ERROR;
584 1.33.2.6 yamt }
585 1.33.2.6 yamt } else while (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
586 1.33.2.6 yamt mutex_exit(&sc->sc_mtx);
587 1.33.2.6 yamt AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
588 1.33.2.6 yamt acpiec_unlock(dv);
589 1.33.2.6 yamt aprint_error_dev(dv, "command takes over %d sec...\n", EC_CMD_TIMEOUT);
590 1.33.2.6 yamt return AE_ERROR;
591 1.1 thorpej }
592 1.33 kochi
593 1.33.2.6 yamt done:
594 1.33.2.6 yamt *val = sc->sc_cur_val;
595 1.33.2.6 yamt
596 1.33.2.6 yamt mutex_exit(&sc->sc_mtx);
597 1.33.2.6 yamt acpiec_unlock(dv);
598 1.33.2.6 yamt return AE_OK;
599 1.1 thorpej }
600 1.1 thorpej
601 1.1 thorpej static ACPI_STATUS
602 1.33.2.6 yamt acpiec_write(device_t dv, uint8_t addr, uint8_t val)
603 1.1 thorpej {
604 1.33.2.6 yamt struct acpiec_softc *sc = device_private(dv);
605 1.33.2.6 yamt int i, timeo = 1000 * EC_CMD_TIMEOUT;
606 1.1 thorpej
607 1.33.2.6 yamt acpiec_lock(dv);
608 1.33.2.6 yamt mutex_enter(&sc->sc_mtx);
609 1.1 thorpej
610 1.33.2.6 yamt sc->sc_cur_addr = addr;
611 1.33.2.6 yamt sc->sc_cur_val = val;
612 1.33.2.6 yamt sc->sc_state = EC_STATE_WRITE;
613 1.1 thorpej
614 1.33.2.6 yamt for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
615 1.33.2.6 yamt acpiec_gpe_state_machine(dv);
616 1.33.2.6 yamt if (sc->sc_state == EC_STATE_FREE)
617 1.33.2.6 yamt goto done;
618 1.33.2.6 yamt delay(1);
619 1.33.2.6 yamt }
620 1.33.2.6 yamt
621 1.33.2.6 yamt if (cold || acpiec_cold) {
622 1.33.2.6 yamt while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
623 1.33.2.6 yamt delay(1000);
624 1.33.2.6 yamt acpiec_gpe_state_machine(dv);
625 1.33.2.6 yamt }
626 1.33.2.6 yamt if (sc->sc_state != EC_STATE_FREE) {
627 1.33.2.6 yamt mutex_exit(&sc->sc_mtx);
628 1.33.2.6 yamt AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
629 1.33.2.6 yamt acpiec_unlock(dv);
630 1.33.2.6 yamt aprint_error_dev(dv, "command timed out, state %d\n",
631 1.33.2.6 yamt sc->sc_state);
632 1.33.2.6 yamt return AE_ERROR;
633 1.33.2.6 yamt }
634 1.33.2.6 yamt } else while (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
635 1.33.2.6 yamt mutex_exit(&sc->sc_mtx);
636 1.33.2.6 yamt AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
637 1.33.2.6 yamt acpiec_unlock(dv);
638 1.33.2.6 yamt aprint_error_dev(dv, "command takes over %d sec...\n", EC_CMD_TIMEOUT);
639 1.33.2.6 yamt return AE_ERROR;
640 1.33.2.6 yamt }
641 1.33.2.6 yamt
642 1.33.2.6 yamt done:
643 1.33.2.6 yamt mutex_exit(&sc->sc_mtx);
644 1.33.2.6 yamt acpiec_unlock(dv);
645 1.33.2.6 yamt return AE_OK;
646 1.1 thorpej }
647 1.1 thorpej
648 1.1 thorpej static ACPI_STATUS
649 1.33.2.6 yamt acpiec_space_handler(UINT32 func, ACPI_PHYSICAL_ADDRESS paddr,
650 1.33.2.6 yamt UINT32 width, ACPI_INTEGER *value, void *arg, void *region_arg)
651 1.1 thorpej {
652 1.33.2.6 yamt device_t dv;
653 1.33.2.6 yamt struct acpiec_softc *sc;
654 1.33.2.6 yamt ACPI_STATUS rv;
655 1.33.2.6 yamt uint8_t addr, reg;
656 1.33.2.6 yamt unsigned int i;
657 1.1 thorpej
658 1.33.2.6 yamt if (paddr > 0xff || width % 8 != 0 || value == NULL || arg == NULL ||
659 1.33.2.6 yamt paddr + width / 8 > 0xff)
660 1.33.2.6 yamt return AE_BAD_PARAMETER;
661 1.1 thorpej
662 1.33.2.6 yamt addr = paddr;
663 1.33.2.6 yamt dv = arg;
664 1.33.2.6 yamt sc = device_private(dv);
665 1.1 thorpej
666 1.33.2.6 yamt rv = AE_OK;
667 1.33.2.6 yamt
668 1.33.2.6 yamt switch (func) {
669 1.4 thorpej case ACPI_READ:
670 1.33.2.6 yamt *value = 0;
671 1.33.2.6 yamt for (i = 0; i < width; i += 8, ++addr) {
672 1.33.2.6 yamt rv = acpiec_read(dv, addr, ®);
673 1.33.2.6 yamt if (rv != AE_OK)
674 1.33.2.6 yamt break;
675 1.33.2.6 yamt *value |= (ACPI_INTEGER)reg << i;
676 1.33.2.6 yamt }
677 1.1 thorpej break;
678 1.4 thorpej case ACPI_WRITE:
679 1.33.2.6 yamt for (i = 0; i < width; i += 8, ++addr) {
680 1.33.2.6 yamt reg = (*value >>i) & 0xff;
681 1.33.2.6 yamt rv = acpiec_write(dv, addr, reg);
682 1.33.2.6 yamt if (rv != AE_OK)
683 1.33.2.6 yamt break;
684 1.33.2.6 yamt }
685 1.1 thorpej break;
686 1.1 thorpej default:
687 1.33.2.6 yamt aprint_error("%s: invalid Address Space function called: %x\n",
688 1.33.2.6 yamt device_xname(dv), (unsigned int)func);
689 1.33.2.6 yamt return AE_BAD_PARAMETER;
690 1.1 thorpej }
691 1.1 thorpej
692 1.33.2.6 yamt return rv;
693 1.1 thorpej }
694 1.1 thorpej
695 1.33.2.6 yamt static void
696 1.33.2.6 yamt acpiec_gpe_query(void *arg)
697 1.1 thorpej {
698 1.33.2.6 yamt device_t dv = arg;
699 1.33.2.6 yamt struct acpiec_softc *sc = device_private(dv);
700 1.33.2.6 yamt uint8_t reg;
701 1.33.2.6 yamt char qxx[5];
702 1.33.2.6 yamt ACPI_STATUS rv;
703 1.1 thorpej int i;
704 1.1 thorpej
705 1.33.2.6 yamt loop:
706 1.33.2.6 yamt mutex_enter(&sc->sc_mtx);
707 1.1 thorpej
708 1.33.2.6 yamt if (sc->sc_got_sci == false)
709 1.33.2.6 yamt cv_wait(&sc->sc_cv_sci, &sc->sc_mtx);
710 1.33.2.6 yamt mutex_exit(&sc->sc_mtx);
711 1.1 thorpej
712 1.33.2.6 yamt acpiec_lock(dv);
713 1.33.2.6 yamt mutex_enter(&sc->sc_mtx);
714 1.1 thorpej
715 1.33.2.6 yamt /* The Query command can always be issued, so be defensive here. */
716 1.33.2.6 yamt sc->sc_got_sci = false;
717 1.33.2.6 yamt sc->sc_state = EC_STATE_QUERY;
718 1.1 thorpej
719 1.33.2.6 yamt for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
720 1.33.2.6 yamt acpiec_gpe_state_machine(dv);
721 1.33.2.6 yamt if (sc->sc_state == EC_STATE_FREE)
722 1.33.2.6 yamt goto done;
723 1.33.2.6 yamt delay(1);
724 1.33.2.6 yamt }
725 1.1 thorpej
726 1.33.2.6 yamt cv_wait(&sc->sc_cv, &sc->sc_mtx);
727 1.33.2.6 yamt
728 1.33.2.6 yamt done:
729 1.33.2.6 yamt reg = sc->sc_cur_val;
730 1.1 thorpej
731 1.33.2.6 yamt mutex_exit(&sc->sc_mtx);
732 1.33.2.6 yamt acpiec_unlock(dv);
733 1.1 thorpej
734 1.33.2.6 yamt if (reg == 0)
735 1.33.2.6 yamt goto loop; /* Spurious query result */
736 1.1 thorpej
737 1.33.2.6 yamt /*
738 1.33.2.6 yamt * Evaluate _Qxx to respond to the controller.
739 1.33.2.6 yamt */
740 1.33.2.6 yamt snprintf(qxx, sizeof(qxx), "_Q%02X", (unsigned int)reg);
741 1.33.2.6 yamt rv = AcpiEvaluateObject(sc->sc_ech, qxx, NULL, NULL);
742 1.33.2.6 yamt if (rv != AE_OK && rv != AE_NOT_FOUND) {
743 1.33.2.6 yamt aprint_error("%s: GPE query method %s failed: %s",
744 1.33.2.6 yamt device_xname(dv), qxx, AcpiFormatException(rv));
745 1.1 thorpej }
746 1.1 thorpej
747 1.33.2.6 yamt goto loop;
748 1.24 kochi }
749 1.1 thorpej
750 1.33.2.6 yamt static void
751 1.33.2.6 yamt acpiec_gpe_state_machine(device_t dv)
752 1.1 thorpej {
753 1.33.2.6 yamt struct acpiec_softc *sc = device_private(dv);
754 1.33.2.6 yamt uint8_t reg;
755 1.1 thorpej
756 1.33.2.6 yamt reg = acpiec_read_status(sc);
757 1.1 thorpej
758 1.33.2.6 yamt if (reg & EC_STATUS_SCI)
759 1.33.2.6 yamt sc->sc_got_sci = true;
760 1.33.2.6 yamt
761 1.33.2.6 yamt switch (sc->sc_state) {
762 1.33.2.6 yamt case EC_STATE_QUERY:
763 1.33.2.6 yamt if ((reg & EC_STATUS_IBF) != 0)
764 1.33.2.6 yamt break; /* Nothing of interest here. */
765 1.33.2.6 yamt acpiec_write_command(sc, EC_COMMAND_QUERY);
766 1.33.2.6 yamt sc->sc_state = EC_STATE_QUERY_VAL;
767 1.33.2.6 yamt break;
768 1.1 thorpej
769 1.33.2.6 yamt case EC_STATE_QUERY_VAL:
770 1.33.2.6 yamt if ((reg & EC_STATUS_OBF) == 0)
771 1.33.2.6 yamt break; /* Nothing of interest here. */
772 1.1 thorpej
773 1.33.2.6 yamt sc->sc_cur_val = acpiec_read_data(sc);
774 1.33.2.6 yamt sc->sc_state = EC_STATE_FREE;
775 1.1 thorpej
776 1.33.2.6 yamt cv_signal(&sc->sc_cv);
777 1.33.2.6 yamt break;
778 1.1 thorpej
779 1.33.2.6 yamt case EC_STATE_READ:
780 1.33.2.6 yamt if ((reg & EC_STATUS_IBF) != 0)
781 1.33.2.6 yamt break; /* Nothing of interest here. */
782 1.33.2.6 yamt
783 1.33.2.6 yamt acpiec_write_command(sc, EC_COMMAND_READ);
784 1.33.2.6 yamt sc->sc_state = EC_STATE_READ_ADDR;
785 1.1 thorpej break;
786 1.1 thorpej
787 1.33.2.6 yamt case EC_STATE_READ_ADDR:
788 1.33.2.6 yamt if ((reg & EC_STATUS_IBF) != 0)
789 1.33.2.6 yamt break; /* Nothing of interest here. */
790 1.33.2.6 yamt
791 1.33.2.6 yamt acpiec_write_data(sc, sc->sc_cur_addr);
792 1.33.2.6 yamt sc->sc_state = EC_STATE_READ_VAL;
793 1.1 thorpej break;
794 1.1 thorpej
795 1.33.2.6 yamt case EC_STATE_READ_VAL:
796 1.33.2.6 yamt if ((reg & EC_STATUS_OBF) == 0)
797 1.33.2.6 yamt break; /* Nothing of interest here. */
798 1.33.2.6 yamt sc->sc_cur_val = acpiec_read_data(sc);
799 1.33.2.6 yamt sc->sc_state = EC_STATE_FREE;
800 1.33.2.6 yamt
801 1.33.2.6 yamt cv_signal(&sc->sc_cv);
802 1.1 thorpej break;
803 1.1 thorpej
804 1.33.2.6 yamt case EC_STATE_WRITE:
805 1.33.2.6 yamt if ((reg & EC_STATUS_IBF) != 0)
806 1.33.2.6 yamt break; /* Nothing of interest here. */
807 1.1 thorpej
808 1.33.2.6 yamt acpiec_write_command(sc, EC_COMMAND_WRITE);
809 1.33.2.6 yamt sc->sc_state = EC_STATE_WRITE_ADDR;
810 1.33.2.6 yamt break;
811 1.1 thorpej
812 1.33.2.6 yamt case EC_STATE_WRITE_ADDR:
813 1.33.2.6 yamt if ((reg & EC_STATUS_IBF) != 0)
814 1.33.2.6 yamt break; /* Nothing of interest here. */
815 1.33.2.6 yamt acpiec_write_data(sc, sc->sc_cur_addr);
816 1.33.2.6 yamt sc->sc_state = EC_STATE_WRITE_VAL;
817 1.33.2.6 yamt break;
818 1.1 thorpej
819 1.33.2.6 yamt case EC_STATE_WRITE_VAL:
820 1.33.2.6 yamt if ((reg & EC_STATUS_IBF) != 0)
821 1.33.2.6 yamt break; /* Nothing of interest here. */
822 1.33.2.6 yamt sc->sc_state = EC_STATE_FREE;
823 1.33.2.6 yamt cv_signal(&sc->sc_cv);
824 1.1 thorpej
825 1.33.2.6 yamt acpiec_write_data(sc, sc->sc_cur_val);
826 1.33.2.6 yamt break;
827 1.1 thorpej
828 1.33.2.6 yamt case EC_STATE_FREE:
829 1.33.2.6 yamt if (sc->sc_got_sci)
830 1.33.2.6 yamt cv_signal(&sc->sc_cv_sci);
831 1.33.2.6 yamt break;
832 1.33.2.6 yamt default:
833 1.33.2.6 yamt panic("invalid state");
834 1.1 thorpej }
835 1.1 thorpej
836 1.33.2.6 yamt if (sc->sc_state != EC_STATE_FREE)
837 1.33.2.6 yamt callout_schedule(&sc->sc_pseudo_intr, 1);
838 1.33.2.6 yamt }
839 1.1 thorpej
840 1.33.2.6 yamt static void
841 1.33.2.6 yamt acpiec_callout(void *arg)
842 1.33.2.6 yamt {
843 1.33.2.6 yamt device_t dv = arg;
844 1.33.2.6 yamt struct acpiec_softc *sc = device_private(dv);
845 1.1 thorpej
846 1.33.2.6 yamt AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
847 1.1 thorpej
848 1.33.2.6 yamt mutex_enter(&sc->sc_mtx);
849 1.33.2.6 yamt acpiec_gpe_state_machine(dv);
850 1.33.2.6 yamt mutex_exit(&sc->sc_mtx);
851 1.24 kochi }
852 1.1 thorpej
853 1.33.2.6 yamt static UINT32
854 1.33.2.6 yamt acpiec_gpe_handler(void *arg)
855 1.1 thorpej {
856 1.33.2.6 yamt device_t dv = arg;
857 1.33.2.6 yamt struct acpiec_softc *sc = device_private(dv);
858 1.1 thorpej
859 1.33.2.6 yamt AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
860 1.1 thorpej
861 1.33.2.6 yamt mutex_enter(&sc->sc_mtx);
862 1.33.2.6 yamt acpiec_gpe_state_machine(dv);
863 1.33.2.6 yamt mutex_exit(&sc->sc_mtx);
864 1.1 thorpej
865 1.33.2.6 yamt return 0;
866 1.33.2.6 yamt }
867 1.1 thorpej
868 1.33.2.6 yamt ACPI_STATUS
869 1.33.2.6 yamt acpiec_bus_read(device_t dv, u_int addr, ACPI_INTEGER *val, int width)
870 1.33.2.6 yamt {
871 1.33.2.6 yamt return acpiec_space_handler(ACPI_READ, addr, width * 8, val, dv, NULL);
872 1.33.2.6 yamt }
873 1.1 thorpej
874 1.33.2.6 yamt ACPI_STATUS
875 1.33.2.6 yamt acpiec_bus_write(device_t dv, u_int addr, ACPI_INTEGER val, int width)
876 1.33.2.6 yamt {
877 1.33.2.6 yamt return acpiec_space_handler(ACPI_WRITE, addr, width * 8, &val, dv, NULL);
878 1.33.2.6 yamt }
879 1.1 thorpej
880 1.33.2.6 yamt ACPI_HANDLE
881 1.33.2.6 yamt acpiec_get_handle(device_t dv)
882 1.33.2.6 yamt {
883 1.33.2.6 yamt struct acpiec_softc *sc = device_private(dv);
884 1.1 thorpej
885 1.33.2.6 yamt return sc->sc_ech;
886 1.1 thorpej }
887