Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20210330' into...
[qemu/ar7.git] / tests / qtest / cmsdk-apb-timer-test.c
blobe85e1f7448e454950fd84217973b83fb76b0b3ce
1 /*
2 * QTest testcase for the CMSDK APB timer device
4 * Copyright (c) 2021 Linaro Limited
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.
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.
17 #include "qemu/osdep.h"
18 #include "libqtest-single.h"
20 /* IoTKit/ARMSSE-200 timer0; driven at 25MHz in mps2-an385, so 40ns per tick */
21 #define TIMER_BASE 0x40000000
23 #define CTRL 0
24 #define VALUE 4
25 #define RELOAD 8
26 #define INTSTATUS 0xc
28 static void test_timer(void)
30 g_assert_true(readl(TIMER_BASE + INTSTATUS) == 0);
32 /* Start timer: will fire after 40 * 1000 == 40000 ns */
33 writel(TIMER_BASE + RELOAD, 1000);
34 writel(TIMER_BASE + CTRL, 9);
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);
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);
46 /* VALUE reloads at the following tick */
47 clock_step(40);
48 g_assert_cmpuint(readl(TIMER_BASE + VALUE), ==, 1000);
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);
56 /* Turn off the timer */
57 writel(TIMER_BASE + CTRL, 0);
60 int main(int argc, char **argv)
62 int r;
64 g_test_init(&argc, &argv, NULL);
66 qtest_start("-machine mps2-an385");
68 qtest_add_func("/cmsdk-apb-timer/timer", test_timer);
70 r = g_test_run();
72 qtest_end();
74 return r;