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