2 * acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/delay.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/interrupt.h>
36 #include <acpi/acpi_bus.h>
37 #include <acpi/acpi_drivers.h>
38 #include <acpi/actypes.h>
40 #define _COMPONENT ACPI_EC_COMPONENT
41 ACPI_MODULE_NAME ("acpi_ec")
43 #define ACPI_EC_COMPONENT 0x00100000
44 #define ACPI_EC_CLASS "embedded_controller"
45 #define ACPI_EC_HID "PNP0C09"
46 #define ACPI_EC_DRIVER_NAME "ACPI Embedded Controller Driver"
47 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
48 #define ACPI_EC_FILE_INFO "info"
51 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
52 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
53 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
54 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
56 #define ACPI_EC_EVENT_OBF 0x01 /* Output buffer full */
57 #define ACPI_EC_EVENT_IBE 0x02 /* Input buffer empty */
59 #define ACPI_EC_DELAY 50 /* Wait 50ms max. during EC ops */
60 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
62 #define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
63 #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
65 #define ACPI_EC_COMMAND_READ 0x80
66 #define ACPI_EC_COMMAND_WRITE 0x81
67 #define ACPI_EC_BURST_ENABLE 0x82
68 #define ACPI_EC_BURST_DISABLE 0x83
69 #define ACPI_EC_COMMAND_QUERY 0x84
71 #define EC_POLLING 0xFF
75 static int acpi_ec_remove (struct acpi_device *device, int type);
76 static int acpi_ec_start (struct acpi_device *device);
77 static int acpi_ec_stop (struct acpi_device *device, int type);
78 static int acpi_ec_burst_add ( struct acpi_device *device);
79 static int acpi_ec_polling_add ( struct acpi_device *device);
81 static struct acpi_driver acpi_ec_driver = {
82 .name = ACPI_EC_DRIVER_NAME,
83 .class = ACPI_EC_CLASS,
86 .add = acpi_ec_polling_add,
87 .remove = acpi_ec_remove,
88 .start = acpi_ec_start,
97 unsigned long gpe_bit;
98 struct acpi_generic_address status_addr;
99 struct acpi_generic_address command_addr;
100 struct acpi_generic_address data_addr;
101 unsigned long global_lock;
108 unsigned long gpe_bit;
109 struct acpi_generic_address status_addr;
110 struct acpi_generic_address command_addr;
111 struct acpi_generic_address data_addr;
112 unsigned long global_lock;
113 unsigned int expect_event;
114 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort*/
115 atomic_t pending_gpe;
116 struct semaphore sem;
117 wait_queue_head_t wait;
124 unsigned long gpe_bit;
125 struct acpi_generic_address status_addr;
126 struct acpi_generic_address command_addr;
127 struct acpi_generic_address data_addr;
128 unsigned long global_lock;
133 static int acpi_ec_polling_wait ( union acpi_ec *ec, u8 event);
134 static int acpi_ec_burst_wait(union acpi_ec *ec, unsigned int event);
135 static int acpi_ec_polling_read ( union acpi_ec *ec, u8 address, u32 *data);
136 static int acpi_ec_burst_read( union acpi_ec *ec, u8 address, u32 *data);
137 static int acpi_ec_polling_write ( union acpi_ec *ec, u8 address, u8 data);
138 static int acpi_ec_burst_write ( union acpi_ec *ec, u8 address, u8 data);
139 static int acpi_ec_polling_query ( union acpi_ec *ec, u32 *data);
140 static int acpi_ec_burst_query ( union acpi_ec *ec, u32 *data);
141 static void acpi_ec_gpe_polling_query ( void *ec_cxt);
142 static void acpi_ec_gpe_burst_query ( void *ec_cxt);
143 static u32 acpi_ec_gpe_polling_handler ( void *data);
144 static u32 acpi_ec_gpe_burst_handler ( void *data);
145 static acpi_status __init
146 acpi_fake_ecdt_polling_callback (
152 static acpi_status __init
153 acpi_fake_ecdt_burst_callback (
160 acpi_ec_polling_get_real_ecdt(void);
162 acpi_ec_burst_get_real_ecdt(void);
163 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
164 static union acpi_ec *ec_ecdt;
166 /* External interfaces use first EC only, so remember */
167 static struct acpi_device *first_ec;
168 static int acpi_ec_polling_mode = EC_POLLING;
170 /* --------------------------------------------------------------------------
171 Transaction Management
172 -------------------------------------------------------------------------- */
174 static inline u32 acpi_ec_read_status(union acpi_ec *ec)
178 acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
187 if (acpi_ec_polling_mode)
188 return acpi_ec_polling_wait (ec, event);
190 return acpi_ec_burst_wait (ec, event);
194 acpi_ec_polling_wait (
198 u32 acpi_ec_status = 0;
199 u32 i = ACPI_EC_UDELAY_COUNT;
204 /* Poll the EC status register waiting for the event to occur. */
206 case ACPI_EC_EVENT_OBF:
208 acpi_hw_low_level_read(8, &acpi_ec_status, &ec->common.status_addr);
209 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
211 udelay(ACPI_EC_UDELAY);
214 case ACPI_EC_EVENT_IBE:
216 acpi_hw_low_level_read(8, &acpi_ec_status, &ec->common.status_addr);
217 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
219 udelay(ACPI_EC_UDELAY);
228 static int acpi_ec_burst_wait(union acpi_ec *ec, unsigned int event)
232 ACPI_FUNCTION_TRACE("acpi_ec_wait");
234 ec->burst.expect_event = event;
237 result = wait_event_interruptible_timeout(ec->burst.wait,
238 !ec->burst.expect_event,
239 msecs_to_jiffies(ACPI_EC_DELAY));
241 ec->burst.expect_event = 0;
245 ACPI_DEBUG_PRINT((ACPI_DB_ERROR," result = %d ", result));
246 return_VALUE(result);
250 * Verify that the event in question has actually happened by
251 * querying EC status. Do the check even if operation timed-out
252 * to make sure that we did not miss interrupt.
255 case ACPI_EC_EVENT_OBF:
256 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
260 case ACPI_EC_EVENT_IBE:
261 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
266 return_VALUE(-ETIME);
272 acpi_ec_enter_burst_mode (
278 ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
280 status = acpi_ec_read_status(ec);
281 if (status != -EINVAL &&
282 !(status & ACPI_EC_FLAG_BURST)){
283 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->common.command_addr);
284 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
286 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
287 return_VALUE(-EINVAL);
289 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
290 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
291 if(tmp != 0x90 ) {/* Burst ACK byte*/
292 return_VALUE(-EINVAL);
296 atomic_set(&ec->burst.leaving_burst , 0);
301 acpi_ec_leave_burst_mode (
306 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
308 atomic_set(&ec->burst.leaving_burst , 1);
309 status = acpi_ec_read_status(ec);
310 if (status != -EINVAL &&
311 (status & ACPI_EC_FLAG_BURST)){
312 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->common.command_addr);
313 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
315 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
316 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"------->wait fail\n"));
317 return_VALUE(-EINVAL);
319 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
320 status = acpi_ec_read_status(ec);
332 if (acpi_ec_polling_mode)
333 return acpi_ec_polling_read(ec, address, data);
335 return acpi_ec_burst_read(ec, address, data);
343 if (acpi_ec_polling_mode)
344 return acpi_ec_polling_write(ec, address, data);
346 return acpi_ec_burst_write(ec, address, data);
349 acpi_ec_polling_read (
354 acpi_status status = AE_OK;
356 unsigned long flags = 0;
359 ACPI_FUNCTION_TRACE("acpi_ec_read");
362 return_VALUE(-EINVAL);
366 if (ec->common.global_lock) {
367 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
368 if (ACPI_FAILURE(status))
369 return_VALUE(-ENODEV);
372 spin_lock_irqsave(&ec->polling.lock, flags);
374 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->common.command_addr);
375 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
379 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
380 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
384 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
386 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
390 spin_unlock_irqrestore(&ec->polling.lock, flags);
392 if (ec->common.global_lock)
393 acpi_release_global_lock(glk);
395 return_VALUE(result);
400 acpi_ec_polling_write (
406 acpi_status status = AE_OK;
407 unsigned long flags = 0;
410 ACPI_FUNCTION_TRACE("acpi_ec_write");
413 return_VALUE(-EINVAL);
415 if (ec->common.global_lock) {
416 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
417 if (ACPI_FAILURE(status))
418 return_VALUE(-ENODEV);
421 spin_lock_irqsave(&ec->polling.lock, flags);
423 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->common.command_addr);
424 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
428 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
429 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
433 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
434 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
438 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
442 spin_unlock_irqrestore(&ec->polling.lock, flags);
444 if (ec->common.global_lock)
445 acpi_release_global_lock(glk);
447 return_VALUE(result);
459 ACPI_FUNCTION_TRACE("acpi_ec_read");
462 return_VALUE(-EINVAL);
467 if (ec->common.global_lock) {
468 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
469 if (ACPI_FAILURE(status))
470 return_VALUE(-ENODEV);
473 WARN_ON(in_interrupt());
474 down(&ec->burst.sem);
476 if(acpi_ec_enter_burst_mode(ec))
479 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->common.command_addr);
480 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
481 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
486 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
487 status= acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
489 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
493 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
494 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
496 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
500 acpi_ec_leave_burst_mode(ec);
503 if (ec->common.global_lock)
504 acpi_release_global_lock(glk);
506 if(atomic_read(&ec->burst.leaving_burst) == 2){
507 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
508 while(atomic_read(&ec->burst.pending_gpe)){
511 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
515 return_VALUE(status);
520 acpi_ec_burst_write (
529 ACPI_FUNCTION_TRACE("acpi_ec_write");
532 return_VALUE(-EINVAL);
534 if (ec->common.global_lock) {
535 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
536 if (ACPI_FAILURE(status))
537 return_VALUE(-ENODEV);
540 WARN_ON(in_interrupt());
541 down(&ec->burst.sem);
543 if(acpi_ec_enter_burst_mode(ec))
546 status = acpi_ec_read_status(ec);
547 if (status != -EINVAL &&
548 !(status & ACPI_EC_FLAG_BURST)){
549 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->common.command_addr);
550 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
553 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
554 if(tmp != 0x90 ) /* Burst ACK byte*/
557 /*Now we are in burst mode*/
559 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->common.command_addr);
560 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
561 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
566 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
567 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
569 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
573 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
574 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
575 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
579 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
583 acpi_ec_leave_burst_mode(ec);
586 if (ec->common.global_lock)
587 acpi_release_global_lock(glk);
589 if(atomic_read(&ec->burst.leaving_burst) == 2){
590 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
591 while(atomic_read(&ec->burst.pending_gpe)){
594 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
598 return_VALUE(status);
602 * Externally callable EC access functions. For now, assume 1 EC only
605 ec_read(u8 addr, u8 *val)
614 ec = acpi_driver_data(first_ec);
616 err = acpi_ec_read(ec, addr, &temp_data);
625 EXPORT_SYMBOL(ec_read);
628 ec_write(u8 addr, u8 val)
636 ec = acpi_driver_data(first_ec);
638 err = acpi_ec_write(ec, addr, val);
642 EXPORT_SYMBOL(ec_write);
649 if (acpi_ec_polling_mode)
650 return acpi_ec_polling_query(ec, data);
652 return acpi_ec_burst_query(ec, data);
655 acpi_ec_polling_query (
660 acpi_status status = AE_OK;
661 unsigned long flags = 0;
664 ACPI_FUNCTION_TRACE("acpi_ec_query");
667 return_VALUE(-EINVAL);
671 if (ec->common.global_lock) {
672 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
673 if (ACPI_FAILURE(status))
674 return_VALUE(-ENODEV);
678 * Query the EC to find out which _Qxx method we need to evaluate.
679 * Note that successful completion of the query causes the ACPI_EC_SCI
680 * bit to be cleared (and thus clearing the interrupt source).
682 spin_lock_irqsave(&ec->polling.lock, flags);
684 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->common.command_addr);
685 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
689 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
694 spin_unlock_irqrestore(&ec->polling.lock, flags);
696 if (ec->common.global_lock)
697 acpi_release_global_lock(glk);
699 return_VALUE(result);
702 acpi_ec_burst_query (
709 ACPI_FUNCTION_TRACE("acpi_ec_query");
712 return_VALUE(-EINVAL);
715 if (ec->common.global_lock) {
716 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
717 if (ACPI_FAILURE(status))
718 return_VALUE(-ENODEV);
721 down(&ec->burst.sem);
722 if(acpi_ec_enter_burst_mode(ec))
725 * Query the EC to find out which _Qxx method we need to evaluate.
726 * Note that successful completion of the query causes the ACPI_EC_SCI
727 * bit to be cleared (and thus clearing the interrupt source).
729 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->common.command_addr);
730 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
732 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
736 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
737 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
742 acpi_ec_leave_burst_mode(ec);
745 if (ec->common.global_lock)
746 acpi_release_global_lock(glk);
748 if(atomic_read(&ec->burst.leaving_burst) == 2){
749 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
750 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
753 return_VALUE(status);
757 /* --------------------------------------------------------------------------
759 -------------------------------------------------------------------------- */
761 union acpi_ec_query_data {
770 if (acpi_ec_polling_mode)
771 acpi_ec_gpe_polling_query(ec_cxt);
773 acpi_ec_gpe_burst_query(ec_cxt);
777 acpi_ec_gpe_polling_query (
780 union acpi_ec *ec = (union acpi_ec *) ec_cxt;
782 unsigned long flags = 0;
783 static char object_name[5] = {'_','Q','0','0','\0'};
784 const char hex[] = {'0','1','2','3','4','5','6','7',
785 '8','9','A','B','C','D','E','F'};
787 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
792 spin_lock_irqsave(&ec->polling.lock, flags);
793 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
794 spin_unlock_irqrestore(&ec->polling.lock, flags);
796 /* TBD: Implement asynch events!
797 * NOTE: All we care about are EC-SCI's. Other EC events are
798 * handled via polling (yuck!). This is because some systems
799 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
800 * a purely interrupt-driven approach (grumble, grumble).
802 if (!(value & ACPI_EC_FLAG_SCI))
805 if (acpi_ec_query(ec, &value))
808 object_name[2] = hex[((value >> 4) & 0x0F)];
809 object_name[3] = hex[(value & 0x0F)];
811 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
813 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
816 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
819 acpi_ec_gpe_burst_query (
822 union acpi_ec *ec = (union acpi_ec *) ec_cxt;
824 int result = -ENODATA;
825 static char object_name[5] = {'_','Q','0','0','\0'};
826 const char hex[] = {'0','1','2','3','4','5','6','7',
827 '8','9','A','B','C','D','E','F'};
829 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
831 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
832 result = acpi_ec_query(ec, &value);
837 object_name[2] = hex[((value >> 4) & 0x0F)];
838 object_name[3] = hex[(value & 0x0F)];
840 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
842 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
844 atomic_dec(&ec->burst.pending_gpe);
849 acpi_ec_gpe_handler (
852 if (acpi_ec_polling_mode)
853 return acpi_ec_gpe_polling_handler(data);
855 return acpi_ec_gpe_burst_handler(data);
858 acpi_ec_gpe_polling_handler (
861 acpi_status status = AE_OK;
862 union acpi_ec *ec = (union acpi_ec *) data;
865 return ACPI_INTERRUPT_NOT_HANDLED;
867 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
869 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
870 acpi_ec_gpe_query, ec);
873 return ACPI_INTERRUPT_HANDLED;
875 return ACPI_INTERRUPT_NOT_HANDLED;
878 acpi_ec_gpe_burst_handler (
881 acpi_status status = AE_OK;
883 union acpi_ec *ec = (union acpi_ec *) data;
886 return ACPI_INTERRUPT_NOT_HANDLED;
888 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
890 value = acpi_ec_read_status(ec);
892 if((value & ACPI_EC_FLAG_IBF) &&
893 !(value & ACPI_EC_FLAG_BURST) &&
894 (atomic_read(&ec->burst.leaving_burst) == 0)) {
896 * the embedded controller disables
897 * burst mode for any reason other
898 * than the burst disable command
899 * to process critical event.
901 atomic_set(&ec->burst.leaving_burst , 2); /* block current pending transaction
903 wake_up(&ec->burst.wait);
905 if ((ec->burst.expect_event == ACPI_EC_EVENT_OBF &&
906 (value & ACPI_EC_FLAG_OBF)) ||
907 (ec->burst.expect_event == ACPI_EC_EVENT_IBE &&
908 !(value & ACPI_EC_FLAG_IBF))) {
909 ec->burst.expect_event = 0;
910 wake_up(&ec->burst.wait);
911 return ACPI_INTERRUPT_HANDLED;
915 if (value & ACPI_EC_FLAG_SCI){
916 atomic_add(1, &ec->burst.pending_gpe) ;
917 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
918 acpi_ec_gpe_query, ec);
919 return status == AE_OK ?
920 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
922 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
923 return status == AE_OK ?
924 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
927 /* --------------------------------------------------------------------------
928 Address Space Management
929 -------------------------------------------------------------------------- */
932 acpi_ec_space_setup (
933 acpi_handle region_handle,
935 void *handler_context,
936 void **return_context)
939 * The EC object is in the handler context and is needed
940 * when calling the acpi_ec_space_handler.
942 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
943 handler_context : NULL;
950 acpi_ec_space_handler (
952 acpi_physical_address address,
955 void *handler_context,
956 void *region_context)
959 union acpi_ec *ec = NULL;
961 acpi_integer f_v = 0;
964 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
966 if ((address > 0xFF) || !value || !handler_context)
967 return_VALUE(AE_BAD_PARAMETER);
969 if (bit_width != 8 && acpi_strict) {
970 printk(KERN_WARNING PREFIX "acpi_ec_space_handler: bit_width should be 8\n");
971 return_VALUE(AE_BAD_PARAMETER);
974 ec = (union acpi_ec *) handler_context;
980 result = acpi_ec_read(ec, (u8) address, (u32 *)&temp);
983 result = acpi_ec_write(ec, (u8) address, (u8) temp);
993 if (function == ACPI_READ)
994 f_v |= temp << 8 * i;
995 if (function == ACPI_WRITE)
1002 if (function == ACPI_READ) {
1003 f_v |= temp << 8 * i;
1011 return_VALUE(AE_BAD_PARAMETER);
1014 return_VALUE(AE_NOT_FOUND);
1017 return_VALUE(AE_TIME);
1020 return_VALUE(AE_OK);
1025 /* --------------------------------------------------------------------------
1026 FS Interface (/proc)
1027 -------------------------------------------------------------------------- */
1029 static struct proc_dir_entry *acpi_ec_dir;
1033 acpi_ec_read_info (struct seq_file *seq, void *offset)
1035 union acpi_ec *ec = (union acpi_ec *) seq->private;
1037 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
1042 seq_printf(seq, "gpe bit: 0x%02x\n",
1043 (u32) ec->common.gpe_bit);
1044 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
1045 (u32) ec->common.status_addr.address, (u32) ec->common.data_addr.address);
1046 seq_printf(seq, "use global lock: %s\n",
1047 ec->common.global_lock?"yes":"no");
1048 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1054 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
1056 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
1059 static struct file_operations acpi_ec_info_ops = {
1060 .open = acpi_ec_info_open_fs,
1062 .llseek = seq_lseek,
1063 .release = single_release,
1064 .owner = THIS_MODULE,
1069 struct acpi_device *device)
1071 struct proc_dir_entry *entry = NULL;
1073 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
1075 if (!acpi_device_dir(device)) {
1076 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1078 if (!acpi_device_dir(device))
1079 return_VALUE(-ENODEV);
1082 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
1083 acpi_device_dir(device));
1085 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
1086 "Unable to create '%s' fs entry\n",
1087 ACPI_EC_FILE_INFO));
1089 entry->proc_fops = &acpi_ec_info_ops;
1090 entry->data = acpi_driver_data(device);
1091 entry->owner = THIS_MODULE;
1100 struct acpi_device *device)
1102 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
1104 if (acpi_device_dir(device)) {
1105 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
1106 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
1107 acpi_device_dir(device) = NULL;
1114 /* --------------------------------------------------------------------------
1116 -------------------------------------------------------------------------- */
1120 acpi_ec_polling_add (
1121 struct acpi_device *device)
1124 acpi_status status = AE_OK;
1125 union acpi_ec *ec = NULL;
1128 ACPI_FUNCTION_TRACE("acpi_ec_add");
1131 return_VALUE(-EINVAL);
1133 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1135 return_VALUE(-ENOMEM);
1136 memset(ec, 0, sizeof(union acpi_ec));
1138 ec->common.handle = device->handle;
1139 ec->common.uid = -1;
1140 spin_lock_init(&ec->polling.lock);
1141 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1142 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1143 acpi_driver_data(device) = ec;
1145 /* Use the global lock for all EC transactions? */
1146 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL, &ec->common.global_lock);
1148 /* If our UID matches the UID for the ECDT-enumerated EC,
1149 we now have the *real* EC info, so kill the makeshift one.*/
1150 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1151 if (ec_ecdt && ec_ecdt->common.uid == uid) {
1152 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1153 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
1155 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit, &acpi_ec_gpe_handler);
1160 /* Get GPE bit assignment (EC events). */
1161 /* TODO: Add support for _GPE returning a package */
1162 status = acpi_evaluate_integer(ec->common.handle, "_GPE", NULL, &ec->common.gpe_bit);
1163 if (ACPI_FAILURE(status)) {
1164 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1165 "Error obtaining GPE bit assignment\n"));
1170 result = acpi_ec_add_fs(device);
1174 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
1175 acpi_device_name(device), acpi_device_bid(device),
1176 (u32) ec->common.gpe_bit);
1185 return_VALUE(result);
1189 struct acpi_device *device)
1192 acpi_status status = AE_OK;
1193 union acpi_ec *ec = NULL;
1196 ACPI_FUNCTION_TRACE("acpi_ec_add");
1199 return_VALUE(-EINVAL);
1201 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1203 return_VALUE(-ENOMEM);
1204 memset(ec, 0, sizeof(union acpi_ec));
1206 ec->common.handle = device->handle;
1207 ec->common.uid = -1;
1208 atomic_set(&ec->burst.pending_gpe, 0);
1209 atomic_set(&ec->burst.leaving_burst , 1);
1210 init_MUTEX(&ec->burst.sem);
1211 init_waitqueue_head(&ec->burst.wait);
1212 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1213 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1214 acpi_driver_data(device) = ec;
1216 /* Use the global lock for all EC transactions? */
1217 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL, &ec->common.global_lock);
1219 /* If our UID matches the UID for the ECDT-enumerated EC,
1220 we now have the *real* EC info, so kill the makeshift one.*/
1221 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1222 if (ec_ecdt && ec_ecdt->common.uid == uid) {
1223 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1224 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
1226 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit, &acpi_ec_gpe_handler);
1231 /* Get GPE bit assignment (EC events). */
1232 /* TODO: Add support for _GPE returning a package */
1233 status = acpi_evaluate_integer(ec->common.handle, "_GPE", NULL, &ec->common.gpe_bit);
1234 if (ACPI_FAILURE(status)) {
1235 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1236 "Error obtaining GPE bit assignment\n"));
1241 result = acpi_ec_add_fs(device);
1245 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
1246 acpi_device_name(device), acpi_device_bid(device),
1247 (u32) ec->common.gpe_bit);
1256 return_VALUE(result);
1262 struct acpi_device *device,
1265 union acpi_ec *ec = NULL;
1267 ACPI_FUNCTION_TRACE("acpi_ec_remove");
1270 return_VALUE(-EINVAL);
1272 ec = acpi_driver_data(device);
1274 acpi_ec_remove_fs(device);
1284 struct acpi_resource *resource,
1287 union acpi_ec *ec = (union acpi_ec *) context;
1288 struct acpi_generic_address *addr;
1290 if (resource->id != ACPI_RSTYPE_IO) {
1295 * The first address region returned is the data port, and
1296 * the second address region returned is the status/command
1299 if (ec->common.data_addr.register_bit_width == 0) {
1300 addr = &ec->common.data_addr;
1301 } else if (ec->common.command_addr.register_bit_width == 0) {
1302 addr = &ec->common.command_addr;
1304 return AE_CTRL_TERMINATE;
1307 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1308 addr->register_bit_width = 8;
1309 addr->register_bit_offset = 0;
1310 addr->address = resource->data.io.min_base_address;
1318 struct acpi_device *device)
1320 acpi_status status = AE_OK;
1321 union acpi_ec *ec = NULL;
1323 ACPI_FUNCTION_TRACE("acpi_ec_start");
1326 return_VALUE(-EINVAL);
1328 ec = acpi_driver_data(device);
1331 return_VALUE(-EINVAL);
1334 * Get I/O port addresses. Convert to GAS format.
1336 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
1337 acpi_ec_io_ports, ec);
1338 if (ACPI_FAILURE(status) || ec->common.command_addr.register_bit_width == 0) {
1339 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
1340 return_VALUE(-ENODEV);
1343 ec->common.status_addr = ec->common.command_addr;
1345 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
1346 (u32) ec->common.gpe_bit, (u32) ec->common.command_addr.address,
1347 (u32) ec->common.data_addr.address));
1351 * Install GPE handler
1353 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
1354 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
1355 if (ACPI_FAILURE(status)) {
1356 return_VALUE(-ENODEV);
1358 acpi_set_gpe_type (NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1359 acpi_enable_gpe (NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1361 status = acpi_install_address_space_handler (ec->common.handle,
1362 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
1363 &acpi_ec_space_setup, ec);
1364 if (ACPI_FAILURE(status)) {
1365 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit, &acpi_ec_gpe_handler);
1366 return_VALUE(-ENODEV);
1369 return_VALUE(AE_OK);
1375 struct acpi_device *device,
1378 acpi_status status = AE_OK;
1379 union acpi_ec *ec = NULL;
1381 ACPI_FUNCTION_TRACE("acpi_ec_stop");
1384 return_VALUE(-EINVAL);
1386 ec = acpi_driver_data(device);
1388 status = acpi_remove_address_space_handler(ec->common.handle,
1389 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
1390 if (ACPI_FAILURE(status))
1391 return_VALUE(-ENODEV);
1393 status = acpi_remove_gpe_handler(NULL, ec->common.gpe_bit, &acpi_ec_gpe_handler);
1394 if (ACPI_FAILURE(status))
1395 return_VALUE(-ENODEV);
1400 static acpi_status __init
1401 acpi_fake_ecdt_callback (
1408 if (acpi_ec_polling_mode)
1409 return acpi_fake_ecdt_polling_callback(handle,
1410 Level, context, retval);
1412 return acpi_fake_ecdt_burst_callback(handle,
1413 Level, context, retval);
1416 static acpi_status __init
1417 acpi_fake_ecdt_polling_callback (
1425 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1426 acpi_ec_io_ports, ec_ecdt);
1427 if (ACPI_FAILURE(status))
1429 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1431 ec_ecdt->common.uid = -1;
1432 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1434 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->common.gpe_bit);
1435 if (ACPI_FAILURE(status))
1437 spin_lock_init(&ec_ecdt->polling.lock);
1438 ec_ecdt->common.global_lock = TRUE;
1439 ec_ecdt->common.handle = handle;
1441 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1442 (u32) ec_ecdt->common.gpe_bit, (u32) ec_ecdt->common.command_addr.address,
1443 (u32) ec_ecdt->common.data_addr.address);
1445 return AE_CTRL_TERMINATE;
1448 static acpi_status __init
1449 acpi_fake_ecdt_burst_callback (
1457 init_MUTEX(&ec_ecdt->burst.sem);
1458 init_waitqueue_head(&ec_ecdt->burst.wait);
1459 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1460 acpi_ec_io_ports, ec_ecdt);
1461 if (ACPI_FAILURE(status))
1463 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1465 ec_ecdt->common.uid = -1;
1466 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1468 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->common.gpe_bit);
1469 if (ACPI_FAILURE(status))
1471 ec_ecdt->common.global_lock = TRUE;
1472 ec_ecdt->common.handle = handle;
1474 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1475 (u32) ec_ecdt->common.gpe_bit, (u32) ec_ecdt->common.command_addr.address,
1476 (u32) ec_ecdt->common.data_addr.address);
1478 return AE_CTRL_TERMINATE;
1482 * Some BIOS (such as some from Gateway laptops) access EC region very early
1483 * such as in BAT0._INI or EC._INI before an EC device is found and
1484 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1485 * required, but if EC regison is accessed early, it is required.
1486 * The routine tries to workaround the BIOS bug by pre-scan EC device
1487 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1488 * op region (since _REG isn't invoked yet). The assumption is true for
1489 * all systems found.
1492 acpi_ec_fake_ecdt(void)
1497 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1499 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1504 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1506 status = acpi_get_devices (ACPI_EC_HID,
1507 acpi_fake_ecdt_callback,
1510 if (ACPI_FAILURE(status)) {
1518 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1523 acpi_ec_get_real_ecdt(void)
1525 if (acpi_ec_polling_mode)
1526 return acpi_ec_polling_get_real_ecdt();
1528 return acpi_ec_burst_get_real_ecdt();
1532 acpi_ec_polling_get_real_ecdt(void)
1535 struct acpi_table_ecdt *ecdt_ptr;
1537 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1538 (struct acpi_table_header **) &ecdt_ptr);
1539 if (ACPI_FAILURE(status))
1542 printk(KERN_INFO PREFIX "Found ECDT\n");
1545 * Generate a temporary ec context to use until the namespace is scanned
1547 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1550 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1552 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1553 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1554 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1555 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1556 spin_lock_init(&ec_ecdt->polling.lock);
1557 /* use the GL just to be safe */
1558 ec_ecdt->common.global_lock = TRUE;
1559 ec_ecdt->common.uid = ecdt_ptr->uid;
1561 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1562 if (ACPI_FAILURE(status)) {
1568 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1577 acpi_ec_burst_get_real_ecdt(void)
1580 struct acpi_table_ecdt *ecdt_ptr;
1582 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1583 (struct acpi_table_header **) &ecdt_ptr);
1584 if (ACPI_FAILURE(status))
1587 printk(KERN_INFO PREFIX "Found ECDT\n");
1590 * Generate a temporary ec context to use until the namespace is scanned
1592 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1595 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1597 init_MUTEX(&ec_ecdt->burst.sem);
1598 init_waitqueue_head(&ec_ecdt->burst.wait);
1599 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1600 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1601 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1602 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1603 /* use the GL just to be safe */
1604 ec_ecdt->common.global_lock = TRUE;
1605 ec_ecdt->common.uid = ecdt_ptr->uid;
1607 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1608 if (ACPI_FAILURE(status)) {
1614 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1621 static int __initdata acpi_fake_ecdt_enabled;
1623 acpi_ec_ecdt_probe (void)
1628 ret = acpi_ec_get_real_ecdt();
1629 /* Try to make a fake ECDT */
1630 if (ret && acpi_fake_ecdt_enabled) {
1631 ret = acpi_ec_fake_ecdt();
1638 * Install GPE handler
1640 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1641 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
1643 if (ACPI_FAILURE(status)) {
1646 acpi_set_gpe_type (NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1647 acpi_enable_gpe (NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
1649 status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
1650 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
1651 &acpi_ec_space_setup, ec_ecdt);
1652 if (ACPI_FAILURE(status)) {
1653 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1654 &acpi_ec_gpe_handler);
1661 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1669 static int __init acpi_ec_init (void)
1673 ACPI_FUNCTION_TRACE("acpi_ec_init");
1678 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1680 return_VALUE(-ENODEV);
1682 /* Now register the driver for the EC */
1683 result = acpi_bus_register_driver(&acpi_ec_driver);
1685 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1686 return_VALUE(-ENODEV);
1689 return_VALUE(result);
1692 subsys_initcall(acpi_ec_init);
1694 /* EC driver currently not unloadable */
1699 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1701 acpi_bus_unregister_driver(&acpi_ec_driver);
1703 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1709 static int __init acpi_fake_ecdt_setup(char *str)
1711 acpi_fake_ecdt_enabled = 1;
1715 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1716 static int __init acpi_ec_set_polling_mode(char *str)
1720 if (!get_option(&str, &burst))
1724 acpi_ec_polling_mode = EC_BURST;
1725 acpi_ec_driver.ops.add = acpi_ec_burst_add;
1727 acpi_ec_polling_mode = EC_POLLING;
1728 acpi_ec_driver.ops.add = acpi_ec_polling_add;
1730 printk(KERN_INFO PREFIX "EC %s mode.\n",
1731 burst ? "burst": "polling");
1734 __setup("ec_burst=", acpi_ec_set_polling_mode);