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> |
| 22 | #include <stdio.h> |
Uwe Hermann | 0846f89 | 2007-08-23 13:34:59 +0000 | [diff] [blame] | 23 | #include "flash.h" |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 24 | |
| 25 | // count to a billion. Time it. If it's < 1 sec, count to 10B, etc. |
| 26 | unsigned long micro = 1; |
| 27 | |
| 28 | void myusec_delay(int time) |
| 29 | { |
| 30 | volatile unsigned long i; |
Uwe Hermann | 0b7afe6 | 2007-04-01 19:44:21 +0000 | [diff] [blame] | 31 | for (i = 0; i < time * micro; i++) ; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void myusec_calibrate_delay() |
| 35 | { |
| 36 | int count = 1000; |
| 37 | unsigned long timeusec; |
| 38 | struct timeval start, end; |
| 39 | int ok = 0; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 40 | |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 41 | printf("Calibrating delay loop... "); |
| 42 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 43 | while (!ok) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 44 | gettimeofday(&start, 0); |
| 45 | myusec_delay(count); |
| 46 | gettimeofday(&end, 0); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 47 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 48 | (end.tv_usec - start.tv_usec); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 49 | count *= 2; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 50 | if (timeusec < 1000000 / 4) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 51 | continue; |
| 52 | ok = 1; |
| 53 | } |
| 54 | |
| 55 | // compute one microsecond. That will be count / time |
| 56 | micro = count / timeusec; |
| 57 | |
Peter Stuge | 80d667b | 2008-09-07 03:14:27 +0000 | [diff] [blame] | 58 | gettimeofday(&start, 0); |
| 59 | myusec_delay(100); |
| 60 | gettimeofday(&end, 0); |
| 61 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 62 | (end.tv_usec - start.tv_usec); |
| 63 | printf_debug("%ldM loops per second, 100 myus = %ld us. ", |
| 64 | (unsigned long)micro, timeusec); |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 65 | printf("OK.\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 66 | } |