blob: 8ca9fcb6aae39435407b5f2464513f37c4e0e32e [file] [log] [blame]
Simon Glass9b4221b2015-05-02 09:25:02 -06001/*
2 * Copyright (c) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <command.h>
9#include <errno.h>
10
11static int test_get_timer(void)
12{
13 ulong base, start, next, diff;
14 int iter;
15
16 base = get_timer(0);
17 start = get_timer(0);
18 for (iter = 0; iter < 10; iter++) {
19 do {
20 next = get_timer(0);
21 } while (start == next);
22
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);
26 return -EINVAL;
27 }
28 start++;
29 }
30
31 /*
32 * Check that get_timer(base) matches our elapsed time, allowing that
33 * an extra millisecond may have passed.
34 */
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);
39 return -EINVAL;
40 }
41
42 return 0;
43}
44
45static int test_timer_get_us(void)
46{
47 ulong prev, next, min = 1000000;
48 long delta;
49 int iter;
50
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();
55 if (next != prev) {
56 delta = next - prev;
57 if (delta < 0) {
58 printf("%s: timer_get_us() went backwards from %lu to %lu\n",
59 __func__, prev, next);
60 return -EINVAL;
61 } else if (delta != 0) {
62 if (delta < min)
63 min = delta;
64 prev = next;
65 iter++;
66 }
67 }
68 }
69
70 if (min != 1) {
71 printf("%s: Minimum microsecond delta should be 1 but is %lu\n",
72 __func__, min);
73 return -EINVAL;
74 }
75
76 return 0;
77}
78
79static int test_time_comparison(void)
80{
81 ulong start_us, end_us, delta_us;
82 long error;
83 ulong start;
84
85 start = get_timer(0);
86 start_us = timer_get_us();
87 while (get_timer(start) < 1000)
88 ;
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)
95 return -EINVAL;
96
97 return 0;
98}
99
100static int test_udelay(void)
101{
102 long error;
103 ulong start, delta;
104 int iter;
105
106 start = get_timer(0);
107 for (iter = 0; iter < 1000; iter++)
108 udelay(1000);
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)
114 return -EINVAL;
115
116 return 0;
117}
118
Joe Hershbergeree20efe2015-05-20 14:27:30 -0500119int do_ut_time(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Simon Glass9b4221b2015-05-02 09:25:02 -0600120{
121 int ret = 0;
122
123 ret |= test_get_timer();
124 ret |= test_timer_get_us();
125 ret |= test_time_comparison();
126 ret |= test_udelay();
127
128 printf("Test %s\n", ret ? "failed" : "passed");
129
130 return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
131}