b6fdc88be7e5aed94cc196fa3db6bebbac17f4e4
[oweals/u-boot.git] / arch / arm / cpu / arm926ejs / mxs / timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Freescale i.MX28 timer driver
4  *
5  * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
6  * on behalf of DENX Software Engineering GmbH
7  *
8  * Based on code from LTIB:
9  * (C) Copyright 2009-2010 Freescale Semiconductor, Inc.
10  */
11
12 #include <common.h>
13 #include <init.h>
14 #include <time.h>
15 #include <asm/io.h>
16 #include <asm/arch/imx-regs.h>
17 #include <asm/arch/sys_proto.h>
18
19 /* Maximum fixed count */
20 #if defined(CONFIG_MX23)
21 #define TIMER_LOAD_VAL 0xffff
22 #elif defined(CONFIG_MX28)
23 #define TIMER_LOAD_VAL 0xffffffff
24 #endif
25
26 DECLARE_GLOBAL_DATA_PTR;
27
28 #define timestamp (gd->arch.tbl)
29 #define lastdec (gd->arch.lastinc)
30
31 /*
32  * This driver uses 1kHz clock source.
33  */
34 #define MXS_INCREMENTER_HZ              1000
35
36 static inline unsigned long tick_to_time(unsigned long tick)
37 {
38         return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
39 }
40
41 static inline unsigned long time_to_tick(unsigned long time)
42 {
43         return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
44 }
45
46 /* Calculate how many ticks happen in "us" microseconds */
47 static inline unsigned long us_to_tick(unsigned long us)
48 {
49         return (us * MXS_INCREMENTER_HZ) / 1000000;
50 }
51
52 int timer_init(void)
53 {
54         struct mxs_timrot_regs *timrot_regs =
55                 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
56
57         /* Reset Timers and Rotary Encoder module */
58         mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
59
60         /* Set fixed_count to 0 */
61 #if defined(CONFIG_MX23)
62         writel(0, &timrot_regs->hw_timrot_timcount0);
63 #elif defined(CONFIG_MX28)
64         writel(0, &timrot_regs->hw_timrot_fixed_count0);
65 #endif
66
67         /* Set UPDATE bit and 1Khz frequency */
68         writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
69                 TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
70                 &timrot_regs->hw_timrot_timctrl0);
71
72         /* Set fixed_count to maximal value */
73 #if defined(CONFIG_MX23)
74         writel(TIMER_LOAD_VAL - 1, &timrot_regs->hw_timrot_timcount0);
75 #elif defined(CONFIG_MX28)
76         writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
77 #endif
78
79         return 0;
80 }
81
82 unsigned long long get_ticks(void)
83 {
84         struct mxs_timrot_regs *timrot_regs =
85                 (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
86         uint32_t now;
87
88         /* Current tick value */
89 #if defined(CONFIG_MX23)
90         /* Upper bits are the valid ones. */
91         now = readl(&timrot_regs->hw_timrot_timcount0) >>
92                 TIMROT_RUNNING_COUNTn_RUNNING_COUNT_OFFSET;
93 #elif defined(CONFIG_MX28)
94         now = readl(&timrot_regs->hw_timrot_running_count0);
95 #else
96 #error "Don't know how to read timrot_regs"
97 #endif
98
99         if (lastdec >= now) {
100                 /*
101                  * normal mode (non roll)
102                  * move stamp forward with absolut diff ticks
103                  */
104                 timestamp += (lastdec - now);
105         } else {
106                 /* we have rollover of decrementer */
107                 timestamp += (TIMER_LOAD_VAL - now) + lastdec;
108
109         }
110         lastdec = now;
111
112         return timestamp;
113 }
114
115 ulong get_timer(ulong base)
116 {
117         return tick_to_time(get_ticks()) - base;
118 }
119
120 /* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
121 #define MXS_HW_DIGCTL_MICROSECONDS      0x8001c0c0
122
123 void __udelay(unsigned long usec)
124 {
125         uint32_t old, new, incr;
126         uint32_t counter = 0;
127
128         old = readl(MXS_HW_DIGCTL_MICROSECONDS);
129
130         while (counter < usec) {
131                 new = readl(MXS_HW_DIGCTL_MICROSECONDS);
132
133                 /* Check if the timer wrapped. */
134                 if (new < old) {
135                         incr = 0xffffffff - old;
136                         incr += new;
137                 } else {
138                         incr = new - old;
139                 }
140
141                 /*
142                  * Check if we are close to the maximum time and the counter
143                  * would wrap if incremented. If that's the case, break out
144                  * from the loop as the requested delay time passed.
145                  */
146                 if (counter + incr < counter)
147                         break;
148
149                 counter += incr;
150                 old = new;
151         }
152 }
153
154 ulong get_tbclk(void)
155 {
156         return MXS_INCREMENTER_HZ;
157 }