1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6 
7 #include <errno.h>
8 #include <time.h>
9 #include <linux/delay.h>
10 #include <test/lib.h>
11 #include <test/ut.h>
12 
test_get_timer(struct unit_test_state * uts)13 static int test_get_timer(struct unit_test_state *uts)
14 {
15 	ulong base, start, next, diff;
16 	int iter;
17 
18 	base = get_timer(0);
19 	start = get_timer(0);
20 	for (iter = 0; iter < 10; iter++) {
21 		do {
22 			next = get_timer(0);
23 		} while (start == next);
24 
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);
28 			return -EINVAL;
29 		}
30 		start++;
31 	}
32 
33 	/*
34 	 * Check that get_timer(base) matches our elapsed time, allowing that
35 	 * an extra millisecond may have passed.
36 	 */
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);
41 			return -EINVAL;
42 	}
43 
44 	return 0;
45 }
46 LIB_TEST(test_get_timer, 0);
47 
test_timer_get_us(struct unit_test_state * uts)48 static int test_timer_get_us(struct unit_test_state *uts)
49 {
50 	ulong prev, next, min = 1000000;
51 	long delta;
52 	int iter;
53 
54 	/* Find the minimum delta we can measure, in microseconds */
55 	prev = timer_get_us();
56 	for (iter = 0; iter < 100; ) {
57 		next = timer_get_us();
58 		if (next != prev) {
59 			delta = next - prev;
60 			if (delta < 0) {
61 				printf("%s: timer_get_us() went backwards from %lu to %lu\n",
62 				       __func__, prev, next);
63 				return -EINVAL;
64 			} else if (delta != 0) {
65 				if (delta < min)
66 					min = delta;
67 				prev = next;
68 				iter++;
69 			}
70 		}
71 	}
72 
73 	if (min != 1) {
74 		printf("%s: Minimum microsecond delta should be 1 but is %lu\n",
75 		       __func__, min);
76 		return -EINVAL;
77 	}
78 
79 	return 0;
80 }
81 LIB_TEST(test_timer_get_us, 0);
82 
test_time_comparison(struct unit_test_state * uts)83 static int test_time_comparison(struct unit_test_state *uts)
84 {
85 	ulong start_us, end_us, delta_us;
86 	long error;
87 	ulong start;
88 
89 	start = get_timer(0);
90 	start_us = timer_get_us();
91 	while (get_timer(start) < 1000)
92 		;
93 	end_us = timer_get_us();
94 	delta_us = end_us - start_us;
95 	error = delta_us - 1000000;
96 	printf("%s: Microsecond time for 1 second: %lu, error = %ld\n",
97 	       __func__, delta_us, error);
98 	if (abs(error) > 1000)
99 		return -EINVAL;
100 
101 	return 0;
102 }
103 LIB_TEST(test_time_comparison, 0);
104 
test_udelay(struct unit_test_state * uts)105 static int test_udelay(struct unit_test_state *uts)
106 {
107 	long error;
108 	ulong start, delta;
109 	int iter;
110 
111 	start = get_timer(0);
112 	for (iter = 0; iter < 1000; iter++)
113 		udelay(1000);
114 	delta = get_timer(start);
115 	error = delta - 1000;
116 	printf("%s: Delay time for 1000 udelay(1000): %lu ms, error = %ld\n",
117 	       __func__, delta, error);
118 	if (abs(error) > 100)
119 		return -EINVAL;
120 
121 	return 0;
122 }
123 LIB_TEST(test_udelay, 0);
124