xref: /openbmc/qemu/tests/qtest/aspeed_gpio-test.c (revision 1f30db92)
1 /*
2  * QTest testcase for the Aspeed GPIO Controller.
3  *
4  * Copyright (c) Meta Platforms, Inc. and affiliates. (http://www.meta.com)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "qemu/bitops.h"
27 #include "qemu/timer.h"
28 #include "qapi/qmp/qdict.h"
29 #include "libqtest-single.h"
30 
31 #define AST2600_GPIO_BASE 0x1E780000
32 
33 #define GPIO_ABCD_DATA_VALUE 0x000
34 #define GPIO_ABCD_DIRECTION  0x004
35 
test_set_colocated_pins(const void * data)36 static void test_set_colocated_pins(const void *data)
37 {
38     QTestState *s = (QTestState *)data;
39 
40     /*
41      * gpioV4-7 occupy bits within a single 32-bit value, so we want to make
42      * sure that modifying one doesn't affect the other.
43      */
44     qtest_qom_set_bool(s, "/machine/soc/gpio", "gpioV4", true);
45     qtest_qom_set_bool(s, "/machine/soc/gpio", "gpioV5", false);
46     qtest_qom_set_bool(s, "/machine/soc/gpio", "gpioV6", true);
47     qtest_qom_set_bool(s, "/machine/soc/gpio", "gpioV7", false);
48     g_assert(qtest_qom_get_bool(s, "/machine/soc/gpio", "gpioV4"));
49     g_assert(!qtest_qom_get_bool(s, "/machine/soc/gpio", "gpioV5"));
50     g_assert(qtest_qom_get_bool(s, "/machine/soc/gpio", "gpioV6"));
51     g_assert(!qtest_qom_get_bool(s, "/machine/soc/gpio", "gpioV7"));
52 }
53 
test_set_input_pins(const void * data)54 static void test_set_input_pins(const void *data)
55 {
56     QTestState *s = (QTestState *)data;
57     char name[16];
58     uint32_t value;
59 
60     qtest_writel(s, AST2600_GPIO_BASE + GPIO_ABCD_DIRECTION, 0x00000000);
61     for (char c = 'A'; c <= 'D'; c++) {
62         for (int i = 0; i < 8; i++) {
63             sprintf(name, "gpio%c%d", c, i);
64             qtest_qom_set_bool(s, "/machine/soc/gpio", name, true);
65         }
66     }
67     value = qtest_readl(s, AST2600_GPIO_BASE + GPIO_ABCD_DATA_VALUE);
68     g_assert_cmphex(value, ==, 0xffffffff);
69 
70     qtest_writel(s, AST2600_GPIO_BASE + GPIO_ABCD_DATA_VALUE, 0x00000000);
71     value = qtest_readl(s, AST2600_GPIO_BASE + GPIO_ABCD_DATA_VALUE);
72     g_assert_cmphex(value, ==, 0xffffffff);
73 }
74 
main(int argc,char ** argv)75 int main(int argc, char **argv)
76 {
77     QTestState *s;
78     int r;
79 
80     g_test_init(&argc, &argv, NULL);
81 
82     s = qtest_init("-machine ast2600-evb");
83     qtest_add_data_func("/ast2600/gpio/set_colocated_pins", s,
84                         test_set_colocated_pins);
85     qtest_add_data_func("/ast2600/gpio/set_input_pins", s, test_set_input_pins);
86     r = g_test_run();
87     qtest_quit(s);
88 
89     return r;
90 }
91