blob: 40a19a50d93b92c580df34625adab8805c3ac022 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass8271f5d2015-05-02 09:25:02 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass8271f5d2015-05-02 09:25:02 -06005 */
6
7#include <common.h>
8#include <command.h>
9#include <errno.h>
Simon Glass10453152019-11-14 12:57:30 -070010#include <time.h>
Simon Glass8271f5d2015-05-02 09:25:02 -060011
12static int test_get_timer(void)
13{
14 ulong base, start, next, diff;
15 int iter;
16
17 base = get_timer(0);
18 start = get_timer(0);
19 for (iter = 0; iter < 10; iter++) {
20 do {
21 next = get_timer(0);
22 } while (start == next);
23
24 if (start + 1 != next) {
25 printf("%s: iter=%d, start=%lu, next=%lu, expected a difference of 1\n",
26 __func__, iter, start, next);
27 return -EINVAL;
28 }
29 start++;
30 }
31
32 /*
33 * Check that get_timer(base) matches our elapsed time, allowing that
34 * an extra millisecond may have passed.
35 */
36 diff = get_timer(base);
37 if (diff != iter && diff != iter + 1) {
38 printf("%s: expected get_timer(base) to match elapsed time: diff=%lu, expected=%d\n",
39 __func__, diff, iter);
40 return -EINVAL;
41 }
42
43 return 0;
44}
45
46static int test_timer_get_us(void)
47{
48 ulong prev, next, min = 1000000;
49 long delta;
50 int iter;
51
52 /* Find the minimum delta we can measure, in microseconds */
53 prev = timer_get_us();
54 for (iter = 0; iter < 100; ) {
55 next = timer_get_us();
56 if (next != prev) {
57 delta = next - prev;
58 if (delta < 0) {
59 printf("%s: timer_get_us() went backwards from %lu to %lu\n",
60 __func__, prev, next);
61 return -EINVAL;
62 } else if (delta != 0) {
63 if (delta < min)
64 min = delta;
65 prev = next;
66 iter++;
67 }
68 }
69 }
70
71 if (min != 1) {
72 printf("%s: Minimum microsecond delta should be 1 but is %lu\n",
73 __func__, min);
74 return -EINVAL;
75 }
76
77 return 0;
78}
79
80static int test_time_comparison(void)
81{
82 ulong start_us, end_us, delta_us;
83 long error;
84 ulong start;
85
86 start = get_timer(0);
87 start_us = timer_get_us();
88 while (get_timer(start) < 1000)
89 ;
90 end_us = timer_get_us();
91 delta_us = end_us - start_us;
92 error = delta_us - 1000000;
93 printf("%s: Microsecond time for 1 second: %lu, error = %ld\n",
94 __func__, delta_us, error);
95 if (abs(error) > 1000)
96 return -EINVAL;
97
98 return 0;
99}
100
101static int test_udelay(void)
102{
103 long error;
104 ulong start, delta;
105 int iter;
106
107 start = get_timer(0);
108 for (iter = 0; iter < 1000; iter++)
109 udelay(1000);
110 delta = get_timer(start);
111 error = delta - 1000;
112 printf("%s: Delay time for 1000 udelay(1000): %lu ms, error = %ld\n",
113 __func__, delta, error);
114 if (abs(error) > 100)
115 return -EINVAL;
116
117 return 0;
118}
119
Joe Hershbergerc812f722015-05-20 14:27:30 -0500120int do_ut_time(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Simon Glass8271f5d2015-05-02 09:25:02 -0600121{
122 int ret = 0;
123
124 ret |= test_get_timer();
125 ret |= test_timer_get_us();
126 ret |= test_time_comparison();
127 ret |= test_udelay();
128
129 printf("Test %s\n", ret ? "failed" : "passed");
130
131 return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
132}