common: Drop linux/delay.h from common header
[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 #include <linux/delay.h>
14
15 static int do_sleep(struct cmd_tbl *cmdtp, int flag, int argc,
16                     char *const argv[])
17 {
18         ulong start = get_timer(0);
19         ulong mdelay = 0;
20         ulong delay;
21         char *frpart;
22
23         if (argc != 2)
24                 return CMD_RET_USAGE;
25
26         delay = simple_strtoul(argv[1], NULL, 10) * CONFIG_SYS_HZ;
27
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
44         while (get_timer(start) < delay) {
45                 if (ctrlc())
46                         return (-1);
47
48                 udelay(100);
49         }
50
51         return 0;
52 }
53
54 U_BOOT_CMD(
55         sleep ,    2,    1,     do_sleep,
56         "delay execution for some time",
57         "N\n"
58         "    - delay execution for N seconds (N is _decimal_ and can be\n"
59         "      fractional)"
60 );
61
62 #ifdef CONFIG_CMD_TIMER
63 static int do_timer(struct cmd_tbl *cmdtp, int flag, int argc,
64                     char *const argv[])
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