1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
11 static int test_get_timer(void)
13 ulong base, start, next, diff;
18 for (iter = 0; iter < 10; iter++) {
21 } while (start == next);
23 if (start + 1 != next) {
24 printf("%s: iter=%d, start=%lu, next=%lu, expected a difference of 1\n",
25 __func__, iter, start, next);
32 * Check that get_timer(base) matches our elapsed time, allowing that
33 * an extra millisecond may have passed.
35 diff = get_timer(base);
36 if (diff != iter && diff != iter + 1) {
37 printf("%s: expected get_timer(base) to match elapsed time: diff=%lu, expected=%d\n",
38 __func__, diff, iter);
45 static int test_timer_get_us(void)
47 ulong prev, next, min = 1000000;
51 /* Find the minimum delta we can measure, in microseconds */
52 prev = timer_get_us();
53 for (iter = 0; iter < 100; ) {
54 next = timer_get_us();
58 printf("%s: timer_get_us() went backwards from %lu to %lu\n",
59 __func__, prev, next);
61 } else if (delta != 0) {
71 printf("%s: Minimum microsecond delta should be 1 but is %lu\n",
79 static int test_time_comparison(void)
81 ulong start_us, end_us, delta_us;
86 start_us = timer_get_us();
87 while (get_timer(start) < 1000)
89 end_us = timer_get_us();
90 delta_us = end_us - start_us;
91 error = delta_us - 1000000;
92 printf("%s: Microsecond time for 1 second: %lu, error = %ld\n",
93 __func__, delta_us, error);
94 if (abs(error) > 1000)
100 static int test_udelay(void)
106 start = get_timer(0);
107 for (iter = 0; iter < 1000; iter++)
109 delta = get_timer(start);
110 error = delta - 1000;
111 printf("%s: Delay time for 1000 udelay(1000): %lu ms, error = %ld\n",
112 __func__, delta, error);
113 if (abs(error) > 100)
119 int do_ut_time(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
123 ret |= test_get_timer();
124 ret |= test_timer_get_us();
125 ret |= test_time_comparison();
126 ret |= test_udelay();
128 printf("Test %s\n", ret ? "failed" : "passed");
130 return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;