1 /*
2  * Copyright (c) 2025 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/ztest.h>
9 #if defined(CONFIG_NORDIC_QSPI_NOR)
10 #include <zephyr/drivers/flash/nrf_qspi_nor.h>
11 #endif
12 
13 #include "extflash.h"
14 
15 #define EXTFLASH_NODE DT_NODELABEL(dut)
16 
17 static const struct device *dev_flash = DEVICE_DT_GET(EXTFLASH_NODE);
18 
check_extflash_string(void)19 static void check_extflash_string(void)
20 {
21 	TC_PRINT("Accessing extflash_string at %p: %s\n",
22 		extflash_string, extflash_string);
23 	zassert_equal(0, strcmp(extflash_string, EXPECTED_EXTFLASH_STRING));
24 }
25 
xip_enable(bool enable)26 static void xip_enable(bool enable)
27 {
28 #if defined(CONFIG_NORDIC_QSPI_NOR)
29 	nrf_qspi_nor_xip_enable(dev_flash, enable);
30 #endif
31 }
32 
ZTEST(xip,test_xip_enable_disable)33 ZTEST(xip, test_xip_enable_disable)
34 {
35 	xip_enable(true);
36 	extflash_function1();
37 	check_extflash_string();
38 	xip_enable(false);
39 
40 	/* This is to ensure that the next XIP access will result in a new
41 	 * transfer from the flash chip, as the required data will not be
42 	 * available in cache.
43 	 */
44 	k_sleep(K_MSEC(10));
45 
46 	xip_enable(true);
47 	extflash_function2();
48 	check_extflash_string();
49 	xip_enable(false);
50 }
51 
ZTEST(xip,test_xip_enabled_at_boot)52 ZTEST(xip, test_xip_enabled_at_boot)
53 {
54 	if (!IS_ENABLED(CONFIG_NORDIC_QSPI_NOR_XIP)) {
55 		ztest_test_skip();
56 	}
57 
58 	extflash_function1();
59 	check_extflash_string();
60 
61 	xip_enable(true);
62 	extflash_function2();
63 	xip_enable(false);
64 
65 	k_sleep(K_MSEC(10));
66 
67 	/* XIP enabled at boot should stay active after it is temporarily
68 	 * enabled at runtime.
69 	 */
70 	extflash_function1();
71 	check_extflash_string();
72 }
73 
third_xip_user(void)74 static void third_xip_user(void)
75 {
76 	xip_enable(true);
77 	check_extflash_string();
78 	xip_enable(false);
79 }
80 
second_xip_user(void)81 static void second_xip_user(void)
82 {
83 	xip_enable(true);
84 
85 	extflash_function2();
86 
87 	third_xip_user();
88 
89 	xip_enable(false);
90 }
91 
ZTEST(xip,test_xip_multiple_users)92 ZTEST(xip, test_xip_multiple_users)
93 {
94 	xip_enable(true);
95 
96 	extflash_function1();
97 
98 	second_xip_user();
99 
100 	extflash_function1();
101 	check_extflash_string();
102 
103 	xip_enable(false);
104 }
105 
106 ZTEST_SUITE(xip, NULL, NULL, NULL, NULL, NULL);
107