Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Misc functions |
| 9 | */ |
| 10 | #include <common.h> |
| 11 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 12 | #include <console.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 13 | #include <linux/delay.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 14 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 15 | static int do_sleep(struct cmd_tbl *cmdtp, int flag, int argc, |
| 16 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 17 | { |
Stefan Roese | f1d6699 | 2005-10-20 16:36:44 +0200 | [diff] [blame] | 18 | ulong start = get_timer(0); |
mario.six@gdsys.cc | ae2eba6 | 2016-07-19 16:20:13 +0200 | [diff] [blame] | 19 | ulong mdelay = 0; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 20 | ulong delay; |
mario.six@gdsys.cc | ae2eba6 | 2016-07-19 16:20:13 +0200 | [diff] [blame] | 21 | char *frpart; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 22 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 23 | if (argc != 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 24 | return CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 25 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 26 | delay = simple_strtoul(argv[1], NULL, 10) * CONFIG_SYS_HZ; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 27 | |
mario.six@gdsys.cc | ae2eba6 | 2016-07-19 16:20:13 +0200 | [diff] [blame] | 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 | |
Stefan Roese | f1d6699 | 2005-10-20 16:36:44 +0200 | [diff] [blame] | 44 | while (get_timer(start) < delay) { |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 45 | if (ctrlc()) |
Stefan Roese | f1d6699 | 2005-10-20 16:36:44 +0200 | [diff] [blame] | 46 | return (-1); |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 47 | |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 48 | udelay(100); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 49 | } |
Stefan Roese | f1d6699 | 2005-10-20 16:36:44 +0200 | [diff] [blame] | 50 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 51 | return 0; |
| 52 | } |
Stefan Roese | f1d6699 | 2005-10-20 16:36:44 +0200 | [diff] [blame] | 53 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 54 | U_BOOT_CMD( |
Detlev Zundel | 409a285 | 2007-05-23 19:02:41 +0200 | [diff] [blame] | 55 | sleep , 2, 1, do_sleep, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 56 | "delay execution for some time", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 57 | "N\n" |
mario.six@gdsys.cc | ae2eba6 | 2016-07-19 16:20:13 +0200 | [diff] [blame] | 58 | " - delay execution for N seconds (N is _decimal_ and can be\n" |
| 59 | " fractional)" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 60 | ); |
Joe Hershberger | 59f3f52 | 2012-10-03 12:14:57 +0000 | [diff] [blame] | 61 | |
| 62 | #ifdef CONFIG_CMD_TIMER |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 63 | static int do_timer(struct cmd_tbl *cmdtp, int flag, int argc, |
| 64 | char *const argv[]) |
Joe Hershberger | 59f3f52 | 2012-10-03 12:14:57 +0000 | [diff] [blame] | 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 |