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 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 22 | #include <sys/time.h> |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <limits.h> |
Uwe Hermann | 0846f89 | 2007-08-23 13:34:59 +0000 | [diff] [blame] | 25 | #include "flash.h" |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 26 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 27 | /* loops per microsecond */ |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 28 | unsigned long micro = 1; |
| 29 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 30 | __attribute__ ((noinline)) void myusec_delay(int usecs) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 31 | { |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 32 | unsigned long i; |
| 33 | for (i = 0; i < usecs * micro; i++) { |
| 34 | /* Make sure the compiler doesn't optimize the loop away. */ |
| 35 | asm volatile ("" : : "rm" (i) ); |
| 36 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 39 | unsigned long measure_delay(int usecs) |
| 40 | { |
| 41 | unsigned long timeusec; |
| 42 | struct timeval start, end; |
| 43 | |
| 44 | gettimeofday(&start, 0); |
| 45 | myusec_delay(usecs); |
| 46 | gettimeofday(&end, 0); |
| 47 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 48 | (end.tv_usec - start.tv_usec); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 49 | /* Protect against time going forward too much. */ |
| 50 | if ((end.tv_sec > start.tv_sec) && |
| 51 | ((end.tv_sec - start.tv_sec) >= LONG_MAX / 1000000 - 1)) |
| 52 | timeusec = LONG_MAX; |
| 53 | /* Protect against time going backwards during leap seconds. */ |
| 54 | if ((end.tv_sec < start.tv_sec) || (timeusec > LONG_MAX)) |
| 55 | timeusec = 1; |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 56 | |
| 57 | return timeusec; |
| 58 | } |
| 59 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 60 | void myusec_calibrate_delay(void) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 61 | { |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 62 | unsigned long count = 1000; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 63 | unsigned long timeusec; |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 64 | int i, tries = 0; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 65 | |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 66 | printf("Calibrating delay loop... "); |
| 67 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 68 | recalibrate: |
Urja Rannikko | f640401 | 2010-04-09 00:02:38 +0000 | [diff] [blame] | 69 | count = 1000; |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 70 | while (1) { |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 71 | timeusec = measure_delay(count); |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 72 | if (timeusec > 1000000 / 4) |
| 73 | break; |
| 74 | if (count >= ULONG_MAX / 2) { |
| 75 | msg_pinfo("timer loop overflow, reduced precision. "); |
| 76 | break; |
| 77 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 78 | count *= 2; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 79 | } |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 80 | tries ++; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 81 | |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 82 | /* Avoid division by zero, but in that case the loop is shot anyway. */ |
| 83 | if (!timeusec) |
| 84 | timeusec = 1; |
| 85 | |
| 86 | /* Compute rounded up number of loops per microsecond. */ |
| 87 | micro = (count * micro) / timeusec + 1; |
| 88 | msg_pdbg("%luM loops per second, ", micro); |
| 89 | |
| 90 | /* Did we try to recalibrate less than 5 times? */ |
| 91 | if (tries < 5) { |
| 92 | /* Recheck our timing to make sure we weren't just hitting |
| 93 | * a scheduler delay or something similar. |
| 94 | */ |
| 95 | for (i = 0; i < 4; i++) { |
| 96 | if (measure_delay(100) < 90) { |
Urja Rannikko | f640401 | 2010-04-09 00:02:38 +0000 | [diff] [blame] | 97 | msg_pdbg("delay more than 10%% too short, " |
Carl-Daniel Hailfinger | 253101e | 2010-03-31 23:55:06 +0000 | [diff] [blame] | 98 | "recalculating... "); |
| 99 | goto recalibrate; |
| 100 | } |
| 101 | } |
| 102 | } else { |
| 103 | msg_perr("delay loop is unreliable, trying to continue "); |
| 104 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 105 | |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame] | 106 | /* We're interested in the actual precision. */ |
| 107 | timeusec = measure_delay(10); |
| 108 | msg_pdbg("10 myus = %ld us, ", timeusec); |
| 109 | timeusec = measure_delay(100); |
| 110 | msg_pdbg("100 myus = %ld us, ", timeusec); |
| 111 | timeusec = measure_delay(1000); |
| 112 | msg_pdbg("1000 myus = %ld us, ", timeusec); |
| 113 | timeusec = measure_delay(10000); |
| 114 | msg_pdbg("10000 myus = %ld us, ", timeusec); |
| 115 | |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 116 | printf("OK.\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 117 | } |
Carl-Daniel Hailfinger | 36cc1c8 | 2009-12-24 03:11:55 +0000 | [diff] [blame] | 118 | |
| 119 | void internal_delay(int usecs) |
| 120 | { |
| 121 | /* If the delay is >1 s, use usleep because timing does not need to |
| 122 | * be so precise. |
| 123 | */ |
| 124 | if (usecs > 1000000) { |
| 125 | usleep(usecs); |
| 126 | } else { |
| 127 | myusec_delay(usecs); |
| 128 | } |
| 129 | } |
| 130 | |