The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/acpica/acpi_ec.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2003 Nate Lawson
    3  * Copyright (c) 2000 Michael Smith
    4  * Copyright (c) 2000 BSDi
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  */
   28 /******************************************************************************
   29  *
   30  * 1. Copyright Notice
   31  *
   32  * Some or all of this work - Copyright (c) 1999, Intel Corp.  All rights
   33  * reserved.
   34  *
   35  * 2. License
   36  *
   37  * 2.1. This is your license from Intel Corp. under its intellectual property
   38  * rights.  You may have additional license terms from the party that provided
   39  * you this software, covering your right to use that party's intellectual
   40  * property rights.
   41  *
   42  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
   43  * copy of the source code appearing in this file ("Covered Code") an
   44  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
   45  * base code distributed originally by Intel ("Original Intel Code") to copy,
   46  * make derivatives, distribute, use and display any portion of the Covered
   47  * Code in any form, with the right to sublicense such rights; and
   48  *
   49  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
   50  * license (with the right to sublicense), under only those claims of Intel
   51  * patents that are infringed by the Original Intel Code, to make, use, sell,
   52  * offer to sell, and import the Covered Code and derivative works thereof
   53  * solely to the minimum extent necessary to exercise the above copyright
   54  * license, and in no event shall the patent license extend to any additions
   55  * to or modifications of the Original Intel Code.  No other license or right
   56  * is granted directly or by implication, estoppel or otherwise;
   57  *
   58  * The above copyright and patent license is granted only if the following
   59  * conditions are met:
   60  *
   61  * 3. Conditions 
   62  *
   63  * 3.1. Redistribution of Source with Rights to Further Distribute Source.  
   64  * Redistribution of source code of any substantial portion of the Covered
   65  * Code or modification with rights to further distribute source must include
   66  * the above Copyright Notice, the above License, this list of Conditions,
   67  * and the following Disclaimer and Export Compliance provision.  In addition,
   68  * Licensee must cause all Covered Code to which Licensee contributes to
   69  * contain a file documenting the changes Licensee made to create that Covered
   70  * Code and the date of any change.  Licensee must include in that file the
   71  * documentation of any changes made by any predecessor Licensee.  Licensee 
   72  * must include a prominent statement that the modification is derived,
   73  * directly or indirectly, from Original Intel Code.
   74  *
   75  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.  
   76  * Redistribution of source code of any substantial portion of the Covered
   77  * Code or modification without rights to further distribute source must
   78  * include the following Disclaimer and Export Compliance provision in the
   79  * documentation and/or other materials provided with distribution.  In
   80  * addition, Licensee may not authorize further sublicense of source of any
   81  * portion of the Covered Code, and must include terms to the effect that the
   82  * license from Licensee to its licensee is limited to the intellectual
   83  * property embodied in the software Licensee provides to its licensee, and
   84  * not to intellectual property embodied in modifications its licensee may
   85  * make.
   86  *
   87  * 3.3. Redistribution of Executable. Redistribution in executable form of any
   88  * substantial portion of the Covered Code or modification must reproduce the
   89  * above Copyright Notice, and the following Disclaimer and Export Compliance
   90  * provision in the documentation and/or other materials provided with the
   91  * distribution.
   92  *
   93  * 3.4. Intel retains all right, title, and interest in and to the Original
   94  * Intel Code.
   95  *
   96  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
   97  * Intel shall be used in advertising or otherwise to promote the sale, use or
   98  * other dealings in products derived from or relating to the Covered Code
   99  * without prior written authorization from Intel.
  100  *
  101  * 4. Disclaimer and Export Compliance
  102  *
  103  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
  104  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
  105  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
  106  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
  107  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
  108  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
  109  * PARTICULAR PURPOSE. 
  110  *
  111  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
  112  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
  113  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
  114  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
  115  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
  116  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
  117  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
  118  * LIMITED REMEDY.
  119  *
  120  * 4.3. Licensee shall not export, either directly or indirectly, any of this
  121  * software or system incorporating such software without first obtaining any
  122  * required license or other approval from the U. S. Department of Commerce or
  123  * any other agency or department of the United States Government.  In the
  124  * event Licensee exports any such software from the United States or
  125  * re-exports any such software from a foreign destination, Licensee shall
  126  * ensure that the distribution and export/re-export of the software is in
  127  * compliance with all laws, regulations, orders, or other restrictions of the
  128  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
  129  * any of its subsidiaries will export/re-export any technical data, process,
  130  * software, or service, directly or indirectly, to any country for which the
  131  * United States government or any agency thereof requires an export license,
  132  * other governmental approval, or letter of assurance, without first obtaining
  133  * such license, approval or letter.
  134  *
  135  *****************************************************************************/
  136 
  137 #include <sys/cdefs.h>
  138 __FBSDID("$FreeBSD: releng/5.3/sys/dev/acpica/acpi_ec.c 133617 2004-08-13 06:21:53Z njl $");
  139 
  140 #include "opt_acpi.h"
  141 #include <sys/param.h>
  142 #include <sys/kernel.h>
  143 #include <sys/bus.h>
  144 #include <sys/malloc.h>
  145 #include <sys/module.h>
  146 #include <sys/sx.h>
  147 
  148 #include <machine/bus.h>
  149 #include <machine/resource.h>
  150 #include <sys/rman.h>
  151 
  152 #include "acpi.h"
  153 #include <dev/acpica/acpivar.h>
  154 
  155 /* Hooks for the ACPI CA debugging infrastructure */
  156 #define _COMPONENT      ACPI_EC
  157 ACPI_MODULE_NAME("EC")
  158 
  159 /*
  160  * EC_COMMAND:
  161  * -----------
  162  */
  163 typedef UINT8                           EC_COMMAND;
  164 
  165 #define EC_COMMAND_UNKNOWN              ((EC_COMMAND) 0x00)
  166 #define EC_COMMAND_READ                 ((EC_COMMAND) 0x80)
  167 #define EC_COMMAND_WRITE                ((EC_COMMAND) 0x81)
  168 #define EC_COMMAND_BURST_ENABLE         ((EC_COMMAND) 0x82)
  169 #define EC_COMMAND_BURST_DISABLE        ((EC_COMMAND) 0x83)
  170 #define EC_COMMAND_QUERY                ((EC_COMMAND) 0x84)
  171 
  172 /* 
  173  * EC_STATUS:
  174  * ----------
  175  * The encoding of the EC status register is illustrated below.
  176  * Note that a set bit (1) indicates the property is TRUE
  177  * (e.g. if bit 0 is set then the output buffer is full).
  178  * +-+-+-+-+-+-+-+-+
  179  * |7|6|5|4|3|2|1|0|    
  180  * +-+-+-+-+-+-+-+-+
  181  *  | | | | | | | |
  182  *  | | | | | | | +- Output Buffer Full?
  183  *  | | | | | | +--- Input Buffer Full?
  184  *  | | | | | +----- <reserved>
  185  *  | | | | +------- Data Register is Command Byte?
  186  *  | | | +--------- Burst Mode Enabled?
  187  *  | | +----------- SCI Event?
  188  *  | +------------- SMI Event?
  189  *  +--------------- <Reserved>
  190  *
  191  */
  192 typedef UINT8                           EC_STATUS;
  193 
  194 #define EC_FLAG_OUTPUT_BUFFER           ((EC_STATUS) 0x01)
  195 #define EC_FLAG_INPUT_BUFFER            ((EC_STATUS) 0x02)
  196 #define EC_FLAG_BURST_MODE              ((EC_STATUS) 0x10)
  197 #define EC_FLAG_SCI                     ((EC_STATUS) 0x20)
  198 
  199 /*
  200  * EC_EVENT:
  201  * ---------
  202  */
  203 typedef UINT8                           EC_EVENT;
  204 
  205 #define EC_EVENT_UNKNOWN                ((EC_EVENT) 0x00)
  206 #define EC_EVENT_OUTPUT_BUFFER_FULL     ((EC_EVENT) 0x01)
  207 #define EC_EVENT_INPUT_BUFFER_EMPTY     ((EC_EVENT) 0x02)
  208 #define EC_EVENT_SCI                    ((EC_EVENT) 0x20)
  209 
  210 /*
  211  * Register access primitives
  212  */
  213 #define EC_GET_DATA(sc)                                                 \
  214         bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
  215 
  216 #define EC_SET_DATA(sc, v)                                              \
  217         bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
  218 
  219 #define EC_GET_CSR(sc)                                                  \
  220         bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
  221 
  222 #define EC_SET_CSR(sc, v)                                               \
  223         bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
  224 
  225 /* Embedded Controller Boot Resources Table (ECDT) */
  226 typedef struct {
  227     ACPI_TABLE_HEADER           header;
  228     ACPI_GENERIC_ADDRESS        control;
  229     ACPI_GENERIC_ADDRESS        data;
  230     UINT32                      uid;
  231     UINT8                       gpe_bit;
  232     char                        ec_id[0];
  233 } ACPI_TABLE_ECDT;
  234 
  235 /* Additional params to pass from the probe routine */
  236 struct acpi_ec_params {
  237     int         glk;
  238     int         gpe_bit;
  239     ACPI_HANDLE gpe_handle;
  240     int         uid;
  241 };
  242 
  243 /* Indicate that this device has already been probed via ECDT. */
  244 #define DEV_ECDT(x)             (acpi_get_magic(x) == (int)&acpi_ec_devclass)
  245 
  246 /*
  247  * Driver softc.
  248  */
  249 struct acpi_ec_softc {
  250     device_t            ec_dev;
  251     ACPI_HANDLE         ec_handle;
  252     int                 ec_uid;
  253     ACPI_HANDLE         ec_gpehandle;
  254     UINT8               ec_gpebit;
  255     UINT8               ec_csrvalue;
  256     
  257     int                 ec_data_rid;
  258     struct resource     *ec_data_res;
  259     bus_space_tag_t     ec_data_tag;
  260     bus_space_handle_t  ec_data_handle;
  261 
  262     int                 ec_csr_rid;
  263     struct resource     *ec_csr_res;
  264     bus_space_tag_t     ec_csr_tag;
  265     bus_space_handle_t  ec_csr_handle;
  266 
  267     int                 ec_glk;
  268     int                 ec_glkhandle;
  269 };
  270 
  271 /*
  272  * XXX njl
  273  * I couldn't find it in the spec but other implementations also use a
  274  * value of 1 ms for the time to acquire global lock.
  275  */
  276 #define EC_LOCK_TIMEOUT 1000
  277 
  278 /* Default interval in microseconds for the status polling loop. */
  279 #define EC_POLL_DELAY   10
  280 
  281 /* Total time in ms spent in the poll loop waiting for a response. */
  282 #define EC_POLL_TIMEOUT 100
  283 
  284 #define EVENT_READY(event, status)                      \
  285         (((event) == EC_EVENT_OUTPUT_BUFFER_FULL &&     \
  286          ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) ||    \
  287          ((event) == EC_EVENT_INPUT_BUFFER_EMPTY &&     \
  288          ((status) & EC_FLAG_INPUT_BUFFER) == 0))
  289 
  290 static int      ec_poll_timeout = EC_POLL_TIMEOUT;
  291 TUNABLE_INT("hw.acpi.ec.poll_timeout", &ec_poll_timeout);
  292 
  293 ACPI_SERIAL_DECL(ec, "ACPI embedded controller");
  294 
  295 static __inline ACPI_STATUS
  296 EcLock(struct acpi_ec_softc *sc)
  297 {
  298     ACPI_STATUS status;
  299 
  300     /* Always acquire the exclusive lock. */
  301     status = AE_OK;
  302     ACPI_SERIAL_BEGIN(ec);
  303 
  304     /* If _GLK is non-zero, also acquire the global lock. */
  305     if (sc->ec_glk) {
  306         status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
  307         if (ACPI_FAILURE(status))
  308             ACPI_SERIAL_END(ec);
  309     }
  310 
  311     return (status);
  312 }
  313 
  314 static __inline void
  315 EcUnlock(struct acpi_ec_softc *sc)
  316 {
  317     if (sc->ec_glk)
  318         AcpiReleaseGlobalLock(sc->ec_glkhandle);
  319     ACPI_SERIAL_END(ec);
  320 }
  321 
  322 static uint32_t         EcGpeHandler(void *Context);
  323 static ACPI_STATUS      EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, 
  324                                 void *Context, void **return_Context);
  325 static ACPI_STATUS      EcSpaceHandler(UINT32 Function,
  326                                 ACPI_PHYSICAL_ADDRESS Address,
  327                                 UINT32 width, ACPI_INTEGER *Value,
  328                                 void *Context, void *RegionContext);
  329 static ACPI_STATUS      EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
  330 static ACPI_STATUS      EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
  331 static ACPI_STATUS      EcRead(struct acpi_ec_softc *sc, UINT8 Address,
  332                                 UINT8 *Data);
  333 static ACPI_STATUS      EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
  334                                 UINT8 *Data);
  335 static int              acpi_ec_probe(device_t dev);
  336 static int              acpi_ec_attach(device_t dev);
  337 static int              acpi_ec_shutdown(device_t dev);
  338 
  339 static device_method_t acpi_ec_methods[] = {
  340     /* Device interface */
  341     DEVMETHOD(device_probe,     acpi_ec_probe),
  342     DEVMETHOD(device_attach,    acpi_ec_attach),
  343     DEVMETHOD(device_shutdown,  acpi_ec_shutdown),
  344 
  345     {0, 0}
  346 };
  347 
  348 static driver_t acpi_ec_driver = {
  349     "acpi_ec",
  350     acpi_ec_methods,
  351     sizeof(struct acpi_ec_softc),
  352 };
  353 
  354 static devclass_t acpi_ec_devclass;
  355 DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
  356 MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
  357 
  358 /*
  359  * Look for an ECDT and if we find one, set up default GPE and 
  360  * space handlers to catch attempts to access EC space before
  361  * we have a real driver instance in place.
  362  * TODO: if people report invalid ECDTs, add a tunable to disable them.
  363  */
  364 void
  365 acpi_ec_ecdt_probe(device_t parent)
  366 {
  367     ACPI_TABLE_ECDT *ecdt;
  368     ACPI_TABLE_HEADER *hdr;
  369     ACPI_STATUS      status;
  370     device_t         child;
  371     ACPI_HANDLE      h;
  372     struct acpi_ec_params *params;
  373 
  374     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  375 
  376     /* Find and validate the ECDT. */
  377     status = AcpiGetFirmwareTable("ECDT", 1, ACPI_LOGICAL_ADDRESSING, &hdr);
  378     ecdt = (ACPI_TABLE_ECDT *)hdr;
  379     if (ACPI_FAILURE(status) ||
  380         ecdt->control.RegisterBitWidth != 8 ||
  381         ecdt->data.RegisterBitWidth != 8) {
  382         return;
  383     }
  384 
  385     /* Create the child device with the given unit number. */
  386     child = BUS_ADD_CHILD(parent, 0, "acpi_ec", ecdt->uid);
  387     if (child == NULL) {
  388         printf("%s: can't add child\n", __func__);
  389         return;
  390     }
  391 
  392     /* Find and save the ACPI handle for this device. */
  393     status = AcpiGetHandle(NULL, ecdt->ec_id, &h);
  394     if (ACPI_FAILURE(status)) {
  395         device_delete_child(parent, child);
  396         printf("%s: can't get handle\n", __func__);
  397         return;
  398     }
  399     acpi_set_handle(child, h);
  400 
  401     /* Set the data and CSR register addresses. */
  402     bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->data.Address,
  403         /*count*/1);
  404     bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->control.Address,
  405         /*count*/1);
  406 
  407     /*
  408      * Store values for the probe/attach routines to use.  Store the
  409      * ECDT GPE bit and set the global lock flag according to _GLK.
  410      * Note that it is not perfectly correct to be evaluating a method
  411      * before initializing devices, but in practice this function
  412      * should be safe to call at this point.
  413      */
  414     params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
  415     params->gpe_handle = NULL;
  416     params->gpe_bit = ecdt->gpe_bit;
  417     params->uid = ecdt->uid;
  418     acpi_GetInteger(h, "_GLK", &params->glk);
  419     acpi_set_private(child, params);
  420     acpi_set_magic(child, (int)&acpi_ec_devclass);
  421 
  422     /* Finish the attach process. */
  423     if (device_probe_and_attach(child) != 0)
  424         device_delete_child(parent, child);
  425 }
  426 
  427 static int
  428 acpi_ec_probe(device_t dev)
  429 {
  430     ACPI_BUFFER buf;
  431     ACPI_HANDLE h;
  432     ACPI_OBJECT *obj;
  433     ACPI_STATUS status;
  434     device_t    peer;
  435     char        desc[64];
  436     int         ret;
  437     struct acpi_ec_params *params;
  438     static char *ec_ids[] = { "PNP0C09", NULL };
  439 
  440     /* Check that this is a device and that EC is not disabled. */
  441     if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
  442         return (ENXIO);
  443 
  444     /*
  445      * If probed via ECDT, set description and continue.  Otherwise,
  446      * we can access the namespace and make sure this is not a
  447      * duplicate probe.
  448      */
  449     ret = ENXIO;
  450     params = NULL;
  451     buf.Pointer = NULL;
  452     buf.Length = ACPI_ALLOCATE_BUFFER;
  453     if (DEV_ECDT(dev)) {
  454         params = acpi_get_private(dev);
  455         ret = 0;
  456     } else if (!acpi_disabled("ec") &&
  457         ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids)) {
  458         params = malloc(sizeof(struct acpi_ec_params), M_TEMP,
  459                         M_WAITOK | M_ZERO);
  460         h = acpi_get_handle(dev);
  461 
  462         /*
  463          * Read the unit ID to check for duplicate attach and the
  464          * global lock value to see if we should acquire it when
  465          * accessing the EC.
  466          */
  467         status = acpi_GetInteger(h, "_UID", &params->uid);
  468         if (ACPI_FAILURE(status))
  469             params->uid = 0;
  470         status = acpi_GetInteger(h, "_GLK", &params->glk);
  471         if (ACPI_FAILURE(status))
  472             params->glk = 0;
  473 
  474         /*
  475          * Evaluate the _GPE method to find the GPE bit used by the EC to
  476          * signal status (SCI).  If it's a package, it contains a reference
  477          * and GPE bit, similar to _PRW.
  478          */
  479         status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
  480         if (ACPI_FAILURE(status)) {
  481             device_printf(dev, "can't evaluate _GPE - %s\n",
  482                           AcpiFormatException(status));
  483             return (ENXIO);
  484         }
  485         obj = (ACPI_OBJECT *)buf.Pointer;
  486         if (obj == NULL)
  487             return (ENXIO);
  488 
  489         switch (obj->Type) {
  490         case ACPI_TYPE_INTEGER:
  491             params->gpe_handle = NULL;
  492             params->gpe_bit = obj->Integer.Value;
  493             break;
  494         case ACPI_TYPE_PACKAGE:
  495             if (!ACPI_PKG_VALID(obj, 2))
  496                 goto out;
  497             params->gpe_handle =
  498                 acpi_GetReference(NULL, &obj->Package.Elements[0]);
  499             if (params->gpe_handle == NULL ||
  500                 acpi_PkgInt32(obj, 1, &params->gpe_bit) != 0)
  501                 goto out;
  502             break;
  503         default:
  504             device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
  505             goto out;
  506         }
  507 
  508         /* Store the values we got from the namespace for attach. */
  509         acpi_set_private(dev, params);
  510 
  511         /*
  512          * Check for a duplicate probe.  This can happen when a probe
  513          * via ECDT succeeded already.  If this is a duplicate, disable
  514          * this device.
  515          */
  516         peer = devclass_get_device(acpi_ec_devclass, params->uid);
  517         if (peer == NULL || !device_is_alive(peer))
  518             ret = 0;
  519         else
  520             device_disable(dev);
  521     }
  522 
  523 out:
  524     if (ret == 0) {
  525         snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
  526                  params->gpe_bit, (params->glk) ? ", GLK" : "",
  527                  DEV_ECDT(dev) ? ", ECDT" : "");
  528         device_set_desc_copy(dev, desc);
  529     }
  530 
  531     if (ret > 0 && params)
  532         free(params, M_TEMP);
  533     if (buf.Pointer)
  534         AcpiOsFree(buf.Pointer);
  535     return (ret);
  536 }
  537 
  538 static int
  539 acpi_ec_attach(device_t dev)
  540 {
  541     struct acpi_ec_softc        *sc;
  542     struct acpi_ec_params       *params;
  543     ACPI_STATUS                 Status;
  544 
  545     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  546 
  547     /* Fetch/initialize softc (assumes softc is pre-zeroed). */
  548     sc = device_get_softc(dev);
  549     params = acpi_get_private(dev);
  550     sc->ec_dev = dev;
  551     sc->ec_handle = acpi_get_handle(dev);
  552 
  553     /* Retrieve previously probed values via device ivars. */
  554     sc->ec_glk = params->glk;
  555     sc->ec_gpebit = params->gpe_bit;
  556     sc->ec_gpehandle = params->gpe_handle;
  557     sc->ec_uid = params->uid;
  558     free(params, M_TEMP);
  559 
  560     /* Attach bus resources for data and command/status ports. */
  561     sc->ec_data_rid = 0;
  562     sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
  563                         &sc->ec_data_rid, RF_ACTIVE);
  564     if (sc->ec_data_res == NULL) {
  565         device_printf(dev, "can't allocate data port\n");
  566         goto error;
  567     }
  568     sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
  569     sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
  570 
  571     sc->ec_csr_rid = 1;
  572     sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
  573                         &sc->ec_csr_rid, RF_ACTIVE);
  574     if (sc->ec_csr_res == NULL) {
  575         device_printf(dev, "can't allocate command/status port\n");
  576         goto error;
  577     }
  578     sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
  579     sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
  580 
  581     /*
  582      * Install a handler for this EC's GPE bit.  We want edge-triggered
  583      * behavior.
  584      */
  585     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
  586     Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
  587                 ACPI_GPE_EDGE_TRIGGERED, &EcGpeHandler, sc);
  588     if (ACPI_FAILURE(Status)) {
  589         device_printf(dev, "can't install GPE handler for %s - %s\n",
  590                       acpi_name(sc->ec_handle), AcpiFormatException(Status));
  591         goto error;
  592     }
  593 
  594     /* 
  595      * Install address space handler
  596      */
  597     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
  598     Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
  599                 &EcSpaceHandler, &EcSpaceSetup, sc);
  600     if (ACPI_FAILURE(Status)) {
  601         device_printf(dev, "can't install address space handler for %s - %s\n",
  602                       acpi_name(sc->ec_handle), AcpiFormatException(Status));
  603         goto error;
  604     }
  605 
  606     /* Enable runtime GPEs for the handler. */
  607     Status = AcpiSetGpeType(sc->ec_gpehandle, sc->ec_gpebit,
  608                             ACPI_GPE_TYPE_RUNTIME);
  609     if (ACPI_FAILURE(Status)) {
  610         device_printf(dev, "AcpiSetGpeType failed: %s\n",
  611                       AcpiFormatException(Status));
  612         goto error;
  613     }
  614     Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
  615     if (ACPI_FAILURE(Status)) {
  616         device_printf(dev, "AcpiEnableGpe failed: %s\n",
  617                       AcpiFormatException(Status));
  618         goto error;
  619     }
  620 
  621     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
  622     return (0);
  623 
  624 error:
  625     AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, &EcGpeHandler);
  626     AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
  627         EcSpaceHandler);
  628     if (sc->ec_csr_res)
  629         bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid, 
  630                              sc->ec_csr_res);
  631     if (sc->ec_data_res)
  632         bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
  633                              sc->ec_data_res);
  634     return (ENXIO);
  635 }
  636 
  637 static int
  638 acpi_ec_shutdown(device_t dev)
  639 {
  640     struct acpi_ec_softc        *sc;
  641 
  642     /* Disable the GPE so we don't get EC events during shutdown. */
  643     sc = device_get_softc(dev);
  644     AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
  645     return (0);
  646 }
  647 
  648 static void
  649 EcGpeQueryHandler(void *Context)
  650 {
  651     struct acpi_ec_softc        *sc = (struct acpi_ec_softc *)Context;
  652     UINT8                       Data;
  653     ACPI_STATUS                 Status;
  654     EC_STATUS                   EcStatus;
  655     char                        qxx[5];
  656 
  657     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  658     KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
  659 
  660     Status = EcLock(sc);
  661     if (ACPI_FAILURE(Status)) {
  662         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
  663                     "GpeQuery lock error: %s\n", AcpiFormatException(Status));
  664         return;
  665     }
  666 
  667     /*
  668      * If the EC_SCI bit of the status register is not set, then pass
  669      * it along to any potential waiters as it may be an IBE/OBF event.
  670      */
  671     EcStatus = EC_GET_CSR(sc);
  672     if ((EcStatus & EC_EVENT_SCI) == 0) {
  673         sc->ec_csrvalue = EcStatus;
  674         wakeup(&sc->ec_csrvalue);
  675         EcUnlock(sc);
  676         goto re_enable;
  677     }
  678 
  679     /*
  680      * Send a query command to the EC to find out which _Qxx call it
  681      * wants to make.  This command clears the SCI bit and also the
  682      * interrupt source since we are edge-triggered.
  683      */
  684     Status = EcCommand(sc, EC_COMMAND_QUERY);
  685     if (ACPI_FAILURE(Status)) {
  686         EcUnlock(sc);
  687         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
  688                     "GPE query failed - %s\n", AcpiFormatException(Status));
  689         goto re_enable;
  690     }
  691     Data = EC_GET_DATA(sc);
  692     EcUnlock(sc);
  693 
  694     /* Ignore the value for "no outstanding event". (13.3.5) */
  695     if (Data == 0)
  696         goto re_enable;
  697 
  698     /* Evaluate _Qxx to respond to the controller. */
  699     sprintf(qxx, "_Q%02x", Data);
  700     strupr(qxx);
  701     Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
  702     if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
  703         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
  704                     "evaluation of GPE query method %s failed - %s\n", 
  705                     qxx, AcpiFormatException(Status));
  706     }
  707 
  708 re_enable:
  709     /* Re-enable the GPE event so we'll get future requests. */
  710     Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
  711     if (ACPI_FAILURE(Status))
  712         printf("EcGpeQueryHandler: AcpiEnableEvent failed\n");
  713 }
  714 
  715 /*
  716  * Handle a GPE.  Currently we only handle SCI events as others must
  717  * be handled by polling in EcWaitEvent().  This is because some ECs
  718  * treat events as level when they should be edge-triggered.
  719  */
  720 static uint32_t
  721 EcGpeHandler(void *Context)
  722 {
  723     struct acpi_ec_softc *sc = Context;
  724     ACPI_STATUS                Status;
  725 
  726     KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
  727 
  728     /* Disable further GPEs while we handle this one. */
  729     AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
  730 
  731     /* Schedule the GPE query handler. */
  732     Status = AcpiOsQueueForExecution(OSD_PRIORITY_GPE, EcGpeQueryHandler,
  733                 Context);
  734     if (ACPI_FAILURE(Status)) {
  735         printf("Queuing GPE query handler failed.\n");
  736         Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
  737         if (ACPI_FAILURE(Status))
  738             printf("EcGpeHandler: AcpiEnableEvent failed\n");
  739     }
  740 
  741     return (0);
  742 }
  743 
  744 static ACPI_STATUS
  745 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
  746              void **RegionContext)
  747 {
  748 
  749     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  750 
  751     /*
  752      * If deactivating a region, always set the output to NULL.  Otherwise,
  753      * just pass the context through.
  754      */
  755     if (Function == ACPI_REGION_DEACTIVATE)
  756         *RegionContext = NULL;
  757     else
  758         *RegionContext = Context;
  759 
  760     return_ACPI_STATUS (AE_OK);
  761 }
  762 
  763 static ACPI_STATUS
  764 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
  765                ACPI_INTEGER *Value, void *Context, void *RegionContext)
  766 {
  767     struct acpi_ec_softc        *sc = (struct acpi_ec_softc *)Context;
  768     ACPI_STATUS                 Status;
  769     UINT8                       EcAddr, EcData;
  770     int                         i;
  771 
  772     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
  773 
  774     if (width % 8 != 0 || Value == NULL || Context == NULL)
  775         return_ACPI_STATUS (AE_BAD_PARAMETER);
  776     if (Address + (width / 8) - 1 > 0xFF)
  777         return_ACPI_STATUS (AE_BAD_ADDRESS);
  778 
  779     if (Function == ACPI_READ)
  780         *Value = 0;
  781     EcAddr = Address;
  782     Status = AE_ERROR;
  783 
  784     Status = EcLock(sc);
  785     if (ACPI_FAILURE(Status))
  786         return_ACPI_STATUS (Status);
  787 
  788     /* Perform the transaction(s), based on width. */
  789     for (i = 0; i < width; i += 8, EcAddr++) {
  790         switch (Function) {
  791         case ACPI_READ:
  792             Status = EcRead(sc, EcAddr, &EcData);
  793             if (ACPI_SUCCESS(Status))
  794                 *Value |= ((ACPI_INTEGER)EcData) << i;
  795             break;
  796         case ACPI_WRITE:
  797             EcData = (UINT8)((*Value) >> i);
  798             Status = EcWrite(sc, EcAddr, &EcData);
  799             break;
  800         default:
  801             device_printf(sc->ec_dev, "invalid EcSpaceHandler function %d\n",
  802                           Function);
  803             Status = AE_BAD_PARAMETER;
  804             break;
  805         }
  806         if (ACPI_FAILURE(Status))
  807             break;
  808     }
  809 
  810     EcUnlock(sc);
  811     return_ACPI_STATUS (Status);
  812 }
  813 
  814 static ACPI_STATUS
  815 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
  816 {
  817     EC_STATUS   EcStatus;
  818     ACPI_STATUS Status;
  819     int         count, i, period, retval, slp_ival;
  820     static int  EcDbgMaxDelay;
  821 
  822     ACPI_SERIAL_ASSERT(ec);
  823     Status = AE_NO_HARDWARE_RESPONSE;
  824 
  825     /* 
  826      * Wait for 1 us before checking the CSR.  Testing shows about
  827      * 50% of requests complete in 1 us and 90% of them complete
  828      * in 5 us or less.
  829      */
  830     AcpiOsStall(1);
  831 
  832     /*
  833      * Poll the EC status register for up to 1 ms in chunks of 10 us 
  834      * to detect completion of the last command.
  835      */
  836     for (i = 0; i < 1000 / EC_POLL_DELAY; i++) {
  837         EcStatus = EC_GET_CSR(sc);
  838         if (EVENT_READY(Event, EcStatus)) {
  839             Status = AE_OK;
  840             break;
  841         }
  842         AcpiOsStall(EC_POLL_DELAY);
  843     }
  844     period = i * EC_POLL_DELAY;
  845 
  846     /*
  847      * If we still don't have a response and we're up and running, wait up
  848      * to ec_poll_timeout ms for completion, sleeping for chunks of 10 ms.
  849      */
  850     slp_ival = 0;
  851     if (Status != AE_OK) {
  852         retval = ENXIO;
  853         count = ec_poll_timeout / 10;
  854         if (count == 0)
  855             count = 1;
  856         slp_ival = hz / 100;
  857         if (slp_ival == 0)
  858             slp_ival = 1;
  859         for (i = 0; i < count; i++) {
  860             if (retval != 0)
  861                 EcStatus = EC_GET_CSR(sc);
  862             else
  863                 EcStatus = sc->ec_csrvalue;
  864             if (EVENT_READY(Event, EcStatus)) {
  865                 Status = AE_OK;
  866                 break;
  867             }
  868             if (!cold)
  869                 retval = tsleep(&sc->ec_csrvalue, PZERO, "ecpoll", slp_ival);
  870             else
  871                 AcpiOsStall(10000);
  872         }
  873     }
  874 
  875     /* Calculate new delay and print it if it exceeds the max. */
  876     if (slp_ival > 0)
  877         period += i * 10000;
  878     if (period > EcDbgMaxDelay) {
  879         EcDbgMaxDelay = period;
  880         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
  881                     "info: new max delay is %d us\n", period);
  882     }
  883 
  884     return (Status);
  885 }    
  886 
  887 static ACPI_STATUS
  888 EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
  889 {
  890     ACPI_STATUS Status;
  891     EC_EVENT    Event;
  892 
  893     ACPI_SERIAL_ASSERT(ec);
  894 
  895     /* Decide what to wait for based on command type. */
  896     switch (cmd) {
  897     case EC_COMMAND_READ:
  898     case EC_COMMAND_WRITE:
  899     case EC_COMMAND_BURST_DISABLE:
  900         Event = EC_EVENT_INPUT_BUFFER_EMPTY;
  901         break;
  902     case EC_COMMAND_QUERY:
  903     case EC_COMMAND_BURST_ENABLE:
  904         Event = EC_EVENT_OUTPUT_BUFFER_FULL;
  905         break;
  906     default:
  907         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
  908                     "EcCommand: Invalid command %#x\n", cmd);
  909         return (AE_BAD_PARAMETER);
  910     }
  911 
  912     /* Run the command and wait for the chosen event. */
  913     EC_SET_CSR(sc, cmd);
  914     Status = EcWaitEvent(sc, Event);
  915     if (ACPI_FAILURE(Status)) {
  916         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
  917                     "EcCommand: no response to %#x\n", cmd);
  918     }
  919 
  920     return (Status);
  921 }
  922 
  923 static ACPI_STATUS
  924 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
  925 {
  926     ACPI_STATUS Status;
  927 
  928     ACPI_SERIAL_ASSERT(ec);
  929 
  930 #ifdef notyet
  931     /* If we can't start burst mode, continue anyway. */
  932     EcCommand(sc, EC_COMMAND_BURST_ENABLE);
  933 #endif
  934 
  935     Status = EcCommand(sc, EC_COMMAND_READ);
  936     if (ACPI_FAILURE(Status))
  937         return (Status);
  938 
  939     EC_SET_DATA(sc, Address);
  940     Status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
  941     if (ACPI_FAILURE(Status)) {
  942         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
  943                     "EcRead: Failed waiting for EC to send data.\n");
  944         return (Status);
  945     }
  946 
  947     *Data = EC_GET_DATA(sc);
  948 
  949 #ifdef notyet
  950     if (sc->ec_burstactive) {
  951         Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
  952         if (ACPI_FAILURE(Status))
  953             return (Status);
  954     }
  955 #endif
  956 
  957     return (AE_OK);
  958 }    
  959 
  960 static ACPI_STATUS
  961 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
  962 {
  963     ACPI_STATUS Status;
  964 
  965     ACPI_SERIAL_ASSERT(ec);
  966 
  967 #ifdef notyet
  968     /* If we can't start burst mode, continue anyway. */
  969     EcCommand(sc, EC_COMMAND_BURST_ENABLE);
  970 #endif
  971 
  972     Status = EcCommand(sc, EC_COMMAND_WRITE);
  973     if (ACPI_FAILURE(Status))
  974         return (Status);
  975 
  976     EC_SET_DATA(sc, Address);
  977     Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
  978     if (ACPI_FAILURE(Status)) {
  979         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
  980                     "EcRead: Failed waiting for EC to process address\n");
  981         return (Status);
  982     }
  983 
  984     EC_SET_DATA(sc, *Data);
  985     Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
  986     if (ACPI_FAILURE(Status)) {
  987         ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
  988                     "EcWrite: Failed waiting for EC to process data\n");
  989         return (Status);
  990     }
  991 
  992 #ifdef notyet
  993     if (sc->ec_burstactive) {
  994         Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
  995         if (ACPI_FAILURE(Status))
  996             return (Status);
  997     }
  998 #endif
  999 
 1000     return (AE_OK);
 1001 }

Cache object: 99c27ba8a84d9a6852f1999e6f6f6ebb


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.