blob: ec4f6bee8e74d0227ece3463c88c4b297a0aaea8 [file] [log] [blame]
Albert Aribaud0c61e6f2010-06-17 19:36:07 +05301/*
Albert ARIBAUD57b4bce2011-04-22 19:41:02 +02002 * Copyright (C) 2010 Albert ARIBAUD <albert.u.boot@aribaud.net>
Albert Aribaud0c61e6f2010-06-17 19:36:07 +05303 *
4 * Based on original Kirkwood support which is
5 * Copyright (C) Marvell International Ltd. and its affiliates
6 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Albert Aribaud0c61e6f2010-06-17 19:36:07 +05309 */
10
11#include <common.h>
Lei Wen5ff8b352011-10-24 16:27:32 +000012#include <asm/io.h>
Albert Aribaud0c61e6f2010-06-17 19:36:07 +053013
14#define UBOOT_CNTR 0 /* counter to use for uboot timer */
15
16/* Timer reload and current value registers */
17struct orion5x_tmr_val {
18 u32 reload; /* Timer reload reg */
19 u32 val; /* Timer value reg */
20};
21
22/* Timer registers */
23struct orion5x_tmr_registers {
24 u32 ctrl; /* Timer control reg */
25 u32 pad[3];
26 struct orion5x_tmr_val tmr[2];
27 u32 wdt_reload;
28 u32 wdt_val;
29};
30
31struct orion5x_tmr_registers *orion5x_tmr_regs =
32 (struct orion5x_tmr_registers *)ORION5X_TIMER_BASE;
33
34/*
35 * ARM Timers Registers Map
36 */
37#define CNTMR_CTRL_REG (&orion5x_tmr_regs->ctrl)
38#define CNTMR_RELOAD_REG(tmrnum) (&orion5x_tmr_regs->tmr[tmrnum].reload)
39#define CNTMR_VAL_REG(tmrnum) (&orion5x_tmr_regs->tmr[tmrnum].val)
40
41/*
42 * ARM Timers Control Register
43 * CPU_TIMERS_CTRL_REG (CTCR)
44 */
45#define CTCR_ARM_TIMER_EN_OFFS(cntr) (cntr * 2)
46#define CTCR_ARM_TIMER_EN_MASK(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS)
47#define CTCR_ARM_TIMER_EN(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS(cntr))
48#define CTCR_ARM_TIMER_DIS(cntr) (0 << CTCR_ARM_TIMER_EN_OFFS(cntr))
49
50#define CTCR_ARM_TIMER_AUTO_OFFS(cntr) ((cntr * 2) + 1)
51#define CTCR_ARM_TIMER_AUTO_MASK(cntr) (1 << 1)
52#define CTCR_ARM_TIMER_AUTO_EN(cntr) (1 << CTCR_ARM_TIMER_AUTO_OFFS(cntr))
53#define CTCR_ARM_TIMER_AUTO_DIS(cntr) (0 << CTCR_ARM_TIMER_AUTO_OFFS(cntr))
54
55/*
56 * ARM Timer\Watchdog Reload Register
57 * CNTMR_RELOAD_REG (TRR)
58 */
59#define TRG_ARM_TIMER_REL_OFFS 0
60#define TRG_ARM_TIMER_REL_MASK 0xffffffff
61
62/*
63 * ARM Timer\Watchdog Register
64 * CNTMR_VAL_REG (TVRG)
65 */
66#define TVR_ARM_TIMER_OFFS 0
67#define TVR_ARM_TIMER_MASK 0xffffffff
68#define TVR_ARM_TIMER_MAX 0xffffffff
69#define TIMER_LOAD_VAL 0xffffffff
70
71static inline ulong read_timer(void)
72{
73 return readl(CNTMR_VAL_REG(UBOOT_CNTR))
74 / (CONFIG_SYS_TCLK / 1000);
75}
76
Heiko Schocherc9ac3ba2011-01-20 22:56:39 +000077DECLARE_GLOBAL_DATA_PTR;
78
Simon Glass66ee6922012-12-13 20:48:34 +000079#define timestamp gd->arch.tbl
Simon Glass582601d2012-12-13 20:48:35 +000080#define lastdec gd->arch.lastinc
Albert Aribaud0c61e6f2010-06-17 19:36:07 +053081
Albert Aribaud0c61e6f2010-06-17 19:36:07 +053082ulong get_timer_masked(void)
83{
84 ulong now = read_timer();
85
86 if (lastdec >= now) {
87 /* normal mode */
88 timestamp += lastdec - now;
89 } else {
90 /* we have an overflow ... */
91 timestamp += lastdec +
92 (TIMER_LOAD_VAL / (CONFIG_SYS_TCLK / 1000)) - now;
93 }
94 lastdec = now;
95
96 return timestamp;
97}
98
Albert Aribaud0c61e6f2010-06-17 19:36:07 +053099ulong get_timer(ulong base)
100{
101 return get_timer_masked() - base;
102}
103
Albert Aribaud0c61e6f2010-06-17 19:36:07 +0530104static inline ulong uboot_cntr_val(void)
105{
106 return readl(CNTMR_VAL_REG(UBOOT_CNTR));
107}
108
109void __udelay(unsigned long usec)
110{
111 uint current;
112 ulong delayticks;
113
114 current = uboot_cntr_val();
115 delayticks = (usec * (CONFIG_SYS_TCLK / 1000000));
116
117 if (current < delayticks) {
118 delayticks -= current;
119 while (uboot_cntr_val() < current)
120 ;
121 while ((TIMER_LOAD_VAL - delayticks) < uboot_cntr_val())
122 ;
123 } else {
124 while (uboot_cntr_val() > (current - delayticks))
125 ;
126 }
127}
128
129/*
130 * init the counter
131 */
132int timer_init(void)
133{
134 unsigned int cntmrctrl;
135
136 /* load value into timer */
137 writel(TIMER_LOAD_VAL, CNTMR_RELOAD_REG(UBOOT_CNTR));
138 writel(TIMER_LOAD_VAL, CNTMR_VAL_REG(UBOOT_CNTR));
139
140 /* enable timer in auto reload mode */
141 cntmrctrl = readl(CNTMR_CTRL_REG);
142 cntmrctrl |= CTCR_ARM_TIMER_EN(UBOOT_CNTR);
143 cntmrctrl |= CTCR_ARM_TIMER_AUTO_EN(UBOOT_CNTR);
144 writel(cntmrctrl, CNTMR_CTRL_REG);
Albert Aribaudd778a2f2010-09-23 21:49:23 +0200145 return 0;
146}
Albert Aribaud0c61e6f2010-06-17 19:36:07 +0530147
Albert Aribaudd778a2f2010-09-23 21:49:23 +0200148void timer_init_r(void)
149{
Albert Aribaud0c61e6f2010-06-17 19:36:07 +0530150 /* init the timestamp and lastdec value */
Graeme Russ17659d72011-07-15 02:21:14 +0000151 lastdec = read_timer();
152 timestamp = 0;
Albert Aribaud0c61e6f2010-06-17 19:36:07 +0530153}
Prafulla Wadaskar96f5c4b2012-02-08 14:15:53 +0530154
155/*
156 * This function is derived from PowerPC code (read timebase as long long).
157 * On ARM it just returns the timer value.
158 */
159unsigned long long get_ticks(void)
160{
161 return get_timer(0);
162}
163
164/*
165 * This function is derived from PowerPC code (timebase clock frequency).
166 * On ARM it returns the number of timer ticks per second.
167 */
168ulong get_tbclk (void)
169{
170 return (ulong)CONFIG_SYS_HZ;
171}