target/alpha: Split out gen_goto_tb
[qemu/armbru.git] / tests / qtest / stm32l4x5_syscfg-test.c
blob506ca08bc243d7c932419b13ac9c03100d6323b0
1 /*
2 * QTest testcase for STM32L4x5_SYSCFG
4 * Copyright (c) 2024 Arnaud Minier <arnaud.minier@telecom-paris.fr>
5 * Copyright (c) 2024 Inès Varhol <ines.varhol@telecom-paris.fr>
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include "libqtest-single.h"
14 #define SYSCFG_BASE_ADDR 0x40010000
15 #define SYSCFG_MEMRMP 0x00
16 #define SYSCFG_CFGR1 0x04
17 #define SYSCFG_EXTICR1 0x08
18 #define SYSCFG_EXTICR2 0x0C
19 #define SYSCFG_EXTICR3 0x10
20 #define SYSCFG_EXTICR4 0x14
21 #define SYSCFG_SCSR 0x18
22 #define SYSCFG_CFGR2 0x1C
23 #define SYSCFG_SWPR 0x20
24 #define SYSCFG_SKR 0x24
25 #define SYSCFG_SWPR2 0x28
26 #define INVALID_ADDR 0x2C
28 /* SoC forwards GPIOs to SysCfg */
29 #define SYSCFG "/machine/soc"
30 #define EXTI "/machine/soc/exti"
32 static void syscfg_writel(unsigned int offset, uint32_t value)
34 writel(SYSCFG_BASE_ADDR + offset, value);
37 static uint32_t syscfg_readl(unsigned int offset)
39 return readl(SYSCFG_BASE_ADDR + offset);
42 static void syscfg_set_irq(int num, int level)
44 qtest_set_irq_in(global_qtest, SYSCFG, NULL, num, level);
47 static void system_reset(void)
49 QDict *response;
50 response = qtest_qmp(global_qtest, "{'execute': 'system_reset'}");
51 g_assert(qdict_haskey(response, "return"));
52 qobject_unref(response);
55 static void test_reset(void)
58 * Test that registers are initialized at the correct values
60 g_assert_cmphex(syscfg_readl(SYSCFG_MEMRMP), ==, 0x00000000);
62 g_assert_cmphex(syscfg_readl(SYSCFG_CFGR1), ==, 0x7C000001);
64 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR1), ==, 0x00000000);
66 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR2), ==, 0x00000000);
68 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR3), ==, 0x00000000);
70 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR4), ==, 0x00000000);
72 g_assert_cmphex(syscfg_readl(SYSCFG_SCSR), ==, 0x00000000);
74 g_assert_cmphex(syscfg_readl(SYSCFG_CFGR2), ==, 0x00000000);
76 g_assert_cmphex(syscfg_readl(SYSCFG_SWPR), ==, 0x00000000);
78 g_assert_cmphex(syscfg_readl(SYSCFG_SKR), ==, 0x00000000);
80 g_assert_cmphex(syscfg_readl(SYSCFG_SWPR2), ==, 0x00000000);
83 static void test_reserved_bits(void)
86 * Test that reserved bits stay at reset value
87 * (which is 0 for all of them) by writing '1'
88 * in all reserved bits (keeping reset value for
89 * other bits) and checking that the
90 * register is still at reset value
92 syscfg_writel(SYSCFG_MEMRMP, 0xFFFFFEF8);
93 g_assert_cmphex(syscfg_readl(SYSCFG_MEMRMP), ==, 0x00000000);
95 syscfg_writel(SYSCFG_CFGR1, 0x7F00FEFF);
96 g_assert_cmphex(syscfg_readl(SYSCFG_CFGR1), ==, 0x7C000001);
98 syscfg_writel(SYSCFG_EXTICR1, 0xFFFF0000);
99 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR1), ==, 0x00000000);
101 syscfg_writel(SYSCFG_EXTICR2, 0xFFFF0000);
102 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR2), ==, 0x00000000);
104 syscfg_writel(SYSCFG_EXTICR3, 0xFFFF0000);
105 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR3), ==, 0x00000000);
107 syscfg_writel(SYSCFG_EXTICR4, 0xFFFF0000);
108 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR4), ==, 0x00000000);
110 syscfg_writel(SYSCFG_SKR, 0xFFFFFF00);
111 g_assert_cmphex(syscfg_readl(SYSCFG_SKR), ==, 0x00000000);
114 static void test_set_and_clear(void)
117 * Test that regular bits can be set and cleared
119 syscfg_writel(SYSCFG_MEMRMP, 0x00000107);
120 g_assert_cmphex(syscfg_readl(SYSCFG_MEMRMP), ==, 0x00000107);
121 syscfg_writel(SYSCFG_MEMRMP, 0x00000000);
122 g_assert_cmphex(syscfg_readl(SYSCFG_MEMRMP), ==, 0x00000000);
124 /* cfgr1 bit 0 is clear only so we keep it set */
125 syscfg_writel(SYSCFG_CFGR1, 0xFCFF0101);
126 g_assert_cmphex(syscfg_readl(SYSCFG_CFGR1), ==, 0xFCFF0101);
127 syscfg_writel(SYSCFG_CFGR1, 0x00000001);
128 g_assert_cmphex(syscfg_readl(SYSCFG_CFGR1), ==, 0x00000001);
130 syscfg_writel(SYSCFG_EXTICR1, 0x0000FFFF);
131 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR1), ==, 0x0000FFFF);
132 syscfg_writel(SYSCFG_EXTICR1, 0x00000000);
133 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR1), ==, 0x00000000);
135 syscfg_writel(SYSCFG_EXTICR2, 0x0000FFFF);
136 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR2), ==, 0x0000FFFF);
137 syscfg_writel(SYSCFG_EXTICR2, 0x00000000);
138 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR2), ==, 0x00000000);
140 syscfg_writel(SYSCFG_EXTICR3, 0x0000FFFF);
141 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR3), ==, 0x0000FFFF);
142 syscfg_writel(SYSCFG_EXTICR3, 0x00000000);
143 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR3), ==, 0x00000000);
145 syscfg_writel(SYSCFG_EXTICR4, 0x0000FFFF);
146 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR4), ==, 0x0000FFFF);
147 syscfg_writel(SYSCFG_EXTICR4, 0x00000000);
148 g_assert_cmphex(syscfg_readl(SYSCFG_EXTICR4), ==, 0x00000000);
150 syscfg_writel(SYSCFG_SKR, 0x000000FF);
151 g_assert_cmphex(syscfg_readl(SYSCFG_SKR), ==, 0x000000FF);
152 syscfg_writel(SYSCFG_SKR, 0x00000000);
153 g_assert_cmphex(syscfg_readl(SYSCFG_SKR), ==, 0x00000000);
156 static void test_clear_by_writing_1(void)
159 * Test that writing '1' doesn't set the bit
161 syscfg_writel(SYSCFG_CFGR2, 0x00000100);
162 g_assert_cmphex(syscfg_readl(SYSCFG_CFGR2), ==, 0x00000000);
165 static void test_set_only_bits(void)
168 * Test that set only bits stay can't be cleared
170 syscfg_writel(SYSCFG_CFGR2, 0x0000000F);
171 syscfg_writel(SYSCFG_CFGR2, 0x00000000);
172 g_assert_cmphex(syscfg_readl(SYSCFG_CFGR2), ==, 0x0000000F);
174 syscfg_writel(SYSCFG_SWPR, 0xFFFFFFFF);
175 syscfg_writel(SYSCFG_SWPR, 0x00000000);
176 g_assert_cmphex(syscfg_readl(SYSCFG_SWPR), ==, 0xFFFFFFFF);
178 syscfg_writel(SYSCFG_SWPR2, 0xFFFFFFFF);
179 syscfg_writel(SYSCFG_SWPR2, 0x00000000);
180 g_assert_cmphex(syscfg_readl(SYSCFG_SWPR2), ==, 0xFFFFFFFF);
182 system_reset();
185 static void test_clear_only_bits(void)
188 * Test that clear only bits stay can't be set
190 syscfg_writel(SYSCFG_CFGR1, 0x00000000);
191 syscfg_writel(SYSCFG_CFGR1, 0x00000001);
192 g_assert_cmphex(syscfg_readl(SYSCFG_CFGR1), ==, 0x00000000);
194 system_reset();
197 static void test_interrupt(void)
200 * Test that GPIO rising lines result in an irq
201 * with the right configuration
203 qtest_irq_intercept_in(global_qtest, EXTI);
205 /* GPIOA is the default source for EXTI lines 0 to 15 */
207 syscfg_set_irq(0, 1);
209 g_assert_true(get_irq(0));
212 syscfg_set_irq(15, 1);
214 g_assert_true(get_irq(15));
216 /* Configure GPIOB[1] as the source input for EXTI1 */
217 syscfg_writel(SYSCFG_EXTICR1, 0x00000010);
219 syscfg_set_irq(17, 1);
221 g_assert_true(get_irq(1));
223 /* Clean the test */
224 syscfg_writel(SYSCFG_EXTICR1, 0x00000000);
225 syscfg_set_irq(0, 0);
226 syscfg_set_irq(15, 0);
227 syscfg_set_irq(17, 0);
230 static void test_irq_pin_multiplexer(void)
233 * Test that syscfg irq sets the right exti irq
236 qtest_irq_intercept_in(global_qtest, EXTI);
238 syscfg_set_irq(0, 1);
240 /* Check that irq 0 was set and irq 15 wasn't */
241 g_assert_true(get_irq(0));
242 g_assert_false(get_irq(15));
244 /* Clean the test */
245 syscfg_set_irq(0, 0);
247 syscfg_set_irq(15, 1);
249 /* Check that irq 15 was set and irq 0 wasn't */
250 g_assert_true(get_irq(15));
251 g_assert_false(get_irq(0));
253 /* Clean the test */
254 syscfg_set_irq(15, 0);
257 static void test_irq_gpio_multiplexer(void)
260 * Test that an irq is generated only by the right GPIO
263 qtest_irq_intercept_in(global_qtest, EXTI);
265 /* GPIOA is the default source for EXTI lines 0 to 15 */
267 /* Check that setting rising pin GPIOA[0] generates an irq */
268 syscfg_set_irq(0, 1);
270 g_assert_true(get_irq(0));
272 /* Clean the test */
273 syscfg_set_irq(0, 0);
275 /* Check that setting rising pin GPIOB[0] doesn't generate an irq */
276 syscfg_set_irq(16, 1);
278 g_assert_false(get_irq(0));
280 /* Clean the test */
281 syscfg_set_irq(16, 0);
283 /* Configure GPIOB[0] as the source input for EXTI0 */
284 syscfg_writel(SYSCFG_EXTICR1, 0x00000001);
286 /* Check that setting rising pin GPIOA[0] doesn't generate an irq */
287 syscfg_set_irq(0, 1);
289 g_assert_false(get_irq(0));
291 /* Clean the test */
292 syscfg_set_irq(0, 0);
294 /* Check that setting rising pin GPIOB[0] generates an irq */
295 syscfg_set_irq(16, 1);
297 g_assert_true(get_irq(0));
299 /* Clean the test */
300 syscfg_set_irq(16, 0);
301 syscfg_writel(SYSCFG_EXTICR1, 0x00000000);
304 int main(int argc, char **argv)
306 int ret;
308 g_test_init(&argc, &argv, NULL);
309 g_test_set_nonfatal_assertions();
311 qtest_add_func("stm32l4x5/syscfg/test_reset", test_reset);
312 qtest_add_func("stm32l4x5/syscfg/test_reserved_bits",
313 test_reserved_bits);
314 qtest_add_func("stm32l4x5/syscfg/test_set_and_clear",
315 test_set_and_clear);
316 qtest_add_func("stm32l4x5/syscfg/test_clear_by_writing_1",
317 test_clear_by_writing_1);
318 qtest_add_func("stm32l4x5/syscfg/test_set_only_bits",
319 test_set_only_bits);
320 qtest_add_func("stm32l4x5/syscfg/test_clear_only_bits",
321 test_clear_only_bits);
322 qtest_add_func("stm32l4x5/syscfg/test_interrupt",
323 test_interrupt);
324 qtest_add_func("stm32l4x5/syscfg/test_irq_pin_multiplexer",
325 test_irq_pin_multiplexer);
326 qtest_add_func("stm32l4x5/syscfg/test_irq_gpio_multiplexer",
327 test_irq_gpio_multiplexer);
329 qtest_start("-machine b-l475e-iot01a");
330 ret = g_test_run();
331 qtest_end();
333 return ret;