Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
3863585b WD |
2 | /* |
3 | * (C) Copyright 2001 | |
4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
3863585b WD |
5 | */ |
6 | ||
7 | /* | |
8 | * Misc functions | |
9 | */ | |
10 | #include <common.h> | |
11 | #include <command.h> | |
24b852a7 | 12 | #include <console.h> |
c05ed00a | 13 | #include <linux/delay.h> |
3863585b | 14 | |
09140113 SG |
15 | static int do_sleep(struct cmd_tbl *cmdtp, int flag, int argc, |
16 | char *const argv[]) | |
3863585b | 17 | { |
c4c13df2 | 18 | ulong start = get_timer(0); |
c4974632 | 19 | ulong mdelay = 0; |
3863585b | 20 | ulong delay; |
c4974632 | 21 | char *frpart; |
3863585b | 22 | |
47e26b1b | 23 | if (argc != 2) |
4c12eeb8 | 24 | return CMD_RET_USAGE; |
3863585b | 25 | |
6d0f6bcf | 26 | delay = simple_strtoul(argv[1], NULL, 10) * CONFIG_SYS_HZ; |
3863585b | 27 | |
c4974632 | 28 | frpart = strchr(argv[1], '.'); |
29 | ||
30 | if (frpart) { | |
31 | uint mult = CONFIG_SYS_HZ / 10; | |
32 | for (frpart++; *frpart != '\0' && mult > 0; frpart++) { | |
33 | if (*frpart < '0' || *frpart > '9') { | |
34 | mdelay = 0; | |
35 | break; | |
36 | } | |
37 | mdelay += (*frpart - '0') * mult; | |
38 | mult /= 10; | |
39 | } | |
40 | } | |
41 | ||
42 | delay += mdelay; | |
43 | ||
c4c13df2 | 44 | while (get_timer(start) < delay) { |
088f1b19 | 45 | if (ctrlc()) |
c4c13df2 | 46 | return (-1); |
47e26b1b | 47 | |
088f1b19 | 48 | udelay(100); |
3863585b | 49 | } |
c4c13df2 | 50 | |
3863585b WD |
51 | return 0; |
52 | } | |
c4c13df2 | 53 | |
0d498393 | 54 | U_BOOT_CMD( |
9912121f | 55 | sleep , 2, 1, do_sleep, |
2fb2604d | 56 | "delay execution for some time", |
8bde7f77 | 57 | "N\n" |
c4974632 | 58 | " - delay execution for N seconds (N is _decimal_ and can be\n" |
59 | " fractional)" | |
8bde7f77 | 60 | ); |
da83bcd7 JH |
61 | |
62 | #ifdef CONFIG_CMD_TIMER | |
09140113 SG |
63 | static int do_timer(struct cmd_tbl *cmdtp, int flag, int argc, |
64 | char *const argv[]) | |
da83bcd7 JH |
65 | { |
66 | static ulong start; | |
67 | ||
68 | if (argc != 2) | |
69 | return CMD_RET_USAGE; | |
70 | ||
71 | if (!strcmp(argv[1], "start")) | |
72 | start = get_timer(0); | |
73 | ||
74 | if (!strcmp(argv[1], "get")) { | |
75 | ulong msecs = get_timer(start) * 1000 / CONFIG_SYS_HZ; | |
76 | printf("%ld.%03d\n", msecs / 1000, (int)(msecs % 1000)); | |
77 | } | |
78 | ||
79 | return 0; | |
80 | } | |
81 | ||
82 | U_BOOT_CMD( | |
83 | timer, 2, 1, do_timer, | |
84 | "access the system timer", | |
85 | "start - Reset the timer reference.\n" | |
86 | "timer get - Print the time since 'start'." | |
87 | ); | |
88 | #endif |