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 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <asm/hardware.h> |
| 29 | |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 30 | /* we always count down the max. */ |
| 31 | #define TIMER_LOAD_VAL 0xffff |
| 32 | |
| 33 | /* macro to read the 16 bit timer */ |
| 34 | #define READ_TIMER (TCNTO1 & 0xffff) |
| 35 | |
| 36 | #ifdef CONFIG_USE_IRQ |
| 37 | #error CONFIG_USE_IRQ NOT supported |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 40 | static ulong timestamp; |
| 41 | static ulong lastdec; |
| 42 | |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 43 | int timer_init (void) |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 44 | { |
| 45 | TCFG0 = 0x000000E9; |
| 46 | TCFG1 = 0x00000004; |
| 47 | TCON = 0x00000900; |
| 48 | TCNTB1 = TIMER_LOAD_VAL; |
| 49 | TCMPB1 = 0; |
| 50 | TCON = 0x00000B00; |
| 51 | TCON = 0x00000900; |
| 52 | |
| 53 | |
| 54 | lastdec = TCNTB1 = TIMER_LOAD_VAL; |
| 55 | timestamp = 0; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * timer without interrupts |
| 61 | */ |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 62 | ulong get_timer (ulong base) |
| 63 | { |
| 64 | return get_timer_masked () - base; |
| 65 | } |
| 66 | |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 67 | void __udelay (unsigned long usec) |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 68 | { |
| 69 | ulong tmo; |
| 70 | |
| 71 | tmo = usec / 1000; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 72 | tmo *= CONFIG_SYS_HZ; |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 73 | tmo /= 8; |
| 74 | |
| 75 | tmo += get_timer (0); |
| 76 | |
| 77 | while (get_timer_masked () < tmo) |
| 78 | /*NOP*/; |
| 79 | } |
| 80 | |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 81 | ulong get_timer_masked (void) |
| 82 | { |
| 83 | ulong now = READ_TIMER; |
| 84 | |
| 85 | if (lastdec >= now) { |
| 86 | /* normal mode */ |
| 87 | timestamp += lastdec - now; |
| 88 | } else { |
| 89 | /* we have an overflow ... */ |
| 90 | timestamp += lastdec + TIMER_LOAD_VAL - now; |
| 91 | } |
| 92 | lastdec = now; |
| 93 | |
| 94 | return timestamp; |
| 95 | } |
| 96 | |
| 97 | void udelay_masked (unsigned long usec) |
| 98 | { |
| 99 | ulong tmo; |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 100 | ulong endtime; |
| 101 | signed long diff; |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 102 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 103 | if (usec >= 1000) { |
| 104 | tmo = usec / 1000; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 105 | tmo *= CONFIG_SYS_HZ; |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 106 | tmo /= 8; |
| 107 | } else { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 108 | tmo = usec * CONFIG_SYS_HZ; |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 109 | tmo /= (1000*8); |
| 110 | } |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 111 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 112 | endtime = get_timer(0) + tmo; |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 113 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 114 | do { |
| 115 | ulong now = get_timer_masked (); |
| 116 | diff = endtime - now; |
| 117 | } while (diff >= 0); |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 118 | } |