1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include <common.h>
41#include <asm/io.h>
42#include <asm/arch/timer_defs.h>
43#include <div64.h>
44
45DECLARE_GLOBAL_DATA_PTR;
46
47static struct davinci_timer * const timer =
48 (struct davinci_timer *)CONFIG_SYS_TIMERBASE;
49
50#define TIMER_LOAD_VAL 0xffffffff
51
52#define TIM_CLK_DIV 16
53
54int timer_init(void)
55{
56
57 writel(0x0, &timer->tcr);
58 writel(0x0, &timer->tgcr);
59 writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &timer->tgcr);
60 writel(0x0, &timer->tim34);
61 writel(TIMER_LOAD_VAL, &timer->prd34);
62 writel(2 << 22, &timer->tcr);
63 gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK / TIM_CLK_DIV;
64 gd->arch.timer_reset_value = 0;
65
66 return(0);
67}
68
69
70
71
72unsigned long long get_ticks(void)
73{
74 unsigned long now = readl(&timer->tim34);
75
76
77 if (now < gd->arch.tbl)
78 gd->arch.tbu++;
79 gd->arch.tbl = now;
80
81 return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
82}
83
84ulong get_timer(ulong base)
85{
86 unsigned long long timer_diff;
87
88 timer_diff = get_ticks() - gd->arch.timer_reset_value;
89
90 return lldiv(timer_diff,
91 (gd->arch.timer_rate_hz / CONFIG_SYS_HZ)) - base;
92}
93
94void __udelay(unsigned long usec)
95{
96 unsigned long long endtime;
97
98 endtime = lldiv((unsigned long long)usec * gd->arch.timer_rate_hz,
99 1000000UL);
100 endtime += get_ticks();
101
102 while (get_ticks() < endtime)
103 ;
104}
105
106
107
108
109
110ulong get_tbclk(void)
111{
112 return gd->arch.timer_rate_hz;
113}
114
115#ifdef CONFIG_HW_WATCHDOG
116static struct davinci_timer * const wdttimer =
117 (struct davinci_timer *)CONFIG_SYS_WDTTIMERBASE;
118
119
120
121
122void davinci_hw_watchdog_enable(void)
123{
124 writel(0x0, &wdttimer->tcr);
125 writel(0x0, &wdttimer->tgcr);
126
127 writel(0x08 | 0x03 | ((TIM_CLK_DIV - 1) << 8), &wdttimer->tgcr);
128 writel(CONFIG_SYS_WDT_PERIOD_LOW, &wdttimer->prd12);
129 writel(CONFIG_SYS_WDT_PERIOD_HIGH, &wdttimer->prd34);
130 writel(2 << 22, &wdttimer->tcr);
131 writel(0x0, &wdttimer->tim12);
132 writel(0x0, &wdttimer->tim34);
133
134 writel(0xa5c64000, &wdttimer->wdtcr);
135
136 writel(0xda7e4000, &wdttimer->wdtcr);
137}
138
139void davinci_hw_watchdog_reset(void)
140{
141 writel(0xa5c64000, &wdttimer->wdtcr);
142 writel(0xda7e4000, &wdttimer->wdtcr);
143}
144#endif
145