]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/imx-common/timer.c
i.MX video: use already defined CONFIG_IPUV3_CLK instead of CONFIG_IPU_CLKRATE
[karo-tx-uboot.git] / arch / arm / imx-common / timer.c
1 /*
2  * (C) Copyright 2007
3  * Sascha Hauer, Pengutronix
4  *
5  * (C) Copyright 2009 Freescale Semiconductor, Inc.
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 <div64.h>
29 #include <asm/arch/imx-regs.h>
30 #include <asm/arch/clock.h>
31
32 #define DEBUG_TIMER_WRAP
33
34 /* General purpose timers registers */
35 struct mxc_gpt {
36         unsigned int control;
37         unsigned int prescaler;
38         unsigned int status;
39         unsigned int nouse[6];
40         unsigned int counter;
41 };
42
43 static struct mxc_gpt *cur_gpt = (struct mxc_gpt *)GPT1_BASE_ADDR;
44
45 /* General purpose timers bitfields */
46 #define GPTCR_SWR               (1 << 15)       /* Software reset */
47 #define GPTCR_FRR               (1 << 9)        /* Freerun / restart */
48 #define GPTCR_CLKSOURCE_IPG     (1 << 6)        /* Clock source */
49 #define GPTCR_CLKSOURCE_CKIH    (2 << 6)
50 #define GPTCR_CLKSOURCE_32kHz   (4 << 6)
51 #if defined(CONFIG_MX6Q)
52 #define GPTCR_CLKSOURCE_OSCDIV8 (5 << 6)
53 #define GPTCR_CLKSOURCE_OSC     (7 << 6)
54 #elif defined(CONFIG_MX6DL)
55 #define GPTCR_M24EN             (1 << 10)
56 #define GPTCR_CLKSOURCE_OSC     ((5 << 6) | GPTCR_M24EN)
57 #else
58 #define GPTCR_CLKSOURCE_OSC     (5 << 6)
59 #endif
60 #define GPTCR_CLKSOURCE_MASK    (7 << 6)
61 #define GPTCR_TEN               1               /* Timer enable */
62
63 #if 1
64 #define GPT_CLKSOURCE           GPTCR_CLKSOURCE_OSC
65 #define GPT_REFCLK              24000000
66 #define GPT_PRESCALER           24
67 #else
68 #define GPT_CLKSOURCE           GPTCR_CLKSOURCE_32kHz
69 #define GPT_REFCLK              32768
70 #define GPT_PRESCALER           1
71 #endif
72 #define GPT_CLK                 (GPT_REFCLK / GPT_PRESCALER)
73
74 #ifdef DEBUG_TIMER_WRAP
75 /*
76  * Let the timer wrap 30 seconds after start to catch misbehaving
77  * timer related code early
78  */
79 #define TIMER_START             (-time_to_tick(30 * CONFIG_SYS_HZ))
80 #else
81 #define TIMER_START             0UL
82 #endif
83
84 DECLARE_GLOBAL_DATA_PTR;
85
86 static inline unsigned long tick_to_time(unsigned long tick)
87 {
88         unsigned long long t = (unsigned long long)tick * CONFIG_SYS_HZ;
89         do_div(t, GPT_CLK);
90         return t;
91 }
92
93 static inline unsigned long time_to_tick(unsigned long time)
94 {
95         unsigned long long ticks = (unsigned long long)time;
96
97         ticks *= GPT_CLK;
98         do_div(ticks, CONFIG_SYS_HZ);
99         return ticks;
100 }
101
102 static inline unsigned long us_to_tick(unsigned long usec)
103 {
104         unsigned long long ticks = (unsigned long long)usec;
105
106         ticks *= GPT_CLK;
107         do_div(ticks, 1000 * CONFIG_SYS_HZ);
108         return ticks;
109 }
110
111 int timer_init(void)
112 {
113         int i;
114
115         /* setup GP Timer 1 */
116         __raw_writel(GPTCR_SWR, &cur_gpt->control);
117
118         /* We have no udelay by now */
119         for (i = 0; i < 100; i++)
120                 __raw_writel(0, &cur_gpt->control);
121
122         __raw_writel(GPT_PRESCALER - 1, &cur_gpt->prescaler);
123
124         /* Freerun Mode, PERCLK1 input */
125         i = __raw_readl(&cur_gpt->control);
126         i &= ~GPTCR_CLKSOURCE_MASK;
127         __raw_writel(i | GPT_CLKSOURCE | GPTCR_TEN, &cur_gpt->control);
128
129         gd->arch.lastinc = __raw_readl(&cur_gpt->counter);
130         gd->arch.tbu = 0;
131         gd->arch.tbl = TIMER_START;
132         gd->arch.timer_rate_hz = GPT_CLK;
133
134         return 0;
135 }
136
137 unsigned long long get_ticks(void)
138 {
139         ulong now = __raw_readl(&cur_gpt->counter); /* current tick value */
140         ulong inc = now - gd->arch.lastinc;
141
142         gd->arch.tbl += inc;
143         gd->arch.lastinc = now;
144         return gd->arch.tbl;
145 }
146
147 ulong get_timer_masked(void)
148 {
149         /*
150          * get_ticks() returns a long long (64 bit), it wraps in
151          * 2^64 / MXC_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
152          * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
153          * 5 * 10^6 days - long enough.
154          */
155         /*
156          * LW: get_ticks() returns a long long with the top 32 bits always ZERO!
157          * Thus the calculation above is not true.
158          * A 64bit timer value would only make sense if it was
159          * consistently used throughout the code. Thus also the parameter
160          * to get_timer() and its return value would need to be 64bit wide!
161          */
162         return tick_to_time(get_ticks());
163 }
164
165 ulong get_timer(ulong base)
166 {
167         return tick_to_time(get_ticks() - time_to_tick(base));
168 }
169
170 #include <asm/gpio.h>
171
172 /* delay x useconds AND preserve advance timstamp value */
173 void __udelay(unsigned long usec)
174 {
175         unsigned long start = __raw_readl(&cur_gpt->counter);
176         unsigned long ticks;
177
178         if (usec == 0)
179                 return;
180
181         ticks = us_to_tick(usec);
182         if (ticks == 0)
183                 ticks++;
184
185         while (__raw_readl(&cur_gpt->counter) - start < ticks)
186                 /* loop till event */;
187 }
188
189 /*
190  * This function is derived from PowerPC code (timebase clock frequency).
191  * On ARM it returns the number of timer ticks per second.
192  */
193 ulong get_tbclk(void)
194 {
195         return gd->arch.timer_rate_hz;
196 }