1/*-
2 * Copyright (c) 2003-2007 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#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/10/sys/dev/acpica/acpi_ec.c 310254 2016-12-19 09:52:32Z hselasky $");
31
32#include "opt_acpi.h"
33#include <sys/param.h>
34#include <sys/kernel.h>
35#include <sys/bus.h>
36#include <sys/lock.h>
37#include <sys/malloc.h>
38#include <sys/module.h>
39#include <sys/sx.h>
40
41#include <machine/bus.h>
42#include <machine/resource.h>
43#include <sys/rman.h>
44
45#include <contrib/dev/acpica/include/acpi.h>
46#include <contrib/dev/acpica/include/accommon.h>
47
48#include <dev/acpica/acpivar.h>
49
50/* Hooks for the ACPI CA debugging infrastructure */
51#define _COMPONENT	ACPI_EC
52ACPI_MODULE_NAME("EC")
53
54/*
55 * EC_COMMAND:
56 * -----------
57 */
58typedef UINT8				EC_COMMAND;
59
60#define EC_COMMAND_UNKNOWN		((EC_COMMAND) 0x00)
61#define EC_COMMAND_READ			((EC_COMMAND) 0x80)
62#define EC_COMMAND_WRITE		((EC_COMMAND) 0x81)
63#define EC_COMMAND_BURST_ENABLE		((EC_COMMAND) 0x82)
64#define EC_COMMAND_BURST_DISABLE	((EC_COMMAND) 0x83)
65#define EC_COMMAND_QUERY		((EC_COMMAND) 0x84)
66
67/*
68 * EC_STATUS:
69 * ----------
70 * The encoding of the EC status register is illustrated below.
71 * Note that a set bit (1) indicates the property is TRUE
72 * (e.g. if bit 0 is set then the output buffer is full).
73 * +-+-+-+-+-+-+-+-+
74 * |7|6|5|4|3|2|1|0|
75 * +-+-+-+-+-+-+-+-+
76 *  | | | | | | | |
77 *  | | | | | | | +- Output Buffer Full?
78 *  | | | | | | +--- Input Buffer Full?
79 *  | | | | | +----- <reserved>
80 *  | | | | +------- Data Register is Command Byte?
81 *  | | | +--------- Burst Mode Enabled?
82 *  | | +----------- SCI Event?
83 *  | +------------- SMI Event?
84 *  +--------------- <reserved>
85 *
86 */
87typedef UINT8				EC_STATUS;
88
89#define EC_FLAG_OUTPUT_BUFFER		((EC_STATUS) 0x01)
90#define EC_FLAG_INPUT_BUFFER		((EC_STATUS) 0x02)
91#define EC_FLAG_DATA_IS_CMD		((EC_STATUS) 0x08)
92#define EC_FLAG_BURST_MODE		((EC_STATUS) 0x10)
93
94/*
95 * EC_EVENT:
96 * ---------
97 */
98typedef UINT8				EC_EVENT;
99
100#define EC_EVENT_UNKNOWN		((EC_EVENT) 0x00)
101#define EC_EVENT_OUTPUT_BUFFER_FULL	((EC_EVENT) 0x01)
102#define EC_EVENT_INPUT_BUFFER_EMPTY	((EC_EVENT) 0x02)
103#define EC_EVENT_SCI			((EC_EVENT) 0x20)
104#define EC_EVENT_SMI			((EC_EVENT) 0x40)
105
106/* Data byte returned after burst enable indicating it was successful. */
107#define EC_BURST_ACK			0x90
108
109/*
110 * Register access primitives
111 */
112#define EC_GET_DATA(sc)							\
113	bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
114
115#define EC_SET_DATA(sc, v)						\
116	bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
117
118#define EC_GET_CSR(sc)							\
119	bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
120
121#define EC_SET_CSR(sc, v)						\
122	bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
123
124/* Additional params to pass from the probe routine */
125struct acpi_ec_params {
126    int		glk;
127    int		gpe_bit;
128    ACPI_HANDLE	gpe_handle;
129    int		uid;
130};
131
132/*
133 * Driver softc.
134 */
135struct acpi_ec_softc {
136    device_t		ec_dev;
137    ACPI_HANDLE		ec_handle;
138    int			ec_uid;
139    ACPI_HANDLE		ec_gpehandle;
140    UINT8		ec_gpebit;
141
142    int			ec_data_rid;
143    struct resource	*ec_data_res;
144    bus_space_tag_t	ec_data_tag;
145    bus_space_handle_t	ec_data_handle;
146
147    int			ec_csr_rid;
148    struct resource	*ec_csr_res;
149    bus_space_tag_t	ec_csr_tag;
150    bus_space_handle_t	ec_csr_handle;
151
152    int			ec_glk;
153    int			ec_glkhandle;
154    int			ec_burstactive;
155    int			ec_sci_pend;
156    volatile u_int	ec_gencount;
157    int			ec_suspending;
158};
159
160/*
161 * XXX njl
162 * I couldn't find it in the spec but other implementations also use a
163 * value of 1 ms for the time to acquire global lock.
164 */
165#define EC_LOCK_TIMEOUT	1000
166
167/* Default delay in microseconds between each run of the status polling loop. */
168#define EC_POLL_DELAY	50
169
170/* Total time in ms spent waiting for a response from EC. */
171#define EC_TIMEOUT	750
172
173#define EVENT_READY(event, status)			\
174	(((event) == EC_EVENT_OUTPUT_BUFFER_FULL &&	\
175	 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) ||	\
176	 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && 	\
177	 ((status) & EC_FLAG_INPUT_BUFFER) == 0))
178
179ACPI_SERIAL_DECL(ec, "ACPI embedded controller");
180
181static SYSCTL_NODE(_debug_acpi, OID_AUTO, ec, CTLFLAG_RD, NULL, "EC debugging");
182
183static int	ec_burst_mode;
184TUNABLE_INT("debug.acpi.ec.burst", &ec_burst_mode);
185SYSCTL_INT(_debug_acpi_ec, OID_AUTO, burst, CTLFLAG_RW, &ec_burst_mode, 0,
186    "Enable use of burst mode (faster for nearly all systems)");
187static int	ec_polled_mode;
188TUNABLE_INT("debug.acpi.ec.polled", &ec_polled_mode);
189SYSCTL_INT(_debug_acpi_ec, OID_AUTO, polled, CTLFLAG_RW, &ec_polled_mode, 0,
190    "Force use of polled mode (only if interrupt mode doesn't work)");
191static int	ec_timeout = EC_TIMEOUT;
192TUNABLE_INT("debug.acpi.ec.timeout", &ec_timeout);
193SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout, CTLFLAG_RW, &ec_timeout,
194    EC_TIMEOUT, "Total time spent waiting for a response (poll+sleep)");
195
196static ACPI_STATUS
197EcLock(struct acpi_ec_softc *sc)
198{
199    ACPI_STATUS	status;
200
201    /* If _GLK is non-zero, acquire the global lock. */
202    status = AE_OK;
203    if (sc->ec_glk) {
204	status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
205	if (ACPI_FAILURE(status))
206	    return (status);
207    }
208    ACPI_SERIAL_BEGIN(ec);
209    return (status);
210}
211
212static void
213EcUnlock(struct acpi_ec_softc *sc)
214{
215    ACPI_SERIAL_END(ec);
216    if (sc->ec_glk)
217	AcpiReleaseGlobalLock(sc->ec_glkhandle);
218}
219
220static UINT32		EcGpeHandler(ACPI_HANDLE, UINT32, void *);
221static ACPI_STATUS	EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
222				void *Context, void **return_Context);
223static ACPI_STATUS	EcSpaceHandler(UINT32 Function,
224				ACPI_PHYSICAL_ADDRESS Address,
225				UINT32 Width, UINT64 *Value,
226				void *Context, void *RegionContext);
227static ACPI_STATUS	EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event,
228				u_int gen_count);
229static ACPI_STATUS	EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
230static ACPI_STATUS	EcRead(struct acpi_ec_softc *sc, UINT8 Address,
231				UINT8 *Data);
232static ACPI_STATUS	EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
233				UINT8 Data);
234static int		acpi_ec_probe(device_t dev);
235static int		acpi_ec_attach(device_t dev);
236static int		acpi_ec_suspend(device_t dev);
237static int		acpi_ec_resume(device_t dev);
238static int		acpi_ec_shutdown(device_t dev);
239static int		acpi_ec_read_method(device_t dev, u_int addr,
240				UINT64 *val, int width);
241static int		acpi_ec_write_method(device_t dev, u_int addr,
242				UINT64 val, int width);
243
244static device_method_t acpi_ec_methods[] = {
245    /* Device interface */
246    DEVMETHOD(device_probe,	acpi_ec_probe),
247    DEVMETHOD(device_attach,	acpi_ec_attach),
248    DEVMETHOD(device_suspend,	acpi_ec_suspend),
249    DEVMETHOD(device_resume,	acpi_ec_resume),
250    DEVMETHOD(device_shutdown,	acpi_ec_shutdown),
251
252    /* Embedded controller interface */
253    DEVMETHOD(acpi_ec_read,	acpi_ec_read_method),
254    DEVMETHOD(acpi_ec_write,	acpi_ec_write_method),
255
256    DEVMETHOD_END
257};
258
259static driver_t acpi_ec_driver = {
260    "acpi_ec",
261    acpi_ec_methods,
262    sizeof(struct acpi_ec_softc),
263};
264
265static devclass_t acpi_ec_devclass;
266DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
267MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
268
269/*
270 * Look for an ECDT and if we find one, set up default GPE and
271 * space handlers to catch attempts to access EC space before
272 * we have a real driver instance in place.
273 *
274 * TODO: Some old Gateway laptops need us to fake up an ECDT or
275 * otherwise attach early so that _REG methods can run.
276 */
277void
278acpi_ec_ecdt_probe(device_t parent)
279{
280    ACPI_TABLE_ECDT *ecdt;
281    ACPI_STATUS	     status;
282    device_t	     child;
283    ACPI_HANDLE	     h;
284    struct acpi_ec_params *params;
285
286    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
287
288    /* Find and validate the ECDT. */
289    status = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
290    if (ACPI_FAILURE(status) ||
291	ecdt->Control.BitWidth != 8 ||
292	ecdt->Data.BitWidth != 8) {
293	return;
294    }
295
296    /* Create the child device with the given unit number. */
297    child = BUS_ADD_CHILD(parent, 3, "acpi_ec", ecdt->Uid);
298    if (child == NULL) {
299	printf("%s: can't add child\n", __func__);
300	return;
301    }
302
303    /* Find and save the ACPI handle for this device. */
304    status = AcpiGetHandle(NULL, ecdt->Id, &h);
305    if (ACPI_FAILURE(status)) {
306	device_delete_child(parent, child);
307	printf("%s: can't get handle\n", __func__);
308	return;
309    }
310    acpi_set_handle(child, h);
311
312    /* Set the data and CSR register addresses. */
313    bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->Data.Address,
314	/*count*/1);
315    bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->Control.Address,
316	/*count*/1);
317
318    /*
319     * Store values for the probe/attach routines to use.  Store the
320     * ECDT GPE bit and set the global lock flag according to _GLK.
321     * Note that it is not perfectly correct to be evaluating a method
322     * before initializing devices, but in practice this function
323     * should be safe to call at this point.
324     */
325    params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
326    params->gpe_handle = NULL;
327    params->gpe_bit = ecdt->Gpe;
328    params->uid = ecdt->Uid;
329    acpi_GetInteger(h, "_GLK", &params->glk);
330    acpi_set_private(child, params);
331
332    /* Finish the attach process. */
333    if (device_probe_and_attach(child) != 0)
334	device_delete_child(parent, child);
335}
336
337static int
338acpi_ec_probe(device_t dev)
339{
340    ACPI_BUFFER buf;
341    ACPI_HANDLE h;
342    ACPI_OBJECT *obj;
343    ACPI_STATUS status;
344    device_t	peer;
345    char	desc[64];
346    int		ecdt;
347    int		ret;
348    struct acpi_ec_params *params;
349    static char *ec_ids[] = { "PNP0C09", NULL };
350
351    /* Check that this is a device and that EC is not disabled. */
352    if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
353	return (ENXIO);
354
355    /*
356     * If probed via ECDT, set description and continue.  Otherwise,
357     * we can access the namespace and make sure this is not a
358     * duplicate probe.
359     */
360    ret = ENXIO;
361    ecdt = 0;
362    buf.Pointer = NULL;
363    buf.Length = ACPI_ALLOCATE_BUFFER;
364    params = acpi_get_private(dev);
365    if (params != NULL) {
366	ecdt = 1;
367	ret = 0;
368    } else if (ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids)) {
369	params = malloc(sizeof(struct acpi_ec_params), M_TEMP,
370			M_WAITOK | M_ZERO);
371	h = acpi_get_handle(dev);
372
373	/*
374	 * Read the unit ID to check for duplicate attach and the
375	 * global lock value to see if we should acquire it when
376	 * accessing the EC.
377	 */
378	status = acpi_GetInteger(h, "_UID", &params->uid);
379	if (ACPI_FAILURE(status))
380	    params->uid = 0;
381	status = acpi_GetInteger(h, "_GLK", &params->glk);
382	if (ACPI_FAILURE(status))
383	    params->glk = 0;
384
385	/*
386	 * Evaluate the _GPE method to find the GPE bit used by the EC to
387	 * signal status (SCI).  If it's a package, it contains a reference
388	 * and GPE bit, similar to _PRW.
389	 */
390	status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
391	if (ACPI_FAILURE(status)) {
392	    device_printf(dev, "can't evaluate _GPE - %s\n",
393			  AcpiFormatException(status));
394	    goto out;
395	}
396	obj = (ACPI_OBJECT *)buf.Pointer;
397	if (obj == NULL)
398	    goto out;
399
400	switch (obj->Type) {
401	case ACPI_TYPE_INTEGER:
402	    params->gpe_handle = NULL;
403	    params->gpe_bit = obj->Integer.Value;
404	    break;
405	case ACPI_TYPE_PACKAGE:
406	    if (!ACPI_PKG_VALID(obj, 2))
407		goto out;
408	    params->gpe_handle =
409		acpi_GetReference(NULL, &obj->Package.Elements[0]);
410	    if (params->gpe_handle == NULL ||
411		acpi_PkgInt32(obj, 1, &params->gpe_bit) != 0)
412		goto out;
413	    break;
414	default:
415	    device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
416	    goto out;
417	}
418
419	/* Store the values we got from the namespace for attach. */
420	acpi_set_private(dev, params);
421
422	/*
423	 * Check for a duplicate probe.  This can happen when a probe
424	 * via ECDT succeeded already.  If this is a duplicate, disable
425	 * this device.
426	 */
427	peer = devclass_get_device(acpi_ec_devclass, params->uid);
428	if (peer == NULL || !device_is_alive(peer))
429	    ret = 0;
430	else
431	    device_disable(dev);
432    }
433
434out:
435    if (ret == 0) {
436	snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
437		 params->gpe_bit, (params->glk) ? ", GLK" : "",
438		 ecdt ? ", ECDT" : "");
439	device_set_desc_copy(dev, desc);
440    }
441
442    if (ret > 0 && params)
443	free(params, M_TEMP);
444    if (buf.Pointer)
445	AcpiOsFree(buf.Pointer);
446    return (ret);
447}
448
449static int
450acpi_ec_attach(device_t dev)
451{
452    struct acpi_ec_softc	*sc;
453    struct acpi_ec_params	*params;
454    ACPI_STATUS			Status;
455
456    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
457
458    /* Fetch/initialize softc (assumes softc is pre-zeroed). */
459    sc = device_get_softc(dev);
460    params = acpi_get_private(dev);
461    sc->ec_dev = dev;
462    sc->ec_handle = acpi_get_handle(dev);
463
464    /* Retrieve previously probed values via device ivars. */
465    sc->ec_glk = params->glk;
466    sc->ec_gpebit = params->gpe_bit;
467    sc->ec_gpehandle = params->gpe_handle;
468    sc->ec_uid = params->uid;
469    sc->ec_suspending = FALSE;
470    acpi_set_private(dev, NULL);
471    free(params, M_TEMP);
472
473    /* Attach bus resources for data and command/status ports. */
474    sc->ec_data_rid = 0;
475    sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
476			&sc->ec_data_rid, RF_ACTIVE);
477    if (sc->ec_data_res == NULL) {
478	device_printf(dev, "can't allocate data port\n");
479	goto error;
480    }
481    sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
482    sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
483
484    sc->ec_csr_rid = 1;
485    sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
486			&sc->ec_csr_rid, RF_ACTIVE);
487    if (sc->ec_csr_res == NULL) {
488	device_printf(dev, "can't allocate command/status port\n");
489	goto error;
490    }
491    sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
492    sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
493
494    /*
495     * Install a handler for this EC's GPE bit.  We want edge-triggered
496     * behavior.
497     */
498    ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
499    Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
500		ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, sc);
501    if (ACPI_FAILURE(Status)) {
502	device_printf(dev, "can't install GPE handler for %s - %s\n",
503		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
504	goto error;
505    }
506
507    /*
508     * Install address space handler
509     */
510    ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
511    Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
512		&EcSpaceHandler, &EcSpaceSetup, sc);
513    if (ACPI_FAILURE(Status)) {
514	device_printf(dev, "can't install address space handler for %s - %s\n",
515		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
516	goto error;
517    }
518
519    /* Enable runtime GPEs for the handler. */
520    Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit);
521    if (ACPI_FAILURE(Status)) {
522	device_printf(dev, "AcpiEnableGpe failed: %s\n",
523		      AcpiFormatException(Status));
524	goto error;
525    }
526
527    ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
528    return (0);
529
530error:
531    AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, EcGpeHandler);
532    AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
533	EcSpaceHandler);
534    if (sc->ec_csr_res)
535	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
536			     sc->ec_csr_res);
537    if (sc->ec_data_res)
538	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
539			     sc->ec_data_res);
540    return (ENXIO);
541}
542
543static int
544acpi_ec_suspend(device_t dev)
545{
546    struct acpi_ec_softc	*sc;
547
548    sc = device_get_softc(dev);
549    sc->ec_suspending = TRUE;
550    return (0);
551}
552
553static int
554acpi_ec_resume(device_t dev)
555{
556    struct acpi_ec_softc	*sc;
557
558    sc = device_get_softc(dev);
559    sc->ec_suspending = FALSE;
560    return (0);
561}
562
563static int
564acpi_ec_shutdown(device_t dev)
565{
566    struct acpi_ec_softc	*sc;
567
568    /* Disable the GPE so we don't get EC events during shutdown. */
569    sc = device_get_softc(dev);
570    AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit);
571    return (0);
572}
573
574/* Methods to allow other devices (e.g., smbat) to read/write EC space. */
575static int
576acpi_ec_read_method(device_t dev, u_int addr, UINT64 *val, int width)
577{
578    struct acpi_ec_softc *sc;
579    ACPI_STATUS status;
580
581    sc = device_get_softc(dev);
582    status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL);
583    if (ACPI_FAILURE(status))
584	return (ENXIO);
585    return (0);
586}
587
588static int
589acpi_ec_write_method(device_t dev, u_int addr, UINT64 val, int width)
590{
591    struct acpi_ec_softc *sc;
592    ACPI_STATUS status;
593
594    sc = device_get_softc(dev);
595    status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL);
596    if (ACPI_FAILURE(status))
597	return (ENXIO);
598    return (0);
599}
600
601static ACPI_STATUS
602EcCheckStatus(struct acpi_ec_softc *sc, const char *msg, EC_EVENT event)
603{
604    ACPI_STATUS status;
605    EC_STATUS ec_status;
606
607    status = AE_NO_HARDWARE_RESPONSE;
608    ec_status = EC_GET_CSR(sc);
609    if (sc->ec_burstactive && !(ec_status & EC_FLAG_BURST_MODE)) {
610	CTR1(KTR_ACPI, "ec burst disabled in waitevent (%s)", msg);
611	sc->ec_burstactive = FALSE;
612    }
613    if (EVENT_READY(event, ec_status)) {
614	CTR2(KTR_ACPI, "ec %s wait ready, status %#x", msg, ec_status);
615	status = AE_OK;
616    }
617    return (status);
618}
619
620static void
621EcGpeQueryHandlerSub(struct acpi_ec_softc *sc)
622{
623    UINT8			Data;
624    ACPI_STATUS			Status;
625    int				retry;
626    char			qxx[5];
627
628    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
629
630    /* Serialize user access with EcSpaceHandler(). */
631    Status = EcLock(sc);
632    if (ACPI_FAILURE(Status)) {
633	device_printf(sc->ec_dev, "GpeQuery lock error: %s\n",
634	    AcpiFormatException(Status));
635	return;
636    }
637
638    /*
639     * Send a query command to the EC to find out which _Qxx call it
640     * wants to make.  This command clears the SCI bit and also the
641     * interrupt source since we are edge-triggered.  To prevent the GPE
642     * that may arise from running the query from causing another query
643     * to be queued, we clear the pending flag only after running it.
644     */
645    for (retry = 0; retry < 2; retry++) {
646	Status = EcCommand(sc, EC_COMMAND_QUERY);
647	if (ACPI_SUCCESS(Status))
648	    break;
649	if (ACPI_FAILURE(EcCheckStatus(sc, "retr_check",
650	    EC_EVENT_INPUT_BUFFER_EMPTY)))
651	    break;
652    }
653    if (ACPI_FAILURE(Status)) {
654	EcUnlock(sc);
655	device_printf(sc->ec_dev, "GPE query failed: %s\n",
656	    AcpiFormatException(Status));
657	return;
658    }
659    Data = EC_GET_DATA(sc);
660
661    /*
662     * We have to unlock before running the _Qxx method below since that
663     * method may attempt to read/write from EC address space, causing
664     * recursive acquisition of the lock.
665     */
666    EcUnlock(sc);
667
668    /* Ignore the value for "no outstanding event". (13.3.5) */
669    CTR2(KTR_ACPI, "ec query ok,%s running _Q%02X", Data ? "" : " not", Data);
670    if (Data == 0)
671	return;
672
673    /* Evaluate _Qxx to respond to the controller. */
674    snprintf(qxx, sizeof(qxx), "_Q%02X", Data);
675    AcpiUtStrupr(qxx);
676    Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
677    if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
678	device_printf(sc->ec_dev, "evaluation of query method %s failed: %s\n",
679	    qxx, AcpiFormatException(Status));
680    }
681}
682
683static void
684EcGpeQueryHandler(void *Context)
685{
686    struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context;
687    int pending;
688
689    KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
690
691    do {
692	/* Read the current pending count */
693	pending = atomic_load_acq_int(&sc->ec_sci_pend);
694
695	/* Call GPE handler function */
696	EcGpeQueryHandlerSub(sc);
697
698	/*
699	 * Try to reset the pending count to zero. If this fails we
700	 * know another GPE event has occurred while handling the
701	 * current GPE event and need to loop.
702	 */
703    } while (!atomic_cmpset_int(&sc->ec_sci_pend, pending, 0));
704}
705
706/*
707 * The GPE handler is called when IBE/OBF or SCI events occur.  We are
708 * called from an unknown lock context.
709 */
710static UINT32
711EcGpeHandler(ACPI_HANDLE GpeDevice, UINT32 GpeNumber, void *Context)
712{
713    struct acpi_ec_softc *sc = Context;
714    ACPI_STATUS		       Status;
715    EC_STATUS		       EcStatus;
716
717    KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
718    CTR0(KTR_ACPI, "ec gpe handler start");
719
720    /*
721     * Notify EcWaitEvent() that the status register is now fresh.  If we
722     * didn't do this, it wouldn't be possible to distinguish an old IBE
723     * from a new one, for example when doing a write transaction (writing
724     * address and then data values.)
725     */
726    atomic_add_int(&sc->ec_gencount, 1);
727    wakeup(sc);
728
729    /*
730     * If the EC_SCI bit of the status register is set, queue a query handler.
731     * It will run the query and _Qxx method later, under the lock.
732     */
733    EcStatus = EC_GET_CSR(sc);
734    if ((EcStatus & EC_EVENT_SCI) &&
735	atomic_fetchadd_int(&sc->ec_sci_pend, 1) == 0) {
736	CTR0(KTR_ACPI, "ec gpe queueing query handler");
737	Status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler, Context);
738	if (ACPI_FAILURE(Status)) {
739	    printf("EcGpeHandler: queuing GPE query handler failed\n");
740	    atomic_store_rel_int(&sc->ec_sci_pend, 0);
741	}
742    }
743    return (ACPI_REENABLE_GPE);
744}
745
746static ACPI_STATUS
747EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
748	     void **RegionContext)
749{
750
751    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
752
753    /*
754     * If deactivating a region, always set the output to NULL.  Otherwise,
755     * just pass the context through.
756     */
757    if (Function == ACPI_REGION_DEACTIVATE)
758	*RegionContext = NULL;
759    else
760	*RegionContext = Context;
761
762    return_ACPI_STATUS (AE_OK);
763}
764
765static ACPI_STATUS
766EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 Width,
767	       UINT64 *Value, void *Context, void *RegionContext)
768{
769    struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
770    ACPI_PHYSICAL_ADDRESS	EcAddr;
771    UINT8			*EcData;
772    ACPI_STATUS			Status;
773
774    ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
775
776    if (Function != ACPI_READ && Function != ACPI_WRITE)
777	return_ACPI_STATUS (AE_BAD_PARAMETER);
778    if (Width % 8 != 0 || Value == NULL || Context == NULL)
779	return_ACPI_STATUS (AE_BAD_PARAMETER);
780    if (Address + Width / 8 > 256)
781	return_ACPI_STATUS (AE_BAD_ADDRESS);
782
783    /*
784     * If booting, check if we need to run the query handler.  If so, we
785     * we call it directly here since our thread taskq is not active yet.
786     */
787    if (cold || rebooting || sc->ec_suspending) {
788	if ((EC_GET_CSR(sc) & EC_EVENT_SCI) &&
789	    atomic_fetchadd_int(&sc->ec_sci_pend, 1) == 0) {
790	    CTR0(KTR_ACPI, "ec running gpe handler directly");
791	    EcGpeQueryHandler(sc);
792	}
793    }
794
795    /* Serialize with EcGpeQueryHandler() at transaction granularity. */
796    Status = EcLock(sc);
797    if (ACPI_FAILURE(Status))
798	return_ACPI_STATUS (Status);
799
800    /* If we can't start burst mode, continue anyway. */
801    Status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
802    if (ACPI_SUCCESS(Status)) {
803	if (EC_GET_DATA(sc) == EC_BURST_ACK) {
804	    CTR0(KTR_ACPI, "ec burst enabled");
805	    sc->ec_burstactive = TRUE;
806	}
807    }
808
809    /* Perform the transaction(s), based on Width. */
810    EcAddr = Address;
811    EcData = (UINT8 *)Value;
812    if (Function == ACPI_READ)
813	*Value = 0;
814    do {
815	switch (Function) {
816	case ACPI_READ:
817	    Status = EcRead(sc, EcAddr, EcData);
818	    break;
819	case ACPI_WRITE:
820	    Status = EcWrite(sc, EcAddr, *EcData);
821	    break;
822	}
823	if (ACPI_FAILURE(Status))
824	    break;
825	EcAddr++;
826	EcData++;
827    } while (EcAddr < Address + Width / 8);
828
829    if (sc->ec_burstactive) {
830	sc->ec_burstactive = FALSE;
831	if (ACPI_SUCCESS(EcCommand(sc, EC_COMMAND_BURST_DISABLE)))
832	    CTR0(KTR_ACPI, "ec disabled burst ok");
833    }
834
835    EcUnlock(sc);
836    return_ACPI_STATUS (Status);
837}
838
839static ACPI_STATUS
840EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event, u_int gen_count)
841{
842    static int	no_intr = 0;
843    ACPI_STATUS	Status;
844    int		count, i, need_poll, slp_ival;
845
846    ACPI_SERIAL_ASSERT(ec);
847    Status = AE_NO_HARDWARE_RESPONSE;
848    need_poll = cold || rebooting || ec_polled_mode || sc->ec_suspending;
849
850    /* Wait for event by polling or GPE (interrupt). */
851    if (need_poll) {
852	count = (ec_timeout * 1000) / EC_POLL_DELAY;
853	if (count == 0)
854	    count = 1;
855	DELAY(10);
856	for (i = 0; i < count; i++) {
857	    Status = EcCheckStatus(sc, "poll", Event);
858	    if (ACPI_SUCCESS(Status))
859		break;
860	    DELAY(EC_POLL_DELAY);
861	}
862    } else {
863	slp_ival = hz / 1000;
864	if (slp_ival != 0) {
865	    count = ec_timeout;
866	} else {
867	    /* hz has less than 1 ms resolution so scale timeout. */
868	    slp_ival = 1;
869	    count = ec_timeout / (1000 / hz);
870	}
871
872	/*
873	 * Wait for the GPE to signal the status changed, checking the
874	 * status register each time we get one.  It's possible to get a
875	 * GPE for an event we're not interested in here (i.e., SCI for
876	 * EC query).
877	 */
878	for (i = 0; i < count; i++) {
879	    if (gen_count == sc->ec_gencount)
880		tsleep(sc, 0, "ecgpe", slp_ival);
881	    /*
882	     * Record new generation count.  It's possible the GPE was
883	     * just to notify us that a query is needed and we need to
884	     * wait for a second GPE to signal the completion of the
885	     * event we are actually waiting for.
886	     */
887	    Status = EcCheckStatus(sc, "sleep", Event);
888	    if (ACPI_SUCCESS(Status)) {
889		if (gen_count == sc->ec_gencount)
890		    no_intr++;
891		else
892		    no_intr = 0;
893		break;
894	    }
895	    gen_count = sc->ec_gencount;
896	}
897
898	/*
899	 * We finished waiting for the GPE and it never arrived.  Try to
900	 * read the register once and trust whatever value we got.  This is
901	 * the best we can do at this point.
902	 */
903	if (ACPI_FAILURE(Status))
904	    Status = EcCheckStatus(sc, "sleep_end", Event);
905    }
906    if (!need_poll && no_intr > 10) {
907	device_printf(sc->ec_dev,
908	    "not getting interrupts, switched to polled mode\n");
909	ec_polled_mode = 1;
910    }
911    if (ACPI_FAILURE(Status))
912	    CTR0(KTR_ACPI, "error: ec wait timed out");
913    return (Status);
914}
915
916static ACPI_STATUS
917EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
918{
919    ACPI_STATUS	status;
920    EC_EVENT	event;
921    EC_STATUS	ec_status;
922    u_int	gen_count;
923
924    ACPI_SERIAL_ASSERT(ec);
925
926    /* Don't use burst mode if user disabled it. */
927    if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE)
928	return (AE_ERROR);
929
930    /* Decide what to wait for based on command type. */
931    switch (cmd) {
932    case EC_COMMAND_READ:
933    case EC_COMMAND_WRITE:
934    case EC_COMMAND_BURST_DISABLE:
935	event = EC_EVENT_INPUT_BUFFER_EMPTY;
936	break;
937    case EC_COMMAND_QUERY:
938    case EC_COMMAND_BURST_ENABLE:
939	event = EC_EVENT_OUTPUT_BUFFER_FULL;
940	break;
941    default:
942	device_printf(sc->ec_dev, "EcCommand: invalid command %#x\n", cmd);
943	return (AE_BAD_PARAMETER);
944    }
945
946    /*
947     * Ensure empty input buffer before issuing command.
948     * Use generation count of zero to force a quick check.
949     */
950    status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, 0);
951    if (ACPI_FAILURE(status))
952	return (status);
953
954    /* Run the command and wait for the chosen event. */
955    CTR1(KTR_ACPI, "ec running command %#x", cmd);
956    gen_count = sc->ec_gencount;
957    EC_SET_CSR(sc, cmd);
958    status = EcWaitEvent(sc, event, gen_count);
959    if (ACPI_SUCCESS(status)) {
960	/* If we succeeded, burst flag should now be present. */
961	if (cmd == EC_COMMAND_BURST_ENABLE) {
962	    ec_status = EC_GET_CSR(sc);
963	    if ((ec_status & EC_FLAG_BURST_MODE) == 0)
964		status = AE_ERROR;
965	}
966    } else
967	device_printf(sc->ec_dev, "EcCommand: no response to %#x\n", cmd);
968    return (status);
969}
970
971static ACPI_STATUS
972EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
973{
974    ACPI_STATUS	status;
975    u_int gen_count;
976    int retry;
977
978    ACPI_SERIAL_ASSERT(ec);
979    CTR1(KTR_ACPI, "ec read from %#x", Address);
980
981    for (retry = 0; retry < 2; retry++) {
982	status = EcCommand(sc, EC_COMMAND_READ);
983	if (ACPI_FAILURE(status))
984	    return (status);
985
986	gen_count = sc->ec_gencount;
987	EC_SET_DATA(sc, Address);
988	status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL, gen_count);
989	if (ACPI_SUCCESS(status)) {
990	    *Data = EC_GET_DATA(sc);
991	    return (AE_OK);
992	}
993	if (ACPI_FAILURE(EcCheckStatus(sc, "retr_check",
994	    EC_EVENT_INPUT_BUFFER_EMPTY)))
995	    break;
996    }
997    device_printf(sc->ec_dev, "EcRead: failed waiting to get data\n");
998    return (status);
999}
1000
1001static ACPI_STATUS
1002EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 Data)
1003{
1004    ACPI_STATUS	status;
1005    u_int gen_count;
1006
1007    ACPI_SERIAL_ASSERT(ec);
1008    CTR2(KTR_ACPI, "ec write to %#x, data %#x", Address, Data);
1009
1010    status = EcCommand(sc, EC_COMMAND_WRITE);
1011    if (ACPI_FAILURE(status))
1012	return (status);
1013
1014    gen_count = sc->ec_gencount;
1015    EC_SET_DATA(sc, Address);
1016    status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1017    if (ACPI_FAILURE(status)) {
1018	device_printf(sc->ec_dev, "EcWrite: failed waiting for sent address\n");
1019	return (status);
1020    }
1021
1022    gen_count = sc->ec_gencount;
1023    EC_SET_DATA(sc, Data);
1024    status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1025    if (ACPI_FAILURE(status)) {
1026	device_printf(sc->ec_dev, "EcWrite: failed waiting for sent data\n");
1027	return (status);
1028    }
1029
1030    return (AE_OK);
1031}
1032