acpi_ec.c revision 1.31 1 /* $NetBSD: acpi_ec.c,v 1.31 2004/05/01 12:03:48 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.31 2004/05/01 12:03:48 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 void EcGpeHandler(void *Context);
250 static ACPI_STATUS EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
251 void *Context, void **return_Context);
252 static ACPI_STATUS EcSpaceHandler(UINT32 Function,
253 ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
254 ACPI_INTEGER *Value, void *Context,
255 void *RegionContext);
256
257 static ACPI_STATUS EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
258 static ACPI_STATUS EcQuery(struct acpi_ec_softc *sc, UINT8 *Data);
259 static ACPI_STATUS EcTransaction(struct acpi_ec_softc *sc,
260 EC_REQUEST *EcRequest);
261 static ACPI_STATUS EcRead(struct acpi_ec_softc *sc, UINT8 Address,
262 UINT8 *Data);
263 static ACPI_STATUS EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
264 UINT8 *Data);
265 static void EcGpeQueryHandler(void *);
266 static __inline int EcIsLocked(struct acpi_ec_softc *);
267 static __inline void EcLock(struct acpi_ec_softc *);
268 static __inline void EcUnlock(struct acpi_ec_softc *);
269
270
271 static int acpiec_match(struct device *, struct cfdata *, void *);
272 static void acpiec_attach(struct device *, struct device *, void *);
273
274 CFATTACH_DECL(acpiec, sizeof(struct acpi_ec_softc),
275 acpiec_match, acpiec_attach, NULL, NULL);
276
277 static struct acpi_ec_softc *ecdt_sc;
278
279 static __inline int
280 EcIsLocked(struct acpi_ec_softc *sc)
281 {
282
283 return (lockstatus(&sc->sc_lock) == LK_EXCLUSIVE);
284 }
285
286 static __inline void
287 EcLock(struct acpi_ec_softc *sc)
288 {
289 ACPI_STATUS rv;
290 int s;
291
292 lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL);
293 if (sc->sc_glk) {
294 rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT,
295 &sc->sc_glkhandle);
296 if (ACPI_FAILURE(rv)) {
297 printf("%s: failed to acquire global lock\n",
298 sc->sc_dev.dv_xname);
299 lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
300 return;
301 }
302 }
303
304 s = splvm(); /* XXX */
305 simple_lock(&sc->sc_slock);
306 sc->sc_flags |= EC_F_TRANSACTION;
307 simple_unlock(&sc->sc_slock);
308 splx(s);
309 }
310
311 static __inline void
312 EcUnlock(struct acpi_ec_softc *sc)
313 {
314 ACPI_STATUS rv;
315 int s;
316
317 /*
318 * Clear & Re-Enable the EC GPE:
319 * -----------------------------
320 * 'Consume' any EC GPE events that we generated while performing
321 * the transaction (e.g. IBF/OBF). Clearing the GPE here shouldn't
322 * have an adverse affect on outstanding EC-SCI's, as the source
323 * (EC-SCI) will still be high and thus should trigger the GPE
324 * immediately after we re-enabling it.
325 */
326 s = splvm(); /* XXX */
327 simple_lock(&sc->sc_slock);
328 if (sc->sc_flags & EC_F_PENDQUERY) {
329 ACPI_STATUS rv2;
330
331 rv2 = AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
332 EcGpeQueryHandler, sc);
333 if (ACPI_FAILURE(rv2))
334 printf("%s: unable to queue pending query: %s\n",
335 sc->sc_dev.dv_xname, AcpiFormatException(rv2));
336 sc->sc_flags &= ~EC_F_PENDQUERY;
337 }
338 sc->sc_flags &= ~EC_F_TRANSACTION;
339 simple_unlock(&sc->sc_slock);
340 splx(s);
341
342 if (sc->sc_glk) {
343 rv = AcpiReleaseGlobalLock(sc->sc_glkhandle);
344 if (ACPI_FAILURE(rv))
345 printf("%s: failed to release global lock\n",
346 sc->sc_dev.dv_xname);
347 }
348 lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
349 }
350
351 /*
352 * acpiec_match:
353 *
354 * Autoconfiguration `match' routine.
355 */
356 static int
357 acpiec_match(struct device *parent, struct cfdata *match, void *aux)
358 {
359 struct acpi_attach_args *aa = aux;
360
361 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
362 return 0;
363
364 return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid);
365 }
366
367 void
368 acpiec_early_attach(struct device *parent)
369 {
370 struct acpi_softc *sc = (struct acpi_softc *)parent;
371 EC_BOOT_RESOURCES *ep;
372 ACPI_HANDLE handle;
373 ACPI_STATUS rv;
374 ACPI_INTEGER tmp;
375
376 rv = AcpiGetFirmwareTable("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
377 (void *)&ep);
378 if (ACPI_FAILURE(rv))
379 return;
380
381 if (ep->EcControl.RegisterBitWidth != 8 ||
382 ep->EcData.RegisterBitWidth != 8) {
383 printf("%s: ECDT data is invalid, RegisterBitWidth=%d/%d\n",
384 parent->dv_xname,
385 ep->EcControl.RegisterBitWidth,
386 ep->EcData.RegisterBitWidth);
387 return;
388 }
389
390 rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ep->EcId, &handle);
391 if (ACPI_FAILURE(rv)) {
392 printf("%s: failed to look up EC object %s: %s\n",
393 parent->dv_xname,
394 ep->EcId, AcpiFormatException(rv));
395 return;
396 }
397
398 ecdt_sc = malloc(sizeof(*ecdt_sc), M_ACPI, M_ZERO);
399
400 strcpy(ecdt_sc->sc_dev.dv_xname, "acpiecdt0"); /* XXX */
401 ecdt_sc->sc_handle = handle;
402
403 ecdt_sc->sc_gpebit = ep->GpeBit;
404 ecdt_sc->sc_uid = ep->Uid;
405
406 ecdt_sc->sc_data_st = sc->sc_iot;
407 if (bus_space_map(ecdt_sc->sc_data_st,
408 (bus_addr_t)(ep->EcData.Address), 1, 0,
409 &ecdt_sc->sc_data_sh) != 0) {
410 printf("%s: bus_space_map failed for EC data.\n",
411 parent->dv_xname);
412 goto out1;
413 }
414
415 ecdt_sc->sc_csr_st = sc->sc_iot;
416 if (bus_space_map(ecdt_sc->sc_csr_st,
417 (bus_addr_t)(ep->EcControl.Address), 1, 0,
418 &ecdt_sc->sc_csr_sh) != 0) {
419 printf("%s: bus_space_map failed for EC control.\n",
420 parent->dv_xname);
421 goto out2;
422 }
423
424 rv = acpi_eval_integer(handle, "_GLK", &tmp);
425 if (ACPI_SUCCESS(rv) && tmp == 1)
426 ecdt_sc->sc_glk = 1;
427
428 rv = AcpiInstallGpeHandler(NULL, ecdt_sc->sc_gpebit,
429 ACPI_EVENT_EDGE_TRIGGERED, EcGpeHandler, ecdt_sc);
430 if (ACPI_FAILURE(rv)) {
431 printf("%s: unable to install GPE handler: %s\n",
432 parent->dv_xname,
433 AcpiFormatException(rv));
434 goto out3;
435 }
436
437 rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
438 ACPI_ADR_SPACE_EC, EcSpaceHandler, EcSpaceSetup, ecdt_sc);
439 if (ACPI_FAILURE(rv)) {
440 printf("%s: unable to install address space handler: %s\n",
441 parent->dv_xname,
442 AcpiFormatException(rv));
443 goto out4;
444 }
445
446 printf("%s: found ECDT, GPE %d\n", parent->dv_xname,
447 ecdt_sc->sc_gpebit);
448
449 lockinit(&ecdt_sc->sc_lock, PWAIT, "eclock", 0, 0);
450 simple_lock_init(&ecdt_sc->sc_slock);
451
452 AcpiOsUnmapMemory(ep, ep->Length);
453 return;
454
455 out4:
456 AcpiRemoveGpeHandler(NULL, ecdt_sc->sc_gpebit, EcGpeHandler);
457 out3:
458 bus_space_unmap(ecdt_sc->sc_csr_st, ecdt_sc->sc_csr_sh, 1);
459 out2:
460 bus_space_unmap(ecdt_sc->sc_data_st, ecdt_sc->sc_data_sh, 1);
461 out1:
462 free(ecdt_sc, M_ACPI);
463 ecdt_sc = NULL;
464
465 AcpiOsUnmapMemory(ep, ep->Length);
466
467 return;
468 }
469
470 /*
471 * acpiec_attach:
472 *
473 * Autoconfiguration `attach' routine.
474 */
475 static void
476 acpiec_attach(struct device *parent, struct device *self, void *aux)
477 {
478 struct acpi_ec_softc *sc = (void *) self;
479 struct acpi_attach_args *aa = aux;
480 struct acpi_io *io0, *io1;
481 struct acpi_resources res;
482 ACPI_STATUS rv;
483 ACPI_INTEGER v;
484
485 ACPI_FUNCTION_TRACE(__FUNCTION__);
486
487 printf(": ACPI Embedded Controller\n");
488
489 lockinit(&sc->sc_lock, PWAIT, "eclock", 0, 0);
490 simple_lock_init(&sc->sc_slock);
491
492 sc->sc_handle = aa->aa_node->ad_handle;
493
494 /* Parse our resources. */
495 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "parsing EC resources\n"));
496 rv = acpi_resource_parse(&sc->sc_dev, sc->sc_handle, "_CRS",
497 &res, &acpi_resource_parse_ops_default);
498 if (ACPI_FAILURE(rv))
499 return;
500
501 #define adi aa->aa_node->ad_devinfo
502
503 /* check if we already attached EC via ECDT */
504 if (ecdt_sc &&
505 ((adi->Valid & ACPI_VALID_UID) == ACPI_VALID_UID) &&
506 ecdt_sc->sc_uid == strtoul(adi->UniqueId.Value, NULL, 10)) {
507 /* detach all ECDT handles */
508 rv = AcpiRemoveAddressSpaceHandler(ACPI_ROOT_OBJECT,
509 ACPI_ADR_SPACE_EC, EcSpaceHandler);
510 if (ACPI_FAILURE(rv))
511 printf("ERROR: RemoveAddressSpaceHandler: %s\n",
512 AcpiFormatException(rv));
513 rv = AcpiRemoveGpeHandler(NULL, ecdt_sc->sc_gpebit,
514 EcGpeHandler);
515 if (ACPI_FAILURE(rv))
516 printf("ERROR: RemoveAddressSpaceHandler: %s\n",
517 AcpiFormatException(rv));
518
519 bus_space_unmap(ecdt_sc->sc_csr_st,
520 ecdt_sc->sc_csr_sh, 1);
521 bus_space_unmap(ecdt_sc->sc_data_st,
522 ecdt_sc->sc_data_sh, 1);
523
524 free(ecdt_sc, M_ACPI);
525 ecdt_sc = NULL;
526 }
527
528 #undef adi
529
530 sc->sc_data_st = aa->aa_iot;
531 io0 = acpi_res_io(&res, 0);
532 if (io0 == NULL) {
533 printf("%s: unable to find data register resource\n",
534 sc->sc_dev.dv_xname);
535 goto out;
536 }
537 if (bus_space_map(sc->sc_data_st, io0->ar_base, io0->ar_length,
538 0, &sc->sc_data_sh) != 0) {
539 printf("%s: unable to map data register\n",
540 sc->sc_dev.dv_xname);
541 goto out;
542 }
543
544 sc->sc_csr_st = aa->aa_iot;
545 io1 = acpi_res_io(&res, 1);
546 if (io1 == NULL) {
547 printf("%s: unable to find csr register resource\n",
548 sc->sc_dev.dv_xname);
549 goto out;
550 }
551 if (bus_space_map(sc->sc_csr_st, io1->ar_base, io1->ar_length,
552 0, &sc->sc_csr_sh) != 0) {
553 printf("%s: unable to map csr register\n",
554 sc->sc_dev.dv_xname);
555 goto out;
556 }
557
558 /*
559 * evaluate _GLK to see if we should acquire global lock
560 * when accessing the EC.
561 */
562 rv = acpi_eval_integer(sc->sc_handle, "_GLK", &v);
563 if (ACPI_FAILURE(rv)) {
564 if (rv != AE_NOT_FOUND)
565 printf("%s: unable to evaluate _GLK: %s\n",
566 sc->sc_dev.dv_xname,
567 AcpiFormatException(rv));
568 sc->sc_glk = 0;
569 } else
570 sc->sc_glk = v;
571
572 /*
573 * Install GPE handler.
574 *
575 * We evaluate the _GPE method to find the GPE bit used by the
576 * Embedded Controller to signal status (SCI).
577 */
578 rv = acpi_eval_integer(sc->sc_handle, "_GPE", &v);
579 if (ACPI_FAILURE(rv)) {
580 printf("%s: unable to evaluate _GPE: %s\n",
581 sc->sc_dev.dv_xname, AcpiFormatException(rv));
582 goto out;
583 }
584 sc->sc_gpebit = v;
585
586 /*
587 * Install a handler for this EC's GPE bit. Note that EC SCIs are
588 * treated as both edge- and level-triggered interrupts; in other words
589 * we clear the status bit immediately after getting an EC-SCI, then
590 * again after we're done processing the event. This guarantees that
591 * events we cause while performing a transaction (e.g. IBE/OBF) get
592 * cleared before re-enabling the GPE.
593 */
594 rv = AcpiInstallGpeHandler(NULL, sc->sc_gpebit,
595 ACPI_EVENT_EDGE_TRIGGERED, EcGpeHandler, sc);
596 if (ACPI_FAILURE(rv)) {
597 printf("%s: unable to install GPE handler: %s\n",
598 sc->sc_dev.dv_xname, AcpiFormatException(rv));
599 goto out;
600 }
601
602 /* Install address space handler. */
603 rv = AcpiInstallAddressSpaceHandler(sc->sc_handle,
604 ACPI_ADR_SPACE_EC, EcSpaceHandler, EcSpaceSetup, sc);
605 if (ACPI_FAILURE(rv)) {
606 printf("%s: unable to install address space handler: %s\n",
607 sc->sc_dev.dv_xname, AcpiFormatException(rv));
608 (void)AcpiRemoveGpeHandler(NULL, sc->sc_gpebit,
609 EcGpeHandler);
610 }
611
612 out:
613 acpi_resource_cleanup(&res);
614 return_VOID;
615 }
616
617 static void
618 EcGpeQueryHandler(void *Context)
619 {
620 struct acpi_ec_softc *sc = Context;
621 UINT8 Data;
622 ACPI_STATUS rv;
623 char qxx[5];
624
625 ACPI_FUNCTION_TRACE(__FUNCTION__);
626
627 for (;;) {
628 /*
629 * Check EC_SCI.
630 *
631 * Bail out if the EC_SCI bit of the status register is not
632 * set. Note that this function should only be called when
633 * this bit is set (polling is used to detect IBE/OBF events).
634 *
635 * It is safe to do this without locking the controller, as
636 * it's OK to call EcQuery when there's no data ready; in the
637 * worst case we should just find nothing waiting for us and
638 * bail.
639 */
640 if ((EC_CSR_READ(sc) & EC_EVENT_SCI) == 0)
641 break;
642
643 EcLock(sc);
644
645 /*
646 * Find out why the EC is signalling us
647 */
648 rv = EcQuery(sc, &Data);
649
650 EcUnlock(sc);
651
652 /*
653 * If we failed to get anything from the EC, give up.
654 */
655 if (ACPI_FAILURE(rv)) {
656 printf("%s: GPE query failed: %s\n",
657 sc->sc_dev.dv_xname, AcpiFormatException(rv));
658 break;
659 }
660
661 /*
662 * Evaluate _Qxx to respond to the controller.
663 */
664 snprintf(qxx, sizeof(qxx), "_Q%02X", Data);
665 rv = AcpiEvaluateObject(sc->sc_handle, qxx,
666 NULL, NULL);
667
668 /*
669 * Ignore spurious query requests.
670 */
671 if (ACPI_FAILURE(rv) &&
672 (Data != 0 || rv != AE_NOT_FOUND)) {
673 printf("%s: evaluation of GPE query method %s "
674 "failed: %s\n", sc->sc_dev.dv_xname, qxx,
675 AcpiFormatException(rv));
676 }
677 }
678
679 /* I know I request Level trigger cleanup */
680 rv = AcpiClearGpe(NULL, sc->sc_gpebit, ACPI_NOT_ISR);
681 if (ACPI_FAILURE(rv))
682 printf("%s: AcpiClearGpe failed: %s\n", sc->sc_dev.dv_xname,
683 AcpiFormatException(rv));
684
685 return_VOID;
686 }
687
688 static void
689 EcGpeHandler(void *Context)
690 {
691 struct acpi_ec_softc *sc = Context;
692 uint32_t csrvalue;
693 ACPI_STATUS rv;
694
695 /*
696 * If EC is locked, the intr must process EcRead/Write wait only.
697 * Query request must be pending.
698 */
699 simple_lock(&sc->sc_slock);
700 if (sc->sc_flags & EC_F_TRANSACTION) {
701 csrvalue = EC_CSR_READ(sc);
702 sc->sc_flags |= EC_F_PENDQUERY;
703
704 if ((csrvalue & EC_FLAG_OUTPUT_BUFFER) != 0 ||
705 (csrvalue & EC_FLAG_INPUT_BUFFER) == 0) {
706 sc->sc_csrvalue = csrvalue;
707 wakeup(&sc->sc_csrvalue);
708 }
709 simple_unlock(&sc->sc_slock);
710 } else {
711 simple_unlock(&sc->sc_slock);
712 /* Enqueue GpeQuery handler. */
713 rv = AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
714 EcGpeQueryHandler, Context);
715 if (ACPI_FAILURE(rv))
716 printf("%s: failed to enqueue query handler: %s\n",
717 sc->sc_dev.dv_xname, AcpiFormatException(rv));
718 }
719 }
720
721 static ACPI_STATUS
722 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
723 void **RegionContext)
724 {
725
726 ACPI_FUNCTION_TRACE(__FUNCTION__);
727
728 /*
729 * Just pass the context through, there's nothing to do here.
730 */
731 if (Function == ACPI_REGION_DEACTIVATE)
732 *RegionContext = NULL;
733 else
734 *RegionContext = Context;
735
736 return_ACPI_STATUS(AE_OK);
737 }
738
739 static ACPI_STATUS
740 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
741 ACPI_INTEGER *Value, void *Context, void *RegionContext)
742 {
743 struct acpi_ec_softc *sc = Context;
744 ACPI_STATUS rv = AE_OK;
745 EC_REQUEST EcRequest;
746 int i;
747
748 ACPI_FUNCTION_TRACE_U32(__FUNCTION__, (UINT32)Address);
749
750 if ((Address > 0xFF) || (width % 8 != 0) || (Value == NULL) ||
751 (Context == NULL))
752 return_ACPI_STATUS(AE_BAD_PARAMETER);
753
754 switch (Function) {
755 case ACPI_READ:
756 EcRequest.Command = EC_COMMAND_READ;
757 EcRequest.Address = Address;
758 (*Value) = 0;
759 break;
760
761 case ACPI_WRITE:
762 EcRequest.Command = EC_COMMAND_WRITE;
763 EcRequest.Address = Address;
764 break;
765
766 default:
767 printf("%s: invalid Address Space function: %d\n",
768 sc->sc_dev.dv_xname, Function);
769 return_ACPI_STATUS(AE_BAD_PARAMETER);
770 }
771
772 /*
773 * Perform the transaction.
774 */
775 for (i = 0; i < width; i += 8) {
776 if (Function == ACPI_READ)
777 EcRequest.Data = 0;
778 else
779 EcRequest.Data = (UINT8)((*Value) >> i);
780
781 rv = EcTransaction(sc, &EcRequest);
782 if (ACPI_FAILURE(rv))
783 break;
784
785 (*Value) |= (UINT32)EcRequest.Data << i;
786 if (++EcRequest.Address == 0)
787 return_ACPI_STATUS(AE_BAD_PARAMETER);
788 }
789
790 return_ACPI_STATUS(rv);
791 }
792
793 static ACPI_STATUS
794 EcWaitEventIntr(struct acpi_ec_softc *sc, EC_EVENT Event)
795 {
796 EC_STATUS EcStatus;
797 int i;
798
799 ACPI_FUNCTION_TRACE_U32(__FUNCTION__, (UINT32)Event);
800
801 /* XXX Need better test for "yes, you have interrupts". */
802 if (cold)
803 return_ACPI_STATUS(EcWaitEvent(sc, Event));
804
805 if (EcIsLocked(sc) == 0)
806 printf("%s: EcWaitEventIntr called without EC lock!\n",
807 sc->sc_dev.dv_xname);
808
809 EcStatus = EC_CSR_READ(sc);
810
811 /* Too long? */
812 for (i = 0; i < 10; i++) {
813 /* Check EC status against the desired event. */
814 if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
815 (EcStatus & EC_FLAG_OUTPUT_BUFFER) != 0)
816 return_ACPI_STATUS(AE_OK);
817
818 if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
819 (EcStatus & EC_FLAG_INPUT_BUFFER) == 0)
820 return_ACPI_STATUS(AE_OK);
821
822 sc->sc_csrvalue = 0;
823 /* XXXJRT Sleeping with a lock held? */
824 if (tsleep(&sc->sc_csrvalue, 0, "EcWait", 1) != EWOULDBLOCK)
825 EcStatus = sc->sc_csrvalue;
826 else
827 EcStatus = EC_CSR_READ(sc);
828 }
829 return_ACPI_STATUS(AE_ERROR);
830 }
831
832 static ACPI_STATUS
833 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
834 {
835 EC_STATUS EcStatus;
836 UINT32 i = 0;
837
838 if (EcIsLocked(sc) == 0)
839 printf("%s: EcWaitEvent called without EC lock!\n",
840 sc->sc_dev.dv_xname);
841
842 /*
843 * Stall 1us:
844 * ----------
845 * Stall for 1 microsecond before reading the status register
846 * for the first time. This allows the EC to set the IBF/OBF
847 * bit to its proper state.
848 *
849 * XXX it is not clear why we read the CSR twice.
850 */
851 AcpiOsStall(1);
852 EcStatus = EC_CSR_READ(sc);
853
854 /*
855 * Wait For Event:
856 * ---------------
857 * Poll the EC status register to detect completion of the last
858 * command. Wait up to 10ms (in 100us chunks) for this to occur.
859 */
860 for (i = 0; i < 100; i++) {
861 EcStatus = EC_CSR_READ(sc);
862
863 if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
864 (EcStatus & EC_FLAG_OUTPUT_BUFFER) != 0)
865 return AE_OK;
866
867 if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
868 (EcStatus & EC_FLAG_INPUT_BUFFER) == 0)
869 return AE_OK;
870
871 AcpiOsStall(10);
872 }
873
874 return AE_ERROR;
875 }
876
877 static ACPI_STATUS
878 EcQuery(struct acpi_ec_softc *sc, UINT8 *Data)
879 {
880 ACPI_STATUS rv;
881
882 if (EcIsLocked(sc) == 0)
883 printf("%s: EcQuery called without EC lock!\n",
884 sc->sc_dev.dv_xname);
885
886 EC_CSR_WRITE(sc, EC_COMMAND_QUERY);
887 rv = EcWaitEventIntr(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
888 if (ACPI_SUCCESS(rv))
889 *Data = EC_DATA_READ(sc);
890
891 if (ACPI_FAILURE(rv))
892 printf("%s: timed out waiting for EC to respond to "
893 "EC_COMMAND_QUERY\n", sc->sc_dev.dv_xname);
894
895 return rv;
896 }
897
898 static ACPI_STATUS
899 EcTransaction(struct acpi_ec_softc *sc, EC_REQUEST *EcRequest)
900 {
901 ACPI_STATUS rv;
902
903 EcLock(sc);
904
905 /*
906 * Perform the transaction.
907 */
908 switch (EcRequest->Command) {
909 case EC_COMMAND_READ:
910 rv = EcRead(sc, EcRequest->Address, &(EcRequest->Data));
911 break;
912
913 case EC_COMMAND_WRITE:
914 rv = EcWrite(sc, EcRequest->Address, &(EcRequest->Data));
915 break;
916
917 default:
918 rv = AE_SUPPORT;
919 break;
920 }
921
922 EcUnlock(sc);
923
924 return rv;
925 }
926
927 static ACPI_STATUS
928 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
929 {
930 ACPI_STATUS rv;
931
932 if (EcIsLocked(sc) == 0)
933 printf("%s: EcRead called without EC lock!\n",
934 sc->sc_dev.dv_xname);
935
936 /* EcBurstEnable(EmbeddedController); */
937
938 EC_CSR_WRITE(sc, EC_COMMAND_READ);
939 if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
940 AE_OK) {
941 printf("%s: EcRead: timeout waiting for EC to process "
942 "read command\n", sc->sc_dev.dv_xname);
943 return rv;
944 }
945
946 EC_DATA_WRITE(sc, Address);
947 if ((rv = EcWaitEventIntr(sc, EC_EVENT_OUTPUT_BUFFER_FULL)) !=
948 AE_OK) {
949 printf("%s: EcRead: timeout waiting for EC to send data\n",
950 sc->sc_dev.dv_xname);
951 return rv;
952 }
953
954 (*Data) = EC_DATA_READ(sc);
955
956 /* EcBurstDisable(EmbeddedController); */
957
958 return AE_OK;
959 }
960
961 static ACPI_STATUS
962 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
963 {
964 ACPI_STATUS rv;
965
966 if (EcIsLocked(sc) == 0)
967 printf("%s: EcWrite called without EC lock!\n",
968 sc->sc_dev.dv_xname);
969
970 /* EcBurstEnable(EmbeddedController); */
971
972 EC_CSR_WRITE(sc, EC_COMMAND_WRITE);
973 if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
974 AE_OK) {
975 printf("%s: EcWrite: timeout waiting for EC to process "
976 "write command\n", sc->sc_dev.dv_xname);
977 return rv;
978 }
979
980 EC_DATA_WRITE(sc, Address);
981 if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
982 AE_OK) {
983 printf("%s: EcWrite: timeout waiting for EC to process "
984 "address\n", sc->sc_dev.dv_xname);
985 return rv;
986 }
987
988 EC_DATA_WRITE(sc, *Data);
989 if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
990 AE_OK) {
991 printf("%s: EcWrite: timeout waiting for EC to process "
992 "data\n", sc->sc_dev.dv_xname);
993 return rv;
994 }
995
996 /* EcBurstDisable(EmbeddedController); */
997
998 return AE_OK;
999 }
1000