ec/acpi: Rework to reduce code duplication
- Move EC send/receive polling code to their own functions - Add named constants for poll timeouts and delay interval - Use human-readable timeout values - Add `send`/`recv` functions which support custom timeouts - Remove extra 10us delays between polling and performing a given transaction - Use constants from `ec.h` for standard EC command opcodes Tested on a Lenovo Edge E530, which takes similar code paths to the Lenovo Twist S230u. Change-Id: Ifda5c030ff81f1046be58aa1fcafdcf71a27cd41 Signed-off-by: Abel Briggs <abelbriggs1@hotmail.com> Reviewed-on: https://review.coreboot.org/c/coreboot/+/64012 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Lean Sheng Tan <sheng.tan@9elements.com>
This commit is contained in:
committed by
Paul Fagerburg
parent
1f52edb093
commit
c3bfbafda5
105
src/ec/acpi/ec.c
105
src/ec/acpi/ec.c
@@ -1,62 +1,73 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
|
||||
#include <console/console.h>
|
||||
#include <device/device.h>
|
||||
#include <arch/io.h>
|
||||
#include <delay.h>
|
||||
#include <stdint.h>
|
||||
#include <types.h>
|
||||
#include "ec.h"
|
||||
|
||||
#define EC_POLL_DELAY_US 10
|
||||
#define EC_SEND_TIMEOUT_US 20000 // 20ms
|
||||
#define EC_RECV_TIMEOUT_US 320000 // 320ms
|
||||
|
||||
static u16 ec_cmd_reg = EC_SC;
|
||||
static u16 ec_data_reg = EC_DATA;
|
||||
|
||||
/*
|
||||
* Poll the EC status/command register for a specified
|
||||
* state until the given timeout elapses.
|
||||
*/
|
||||
static int wait_ec_sc(int timeout_us, u8 mask, u8 state)
|
||||
{
|
||||
while (timeout_us > 0 && (inb(ec_cmd_reg) & mask) != state) {
|
||||
udelay(EC_POLL_DELAY_US);
|
||||
timeout_us -= EC_POLL_DELAY_US;
|
||||
}
|
||||
|
||||
return timeout_us > 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
bool ec_ready_send(int timeout_us)
|
||||
{
|
||||
return wait_ec_sc(timeout_us, EC_IBF, 0) == 0;
|
||||
}
|
||||
|
||||
bool ec_ready_recv(int timeout_us)
|
||||
{
|
||||
return wait_ec_sc(timeout_us, EC_OBF, EC_OBF) == 0;
|
||||
}
|
||||
|
||||
int send_ec_command(u8 command)
|
||||
{
|
||||
int timeout;
|
||||
return send_ec_command_timeout(command, EC_SEND_TIMEOUT_US);
|
||||
}
|
||||
|
||||
timeout = 0x7ff;
|
||||
while ((inb(ec_cmd_reg) & EC_IBF) && --timeout) {
|
||||
udelay(10);
|
||||
if ((timeout & 0xff) == 0)
|
||||
printk(BIOS_SPEW, ".");
|
||||
}
|
||||
if (!timeout) {
|
||||
int send_ec_command_timeout(u8 command, int timeout_us)
|
||||
{
|
||||
if (!ec_ready_send(timeout_us)) {
|
||||
printk(BIOS_DEBUG, "Timeout while sending command 0x%02x to EC!\n",
|
||||
command);
|
||||
// return -1;
|
||||
}
|
||||
|
||||
udelay(10);
|
||||
|
||||
outb(command, ec_cmd_reg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int send_ec_data(u8 data)
|
||||
{
|
||||
int timeout;
|
||||
return send_ec_data_timeout(data, EC_SEND_TIMEOUT_US);
|
||||
}
|
||||
|
||||
timeout = 0x7ff;
|
||||
while ((inb(ec_cmd_reg) & EC_IBF) && --timeout) { // wait for IBF = 0
|
||||
udelay(10);
|
||||
if ((timeout & 0xff) == 0)
|
||||
printk(BIOS_SPEW, ".");
|
||||
}
|
||||
if (!timeout) {
|
||||
int send_ec_data_timeout(u8 data, int timeout_us)
|
||||
{
|
||||
if (!ec_ready_send(timeout_us)) {
|
||||
printk(BIOS_DEBUG, "Timeout while sending data 0x%02x to EC!\n",
|
||||
data);
|
||||
// return -1;
|
||||
}
|
||||
|
||||
udelay(10);
|
||||
|
||||
outb(data, ec_data_reg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int send_ec_data_nowait(u8 data)
|
||||
{
|
||||
outb(data, ec_data_reg);
|
||||
|
||||
return 0;
|
||||
@@ -64,25 +75,18 @@ int send_ec_data_nowait(u8 data)
|
||||
|
||||
u8 recv_ec_data(void)
|
||||
{
|
||||
int timeout;
|
||||
return recv_ec_data_timeout(EC_RECV_TIMEOUT_US);
|
||||
}
|
||||
|
||||
u8 recv_ec_data_timeout(int timeout_us)
|
||||
{
|
||||
u8 data;
|
||||
|
||||
timeout = 0x7fff;
|
||||
while (--timeout) { // Wait for OBF = 1
|
||||
if (inb(ec_cmd_reg) & EC_OBF) {
|
||||
break;
|
||||
}
|
||||
udelay(10);
|
||||
if ((timeout & 0xff) == 0)
|
||||
printk(BIOS_SPEW, ".");
|
||||
}
|
||||
if (!timeout) {
|
||||
printk(BIOS_DEBUG, "\nTimeout while receiving data from EC!\n");
|
||||
if (!ec_ready_recv(timeout_us)) {
|
||||
printk(BIOS_DEBUG, "Timeout while receiving data from EC!\n");
|
||||
// return -1;
|
||||
}
|
||||
|
||||
udelay(10);
|
||||
|
||||
data = inb(ec_data_reg);
|
||||
printk(BIOS_SPEW, "%s: 0x%02x\n", __func__, data);
|
||||
|
||||
@@ -91,14 +95,15 @@ u8 recv_ec_data(void)
|
||||
|
||||
void ec_clear_out_queue(void)
|
||||
{
|
||||
int timeout = 0x7fff;
|
||||
int timeout = EC_RECV_TIMEOUT_US;
|
||||
printk(BIOS_SPEW, "Clearing EC output queue...\n");
|
||||
while (--timeout && (inb(ec_cmd_reg) & EC_OBF)) {
|
||||
while (timeout > 0 && inb(ec_cmd_reg) & EC_OBF) {
|
||||
u8 data = inb(ec_data_reg);
|
||||
printk(BIOS_SPEW, "Discarding a garbage byte: 0x%02x\n", data);
|
||||
udelay(10);
|
||||
udelay(EC_POLL_DELAY_US);
|
||||
timeout -= EC_POLL_DELAY_US;
|
||||
}
|
||||
if (!timeout)
|
||||
if (timeout <= 0)
|
||||
printk(BIOS_ERR, "Timeout while clearing EC output queue!\n");
|
||||
else
|
||||
printk(BIOS_SPEW, "EC output queue has been cleared.\n");
|
||||
@@ -106,7 +111,7 @@ void ec_clear_out_queue(void)
|
||||
|
||||
u8 ec_read(u8 addr)
|
||||
{
|
||||
send_ec_command(0x80);
|
||||
send_ec_command(RD_EC);
|
||||
send_ec_data(addr);
|
||||
|
||||
return recv_ec_data();
|
||||
@@ -114,7 +119,7 @@ u8 ec_read(u8 addr)
|
||||
|
||||
int ec_write(u8 addr, u8 data)
|
||||
{
|
||||
send_ec_command(0x81);
|
||||
send_ec_command(WR_EC);
|
||||
send_ec_data(addr);
|
||||
return send_ec_data(data);
|
||||
}
|
||||
@@ -126,7 +131,7 @@ u8 ec_status(void)
|
||||
|
||||
u8 ec_query(void)
|
||||
{
|
||||
send_ec_command(0x84);
|
||||
send_ec_command(QR_EC);
|
||||
return recv_ec_data();
|
||||
}
|
||||
|
||||
|
@@ -3,7 +3,7 @@
|
||||
#ifndef _EC_ACPI_H
|
||||
#define _EC_ACPI_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <types.h>
|
||||
|
||||
#define EC_DATA 0x62
|
||||
#define EC_SC 0x66
|
||||
@@ -11,7 +11,7 @@
|
||||
/* EC_SC input */
|
||||
#define EC_SMI_EVT (1 << 6) // 1: SMI event pending
|
||||
#define EC_SCI_EVT (1 << 5) // 1: SCI event pending
|
||||
#define EC_BURST (1 << 4) // controller is in burst mode
|
||||
#define EC_BURST (1 << 4) // 1: controller is in burst mode
|
||||
#define EC_CMD (1 << 3) // 1: byte in data register is command
|
||||
// 0: byte in data register is data
|
||||
#define EC_IBF (1 << 1) // 1: input buffer full (data ready for ec)
|
||||
@@ -23,10 +23,14 @@
|
||||
#define BD_EC 0x83 // Burst Disable Embedded Controller
|
||||
#define QR_EC 0x84 // Query Embedded Controller
|
||||
|
||||
bool ec_ready_send(int timeout_us);
|
||||
bool ec_ready_recv(int timeout_us);
|
||||
int send_ec_command(u8 command);
|
||||
int send_ec_command_timeout(u8 command, int timeout);
|
||||
int send_ec_data(u8 data);
|
||||
int send_ec_data_nowait(u8 data);
|
||||
int send_ec_data_timeout(u8 data, int timeout);
|
||||
u8 recv_ec_data(void);
|
||||
u8 recv_ec_data_timeout(int timeout);
|
||||
void ec_clear_out_queue(void);
|
||||
u8 ec_status(void);
|
||||
u8 ec_query(void);
|
||||
|
Reference in New Issue
Block a user