blob: a1eef72a6c2eb7f2ff4603b074d7dd55fe8d143f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00002/*
3 * (C) Copyright 2001
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk38635852002-08-27 05:55:31 +00005 */
6
7/*
8 * Misc functions
9 */
10#include <common.h>
11#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070012#include <console.h>
wdenk38635852002-08-27 05:55:31 +000013
Kim Phillips088f1b12012-10-29 13:34:31 +000014static int do_sleep(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000015{
Stefan Roesec4c13df2005-10-20 16:36:44 +020016 ulong start = get_timer(0);
mario.six@gdsys.ccc4974632016-07-19 16:20:13 +020017 ulong mdelay = 0;
wdenk38635852002-08-27 05:55:31 +000018 ulong delay;
mario.six@gdsys.ccc4974632016-07-19 16:20:13 +020019 char *frpart;
wdenk38635852002-08-27 05:55:31 +000020
Wolfgang Denk47e26b12010-07-17 01:06:04 +020021 if (argc != 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000022 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +000023
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020024 delay = simple_strtoul(argv[1], NULL, 10) * CONFIG_SYS_HZ;
wdenk38635852002-08-27 05:55:31 +000025
mario.six@gdsys.ccc4974632016-07-19 16:20:13 +020026 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
Stefan Roesec4c13df2005-10-20 16:36:44 +020042 while (get_timer(start) < delay) {
Kim Phillips088f1b12012-10-29 13:34:31 +000043 if (ctrlc())
Stefan Roesec4c13df2005-10-20 16:36:44 +020044 return (-1);
Wolfgang Denk47e26b12010-07-17 01:06:04 +020045
Kim Phillips088f1b12012-10-29 13:34:31 +000046 udelay(100);
wdenk38635852002-08-27 05:55:31 +000047 }
Stefan Roesec4c13df2005-10-20 16:36:44 +020048
wdenk38635852002-08-27 05:55:31 +000049 return 0;
50}
Stefan Roesec4c13df2005-10-20 16:36:44 +020051
wdenk0d498392003-07-01 21:06:45 +000052U_BOOT_CMD(
Detlev Zundel99121212007-05-23 19:02:41 +020053 sleep , 2, 1, do_sleep,
Peter Tyser2fb26042009-01-27 18:03:12 -060054 "delay execution for some time",
wdenk8bde7f72003-06-27 21:31:46 +000055 "N\n"
mario.six@gdsys.ccc4974632016-07-19 16:20:13 +020056 " - delay execution for N seconds (N is _decimal_ and can be\n"
57 " fractional)"
wdenk8bde7f72003-06-27 21:31:46 +000058);
Joe Hershbergerda83bcd2012-10-03 12:14:57 +000059
60#ifdef CONFIG_CMD_TIMER
61static 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
79U_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