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 | |
| 27 | // count to a billion. Time it. If it's < 1 sec, count to 10B, etc. |
| 28 | unsigned long micro = 1; |
| 29 | |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 30 | void myusec_delay(int usecs) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 31 | { |
| 32 | volatile unsigned long i; |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 33 | for (i = 0; i < usecs * micro; i++) ; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame^] | 36 | unsigned long measure_delay(int usecs) |
| 37 | { |
| 38 | unsigned long timeusec; |
| 39 | struct timeval start, end; |
| 40 | |
| 41 | gettimeofday(&start, 0); |
| 42 | myusec_delay(usecs); |
| 43 | gettimeofday(&end, 0); |
| 44 | timeusec = 1000000 * (end.tv_sec - start.tv_sec) + |
| 45 | (end.tv_usec - start.tv_usec); |
| 46 | |
| 47 | return timeusec; |
| 48 | } |
| 49 | |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 50 | void myusec_calibrate_delay(void) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 51 | { |
| 52 | int count = 1000; |
| 53 | unsigned long timeusec; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 54 | int ok = 0; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 55 | |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 56 | printf("Calibrating delay loop... "); |
| 57 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 58 | while (!ok) { |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame^] | 59 | timeusec = measure_delay(count); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 60 | count *= 2; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 61 | if (timeusec < 1000000 / 4) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 62 | continue; |
| 63 | ok = 1; |
| 64 | } |
| 65 | |
| 66 | // compute one microsecond. That will be count / time |
| 67 | micro = count / timeusec; |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame^] | 68 | msg_pdbg("%ldM loops per second, ", micro); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 69 | |
Carl-Daniel Hailfinger | b811461 | 2010-03-27 16:16:01 +0000 | [diff] [blame^] | 70 | /* We're interested in the actual precision. */ |
| 71 | timeusec = measure_delay(10); |
| 72 | msg_pdbg("10 myus = %ld us, ", timeusec); |
| 73 | timeusec = measure_delay(100); |
| 74 | msg_pdbg("100 myus = %ld us, ", timeusec); |
| 75 | timeusec = measure_delay(1000); |
| 76 | msg_pdbg("1000 myus = %ld us, ", timeusec); |
| 77 | timeusec = measure_delay(10000); |
| 78 | msg_pdbg("10000 myus = %ld us, ", timeusec); |
| 79 | |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 80 | printf("OK.\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 81 | } |
Carl-Daniel Hailfinger | 36cc1c8 | 2009-12-24 03:11:55 +0000 | [diff] [blame] | 82 | |
| 83 | void internal_delay(int usecs) |
| 84 | { |
| 85 | /* If the delay is >1 s, use usleep because timing does not need to |
| 86 | * be so precise. |
| 87 | */ |
| 88 | if (usecs > 1000000) { |
| 89 | usleep(usecs); |
| 90 | } else { |
| 91 | myusec_delay(usecs); |
| 92 | } |
| 93 | } |
| 94 | |