blob: 4926bb7779606e48b90a8c939b59b5a5f35d2ba4 [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>
Lei Wen749941a2011-10-24 16:27:32 +000013#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Albert Aribaudac2ba9e2010-06-17 19:36:07 +053015
16#define UBOOT_CNTR 0 /* counter to use for uboot timer */
17
18/* Timer reload and current value registers */
19struct orion5x_tmr_val {
20 u32 reload; /* Timer reload reg */
21 u32 val; /* Timer value reg */
22};
23
24/* Timer registers */
25struct orion5x_tmr_registers {
26 u32 ctrl; /* Timer control reg */
27 u32 pad[3];
28 struct orion5x_tmr_val tmr[2];
29 u32 wdt_reload;
30 u32 wdt_val;
31};
32
33struct orion5x_tmr_registers *orion5x_tmr_regs =
34 (struct orion5x_tmr_registers *)ORION5X_TIMER_BASE;
35
36/*
37 * ARM Timers Registers Map
38 */
39#define CNTMR_CTRL_REG (&orion5x_tmr_regs->ctrl)
40#define CNTMR_RELOAD_REG(tmrnum) (&orion5x_tmr_regs->tmr[tmrnum].reload)
41#define CNTMR_VAL_REG(tmrnum) (&orion5x_tmr_regs->tmr[tmrnum].val)
42
43/*
44 * ARM Timers Control Register
45 * CPU_TIMERS_CTRL_REG (CTCR)
46 */
47#define CTCR_ARM_TIMER_EN_OFFS(cntr) (cntr * 2)
48#define CTCR_ARM_TIMER_EN_MASK(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS)
49#define CTCR_ARM_TIMER_EN(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS(cntr))
50#define CTCR_ARM_TIMER_DIS(cntr) (0 << CTCR_ARM_TIMER_EN_OFFS(cntr))
51
52#define CTCR_ARM_TIMER_AUTO_OFFS(cntr) ((cntr * 2) + 1)
53#define CTCR_ARM_TIMER_AUTO_MASK(cntr) (1 << 1)
54#define CTCR_ARM_TIMER_AUTO_EN(cntr) (1 << CTCR_ARM_TIMER_AUTO_OFFS(cntr))
55#define CTCR_ARM_TIMER_AUTO_DIS(cntr) (0 << CTCR_ARM_TIMER_AUTO_OFFS(cntr))
56
57/*
58 * ARM Timer\Watchdog Reload Register
59 * CNTMR_RELOAD_REG (TRR)
60 */
61#define TRG_ARM_TIMER_REL_OFFS 0
62#define TRG_ARM_TIMER_REL_MASK 0xffffffff
63
64/*
65 * ARM Timer\Watchdog Register
66 * CNTMR_VAL_REG (TVRG)
67 */
68#define TVR_ARM_TIMER_OFFS 0
69#define TVR_ARM_TIMER_MASK 0xffffffff
70#define TVR_ARM_TIMER_MAX 0xffffffff
71#define TIMER_LOAD_VAL 0xffffffff
72
73static inline ulong read_timer(void)
74{
75 return readl(CNTMR_VAL_REG(UBOOT_CNTR))
76 / (CONFIG_SYS_TCLK / 1000);
77}
78
Heiko Schocher5504dab2011-01-20 22:56:39 +000079DECLARE_GLOBAL_DATA_PTR;
80
Simon Glass2655ee12012-12-13 20:48:34 +000081#define timestamp gd->arch.tbl
Simon Glassa848da52012-12-13 20:48:35 +000082#define lastdec gd->arch.lastinc
Albert Aribaudac2ba9e2010-06-17 19:36:07 +053083
Patrick Delaunay9858a602018-10-05 11:33:52 +020084static ulong get_timer_masked(void)
Albert Aribaudac2ba9e2010-06-17 19:36:07 +053085{
86 ulong now = read_timer();
87
88 if (lastdec >= now) {
89 /* normal mode */
90 timestamp += lastdec - now;
91 } else {
92 /* we have an overflow ... */
93 timestamp += lastdec +
94 (TIMER_LOAD_VAL / (CONFIG_SYS_TCLK / 1000)) - now;
95 }
96 lastdec = now;
97
98 return timestamp;
99}
100
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530101ulong get_timer(ulong base)
102{
103 return get_timer_masked() - base;
104}
105
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530106static inline ulong uboot_cntr_val(void)
107{
108 return readl(CNTMR_VAL_REG(UBOOT_CNTR));
109}
110
111void __udelay(unsigned long usec)
112{
113 uint current;
114 ulong delayticks;
115
116 current = uboot_cntr_val();
117 delayticks = (usec * (CONFIG_SYS_TCLK / 1000000));
118
119 if (current < delayticks) {
120 delayticks -= current;
121 while (uboot_cntr_val() < current)
122 ;
123 while ((TIMER_LOAD_VAL - delayticks) < uboot_cntr_val())
124 ;
125 } else {
126 while (uboot_cntr_val() > (current - delayticks))
127 ;
128 }
129}
130
131/*
132 * init the counter
133 */
134int timer_init(void)
135{
136 unsigned int cntmrctrl;
137
138 /* load value into timer */
139 writel(TIMER_LOAD_VAL, CNTMR_RELOAD_REG(UBOOT_CNTR));
140 writel(TIMER_LOAD_VAL, CNTMR_VAL_REG(UBOOT_CNTR));
141
142 /* enable timer in auto reload mode */
143 cntmrctrl = readl(CNTMR_CTRL_REG);
144 cntmrctrl |= CTCR_ARM_TIMER_EN(UBOOT_CNTR);
145 cntmrctrl |= CTCR_ARM_TIMER_AUTO_EN(UBOOT_CNTR);
146 writel(cntmrctrl, CNTMR_CTRL_REG);
Albert Aribaudfd5f9732010-09-23 21:49:23 +0200147 return 0;
148}
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530149
Albert Aribaudfd5f9732010-09-23 21:49:23 +0200150void timer_init_r(void)
151{
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530152 /* init the timestamp and lastdec value */
Graeme Russ944a7fe2011-07-15 02:21:14 +0000153 lastdec = read_timer();
154 timestamp = 0;
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530155}
Prafulla Wadaskarf4ce6ad2012-02-08 14:15:53 +0530156
157/*
158 * This function is derived from PowerPC code (read timebase as long long).
159 * On ARM it just returns the timer value.
160 */
161unsigned long long get_ticks(void)
162{
163 return get_timer(0);
164}
165
166/*
167 * This function is derived from PowerPC code (timebase clock frequency).
168 * On ARM it returns the number of timer ticks per second.
169 */
Simon Glassa9dc0682019-12-28 10:44:59 -0700170ulong get_tbclk(void)
Prafulla Wadaskarf4ce6ad2012-02-08 14:15:53 +0530171{
172 return (ulong)CONFIG_SYS_HZ;
173}