d2b5b4d3fcd20b1fc23153be2f110ae65ac3d4b0
[oweals/u-boot.git] / arch / arm / cpu / armv7 / s5p-common / timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2009 Samsung Electronics
4  * Heungjun Kim <riverful.kim@samsung.com>
5  * Inki Dae <inki.dae@samsung.com>
6  * Minkyu Kang <mk7.kang@samsung.com>
7  */
8
9 #include <common.h>
10 #include <div64.h>
11 #include <init.h>
12 #include <time.h>
13 #include <asm/io.h>
14 #include <asm/arch/pwm.h>
15 #include <asm/arch/clk.h>
16
17 /* Use the old PWM interface for now */
18 #undef CONFIG_DM_PWM
19 #include <pwm.h>
20
21 DECLARE_GLOBAL_DATA_PTR;
22
23 unsigned long get_current_tick(void);
24 static void reset_timer_masked(void);
25
26 /* macro to read the 16 bit timer */
27 static inline struct s5p_timer *s5p_get_base_timer(void)
28 {
29         return (struct s5p_timer *)samsung_get_base_timer();
30 }
31
32 /**
33  * Read the countdown timer.
34  *
35  * This operates at 1MHz and counts downwards. It will wrap about every
36  * hour (2^32 microseconds).
37  *
38  * @return current value of timer
39  */
40 static unsigned long timer_get_us_down(void)
41 {
42         struct s5p_timer *const timer = s5p_get_base_timer();
43
44         return readl(&timer->tcnto4);
45 }
46
47 int timer_init(void)
48 {
49         /* PWM Timer 4 */
50         pwm_init(4, MUX_DIV_4, 0);
51         pwm_config(4, 100000, 100000);
52         pwm_enable(4);
53
54         /* Use this as the current monotonic time in us */
55         gd->arch.timer_reset_value = 0;
56
57         /* Use this as the last timer value we saw */
58         gd->arch.lastinc = timer_get_us_down();
59         reset_timer_masked();
60
61         return 0;
62 }
63
64 /*
65  * timer without interrupts
66  */
67 unsigned long get_timer(unsigned long base)
68 {
69         unsigned long long time_ms;
70
71         ulong now = timer_get_us_down();
72
73         /*
74          * Increment the time by the amount elapsed since the last read.
75          * The timer may have wrapped around, but it makes no difference to
76          * our arithmetic here.
77          */
78         gd->arch.timer_reset_value += gd->arch.lastinc - now;
79         gd->arch.lastinc = now;
80
81         /* Divide by 1000 to convert from us to ms */
82         time_ms = gd->arch.timer_reset_value;
83         do_div(time_ms, 1000);
84         return time_ms - base;
85 }
86
87 unsigned long __attribute__((no_instrument_function)) timer_get_us(void)
88 {
89         static unsigned long base_time_us;
90
91         struct s5p_timer *const timer =
92                 (struct s5p_timer *)samsung_get_base_timer();
93         unsigned long now_downward_us = readl(&timer->tcnto4);
94
95         if (!base_time_us)
96                 base_time_us = now_downward_us;
97
98         /* Note that this timer counts downward. */
99         return base_time_us - now_downward_us;
100 }
101
102 /* delay x useconds */
103 void __udelay(unsigned long usec)
104 {
105         unsigned long count_value;
106
107         count_value = timer_get_us_down();
108         while ((int)(count_value - timer_get_us_down()) < (int)usec)
109                 ;
110 }
111
112 static void reset_timer_masked(void)
113 {
114         struct s5p_timer *const timer = s5p_get_base_timer();
115
116         /* reset time */
117         gd->arch.lastinc = readl(&timer->tcnto4);
118         gd->arch.tbl = 0;
119 }
120
121 /*
122  * This function is derived from PowerPC code (read timebase as long long).
123  * On ARM it just returns the timer value.
124  */
125 unsigned long long get_ticks(void)
126 {
127         return get_timer(0);
128 }
129
130 /*
131  * This function is derived from PowerPC code (timebase clock frequency).
132  * On ARM it returns the number of timer ticks per second.
133  */
134 unsigned long get_tbclk(void)
135 {
136         return CONFIG_SYS_HZ;
137 }