blob: e4500ae4b07effc674df14783d9061ca5c722ea5 [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>
22#include <stdio.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000023#include "flash.h"
Ollie Lhocbbf1252004-03-17 22:22:08 +000024
25// count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
26unsigned long micro = 1;
27
28void myusec_delay(int time)
29{
30 volatile unsigned long i;
Uwe Hermann0b7afe62007-04-01 19:44:21 +000031 for (i = 0; i < time * micro; i++) ;
Ollie Lhocbbf1252004-03-17 22:22:08 +000032}
33
34void myusec_calibrate_delay()
35{
36 int count = 1000;
37 unsigned long timeusec;
38 struct timeval start, end;
39 int ok = 0;
Ollie Lhocbbf1252004-03-17 22:22:08 +000040
Stefan Reinauer70385642007-04-06 11:58:03 +000041 printf("Calibrating delay loop... ");
42
Ollie Lhocbbf1252004-03-17 22:22:08 +000043 while (!ok) {
Ollie Lhocbbf1252004-03-17 22:22:08 +000044 gettimeofday(&start, 0);
45 myusec_delay(count);
46 gettimeofday(&end, 0);
Ollie Lho761bf1b2004-03-20 16:46:10 +000047 timeusec = 1000000 * (end.tv_sec - start.tv_sec) +
48 (end.tv_usec - start.tv_usec);
Ollie Lhocbbf1252004-03-17 22:22:08 +000049 count *= 2;
Ollie Lho761bf1b2004-03-20 16:46:10 +000050 if (timeusec < 1000000 / 4)
Ollie Lhocbbf1252004-03-17 22:22:08 +000051 continue;
52 ok = 1;
53 }
54
55 // compute one microsecond. That will be count / time
56 micro = count / timeusec;
57
Peter Stuge80d667b2008-09-07 03:14:27 +000058 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 Hermanna502dce2007-10-17 23:55:15 +000065 printf("OK.\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +000066}