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 |
Uwe Hermann | ca7f0e4 | 2007-09-09 20:02:45 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 21 | #include <sys/time.h> |
Uwe Hermann | 0846f89 | 2007-08-23 13:34:59 +0000 | [diff] [blame] | 22 | #include "flash.h" |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 23 | |
| 24 | // count to a billion. Time it. If it's < 1 sec, count to 10B, etc. |
| 25 | unsigned long micro = 1; |
| 26 | |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 27 | void myusec_delay(int usecs) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 28 | { |
| 29 | volatile unsigned long i; |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 30 | for (i = 0; i < usecs * micro; i++) ; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 33 | void myusec_calibrate_delay(void) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 34 | { |
| 35 | int count = 1000; |
| 36 | unsigned long timeusec; |
| 37 | struct timeval start, end; |
| 38 | int ok = 0; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 39 | |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 40 | printf("Calibrating delay loop... "); |
| 41 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 42 | while (!ok) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 43 | gettimeofday(&start, 0); |
| 44 | myusec_delay(count); |
| 45 | gettimeofday(&end, 0); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 46 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 47 | (end.tv_usec - start.tv_usec); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 48 | count *= 2; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 49 | if (timeusec < 1000000 / 4) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 50 | continue; |
| 51 | ok = 1; |
| 52 | } |
| 53 | |
| 54 | // compute one microsecond. That will be count / time |
| 55 | micro = count / timeusec; |
| 56 | |
Peter Stuge | 80d667b | 2008-09-07 03:14:27 +0000 | [diff] [blame] | 57 | gettimeofday(&start, 0); |
| 58 | myusec_delay(100); |
| 59 | gettimeofday(&end, 0); |
| 60 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 61 | (end.tv_usec - start.tv_usec); |
| 62 | printf_debug("%ldM loops per second, 100 myus = %ld us. ", |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 63 | (unsigned long)micro, timeusec); |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 64 | printf("OK.\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 65 | } |