11eca58aaSPeter Maydell /* 21eca58aaSPeter Maydell * QTest testcase for the SSE timer device 31eca58aaSPeter Maydell * 41eca58aaSPeter Maydell * Copyright (c) 2021 Linaro Limited 51eca58aaSPeter Maydell * 61eca58aaSPeter Maydell * This program is free software; you can redistribute it and/or modify it 71eca58aaSPeter Maydell * under the terms of the GNU General Public License as published by the 81eca58aaSPeter Maydell * Free Software Foundation; either version 2 of the License, or 91eca58aaSPeter Maydell * (at your option) any later version. 101eca58aaSPeter Maydell * 111eca58aaSPeter Maydell * This program is distributed in the hope that it will be useful, but WITHOUT 121eca58aaSPeter Maydell * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 131eca58aaSPeter Maydell * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 141eca58aaSPeter Maydell * for more details. 151eca58aaSPeter Maydell */ 161eca58aaSPeter Maydell 171eca58aaSPeter Maydell #include "qemu/osdep.h" 181eca58aaSPeter Maydell #include "libqtest-single.h" 191eca58aaSPeter Maydell 201eca58aaSPeter Maydell /* 211eca58aaSPeter Maydell * SSE-123/SSE-300 timer in the mps3-an547 board, where it is driven 221eca58aaSPeter Maydell * at 32MHz, so 31.25ns per tick. 231eca58aaSPeter Maydell */ 241eca58aaSPeter Maydell #define TIMER_BASE 0x48000000 251eca58aaSPeter Maydell 261eca58aaSPeter Maydell /* PERIPHNSPPC0 register in the SSE-300 Secure Access Configuration block */ 271eca58aaSPeter Maydell #define PERIPHNSPPC0 (0x50080000 + 0x70) 281eca58aaSPeter Maydell 291eca58aaSPeter Maydell /* Base of the System Counter control frame */ 301eca58aaSPeter Maydell #define COUNTER_BASE 0x58100000 311eca58aaSPeter Maydell 321eca58aaSPeter Maydell /* SSE counter register offsets in the control frame */ 331eca58aaSPeter Maydell #define CNTCR 0 341eca58aaSPeter Maydell #define CNTSR 0x4 351eca58aaSPeter Maydell #define CNTCV_LO 0x8 361eca58aaSPeter Maydell #define CNTCV_HI 0xc 371eca58aaSPeter Maydell #define CNTSCR 0x10 381eca58aaSPeter Maydell 391eca58aaSPeter Maydell /* SSE timer register offsets */ 401eca58aaSPeter Maydell #define CNTPCT_LO 0 411eca58aaSPeter Maydell #define CNTPCT_HI 4 421eca58aaSPeter Maydell #define CNTFRQ 0x10 431eca58aaSPeter Maydell #define CNTP_CVAL_LO 0x20 441eca58aaSPeter Maydell #define CNTP_CVAL_HI 0x24 451eca58aaSPeter Maydell #define CNTP_TVAL 0x28 461eca58aaSPeter Maydell #define CNTP_CTL 0x2c 471eca58aaSPeter Maydell #define CNTP_AIVAL_LO 0x40 481eca58aaSPeter Maydell #define CNTP_AIVAL_HI 0x44 491eca58aaSPeter Maydell #define CNTP_AIVAL_RELOAD 0x48 501eca58aaSPeter Maydell #define CNTP_AIVAL_CTL 0x4c 511eca58aaSPeter Maydell 521eca58aaSPeter Maydell /* 4 ticks in nanoseconds (so we can work in integers) */ 531eca58aaSPeter Maydell #define FOUR_TICKS 125 541eca58aaSPeter Maydell 551eca58aaSPeter Maydell static void clock_step_ticks(uint64_t ticks) 561eca58aaSPeter Maydell { 571eca58aaSPeter Maydell /* 581eca58aaSPeter Maydell * Advance the qtest clock by however many nanoseconds we 591eca58aaSPeter Maydell * need to move the timer forward the specified number of ticks. 601eca58aaSPeter Maydell * ticks must be a multiple of 4, so we get a whole number of ns. 611eca58aaSPeter Maydell */ 621eca58aaSPeter Maydell assert(!(ticks & 3)); 631eca58aaSPeter Maydell clock_step(FOUR_TICKS * (ticks >> 2)); 641eca58aaSPeter Maydell } 651eca58aaSPeter Maydell 661eca58aaSPeter Maydell static void reset_counter_and_timer(void) 671eca58aaSPeter Maydell { 681eca58aaSPeter Maydell /* 691eca58aaSPeter Maydell * Reset the system counter and the timer between tests. This 701eca58aaSPeter Maydell * isn't a full reset, but it's sufficient for what the tests check. 711eca58aaSPeter Maydell */ 721eca58aaSPeter Maydell writel(COUNTER_BASE + CNTCR, 0); 731eca58aaSPeter Maydell writel(TIMER_BASE + CNTP_CTL, 0); 741eca58aaSPeter Maydell writel(TIMER_BASE + CNTP_AIVAL_CTL, 0); 751eca58aaSPeter Maydell writel(COUNTER_BASE + CNTCV_LO, 0); 761eca58aaSPeter Maydell writel(COUNTER_BASE + CNTCV_HI, 0); 771eca58aaSPeter Maydell } 781eca58aaSPeter Maydell 791eca58aaSPeter Maydell static void test_counter(void) 801eca58aaSPeter Maydell { 811eca58aaSPeter Maydell /* Basic counter functionality test */ 821eca58aaSPeter Maydell 831eca58aaSPeter Maydell reset_counter_and_timer(); 841eca58aaSPeter Maydell /* The counter should start disabled: check that it doesn't move */ 851eca58aaSPeter Maydell clock_step_ticks(100); 861eca58aaSPeter Maydell g_assert_cmpuint(readl(COUNTER_BASE + CNTCV_LO), ==, 0); 871eca58aaSPeter Maydell g_assert_cmpuint(readl(COUNTER_BASE + CNTCV_HI), ==, 0); 881eca58aaSPeter Maydell /* Now enable it and check that it does count */ 891eca58aaSPeter Maydell writel(COUNTER_BASE + CNTCR, 1); 901eca58aaSPeter Maydell clock_step_ticks(100); 911eca58aaSPeter Maydell g_assert_cmpuint(readl(COUNTER_BASE + CNTCV_LO), ==, 100); 921eca58aaSPeter Maydell g_assert_cmpuint(readl(COUNTER_BASE + CNTCV_HI), ==, 0); 931eca58aaSPeter Maydell /* Check the counter scaling functionality */ 941eca58aaSPeter Maydell writel(COUNTER_BASE + CNTCR, 0); 951eca58aaSPeter Maydell writel(COUNTER_BASE + CNTSCR, 0x00100000); /* 1/16th normal speed */ 961eca58aaSPeter Maydell writel(COUNTER_BASE + CNTCR, 5); /* EN, SCEN */ 971eca58aaSPeter Maydell clock_step_ticks(160); 981eca58aaSPeter Maydell g_assert_cmpuint(readl(COUNTER_BASE + CNTCV_LO), ==, 110); 991eca58aaSPeter Maydell g_assert_cmpuint(readl(COUNTER_BASE + CNTCV_HI), ==, 0); 1001eca58aaSPeter Maydell } 1011eca58aaSPeter Maydell 102f277d1c3SPeter Maydell static void test_timer(void) 103f277d1c3SPeter Maydell { 104f277d1c3SPeter Maydell /* Basic timer functionality test */ 105f277d1c3SPeter Maydell 106f277d1c3SPeter Maydell reset_counter_and_timer(); 107f277d1c3SPeter Maydell /* 108f277d1c3SPeter Maydell * The timer is behind a Peripheral Protection Controller, and 109f277d1c3SPeter Maydell * qtest accesses are always non-secure (no memory attributes), 110f277d1c3SPeter Maydell * so we must program the PPC to accept NS transactions. TIMER0 111f277d1c3SPeter Maydell * is on port 0 of PPC0, controlled by bit 0 of this register. 112f277d1c3SPeter Maydell */ 113f277d1c3SPeter Maydell writel(PERIPHNSPPC0, 1); 114f277d1c3SPeter Maydell /* We must enable the System Counter or the timer won't run. */ 115f277d1c3SPeter Maydell writel(COUNTER_BASE + CNTCR, 1); 116f277d1c3SPeter Maydell 117f277d1c3SPeter Maydell /* Timer starts disabled and with a counter of 0 */ 118f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 0); 119f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTPCT_LO), ==, 0); 120f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTPCT_HI), ==, 0); 121f277d1c3SPeter Maydell 122f277d1c3SPeter Maydell /* Turn it on */ 123f277d1c3SPeter Maydell writel(TIMER_BASE + CNTP_CTL, 1); 124f277d1c3SPeter Maydell 125f277d1c3SPeter Maydell /* Is the timer ticking? */ 126f277d1c3SPeter Maydell clock_step_ticks(100); 127f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTPCT_LO), ==, 100); 128f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTPCT_HI), ==, 0); 129f277d1c3SPeter Maydell 130f277d1c3SPeter Maydell /* Set the CompareValue to 4000 ticks */ 131f277d1c3SPeter Maydell writel(TIMER_BASE + CNTP_CVAL_LO, 4000); 132f277d1c3SPeter Maydell writel(TIMER_BASE + CNTP_CVAL_HI, 0); 133f277d1c3SPeter Maydell 134f277d1c3SPeter Maydell /* Check TVAL view of the counter */ 135f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_TVAL), ==, 3900); 136f277d1c3SPeter Maydell 137f277d1c3SPeter Maydell /* Advance to the CompareValue mark and check ISTATUS is set */ 138f277d1c3SPeter Maydell clock_step_ticks(3900); 139f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_TVAL), ==, 0); 140f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 5); 141f277d1c3SPeter Maydell 142f277d1c3SPeter Maydell /* Now exercise the auto-reload part of the timer */ 143f277d1c3SPeter Maydell writel(TIMER_BASE + CNTP_AIVAL_RELOAD, 200); 144f277d1c3SPeter Maydell writel(TIMER_BASE + CNTP_AIVAL_CTL, 1); 145f277d1c3SPeter Maydell 146f277d1c3SPeter Maydell /* Check AIVAL was reloaded and that ISTATUS is now clear */ 147f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_LO), ==, 4200); 148f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_HI), ==, 0); 149f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 1); 150f277d1c3SPeter Maydell 151f277d1c3SPeter Maydell /* 152f277d1c3SPeter Maydell * Check that when we advance forward to the reload time the interrupt 153f277d1c3SPeter Maydell * fires and the value reloads 154f277d1c3SPeter Maydell */ 155f277d1c3SPeter Maydell clock_step_ticks(100); 156f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 1); 157f277d1c3SPeter Maydell clock_step_ticks(100); 158f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 5); 159f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_LO), ==, 4400); 160f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_HI), ==, 0); 161f277d1c3SPeter Maydell 162f277d1c3SPeter Maydell clock_step_ticks(100); 163f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 5); 164f277d1c3SPeter Maydell /* Check that writing 0 to CLR clears the interrupt */ 165f277d1c3SPeter Maydell writel(TIMER_BASE + CNTP_AIVAL_CTL, 1); 166f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 1); 167f277d1c3SPeter Maydell /* Check that when we move forward to the reload time it fires again */ 168f277d1c3SPeter Maydell clock_step_ticks(100); 169f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 5); 170f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_LO), ==, 4600); 171f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_HI), ==, 0); 172f277d1c3SPeter Maydell 173f277d1c3SPeter Maydell /* 174f277d1c3SPeter Maydell * Step the clock far enough that we overflow the low half of the 175f277d1c3SPeter Maydell * CNTPCT and AIVAL registers, and check that their high halves 176f277d1c3SPeter Maydell * give the right values. We do the forward movement in 177f277d1c3SPeter Maydell * non-autoinc mode because otherwise it takes forever as the 178f277d1c3SPeter Maydell * timer has to emulate all the 'reload at t + N, t + 2N, etc' 179f277d1c3SPeter Maydell * steps. 180f277d1c3SPeter Maydell */ 181f277d1c3SPeter Maydell writel(TIMER_BASE + CNTP_AIVAL_CTL, 0); 182f277d1c3SPeter Maydell clock_step_ticks(0x42ULL << 32); 183f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTPCT_LO), ==, 4400); 184*58045186SInès Varhol g_assert_cmphex(readl(TIMER_BASE + CNTPCT_HI), ==, 0x42); 185f277d1c3SPeter Maydell 186f277d1c3SPeter Maydell /* Turn on the autoinc again to check AIVAL_HI */ 187f277d1c3SPeter Maydell writel(TIMER_BASE + CNTP_AIVAL_CTL, 1); 188f277d1c3SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_AIVAL_LO), ==, 4600); 189*58045186SInès Varhol g_assert_cmphex(readl(TIMER_BASE + CNTP_AIVAL_HI), ==, 0x42); 190f277d1c3SPeter Maydell } 191f277d1c3SPeter Maydell 192bf7ca803SPeter Maydell static void test_timer_scale_change(void) 193bf7ca803SPeter Maydell { 194bf7ca803SPeter Maydell /* 195bf7ca803SPeter Maydell * Test that the timer responds correctly to counter 196bf7ca803SPeter Maydell * scaling changes while it has an active timer. 197bf7ca803SPeter Maydell */ 198bf7ca803SPeter Maydell reset_counter_and_timer(); 199bf7ca803SPeter Maydell /* Give ourselves access to the timer, and enable the counter and timer */ 200bf7ca803SPeter Maydell writel(PERIPHNSPPC0, 1); 201bf7ca803SPeter Maydell writel(COUNTER_BASE + CNTCR, 1); 202bf7ca803SPeter Maydell writel(TIMER_BASE + CNTP_CTL, 1); 203bf7ca803SPeter Maydell /* Set the CompareValue to 4000 ticks */ 204bf7ca803SPeter Maydell writel(TIMER_BASE + CNTP_CVAL_LO, 4000); 205bf7ca803SPeter Maydell writel(TIMER_BASE + CNTP_CVAL_HI, 0); 206bf7ca803SPeter Maydell /* Advance halfway and check ISTATUS is not set */ 207bf7ca803SPeter Maydell clock_step_ticks(2000); 208bf7ca803SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 1); 209bf7ca803SPeter Maydell /* Reprogram the counter to run at 1/16th speed */ 210bf7ca803SPeter Maydell writel(COUNTER_BASE + CNTCR, 0); 211bf7ca803SPeter Maydell writel(COUNTER_BASE + CNTSCR, 0x00100000); /* 1/16th normal speed */ 212bf7ca803SPeter Maydell writel(COUNTER_BASE + CNTCR, 5); /* EN, SCEN */ 213bf7ca803SPeter Maydell /* Advance to where the timer would have fired and check it has not */ 214bf7ca803SPeter Maydell clock_step_ticks(2000); 215bf7ca803SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 1); 216bf7ca803SPeter Maydell /* Advance to where the timer must fire at the new clock rate */ 217bf7ca803SPeter Maydell clock_step_ticks(29996); 218bf7ca803SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 1); 219bf7ca803SPeter Maydell clock_step_ticks(4); 220bf7ca803SPeter Maydell g_assert_cmpuint(readl(TIMER_BASE + CNTP_CTL), ==, 5); 221bf7ca803SPeter Maydell } 222bf7ca803SPeter Maydell 2231eca58aaSPeter Maydell int main(int argc, char **argv) 2241eca58aaSPeter Maydell { 2251eca58aaSPeter Maydell int r; 2261eca58aaSPeter Maydell 2271eca58aaSPeter Maydell g_test_init(&argc, &argv, NULL); 2281eca58aaSPeter Maydell 2291eca58aaSPeter Maydell qtest_start("-machine mps3-an547"); 2301eca58aaSPeter Maydell 2311eca58aaSPeter Maydell qtest_add_func("/sse-timer/counter", test_counter); 232f277d1c3SPeter Maydell qtest_add_func("/sse-timer/timer", test_timer); 233bf7ca803SPeter Maydell qtest_add_func("/sse-timer/timer-scale-change", test_timer_scale_change); 2341eca58aaSPeter Maydell 2351eca58aaSPeter Maydell r = g_test_run(); 2361eca58aaSPeter Maydell 2371eca58aaSPeter Maydell qtest_end(); 2381eca58aaSPeter Maydell 2391eca58aaSPeter Maydell return r; 2401eca58aaSPeter Maydell } 241