blob: f25af7a39016e9be6eeb71cb509dfba0fec3b80b [file] [log] [blame]
wdenk074cff02004-02-24 00:16:43 +00001/*
2 * (C) Copyright 2004
3 * DAVE Srl
4 * http://www.dave-tech.it
5 * http://www.wawnet.biz
6 * mailto:info@wawnet.biz
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk074cff02004-02-24 00:16:43 +00009 */
10
11#include <common.h>
12#include <asm/hardware.h>
13
wdenk074cff02004-02-24 00:16:43 +000014/* we always count down the max. */
15#define TIMER_LOAD_VAL 0xffff
16
17/* macro to read the 16 bit timer */
18#define READ_TIMER (TCNTO1 & 0xffff)
19
20#ifdef CONFIG_USE_IRQ
21#error CONFIG_USE_IRQ NOT supported
wdenk074cff02004-02-24 00:16:43 +000022#endif
23
wdenk074cff02004-02-24 00:16:43 +000024static ulong timestamp;
25static ulong lastdec;
26
Jean-Christophe PLAGNIOL-VILLARDb54384e2009-05-15 23:47:02 +020027int timer_init (void)
wdenk074cff02004-02-24 00:16:43 +000028{
29 TCFG0 = 0x000000E9;
30 TCFG1 = 0x00000004;
31 TCON = 0x00000900;
32 TCNTB1 = TIMER_LOAD_VAL;
33 TCMPB1 = 0;
34 TCON = 0x00000B00;
35 TCON = 0x00000900;
36
37
38 lastdec = TCNTB1 = TIMER_LOAD_VAL;
39 timestamp = 0;
40 return 0;
41}
42
43/*
44 * timer without interrupts
45 */
wdenk074cff02004-02-24 00:16:43 +000046ulong get_timer (ulong base)
47{
48 return get_timer_masked () - base;
49}
50
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010051void __udelay (unsigned long usec)
wdenk074cff02004-02-24 00:16:43 +000052{
53 ulong tmo;
54
55 tmo = usec / 1000;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020056 tmo *= CONFIG_SYS_HZ;
wdenk074cff02004-02-24 00:16:43 +000057 tmo /= 8;
58
59 tmo += get_timer (0);
60
61 while (get_timer_masked () < tmo)
62 /*NOP*/;
63}
64
wdenk074cff02004-02-24 00:16:43 +000065ulong get_timer_masked (void)
66{
67 ulong now = READ_TIMER;
68
69 if (lastdec >= now) {
70 /* normal mode */
71 timestamp += lastdec - now;
72 } else {
73 /* we have an overflow ... */
74 timestamp += lastdec + TIMER_LOAD_VAL - now;
75 }
76 lastdec = now;
77
78 return timestamp;
79}
80
81void udelay_masked (unsigned long usec)
82{
83 ulong tmo;
wdenk101e8df2005-04-04 12:08:28 +000084 ulong endtime;
85 signed long diff;
wdenk074cff02004-02-24 00:16:43 +000086
wdenk101e8df2005-04-04 12:08:28 +000087 if (usec >= 1000) {
88 tmo = usec / 1000;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020089 tmo *= CONFIG_SYS_HZ;
wdenk101e8df2005-04-04 12:08:28 +000090 tmo /= 8;
91 } else {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020092 tmo = usec * CONFIG_SYS_HZ;
wdenk101e8df2005-04-04 12:08:28 +000093 tmo /= (1000*8);
94 }
wdenk074cff02004-02-24 00:16:43 +000095
wdenk101e8df2005-04-04 12:08:28 +000096 endtime = get_timer(0) + tmo;
wdenk074cff02004-02-24 00:16:43 +000097
wdenk101e8df2005-04-04 12:08:28 +000098 do {
99 ulong now = get_timer_masked ();
100 diff = endtime - now;
101 } while (diff >= 0);
wdenk074cff02004-02-24 00:16:43 +0000102}