blob: 1f1c7ff340fbc2f776564c61902028f9919bf1d2 [file] [log] [blame]
Minkyu Kang399e5ae2009-10-01 17:20:01 +09001/*
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>
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
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.
14 *
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.
19 *
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,
23 * MA 02111-1307 USA
24 */
25
26#include <common.h>
27#include <asm/io.h>
28#include <asm/arch/pwm.h>
29#include <asm/arch/clk.h>
30
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
37
38#define TCON_TIMER4_SHIFT 20
39
40static unsigned long count_value;
41
42/* Internal tick units */
43static unsigned long long timestamp; /* Monotonic incrementing timer */
44static unsigned long lastdec; /* Last decremneter snapshot */
45
46/* macro to read the 16 bit timer */
Minkyu Kang37168da2010-08-19 20:41:50 +090047static inline struct s5p_timer *s5p_get_base_timer(void)
Minkyu Kang399e5ae2009-10-01 17:20:01 +090048{
Minkyu Kang37168da2010-08-19 20:41:50 +090049 return (struct s5p_timer *)samsung_get_base_timer();
Minkyu Kang399e5ae2009-10-01 17:20:01 +090050}
51
52int timer_init(void)
53{
Minkyu Kang37168da2010-08-19 20:41:50 +090054 struct s5p_timer *const timer = s5p_get_base_timer();
Minkyu Kang399e5ae2009-10-01 17:20:01 +090055 u32 val;
56
57 /*
58 * @ PWM Timer 4
59 * Timer Freq(HZ) =
60 * PCLK / { (prescaler_value + 1) * (divider_value) }
61 */
62
63 /* set prescaler : 16 */
64 /* set divider : 2 */
65 writel((PRESCALER_1 & 0xff) << 8, &timer->tcfg0);
66 writel((MUX_DIV_2 & 0xf) << MUX4_DIV_SHIFT, &timer->tcfg1);
67
68 if (count_value == 0) {
69 /* reset initial value */
70 /* count_value = 2085937.5(HZ) (per 1 sec)*/
71 count_value = get_pclk() / ((PRESCALER_1 + 1) *
72 (MUX_DIV_2 + 1));
73
74 /* count_value / 100 = 20859.375(HZ) (per 10 msec) */
75 count_value = count_value / 100;
76 }
77
78 /* set count value */
79 writel(count_value, &timer->tcntb4);
80 lastdec = count_value;
81
82 val = (readl(&timer->tcon) & ~(0x07 << TCON_TIMER4_SHIFT)) |
Minkyu Kang37168da2010-08-19 20:41:50 +090083 TCON4_AUTO_RELOAD;
Minkyu Kang399e5ae2009-10-01 17:20:01 +090084
85 /* auto reload & manual update */
Minkyu Kang37168da2010-08-19 20:41:50 +090086 writel(val | TCON4_UPDATE, &timer->tcon);
Minkyu Kang399e5ae2009-10-01 17:20:01 +090087
88 /* start PWM timer 4 */
Minkyu Kang37168da2010-08-19 20:41:50 +090089 writel(val | TCON4_START, &timer->tcon);
Minkyu Kang399e5ae2009-10-01 17:20:01 +090090
91 timestamp = 0;
92
93 return 0;
94}
95
96/*
97 * timer without interrupts
98 */
99void reset_timer(void)
100{
101 reset_timer_masked();
102}
103
104unsigned long get_timer(unsigned long base)
105{
106 return get_timer_masked() - base;
107}
108
109void set_timer(unsigned long t)
110{
111 timestamp = t;
112}
113
114/* delay x useconds */
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100115void __udelay(unsigned long usec)
Minkyu Kang399e5ae2009-10-01 17:20:01 +0900116{
117 unsigned long tmo, tmp;
118
119 if (usec >= 1000) {
120 /*
121 * if "big" number, spread normalization
122 * to seconds
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.
126 */
127 tmo = usec / 1000;
128 tmo *= (CONFIG_SYS_HZ * count_value / 10);
129 tmo /= 1000;
130 } else {
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);
134 }
135
136 /* get current timestamp */
137 tmp = get_timer(0);
138
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();
144 else
145 tmo += tmp;
146
147 /* loop till event */
148 while (get_timer_masked() < tmo)
149 ; /* nop */
150}
151
152void reset_timer_masked(void)
153{
Minkyu Kang37168da2010-08-19 20:41:50 +0900154 struct s5p_timer *const timer = s5p_get_base_timer();
Minkyu Kang399e5ae2009-10-01 17:20:01 +0900155
156 /* reset time */
157 lastdec = readl(&timer->tcnto4);
158 timestamp = 0;
159}
160
161unsigned long get_timer_masked(void)
162{
Minkyu Kang37168da2010-08-19 20:41:50 +0900163 struct s5p_timer *const timer = s5p_get_base_timer();
Minkyu Kang399e5ae2009-10-01 17:20:01 +0900164 unsigned long now = readl(&timer->tcnto4);
165
166 if (lastdec >= now)
167 timestamp += lastdec - now;
168 else
169 timestamp += lastdec + count_value - now;
170
171 lastdec = now;
172
173 return timestamp;
174}
175
176/*
177 * This function is derived from PowerPC code (read timebase as long long).
178 * On ARM it just returns the timer value.
179 */
180unsigned long long get_ticks(void)
181{
182 return get_timer(0);
183}
184
185/*
186 * This function is derived from PowerPC code (timebase clock frequency).
187 * On ARM it returns the number of timer ticks per second.
188 */
189unsigned long get_tbclk(void)
190{
191 return CONFIG_SYS_HZ;
192}