xref: /aosp_15_r20/external/flashrom/udelay.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2000 Silicon Integrated System Corporation
5  * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
6  * Copyright (C) 2024 Google LLC
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18 
19 #ifndef __LIBPAYLOAD__
20 
21 #include <stdbool.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <time.h>
25 #include <sys/time.h>
26 #include <stdlib.h>
27 #include <limits.h>
28 #include "flash.h"
29 #include "programmer.h"
30 
31 #if HAVE_CLOCK_GETTIME == 1
32 
clock_usec_delay(int usecs)33 static void clock_usec_delay(int usecs)
34 {
35 	static clockid_t clock_id =
36 #ifdef _POSIX_MONOTONIC_CLOCK
37 		CLOCK_MONOTONIC;
38 #else
39 		CLOCK_REALTIME;
40 #endif
41 
42 	struct timespec now;
43 	if (clock_gettime(clock_id, &now)) {
44 		/* Fall back to realtime clock if monotonic doesn't work */
45 		if (clock_id != CLOCK_REALTIME && errno == EINVAL) {
46 			clock_id = CLOCK_REALTIME;
47 			clock_gettime(clock_id, &now);
48 		}
49 	}
50 
51 	const long end_nsec = now.tv_nsec + usecs * 1000L;
52 	const struct timespec end = {
53 		end_nsec / (1000 * 1000 * 1000) + now.tv_sec,
54 		end_nsec % (1000 * 1000 * 1000)
55 	};
56 	do {
57 		clock_gettime(clock_id, &now);
58 	} while (now.tv_sec < end.tv_sec || (now.tv_sec == end.tv_sec && now.tv_nsec < end.tv_nsec));
59 }
60 
61 #else
62 
clock_usec_delay(unsigned int usecs)63 static void clock_usec_delay(unsigned int usecs) {
64 	struct timeval start, end;
65 	unsigned long elapsed = 0;
66 
67 	gettimeofday(&start, NULL);
68 
69 	while (elapsed < usecs) {
70 		gettimeofday(&end, NULL);
71 		elapsed = 1000000 * (end.tv_sec - start.tv_sec) +
72 			   (end.tv_usec - start.tv_usec);
73 		/* Protect against time going forward too much. */
74 		if ((end.tv_sec > start.tv_sec) &&
75 		    ((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1))
76 			elapsed = 0;
77 		/* Protect against time going backwards during leap seconds. */
78 		if ((end.tv_sec < start.tv_sec) || (elapsed > LONG_MAX))
79 			elapsed = 0;
80 	}
81 }
82 
83 #endif /* HAVE_CLOCK_GETTIME == 1 */
84 
85 /* Not very precise sleep. */
internal_sleep(unsigned int usecs)86 void internal_sleep(unsigned int usecs)
87 {
88 #if IS_WINDOWS
89 	Sleep((usecs + 999) / 1000);
90 #else
91 	nanosleep(&(struct timespec){usecs / 1000000, (usecs * 1000) % 1000000000UL}, NULL);
92 #endif
93 }
94 
95 static const unsigned min_sleep = CONFIG_DELAY_MINIMUM_SLEEP_US;
96 
97 /* Precise delay. */
default_delay(unsigned int usecs)98 void default_delay(unsigned int usecs)
99 {
100 	if (usecs < min_sleep) {
101 		clock_usec_delay(usecs);
102 	} else {
103 		internal_sleep(usecs);
104 	}
105 }
106 
107 #else
108 #include <libpayload.h>
109 
default_delay(unsigned int usecs)110 void default_delay(unsigned int usecs)
111 {
112 	udelay(usecs);
113 }
114 #endif
115