wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 1 | /* |
| 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 Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <asm/hardware.h> |
| 13 | |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 14 | /* 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 |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 22 | #endif |
| 23 | |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 24 | static ulong timestamp; |
| 25 | static ulong lastdec; |
| 26 | |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 27 | int timer_init (void) |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 28 | { |
| 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 | */ |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 46 | ulong get_timer (ulong base) |
| 47 | { |
| 48 | return get_timer_masked () - base; |
| 49 | } |
| 50 | |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 51 | void __udelay (unsigned long usec) |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 52 | { |
| 53 | ulong tmo; |
| 54 | |
| 55 | tmo = usec / 1000; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 56 | tmo *= CONFIG_SYS_HZ; |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 57 | tmo /= 8; |
| 58 | |
| 59 | tmo += get_timer (0); |
| 60 | |
| 61 | while (get_timer_masked () < tmo) |
| 62 | /*NOP*/; |
| 63 | } |
| 64 | |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 65 | ulong 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 | |
| 81 | void udelay_masked (unsigned long usec) |
| 82 | { |
| 83 | ulong tmo; |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 84 | ulong endtime; |
| 85 | signed long diff; |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 86 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 87 | if (usec >= 1000) { |
| 88 | tmo = usec / 1000; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 89 | tmo *= CONFIG_SYS_HZ; |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 90 | tmo /= 8; |
| 91 | } else { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 92 | tmo = usec * CONFIG_SYS_HZ; |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 93 | tmo /= (1000*8); |
| 94 | } |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 95 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 96 | endtime = get_timer(0) + tmo; |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 97 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 98 | do { |
| 99 | ulong now = get_timer_masked (); |
| 100 | diff = endtime - now; |
| 101 | } while (diff >= 0); |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 102 | } |