xref: /openbmc/u-boot/cmd/misc.c (revision e8f80a5a)
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 
do_sleep(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])14 static int do_sleep(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
15 {
16 	ulong start = get_timer(0);
17 	ulong mdelay = 0;
18 	ulong delay;
19 	char *frpart;
20 
21 	if (argc != 2)
22 		return CMD_RET_USAGE;
23 
24 	delay = simple_strtoul(argv[1], NULL, 10) * CONFIG_SYS_HZ;
25 
26 	frpart = strchr(argv[1], '.');
27 
28 	if (frpart) {
29 		uint mult = CONFIG_SYS_HZ / 10;
30 		for (frpart++; *frpart != '\0' && mult > 0; frpart++) {
31 			if (*frpart < '0' || *frpart > '9') {
32 				mdelay = 0;
33 				break;
34 			}
35 			mdelay += (*frpart - '0') * mult;
36 			mult /= 10;
37 		}
38 	}
39 
40 	delay += mdelay;
41 
42 	while (get_timer(start) < delay) {
43 		if (ctrlc())
44 			return (-1);
45 
46 		udelay(100);
47 	}
48 
49 	return 0;
50 }
51 
52 U_BOOT_CMD(
53 	sleep ,    2,    1,     do_sleep,
54 	"delay execution for some time",
55 	"N\n"
56 	"    - delay execution for N seconds (N is _decimal_ and can be\n"
57 	"      fractional)"
58 );
59 
60 #ifdef CONFIG_CMD_TIMER
do_timer(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])61 static int do_timer(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
62 {
63 	static ulong start;
64 
65 	if (argc != 2)
66 		return CMD_RET_USAGE;
67 
68 	if (!strcmp(argv[1], "start"))
69 		start = get_timer(0);
70 
71 	if (!strcmp(argv[1], "get")) {
72 		ulong msecs = get_timer(start) * 1000 / CONFIG_SYS_HZ;
73 		printf("%ld.%03d\n", msecs / 1000, (int)(msecs % 1000));
74 	}
75 
76 	return 0;
77 }
78 
79 U_BOOT_CMD(
80 	timer,    2,    1,     do_timer,
81 	"access the system timer",
82 	"start - Reset the timer reference.\n"
83 	"timer get   - Print the time since 'start'."
84 );
85 #endif
86