Matthias Weisser | 6052ac8 | 2010-08-09 13:31:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007-2008 |
Stelian Pop | c9e798d | 2011-11-01 00:00:39 +0100 | [diff] [blame] | 3 | * Stelian Pop <stelian@popies.net> |
Matthias Weisser | 6052ac8 | 2010-08-09 13:31:49 +0200 | [diff] [blame] | 4 | * Lead Tech Design <www.leadtechdesign.com> |
| 5 | * |
| 6 | * (C) Copyright 2010 |
| 7 | * Matthias Weisser, Graf-Syteco <weisserm@arcor.de> |
| 8 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
| 28 | #include <div64.h> |
| 29 | #include <common.h> |
| 30 | #include <asm/io.h> |
| 31 | #include <asm/arch/hardware.h> |
| 32 | |
| 33 | #define TIMER_LOAD_VAL 0xffffffff |
| 34 | #define TIMER_FREQ (CONFIG_MB86R0x_IOCLK / 256) |
| 35 | |
Heiko Schocher | c9ac3ba | 2011-01-20 22:56:39 +0000 | [diff] [blame] | 36 | DECLARE_GLOBAL_DATA_PTR; |
| 37 | |
Simon Glass | 66ee692 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 38 | #define timestamp gd->arch.tbl |
Simon Glass | 582601d | 2012-12-13 20:48:35 +0000 | [diff] [blame^] | 39 | #define lastdec gd->arch.lastinc |
Matthias Weisser | 6052ac8 | 2010-08-09 13:31:49 +0200 | [diff] [blame] | 40 | |
| 41 | static inline unsigned long long tick_to_time(unsigned long long tick) |
| 42 | { |
| 43 | tick *= CONFIG_SYS_HZ; |
| 44 | do_div(tick, TIMER_FREQ); |
| 45 | |
| 46 | return tick; |
| 47 | } |
| 48 | |
| 49 | static inline unsigned long long usec_to_tick(unsigned long long usec) |
| 50 | { |
| 51 | usec *= TIMER_FREQ; |
| 52 | do_div(usec, 1000000); |
| 53 | |
| 54 | return usec; |
| 55 | } |
| 56 | |
| 57 | /* nothing really to do with interrupts, just starts up a counter. */ |
| 58 | int timer_init(void) |
| 59 | { |
| 60 | struct mb86r0x_timer * timer = (struct mb86r0x_timer *) |
| 61 | MB86R0x_TIMER_BASE; |
| 62 | ulong ctrl = readl(&timer->control); |
| 63 | |
| 64 | writel(TIMER_LOAD_VAL, &timer->load); |
| 65 | |
| 66 | ctrl |= MB86R0x_TIMER_ENABLE | MB86R0x_TIMER_PRS_8S | |
| 67 | MB86R0x_TIMER_SIZE_32; |
| 68 | |
| 69 | writel(ctrl, &timer->control); |
| 70 | |
Graeme Russ | 17659d7 | 2011-07-15 02:21:14 +0000 | [diff] [blame] | 71 | /* capture current value time */ |
| 72 | lastdec = readl(&timer->value); |
| 73 | timestamp = 0; /* start "advancing" time stamp from 0 */ |
Matthias Weisser | 6052ac8 | 2010-08-09 13:31:49 +0200 | [diff] [blame] | 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * timer without interrupts |
| 80 | */ |
| 81 | unsigned long long get_ticks(void) |
| 82 | { |
| 83 | struct mb86r0x_timer * timer = (struct mb86r0x_timer *) |
| 84 | MB86R0x_TIMER_BASE; |
| 85 | ulong now = readl(&timer->value); |
| 86 | |
| 87 | if (now <= lastdec) { |
| 88 | /* normal mode (non roll) */ |
| 89 | /* move stamp forward with absolut diff ticks */ |
| 90 | timestamp += lastdec - now; |
| 91 | } else { |
| 92 | /* we have rollover of incrementer */ |
| 93 | timestamp += lastdec + TIMER_LOAD_VAL - now; |
| 94 | } |
| 95 | lastdec = now; |
| 96 | return timestamp; |
| 97 | } |
| 98 | |
Matthias Weisser | 6052ac8 | 2010-08-09 13:31:49 +0200 | [diff] [blame] | 99 | ulong get_timer_masked(void) |
| 100 | { |
| 101 | return tick_to_time(get_ticks()); |
| 102 | } |
| 103 | |
| 104 | void __udelay(unsigned long usec) |
| 105 | { |
| 106 | unsigned long long tmp; |
| 107 | ulong tmo; |
| 108 | |
| 109 | tmo = usec_to_tick(usec); |
| 110 | tmp = get_ticks(); /* get current timestamp */ |
| 111 | |
| 112 | while ((get_ticks() - tmp) < tmo) /* loop till event */ |
| 113 | /*NOP*/; |
| 114 | } |
| 115 | |
Matthias Weisser | 6052ac8 | 2010-08-09 13:31:49 +0200 | [diff] [blame] | 116 | ulong get_timer(ulong base) |
| 117 | { |
| 118 | return get_timer_masked() - base; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * This function is derived from PowerPC code (timebase clock frequency). |
| 123 | * On ARM it returns the number of timer ticks per second. |
| 124 | */ |
| 125 | ulong get_tbclk(void) |
| 126 | { |
| 127 | ulong tbclk; |
| 128 | |
| 129 | tbclk = TIMER_FREQ; |
| 130 | return tbclk; |
| 131 | } |