blob: 386d3900d22154921881d4988a15990d259e1193 [file] [log] [blame]
Edward O'Callaghan32960e32014-11-23 17:38:52 +11001/*
2 * SMI handler -- mostly takes care of SMIs from the EC
3 *
4 * Copyright (C) 2014 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5 * Subject to the GNU GPL v2, or (at your option) any later version.
6 */
7#include "ec.h"
8
9#include <arch/io.h>
10#include <console/console.h>
11#include <cpu/x86/smm.h>
12#include <delay.h>
13#include <ec/compal/ene932/ec.h>
14#include <southbridge/amd/agesa/hudson/hudson.h>
15#include <southbridge/amd/agesa/hudson/smi.h>
16
17#define ACPI_PM1_CNT_SLEEP(state) ((1 << 13) | (state & 0x7) << 10)
18
19enum sleep_states {
20 S0 = 0,
21 S1 = 1,
22 S3 = 3,
23 S4 = 4,
24 S5 = 5,
25};
26
27enum ec_smi_event {
28 EC_SMI_EVENT_IDLE = 0x80,
29 EC_SMI_BATTERY_LOW = 0xb3,
30};
31
32/* Tell EC to operate in APM mode. Events generate SMIs instead of SCIs */
33static void ec_enter_apm_mode(void)
34{
35 ec_kbc_write_cmd(0x59);
36 ec_kbc_write_ib(0xE9);
37}
38/* Tell EC to operate in ACPI mode, thus generating SCIs on events, not SMIs */
39static void ec_enter_acpi_mode(void)
40{
41 ec_kbc_write_cmd(0x59);
42 ec_kbc_write_ib(0xE8);
43}
44
45static uint8_t ec_get_smi_event(void)
46{
47 ec_kbc_write_cmd(0x56);
48 return ec_kbc_read_ob();
49}
50
51static void ec_process_smi(uint8_t src)
52{
53 /* Reading the SMI source satisfies the EC in terms of responding to
54 * the event, regardless of whether we take an action or not.
55 */
56
57 switch (src) {
58 case EC_SMI_BATTERY_LOW:
59 printk(BIOS_DEBUG, "Battery low. Shutting down\n");
60 outl(ACPI_PM1_CNT_SLEEP(S5), ACPI_PM1_CNT_BLK);
61 break;
62 default:
63 printk(BIOS_DEBUG, "EC_SMI event 0x%x\n", src);
64 }
65}
66
67static void handle_ec_smi(void)
68{
69 uint8_t src;
70
71 while ((src = ec_get_smi_event()) != EC_SMI_EVENT_IDLE)
72 ec_process_smi(src);
73}
74
75static void handle_lid_smi(void)
76{
77 /* Only triggered in non-ACPI mode on lid close. */
78 outl(ACPI_PM1_CNT_SLEEP(S4), ACPI_PM1_CNT_BLK);
79}
80
81int mainboard_smi_apmc(uint8_t data)
82{
83 switch (data) {
84 case ACPI_SMI_CMD_ENABLE:
85 printk(BIOS_DEBUG, "Enable ACPI mode\n");
86 ec_enter_acpi_mode();
87 hudson_disable_gevent_smi(EC_LID_GEVENT);
88 break;
89 case ACPI_SMI_CMD_DISABLE:
90 printk(BIOS_DEBUG, "Disable ACPI mode\n");
91 ec_enter_apm_mode();
92 hudson_configure_gevent_smi(EC_LID_GEVENT, SMI_MODE_SMI,
93 SMI_LVL_LOW);
94 break;
95 default:
96 printk(BIOS_DEBUG, "Unhandled ACPI command: 0x%x\n", data);
97 }
98 return 0;
99}
100
101void mainboard_smi_gpi(uint32_t gpi_sts)
102{
103 if (gpi_sts & (1 << EC_SMI_GEVENT))
104 handle_ec_smi();
105 if (gpi_sts & (1 << EC_LID_GEVENT))
106 handle_lid_smi();
107}