2004-02-24 00:16:43 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2004
|
|
|
|
* DAVE Srl
|
|
|
|
* http://www.dave-tech.it
|
|
|
|
* http://www.wawnet.biz
|
|
|
|
* mailto:info@wawnet.biz
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2004-02-24 00:16:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <asm/hardware.h>
|
|
|
|
|
|
|
|
/* we always count down the max. */
|
|
|
|
#define TIMER_LOAD_VAL 0xffff
|
|
|
|
|
|
|
|
/* macro to read the 16 bit timer */
|
|
|
|
#define READ_TIMER (TCNTO1 & 0xffff)
|
|
|
|
|
|
|
|
#ifdef CONFIG_USE_IRQ
|
|
|
|
#error CONFIG_USE_IRQ NOT supported
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static ulong timestamp;
|
|
|
|
static ulong lastdec;
|
|
|
|
|
2009-05-15 21:47:02 +00:00
|
|
|
int timer_init (void)
|
2004-02-24 00:16:43 +00:00
|
|
|
{
|
|
|
|
TCFG0 = 0x000000E9;
|
|
|
|
TCFG1 = 0x00000004;
|
|
|
|
TCON = 0x00000900;
|
|
|
|
TCNTB1 = TIMER_LOAD_VAL;
|
|
|
|
TCMPB1 = 0;
|
|
|
|
TCON = 0x00000B00;
|
|
|
|
TCON = 0x00000900;
|
|
|
|
|
|
|
|
|
|
|
|
lastdec = TCNTB1 = TIMER_LOAD_VAL;
|
|
|
|
timestamp = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* timer without interrupts
|
|
|
|
*/
|
|
|
|
ulong get_timer (ulong base)
|
|
|
|
{
|
|
|
|
return get_timer_masked () - base;
|
|
|
|
}
|
|
|
|
|
2009-11-24 13:09:21 +00:00
|
|
|
void __udelay (unsigned long usec)
|
2004-02-24 00:16:43 +00:00
|
|
|
{
|
|
|
|
ulong tmo;
|
|
|
|
|
|
|
|
tmo = usec / 1000;
|
2008-10-16 13:01:15 +00:00
|
|
|
tmo *= CONFIG_SYS_HZ;
|
2004-02-24 00:16:43 +00:00
|
|
|
tmo /= 8;
|
|
|
|
|
|
|
|
tmo += get_timer (0);
|
|
|
|
|
|
|
|
while (get_timer_masked () < tmo)
|
|
|
|
/*NOP*/;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulong get_timer_masked (void)
|
|
|
|
{
|
|
|
|
ulong now = READ_TIMER;
|
|
|
|
|
|
|
|
if (lastdec >= now) {
|
|
|
|
/* normal mode */
|
|
|
|
timestamp += lastdec - now;
|
|
|
|
} else {
|
|
|
|
/* we have an overflow ... */
|
|
|
|
timestamp += lastdec + TIMER_LOAD_VAL - now;
|
|
|
|
}
|
|
|
|
lastdec = now;
|
|
|
|
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void udelay_masked (unsigned long usec)
|
|
|
|
{
|
|
|
|
ulong tmo;
|
2005-04-04 12:08:28 +00:00
|
|
|
ulong endtime;
|
|
|
|
signed long diff;
|
2004-02-24 00:16:43 +00:00
|
|
|
|
2005-04-04 12:08:28 +00:00
|
|
|
if (usec >= 1000) {
|
|
|
|
tmo = usec / 1000;
|
2008-10-16 13:01:15 +00:00
|
|
|
tmo *= CONFIG_SYS_HZ;
|
2005-04-04 12:08:28 +00:00
|
|
|
tmo /= 8;
|
|
|
|
} else {
|
2008-10-16 13:01:15 +00:00
|
|
|
tmo = usec * CONFIG_SYS_HZ;
|
2005-04-04 12:08:28 +00:00
|
|
|
tmo /= (1000*8);
|
|
|
|
}
|
2004-02-24 00:16:43 +00:00
|
|
|
|
2005-04-04 12:08:28 +00:00
|
|
|
endtime = get_timer(0) + tmo;
|
2004-02-24 00:16:43 +00:00
|
|
|
|
2005-04-04 12:08:28 +00:00
|
|
|
do {
|
|
|
|
ulong now = get_timer_masked ();
|
|
|
|
diff = endtime - now;
|
|
|
|
} while (diff >= 0);
|
2004-02-24 00:16:43 +00:00
|
|
|
}
|