Michał Żygowski has uploaded this change for review.

View Change

acpi_ec: Implement basic ACPI embedded controller API

Implement basic functions to read/write EC registers and send standard
ACPI EC commands. Those may prove useful in possible future
implementations of embedded eontrollers in flashrom and are required
to support EC firmware flashing on Tuxedo laptops.

Signed-off-by: Michał Żygowski <michal.zygowski@3mdeb.com>
Signed-off-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
Change-Id: Ie3bae8d81c80ae2f286b619e974869e3f2f4545d
---
A acpi_ec.c
A acpi_ec.h
2 files changed, 158 insertions(+), 0 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/14/55714/1
diff --git a/acpi_ec.c b/acpi_ec.c
new file mode 100644
index 0000000..f51b763
--- /dev/null
+++ b/acpi_ec.c
@@ -0,0 +1,106 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2021, TUXEDO Computers GmbH
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "acpi_ec.h"
+#include "flash.h"
+#include "hwaccess.h"
+
+bool ec_wait_for_ibuf(uint8_t control_port)
+{
+ unsigned int i;
+
+ for (i = 0; (INB(control_port) & EC_STS_IBF) != 0; ++i) {
+ if (i == EC_MAX_STATUS_CHECKS)
+ return false;
+ }
+
+ return true;
+}
+
+bool ec_wait_for_obuf(uint8_t control_port, unsigned int max_checks)
+{
+ unsigned int i;
+
+ for (i = 0; (INB(control_port) & EC_STS_OBF) == 0; ++i) {
+ if (i == max_checks)
+ return false;
+ }
+
+ return true;
+}
+
+bool ec_write_cmd(uint8_t control_port, uint8_t cmd)
+{
+ const bool success = ec_wait_for_ibuf(control_port);
+ if (success)
+ OUTB(cmd, control_port);
+
+ return success;
+}
+
+bool ec_read_byte(uint8_t control_port, uint8_t data_port, uint8_t *data)
+{
+ const bool success = ec_wait_for_obuf(control_port, EC_MAX_STATUS_CHECKS);
+ if (success)
+ *data = INB(data_port);
+
+ return success;
+}
+
+bool ec_write_byte(uint8_t control_port, uint8_t data_port, uint8_t data)
+{
+ const bool success = ec_wait_for_ibuf(control_port);
+ if (success)
+ OUTB(data, data_port);
+
+ return success;
+}
+
+bool ec_read_reg(uint8_t address, uint8_t *data)
+{
+ if (!ec_wait_for_ibuf(EC_CONTROL))
+ return false;
+ OUTB(EC_CMD_READ_REG, EC_CONTROL);
+
+ if (!ec_wait_for_ibuf(EC_CONTROL))
+ return false;
+ OUTB(address, EC_DATA);
+
+ if (!ec_wait_for_ibuf(EC_CONTROL))
+ return false;
+ *data = INB(EC_DATA);
+
+ return true;
+}
+
+bool ec_write_reg(uint8_t address, uint8_t data)
+{
+ if (!ec_wait_for_ibuf(EC_CONTROL))
+ return false;
+ OUTB(EC_CMD_WRITE_REG, EC_CONTROL);
+
+ if (!ec_wait_for_ibuf(EC_CONTROL))
+ return false;
+ OUTB(address, EC_DATA);
+
+ if (!ec_wait_for_ibuf(EC_CONTROL))
+ return false;
+ OUTB(data, EC_DATA);
+
+ return true;
+}
diff --git a/acpi_ec.h b/acpi_ec.h
new file mode 100644
index 0000000..001d407
--- /dev/null
+++ b/acpi_ec.h
@@ -0,0 +1,52 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2021, TUXEDO Computers GmbH
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __EC_H__
+#define __EC_H__ 1
+
+#include <stdbool.h>
+#include <stdint.h>
+
+/*
+ * Generic IO functions for ACPI-compliant embedded controllers
+ */
+
+/* Standard ports */
+#define EC_DATA 0x62
+#define EC_CONTROL 0x66 /* Read status, write commands */
+
+/* Standard commands */
+#define EC_CMD_READ_REG 0x80 /* Read register's value */
+#define EC_CMD_WRITE_REG 0x81 /* Write register's value */
+
+/* Some of the status bits */
+#define EC_STS_IBF (1 << 1) /* EC's input buffer full (host can't write) */
+#define EC_STS_OBF (1 << 0) /* EC's output buffer full (host can read) */
+
+/* How many iterations to wait for input or output buffer */
+#define EC_MAX_STATUS_CHECKS 100000
+
+bool ec_wait_for_ibuf(uint8_t control_port);
+bool ec_wait_for_obuf(uint8_t control_port, unsigned int max_checks);
+
+bool ec_write_cmd(uint8_t control_port, uint8_t cmd);
+bool ec_read_byte(uint8_t control_port, uint8_t data_port, uint8_t *data);
+bool ec_write_byte(uint8_t control_port, uint8_t data_port, uint8_t data);
+
+/* These implement standard ACPI commands and thus use standard ports */
+bool ec_read_reg(uint8_t address, uint8_t *data);
+bool ec_write_reg(uint8_t address, uint8_t data);
+
+#endif /* !__EC_H__ */

To view, visit change 55714. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: Ie3bae8d81c80ae2f286b619e974869e3f2f4545d
Gerrit-Change-Number: 55714
Gerrit-PatchSet: 1
Gerrit-Owner: Michał Żygowski <michal.zygowski@3mdeb.com>
Gerrit-MessageType: newchange