acpi_ec.c revision 1.37 1 /* $NetBSD: acpi_ec.c,v 1.37 2006/02/20 12:17:49 kochi Exp $ */
2
3 /*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*-
39 * Copyright (c) 2000 Michael Smith
40 * Copyright (c) 2000 BSDi
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 /******************************************************************************
66 *
67 * 1. Copyright Notice
68 *
69 * Some or all of this work - Copyright (c) 1999, Intel Corp. All rights
70 * reserved.
71 *
72 * 2. License
73 *
74 * 2.1. This is your license from Intel Corp. under its intellectual property
75 * rights. You may have additional license terms from the party that provided
76 * you this software, covering your right to use that party's intellectual
77 * property rights.
78 *
79 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
80 * copy of the source code appearing in this file ("Covered Code") an
81 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
82 * base code distributed originally by Intel ("Original Intel Code") to copy,
83 * make derivatives, distribute, use and display any portion of the Covered
84 * Code in any form, with the right to sublicense such rights; and
85 *
86 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
87 * license (with the right to sublicense), under only those claims of Intel
88 * patents that are infringed by the Original Intel Code, to make, use, sell,
89 * offer to sell, and import the Covered Code and derivative works thereof
90 * solely to the minimum extent necessary to exercise the above copyright
91 * license, and in no event shall the patent license extend to any additions
92 * to or modifications of the Original Intel Code. No other license or right
93 * is granted directly or by implication, estoppel or otherwise;
94 *
95 * The above copyright and patent license is granted only if the following
96 * conditions are met:
97 *
98 * 3. Conditions
99 *
100 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
101 * Redistribution of source code of any substantial portion of the Covered
102 * Code or modification with rights to further distribute source must include
103 * the above Copyright Notice, the above License, this list of Conditions,
104 * and the following Disclaimer and Export Compliance provision. In addition,
105 * Licensee must cause all Covered Code to which Licensee contributes to
106 * contain a file documenting the changes Licensee made to create that Covered
107 * Code and the date of any change. Licensee must include in that file the
108 * documentation of any changes made by any predecessor Licensee. Licensee
109 * must include a prominent statement that the modification is derived,
110 * directly or indirectly, from Original Intel Code.
111 *
112 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
113 * Redistribution of source code of any substantial portion of the Covered
114 * Code or modification without rights to further distribute source must
115 * include the following Disclaimer and Export Compliance provision in the
116 * documentation and/or other materials provided with distribution. In
117 * addition, Licensee may not authorize further sublicense of source of any
118 * portion of the Covered Code, and must include terms to the effect that the
119 * license from Licensee to its licensee is limited to the intellectual
120 * property embodied in the software Licensee provides to its licensee, and
121 * not to intellectual property embodied in modifications its licensee may
122 * make.
123 *
124 * 3.3. Redistribution of Executable. Redistribution in executable form of any
125 * substantial portion of the Covered Code or modification must reproduce the
126 * above Copyright Notice, and the following Disclaimer and Export Compliance
127 * provision in the documentation and/or other materials provided with the
128 * distribution.
129 *
130 * 3.4. Intel retains all right, title, and interest in and to the Original
131 * Intel Code.
132 *
133 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
134 * Intel shall be used in advertising or otherwise to promote the sale, use or
135 * other dealings in products derived from or relating to the Covered Code
136 * without prior written authorization from Intel.
137 *
138 * 4. Disclaimer and Export Compliance
139 *
140 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
141 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
142 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
143 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
144 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
145 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
146 * PARTICULAR PURPOSE.
147 *
148 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
149 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
150 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
151 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
152 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
153 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
154 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
155 * LIMITED REMEDY.
156 *
157 * 4.3. Licensee shall not export, either directly or indirectly, any of this
158 * software or system incorporating such software without first obtaining any
159 * required license or other approval from the U. S. Department of Commerce or
160 * any other agency or department of the United States Government. In the
161 * event Licensee exports any such software from the United States or
162 * re-exports any such software from a foreign destination, Licensee shall
163 * ensure that the distribution and export/re-export of the software is in
164 * compliance with all laws, regulations, orders, or other restrictions of the
165 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
166 * any of its subsidiaries will export/re-export any technical data, process,
167 * software, or service, directly or indirectly, to any country for which the
168 * United States government or any agency thereof requires an export license,
169 * other governmental approval, or letter of assurance, without first obtaining
170 * such license, approval or letter.
171 *
172 *****************************************************************************/
173
174 #include <sys/cdefs.h>
175 __KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.37 2006/02/20 12:17:49 kochi Exp $");
176
177 #include <sys/param.h>
178 #include <sys/systm.h>
179 #include <sys/device.h>
180 #include <sys/malloc.h>
181 #include <sys/proc.h>
182 #include <sys/kernel.h>
183
184 #include <machine/bus.h>
185
186 #include <dev/acpi/acpica.h>
187 #include <dev/acpi/acpireg.h>
188 #include <dev/acpi/acpivar.h>
189 #include <dev/acpi/acpi_ecreg.h>
190
191 MALLOC_DECLARE(M_ACPI);
192
193 #define _COMPONENT ACPI_EC_COMPONENT
194 ACPI_MODULE_NAME("EC")
195
196 struct acpi_ec_softc {
197 struct device sc_dev; /* base device glue */
198 ACPI_HANDLE sc_handle; /* ACPI handle */
199
200 UINT32 sc_gpebit; /* our GPE interrupt bit */
201
202 bus_space_tag_t sc_data_st; /* space tag for data register */
203 bus_space_handle_t sc_data_sh; /* space handle for data register */
204
205 bus_space_tag_t sc_csr_st; /* space tag for control register */
206 bus_space_handle_t sc_csr_sh; /* space handle for control register */
207
208 int sc_flags; /* see below */
209
210 uint32_t sc_csrvalue; /* saved control register */
211 uint32_t sc_uid; /* _UID in namespace (ECDT only) */
212
213 struct lock sc_lock; /* serialize operations to this EC */
214 struct simplelock sc_slock; /* protect against interrupts */
215 UINT32 sc_glkhandle; /* global lock handle */
216 UINT32 sc_glk; /* need global lock? */
217 };
218
219 static const char * const ec_hid[] = {
220 "PNP0C09",
221 NULL
222 };
223
224 #define EC_F_TRANSACTION 0x01 /* doing transaction */
225 #define EC_F_PENDQUERY 0x02 /* query is pending */
226
227 /*
228 * how long to wait to acquire global lock.
229 * the value is taken from FreeBSD driver.
230 */
231 #define EC_LOCK_TIMEOUT 1000
232
233 #define EC_DATA_READ(sc) \
234 bus_space_read_1((sc)->sc_data_st, (sc)->sc_data_sh, 0)
235 #define EC_DATA_WRITE(sc, v) \
236 bus_space_write_1((sc)->sc_data_st, (sc)->sc_data_sh, 0, (v))
237
238 #define EC_CSR_READ(sc) \
239 bus_space_read_1((sc)->sc_csr_st, (sc)->sc_csr_sh, 0)
240 #define EC_CSR_WRITE(sc, v) \
241 bus_space_write_1((sc)->sc_csr_st, (sc)->sc_csr_sh, 0, (v))
242
243 typedef struct {
244 EC_COMMAND Command;
245 UINT8 Address;
246 UINT8 Data;
247 } EC_REQUEST;
248
249 static UINT32 EcGpeHandler(void *);
250 static ACPI_STATUS EcSpaceSetup(ACPI_HANDLE, UINT32, void *, void **);
251 static ACPI_STATUS EcSpaceHandler(UINT32, ACPI_PHYSICAL_ADDRESS, UINT32,
252 ACPI_INTEGER *, void *, void *);
253
254 static ACPI_STATUS EcWaitEvent(struct acpi_ec_softc *, EC_EVENT);
255 static ACPI_STATUS EcQuery(struct acpi_ec_softc *, UINT8 *);
256 static ACPI_STATUS EcTransaction(struct acpi_ec_softc *, EC_REQUEST *);
257 static ACPI_STATUS EcRead(struct acpi_ec_softc *, UINT8, UINT8 *);
258 static ACPI_STATUS EcWrite(struct acpi_ec_softc *, UINT8, UINT8 *);
259 static void EcGpeQueryHandler(void *);
260 static inline int EcIsLocked(struct acpi_ec_softc *);
261 static inline void EcLock(struct acpi_ec_softc *);
262 static inline void EcUnlock(struct acpi_ec_softc *);
263
264
265 static int acpiec_match(struct device *, struct cfdata *, void *);
266 static void acpiec_attach(struct device *, struct device *, void *);
267
268 CFATTACH_DECL(acpiec, sizeof(struct acpi_ec_softc),
269 acpiec_match, acpiec_attach, NULL, NULL);
270
271 static struct acpi_ec_softc *ecdt_sc;
272
273 static inline int
274 EcIsLocked(struct acpi_ec_softc *sc)
275 {
276
277 return (lockstatus(&sc->sc_lock) == LK_EXCLUSIVE);
278 }
279
280 static inline void
281 EcLock(struct acpi_ec_softc *sc)
282 {
283 ACPI_STATUS rv;
284 int s;
285
286 lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL);
287 if (sc->sc_glk) {
288 rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT,
289 &sc->sc_glkhandle);
290 if (ACPI_FAILURE(rv)) {
291 printf("%s: failed to acquire global lock\n",
292 sc->sc_dev.dv_xname);
293 lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
294 return;
295 }
296 }
297
298 s = splvm(); /* XXX */
299 simple_lock(&sc->sc_slock);
300 sc->sc_flags |= EC_F_TRANSACTION;
301 simple_unlock(&sc->sc_slock);
302 splx(s);
303 }
304
305 static inline void
306 EcUnlock(struct acpi_ec_softc *sc)
307 {
308 ACPI_STATUS rv;
309 int s;
310
311 /*
312 * Clear & Re-Enable the EC GPE:
313 * -----------------------------
314 * 'Consume' any EC GPE events that we generated while performing
315 * the transaction (e.g. IBF/OBF). Clearing the GPE here shouldn't
316 * have an adverse affect on outstanding EC-SCI's, as the source
317 * (EC-SCI) will still be high and thus should trigger the GPE
318 * immediately after we re-enabling it.
319 */
320 s = splvm(); /* XXX */
321 simple_lock(&sc->sc_slock);
322 if (sc->sc_flags & EC_F_PENDQUERY) {
323 ACPI_STATUS rv2;
324
325 rv2 = AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
326 EcGpeQueryHandler, sc);
327 if (ACPI_FAILURE(rv2))
328 printf("%s: unable to queue pending query: %s\n",
329 sc->sc_dev.dv_xname, AcpiFormatException(rv2));
330 sc->sc_flags &= ~EC_F_PENDQUERY;
331 }
332 sc->sc_flags &= ~EC_F_TRANSACTION;
333 simple_unlock(&sc->sc_slock);
334 splx(s);
335
336 if (sc->sc_glk) {
337 rv = AcpiReleaseGlobalLock(sc->sc_glkhandle);
338 if (ACPI_FAILURE(rv))
339 printf("%s: failed to release global lock\n",
340 sc->sc_dev.dv_xname);
341 }
342 lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
343 }
344
345 /*
346 * acpiec_match:
347 *
348 * Autoconfiguration `match' routine.
349 */
350 static int
351 acpiec_match(struct device *parent, struct cfdata *match, void *aux)
352 {
353 struct acpi_attach_args *aa = aux;
354
355 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
356 return 0;
357
358 return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid);
359 }
360
361 void
362 acpiec_early_attach(struct device *parent)
363 {
364 struct acpi_softc *sc = (struct acpi_softc *)parent;
365 EC_BOOT_RESOURCES *ep;
366 ACPI_HANDLE handle;
367 ACPI_STATUS rv;
368 ACPI_INTEGER tmp;
369
370 rv = AcpiGetFirmwareTable("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
371 (void *)&ep);
372 if (ACPI_FAILURE(rv))
373 return;
374
375 if (ep->EcControl.RegisterBitWidth != 8 ||
376 ep->EcData.RegisterBitWidth != 8) {
377 printf("%s: ECDT data is invalid, RegisterBitWidth=%d/%d\n",
378 parent->dv_xname,
379 ep->EcControl.RegisterBitWidth,
380 ep->EcData.RegisterBitWidth);
381 return;
382 }
383
384 rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ep->EcId, &handle);
385 if (ACPI_FAILURE(rv)) {
386 printf("%s: failed to look up EC object %s: %s\n",
387 parent->dv_xname,
388 ep->EcId, AcpiFormatException(rv));
389 return;
390 }
391
392 ecdt_sc = malloc(sizeof(*ecdt_sc), M_ACPI, M_ZERO);
393
394 strcpy(ecdt_sc->sc_dev.dv_xname, "acpiecdt0"); /* XXX */
395 ecdt_sc->sc_handle = handle;
396
397 ecdt_sc->sc_gpebit = ep->GpeBit;
398 ecdt_sc->sc_uid = ep->Uid;
399
400 ecdt_sc->sc_data_st = sc->sc_iot;
401 if (bus_space_map(ecdt_sc->sc_data_st,
402 (bus_addr_t)(ep->EcData.Address), 1, 0,
403 &ecdt_sc->sc_data_sh) != 0) {
404 printf("%s: bus_space_map failed for EC data.\n",
405 parent->dv_xname);
406 goto out1;
407 }
408
409 ecdt_sc->sc_csr_st = sc->sc_iot;
410 if (bus_space_map(ecdt_sc->sc_csr_st,
411 (bus_addr_t)(ep->EcControl.Address), 1, 0,
412 &ecdt_sc->sc_csr_sh) != 0) {
413 printf("%s: bus_space_map failed for EC control.\n",
414 parent->dv_xname);
415 goto out2;
416 }
417
418 rv = acpi_eval_integer(handle, "_GLK", &tmp);
419 if (ACPI_SUCCESS(rv) && tmp == 1)
420 ecdt_sc->sc_glk = 1;
421
422 rv = AcpiInstallGpeHandler(NULL, ecdt_sc->sc_gpebit,
423 ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, ecdt_sc);
424 if (ACPI_FAILURE(rv)) {
425 printf("%s: unable to install GPE handler: %s\n",
426 parent->dv_xname, AcpiFormatException(rv));
427 goto out3;
428 }
429
430 rv = AcpiSetGpeType(NULL, ecdt_sc->sc_gpebit, ACPI_GPE_TYPE_RUNTIME);
431 if (ACPI_FAILURE(rv)) {
432 printf("%s: unable to set GPE type: %s\n",
433 parent->dv_xname, AcpiFormatException(rv));
434 goto out4;
435 }
436
437 rv = AcpiEnableGpe(NULL, ecdt_sc->sc_gpebit, ACPI_NOT_ISR);
438 if (ACPI_FAILURE(rv)) {
439 printf("%s: unable to enable GPE: %s\n",
440 parent->dv_xname, AcpiFormatException(rv));
441 goto out4;
442 }
443
444 rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
445 ACPI_ADR_SPACE_EC, EcSpaceHandler, EcSpaceSetup, ecdt_sc);
446 if (ACPI_FAILURE(rv)) {
447 printf("%s: unable to install address space handler: %s\n",
448 parent->dv_xname,
449 AcpiFormatException(rv));
450 goto out4;
451 }
452
453 printf("%s: found ECDT, GPE %d\n", parent->dv_xname,
454 ecdt_sc->sc_gpebit);
455
456 lockinit(&ecdt_sc->sc_lock, PWAIT, "eclock", 0, 0);
457 simple_lock_init(&ecdt_sc->sc_slock);
458
459 AcpiOsUnmapMemory(ep, ep->Length);
460 return;
461
462 out4:
463 AcpiRemoveGpeHandler(NULL, ecdt_sc->sc_gpebit, EcGpeHandler);
464 out3:
465 bus_space_unmap(ecdt_sc->sc_csr_st, ecdt_sc->sc_csr_sh, 1);
466 out2:
467 bus_space_unmap(ecdt_sc->sc_data_st, ecdt_sc->sc_data_sh, 1);
468 out1:
469 free(ecdt_sc, M_ACPI);
470 ecdt_sc = NULL;
471
472 AcpiOsUnmapMemory(ep, ep->Length);
473
474 return;
475 }
476
477 /*
478 * acpiec_attach:
479 *
480 * Autoconfiguration `attach' routine.
481 */
482 static void
483 acpiec_attach(struct device *parent, struct device *self, void *aux)
484 {
485 struct acpi_ec_softc *sc = (void *) self;
486 struct acpi_attach_args *aa = aux;
487 struct acpi_io *io0, *io1;
488 struct acpi_resources res;
489 ACPI_STATUS rv;
490 ACPI_INTEGER v;
491
492 ACPI_FUNCTION_TRACE(__FUNCTION__);
493
494 aprint_naive(": ACPI Embedded Controller\n");
495 aprint_normal(": ACPI Embedded Controller\n");
496
497 lockinit(&sc->sc_lock, PWAIT, "eclock", 0, 0);
498 simple_lock_init(&sc->sc_slock);
499
500 sc->sc_handle = aa->aa_node->ad_handle;
501
502 /* Parse our resources. */
503 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "parsing EC resources\n"));
504 rv = acpi_resource_parse(&sc->sc_dev, sc->sc_handle, "_CRS",
505 &res, &acpi_resource_parse_ops_default);
506 if (ACPI_FAILURE(rv))
507 return;
508
509 #define adi aa->aa_node->ad_devinfo
510
511 /* check if we already attached EC via ECDT */
512 if (ecdt_sc &&
513 ((adi->Valid & ACPI_VALID_UID) == ACPI_VALID_UID) &&
514 ecdt_sc->sc_uid == strtoul(adi->UniqueId.Value, NULL, 10)) {
515 /* detach all ECDT handles */
516 rv = AcpiRemoveAddressSpaceHandler(ACPI_ROOT_OBJECT,
517 ACPI_ADR_SPACE_EC, EcSpaceHandler);
518 if (ACPI_FAILURE(rv))
519 aprint_error("ERROR: RemoveAddressSpaceHandler: %s\n",
520 AcpiFormatException(rv));
521 rv = AcpiRemoveGpeHandler(NULL, ecdt_sc->sc_gpebit,
522 EcGpeHandler);
523 if (ACPI_FAILURE(rv))
524 aprint_error("ERROR: RemoveAddressSpaceHandler: %s\n",
525 AcpiFormatException(rv));
526
527 bus_space_unmap(ecdt_sc->sc_csr_st,
528 ecdt_sc->sc_csr_sh, 1);
529 bus_space_unmap(ecdt_sc->sc_data_st,
530 ecdt_sc->sc_data_sh, 1);
531
532 free(ecdt_sc, M_ACPI);
533 ecdt_sc = NULL;
534 }
535
536 #undef adi
537
538 sc->sc_data_st = aa->aa_iot;
539 io0 = acpi_res_io(&res, 0);
540 if (io0 == NULL) {
541 aprint_error("%s: unable to find data register resource\n",
542 sc->sc_dev.dv_xname);
543 goto out;
544 }
545 if (bus_space_map(sc->sc_data_st, io0->ar_base, io0->ar_length,
546 0, &sc->sc_data_sh) != 0) {
547 aprint_error("%s: unable to map data register\n",
548 sc->sc_dev.dv_xname);
549 goto out;
550 }
551
552 sc->sc_csr_st = aa->aa_iot;
553 io1 = acpi_res_io(&res, 1);
554 if (io1 == NULL) {
555 aprint_error("%s: unable to find csr register resource\n",
556 sc->sc_dev.dv_xname);
557 goto out;
558 }
559 if (bus_space_map(sc->sc_csr_st, io1->ar_base, io1->ar_length,
560 0, &sc->sc_csr_sh) != 0) {
561 aprint_error("%s: unable to map csr register\n",
562 sc->sc_dev.dv_xname);
563 goto out;
564 }
565
566 /*
567 * evaluate _GLK to see if we should acquire global lock
568 * when accessing the EC.
569 */
570 rv = acpi_eval_integer(sc->sc_handle, "_GLK", &v);
571 if (ACPI_FAILURE(rv)) {
572 if (rv != AE_NOT_FOUND)
573 aprint_error("%s: unable to evaluate _GLK: %s\n",
574 sc->sc_dev.dv_xname,
575 AcpiFormatException(rv));
576 sc->sc_glk = 0;
577 } else
578 sc->sc_glk = v;
579
580 /*
581 * Install GPE handler.
582 *
583 * We evaluate the _GPE method to find the GPE bit used by the
584 * Embedded Controller to signal status (SCI).
585 */
586 rv = acpi_eval_integer(sc->sc_handle, "_GPE", &v);
587 if (ACPI_FAILURE(rv)) {
588 aprint_error("%s: unable to evaluate _GPE: %s\n",
589 sc->sc_dev.dv_xname, AcpiFormatException(rv));
590 goto out;
591 }
592 sc->sc_gpebit = v;
593
594 /*
595 * Install a handler for this EC's GPE bit. Note that EC SCIs are
596 * treated as both edge- and level-triggered interrupts; in other words
597 * we clear the status bit immediately after getting an EC-SCI, then
598 * again after we're done processing the event. This guarantees that
599 * events we cause while performing a transaction (e.g. IBE/OBF) get
600 * cleared before re-enabling the GPE.
601 */
602 rv = AcpiInstallGpeHandler(NULL, sc->sc_gpebit,
603 ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, sc);
604 if (ACPI_FAILURE(rv)) {
605 aprint_error("%s: unable to install GPE handler: %s\n",
606 sc->sc_dev.dv_xname, AcpiFormatException(rv));
607 goto out;
608 }
609
610 rv = AcpiSetGpeType(NULL, sc->sc_gpebit, ACPI_GPE_TYPE_RUNTIME);
611 if (ACPI_FAILURE(rv)) {
612 aprint_error("%s: unable to set GPE type: %s\n",
613 sc->sc_dev.dv_xname, AcpiFormatException(rv));
614 goto out2;
615 }
616
617 rv = AcpiEnableGpe(NULL, sc->sc_gpebit, ACPI_NOT_ISR);
618 if (ACPI_FAILURE(rv)) {
619 aprint_error("%s: unable to enable GPE: %s\n",
620 sc->sc_dev.dv_xname, AcpiFormatException(rv));
621 goto out2;
622 }
623
624 /* Install address space handler. */
625 rv = AcpiInstallAddressSpaceHandler(sc->sc_handle,
626 ACPI_ADR_SPACE_EC, EcSpaceHandler, EcSpaceSetup, sc);
627 if (ACPI_FAILURE(rv)) {
628 aprint_error("%s: unable to install address space handler: %s\n",
629 sc->sc_dev.dv_xname, AcpiFormatException(rv));
630 goto out2;
631 }
632
633 return_VOID;
634 out2:
635 (void)AcpiRemoveGpeHandler(NULL, sc->sc_gpebit, EcGpeHandler);
636
637 out:
638 acpi_resource_cleanup(&res);
639 return_VOID;
640 }
641
642 static void
643 EcGpeQueryHandler(void *Context)
644 {
645 struct acpi_ec_softc *sc = Context;
646 UINT8 Data;
647 ACPI_STATUS rv;
648 char qxx[5];
649
650 ACPI_FUNCTION_TRACE(__FUNCTION__);
651
652 for (;;) {
653 /*
654 * Check EC_SCI.
655 *
656 * Bail out if the EC_SCI bit of the status register is not
657 * set. Note that this function should only be called when
658 * this bit is set (polling is used to detect IBE/OBF events).
659 *
660 * It is safe to do this without locking the controller, as
661 * it's OK to call EcQuery when there's no data ready; in the
662 * worst case we should just find nothing waiting for us and
663 * bail.
664 */
665 if ((EC_CSR_READ(sc) & EC_EVENT_SCI) == 0)
666 break;
667
668 EcLock(sc);
669
670 /*
671 * Find out why the EC is signalling us
672 */
673 rv = EcQuery(sc, &Data);
674
675 EcUnlock(sc);
676
677 /*
678 * If we failed to get anything from the EC, give up.
679 */
680 if (ACPI_FAILURE(rv)) {
681 printf("%s: GPE query failed: %s\n",
682 sc->sc_dev.dv_xname, AcpiFormatException(rv));
683 break;
684 }
685
686 /*
687 * Evaluate _Qxx to respond to the controller.
688 */
689 snprintf(qxx, sizeof(qxx), "_Q%02X", Data);
690 rv = AcpiEvaluateObject(sc->sc_handle, qxx,
691 NULL, NULL);
692
693 /*
694 * Ignore spurious query requests.
695 */
696 if (ACPI_FAILURE(rv) &&
697 (Data != 0 || rv != AE_NOT_FOUND)) {
698 printf("%s: evaluation of GPE query method %s "
699 "failed: %s\n", sc->sc_dev.dv_xname, qxx,
700 AcpiFormatException(rv));
701 }
702 }
703
704 /* I know I request Level trigger cleanup */
705 rv = AcpiClearGpe(NULL, sc->sc_gpebit, ACPI_NOT_ISR);
706 if (ACPI_FAILURE(rv))
707 printf("%s: AcpiClearGpe failed: %s\n", sc->sc_dev.dv_xname,
708 AcpiFormatException(rv));
709
710 return_VOID;
711 }
712
713 static UINT32
714 EcGpeHandler(void *Context)
715 {
716 struct acpi_ec_softc *sc = Context;
717 uint32_t csrvalue;
718 ACPI_STATUS rv;
719
720 /*
721 * If EC is locked, the intr must process EcRead/Write wait only.
722 * Query request must be pending.
723 */
724 simple_lock(&sc->sc_slock);
725 if (sc->sc_flags & EC_F_TRANSACTION) {
726 csrvalue = EC_CSR_READ(sc);
727 sc->sc_flags |= EC_F_PENDQUERY;
728
729 if ((csrvalue & EC_FLAG_OUTPUT_BUFFER) != 0 ||
730 (csrvalue & EC_FLAG_INPUT_BUFFER) == 0) {
731 sc->sc_csrvalue = csrvalue;
732 wakeup(&sc->sc_csrvalue);
733 }
734 simple_unlock(&sc->sc_slock);
735 } else {
736 simple_unlock(&sc->sc_slock);
737 /* Enqueue GpeQuery handler. */
738 rv = AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
739 EcGpeQueryHandler, Context);
740 if (ACPI_FAILURE(rv))
741 printf("%s: failed to enqueue query handler: %s\n",
742 sc->sc_dev.dv_xname, AcpiFormatException(rv));
743 }
744
745 return 0; /* XXX not yet used in ACPI-CA */
746 }
747
748 static ACPI_STATUS
749 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
750 void **RegionContext)
751 {
752
753 ACPI_FUNCTION_TRACE(__FUNCTION__);
754
755 /*
756 * Just pass the context through, there's nothing to do here.
757 */
758 if (Function == ACPI_REGION_DEACTIVATE)
759 *RegionContext = NULL;
760 else
761 *RegionContext = Context;
762
763 return_ACPI_STATUS(AE_OK);
764 }
765
766 static ACPI_STATUS
767 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
768 ACPI_INTEGER *Value, void *Context, void *RegionContext)
769 {
770 struct acpi_ec_softc *sc = Context;
771 ACPI_STATUS rv = AE_OK;
772 EC_REQUEST EcRequest;
773 int i;
774
775 ACPI_FUNCTION_TRACE_U32(__FUNCTION__, (UINT32)Address);
776
777 if ((Address > 0xFF) || (width % 8 != 0) || (Value == NULL) ||
778 (Context == NULL))
779 return_ACPI_STATUS(AE_BAD_PARAMETER);
780
781 switch (Function) {
782 case ACPI_READ:
783 EcRequest.Command = EC_COMMAND_READ;
784 EcRequest.Address = Address;
785 (*Value) = 0;
786 break;
787
788 case ACPI_WRITE:
789 EcRequest.Command = EC_COMMAND_WRITE;
790 EcRequest.Address = Address;
791 break;
792
793 default:
794 printf("%s: invalid Address Space function: %d\n",
795 sc->sc_dev.dv_xname, Function);
796 return_ACPI_STATUS(AE_BAD_PARAMETER);
797 }
798
799 /*
800 * Perform the transaction.
801 */
802 for (i = 0; i < width; i += 8) {
803 if (Function == ACPI_READ)
804 EcRequest.Data = 0;
805 else
806 EcRequest.Data = (UINT8)((*Value) >> i);
807
808 rv = EcTransaction(sc, &EcRequest);
809 if (ACPI_FAILURE(rv))
810 break;
811
812 (*Value) |= (UINT32)EcRequest.Data << i;
813 if (++EcRequest.Address == 0)
814 return_ACPI_STATUS(AE_BAD_PARAMETER);
815 }
816
817 return_ACPI_STATUS(rv);
818 }
819
820 static ACPI_STATUS
821 EcWaitEventIntr(struct acpi_ec_softc *sc, EC_EVENT Event)
822 {
823 EC_STATUS EcStatus;
824 int i;
825
826 ACPI_FUNCTION_TRACE_U32(__FUNCTION__, (UINT32)Event);
827
828 /* XXX Need better test for "yes, you have interrupts". */
829 if (cold)
830 return_ACPI_STATUS(EcWaitEvent(sc, Event));
831
832 if (EcIsLocked(sc) == 0)
833 printf("%s: EcWaitEventIntr called without EC lock!\n",
834 sc->sc_dev.dv_xname);
835
836 EcStatus = EC_CSR_READ(sc);
837
838 /* Too long? */
839 for (i = 0; i < 10; i++) {
840 /* Check EC status against the desired event. */
841 if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
842 (EcStatus & EC_FLAG_OUTPUT_BUFFER) != 0)
843 return_ACPI_STATUS(AE_OK);
844
845 if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
846 (EcStatus & EC_FLAG_INPUT_BUFFER) == 0)
847 return_ACPI_STATUS(AE_OK);
848
849 sc->sc_csrvalue = 0;
850 /* XXXJRT Sleeping with a lock held? */
851 if (tsleep(&sc->sc_csrvalue, 0, "EcWait", (hz + 99) / 100)
852 != EWOULDBLOCK)
853 EcStatus = sc->sc_csrvalue;
854 else
855 EcStatus = EC_CSR_READ(sc);
856 }
857 return_ACPI_STATUS(AE_ERROR);
858 }
859
860 static ACPI_STATUS
861 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
862 {
863 EC_STATUS EcStatus;
864 UINT32 i = 0;
865
866 if (EcIsLocked(sc) == 0)
867 printf("%s: EcWaitEvent called without EC lock!\n",
868 sc->sc_dev.dv_xname);
869
870 /*
871 * Stall 1us:
872 * ----------
873 * Stall for 1 microsecond before reading the status register
874 * for the first time. This allows the EC to set the IBF/OBF
875 * bit to its proper state.
876 *
877 * XXX it is not clear why we read the CSR twice.
878 */
879 AcpiOsStall(1);
880 EcStatus = EC_CSR_READ(sc);
881
882 /*
883 * Wait For Event:
884 * ---------------
885 * Poll the EC status register to detect completion of the last
886 * command. Wait up to 10ms (in 100us chunks) for this to occur.
887 */
888 for (i = 0; i < 100; i++) {
889 EcStatus = EC_CSR_READ(sc);
890
891 if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
892 (EcStatus & EC_FLAG_OUTPUT_BUFFER) != 0)
893 return AE_OK;
894
895 if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
896 (EcStatus & EC_FLAG_INPUT_BUFFER) == 0)
897 return AE_OK;
898
899 AcpiOsStall(10);
900 }
901
902 return AE_ERROR;
903 }
904
905 static ACPI_STATUS
906 EcQuery(struct acpi_ec_softc *sc, UINT8 *Data)
907 {
908 ACPI_STATUS rv;
909
910 if (EcIsLocked(sc) == 0)
911 printf("%s: EcQuery called without EC lock!\n",
912 sc->sc_dev.dv_xname);
913
914 EC_CSR_WRITE(sc, EC_COMMAND_QUERY);
915 rv = EcWaitEventIntr(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
916 if (ACPI_SUCCESS(rv))
917 *Data = EC_DATA_READ(sc);
918
919 if (ACPI_FAILURE(rv))
920 printf("%s: timed out waiting for EC to respond to "
921 "EC_COMMAND_QUERY\n", sc->sc_dev.dv_xname);
922
923 return rv;
924 }
925
926 static ACPI_STATUS
927 EcTransaction(struct acpi_ec_softc *sc, EC_REQUEST *EcRequest)
928 {
929 ACPI_STATUS rv;
930
931 EcLock(sc);
932
933 /*
934 * Perform the transaction.
935 */
936 switch (EcRequest->Command) {
937 case EC_COMMAND_READ:
938 rv = EcRead(sc, EcRequest->Address, &(EcRequest->Data));
939 break;
940
941 case EC_COMMAND_WRITE:
942 rv = EcWrite(sc, EcRequest->Address, &(EcRequest->Data));
943 break;
944
945 default:
946 rv = AE_SUPPORT;
947 break;
948 }
949
950 EcUnlock(sc);
951
952 return rv;
953 }
954
955 static ACPI_STATUS
956 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
957 {
958 ACPI_STATUS rv;
959
960 if (EcIsLocked(sc) == 0)
961 printf("%s: EcRead called without EC lock!\n",
962 sc->sc_dev.dv_xname);
963
964 /* EcBurstEnable(EmbeddedController); */
965
966 EC_CSR_WRITE(sc, EC_COMMAND_READ);
967 if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
968 AE_OK) {
969 printf("%s: EcRead: timeout waiting for EC to process "
970 "read command\n", sc->sc_dev.dv_xname);
971 return rv;
972 }
973
974 EC_DATA_WRITE(sc, Address);
975 if ((rv = EcWaitEventIntr(sc, EC_EVENT_OUTPUT_BUFFER_FULL)) !=
976 AE_OK) {
977 printf("%s: EcRead: timeout waiting for EC to send data\n",
978 sc->sc_dev.dv_xname);
979 return rv;
980 }
981
982 (*Data) = EC_DATA_READ(sc);
983
984 /* EcBurstDisable(EmbeddedController); */
985
986 return AE_OK;
987 }
988
989 static ACPI_STATUS
990 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
991 {
992 ACPI_STATUS rv;
993
994 if (EcIsLocked(sc) == 0)
995 printf("%s: EcWrite called without EC lock!\n",
996 sc->sc_dev.dv_xname);
997
998 /* EcBurstEnable(EmbeddedController); */
999
1000 EC_CSR_WRITE(sc, EC_COMMAND_WRITE);
1001 if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
1002 AE_OK) {
1003 printf("%s: EcWrite: timeout waiting for EC to process "
1004 "write command\n", sc->sc_dev.dv_xname);
1005 return rv;
1006 }
1007
1008 EC_DATA_WRITE(sc, Address);
1009 if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
1010 AE_OK) {
1011 printf("%s: EcWrite: timeout waiting for EC to process "
1012 "address\n", sc->sc_dev.dv_xname);
1013 return rv;
1014 }
1015
1016 EC_DATA_WRITE(sc, *Data);
1017 if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
1018 AE_OK) {
1019 printf("%s: EcWrite: timeout waiting for EC to process "
1020 "data\n", sc->sc_dev.dv_xname);
1021 return rv;
1022 }
1023
1024 /* EcBurstDisable(EmbeddedController); */
1025
1026 return AE_OK;
1027 }
1028