]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/arm926ejs/mxs/timer.c
mtd: nand: mxc: use driver from Ka-Ro branch
[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  * SPDX-License-Identifier:     GPL-2.0+ 
11  */
12
13 #include <common.h>
14 #include <asm/io.h>
15 #include <asm/arch/imx-regs.h>
16 #include <asm/arch/sys_proto.h>
17
18 /* Maximum fixed count */
19 #if defined(CONFIG_MX23)
20 #define TIMER_LOAD_VAL 0xffff
21 #elif defined(CONFIG_MX28)
22 #define TIMER_LOAD_VAL 0xffffffff
23 #endif
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 /* Enable this to verify that the code can correctly
28  * handle the timer rollover
29  */
30 /* #define DEBUG_TIMER_WRAP */
31
32 #ifdef DEBUG_TIMER_WRAP
33 /*
34  * Let the timer wrap 15 seconds after start to catch misbehaving
35  * timer related code early
36  */
37 #define TIMER_START             (-time_to_tick(15 * CONFIG_SYS_HZ))
38 #else
39 #define TIMER_START             0UL
40 #endif
41
42 /*
43  * This driver uses 1kHz clock source.
44  */
45 #define MXS_INCREMENTER_HZ              1000
46
47 static inline unsigned long tick_to_time(unsigned long tick)
48 {
49         return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
50 }
51
52 static inline unsigned long time_to_tick(unsigned long time)
53 {
54         return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
55 }
56
57 int timer_init(void)
58 {
59         struct mxs_timrot_regs *timrot_regs =
60                 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
61
62         /* Reset Timers and Rotary Encoder module */
63         mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
64
65         /* Set fixed_count to 0 */
66 #if defined(CONFIG_MX23)
67         writel(0, &timrot_regs->hw_timrot_timcount0);
68 #elif defined(CONFIG_MX28)
69         writel(0, &timrot_regs->hw_timrot_fixed_count0);
70 #endif
71
72         /* Set UPDATE bit and 1Khz frequency */
73         writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
74                 TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
75                 &timrot_regs->hw_timrot_timctrl0);
76
77         /* Set fixed_count to maximal value */
78 #if defined(CONFIG_MX23)
79         writel(TIMER_LOAD_VAL - 1, &timrot_regs->hw_timrot_timcount0);
80 #elif defined(CONFIG_MX28)
81         writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
82 #endif
83
84 #ifndef DEBUG_TIMER_WRAP
85         /* Set fixed_count to maximum value */
86         writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
87 #else
88         /* Set fixed_count so that the counter will wrap after 20 seconds */
89         writel(20 * MXS_INCREMENTER_HZ,
90                 &timrot_regs->hw_timrot_fixed_count0);
91         gd->arch.lastinc = TIMER_LOAD_VAL - 20 * MXS_INCREMENTER_HZ;
92 #endif
93 #ifdef DEBUG_TIMER_WRAP
94         /* Make the usec counter roll over 30 seconds after startup */
95         writel(-30000000, MXS_HW_DIGCTL_MICROSECONDS);
96 #endif
97         writel(TIMROT_TIMCTRLn_UPDATE,
98                 &timrot_regs->hw_timrot_timctrl0_clr);
99 #ifdef DEBUG_TIMER_WRAP
100         /* Set fixed_count to maximal value for subsequent loads */
101         writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
102 #endif
103         gd->arch.timer_rate_hz = MXS_INCREMENTER_HZ;
104         gd->arch.tbl = TIMER_START;
105         gd->arch.tbu = 0;
106         return 0;
107 }
108
109 /* Note: This function works correctly for TIMER_LOAD_VAL == 0xffffffff!
110  * The rollover is handled automagically due to the properties of
111  * two's complement arithmetic.
112  * For any other value of TIMER_LOAD_VAL the calculations would have
113  * to be done modulus(TIMER_LOAD_VAL + 1).
114  */
115 unsigned long long get_ticks(void)
116 {
117         struct mxs_timrot_regs *timrot_regs =
118                 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
119         unsigned long now;
120 #if defined(CONFIG_MX23)
121         /* Upper bits are the valid ones. */
122         now = readl(&timrot_regs->hw_timrot_timcount0) >>
123                 TIMROT_RUNNING_COUNTn_RUNNING_COUNT_OFFSET;
124 #else
125         /* The timer is counting down, so subtract the register value from
126          * the counter period length (implicitly 2^32) to get an incrementing
127          * timestamp
128          */
129         now = -readl(&timrot_regs->hw_timrot_running_count0);
130 #endif
131         ulong inc = now - gd->arch.lastinc;
132
133         if (gd->arch.tbl + inc < gd->arch.tbl)
134                 gd->arch.tbu++;
135         gd->arch.tbl += inc;
136         gd->arch.lastinc = now;
137         return ((unsigned long long)gd->arch.tbu << 32) | gd->arch.tbl;
138 }
139
140 ulong get_timer_masked(void)
141 {
142         return tick_to_time(get_ticks());
143 }
144
145 ulong get_timer(ulong base)
146 {
147         /* NOTE: time_to_tick(base) is required to correctly handle rollover! */
148         return tick_to_time(get_ticks() - time_to_tick(base));
149 }
150
151 /* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
152 #define MXS_HW_DIGCTL_MICROSECONDS      0x8001c0c0
153
154 void __udelay(unsigned long usec)
155 {
156         uint32_t start = readl(MXS_HW_DIGCTL_MICROSECONDS);
157
158         while (readl(MXS_HW_DIGCTL_MICROSECONDS) - start <= usec)
159                 /* use '<=' to guarantee a delay of _at least_
160                  * the given number of microseconds.
161                  * No need for fancy rollover checks
162                  * Two's complement arithmetic applied correctly
163                  * does everything that's needed  automagically!
164                  */
165                 ;
166 }
167
168 ulong get_tbclk(void)
169 {
170         return gd->arch.timer_rate_hz;
171 }