]> git.ipfire.org Git - thirdparty/u-boot.git/blame - test/time_ut.c
Merge tag 'u-boot-imx-20200511' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[thirdparty/u-boot.git] / test / time_ut.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
8271f5d4
SG
2/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
8271f5d4
SG
5 */
6
7#include <common.h>
8#include <command.h>
9#include <errno.h>
1045315d 10#include <time.h>
8271f5d4
SG
11
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
c812f722 120int do_ut_time(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
8271f5d4
SG
121{
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}