3 * Texas Instruments <www.ti.com>
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Alex Zuepke <azu@sysgo.de>
13 * (C) Copyright 2002-2004
14 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
17 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
19 * (C) Copyright 2009-2010 Freescale Semiconductor, Inc.
21 * See file CREDITS for list of people who contributed to this
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License as
26 * published by the Free Software Foundation; either version 2 of
27 * the License, or (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
41 #include <asm/arch/mx23.h>
42 #include <asm/arch/timrot.h>
44 #define CONFIG_USE_TIMER0
46 #if defined(CONFIG_USE_TIMER0)
47 #define TIMCTRL TIMCTRL0
48 #define TIMCOUNT TIMCOUNT0
49 #elif defined(CONFIG_USE_TIMER1)
50 #define TIMCTRL TIMCTRL1
51 #define TIMCOUNT TIMCOUNT1
52 #elif defined(CONFIG_USE_TIMER2)
53 #define TIMCTRL TIMCTRL2
54 #define TIMCOUNT TIMCOUNT2
55 #elif defined(CONFIG_USE_TIMER3)
56 #define TIMCTRL TIMCTRL3
57 #define TIMCOUNT TIMCOUNT3
59 #error "Define which MX23 timer to use"
62 #define TIMER_LOAD_VAL 0x0000ffff
64 /* macro to read the 16 bit timer */
65 #define READ_TIMER ((REG_RD(TIMROT_BASE + TIMCOUNT) & 0xffff0000) >> 16)
67 static ulong timestamp;
75 * Reset Timers and Rotary Encoder module
79 REG_CLR(TIMROT_BASE + ROTCTRL, 1 << 31);
80 while (REG_RD(TIMROT_BASE + ROTCTRL) & (1 << 31))
84 REG_CLR(TIMROT_BASE + ROTCTRL, 1 << 30);
86 /* Set SFTRST and wait until CLKGATE is set */
87 REG_SET(TIMROT_BASE + ROTCTRL, 1 << 31);
88 while (!(REG_RD(TIMROT_BASE + ROTCTRL) & (1 << 30)))
91 /* Clear SFTRST and CLKGATE */
92 REG_CLR(TIMROT_BASE + ROTCTRL, 1 << 31);
93 REG_CLR(TIMROT_BASE + ROTCTRL, 1 << 30);
96 * Now initialize timer
99 /* Set fixed_count to 0 */
100 REG_WR(TIMROT_BASE + TIMCOUNT, 0);
102 /* set UPDATE bit and 1Khz frequency */
103 REG_WR(TIMROT_BASE + TIMCTRL,
104 TIMCTRL_RELOAD | TIMCTRL_UPDATE | TIMCTRL_SELECT_1KHZ);
106 /* Set fixed_count to maximal value */
107 REG_WR(TIMROT_BASE + TIMCOUNT, TIMER_LOAD_VAL);
109 /* init the timestamp and lastdec value */
110 reset_timer_masked();
116 * timer without interrupts
119 void reset_timer(void)
121 reset_timer_masked();
124 ulong get_timer(ulong base)
126 return get_timer_masked() - base;
129 void set_timer(ulong t)
134 /* delay x useconds AND perserve advance timstamp value */
135 void udelay(unsigned long usec)
140 /* if "big" number, spread normalization to seconds */
142 /* start to normalize for usec to ticks per sec */
143 tmo *= CONFIG_SYS_HZ;
144 /* find number of "ticks" to wait to achieve target */
146 /* finish normalize. */
148 /* else small number, don't kill it prior to HZ multiply */
149 tmo = usec * CONFIG_SYS_HZ;
154 /* get current timestamp */
155 if ((tmo + tmp + 1) < tmp)
156 /* if setting this fordward will roll time stamp */
157 reset_timer_masked();
158 /* reset "advancing" timestamp to 0, set lastdec value */
161 /* else, set advancing stamp wake up time */
163 while (get_timer_masked() < tmo)/* loop till event */
167 void reset_timer_masked(void)
170 lastdec = READ_TIMER; /* capure current decrementer value time */
171 timestamp = 0; /* start "advancing" time stamp from 0 */
174 ulong get_timer_masked(void)
176 ulong now = READ_TIMER; /* current tick value */
178 if (lastdec >= now) { /* normal mode (non roll) */
180 timestamp += lastdec - now;
181 /* move stamp fordward with absoulte diff ticks */
183 /* we have overflow of the count down timer */
184 /* nts = ts + ld + (TLV - now)
185 * ts=old stamp, ld=time that passed before passing through -1
186 * (TLV-now) amount of time after passing though -1
187 * nts = new "advancing time stamp"...it could also roll
188 * and cause problems.
190 timestamp += lastdec + TIMER_LOAD_VAL - now + 1;
197 /* waits specified delay value and resets timestamp */
198 void udelay_masked(unsigned long usec)
205 /* if "big" number, spread normalization to seconds */
207 /* start to normalize for usec to ticks per sec */
208 tmo *= CONFIG_SYS_HZ;
209 /* find number of "ticks" to wait to achieve target */
211 /* finish normalize. */
213 /* else small number, don't kill it prior to HZ multiply */
214 tmo = usec * CONFIG_SYS_HZ;
218 endtime = get_timer_masked() + tmo;
221 ulong now = get_timer_masked();
222 diff = endtime - now;
227 * This function is derived from PowerPC code (read timebase as long long).
228 * On ARM it just returns the timer value.
230 unsigned long long get_ticks(void)
236 * This function is derived from PowerPC code (timebase clock frequency).
237 * On ARM it returns the number of timer ticks per second.
239 ulong get_tbclk(void)
243 tbclk = CONFIG_SYS_HZ;