1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
11 #include <linux/delay.h>
13 static int test_get_timer(void)
15 ulong base, start, next, diff;
20 for (iter = 0; iter < 10; iter++) {
23 } while (start == next);
25 if (start + 1 != next) {
26 printf("%s: iter=%d, start=%lu, next=%lu, expected a difference of 1\n",
27 __func__, iter, start, next);
34 * Check that get_timer(base) matches our elapsed time, allowing that
35 * an extra millisecond may have passed.
37 diff = get_timer(base);
38 if (diff != iter && diff != iter + 1) {
39 printf("%s: expected get_timer(base) to match elapsed time: diff=%lu, expected=%d\n",
40 __func__, diff, iter);
47 static int test_timer_get_us(void)
49 ulong prev, next, min = 1000000;
53 /* Find the minimum delta we can measure, in microseconds */
54 prev = timer_get_us();
55 for (iter = 0; iter < 100; ) {
56 next = timer_get_us();
60 printf("%s: timer_get_us() went backwards from %lu to %lu\n",
61 __func__, prev, next);
63 } else if (delta != 0) {
73 printf("%s: Minimum microsecond delta should be 1 but is %lu\n",
81 static int test_time_comparison(void)
83 ulong start_us, end_us, delta_us;
88 start_us = timer_get_us();
89 while (get_timer(start) < 1000)
91 end_us = timer_get_us();
92 delta_us = end_us - start_us;
93 error = delta_us - 1000000;
94 printf("%s: Microsecond time for 1 second: %lu, error = %ld\n",
95 __func__, delta_us, error);
96 if (abs(error) > 1000)
102 static int test_udelay(void)
108 start = get_timer(0);
109 for (iter = 0; iter < 1000; iter++)
111 delta = get_timer(start);
112 error = delta - 1000;
113 printf("%s: Delay time for 1000 udelay(1000): %lu ms, error = %ld\n",
114 __func__, delta, error);
115 if (abs(error) > 100)
121 int do_ut_time(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
125 ret |= test_get_timer();
126 ret |= test_timer_get_us();
127 ret |= test_time_comparison();
128 ret |= test_udelay();
130 printf("Test %s\n", ret ? "failed" : "passed");
132 return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;