diff --git a/tests/kernel/device/app.overlay b/tests/kernel/device/app.overlay index d7426f2b6134f86..108f14bdb3ec990 100644 --- a/tests/kernel/device/app.overlay +++ b/tests/kernel/device/app.overlay @@ -52,6 +52,20 @@ status = "okay"; }; + fakedeferdriver@E7000000 { + compatible = "fakedeferdriver"; + reg = <0xE7000000 0x2000>; + status = "okay"; + zephyr,deferred-init; + }; + + fakedeferdriver@E8000000 { + compatible = "fakedeferdriver"; + reg = <0xE8000000 0x2000>; + status = "okay"; + zephyr,deferred-init; + }; + fakedomain_0: fakedomain_0 { compatible = "fakedomain"; status = "okay"; diff --git a/tests/kernel/device/boards/hifive_unmatched.overlay b/tests/kernel/device/boards/hifive_unmatched.overlay index 40fa5e74adb48da..05f9992d889c141 100644 --- a/tests/kernel/device/boards/hifive_unmatched.overlay +++ b/tests/kernel/device/boards/hifive_unmatched.overlay @@ -49,6 +49,20 @@ status = "okay"; }; + fakedeferdriver@E7000000 { + compatible = "fakedeferdriver"; + reg = <0x0 0xE7000000 0x0 0x2000>; + status = "okay"; + zephyr,deferred-init; + }; + + fakedeferdriver@E8000000 { + compatible = "fakedeferdriver"; + reg = <0x0 0xE8000000 0x0 0x2000>; + status = "okay"; + zephyr,deferred-init; + }; + fakedomain_0: fakedomain_0 { compatible = "fakedomain"; status = "okay"; diff --git a/tests/kernel/device/dts/bindings/fakedeferdriver.yml b/tests/kernel/device/dts/bindings/fakedeferdriver.yml new file mode 100644 index 000000000000000..672ed99d1d3ac00 --- /dev/null +++ b/tests/kernel/device/dts/bindings/fakedeferdriver.yml @@ -0,0 +1,8 @@ +# Copyright (c) 2023 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +description: Properties for fake deferred driver. + +compatible: "fakedeferdriver" + +include: base.yaml diff --git a/tests/kernel/device/src/main.c b/tests/kernel/device/src/main.c index 4c02a52ce94ed9b..437f2ae056c746f 100644 --- a/tests/kernel/device/src/main.c +++ b/tests/kernel/device/src/main.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -21,10 +22,23 @@ #define MY_DRIVER_A "my_driver_A" #define MY_DRIVER_B "my_driver_B" +#define FAKEDEFERDRIVER0 DEVICE_DT_GET(DT_PATH(fakedeferdriver_e7000000)) +#define FAKEDEFERDRIVER1 DEVICE_DT_GET(DT_PATH(fakedeferdriver_e8000000)) + /* A device without init call */ DEVICE_DEFINE(dummy_noinit, DUMMY_NOINIT, NULL, NULL, NULL, NULL, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL); +/* To access from userspace, the device needs an API. Use a dummy GPIO one */ +static const struct gpio_driver_api fakedeferdriverapi; + +/* Fake deferred devices */ +DEVICE_DT_DEFINE(DT_INST(0, fakedeferdriver), NULL, NULL, NULL, NULL, + POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL); +DEVICE_DT_DEFINE(DT_INST(1, fakedeferdriver), NULL, NULL, NULL, NULL, + POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &fakedeferdriverapi); + /** * @brief Test cases to verify device objects * @@ -387,9 +401,41 @@ ZTEST(device, test_abstraction_driver_common) zassert_true(baz == 2, "common API do_that fail"); } +ZTEST(device, test_deferred_init) +{ + int ret; + + zassert_false(device_is_ready(FAKEDEFERDRIVER0)); + + ret = device_init(FAKEDEFERDRIVER0); + zassert_true(ret == 0); + + zassert_true(device_is_ready(FAKEDEFERDRIVER0)); +} + +ZTEST_USER(device, test_deferred_init_user) +{ + int ret; + + zassert_false(device_is_ready(FAKEDEFERDRIVER1)); + + ret = device_init(FAKEDEFERDRIVER1); + zassert_true(ret == 0); + + zassert_true(device_is_ready(FAKEDEFERDRIVER1)); +} + +void *user_setup(void) +{ +#ifdef CONFIG_USERSPACE + k_object_access_grant(FAKEDEFERDRIVER1, k_current_get()); +#endif + + return NULL; +} /** * @} */ -ZTEST_SUITE(device, NULL, NULL, NULL, NULL, NULL); +ZTEST_SUITE(device, NULL, user_setup, NULL, NULL, NULL);