Uwe Hermann | ca7f0e4 | 2007-09-09 20:02:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Uwe Hermann | d22a1d4 | 2007-09-09 20:21:05 +0000 | [diff] [blame] | 4 | * Copyright (C) 2000 Silicon Integrated System Corporation |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009,2010 Carl-Daniel Hailfinger |
Uwe Hermann | ca7f0e4 | 2007-09-09 20:02:45 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
Uwe Hermann | ca7f0e4 | 2007-09-09 20:02:45 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 18 | #ifndef __LIBPAYLOAD__ |
| 19 | |
Nico Huber | 8624e8c | 2012-11-05 21:46:33 +0100 | [diff] [blame] | 20 | #include <stdbool.h> |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 21 | #include <unistd.h> |
Nico Huber | 8624e8c | 2012-11-05 21:46:33 +0100 | [diff] [blame] | 22 | #include <errno.h> |
Stefan Tauner | 839db6d | 2015-11-14 02:55:22 +0000 | [diff] [blame] | 23 | #include <time.h> |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 24 | #include <sys/time.h> |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <limits.h> |
Uwe Hermann | 0846f89 | 2007-08-23 13:34:59 +0000 | [diff] [blame] | 27 | #include "flash.h" |
Jacob Garber | 6c68363 | 2019-06-21 15:33:09 -0600 | [diff] [blame] | 28 | #include "programmer.h" |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 29 | |
Nico Huber | 8624e8c | 2012-11-05 21:46:33 +0100 | [diff] [blame] | 30 | static bool use_clock_gettime = false; |
| 31 | |
| 32 | #if HAVE_CLOCK_GETTIME == 1 |
| 33 | |
| 34 | #ifdef _POSIX_MONOTONIC_CLOCK |
| 35 | static clockid_t clock_id = CLOCK_MONOTONIC; |
| 36 | #else |
| 37 | static clockid_t clock_id = CLOCK_REALTIME; |
| 38 | #endif |
| 39 | |
| 40 | static void clock_usec_delay(int usecs) |
| 41 | { |
| 42 | struct timespec now; |
| 43 | clock_gettime(clock_id, &now); |
| 44 | |
| 45 | const long end_nsec = now.tv_nsec + usecs * 1000L; |
| 46 | const struct timespec end = { |
| 47 | end_nsec / (1000 * 1000 * 1000) + now.tv_sec, |
| 48 | end_nsec % (1000 * 1000 * 1000) |
| 49 | }; |
| 50 | do { |
| 51 | clock_gettime(clock_id, &now); |
| 52 | } while (now.tv_sec < end.tv_sec || (now.tv_sec == end.tv_sec && now.tv_nsec < end.tv_nsec)); |
| 53 | } |
| 54 | |
| 55 | static int clock_check_res(void) |
| 56 | { |
| 57 | struct timespec res; |
| 58 | if (!clock_getres(clock_id, &res)) { |
| 59 | if (res.tv_sec == 0 && res.tv_nsec <= 100) { |
| 60 | msg_pinfo("Using clock_gettime for delay loops (clk_id: %d, resolution: %ldns).\n", |
| 61 | (int)clock_id, res.tv_nsec); |
| 62 | use_clock_gettime = true; |
| 63 | return 1; |
| 64 | } |
| 65 | } else if (clock_id != CLOCK_REALTIME && errno == EINVAL) { |
| 66 | /* Try again with CLOCK_REALTIME. */ |
| 67 | clock_id = CLOCK_REALTIME; |
| 68 | return clock_check_res(); |
| 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | #else |
| 73 | |
| 74 | static inline void clock_usec_delay(int usecs) {} |
| 75 | static inline int clock_check_res(void) { return 0; } |
| 76 | |
| 77 | #endif /* HAVE_CLOCK_GETTIME == 1 */ |
| 78 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 79 | /* loops per microsecond */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 80 | static unsigned long micro = 1; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 81 | |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 82 | __attribute__ ((noinline)) void myusec_delay(unsigned int usecs) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 83 | { |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 84 | unsigned long i; |
| 85 | for (i = 0; i < usecs * micro; i++) { |
| 86 | /* Make sure the compiler doesn't optimize the loop away. */ |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 87 | __asm__ volatile ("" : : "rm" (i) ); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 88 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 91 | static unsigned long measure_os_delay_resolution(void) |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 92 | { |
| 93 | unsigned long timeusec; |
| 94 | struct timeval start, end; |
| 95 | unsigned long counter = 0; |
Elyes HAOUAS | 0cacb11 | 2019-02-04 12:16:38 +0100 | [diff] [blame] | 96 | |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 97 | gettimeofday(&start, NULL); |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 98 | timeusec = 0; |
Elyes HAOUAS | 0cacb11 | 2019-02-04 12:16:38 +0100 | [diff] [blame] | 99 | |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 100 | while (!timeusec && (++counter < 1000000000)) { |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 101 | gettimeofday(&end, NULL); |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 102 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 103 | (end.tv_usec - start.tv_usec); |
| 104 | /* Protect against time going forward too much. */ |
| 105 | if ((end.tv_sec > start.tv_sec) && |
| 106 | ((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1)) |
| 107 | timeusec = 0; |
| 108 | /* Protect against time going backwards during leap seconds. */ |
| 109 | if ((end.tv_sec < start.tv_sec) || (timeusec > LONG_MAX)) |
| 110 | timeusec = 0; |
| 111 | } |
| 112 | return timeusec; |
| 113 | } |
| 114 | |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 115 | static unsigned long measure_delay(unsigned int usecs) |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 116 | { |
| 117 | unsigned long timeusec; |
| 118 | struct timeval start, end; |
Elyes HAOUAS | 0cacb11 | 2019-02-04 12:16:38 +0100 | [diff] [blame] | 119 | |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 120 | gettimeofday(&start, NULL); |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 121 | myusec_delay(usecs); |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 122 | gettimeofday(&end, NULL); |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 123 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 124 | (end.tv_usec - start.tv_usec); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 125 | /* Protect against time going forward too much. */ |
| 126 | if ((end.tv_sec > start.tv_sec) && |
| 127 | ((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1)) |
| 128 | timeusec = LONG_MAX; |
| 129 | /* Protect against time going backwards during leap seconds. */ |
| 130 | if ((end.tv_sec < start.tv_sec) || (timeusec > LONG_MAX)) |
| 131 | timeusec = 1; |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 132 | |
| 133 | return timeusec; |
| 134 | } |
| 135 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 136 | void myusec_calibrate_delay(void) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 137 | { |
Nico Huber | 8624e8c | 2012-11-05 21:46:33 +0100 | [diff] [blame] | 138 | if (clock_check_res()) |
| 139 | return; |
| 140 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 141 | unsigned long count = 1000; |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 142 | unsigned long timeusec, resolution; |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 143 | int i, tries = 0; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 144 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 145 | msg_pinfo("Calibrating delay loop... "); |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 146 | resolution = measure_os_delay_resolution(); |
| 147 | if (resolution) { |
Carl-Daniel Hailfinger | 9f5f215 | 2010-06-04 23:20:21 +0000 | [diff] [blame] | 148 | msg_pdbg("OS timer resolution is %lu usecs, ", resolution); |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 149 | } else { |
| 150 | msg_pinfo("OS timer resolution is unusable. "); |
| 151 | } |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 152 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 153 | recalibrate: |
Urja Rannikko | f640401 | 2010-04-09 00:02:38 +0000 | [diff] [blame] | 154 | count = 1000; |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 155 | while (1) { |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 156 | timeusec = measure_delay(count); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 157 | if (timeusec > 1000000 / 4) |
| 158 | break; |
| 159 | if (count >= ULONG_MAX / 2) { |
| 160 | msg_pinfo("timer loop overflow, reduced precision. "); |
| 161 | break; |
| 162 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 163 | count *= 2; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 164 | } |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 165 | tries ++; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 166 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 167 | /* Avoid division by zero, but in that case the loop is shot anyway. */ |
| 168 | if (!timeusec) |
| 169 | timeusec = 1; |
Elyes HAOUAS | 124ef38 | 2018-03-27 12:15:09 +0200 | [diff] [blame] | 170 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 171 | /* Compute rounded up number of loops per microsecond. */ |
| 172 | micro = (count * micro) / timeusec + 1; |
| 173 | msg_pdbg("%luM loops per second, ", micro); |
| 174 | |
| 175 | /* Did we try to recalibrate less than 5 times? */ |
| 176 | if (tries < 5) { |
| 177 | /* Recheck our timing to make sure we weren't just hitting |
| 178 | * a scheduler delay or something similar. |
| 179 | */ |
| 180 | for (i = 0; i < 4; i++) { |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 181 | if (resolution && (resolution < 10)) { |
| 182 | timeusec = measure_delay(100); |
Elyes HAOUAS | 124ef38 | 2018-03-27 12:15:09 +0200 | [diff] [blame] | 183 | } else if (resolution && |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 184 | (resolution < ULONG_MAX / 200)) { |
| 185 | timeusec = measure_delay(resolution * 10) * |
| 186 | 100 / (resolution * 10); |
| 187 | } else { |
| 188 | /* This workaround should be active for broken |
| 189 | * OS and maybe libpayload. The criterion |
| 190 | * here is horrible or non-measurable OS timer |
| 191 | * resolution which will result in |
| 192 | * measure_delay(100)=0 whereas a longer delay |
| 193 | * (1000 ms) may be sufficient |
| 194 | * to get a nonzero time measurement. |
| 195 | */ |
| 196 | timeusec = measure_delay(1000000) / 10000; |
| 197 | } |
| 198 | if (timeusec < 90) { |
| 199 | msg_pdbg("delay more than 10%% too short (got " |
| 200 | "%lu%% of expected delay), " |
| 201 | "recalculating... ", timeusec); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 202 | goto recalibrate; |
| 203 | } |
| 204 | } |
| 205 | } else { |
| 206 | msg_perr("delay loop is unreliable, trying to continue "); |
| 207 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 208 | |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 209 | /* We're interested in the actual precision. */ |
| 210 | timeusec = measure_delay(10); |
| 211 | msg_pdbg("10 myus = %ld us, ", timeusec); |
| 212 | timeusec = measure_delay(100); |
| 213 | msg_pdbg("100 myus = %ld us, ", timeusec); |
| 214 | timeusec = measure_delay(1000); |
| 215 | msg_pdbg("1000 myus = %ld us, ", timeusec); |
| 216 | timeusec = measure_delay(10000); |
| 217 | msg_pdbg("10000 myus = %ld us, ", timeusec); |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 218 | timeusec = measure_delay(resolution * 4); |
| 219 | msg_pdbg("%ld myus = %ld us, ", resolution * 4, timeusec); |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 220 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 221 | msg_pinfo("OK.\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 222 | } |
Carl-Daniel Hailfinger | 36cc1c8 | 2009-12-24 03:11:55 +0000 | [diff] [blame] | 223 | |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 224 | /* Not very precise sleep. */ |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 225 | void internal_sleep(unsigned int usecs) |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 226 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 227 | #if IS_WINDOWS |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 228 | Sleep((usecs + 999) / 1000); |
Stefan Tauner | 839db6d | 2015-11-14 02:55:22 +0000 | [diff] [blame] | 229 | #elif defined(__DJGPP__) |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 230 | sleep(usecs / 1000000); |
| 231 | usleep(usecs % 1000000); |
Stefan Tauner | 839db6d | 2015-11-14 02:55:22 +0000 | [diff] [blame] | 232 | #else |
| 233 | nanosleep(&(struct timespec){usecs / 1000000, (usecs * 1000) % 1000000000UL}, NULL); |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 234 | #endif |
| 235 | } |
| 236 | |
| 237 | /* Precise delay. */ |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 238 | void internal_delay(unsigned int usecs) |
Carl-Daniel Hailfinger | 36cc1c8 | 2009-12-24 03:11:55 +0000 | [diff] [blame] | 239 | { |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 240 | /* If the delay is >1 s, use internal_sleep because timing does not need to be so precise. */ |
Carl-Daniel Hailfinger | 36cc1c8 | 2009-12-24 03:11:55 +0000 | [diff] [blame] | 241 | if (usecs > 1000000) { |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 242 | internal_sleep(usecs); |
Nico Huber | 8624e8c | 2012-11-05 21:46:33 +0100 | [diff] [blame] | 243 | } else if (use_clock_gettime) { |
| 244 | clock_usec_delay(usecs); |
Carl-Daniel Hailfinger | 36cc1c8 | 2009-12-24 03:11:55 +0000 | [diff] [blame] | 245 | } else { |
| 246 | myusec_delay(usecs); |
| 247 | } |
| 248 | } |
| 249 | |
Elyes HAOUAS | 1b36593 | 2018-04-25 16:03:51 +0200 | [diff] [blame] | 250 | #else |
Patrick Georgi | 97bc95c | 2011-03-08 07:17:44 +0000 | [diff] [blame] | 251 | #include <libpayload.h> |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 252 | |
| 253 | void myusec_calibrate_delay(void) |
| 254 | { |
| 255 | get_cpu_speed(); |
| 256 | } |
| 257 | |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 258 | void internal_delay(unsigned int usecs) |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 259 | { |
| 260 | udelay(usecs); |
| 261 | } |
| 262 | #endif |