summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWai-Hong Tam <waihong@google.com>2022-02-14 18:11:14 -0800
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-09-20 00:30:51 +0000
commit7c2fa195590a1e496fe97ac10cc545138a66699c (patch)
tree306959b00a61cd4ea9e83e7e7449875f52746620
parent145cc9689197c98c509fc4bd7352adc3daf2c092 (diff)
downloadchrome-ec-7c2fa195590a1e496fe97ac10cc545138a66699c.tar.gz
herobrine: Wait negotiated VBUS transition before enabling 5V rail
Previous, the PP5000 is enabled at GPIO init. In the dead battery case, enabling PP5000 overloads the battery charger at the initial 5V 500mA supply. We want to defer it until the PD negotiated VBUS transition, so the battery charger won't be overloaded. A window of PD negotiation is created. It starts from the Type-C state reaching Attached.SNK, and eneds when the PD contract is created. The VBUS may be raised anytime in this window. The current implementation is the worst case scenario. Every message the PD negotiation is received at the last moment before timeout. The extra time is added to compensate the delay internally, like the decision of the DPM. The unit tests are added. BRANCH=None BUG=b:215726554 TEST=Booted the Herobrine board with no-battery. The 5V rail got delayed. Change-Id: I5529762de3495e4cb6702e887bf6ee09d1711f14 Signed-off-by: Wai-Hong Tam <waihong@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3501013 Reviewed-by: Sam Hurst <shurst@google.com> Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r--common/charger.c7
-rw-r--r--include/charger.h7
-rw-r--r--zephyr/projects/herobrine/CMakeLists.txt5
-rw-r--r--zephyr/projects/herobrine/include/board_chipset.h11
-rw-r--r--zephyr/projects/herobrine/src/board_chipset.c83
-rw-r--r--zephyr/test/herobrine/CMakeLists.txt14
-rw-r--r--zephyr/test/herobrine/Kconfig12
-rw-r--r--zephyr/test/herobrine/README.md3
-rw-r--r--zephyr/test/herobrine/boards/native_posix.overlay26
-rw-r--r--zephyr/test/herobrine/prj.conf11
-rw-r--r--zephyr/test/herobrine/src/board_chipset.c76
-rw-r--r--zephyr/test/herobrine/testcase.yaml10
12 files changed, 264 insertions, 1 deletions
diff --git a/common/charger.c b/common/charger.c
index 81404ed275..c19dd85832 100644
--- a/common/charger.c
+++ b/common/charger.c
@@ -778,3 +778,10 @@ enum ec_error_list charger_enable_linear_charge(int chgnum, bool enable)
return EC_ERROR_UNIMPLEMENTED;
}
+
+#ifdef CONFIG_CHARGER_MIN_BAT_PCT_FOR_POWER_ON
+inline int charger_get_min_bat_pct_for_power_on(void)
+{
+ return CONFIG_CHARGER_MIN_BAT_PCT_FOR_POWER_ON;
+}
+#endif
diff --git a/include/charger.h b/include/charger.h
index b6b3be3426..046ed3acc2 100644
--- a/include/charger.h
+++ b/include/charger.h
@@ -389,10 +389,15 @@ enum ec_error_list charger_enable_linear_charge(int chgnum, bool enable);
*/
enum ec_error_list charger_enable_bypass_mode(int chgnum, int enable);
-/*
+/**
* Print all charger info for debugging purposes
* @param chgnum: charger IC index.
*/
void print_charger_debug(int chgnum);
+/**
+ * Get the value of CONFIG_CHARGER_MIN_BAT_PCT_FOR_POWER_ON
+ */
+int charger_get_min_bat_pct_for_power_on(void);
+
#endif /* __CROS_EC_CHARGER_H */
diff --git a/zephyr/projects/herobrine/CMakeLists.txt b/zephyr/projects/herobrine/CMakeLists.txt
index 39472f424b..75aae3419e 100644
--- a/zephyr/projects/herobrine/CMakeLists.txt
+++ b/zephyr/projects/herobrine/CMakeLists.txt
@@ -8,6 +8,11 @@ find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}")
cros_ec_library_include_directories(include)
+# Common Herobrine implementation
+zephyr_library_sources(
+ "src/board_chipset.c"
+)
+
# Board specific implementation
zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_USBC
"src/usbc_config.c"
diff --git a/zephyr/projects/herobrine/include/board_chipset.h b/zephyr/projects/herobrine/include/board_chipset.h
new file mode 100644
index 0000000000..8350ef10ff
--- /dev/null
+++ b/zephyr/projects/herobrine/include/board_chipset.h
@@ -0,0 +1,11 @@
+/* Copyright 2022 The ChromiumOS Authors.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef __CROS_EC_HEROBRINE_BOARD_CHIPSET_H
+#define __CROS_EC_HEROBRINE_BOARD_CHIPSET_H
+
+__test_only void reset_pp5000_inited(void);
+
+#endif /* __CROS_EC_HEROBRINE_BOARD_CHIPSET_H */
diff --git a/zephyr/projects/herobrine/src/board_chipset.c b/zephyr/projects/herobrine/src/board_chipset.c
new file mode 100644
index 0000000000..6a58eee99e
--- /dev/null
+++ b/zephyr/projects/herobrine/src/board_chipset.c
@@ -0,0 +1,83 @@
+/* Copyright 2022 The ChromiumOS Authors.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/* Herobrine chipset-specific configuration */
+
+#include "charger.h"
+#include "common.h"
+#include "console.h"
+#include "battery.h"
+#include "gpio.h"
+#include "hooks.h"
+#include "timer.h"
+#include "usb_pd.h"
+
+#include "board_chipset.h"
+
+#define CPRINTS(format, args...) cprints(CC_HOOK, format, ##args)
+#define CPRINTF(format, args...) cprintf(CC_HOOK, format, ##args)
+
+/*
+ * A window of PD negotiation. It starts from the Type-C state reaching
+ * Attached.SNK, and ends when the PD contract is created. The VBUS may be
+ * raised anytime in this window.
+ *
+ * The current implementation is the worst case scenario: every message the PD
+ * negotiation is received at the last moment before timeout. More extra time
+ * is added to compensate the delay internally, like the decision of the DPM.
+ *
+ * TODO(waihong): Cancel this timer when the PD contract is negotiated.
+ */
+#define PD_READY_TIMEOUT \
+ (PD_T_SINK_WAIT_CAP + PD_T_SENDER_RESPONSE + PD_T_SINK_TRANSITION + \
+ 20 * MSEC)
+
+#define PD_READY_POLL_DELAY (10 * MSEC)
+
+static timestamp_t pd_ready_timeout;
+
+static bool pp5000_inited;
+
+__test_only void reset_pp5000_inited(void)
+{
+ pp5000_inited = false;
+}
+
+/* Called on USB PD connected */
+static void board_usb_pd_connect(void)
+{
+ int soc = -1;
+
+ /* First boot, battery unattached or low SOC */
+ if (!pp5000_inited &&
+ ((battery_state_of_charge_abs(&soc) != EC_SUCCESS ||
+ soc < charger_get_min_bat_pct_for_power_on()))) {
+ pd_ready_timeout = get_time();
+ pd_ready_timeout.val += PD_READY_TIMEOUT;
+ }
+}
+DECLARE_HOOK(HOOK_USB_PD_CONNECT, board_usb_pd_connect, HOOK_PRIO_DEFAULT);
+
+static void wait_pd_ready(void)
+{
+ CPRINTS("Wait PD negotiated VBUS transition %u",
+ pd_ready_timeout.le.lo);
+ while (pd_ready_timeout.val && get_time().val < pd_ready_timeout.val)
+ usleep(PD_READY_POLL_DELAY);
+}
+
+/* Called on AP S5 -> S3 transition */
+static void board_chipset_pre_init(void)
+{
+ if (!pp5000_inited) {
+ if (pd_ready_timeout.val) {
+ wait_pd_ready();
+ }
+ CPRINTS("Enable 5V rail");
+ gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(gpio_en_pp5000_s5), 1);
+ pp5000_inited = true;
+ }
+}
+DECLARE_HOOK(HOOK_CHIPSET_PRE_INIT, board_chipset_pre_init, HOOK_PRIO_DEFAULT);
diff --git a/zephyr/test/herobrine/CMakeLists.txt b/zephyr/test/herobrine/CMakeLists.txt
new file mode 100644
index 0000000000..8209eb77fb
--- /dev/null
+++ b/zephyr/test/herobrine/CMakeLists.txt
@@ -0,0 +1,14 @@
+# Copyright 2022 The ChromiumOS Authors.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+cmake_minimum_required(VERSION 3.13.1)
+find_package(Zephyr REQUIRED HINTS "${ZEPHYR_BASE}")
+project(herobrine)
+
+zephyr_include_directories("${PLATFORM_EC}/zephyr/projects/herobrine/include")
+
+target_sources_ifdef(CONFIG_TEST_BOARD_CHIPSET
+ app PRIVATE src/board_chipset.c)
+target_sources_ifdef(CONFIG_TEST_BOARD_CHIPSET
+ app PRIVATE ${PLATFORM_EC}/zephyr/projects/herobrine/src/board_chipset.c)
diff --git a/zephyr/test/herobrine/Kconfig b/zephyr/test/herobrine/Kconfig
new file mode 100644
index 0000000000..415e6e58af
--- /dev/null
+++ b/zephyr/test/herobrine/Kconfig
@@ -0,0 +1,12 @@
+# Copyright 2022 The ChromiumOS Authors.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+config TEST_BOARD_CHIPSET
+ bool "Run the tests intended for board_chipset"
+ help
+ Include board_chipset.c into the binary. Test their functions in
+ different combinations: good battery vs low battery, normal boot
+ vs delayed boot, etc.
+
+source "Kconfig.zephyr"
diff --git a/zephyr/test/herobrine/README.md b/zephyr/test/herobrine/README.md
new file mode 100644
index 0000000000..398b27e304
--- /dev/null
+++ b/zephyr/test/herobrine/README.md
@@ -0,0 +1,3 @@
+Tests for board specific code under `zephyr/projects/herobrine/src`.
+
+Run with ./twister -T zephyr/test/herobrine
diff --git a/zephyr/test/herobrine/boards/native_posix.overlay b/zephyr/test/herobrine/boards/native_posix.overlay
new file mode 100644
index 0000000000..bfecc9a7d5
--- /dev/null
+++ b/zephyr/test/herobrine/boards/native_posix.overlay
@@ -0,0 +1,26 @@
+/* Copyright 2022 The ChromiumOS Authors.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <board-overlays/native_posix.dts>
+
+/ {
+ /*
+ * Keep these GPIOs in pin order.
+ * If you need to add one, make sure you increase
+ * ngpios in the gpio0 node further down.
+ */
+ named-gpios {
+ compatible = "named-gpios";
+
+ gpio_en_pp5000_s5: en_pp5000_s5 {
+ gpios = <&gpio0 2 GPIO_OUTPUT_HIGH>;
+ enum-name = "GPIO_EN_PP5000";
+ };
+ };
+};
+
+&gpio0 {
+ ngpios = <3>;
+};
diff --git a/zephyr/test/herobrine/prj.conf b/zephyr/test/herobrine/prj.conf
new file mode 100644
index 0000000000..3334f11939
--- /dev/null
+++ b/zephyr/test/herobrine/prj.conf
@@ -0,0 +1,11 @@
+# Copyright 2022 The ChromiumOS Authors.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+CONFIG_ZTEST=y
+CONFIG_ZTEST_ASSERT_VERBOSE=1
+CONFIG_ZTEST_NEW_API=y
+CONFIG_ASSERT=y
+
+CONFIG_CROS_EC=y
+CONFIG_PLATFORM_EC=y
diff --git a/zephyr/test/herobrine/src/board_chipset.c b/zephyr/test/herobrine/src/board_chipset.c
new file mode 100644
index 0000000000..77bdb14e16
--- /dev/null
+++ b/zephyr/test/herobrine/src/board_chipset.c
@@ -0,0 +1,76 @@
+/* Copyright 2022 The ChromiumOS Authors.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <zephyr/kernel.h>
+#include <zephyr/ztest.h>
+
+#include "hooks.h"
+#include "board_chipset.h"
+
+static int battery_soc_abs_value = 50;
+
+int battery_state_of_charge_abs(int *percent)
+{
+ *percent = battery_soc_abs_value;
+ return EC_SUCCESS;
+}
+
+int charger_get_min_bat_pct_for_power_on(void)
+{
+ return 2;
+}
+
+ZTEST_USER(board_chipset, test_good_battery_normal_boot)
+{
+ timestamp_t start_time;
+ uint64_t time_diff_us;
+
+ battery_soc_abs_value = 50;
+
+ start_time = get_time();
+ hook_notify(HOOK_CHIPSET_PRE_INIT);
+ time_diff_us = get_time().val - start_time.val;
+
+ zassert_true(time_diff_us < 10, "CHIPSET_PRE_INIT hook delayed", NULL);
+}
+
+ZTEST_USER(board_chipset, test_low_battery_normal_boot)
+{
+ timestamp_t start_time;
+ uint64_t time_diff_us;
+
+ battery_soc_abs_value = 1;
+
+ start_time = get_time();
+ hook_notify(HOOK_CHIPSET_PRE_INIT);
+ time_diff_us = get_time().val - start_time.val;
+
+ zassert_true(time_diff_us < 10, "CHIPSET_PRE_INIT hook delayed", NULL);
+}
+
+ZTEST_USER(board_chipset, test_low_battery_delayed_boot)
+{
+ timestamp_t start_time;
+ uint64_t time_diff_us;
+
+ battery_soc_abs_value = 1;
+ /* The PD connect event delays the power on sequence */
+ hook_notify(HOOK_USB_PD_CONNECT);
+
+ start_time = get_time();
+ hook_notify(HOOK_CHIPSET_PRE_INIT);
+ time_diff_us = get_time().val - start_time.val;
+
+ zassert_true(time_diff_us > 500000, "CHIPSET_PRE_INIT hook not delayed",
+ NULL);
+}
+
+static void test_before(void *data)
+{
+ ARG_UNUSED(data);
+ reset_pp5000_inited();
+}
+
+ZTEST_SUITE(board_chipset, NULL, NULL, test_before, NULL, NULL);
diff --git a/zephyr/test/herobrine/testcase.yaml b/zephyr/test/herobrine/testcase.yaml
new file mode 100644
index 0000000000..e5f17a3848
--- /dev/null
+++ b/zephyr/test/herobrine/testcase.yaml
@@ -0,0 +1,10 @@
+# Copyright 2022 The ChromiumOS Authors.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+common:
+ platform_allow: native_posix
+tests:
+ herobrine.board_chipset:
+ extra_configs:
+ - CONFIG_TEST_BOARD_CHIPSET=y