blob: 80b82dbfd8331af9b7fd35edaa5cf8a437eb5c2f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass9b4221b2015-05-02 09:25:02 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass9b4221b2015-05-02 09:25:02 -06005 */
6
7#include <common.h>
8#include <command.h>
9#include <errno.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070010#include <time.h>
Simon Glassdbd79542020-05-10 11:40:11 -060011#include <linux/delay.h>
Simon Glass9b4221b2015-05-02 09:25:02 -060012
13static int test_get_timer(void)
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
47static int test_timer_get_us(void)
48{
49 ulong prev, next, min = 1000000;
50 long delta;
51 int iter;
52
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();
57 if (next != prev) {
58 delta = next - prev;
59 if (delta < 0) {
60 printf("%s: timer_get_us() went backwards from %lu to %lu\n",
61 __func__, prev, next);
62 return -EINVAL;
63 } else if (delta != 0) {
64 if (delta < min)
65 min = delta;
66 prev = next;
67 iter++;
68 }
69 }
70 }
71
72 if (min != 1) {
73 printf("%s: Minimum microsecond delta should be 1 but is %lu\n",
74 __func__, min);
75 return -EINVAL;
76 }
77
78 return 0;
79}
80
81static int test_time_comparison(void)
82{
83 ulong start_us, end_us, delta_us;
84 long error;
85 ulong start;
86
87 start = get_timer(0);
88 start_us = timer_get_us();
89 while (get_timer(start) < 1000)
90 ;
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)
97 return -EINVAL;
98
99 return 0;
100}
101
102static int test_udelay(void)
103{
104 long error;
105 ulong start, delta;
106 int iter;
107
108 start = get_timer(0);
109 for (iter = 0; iter < 1000; iter++)
110 udelay(1000);
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)
116 return -EINVAL;
117
118 return 0;
119}
120
Simon Glassed38aef2020-05-10 11:40:03 -0600121int do_ut_time(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Simon Glass9b4221b2015-05-02 09:25:02 -0600122{
123 int ret = 0;
124
125 ret |= test_get_timer();
126 ret |= test_timer_get_us();
127 ret |= test_time_comparison();
128 ret |= test_udelay();
129
130 printf("Test %s\n", ret ? "failed" : "passed");
131
132 return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
133}