Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 8271f5d | 2015-05-02 09:25:02 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 8271f5d | 2015-05-02 09:25:02 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 8271f5d | 2015-05-02 09:25:02 -0600 | [diff] [blame] | 7 | #include <command.h> |
| 8 | #include <errno.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 9 | #include <time.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 10 | #include <linux/delay.h> |
Simon Glass | 8271f5d | 2015-05-02 09:25:02 -0600 | [diff] [blame] | 11 | |
| 12 | static 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 | |
| 46 | static 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 | |
| 80 | static 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 | |
| 101 | static 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 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 120 | int do_ut_time(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Simon Glass | 8271f5d | 2015-05-02 09:25:02 -0600 | [diff] [blame] | 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 | } |