xref: /haiku/src/add-ons/kernel/bus_managers/acpi/EmbeddedController.cpp (revision efafab643ce980e3f3c916795ed302599f6b4f66)
1 /*
2  * Copyright 2013, Jérôme Duval, korli@users.berlios.de.
3  * Copyright (c) 2009 Clemens Zeidler
4  * Copyright (c) 2003-2007 Nate Lawson
5  * Copyright (c) 2000 Michael Smith
6  * Copyright (c) 2000 BSDi
7  * All rights reserved.
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  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 
32 #include "EmbeddedController.h"
33 
34 #include <kernel.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include <condition_variable.h>
40 #include <Errors.h>
41 #include <KernelExport.h>
42 #include <drivers/PCI.h>
43 
44 
45 #define ACPI_EC_DRIVER_NAME "drivers/power/acpi_embedded_controller/driver_v1"
46 
47 #define ACPI_EC_DEVICE_NAME "drivers/power/acpi_embedded_controller/device_v1"
48 
49 /* Base Namespace devices are published to */
50 #define ACPI_EC_BASENAME "power/embedded_controller/%d"
51 
52 // name of pnp generator of path ids
53 #define ACPI_EC_PATHID_GENERATOR "embedded_controller/path_id"
54 
55 
56 uint8
57 bus_space_read_1(int address)
58 {
59 	return gPCIManager->read_io_8(address);
60 }
61 
62 
63 void
64 bus_space_write_1(int address, uint8 value)
65 {
66 	gPCIManager->write_io_8(address, value);
67 }
68 
69 
70 status_t
71 acpi_GetInteger(acpi_device_module_info* acpi, acpi_device& acpiCookie,
72 	const char* path, int* number)
73 {
74 	acpi_data buf;
75 	acpi_object_type object;
76 	buf.pointer = &object;
77 	buf.length = sizeof(acpi_object_type);
78 
79 	// Assume that what we've been pointed at is an Integer object, or
80 	// a method that will return an Integer.
81 	status_t status = acpi->evaluate_method(acpiCookie, path, NULL, &buf);
82 	if (status == B_OK) {
83 		if (object.object_type == ACPI_TYPE_INTEGER)
84 			*number = object.integer.integer;
85 		else
86 			status = B_BAD_VALUE;
87 	}
88 	return status;
89 }
90 
91 
92 acpi_handle
93 acpi_GetReference(acpi_module_info* acpi, acpi_handle scope,
94 	acpi_object_type* obj)
95 {
96 	if (obj == NULL)
97 		return NULL;
98 
99 	switch (obj->object_type) {
100 		case ACPI_TYPE_LOCAL_REFERENCE:
101 		case ACPI_TYPE_ANY:
102 			return obj->reference.handle;
103 
104 		case ACPI_TYPE_STRING:
105 		{
106 			// The String object usually contains a fully-qualified path, so
107 			// scope can be NULL.
108 			// TODO: This may not always be the case.
109 			acpi_handle handle;
110 			if (acpi->get_handle(scope, obj->string.string, &handle)
111 					== B_OK)
112 				return handle;
113 		}
114 	}
115 
116 	return NULL;
117 }
118 
119 
120 status_t
121 acpi_PkgInt(acpi_object_type* res, int idx, int* dst)
122 {
123 	acpi_object_type* obj = &res->package.objects[idx];
124 	if (obj == NULL || obj->object_type != ACPI_TYPE_INTEGER)
125 		return B_BAD_VALUE;
126 	*dst = obj->integer.integer;
127 
128 	return B_OK;
129 }
130 
131 
132 status_t
133 acpi_PkgInt32(acpi_object_type* res, int idx, uint32* dst)
134 {
135 	int tmp;
136 
137 	status_t status = acpi_PkgInt(res, idx, &tmp);
138 	if (status == B_OK)
139 		*dst = (uint32) tmp;
140 
141 	return status;
142 }
143 
144 
145 acpi_status
146 embedded_controller_io_ports_parse_callback(ACPI_RESOURCE* resource,
147 	void* _context)
148 {
149 	acpi_ec_cookie* sc = (acpi_ec_cookie*)_context;
150 	if (resource->Type != ACPI_RESOURCE_TYPE_IO)
151 		return AE_OK;
152 	if (sc->ec_data_pci_address == 0) {
153 		sc->ec_data_pci_address = resource->Data.Io.Minimum;
154 	} else if (sc->ec_csr_pci_address == 0) {
155 		sc->ec_csr_pci_address = resource->Data.Io.Minimum;
156 	} else {
157 		return AE_CTRL_TERMINATE;
158 	}
159 
160 	return AE_OK;
161 }
162 
163 
164 // #pragma mark -
165 
166 
167 static status_t
168 embedded_controller_open(void* initCookie, const char* path, int flags,
169 	void** cookie)
170 {
171 	acpi_ec_cookie* device = (acpi_ec_cookie*) initCookie;
172 	*cookie = device;
173 
174 	return B_OK;
175 }
176 
177 
178 static status_t
179 embedded_controller_close(void* cookie)
180 {
181 	return B_OK;
182 }
183 
184 
185 static status_t
186 embedded_controller_read(void* _cookie, off_t position, void* buffer,
187 	size_t* numBytes)
188 {
189 	return B_IO_ERROR;
190 }
191 
192 
193 static status_t
194 embedded_controller_write(void* cookie, off_t position, const void* buffer,
195 	size_t* numBytes)
196 {
197 	return B_IO_ERROR;
198 }
199 
200 
201 status_t
202 embedded_controller_control(void* _cookie, uint32 op, void* arg, size_t len)
203 {
204 	return B_ERROR;
205 }
206 
207 
208 static status_t
209 embedded_controller_free(void* cookie)
210 {
211 	return B_OK;
212 }
213 
214 
215 //	#pragma mark - driver module API
216 
217 
218 static int32
219 acpi_get_type(device_node* dev)
220 {
221 	const char *bus;
222 	if (gDeviceManager->get_attr_string(dev, B_DEVICE_BUS, &bus, false))
223 		return -1;
224 
225 	if (strcmp(bus, "acpi"))
226 		return -1;
227 
228 	uint32 deviceType;
229 	if (gDeviceManager->get_attr_uint32(dev, ACPI_DEVICE_TYPE_ITEM,
230 			&deviceType, false) != B_OK)
231 		return -1;
232 
233 	return deviceType;
234 }
235 
236 
237 static float
238 embedded_controller_support(device_node* dev)
239 {
240 	TRACE("embedded_controller_support()\n");
241 
242 	// Check that this is a device
243 	if (acpi_get_type(dev) != ACPI_TYPE_DEVICE)
244 		return 0.0;
245 
246 	const char* name;
247 	if (gDeviceManager->get_attr_string(dev, ACPI_DEVICE_HID_ITEM, &name, false)
248 			!= B_OK)
249 		return 0.0;
250 
251 	// Test all known IDs
252 
253 	static const char* kEmbeddedControllerIDs[] = { "PNP0C09" };
254 
255 	for (size_t i = 0; i < sizeof(kEmbeddedControllerIDs)
256 			/ sizeof(kEmbeddedControllerIDs[0]); i++) {
257 		if (!strcmp(name, kEmbeddedControllerIDs[i])) {
258 			TRACE("supported device found %s\n", name);
259 			return 0.6;
260 		}
261 	}
262 
263 	return 0.0;
264 }
265 
266 
267 static status_t
268 embedded_controller_register_device(device_node* node)
269 {
270 	device_attr attrs[] = {
271 		{ B_DEVICE_PRETTY_NAME, B_STRING_TYPE,
272 			{ string: "ACPI embedded controller" }},
273 		{ NULL }
274 	};
275 
276 	return gDeviceManager->register_node(node, ACPI_EC_DRIVER_NAME, attrs,
277 		NULL, NULL);
278 }
279 
280 
281 static status_t
282 embedded_controller_init_driver(device_node* dev, void** _driverCookie)
283 {
284 	TRACE("init driver\n");
285 
286 	acpi_ec_cookie* sc;
287 	sc = (acpi_ec_cookie*)malloc(sizeof(acpi_ec_cookie));
288 	if (sc == NULL)
289 		return B_NO_MEMORY;
290 
291 	memset(sc, 0, sizeof(acpi_ec_cookie));
292 
293 	*_driverCookie = sc;
294 	sc->ec_dev = dev;
295 
296 	sc->ec_condition_var.Init(NULL, "ec condition variable");
297 	mutex_init(&sc->ec_lock, "ec lock");
298 	device_node* parent = gDeviceManager->get_parent_node(dev);
299 	gDeviceManager->get_driver(parent, (driver_module_info**)&sc->ec_acpi,
300 		(void**)&sc->ec_handle);
301 	gDeviceManager->put_node(parent);
302 
303 	if (get_module(B_ACPI_MODULE_NAME, (module_info**)&sc->ec_acpi_module)
304 			!= B_OK)
305 		return B_ERROR;
306 
307 	acpi_data buf;
308 	buf.pointer = NULL;
309 	buf.length = ACPI_ALLOCATE_BUFFER;
310 
311 	// Read the unit ID to check for duplicate attach and the
312 	// global lock value to see if we should acquire it when
313 	// accessing the EC.
314 	status_t status = acpi_GetInteger(sc->ec_acpi, sc->ec_handle, "_UID",
315 		&sc->ec_uid);
316 	if (status != B_OK)
317 		sc->ec_uid = 0;
318 	status = acpi_GetInteger(sc->ec_acpi, sc->ec_handle, "_GLK", &sc->ec_glk);
319 	if (status != B_OK)
320 		sc->ec_glk = 0;
321 
322 	// Evaluate the _GPE method to find the GPE bit used by the EC to
323 	// signal status (SCI).  If it's a package, it contains a reference
324 	// and GPE bit, similar to _PRW.
325 	status = sc->ec_acpi->evaluate_method(sc->ec_handle, "_GPE", NULL, &buf);
326 	if (status != B_OK) {
327 		ERROR("can't evaluate _GPE\n");
328 		goto error;
329 	}
330 
331 	acpi_object_type* obj;
332 	obj = (acpi_object_type*)buf.pointer;
333 	if (obj == NULL)
334 		goto error;
335 
336 	switch (obj->object_type) {
337 		case ACPI_TYPE_INTEGER:
338 			sc->ec_gpehandle = NULL;
339 			sc->ec_gpebit = obj->integer.integer;
340 			break;
341 		case ACPI_TYPE_PACKAGE:
342 			if (!ACPI_PKG_VALID(obj, 2))
343 				goto error;
344 			sc->ec_gpehandle = acpi_GetReference(sc->ec_acpi_module, NULL,
345 				&obj->package.objects[0]);
346 			if (sc->ec_gpehandle == NULL
347 				|| acpi_PkgInt32(obj, 1, (uint32*)&sc->ec_gpebit) != B_OK)
348 				goto error;
349 			break;
350 		default:
351 			ERROR("_GPE has invalid type %i\n", int(obj->object_type));
352 			goto error;
353 	}
354 
355 	sc->ec_suspending = FALSE;
356 
357 	// Attach bus resources for data and command/status ports.
358 	status = sc->ec_acpi->walk_resources(sc->ec_handle, (ACPI_STRING)"_CRS",
359 		embedded_controller_io_ports_parse_callback, sc);
360 	if (status != B_OK) {
361 		ERROR("Error while getting IO ports addresses\n");
362 		goto error;
363 	}
364 
365 	// Install a handler for this EC's GPE bit.  We want edge-triggered
366 	// behavior.
367 	TRACE("attaching GPE handler\n");
368 	status = sc->ec_acpi_module->install_gpe_handler(sc->ec_gpehandle,
369 		sc->ec_gpebit, ACPI_GPE_EDGE_TRIGGERED, &EcGpeHandler, sc);
370 	if (status != B_OK) {
371 		TRACE("can't install ec GPE handler\n");
372 		goto error;
373 	}
374 
375 	// Install address space handler
376 	TRACE("attaching address space handler\n");
377 	status = sc->ec_acpi->install_address_space_handler(sc->ec_handle,
378 		ACPI_ADR_SPACE_EC, &EcSpaceHandler, &EcSpaceSetup, sc);
379 	if (status != B_OK) {
380 		ERROR("can't install address space handler\n");
381 		goto error;
382 	}
383 
384 	// Enable runtime GPEs for the handler.
385 	status = sc->ec_acpi_module->enable_gpe(sc->ec_gpehandle, sc->ec_gpebit);
386 	if (status != B_OK) {
387 		ERROR("AcpiEnableGpe failed.\n");
388 		goto error;
389 	}
390 
391 	return 0;
392 
393 error:
394 	free(buf.pointer);
395 
396 	sc->ec_acpi_module->remove_gpe_handler(sc->ec_gpehandle, sc->ec_gpebit,
397 		&EcGpeHandler);
398 	sc->ec_acpi->remove_address_space_handler(sc->ec_handle, ACPI_ADR_SPACE_EC,
399 		EcSpaceHandler);
400 
401 	return ENXIO;
402 }
403 
404 
405 static void
406 embedded_controller_uninit_driver(void* driverCookie)
407 {
408 	acpi_ec_cookie* sc = (struct acpi_ec_cookie*)driverCookie;
409 	mutex_destroy(&sc->ec_lock);
410 	free(sc);
411 	put_module(B_ACPI_MODULE_NAME);
412 }
413 
414 
415 static status_t
416 embedded_controller_register_child_devices(void* _cookie)
417 {
418 	device_node* node = ((acpi_ec_cookie*)_cookie)->ec_dev;
419 
420 	int pathID = gDeviceManager->create_id(ACPI_EC_PATHID_GENERATOR);
421 	if (pathID < 0) {
422 		TRACE("register_child_device couldn't create a path_id\n");
423 		return B_ERROR;
424 	}
425 
426 	char name[128];
427 	snprintf(name, sizeof(name), ACPI_EC_BASENAME, pathID);
428 
429 	return gDeviceManager->publish_device(node, name, ACPI_EC_DEVICE_NAME);
430 }
431 
432 
433 static status_t
434 embedded_controller_init_device(void* driverCookie, void** cookie)
435 {
436 	return B_ERROR;
437 }
438 
439 
440 static void
441 embedded_controller_uninit_device(void* _cookie)
442 {
443 	acpi_ec_cookie* device = (acpi_ec_cookie*)_cookie;
444 	free(device);
445 }
446 
447 
448 driver_module_info embedded_controller_driver_module = {
449 	{
450 		ACPI_EC_DRIVER_NAME,
451 		0,
452 		NULL
453 	},
454 
455 	embedded_controller_support,
456 	embedded_controller_register_device,
457 	embedded_controller_init_driver,
458 	embedded_controller_uninit_driver,
459 	embedded_controller_register_child_devices,
460 	NULL,	// rescan
461 	NULL,	// removed
462 };
463 
464 
465 struct device_module_info embedded_controller_device_module = {
466 	{
467 		ACPI_EC_DEVICE_NAME,
468 		0,
469 		NULL
470 	},
471 
472 	embedded_controller_init_device,
473 	embedded_controller_uninit_device,
474 	NULL,
475 
476 	embedded_controller_open,
477 	embedded_controller_close,
478 	embedded_controller_free,
479 	embedded_controller_read,
480 	embedded_controller_write,
481 	NULL,
482 	embedded_controller_control,
483 
484 	NULL,
485 	NULL
486 };
487 
488 
489 // #pragma mark -
490 
491 
492 static acpi_status
493 EcCheckStatus(struct acpi_ec_cookie* sc, const char* msg, EC_EVENT event)
494 {
495 	acpi_status status = AE_NO_HARDWARE_RESPONSE;
496 	EC_STATUS ec_status = EC_GET_CSR(sc);
497 
498 	if (sc->ec_burstactive && !(ec_status & EC_FLAG_BURST_MODE)) {
499 		TRACE("burst disabled in waitevent (%s)\n", msg);
500 		sc->ec_burstactive = false;
501 	}
502 	if (EVENT_READY(event, ec_status)) {
503 		TRACE("%s wait ready, status %#x\n", msg, ec_status);
504 		status = AE_OK;
505 	}
506 	return status;
507 }
508 
509 
510 static void
511 EcGpeQueryHandlerSub(struct acpi_ec_cookie *sc)
512 {
513 	// Serialize user access with EcSpaceHandler().
514 	status_t status = EcLock(sc);
515 	if (status != B_OK) {
516 		TRACE("GpeQuery lock error.\n");
517 		return;
518 	}
519 
520 	// Send a query command to the EC to find out which _Qxx call it
521 	// wants to make.  This command clears the SCI bit and also the
522 	// interrupt source since we are edge-triggered.  To prevent the GPE
523 	// that may arise from running the query from causing another query
524 	// to be queued, we clear the pending flag only after running it.
525 	acpi_status acpi_status = AE_ERROR;
526 	for (uint8 retry = 0; retry < 2; retry++) {
527 		acpi_status = EcCommand(sc, EC_COMMAND_QUERY);
528 		if (acpi_status == AE_OK)
529 			break;
530 		if (EcCheckStatus(sc, "retr_check",
531 			EC_EVENT_INPUT_BUFFER_EMPTY) != AE_OK)
532 			break;
533 	}
534 
535 	if (acpi_status != AE_OK) {
536 		EcUnlock(sc);
537 		TRACE("GPE query failed.\n");
538 		return;
539 	}
540 	uint8 data = EC_GET_DATA(sc);
541 
542 	// We have to unlock before running the _Qxx method below since that
543 	// method may attempt to read/write from EC address space, causing
544 	// recursive acquisition of the lock.
545 	EcUnlock(sc);
546 
547 	// Ignore the value for "no outstanding event". (13.3.5)
548 	TRACE("query ok,%s running _Q%02X\n", data ? "" : " not", data);
549 	if (data == 0)
550 		return;
551 
552 	// Evaluate _Qxx to respond to the controller.
553 	char qxx[5];
554 	snprintf(qxx, sizeof(qxx), "_Q%02X", data);
555 	AcpiUtStrupr(qxx);
556 	status = sc->ec_acpi->evaluate_method(sc->ec_handle, qxx, NULL, NULL);
557 	if (status != B_OK) {
558 		TRACE("evaluation of query method %s failed\n", qxx);
559 	}
560 }
561 
562 
563 static void
564 EcGpeQueryHandler(void* context)
565 {
566 	struct acpi_ec_cookie* sc = (struct acpi_ec_cookie*)context;
567 	int32 pending;
568 
569 	ASSERT(context != NULL);
570 
571 	do {
572 		// Read the current pending count
573 		pending = atomic_get(&sc->ec_sci_pending);
574 
575 		// Call GPE handler function
576 		EcGpeQueryHandlerSub(sc);
577 
578 		// Try to reset the pending count to zero. If this fails we
579 		// know another GPE event has occurred while handling the
580 		// current GPE event and need to loop.
581 	} while (atomic_test_and_set(&sc->ec_sci_pending, 0, pending));
582 }
583 
584 
585 /*!	The GPE handler is called when IBE/OBF or SCI events occur.  We are
586 	called from an unknown lock context.
587 */
588 static uint32
589 EcGpeHandler(acpi_handle gpeDevice, uint32 gpeNumber, void* context)
590 {
591 	struct acpi_ec_cookie* sc = (acpi_ec_cookie*)context;
592 
593 	ASSERT(context != NULL);//, ("EcGpeHandler called with NULL"));
594 	TRACE("gpe handler start\n");
595 
596 	// Notify EcWaitEvent() that the status register is now fresh.  If we
597 	// didn't do this, it wouldn't be possible to distinguish an old IBE
598 	// from a new one, for example when doing a write transaction (writing
599 	// address and then data values.)
600 	atomic_add(&sc->ec_gencount, 1);
601 	sc->ec_condition_var.NotifyAll();
602 
603 	// If the EC_SCI bit of the status register is set, queue a query handler.
604 	// It will run the query and _Qxx method later, under the lock.
605 	EC_STATUS ecStatus = EC_GET_CSR(sc);
606 	if ((ecStatus & EC_EVENT_SCI) && atomic_add(&sc->ec_sci_pending, 1) == 0) {
607 		TRACE("gpe queueing query handler\n");
608 		acpi_status status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler,
609 			context);
610 		if (status != AE_OK) {
611 			dprintf("EcGpeHandler: queuing GPE query handler failed\n");
612 			atomic_add(&sc->ec_sci_pending, -1);
613 		}
614 	}
615 	return ACPI_REENABLE_GPE;
616 }
617 
618 
619 static acpi_status
620 EcSpaceSetup(acpi_handle region, uint32 function, void* context,
621 	void** regionContext)
622 {
623 	// If deactivating a region, always set the output to NULL.  Otherwise,
624 	// just pass the context through.
625 	if (function == ACPI_REGION_DEACTIVATE)
626 		*regionContext = NULL;
627 	else
628 		*regionContext = context;
629 
630 	return AE_OK;
631 }
632 
633 
634 static acpi_status
635 EcSpaceHandler(uint32 function, acpi_physical_address address, uint32 width,
636 	int* value, void* context, void* regionContext)
637 {
638 	TRACE("enter EcSpaceHandler\n");
639 	struct acpi_ec_cookie* sc = (struct acpi_ec_cookie*)context;
640 
641 	if (function != ACPI_READ && function != ACPI_WRITE)
642 		return AE_BAD_PARAMETER;
643 	if (width % 8 != 0 || value == NULL || context == NULL)
644 		return AE_BAD_PARAMETER;
645 	if (address + width / 8 > 256)
646 		return AE_BAD_ADDRESS;
647 
648 	// If booting, check if we need to run the query handler.  If so, we
649 	// we call it directly here as scheduling and dpc might not be up yet.
650 	// (Not sure if it's needed)
651 
652 	if (gKernelStartup || gKernelShutdown || sc->ec_suspending) {
653 		if ((EC_GET_CSR(sc) & EC_EVENT_SCI) &&
654 			atomic_add(&sc->ec_sci_pending, 1) == 0) {
655 			//CTR0(KTR_ACPI, "ec running gpe handler directly");
656 			EcGpeQueryHandler(sc);
657 		}
658 	}
659 
660 	// Serialize with EcGpeQueryHandler() at transaction granularity.
661 	acpi_status status = EcLock(sc);
662 	if (status != B_OK)
663 		return AE_NOT_ACQUIRED;
664 
665 	// If we can't start burst mode, continue anyway.
666 	status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
667 	if (status == B_OK) {
668 		if (EC_GET_DATA(sc) == EC_BURST_ACK) {
669 			TRACE("burst enabled.\n");
670 			sc->ec_burstactive = TRUE;
671 		}
672 	}
673 
674 	// Perform the transaction(s), based on width.
675 	ACPI_PHYSICAL_ADDRESS ecAddr = address;
676 	uint8* ecData = (uint8 *) value;
677 	if (function == ACPI_READ)
678 		*value = 0;
679 	do {
680 		switch (function) {
681 			case ACPI_READ:
682 				status = EcRead(sc, ecAddr, ecData);
683 				break;
684 			case ACPI_WRITE:
685 				status = EcWrite(sc, ecAddr, *ecData);
686 				break;
687 		}
688 		if (status != AE_OK)
689 			break;
690 		ecAddr++;
691 		ecData++;
692 	} while (ecAddr < address + width / 8);
693 
694 	if (sc->ec_burstactive) {
695 		sc->ec_burstactive = FALSE;
696 		if (EcCommand(sc, EC_COMMAND_BURST_DISABLE) == AE_OK)
697 			TRACE("disabled burst ok.");
698 	}
699 
700 	EcUnlock(sc);
701 	return status;
702 }
703 
704 
705 static acpi_status
706 EcWaitEvent(struct acpi_ec_cookie* sc, EC_EVENT event, int32 generationCount)
707 {
708 	static int32 noIntr = 0;
709 	acpi_status status = AE_NO_HARDWARE_RESPONSE;
710 	int32 count, i;
711 
712 	int needPoll = ec_polled_mode || sc->ec_suspending
713 		|| gKernelStartup || gKernelShutdown;
714 
715 	// Wait for event by polling or GPE (interrupt).
716 	if (needPoll) {
717 		count = (ec_timeout * 1000) / EC_POLL_DELAY;
718 		if (count == 0)
719 			count = 1;
720 		spin(10);
721 		for (i = 0; i < count; i++) {
722 			status = EcCheckStatus(sc, "poll", event);
723 			if (status == AE_OK)
724 				break;
725 			spin(EC_POLL_DELAY);
726 		}
727 	} else {
728 		// Wait for the GPE to signal the status changed, checking the
729 		// status register each time we get one.  It's possible to get a
730 		// GPE for an event we're not interested in here (i.e., SCI for
731 		// EC query).
732 		status_t waitStatus = B_NO_ERROR;
733 		for (i = 0; i < ec_timeout; i++) {
734 			if (generationCount == sc->ec_gencount) {
735 				waitStatus =
736 					sc->ec_condition_var.Wait(B_RELATIVE_TIMEOUT, 1000);
737 			}
738 			/*
739 			 * Record new generation count.  It's possible the GPE was
740 			 * just to notify us that a query is needed and we need to
741 			 * wait for a second GPE to signal the completion of the
742 			 * event we are actually waiting for.
743 			 */
744 			status = EcCheckStatus(sc, "sleep", event);
745 			if (status == AE_OK) {
746 				if (generationCount == sc->ec_gencount)
747 					noIntr++;
748 				else
749 					noIntr = 0;
750 				break;
751 			}
752 			generationCount = sc->ec_gencount;
753 		}
754 
755 		/*
756 		 * We finished waiting for the GPE and it never arrived.  Try to
757 		 * read the register once and trust whatever value we got.  This is
758 		 * the best we can do at this point.
759 		 */
760 		if (status != AE_OK)
761 			status = EcCheckStatus(sc, "sleep_end", event);
762 	}
763 	if (!needPoll && noIntr > 10) {
764 		TRACE("not getting interrupts, switched to polled mode\n");
765 		ec_polled_mode = true;
766 	}
767 
768 	if (status != AE_OK)
769 		TRACE("error: ec wait timed out\n");
770 
771 	return status;
772 }
773 
774 
775 static acpi_status
776 EcCommand(struct acpi_ec_cookie* sc, EC_COMMAND cmd)
777 {
778 	// Don't use burst mode if user disabled it.
779 	if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE)
780 		return AE_ERROR;
781 
782 	// Decide what to wait for based on command type.
783 	EC_EVENT event;
784 	switch (cmd) {
785 		case EC_COMMAND_READ:
786 		case EC_COMMAND_WRITE:
787 		case EC_COMMAND_BURST_DISABLE:
788 			event = EC_EVENT_INPUT_BUFFER_EMPTY;
789 			break;
790 		case EC_COMMAND_QUERY:
791 		case EC_COMMAND_BURST_ENABLE:
792 			event = EC_EVENT_OUTPUT_BUFFER_FULL;
793 			break;
794 		default:
795 			TRACE("EcCommand: invalid command %#x\n", cmd);
796 			return AE_BAD_PARAMETER;
797 	}
798 
799 	// Ensure empty input buffer before issuing command.
800 	// Use generation count of zero to force a quick check.
801 	acpi_status status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, 0);
802 	if (status != AE_OK)
803 		return status;
804 
805 	// Run the command and wait for the chosen event.
806 	TRACE("running command %#x\n", cmd);
807 	int32 generationCount = sc->ec_gencount;
808 	EC_SET_CSR(sc, cmd);
809 	status = EcWaitEvent(sc, event, generationCount);
810 	if (status == AE_OK) {
811 		// If we succeeded, burst flag should now be present.
812 		if (cmd == EC_COMMAND_BURST_ENABLE) {
813 			EC_STATUS ec_status = EC_GET_CSR(sc);
814 			if ((ec_status & EC_FLAG_BURST_MODE) == 0)
815 				status = AE_ERROR;
816 		}
817 	} else
818 		TRACE("EcCommand: no response to %#x\n", cmd);
819 
820 	return status;
821 }
822 
823 
824 static acpi_status
825 EcRead(struct acpi_ec_cookie* sc, uint8 address, uint8* readData)
826 {
827 	TRACE("read from %#x\n", address);
828 
829 	acpi_status status;
830 	for (uint8 retry = 0; retry < 2; retry++) {
831 		status = EcCommand(sc, EC_COMMAND_READ);
832 		if (status != AE_OK)
833 			return status;
834 
835 		int32 generationCount = sc->ec_gencount;
836 		EC_SET_DATA(sc, address);
837 		status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL, generationCount);
838 		if (status == AE_OK) {
839 			*readData = EC_GET_DATA(sc);
840 			return AE_OK;
841 		}
842 		if (EcCheckStatus(sc, "retr_check", EC_EVENT_INPUT_BUFFER_EMPTY)
843 				!= AE_OK) {
844 			break;
845 		}
846 	}
847 
848 	TRACE("EcRead: failed waiting to get data\n");
849 	return status;
850 }
851 
852 
853 static acpi_status
854 EcWrite(struct acpi_ec_cookie* sc, uint8 address, uint8 writeData)
855 {
856 	acpi_status status = EcCommand(sc, EC_COMMAND_WRITE);
857 	if (status != AE_OK)
858 		return status;
859 
860 	int32 generationCount = sc->ec_gencount;
861 	EC_SET_DATA(sc, address);
862 	status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, generationCount);
863 	if (status != AE_OK) {
864 		TRACE("EcWrite: failed waiting for sent address\n");
865 		return status;
866 	}
867 
868 	generationCount = sc->ec_gencount;
869 	EC_SET_DATA(sc, writeData);
870 	status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, generationCount);
871 	if (status != AE_OK) {
872 		TRACE("EcWrite: failed waiting for sent data\n");
873 		return status;
874 	}
875 
876 	return AE_OK;
877 }
878