xref: /qemu/tests/qtest/cmsdk-apb-timer-test.c (revision b355f08a)
1 /*
2  * QTest testcase for the CMSDK APB timer device
3  *
4  * Copyright (c) 2021 Linaro Limited
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14  * for more details.
15  */
16 
17 #include "qemu/osdep.h"
18 #include "libqtest-single.h"
19 
20 /* IoTKit/ARMSSE-200 timer0; driven at 25MHz in mps2-an385, so 40ns per tick */
21 #define TIMER_BASE 0x40000000
22 
23 #define CTRL 0
24 #define VALUE 4
25 #define RELOAD 8
26 #define INTSTATUS 0xc
27 
28 static void test_timer(void)
29 {
30     g_assert_true(readl(TIMER_BASE + INTSTATUS) == 0);
31 
32     /* Start timer: will fire after 40 * 1000 == 40000 ns */
33     writel(TIMER_BASE + RELOAD, 1000);
34     writel(TIMER_BASE + CTRL, 9);
35 
36     /* Step to just past the 500th tick and check VALUE */
37     clock_step(40 * 500 + 1);
38     g_assert_cmpuint(readl(TIMER_BASE + INTSTATUS), ==, 0);
39     g_assert_cmpuint(readl(TIMER_BASE + VALUE), ==, 500);
40 
41     /* Just past the 1000th tick: timer should have fired */
42     clock_step(40 * 500);
43     g_assert_cmpuint(readl(TIMER_BASE + INTSTATUS), ==, 1);
44     g_assert_cmpuint(readl(TIMER_BASE + VALUE), ==, 0);
45 
46     /* VALUE reloads at the following tick */
47     clock_step(40);
48     g_assert_cmpuint(readl(TIMER_BASE + VALUE), ==, 1000);
49 
50     /* Check write-1-to-clear behaviour of INTSTATUS */
51     writel(TIMER_BASE + INTSTATUS, 0);
52     g_assert_cmpuint(readl(TIMER_BASE + INTSTATUS), ==, 1);
53     writel(TIMER_BASE + INTSTATUS, 1);
54     g_assert_cmpuint(readl(TIMER_BASE + INTSTATUS), ==, 0);
55 
56     /* Turn off the timer */
57     writel(TIMER_BASE + CTRL, 0);
58 }
59 
60 int main(int argc, char **argv)
61 {
62     int r;
63 
64     g_test_init(&argc, &argv, NULL);
65 
66     qtest_start("-machine mps2-an385");
67 
68     qtest_add_func("/cmsdk-apb-timer/timer", test_timer);
69 
70     r = g_test_run();
71 
72     qtest_end();
73 
74     return r;
75 }
76