4f6a66d8039ded186dfa5e1cb457c90b62209c8c
[oweals/u-boot.git] / arch / arm / cpu / armv7 / sunxi / timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2007-2011
4  * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
5  * Tom Cubie <tangliang@allwinnertech.com>
6  */
7
8 #include <common.h>
9 #include <init.h>
10 #include <time.h>
11 #include <asm/io.h>
12 #include <asm/arch/timer.h>
13
14 DECLARE_GLOBAL_DATA_PTR;
15
16 #define TIMER_MODE   (0x0 << 7) /* continuous mode */
17 #define TIMER_DIV    (0x0 << 4) /* pre scale 1 */
18 #define TIMER_SRC    (0x1 << 2) /* osc24m */
19 #define TIMER_RELOAD (0x1 << 1) /* reload internal value */
20 #define TIMER_EN     (0x1 << 0) /* enable timer */
21
22 #define TIMER_CLOCK             (24 * 1000 * 1000)
23 #define COUNT_TO_USEC(x)        ((x) / 24)
24 #define USEC_TO_COUNT(x)        ((x) * 24)
25 #define TICKS_PER_HZ            (TIMER_CLOCK / CONFIG_SYS_HZ)
26 #define TICKS_TO_HZ(x)          ((x) / TICKS_PER_HZ)
27
28 #define TIMER_LOAD_VAL          0xffffffff
29
30 #define TIMER_NUM               0       /* we use timer 0 */
31
32 /* read the 32-bit timer */
33 static ulong read_timer(void)
34 {
35         struct sunxi_timer_reg *timers =
36                 (struct sunxi_timer_reg *)SUNXI_TIMER_BASE;
37         struct sunxi_timer *timer = &timers->timer[TIMER_NUM];
38
39         /*
40          * The hardware timer counts down, therefore we invert to
41          * produce an incrementing timer.
42          */
43         return ~readl(&timer->val);
44 }
45
46 /* init timer register */
47 int timer_init(void)
48 {
49         struct sunxi_timer_reg *timers =
50                 (struct sunxi_timer_reg *)SUNXI_TIMER_BASE;
51         struct sunxi_timer *timer = &timers->timer[TIMER_NUM];
52         writel(TIMER_LOAD_VAL, &timer->inter);
53         writel(TIMER_MODE | TIMER_DIV | TIMER_SRC | TIMER_RELOAD | TIMER_EN,
54                &timer->ctl);
55
56         return 0;
57 }
58
59 /* timer without interrupts */
60 static ulong get_timer_masked(void)
61 {
62         /* current tick value */
63         ulong now = TICKS_TO_HZ(read_timer());
64
65         if (now >= gd->arch.lastinc)    /* normal (non rollover) */
66                 gd->arch.tbl += (now - gd->arch.lastinc);
67         else {
68                 /* rollover */
69                 gd->arch.tbl += (TICKS_TO_HZ(TIMER_LOAD_VAL)
70                                 - gd->arch.lastinc) + now;
71         }
72         gd->arch.lastinc = now;
73
74         return gd->arch.tbl;
75 }
76
77 ulong get_timer(ulong base)
78 {
79         return get_timer_masked() - base;
80 }
81
82 /* delay x useconds */
83 void __udelay(unsigned long usec)
84 {
85         long tmo = USEC_TO_COUNT(usec);
86         ulong now, last = read_timer();
87
88         while (tmo > 0) {
89                 now = read_timer();
90                 if (now > last) /* normal (non rollover) */
91                         tmo -= now - last;
92                 else            /* rollover */
93                         tmo -= TIMER_LOAD_VAL - last + now;
94                 last = now;
95         }
96 }
97
98 /*
99  * This function is derived from PowerPC code (read timebase as long long).
100  * On ARM it just returns the timer value.
101  */
102 unsigned long long get_ticks(void)
103 {
104         return get_timer(0);
105 }
106
107 /*
108  * This function is derived from PowerPC code (timebase clock frequency).
109  * On ARM it returns the number of timer ticks per second.
110  */
111 ulong get_tbclk(void)
112 {
113         return CONFIG_SYS_HZ;
114 }