2 * acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 36 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/delay.h>
31 #include <linux/compatmac.h>
32 #include <linux/proc_fs.h>
34 #include <acpi/acpi_bus.h>
35 #include <acpi/acpi_drivers.h>
36 #include <acpi/actypes.h>
38 #define _COMPONENT ACPI_EC_COMPONENT
39 ACPI_MODULE_NAME ("acpi_ec")
41 #define PREFIX "ACPI: "
44 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
45 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
46 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
48 #define ACPI_EC_EVENT_OBF 0x01 /* Output buffer full */
49 #define ACPI_EC_EVENT_IBE 0x02 /* Input buffer empty */
51 #define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
52 #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
53 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
55 #define ACPI_EC_COMMAND_READ 0x80
56 #define ACPI_EC_COMMAND_WRITE 0x81
57 #define ACPI_EC_COMMAND_QUERY 0x84
59 static int acpi_ec_add (struct acpi_device *device);
60 static int acpi_ec_remove (struct acpi_device *device, int type);
61 static int acpi_ec_start (struct acpi_device *device);
62 static int acpi_ec_stop (struct acpi_device *device, int type);
64 static struct acpi_driver acpi_ec_driver = {
65 .name = ACPI_EC_DRIVER_NAME,
66 .class = ACPI_EC_CLASS,
70 .remove = acpi_ec_remove,
71 .start = acpi_ec_start,
79 unsigned long gpe_bit;
80 struct acpi_generic_address status_addr;
81 struct acpi_generic_address command_addr;
82 struct acpi_generic_address data_addr;
83 unsigned long global_lock;
87 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
88 static struct acpi_ec *ec_ecdt;
90 /* External interfaces use first EC only, so remember */
91 static struct acpi_device *first_ec;
93 /* --------------------------------------------------------------------------
94 Transaction Management
95 -------------------------------------------------------------------------- */
102 u32 acpi_ec_status = 0;
103 u32 i = ACPI_EC_UDELAY_COUNT;
108 /* Poll the EC status register waiting for the event to occur. */
110 case ACPI_EC_EVENT_OBF:
112 acpi_hw_low_level_read(8, &acpi_ec_status, &ec->status_addr);
113 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
115 udelay(ACPI_EC_UDELAY);
118 case ACPI_EC_EVENT_IBE:
120 acpi_hw_low_level_read(8, &acpi_ec_status, &ec->status_addr);
121 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
123 udelay(ACPI_EC_UDELAY);
140 acpi_status status = AE_OK;
142 unsigned long flags = 0;
145 ACPI_FUNCTION_TRACE("acpi_ec_read");
148 return_VALUE(-EINVAL);
152 if (ec->global_lock) {
153 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
154 if (ACPI_FAILURE(status))
155 return_VALUE(-ENODEV);
158 spin_lock_irqsave(&ec->lock, flags);
160 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->command_addr);
161 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
165 acpi_hw_low_level_write(8, address, &ec->data_addr);
166 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
171 acpi_hw_low_level_read(8, data, &ec->data_addr);
173 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
177 spin_unlock_irqrestore(&ec->lock, flags);
180 acpi_release_global_lock(glk);
182 return_VALUE(result);
193 acpi_status status = AE_OK;
194 unsigned long flags = 0;
197 ACPI_FUNCTION_TRACE("acpi_ec_write");
200 return_VALUE(-EINVAL);
202 if (ec->global_lock) {
203 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
204 if (ACPI_FAILURE(status))
205 return_VALUE(-ENODEV);
208 spin_lock_irqsave(&ec->lock, flags);
210 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->command_addr);
211 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
215 acpi_hw_low_level_write(8, address, &ec->data_addr);
216 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
220 acpi_hw_low_level_write(8, data, &ec->data_addr);
221 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
225 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
229 spin_unlock_irqrestore(&ec->lock, flags);
232 acpi_release_global_lock(glk);
234 return_VALUE(result);
238 * Externally callable EC access functions. For now, assume 1 EC only
241 ec_read(u8 addr, u8 *val)
250 ec = acpi_driver_data(first_ec);
252 err = acpi_ec_read(ec, addr, &temp_data);
263 ec_write(u8 addr, u8 val)
271 ec = acpi_driver_data(first_ec);
273 err = acpi_ec_write(ec, addr, val);
285 acpi_status status = AE_OK;
286 unsigned long flags = 0;
289 ACPI_FUNCTION_TRACE("acpi_ec_query");
292 return_VALUE(-EINVAL);
296 if (ec->global_lock) {
297 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
298 if (ACPI_FAILURE(status))
299 return_VALUE(-ENODEV);
303 * Query the EC to find out which _Qxx method we need to evaluate.
304 * Note that successful completion of the query causes the ACPI_EC_SCI
305 * bit to be cleared (and thus clearing the interrupt source).
307 spin_lock_irqsave(&ec->lock, flags);
309 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->command_addr);
310 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
314 acpi_hw_low_level_read(8, data, &ec->data_addr);
319 spin_unlock_irqrestore(&ec->lock, flags);
322 acpi_release_global_lock(glk);
324 return_VALUE(result);
328 /* --------------------------------------------------------------------------
330 -------------------------------------------------------------------------- */
332 struct acpi_ec_query_data {
341 struct acpi_ec *ec = (struct acpi_ec *) ec_cxt;
343 unsigned long flags = 0;
344 static char object_name[5] = {'_','Q','0','0','\0'};
345 const char hex[] = {'0','1','2','3','4','5','6','7',
346 '8','9','A','B','C','D','E','F'};
348 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
353 spin_lock_irqsave(&ec->lock, flags);
354 acpi_hw_low_level_read(8, &value, &ec->command_addr);
355 spin_unlock_irqrestore(&ec->lock, flags);
357 /* TBD: Implement asynch events!
358 * NOTE: All we care about are EC-SCI's. Other EC events are
359 * handled via polling (yuck!). This is because some systems
360 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
361 * a purely interrupt-driven approach (grumble, grumble).
363 if (!(value & ACPI_EC_FLAG_SCI))
366 if (acpi_ec_query(ec, &value))
369 object_name[2] = hex[((value >> 4) & 0x0F)];
370 object_name[3] = hex[(value & 0x0F)];
372 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
374 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
377 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
381 acpi_ec_gpe_handler (
384 acpi_status status = AE_OK;
385 struct acpi_ec *ec = (struct acpi_ec *) data;
390 acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
392 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
393 acpi_ec_gpe_query, ec);
396 /* --------------------------------------------------------------------------
397 Address Space Management
398 -------------------------------------------------------------------------- */
401 acpi_ec_space_setup (
402 acpi_handle region_handle,
404 void *handler_context,
405 void **return_context)
408 * The EC object is in the handler context and is needed
409 * when calling the acpi_ec_space_handler.
411 if(function == ACPI_REGION_DEACTIVATE)
412 *return_context = NULL;
414 *return_context = handler_context;
421 acpi_ec_space_handler (
423 acpi_physical_address address,
426 void *handler_context,
427 void *region_context)
430 struct acpi_ec *ec = NULL;
433 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
435 if ((address > 0xFF) || (bit_width != 8) || !value || !handler_context)
436 return_VALUE(AE_BAD_PARAMETER);
438 ec = (struct acpi_ec *) handler_context;
442 result = acpi_ec_read(ec, (u8) address, &temp);
443 *value = (acpi_integer) temp;
446 result = acpi_ec_write(ec, (u8) address, (u8) *value);
455 return_VALUE(AE_BAD_PARAMETER);
458 return_VALUE(AE_NOT_FOUND);
461 return_VALUE(AE_TIME);
470 /* --------------------------------------------------------------------------
472 -------------------------------------------------------------------------- */
474 struct proc_dir_entry *acpi_ec_dir;
486 struct acpi_ec *ec = (struct acpi_ec *) data;
490 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
492 if (!ec || (off != 0))
495 p += sprintf(p, "gpe bit: 0x%02x\n",
497 p += sprintf(p, "ports: 0x%02x, 0x%02x\n",
498 (u32) ec->status_addr.address, (u32) ec->data_addr.address);
499 p += sprintf(p, "use global lock: %s\n",
500 ec->global_lock?"yes":"no");
504 if (len <= off+count) *eof = 1;
507 if (len>count) len = count;
516 struct acpi_device *device)
518 struct proc_dir_entry *entry = NULL;
520 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
522 if (!acpi_device_dir(device)) {
523 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
525 if (!acpi_device_dir(device))
526 return_VALUE(-ENODEV);
529 entry = create_proc_read_entry(ACPI_EC_FILE_INFO, S_IRUGO,
530 acpi_device_dir(device), acpi_ec_read_info,
531 acpi_driver_data(device));
533 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
534 "Unable to create '%s' fs entry\n",
543 struct acpi_device *device)
545 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
547 if (acpi_device_dir(device)) {
548 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
549 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
550 acpi_device_dir(device) = NULL;
557 /* --------------------------------------------------------------------------
559 -------------------------------------------------------------------------- */
563 struct acpi_device *device)
566 acpi_status status = AE_OK;
567 struct acpi_ec *ec = NULL;
570 ACPI_FUNCTION_TRACE("acpi_ec_add");
573 return_VALUE(-EINVAL);
575 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
577 return_VALUE(-ENOMEM);
578 memset(ec, 0, sizeof(struct acpi_ec));
580 ec->handle = device->handle;
582 ec->lock = SPIN_LOCK_UNLOCKED;
583 sprintf(acpi_device_name(device), "%s", ACPI_EC_DEVICE_NAME);
584 sprintf(acpi_device_class(device), "%s", ACPI_EC_CLASS);
585 acpi_driver_data(device) = ec;
587 /* Use the global lock for all EC transactions? */
588 acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
590 /* If our UID matches the UID for the ECDT-enumerated EC,
591 we now have the *real* EC info, so kill the makeshift one.*/
592 acpi_evaluate_integer(ec->handle, "_UID", NULL, &uid);
593 if (ec_ecdt && ec_ecdt->uid == uid) {
594 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
595 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
597 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit, &acpi_ec_gpe_handler);
602 /* Get GPE bit assignment (EC events). */
603 /* TODO: Add support for _GPE returning a package */
604 status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe_bit);
605 if (ACPI_FAILURE(status)) {
606 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
607 "Error obtaining GPE bit assignment\n"));
612 result = acpi_ec_add_fs(device);
616 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
617 acpi_device_name(device), acpi_device_bid(device),
627 return_VALUE(result);
633 struct acpi_device *device,
636 struct acpi_ec *ec = NULL;
638 ACPI_FUNCTION_TRACE("acpi_ec_remove");
641 return_VALUE(-EINVAL);
643 ec = acpi_driver_data(device);
645 acpi_ec_remove_fs(device);
655 struct acpi_resource *resource,
658 struct acpi_ec *ec = (struct acpi_ec *) context;
659 struct acpi_generic_address *addr;
661 if (resource->id != ACPI_RSTYPE_IO) {
666 * The first address region returned is the data port, and
667 * the second address region returned is the status/command
670 if (ec->data_addr.register_bit_width == 0) {
671 addr = &ec->data_addr;
672 } else if (ec->command_addr.register_bit_width == 0) {
673 addr = &ec->command_addr;
675 return AE_CTRL_TERMINATE;
678 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
679 addr->register_bit_width = 8;
680 addr->register_bit_offset = 0;
681 addr->address = resource->data.io.min_base_address;
689 struct acpi_device *device)
691 acpi_status status = AE_OK;
692 struct acpi_ec *ec = NULL;
694 ACPI_FUNCTION_TRACE("acpi_ec_start");
697 return_VALUE(-EINVAL);
699 ec = acpi_driver_data(device);
702 return_VALUE(-EINVAL);
705 * Get I/O port addresses. Convert to GAS format.
707 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
708 acpi_ec_io_ports, ec);
709 if (ACPI_FAILURE(status) || ec->command_addr.register_bit_width == 0) {
710 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
711 return_VALUE(-ENODEV);
714 ec->status_addr = ec->command_addr;
716 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
717 (u32) ec->gpe_bit, (u32) ec->command_addr.address,
718 (u32) ec->data_addr.address));
721 * Install GPE handler
723 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
724 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
725 if (ACPI_FAILURE(status)) {
726 return_VALUE(-ENODEV);
729 status = acpi_install_address_space_handler (ec->handle,
730 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
731 &acpi_ec_space_setup, ec);
732 if (ACPI_FAILURE(status)) {
733 acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
734 return_VALUE(-ENODEV);
743 struct acpi_device *device,
746 acpi_status status = AE_OK;
747 struct acpi_ec *ec = NULL;
749 ACPI_FUNCTION_TRACE("acpi_ec_stop");
752 return_VALUE(-EINVAL);
754 ec = acpi_driver_data(device);
756 status = acpi_remove_address_space_handler(ec->handle,
757 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
758 if (ACPI_FAILURE(status))
759 return_VALUE(-ENODEV);
761 status = acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
762 if (ACPI_FAILURE(status))
763 return_VALUE(-ENODEV);
770 acpi_ec_ecdt_probe (void)
773 struct acpi_table_ecdt *ecdt_ptr;
775 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
776 (struct acpi_table_header **) &ecdt_ptr);
777 if (ACPI_FAILURE(status))
780 printk(KERN_INFO PREFIX "Found ECDT\n");
783 * Generate a temporary ec context to use until the namespace is scanned
785 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
788 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
790 ec_ecdt->command_addr = ecdt_ptr->ec_control;
791 ec_ecdt->status_addr = ecdt_ptr->ec_control;
792 ec_ecdt->data_addr = ecdt_ptr->ec_data;
793 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
794 ec_ecdt->lock = SPIN_LOCK_UNLOCKED;
795 /* use the GL just to be safe */
796 ec_ecdt->global_lock = TRUE;
797 ec_ecdt->uid = ecdt_ptr->uid;
799 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
800 if (ACPI_FAILURE(status)) {
805 * Install GPE handler
807 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
808 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
810 if (ACPI_FAILURE(status)) {
814 status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
815 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
816 &acpi_ec_space_setup, ec_ecdt);
817 if (ACPI_FAILURE(status)) {
818 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
819 &acpi_ec_gpe_handler);
826 printk(KERN_ERR PREFIX "Could not use ECDT\n");
839 ACPI_FUNCTION_TRACE("acpi_ec_init");
841 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
843 return_VALUE(-ENODEV);
845 result = acpi_bus_register_driver(&acpi_ec_driver);
847 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
848 return_VALUE(-ENODEV);
854 /* EC can't be unloaded atm, so don't compile these */
859 ACPI_FUNCTION_TRACE("acpi_ec_exit");
861 acpi_bus_unregister_driver(&acpi_ec_driver);
863 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);