• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2009 Faraday Technology
4  * Po-Yu Chuang <ratbert@faraday-tech.com>
5  *
6  * Copyright (C) 2011 Andes Technology Corporation
7  * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
8  * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
9  */
10 #ifndef CONFIG_TIMER
11 #include <common.h>
12 #include <irq_func.h>
13 #include <time.h>
14 #include <asm/io.h>
15 #include <faraday/fttmr010.h>
16 
17 static ulong timestamp;
18 static ulong lastdec;
19 
timer_init(void)20 int timer_init(void)
21 {
22 	struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
23 	unsigned int cr;
24 
25 	debug("%s()\n", __func__);
26 
27 	/* disable timers */
28 	writel(0, &tmr->cr);
29 
30 #ifdef CONFIG_FTTMR010_EXT_CLK
31 	/* use 32768Hz oscillator for RTC, WDT, TIMER */
32 	ftpmu010_32768osc_enable();
33 #endif
34 
35 	/* setup timer */
36 	writel(TIMER_LOAD_VAL, &tmr->timer3_load);
37 	writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
38 	writel(0, &tmr->timer3_match1);
39 	writel(0, &tmr->timer3_match2);
40 
41 	/* we don't want timer to issue interrupts */
42 	writel(FTTMR010_TM3_MATCH1 |
43 	       FTTMR010_TM3_MATCH2 |
44 	       FTTMR010_TM3_OVERFLOW,
45 	       &tmr->interrupt_mask);
46 
47 	cr = readl(&tmr->cr);
48 #ifdef CONFIG_FTTMR010_EXT_CLK
49 	cr |= FTTMR010_TM3_CLOCK;	/* use external clock */
50 #endif
51 	cr |= FTTMR010_TM3_ENABLE;
52 	writel(cr, &tmr->cr);
53 
54 	/* init the timestamp and lastdec value */
55 	reset_timer_masked();
56 
57 	return 0;
58 }
59 
60 /*
61  * timer without interrupts
62  */
63 
64 /*
65  * reset time
66  */
reset_timer_masked(void)67 void reset_timer_masked(void)
68 {
69 	struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
70 
71 	/* capure current decrementer value time */
72 #ifdef CONFIG_FTTMR010_EXT_CLK
73 	lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
74 #else
75 	lastdec = readl(&tmr->timer3_counter) /
76 			(CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
77 #endif
78 	timestamp = 0;		/* start "advancing" time stamp from 0 */
79 
80 	debug("%s(): lastdec = %lx\n", __func__, lastdec);
81 }
82 
reset_timer(void)83 void reset_timer(void)
84 {
85 	debug("%s()\n", __func__);
86 	reset_timer_masked();
87 }
88 
89 /*
90  * return timer ticks
91  */
get_timer_masked(void)92 ulong get_timer_masked(void)
93 {
94 	struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
95 
96 	/* current tick value */
97 #ifdef CONFIG_FTTMR010_EXT_CLK
98 	ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
99 #else
100 	ulong now = readl(&tmr->timer3_counter) /
101 			(CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
102 #endif
103 
104 	debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
105 
106 	if (lastdec >= now) {
107 		/*
108 		 * normal mode (non roll)
109 		 * move stamp fordward with absoulte diff ticks
110 		 */
111 		timestamp += lastdec - now;
112 	} else {
113 		/*
114 		 * we have overflow of the count down timer
115 		 *
116 		 * nts = ts + ld + (TLV - now)
117 		 * ts=old stamp, ld=time that passed before passing through -1
118 		 * (TLV-now) amount of time after passing though -1
119 		 * nts = new "advancing time stamp"...it could also roll and
120 		 * cause problems.
121 		 */
122 		timestamp += lastdec + TIMER_LOAD_VAL - now;
123 	}
124 
125 	lastdec = now;
126 
127 	debug("%s() returns %lx\n", __func__, timestamp);
128 
129 	return timestamp;
130 }
131 
132 /*
133  * return difference between timer ticks and base
134  */
get_timer(ulong base)135 ulong get_timer(ulong base)
136 {
137 	debug("%s(%lx)\n", __func__, base);
138 	return get_timer_masked() - base;
139 }
140 
set_timer(ulong t)141 void set_timer(ulong t)
142 {
143 	debug("%s(%lx)\n", __func__, t);
144 	timestamp = t;
145 }
146 
147 /* delay x useconds AND preserve advance timestamp value */
__udelay(unsigned long usec)148 void __udelay(unsigned long usec)
149 {
150 	struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
151 
152 #ifdef CONFIG_FTTMR010_EXT_CLK
153 	long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
154 #else
155 	long tmo = usec * ((CONFIG_SYS_CLK_FREQ / 2) / 1000) / 1000;
156 #endif
157 	unsigned long now, last = readl(&tmr->timer3_counter);
158 
159 	debug("%s(%lu)\n", __func__, usec);
160 	while (tmo > 0) {
161 		now = readl(&tmr->timer3_counter);
162 		if (now > last) /* count down timer overflow */
163 			tmo -= TIMER_LOAD_VAL + last - now;
164 		else
165 			tmo -= last - now;
166 		last = now;
167 	}
168 }
169 
170 /*
171  * This function is derived from PowerPC code (read timebase as long long).
172  * On ARM it just returns the timer value.
173  */
get_ticks(void)174 unsigned long long get_ticks(void)
175 {
176 	debug("%s()\n", __func__);
177 	return get_timer(0);
178 }
179 
180 /*
181  * This function is derived from PowerPC code (timebase clock frequency).
182  * On ARM it returns the number of timer ticks per second.
183  */
get_tbclk(void)184 ulong get_tbclk(void)
185 {
186 	debug("%s()\n", __func__);
187 #ifdef CONFIG_FTTMR010_EXT_CLK
188 	return CONFIG_SYS_HZ;
189 #else
190 	return CONFIG_SYS_CLK_FREQ;
191 #endif
192 }
193 #endif /* CONFIG_TIMER */
194