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