acpi_ec.c revision 1.1 1 /* $NetBSD: acpi_ec.c,v 1.1 2001/09/29 05:34:23 thorpej 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/param.h>
175 #include <sys/systm.h>
176 #include <sys/device.h>
177 #include <sys/proc.h>
178 #include <sys/kernel.h>
179
180 #include <machine/bus.h>
181
182 #include <dev/acpi/acpica.h>
183 #include <dev/acpi/acpivar.h>
184 #include <dev/acpi/acpi_ecreg.h>
185
186 #define _COMPONENT ACPI_EC
187 MODULE_NAME("EC")
188
189 struct acpi_ec_softc {
190 struct device sc_dev; /* base device glue */
191 struct acpi_devnode *sc_node; /* our ACPI devnode */
192
193 struct acpi_resources sc_res; /* our bus resources */
194
195 UINT32 sc_gpebit; /* our GPE interrupt bit */
196
197 bus_space_tag_t sc_data_st; /* space tag for data register */
198 bus_space_handle_t sc_data_sh; /* space handle for data register */
199
200 bus_space_tag_t sc_csr_st; /* space tag for control register */
201 bus_space_handle_t sc_csr_sh; /* space handle for control register */
202
203 int sc_flags; /* see below */
204
205 uint32_t sc_csrvalue; /* saved control register */
206 };
207
208 #define EC_F_LOCKED 0x01 /* EC is locked */
209 #define EC_F_PENDQUERY 0x02 /* query is pending */
210
211 #define EC_LOCK_TIMEOUT 1000 /* 1ms */
212
213 #define EC_DATA_READ(sc) \
214 bus_space_read_1((sc)->sc_data_st, (sc)->sc_data_sh, 0)
215 #define EC_DATA_WRITE(sc, v) \
216 bus_space_write_1((sc)->sc_data_st, (sc)->sc_data_sh, 0, (v))
217
218 #define EC_CSR_READ(sc) \
219 bus_space_read_1((sc)->sc_csr_st, (sc)->sc_csr_sh, 0)
220 #define EC_CSR_WRITE(sc, v) \
221 bus_space_write_1((sc)->sc_csr_st, (sc)->sc_csr_sh, 0, (v))
222
223 static __inline ACPI_STATUS
224 EcLock(struct acpi_ec_softc *sc)
225 {
226 ACPI_STATUS status;
227
228 status = AcpiAcquireGlobalLock();
229 if (status == AE_OK)
230 (sc)->sc_flags |= EC_F_LOCKED;
231 return (status);
232 }
233
234 static __inline void
235 EcUnlock(struct acpi_ec_softc *sc)
236 {
237
238 (sc)->sc_flags &= ~EC_F_LOCKED;
239 AcpiReleaseGlobalLock();
240 }
241
242 static __inline int
243 EcIsLocked(struct acpi_ec_softc *sc)
244 {
245
246 return (((sc)->sc_flags & EC_F_LOCKED) != 0);
247 }
248
249 typedef struct {
250 EC_COMMAND Command;
251 UINT8 Address;
252 UINT8 Data;
253 } EC_REQUEST;
254
255 static void EcGpeHandler(void *Context);
256 static ACPI_STATUS EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
257 void *Context, void **return_Context);
258 static ACPI_STATUS EcSpaceHandler(UINT32 Function,
259 ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
260 UINT32 *Value, void *Context, void *RegionContext);
261
262 static ACPI_STATUS EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
263 static ACPI_STATUS EcQuery(struct acpi_ec_softc *sc, UINT8 *Data);
264 static ACPI_STATUS EcTransaction(struct acpi_ec_softc *sc,
265 EC_REQUEST *EcRequest);
266 static ACPI_STATUS EcRead(struct acpi_ec_softc *sc, UINT8 Address,
267 UINT8 *Data);
268 static ACPI_STATUS EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
269 UINT8 *Data);
270
271 int acpiec_match(struct device *, struct cfdata *, void *);
272 void acpiec_attach(struct device *, struct device *, void *);
273
274 struct cfattach acpiec_ca = {
275 sizeof(struct acpi_ec_softc), acpiec_match, acpiec_attach,
276 };
277
278 /*
279 * acpiec_match:
280 *
281 * Autoconfiguration `match' routine.
282 */
283 int
284 acpiec_match(struct device *parent, struct cfdata *match, void *aux)
285 {
286 struct acpi_attach_args *aa = aux;
287
288 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
289 return (0);
290
291 if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C09") == 0)
292 return (1);
293
294 return (0);
295 }
296
297 /*
298 * acpiec_attach:
299 *
300 * Autoconfiguration `attach' routine.
301 */
302 void
303 acpiec_attach(struct device *parent, struct device *self, void *aux)
304 {
305 struct acpi_ec_softc *sc = (void *) self;
306 struct acpi_attach_args *aa = aux;
307 struct acpi_io *io0, *io1;
308 ACPI_STATUS rv;
309
310 FUNCTION_TRACE(__FUNCTION__);
311
312 printf(": ACPI Embedded Controller\n");
313
314 sc->sc_node = aa->aa_node;
315
316 /* Parse our resources. */
317 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "parsing EC resources\n"));
318 rv = acpi_resource_parse(&sc->sc_dev, sc->sc_node, &sc->sc_res,
319 &acpi_resource_parse_ops_default);
320 if (rv != AE_OK) {
321 printf("%s: unable to parse resources: %d\n",
322 sc->sc_dev.dv_xname, rv);
323 return;
324 }
325
326 sc->sc_data_st = aa->aa_iot;
327 io0 = acpi_res_io(&sc->sc_res, 0);
328 if (io0 == NULL) {
329 printf("%s: unable to find data register resource\n",
330 sc->sc_dev.dv_xname);
331 return;
332 }
333 if (bus_space_map(sc->sc_data_st, io0->ar_base, io0->ar_length,
334 0, &sc->sc_data_sh) != 0) {
335 printf("%s: unable to map data register\n",
336 sc->sc_dev.dv_xname);
337 return;
338 }
339
340 sc->sc_csr_st = aa->aa_iot;
341 io1 = acpi_res_io(&sc->sc_res, 1);
342 if (io1 == NULL) {
343 printf("%s: unable to find csr register resource\n",
344 sc->sc_dev.dv_xname);
345 return;
346 }
347 if (bus_space_map(sc->sc_csr_st, io1->ar_base, io1->ar_length,
348 0, &sc->sc_csr_sh) != 0) {
349 printf("%s: unable to map csr register\n",
350 sc->sc_dev.dv_xname);
351 return;
352 }
353
354 /*
355 * Install GPE handler.
356 *
357 * We evaluate the _GPE method to find the GPE bit used by the
358 * Embedded Controller to signal status (SCI).
359 */
360 if ((rv = acpi_eval_integer(sc->sc_node->ad_handle, "_GPE",
361 &sc->sc_gpebit)) != AE_OK) {
362 printf("%s: unable to evaluate _GPE: %d\n",
363 sc->sc_dev.dv_xname, rv);
364 return;
365 }
366
367 /*
368 * Install a handler for this EC's GPE bit. Note that EC SCIs are
369 * treated as both edge- and level-triggered interrupts; in other words
370 * we clear the status bit immediately after getting an EC-SCI, then
371 * again after we're done processing the event. This guarantees that
372 * events we cause while performing a transaction (e.g. IBE/OBF) get
373 * cleared before re-enabling the GPE.
374 */
375 if ((rv = AcpiInstallGpeHandler(sc->sc_gpebit,
376 ACPI_EVENT_LEVEL_TRIGGERED | ACPI_EVENT_EDGE_TRIGGERED,
377 EcGpeHandler, sc)) != AE_OK) {
378 printf("%s: unable to install GPE handler: %d\n",
379 sc->sc_dev.dv_xname, rv);
380 return;
381 }
382
383 /* Install address space handler. */
384 if ((rv = AcpiInstallAddressSpaceHandler(sc->sc_node->ad_handle,
385 ACPI_ADR_SPACE_EC, EcSpaceHandler, EcSpaceSetup, sc)) != AE_OK) {
386 printf("%s: unable to install address space handler: %d\n",
387 sc->sc_dev.dv_xname, rv);
388 return;
389 }
390
391 return_VOID;
392 }
393
394 static void
395 EcGpeQueryHandler(void *Context)
396 {
397 struct acpi_ec_softc *sc = Context;
398 UINT8 Data;
399 ACPI_STATUS Status;
400 char qxx[5];
401
402 FUNCTION_TRACE(__FUNCTION__);
403
404 for (;;) {
405 /*
406 * Check EC_SCI.
407 *
408 * Bail out if the EC_SCI bit of the status register is not
409 * set. Note that this function should only be called when
410 * this bit is set (polling is used to detect IBE/OBF events).
411 *
412 * It is safe to do this without locking the controller, as
413 * it's OK to call EcQuery when there's no data ready; in the
414 * worst case we should just find nothing waiting for us and
415 * bail.
416 */
417 if ((EC_CSR_READ(sc) & EC_EVENT_SCI) == 0)
418 break;
419
420 /*
421 * Find out why the EC is signalling us
422 */
423 Status = EcQuery(sc, &Data);
424
425 /*
426 * If we failed to get anything from the EC, give up.
427 */
428 if (Status != AE_OK) {
429 printf("%s: GPE query failed: %d\n",
430 sc->sc_dev.dv_xname, Status);
431 break;
432 }
433
434 /*
435 * Evaluate _Qxx to respond to the controller.
436 */
437 sprintf(qxx, "_Q%02x", Data);
438 strupr(qxx);
439 Status = AcpiEvaluateObject(sc->sc_node->ad_handle, qxx,
440 NULL, NULL);
441
442 /*
443 * Ignore spurious query requests.
444 */
445 if (Status != AE_OK &&
446 (Data != 0 || Status != AE_NOT_FOUND)) {
447 printf("%s: evaluation of GPE query method %s "
448 "failed: %d\n", sc->sc_dev.dv_xname, qxx, Status);
449 }
450 }
451
452 /* I know I request Level trigger cleanup */
453 if (AcpiClearEvent(sc->sc_gpebit, ACPI_EVENT_GPE) != AE_OK)
454 printf("%s: AcpiClearEvent failed\n", sc->sc_dev.dv_xname);
455 if (AcpiEnableEvent(sc->sc_gpebit, ACPI_EVENT_GPE) != AE_OK)
456 printf("%s: AcpiEnableEvent failed\n", sc->sc_dev.dv_xname);
457
458 return_VOID;
459 }
460
461 static void
462 EcGpeHandler(void *Context)
463 {
464 struct acpi_ec_softc *sc = Context;
465 uint32_t csrvalue;
466
467 /*
468 * If EC is locked, the intr must process EcRead/Write wait only.
469 * Query request must be pending.
470 */
471 if (EcIsLocked(sc)) {
472 csrvalue = EC_CSR_READ(sc);
473 if (csrvalue & EC_EVENT_SCI)
474 sc->sc_flags |= EC_F_PENDQUERY;
475
476 if ((csrvalue & EC_FLAG_OUTPUT_BUFFER) != 0 ||
477 (csrvalue & EC_FLAG_INPUT_BUFFER) == 0) {
478 sc->sc_csrvalue = csrvalue;
479 wakeup(&sc->sc_csrvalue);
480 }
481 } else {
482 /* Enqueue GpeQuery handler. */
483 if (AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
484 EcGpeQueryHandler, Context) != AE_OK) {
485 printf("%s: failed to enqueue query handler\n",
486 sc->sc_dev.dv_xname);
487 }
488 }
489 }
490
491 static ACPI_STATUS
492 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
493 void **RegionContext)
494 {
495
496 FUNCTION_TRACE(__FUNCTION__);
497
498 /*
499 * Just pass the context through, there's nothing to do here.
500 */
501 *RegionContext = Context;
502
503 return_ACPI_STATUS(AE_OK);
504 }
505
506 static ACPI_STATUS
507 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
508 UINT32 *Value, void *Context, void *RegionContext)
509 {
510 struct acpi_ec_softc *sc = Context;
511 ACPI_STATUS Status = AE_OK;
512 EC_REQUEST EcRequest;
513 int i;
514
515 FUNCTION_TRACE_U32(__FUNCTION__, (UINT32)Address);
516
517 if ((Address > 0xFF) || (width % 8 != 0) || (Value == NULL) ||
518 (Context == NULL))
519 return_ACPI_STATUS(AE_BAD_PARAMETER);
520
521 switch (Function) {
522 case ACPI_READ_ADR_SPACE:
523 EcRequest.Command = EC_COMMAND_READ;
524 EcRequest.Address = Address;
525 (*Value) = 0;
526 break;
527
528 case ACPI_WRITE_ADR_SPACE:
529 EcRequest.Command = EC_COMMAND_WRITE;
530 EcRequest.Address = Address;
531 break;
532
533 default:
534 printf("%s: invalid Address Space function: %d\n",
535 sc->sc_dev.dv_xname, Function);
536 return_ACPI_STATUS(AE_BAD_PARAMETER);
537 }
538
539 /*
540 * Perform the transaction.
541 */
542 (*Value) = 0;
543 for (i = 0; i < width; i += 8) {
544 if (Function == ACPI_READ_ADR_SPACE)
545 EcRequest.Data = 0;
546 else
547 EcRequest.Data = (UINT8)((*Value) >> i);
548
549 if ((Status = EcTransaction(sc, &EcRequest)) != AE_OK)
550 break;
551
552 (*Value) |= (UINT32)EcRequest.Data << i;
553 if (++EcRequest.Address == 0)
554 return_ACPI_STATUS(AE_BAD_PARAMETER);
555 }
556
557 return_ACPI_STATUS(Status);
558 }
559
560 static ACPI_STATUS
561 EcWaitEventIntr(struct acpi_ec_softc *sc, EC_EVENT Event)
562 {
563 EC_STATUS EcStatus;
564 int i;
565
566 FUNCTION_TRACE_U32(__FUNCTION__, (UINT32)Event);
567
568 /* XXX Need better test for "yes, you have interrupts". */
569 if (cold)
570 return_ACPI_STATUS(EcWaitEvent(sc, Event));
571
572 if (EcIsLocked(sc) == 0)
573 printf("%s: EcWaitEventIntr called without EC lock!\n",
574 sc->sc_dev.dv_xname);
575
576 EcStatus = EC_CSR_READ(sc);
577
578 /* Too long? */
579 for (i = 0; i < 10; i++) {
580 /* Check EC status against the desired event. */
581 if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
582 (EcStatus & EC_FLAG_OUTPUT_BUFFER) != 0)
583 return_ACPI_STATUS(AE_OK);
584
585 if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
586 (EcStatus & EC_FLAG_INPUT_BUFFER) == 0)
587 return_ACPI_STATUS(AE_OK);
588
589 sc->sc_csrvalue = 0;
590 /* XXXJRT Sleeping with a lock held? */
591 if (tsleep(&sc->sc_csrvalue, 0, "EcWait", 1) != EWOULDBLOCK)
592 EcStatus = sc->sc_csrvalue;
593 else
594 EcStatus = EC_CSR_READ(sc);
595 }
596 return_ACPI_STATUS(AE_ERROR);
597 }
598
599 static ACPI_STATUS
600 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
601 {
602 EC_STATUS EcStatus;
603 UINT32 i = 0;
604
605 if (EcIsLocked(sc) == 0)
606 printf("%s: EcWaitEvent called without EC lock!\n",
607 sc->sc_dev.dv_xname);
608
609 /*
610 * Stall 1us:
611 * ----------
612 * Stall for 1 microsecond before reading the status register
613 * for the first time. This allows the EC to set the IBF/OBF
614 * bit to its proper state.
615 *
616 * XXX it is not clear why we read the CSR twice.
617 */
618 AcpiOsStall(1);
619 EcStatus = EC_CSR_READ(sc);
620
621 /*
622 * Wait For Event:
623 * ---------------
624 * Poll the EC status register to detect completion of the last
625 * command. Wait up to 10ms (in 100us chunks) for this to occur.
626 */
627 for (i = 0; i < 100; i++) {
628 EcStatus = EC_CSR_READ(sc);
629
630 if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
631 (EcStatus & EC_FLAG_OUTPUT_BUFFER) != 0)
632 return (AE_OK);
633
634 if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
635 (EcStatus & EC_FLAG_INPUT_BUFFER) == 0)
636 return (AE_OK);
637
638 AcpiOsStall(10);
639 }
640
641 return (AE_ERROR);
642 }
643
644 static ACPI_STATUS
645 EcQuery(struct acpi_ec_softc *sc, UINT8 *Data)
646 {
647 ACPI_STATUS Status;
648
649 if ((Status = EcLock(sc)) != AE_OK)
650 return (Status);
651
652 EC_CSR_WRITE(sc, EC_COMMAND_QUERY);
653 Status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
654 if (Status == AE_OK)
655 *Data = EC_DATA_READ(sc);
656
657 EcUnlock(sc);
658
659 if (Status != AE_OK)
660 printf("s: timed out waiting for EC to respond to "
661 "EC_COMMAND_QUERY\n", sc->sc_dev.dv_xname);
662
663 return (Status);
664 }
665
666 static ACPI_STATUS
667 EcTransaction(struct acpi_ec_softc *sc, EC_REQUEST *EcRequest)
668 {
669 ACPI_STATUS Status;
670
671 if ((Status = EcLock(sc)) != AE_OK)
672 return (Status);
673
674 /*
675 * Perform the transaction.
676 */
677 switch (EcRequest->Command) {
678 case EC_COMMAND_READ:
679 Status = EcRead(sc, EcRequest->Address, &(EcRequest->Data));
680 break;
681
682 case EC_COMMAND_WRITE:
683 Status = EcWrite(sc, EcRequest->Address, &(EcRequest->Data));
684 break;
685
686 default:
687 Status = AE_SUPPORT;
688 break;
689 }
690
691 /*
692 * Clear & Re-Enable the EC GPE:
693 * -----------------------------
694 * 'Consume' any EC GPE events that we generated while performing
695 * the transaction (e.g. IBF/OBF). Clearing the GPE here shouldn't
696 * have an adverse affect on outstanding EC-SCI's, as the source
697 * (EC-SCI) will still be high and thus should trigger the GPE
698 * immediately after we re-enabling it.
699 */
700 if (sc->sc_flags & EC_F_PENDQUERY) {
701 if (AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
702 EcGpeQueryHandler, sc) != AE_OK)
703 printf("%s: unable to queue pending query\n",
704 sc->sc_dev.dv_xname);
705 sc->sc_flags &= ~EC_F_PENDQUERY;
706 }
707
708 if (AcpiClearEvent(sc->sc_gpebit, ACPI_EVENT_GPE) != AE_OK)
709 printf("%s: EcRequest: unable to clear EC GPE\n",
710 sc->sc_dev.dv_xname);
711 if (AcpiEnableEvent(sc->sc_gpebit, ACPI_EVENT_GPE) != AE_OK)
712 printf("%s: EcRequest: unable to reenable EC GPE\n",
713 sc->sc_dev.dv_xname);
714
715 EcUnlock(sc);
716
717 return(Status);
718 }
719
720 static ACPI_STATUS
721 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
722 {
723 ACPI_STATUS Status;
724
725 if (EcIsLocked(sc) == 0)
726 printf("%s: EcRead called without EC lock!\n",
727 sc->sc_dev.dv_xname);
728
729 /* EcBurstEnable(EmbeddedController); */
730
731 EC_CSR_WRITE(sc, EC_COMMAND_READ);
732 if ((Status = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
733 AE_OK) {
734 printf("%s: EcRead: timeout waiting for EC to process "
735 "read command\n", sc->sc_dev.dv_xname);
736 return (Status);
737 }
738
739 EC_DATA_WRITE(sc, Address);
740 if ((Status = EcWaitEventIntr(sc, EC_EVENT_OUTPUT_BUFFER_FULL)) !=
741 AE_OK) {
742 printf("%s: EcRead: timeout waiting for EC to send data\n",
743 sc->sc_dev.dv_xname);
744 return (Status);
745 }
746
747 (*Data) = EC_DATA_READ(sc);
748
749 /* EcBurstDisable(EmbeddedController); */
750
751 return (AE_OK);
752 }
753
754 static ACPI_STATUS
755 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
756 {
757 ACPI_STATUS Status;
758
759 if (EcIsLocked(sc) == 0)
760 printf("%s: EcWrite called without EC lock!\n",
761 sc->sc_dev.dv_xname);
762
763 /* EcBurstEnable(EmbeddedController); */
764
765 EC_CSR_WRITE(sc, EC_COMMAND_WRITE);
766 if ((Status = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
767 AE_OK) {
768 printf("%s: EcWrite: timeout waiting for EC to process "
769 "write command\n", sc->sc_dev.dv_xname);
770 return (Status);
771 }
772
773 EC_DATA_WRITE(sc, Address);
774 if ((Status = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
775 AE_OK) {
776 printf("%s: EcWrite: timeout waiting for EC to process "
777 "address\n", sc->sc_dev.dv_xname);
778 return (Status);
779 }
780
781 EC_DATA_WRITE(sc, *Data);
782 if ((Status = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
783 AE_OK) {
784 printf("%s: EcWrite: timeout waiting for EC to process "
785 "data\n", sc->sc_dev.dv_xname);
786 return (Status);
787 }
788
789 /* EcBurstDisable(EmbeddedController); */
790
791 return (AE_OK);
792 }
793