]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/arm925t/timer.c
Add GPL-2.0+ SPDX-License-Identifier to source files
[karo-tx-uboot.git] / arch / arm / cpu / arm925t / timer.c
1 /*
2  * (C) Copyright 2009
3  * 2N Telekomunikace, <www.2n.cz>
4  *
5  * (C) Copyright 2003
6  * Texas Instruments, <www.ti.com>
7  *
8  * (C) Copyright 2002
9  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
10  * Marius Groeger <mgroeger@sysgo.de>
11  *
12  * (C) Copyright 2002
13  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
14  * Alex Zuepke <azu@sysgo.de>
15  *
16  * (C) Copyright 2002
17  * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
18  *
19  * SPDX-License-Identifier:     GPL-2.0+ 
20  */
21
22 #include <common.h>
23 #include <arm925t.h>
24 #include <configs/omap1510.h>
25 #include <asm/io.h>
26
27 #define TIMER_LOAD_VAL  0xffffffff
28 #define TIMER_CLOCK     (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV))
29
30 static uint32_t timestamp;
31 static uint32_t lastdec;
32
33 /* nothing really to do with interrupts, just starts up a counter. */
34 int timer_init (void)
35 {
36         /* Start the decrementer ticking down from 0xffffffff */
37         __raw_writel(TIMER_LOAD_VAL, CONFIG_SYS_TIMERBASE + LOAD_TIM);
38         __raw_writel(MPUTIM_ST | MPUTIM_AR | MPUTIM_CLOCK_ENABLE |
39                 (CONFIG_SYS_PTV << MPUTIM_PTV_BIT),
40                 CONFIG_SYS_TIMERBASE + CNTL_TIMER);
41
42         /* init the timestamp and lastdec value */
43         lastdec = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM) /
44                         (TIMER_CLOCK / CONFIG_SYS_HZ);
45         timestamp = 0;         /* start "advancing" time stamp from 0 */
46
47         return 0;
48 }
49
50 /*
51  * timer without interrupts
52  */
53 ulong get_timer (ulong base)
54 {
55         return get_timer_masked () - base;
56 }
57
58 /* delay x useconds AND preserve advance timestamp value */
59 void __udelay (unsigned long usec)
60 {
61         int32_t tmo = usec * (TIMER_CLOCK / 1000) / 1000;
62         uint32_t now, last = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM);
63
64         while (tmo > 0) {
65                 now = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM);
66                 if (last < now) /* count down timer underflow */
67                         tmo -= TIMER_LOAD_VAL - now + last;
68                 else
69                         tmo -= last - now;
70                 last = now;
71         }
72 }
73
74 ulong get_timer_masked (void)
75 {
76         uint32_t now = __raw_readl(CONFIG_SYS_TIMERBASE + READ_TIM) /
77                         (TIMER_CLOCK / CONFIG_SYS_HZ);
78         if (lastdec < now)      /* count down timer underflow */
79                 timestamp += TIMER_LOAD_VAL / (TIMER_CLOCK / CONFIG_SYS_HZ) -
80                                 now + lastdec;
81         else
82                 timestamp += lastdec - now;
83         lastdec = now;
84
85         return timestamp;
86 }
87
88 /*
89  * This function is derived from PowerPC code (read timebase as long long).
90  * On ARM it just returns the timer value.
91  */
92 unsigned long long get_ticks(void)
93 {
94         return get_timer(0);
95 }
96
97 /*
98  * This function is derived from PowerPC code (timebase clock frequency).
99  * On ARM it returns the number of timer ticks per second.
100  */
101 ulong get_tbclk (void)
102 {
103         return CONFIG_SYS_HZ;
104 }