Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 1 | /* |
Alessandro Rubini | 095a460 | 2009-06-29 10:52:37 +0200 | [diff] [blame] | 2 | * (C) Copyright 2009 Alessandro Rubini |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <asm/io.h> |
Alessandro Rubini | 095a460 | 2009-06-29 10:52:37 +0200 | [diff] [blame] | 9 | #include <asm/arch/mtu.h> |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 10 | |
Alessandro Rubini | 095a460 | 2009-06-29 10:52:37 +0200 | [diff] [blame] | 11 | /* |
| 12 | * The timer is a decrementer, we'll left it free running at 2.4MHz. |
| 13 | * We have 2.4 ticks per microsecond and an overflow in almost 30min |
| 14 | */ |
| 15 | #define TIMER_CLOCK (24 * 100 * 1000) |
| 16 | #define COUNT_TO_USEC(x) ((x) * 5 / 12) /* overflows at 6min */ |
| 17 | #define USEC_TO_COUNT(x) ((x) * 12 / 5) /* overflows at 6min */ |
| 18 | #define TICKS_PER_HZ (TIMER_CLOCK / CONFIG_SYS_HZ) |
| 19 | #define TICKS_TO_HZ(x) ((x) / TICKS_PER_HZ) |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 20 | |
Alessandro Rubini | 4b894a9 | 2009-11-25 23:41:51 +0100 | [diff] [blame] | 21 | /* macro to read the decrementing 32 bit timer as an increasing count */ |
| 22 | #define READ_TIMER() (0 - readl(CONFIG_SYS_TIMERBASE + MTU_VAL(0))) |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 23 | |
Alessandro Rubini | 095a460 | 2009-06-29 10:52:37 +0200 | [diff] [blame] | 24 | /* Configure a free-running, auto-wrap counter with no prescaler */ |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 25 | int timer_init(void) |
| 26 | { |
Graeme Russ | 4769be2 | 2011-07-15 02:19:44 +0000 | [diff] [blame] | 27 | ulong val; |
| 28 | |
Alessandro Rubini | 095a460 | 2009-06-29 10:52:37 +0200 | [diff] [blame] | 29 | writel(MTU_CRn_ENA | MTU_CRn_PRESCALE_1 | MTU_CRn_32BITS, |
| 30 | CONFIG_SYS_TIMERBASE + MTU_CR(0)); |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 31 | |
Graeme Russ | 4769be2 | 2011-07-15 02:19:44 +0000 | [diff] [blame] | 32 | /* Reset the timer */ |
Alessandro Rubini | 4b894a9 | 2009-11-25 23:41:51 +0100 | [diff] [blame] | 33 | writel(0, CONFIG_SYS_TIMERBASE + MTU_LR(0)); |
| 34 | /* |
| 35 | * The load-register isn't really immediate: it changes on clock |
| 36 | * edges, so we must wait for our newly-written value to appear. |
| 37 | * Since we might miss reading 0, wait for any change in value. |
| 38 | */ |
| 39 | val = READ_TIMER(); |
| 40 | while (READ_TIMER() == val) |
| 41 | ; |
Graeme Russ | 4769be2 | 2011-07-15 02:19:44 +0000 | [diff] [blame] | 42 | |
| 43 | return 0; |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Alessandro Rubini | 095a460 | 2009-06-29 10:52:37 +0200 | [diff] [blame] | 46 | /* Return how many HZ passed since "base" */ |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 47 | ulong get_timer(ulong base) |
| 48 | { |
Alessandro Rubini | 095a460 | 2009-06-29 10:52:37 +0200 | [diff] [blame] | 49 | return TICKS_TO_HZ(READ_TIMER()) - base; |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Alessandro Rubini | 095a460 | 2009-06-29 10:52:37 +0200 | [diff] [blame] | 52 | /* Delay x useconds */ |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 53 | void __udelay(unsigned long usec) |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 54 | { |
Alessandro Rubini | 095a460 | 2009-06-29 10:52:37 +0200 | [diff] [blame] | 55 | ulong ini, end; |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 56 | |
Alessandro Rubini | 095a460 | 2009-06-29 10:52:37 +0200 | [diff] [blame] | 57 | ini = READ_TIMER(); |
| 58 | end = ini + USEC_TO_COUNT(usec); |
| 59 | while ((signed)(end - READ_TIMER()) > 0) |
| 60 | ; |
Alessandro Rubini | d5254f1 | 2009-01-24 18:10:37 +0100 | [diff] [blame] | 61 | } |
Anatolij Gustschin | b39643b | 2012-03-28 02:56:30 +0000 | [diff] [blame] | 62 | |
| 63 | unsigned long long get_ticks(void) |
| 64 | { |
| 65 | return get_timer(0); |
| 66 | } |
| 67 | |
| 68 | ulong get_tbclk(void) |
| 69 | { |
| 70 | return CONFIG_SYS_HZ; |
| 71 | } |