Yoshihiro Shimoda | ad1a3a9 | 2007-12-03 22:58:45 +0900 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007 |
| 3 | * Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com> |
| 4 | * |
| 5 | * (C) Copyright 2007 |
| 6 | * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org> |
| 7 | * |
| 8 | * (C) Copyright 2003 |
| 9 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 10 | * |
| 11 | * See file CREDITS for list of people who contributed to this |
| 12 | * project. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of |
| 17 | * the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 27 | * MA 02111-1307 USA |
| 28 | */ |
| 29 | |
| 30 | #include <common.h> |
| 31 | #include <asm/processor.h> |
| 32 | #include <asm/io.h> |
| 33 | |
| 34 | #define TMU_MAX_COUNTER (~0UL) |
| 35 | |
| 36 | static void tmu_timer_start(unsigned int timer) |
| 37 | { |
| 38 | if (timer > 2) |
| 39 | return; |
| 40 | |
| 41 | outb(inb(TSTR) | (1 << timer), TSTR); |
| 42 | } |
| 43 | |
| 44 | static void tmu_timer_stop(unsigned int timer) |
| 45 | { |
| 46 | u8 val = inb(TSTR); |
| 47 | |
| 48 | if (timer > 2) |
| 49 | return; |
| 50 | outb(val & ~(1 << timer), TSTR); |
| 51 | } |
| 52 | |
| 53 | int timer_init(void) |
| 54 | { |
| 55 | /* Divide clock by 4 */ |
| 56 | outw(0, TCR0); |
| 57 | |
| 58 | tmu_timer_stop(0); |
| 59 | tmu_timer_start(0); |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | In theory we should return a true 64bit value (ie something that doesn't |
| 65 | overflow). However, we don't. Therefore if TMU runs at fastest rate of |
| 66 | 6.75 MHz this value will wrap after u-boot has been running for approx |
| 67 | 10 minutes. |
| 68 | */ |
| 69 | unsigned long long get_ticks(void) |
| 70 | { |
| 71 | return (0 - inl(TCNT0)); |
| 72 | } |
| 73 | |
| 74 | unsigned long get_timer(unsigned long base) |
| 75 | { |
| 76 | return ((0 - inl(TCNT0)) - base); |
| 77 | } |
| 78 | |
| 79 | void set_timer(unsigned long t) |
| 80 | { |
| 81 | outl(0 - t, TCNT0); |
| 82 | } |
| 83 | |
| 84 | void reset_timer(void) |
| 85 | { |
| 86 | tmu_timer_stop(0); |
| 87 | set_timer(0); |
| 88 | tmu_timer_start(0); |
| 89 | } |
| 90 | |
| 91 | void udelay(unsigned long usec) |
| 92 | { |
| 93 | unsigned int start = get_timer(0); |
| 94 | unsigned int end = start + (usec * ((CFG_HZ + 500000) / 1000000)); |
| 95 | |
| 96 | while (get_timer(0) < end) |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | unsigned long get_tbclk(void) |
| 101 | { |
| 102 | return CFG_HZ; |
| 103 | } |