xref: /freebsd/sys/dev/acpica/acpi_ec.c (revision 817420dc8eac7df799c78f5309b75092b7f7cd40)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  *	$FreeBSD$
28  */
29 /******************************************************************************
30  *
31  * 1. Copyright Notice
32  *
33  * Some or all of this work - Copyright (c) 1999, Intel Corp.  All rights
34  * reserved.
35  *
36  * 2. License
37  *
38  * 2.1. This is your license from Intel Corp. under its intellectual property
39  * rights.  You may have additional license terms from the party that provided
40  * you this software, covering your right to use that party's intellectual
41  * property rights.
42  *
43  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
44  * copy of the source code appearing in this file ("Covered Code") an
45  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
46  * base code distributed originally by Intel ("Original Intel Code") to copy,
47  * make derivatives, distribute, use and display any portion of the Covered
48  * Code in any form, with the right to sublicense such rights; and
49  *
50  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
51  * license (with the right to sublicense), under only those claims of Intel
52  * patents that are infringed by the Original Intel Code, to make, use, sell,
53  * offer to sell, and import the Covered Code and derivative works thereof
54  * solely to the minimum extent necessary to exercise the above copyright
55  * license, and in no event shall the patent license extend to any additions
56  * to or modifications of the Original Intel Code.  No other license or right
57  * is granted directly or by implication, estoppel or otherwise;
58  *
59  * The above copyright and patent license is granted only if the following
60  * conditions are met:
61  *
62  * 3. Conditions
63  *
64  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
65  * Redistribution of source code of any substantial portion of the Covered
66  * Code or modification with rights to further distribute source must include
67  * the above Copyright Notice, the above License, this list of Conditions,
68  * and the following Disclaimer and Export Compliance provision.  In addition,
69  * Licensee must cause all Covered Code to which Licensee contributes to
70  * contain a file documenting the changes Licensee made to create that Covered
71  * Code and the date of any change.  Licensee must include in that file the
72  * documentation of any changes made by any predecessor Licensee.  Licensee
73  * must include a prominent statement that the modification is derived,
74  * directly or indirectly, from Original Intel Code.
75  *
76  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
77  * Redistribution of source code of any substantial portion of the Covered
78  * Code or modification without rights to further distribute source must
79  * include the following Disclaimer and Export Compliance provision in the
80  * documentation and/or other materials provided with distribution.  In
81  * addition, Licensee may not authorize further sublicense of source of any
82  * portion of the Covered Code, and must include terms to the effect that the
83  * license from Licensee to its licensee is limited to the intellectual
84  * property embodied in the software Licensee provides to its licensee, and
85  * not to intellectual property embodied in modifications its licensee may
86  * make.
87  *
88  * 3.3. Redistribution of Executable. Redistribution in executable form of any
89  * substantial portion of the Covered Code or modification must reproduce the
90  * above Copyright Notice, and the following Disclaimer and Export Compliance
91  * provision in the documentation and/or other materials provided with the
92  * distribution.
93  *
94  * 3.4. Intel retains all right, title, and interest in and to the Original
95  * Intel Code.
96  *
97  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
98  * Intel shall be used in advertising or otherwise to promote the sale, use or
99  * other dealings in products derived from or relating to the Covered Code
100  * without prior written authorization from Intel.
101  *
102  * 4. Disclaimer and Export Compliance
103  *
104  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
105  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
106  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
107  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
108  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
109  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
110  * PARTICULAR PURPOSE.
111  *
112  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
113  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
114  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
115  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
116  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
117  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
118  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
119  * LIMITED REMEDY.
120  *
121  * 4.3. Licensee shall not export, either directly or indirectly, any of this
122  * software or system incorporating such software without first obtaining any
123  * required license or other approval from the U. S. Department of Commerce or
124  * any other agency or department of the United States Government.  In the
125  * event Licensee exports any such software from the United States or
126  * re-exports any such software from a foreign destination, Licensee shall
127  * ensure that the distribution and export/re-export of the software is in
128  * compliance with all laws, regulations, orders, or other restrictions of the
129  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
130  * any of its subsidiaries will export/re-export any technical data, process,
131  * software, or service, directly or indirectly, to any country for which the
132  * United States government or any agency thereof requires an export license,
133  * other governmental approval, or letter of assurance, without first obtaining
134  * such license, approval or letter.
135  *
136  *****************************************************************************/
137 
138 #include "opt_acpi.h"
139 #include <sys/param.h>
140 #include <sys/kernel.h>
141 #include <sys/bus.h>
142 
143 #include <machine/bus.h>
144 #include <machine/resource.h>
145 #include <sys/rman.h>
146 
147 #include "acpi.h"
148 
149 #include <dev/acpica/acpivar.h>
150 #include <dev/acpica/acpi_ecreg.h>
151 
152 struct acpi_ec_softc {
153     device_t		ec_dev;
154     ACPI_HANDLE		ec_handle;
155     ACPI_HANDLE		ec_semaphore;
156     UINT32		ec_gpebit;
157 
158     int			ec_data_rid;
159     struct resource	*ec_data_res;
160     bus_space_tag_t	ec_data_tag;
161     bus_space_handle_t	ec_data_handle;
162 
163     int			ec_csr_rid;
164     struct resource	*ec_csr_res;
165     bus_space_tag_t	ec_csr_tag;
166     bus_space_handle_t	ec_csr_handle;
167 
168     int			ec_locked;
169 };
170 
171 #define EC_LOCK_TIMEOUT	1000	/* 1ms */
172 
173 static __inline ACPI_STATUS
174 EcLock(struct acpi_ec_softc *sc)
175 {
176     ACPI_STATUS	status;
177 
178     status = AcpiOsWaitSemaphore((sc)->ec_semaphore, 1, EC_LOCK_TIMEOUT);
179     (sc)->ec_locked = 1;
180     return(status);
181 }
182 
183 static __inline void
184 EcUnlock(struct acpi_ec_softc *sc)
185 {
186     (sc)->ec_locked = 0;
187     AcpiOsSignalSemaphore((sc)->ec_semaphore, 1);
188 }
189 
190 static __inline int
191 EcIsLocked(struct acpi_ec_softc *sc)
192 {
193     return((sc)->ec_locked != 0);
194 }
195 
196 typedef struct
197 {
198     EC_COMMAND              Command;
199     UINT8                   Address;
200     UINT8                   Data;
201 } EC_REQUEST;
202 
203 static struct acpi_ec_softc	acpi_ec_default;	/* for the default EC handler */
204 
205 static void		EcGpeHandler(void *Context);
206 static ACPI_STATUS	EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
207 					   void *Context, void **return_Context);
208 static ACPI_STATUS	EcSpaceHandler(UINT32 Function, UINT32 Address, UINT32 width, UINT32 *Value,
209 				      void *Context, void *RegionContext);
210 static ACPI_STATUS	EcDefaultSpaceHandler(UINT32 Function, UINT32 Address, UINT32 width, UINT32 *Value,
211 					      void *Context, void *RegionContext);
212 
213 static ACPI_STATUS	EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
214 static ACPI_STATUS	EcQuery(struct acpi_ec_softc *sc, UINT8 *Data);
215 static ACPI_STATUS	EcTransaction(struct acpi_ec_softc *sc, EC_REQUEST *EcRequest);
216 static ACPI_STATUS	EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data);
217 static ACPI_STATUS	EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data);
218 
219 static void		acpi_ec_identify(driver_t driver, device_t bus);
220 static int		acpi_ec_probe(device_t dev);
221 static int		acpi_ec_attach(device_t dev);
222 
223 static device_method_t acpi_ec_methods[] = {
224     /* Device interface */
225     DEVMETHOD(device_identify,	acpi_ec_identify),
226     DEVMETHOD(device_probe,	acpi_ec_probe),
227     DEVMETHOD(device_attach,	acpi_ec_attach),
228 
229     {0, 0}
230 };
231 
232 static driver_t acpi_ec_driver = {
233     "acpi_ec",
234     acpi_ec_methods,
235     sizeof(struct acpi_ec_softc),
236 };
237 
238 devclass_t acpi_ec_devclass;
239 DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
240 
241 /*
242  * Look for an ECDT table and if we find one, set up a default EC
243  * space handler to catch possible attempts to access EC space before
244  * we have a real driver instance in place.
245  * We're not really an identify routine, but because we get called
246  * before most other things, this works out OK.
247  */
248 static void
249 acpi_ec_identify(driver_t driver, device_t bus)
250 {
251     ACPI_STATUS	Status;
252 
253     /* XXX implement - need an ACPI 2.0 system to test this */
254 
255     /*
256      * XXX install a do-nothing handler at the top of the namespace to catch
257      *     bogus accesses being made due to apparent interpreter bugs.
258      */
259     acpi_ec_default.ec_dev = bus;
260     if ((Status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, ADDRESS_SPACE_EC,
261 						 &EcDefaultSpaceHandler, &EcSpaceSetup,
262 						 &acpi_ec_default)) != AE_OK) {
263 	device_printf(acpi_ec_default.ec_dev, "can't install default EC address space handler - %s\n",
264 		      acpi_strerror(Status));
265     }
266 }
267 
268 /*
269  * We could setup resources in the probe routine in order to have them printed
270  * when the device is attached.
271  */
272 static int
273 acpi_ec_probe(device_t dev)
274 {
275     if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
276 	acpi_MatchHid(dev, "PNP0C09")) {
277 
278 	/*
279 	 * Set device description
280 	 */
281 	device_set_desc(dev, "embedded controller");
282 
283 	return(0);
284     }
285     return(ENXIO);
286 }
287 
288 static int
289 acpi_ec_attach(device_t dev)
290 {
291     struct acpi_ec_softc	*sc;
292     ACPI_BUFFER			*bufp;
293     UINT32			*param;
294     ACPI_STATUS			Status;
295     struct acpi_object_list	*args;
296 
297     /*
298      * Fetch/initialise softc
299      */
300     sc = device_get_softc(dev);
301     sc->ec_dev = dev;
302     sc->ec_handle = acpi_get_handle(dev);
303 
304     /*
305      * Evaluate resources
306      */
307     acpi_parse_resources(sc->ec_dev, sc->ec_handle, &acpi_res_parse_set);
308 
309     /*
310      * Attach bus resources
311      */
312     sc->ec_data_rid = 0;
313     if ((sc->ec_data_res = bus_alloc_resource(sc->ec_dev, SYS_RES_IOPORT, &sc->ec_data_rid,
314 					      0, ~0, 1, RF_ACTIVE)) == NULL) {
315 	device_printf(dev, "can't allocate data port\n");
316 	return(ENXIO);
317     }
318     sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
319     sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
320 
321     sc->ec_csr_rid = 1;
322     if ((sc->ec_csr_res = bus_alloc_resource(sc->ec_dev, SYS_RES_IOPORT, &sc->ec_csr_rid,
323 					     0, ~0, 1, RF_ACTIVE)) == NULL) {
324 	device_printf(dev, "can't allocate command/status port\n");
325 	return(ENXIO);
326     }
327     sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
328     sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
329 
330     /*
331      * Create serialisation semaphore
332      */
333     if ((Status = AcpiOsCreateSemaphore(1, 1, &sc->ec_semaphore)) != AE_OK) {
334 	device_printf(dev, "can't create semaphore - %s\n", acpi_strerror(Status));
335 	return(ENXIO);
336     }
337 
338     /*
339      * Install GPE handler
340      *
341      * Evaluate the _GPE method to find the GPE bit used by the EC to signal
342      * status (SCI).
343      */
344     if ((bufp = acpi_AllocBuffer(16)) == NULL)
345 	return(ENOMEM);
346     if ((Status = AcpiEvaluateObject(sc->ec_handle, "_GPE", NULL, bufp)) != AE_OK) {
347 	device_printf(dev, "can't evaluate _GPE method - %s\n", acpi_strerror(Status));
348 	return(ENXIO);
349     }
350     param = (UINT32 *)bufp->Pointer;
351     if (param[0] != ACPI_TYPE_NUMBER) {
352 	device_printf(dev, "_GPE method returned bad result\n");
353 	return(ENXIO);
354     }
355     sc->ec_gpebit = param[1];
356     AcpiOsFree(bufp);
357 
358     /*
359      * Install a handler for this EC's GPE bit.  Note that EC SCIs are
360      * treated as both edge- and level-triggered interrupts; in other words
361      * we clear the status bit immediately after getting an EC-SCI, then
362      * again after we're done processing the event.  This guarantees that
363      * events we cause while performing a transaction (e.g. IBE/OBF) get
364      * cleared before re-enabling the GPE.
365      */
366     if ((Status = AcpiInstallGpeHandler(sc->ec_gpebit, ACPI_EVENT_LEVEL_TRIGGERED | ACPI_EVENT_EDGE_TRIGGERED,
367 					EcGpeHandler, sc)) != AE_OK) {
368 	device_printf(dev, "can't install GPE handler - %s\n", acpi_strerror(Status));
369 	return(ENXIO);
370     }
371 
372     /*
373      * Install address space handler
374      */
375     if ((Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ADDRESS_SPACE_EC,
376 						 &EcSpaceHandler, &EcSpaceSetup, sc)) != AE_OK) {
377 	device_printf(dev, "can't install address space handler - %s\n", acpi_strerror(Status));
378 	return(ENXIO);
379     }
380 
381     /*
382      * Evaluate _REG to indicate that the region is now available.
383      */
384     if ((args = acpi_AllocObjectList(2)) == NULL)
385 	return(ENOMEM);
386     args->object[0].Type = ACPI_TYPE_NUMBER;
387     args->object[0].Number.Value = ADDRESS_SPACE_EC;
388     args->object[1].Type = ACPI_TYPE_NUMBER;
389     args->object[1].Number.Value = 1;
390     Status = AcpiEvaluateObject(sc->ec_handle, "_REG", (ACPI_OBJECT_LIST *)args, NULL);
391     AcpiOsFree(args);
392     /*
393      * If evaluation failed for some reason other than that the method didn't
394      * exist, that's bad and we should not attach.
395      */
396     if ((Status != AE_OK) && (Status != AE_NOT_FOUND)) {
397 	device_printf(dev, "can't evaluate _REG method - %s\n", acpi_strerror(Status));
398 	return(ENXIO);
399     }
400 
401     return(0);
402 }
403 
404 static void
405 EcGpeHandler(void *Context)
406 {
407     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
408     UINT8			Data;
409     ACPI_STATUS			Status;
410     char			qxx[5];
411 
412     for (;;) {
413 
414 	/*
415 	 * Check EC_SCI.
416 	 *
417 	 * Bail out if the EC_SCI bit of the status register is not set.
418 	 * Note that this function should only be called when
419 	 * this bit is set (polling is used to detect IBE/OBF events).
420 	 *
421 	 * It is safe to do this without locking the controller, as it's
422 	 * OK to call EcQuery when there's no data ready; in the worst
423 	 * case we should just find nothing waiting for us and bail.
424 	 */
425 	if (!(EC_GET_CSR(sc) & EC_EVENT_SCI))
426 	    break;
427 
428 	/*
429 	 * Find out why the EC is signalling us
430 	 */
431 	Status = EcQuery(sc, &Data);
432 
433 	/*
434 	 * If we failed to get anything from the EC, give up
435 	 */
436 	if (Status != AE_OK) {
437 	    device_printf(sc->ec_dev, "GPE query failed - %s\n", acpi_strerror(Status));
438 	    break;
439 	}
440 
441 	/*
442 	 * Evaluate _Qxx to respond to the controller.
443 	 */
444 	sprintf(qxx, "_Q%02x", Data);
445 	strupr(qxx);
446 	if ((Status - AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL)) != AE_OK) {
447 	    device_printf(sc->ec_dev, "evaluation of GPE query method %s failed - %s\n",
448 			  qxx, acpi_strerror(Status));
449 	}
450     }
451 }
452 
453 static ACPI_STATUS
454 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context, void **RegionContext)
455 {
456     /*
457      * Just pass the context through, there's nothing to do here.
458      */
459     *RegionContext = Context;
460 
461     return(AE_OK);
462 }
463 
464 static ACPI_STATUS
465 EcSpaceHandler(UINT32 Function, UINT32 Address, UINT32 width, UINT32 *Value, void *Context, void *RegionContext)
466 {
467     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
468     ACPI_STATUS			Status = AE_OK;
469     EC_REQUEST			EcRequest;
470 
471     if ((Address > 0xFF) || (width != 8) || (Value == NULL) || (Context == NULL))
472         return(AE_BAD_PARAMETER);
473 
474     switch (Function) {
475     case ADDRESS_SPACE_READ:
476         EcRequest.Command = EC_COMMAND_READ;
477         EcRequest.Address = Address;
478         EcRequest.Data = 0;
479         break;
480 
481     case ADDRESS_SPACE_WRITE:
482         EcRequest.Command = EC_COMMAND_WRITE;
483         EcRequest.Address = Address;
484         EcRequest.Data = (UINT8)(*Value);
485         break;
486 
487     default:
488 	device_printf(sc->ec_dev, "invalid Address Space function %d\n", Function);
489         return(AE_BAD_PARAMETER);
490     }
491 
492     /*
493      * Perform the transaction.
494      */
495     if ((Status = EcTransaction(sc, &EcRequest)) == AE_OK)
496         (*Value) = (UINT32)EcRequest.Data;
497 
498     return(Status);
499 }
500 
501 static ACPI_STATUS
502 EcDefaultSpaceHandler(UINT32 Function, UINT32 Address, UINT32 width, UINT32 *Value, void *Context, void *RegionContext)
503 {
504     if ((Address > 0xFF) || (width != 8) || (Value == NULL) || (Context == NULL))
505         return(AE_BAD_PARAMETER);
506 
507     switch (Function) {
508     case ADDRESS_SPACE_READ:
509 	printf("ACPI: Illegal EC read from 0x%x\n", Address);
510 	*Value = 0;
511 	break;
512     case ADDRESS_SPACE_WRITE:
513 	printf("ACPI: Illegal EC write 0x%x to 0x%x\n", *Value, Address);
514 	break;
515     default:
516 	printf("ACPI: Illegal EC unknown operation");
517 	break;
518     }
519     /* let things keep going */
520     return(AE_OK);
521 }
522 
523 static ACPI_STATUS
524 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
525 {
526     EC_STATUS	EcStatus;
527     UINT32	i = 0;
528 
529     if (!EcIsLocked(sc))
530 	device_printf(sc->ec_dev, "EcWaitEvent called without EC lock!\n");
531 
532     /*
533      * Stall 1us:
534      * ----------
535      * Stall for 1 microsecond before reading the status register
536      * for the first time.  This allows the EC to set the IBF/OBF
537      * bit to its proper state.
538      *
539      * XXX it is not clear why we read the CSR twice.
540      */
541     AcpiOsSleepUsec(1);
542     EcStatus = EC_GET_CSR(sc);
543 
544     /*
545      * Wait For Event:
546      * ---------------
547      * Poll the EC status register to detect completion of the last
548      * command.  Wait up to 10ms (in 100us chunks) for this to occur.
549      */
550     for (i = 0; i < 100; i++) {
551 	EcStatus = EC_GET_CSR(sc);
552 
553         if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
554             (EcStatus & EC_FLAG_OUTPUT_BUFFER))
555 	    return(AE_OK);
556 
557 	if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
558             !(EcStatus & EC_FLAG_INPUT_BUFFER))
559 	    return(AE_OK);
560 
561 	AcpiOsSleepUsec(100);
562     }
563 
564     return(AE_ERROR);
565 }
566 
567 static ACPI_STATUS
568 EcQuery(struct acpi_ec_softc *sc, UINT8 *Data)
569 {
570     ACPI_STATUS	Status;
571 
572     if ((Status = EcLock(sc)) != AE_OK)
573 	return(Status);
574 
575     EC_SET_CSR(sc, EC_COMMAND_QUERY);
576     Status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
577     if (Status == AE_OK)
578 	*Data = EC_GET_DATA(sc);
579 
580     EcUnlock(sc);
581 
582     if (Status != AE_OK)
583 	device_printf(sc->ec_dev, "timeout waiting for EC to respond to EC_COMMAND_QUERY\n");
584     return(Status);
585 }
586 
587 
588 static ACPI_STATUS
589 EcTransaction(struct acpi_ec_softc *sc, EC_REQUEST *EcRequest)
590 {
591     ACPI_STATUS	Status;
592 
593     /*
594      * Lock the EC
595      */
596     if ((Status = EcLock(sc)) != AE_OK)
597 	return(Status);
598 
599     /*
600      * Disable EC GPE:
601      * ---------------
602      * Disable EC interrupts (GPEs) from occuring during this transaction.
603      * This is done here as EcTransaction() is also called by the EC GPE
604      * handler -- where disabling/re-enabling the EC GPE is automatically
605      * handled by the ACPI Core Subsystem.
606      */
607     if (AcpiDisableEvent(sc->ec_gpebit, ACPI_EVENT_GPE) != AE_OK)
608 	device_printf(sc->ec_dev, "EcRequest: Unable to disable the EC GPE.\n");
609 
610     /*
611      * Perform the transaction.
612      */
613     switch (EcRequest->Command) {
614     case EC_COMMAND_READ:
615 	Status = EcRead(sc, EcRequest->Address, &(EcRequest->Data));
616 	break;
617 
618     case EC_COMMAND_WRITE:
619 	Status = EcWrite(sc, EcRequest->Address, &(EcRequest->Data));
620 	break;
621 
622     default:
623 	Status = AE_SUPPORT;
624 	break;
625     }
626 
627     /*
628      * Clear & Re-Enable the EC GPE:
629      * -----------------------------
630      * 'Consume' any EC GPE events that we generated while performing
631      * the transaction (e.g. IBF/OBF).	Clearing the GPE here shouldn't
632      * have an adverse affect on outstanding EC-SCI's, as the source
633      * (EC-SCI) will still be high and thus should trigger the GPE
634      * immediately after we re-enabling it.
635      */
636     if (AcpiClearEvent(sc->ec_gpebit, ACPI_EVENT_GPE) != AE_OK)
637 	device_printf(sc->ec_dev, "EcRequest: Unable to clear the EC GPE.\n");
638     if (AcpiEnableEvent(sc->ec_gpebit, ACPI_EVENT_GPE) != AE_OK)
639 	device_printf(sc->ec_dev, "EcRequest: Unable to re-enable the EC GPE.\n");
640 
641     /*
642      * Unlock the EC
643      */
644     EcUnlock(sc);
645 
646     return(Status);
647 }
648 
649 
650 static ACPI_STATUS
651 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
652 {
653     ACPI_STATUS	Status;
654 
655     if (!EcIsLocked(sc))
656 	device_printf(sc->ec_dev, "EcRead called without EC lock!\n");
657 
658     /*EcBurstEnable(EmbeddedController);*/
659 
660     EC_SET_CSR(sc, EC_COMMAND_READ);
661     if ((Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) != AE_OK) {
662 	device_printf(sc->ec_dev, "EcRead: Failed waiting for EC to process read command.\n");
663 	return(Status);
664     }
665 
666     EC_SET_DATA(sc, Address);
667     if ((Status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL)) != AE_OK) {
668 	device_printf(sc->ec_dev, "EcRead: Failed waiting for EC to send data.\n");
669 	return(Status);
670     }
671 
672     (*Data) = EC_GET_DATA(sc);
673 
674     /*EcBurstDisable(EmbeddedController);*/
675 
676     return(AE_OK);
677 }
678 
679 static ACPI_STATUS
680 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
681 {
682     ACPI_STATUS	Status;
683 
684     if (!EcIsLocked(sc))
685 	device_printf(sc->ec_dev, "EcWrite called without EC lock!\n");
686 
687     /*EcBurstEnable(EmbeddedController);*/
688 
689     EC_SET_CSR(sc, EC_COMMAND_WRITE);
690     if ((Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) != AE_OK) {
691 	device_printf(sc->ec_dev, "EcWrite: Failed waiting for EC to process write command.\n");
692 	return(Status);
693     }
694 
695     EC_SET_DATA(sc, Address);
696     if ((Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) != AE_OK) {
697 	device_printf(sc->ec_dev, "EcRead: Failed waiting for EC to process address.\n");
698 	return(Status);
699     }
700 
701     EC_SET_DATA(sc, *Data);
702     if ((Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) != AE_OK) {
703 	device_printf(sc->ec_dev, "EcWrite: Failed waiting for EC to process data.\n");
704 	return(Status);
705     }
706 
707     /*EcBurstDisable(EmbeddedController);*/
708 
709     return(AE_OK);
710 }
711