Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 1 | /* |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 2 | * (C) Copyright 2007 |
| 3 | * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org> |
| 4 | * |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 5 | * (C) Copyright 2003 |
| 6 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 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/processor.h> |
| 29 | |
| 30 | #define TMU_MAX_COUNTER (~0UL) |
| 31 | |
| 32 | static void tmu_timer_start (unsigned int timer) |
| 33 | { |
| 34 | if (timer > 2) |
| 35 | return; |
| 36 | |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 37 | *((volatile unsigned char *) TSTR) |= (1 << timer); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 38 | } |
| 39 | |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 40 | static void tmu_timer_stop (unsigned int timer) |
| 41 | { |
| 42 | u8 val = *((volatile u8 *)TSTR); |
| 43 | if (timer > 2) |
| 44 | return; |
| 45 | *((volatile unsigned char *)TSTR) = val &~(1 << timer); |
| 46 | } |
| 47 | |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 48 | int timer_init (void) |
| 49 | { |
| 50 | /* Divide clock by 4 */ |
| 51 | *(volatile u16 *)TCR0 = 0; |
| 52 | |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 53 | tmu_timer_stop(0); |
| 54 | tmu_timer_start(0); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | In theory we should return a true 64bit value (ie something that doesn't |
| 60 | overflow). However, we don't. Therefore if TMU runs at fastest rate of |
| 61 | 6.75 MHz this value will wrap after u-boot has been running for approx |
| 62 | 10 minutes. |
| 63 | */ |
| 64 | unsigned long long get_ticks (void) |
| 65 | { |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 66 | return (0 - *((volatile u32 *) TCNT0)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | unsigned long get_timer (unsigned long base) |
| 70 | { |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 71 | return ((0 - *((volatile u32 *) TCNT0)) - base); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void set_timer (unsigned long t) |
| 75 | { |
| 76 | *((volatile unsigned int *) TCNT0) = (0 - t); |
| 77 | } |
| 78 | |
| 79 | void reset_timer (void) |
| 80 | { |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 81 | tmu_timer_stop(0); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 82 | set_timer (0); |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 83 | tmu_timer_start(0); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void udelay (unsigned long usec) |
| 87 | { |
| 88 | unsigned int start = get_timer (0); |
Nobuhiro Iwamatsu | 547b67f | 2007-09-23 02:12:30 +0900 | [diff] [blame] | 89 | unsigned int end = start + (usec * ((CFG_HZ + 500000) / 1000000)); |
Nobuhiro Iwamatsu | 970dc33 | 2007-05-13 20:58:00 +0900 | [diff] [blame] | 90 | |
| 91 | while (get_timer (0) < end) |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | unsigned long get_tbclk (void) |
| 96 | { |
| 97 | return CFG_HZ; |
| 98 | } |