blob: b373e59e6fe3cc06a7cab5046bfb8cbd68e6a0a7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Albert Aribaudac2ba9e2010-06-17 19:36:07 +05302/*
Albert ARIBAUD340983d2011-04-22 19:41:02 +02003 * Copyright (C) 2010 Albert ARIBAUD <albert.u.boot@aribaud.net>
Albert Aribaudac2ba9e2010-06-17 19:36:07 +05304 *
5 * Based on original Kirkwood support which is
6 * Copyright (C) Marvell International Ltd. and its affiliates
7 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
Albert Aribaudac2ba9e2010-06-17 19:36:07 +05308 */
9
10#include <common.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070012#include <time.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Lei Wen749941a2011-10-24 16:27:32 +000014#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Albert Aribaudac2ba9e2010-06-17 19:36:07 +053016
17#define UBOOT_CNTR 0 /* counter to use for uboot timer */
18
19/* Timer reload and current value registers */
20struct orion5x_tmr_val {
21 u32 reload; /* Timer reload reg */
22 u32 val; /* Timer value reg */
23};
24
25/* Timer registers */
26struct orion5x_tmr_registers {
27 u32 ctrl; /* Timer control reg */
28 u32 pad[3];
29 struct orion5x_tmr_val tmr[2];
30 u32 wdt_reload;
31 u32 wdt_val;
32};
33
34struct orion5x_tmr_registers *orion5x_tmr_regs =
35 (struct orion5x_tmr_registers *)ORION5X_TIMER_BASE;
36
37/*
38 * ARM Timers Registers Map
39 */
40#define CNTMR_CTRL_REG (&orion5x_tmr_regs->ctrl)
41#define CNTMR_RELOAD_REG(tmrnum) (&orion5x_tmr_regs->tmr[tmrnum].reload)
42#define CNTMR_VAL_REG(tmrnum) (&orion5x_tmr_regs->tmr[tmrnum].val)
43
44/*
45 * ARM Timers Control Register
46 * CPU_TIMERS_CTRL_REG (CTCR)
47 */
48#define CTCR_ARM_TIMER_EN_OFFS(cntr) (cntr * 2)
49#define CTCR_ARM_TIMER_EN_MASK(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS)
50#define CTCR_ARM_TIMER_EN(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS(cntr))
51#define CTCR_ARM_TIMER_DIS(cntr) (0 << CTCR_ARM_TIMER_EN_OFFS(cntr))
52
53#define CTCR_ARM_TIMER_AUTO_OFFS(cntr) ((cntr * 2) + 1)
54#define CTCR_ARM_TIMER_AUTO_MASK(cntr) (1 << 1)
55#define CTCR_ARM_TIMER_AUTO_EN(cntr) (1 << CTCR_ARM_TIMER_AUTO_OFFS(cntr))
56#define CTCR_ARM_TIMER_AUTO_DIS(cntr) (0 << CTCR_ARM_TIMER_AUTO_OFFS(cntr))
57
58/*
59 * ARM Timer\Watchdog Reload Register
60 * CNTMR_RELOAD_REG (TRR)
61 */
62#define TRG_ARM_TIMER_REL_OFFS 0
63#define TRG_ARM_TIMER_REL_MASK 0xffffffff
64
65/*
66 * ARM Timer\Watchdog Register
67 * CNTMR_VAL_REG (TVRG)
68 */
69#define TVR_ARM_TIMER_OFFS 0
70#define TVR_ARM_TIMER_MASK 0xffffffff
71#define TVR_ARM_TIMER_MAX 0xffffffff
Wolfgang Denk62fb2b42021-09-27 17:42:39 +020072#define TIMER_LOAD_VAL 0xffffffff
Albert Aribaudac2ba9e2010-06-17 19:36:07 +053073
74static inline ulong read_timer(void)
75{
76 return readl(CNTMR_VAL_REG(UBOOT_CNTR))
Tom Rini6a5dccc2022-11-16 13:10:41 -050077 / (CFG_SYS_TCLK / 1000);
Albert Aribaudac2ba9e2010-06-17 19:36:07 +053078}
79
Heiko Schocher5504dab2011-01-20 22:56:39 +000080DECLARE_GLOBAL_DATA_PTR;
81
Simon Glass2655ee12012-12-13 20:48:34 +000082#define timestamp gd->arch.tbl
Simon Glassa848da52012-12-13 20:48:35 +000083#define lastdec gd->arch.lastinc
Albert Aribaudac2ba9e2010-06-17 19:36:07 +053084
Patrick Delaunay9858a602018-10-05 11:33:52 +020085static ulong get_timer_masked(void)
Albert Aribaudac2ba9e2010-06-17 19:36:07 +053086{
87 ulong now = read_timer();
88
89 if (lastdec >= now) {
90 /* normal mode */
91 timestamp += lastdec - now;
92 } else {
93 /* we have an overflow ... */
94 timestamp += lastdec +
Tom Rini6a5dccc2022-11-16 13:10:41 -050095 (TIMER_LOAD_VAL / (CFG_SYS_TCLK / 1000)) - now;
Albert Aribaudac2ba9e2010-06-17 19:36:07 +053096 }
97 lastdec = now;
98
99 return timestamp;
100}
101
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530102ulong get_timer(ulong base)
103{
104 return get_timer_masked() - base;
105}
106
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530107static inline ulong uboot_cntr_val(void)
108{
109 return readl(CNTMR_VAL_REG(UBOOT_CNTR));
110}
111
112void __udelay(unsigned long usec)
113{
114 uint current;
115 ulong delayticks;
116
117 current = uboot_cntr_val();
Tom Rini6a5dccc2022-11-16 13:10:41 -0500118 delayticks = (usec * (CFG_SYS_TCLK / 1000000));
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530119
120 if (current < delayticks) {
121 delayticks -= current;
122 while (uboot_cntr_val() < current)
123 ;
124 while ((TIMER_LOAD_VAL - delayticks) < uboot_cntr_val())
125 ;
126 } else {
127 while (uboot_cntr_val() > (current - delayticks))
128 ;
129 }
130}
131
132/*
133 * init the counter
134 */
135int timer_init(void)
136{
137 unsigned int cntmrctrl;
138
139 /* load value into timer */
140 writel(TIMER_LOAD_VAL, CNTMR_RELOAD_REG(UBOOT_CNTR));
141 writel(TIMER_LOAD_VAL, CNTMR_VAL_REG(UBOOT_CNTR));
142
143 /* enable timer in auto reload mode */
144 cntmrctrl = readl(CNTMR_CTRL_REG);
145 cntmrctrl |= CTCR_ARM_TIMER_EN(UBOOT_CNTR);
146 cntmrctrl |= CTCR_ARM_TIMER_AUTO_EN(UBOOT_CNTR);
147 writel(cntmrctrl, CNTMR_CTRL_REG);
Albert Aribaudfd5f9732010-09-23 21:49:23 +0200148 return 0;
149}
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530150
Albert Aribaudfd5f9732010-09-23 21:49:23 +0200151void timer_init_r(void)
152{
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530153 /* init the timestamp and lastdec value */
Graeme Russ944a7fe2011-07-15 02:21:14 +0000154 lastdec = read_timer();
155 timestamp = 0;
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530156}
Prafulla Wadaskarf4ce6ad2012-02-08 14:15:53 +0530157
158/*
159 * This function is derived from PowerPC code (read timebase as long long).
160 * On ARM it just returns the timer value.
161 */
162unsigned long long get_ticks(void)
163{
164 return get_timer(0);
165}
166
167/*
168 * This function is derived from PowerPC code (timebase clock frequency).
169 * On ARM it returns the number of timer ticks per second.
170 */
Simon Glassa9dc0682019-12-28 10:44:59 -0700171ulong get_tbclk(void)
Prafulla Wadaskarf4ce6ad2012-02-08 14:15:53 +0530172{
173 return (ulong)CONFIG_SYS_HZ;
174}