5adccab012ad0494de7b7e1d49746f476a25be6f
[oweals/u-boot.git] / arch / arm / cpu / armv7 / stv0991 / timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014, STMicroelectronics - All Rights Reserved
4  * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
5  */
6
7 #include <common.h>
8 #include <init.h>
9 #include <time.h>
10 #include <asm/io.h>
11 #include <asm/arch-stv0991/hardware.h>
12 #include <asm/arch-stv0991/stv0991_cgu.h>
13 #include <asm/arch-stv0991/stv0991_gpt.h>
14
15 static struct stv0991_cgu_regs *const stv0991_cgu_regs = \
16                                 (struct stv0991_cgu_regs *) (CGU_BASE_ADDR);
17
18 #define READ_TIMER()    (readl(&gpt1_regs_ptr->cnt) & GPT_FREE_RUNNING)
19 #define GPT_RESOLUTION  (CONFIG_STV0991_HZ_CLOCK / CONFIG_STV0991_HZ)
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 #define timestamp gd->arch.tbl
24 #define lastdec gd->arch.lastinc
25
26 static ulong get_timer_masked(void);
27
28 int timer_init(void)
29 {
30         /* Timer1 clock configuration */
31         writel(TIMER1_CLK_CFG, &stv0991_cgu_regs->tim_freq);
32         writel(readl(&stv0991_cgu_regs->cgu_enable_2) |
33                         TIMER1_CLK_EN, &stv0991_cgu_regs->cgu_enable_2);
34
35         /* Stop the timer */
36         writel(readl(&gpt1_regs_ptr->cr1) & ~GPT_CR1_CEN, &gpt1_regs_ptr->cr1);
37         writel(GPT_PRESCALER_128, &gpt1_regs_ptr->psc);
38         /* Configure timer for auto-reload */
39         writel(readl(&gpt1_regs_ptr->cr1) | GPT_MODE_AUTO_RELOAD,
40                         &gpt1_regs_ptr->cr1);
41
42         /* load value for free running */
43         writel(GPT_FREE_RUNNING, &gpt1_regs_ptr->arr);
44
45         /* start timer */
46         writel(readl(&gpt1_regs_ptr->cr1) | GPT_CR1_CEN,
47                         &gpt1_regs_ptr->cr1);
48
49         /* Reset the timer */
50         lastdec = READ_TIMER();
51         timestamp = 0;
52
53         return 0;
54 }
55
56 /*
57  * timer without interrupts
58  */
59 ulong get_timer(ulong base)
60 {
61         return (get_timer_masked() / GPT_RESOLUTION) - base;
62 }
63
64 void __udelay(unsigned long usec)
65 {
66         ulong tmo;
67         ulong start = get_timer_masked();
68         ulong tenudelcnt = CONFIG_STV0991_HZ_CLOCK / (1000 * 100);
69         ulong rndoff;
70
71         rndoff = (usec % 10) ? 1 : 0;
72
73         /* tenudelcnt timer tick gives 10 microsecconds delay */
74         tmo = ((usec / 10) + rndoff) * tenudelcnt;
75
76         while ((ulong) (get_timer_masked() - start) < tmo)
77                 ;
78 }
79
80 static ulong get_timer_masked(void)
81 {
82         ulong now = READ_TIMER();
83
84         if (now >= lastdec) {
85                 /* normal mode */
86                 timestamp += now - lastdec;
87         } else {
88                 /* we have an overflow ... */
89                 timestamp += now + GPT_FREE_RUNNING - lastdec;
90         }
91         lastdec = now;
92
93         return timestamp;
94 }
95
96 /*
97  * This function is derived from PowerPC code (read timebase as long long).
98  * On ARM it just returns the timer value.
99  */
100 unsigned long long get_ticks(void)
101 {
102         return get_timer(0);
103 }
104
105 /*
106  * This function is derived from PowerPC code (timebase clock frequency).
107  * On ARM it returns the number of timer ticks per second.
108  */
109 ulong get_tbclk(void)
110 {
111         return CONFIG_STV0991_HZ;
112 }