blob: 089ef47b371f672f85fd1c016e27b67b11327f0f [file] [log] [blame]
Albert Aribaudac2ba9e2010-06-17 19:36:07 +05301/*
2 * Copyright (C) 2010 Albert ARIBAUD <albert.aribaud@free.fr>
3 *
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
93static ulong timestamp;
94static ulong lastdec;
95
96void reset_timer_masked(void)
97{
98 /* reset time */
99 lastdec = read_timer();
100 timestamp = 0;
101}
102
103ulong get_timer_masked(void)
104{
105 ulong now = read_timer();
106
107 if (lastdec >= now) {
108 /* normal mode */
109 timestamp += lastdec - now;
110 } else {
111 /* we have an overflow ... */
112 timestamp += lastdec +
113 (TIMER_LOAD_VAL / (CONFIG_SYS_TCLK / 1000)) - now;
114 }
115 lastdec = now;
116
117 return timestamp;
118}
119
120void reset_timer(void)
121{
122 reset_timer_masked();
123}
124
125ulong get_timer(ulong base)
126{
127 return get_timer_masked() - base;
128}
129
130void set_timer(ulong t)
131{
132 timestamp = t;
133}
134
135static inline ulong uboot_cntr_val(void)
136{
137 return readl(CNTMR_VAL_REG(UBOOT_CNTR));
138}
139
140void __udelay(unsigned long usec)
141{
142 uint current;
143 ulong delayticks;
144
145 current = uboot_cntr_val();
146 delayticks = (usec * (CONFIG_SYS_TCLK / 1000000));
147
148 if (current < delayticks) {
149 delayticks -= current;
150 while (uboot_cntr_val() < current)
151 ;
152 while ((TIMER_LOAD_VAL - delayticks) < uboot_cntr_val())
153 ;
154 } else {
155 while (uboot_cntr_val() > (current - delayticks))
156 ;
157 }
158}
159
160/*
161 * init the counter
162 */
163int timer_init(void)
164{
165 unsigned int cntmrctrl;
166
167 /* load value into timer */
168 writel(TIMER_LOAD_VAL, CNTMR_RELOAD_REG(UBOOT_CNTR));
169 writel(TIMER_LOAD_VAL, CNTMR_VAL_REG(UBOOT_CNTR));
170
171 /* enable timer in auto reload mode */
172 cntmrctrl = readl(CNTMR_CTRL_REG);
173 cntmrctrl |= CTCR_ARM_TIMER_EN(UBOOT_CNTR);
174 cntmrctrl |= CTCR_ARM_TIMER_AUTO_EN(UBOOT_CNTR);
175 writel(cntmrctrl, CNTMR_CTRL_REG);
Albert Aribaudfd5f9732010-09-23 21:49:23 +0200176 return 0;
177}
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530178
Albert Aribaudfd5f9732010-09-23 21:49:23 +0200179void timer_init_r(void)
180{
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530181 /* init the timestamp and lastdec value */
182 reset_timer_masked();
Albert Aribaudac2ba9e2010-06-17 19:36:07 +0530183}