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>
31 #define PRESCALER_1 (16 - 1) /* prescaler of timer 2, 3, 4 */
32 #define MUX_DIV_2 1 /* 1/2 period */
33 #define MUX_DIV_4 2 /* 1/4 period */
34 #define MUX_DIV_8 3 /* 1/8 period */
35 #define MUX_DIV_16 4 /* 1/16 period */
36 #define MUX4_DIV_SHIFT 16
38 #define TCON_TIMER4_SHIFT 20
40 static unsigned long count_value;
42 /* Internal tick units */
43 static unsigned long long timestamp; /* Monotonic incrementing timer */
44 static unsigned long lastdec; /* Last decremneter snapshot */
46 /* macro to read the 16 bit timer */
47 static inline struct s5p_timer *s5p_get_base_timer(void)
49 return (struct s5p_timer *)samsung_get_base_timer();
54 struct s5p_timer *const timer = s5p_get_base_timer();
60 * PWM_CLK / { (prescaler_value + 1) * (divider_value) }
63 /* set prescaler : 16 */
65 writel((PRESCALER_1 & 0xff) << 8, &timer->tcfg0);
66 writel((MUX_DIV_2 & 0xf) << MUX4_DIV_SHIFT, &timer->tcfg1);
68 if (count_value == 0) {
69 /* reset initial value */
70 /* count_value = 2085937.5(HZ) (per 1 sec)*/
71 count_value = get_pwm_clk() / ((PRESCALER_1 + 1) *
74 /* count_value / 100 = 20859.375(HZ) (per 10 msec) */
75 count_value = count_value / 100;
79 writel(count_value, &timer->tcntb4);
80 lastdec = count_value;
82 val = (readl(&timer->tcon) & ~(0x07 << TCON_TIMER4_SHIFT)) |
85 /* auto reload & manual update */
86 writel(val | TCON4_UPDATE, &timer->tcon);
88 /* start PWM timer 4 */
89 writel(val | TCON4_START, &timer->tcon);
97 * timer without interrupts
99 void reset_timer(void)
101 reset_timer_masked();
104 unsigned long get_timer(unsigned long base)
106 return get_timer_masked() - base;
109 void set_timer(unsigned long t)
114 /* delay x useconds */
115 void __udelay(unsigned long usec)
117 unsigned long tmo, tmp;
121 * if "big" number, spread normalization
123 * 1. start to normalize for usec to ticks per sec
124 * 2. find number of "ticks" to wait to achieve target
125 * 3. finish normalize.
128 tmo *= (CONFIG_SYS_HZ * count_value / 10);
131 /* else small number, don't kill it prior to HZ multiply */
132 tmo = usec * CONFIG_SYS_HZ * count_value / 10;
133 tmo /= (1000 * 1000);
136 /* get current timestamp */
139 /* if setting this fordward will roll time stamp */
140 /* reset "advancing" timestamp to 0, set lastdec value */
141 /* else, set advancing stamp wake up time */
142 if ((tmo + tmp + 1) < tmp)
143 reset_timer_masked();
147 /* loop till event */
148 while (get_timer_masked() < tmo)
152 void reset_timer_masked(void)
154 struct s5p_timer *const timer = s5p_get_base_timer();
157 lastdec = readl(&timer->tcnto4);
161 unsigned long get_timer_masked(void)
163 struct s5p_timer *const timer = s5p_get_base_timer();
164 unsigned long now = readl(&timer->tcnto4);
167 timestamp += lastdec - now;
169 timestamp += lastdec + count_value - now;
177 * This function is derived from PowerPC code (read timebase as long long).
178 * On ARM it just returns the timer value.
180 unsigned long long get_ticks(void)
186 * This function is derived from PowerPC code (timebase clock frequency).
187 * On ARM it returns the number of timer ticks per second.
189 unsigned long get_tbclk(void)
191 return CONFIG_SYS_HZ;