target/arm: Move debug routines to debug_helper.c
[qemu/ar7.git] / tests / test-arm-mptimer.c
blob156a39f50dd87094189ea1b6d25eeedc2efa56e0
1 /*
2 * QTest testcase for the ARM MPTimer
4 * Copyright (c) 2016 Dmitry Osipenko <digetx@gmail.com>
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
10 #include "qemu/osdep.h"
11 #include "qemu/timer.h"
12 #include "libqtest.h"
14 #define TIMER_BLOCK_SCALE(s) ((((s) & 0xff) + 1) * 10)
16 #define TIMER_BLOCK_STEP(scaler, steps_nb) \
17 clock_step(TIMER_BLOCK_SCALE(scaler) * (int64_t)(steps_nb) + 1)
19 #define TIMER_BASE_PHYS 0x1e000600
21 #define TIMER_LOAD 0x00
22 #define TIMER_COUNTER 0x04
23 #define TIMER_CONTROL 0x08
24 #define TIMER_INTSTAT 0x0C
26 #define TIMER_CONTROL_ENABLE (1 << 0)
27 #define TIMER_CONTROL_PERIODIC (1 << 1)
28 #define TIMER_CONTROL_IT_ENABLE (1 << 2)
29 #define TIMER_CONTROL_PRESCALER(p) (((p) & 0xff) << 8)
31 #define PERIODIC 1
32 #define ONESHOT 0
33 #define NOSCALE 0
35 static int nonscaled = NOSCALE;
36 static int scaled = 122;
38 static void timer_load(uint32_t load)
40 writel(TIMER_BASE_PHYS + TIMER_LOAD, load);
43 static void timer_start(int periodic, uint32_t scale)
45 uint32_t ctl = TIMER_CONTROL_ENABLE | TIMER_CONTROL_PRESCALER(scale);
47 if (periodic) {
48 ctl |= TIMER_CONTROL_PERIODIC;
51 writel(TIMER_BASE_PHYS + TIMER_CONTROL, ctl);
54 static void timer_stop(void)
56 writel(TIMER_BASE_PHYS + TIMER_CONTROL, 0);
59 static void timer_int_clr(void)
61 writel(TIMER_BASE_PHYS + TIMER_INTSTAT, 1);
64 static void timer_reset(void)
66 timer_stop();
67 timer_load(0);
68 timer_int_clr();
71 static uint32_t timer_get_and_clr_int_sts(void)
73 uint32_t int_sts = readl(TIMER_BASE_PHYS + TIMER_INTSTAT);
75 if (int_sts) {
76 timer_int_clr();
79 return int_sts;
82 static uint32_t timer_counter(void)
84 return readl(TIMER_BASE_PHYS + TIMER_COUNTER);
87 static void timer_set_counter(uint32_t value)
89 writel(TIMER_BASE_PHYS + TIMER_COUNTER, value);
92 static void test_timer_oneshot(gconstpointer arg)
94 int scaler = *((int *) arg);
96 timer_reset();
97 timer_load(9999999);
98 timer_start(ONESHOT, scaler);
100 TIMER_BLOCK_STEP(scaler, 9999);
102 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
103 g_assert_cmpuint(timer_counter(), ==, 9990000);
105 TIMER_BLOCK_STEP(scaler, 9990000);
107 g_assert_cmpuint(timer_counter(), ==, 0);
108 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
110 TIMER_BLOCK_STEP(scaler, 9990000);
112 g_assert_cmpuint(timer_counter(), ==, 0);
113 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
116 static void test_timer_pause(gconstpointer arg)
118 int scaler = *((int *) arg);
120 timer_reset();
121 timer_load(999999999);
122 timer_start(ONESHOT, scaler);
124 TIMER_BLOCK_STEP(scaler, 999);
126 g_assert_cmpuint(timer_counter(), ==, 999999000);
127 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
129 TIMER_BLOCK_STEP(scaler, 9000);
131 g_assert_cmpuint(timer_counter(), ==, 999990000);
132 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
134 timer_stop();
136 g_assert_cmpuint(timer_counter(), ==, 999990000);
137 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
139 TIMER_BLOCK_STEP(scaler, 90000);
141 g_assert_cmpuint(timer_counter(), ==, 999990000);
142 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
144 timer_start(ONESHOT, scaler);
146 TIMER_BLOCK_STEP(scaler, 999990000);
148 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
149 g_assert_cmpuint(timer_counter(), ==, 0);
151 TIMER_BLOCK_STEP(scaler, 999990000);
153 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
154 g_assert_cmpuint(timer_counter(), ==, 0);
157 static void test_timer_reload(gconstpointer arg)
159 int scaler = *((int *) arg);
161 timer_reset();
162 timer_load(UINT32_MAX);
163 timer_start(ONESHOT, scaler);
165 TIMER_BLOCK_STEP(scaler, 90000);
167 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 90000);
168 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
170 timer_load(UINT32_MAX);
172 TIMER_BLOCK_STEP(scaler, 90000);
174 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 90000);
175 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
178 static void test_timer_periodic(gconstpointer arg)
180 int scaler = *((int *) arg);
181 int repeat = 10;
183 timer_reset();
184 timer_load(100);
185 timer_start(PERIODIC, scaler);
187 while (repeat--) {
188 clock_step(TIMER_BLOCK_SCALE(scaler) * (101 + repeat) + 1);
190 g_assert_cmpuint(timer_counter(), ==, 100 - repeat);
191 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
193 clock_step(TIMER_BLOCK_SCALE(scaler) * (101 - repeat) - 1);
197 static void test_timer_oneshot_to_periodic(gconstpointer arg)
199 int scaler = *((int *) arg);
201 timer_reset();
202 timer_load(10000);
203 timer_start(ONESHOT, scaler);
205 TIMER_BLOCK_STEP(scaler, 1000);
207 g_assert_cmpuint(timer_counter(), ==, 9000);
208 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
210 timer_start(PERIODIC, scaler);
212 TIMER_BLOCK_STEP(scaler, 14001);
214 g_assert_cmpuint(timer_counter(), ==, 5000);
215 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
218 static void test_timer_periodic_to_oneshot(gconstpointer arg)
220 int scaler = *((int *) arg);
222 timer_reset();
223 timer_load(99999999);
224 timer_start(PERIODIC, scaler);
226 TIMER_BLOCK_STEP(scaler, 999);
228 g_assert_cmpuint(timer_counter(), ==, 99999000);
229 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
231 timer_start(ONESHOT, scaler);
233 TIMER_BLOCK_STEP(scaler, 99999009);
235 g_assert_cmpuint(timer_counter(), ==, 0);
236 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
239 static void test_timer_prescaler(void)
241 timer_reset();
242 timer_load(9999999);
243 timer_start(ONESHOT, NOSCALE);
245 TIMER_BLOCK_STEP(NOSCALE, 9999998);
247 g_assert_cmpuint(timer_counter(), ==, 1);
248 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
250 TIMER_BLOCK_STEP(NOSCALE, 1);
252 g_assert_cmpuint(timer_counter(), ==, 0);
253 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
255 timer_reset();
256 timer_load(9999999);
257 timer_start(ONESHOT, 0xAB);
259 TIMER_BLOCK_STEP(0xAB, 9999998);
261 g_assert_cmpuint(timer_counter(), ==, 1);
262 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
264 TIMER_BLOCK_STEP(0xAB, 1);
266 g_assert_cmpuint(timer_counter(), ==, 0);
267 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
270 static void test_timer_prescaler_on_the_fly(void)
272 timer_reset();
273 timer_load(9999999);
274 timer_start(ONESHOT, NOSCALE);
276 TIMER_BLOCK_STEP(NOSCALE, 999);
278 g_assert_cmpuint(timer_counter(), ==, 9999000);
279 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
281 timer_start(ONESHOT, 0xAB);
283 TIMER_BLOCK_STEP(0xAB, 9000);
285 g_assert_cmpuint(timer_counter(), ==, 9990000);
286 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
289 static void test_timer_set_oneshot_counter_to_0(gconstpointer arg)
291 int scaler = *((int *) arg);
293 timer_reset();
294 timer_load(UINT32_MAX);
295 timer_start(ONESHOT, scaler);
297 TIMER_BLOCK_STEP(scaler, 1);
299 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 1);
300 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
302 timer_set_counter(0);
304 TIMER_BLOCK_STEP(scaler, 10);
306 g_assert_cmpuint(timer_counter(), ==, 0);
307 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
310 static void test_timer_set_periodic_counter_to_0(gconstpointer arg)
312 int scaler = *((int *) arg);
314 timer_reset();
315 timer_load(UINT32_MAX);
316 timer_start(PERIODIC, scaler);
318 TIMER_BLOCK_STEP(scaler, 1);
320 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 1);
321 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
323 timer_set_counter(0);
325 TIMER_BLOCK_STEP(scaler, 1);
327 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - (scaler ? 0 : 1));
328 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
330 timer_reset();
331 timer_set_counter(UINT32_MAX);
332 timer_start(PERIODIC, scaler);
334 TIMER_BLOCK_STEP(scaler, 1);
336 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 1);
337 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
339 timer_set_counter(0);
341 TIMER_BLOCK_STEP(scaler, 1);
343 g_assert_cmpuint(timer_counter(), ==, 0);
344 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
347 static void test_timer_noload_oneshot(gconstpointer arg)
349 int scaler = *((int *) arg);
351 timer_reset();
352 timer_start(ONESHOT, scaler);
354 TIMER_BLOCK_STEP(scaler, 1);
356 g_assert_cmpuint(timer_counter(), ==, 0);
357 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
359 TIMER_BLOCK_STEP(scaler, 1);
361 g_assert_cmpuint(timer_counter(), ==, 0);
362 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
365 static void test_timer_noload_periodic(gconstpointer arg)
367 int scaler = *((int *) arg);
369 timer_reset();
370 timer_start(PERIODIC, scaler);
372 TIMER_BLOCK_STEP(scaler, 1);
374 g_assert_cmpuint(timer_counter(), ==, 0);
375 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
377 TIMER_BLOCK_STEP(scaler, 1);
379 g_assert_cmpuint(timer_counter(), ==, 0);
380 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
383 static void test_timer_zero_load_oneshot(gconstpointer arg)
385 int scaler = *((int *) arg);
387 timer_reset();
388 timer_start(ONESHOT, scaler);
390 TIMER_BLOCK_STEP(scaler, 1);
392 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
393 g_assert_cmpuint(timer_counter(), ==, 0);
395 timer_load(0);
397 TIMER_BLOCK_STEP(scaler, 1);
399 g_assert_cmpuint(timer_counter(), ==, 0);
400 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
402 TIMER_BLOCK_STEP(scaler, 1);
404 g_assert_cmpuint(timer_counter(), ==, 0);
405 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
408 static void test_timer_zero_load_periodic(gconstpointer arg)
410 int scaler = *((int *) arg);
412 timer_reset();
413 timer_start(PERIODIC, scaler);
415 TIMER_BLOCK_STEP(scaler, 1);
417 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
418 g_assert_cmpuint(timer_counter(), ==, 0);
420 timer_load(0);
422 TIMER_BLOCK_STEP(scaler, 1);
424 g_assert_cmpuint(timer_counter(), ==, 0);
425 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
427 TIMER_BLOCK_STEP(scaler, 1);
429 g_assert_cmpuint(timer_counter(), ==, 0);
430 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
433 static void test_timer_zero_load_oneshot_to_nonzero(gconstpointer arg)
435 int scaler = *((int *) arg);
437 timer_reset();
438 timer_start(ONESHOT, scaler);
440 TIMER_BLOCK_STEP(scaler, 1);
442 g_assert_cmpuint(timer_counter(), ==, 0);
443 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
445 timer_load(0);
447 TIMER_BLOCK_STEP(scaler, 1);
449 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
450 g_assert_cmpuint(timer_counter(), ==, 0);
452 timer_load(999);
454 TIMER_BLOCK_STEP(scaler, 1001);
456 g_assert_cmpuint(timer_counter(), ==, 0);
457 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
460 static void test_timer_zero_load_periodic_to_nonzero(gconstpointer arg)
462 int scaler = *((int *) arg);
463 int i;
465 timer_reset();
466 timer_start(PERIODIC, scaler);
468 TIMER_BLOCK_STEP(scaler, 1);
470 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
471 g_assert_cmpuint(timer_counter(), ==, 0);
473 timer_load(0);
475 TIMER_BLOCK_STEP(scaler, 1);
477 g_assert_cmpuint(timer_counter(), ==, 0);
478 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
480 timer_load(1999999);
482 for (i = 1; i < 10; i++) {
483 TIMER_BLOCK_STEP(scaler, 2000001);
485 g_assert_cmpuint(timer_counter(), ==, 1999999 - i);
486 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
487 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
491 static void test_timer_nonzero_load_oneshot_to_zero(gconstpointer arg)
493 int scaler = *((int *) arg);
495 timer_reset();
496 timer_start(ONESHOT, scaler);
498 TIMER_BLOCK_STEP(scaler, 1);
500 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
501 g_assert_cmpuint(timer_counter(), ==, 0);
503 timer_load(UINT32_MAX);
504 timer_load(0);
506 TIMER_BLOCK_STEP(scaler, 100);
508 g_assert_cmpuint(timer_counter(), ==, 0);
509 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
512 static void test_timer_nonzero_load_periodic_to_zero(gconstpointer arg)
514 int scaler = *((int *) arg);
516 timer_reset();
517 timer_start(PERIODIC, scaler);
519 TIMER_BLOCK_STEP(scaler, 1);
521 g_assert_cmpuint(timer_counter(), ==, 0);
522 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
524 timer_load(UINT32_MAX);
525 timer_load(0);
527 TIMER_BLOCK_STEP(scaler, 100);
529 g_assert_cmpuint(timer_counter(), ==, 0);
530 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
533 static void test_timer_set_periodic_counter_on_the_fly(gconstpointer arg)
535 int scaler = *((int *) arg);
537 timer_reset();
538 timer_load(UINT32_MAX / 2);
539 timer_start(PERIODIC, scaler);
541 TIMER_BLOCK_STEP(scaler, 100);
543 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX / 2 - 100);
544 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
546 timer_set_counter(UINT32_MAX);
548 TIMER_BLOCK_STEP(scaler, 100);
550 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 100);
551 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
554 static void test_timer_enable_and_set_counter(gconstpointer arg)
556 int scaler = *((int *) arg);
558 timer_reset();
559 timer_start(ONESHOT, scaler);
561 TIMER_BLOCK_STEP(scaler, 1);
563 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
565 timer_set_counter(UINT32_MAX);
567 TIMER_BLOCK_STEP(scaler, 100);
569 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 100);
570 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
573 static void test_timer_set_counter_and_enable(gconstpointer arg)
575 int scaler = *((int *) arg);
577 timer_reset();
578 timer_set_counter(UINT32_MAX);
579 timer_start(ONESHOT, scaler);
581 TIMER_BLOCK_STEP(scaler, 100);
583 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 100);
584 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
587 static void test_timer_set_counter_disabled(void)
589 timer_reset();
590 timer_set_counter(999999999);
592 TIMER_BLOCK_STEP(NOSCALE, 100);
594 g_assert_cmpuint(timer_counter(), ==, 999999999);
595 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
598 static void test_timer_load_disabled(void)
600 timer_reset();
601 timer_load(999999999);
603 TIMER_BLOCK_STEP(NOSCALE, 100);
605 g_assert_cmpuint(timer_counter(), ==, 999999999);
606 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
609 static void test_timer_oneshot_with_counter_0_on_start(gconstpointer arg)
611 int scaler = *((int *) arg);
613 timer_reset();
614 timer_load(999);
615 timer_set_counter(0);
616 timer_start(ONESHOT, scaler);
618 TIMER_BLOCK_STEP(scaler, 100);
620 g_assert_cmpuint(timer_counter(), ==, 0);
621 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
623 TIMER_BLOCK_STEP(scaler, 100);
625 g_assert_cmpuint(timer_counter(), ==, 0);
626 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
629 static void test_timer_periodic_with_counter_0_on_start(gconstpointer arg)
631 int scaler = *((int *) arg);
632 int i;
634 timer_reset();
635 timer_load(UINT32_MAX);
636 timer_set_counter(0);
638 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
639 g_assert_cmpuint(timer_counter(), ==, 0);
641 timer_start(PERIODIC, scaler);
643 TIMER_BLOCK_STEP(scaler, 100);
645 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
646 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX + (scaler ? 1 : 0) - 100);
648 TIMER_BLOCK_STEP(scaler, 100);
650 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX + (scaler ? 1 : 0) - 200);
651 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
653 timer_reset();
654 timer_load(1999999);
655 timer_set_counter(0);
657 g_assert_cmpuint(timer_counter(), ==, 0);
658 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
660 TIMER_BLOCK_STEP(scaler, 1);
662 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
664 timer_start(PERIODIC, scaler);
666 TIMER_BLOCK_STEP(scaler, 1);
668 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
670 for (i = 2 - (!!scaler ? 1 : 0); i < 10; i++) {
671 TIMER_BLOCK_STEP(scaler, 2000001);
673 g_assert_cmpuint(timer_counter(), ==, 1999999 - i);
674 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
675 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
679 static void test_periodic_counter(gconstpointer arg)
681 const int test_load = 10;
682 int scaler = *((int *) arg);
683 int test_val;
685 timer_reset();
686 timer_load(test_load);
687 timer_start(PERIODIC, scaler);
689 clock_step(1);
691 for (test_val = 0; test_val <= test_load; test_val++) {
692 clock_step(TIMER_BLOCK_SCALE(scaler) * test_load);
693 g_assert_cmpint(timer_counter(), ==, test_val);
697 static void test_timer_set_counter_periodic_with_zero_load(gconstpointer arg)
699 int scaler = *((int *) arg);
701 timer_reset();
702 timer_start(PERIODIC, scaler);
703 timer_load(0);
705 TIMER_BLOCK_STEP(scaler, 1);
707 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
709 TIMER_BLOCK_STEP(scaler, 1);
711 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
713 timer_set_counter(999);
715 TIMER_BLOCK_STEP(scaler, 999);
717 g_assert_cmpuint(timer_counter(), ==, 0);
718 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
720 TIMER_BLOCK_STEP(scaler, 1);
722 g_assert_cmpuint(timer_counter(), ==, 0);
723 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
726 static void test_timer_set_oneshot_load_to_0(gconstpointer arg)
728 int scaler = *((int *) arg);
730 timer_reset();
731 timer_load(UINT32_MAX);
732 timer_start(ONESHOT, scaler);
734 TIMER_BLOCK_STEP(scaler, 100);
736 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 100);
737 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
739 timer_load(0);
741 TIMER_BLOCK_STEP(scaler, 100);
743 g_assert_cmpuint(timer_counter(), ==, 0);
744 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
746 TIMER_BLOCK_STEP(scaler, 100);
748 g_assert_cmpuint(timer_counter(), ==, 0);
749 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
752 static void test_timer_set_periodic_load_to_0(gconstpointer arg)
754 int scaler = *((int *) arg);
756 timer_reset();
757 timer_load(UINT32_MAX);
758 timer_start(PERIODIC, scaler);
760 TIMER_BLOCK_STEP(scaler, 100);
762 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 100);
763 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
765 timer_load(0);
767 TIMER_BLOCK_STEP(scaler, 100);
769 g_assert_cmpuint(timer_counter(), ==, 0);
770 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
772 TIMER_BLOCK_STEP(scaler, 100);
774 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
775 g_assert_cmpuint(timer_counter(), ==, 0);
778 static void test_deferred_trigger(void)
780 int mode = ONESHOT;
782 again:
783 timer_reset();
784 timer_start(mode, 255);
786 clock_step(100);
788 g_assert_cmpuint(timer_counter(), ==, 0);
790 TIMER_BLOCK_STEP(255, 1);
792 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
794 timer_reset();
795 timer_load(2);
796 timer_start(mode, 255);
798 clock_step(100);
800 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
802 TIMER_BLOCK_STEP(255, 1);
804 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
806 TIMER_BLOCK_STEP(255, 1);
808 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
810 timer_reset();
811 timer_load(UINT32_MAX);
812 timer_start(mode, 255);
814 clock_step(100);
816 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
818 timer_set_counter(0);
820 clock_step(100);
822 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
824 TIMER_BLOCK_STEP(255, 1);
826 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
828 timer_reset();
829 timer_load(UINT32_MAX);
830 timer_start(mode, 255);
832 clock_step(100);
834 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
836 timer_load(0);
838 clock_step(100);
840 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
842 TIMER_BLOCK_STEP(255, 1);
844 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
846 if (mode == ONESHOT) {
847 mode = PERIODIC;
848 goto again;
852 static void test_timer_zero_load_mode_switch(gconstpointer arg)
854 int scaler = *((int *) arg);
856 timer_reset();
857 timer_load(0);
858 timer_start(PERIODIC, scaler);
860 TIMER_BLOCK_STEP(scaler, 1);
862 g_assert_cmpuint(timer_counter(), ==, 0);
863 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
865 TIMER_BLOCK_STEP(scaler, 1);
867 timer_start(ONESHOT, scaler);
869 TIMER_BLOCK_STEP(scaler, 1);
871 g_assert_cmpuint(timer_counter(), ==, 0);
872 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
874 TIMER_BLOCK_STEP(scaler, 1);
876 g_assert_cmpuint(timer_counter(), ==, 0);
877 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
879 TIMER_BLOCK_STEP(scaler, 1);
881 timer_start(PERIODIC, scaler);
883 TIMER_BLOCK_STEP(scaler, 1);
885 g_assert_cmpuint(timer_counter(), ==, 0);
886 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
889 static void test_timer_zero_load_prescaled_periodic_to_nonscaled_oneshot(void)
891 timer_reset();
892 timer_load(0);
893 timer_start(PERIODIC, 255);
895 TIMER_BLOCK_STEP(255, 1);
897 g_assert_cmpuint(timer_counter(), ==, 0);
898 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
899 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
901 TIMER_BLOCK_STEP(255, 1);
903 g_assert_cmpuint(timer_counter(), ==, 0);
904 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
905 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
907 TIMER_BLOCK_STEP(255, 1);
909 timer_start(ONESHOT, NOSCALE);
911 TIMER_BLOCK_STEP(NOSCALE, 1);
913 g_assert_cmpuint(timer_counter(), ==, 0);
914 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
915 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
917 TIMER_BLOCK_STEP(NOSCALE, 1);
919 g_assert_cmpuint(timer_counter(), ==, 0);
920 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
923 static void test_timer_zero_load_prescaled_oneshot_to_nonscaled_periodic(void)
925 timer_reset();
926 timer_load(0);
927 timer_start(ONESHOT, 255);
929 TIMER_BLOCK_STEP(255, 1);
931 g_assert_cmpuint(timer_counter(), ==, 0);
932 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
933 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
935 timer_start(PERIODIC, NOSCALE);
937 TIMER_BLOCK_STEP(NOSCALE, 1);
939 g_assert_cmpuint(timer_counter(), ==, 0);
940 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
943 static void test_timer_zero_load_nonscaled_oneshot_to_prescaled_periodic(void)
945 timer_reset();
946 timer_load(0);
947 timer_start(ONESHOT, NOSCALE);
949 TIMER_BLOCK_STEP(NOSCALE, 1);
951 g_assert_cmpuint(timer_counter(), ==, 0);
952 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
954 timer_start(PERIODIC, 255);
956 TIMER_BLOCK_STEP(255, 1);
958 g_assert_cmpuint(timer_counter(), ==, 0);
959 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
960 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
962 TIMER_BLOCK_STEP(255, 1);
964 g_assert_cmpuint(timer_counter(), ==, 0);
965 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
966 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
969 static void test_timer_zero_load_nonscaled_periodic_to_prescaled_oneshot(void)
971 timer_reset();
972 timer_load(0);
973 timer_start(PERIODIC, NOSCALE);
975 TIMER_BLOCK_STEP(NOSCALE, 1);
977 g_assert_cmpuint(timer_counter(), ==, 0);
978 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
980 timer_start(ONESHOT, 255);
982 TIMER_BLOCK_STEP(255, 1);
984 g_assert_cmpuint(timer_counter(), ==, 0);
985 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
986 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
988 TIMER_BLOCK_STEP(255, 1);
990 g_assert_cmpuint(timer_counter(), ==, 0);
991 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
995 * Add a qtest test that comes in two versions: one with
996 * a timer scaler setting, and one with the timer nonscaled.
998 static void add_scaler_test(const char *str, bool scale,
999 void (*fn)(const void *))
1001 char *name;
1002 int *scaler = scale ? &scaled : &nonscaled;
1004 name = g_strdup_printf("%s=%d", str, *scaler);
1005 qtest_add_data_func(name, scaler, fn);
1006 g_free(name);
1009 int main(int argc, char **argv)
1011 int ret;
1012 int scale;
1014 g_test_init(&argc, &argv, NULL);
1016 qtest_add_func("mptimer/deferred_trigger", test_deferred_trigger);
1017 qtest_add_func("mptimer/load_disabled", test_timer_load_disabled);
1018 qtest_add_func("mptimer/set_counter_disabled", test_timer_set_counter_disabled);
1019 qtest_add_func("mptimer/zero_load_prescaled_periodic_to_nonscaled_oneshot",
1020 test_timer_zero_load_prescaled_periodic_to_nonscaled_oneshot);
1021 qtest_add_func("mptimer/zero_load_prescaled_oneshot_to_nonscaled_periodic",
1022 test_timer_zero_load_prescaled_oneshot_to_nonscaled_periodic);
1023 qtest_add_func("mptimer/zero_load_nonscaled_oneshot_to_prescaled_periodic",
1024 test_timer_zero_load_nonscaled_oneshot_to_prescaled_periodic);
1025 qtest_add_func("mptimer/zero_load_nonscaled_periodic_to_prescaled_oneshot",
1026 test_timer_zero_load_nonscaled_periodic_to_prescaled_oneshot);
1027 qtest_add_func("mptimer/prescaler", test_timer_prescaler);
1028 qtest_add_func("mptimer/prescaler_on_the_fly", test_timer_prescaler_on_the_fly);
1030 for (scale = 0; scale < 2; scale++) {
1031 add_scaler_test("mptimer/oneshot scaler",
1032 scale, test_timer_oneshot);
1033 add_scaler_test("mptimer/pause scaler",
1034 scale, test_timer_pause);
1035 add_scaler_test("mptimer/reload scaler",
1036 scale, test_timer_reload);
1037 add_scaler_test("mptimer/periodic scaler",
1038 scale, test_timer_periodic);
1039 add_scaler_test("mptimer/oneshot_to_periodic scaler",
1040 scale, test_timer_oneshot_to_periodic);
1041 add_scaler_test("mptimer/periodic_to_oneshot scaler",
1042 scale, test_timer_periodic_to_oneshot);
1043 add_scaler_test("mptimer/set_oneshot_counter_to_0 scaler",
1044 scale, test_timer_set_oneshot_counter_to_0);
1045 add_scaler_test("mptimer/set_periodic_counter_to_0 scaler",
1046 scale, test_timer_set_periodic_counter_to_0);
1047 add_scaler_test("mptimer/noload_oneshot scaler",
1048 scale, test_timer_noload_oneshot);
1049 add_scaler_test("mptimer/noload_periodic scaler",
1050 scale, test_timer_noload_periodic);
1051 add_scaler_test("mptimer/zero_load_oneshot scaler",
1052 scale, test_timer_zero_load_oneshot);
1053 add_scaler_test("mptimer/zero_load_periodic scaler",
1054 scale, test_timer_zero_load_periodic);
1055 add_scaler_test("mptimer/zero_load_oneshot_to_nonzero scaler",
1056 scale, test_timer_zero_load_oneshot_to_nonzero);
1057 add_scaler_test("mptimer/zero_load_periodic_to_nonzero scaler",
1058 scale, test_timer_zero_load_periodic_to_nonzero);
1059 add_scaler_test("mptimer/nonzero_load_oneshot_to_zero scaler",
1060 scale, test_timer_nonzero_load_oneshot_to_zero);
1061 add_scaler_test("mptimer/nonzero_load_periodic_to_zero scaler",
1062 scale, test_timer_nonzero_load_periodic_to_zero);
1063 add_scaler_test("mptimer/set_periodic_counter_on_the_fly scaler",
1064 scale, test_timer_set_periodic_counter_on_the_fly);
1065 add_scaler_test("mptimer/enable_and_set_counter scaler",
1066 scale, test_timer_enable_and_set_counter);
1067 add_scaler_test("mptimer/set_counter_and_enable scaler",
1068 scale, test_timer_set_counter_and_enable);
1069 add_scaler_test("mptimer/oneshot_with_counter_0_on_start scaler",
1070 scale, test_timer_oneshot_with_counter_0_on_start);
1071 add_scaler_test("mptimer/periodic_with_counter_0_on_start scaler",
1072 scale, test_timer_periodic_with_counter_0_on_start);
1073 add_scaler_test("mptimer/periodic_counter scaler",
1074 scale, test_periodic_counter);
1075 add_scaler_test("mptimer/set_counter_periodic_with_zero_load scaler",
1076 scale, test_timer_set_counter_periodic_with_zero_load);
1077 add_scaler_test("mptimer/set_oneshot_load_to_0 scaler",
1078 scale, test_timer_set_oneshot_load_to_0);
1079 add_scaler_test("mptimer/set_periodic_load_to_0 scaler",
1080 scale, test_timer_set_periodic_load_to_0);
1081 add_scaler_test("mptimer/zero_load_mode_switch scaler",
1082 scale, test_timer_zero_load_mode_switch);
1085 qtest_start("-machine vexpress-a9");
1086 ret = g_test_run();
1087 qtest_end();
1089 return ret;