167754Smsmith/******************************************************************************
267754Smsmith *
367754Smsmith * Module Name: evxfevnt - External Interfaces, ACPI event disable/enable
467754Smsmith *
567754Smsmith *****************************************************************************/
667754Smsmith
7217365Sjkim/*
8281075Sdim * Copyright (C) 2000 - 2015, Intel Corp.
970243Smsmith * All rights reserved.
1067754Smsmith *
11217365Sjkim * Redistribution and use in source and binary forms, with or without
12217365Sjkim * modification, are permitted provided that the following conditions
13217365Sjkim * are met:
14217365Sjkim * 1. Redistributions of source code must retain the above copyright
15217365Sjkim *    notice, this list of conditions, and the following disclaimer,
16217365Sjkim *    without modification.
17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18217365Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
19217365Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
20217365Sjkim *    including a substantially similar Disclaimer requirement for further
21217365Sjkim *    binary redistribution.
22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
23217365Sjkim *    of any contributors may be used to endorse or promote products derived
24217365Sjkim *    from this software without specific prior written permission.
2567754Smsmith *
26217365Sjkim * Alternatively, this software may be distributed under the terms of the
27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
28217365Sjkim * Software Foundation.
2967754Smsmith *
30217365Sjkim * NO WARRANTY
31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41217365Sjkim * POSSIBILITY OF SUCH DAMAGES.
42217365Sjkim */
4367754Smsmith
44281075Sdim#define EXPORT_ACPI_INTERFACES
4567754Smsmith
46193341Sjkim#include <contrib/dev/acpica/include/acpi.h>
47193341Sjkim#include <contrib/dev/acpica/include/accommon.h>
48193341Sjkim#include <contrib/dev/acpica/include/actables.h>
4967754Smsmith
5077424Smsmith#define _COMPONENT          ACPI_EVENTS
5191116Smsmith        ACPI_MODULE_NAME    ("evxfevnt")
5267754Smsmith
5367754Smsmith
54231844Sjkim#if (!ACPI_REDUCED_HARDWARE) /* Entire module */
5577424Smsmith/*******************************************************************************
5667754Smsmith *
5767754Smsmith * FUNCTION:    AcpiEnable
5867754Smsmith *
5967754Smsmith * PARAMETERS:  None
6067754Smsmith *
6167754Smsmith * RETURN:      Status
6267754Smsmith *
6367754Smsmith * DESCRIPTION: Transfers the system into ACPI mode.
6467754Smsmith *
6577424Smsmith ******************************************************************************/
6667754Smsmith
6767754SmsmithACPI_STATUS
68151937SjkimAcpiEnable (
69151937Sjkim    void)
7067754Smsmith{
7187031Smsmith    ACPI_STATUS             Status = AE_OK;
7267754Smsmith
7367754Smsmith
74167802Sjkim    ACPI_FUNCTION_TRACE (AcpiEnable);
7567754Smsmith
7667754Smsmith
77167802Sjkim    /* ACPI tables must be present */
7867754Smsmith
79167802Sjkim    if (!AcpiTbTablesLoaded ())
8067754Smsmith    {
8167754Smsmith        return_ACPI_STATUS (AE_NO_ACPI_TABLES);
8267754Smsmith    }
8367754Smsmith
84246849Sjkim    /* If the Hardware Reduced flag is set, machine is always in acpi mode */
85246849Sjkim
86246849Sjkim    if (AcpiGbl_ReducedHardware)
87246849Sjkim    {
88246849Sjkim        return_ACPI_STATUS (AE_OK);
89246849Sjkim    }
90246849Sjkim
91167802Sjkim    /* Check current mode */
92167802Sjkim
93102550Siwasaki    if (AcpiHwGetMode() == ACPI_SYS_MODE_ACPI)
9467754Smsmith    {
95114237Snjl        ACPI_DEBUG_PRINT ((ACPI_DB_INIT, "System is already in ACPI mode\n"));
9667754Smsmith    }
9787031Smsmith    else
9887031Smsmith    {
9987031Smsmith        /* Transition to ACPI mode */
10067754Smsmith
10191116Smsmith        Status = AcpiHwSetMode (ACPI_SYS_MODE_ACPI);
10287031Smsmith        if (ACPI_FAILURE (Status))
10387031Smsmith        {
104167802Sjkim            ACPI_ERROR ((AE_INFO, "Could not transition to ACPI mode"));
10587031Smsmith            return_ACPI_STATUS (Status);
10687031Smsmith        }
10787031Smsmith
108151937Sjkim        ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
109151937Sjkim            "Transition to ACPI mode successful\n"));
11067754Smsmith    }
11167754Smsmith
11267754Smsmith    return_ACPI_STATUS (Status);
11367754Smsmith}
11467754Smsmith
115167802SjkimACPI_EXPORT_SYMBOL (AcpiEnable)
11667754Smsmith
117167802Sjkim
11877424Smsmith/*******************************************************************************
11967754Smsmith *
12067754Smsmith * FUNCTION:    AcpiDisable
12167754Smsmith *
12267754Smsmith * PARAMETERS:  None
12367754Smsmith *
12467754Smsmith * RETURN:      Status
12567754Smsmith *
126151937Sjkim * DESCRIPTION: Transfers the system into LEGACY (non-ACPI) mode.
12767754Smsmith *
12877424Smsmith ******************************************************************************/
12967754Smsmith
13067754SmsmithACPI_STATUS
131151937SjkimAcpiDisable (
132151937Sjkim    void)
13367754Smsmith{
13487031Smsmith    ACPI_STATUS             Status = AE_OK;
13567754Smsmith
13667754Smsmith
137167802Sjkim    ACPI_FUNCTION_TRACE (AcpiDisable);
13867754Smsmith
139117521Snjl
140246849Sjkim    /* If the Hardware Reduced flag is set, machine is always in acpi mode */
141246849Sjkim
142246849Sjkim    if (AcpiGbl_ReducedHardware)
143246849Sjkim    {
144246849Sjkim        return_ACPI_STATUS (AE_OK);
145246849Sjkim    }
146246849Sjkim
147102550Siwasaki    if (AcpiHwGetMode() == ACPI_SYS_MODE_LEGACY)
14867754Smsmith    {
149151937Sjkim        ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
150151937Sjkim            "System is already in legacy (non-ACPI) mode\n"));
151102550Siwasaki    }
152102550Siwasaki    else
153102550Siwasaki    {
154102550Siwasaki        /* Transition to LEGACY mode */
155114237Snjl
156102550Siwasaki        Status = AcpiHwSetMode (ACPI_SYS_MODE_LEGACY);
15791116Smsmith
15887031Smsmith        if (ACPI_FAILURE (Status))
15987031Smsmith        {
160167802Sjkim            ACPI_ERROR ((AE_INFO,
161151937Sjkim                "Could not exit ACPI mode to legacy mode"));
16287031Smsmith            return_ACPI_STATUS (Status);
16387031Smsmith        }
164102550Siwasaki
165114237Snjl        ACPI_DEBUG_PRINT ((ACPI_DB_INIT, "ACPI mode disabled\n"));
16667754Smsmith    }
16767754Smsmith
16867754Smsmith    return_ACPI_STATUS (Status);
16967754Smsmith}
17067754Smsmith
171167802SjkimACPI_EXPORT_SYMBOL (AcpiDisable)
17267754Smsmith
173167802Sjkim
17477424Smsmith/*******************************************************************************
17567754Smsmith *
17667754Smsmith * FUNCTION:    AcpiEnableEvent
17767754Smsmith *
178117521Snjl * PARAMETERS:  Event           - The fixed eventto be enabled
179117521Snjl *              Flags           - Reserved
18067754Smsmith *
18167754Smsmith * RETURN:      Status
18267754Smsmith *
183117521Snjl * DESCRIPTION: Enable an ACPI event (fixed)
18467754Smsmith *
18567754Smsmith ******************************************************************************/
18667754Smsmith
18767754SmsmithACPI_STATUS
18867754SmsmithAcpiEnableEvent (
18967754Smsmith    UINT32                  Event,
19084491Smsmith    UINT32                  Flags)
19167754Smsmith{
19267754Smsmith    ACPI_STATUS             Status = AE_OK;
19399679Siwasaki    UINT32                  Value;
19467754Smsmith
19567754Smsmith
196167802Sjkim    ACPI_FUNCTION_TRACE (AcpiEnableEvent);
19767754Smsmith
19867754Smsmith
199117521Snjl    /* Decode the Fixed Event */
20067754Smsmith
201117521Snjl    if (Event > ACPI_EVENT_MAX)
20267754Smsmith    {
203117521Snjl        return_ACPI_STATUS (AE_BAD_PARAMETER);
204117521Snjl    }
20567754Smsmith
206117521Snjl    /*
207193267Sjkim     * Enable the requested fixed event (by writing a one to the enable
208193267Sjkim     * register bit)
209117521Snjl     */
210193267Sjkim    Status = AcpiWriteBitRegister (
211193267Sjkim                AcpiGbl_FixedEventInfo[Event].EnableRegisterId,
212193267Sjkim                ACPI_ENABLE_EVENT);
213117521Snjl    if (ACPI_FAILURE (Status))
214117521Snjl    {
215117521Snjl        return_ACPI_STATUS (Status);
216117521Snjl    }
21767754Smsmith
218117521Snjl    /* Make sure that the hardware responded */
21967754Smsmith
220193267Sjkim    Status = AcpiReadBitRegister (
221193267Sjkim                AcpiGbl_FixedEventInfo[Event].EnableRegisterId, &Value);
222117521Snjl    if (ACPI_FAILURE (Status))
223117521Snjl    {
224117521Snjl        return_ACPI_STATUS (Status);
225117521Snjl    }
22669450Smsmith
227117521Snjl    if (Value != 1)
228117521Snjl    {
229167802Sjkim        ACPI_ERROR ((AE_INFO,
230167802Sjkim            "Could not enable %s event", AcpiUtGetEventName (Event)));
231117521Snjl        return_ACPI_STATUS (AE_NO_HARDWARE_RESPONSE);
232117521Snjl    }
23391116Smsmith
234117521Snjl    return_ACPI_STATUS (Status);
235117521Snjl}
23699679Siwasaki
237167802SjkimACPI_EXPORT_SYMBOL (AcpiEnableEvent)
23867754Smsmith
239167802Sjkim
240117521Snjl/*******************************************************************************
241117521Snjl *
242129684Snjl * FUNCTION:    AcpiDisableEvent
243129684Snjl *
244216471Sjkim * PARAMETERS:  Event           - The fixed event to be disabled
245129684Snjl *              Flags           - Reserved
246129684Snjl *
247129684Snjl * RETURN:      Status
248129684Snjl *
249129684Snjl * DESCRIPTION: Disable an ACPI event (fixed)
250129684Snjl *
251129684Snjl ******************************************************************************/
252129684Snjl
253129684SnjlACPI_STATUS
254129684SnjlAcpiDisableEvent (
255129684Snjl    UINT32                  Event,
256129684Snjl    UINT32                  Flags)
257129684Snjl{
258129684Snjl    ACPI_STATUS             Status = AE_OK;
259129684Snjl    UINT32                  Value;
260129684Snjl
261129684Snjl
262167802Sjkim    ACPI_FUNCTION_TRACE (AcpiDisableEvent);
263129684Snjl
264129684Snjl
265129684Snjl    /* Decode the Fixed Event */
266129684Snjl
267129684Snjl    if (Event > ACPI_EVENT_MAX)
268129684Snjl    {
269129684Snjl        return_ACPI_STATUS (AE_BAD_PARAMETER);
270129684Snjl    }
271129684Snjl
272117521Snjl    /*
273193267Sjkim     * Disable the requested fixed event (by writing a zero to the enable
274193267Sjkim     * register bit)
275117521Snjl     */
276193267Sjkim    Status = AcpiWriteBitRegister (
277193267Sjkim                AcpiGbl_FixedEventInfo[Event].EnableRegisterId,
278193267Sjkim                ACPI_DISABLE_EVENT);
279129684Snjl    if (ACPI_FAILURE (Status))
280117521Snjl    {
281129684Snjl        return_ACPI_STATUS (Status);
282117521Snjl    }
283129684Snjl
284193267Sjkim    Status = AcpiReadBitRegister (
285193267Sjkim                AcpiGbl_FixedEventInfo[Event].EnableRegisterId, &Value);
286129684Snjl    if (ACPI_FAILURE (Status))
287117521Snjl    {
288129684Snjl        return_ACPI_STATUS (Status);
289117521Snjl    }
290117521Snjl
291129684Snjl    if (Value != 0)
292117521Snjl    {
293167802Sjkim        ACPI_ERROR ((AE_INFO,
294167802Sjkim            "Could not disable %s events", AcpiUtGetEventName (Event)));
295129684Snjl        return_ACPI_STATUS (AE_NO_HARDWARE_RESPONSE);
296117521Snjl    }
297129684Snjl
29867754Smsmith    return_ACPI_STATUS (Status);
29967754Smsmith}
30067754Smsmith
301167802SjkimACPI_EXPORT_SYMBOL (AcpiDisableEvent)
30267754Smsmith
303167802Sjkim
30477424Smsmith/*******************************************************************************
30567754Smsmith *
30667754Smsmith * FUNCTION:    AcpiClearEvent
30767754Smsmith *
308117521Snjl * PARAMETERS:  Event           - The fixed event to be cleared
30967754Smsmith *
31067754Smsmith * RETURN:      Status
31167754Smsmith *
312117521Snjl * DESCRIPTION: Clear an ACPI event (fixed)
31367754Smsmith *
31467754Smsmith ******************************************************************************/
31567754Smsmith
31667754SmsmithACPI_STATUS
31767754SmsmithAcpiClearEvent (
318117521Snjl    UINT32                  Event)
31967754Smsmith{
32067754Smsmith    ACPI_STATUS             Status = AE_OK;
32167754Smsmith
32267754Smsmith
323167802Sjkim    ACPI_FUNCTION_TRACE (AcpiClearEvent);
32467754Smsmith
32567754Smsmith
326117521Snjl    /* Decode the Fixed Event */
32767754Smsmith
328117521Snjl    if (Event > ACPI_EVENT_MAX)
32967754Smsmith    {
330117521Snjl        return_ACPI_STATUS (AE_BAD_PARAMETER);
331117521Snjl    }
33267754Smsmith
333117521Snjl    /*
334193267Sjkim     * Clear the requested fixed event (By writing a one to the status
335193267Sjkim     * register bit)
336117521Snjl     */
337193267Sjkim    Status = AcpiWriteBitRegister (
338193267Sjkim                AcpiGbl_FixedEventInfo[Event].StatusRegisterId,
339193267Sjkim                ACPI_CLEAR_STATUS);
34067754Smsmith
341117521Snjl    return_ACPI_STATUS (Status);
342117521Snjl}
34367754Smsmith
344167802SjkimACPI_EXPORT_SYMBOL (AcpiClearEvent)
34567754Smsmith
346167802Sjkim
347117521Snjl/*******************************************************************************
348117521Snjl *
34967754Smsmith * FUNCTION:    AcpiGetEventStatus
35067754Smsmith *
351117521Snjl * PARAMETERS:  Event           - The fixed event
352151937Sjkim *              EventStatus     - Where the current status of the event will
35367754Smsmith *                                be returned
35467754Smsmith *
35567754Smsmith * RETURN:      Status
35667754Smsmith *
35767754Smsmith * DESCRIPTION: Obtains and returns the current status of the event
35867754Smsmith *
35967754Smsmith ******************************************************************************/
36067754Smsmith
36167754SmsmithACPI_STATUS
36267754SmsmithAcpiGetEventStatus (
36367754Smsmith    UINT32                  Event,
36467754Smsmith    ACPI_EVENT_STATUS       *EventStatus)
36567754Smsmith{
366281075Sdim    ACPI_STATUS             Status;
367281075Sdim    ACPI_EVENT_STATUS       LocalEventStatus = 0;
368281075Sdim    UINT32                  InByte;
36967754Smsmith
37067754Smsmith
371167802Sjkim    ACPI_FUNCTION_TRACE (AcpiGetEventStatus);
37267754Smsmith
37367754Smsmith
37467754Smsmith    if (!EventStatus)
37567754Smsmith    {
37667754Smsmith        return_ACPI_STATUS (AE_BAD_PARAMETER);
37767754Smsmith    }
37867754Smsmith
379117521Snjl    /* Decode the Fixed Event */
38067754Smsmith
381117521Snjl    if (Event > ACPI_EVENT_MAX)
38267754Smsmith    {
383117521Snjl        return_ACPI_STATUS (AE_BAD_PARAMETER);
384117521Snjl    }
38567754Smsmith
386281075Sdim    /* Fixed event currently can be dispatched? */
38767754Smsmith
388281075Sdim    if (AcpiGbl_FixedEventHandlers[Event].Handler)
389281075Sdim    {
390281075Sdim        LocalEventStatus |= ACPI_EVENT_FLAG_HAS_HANDLER;
391281075Sdim    }
392281075Sdim
393281075Sdim    /* Fixed event currently enabled? */
394281075Sdim
395193267Sjkim    Status = AcpiReadBitRegister (
396281075Sdim                AcpiGbl_FixedEventInfo[Event].EnableRegisterId, &InByte);
397281075Sdim    if (ACPI_FAILURE (Status))
398281075Sdim    {
399281075Sdim        return_ACPI_STATUS (Status);
400281075Sdim    }
401117521Snjl
402281075Sdim    if (InByte)
403281075Sdim    {
404281687Sjkim        LocalEventStatus |=
405281687Sjkim            (ACPI_EVENT_FLAG_ENABLED | ACPI_EVENT_FLAG_ENABLE_SET);
406281075Sdim    }
407281075Sdim
408281075Sdim    /* Fixed event currently active? */
409281075Sdim
410281075Sdim    Status = AcpiReadBitRegister (
411281075Sdim                AcpiGbl_FixedEventInfo[Event].StatusRegisterId, &InByte);
412281075Sdim    if (ACPI_FAILURE (Status))
413281075Sdim    {
414281075Sdim        return_ACPI_STATUS (Status);
415281075Sdim    }
416281075Sdim
417281075Sdim    if (InByte)
418281075Sdim    {
419281687Sjkim        LocalEventStatus |= ACPI_EVENT_FLAG_STATUS_SET;
420281075Sdim    }
421281075Sdim
422281075Sdim    (*EventStatus) = LocalEventStatus;
423281075Sdim    return_ACPI_STATUS (AE_OK);
424117521Snjl}
425117521Snjl
426167802SjkimACPI_EXPORT_SYMBOL (AcpiGetEventStatus)
427117521Snjl
428231844Sjkim#endif /* !ACPI_REDUCED_HARDWARE */
429