blob: 3b6666b7914143d3c39d7b55bc7f090f9b66a389 [file] [log] [blame]
Wolfgang Denka48499f2008-04-11 15:11:26 +02001/*
2 * (C) Copyright 2004
3 * Texas Instruments
4 * Richard Woodruff <r-woodruff2@ti.com>
5 *
6 * (C) Copyright 2002
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Marius Groeger <mgroeger@sysgo.de>
9 * Alex Zuepke <azu@sysgo.de>
10 *
11 * (C) Copyright 2002
Detlev Zundelf1b3f2b2009-05-13 10:54:10 +020012 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
Wolfgang Denka48499f2008-04-11 15:11:26 +020013 *
14 * See file CREDITS for list of people who contributed to this
15 * project.
16 *
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 * MA 02111-1307 USA
31 */
32
33#include <common.h>
Hunter, Jon4db99e12013-04-03 09:35:36 +000034#include <asm/io.h>
Wolfgang Denka48499f2008-04-11 15:11:26 +020035#include <asm/arch/bits.h>
Sascha Hauer7a19cea2008-03-26 20:40:36 +010036#include <asm/arch/omap2420.h>
Wolfgang Denka48499f2008-04-11 15:11:26 +020037
Hunter, Jon4db99e12013-04-03 09:35:36 +000038#define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV))
Wolfgang Denka48499f2008-04-11 15:11:26 +020039#define TIMER_LOAD_VAL 0
40
41/* macro to read the 32 bit timer */
Hunter, Jon4db99e12013-04-03 09:35:36 +000042#define READ_TIMER readl(CONFIG_SYS_TIMERBASE+TCRR) \
43 / (TIMER_CLOCK / CONFIG_SYS_HZ)
Wolfgang Denka48499f2008-04-11 15:11:26 +020044
Heiko Schocherf2015952010-12-09 22:01:15 +000045DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denka48499f2008-04-11 15:11:26 +020046
Jean-Christophe PLAGNIOL-VILLARD8c9fc002009-05-15 23:47:02 +020047int timer_init (void)
Wolfgang Denka48499f2008-04-11 15:11:26 +020048{
49 int32_t val;
50
51 /* Start the counter ticking up */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020052 *((int32_t *) (CONFIG_SYS_TIMERBASE + TLDR)) = TIMER_LOAD_VAL; /* reload value on overflow*/
Ladislav Michl993e57d2009-03-30 18:58:41 +020053 val = (CONFIG_SYS_PTV << 2) | BIT5 | BIT1 | BIT0; /* mask to enable timer*/
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020054 *((int32_t *) (CONFIG_SYS_TIMERBASE + TCLR)) = val; /* start timer */
Wolfgang Denka48499f2008-04-11 15:11:26 +020055
Wolfgang Denk68ad3dc2011-09-05 05:55:59 +000056 /* reset time */
Simon Glassa848da52012-12-13 20:48:35 +000057 gd->arch.lastinc = READ_TIMER; /* capture current incrementer value */
Simon Glass2655ee12012-12-13 20:48:34 +000058 gd->arch.tbl = 0; /* start "advancing" time stamp */
Wolfgang Denka48499f2008-04-11 15:11:26 +020059
60 return(0);
61}
62/*
63 * timer without interrupts
64 */
Wolfgang Denka48499f2008-04-11 15:11:26 +020065ulong get_timer (ulong base)
66{
67 return get_timer_masked () - base;
68}
69
Wolfgang Denkd7cb3552010-05-21 23:13:18 +020070/* delay x useconds AND preserve advance timestamp value */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010071void __udelay (unsigned long usec)
Wolfgang Denka48499f2008-04-11 15:11:26 +020072{
73 ulong tmo, tmp;
74
Wolfgang Denkd7cb3552010-05-21 23:13:18 +020075 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
76 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
77 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
78 tmo /= 1000; /* finish normalize. */
79 } else { /* else small number, don't kill it prior to HZ multiply */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020080 tmo = usec * CONFIG_SYS_HZ;
Wolfgang Denka48499f2008-04-11 15:11:26 +020081 tmo /= (1000*1000);
82 }
83
84 tmp = get_timer (0); /* get current timestamp */
Wolfgang Denk68ad3dc2011-09-05 05:55:59 +000085 if ((tmo + tmp + 1) < tmp) { /* if setting this forward will roll */
86 /* time stamp, then reset time */
Simon Glassa848da52012-12-13 20:48:35 +000087 gd->arch.lastinc = READ_TIMER; /* capture incrementer value */
Simon Glass2655ee12012-12-13 20:48:34 +000088 gd->arch.tbl = 0; /* start time stamp */
Wolfgang Denk68ad3dc2011-09-05 05:55:59 +000089 } else {
Wolfgang Denkd7cb3552010-05-21 23:13:18 +020090 tmo += tmp; /* else, set advancing stamp wake up time */
Wolfgang Denk68ad3dc2011-09-05 05:55:59 +000091 }
Wolfgang Denka48499f2008-04-11 15:11:26 +020092 while (get_timer_masked () < tmo)/* loop till event */
93 /*NOP*/;
94}
95
Wolfgang Denka48499f2008-04-11 15:11:26 +020096ulong get_timer_masked (void)
97{
98 ulong now = READ_TIMER; /* current tick value */
99
Simon Glassa848da52012-12-13 20:48:35 +0000100 if (now >= gd->arch.lastinc) { /* normal mode (non roll) */
Simon Glass2655ee12012-12-13 20:48:34 +0000101 /* move stamp fordward with absoulte diff ticks */
Simon Glassa848da52012-12-13 20:48:35 +0000102 gd->arch.tbl += (now - gd->arch.lastinc);
Simon Glass2655ee12012-12-13 20:48:34 +0000103 } else {
104 /* we have rollover of incrementer */
Hunter, Jon4db99e12013-04-03 09:35:36 +0000105 gd->arch.tbl += ((0xFFFFFFFF / (TIMER_CLOCK / CONFIG_SYS_HZ))
106 - gd->arch.lastinc) + now;
Simon Glass2655ee12012-12-13 20:48:34 +0000107 }
Simon Glassa848da52012-12-13 20:48:35 +0000108 gd->arch.lastinc = now;
Simon Glass2655ee12012-12-13 20:48:34 +0000109 return gd->arch.tbl;
Wolfgang Denka48499f2008-04-11 15:11:26 +0200110}
111
112/* waits specified delay value and resets timestamp */
113void udelay_masked (unsigned long usec)
114{
115 ulong tmo;
116 ulong endtime;
117 signed long diff;
118
119 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
120 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200121 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
Wolfgang Denka48499f2008-04-11 15:11:26 +0200122 tmo /= 1000; /* finish normalize. */
123 } else { /* else small number, don't kill it prior to HZ multiply */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200124 tmo = usec * CONFIG_SYS_HZ;
Wolfgang Denka48499f2008-04-11 15:11:26 +0200125 tmo /= (1000*1000);
126 }
127 endtime = get_timer_masked () + tmo;
128
129 do {
130 ulong now = get_timer_masked ();
131 diff = endtime - now;
132 } while (diff >= 0);
133}
134
135/*
136 * This function is derived from PowerPC code (read timebase as long long).
137 * On ARM it just returns the timer value.
138 */
139unsigned long long get_ticks(void)
140{
141 return get_timer(0);
142}
143/*
144 * This function is derived from PowerPC code (timebase clock frequency).
145 * On ARM it returns the number of timer ticks per second.
146 */
147ulong get_tbclk (void)
148{
149 ulong tbclk;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200150 tbclk = CONFIG_SYS_HZ;
Wolfgang Denka48499f2008-04-11 15:11:26 +0200151 return tbclk;
152}