blob: ac580177528c3c4ace90e26b01e7f55e98f47c88 [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
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +00005 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
Uwe Hermannca7f0e42007-09-09 20:02:45 +00006 *
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 Lhocbbf1252004-03-17 22:22:08 +000022#include <sys/time.h>
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +000023#include <stdlib.h>
24#include <limits.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000025#include "flash.h"
Ollie Lhocbbf1252004-03-17 22:22:08 +000026
27// count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
28unsigned long micro = 1;
29
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000030void myusec_delay(int usecs)
Ollie Lhocbbf1252004-03-17 22:22:08 +000031{
32 volatile unsigned long i;
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000033 for (i = 0; i < usecs * micro; i++) ;
Ollie Lhocbbf1252004-03-17 22:22:08 +000034}
35
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +000036unsigned 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 Hermann7b2969b2009-04-15 10:52:49 +000050void myusec_calibrate_delay(void)
Ollie Lhocbbf1252004-03-17 22:22:08 +000051{
52 int count = 1000;
53 unsigned long timeusec;
Ollie Lhocbbf1252004-03-17 22:22:08 +000054 int ok = 0;
Ollie Lhocbbf1252004-03-17 22:22:08 +000055
Stefan Reinauer70385642007-04-06 11:58:03 +000056 printf("Calibrating delay loop... ");
57
Ollie Lhocbbf1252004-03-17 22:22:08 +000058 while (!ok) {
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +000059 timeusec = measure_delay(count);
Ollie Lhocbbf1252004-03-17 22:22:08 +000060 count *= 2;
Ollie Lho761bf1b2004-03-20 16:46:10 +000061 if (timeusec < 1000000 / 4)
Ollie Lhocbbf1252004-03-17 22:22:08 +000062 continue;
63 ok = 1;
64 }
65
66 // compute one microsecond. That will be count / time
67 micro = count / timeusec;
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +000068 msg_pdbg("%ldM loops per second, ", micro);
Ollie Lhocbbf1252004-03-17 22:22:08 +000069
Carl-Daniel Hailfingerb8114612010-03-27 16:16:01 +000070 /* 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 Hermanna502dce2007-10-17 23:55:15 +000080 printf("OK.\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +000081}
Carl-Daniel Hailfinger36cc1c82009-12-24 03:11:55 +000082
83void 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