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>
15 #include <faraday/fttmr010.h>
17 static ulong timestamp;
22 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
25 debug("%s()\n", __func__);
30 #ifdef CONFIG_FTTMR010_EXT_CLK
31 /* use 32768Hz oscillator for RTC, WDT, TIMER */
32 ftpmu010_32768osc_enable();
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);
41 /* we don't want timer to issue interrupts */
42 writel(FTTMR010_TM3_MATCH1 |
44 FTTMR010_TM3_OVERFLOW,
45 &tmr->interrupt_mask);
48 #ifdef CONFIG_FTTMR010_EXT_CLK
49 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
51 cr |= FTTMR010_TM3_ENABLE;
54 /* init the timestamp and lastdec value */
61 * timer without interrupts
67 void reset_timer_masked(void)
69 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
71 /* capure current decrementer value time */
72 #ifdef CONFIG_FTTMR010_EXT_CLK
73 lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
75 lastdec = readl(&tmr->timer3_counter) /
76 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
78 timestamp = 0; /* start "advancing" time stamp from 0 */
80 debug("%s(): lastdec = %lx\n", __func__, lastdec);
83 void reset_timer(void)
85 debug("%s()\n", __func__);
92 ulong get_timer_masked(void)
94 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
96 /* current tick value */
97 #ifdef CONFIG_FTTMR010_EXT_CLK
98 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
100 ulong now = readl(&tmr->timer3_counter) /
101 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
104 debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
106 if (lastdec >= now) {
108 * normal mode (non roll)
109 * move stamp fordward with absoulte diff ticks
111 timestamp += lastdec - now;
114 * we have overflow of the count down timer
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
122 timestamp += lastdec + TIMER_LOAD_VAL - now;
127 debug("%s() returns %lx\n", __func__, timestamp);
133 * return difference between timer ticks and base
135 ulong get_timer(ulong base)
137 debug("%s(%lx)\n", __func__, base);
138 return get_timer_masked() - base;
141 void set_timer(ulong t)
143 debug("%s(%lx)\n", __func__, t);
147 /* delay x useconds AND preserve advance timestamp value */
148 void __udelay(unsigned long usec)
150 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
152 #ifdef CONFIG_FTTMR010_EXT_CLK
153 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
155 long tmo = usec * ((CONFIG_SYS_CLK_FREQ / 2) / 1000) / 1000;
157 unsigned long now, last = readl(&tmr->timer3_counter);
159 debug("%s(%lu)\n", __func__, usec);
161 now = readl(&tmr->timer3_counter);
162 if (now > last) /* count down timer overflow */
163 tmo -= TIMER_LOAD_VAL + last - now;
171 * This function is derived from PowerPC code (read timebase as long long).
172 * On ARM it just returns the timer value.
174 unsigned long long get_ticks(void)
176 debug("%s()\n", __func__);
181 * This function is derived from PowerPC code (timebase clock frequency).
182 * On ARM it returns the number of timer ticks per second.
184 ulong get_tbclk(void)
186 debug("%s()\n", __func__);
187 #ifdef CONFIG_FTTMR010_EXT_CLK
188 return CONFIG_SYS_HZ;
190 return CONFIG_SYS_CLK_FREQ;
193 #endif /* CONFIG_TIMER */