common: Drop init.h from common header
[pandora-u-boot.git] / arch / nds32 / cpu / n1213 / ag101 / timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
9  */
10 #ifndef CONFIG_TIMER
11 #include <common.h>
12 #include <init.h>
13 #include <irq_func.h>
14 #include <time.h>
15 #include <asm/io.h>
16 #include <faraday/fttmr010.h>
17
18 static ulong timestamp;
19 static ulong lastdec;
20
21 int timer_init(void)
22 {
23         struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
24         unsigned int cr;
25
26         debug("%s()\n", __func__);
27
28         /* disable timers */
29         writel(0, &tmr->cr);
30
31 #ifdef CONFIG_FTTMR010_EXT_CLK
32         /* use 32768Hz oscillator for RTC, WDT, TIMER */
33         ftpmu010_32768osc_enable();
34 #endif
35
36         /* setup timer */
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);
41
42         /* we don't want timer to issue interrupts */
43         writel(FTTMR010_TM3_MATCH1 |
44                FTTMR010_TM3_MATCH2 |
45                FTTMR010_TM3_OVERFLOW,
46                &tmr->interrupt_mask);
47
48         cr = readl(&tmr->cr);
49 #ifdef CONFIG_FTTMR010_EXT_CLK
50         cr |= FTTMR010_TM3_CLOCK;       /* use external clock */
51 #endif
52         cr |= FTTMR010_TM3_ENABLE;
53         writel(cr, &tmr->cr);
54
55         /* init the timestamp and lastdec value */
56         reset_timer_masked();
57
58         return 0;
59 }
60
61 /*
62  * timer without interrupts
63  */
64
65 /*
66  * reset time
67  */
68 void reset_timer_masked(void)
69 {
70         struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
71
72         /* capure current decrementer value time */
73 #ifdef CONFIG_FTTMR010_EXT_CLK
74         lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
75 #else
76         lastdec = readl(&tmr->timer3_counter) /
77                         (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
78 #endif
79         timestamp = 0;          /* start "advancing" time stamp from 0 */
80
81         debug("%s(): lastdec = %lx\n", __func__, lastdec);
82 }
83
84 void reset_timer(void)
85 {
86         debug("%s()\n", __func__);
87         reset_timer_masked();
88 }
89
90 /*
91  * return timer ticks
92  */
93 ulong get_timer_masked(void)
94 {
95         struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
96
97         /* current tick value */
98 #ifdef CONFIG_FTTMR010_EXT_CLK
99         ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
100 #else
101         ulong now = readl(&tmr->timer3_counter) /
102                         (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
103 #endif
104
105         debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
106
107         if (lastdec >= now) {
108                 /*
109                  * normal mode (non roll)
110                  * move stamp fordward with absoulte diff ticks
111                  */
112                 timestamp += lastdec - now;
113         } else {
114                 /*
115                  * we have overflow of the count down timer
116                  *
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
121                  * cause problems.
122                  */
123                 timestamp += lastdec + TIMER_LOAD_VAL - now;
124         }
125
126         lastdec = now;
127
128         debug("%s() returns %lx\n", __func__, timestamp);
129
130         return timestamp;
131 }
132
133 /*
134  * return difference between timer ticks and base
135  */
136 ulong get_timer(ulong base)
137 {
138         debug("%s(%lx)\n", __func__, base);
139         return get_timer_masked() - base;
140 }
141
142 void set_timer(ulong t)
143 {
144         debug("%s(%lx)\n", __func__, t);
145         timestamp = t;
146 }
147
148 /* delay x useconds AND preserve advance timestamp value */
149 void __udelay(unsigned long usec)
150 {
151         struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
152
153 #ifdef CONFIG_FTTMR010_EXT_CLK
154         long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
155 #else
156         long tmo = usec * ((CONFIG_SYS_CLK_FREQ / 2) / 1000) / 1000;
157 #endif
158         unsigned long now, last = readl(&tmr->timer3_counter);
159
160         debug("%s(%lu)\n", __func__, usec);
161         while (tmo > 0) {
162                 now = readl(&tmr->timer3_counter);
163                 if (now > last) /* count down timer overflow */
164                         tmo -= TIMER_LOAD_VAL + last - now;
165                 else
166                         tmo -= last - now;
167                 last = now;
168         }
169 }
170
171 /*
172  * This function is derived from PowerPC code (read timebase as long long).
173  * On ARM it just returns the timer value.
174  */
175 unsigned long long get_ticks(void)
176 {
177         debug("%s()\n", __func__);
178         return get_timer(0);
179 }
180
181 /*
182  * This function is derived from PowerPC code (timebase clock frequency).
183  * On ARM it returns the number of timer ticks per second.
184  */
185 ulong get_tbclk(void)
186 {
187         debug("%s()\n", __func__);
188 #ifdef CONFIG_FTTMR010_EXT_CLK
189         return CONFIG_SYS_HZ;
190 #else
191         return CONFIG_SYS_CLK_FREQ;
192 #endif
193 }
194 #endif /* CONFIG_TIMER */