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