xref: /freebsd/sys/dev/acpica/acpi_ec.c (revision 6adf353a56a161443406b44a45d00c688ca7b857)
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 
151 /*
152  * Hooks for the ACPI CA debugging infrastructure
153  */
154 #define _COMPONENT	ACPI_EC
155 MODULE_NAME("EC")
156 
157 /*
158  * EC_COMMAND:
159  * -----------
160  */
161 typedef UINT8				EC_COMMAND;
162 
163 #define EC_COMMAND_UNKNOWN		((EC_COMMAND) 0x00)
164 #define EC_COMMAND_READ			((EC_COMMAND) 0x80)
165 #define EC_COMMAND_WRITE		((EC_COMMAND) 0x81)
166 #define EC_COMMAND_BURST_ENABLE		((EC_COMMAND) 0x82)
167 #define EC_COMMAND_BURST_DISABLE	((EC_COMMAND) 0x83)
168 #define EC_COMMAND_QUERY		((EC_COMMAND) 0x84)
169 
170 /*
171  * EC_STATUS:
172  * ----------
173  * The encoding of the EC status register is illustrated below.
174  * Note that a set bit (1) indicates the property is TRUE
175  * (e.g. if bit 0 is set then the output buffer is full).
176  * +-+-+-+-+-+-+-+-+
177  * |7|6|5|4|3|2|1|0|
178  * +-+-+-+-+-+-+-+-+
179  *  | | | | | | | |
180  *  | | | | | | | +- Output Buffer Full?
181  *  | | | | | | +--- Input Buffer Full?
182  *  | | | | | +----- <reserved>
183  *  | | | | +------- Data Register is Command Byte?
184  *  | | | +--------- Burst Mode Enabled?
185  *  | | +----------- SCI Event?
186  *  | +------------- SMI Event?
187  *  +--------------- <Reserved>
188  *
189  */
190 typedef UINT8				EC_STATUS;
191 
192 #define EC_FLAG_OUTPUT_BUFFER		((EC_STATUS) 0x01)
193 #define EC_FLAG_INPUT_BUFFER		((EC_STATUS) 0x02)
194 #define EC_FLAG_BURST_MODE		((EC_STATUS) 0x10)
195 #define EC_FLAG_SCI			((EC_STATUS) 0x20)
196 
197 /*
198  * EC_EVENT:
199  * ---------
200  */
201 typedef UINT8				EC_EVENT;
202 
203 #define EC_EVENT_UNKNOWN		((EC_EVENT) 0x00)
204 #define EC_EVENT_OUTPUT_BUFFER_FULL	((EC_EVENT) 0x01)
205 #define EC_EVENT_INPUT_BUFFER_EMPTY	((EC_EVENT) 0x02)
206 #define EC_EVENT_SCI			((EC_EVENT) 0x20)
207 
208 /*
209  * Register access primitives
210  */
211 #define EC_GET_DATA(sc)							\
212 	bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
213 
214 #define EC_SET_DATA(sc, v)						\
215 	bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
216 
217 #define EC_GET_CSR(sc)							\
218 	bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
219 
220 #define EC_SET_CSR(sc, v)						\
221 	bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
222 
223 /*
224  * Driver softc.
225  */
226 struct acpi_ec_softc {
227     device_t		ec_dev;
228     ACPI_HANDLE		ec_handle;
229     UINT32		ec_gpebit;
230 
231     int			ec_data_rid;
232     struct resource	*ec_data_res;
233     bus_space_tag_t	ec_data_tag;
234     bus_space_handle_t	ec_data_handle;
235 
236     int			ec_csr_rid;
237     struct resource	*ec_csr_res;
238     bus_space_tag_t	ec_csr_tag;
239     bus_space_handle_t	ec_csr_handle;
240 
241     int			ec_locked;
242     int			ec_pendquery;
243     int			ec_csrvalue;
244 };
245 
246 #define EC_LOCK_TIMEOUT	1000	/* 1ms */
247 
248 static __inline ACPI_STATUS
249 EcLock(struct acpi_ec_softc *sc)
250 {
251     ACPI_STATUS	status;
252 
253     status = AcpiAcquireGlobalLock();
254     (sc)->ec_locked = 1;
255     return(status);
256 }
257 
258 static __inline void
259 EcUnlock(struct acpi_ec_softc *sc)
260 {
261     (sc)->ec_locked = 0;
262     AcpiReleaseGlobalLock();
263 }
264 
265 static __inline int
266 EcIsLocked(struct acpi_ec_softc *sc)
267 {
268     return((sc)->ec_locked != 0);
269 }
270 
271 typedef struct
272 {
273     EC_COMMAND              Command;
274     UINT8                   Address;
275     UINT8                   Data;
276 } EC_REQUEST;
277 
278 static void		EcGpeHandler(void *Context);
279 static ACPI_STATUS	EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
280 					   void *Context, void **return_Context);
281 static ACPI_STATUS	EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width, UINT32 *Value,
282 				      void *Context, void *RegionContext);
283 
284 static ACPI_STATUS	EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
285 static ACPI_STATUS	EcQuery(struct acpi_ec_softc *sc, UINT8 *Data);
286 static ACPI_STATUS	EcTransaction(struct acpi_ec_softc *sc, EC_REQUEST *EcRequest);
287 static ACPI_STATUS	EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data);
288 static ACPI_STATUS	EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data);
289 
290 static void		acpi_ec_identify(driver_t driver, device_t bus);
291 static int		acpi_ec_probe(device_t dev);
292 static int		acpi_ec_attach(device_t dev);
293 
294 static device_method_t acpi_ec_methods[] = {
295     /* Device interface */
296     DEVMETHOD(device_identify,	acpi_ec_identify),
297     DEVMETHOD(device_probe,	acpi_ec_probe),
298     DEVMETHOD(device_attach,	acpi_ec_attach),
299 
300     {0, 0}
301 };
302 
303 static driver_t acpi_ec_driver = {
304     "acpi_ec",
305     acpi_ec_methods,
306     sizeof(struct acpi_ec_softc),
307 };
308 
309 devclass_t acpi_ec_devclass;
310 DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
311 
312 /*
313  * Look for an ECDT table and if we find one, set up a default EC
314  * space handler to catch possible attempts to access EC space before
315  * we have a real driver instance in place.
316  * We're not really an identify routine, but because we get called
317  * before most other things, this works out OK.
318  */
319 static void
320 acpi_ec_identify(driver_t driver, device_t bus)
321 {
322     FUNCTION_TRACE(__func__);
323 
324     /* XXX implement - need an ACPI 2.0 system to test this */
325 
326     return_VOID;
327 }
328 
329 /*
330  * We could setup resources in the probe routine in order to have them printed
331  * when the device is attached.
332  */
333 static int
334 acpi_ec_probe(device_t dev)
335 {
336 
337     if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
338 	!acpi_disabled("ec") &&
339 	acpi_MatchHid(dev, "PNP0C09")) {
340 
341 	/*
342 	 * Set device description
343 	 */
344 	device_set_desc(dev, "embedded controller");
345 
346 	return(0);
347     }
348     return(ENXIO);
349 }
350 
351 static int
352 acpi_ec_attach(device_t dev)
353 {
354     struct acpi_ec_softc	*sc;
355     ACPI_STATUS			Status;
356 
357     FUNCTION_TRACE(__func__);
358 
359     /*
360      * Fetch/initialise softc
361      */
362     sc = device_get_softc(dev);
363     bzero(sc, sizeof(*sc));
364     sc->ec_dev = dev;
365     sc->ec_handle = acpi_get_handle(dev);
366 
367     /*
368      * Evaluate resources
369      */
370     DEBUG_PRINT(TRACE_RESOURCES, ("parsing EC resources\n"));
371     acpi_parse_resources(sc->ec_dev, sc->ec_handle, &acpi_res_parse_set);
372 
373     /*
374      * Attach bus resources
375      */
376     sc->ec_data_rid = 0;
377     if ((sc->ec_data_res = bus_alloc_resource(sc->ec_dev, SYS_RES_IOPORT, &sc->ec_data_rid,
378 					      0, ~0, 1, RF_ACTIVE)) == NULL) {
379 	device_printf(dev, "can't allocate data port\n");
380 	return_VALUE(ENXIO);
381     }
382     sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
383     sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
384 
385     sc->ec_csr_rid = 1;
386     if ((sc->ec_csr_res = bus_alloc_resource(sc->ec_dev, SYS_RES_IOPORT, &sc->ec_csr_rid,
387 					     0, ~0, 1, RF_ACTIVE)) == NULL) {
388 	device_printf(dev, "can't allocate command/status port\n");
389 	return_VALUE(ENXIO);
390     }
391     sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
392     sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
393 
394     /*
395      * Install GPE handler
396      *
397      * Evaluate the _GPE method to find the GPE bit used by the EC to signal
398      * status (SCI).
399      */
400     DEBUG_PRINT(TRACE_RESOURCES, ("attaching GPE\n"));
401     if ((Status = acpi_EvaluateInteger(sc->ec_handle, "_GPE", &sc->ec_gpebit)) != AE_OK) {
402 	device_printf(dev, "can't evaluate _GPE - %s\n", AcpiFormatException(Status));
403 	return_VALUE(ENXIO);
404     }
405 
406     /*
407      * Install a handler for this EC's GPE bit.  Note that EC SCIs are
408      * treated as both edge- and level-triggered interrupts; in other words
409      * we clear the status bit immediately after getting an EC-SCI, then
410      * again after we're done processing the event.  This guarantees that
411      * events we cause while performing a transaction (e.g. IBE/OBF) get
412      * cleared before re-enabling the GPE.
413      */
414     if ((Status = AcpiInstallGpeHandler(sc->ec_gpebit, ACPI_EVENT_LEVEL_TRIGGERED | ACPI_EVENT_EDGE_TRIGGERED,
415 					EcGpeHandler, sc)) != AE_OK) {
416 	device_printf(dev, "can't install GPE handler for %s - %s\n",
417 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
418 	return_VALUE(ENXIO);
419     }
420 
421     /*
422      * Install address space handler
423      */
424     DEBUG_PRINT(TRACE_RESOURCES, ("attaching address space handler\n"));
425     if ((Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
426 						 EcSpaceHandler, EcSpaceSetup, sc)) != AE_OK) {
427 	device_printf(dev, "can't install address space handler for %s - %s\n",
428 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
429 	panic("very suck");
430 	return_VALUE(ENXIO);
431     }
432     DEBUG_PRINT(TRACE_RESOURCES, ("attach complete\n"));
433 
434     return_VALUE(0);
435 }
436 
437 static void
438 EcGpeQueryHandler(void *Context)
439 {
440     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
441     UINT8			Data;
442     ACPI_STATUS			Status;
443     char			qxx[5];
444 
445     FUNCTION_TRACE(__func__);
446 
447     for (;;) {
448 
449 	/*
450 	 * Check EC_SCI.
451 	 *
452 	 * Bail out if the EC_SCI bit of the status register is not set.
453 	 * Note that this function should only be called when
454 	 * this bit is set (polling is used to detect IBE/OBF events).
455 	 *
456 	 * It is safe to do this without locking the controller, as it's
457 	 * OK to call EcQuery when there's no data ready; in the worst
458 	 * case we should just find nothing waiting for us and bail.
459 	 */
460 	if (!(EC_GET_CSR(sc) & EC_EVENT_SCI))
461 	    break;
462 
463 	/*
464 	 * Find out why the EC is signalling us
465 	 */
466 	Status = EcQuery(sc, &Data);
467 
468 	/*
469 	 * If we failed to get anything from the EC, give up
470 	 */
471 	if (Status != AE_OK) {
472 	    device_printf(sc->ec_dev, "GPE query failed - %s\n", AcpiFormatException(Status));
473 	    break;
474 	}
475 
476 	/*
477 	 * Evaluate _Qxx to respond to the controller.
478 	 */
479 	sprintf(qxx, "_Q%02x", Data);
480 	strupr(qxx);
481 	Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
482 	/*
483 	 * Ignore spurious query requests.
484 	 */
485 	if (Status != AE_OK && (Data != 0 || Status != AE_NOT_FOUND)) {
486 	    device_printf(sc->ec_dev, "evaluation of GPE query method %s failed - %s\n",
487 			  qxx, AcpiFormatException(Status));
488 	}
489     }
490         /* I know I request Level trigger cleanup */
491     if(AcpiClearEvent(sc->ec_gpebit,ACPI_EVENT_GPE) != AE_OK)
492 	    printf("EcGpeQueryHandler:ClearEvent Failed\n");
493     if(AcpiEnableEvent(sc->ec_gpebit,ACPI_EVENT_GPE) != AE_OK)
494 	    printf("EcGpeQueryHandler:EnableEvent Failed\n");
495     return_VOID;
496 }
497 
498 /*
499  * Handle a GPE sent to us.
500  */
501 static void
502 EcGpeHandler(void *Context)
503 {
504     struct acpi_ec_softc *sc = Context;
505     int csrvalue;
506 
507     /*
508      * If EC is locked, the intr must process EcRead/Write wait only.
509      * Query request must be pending.
510      */
511     if (EcIsLocked(sc)){
512 	csrvalue = EC_GET_CSR(sc);
513 	if (csrvalue & EC_EVENT_SCI)
514 	    sc->ec_pendquery = 1;
515 	if ((csrvalue & EC_FLAG_OUTPUT_BUFFER)
516 	    || !(csrvalue & EC_FLAG_INPUT_BUFFER)) {
517 	    sc->ec_csrvalue = csrvalue;
518 	    wakeup((void *)&sc->ec_csrvalue);
519 	}
520     }else{
521 	/* Queue GpeQuery Handler */
522 	if (AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
523 				    EcGpeQueryHandler,Context) != AE_OK){
524 	    printf("QueryHandler Queuing Failed\n");
525 	}
526     }
527     return;
528 }
529 
530 static ACPI_STATUS
531 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context, void **RegionContext)
532 {
533 
534     FUNCTION_TRACE(__func__);
535 
536     /*
537      * Just pass the context through, there's nothing to do here.
538      */
539     *RegionContext = Context;
540 
541     return_ACPI_STATUS(AE_OK);
542 }
543 
544 static ACPI_STATUS
545 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width, UINT32 *Value,
546 	       void *Context, void *RegionContext)
547 {
548     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
549     ACPI_STATUS			Status = AE_OK;
550     EC_REQUEST			EcRequest;
551     int				i;
552 
553     FUNCTION_TRACE_U32(__func__, (UINT32)Address);
554 
555     if ((Address > 0xFF) || (width % 8 != 0) || (Value == NULL) || (Context == NULL))
556         return_ACPI_STATUS(AE_BAD_PARAMETER);
557 
558     switch (Function) {
559     case ACPI_READ_ADR_SPACE:
560         EcRequest.Command = EC_COMMAND_READ;
561         EcRequest.Address = Address;
562 	(*Value) = 0;
563         break;
564 
565     case ACPI_WRITE_ADR_SPACE:
566         EcRequest.Command = EC_COMMAND_WRITE;
567         EcRequest.Address = Address;
568         break;
569 
570     default:
571 	device_printf(sc->ec_dev, "invalid Address Space function %d\n", Function);
572         return_ACPI_STATUS(AE_BAD_PARAMETER);
573     }
574 
575     /*
576      * Perform the transaction.
577      */
578     for (i = 0; i < width; i += 8) {
579 	if (Function == ACPI_READ_ADR_SPACE)
580 	    EcRequest.Data = 0;
581 	else
582 	    EcRequest.Data = (UINT8)((*Value) >> i);
583 	if ((Status = EcTransaction(sc, &EcRequest)) != AE_OK)
584 	    break;
585         (*Value) |= (UINT32)EcRequest.Data << i;
586 	if (++EcRequest.Address == 0)
587             return_ACPI_STATUS(AE_BAD_PARAMETER);
588     }
589     return_ACPI_STATUS(Status);
590 }
591 
592 /*
593  * Wait for an event interrupt for a specific condition.
594  */
595 static ACPI_STATUS
596 EcWaitEventIntr(struct acpi_ec_softc *sc, EC_EVENT Event)
597 {
598     EC_STATUS	EcStatus;
599     int		i;
600 
601     FUNCTION_TRACE_U32(__func__, (UINT32)Event);
602 
603     /* XXX this should test whether interrupts are available some other way */
604     if(cold)
605 	return_ACPI_STATUS(EcWaitEvent(sc, Event));
606 
607     if (!EcIsLocked(sc))
608 	device_printf(sc->ec_dev, "EcWaitEventIntr called without EC lock!\n");
609 
610     EcStatus = EC_GET_CSR(sc);
611 
612     /* XXX waiting too long? */
613     for(i = 0; i < 10; i++){
614 	/*
615 	 * Check EC status against the desired event.
616 	 */
617     	if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
618 	    (EcStatus & EC_FLAG_OUTPUT_BUFFER))
619 	    return_ACPI_STATUS(AE_OK);
620 
621 	if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
622 	    !(EcStatus & EC_FLAG_INPUT_BUFFER))
623 	    return_ACPI_STATUS(AE_OK);
624 
625 	sc->ec_csrvalue = 0;
626 	if (ACPI_MSLEEP(&sc->ec_csrvalue, &acpi_mutex, PZERO, "EcWait", 1) != EWOULDBLOCK){
627 	    EcStatus = sc->ec_csrvalue;
628 	}else{
629 	    EcStatus = EC_GET_CSR(sc);
630 	}
631     }
632     return_ACPI_STATUS(AE_ERROR);
633 }
634 
635 static ACPI_STATUS
636 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
637 {
638     EC_STATUS	EcStatus;
639     UINT32	i = 0;
640 
641     if (!EcIsLocked(sc))
642 	device_printf(sc->ec_dev, "EcWaitEvent called without EC lock!\n");
643 
644     /*
645      * Stall 1us:
646      * ----------
647      * Stall for 1 microsecond before reading the status register
648      * for the first time.  This allows the EC to set the IBF/OBF
649      * bit to its proper state.
650      *
651      * XXX it is not clear why we read the CSR twice.
652      */
653     AcpiOsStall(1);
654     EcStatus = EC_GET_CSR(sc);
655 
656     /*
657      * Wait For Event:
658      * ---------------
659      * Poll the EC status register to detect completion of the last
660      * command.  Wait up to 10ms (in 10us chunks) for this to occur.
661      */
662     for (i = 0; i < 1000; i++) {
663 	EcStatus = EC_GET_CSR(sc);
664 
665         if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
666             (EcStatus & EC_FLAG_OUTPUT_BUFFER))
667 	    return(AE_OK);
668 
669 	if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
670             !(EcStatus & EC_FLAG_INPUT_BUFFER))
671 	    return(AE_OK);
672 
673 	AcpiOsStall(10);
674     }
675 
676     return(AE_ERROR);
677 }
678 
679 static ACPI_STATUS
680 EcQuery(struct acpi_ec_softc *sc, UINT8 *Data)
681 {
682     ACPI_STATUS	Status;
683 
684     if ((Status = EcLock(sc)) != AE_OK)
685 	return(Status);
686 
687     EC_SET_CSR(sc, EC_COMMAND_QUERY);
688     Status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
689     if (Status == AE_OK)
690 	*Data = EC_GET_DATA(sc);
691 
692     EcUnlock(sc);
693 
694     if (Status != AE_OK)
695 	device_printf(sc->ec_dev, "timeout waiting for EC to respond to EC_COMMAND_QUERY\n");
696     return(Status);
697 }
698 
699 static ACPI_STATUS
700 EcTransaction(struct acpi_ec_softc *sc, EC_REQUEST *EcRequest)
701 {
702     ACPI_STATUS	Status;
703 
704     /*
705      * Lock the EC
706      */
707     if ((Status = EcLock(sc)) != AE_OK)
708 	return(Status);
709 
710     /*
711      * Perform the transaction.
712      */
713     switch (EcRequest->Command) {
714     case EC_COMMAND_READ:
715 	Status = EcRead(sc, EcRequest->Address, &(EcRequest->Data));
716 	break;
717 
718     case EC_COMMAND_WRITE:
719 	Status = EcWrite(sc, EcRequest->Address, &(EcRequest->Data));
720 	break;
721 
722     default:
723 	Status = AE_SUPPORT;
724 	break;
725     }
726 
727     /*
728      * Unlock the EC
729      */
730     EcUnlock(sc);
731 
732     /*
733      * Clear & Re-Enable the EC GPE:
734      * -----------------------------
735      * 'Consume' any EC GPE events that we generated while performing
736      * the transaction (e.g. IBF/OBF).	Clearing the GPE here shouldn't
737      * have an adverse affect on outstanding EC-SCI's, as the source
738      * (EC-SCI) will still be high and thus should trigger the GPE
739      * immediately after we re-enabling it.
740      */
741     if (sc->ec_pendquery){
742 	    if(AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
743 		EcGpeQueryHandler, sc) != AE_OK)
744 		    printf("Pend Query Queuing Failed\n");
745 	    sc->ec_pendquery = 0;
746     }
747 
748     if (AcpiClearEvent(sc->ec_gpebit, ACPI_EVENT_GPE) != AE_OK)
749 	device_printf(sc->ec_dev, "EcRequest: Unable to clear the EC GPE.\n");
750     if (AcpiEnableEvent(sc->ec_gpebit, ACPI_EVENT_GPE) != AE_OK)
751 	device_printf(sc->ec_dev, "EcRequest: Unable to re-enable the EC GPE.\n");
752 
753     return(Status);
754 }
755 
756 
757 static ACPI_STATUS
758 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
759 {
760     ACPI_STATUS	Status;
761 
762     if (!EcIsLocked(sc))
763 	device_printf(sc->ec_dev, "EcRead called without EC lock!\n");
764 
765     /*EcBurstEnable(EmbeddedController);*/
766 
767     EC_SET_CSR(sc, EC_COMMAND_READ);
768     if ((Status = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) != AE_OK) {
769 	device_printf(sc->ec_dev, "EcRead: Failed waiting for EC to process read command.\n");
770 	return(Status);
771     }
772 
773     EC_SET_DATA(sc, Address);
774     if ((Status = EcWaitEventIntr(sc, EC_EVENT_OUTPUT_BUFFER_FULL)) != AE_OK) {
775 	device_printf(sc->ec_dev, "EcRead: Failed waiting for EC to send data.\n");
776 	return(Status);
777     }
778 
779     (*Data) = EC_GET_DATA(sc);
780 
781     /*EcBurstDisable(EmbeddedController);*/
782 
783     return(AE_OK);
784 }
785 
786 static ACPI_STATUS
787 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
788 {
789     ACPI_STATUS	Status;
790 
791     if (!EcIsLocked(sc))
792 	device_printf(sc->ec_dev, "EcWrite called without EC lock!\n");
793 
794     /*EcBurstEnable(EmbeddedController);*/
795 
796     EC_SET_CSR(sc, EC_COMMAND_WRITE);
797     if ((Status = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) != AE_OK) {
798 	device_printf(sc->ec_dev, "EcWrite: Failed waiting for EC to process write command.\n");
799 	return(Status);
800     }
801 
802     EC_SET_DATA(sc, Address);
803     if ((Status = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) != AE_OK) {
804 	device_printf(sc->ec_dev, "EcRead: Failed waiting for EC to process address.\n");
805 	return(Status);
806     }
807 
808     EC_SET_DATA(sc, *Data);
809     if ((Status = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) != AE_OK) {
810 	device_printf(sc->ec_dev, "EcWrite: Failed waiting for EC to process data.\n");
811 	return(Status);
812     }
813 
814     /*EcBurstDisable(EmbeddedController);*/
815 
816     return(AE_OK);
817 }
818