blob: 5bc43c8c19b8e025cf9acde398690a8e1363a702 [file] [log] [blame]
Albert Aribaudac2ba9e2010-06-17 19:36:07 +05301/*
Albert ARIBAUD340983d2011-04-22 19:41:02 +02002 * Copyright (C) 2010 Albert ARIBAUD <albert.u.boot@aribaud.net>
Albert Aribaudac2ba9e2010-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 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 * MA 02110-1301 USA
25 */
26
27#include <common.h>
28#include <asm/arch/orion5x.h>
29
30#define UBOOT_CNTR 0 /* counter to use for uboot timer */
31
32/* Timer reload and current value registers */
33struct orion5x_tmr_val {
34 u32 reload; /* Timer reload reg */
35 u32 val; /* Timer value reg */
36};
37
38/* Timer registers */
39struct orion5x_tmr_registers {
40 u32 ctrl; /* Timer control reg */
41 u32 pad[3];
42 struct orion5x_tmr_val tmr[2];
43 u32 wdt_reload;
44 u32 wdt_val;
45};
46
47struct orion5x_tmr_registers *orion5x_tmr_regs =
48 (struct orion5x_tmr_registers *)ORION5X_TIMER_BASE;
49
50/*
51 * ARM Timers Registers Map
52 */
53#define CNTMR_CTRL_REG (&orion5x_tmr_regs->ctrl)
54#define CNTMR_RELOAD_REG(tmrnum) (&orion5x_tmr_regs->tmr[tmrnum].reload)
55#define CNTMR_VAL_REG(tmrnum) (&orion5x_tmr_regs->tmr[tmrnum].val)
56
57/*
58 * ARM Timers Control Register
59 * CPU_TIMERS_CTRL_REG (CTCR)
60 */
61#define CTCR_ARM_TIMER_EN_OFFS(cntr) (cntr * 2)
62#define CTCR_ARM_TIMER_EN_MASK(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS)
63#define CTCR_ARM_TIMER_EN(cntr) (1 << CTCR_ARM_TIMER_EN_OFFS(cntr))
64#define CTCR_ARM_TIMER_DIS(cntr) (0 << CTCR_ARM_TIMER_EN_OFFS(cntr))
65
66#define CTCR_ARM_TIMER_AUTO_OFFS(cntr) ((cntr * 2) + 1)
67#define CTCR_ARM_TIMER_AUTO_MASK(cntr) (1 << 1)
68#define CTCR_ARM_TIMER_AUTO_EN(cntr) (1 << CTCR_ARM_TIMER_AUTO_OFFS(cntr))
69#define CTCR_ARM_TIMER_AUTO_DIS(cntr) (0 << CTCR_ARM_TIMER_AUTO_OFFS(cntr))
70
71/*
72 * ARM Timer\Watchdog Reload Register
73 * CNTMR_RELOAD_REG (TRR)
74 */
75#define TRG_ARM_TIMER_REL_OFFS 0
76#define TRG_ARM_TIMER_REL_MASK 0xffffffff
77
78/*
79 * ARM Timer\Watchdog Register
80 * CNTMR_VAL_REG (TVRG)
81 */
82#define TVR_ARM_TIMER_OFFS 0
83#define TVR_ARM_TIMER_MASK 0xffffffff
84#define TVR_ARM_TIMER_MAX 0xffffffff
85#define TIMER_LOAD_VAL 0xffffffff
86
87static inline ulong read_timer(void)
88{
89 return readl(CNTMR_VAL_REG(UBOOT_CNTR))
90 / (CONFIG_SYS_TCLK / 1000);
91}
92
Heiko Schocher5504dab2011-01-20 22:56:39 +000093DECLARE_GLOBAL_DATA_PTR;
94
95#define timestamp gd->tbl
96#define lastdec gd->lastinc
Albert Aribaudac2ba9e2010-06-17 19:36:07 +053097
98void reset_timer_masked(void)
99{
100 /* reset time */
101 lastdec = read_timer();
102 timestamp = 0;
103}
104
105ulong get_timer_masked(void)
106{
107 ulong now = read_timer();
108
109 if (lastdec >= now) {
110 /* normal mode */
111 timestamp += lastdec - now;
112 } else {
113 /* we have an overflow ... */
114 timestamp += lastdec +
115 (TIMER_LOAD_VAL / (CONFIG_SYS_TCLK / 1000)) - now;
116 }
117 lastdec = now;
118
119 return timestamp;
120}
121
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530122ulong get_timer(ulong base)
123{
124 return get_timer_masked() - base;
125}
126
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530127static inline ulong uboot_cntr_val(void)
128{
129 return readl(CNTMR_VAL_REG(UBOOT_CNTR));
130}
131
132void __udelay(unsigned long usec)
133{
134 uint current;
135 ulong delayticks;
136
137 current = uboot_cntr_val();
138 delayticks = (usec * (CONFIG_SYS_TCLK / 1000000));
139
140 if (current < delayticks) {
141 delayticks -= current;
142 while (uboot_cntr_val() < current)
143 ;
144 while ((TIMER_LOAD_VAL - delayticks) < uboot_cntr_val())
145 ;
146 } else {
147 while (uboot_cntr_val() > (current - delayticks))
148 ;
149 }
150}
151
152/*
153 * init the counter
154 */
155int timer_init(void)
156{
157 unsigned int cntmrctrl;
158
159 /* load value into timer */
160 writel(TIMER_LOAD_VAL, CNTMR_RELOAD_REG(UBOOT_CNTR));
161 writel(TIMER_LOAD_VAL, CNTMR_VAL_REG(UBOOT_CNTR));
162
163 /* enable timer in auto reload mode */
164 cntmrctrl = readl(CNTMR_CTRL_REG);
165 cntmrctrl |= CTCR_ARM_TIMER_EN(UBOOT_CNTR);
166 cntmrctrl |= CTCR_ARM_TIMER_AUTO_EN(UBOOT_CNTR);
167 writel(cntmrctrl, CNTMR_CTRL_REG);
Albert Aribaudfd5f9732010-09-23 21:49:23 +0200168 return 0;
169}
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530170
Albert Aribaudfd5f9732010-09-23 21:49:23 +0200171void timer_init_r(void)
172{
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530173 /* init the timestamp and lastdec value */
174 reset_timer_masked();
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530175}