2 * Copyright (C) 2010 Albert ARIBAUD <albert.u.boot@aribaud.net>
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>
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
30 #define UBOOT_CNTR 0 /* counter to use for uboot timer */
32 /* Timer reload and current value registers */
33 struct orion5x_tmr_val {
34 u32 reload; /* Timer reload reg */
35 u32 val; /* Timer value reg */
39 struct orion5x_tmr_registers {
40 u32 ctrl; /* Timer control reg */
42 struct orion5x_tmr_val tmr[2];
47 struct orion5x_tmr_registers *orion5x_tmr_regs =
48 (struct orion5x_tmr_registers *)ORION5X_TIMER_BASE;
51 * ARM Timers Registers Map
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)
58 * ARM Timers Control Register
59 * CPU_TIMERS_CTRL_REG (CTCR)
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))
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))
72 * ARM Timer\Watchdog Reload Register
73 * CNTMR_RELOAD_REG (TRR)
75 #define TRG_ARM_TIMER_REL_OFFS 0
76 #define TRG_ARM_TIMER_REL_MASK 0xffffffff
79 * ARM Timer\Watchdog Register
80 * CNTMR_VAL_REG (TVRG)
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
87 static inline ulong read_timer(void)
89 return readl(CNTMR_VAL_REG(UBOOT_CNTR))
90 / (CONFIG_SYS_TCLK / 1000);
93 DECLARE_GLOBAL_DATA_PTR;
95 #define timestamp gd->tbl
96 #define lastdec gd->lastinc
98 ulong get_timer_masked(void)
100 ulong now = read_timer();
102 if (lastdec >= now) {
104 timestamp += lastdec - now;
106 /* we have an overflow ... */
107 timestamp += lastdec +
108 (TIMER_LOAD_VAL / (CONFIG_SYS_TCLK / 1000)) - now;
115 ulong get_timer(ulong base)
117 return get_timer_masked() - base;
120 static inline ulong uboot_cntr_val(void)
122 return readl(CNTMR_VAL_REG(UBOOT_CNTR));
125 void __udelay(unsigned long usec)
130 current = uboot_cntr_val();
131 delayticks = (usec * (CONFIG_SYS_TCLK / 1000000));
133 if (current < delayticks) {
134 delayticks -= current;
135 while (uboot_cntr_val() < current)
137 while ((TIMER_LOAD_VAL - delayticks) < uboot_cntr_val())
140 while (uboot_cntr_val() > (current - delayticks))
150 unsigned int cntmrctrl;
152 /* load value into timer */
153 writel(TIMER_LOAD_VAL, CNTMR_RELOAD_REG(UBOOT_CNTR));
154 writel(TIMER_LOAD_VAL, CNTMR_VAL_REG(UBOOT_CNTR));
156 /* enable timer in auto reload mode */
157 cntmrctrl = readl(CNTMR_CTRL_REG);
158 cntmrctrl |= CTCR_ARM_TIMER_EN(UBOOT_CNTR);
159 cntmrctrl |= CTCR_ARM_TIMER_AUTO_EN(UBOOT_CNTR);
160 writel(cntmrctrl, CNTMR_CTRL_REG);
164 void timer_init_r(void)
166 /* init the timestamp and lastdec value */
167 lastdec = read_timer();
172 * This function is derived from PowerPC code (read timebase as long long).
173 * On ARM it just returns the timer value.
175 unsigned long long get_ticks(void)
181 * This function is derived from PowerPC code (timebase clock frequency).
182 * On ARM it returns the number of timer ticks per second.
184 ulong get_tbclk (void)
186 return (ulong)CONFIG_SYS_HZ;