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>
16 #include <faraday/fttmr010.h>
18 static ulong timestamp;
23 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
26 debug("%s()\n", __func__);
31 #ifdef CONFIG_FTTMR010_EXT_CLK
32 /* use 32768Hz oscillator for RTC, WDT, TIMER */
33 ftpmu010_32768osc_enable();
37 writel(TIMER_LOAD_VAL, &tmr->timer3_load);
38 writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
39 writel(0, &tmr->timer3_match1);
40 writel(0, &tmr->timer3_match2);
42 /* we don't want timer to issue interrupts */
43 writel(FTTMR010_TM3_MATCH1 |
45 FTTMR010_TM3_OVERFLOW,
46 &tmr->interrupt_mask);
49 #ifdef CONFIG_FTTMR010_EXT_CLK
50 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
52 cr |= FTTMR010_TM3_ENABLE;
55 /* init the timestamp and lastdec value */
62 * timer without interrupts
68 void reset_timer_masked(void)
70 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
72 /* capure current decrementer value time */
73 #ifdef CONFIG_FTTMR010_EXT_CLK
74 lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
76 lastdec = readl(&tmr->timer3_counter) /
77 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
79 timestamp = 0; /* start "advancing" time stamp from 0 */
81 debug("%s(): lastdec = %lx\n", __func__, lastdec);
84 void reset_timer(void)
86 debug("%s()\n", __func__);
93 ulong get_timer_masked(void)
95 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
97 /* current tick value */
98 #ifdef CONFIG_FTTMR010_EXT_CLK
99 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
101 ulong now = readl(&tmr->timer3_counter) /
102 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
105 debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
107 if (lastdec >= now) {
109 * normal mode (non roll)
110 * move stamp fordward with absoulte diff ticks
112 timestamp += lastdec - now;
115 * we have overflow of the count down timer
117 * nts = ts + ld + (TLV - now)
118 * ts=old stamp, ld=time that passed before passing through -1
119 * (TLV-now) amount of time after passing though -1
120 * nts = new "advancing time stamp"...it could also roll and
123 timestamp += lastdec + TIMER_LOAD_VAL - now;
128 debug("%s() returns %lx\n", __func__, timestamp);
134 * return difference between timer ticks and base
136 ulong get_timer(ulong base)
138 debug("%s(%lx)\n", __func__, base);
139 return get_timer_masked() - base;
142 void set_timer(ulong t)
144 debug("%s(%lx)\n", __func__, t);
148 /* delay x useconds AND preserve advance timestamp value */
149 void __udelay(unsigned long usec)
151 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
153 #ifdef CONFIG_FTTMR010_EXT_CLK
154 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
156 long tmo = usec * ((CONFIG_SYS_CLK_FREQ / 2) / 1000) / 1000;
158 unsigned long now, last = readl(&tmr->timer3_counter);
160 debug("%s(%lu)\n", __func__, usec);
162 now = readl(&tmr->timer3_counter);
163 if (now > last) /* count down timer overflow */
164 tmo -= TIMER_LOAD_VAL + last - now;
172 * This function is derived from PowerPC code (read timebase as long long).
173 * On ARM it just returns the timer value.
175 unsigned long long get_ticks(void)
177 debug("%s()\n", __func__);
182 * This function is derived from PowerPC code (timebase clock frequency).
183 * On ARM it returns the number of timer ticks per second.
185 ulong get_tbclk(void)
187 debug("%s()\n", __func__);
188 #ifdef CONFIG_FTTMR010_EXT_CLK
189 return CONFIG_SYS_HZ;
191 return CONFIG_SYS_CLK_FREQ;
194 #endif /* CONFIG_TIMER */