193588ea10bb149e0bb8907bc3c64bad95ebc34f
[oweals/u-boot.git] / arch / arm / cpu / arm926ejs / mx27 / timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2002
4  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
5  * Marius Groeger <mgroeger@sysgo.de>
6  *
7  * (C) Copyright 2002
8  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9  * Alex Zuepke <azu@sysgo.de>
10  *
11  * (C) Copyright 2002
12  * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
13  *
14  * (C) Copyright 2009
15  * Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com>
16  */
17
18 #include <common.h>
19 #include <div64.h>
20 #include <init.h>
21 #include <time.h>
22 #include <asm/io.h>
23 #include <asm/arch/imx-regs.h>
24
25 /* General purpose timers bitfields */
26 #define GPTCR_SWR               (1 << 15)       /* Software reset       */
27 #define GPTCR_FRR               (1 << 8)        /* Freerun / restart    */
28 #define GPTCR_CLKSOURCE_32      (4 << 1)        /* Clock source         */
29 #define GPTCR_TEN               1               /* Timer enable         */
30
31 DECLARE_GLOBAL_DATA_PTR;
32
33 #define timestamp       (gd->arch.tbl)
34 #define lastinc         (gd->arch.lastinc)
35
36 /*
37  * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
38  * "tick" is internal timer period
39  */
40 #ifdef CONFIG_MX27_TIMER_HIGH_PRECISION
41 /* ~0.4% error - measured with stop-watch on 100s boot-delay */
42 static inline unsigned long long tick_to_time(unsigned long long tick)
43 {
44         tick *= CONFIG_SYS_HZ;
45         do_div(tick, CONFIG_MX27_CLK32);
46         return tick;
47 }
48
49 static inline unsigned long long time_to_tick(unsigned long long time)
50 {
51         time *= CONFIG_MX27_CLK32;
52         do_div(time, CONFIG_SYS_HZ);
53         return time;
54 }
55
56 static inline unsigned long long us_to_tick(unsigned long long us)
57 {
58         us = us * CONFIG_MX27_CLK32 + 999999;
59         do_div(us, 1000000);
60         return us;
61 }
62 #else
63 /* ~2% error */
64 #define TICK_PER_TIME   ((CONFIG_MX27_CLK32 + CONFIG_SYS_HZ / 2) / \
65                 CONFIG_SYS_HZ)
66 #define US_PER_TICK     (1000000 / CONFIG_MX27_CLK32)
67
68 static inline unsigned long long tick_to_time(unsigned long long tick)
69 {
70         do_div(tick, TICK_PER_TIME);
71         return tick;
72 }
73
74 static inline unsigned long long time_to_tick(unsigned long long time)
75 {
76         return time * TICK_PER_TIME;
77 }
78
79 static inline unsigned long long us_to_tick(unsigned long long us)
80 {
81         us += US_PER_TICK - 1;
82         do_div(us, US_PER_TICK);
83         return us;
84 }
85 #endif
86
87 /* nothing really to do with interrupts, just starts up a counter. */
88 /* The 32768Hz 32-bit timer overruns in 131072 seconds */
89 int timer_init(void)
90 {
91         int i;
92         struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
93         struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
94
95         /* setup GP Timer 1 */
96         writel(GPTCR_SWR, &regs->gpt_tctl);
97
98         writel(readl(&pll->pccr0) | PCCR0_GPT1_EN, &pll->pccr0);
99         writel(readl(&pll->pccr1) | PCCR1_PERCLK1_EN, &pll->pccr1);
100
101         for (i = 0; i < 100; i++)
102                 writel(0, &regs->gpt_tctl); /* We have no udelay by now */
103         writel(0, &regs->gpt_tprer); /* 32Khz */
104         /* Freerun Mode, PERCLK1 input */
105         writel(readl(&regs->gpt_tctl) | GPTCR_CLKSOURCE_32 | GPTCR_FRR,
106                         &regs->gpt_tctl);
107         writel(readl(&regs->gpt_tctl) | GPTCR_TEN, &regs->gpt_tctl);
108
109         return 0;
110 }
111
112 unsigned long long get_ticks(void)
113 {
114         struct gpt_regs *regs = (struct gpt_regs *)IMX_TIM1_BASE;
115         ulong now = readl(&regs->gpt_tcn); /* current tick value */
116
117         if (now >= lastinc) {
118                 /*
119                  * normal mode (non roll)
120                  * move stamp forward with absolut diff ticks
121                  */
122                 timestamp += (now - lastinc);
123         } else {
124                 /* we have rollover of incrementer */
125                 timestamp += (0xFFFFFFFF - lastinc) + now;
126         }
127         lastinc = now;
128         return timestamp;
129 }
130
131 static ulong get_timer_masked(void)
132 {
133         /*
134          * get_ticks() returns a long long (64 bit), it wraps in
135          * 2^64 / CONFIG_MX27_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
136          * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
137          * 5 * 10^6 days - long enough.
138          */
139         return tick_to_time(get_ticks());
140 }
141
142 ulong get_timer(ulong base)
143 {
144         return get_timer_masked() - base;
145 }
146
147 /* delay x useconds AND preserve advance timstamp value */
148 void __udelay(unsigned long usec)
149 {
150         unsigned long long tmp;
151         ulong tmo;
152
153         tmo = us_to_tick(usec);
154         tmp = get_ticks() + tmo;        /* get current timestamp */
155
156         while (get_ticks() < tmp)       /* loop till event */
157                  /*NOP*/;
158 }
159
160 ulong get_tbclk(void)
161 {
162         return CONFIG_MX27_CLK32;
163 }