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>
18 #include <linux/delay.h>
20 static ulong timestamp;
25 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
28 debug("%s()\n", __func__);
33 #ifdef CONFIG_FTTMR010_EXT_CLK
34 /* use 32768Hz oscillator for RTC, WDT, TIMER */
35 ftpmu010_32768osc_enable();
39 writel(TIMER_LOAD_VAL, &tmr->timer3_load);
40 writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
41 writel(0, &tmr->timer3_match1);
42 writel(0, &tmr->timer3_match2);
44 /* we don't want timer to issue interrupts */
45 writel(FTTMR010_TM3_MATCH1 |
47 FTTMR010_TM3_OVERFLOW,
48 &tmr->interrupt_mask);
51 #ifdef CONFIG_FTTMR010_EXT_CLK
52 cr |= FTTMR010_TM3_CLOCK; /* use external clock */
54 cr |= FTTMR010_TM3_ENABLE;
57 /* init the timestamp and lastdec value */
64 * timer without interrupts
70 void reset_timer_masked(void)
72 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
74 /* capure current decrementer value time */
75 #ifdef CONFIG_FTTMR010_EXT_CLK
76 lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
78 lastdec = readl(&tmr->timer3_counter) /
79 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
81 timestamp = 0; /* start "advancing" time stamp from 0 */
83 debug("%s(): lastdec = %lx\n", __func__, lastdec);
86 void reset_timer(void)
88 debug("%s()\n", __func__);
95 ulong get_timer_masked(void)
97 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
99 /* current tick value */
100 #ifdef CONFIG_FTTMR010_EXT_CLK
101 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
103 ulong now = readl(&tmr->timer3_counter) /
104 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
107 debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
109 if (lastdec >= now) {
111 * normal mode (non roll)
112 * move stamp fordward with absoulte diff ticks
114 timestamp += lastdec - now;
117 * we have overflow of the count down timer
119 * nts = ts + ld + (TLV - now)
120 * ts=old stamp, ld=time that passed before passing through -1
121 * (TLV-now) amount of time after passing though -1
122 * nts = new "advancing time stamp"...it could also roll and
125 timestamp += lastdec + TIMER_LOAD_VAL - now;
130 debug("%s() returns %lx\n", __func__, timestamp);
136 * return difference between timer ticks and base
138 ulong get_timer(ulong base)
140 debug("%s(%lx)\n", __func__, base);
141 return get_timer_masked() - base;
144 void set_timer(ulong t)
146 debug("%s(%lx)\n", __func__, t);
150 /* delay x useconds AND preserve advance timestamp value */
151 void __udelay(unsigned long usec)
153 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
155 #ifdef CONFIG_FTTMR010_EXT_CLK
156 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
158 long tmo = usec * ((CONFIG_SYS_CLK_FREQ / 2) / 1000) / 1000;
160 unsigned long now, last = readl(&tmr->timer3_counter);
162 debug("%s(%lu)\n", __func__, usec);
164 now = readl(&tmr->timer3_counter);
165 if (now > last) /* count down timer overflow */
166 tmo -= TIMER_LOAD_VAL + last - now;
174 * This function is derived from PowerPC code (read timebase as long long).
175 * On ARM it just returns the timer value.
177 unsigned long long get_ticks(void)
179 debug("%s()\n", __func__);
184 * This function is derived from PowerPC code (timebase clock frequency).
185 * On ARM it returns the number of timer ticks per second.
187 ulong get_tbclk(void)
189 debug("%s()\n", __func__);
190 #ifdef CONFIG_FTTMR010_EXT_CLK
191 return CONFIG_SYS_HZ;
193 return CONFIG_SYS_CLK_FREQ;
196 #endif /* CONFIG_TIMER */