blob: 6e0dd0d2bd41b78945c216eaaeb62d82ec933782 [file] [log] [blame]
Cyril Chemparathy692a7af2010-06-07 14:13:32 -04001/*
2 * TNETV107X: Timer implementation
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Cyril Chemparathy692a7af2010-06-07 14:13:32 -04005 */
6
7#include <common.h>
8#include <asm/io.h>
9#include <asm/arch/clock.h>
10
11struct 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
28static ulong timestamp;
29static ulong lastinc;
30
31int 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, &regs->tcr);
39 __raw_writel(0x0, &regs->tgcr);
40 __raw_writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &regs->tgcr);
41 __raw_writel(0x0, &regs->tim34);
42 __raw_writel(TIMER_LOAD_VAL, &regs->prd34);
43 __raw_writel(2 << 22, &regs->tcr);
44
45 return 0;
46}
47
Cyril Chemparathy692a7af2010-06-07 14:13:32 -040048static ulong get_timer_raw(void)
49{
50 ulong now = __raw_readl(&regs->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
62ulong get_timer(ulong base)
63{
64 return (get_timer_raw() / (TIMER_LOAD_VAL / TIM_CLK_DIV)) - base;
65}
66
Cyril Chemparathy692a7af2010-06-07 14:13:32 -040067unsigned long long get_ticks(void)
68{
69 return get_timer(0);
70}
71
72void __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
90ulong get_tbclk(void)
91{
92 return CONFIG_SYS_HZ;
93}