blob: b1eef27daee46329ef28ce651f4d0ccf58b626fb [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020014 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Denka48499f2008-04-11 15:11:26 +020015 */
16
17#include <common.h>
Hunter, Jon4db99e12013-04-03 09:35:36 +000018#include <asm/io.h>
Wolfgang Denka48499f2008-04-11 15:11:26 +020019#include <asm/arch/bits.h>
Sascha Hauer7a19cea2008-03-26 20:40:36 +010020#include <asm/arch/omap2420.h>
Wolfgang Denka48499f2008-04-11 15:11:26 +020021
Hunter, Jon4db99e12013-04-03 09:35:36 +000022#define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV))
Wolfgang Denka48499f2008-04-11 15:11:26 +020023#define TIMER_LOAD_VAL 0
24
25/* macro to read the 32 bit timer */
Hunter, Jon4db99e12013-04-03 09:35:36 +000026#define READ_TIMER readl(CONFIG_SYS_TIMERBASE+TCRR) \
27 / (TIMER_CLOCK / CONFIG_SYS_HZ)
Wolfgang Denka48499f2008-04-11 15:11:26 +020028
Heiko Schocherf2015952010-12-09 22:01:15 +000029DECLARE_GLOBAL_DATA_PTR;
Wolfgang Denka48499f2008-04-11 15:11:26 +020030
Jean-Christophe PLAGNIOL-VILLARD8c9fc002009-05-15 23:47:02 +020031int timer_init (void)
Wolfgang Denka48499f2008-04-11 15:11:26 +020032{
33 int32_t val;
34
35 /* Start the counter ticking up */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020036 *((int32_t *) (CONFIG_SYS_TIMERBASE + TLDR)) = TIMER_LOAD_VAL; /* reload value on overflow*/
Ladislav Michl993e57d2009-03-30 18:58:41 +020037 val = (CONFIG_SYS_PTV << 2) | BIT5 | BIT1 | BIT0; /* mask to enable timer*/
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020038 *((int32_t *) (CONFIG_SYS_TIMERBASE + TCLR)) = val; /* start timer */
Wolfgang Denka48499f2008-04-11 15:11:26 +020039
Wolfgang Denk68ad3dc2011-09-05 05:55:59 +000040 /* reset time */
Simon Glassa848da52012-12-13 20:48:35 +000041 gd->arch.lastinc = READ_TIMER; /* capture current incrementer value */
Simon Glass2655ee12012-12-13 20:48:34 +000042 gd->arch.tbl = 0; /* start "advancing" time stamp */
Wolfgang Denka48499f2008-04-11 15:11:26 +020043
44 return(0);
45}
46/*
47 * timer without interrupts
48 */
Wolfgang Denka48499f2008-04-11 15:11:26 +020049ulong get_timer (ulong base)
50{
51 return get_timer_masked () - base;
52}
53
Wolfgang Denkd7cb3552010-05-21 23:13:18 +020054/* delay x useconds AND preserve advance timestamp value */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010055void __udelay (unsigned long usec)
Wolfgang Denka48499f2008-04-11 15:11:26 +020056{
57 ulong tmo, tmp;
58
Wolfgang Denkd7cb3552010-05-21 23:13:18 +020059 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
60 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
61 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
62 tmo /= 1000; /* finish normalize. */
63 } else { /* else small number, don't kill it prior to HZ multiply */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020064 tmo = usec * CONFIG_SYS_HZ;
Wolfgang Denka48499f2008-04-11 15:11:26 +020065 tmo /= (1000*1000);
66 }
67
68 tmp = get_timer (0); /* get current timestamp */
Wolfgang Denk68ad3dc2011-09-05 05:55:59 +000069 if ((tmo + tmp + 1) < tmp) { /* if setting this forward will roll */
70 /* time stamp, then reset time */
Simon Glassa848da52012-12-13 20:48:35 +000071 gd->arch.lastinc = READ_TIMER; /* capture incrementer value */
Simon Glass2655ee12012-12-13 20:48:34 +000072 gd->arch.tbl = 0; /* start time stamp */
Wolfgang Denk68ad3dc2011-09-05 05:55:59 +000073 } else {
Wolfgang Denkd7cb3552010-05-21 23:13:18 +020074 tmo += tmp; /* else, set advancing stamp wake up time */
Wolfgang Denk68ad3dc2011-09-05 05:55:59 +000075 }
Wolfgang Denka48499f2008-04-11 15:11:26 +020076 while (get_timer_masked () < tmo)/* loop till event */
77 /*NOP*/;
78}
79
Wolfgang Denka48499f2008-04-11 15:11:26 +020080ulong get_timer_masked (void)
81{
82 ulong now = READ_TIMER; /* current tick value */
83
Simon Glassa848da52012-12-13 20:48:35 +000084 if (now >= gd->arch.lastinc) { /* normal mode (non roll) */
Simon Glass2655ee12012-12-13 20:48:34 +000085 /* move stamp fordward with absoulte diff ticks */
Simon Glassa848da52012-12-13 20:48:35 +000086 gd->arch.tbl += (now - gd->arch.lastinc);
Simon Glass2655ee12012-12-13 20:48:34 +000087 } else {
88 /* we have rollover of incrementer */
Hunter, Jon4db99e12013-04-03 09:35:36 +000089 gd->arch.tbl += ((0xFFFFFFFF / (TIMER_CLOCK / CONFIG_SYS_HZ))
90 - gd->arch.lastinc) + now;
Simon Glass2655ee12012-12-13 20:48:34 +000091 }
Simon Glassa848da52012-12-13 20:48:35 +000092 gd->arch.lastinc = now;
Simon Glass2655ee12012-12-13 20:48:34 +000093 return gd->arch.tbl;
Wolfgang Denka48499f2008-04-11 15:11:26 +020094}
95
96/* waits specified delay value and resets timestamp */
97void udelay_masked (unsigned long usec)
98{
99 ulong tmo;
100 ulong endtime;
101 signed long diff;
102
103 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
104 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200105 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
Wolfgang Denka48499f2008-04-11 15:11:26 +0200106 tmo /= 1000; /* finish normalize. */
107 } else { /* else small number, don't kill it prior to HZ multiply */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200108 tmo = usec * CONFIG_SYS_HZ;
Wolfgang Denka48499f2008-04-11 15:11:26 +0200109 tmo /= (1000*1000);
110 }
111 endtime = get_timer_masked () + tmo;
112
113 do {
114 ulong now = get_timer_masked ();
115 diff = endtime - now;
116 } while (diff >= 0);
117}
118
119/*
120 * This function is derived from PowerPC code (read timebase as long long).
121 * On ARM it just returns the timer value.
122 */
123unsigned long long get_ticks(void)
124{
125 return get_timer(0);
126}
127/*
128 * This function is derived from PowerPC code (timebase clock frequency).
129 * On ARM it returns the number of timer ticks per second.
130 */
131ulong get_tbclk (void)
132{
133 ulong tbclk;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200134 tbclk = CONFIG_SYS_HZ;
Wolfgang Denka48499f2008-04-11 15:11:26 +0200135 return tbclk;
136}