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

Cache object: 42144ab0d8e1e77efc1a7d7c13cb9251


[ 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.