Cyril Chemparathy | 692a7af | 2010-06-07 14:13:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * TNETV107X: Timer implementation |
| 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Cyril Chemparathy | 692a7af | 2010-06-07 14:13:32 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <asm/io.h> |
| 9 | #include <asm/arch/clock.h> |
| 10 | |
| 11 | struct timer_regs { |
| 12 | u_int32_t pid12; |
| 13 | u_int32_t pad[3]; |
| 14 | u_int32_t tim12; |
| 15 | u_int32_t tim34; |
| 16 | u_int32_t prd12; |
| 17 | u_int32_t prd34; |
| 18 | u_int32_t tcr; |
| 19 | u_int32_t tgcr; |
| 20 | u_int32_t wdtcr; |
| 21 | }; |
| 22 | |
| 23 | #define regs ((struct timer_regs *)CONFIG_SYS_TIMERBASE) |
| 24 | |
| 25 | #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ) |
| 26 | #define TIM_CLK_DIV 16 |
| 27 | |
| 28 | static ulong timestamp; |
| 29 | static ulong lastinc; |
| 30 | |
| 31 | int timer_init(void) |
| 32 | { |
| 33 | clk_enable(TNETV107X_LPSC_TIMER0); |
| 34 | |
| 35 | lastinc = timestamp = 0; |
| 36 | |
| 37 | /* We are using timer34 in unchained 32-bit mode, full speed */ |
| 38 | __raw_writel(0x0, ®s->tcr); |
| 39 | __raw_writel(0x0, ®s->tgcr); |
| 40 | __raw_writel(0x06 | ((TIM_CLK_DIV - 1) << 8), ®s->tgcr); |
| 41 | __raw_writel(0x0, ®s->tim34); |
| 42 | __raw_writel(TIMER_LOAD_VAL, ®s->prd34); |
| 43 | __raw_writel(2 << 22, ®s->tcr); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Cyril Chemparathy | 692a7af | 2010-06-07 14:13:32 -0400 | [diff] [blame] | 48 | static ulong get_timer_raw(void) |
| 49 | { |
| 50 | ulong now = __raw_readl(®s->tim34); |
| 51 | |
| 52 | if (now >= lastinc) |
| 53 | timestamp += now - lastinc; |
| 54 | else |
| 55 | timestamp += now + TIMER_LOAD_VAL - lastinc; |
| 56 | |
| 57 | lastinc = now; |
| 58 | |
| 59 | return timestamp; |
| 60 | } |
| 61 | |
| 62 | ulong get_timer(ulong base) |
| 63 | { |
| 64 | return (get_timer_raw() / (TIMER_LOAD_VAL / TIM_CLK_DIV)) - base; |
| 65 | } |
| 66 | |
Cyril Chemparathy | 692a7af | 2010-06-07 14:13:32 -0400 | [diff] [blame] | 67 | unsigned long long get_ticks(void) |
| 68 | { |
| 69 | return get_timer(0); |
| 70 | } |
| 71 | |
| 72 | void __udelay(unsigned long usec) |
| 73 | { |
| 74 | ulong tmo; |
| 75 | ulong endtime; |
| 76 | signed long diff; |
| 77 | |
| 78 | tmo = CONFIG_SYS_HZ_CLOCK / 1000; |
| 79 | tmo *= usec; |
| 80 | tmo /= (1000 * TIM_CLK_DIV); |
| 81 | |
| 82 | endtime = get_timer_raw() + tmo; |
| 83 | |
| 84 | do { |
| 85 | ulong now = get_timer_raw(); |
| 86 | diff = endtime - now; |
| 87 | } while (diff >= 0); |
| 88 | } |
| 89 | |
| 90 | ulong get_tbclk(void) |
| 91 | { |
| 92 | return CONFIG_SYS_HZ; |
| 93 | } |