From b863c6d01b06931b3553f699589514a46dc14eb5 Mon Sep 17 00:00:00 2001 From: Robert Zieba Date: Wed, 30 Nov 2022 22:43:33 +0000 Subject: zephyr/test/skyrim: Add baseboard USB PD tests Add baseboard USB PD tests. BRANCH=none BUG=b:247151116 TEST=Ran tests Change-Id: Ie6e5ac2b6caa93e3d0f166c4d957ee96973e0c2d Signed-off-by: Robert Zieba Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4148775 Reviewed-by: Abe Levkoy --- zephyr/test/skyrim/CMakeLists.txt | 2 + zephyr/test/skyrim/Kconfig | 13 ++ zephyr/test/skyrim/boards/native_posix.overlay | 19 ++ zephyr/test/skyrim/src/baseboard/usb_pd_policy.c | 263 +++++++++++++++++++++++ zephyr/test/skyrim/testcase.yaml | 11 + 5 files changed, 308 insertions(+) create mode 100644 zephyr/test/skyrim/src/baseboard/usb_pd_policy.c diff --git a/zephyr/test/skyrim/CMakeLists.txt b/zephyr/test/skyrim/CMakeLists.txt index 69bc856d1f..8fbcc78432 100644 --- a/zephyr/test/skyrim/CMakeLists.txt +++ b/zephyr/test/skyrim/CMakeLists.txt @@ -11,3 +11,5 @@ zephyr_include_directories("${PLATFORM_EC_PROGRAM_DIR}/skyrim/include") add_subdirectory(${PLATFORM_EC}/zephyr/test/test_utils test_utils) target_sources(app PRIVATE src/${CONFIG_TEST_BOARD_NAME}/common.c) + +target_sources_ifdef(CONFIG_TEST_BOARD_USB_PD_POLICY app PRIVATE src/baseboard/usb_pd_policy.c ${PLATFORM_EC_PROGRAM_DIR}/skyrim/src/usb_pd_policy.c) \ No newline at end of file diff --git a/zephyr/test/skyrim/Kconfig b/zephyr/test/skyrim/Kconfig index 30e9ed13d2..41685834be 100644 --- a/zephyr/test/skyrim/Kconfig +++ b/zephyr/test/skyrim/Kconfig @@ -48,4 +48,17 @@ config TEST_BOARD_NAME default "skyrim" if TEST_BOARD_SKYRIM default "winterhold" if TEST_BOARD_WINTERHOLD +config TEST_BOARD_USB_PD_POLICY + bool "Enable USB PD policy specific tests" + select TEST_ENABLE_USB_PD_HOST_CMD + default n + +config TEST_ENABLE_USB_PD_HOST_CMD + bool "Fake config to enable this feature" + default n + +config USB_PD_DISCHARGE + bool "Fake config to enable this feature" + default n + source "Kconfig.zephyr" diff --git a/zephyr/test/skyrim/boards/native_posix.overlay b/zephyr/test/skyrim/boards/native_posix.overlay index a3045bf332..7328610584 100644 --- a/zephyr/test/skyrim/boards/native_posix.overlay +++ b/zephyr/test/skyrim/boards/native_posix.overlay @@ -10,5 +10,24 @@ /* GPIOs shared by all boards */ named-gpios { compatible = "named-gpios"; + + gpio_pg_pwr_s5: pg_pwr_s5 { + gpios = <&gpio0 0 GPIO_INPUT>; + enum-name = "GPIO_S5_PGOOD"; + }; + + gpio_usb_c0_tcpc_fastsw_ctl_en: usb_c0_tcpc_fastsw_ctl_en { + gpios = <&gpio0 1 GPIO_OUTPUT_LOW>; + enum-name = "IOEX_USB_C0_TCPC_FASTSW_CTL_EN"; + }; + + gpio_usb_c1_tcpc_fastsw_ctl_en: usb_c1_tcpc_fastsw_ctl_en { + gpios = <&gpio0 2 GPIO_OUTPUT_LOW>; + enum-name = "IOEX_USB_C1_TCPC_FASTSW_CTL_EN"; + }; }; +}; + +&gpio0 { + ngpios = <23>; }; \ No newline at end of file diff --git a/zephyr/test/skyrim/src/baseboard/usb_pd_policy.c b/zephyr/test/skyrim/src/baseboard/usb_pd_policy.c new file mode 100644 index 0000000000..1a8e5e8805 --- /dev/null +++ b/zephyr/test/skyrim/src/baseboard/usb_pd_policy.c @@ -0,0 +1,263 @@ +/* Copyright 2023 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +#include +#include +#include +#include + +#include +#include +#include + +FAKE_VALUE_FUNC(int, ppc_vbus_source_enable, int, int); +FAKE_VOID_FUNC(pd_set_vbus_discharge, int, int); +FAKE_VOID_FUNC(pd_send_host_event, int); +FAKE_VALUE_FUNC(bool, tcpm_get_src_ctrl, int); +FAKE_VALUE_FUNC(int, ppc_vbus_sink_enable, int, int); + +int board_is_sourcing_vbus(int port); + +ZTEST(usb_pd_policy, pd_check_vconn_swap) +{ + const struct gpio_dt_spec *gpio = + GPIO_DT_FROM_NODELABEL(gpio_pg_pwr_s5); + + /* + * The value of pd_check_vconn_swap should follow gpio_pg_pwr_s5 + * for all ports. + */ + zassert_ok(gpio_emul_input_set(gpio->port, gpio->pin, true)); + zassert_true(pd_check_vconn_swap(0)); + zassert_true(pd_check_vconn_swap(1)); + + zassert_ok(gpio_emul_input_set(gpio->port, gpio->pin, false)); + zassert_false(pd_check_vconn_swap(0)); + zassert_false(pd_check_vconn_swap(1)); +} + +ZTEST(usb_pd_policy, pd_power_supply_reset_c0_success) +{ + ppc_vbus_source_enable_fake.return_val = EC_SUCCESS; + ppc_vbus_sink_enable_fake.return_val = EC_SUCCESS; + + pd_power_supply_reset(0); + + zassert_equal(ppc_vbus_source_enable_fake.call_count, 1); + /* port */ + zassert_equal(ppc_vbus_source_enable_fake.arg0_val, 0); + /* enable */ + zassert_equal(ppc_vbus_source_enable_fake.arg1_val, 0); + + if (IS_ENABLED(CONFIG_USB_PD_DISCHARGE)) { + zassert_equal(pd_set_vbus_discharge_fake.call_count, 1); + /* port */ + zassert_equal(pd_set_vbus_discharge_fake.arg0_val, 0); + /* enable */ + zassert_equal(pd_set_vbus_discharge_fake.arg1_val, 1); + } + + zassert_equal(pd_send_host_event_fake.call_count, 1); +} + +ZTEST(usb_pd_policy, pd_power_supply_reset_c1_success) +{ + ppc_vbus_source_enable_fake.return_val = EC_SUCCESS; + ppc_vbus_sink_enable_fake.return_val = EC_SUCCESS; + + pd_power_supply_reset(1); + + zassert_equal(ppc_vbus_source_enable_fake.call_count, 1); + /* port */ + zassert_equal(ppc_vbus_source_enable_fake.arg0_val, 1); + /* enable */ + zassert_equal(ppc_vbus_source_enable_fake.arg1_val, 0); + + if (IS_ENABLED(CONFIG_USB_PD_DISCHARGE)) { + zassert_equal(pd_set_vbus_discharge_fake.call_count, 1); + /* port */ + zassert_equal(pd_set_vbus_discharge_fake.arg0_val, 1); + /* enable */ + zassert_equal(pd_set_vbus_discharge_fake.arg1_val, 1); + } + + zassert_equal(pd_send_host_event_fake.call_count, 1); +} + +ZTEST(usb_pd_policy, pd_set_power_supply_ready_c0_success) +{ + ppc_vbus_source_enable_fake.return_val = EC_SUCCESS; + ppc_vbus_sink_enable_fake.return_val = EC_SUCCESS; + + zassert_equal(pd_set_power_supply_ready(0), EC_SUCCESS); + + zassert_equal(ppc_vbus_sink_enable_fake.call_count, 1); + /* port */ + zassert_equal(ppc_vbus_sink_enable_fake.arg0_val, 0); + /* enable */ + zassert_equal(ppc_vbus_sink_enable_fake.arg1_val, 0); + + if (IS_ENABLED(CONFIG_USB_PD_DISCHARGE)) { + zassert_equal(pd_set_vbus_discharge_fake.call_count, 1); + /* port */ + zassert_equal(pd_set_vbus_discharge_fake.arg0_val, 0); + /* enable */ + zassert_equal(pd_set_vbus_discharge_fake.arg1_val, 0); + } + + zassert_equal(ppc_vbus_source_enable_fake.call_count, 1); + /* port */ + zassert_equal(ppc_vbus_source_enable_fake.arg0_val, 0); + /* enable */ + zassert_equal(ppc_vbus_source_enable_fake.arg1_val, 1); + + zassert_equal(pd_send_host_event_fake.call_count, 1); +} + +ZTEST(usb_pd_policy, pd_set_power_supply_ready_c1_success) +{ + ppc_vbus_source_enable_fake.return_val = EC_SUCCESS; + ppc_vbus_sink_enable_fake.return_val = EC_SUCCESS; + + zassert_equal(pd_set_power_supply_ready(1), EC_SUCCESS); + + zassert_equal(ppc_vbus_sink_enable_fake.call_count, 1); + /* port */ + zassert_equal(ppc_vbus_sink_enable_fake.arg0_val, 1); + /* enable */ + zassert_equal(ppc_vbus_sink_enable_fake.arg1_val, 0); + + if (IS_ENABLED(CONFIG_USB_PD_DISCHARGE)) { + zassert_equal(pd_set_vbus_discharge_fake.call_count, 1); + /* port */ + zassert_equal(pd_set_vbus_discharge_fake.arg0_val, 1); + /* enable */ + zassert_equal(pd_set_vbus_discharge_fake.arg1_val, 0); + } + + zassert_equal(ppc_vbus_source_enable_fake.call_count, 1); + /* port */ + zassert_equal(ppc_vbus_source_enable_fake.arg0_val, 1); + /* enable */ + zassert_equal(ppc_vbus_source_enable_fake.arg1_val, 1); + + zassert_equal(pd_send_host_event_fake.call_count, 1); +} + +ZTEST(usb_pd_policy, pd_set_power_supply_ready_c0_failure) +{ + /* Test with ppc_vbus_sink_enable_fake failing. */ + ppc_vbus_sink_enable_fake.return_val = EC_ERROR_INVAL; + zassert_not_equal(pd_set_power_supply_ready(0), EC_SUCCESS); + zassert_equal(ppc_vbus_sink_enable_fake.call_count, 1); + /* port */ + zassert_equal(ppc_vbus_sink_enable_fake.arg0_val, 0); + /* enable */ + zassert_equal(ppc_vbus_sink_enable_fake.arg1_val, 0); + + /* Test with ppc_vbus_source_enable failing. */ + ppc_vbus_sink_enable_fake.return_val = EC_SUCCESS; + ppc_vbus_source_enable_fake.return_val = EC_ERROR_INVAL; + zassert_not_equal(pd_set_power_supply_ready(0), EC_SUCCESS); + zassert_equal(ppc_vbus_source_enable_fake.call_count, 1); + /* port */ + zassert_equal(ppc_vbus_source_enable_fake.arg0_val, 0); + /* enable */ + zassert_equal(ppc_vbus_source_enable_fake.arg1_val, 1); + + zassert_equal(pd_send_host_event_fake.call_count, 0); +} + +ZTEST(usb_pd_policy, pd_set_power_supply_ready_c1_failure) +{ + /* Test with ppc_vbus_sink_enable_fake failing. */ + ppc_vbus_sink_enable_fake.return_val = EC_ERROR_INVAL; + zassert_not_equal(pd_set_power_supply_ready(1), EC_SUCCESS); + zassert_equal(ppc_vbus_sink_enable_fake.call_count, 1); + /* port */ + zassert_equal(ppc_vbus_sink_enable_fake.arg0_val, 1); + /* enable */ + zassert_equal(ppc_vbus_sink_enable_fake.arg1_val, 0); + + /* Test with ppc_vbus_source_enable failing. */ + ppc_vbus_sink_enable_fake.return_val = EC_SUCCESS; + ppc_vbus_source_enable_fake.return_val = EC_ERROR_INVAL; + zassert_not_equal(pd_set_power_supply_ready(1), EC_SUCCESS); + zassert_equal(ppc_vbus_source_enable_fake.call_count, 1); + /* port */ + zassert_equal(ppc_vbus_source_enable_fake.arg0_val, 1); + /* enable */ + zassert_equal(ppc_vbus_source_enable_fake.arg1_val, 1); + + zassert_equal(pd_send_host_event_fake.call_count, 0); +} + +ZTEST(usb_pd_policy, board_pd_set_frs_enable) +{ + const struct gpio_dt_spec *c0 = + GPIO_DT_FROM_NODELABEL(gpio_usb_c0_tcpc_fastsw_ctl_en); + const struct gpio_dt_spec *c1 = + GPIO_DT_FROM_NODELABEL(gpio_usb_c1_tcpc_fastsw_ctl_en); + + /* Enables to each port should just change the corresponding GPIO. */ + zassert_equal(board_pd_set_frs_enable(0, true), EC_SUCCESS); + zassert_equal(gpio_emul_output_get(c0->port, c0->pin), 1); + + zassert_equal(board_pd_set_frs_enable(0, false), EC_SUCCESS); + zassert_equal(gpio_emul_output_get(c0->port, c0->pin), 0); + + zassert_equal(board_pd_set_frs_enable(1, true), EC_SUCCESS); + zassert_equal(gpio_emul_output_get(c1->port, c1->pin), 1); + + zassert_equal(board_pd_set_frs_enable(1, false), EC_SUCCESS); + zassert_equal(gpio_emul_output_get(c1->port, c1->pin), 0); +} + +ZTEST(usb_pd_policy, board_is_sourcing_vbus_c0_true) +{ + tcpm_get_src_ctrl_fake.return_val = true; + zassert_true(board_is_sourcing_vbus(0)); + zassert_equal(tcpm_get_src_ctrl_fake.call_count, 1); + /* port */ + zassert_equal(tcpm_get_src_ctrl_fake.arg0_val, 0); +} + +ZTEST(usb_pd_policy, board_is_sourcing_vbus_c0_false) +{ + tcpm_get_src_ctrl_fake.return_val = false; + zassert_false(board_is_sourcing_vbus(0)); + zassert_equal(tcpm_get_src_ctrl_fake.call_count, 1); + /* port */ + zassert_equal(tcpm_get_src_ctrl_fake.arg0_val, 0); +} + +ZTEST(usb_pd_policy, board_is_sourcing_vbus_c1_true) +{ + tcpm_get_src_ctrl_fake.return_val = true; + zassert_true(board_is_sourcing_vbus(1)); + zassert_equal(tcpm_get_src_ctrl_fake.call_count, 1); + /* port */ + zassert_equal(tcpm_get_src_ctrl_fake.arg0_val, 1); +} + +ZTEST(usb_pd_policy, board_is_sourcing_vbus_c1_false) +{ + tcpm_get_src_ctrl_fake.return_val = false; + zassert_false(board_is_sourcing_vbus(1)); + zassert_equal(tcpm_get_src_ctrl_fake.call_count, 1); + /* port */ + zassert_equal(tcpm_get_src_ctrl_fake.arg0_val, 1); +} + +static void usb_pd_policy_before(void *fixture) +{ + ARG_UNUSED(fixture); + RESET_FAKE(ppc_vbus_source_enable); + RESET_FAKE(pd_set_vbus_discharge); + RESET_FAKE(pd_send_host_event); + RESET_FAKE(tcpm_get_src_ctrl); + RESET_FAKE(ppc_vbus_sink_enable); +} + +ZTEST_SUITE(usb_pd_policy, NULL, NULL, usb_pd_policy_before, NULL, NULL); diff --git a/zephyr/test/skyrim/testcase.yaml b/zephyr/test/skyrim/testcase.yaml index d65eb29738..29d22a2fcd 100644 --- a/zephyr/test/skyrim/testcase.yaml +++ b/zephyr/test/skyrim/testcase.yaml @@ -10,6 +10,17 @@ tests: extra_configs: - CONFIG_TEST_BOARD_BASEBOARD=y + skyrim.baseboard.usb_pd_policy: + extra_configs: + - CONFIG_TEST_BOARD_BASEBOARD=y + - CONFIG_TEST_BOARD_USB_PD_POLICY=y + + skyrim.baseboard.usb_pd_policy.discharge: + extra_configs: + - CONFIG_TEST_BOARD_BASEBOARD=y + - CONFIG_TEST_BOARD_USB_PD_POLICY=y + - CONFIG_USB_PD_DISCHARGE=y + # Crystaldrift tests skyrim.crystaldrift: extra_configs: -- cgit v1.2.1