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