xref: /openbmc/qemu/tests/qtest/dm163-test.c (revision d1e8bea9c9c1869bfbb28b7a3e9d8e3b6f8aca46)
1a0c325c4SInès Varhol /*
2a0c325c4SInès Varhol  * QTest testcase for DM163
3a0c325c4SInès Varhol  *
4a0c325c4SInès Varhol  * Copyright (C) 2024 Samuel Tardieu <sam@rfc1149.net>
5a0c325c4SInès Varhol  * Copyright (C) 2024 Arnaud Minier <arnaud.minier@telecom-paris.fr>
6a0c325c4SInès Varhol  * Copyright (C) 2024 Inès Varhol <ines.varhol@telecom-paris.fr>
7a0c325c4SInès Varhol  *
8a0c325c4SInès Varhol  * SPDX-License-Identifier: GPL-2.0-or-later
9a0c325c4SInès Varhol  */
10a0c325c4SInès Varhol 
11a0c325c4SInès Varhol #include "qemu/osdep.h"
12a0c325c4SInès Varhol #include "libqtest.h"
13a0c325c4SInès Varhol 
14a0c325c4SInès Varhol enum DM163_INPUTS {
15a0c325c4SInès Varhol     SIN = 8,
16a0c325c4SInès Varhol     DCK = 9,
17a0c325c4SInès Varhol     RST_B = 10,
18a0c325c4SInès Varhol     LAT_B = 11,
19a0c325c4SInès Varhol     SELBK = 12,
20a0c325c4SInès Varhol     EN_B = 13
21a0c325c4SInès Varhol };
22a0c325c4SInès Varhol 
23a0c325c4SInès Varhol #define DEVICE_NAME "/machine/dm163"
24a0c325c4SInès Varhol #define GPIO_OUT(name, value) qtest_set_irq_in(qts, DEVICE_NAME, NULL, name,   \
25a0c325c4SInès Varhol                                                value)
26a0c325c4SInès Varhol #define GPIO_PULSE(name)                                                       \
27a0c325c4SInès Varhol   do {                                                                         \
28a0c325c4SInès Varhol     GPIO_OUT(name, 1);                                                         \
29a0c325c4SInès Varhol     GPIO_OUT(name, 0);                                                         \
30a0c325c4SInès Varhol   } while (0)
31a0c325c4SInès Varhol 
32a0c325c4SInès Varhol 
rise_gpio_pin_dck(QTestState * qts)33a0c325c4SInès Varhol static void rise_gpio_pin_dck(QTestState *qts)
34a0c325c4SInès Varhol {
35a0c325c4SInès Varhol     /* Configure output mode for pin PB1 */
36a0c325c4SInès Varhol     qtest_writel(qts, 0x48000400, 0xFFFFFEB7);
37a0c325c4SInès Varhol     /* Write 1 in ODR for PB1 */
38a0c325c4SInès Varhol     qtest_writel(qts, 0x48000414, 0x00000002);
39a0c325c4SInès Varhol }
40a0c325c4SInès Varhol 
lower_gpio_pin_dck(QTestState * qts)41a0c325c4SInès Varhol static void lower_gpio_pin_dck(QTestState *qts)
42a0c325c4SInès Varhol {
43a0c325c4SInès Varhol     /* Configure output mode for pin PB1 */
44a0c325c4SInès Varhol     qtest_writel(qts, 0x48000400, 0xFFFFFEB7);
45a0c325c4SInès Varhol     /* Write 0 in ODR for PB1 */
46a0c325c4SInès Varhol     qtest_writel(qts, 0x48000414, 0x00000000);
47a0c325c4SInès Varhol }
48a0c325c4SInès Varhol 
rise_gpio_pin_selbk(QTestState * qts)49a0c325c4SInès Varhol static void rise_gpio_pin_selbk(QTestState *qts)
50a0c325c4SInès Varhol {
51a0c325c4SInès Varhol     /* Configure output mode for pin PC5 */
52a0c325c4SInès Varhol     qtest_writel(qts, 0x48000800, 0xFFFFF7FF);
53a0c325c4SInès Varhol     /* Write 1 in ODR for PC5 */
54a0c325c4SInès Varhol     qtest_writel(qts, 0x48000814, 0x00000020);
55a0c325c4SInès Varhol }
56a0c325c4SInès Varhol 
lower_gpio_pin_selbk(QTestState * qts)57a0c325c4SInès Varhol static void lower_gpio_pin_selbk(QTestState *qts)
58a0c325c4SInès Varhol {
59a0c325c4SInès Varhol     /* Configure output mode for pin PC5 */
60a0c325c4SInès Varhol     qtest_writel(qts, 0x48000800, 0xFFFFF7FF);
61a0c325c4SInès Varhol     /* Write 0 in ODR for PC5 */
62a0c325c4SInès Varhol     qtest_writel(qts, 0x48000814, 0x00000000);
63a0c325c4SInès Varhol }
64a0c325c4SInès Varhol 
rise_gpio_pin_lat_b(QTestState * qts)65a0c325c4SInès Varhol static void rise_gpio_pin_lat_b(QTestState *qts)
66a0c325c4SInès Varhol {
67a0c325c4SInès Varhol     /* Configure output mode for pin PC4 */
68a0c325c4SInès Varhol     qtest_writel(qts, 0x48000800, 0xFFFFFDFF);
69a0c325c4SInès Varhol     /* Write 1 in ODR for PC4 */
70a0c325c4SInès Varhol     qtest_writel(qts, 0x48000814, 0x00000010);
71a0c325c4SInès Varhol }
72a0c325c4SInès Varhol 
lower_gpio_pin_lat_b(QTestState * qts)73a0c325c4SInès Varhol static void lower_gpio_pin_lat_b(QTestState *qts)
74a0c325c4SInès Varhol {
75a0c325c4SInès Varhol     /* Configure output mode for pin PC4 */
76a0c325c4SInès Varhol     qtest_writel(qts, 0x48000800, 0xFFFFFDFF);
77a0c325c4SInès Varhol     /* Write 0 in ODR for PC4 */
78a0c325c4SInès Varhol     qtest_writel(qts, 0x48000814, 0x00000000);
79a0c325c4SInès Varhol }
80a0c325c4SInès Varhol 
rise_gpio_pin_rst_b(QTestState * qts)81a0c325c4SInès Varhol static void rise_gpio_pin_rst_b(QTestState *qts)
82a0c325c4SInès Varhol {
83a0c325c4SInès Varhol     /* Configure output mode for pin PC3 */
84a0c325c4SInès Varhol     qtest_writel(qts, 0x48000800, 0xFFFFFF7F);
85a0c325c4SInès Varhol     /* Write 1 in ODR for PC3 */
86a0c325c4SInès Varhol     qtest_writel(qts, 0x48000814, 0x00000008);
87a0c325c4SInès Varhol }
88a0c325c4SInès Varhol 
lower_gpio_pin_rst_b(QTestState * qts)89a0c325c4SInès Varhol static void lower_gpio_pin_rst_b(QTestState *qts)
90a0c325c4SInès Varhol {
91a0c325c4SInès Varhol     /* Configure output mode for pin PC3 */
92a0c325c4SInès Varhol     qtest_writel(qts, 0x48000800, 0xFFFFFF7F);
93a0c325c4SInès Varhol     /* Write 0 in ODR for PC3 */
94a0c325c4SInès Varhol     qtest_writel(qts, 0x48000814, 0x00000000);
95a0c325c4SInès Varhol }
96a0c325c4SInès Varhol 
rise_gpio_pin_sin(QTestState * qts)97a0c325c4SInès Varhol static void rise_gpio_pin_sin(QTestState *qts)
98a0c325c4SInès Varhol {
99a0c325c4SInès Varhol     /* Configure output mode for pin PA4 */
100a0c325c4SInès Varhol     qtest_writel(qts, 0x48000000, 0xFFFFFDFF);
101a0c325c4SInès Varhol     /* Write 1 in ODR for PA4 */
102a0c325c4SInès Varhol     qtest_writel(qts, 0x48000014, 0x00000010);
103a0c325c4SInès Varhol }
104a0c325c4SInès Varhol 
lower_gpio_pin_sin(QTestState * qts)105a0c325c4SInès Varhol static void lower_gpio_pin_sin(QTestState *qts)
106a0c325c4SInès Varhol {
107a0c325c4SInès Varhol     /* Configure output mode for pin PA4 */
108a0c325c4SInès Varhol     qtest_writel(qts, 0x48000000, 0xFFFFFDFF);
109a0c325c4SInès Varhol     /* Write 0 in ODR for PA4 */
110a0c325c4SInès Varhol     qtest_writel(qts, 0x48000014, 0x00000000);
111a0c325c4SInès Varhol }
112a0c325c4SInès Varhol 
test_dm163_bank(const void * opaque)113a0c325c4SInès Varhol static void test_dm163_bank(const void *opaque)
114a0c325c4SInès Varhol {
115a0c325c4SInès Varhol     const unsigned bank = (uintptr_t) opaque;
116a0c325c4SInès Varhol     const int width = bank ? 192 : 144;
117a0c325c4SInès Varhol 
118a0c325c4SInès Varhol     QTestState *qts = qtest_initf("-M b-l475e-iot01a");
119a0c325c4SInès Varhol     qtest_irq_intercept_out_named(qts, DEVICE_NAME, "sout");
120a0c325c4SInès Varhol     GPIO_OUT(RST_B, 1);
121a0c325c4SInès Varhol     GPIO_OUT(EN_B, 0);
122a0c325c4SInès Varhol     GPIO_OUT(DCK, 0);
123a0c325c4SInès Varhol     GPIO_OUT(SELBK, bank);
124a0c325c4SInès Varhol     GPIO_OUT(LAT_B, 1);
125a0c325c4SInès Varhol 
126a0c325c4SInès Varhol     /* Fill bank with zeroes */
127a0c325c4SInès Varhol     GPIO_OUT(SIN, 0);
128a0c325c4SInès Varhol     for (int i = 0; i < width; i++) {
129a0c325c4SInès Varhol         GPIO_PULSE(DCK);
130a0c325c4SInès Varhol     }
131a0c325c4SInès Varhol     /* Fill bank with ones, check that we get the previous zeroes */
132a0c325c4SInès Varhol     GPIO_OUT(SIN, 1);
133a0c325c4SInès Varhol     for (int i = 0; i < width; i++) {
134a0c325c4SInès Varhol         GPIO_PULSE(DCK);
135a0c325c4SInès Varhol         g_assert(!qtest_get_irq(qts, 0));
136a0c325c4SInès Varhol     }
137a0c325c4SInès Varhol 
138a0c325c4SInès Varhol     /* Pulse one more bit in the bank, check that we get a one */
139a0c325c4SInès Varhol     GPIO_PULSE(DCK);
140a0c325c4SInès Varhol     g_assert(qtest_get_irq(qts, 0));
141a0c325c4SInès Varhol 
142a0c325c4SInès Varhol     qtest_quit(qts);
143a0c325c4SInès Varhol }
144a0c325c4SInès Varhol 
test_dm163_gpio_connection(void)145a0c325c4SInès Varhol static void test_dm163_gpio_connection(void)
146a0c325c4SInès Varhol {
147a0c325c4SInès Varhol     QTestState *qts = qtest_init("-M b-l475e-iot01a");
148a0c325c4SInès Varhol     qtest_irq_intercept_in(qts, DEVICE_NAME);
149a0c325c4SInès Varhol 
150a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, SIN));
151a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, DCK));
152a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, RST_B));
153a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, LAT_B));
154a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, SELBK));
155a0c325c4SInès Varhol 
156a0c325c4SInès Varhol     rise_gpio_pin_dck(qts);
157a0c325c4SInès Varhol     g_assert_true(qtest_get_irq(qts, DCK));
158a0c325c4SInès Varhol     lower_gpio_pin_dck(qts);
159a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, DCK));
160a0c325c4SInès Varhol 
161a0c325c4SInès Varhol     rise_gpio_pin_lat_b(qts);
162a0c325c4SInès Varhol     g_assert_true(qtest_get_irq(qts, LAT_B));
163a0c325c4SInès Varhol     lower_gpio_pin_lat_b(qts);
164a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, LAT_B));
165a0c325c4SInès Varhol 
166a0c325c4SInès Varhol     rise_gpio_pin_selbk(qts);
167a0c325c4SInès Varhol     g_assert_true(qtest_get_irq(qts, SELBK));
168a0c325c4SInès Varhol     lower_gpio_pin_selbk(qts);
169a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, SELBK));
170a0c325c4SInès Varhol 
171a0c325c4SInès Varhol     rise_gpio_pin_rst_b(qts);
172a0c325c4SInès Varhol     g_assert_true(qtest_get_irq(qts, RST_B));
173a0c325c4SInès Varhol     lower_gpio_pin_rst_b(qts);
174a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, RST_B));
175a0c325c4SInès Varhol 
176a0c325c4SInès Varhol     rise_gpio_pin_sin(qts);
177a0c325c4SInès Varhol     g_assert_true(qtest_get_irq(qts, SIN));
178a0c325c4SInès Varhol     lower_gpio_pin_sin(qts);
179a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, SIN));
180a0c325c4SInès Varhol 
181a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, DCK));
182a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, LAT_B));
183a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, SELBK));
184a0c325c4SInès Varhol     g_assert_false(qtest_get_irq(qts, RST_B));
185*d1e8bea9SPeter Maydell 
186*d1e8bea9SPeter Maydell     qtest_quit(qts);
187a0c325c4SInès Varhol }
188a0c325c4SInès Varhol 
main(int argc,char ** argv)189a0c325c4SInès Varhol int main(int argc, char **argv)
190a0c325c4SInès Varhol {
191a0c325c4SInès Varhol     g_test_init(&argc, &argv, NULL);
192a0c325c4SInès Varhol     qtest_add_data_func("/dm163/bank0", (void *)0, test_dm163_bank);
193a0c325c4SInès Varhol     qtest_add_data_func("/dm163/bank1", (void *)1, test_dm163_bank);
194a0c325c4SInès Varhol     qtest_add_func("/dm163/gpio_connection", test_dm163_gpio_connection);
195a0c325c4SInès Varhol     return g_test_run();
196a0c325c4SInès Varhol }
197