1 /*
2  * QTest testcase for the ASPEED AST2700 GPIO Controller.
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  * Copyright (C) 2024 ASPEED Technology Inc.
6  */
7 
8 #include "qemu/osdep.h"
9 #include "qemu/bitops.h"
10 #include "qemu/timer.h"
11 #include "qapi/qmp/qdict.h"
12 #include "libqtest-single.h"
13 
14 #define AST2700_GPIO_BASE 0x14C0B000
15 #define GPIOA0_CONTROL 0x180
16 
test_output_pins(const char * machine,const uint32_t base)17 static void test_output_pins(const char *machine, const uint32_t base)
18 {
19     QTestState *s = qtest_init(machine);
20     uint32_t offset = 0;
21     uint32_t value = 0;
22     uint32_t pin = 0;
23 
24     for (char c = 'A'; c <= 'D'; c++) {
25         for (int i = 0; i < 8; i++) {
26             offset = base + (pin * 4);
27 
28             /* output direction and output hi */
29             qtest_writel(s, offset, 0x00000003);
30             value = qtest_readl(s, offset);
31             g_assert_cmphex(value, ==, 0x00000003);
32 
33             /* output direction and output low */
34             qtest_writel(s, offset, 0x00000002);
35             value = qtest_readl(s, offset);
36             g_assert_cmphex(value, ==, 0x00000002);
37             pin++;
38         }
39     }
40 
41     qtest_quit(s);
42 }
43 
test_input_pins(const char * machine,const uint32_t base)44 static void test_input_pins(const char *machine, const uint32_t base)
45 {
46     QTestState *s = qtest_init(machine);
47     char name[16];
48     uint32_t offset = 0;
49     uint32_t value = 0;
50     uint32_t pin = 0;
51 
52     for (char c = 'A'; c <= 'D'; c++) {
53         for (int i = 0; i < 8; i++) {
54             sprintf(name, "gpio%c%d", c, i);
55             offset = base + (pin * 4);
56             /* input direction */
57             qtest_writel(s, offset, 0);
58 
59             /* set input */
60             qtest_qom_set_bool(s, "/machine/soc/gpio", name, true);
61             value = qtest_readl(s, offset);
62             g_assert_cmphex(value, ==, 0x00002000);
63 
64             /* clear input */
65             qtest_qom_set_bool(s, "/machine/soc/gpio", name, false);
66             value = qtest_readl(s, offset);
67             g_assert_cmphex(value, ==, 0);
68             pin++;
69         }
70     }
71 
72     qtest_quit(s);
73 }
74 
test_2700_input_pins(void)75 static void test_2700_input_pins(void)
76 {
77     test_input_pins("-machine ast2700-evb",
78                     AST2700_GPIO_BASE + GPIOA0_CONTROL);
79 }
80 
test_2700_output_pins(void)81 static void test_2700_output_pins(void)
82 {
83     test_output_pins("-machine ast2700-evb",
84                      AST2700_GPIO_BASE + GPIOA0_CONTROL);
85 }
86 
main(int argc,char ** argv)87 int main(int argc, char **argv)
88 {
89     g_test_init(&argc, &argv, NULL);
90 
91     qtest_add_func("/ast2700/gpio/input_pins", test_2700_input_pins);
92     qtest_add_func("/ast2700/gpio/output_pins", test_2700_output_pins);
93 
94     return g_test_run();
95 }
96