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 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 23 | #include <sys/time.h> |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <limits.h> |
Uwe Hermann | 0846f89 | 2007-08-23 13:34:59 +0000 | [diff] [blame] | 26 | #include "flash.h" |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 27 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 28 | /* loops per microsecond */ |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 29 | unsigned long micro = 1; |
| 30 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 31 | __attribute__ ((noinline)) void myusec_delay(int usecs) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 32 | { |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 33 | unsigned long i; |
| 34 | for (i = 0; i < usecs * micro; i++) { |
| 35 | /* Make sure the compiler doesn't optimize the loop away. */ |
| 36 | asm volatile ("" : : "rm" (i) ); |
| 37 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame^] | 40 | unsigned long measure_os_delay_resolution(void) |
| 41 | { |
| 42 | unsigned long timeusec; |
| 43 | struct timeval start, end; |
| 44 | unsigned long counter = 0; |
| 45 | |
| 46 | gettimeofday(&start, 0); |
| 47 | timeusec = 0; |
| 48 | |
| 49 | while (!timeusec && (++counter < 1000000000)) { |
| 50 | gettimeofday(&end, 0); |
| 51 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 52 | (end.tv_usec - start.tv_usec); |
| 53 | /* Protect against time going forward too much. */ |
| 54 | if ((end.tv_sec > start.tv_sec) && |
| 55 | ((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1)) |
| 56 | timeusec = 0; |
| 57 | /* Protect against time going backwards during leap seconds. */ |
| 58 | if ((end.tv_sec < start.tv_sec) || (timeusec > LONG_MAX)) |
| 59 | timeusec = 0; |
| 60 | } |
| 61 | return timeusec; |
| 62 | } |
| 63 | |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 64 | unsigned long measure_delay(int usecs) |
| 65 | { |
| 66 | unsigned long timeusec; |
| 67 | struct timeval start, end; |
| 68 | |
| 69 | gettimeofday(&start, 0); |
| 70 | myusec_delay(usecs); |
| 71 | gettimeofday(&end, 0); |
| 72 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 73 | (end.tv_usec - start.tv_usec); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 74 | /* Protect against time going forward too much. */ |
| 75 | if ((end.tv_sec > start.tv_sec) && |
| 76 | ((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1)) |
| 77 | timeusec = LONG_MAX; |
| 78 | /* Protect against time going backwards during leap seconds. */ |
| 79 | if ((end.tv_sec < start.tv_sec) || (timeusec > LONG_MAX)) |
| 80 | timeusec = 1; |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 81 | |
| 82 | return timeusec; |
| 83 | } |
| 84 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 85 | void myusec_calibrate_delay(void) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 86 | { |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 87 | unsigned long count = 1000; |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame^] | 88 | unsigned long timeusec, resolution; |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 89 | int i, tries = 0; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 90 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 91 | msg_pinfo("Calibrating delay loop... "); |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame^] | 92 | resolution = measure_os_delay_resolution(); |
| 93 | if (resolution) { |
| 94 | msg_pdbg("OS timer resolution is %u usecs, ", resolution); |
| 95 | } else { |
| 96 | msg_pinfo("OS timer resolution is unusable. "); |
| 97 | } |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 98 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 99 | recalibrate: |
Urja Rannikko | f640401 | 2010-04-09 00:02:38 +0000 | [diff] [blame] | 100 | count = 1000; |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 101 | while (1) { |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 102 | timeusec = measure_delay(count); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 103 | if (timeusec > 1000000 / 4) |
| 104 | break; |
| 105 | if (count >= ULONG_MAX / 2) { |
| 106 | msg_pinfo("timer loop overflow, reduced precision. "); |
| 107 | break; |
| 108 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 109 | count *= 2; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 110 | } |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 111 | tries ++; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 112 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 113 | /* Avoid division by zero, but in that case the loop is shot anyway. */ |
| 114 | if (!timeusec) |
| 115 | timeusec = 1; |
| 116 | |
| 117 | /* Compute rounded up number of loops per microsecond. */ |
| 118 | micro = (count * micro) / timeusec + 1; |
| 119 | msg_pdbg("%luM loops per second, ", micro); |
| 120 | |
| 121 | /* Did we try to recalibrate less than 5 times? */ |
| 122 | if (tries < 5) { |
| 123 | /* Recheck our timing to make sure we weren't just hitting |
| 124 | * a scheduler delay or something similar. |
| 125 | */ |
| 126 | for (i = 0; i < 4; i++) { |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame^] | 127 | if (resolution && (resolution < 10)) { |
| 128 | timeusec = measure_delay(100); |
| 129 | } else if (resolution && |
| 130 | (resolution < ULONG_MAX / 200)) { |
| 131 | timeusec = measure_delay(resolution * 10) * |
| 132 | 100 / (resolution * 10); |
| 133 | } else { |
| 134 | /* This workaround should be active for broken |
| 135 | * OS and maybe libpayload. The criterion |
| 136 | * here is horrible or non-measurable OS timer |
| 137 | * resolution which will result in |
| 138 | * measure_delay(100)=0 whereas a longer delay |
| 139 | * (1000 ms) may be sufficient |
| 140 | * to get a nonzero time measurement. |
| 141 | */ |
| 142 | timeusec = measure_delay(1000000) / 10000; |
| 143 | } |
| 144 | if (timeusec < 90) { |
| 145 | msg_pdbg("delay more than 10%% too short (got " |
| 146 | "%lu%% of expected delay), " |
| 147 | "recalculating... ", timeusec); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 148 | goto recalibrate; |
| 149 | } |
| 150 | } |
| 151 | } else { |
| 152 | msg_perr("delay loop is unreliable, trying to continue "); |
| 153 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 154 | |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 155 | /* We're interested in the actual precision. */ |
| 156 | timeusec = measure_delay(10); |
| 157 | msg_pdbg("10 myus = %ld us, ", timeusec); |
| 158 | timeusec = measure_delay(100); |
| 159 | msg_pdbg("100 myus = %ld us, ", timeusec); |
| 160 | timeusec = measure_delay(1000); |
| 161 | msg_pdbg("1000 myus = %ld us, ", timeusec); |
| 162 | timeusec = measure_delay(10000); |
| 163 | msg_pdbg("10000 myus = %ld us, ", timeusec); |
Carl-Daniel Hailfinger | b929d11 | 2010-06-03 21:48:13 +0000 | [diff] [blame^] | 164 | timeusec = measure_delay(resolution * 4); |
| 165 | msg_pdbg("%ld myus = %ld us, ", resolution * 4, timeusec); |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 166 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 167 | msg_pinfo("OK.\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 168 | } |
Carl-Daniel Hailfinger | 36cc1c8 | 2009-12-24 03:11:55 +0000 | [diff] [blame] | 169 | |
| 170 | void internal_delay(int usecs) |
| 171 | { |
| 172 | /* If the delay is >1 s, use usleep because timing does not need to |
| 173 | * be so precise. |
| 174 | */ |
| 175 | if (usecs > 1000000) { |
| 176 | usleep(usecs); |
| 177 | } else { |
| 178 | myusec_delay(usecs); |
| 179 | } |
| 180 | } |
| 181 | |