1e014ad9c9284b78a22728f9deceac1a0f7bd52e
[oweals/u-boot.git] / arch / arm / mach-at91 / arm920t / timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2002
4  * Lineo, Inc. <www.lineo.com>
5  * Bernhard Kuhn <bkuhn@lineo.com>
6  *
7  * (C) Copyright 2002
8  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9  * Marius Groeger <mgroeger@sysgo.de>
10  *
11  * (C) Copyright 2002
12  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13  * Alex Zuepke <azu@sysgo.de>
14  */
15
16 #include <common.h>
17 #include <init.h>
18 #include <time.h>
19
20 #include <asm/io.h>
21 #include <asm/arch/hardware.h>
22 #include <asm/arch/at91_tc.h>
23 #include <asm/arch/clk.h>
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 /* the number of clocks per CONFIG_SYS_HZ */
28 #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK/CONFIG_SYS_HZ)
29
30 int timer_init(void)
31 {
32         at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC;
33
34         at91_periph_clk_enable(ATMEL_ID_TC0);
35
36         writel(0, &tc->bcr);
37         writel(AT91_TC_BMR_TC0XC0S_NONE | AT91_TC_BMR_TC1XC1S_NONE |
38                 AT91_TC_BMR_TC2XC2S_NONE , &tc->bmr);
39
40         writel(AT91_TC_CCR_CLKDIS, &tc->tc[0].ccr);
41         /* set to MCLK/2 and restart the timer
42         when the value in TC_RC is reached */
43         writel(AT91_TC_CMR_TCCLKS_CLOCK1 | AT91_TC_CMR_CPCTRG, &tc->tc[0].cmr);
44
45         writel(0xFFFFFFFF, &tc->tc[0].idr); /* disable interrupts */
46         writel(TIMER_LOAD_VAL, &tc->tc[0].rc);
47
48         writel(AT91_TC_CCR_SWTRG | AT91_TC_CCR_CLKEN, &tc->tc[0].ccr);
49         gd->arch.lastinc = 0;
50         gd->arch.tbl = 0;
51
52         return 0;
53 }
54
55 /*
56  * timer without interrupts
57  */
58 ulong get_timer_raw(void)
59 {
60         at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC;
61         u32 now;
62
63         now = readl(&tc->tc[0].cv) & 0x0000ffff;
64
65         if (now >= gd->arch.lastinc) {
66                 /* normal mode */
67                 gd->arch.tbl += now - gd->arch.lastinc;
68         } else {
69                 /* we have an overflow ... */
70                 gd->arch.tbl += now + TIMER_LOAD_VAL - gd->arch.lastinc;
71         }
72         gd->arch.lastinc = now;
73
74         return gd->arch.tbl;
75 }
76
77 static ulong get_timer_masked(void)
78 {
79         return get_timer_raw()/TIMER_LOAD_VAL;
80 }
81
82 ulong get_timer(ulong base)
83 {
84         return get_timer_masked() - base;
85 }
86
87 void __udelay(unsigned long usec)
88 {
89         u32 tmo;
90         u32 endtime;
91         signed long diff;
92
93         tmo = CONFIG_SYS_HZ_CLOCK / 1000;
94         tmo *= usec;
95         tmo /= 1000;
96
97         endtime = get_timer_raw() + tmo;
98
99         do {
100                 u32 now = get_timer_raw();
101                 diff = endtime - now;
102         } while (diff >= 0);
103 }
104
105 /*
106  * This function is derived from PowerPC code (read timebase as long long).
107  * On ARM it just returns the timer value.
108  */
109 unsigned long long get_ticks(void)
110 {
111         return get_timer(0);
112 }
113
114 /*
115  * This function is derived from PowerPC code (timebase clock frequency).
116  * On ARM it returns the number of timer ticks per second.
117  */
118 ulong get_tbclk(void)
119 {
120         return CONFIG_SYS_HZ;
121 }