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. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 22 | #ifndef __LIBPAYLOAD__ |
| 23 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
Stefan Tauner | 839db6d | 2015-11-14 02:55:22 +0000 | [diff] [blame] | 25 | #include <time.h> |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 26 | #include <sys/time.h> |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <limits.h> |
Uwe Hermann | 0846f89 | 2007-08-23 13:34:59 +0000 | [diff] [blame] | 29 | #include "flash.h" |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 30 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 31 | /* loops per microsecond */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 32 | static unsigned long micro = 1; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 33 | |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 34 | __attribute__ ((noinline)) void myusec_delay(unsigned int usecs) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 35 | { |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 36 | unsigned long i; |
| 37 | for (i = 0; i < usecs * micro; i++) { |
| 38 | /* Make sure the compiler doesn't optimize the loop away. */ |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 39 | __asm__ volatile ("" : : "rm" (i) ); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 40 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 43 | static unsigned long measure_os_delay_resolution(void) |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 44 | { |
| 45 | unsigned long timeusec; |
| 46 | struct timeval start, end; |
| 47 | unsigned long counter = 0; |
| 48 | |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 49 | gettimeofday(&start, NULL); |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 50 | timeusec = 0; |
| 51 | |
| 52 | while (!timeusec && (++counter < 1000000000)) { |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 53 | gettimeofday(&end, NULL); |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 54 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 55 | (end.tv_usec - start.tv_usec); |
| 56 | /* Protect against time going forward too much. */ |
| 57 | if ((end.tv_sec > start.tv_sec) && |
| 58 | ((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1)) |
| 59 | timeusec = 0; |
| 60 | /* Protect against time going backwards during leap seconds. */ |
| 61 | if ((end.tv_sec < start.tv_sec) || (timeusec > LONG_MAX)) |
| 62 | timeusec = 0; |
| 63 | } |
| 64 | return timeusec; |
| 65 | } |
| 66 | |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 67 | static unsigned long measure_delay(unsigned int usecs) |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 68 | { |
| 69 | unsigned long timeusec; |
| 70 | struct timeval start, end; |
| 71 | |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 72 | gettimeofday(&start, NULL); |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 73 | myusec_delay(usecs); |
Peter Huewe | 73f8ec8 | 2011-01-24 19:15:51 +0000 | [diff] [blame] | 74 | gettimeofday(&end, NULL); |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 75 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 76 | (end.tv_usec - start.tv_usec); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 77 | /* Protect against time going forward too much. */ |
| 78 | if ((end.tv_sec > start.tv_sec) && |
| 79 | ((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1)) |
| 80 | timeusec = LONG_MAX; |
| 81 | /* Protect against time going backwards during leap seconds. */ |
| 82 | if ((end.tv_sec < start.tv_sec) || (timeusec > LONG_MAX)) |
| 83 | timeusec = 1; |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 84 | |
| 85 | return timeusec; |
| 86 | } |
| 87 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 88 | void myusec_calibrate_delay(void) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 89 | { |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 90 | unsigned long count = 1000; |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 91 | unsigned long timeusec, resolution; |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 92 | int i, tries = 0; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 93 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 94 | msg_pinfo("Calibrating delay loop... "); |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 95 | resolution = measure_os_delay_resolution(); |
| 96 | if (resolution) { |
Carl-Daniel Hailfinger | 9f5f215 | 2010-06-04 23:20:21 +0000 | [diff] [blame] | 97 | msg_pdbg("OS timer resolution is %lu usecs, ", resolution); |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 98 | } else { |
| 99 | msg_pinfo("OS timer resolution is unusable. "); |
| 100 | } |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 101 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 102 | recalibrate: |
Urja Rannikko | f640401 | 2010-04-09 00:02:38 +0000 | [diff] [blame] | 103 | count = 1000; |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 104 | while (1) { |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 105 | timeusec = measure_delay(count); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 106 | if (timeusec > 1000000 / 4) |
| 107 | break; |
| 108 | if (count >= ULONG_MAX / 2) { |
| 109 | msg_pinfo("timer loop overflow, reduced precision. "); |
| 110 | break; |
| 111 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 112 | count *= 2; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 113 | } |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 114 | tries ++; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 115 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 116 | /* Avoid division by zero, but in that case the loop is shot anyway. */ |
| 117 | if (!timeusec) |
| 118 | timeusec = 1; |
| 119 | |
| 120 | /* Compute rounded up number of loops per microsecond. */ |
| 121 | micro = (count * micro) / timeusec + 1; |
| 122 | msg_pdbg("%luM loops per second, ", micro); |
| 123 | |
| 124 | /* Did we try to recalibrate less than 5 times? */ |
| 125 | if (tries < 5) { |
| 126 | /* Recheck our timing to make sure we weren't just hitting |
| 127 | * a scheduler delay or something similar. |
| 128 | */ |
| 129 | for (i = 0; i < 4; i++) { |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 130 | if (resolution && (resolution < 10)) { |
| 131 | timeusec = measure_delay(100); |
| 132 | } else if (resolution && |
| 133 | (resolution < ULONG_MAX / 200)) { |
| 134 | timeusec = measure_delay(resolution * 10) * |
| 135 | 100 / (resolution * 10); |
| 136 | } else { |
| 137 | /* This workaround should be active for broken |
| 138 | * OS and maybe libpayload. The criterion |
| 139 | * here is horrible or non-measurable OS timer |
| 140 | * resolution which will result in |
| 141 | * measure_delay(100)=0 whereas a longer delay |
| 142 | * (1000 ms) may be sufficient |
| 143 | * to get a nonzero time measurement. |
| 144 | */ |
| 145 | timeusec = measure_delay(1000000) / 10000; |
| 146 | } |
| 147 | if (timeusec < 90) { |
| 148 | msg_pdbg("delay more than 10%% too short (got " |
| 149 | "%lu%% of expected delay), " |
| 150 | "recalculating... ", timeusec); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 151 | goto recalibrate; |
| 152 | } |
| 153 | } |
| 154 | } else { |
| 155 | msg_perr("delay loop is unreliable, trying to continue "); |
| 156 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 157 | |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 158 | /* We're interested in the actual precision. */ |
| 159 | timeusec = measure_delay(10); |
| 160 | msg_pdbg("10 myus = %ld us, ", timeusec); |
| 161 | timeusec = measure_delay(100); |
| 162 | msg_pdbg("100 myus = %ld us, ", timeusec); |
| 163 | timeusec = measure_delay(1000); |
| 164 | msg_pdbg("1000 myus = %ld us, ", timeusec); |
| 165 | timeusec = measure_delay(10000); |
| 166 | msg_pdbg("10000 myus = %ld us, ", timeusec); |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame] | 167 | timeusec = measure_delay(resolution * 4); |
| 168 | msg_pdbg("%ld myus = %ld us, ", resolution * 4, timeusec); |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 169 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 170 | msg_pinfo("OK.\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 171 | } |
Carl-Daniel Hailfinger | 36cc1c8 | 2009-12-24 03:11:55 +0000 | [diff] [blame] | 172 | |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 173 | /* Not very precise sleep. */ |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 174 | void internal_sleep(unsigned int usecs) |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 175 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 176 | #if IS_WINDOWS |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 177 | Sleep((usecs + 999) / 1000); |
Stefan Tauner | 839db6d | 2015-11-14 02:55:22 +0000 | [diff] [blame] | 178 | #elif defined(__DJGPP__) |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 179 | sleep(usecs / 1000000); |
| 180 | usleep(usecs % 1000000); |
Stefan Tauner | 839db6d | 2015-11-14 02:55:22 +0000 | [diff] [blame] | 181 | #else |
| 182 | nanosleep(&(struct timespec){usecs / 1000000, (usecs * 1000) % 1000000000UL}, NULL); |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 183 | #endif |
| 184 | } |
| 185 | |
| 186 | /* Precise delay. */ |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 187 | void internal_delay(unsigned int usecs) |
Carl-Daniel Hailfinger | 36cc1c8 | 2009-12-24 03:11:55 +0000 | [diff] [blame] | 188 | { |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 189 | /* 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] | 190 | if (usecs > 1000000) { |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 191 | internal_sleep(usecs); |
Carl-Daniel Hailfinger | 36cc1c8 | 2009-12-24 03:11:55 +0000 | [diff] [blame] | 192 | } else { |
| 193 | myusec_delay(usecs); |
| 194 | } |
| 195 | } |
| 196 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 197 | #else |
Patrick Georgi | 97bc95c | 2011-03-08 07:17:44 +0000 | [diff] [blame] | 198 | #include <libpayload.h> |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 199 | |
| 200 | void myusec_calibrate_delay(void) |
| 201 | { |
| 202 | get_cpu_speed(); |
| 203 | } |
| 204 | |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 205 | void internal_delay(unsigned int usecs) |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 206 | { |
| 207 | udelay(usecs); |
| 208 | } |
| 209 | #endif |