| 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 |
* |
| 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 |
|
| 18 |
#include "platform/udelay.h" |
| 19 |
|
| 20 |
#include <stdbool.h> |
| 21 |
#include <unistd.h> |
| 22 |
#include <errno.h> |
| 23 |
#include <time.h> |
| 24 |
#include <sys/time.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <limits.h> |
| 27 |
#include "flash.h" |
| 28 |
#include "programmer.h" |
| 29 |
|
| 30 |
/* loops per microsecond */ |
| 31 |
static unsigned long micro = 1; |
| 32 |
|
| 33 |
__attribute__ ((noinline)) static void myusec_delay(unsigned int usecs) |
| 34 |
{ |
| 35 |
unsigned long i; |
| 36 |
for (i = 0; i < usecs * micro; i++) { |
| 37 |
/* Make sure the compiler doesn't optimize the loop away. */ |
| 38 |
__asm__ volatile ("" : : "rm" (i) ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
static unsigned long measure_os_delay_resolution(void) |
| 43 |
{ |
| 44 |
unsigned long timeusec; |
| 45 |
struct timeval start, end; |
| 46 |
unsigned long counter = 0; |
| 47 |
|
| 48 |
gettimeofday(&start, NULL); |
| 49 |
timeusec = 0; |
| 50 |
|
| 51 |
while (!timeusec && (++counter < 1000000000)) { |
| 52 |
gettimeofday(&end, NULL); |
| 53 |
timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 54 |
(end.tv_usec - start.tv_usec); |
| 55 |
/* Protect against time going forward too much. */ |
| 56 |
if ((end.tv_sec > start.tv_sec) && |
| 57 |
((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1)) |
| 58 |
timeusec = 0; |
| 59 |
/* Protect against time going backwards during leap seconds. */ |
| 60 |
if ((end.tv_sec < start.tv_sec) || (timeusec > LONG_MAX)) |
| 61 |
timeusec = 0; |
| 62 |
} |
| 63 |
return timeusec; |
| 64 |
} |
| 65 |
|
| 66 |
static unsigned long measure_delay(unsigned int usecs) |
| 67 |
{ |
| 68 |
unsigned long timeusec; |
| 69 |
struct timeval start, end; |
| 70 |
|
| 71 |
gettimeofday(&start, NULL); |
| 72 |
myusec_delay(usecs); |
| 73 |
gettimeofday(&end, NULL); |
| 74 |
timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 75 |
(end.tv_usec - start.tv_usec); |
| 76 |
/* Protect against time going forward too much. */ |
| 77 |
if ((end.tv_sec > start.tv_sec) && |
| 78 |
((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1)) |
| 79 |
timeusec = LONG_MAX; |
| 80 |
/* Protect against time going backwards during leap seconds. */ |
| 81 |
if ((end.tv_sec < start.tv_sec) || (timeusec > LONG_MAX)) |
| 82 |
timeusec = 1; |
| 83 |
|
| 84 |
return timeusec; |
| 85 |
} |
| 86 |
|
| 87 |
static void myusec_calibrate_delay(void) |
| 88 |
{ |
| 89 |
unsigned long count = 1000; |
| 90 |
unsigned long timeusec, resolution; |
| 91 |
int i, tries = 0; |
| 92 |
|
| 93 |
msg_pinfo("Calibrating delay loop... "); |
| 94 |
/* Timing resolution on DJGPP is about 50ms, but measure it precisely. */ |
| 95 |
resolution = measure_os_delay_resolution(); |
| 96 |
if (resolution) { |
| 97 |
msg_pdbg("OS timer resolution is %lu usecs, ", resolution); |
| 98 |
} else { |
| 99 |
msg_pinfo("OS timer resolution is unusable. "); |
| 100 |
} |
| 101 |
|
| 102 |
recalibrate: |
| 103 |
count = 1000; |
| 104 |
while (1) { |
| 105 |
timeusec = measure_delay(count); |
| 106 |
if (timeusec > 1000000 / 4) |
| 107 |
break; |
| 108 |
if (count >= ULONG_MAX / 2) { |
| 109 |
msg_pinfo("timer loop overflow, reduced precision. "); |
| 110 |
break; |
| 111 |
} |
| 112 |
count *= 2; |
| 113 |
} |
| 114 |
tries ++; |
| 115 |
|
| 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++) { |
| 130 |
timeusec = measure_delay(resolution * 10) * |
| 131 |
100 / (resolution * 10); |
| 132 |
|
| 133 |
if (timeusec < 90) { |
| 134 |
msg_pdbg("delay more than 10%% too short (got " |
| 135 |
"%lu%% of expected delay), " |
| 136 |
"recalculating... ", timeusec); |
| 137 |
goto recalibrate; |
| 138 |
} |
| 139 |
} |
| 140 |
} else { |
| 141 |
msg_perr("delay loop is unreliable, trying to continue "); |
| 142 |
} |
| 143 |
|
| 144 |
/* We're interested in the actual precision. */ |
| 145 |
timeusec = measure_delay(resolution * 4); |
| 146 |
msg_pdbg("%ld myus = %ld us, ", resolution * 4, timeusec); |
| 147 |
|
| 148 |
msg_pinfo("OK.\n"); |
| 149 |
} |
| 150 |
|
| 151 |
/* Not very precise sleep. */ |
| 152 |
void internal_sleep(unsigned int usecs) |
| 153 |
{ |
| 154 |
sleep(usecs / 1000000); |
| 155 |
usleep(usecs % 1000000); |
| 156 |
} |
| 157 |
|
| 158 |
static const unsigned min_sleep = CONFIG_DELAY_MINIMUM_SLEEP_US; |
| 159 |
|
| 160 |
/* Precise delay. */ |
| 161 |
void default_delay(unsigned int usecs) |
| 162 |
{ |
| 163 |
static bool calibrated = false; |
| 164 |
|
| 165 |
if (usecs < min_sleep) { |
| 166 |
if (!calibrated) { |
| 167 |
myusec_calibrate_delay(); |
| 168 |
calibrated = true; |
| 169 |
} |
| 170 |
myusec_delay(usecs); |
| 171 |
} else { |
| 172 |
internal_sleep(usecs); |
| 173 |
} |
| 174 |
} |