1 // SPDX-License-Identifier: GPL-2.0+
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>
14 #include <asm/arch/pwm.h>
15 #include <asm/arch/clk.h>
17 /* Use the old PWM interface for now */
21 DECLARE_GLOBAL_DATA_PTR;
23 unsigned long get_current_tick(void);
24 static void reset_timer_masked(void);
26 /* macro to read the 16 bit timer */
27 static inline struct s5p_timer *s5p_get_base_timer(void)
29 return (struct s5p_timer *)samsung_get_base_timer();
33 * Read the countdown timer.
35 * This operates at 1MHz and counts downwards. It will wrap about every
36 * hour (2^32 microseconds).
38 * @return current value of timer
40 static unsigned long timer_get_us_down(void)
42 struct s5p_timer *const timer = s5p_get_base_timer();
44 return readl(&timer->tcnto4);
50 pwm_init(4, MUX_DIV_4, 0);
51 pwm_config(4, 100000, 100000);
54 /* Use this as the current monotonic time in us */
55 gd->arch.timer_reset_value = 0;
57 /* Use this as the last timer value we saw */
58 gd->arch.lastinc = timer_get_us_down();
65 * timer without interrupts
67 unsigned long get_timer(unsigned long base)
69 unsigned long long time_ms;
71 ulong now = timer_get_us_down();
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.
78 gd->arch.timer_reset_value += gd->arch.lastinc - now;
79 gd->arch.lastinc = now;
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;
87 unsigned long __attribute__((no_instrument_function)) timer_get_us(void)
89 static unsigned long base_time_us;
91 struct s5p_timer *const timer =
92 (struct s5p_timer *)samsung_get_base_timer();
93 unsigned long now_downward_us = readl(&timer->tcnto4);
96 base_time_us = now_downward_us;
98 /* Note that this timer counts downward. */
99 return base_time_us - now_downward_us;
102 /* delay x useconds */
103 void __udelay(unsigned long usec)
105 unsigned long count_value;
107 count_value = timer_get_us_down();
108 while ((int)(count_value - timer_get_us_down()) < (int)usec)
112 static void reset_timer_masked(void)
114 struct s5p_timer *const timer = s5p_get_base_timer();
117 gd->arch.lastinc = readl(&timer->tcnto4);
122 * This function is derived from PowerPC code (read timebase as long long).
123 * On ARM it just returns the timer value.
125 unsigned long long get_ticks(void)
131 * This function is derived from PowerPC code (timebase clock frequency).
132 * On ARM it returns the number of timer ticks per second.
134 unsigned long get_tbclk(void)
136 return CONFIG_SYS_HZ;