blob: f2e362102e6653d6a50c1313a2141a49c6855d9c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Macpaul Lin83dbca72011-10-11 22:33:18 +00002/*
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>
Macpaul Lin83dbca72011-10-11 22:33:18 +00009 */
rick6d564b82017-05-17 10:59:20 +080010#ifndef CONFIG_TIMER
Macpaul Lin83dbca72011-10-11 22:33:18 +000011#include <common.h>
Simon Glass9b61c7c2019-11-14 12:57:41 -070012#include <irq_func.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070013#include <time.h>
Macpaul Lin83dbca72011-10-11 22:33:18 +000014#include <asm/io.h>
15#include <faraday/fttmr010.h>
16
17static ulong timestamp;
18static ulong lastdec;
19
20int timer_init(void)
21{
Macpaul Linf4a7b802011-11-23 13:56:50 +080022 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Macpaul Lin83dbca72011-10-11 22:33:18 +000023 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 */
67void reset_timer_masked(void)
68{
Macpaul Linf4a7b802011-11-23 13:56:50 +080069 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Macpaul Lin83dbca72011-10-11 22:33:18 +000070
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
Axel Lin55edeb52013-07-08 14:29:52 +080075 lastdec = readl(&tmr->timer3_counter) /
76 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
Macpaul Lin83dbca72011-10-11 22:33:18 +000077#endif
78 timestamp = 0; /* start "advancing" time stamp from 0 */
79
80 debug("%s(): lastdec = %lx\n", __func__, lastdec);
81}
82
83void reset_timer(void)
84{
85 debug("%s()\n", __func__);
86 reset_timer_masked();
87}
88
89/*
90 * return timer ticks
91 */
92ulong get_timer_masked(void)
93{
Macpaul Linf4a7b802011-11-23 13:56:50 +080094 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Macpaul Lin83dbca72011-10-11 22:33:18 +000095
96 /* current tick value */
97#ifdef CONFIG_FTTMR010_EXT_CLK
98 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
99#else
Axel Lin55edeb52013-07-08 14:29:52 +0800100 ulong now = readl(&tmr->timer3_counter) /
101 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
Macpaul Lin83dbca72011-10-11 22:33:18 +0000102#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 */
135ulong get_timer(ulong base)
136{
137 debug("%s(%lx)\n", __func__, base);
138 return get_timer_masked() - base;
139}
140
141void 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 */
148void __udelay(unsigned long usec)
149{
Macpaul Linf4a7b802011-11-23 13:56:50 +0800150 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Macpaul Lin83dbca72011-10-11 22:33:18 +0000151
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 */
174unsigned 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 */
184ulong 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}
rick6d564b82017-05-17 10:59:20 +0800193#endif /* CONFIG_TIMER */