115e32d5dSMike Smith /*- 21f04e8f5SNate Lawson * Copyright (c) 2003 Nate Lawson 315e32d5dSMike Smith * Copyright (c) 2000 Michael Smith 415e32d5dSMike Smith * Copyright (c) 2000 BSDi 515e32d5dSMike Smith * All rights reserved. 615e32d5dSMike Smith * 715e32d5dSMike Smith * Redistribution and use in source and binary forms, with or without 815e32d5dSMike Smith * modification, are permitted provided that the following conditions 915e32d5dSMike Smith * are met: 1015e32d5dSMike Smith * 1. Redistributions of source code must retain the above copyright 1115e32d5dSMike Smith * notice, this list of conditions and the following disclaimer. 1215e32d5dSMike Smith * 2. Redistributions in binary form must reproduce the above copyright 1315e32d5dSMike Smith * notice, this list of conditions and the following disclaimer in the 1415e32d5dSMike Smith * documentation and/or other materials provided with the distribution. 1515e32d5dSMike Smith * 1615e32d5dSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1715e32d5dSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1815e32d5dSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1915e32d5dSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2015e32d5dSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2115e32d5dSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2215e32d5dSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2315e32d5dSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2415e32d5dSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2515e32d5dSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2615e32d5dSMike Smith * SUCH DAMAGE. 2715e32d5dSMike Smith */ 28098ca2bdSWarner Losh /*- 29098ca2bdSWarner Losh ****************************************************************************** 3015e32d5dSMike Smith * 3115e32d5dSMike Smith * 1. Copyright Notice 3215e32d5dSMike Smith * 3315e32d5dSMike Smith * Some or all of this work - Copyright (c) 1999, Intel Corp. All rights 3415e32d5dSMike Smith * reserved. 3515e32d5dSMike Smith * 3615e32d5dSMike Smith * 2. License 3715e32d5dSMike Smith * 3815e32d5dSMike Smith * 2.1. This is your license from Intel Corp. under its intellectual property 3915e32d5dSMike Smith * rights. You may have additional license terms from the party that provided 4015e32d5dSMike Smith * you this software, covering your right to use that party's intellectual 4115e32d5dSMike Smith * property rights. 4215e32d5dSMike Smith * 4315e32d5dSMike Smith * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a 4415e32d5dSMike Smith * copy of the source code appearing in this file ("Covered Code") an 4515e32d5dSMike Smith * irrevocable, perpetual, worldwide license under Intel's copyrights in the 4615e32d5dSMike Smith * base code distributed originally by Intel ("Original Intel Code") to copy, 4715e32d5dSMike Smith * make derivatives, distribute, use and display any portion of the Covered 4815e32d5dSMike Smith * Code in any form, with the right to sublicense such rights; and 4915e32d5dSMike Smith * 5015e32d5dSMike Smith * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent 5115e32d5dSMike Smith * license (with the right to sublicense), under only those claims of Intel 5215e32d5dSMike Smith * patents that are infringed by the Original Intel Code, to make, use, sell, 5315e32d5dSMike Smith * offer to sell, and import the Covered Code and derivative works thereof 5415e32d5dSMike Smith * solely to the minimum extent necessary to exercise the above copyright 5515e32d5dSMike Smith * license, and in no event shall the patent license extend to any additions 5615e32d5dSMike Smith * to or modifications of the Original Intel Code. No other license or right 5715e32d5dSMike Smith * is granted directly or by implication, estoppel or otherwise; 5815e32d5dSMike Smith * 5915e32d5dSMike Smith * The above copyright and patent license is granted only if the following 6015e32d5dSMike Smith * conditions are met: 6115e32d5dSMike Smith * 6215e32d5dSMike Smith * 3. Conditions 6315e32d5dSMike Smith * 6415e32d5dSMike Smith * 3.1. Redistribution of Source with Rights to Further Distribute Source. 6515e32d5dSMike Smith * Redistribution of source code of any substantial portion of the Covered 6615e32d5dSMike Smith * Code or modification with rights to further distribute source must include 6715e32d5dSMike Smith * the above Copyright Notice, the above License, this list of Conditions, 6815e32d5dSMike Smith * and the following Disclaimer and Export Compliance provision. In addition, 6915e32d5dSMike Smith * Licensee must cause all Covered Code to which Licensee contributes to 7015e32d5dSMike Smith * contain a file documenting the changes Licensee made to create that Covered 7115e32d5dSMike Smith * Code and the date of any change. Licensee must include in that file the 7215e32d5dSMike Smith * documentation of any changes made by any predecessor Licensee. Licensee 7315e32d5dSMike Smith * must include a prominent statement that the modification is derived, 7415e32d5dSMike Smith * directly or indirectly, from Original Intel Code. 7515e32d5dSMike Smith * 7615e32d5dSMike Smith * 3.2. Redistribution of Source with no Rights to Further Distribute Source. 7715e32d5dSMike Smith * Redistribution of source code of any substantial portion of the Covered 7815e32d5dSMike Smith * Code or modification without rights to further distribute source must 7915e32d5dSMike Smith * include the following Disclaimer and Export Compliance provision in the 8015e32d5dSMike Smith * documentation and/or other materials provided with distribution. In 8115e32d5dSMike Smith * addition, Licensee may not authorize further sublicense of source of any 8215e32d5dSMike Smith * portion of the Covered Code, and must include terms to the effect that the 8315e32d5dSMike Smith * license from Licensee to its licensee is limited to the intellectual 8415e32d5dSMike Smith * property embodied in the software Licensee provides to its licensee, and 8515e32d5dSMike Smith * not to intellectual property embodied in modifications its licensee may 8615e32d5dSMike Smith * make. 8715e32d5dSMike Smith * 8815e32d5dSMike Smith * 3.3. Redistribution of Executable. Redistribution in executable form of any 8915e32d5dSMike Smith * substantial portion of the Covered Code or modification must reproduce the 9015e32d5dSMike Smith * above Copyright Notice, and the following Disclaimer and Export Compliance 9115e32d5dSMike Smith * provision in the documentation and/or other materials provided with the 9215e32d5dSMike Smith * distribution. 9315e32d5dSMike Smith * 9415e32d5dSMike Smith * 3.4. Intel retains all right, title, and interest in and to the Original 9515e32d5dSMike Smith * Intel Code. 9615e32d5dSMike Smith * 9715e32d5dSMike Smith * 3.5. Neither the name Intel nor any other trademark owned or controlled by 9815e32d5dSMike Smith * Intel shall be used in advertising or otherwise to promote the sale, use or 9915e32d5dSMike Smith * other dealings in products derived from or relating to the Covered Code 10015e32d5dSMike Smith * without prior written authorization from Intel. 10115e32d5dSMike Smith * 10215e32d5dSMike Smith * 4. Disclaimer and Export Compliance 10315e32d5dSMike Smith * 10415e32d5dSMike Smith * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED 10515e32d5dSMike Smith * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE 10615e32d5dSMike Smith * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE, 10715e32d5dSMike Smith * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY 10815e32d5dSMike Smith * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY 10915e32d5dSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A 11015e32d5dSMike Smith * PARTICULAR PURPOSE. 11115e32d5dSMike Smith * 11215e32d5dSMike Smith * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES 11315e32d5dSMike Smith * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR 11415e32d5dSMike Smith * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT, 11515e32d5dSMike Smith * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY 11615e32d5dSMike Smith * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL 11715e32d5dSMike Smith * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS 11815e32d5dSMike Smith * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY 11915e32d5dSMike Smith * LIMITED REMEDY. 12015e32d5dSMike Smith * 12115e32d5dSMike Smith * 4.3. Licensee shall not export, either directly or indirectly, any of this 12215e32d5dSMike Smith * software or system incorporating such software without first obtaining any 12315e32d5dSMike Smith * required license or other approval from the U. S. Department of Commerce or 12415e32d5dSMike Smith * any other agency or department of the United States Government. In the 12515e32d5dSMike Smith * event Licensee exports any such software from the United States or 12615e32d5dSMike Smith * re-exports any such software from a foreign destination, Licensee shall 12715e32d5dSMike Smith * ensure that the distribution and export/re-export of the software is in 12815e32d5dSMike Smith * compliance with all laws, regulations, orders, or other restrictions of the 12915e32d5dSMike Smith * U.S. Export Administration Regulations. Licensee agrees that neither it nor 13015e32d5dSMike Smith * any of its subsidiaries will export/re-export any technical data, process, 13115e32d5dSMike Smith * software, or service, directly or indirectly, to any country for which the 13215e32d5dSMike Smith * United States government or any agency thereof requires an export license, 13315e32d5dSMike Smith * other governmental approval, or letter of assurance, without first obtaining 13415e32d5dSMike Smith * such license, approval or letter. 13515e32d5dSMike Smith * 13615e32d5dSMike Smith *****************************************************************************/ 13715e32d5dSMike Smith 138aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 139aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 140aad970f1SDavid E. O'Brien 14115e32d5dSMike Smith #include "opt_acpi.h" 14215e32d5dSMike Smith #include <sys/param.h> 14315e32d5dSMike Smith #include <sys/kernel.h> 14415e32d5dSMike Smith #include <sys/bus.h> 1450025fb0fSNate Lawson #include <sys/malloc.h> 146b0eefa38SNate Lawson #include <sys/module.h> 147b0eefa38SNate Lawson #include <sys/sx.h> 14815e32d5dSMike Smith 14915e32d5dSMike Smith #include <machine/bus.h> 15015e32d5dSMike Smith #include <machine/resource.h> 15115e32d5dSMike Smith #include <sys/rman.h> 15215e32d5dSMike Smith 1532a191126SDavid E. O'Brien #include <contrib/dev/acpica/acpi.h> 15415e32d5dSMike Smith #include <dev/acpica/acpivar.h> 15515e32d5dSMike Smith 156f4b7de15SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 157a9cf0dffSMike Smith #define _COMPONENT ACPI_EC 158dbd0058aSMike Smith ACPI_MODULE_NAME("EC") 1590ae55423SMike Smith 160da3b867eSMike Smith /* 161da3b867eSMike Smith * EC_COMMAND: 162da3b867eSMike Smith * ----------- 163da3b867eSMike Smith */ 164da3b867eSMike Smith typedef UINT8 EC_COMMAND; 165da3b867eSMike Smith 166da3b867eSMike Smith #define EC_COMMAND_UNKNOWN ((EC_COMMAND) 0x00) 167da3b867eSMike Smith #define EC_COMMAND_READ ((EC_COMMAND) 0x80) 168da3b867eSMike Smith #define EC_COMMAND_WRITE ((EC_COMMAND) 0x81) 169da3b867eSMike Smith #define EC_COMMAND_BURST_ENABLE ((EC_COMMAND) 0x82) 170da3b867eSMike Smith #define EC_COMMAND_BURST_DISABLE ((EC_COMMAND) 0x83) 171da3b867eSMike Smith #define EC_COMMAND_QUERY ((EC_COMMAND) 0x84) 172da3b867eSMike Smith 173da3b867eSMike Smith /* 174da3b867eSMike Smith * EC_STATUS: 175da3b867eSMike Smith * ---------- 176da3b867eSMike Smith * The encoding of the EC status register is illustrated below. 177da3b867eSMike Smith * Note that a set bit (1) indicates the property is TRUE 178da3b867eSMike Smith * (e.g. if bit 0 is set then the output buffer is full). 179da3b867eSMike Smith * +-+-+-+-+-+-+-+-+ 180da3b867eSMike Smith * |7|6|5|4|3|2|1|0| 181da3b867eSMike Smith * +-+-+-+-+-+-+-+-+ 182da3b867eSMike Smith * | | | | | | | | 183da3b867eSMike Smith * | | | | | | | +- Output Buffer Full? 184da3b867eSMike Smith * | | | | | | +--- Input Buffer Full? 185da3b867eSMike Smith * | | | | | +----- <reserved> 186da3b867eSMike Smith * | | | | +------- Data Register is Command Byte? 187da3b867eSMike Smith * | | | +--------- Burst Mode Enabled? 188da3b867eSMike Smith * | | +----------- SCI Event? 189da3b867eSMike Smith * | +------------- SMI Event? 190da3b867eSMike Smith * +--------------- <Reserved> 191da3b867eSMike Smith * 192da3b867eSMike Smith */ 193da3b867eSMike Smith typedef UINT8 EC_STATUS; 194da3b867eSMike Smith 195da3b867eSMike Smith #define EC_FLAG_OUTPUT_BUFFER ((EC_STATUS) 0x01) 196da3b867eSMike Smith #define EC_FLAG_INPUT_BUFFER ((EC_STATUS) 0x02) 197da3b867eSMike Smith #define EC_FLAG_BURST_MODE ((EC_STATUS) 0x10) 198da3b867eSMike Smith #define EC_FLAG_SCI ((EC_STATUS) 0x20) 199da3b867eSMike Smith 200da3b867eSMike Smith /* 201da3b867eSMike Smith * EC_EVENT: 202da3b867eSMike Smith * --------- 203da3b867eSMike Smith */ 204da3b867eSMike Smith typedef UINT8 EC_EVENT; 205da3b867eSMike Smith 206da3b867eSMike Smith #define EC_EVENT_UNKNOWN ((EC_EVENT) 0x00) 207da3b867eSMike Smith #define EC_EVENT_OUTPUT_BUFFER_FULL ((EC_EVENT) 0x01) 208da3b867eSMike Smith #define EC_EVENT_INPUT_BUFFER_EMPTY ((EC_EVENT) 0x02) 209da3b867eSMike Smith #define EC_EVENT_SCI ((EC_EVENT) 0x20) 210da3b867eSMike Smith 211da3b867eSMike Smith /* 212da3b867eSMike Smith * Register access primitives 213da3b867eSMike Smith */ 214da3b867eSMike Smith #define EC_GET_DATA(sc) \ 215da3b867eSMike Smith bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0) 216da3b867eSMike Smith 217da3b867eSMike Smith #define EC_SET_DATA(sc, v) \ 218da3b867eSMike Smith bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v)) 219da3b867eSMike Smith 220da3b867eSMike Smith #define EC_GET_CSR(sc) \ 221da3b867eSMike Smith bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0) 222da3b867eSMike Smith 223da3b867eSMike Smith #define EC_SET_CSR(sc, v) \ 224da3b867eSMike Smith bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v)) 225da3b867eSMike Smith 226f8335e3aSNate Lawson /* Embedded Controller Boot Resources Table (ECDT) */ 227f8335e3aSNate Lawson typedef struct { 228f8335e3aSNate Lawson ACPI_TABLE_HEADER header; 229f8335e3aSNate Lawson ACPI_GENERIC_ADDRESS control; 230f8335e3aSNate Lawson ACPI_GENERIC_ADDRESS data; 231f8335e3aSNate Lawson UINT32 uid; 232f8335e3aSNate Lawson UINT8 gpe_bit; 233f8335e3aSNate Lawson char ec_id[0]; 234f8335e3aSNate Lawson } ACPI_TABLE_ECDT; 235f8335e3aSNate Lawson 2360025fb0fSNate Lawson /* Additional params to pass from the probe routine */ 2370025fb0fSNate Lawson struct acpi_ec_params { 2380025fb0fSNate Lawson int glk; 2390025fb0fSNate Lawson int gpe_bit; 2400025fb0fSNate Lawson ACPI_HANDLE gpe_handle; 2410025fb0fSNate Lawson int uid; 2420025fb0fSNate Lawson }; 2430025fb0fSNate Lawson 244f8335e3aSNate Lawson /* Indicate that this device has already been probed via ECDT. */ 2450025fb0fSNate Lawson #define DEV_ECDT(x) (acpi_get_magic(x) == (int)&acpi_ec_devclass) 246f8335e3aSNate Lawson 247da3b867eSMike Smith /* 248da3b867eSMike Smith * Driver softc. 249da3b867eSMike Smith */ 25015e32d5dSMike Smith struct acpi_ec_softc { 25115e32d5dSMike Smith device_t ec_dev; 25215e32d5dSMike Smith ACPI_HANDLE ec_handle; 2530025fb0fSNate Lawson int ec_uid; 2540025fb0fSNate Lawson ACPI_HANDLE ec_gpehandle; 2553a371f32SNate Lawson UINT8 ec_gpebit; 2563a371f32SNate Lawson UINT8 ec_csrvalue; 25715e32d5dSMike Smith 25815e32d5dSMike Smith int ec_data_rid; 25915e32d5dSMike Smith struct resource *ec_data_res; 26015e32d5dSMike Smith bus_space_tag_t ec_data_tag; 26115e32d5dSMike Smith bus_space_handle_t ec_data_handle; 26215e32d5dSMike Smith 26315e32d5dSMike Smith int ec_csr_rid; 26415e32d5dSMike Smith struct resource *ec_csr_res; 26515e32d5dSMike Smith bus_space_tag_t ec_csr_tag; 26615e32d5dSMike Smith bus_space_handle_t ec_csr_handle; 26715e32d5dSMike Smith 2681f04e8f5SNate Lawson int ec_glk; 2691f04e8f5SNate Lawson int ec_glkhandle; 27015e32d5dSMike Smith }; 27115e32d5dSMike Smith 2721f04e8f5SNate Lawson /* 273f4b7de15SNate Lawson * XXX njl 2741f04e8f5SNate Lawson * I couldn't find it in the spec but other implementations also use a 2751f04e8f5SNate Lawson * value of 1 ms for the time to acquire global lock. 2761f04e8f5SNate Lawson */ 2771f04e8f5SNate Lawson #define EC_LOCK_TIMEOUT 1000 2784690674eSMitsuru IWASAKI 279b0eefa38SNate Lawson /* Default interval in microseconds for the status polling loop. */ 280b0eefa38SNate Lawson #define EC_POLL_DELAY 10 2811f04e8f5SNate Lawson 2821f04e8f5SNate Lawson /* Total time in ms spent in the poll loop waiting for a response. */ 283c87cfaedSNate Lawson #define EC_POLL_TIMEOUT 100 28415e32d5dSMike Smith 285ff40920eSNate Lawson #define EVENT_READY(event, status) \ 286ff40920eSNate Lawson (((event) == EC_EVENT_OUTPUT_BUFFER_FULL && \ 287ff40920eSNate Lawson ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) || \ 288ff40920eSNate Lawson ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && \ 289ff40920eSNate Lawson ((status) & EC_FLAG_INPUT_BUFFER) == 0)) 290ff40920eSNate Lawson 291a329ebcaSNate Lawson static int ec_poll_timeout = EC_POLL_TIMEOUT; 292a329ebcaSNate Lawson TUNABLE_INT("hw.acpi.ec.poll_timeout", &ec_poll_timeout); 293a329ebcaSNate Lawson 294f4b7de15SNate Lawson ACPI_SERIAL_DECL(ec, "ACPI embedded controller"); 295f4b7de15SNate Lawson 29615e32d5dSMike Smith static __inline ACPI_STATUS 29715e32d5dSMike Smith EcLock(struct acpi_ec_softc *sc) 29815e32d5dSMike Smith { 299f4b7de15SNate Lawson ACPI_STATUS status; 30015e32d5dSMike Smith 301f4b7de15SNate Lawson /* Always acquire the exclusive lock. */ 302f4b7de15SNate Lawson status = AE_OK; 303f4b7de15SNate Lawson ACPI_SERIAL_BEGIN(ec); 3041f04e8f5SNate Lawson 3051f04e8f5SNate Lawson /* If _GLK is non-zero, also acquire the global lock. */ 3061f04e8f5SNate Lawson if (sc->ec_glk) { 3071f04e8f5SNate Lawson status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle); 3081f04e8f5SNate Lawson if (ACPI_FAILURE(status)) 309f4b7de15SNate Lawson ACPI_SERIAL_END(ec); 3101f04e8f5SNate Lawson } 311c573e654SMitsuru IWASAKI 31215e32d5dSMike Smith return (status); 31315e32d5dSMike Smith } 31415e32d5dSMike Smith 31515e32d5dSMike Smith static __inline void 31615e32d5dSMike Smith EcUnlock(struct acpi_ec_softc *sc) 31715e32d5dSMike Smith { 3181f04e8f5SNate Lawson if (sc->ec_glk) 3191f04e8f5SNate Lawson AcpiReleaseGlobalLock(sc->ec_glkhandle); 320f4b7de15SNate Lawson ACPI_SERIAL_END(ec); 32115e32d5dSMike Smith } 32215e32d5dSMike Smith 323f3fc4f8bSNate Lawson static uint32_t EcGpeHandler(void *Context); 32415e32d5dSMike Smith static ACPI_STATUS EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, 32515e32d5dSMike Smith void *Context, void **return_Context); 326464c662eSNate Lawson static ACPI_STATUS EcSpaceHandler(UINT32 Function, 327464c662eSNate Lawson ACPI_PHYSICAL_ADDRESS Address, 328464c662eSNate Lawson UINT32 width, ACPI_INTEGER *Value, 32915e32d5dSMike Smith void *Context, void *RegionContext); 33015e32d5dSMike Smith static ACPI_STATUS EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event); 3311f04e8f5SNate Lawson static ACPI_STATUS EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd); 332464c662eSNate Lawson static ACPI_STATUS EcRead(struct acpi_ec_softc *sc, UINT8 Address, 333464c662eSNate Lawson UINT8 *Data); 334464c662eSNate Lawson static ACPI_STATUS EcWrite(struct acpi_ec_softc *sc, UINT8 Address, 335464c662eSNate Lawson UINT8 *Data); 33615e32d5dSMike Smith static int acpi_ec_probe(device_t dev); 33715e32d5dSMike Smith static int acpi_ec_attach(device_t dev); 338340a7f6aSNate Lawson static int acpi_ec_shutdown(device_t dev); 339e33bea8dSNate Lawson static int acpi_ec_read_method(device_t dev, u_int addr, 340e33bea8dSNate Lawson ACPI_INTEGER *val, int width); 341e33bea8dSNate Lawson static int acpi_ec_write_method(device_t dev, u_int addr, 342e33bea8dSNate Lawson ACPI_INTEGER val, int width); 34315e32d5dSMike Smith 34415e32d5dSMike Smith static device_method_t acpi_ec_methods[] = { 34515e32d5dSMike Smith /* Device interface */ 34615e32d5dSMike Smith DEVMETHOD(device_probe, acpi_ec_probe), 34715e32d5dSMike Smith DEVMETHOD(device_attach, acpi_ec_attach), 348340a7f6aSNate Lawson DEVMETHOD(device_shutdown, acpi_ec_shutdown), 34915e32d5dSMike Smith 350e33bea8dSNate Lawson /* Embedded controller interface */ 351e33bea8dSNate Lawson DEVMETHOD(acpi_ec_read, acpi_ec_read_method), 352e33bea8dSNate Lawson DEVMETHOD(acpi_ec_write, acpi_ec_write_method), 353e33bea8dSNate Lawson 35415e32d5dSMike Smith {0, 0} 35515e32d5dSMike Smith }; 35615e32d5dSMike Smith 35715e32d5dSMike Smith static driver_t acpi_ec_driver = { 35815e32d5dSMike Smith "acpi_ec", 35915e32d5dSMike Smith acpi_ec_methods, 36015e32d5dSMike Smith sizeof(struct acpi_ec_softc), 36115e32d5dSMike Smith }; 36215e32d5dSMike Smith 3633273b005SMike Smith static devclass_t acpi_ec_devclass; 36415e32d5dSMike Smith DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0); 36564278df5SNate Lawson MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1); 36615e32d5dSMike Smith 36715e32d5dSMike Smith /* 368f8335e3aSNate Lawson * Look for an ECDT and if we find one, set up default GPE and 369f8335e3aSNate Lawson * space handlers to catch attempts to access EC space before 37015e32d5dSMike Smith * we have a real driver instance in place. 371f8335e3aSNate Lawson * TODO: if people report invalid ECDTs, add a tunable to disable them. 37215e32d5dSMike Smith */ 373f8335e3aSNate Lawson void 374f8335e3aSNate Lawson acpi_ec_ecdt_probe(device_t parent) 37515e32d5dSMike Smith { 376f8335e3aSNate Lawson ACPI_TABLE_ECDT *ecdt; 37799eb9c8bSNate Lawson ACPI_TABLE_HEADER *hdr; 378f8335e3aSNate Lawson ACPI_STATUS status; 379f8335e3aSNate Lawson device_t child; 380f8335e3aSNate Lawson ACPI_HANDLE h; 3810025fb0fSNate Lawson struct acpi_ec_params *params; 382f8335e3aSNate Lawson 383b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 38415e32d5dSMike Smith 385f8335e3aSNate Lawson /* Find and validate the ECDT. */ 38699eb9c8bSNate Lawson status = AcpiGetFirmwareTable("ECDT", 1, ACPI_LOGICAL_ADDRESSING, &hdr); 38799eb9c8bSNate Lawson ecdt = (ACPI_TABLE_ECDT *)hdr; 388f8335e3aSNate Lawson if (ACPI_FAILURE(status) || 389f8335e3aSNate Lawson ecdt->control.RegisterBitWidth != 8 || 390f8335e3aSNate Lawson ecdt->data.RegisterBitWidth != 8) { 391f8335e3aSNate Lawson return; 39215e32d5dSMike Smith } 39315e32d5dSMike Smith 394f8335e3aSNate Lawson /* Create the child device with the given unit number. */ 395f8335e3aSNate Lawson child = BUS_ADD_CHILD(parent, 0, "acpi_ec", ecdt->uid); 396f8335e3aSNate Lawson if (child == NULL) { 3970025fb0fSNate Lawson printf("%s: can't add child\n", __func__); 398f8335e3aSNate Lawson return; 399f8335e3aSNate Lawson } 400f8335e3aSNate Lawson 401f8335e3aSNate Lawson /* Find and save the ACPI handle for this device. */ 402f8335e3aSNate Lawson status = AcpiGetHandle(NULL, ecdt->ec_id, &h); 403f8335e3aSNate Lawson if (ACPI_FAILURE(status)) { 404f8335e3aSNate Lawson device_delete_child(parent, child); 4050025fb0fSNate Lawson printf("%s: can't get handle\n", __func__); 406f8335e3aSNate Lawson return; 407f8335e3aSNate Lawson } 408f8335e3aSNate Lawson acpi_set_handle(child, h); 409f8335e3aSNate Lawson 410f8335e3aSNate Lawson /* Set the data and CSR register addresses. */ 411f8335e3aSNate Lawson bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->data.Address, 412f8335e3aSNate Lawson /*count*/1); 413f8335e3aSNate Lawson bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->control.Address, 414f8335e3aSNate Lawson /*count*/1); 415f8335e3aSNate Lawson 41615e32d5dSMike Smith /* 417f8335e3aSNate Lawson * Store values for the probe/attach routines to use. Store the 418c868ac7dSNate Lawson * ECDT GPE bit and set the global lock flag according to _GLK. 4190025fb0fSNate Lawson * Note that it is not perfectly correct to be evaluating a method 4200025fb0fSNate Lawson * before initializing devices, but in practice this function 4210025fb0fSNate Lawson * should be safe to call at this point. 42215e32d5dSMike Smith */ 4230025fb0fSNate Lawson params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO); 4240025fb0fSNate Lawson params->gpe_handle = NULL; 4250025fb0fSNate Lawson params->gpe_bit = ecdt->gpe_bit; 4260025fb0fSNate Lawson params->uid = ecdt->uid; 4270025fb0fSNate Lawson acpi_GetInteger(h, "_GLK", ¶ms->glk); 4280025fb0fSNate Lawson acpi_set_private(child, params); 4290025fb0fSNate Lawson acpi_set_magic(child, (int)&acpi_ec_devclass); 430f8335e3aSNate Lawson 431f8335e3aSNate Lawson /* Finish the attach process. */ 432f8335e3aSNate Lawson if (device_probe_and_attach(child) != 0) 433f8335e3aSNate Lawson device_delete_child(parent, child); 434f8335e3aSNate Lawson } 435f8335e3aSNate Lawson 43615e32d5dSMike Smith static int 43715e32d5dSMike Smith acpi_ec_probe(device_t dev) 43815e32d5dSMike Smith { 4390025fb0fSNate Lawson ACPI_BUFFER buf; 440f8335e3aSNate Lawson ACPI_HANDLE h; 4410025fb0fSNate Lawson ACPI_OBJECT *obj; 442f8335e3aSNate Lawson ACPI_STATUS status; 443f8335e3aSNate Lawson device_t peer; 444f8335e3aSNate Lawson char desc[64]; 4450025fb0fSNate Lawson int ret; 4460025fb0fSNate Lawson struct acpi_ec_params *params; 4475fcc8a58SNate Lawson static char *ec_ids[] = { "PNP0C09", NULL }; 4480ae55423SMike Smith 449c72508e2SNate Lawson /* Check that this is a device and that EC is not disabled. */ 450c72508e2SNate Lawson if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec")) 451f8335e3aSNate Lawson return (ENXIO); 45215e32d5dSMike Smith 45315e32d5dSMike Smith /* 454f8335e3aSNate Lawson * If probed via ECDT, set description and continue. Otherwise, 455f8335e3aSNate Lawson * we can access the namespace and make sure this is not a 456f8335e3aSNate Lawson * duplicate probe. 45715e32d5dSMike Smith */ 4580025fb0fSNate Lawson ret = ENXIO; 4590025fb0fSNate Lawson params = NULL; 4600025fb0fSNate Lawson buf.Pointer = NULL; 4610025fb0fSNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 4620025fb0fSNate Lawson if (DEV_ECDT(dev)) { 4630025fb0fSNate Lawson params = acpi_get_private(dev); 4641f04e8f5SNate Lawson ret = 0; 4655fcc8a58SNate Lawson } else if (!acpi_disabled("ec") && 4665fcc8a58SNate Lawson ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids)) { 4670025fb0fSNate Lawson params = malloc(sizeof(struct acpi_ec_params), M_TEMP, 4680025fb0fSNate Lawson M_WAITOK | M_ZERO); 469f8335e3aSNate Lawson h = acpi_get_handle(dev); 470f8335e3aSNate Lawson 471f8335e3aSNate Lawson /* 472f8335e3aSNate Lawson * Read the unit ID to check for duplicate attach and the 473f8335e3aSNate Lawson * global lock value to see if we should acquire it when 474f8335e3aSNate Lawson * accessing the EC. 475f8335e3aSNate Lawson */ 4760025fb0fSNate Lawson status = acpi_GetInteger(h, "_UID", ¶ms->uid); 477f8335e3aSNate Lawson if (ACPI_FAILURE(status)) 4780025fb0fSNate Lawson params->uid = 0; 4790025fb0fSNate Lawson status = acpi_GetInteger(h, "_GLK", ¶ms->glk); 480f8335e3aSNate Lawson if (ACPI_FAILURE(status)) 4810025fb0fSNate Lawson params->glk = 0; 482f8335e3aSNate Lawson 483f8335e3aSNate Lawson /* 484f8335e3aSNate Lawson * Evaluate the _GPE method to find the GPE bit used by the EC to 4850025fb0fSNate Lawson * signal status (SCI). If it's a package, it contains a reference 4860025fb0fSNate Lawson * and GPE bit, similar to _PRW. 487f8335e3aSNate Lawson */ 4880025fb0fSNate Lawson status = AcpiEvaluateObject(h, "_GPE", NULL, &buf); 489f8335e3aSNate Lawson if (ACPI_FAILURE(status)) { 490f8335e3aSNate Lawson device_printf(dev, "can't evaluate _GPE - %s\n", 491f8335e3aSNate Lawson AcpiFormatException(status)); 492a6761eb3SNate Lawson goto out; 493f8335e3aSNate Lawson } 4940025fb0fSNate Lawson obj = (ACPI_OBJECT *)buf.Pointer; 4950025fb0fSNate Lawson if (obj == NULL) 496a6761eb3SNate Lawson goto out; 4970025fb0fSNate Lawson 4980025fb0fSNate Lawson switch (obj->Type) { 4990025fb0fSNate Lawson case ACPI_TYPE_INTEGER: 5000025fb0fSNate Lawson params->gpe_handle = NULL; 5010025fb0fSNate Lawson params->gpe_bit = obj->Integer.Value; 5020025fb0fSNate Lawson break; 5030025fb0fSNate Lawson case ACPI_TYPE_PACKAGE: 5040025fb0fSNate Lawson if (!ACPI_PKG_VALID(obj, 2)) 5050025fb0fSNate Lawson goto out; 5060025fb0fSNate Lawson params->gpe_handle = 5070025fb0fSNate Lawson acpi_GetReference(NULL, &obj->Package.Elements[0]); 5080025fb0fSNate Lawson if (params->gpe_handle == NULL || 5090025fb0fSNate Lawson acpi_PkgInt32(obj, 1, ¶ms->gpe_bit) != 0) 5100025fb0fSNate Lawson goto out; 5110025fb0fSNate Lawson break; 5120025fb0fSNate Lawson default: 5130025fb0fSNate Lawson device_printf(dev, "_GPE has invalid type %d\n", obj->Type); 5140025fb0fSNate Lawson goto out; 5150025fb0fSNate Lawson } 516f8335e3aSNate Lawson 517f8335e3aSNate Lawson /* Store the values we got from the namespace for attach. */ 5180025fb0fSNate Lawson acpi_set_private(dev, params); 519f8335e3aSNate Lawson 520f8335e3aSNate Lawson /* 521f8335e3aSNate Lawson * Check for a duplicate probe. This can happen when a probe 522c868ac7dSNate Lawson * via ECDT succeeded already. If this is a duplicate, disable 523c868ac7dSNate Lawson * this device. 524f8335e3aSNate Lawson */ 5250025fb0fSNate Lawson peer = devclass_get_device(acpi_ec_devclass, params->uid); 526c868ac7dSNate Lawson if (peer == NULL || !device_is_alive(peer)) 527f8335e3aSNate Lawson ret = 0; 528c868ac7dSNate Lawson else 529c868ac7dSNate Lawson device_disable(dev); 530c868ac7dSNate Lawson } 531f8335e3aSNate Lawson 5320025fb0fSNate Lawson out: 533c868ac7dSNate Lawson if (ret == 0) { 534c868ac7dSNate Lawson snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s", 5350025fb0fSNate Lawson params->gpe_bit, (params->glk) ? ", GLK" : "", 536c868ac7dSNate Lawson DEV_ECDT(dev) ? ", ECDT" : ""); 537c868ac7dSNate Lawson device_set_desc_copy(dev, desc); 53815e32d5dSMike Smith } 5391f04e8f5SNate Lawson 5400025fb0fSNate Lawson if (ret > 0 && params) 5410025fb0fSNate Lawson free(params, M_TEMP); 5420025fb0fSNate Lawson if (buf.Pointer) 5430025fb0fSNate Lawson AcpiOsFree(buf.Pointer); 5441f04e8f5SNate Lawson return (ret); 54515e32d5dSMike Smith } 54615e32d5dSMike Smith 54715e32d5dSMike Smith static int 54815e32d5dSMike Smith acpi_ec_attach(device_t dev) 54915e32d5dSMike Smith { 55015e32d5dSMike Smith struct acpi_ec_softc *sc; 5510025fb0fSNate Lawson struct acpi_ec_params *params; 55215e32d5dSMike Smith ACPI_STATUS Status; 553dbd0058aSMike Smith 554b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 5550ae55423SMike Smith 5561f04e8f5SNate Lawson /* Fetch/initialize softc (assumes softc is pre-zeroed). */ 55715e32d5dSMike Smith sc = device_get_softc(dev); 5580025fb0fSNate Lawson params = acpi_get_private(dev); 55915e32d5dSMike Smith sc->ec_dev = dev; 56015e32d5dSMike Smith sc->ec_handle = acpi_get_handle(dev); 56115e32d5dSMike Smith 562f8335e3aSNate Lawson /* Retrieve previously probed values via device ivars. */ 5630025fb0fSNate Lawson sc->ec_glk = params->glk; 5640025fb0fSNate Lawson sc->ec_gpebit = params->gpe_bit; 5650025fb0fSNate Lawson sc->ec_gpehandle = params->gpe_handle; 5660025fb0fSNate Lawson sc->ec_uid = params->uid; 5670025fb0fSNate Lawson free(params, M_TEMP); 568f8335e3aSNate Lawson 5691f04e8f5SNate Lawson /* Attach bus resources for data and command/status ports. */ 57015e32d5dSMike Smith sc->ec_data_rid = 0; 5715f96beb9SNate Lawson sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT, 5725f96beb9SNate Lawson &sc->ec_data_rid, RF_ACTIVE); 573464c662eSNate Lawson if (sc->ec_data_res == NULL) { 57415e32d5dSMike Smith device_printf(dev, "can't allocate data port\n"); 575f3fc4f8bSNate Lawson goto error; 57615e32d5dSMike Smith } 57715e32d5dSMike Smith sc->ec_data_tag = rman_get_bustag(sc->ec_data_res); 57815e32d5dSMike Smith sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res); 57915e32d5dSMike Smith 58015e32d5dSMike Smith sc->ec_csr_rid = 1; 5815f96beb9SNate Lawson sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT, 5825f96beb9SNate Lawson &sc->ec_csr_rid, RF_ACTIVE); 583464c662eSNate Lawson if (sc->ec_csr_res == NULL) { 58415e32d5dSMike Smith device_printf(dev, "can't allocate command/status port\n"); 585f3fc4f8bSNate Lawson goto error; 58615e32d5dSMike Smith } 58715e32d5dSMike Smith sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res); 58815e32d5dSMike Smith sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res); 58915e32d5dSMike Smith 59015e32d5dSMike Smith /* 5911f04e8f5SNate Lawson * Install a handler for this EC's GPE bit. We want edge-triggered 5921f04e8f5SNate Lawson * behavior. 59315e32d5dSMike Smith */ 5941f04e8f5SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n")); 5950025fb0fSNate Lawson Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, 596b7d13479SNate Lawson ACPI_GPE_EDGE_TRIGGERED, &EcGpeHandler, sc); 597464c662eSNate Lawson if (ACPI_FAILURE(Status)) { 59876d1dff4SMike Smith device_printf(dev, "can't install GPE handler for %s - %s\n", 599bfae45aaSMike Smith acpi_name(sc->ec_handle), AcpiFormatException(Status)); 600f3fc4f8bSNate Lawson goto error; 60115e32d5dSMike Smith } 60215e32d5dSMike Smith 60315e32d5dSMike Smith /* 60415e32d5dSMike Smith * Install address space handler 60515e32d5dSMike Smith */ 6064c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n")); 607464c662eSNate Lawson Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC, 6081f04e8f5SNate Lawson &EcSpaceHandler, &EcSpaceSetup, sc); 609464c662eSNate Lawson if (ACPI_FAILURE(Status)) { 61076d1dff4SMike Smith device_printf(dev, "can't install address space handler for %s - %s\n", 611bfae45aaSMike Smith acpi_name(sc->ec_handle), AcpiFormatException(Status)); 612f3fc4f8bSNate Lawson goto error; 613f3fc4f8bSNate Lawson } 614f3fc4f8bSNate Lawson 615f3fc4f8bSNate Lawson /* Enable runtime GPEs for the handler. */ 616f3fc4f8bSNate Lawson Status = AcpiSetGpeType(sc->ec_gpehandle, sc->ec_gpebit, 617f3fc4f8bSNate Lawson ACPI_GPE_TYPE_RUNTIME); 618f3fc4f8bSNate Lawson if (ACPI_FAILURE(Status)) { 619f3fc4f8bSNate Lawson device_printf(dev, "AcpiSetGpeType failed: %s\n", 620f3fc4f8bSNate Lawson AcpiFormatException(Status)); 621f3fc4f8bSNate Lawson goto error; 622f3fc4f8bSNate Lawson } 623f3fc4f8bSNate Lawson Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR); 624f3fc4f8bSNate Lawson if (ACPI_FAILURE(Status)) { 625f3fc4f8bSNate Lawson device_printf(dev, "AcpiEnableGpe failed: %s\n", 626f3fc4f8bSNate Lawson AcpiFormatException(Status)); 627f3fc4f8bSNate Lawson goto error; 62815e32d5dSMike Smith } 6291f04e8f5SNate Lawson 6301f04e8f5SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n")); 6311f04e8f5SNate Lawson return (0); 632464c662eSNate Lawson 633f3fc4f8bSNate Lawson error: 634f3fc4f8bSNate Lawson AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, &EcGpeHandler); 635f3fc4f8bSNate Lawson AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC, 636f3fc4f8bSNate Lawson EcSpaceHandler); 637f8372adeSTakanori Watanabe if (sc->ec_csr_res) 638f8372adeSTakanori Watanabe bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid, 639f8372adeSTakanori Watanabe sc->ec_csr_res); 640f8372adeSTakanori Watanabe if (sc->ec_data_res) 641f8372adeSTakanori Watanabe bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid, 642f8372adeSTakanori Watanabe sc->ec_data_res); 643f3fc4f8bSNate Lawson return (ENXIO); 6441f04e8f5SNate Lawson } 6451f04e8f5SNate Lawson 646340a7f6aSNate Lawson static int 647340a7f6aSNate Lawson acpi_ec_shutdown(device_t dev) 648340a7f6aSNate Lawson { 649340a7f6aSNate Lawson struct acpi_ec_softc *sc; 650340a7f6aSNate Lawson 651340a7f6aSNate Lawson /* Disable the GPE so we don't get EC events during shutdown. */ 652340a7f6aSNate Lawson sc = device_get_softc(dev); 653340a7f6aSNate Lawson AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR); 654340a7f6aSNate Lawson return (0); 655340a7f6aSNate Lawson } 656340a7f6aSNate Lawson 657e33bea8dSNate Lawson /* Methods to allow other devices (e.g., smbat) to read/write EC space. */ 658e33bea8dSNate Lawson static int 659e33bea8dSNate Lawson acpi_ec_read_method(device_t dev, u_int addr, ACPI_INTEGER *val, int width) 660e33bea8dSNate Lawson { 661e33bea8dSNate Lawson struct acpi_ec_softc *sc; 662e33bea8dSNate Lawson ACPI_STATUS status; 663e33bea8dSNate Lawson 664e33bea8dSNate Lawson sc = device_get_softc(dev); 665e33bea8dSNate Lawson status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL); 666e33bea8dSNate Lawson if (ACPI_FAILURE(status)) 667e33bea8dSNate Lawson return (ENXIO); 668e33bea8dSNate Lawson return (0); 669e33bea8dSNate Lawson } 670e33bea8dSNate Lawson 671e33bea8dSNate Lawson static int 672e33bea8dSNate Lawson acpi_ec_write_method(device_t dev, u_int addr, ACPI_INTEGER val, int width) 673e33bea8dSNate Lawson { 674e33bea8dSNate Lawson struct acpi_ec_softc *sc; 675e33bea8dSNate Lawson ACPI_STATUS status; 676e33bea8dSNate Lawson 677e33bea8dSNate Lawson sc = device_get_softc(dev); 678e33bea8dSNate Lawson status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL); 679e33bea8dSNate Lawson if (ACPI_FAILURE(status)) 680e33bea8dSNate Lawson return (ENXIO); 681e33bea8dSNate Lawson return (0); 682e33bea8dSNate Lawson } 683e33bea8dSNate Lawson 68415e32d5dSMike Smith static void 685c07572e7STakanori Watanabe EcGpeQueryHandler(void *Context) 68615e32d5dSMike Smith { 68715e32d5dSMike Smith struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context; 68815e32d5dSMike Smith UINT8 Data; 68915e32d5dSMike Smith ACPI_STATUS Status; 6901f04e8f5SNate Lawson EC_STATUS EcStatus; 69115e32d5dSMike Smith char qxx[5]; 69215e32d5dSMike Smith 693b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 6941f04e8f5SNate Lawson KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL")); 69515e32d5dSMike Smith 6963a371f32SNate Lawson Status = EcLock(sc); 6973a371f32SNate Lawson if (ACPI_FAILURE(Status)) { 6983a371f32SNate Lawson ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 6993a371f32SNate Lawson "GpeQuery lock error: %s\n", AcpiFormatException(Status)); 7003a371f32SNate Lawson return; 7013a371f32SNate Lawson } 7023a371f32SNate Lawson 70315e32d5dSMike Smith /* 7043a371f32SNate Lawson * If the EC_SCI bit of the status register is not set, then pass 7053a371f32SNate Lawson * it along to any potential waiters as it may be an IBE/OBF event. 70615e32d5dSMike Smith */ 7071f04e8f5SNate Lawson EcStatus = EC_GET_CSR(sc); 708ff40920eSNate Lawson if ((EcStatus & EC_EVENT_SCI) == 0) { 709c4a9fa45SNate Lawson CTR1(KTR_ACPI, "ec event was not SCI, status %#x", EcStatus); 7103a371f32SNate Lawson sc->ec_csrvalue = EcStatus; 7113a371f32SNate Lawson wakeup(&sc->ec_csrvalue); 7123a371f32SNate Lawson EcUnlock(sc); 7131f04e8f5SNate Lawson goto re_enable; 714ff40920eSNate Lawson } 71515e32d5dSMike Smith 7163a371f32SNate Lawson /* 7173a371f32SNate Lawson * Send a query command to the EC to find out which _Qxx call it 7183a371f32SNate Lawson * wants to make. This command clears the SCI bit and also the 7193a371f32SNate Lawson * interrupt source since we are edge-triggered. 7203a371f32SNate Lawson */ 7213a371f32SNate Lawson Status = EcCommand(sc, EC_COMMAND_QUERY); 722dbd0058aSMike Smith if (ACPI_FAILURE(Status)) { 7233a371f32SNate Lawson EcUnlock(sc); 7246971b3c7SMitsuru IWASAKI ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 7256971b3c7SMitsuru IWASAKI "GPE query failed - %s\n", AcpiFormatException(Status)); 7261f04e8f5SNate Lawson goto re_enable; 72715e32d5dSMike Smith } 7283a371f32SNate Lawson Data = EC_GET_DATA(sc); 7293a371f32SNate Lawson EcUnlock(sc); 73015e32d5dSMike Smith 7311f04e8f5SNate Lawson /* Ignore the value for "no outstanding event". (13.3.5) */ 732c4a9fa45SNate Lawson CTR2(KTR_ACPI, "ec query ok,%s running _Q%02x", Data ? "" : " not", Data); 7331f04e8f5SNate Lawson if (Data == 0) 7341f04e8f5SNate Lawson goto re_enable; 7351f04e8f5SNate Lawson 7361f04e8f5SNate Lawson /* Evaluate _Qxx to respond to the controller. */ 73715e32d5dSMike Smith sprintf(qxx, "_Q%02x", Data); 738b3919c8dSMark Santcroos AcpiUtStrupr(qxx); 739ee785aa9SJohn Baldwin Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL); 7401f04e8f5SNate Lawson if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) { 7416971b3c7SMitsuru IWASAKI ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 7426971b3c7SMitsuru IWASAKI "evaluation of GPE query method %s failed - %s\n", 743bfae45aaSMike Smith qxx, AcpiFormatException(Status)); 74415e32d5dSMike Smith } 7451f04e8f5SNate Lawson 7461f04e8f5SNate Lawson re_enable: 7471f04e8f5SNate Lawson /* Re-enable the GPE event so we'll get future requests. */ 748f3fc4f8bSNate Lawson Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR); 7491f04e8f5SNate Lawson if (ACPI_FAILURE(Status)) 7501f04e8f5SNate Lawson printf("EcGpeQueryHandler: AcpiEnableEvent failed\n"); 751c07572e7STakanori Watanabe } 75242f6d122SMike Smith 753a9cf0dffSMike Smith /* 7541f04e8f5SNate Lawson * Handle a GPE. Currently we only handle SCI events as others must 7551f04e8f5SNate Lawson * be handled by polling in EcWaitEvent(). This is because some ECs 7561f04e8f5SNate Lawson * treat events as level when they should be edge-triggered. 757a9cf0dffSMike Smith */ 758f3fc4f8bSNate Lawson static uint32_t 759a9cf0dffSMike Smith EcGpeHandler(void *Context) 760c07572e7STakanori Watanabe { 761c07572e7STakanori Watanabe struct acpi_ec_softc *sc = Context; 7621f04e8f5SNate Lawson ACPI_STATUS Status; 763a9cf0dffSMike Smith 7641f04e8f5SNate Lawson KASSERT(Context != NULL, ("EcGpeHandler called with NULL")); 7651f04e8f5SNate Lawson 7661395b555SNate Lawson /* 7671395b555SNate Lawson * Disable further GPEs while we handle this one. Since we are directly 7681395b555SNate Lawson * called by ACPI-CA and it may have unknown locks held, we specify the 7691395b555SNate Lawson * ACPI_ISR flag to keep it from acquiring any more mutexes (which could 7701395b555SNate Lawson * potentially sleep.) 7711395b555SNate Lawson */ 7721395b555SNate Lawson AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_ISR); 7731f04e8f5SNate Lawson 7741f04e8f5SNate Lawson /* Schedule the GPE query handler. */ 7751f04e8f5SNate Lawson Status = AcpiOsQueueForExecution(OSD_PRIORITY_GPE, EcGpeQueryHandler, 7761f04e8f5SNate Lawson Context); 7771f04e8f5SNate Lawson if (ACPI_FAILURE(Status)) { 7781f04e8f5SNate Lawson printf("Queuing GPE query handler failed.\n"); 7791395b555SNate Lawson Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_ISR); 7801f04e8f5SNate Lawson if (ACPI_FAILURE(Status)) 7811f04e8f5SNate Lawson printf("EcGpeHandler: AcpiEnableEvent failed\n"); 782c07572e7STakanori Watanabe } 783f3fc4f8bSNate Lawson 784f3fc4f8bSNate Lawson return (0); 78515e32d5dSMike Smith } 78615e32d5dSMike Smith 78715e32d5dSMike Smith static ACPI_STATUS 788464c662eSNate Lawson EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context, 789464c662eSNate Lawson void **RegionContext) 79015e32d5dSMike Smith { 79142f6d122SMike Smith 792b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 79342f6d122SMike Smith 79415e32d5dSMike Smith /* 79516844a97SNate Lawson * If deactivating a region, always set the output to NULL. Otherwise, 79616844a97SNate Lawson * just pass the context through. 79715e32d5dSMike Smith */ 79816844a97SNate Lawson if (Function == ACPI_REGION_DEACTIVATE) 79916844a97SNate Lawson *RegionContext = NULL; 80016844a97SNate Lawson else 80115e32d5dSMike Smith *RegionContext = Context; 80215e32d5dSMike Smith 80342f6d122SMike Smith return_ACPI_STATUS (AE_OK); 80415e32d5dSMike Smith } 80515e32d5dSMike Smith 80615e32d5dSMike Smith static ACPI_STATUS 807464c662eSNate Lawson EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width, 808464c662eSNate Lawson ACPI_INTEGER *Value, void *Context, void *RegionContext) 80915e32d5dSMike Smith { 81015e32d5dSMike Smith struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context; 8114ed391b8SNate Lawson ACPI_STATUS Status; 8121f04e8f5SNate Lawson UINT8 EcAddr, EcData; 813ee785aa9SJohn Baldwin int i; 81415e32d5dSMike Smith 815b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address); 8160ae55423SMike Smith 8174ed391b8SNate Lawson if (width % 8 != 0 || Value == NULL || Context == NULL) 8180ae55423SMike Smith return_ACPI_STATUS (AE_BAD_PARAMETER); 8194ed391b8SNate Lawson if (Address + (width / 8) - 1 > 0xFF) 8204ed391b8SNate Lawson return_ACPI_STATUS (AE_BAD_ADDRESS); 82115e32d5dSMike Smith 8224ed391b8SNate Lawson if (Function == ACPI_READ) 8234ed391b8SNate Lawson *Value = 0; 8241f04e8f5SNate Lawson EcAddr = Address; 8254ed391b8SNate Lawson Status = AE_ERROR; 8264ed391b8SNate Lawson 8271f04e8f5SNate Lawson Status = EcLock(sc); 828464c662eSNate Lawson if (ACPI_FAILURE(Status)) 829f4b7de15SNate Lawson return_ACPI_STATUS (Status); 8301f04e8f5SNate Lawson 831f4b7de15SNate Lawson /* Perform the transaction(s), based on width. */ 832f4b7de15SNate Lawson for (i = 0; i < width; i += 8, EcAddr++) { 8331f04e8f5SNate Lawson switch (Function) { 8341f04e8f5SNate Lawson case ACPI_READ: 8351f04e8f5SNate Lawson Status = EcRead(sc, EcAddr, &EcData); 8364ed391b8SNate Lawson if (ACPI_SUCCESS(Status)) 8374ed391b8SNate Lawson *Value |= ((ACPI_INTEGER)EcData) << i; 838ee785aa9SJohn Baldwin break; 8391f04e8f5SNate Lawson case ACPI_WRITE: 8401f04e8f5SNate Lawson EcData = (UINT8)((*Value) >> i); 8411f04e8f5SNate Lawson Status = EcWrite(sc, EcAddr, &EcData); 8421f04e8f5SNate Lawson break; 8431f04e8f5SNate Lawson default: 8441f04e8f5SNate Lawson device_printf(sc->ec_dev, "invalid EcSpaceHandler function %d\n", 8451f04e8f5SNate Lawson Function); 8461f04e8f5SNate Lawson Status = AE_BAD_PARAMETER; 8471f04e8f5SNate Lawson break; 8481f04e8f5SNate Lawson } 8491f04e8f5SNate Lawson if (ACPI_FAILURE(Status)) 8504ed391b8SNate Lawson break; 851ee785aa9SJohn Baldwin } 8524ed391b8SNate Lawson 853f4b7de15SNate Lawson EcUnlock(sc); 8540ae55423SMike Smith return_ACPI_STATUS (Status); 85515e32d5dSMike Smith } 8562a4ac806SMike Smith 85715e32d5dSMike Smith static ACPI_STATUS 85815e32d5dSMike Smith EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event) 85915e32d5dSMike Smith { 86015e32d5dSMike Smith EC_STATUS EcStatus; 8611f04e8f5SNate Lawson ACPI_STATUS Status; 862b0eefa38SNate Lawson int count, i, period, retval, slp_ival; 86315e32d5dSMike Smith 864f4b7de15SNate Lawson ACPI_SERIAL_ASSERT(ec); 8651f04e8f5SNate Lawson Status = AE_NO_HARDWARE_RESPONSE; 86615e32d5dSMike Smith 86715e32d5dSMike Smith /* 8681f04e8f5SNate Lawson * Wait for 1 us before checking the CSR. Testing shows about 8691f04e8f5SNate Lawson * 50% of requests complete in 1 us and 90% of them complete 8701f04e8f5SNate Lawson * in 5 us or less. 87115e32d5dSMike Smith */ 872da3b867eSMike Smith AcpiOsStall(1); 87315e32d5dSMike Smith 87415e32d5dSMike Smith /* 875b0eefa38SNate Lawson * Poll the EC status register for up to 1 ms in chunks of 10 us 876b0eefa38SNate Lawson * to detect completion of the last command. 87715e32d5dSMike Smith */ 878b0eefa38SNate Lawson for (i = 0; i < 1000 / EC_POLL_DELAY; i++) { 87915e32d5dSMike Smith EcStatus = EC_GET_CSR(sc); 880ff40920eSNate Lawson if (EVENT_READY(Event, EcStatus)) { 8811f04e8f5SNate Lawson Status = AE_OK; 88215e32d5dSMike Smith break; 8831f04e8f5SNate Lawson } 884b0eefa38SNate Lawson AcpiOsStall(EC_POLL_DELAY); 8851f04e8f5SNate Lawson } 886b0eefa38SNate Lawson period = i * EC_POLL_DELAY; 887ff40920eSNate Lawson 888ff40920eSNate Lawson /* 889636af382SNate Lawson * If we still don't have a response and we're up and running, wait up 890636af382SNate Lawson * to ec_poll_timeout ms for completion, sleeping for chunks of 10 ms. 891ff40920eSNate Lawson */ 892b0eefa38SNate Lawson slp_ival = 0; 893b0eefa38SNate Lawson if (Status != AE_OK) { 894b0eefa38SNate Lawson retval = ENXIO; 895b0eefa38SNate Lawson count = ec_poll_timeout / 10; 896b0eefa38SNate Lawson if (count == 0) 897b0eefa38SNate Lawson count = 1; 898b0eefa38SNate Lawson slp_ival = hz / 100; 899b0eefa38SNate Lawson if (slp_ival == 0) 900b0eefa38SNate Lawson slp_ival = 1; 901b0eefa38SNate Lawson for (i = 0; i < count; i++) { 9023a371f32SNate Lawson if (retval != 0) 903ff40920eSNate Lawson EcStatus = EC_GET_CSR(sc); 9043a371f32SNate Lawson else 9053a371f32SNate Lawson EcStatus = sc->ec_csrvalue; 906ff40920eSNate Lawson if (EVENT_READY(Event, EcStatus)) { 907ff40920eSNate Lawson Status = AE_OK; 908ff40920eSNate Lawson break; 909ff40920eSNate Lawson } 910b0eefa38SNate Lawson if (!cold) 911b0eefa38SNate Lawson retval = tsleep(&sc->ec_csrvalue, PZERO, "ecpoll", slp_ival); 912b0eefa38SNate Lawson else 913c4a9fa45SNate Lawson AcpiOsStall(hz * 1000); // XXX too big 914ff40920eSNate Lawson } 9151f04e8f5SNate Lawson } 9161f04e8f5SNate Lawson 917c4a9fa45SNate Lawson /* Calculate new delay and log it. */ 918b0eefa38SNate Lawson if (slp_ival > 0) 919c4a9fa45SNate Lawson period += i * (hz * 1000); 920c4a9fa45SNate Lawson CTR2(KTR_ACPI, "ec got event %#x after %d us", EcStatus, period); 92144904c40SNate Lawson 9221f04e8f5SNate Lawson return (Status); 9231f04e8f5SNate Lawson } 9241f04e8f5SNate Lawson 9251f04e8f5SNate Lawson static ACPI_STATUS 9261f04e8f5SNate Lawson EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd) 9271f04e8f5SNate Lawson { 9281f04e8f5SNate Lawson ACPI_STATUS Status; 9291f04e8f5SNate Lawson EC_EVENT Event; 9301f04e8f5SNate Lawson 931f4b7de15SNate Lawson ACPI_SERIAL_ASSERT(ec); 9321f04e8f5SNate Lawson 9331f04e8f5SNate Lawson /* Decide what to wait for based on command type. */ 9341f04e8f5SNate Lawson switch (cmd) { 9351f04e8f5SNate Lawson case EC_COMMAND_READ: 93615e32d5dSMike Smith case EC_COMMAND_WRITE: 9371f04e8f5SNate Lawson case EC_COMMAND_BURST_DISABLE: 9381f04e8f5SNate Lawson Event = EC_EVENT_INPUT_BUFFER_EMPTY; 9391f04e8f5SNate Lawson break; 9401f04e8f5SNate Lawson case EC_COMMAND_QUERY: 9411f04e8f5SNate Lawson case EC_COMMAND_BURST_ENABLE: 9421f04e8f5SNate Lawson Event = EC_EVENT_OUTPUT_BUFFER_FULL; 94315e32d5dSMike Smith break; 94415e32d5dSMike Smith default: 9456971b3c7SMitsuru IWASAKI ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 9461f04e8f5SNate Lawson "EcCommand: Invalid command %#x\n", cmd); 9471f04e8f5SNate Lawson return (AE_BAD_PARAMETER); 948464c662eSNate Lawson } 9491f04e8f5SNate Lawson 9501f04e8f5SNate Lawson /* Run the command and wait for the chosen event. */ 9511f04e8f5SNate Lawson EC_SET_CSR(sc, cmd); 9521f04e8f5SNate Lawson Status = EcWaitEvent(sc, Event); 9531f04e8f5SNate Lawson if (ACPI_FAILURE(Status)) { 9546971b3c7SMitsuru IWASAKI ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 9551f04e8f5SNate Lawson "EcCommand: no response to %#x\n", cmd); 956464c662eSNate Lawson } 95715e32d5dSMike Smith 95815e32d5dSMike Smith return (Status); 95915e32d5dSMike Smith } 96015e32d5dSMike Smith 96115e32d5dSMike Smith static ACPI_STATUS 96215e32d5dSMike Smith EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data) 96315e32d5dSMike Smith { 96415e32d5dSMike Smith ACPI_STATUS Status; 96515e32d5dSMike Smith 966f4b7de15SNate Lawson ACPI_SERIAL_ASSERT(ec); 967c4a9fa45SNate Lawson CTR1(KTR_ACPI, "ec read from %#x", Address); 96815e32d5dSMike Smith 9691f04e8f5SNate Lawson #ifdef notyet 9701f04e8f5SNate Lawson /* If we can't start burst mode, continue anyway. */ 9711f04e8f5SNate Lawson EcCommand(sc, EC_COMMAND_BURST_ENABLE); 9721f04e8f5SNate Lawson #endif 97315e32d5dSMike Smith 9741f04e8f5SNate Lawson Status = EcCommand(sc, EC_COMMAND_READ); 9751f04e8f5SNate Lawson if (ACPI_FAILURE(Status)) 97615e32d5dSMike Smith return (Status); 97715e32d5dSMike Smith 97815e32d5dSMike Smith EC_SET_DATA(sc, Address); 9791f04e8f5SNate Lawson Status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL); 980464c662eSNate Lawson if (ACPI_FAILURE(Status)) { 9816971b3c7SMitsuru IWASAKI ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 9826971b3c7SMitsuru IWASAKI "EcRead: Failed waiting for EC to send data.\n"); 98315e32d5dSMike Smith return (Status); 98415e32d5dSMike Smith } 98515e32d5dSMike Smith 986464c662eSNate Lawson *Data = EC_GET_DATA(sc); 98715e32d5dSMike Smith 9881f04e8f5SNate Lawson #ifdef notyet 9891f04e8f5SNate Lawson if (sc->ec_burstactive) { 9901f04e8f5SNate Lawson Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE); 9911f04e8f5SNate Lawson if (ACPI_FAILURE(Status)) 9921f04e8f5SNate Lawson return (Status); 9931f04e8f5SNate Lawson } 9941f04e8f5SNate Lawson #endif 99515e32d5dSMike Smith 99615e32d5dSMike Smith return (AE_OK); 99715e32d5dSMike Smith } 99815e32d5dSMike Smith 99915e32d5dSMike Smith static ACPI_STATUS 100015e32d5dSMike Smith EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data) 100115e32d5dSMike Smith { 100215e32d5dSMike Smith ACPI_STATUS Status; 100315e32d5dSMike Smith 1004f4b7de15SNate Lawson ACPI_SERIAL_ASSERT(ec); 1005c4a9fa45SNate Lawson CTR2(KTR_ACPI, "ec write to %#x, data %#x", Address, *Data); 100615e32d5dSMike Smith 10071f04e8f5SNate Lawson #ifdef notyet 10081f04e8f5SNate Lawson /* If we can't start burst mode, continue anyway. */ 10091f04e8f5SNate Lawson EcCommand(sc, EC_COMMAND_BURST_ENABLE); 10101f04e8f5SNate Lawson #endif 101115e32d5dSMike Smith 10121f04e8f5SNate Lawson Status = EcCommand(sc, EC_COMMAND_WRITE); 10131f04e8f5SNate Lawson if (ACPI_FAILURE(Status)) 101415e32d5dSMike Smith return (Status); 101515e32d5dSMike Smith 101615e32d5dSMike Smith EC_SET_DATA(sc, Address); 10171f04e8f5SNate Lawson Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY); 1018464c662eSNate Lawson if (ACPI_FAILURE(Status)) { 10196971b3c7SMitsuru IWASAKI ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 10201f04e8f5SNate Lawson "EcRead: Failed waiting for EC to process address\n"); 102115e32d5dSMike Smith return (Status); 102215e32d5dSMike Smith } 102315e32d5dSMike Smith 102415e32d5dSMike Smith EC_SET_DATA(sc, *Data); 10251f04e8f5SNate Lawson Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY); 1026464c662eSNate Lawson if (ACPI_FAILURE(Status)) { 10276971b3c7SMitsuru IWASAKI ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 10281f04e8f5SNate Lawson "EcWrite: Failed waiting for EC to process data\n"); 102915e32d5dSMike Smith return (Status); 103015e32d5dSMike Smith } 103115e32d5dSMike Smith 10321f04e8f5SNate Lawson #ifdef notyet 10331f04e8f5SNate Lawson if (sc->ec_burstactive) { 10341f04e8f5SNate Lawson Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE); 10351f04e8f5SNate Lawson if (ACPI_FAILURE(Status)) 10361f04e8f5SNate Lawson return (Status); 10371f04e8f5SNate Lawson } 10381f04e8f5SNate Lawson #endif 103915e32d5dSMike Smith 104015e32d5dSMike Smith return (AE_OK); 104115e32d5dSMike Smith } 1042