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