1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2009 Faraday Technology
4 * Po-Yu Chuang <ratbert@faraday-tech.com>
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>
17 #include <faraday/fttmr010.h>
19 static ulong timestamp;
24 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
27 debug("%s()\n", __func__);
32 #ifdef CONFIG_FTTMR010_EXT_CLK
33 /* use 32768Hz oscillator for RTC, WDT, TIMER */
34 ftpmu010_32768osc_enable();
38 writel(TIMER_LOAD_VAL, &tmr->timer3_load);
39 writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
40 writel(0, &tmr->timer3_match1);
41 writel(0, &tmr->timer3_match2);
43 /* we don't want timer to issue interrupts */
44 writel(FTTMR010_TM3_MATCH1 |
46 FTTMR010_TM3_OVERFLOW,
47 &tmr->interrupt_mask);
50 #ifdef CONFIG_FTTMR010_EXT_CLK
51 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
53 cr |= FTTMR010_TM3_ENABLE;
56 /* init the timestamp and lastdec value */
63 * timer without interrupts
69 void reset_timer_masked(void)
71 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
73 /* capure current decrementer value time */
74 #ifdef CONFIG_FTTMR010_EXT_CLK
75 lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
77 lastdec = readl(&tmr->timer3_counter) /
78 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
80 timestamp = 0; /* start "advancing" time stamp from 0 */
82 debug("%s(): lastdec = %lx\n", __func__, lastdec);
85 void reset_timer(void)
87 debug("%s()\n", __func__);
94 ulong get_timer_masked(void)
96 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
98 /* current tick value */
99 #ifdef CONFIG_FTTMR010_EXT_CLK
100 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
102 ulong now = readl(&tmr->timer3_counter) /
103 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
106 debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
108 if (lastdec >= now) {
110 * normal mode (non roll)
111 * move stamp fordward with absoulte diff ticks
113 timestamp += lastdec - now;
116 * we have overflow of the count down timer
118 * nts = ts + ld + (TLV - now)
119 * ts=old stamp, ld=time that passed before passing through -1
120 * (TLV-now) amount of time after passing though -1
121 * nts = new "advancing time stamp"...it could also roll and
124 timestamp += lastdec + TIMER_LOAD_VAL - now;
129 debug("%s() returns %lx\n", __func__, timestamp);
135 * return difference between timer ticks and base
137 ulong get_timer(ulong base)
139 debug("%s(%lx)\n", __func__, base);
140 return get_timer_masked() - base;
143 void set_timer(ulong t)
145 debug("%s(%lx)\n", __func__, t);
149 /* delay x useconds AND preserve advance timestamp value */
150 void __udelay(unsigned long usec)
152 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
154 #ifdef CONFIG_FTTMR010_EXT_CLK
155 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
157 long tmo = usec * ((CONFIG_SYS_CLK_FREQ / 2) / 1000) / 1000;
159 unsigned long now, last = readl(&tmr->timer3_counter);
161 debug("%s(%lu)\n", __func__, usec);
163 now = readl(&tmr->timer3_counter);
164 if (now > last) /* count down timer overflow */
165 tmo -= TIMER_LOAD_VAL + last - now;
173 * This function is derived from PowerPC code (read timebase as long long).
174 * On ARM it just returns the timer value.
176 unsigned long long get_ticks(void)
178 debug("%s()\n", __func__);
183 * This function is derived from PowerPC code (timebase clock frequency).
184 * On ARM it returns the number of timer ticks per second.
186 ulong get_tbclk(void)
188 debug("%s()\n", __func__);
189 #ifdef CONFIG_FTTMR010_EXT_CLK
190 return CONFIG_SYS_HZ;
192 return CONFIG_SYS_CLK_FREQ;
195 #endif /* CONFIG_TIMER */