2 * ec.c - Embedded controller support
4 * Copyright (C) 2000 Andrew Henroid
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/kernel.h>
22 #include <linux/acpi.h>
23 #include <linux/slab.h>
28 #define _COMPONENT OS_DEPENDENT
31 #define ACPI_EC_HID "PNP0C09"
47 ACPI_EC_BURST_ENABLE
= 0x82,
48 ACPI_EC_BURST_DISABLE
= 0x83,
54 ACPI_HANDLE acpi_handle
;
56 ACPI_IO_ADDRESS status_port
;
57 ACPI_IO_ADDRESS data_port
;
69 static char object_name
[] = {'_', 'Q', '0', '0', '\0'};
71 static char hex
[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
79 EC_STATUS ec_status
= 0;
82 if (!ec
|| ((wait_event
!= EC_EVENT_OUTPUT_BUFFER_FULL
)
83 && (wait_event
!= EC_EVENT_INPUT_BUFFER_EMPTY
)))
84 return(AE_BAD_PARAMETER
);
89 * Poll the EC status register waiting for the event to occur.
90 * Note that we'll wait a maximum of 1ms in 10us chunks.
93 case EC_EVENT_OUTPUT_BUFFER_FULL
:
95 ec_status
= acpi_os_in8(ec
->status_port
);
96 if (ec_status
& EC_FLAG_OUTPUT_BUFFER
)
98 acpi_os_sleep_usec(10);
101 case EC_EVENT_INPUT_BUFFER_EMPTY
:
103 ec_status
= acpi_os_in8(ec
->status_port
);
104 if (!(ec_status
& EC_FLAG_INPUT_BUFFER
))
106 acpi_os_sleep_usec(10);
117 ACPI_IO_ADDRESS io_port
,
121 ACPI_STATUS status
= AE_OK
;
124 return(AE_BAD_PARAMETER
);
126 *data
= acpi_os_in8(io_port
);
129 status
= ec_io_wait(ec
, wait_event
);
137 ACPI_IO_ADDRESS io_port
,
141 ACPI_STATUS status
= AE_OK
;
144 return(AE_BAD_PARAMETER
);
146 acpi_os_out8(io_port
, data
);
149 status
= ec_io_wait(ec
, wait_event
);
160 ACPI_STATUS status
= AE_OK
;
162 FUNCTION_TRACE("ec_read");
165 return_ACPI_STATUS(AE_BAD_PARAMETER
);
167 status
= ec_io_write(ec
, ec
->status_port
, EC_COMMAND_READ
, EC_EVENT_INPUT_BUFFER_EMPTY
);
168 if (ACPI_FAILURE(status
)) {
169 DEBUG_PRINT(ACPI_WARN
, ("Unable to send 'read command' to EC.\n"));
170 return_ACPI_STATUS(status
);
173 status
= ec_io_write(ec
, ec
->data_port
, address
, EC_EVENT_OUTPUT_BUFFER_FULL
);
174 if (ACPI_FAILURE(status
)) {
175 DEBUG_PRINT(ACPI_WARN
, ("Unable to send 'read address' to EC.\n"));
176 return_ACPI_STATUS(status
);
179 status
= ec_io_read(ec
, ec
->data_port
, data
, EC_EVENT_NONE
);
181 DEBUG_PRINT(ACPI_INFO
, ("Read data[0x%02x] from address[0x%02x] on ec.\n", (*data
), address
));
183 return_ACPI_STATUS(status
);
192 ACPI_STATUS status
= AE_OK
;
194 FUNCTION_TRACE("ec_write");
197 return_ACPI_STATUS(AE_BAD_PARAMETER
);
199 status
= ec_io_write(ec
, ec
->status_port
, EC_COMMAND_WRITE
, EC_EVENT_INPUT_BUFFER_EMPTY
);
200 if (ACPI_FAILURE(status
)) {
201 DEBUG_PRINT(ACPI_WARN
, ("Unable to send 'write command' to EC.\n"));
202 return_ACPI_STATUS(status
);
205 status
= ec_io_write(ec
, ec
->data_port
, address
, EC_EVENT_INPUT_BUFFER_EMPTY
);
206 if (ACPI_FAILURE(status
)) {
207 DEBUG_PRINT(ACPI_WARN
, ("Unable to send 'write address' to EC.\n"));
208 return_ACPI_STATUS(status
);
211 status
= ec_io_write(ec
, ec
->data_port
, data
, EC_EVENT_INPUT_BUFFER_EMPTY
);
212 if (ACPI_FAILURE(status
)) {
213 DEBUG_PRINT(ACPI_WARN
, ("Unable to send 'write data' to EC.\n"));
214 return_ACPI_STATUS(status
);
217 DEBUG_PRINT(ACPI_INFO
, ("Wrote data[0x%02x] to address[0x%02x] on ec.\n", data
, address
));
219 return_ACPI_STATUS(status
);
227 ACPI_STATUS status
= AE_OK
;
229 FUNCTION_TRACE("ec_transaction");
232 return_ACPI_STATUS(AE_BAD_PARAMETER
);
235 * Obtaining semaphore (mutex) to serialize all EC transactions.
238 DEBUG_PRINT(ACPI_INFO, ("Calling acpi_os_wait_semaphore(%p, 1, %d)\n", ec->mutex, EC_DEFAULT_TIMEOUT));
239 status = acpi_os_wait_semaphore(ec->mutex, 1, EC_DEFAULT_TIMEOUT);
240 if (ACPI_FAILURE(status))
241 return_ACPI_STATUS(status);
245 * Perform the transaction.
247 switch (request
->command
) {
249 case EC_COMMAND_READ
:
250 status
= ec_read(ec
, request
->address
, &(request
->data
));
253 case EC_COMMAND_WRITE
:
254 status
= ec_write(ec
, request
->address
, request
->data
);
263 * Signal the semaphore (mutex) to indicate transaction completion.
266 DEBUG_PRINT(ACPI_INFO, ("Calling acpi_os_signal_semaphore(%p, 1)\n", ec->mutex));
267 acpi_os_signal_semaphore(ec->mutex, 1);
270 return_ACPI_STATUS(status
);
275 ACPI_HANDLE region_handle
,
277 void *handler_context
,
278 void **return_context
)
280 // TODO: What is this function for?
282 * The ec object is in the handler context and is needed
283 * when calling the ec_space_handler.
285 *return_context
= handler_context
;
297 ACPI_STATUS status
= AE_OK
;
298 EC_QUERY_DATA
*ec_q
= (EC_QUERY_DATA
*)context
;
300 FUNCTION_TRACE("ec_query_handler");
302 if (!ec_q
|| !ec_q
->ec
) {
303 DEBUG_PRINT(ACPI_ERROR
, ("Invalid (NULL) context.\n"));
310 * Evaluate corresponding _Qxx method. Note that a zero query
311 * value indicates a spurious EC_SCI (no such thing as _Q00).
313 object_name
[2] = hex
[((ec_q
->data
>> 4) & 0x0F)];
314 object_name
[3] = hex
[(ec_q
->data
& 0x0F)];
316 DEBUG_PRINT(ACPI_INFO
, ("Read query data[0x%02x] from ec - evaluating [%s].\n", ec_q
->data
, object_name
));
318 status
= acpi_evaluate_object(ec_q
->ec
->acpi_handle
, object_name
, NULL
, NULL
);
329 ec_gpe_handler(void *context
)
331 ACPI_STATUS status
= AE_OK
;
332 ec_context_t
*ec
= (ec_context_t
*) context
;
333 EC_QUERY_DATA
*ec_q
= NULL
;
334 EC_STATUS ec_status
= 0;
336 FUNCTION_TRACE("ec_gpe_handler");
339 DEBUG_PRINT(ACPI_INFO
, ("Invalid (NULL) context.\n"));
348 * Check the EC_SCI bit to see if this is an EC_SCI event. If not (e.g.
349 * OBF/IBE) just return, as we already poll to detect these events.
351 ec_status
= acpi_os_in8(ec
->status_port
);
352 DEBUG_PRINT(ACPI_INFO
, ("EC Status Register: [0x%02x]\n", ec_status
));
353 if (!(ec_status
& EC_FLAG_SCI
))
356 DEBUG_PRINT(ACPI_INFO
, ("EC_SCI detected - running QUERY.\n"));
358 // TODO: Need GFP_ATOMIC 'switch' for OSL interface...
359 ec_q
= kmalloc(sizeof(EC_QUERY_DATA
), GFP_ATOMIC
);
361 DEBUG_PRINT(ACPI_INFO
, ("Memory allocation failure.\n"));
371 * Query the EC to find out which _Qxx method we need to evaluate.
372 * Note that successful completion of the query causes the EC_SCI
373 * bit to be cleared (and thus clearing the interrupt source).
375 status
= ec_io_write(ec
, ec
->status_port
, EC_COMMAND_QUERY
, EC_EVENT_OUTPUT_BUFFER_FULL
);
376 if (ACPI_FAILURE(status
)) {
377 DEBUG_PRINT(ACPI_WARN
, ("Unable to send 'query command' to EC.\n"));
381 status
= ec_io_read(ec
, ec
->data_port
, &(ec_q
->data
), EC_EVENT_NONE
);
382 if (ACPI_FAILURE(status
)) {
383 DEBUG_PRINT(ACPI_WARN
, ("Error reading query data.\n"));
390 DEBUG_PRINT(ACPI_WARN
, ("Spurious EC SCI detected.\n"));
396 * Defer _Qxx Execution:
397 * ---------------------
398 * Can't evaluate this method now 'cause we're at interrupt-level.
400 status
= acpi_os_queue_for_execution(OSD_PRIORITY_GPE
, ec_query_handler
, ec_q
);
401 if (ACPI_FAILURE(status
)) {
402 DEBUG_PRINT(ACPI_ERROR
, ("Unable to defer _Qxx method evaluation.\n"));
407 if (ACPI_FAILURE(status
))
417 void *handler_context
,
418 void **region_context
)
420 FUNCTION_TRACE("acpi_ec_region_setup");
422 printk("acpi_ec_region_setup\n");
424 if (function
== ACPI_REGION_DEACTIVATE
)
428 acpi_cm_free (*region_context
);
429 *region_context
= NULL
;
432 return_ACPI_STATUS (AE_OK
);
435 *region_context
= NULL
;
437 return_ACPI_STATUS (AE_OK
);
440 /*****************************************************************************
442 * FUNCTION: ec_region_handler
444 * PARAMETERS: function - Read or Write operation
445 * address - Where in the space to read or write
446 * bit_width - Field width in bits (8, 16, or 32)
447 * value - Pointer to in or out value
448 * context - context pointer
452 * DESCRIPTION: Handler for the Embedded Controller (EC) address space
455 ****************************************************************************/
460 ACPI_PHYSICAL_ADDRESS address
,
463 void *handler_context
,
464 void *region_context
)
466 ACPI_STATUS status
= AE_OK
;
467 ec_context_t
*ec
= NULL
;
468 EC_REQUEST ec_request
;
470 FUNCTION_TRACE("ec_space_handler");
472 if (address
> 0xFF || bit_width
!= 8 || !value
|| !handler_context
)
473 return_ACPI_STATUS(AE_BAD_PARAMETER
);
475 ec
= (ec_context_t
*)handler_context
;
479 case ADDRESS_SPACE_READ
:
480 ec_request
.command
= EC_COMMAND_READ
;
481 ec_request
.address
= address
;
485 case ADDRESS_SPACE_WRITE
:
486 ec_request
.command
= EC_COMMAND_WRITE
;
487 ec_request
.address
= address
;
488 ec_request
.data
= (UINT8
)(*value
);
492 DEBUG_PRINT(ACPI_WARN
, ("Received request with invalid function [0x%08X].\n", function
));
493 return_ACPI_STATUS(AE_BAD_PARAMETER
);
497 DEBUG_PRINT(ACPI_INFO
, ("device[ec] command[0x%02X] address[0x%02X] data[0x%02X]\n", ec_request
.command
, ec_request
.address
, ec_request
.data
));
500 * Perform the Transaction.
502 status
= ec_transaction(ec
, &ec_request
);
503 if (ACPI_SUCCESS(status
))
504 (*value
) = (UINT32
)ec_request
.data
;
506 return_ACPI_STATUS(status
);
510 * Get Embedded Controller information
523 ec_context_t
*ec_cxt
;
527 if (acpi_get_current_resources(handle
, &buf
) != AE_BUFFER_OVERFLOW
)
530 buf
.pointer
= kmalloc(buf
.length
, GFP_KERNEL
);
534 if (!ACPI_SUCCESS(acpi_get_current_resources(handle
, &buf
))) {
539 ec_cxt
= kmalloc(sizeof(ec_context_t
), GFP_KERNEL
);
545 ec_cxt
->acpi_handle
= handle
;
547 res
= (RESOURCE
*) buf
.pointer
;
548 ec_cxt
->data_port
= res
->data
.io
.min_base_address
;
549 res
= NEXT_RESOURCE(res
);
550 ec_cxt
->status_port
= (int) res
->data
.io
.min_base_address
;
554 /* determine GPE bit */
555 /* BUG: in acpi 2.0 this could return a package */
556 buf
.length
= sizeof(obj
);
558 if (!ACPI_SUCCESS(acpi_evaluate_object(handle
, "_GPE", NULL
, &buf
))
559 || obj
.type
!= ACPI_TYPE_NUMBER
)
562 ec_cxt
->gpe_bit
= obj
.number
.value
;
564 /* determine if we need the Global Lock when accessing */
565 buf
.length
= sizeof(obj
);
568 status
= acpi_evaluate_object(handle
, "_GLK", NULL
, &buf
);
569 if (status
== AE_NOT_FOUND
)
570 ec_cxt
->need_global_lock
= 0;
571 else if (!ACPI_SUCCESS(status
) || obj
.type
!= ACPI_TYPE_NUMBER
) {
572 DEBUG_PRINT(ACPI_ERROR
, ("_GLK failed\n"));
576 ec_cxt
->need_global_lock
= obj
.number
.value
;
578 printk(KERN_INFO
"ACPI: found EC @ (0x%02x,0x%02x,gpe %d GL %d)\n",
579 ec_cxt
->data_port
, ec_cxt
->status_port
, ec_cxt
->gpe_bit
,
580 ec_cxt
->need_global_lock
);
582 if (!ACPI_SUCCESS(acpi_install_gpe_handler(
584 ACPI_EVENT_EDGE_TRIGGERED
,
588 REPORT_ERROR(("Could not install GPE handler for EC.\n"));
592 status
= acpi_install_address_space_handler (handle
, ADDRESS_SPACE_EC
,
593 ec_region_handler
, ec_region_setup
, ec_cxt
);
595 if (!ACPI_SUCCESS(status
)) {
596 REPORT_ERROR(("Could not install EC address "
597 "space handler, error %s\n", acpi_cm_format_exception (status
)));
606 acpi_get_devices(ACPI_EC_HID
,
615 acpi_ec_terminate(void)
618 /* walk list of EC's */
619 /* free their context and release resources */