blob: 41603000e3f603c206fbd0928e9b34a304f59064 [file] [log] [blame]
Uwe Hermannca7f0e42007-09-09 20:02:45 +00001/*
2 * This file is part of the flashrom project.
3 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
Uwe Hermannca7f0e42007-09-09 20:02:45 +00005 *
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 Lhocbbf1252004-03-17 22:22:08 +000021#include <sys/time.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000022#include "flash.h"
Ollie Lhocbbf1252004-03-17 22:22:08 +000023
24// count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
25unsigned long micro = 1;
26
27void myusec_delay(int time)
28{
29 volatile unsigned long i;
Uwe Hermann0b7afe62007-04-01 19:44:21 +000030 for (i = 0; i < time * micro; i++) ;
Ollie Lhocbbf1252004-03-17 22:22:08 +000031}
32
Uwe Hermann7b2969b2009-04-15 10:52:49 +000033void myusec_calibrate_delay(void)
Ollie Lhocbbf1252004-03-17 22:22:08 +000034{
35 int count = 1000;
36 unsigned long timeusec;
37 struct timeval start, end;
38 int ok = 0;
Ollie Lhocbbf1252004-03-17 22:22:08 +000039
Stefan Reinauer70385642007-04-06 11:58:03 +000040 printf("Calibrating delay loop... ");
41
Ollie Lhocbbf1252004-03-17 22:22:08 +000042 while (!ok) {
Ollie Lhocbbf1252004-03-17 22:22:08 +000043 gettimeofday(&start, 0);
44 myusec_delay(count);
45 gettimeofday(&end, 0);
Ollie Lho761bf1b2004-03-20 16:46:10 +000046 timeusec = 1000000 * (end.tv_sec - start.tv_sec) +
47 (end.tv_usec - start.tv_usec);
Ollie Lhocbbf1252004-03-17 22:22:08 +000048 count *= 2;
Ollie Lho761bf1b2004-03-20 16:46:10 +000049 if (timeusec < 1000000 / 4)
Ollie Lhocbbf1252004-03-17 22:22:08 +000050 continue;
51 ok = 1;
52 }
53
54 // compute one microsecond. That will be count / time
55 micro = count / timeusec;
56
Peter Stuge80d667b2008-09-07 03:14:27 +000057 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 Hermann394131e2008-10-18 21:14:13 +000063 (unsigned long)micro, timeusec);
Uwe Hermanna502dce2007-10-17 23:55:15 +000064 printf("OK.\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +000065}