blob: 8208b6de90c0dbbdcfdf3d9317ce1bb6ac1138d9 [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 Glass97589732020-05-10 11:40:02 -060012#include <init.h>
Simon Glass9b61c7c2019-11-14 12:57:41 -070013#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070015#include <time.h>
Macpaul Lin83dbca72011-10-11 22:33:18 +000016#include <asm/io.h>
17#include <faraday/fttmr010.h>
18
19static ulong timestamp;
20static ulong lastdec;
21
22int timer_init(void)
23{
Macpaul Linf4a7b802011-11-23 13:56:50 +080024 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Macpaul Lin83dbca72011-10-11 22:33:18 +000025 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 */
69void reset_timer_masked(void)
70{
Macpaul Linf4a7b802011-11-23 13:56:50 +080071 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Macpaul Lin83dbca72011-10-11 22:33:18 +000072
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
Axel Lin55edeb52013-07-08 14:29:52 +080077 lastdec = readl(&tmr->timer3_counter) /
78 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
Macpaul Lin83dbca72011-10-11 22:33:18 +000079#endif
80 timestamp = 0; /* start "advancing" time stamp from 0 */
81
82 debug("%s(): lastdec = %lx\n", __func__, lastdec);
83}
84
85void reset_timer(void)
86{
87 debug("%s()\n", __func__);
88 reset_timer_masked();
89}
90
91/*
92 * return timer ticks
93 */
94ulong get_timer_masked(void)
95{
Macpaul Linf4a7b802011-11-23 13:56:50 +080096 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Macpaul Lin83dbca72011-10-11 22:33:18 +000097
98 /* current tick value */
99#ifdef CONFIG_FTTMR010_EXT_CLK
100 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
101#else
Axel Lin55edeb52013-07-08 14:29:52 +0800102 ulong now = readl(&tmr->timer3_counter) /
103 (CONFIG_SYS_CLK_FREQ / 2 / CONFIG_SYS_HZ);
Macpaul Lin83dbca72011-10-11 22:33:18 +0000104#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 */
137ulong get_timer(ulong base)
138{
139 debug("%s(%lx)\n", __func__, base);
140 return get_timer_masked() - base;
141}
142
143void 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 */
150void __udelay(unsigned long usec)
151{
Macpaul Linf4a7b802011-11-23 13:56:50 +0800152 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
Macpaul Lin83dbca72011-10-11 22:33:18 +0000153
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 */
176unsigned 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 */
186ulong 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}
rick6d564b82017-05-17 10:59:20 +0800195#endif /* CONFIG_TIMER */