summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJonathon Hall <jonathon.hall@puri.sm>2023-04-12 16:01:16 -0400
committerFelix Held <felix-coreboot@felixheld.de>2023-05-26 14:57:27 +0000
commita41c825068f4f5afc3fd6839a796d868241d91cd (patch)
tree5fb8c5bf1ec3c7ce03205148660906ab2f5ee294 /src
parente19d33bf72a9db2475878fcf4787eaf3ca2dcd14 (diff)
ec/purism,system76: Provide probe for fixed Librem-EC jack detect
Provide system76_ec_cmd() to send arbitrary commands to the EC. Provide librem_ec_has_jack_detect() to probe for the jack detect fix. Change-Id: Ic7bda0ce230a3ad68dfeb7b01a0e04f70dab9e5d Signed-off-by: Jonathon Hall <jonathon.hall@puri.sm> Reviewed-on: https://review.coreboot.org/c/coreboot/+/74390 Reviewed-by: Angel Pons <th3fanbus@gmail.com> Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Tim Crawford <tcrawford@system76.com>
Diffstat (limited to 'src')
-rw-r--r--src/ec/purism/librem-ec/Makefile.inc1
-rw-r--r--src/ec/purism/librem-ec/librem_ec.c22
-rw-r--r--src/ec/purism/librem-ec/librem_ec.h14
-rw-r--r--src/ec/system76/ec/system76_ec.c51
-rw-r--r--src/ec/system76/ec/system76_ec.h17
5 files changed, 105 insertions, 0 deletions
diff --git a/src/ec/purism/librem-ec/Makefile.inc b/src/ec/purism/librem-ec/Makefile.inc
index 5e2b2dfac8..464371f272 100644
--- a/src/ec/purism/librem-ec/Makefile.inc
+++ b/src/ec/purism/librem-ec/Makefile.inc
@@ -2,6 +2,7 @@
ifeq ($(CONFIG_EC_LIBREM_EC),y)
all-y += ../../system76/ec/system76_ec.c
+all-y += librem_ec.c
smm-$(CONFIG_DEBUG_SMI) += ../../system76/ec/system76_ec.c
endif
diff --git a/src/ec/purism/librem-ec/librem_ec.c b/src/ec/purism/librem-ec/librem_ec.c
new file mode 100644
index 0000000000..8298b73644
--- /dev/null
+++ b/src/ec/purism/librem-ec/librem_ec.c
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include "librem_ec.h"
+#include "../../system76/ec/system76_ec.h"
+#include <stddef.h>
+
+#define CMD_PROBE 1
+
+bool librem_ec_has_jack_detect(void)
+{
+ /* The 'flags' field in the probe command reply was added in an update.
+ Send 4 bytes of zeroes in the "request" to zero out the field if the
+ EC does not set it for its reply. */
+ const uint8_t request_data[4] = {0};
+ uint8_t reply_data[4] = {0};
+ bool ec_cmd_success = system76_ec_cmd(CMD_PROBE, request_data,
+ ARRAY_SIZE(request_data), reply_data, ARRAY_SIZE(reply_data));
+ if (!ec_cmd_success)
+ return false;
+ /* Byte 3 is flags, bit 0 is the jack detect flag */
+ return reply_data[3] & 0x01;
+}
diff --git a/src/ec/purism/librem-ec/librem_ec.h b/src/ec/purism/librem-ec/librem_ec.h
new file mode 100644
index 0000000000..f3a6a78fd9
--- /dev/null
+++ b/src/ec/purism/librem-ec/librem_ec.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef LIBREM_EC_H
+#define LIBREM_EC_H
+
+#include <stdbool.h>
+
+/*
+ * Check whether librem-ec has working jack detect. This was fixed in librem-ec
+ * 1.13, so we only use the verbs with jack detect if the EC has been updated.
+ */
+bool librem_ec_has_jack_detect(void);
+
+#endif
diff --git a/src/ec/system76/ec/system76_ec.c b/src/ec/system76/ec/system76_ec.c
index ddcb602d4c..828376d0e5 100644
--- a/src/ec/system76/ec/system76_ec.c
+++ b/src/ec/system76/ec/system76_ec.c
@@ -1,7 +1,9 @@
/* SPDX-License-Identifier: GPL-2.0-only */
+#include "system76_ec.h"
#include <arch/io.h>
#include <console/system76_ec.h>
+#include <console/console.h>
#include <timer.h>
// This is the command region for System76 EC firmware. It must be
@@ -11,10 +13,13 @@
#define REG_CMD 0
#define REG_RESULT 1
+#define REG_DATA 2 // Start of command data
// When command register is 0, command is complete
#define CMD_FINISHED 0
+#define RESULT_OK 0
+
// Print command. Registers are unique for each command
#define CMD_PRINT 4
#define CMD_PRINT_REG_FLAGS 2
@@ -59,3 +64,49 @@ void system76_ec_print(uint8_t byte)
if (byte == '\n' || len >= (SYSTEM76_EC_SIZE - CMD_PRINT_REG_DATA))
system76_ec_flush();
}
+
+bool system76_ec_cmd(uint8_t cmd, const uint8_t *request_data,
+ uint8_t request_size, uint8_t *reply_data, uint8_t reply_size)
+{
+ if (request_size > SYSTEM76_EC_SIZE - REG_DATA ||
+ reply_size > SYSTEM76_EC_SIZE - REG_DATA) {
+ printk(BIOS_ERR, "EC command %d too long - request size %u, reply size %u\n",
+ cmd, request_size, reply_size);
+ return false;
+ }
+
+ /* If any data were buffered by system76_ec_print(), flush it first */
+ uint8_t buffered_len = system76_ec_read(CMD_PRINT_REG_LEN);
+ if (buffered_len > 0)
+ system76_ec_flush();
+
+ /* Write the data */
+ uint8_t i;
+ for (i = 0; i < request_size; ++i)
+ system76_ec_write(REG_DATA + i, request_data[i]);
+
+ /* Write the command */
+ system76_ec_write(REG_CMD, cmd);
+
+ /* Wait for the command to complete */
+ bool ret = true;
+ int elapsed = wait_ms(1000, system76_ec_read(REG_CMD) == CMD_FINISHED);
+ if (elapsed == 0) {
+ /* Timed out: fail the command, don't attempt to read a reply. */
+ printk(BIOS_WARNING, "EC command %d timed out - request size %d, reply size %d\n",
+ cmd, request_size, reply_size);
+ ret = false;
+ } else {
+ /* Read the reply */
+ for (i = 0; i < reply_size; ++i)
+ reply_data[i] = system76_ec_read(REG_DATA+i);
+ /* Check the reply status */
+ ret = (system76_ec_read(REG_RESULT) == RESULT_OK);
+ }
+
+ /* Reset the flags and length so we can buffer console prints again */
+ system76_ec_write(CMD_PRINT_REG_FLAGS, 0);
+ system76_ec_write(CMD_PRINT_REG_LEN, 0);
+
+ return ret;
+}
diff --git a/src/ec/system76/ec/system76_ec.h b/src/ec/system76/ec/system76_ec.h
new file mode 100644
index 0000000000..9675bd0caf
--- /dev/null
+++ b/src/ec/system76/ec/system76_ec.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef SYSTEM76_EC_H
+#define SYSTEM76_EC_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+/*
+ * Send a command to the EC. request_data/request_size are the request payload,
+ * request_data can be NULL if request_size is 0. reply_data/reply_size are
+ * the reply payload, reply_data can be NULL if reply_size is 0.
+ */
+bool system76_ec_cmd(uint8_t cmd, const uint8_t *request_data,
+ uint8_t request_size, uint8_t *reply_data, uint8_t reply_size);
+
+#endif