2 * Copyright (C) 2009 Samsung Electronics
3 * Heungjun Kim <riverful.kim@samsung.com>
4 * Inki Dae <inki.dae@samsung.com>
5 * Minkyu Kang <mk7.kang@samsung.com>
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #include <asm/arch/pwm.h>
29 #include <asm/arch/clk.h>
32 static unsigned long count_value;
34 /* Internal tick units */
35 static unsigned long long timestamp; /* Monotonic incrementing timer */
36 static unsigned long lastdec; /* Last decremneter snapshot */
38 /* macro to read the 16 bit timer */
39 static inline struct s5p_timer *s5p_get_base_timer(void)
41 return (struct s5p_timer *)samsung_get_base_timer();
47 pwm_init(4, MUX_DIV_2, 0);
55 * timer without interrupts
57 void reset_timer(void)
62 unsigned long get_timer(unsigned long base)
64 return get_timer_masked() - base;
67 void set_timer(unsigned long t)
72 /* delay x useconds */
73 void __udelay(unsigned long usec)
75 struct s5p_timer *const timer = s5p_get_base_timer();
76 unsigned long tmo, tmp;
78 count_value = readl(&timer->tcntb4);
82 * if "big" number, spread normalization
84 * 1. start to normalize for usec to ticks per sec
85 * 2. find number of "ticks" to wait to achieve target
86 * 3. finish normalize.
89 tmo *= (CONFIG_SYS_HZ * count_value / 10);
92 /* else small number, don't kill it prior to HZ multiply */
93 tmo = usec * CONFIG_SYS_HZ * count_value / 10;
97 /* get current timestamp */
100 /* if setting this fordward will roll time stamp */
101 /* reset "advancing" timestamp to 0, set lastdec value */
102 /* else, set advancing stamp wake up time */
103 if ((tmo + tmp + 1) < tmp)
104 reset_timer_masked();
108 /* loop till event */
109 while (get_timer_masked() < tmo)
113 void reset_timer_masked(void)
115 struct s5p_timer *const timer = s5p_get_base_timer();
118 lastdec = readl(&timer->tcnto4);
122 unsigned long get_timer_masked(void)
124 struct s5p_timer *const timer = s5p_get_base_timer();
125 unsigned long now = readl(&timer->tcnto4);
128 timestamp += lastdec - now;
130 timestamp += lastdec + count_value - now;
138 * This function is derived from PowerPC code (read timebase as long long).
139 * On ARM it just returns the timer value.
141 unsigned long long get_ticks(void)
147 * This function is derived from PowerPC code (timebase clock frequency).
148 * On ARM it returns the number of timer ticks per second.
150 unsigned long get_tbclk(void)
152 return CONFIG_SYS_HZ;