blob: 4142932d08eaa830c56434ba1161dcf43e1ef5d9 [file] [log] [blame]
Sergey Kubushyne8f39122007-08-10 20:26:18 +02001/*
2 * (C) Copyright 2003
3 * Texas Instruments <www.ti.com>
4 *
5 * (C) Copyright 2002
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
8 *
9 * (C) Copyright 2002
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Alex Zuepke <azu@sysgo.de>
12 *
13 * (C) Copyright 2002-2004
Detlev Zundelf1b3f2b2009-05-13 10:54:10 +020014 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
Sergey Kubushyne8f39122007-08-10 20:26:18 +020015 *
16 * (C) Copyright 2004
17 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
18 *
19 * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
20 *
21 * See file CREDITS for list of people who contributed to this
22 * project.
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License as
26 * published by the Free Software Foundation; either version 2 of
27 * the License, or (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 * MA 02111-1307 USA
38 */
39
40#include <common.h>
Nick Thompson283398b2009-11-12 11:02:17 -050041#include <asm/io.h>
Heiko Schocher420cd0a2011-09-14 19:44:00 +000042#include <asm/arch/timer_defs.h>
Christian Riesch93757cf2011-12-09 16:54:01 +010043#include <div64.h>
Sergey Kubushyne8f39122007-08-10 20:26:18 +020044
Nick Thompson1c92d8e2010-12-11 10:46:46 -050045DECLARE_GLOBAL_DATA_PTR;
46
Nick Thompson283398b2009-11-12 11:02:17 -050047static struct davinci_timer * const timer =
48 (struct davinci_timer *)CONFIG_SYS_TIMERBASE;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020049
Nick Thompson1c92d8e2010-12-11 10:46:46 -050050#define TIMER_LOAD_VAL 0xffffffff
Peter Pearsee7680982008-02-01 16:50:24 +000051
Nick Thompson1c92d8e2010-12-11 10:46:46 -050052#define TIM_CLK_DIV 16
Sergey Kubushyne8f39122007-08-10 20:26:18 +020053
54int timer_init(void)
55{
56 /* We are using timer34 in unchained 32-bit mode, full speed */
Nick Thompson283398b2009-11-12 11:02:17 -050057 writel(0x0, &timer->tcr);
58 writel(0x0, &timer->tgcr);
59 writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &timer->tgcr);
60 writel(0x0, &timer->tim34);
61 writel(TIMER_LOAD_VAL, &timer->prd34);
Nick Thompson283398b2009-11-12 11:02:17 -050062 writel(2 << 22, &timer->tcr);
Simon Glass6ed6e032012-12-13 20:48:32 +000063 gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK / TIM_CLK_DIV;
Simon Glass9cbe003a2012-12-13 20:48:36 +000064 gd->arch.timer_reset_value = 0;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020065
66 return(0);
67}
68
Nick Thompson1c92d8e2010-12-11 10:46:46 -050069/*
70 * Get the current 64 bit timer tick count
71 */
72unsigned long long get_ticks(void)
Sergey Kubushyne8f39122007-08-10 20:26:18 +020073{
Nick Thompson1c92d8e2010-12-11 10:46:46 -050074 unsigned long now = readl(&timer->tim34);
75
76 /* increment tbu if tbl has rolled over */
Simon Glass2655ee12012-12-13 20:48:34 +000077 if (now < gd->arch.tbl)
Simon Glass8ca15202012-12-13 20:48:33 +000078 gd->arch.tbu++;
Simon Glass2655ee12012-12-13 20:48:34 +000079 gd->arch.tbl = now;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020080
Simon Glass2655ee12012-12-13 20:48:34 +000081 return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020082}
83
Dirk Behme866c3212008-03-26 09:53:29 +010084ulong get_timer(ulong base)
85{
Nick Thompson1c92d8e2010-12-11 10:46:46 -050086 unsigned long long timer_diff;
Dirk Behme866c3212008-03-26 09:53:29 +010087
Simon Glass9cbe003a2012-12-13 20:48:36 +000088 timer_diff = get_ticks() - gd->arch.timer_reset_value;
Nick Thompson1c92d8e2010-12-11 10:46:46 -050089
Simon Glass6ed6e032012-12-13 20:48:32 +000090 return lldiv(timer_diff,
91 (gd->arch.timer_rate_hz / CONFIG_SYS_HZ)) - base;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020092}
93
Ingo van Lilf0f778a2009-11-24 14:09:21 +010094void __udelay(unsigned long usec)
Sergey Kubushyne8f39122007-08-10 20:26:18 +020095{
Nick Thompson1c92d8e2010-12-11 10:46:46 -050096 unsigned long long endtime;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020097
Simon Glass6ed6e032012-12-13 20:48:32 +000098 endtime = lldiv((unsigned long long)usec * gd->arch.timer_rate_hz,
Christian Riesch93757cf2011-12-09 16:54:01 +010099 1000000UL);
Nick Thompson1c92d8e2010-12-11 10:46:46 -0500100 endtime += get_ticks();
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200101
Nick Thompson1c92d8e2010-12-11 10:46:46 -0500102 while (get_ticks() < endtime)
103 ;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200104}
105
106/*
107 * This function is derived from PowerPC code (timebase clock frequency).
108 * On ARM it returns the number of timer ticks per second.
109 */
110ulong get_tbclk(void)
111{
Simon Glass6ed6e032012-12-13 20:48:32 +0000112 return gd->arch.timer_rate_hz;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200113}
Heiko Schochera2cddad2011-09-14 19:44:02 +0000114
115#ifdef CONFIG_HW_WATCHDOG
116static struct davinci_timer * const wdttimer =
117 (struct davinci_timer *)CONFIG_SYS_WDTTIMERBASE;
118
119/*
120 * See prufw2.pdf for using Timer as a WDT
121 */
122void davinci_hw_watchdog_enable(void)
123{
124 writel(0x0, &wdttimer->tcr);
125 writel(0x0, &wdttimer->tgcr);
126 /* TIMMODE = 2h */
127 writel(0x08 | 0x03 | ((TIM_CLK_DIV - 1) << 8), &wdttimer->tgcr);
128 writel(CONFIG_SYS_WDT_PERIOD_LOW, &wdttimer->prd12);
129 writel(CONFIG_SYS_WDT_PERIOD_HIGH, &wdttimer->prd34);
130 writel(2 << 22, &wdttimer->tcr);
131 writel(0x0, &wdttimer->tim12);
132 writel(0x0, &wdttimer->tim34);
133 /* set WDEN bit, WDKEY 0xa5c6 */
134 writel(0xa5c64000, &wdttimer->wdtcr);
135 /* clear counter register */
136 writel(0xda7e4000, &wdttimer->wdtcr);
137}
138
139void davinci_hw_watchdog_reset(void)
140{
141 writel(0xa5c64000, &wdttimer->wdtcr);
142 writel(0xda7e4000, &wdttimer->wdtcr);
143}
144#endif