]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/arm926ejs/mxs/timer.c
TX6 Release 2013-04-22
[karo-tx-uboot.git] / arch / arm / cpu / arm926ejs / mxs / timer.c
1 /*
2  * Freescale i.MX28 timer driver
3  *
4  * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5  * on behalf of DENX Software Engineering GmbH
6  *
7  * Based on code from LTIB:
8  * (C) Copyright 2009-2010 Freescale Semiconductor, Inc.
9  *
10  * See file CREDITS for list of people who contributed to this
11  * project.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of
16  * the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26  * MA 02111-1307 USA
27  */
28
29 #include <common.h>
30 #include <asm/io.h>
31 #include <asm/arch/imx-regs.h>
32 #include <asm/arch/sys_proto.h>
33
34 /* Maximum fixed count */
35 #define TIMER_LOAD_VAL  0xffffffff
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 /* Enable this to verify that the code can correctly
40  * handle the timer rollover
41  */
42 /* #define DEBUG_TIMER_WRAP */
43
44 #ifdef DEBUG_TIMER_WRAP
45 /*
46  * Let the timer wrap 15 seconds after start to catch misbehaving
47  * timer related code early
48  */
49 #define TIMER_START             (-time_to_tick(15 * CONFIG_SYS_HZ))
50 #else
51 #define TIMER_START             0UL
52 #endif
53
54 /*
55  * This driver uses 1kHz clock source.
56  */
57 #define MXS_INCREMENTER_HZ              1000
58
59 static inline unsigned long tick_to_time(unsigned long tick)
60 {
61         return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
62 }
63
64 static inline unsigned long time_to_tick(unsigned long time)
65 {
66         return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
67 }
68
69 int timer_init(void)
70 {
71         struct mxs_timrot_regs *timrot_regs =
72                 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
73
74         /* Reset Timers and Rotary Encoder module */
75         mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
76
77         /* Set fixed_count to 0 */
78         writel(0, &timrot_regs->hw_timrot_fixed_count0);
79
80         /* Set UPDATE bit and 1Khz frequency */
81         writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
82                 TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
83                 &timrot_regs->hw_timrot_timctrl0);
84
85 #ifndef DEBUG_TIMER_WRAP
86         /* Set fixed_count to maximum value */
87         writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
88 #else
89         /* Set fixed_count so that the counter will wrap after 20 seconds */
90         writel(20 * MXS_INCREMENTER_HZ,
91                 &timrot_regs->hw_timrot_fixed_count0);
92         gd->arch.lastinc = TIMER_LOAD_VAL - 20 * MXS_INCREMENTER_HZ;
93 #endif
94 #ifdef DEBUG_TIMER_WRAP
95         /* Make the usec counter roll over 30 seconds after startup */
96         writel(-30000000, MXS_HW_DIGCTL_MICROSECONDS);
97 #endif
98         writel(TIMROT_TIMCTRLn_UPDATE,
99                 &timrot_regs->hw_timrot_timctrl0_clr);
100 #ifdef DEBUG_TIMER_WRAP
101         /* Set fixed_count to maximal value for subsequent loads */
102         writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
103 #endif
104         gd->arch.timer_rate_hz = MXS_INCREMENTER_HZ;
105         gd->arch.tbl = TIMER_START;
106         gd->arch.tbu = 0;
107         return 0;
108 }
109
110 /* Note: This function works correctly for TIMER_LOAD_VAL == 0xffffffff!
111  * The rollover is handled automagically due to the properties of
112  * two's complement arithmetic.
113  * For any other value of TIMER_LOAD_VAL the calculations would have
114  * to be done modulus(TIMER_LOAD_VAL + 1).
115  */
116 unsigned long long get_ticks(void)
117 {
118         struct mxs_timrot_regs *timrot_regs =
119                 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
120         /* The timer is counting down, so subtract the register value from
121          * the counter period length to get an incrementing timestamp
122          */
123         unsigned long now = -readl(&timrot_regs->hw_timrot_running_count0);
124         ulong inc = now - gd->arch.lastinc;
125
126         gd->arch.tbl += inc;
127         gd->arch.lastinc = now;
128         /* Since the get_timer() function only uses a 32bit value
129          * it doesn't make sense to return a real 64 bit value here.
130          */
131         return gd->arch.tbl;
132 }
133
134 ulong get_timer_masked(void)
135 {
136         return tick_to_time(get_ticks());
137 }
138
139 ulong get_timer(ulong base)
140 {
141         /* NOTE: time_to_tick(base) is required to correctly handle rollover! */
142         return tick_to_time(get_ticks() - time_to_tick(base));
143 }
144
145 /* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
146 #define MXS_HW_DIGCTL_MICROSECONDS      0x8001c0c0
147
148 void __udelay(unsigned long usec)
149 {
150         uint32_t start = readl(MXS_HW_DIGCTL_MICROSECONDS);
151
152         while (readl(MXS_HW_DIGCTL_MICROSECONDS) - start <= usec)
153                 /* use '<=' to guarantee a delay of _at least_
154                  * the given number of microseconds.
155                  * No need for fancy rollover checks
156                  * Two's complement arithmetic applied correctly
157                  * does everything that's needed  automagically!
158                  */
159                 ;
160 }
161
162 ulong get_tbclk(void)
163 {
164         return gd->arch.timer_rate_hz;
165 }