blob: d0f783e66013ab41de312a5dffe16d97b643fbd1 [file] [log] [blame]
Tom Warren41b68382011-01-27 10:58:05 +00001/*
2 * (C) Copyright 2010,2011
3 * NVIDIA Corporation <www.nvidia.com>
4 *
5 * (C) Copyright 2008
6 * Texas Instruments
7 *
8 * Richard Woodruff <r-woodruff2@ti.com>
9 * Syed Moahmmed Khasim <khasim@ti.com>
10 *
11 * (C) Copyright 2002
12 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13 * Marius Groeger <mgroeger@sysgo.de>
14 * Alex Zuepke <azu@sysgo.de>
15 *
16 * (C) Copyright 2002
17 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
18 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020019 * SPDX-License-Identifier: GPL-2.0+
Tom Warren41b68382011-01-27 10:58:05 +000020 */
21
22#include <common.h>
23#include <asm/io.h>
Tom Warrenab371962012-09-19 15:50:56 -070024#include <asm/arch/tegra.h>
25#include <asm/arch-tegra/timer.h>
Tom Warren41b68382011-01-27 10:58:05 +000026
27DECLARE_GLOBAL_DATA_PTR;
28
Tom Warren41b68382011-01-27 10:58:05 +000029/* counter runs at 1MHz */
Simon Glass28cb1612011-08-30 06:23:12 +000030#define TIMER_CLK 1000000
Tom Warren41b68382011-01-27 10:58:05 +000031#define TIMER_LOAD_VAL 0xffffffff
32
33/* timer without interrupts */
Tom Warren41b68382011-01-27 10:58:05 +000034ulong get_timer(ulong base)
35{
36 return get_timer_masked() - base;
37}
38
Tom Warren41b68382011-01-27 10:58:05 +000039/* delay x useconds */
40void __udelay(unsigned long usec)
41{
42 long tmo = usec * (TIMER_CLK / 1000) / 1000;
Simon Glass28cb1612011-08-30 06:23:12 +000043 unsigned long now, last = timer_get_us();
Tom Warren41b68382011-01-27 10:58:05 +000044
45 while (tmo > 0) {
Simon Glass28cb1612011-08-30 06:23:12 +000046 now = timer_get_us();
Tom Warren41b68382011-01-27 10:58:05 +000047 if (last > now) /* count up timer overflow */
48 tmo -= TIMER_LOAD_VAL - last + now;
49 else
50 tmo -= now - last;
51 last = now;
52 }
53}
54
Tom Warren41b68382011-01-27 10:58:05 +000055ulong get_timer_masked(void)
56{
57 ulong now;
58
59 /* current tick value */
Simon Glass28cb1612011-08-30 06:23:12 +000060 now = timer_get_us() / (TIMER_CLK / CONFIG_SYS_HZ);
Tom Warren41b68382011-01-27 10:58:05 +000061
Simon Glassa848da52012-12-13 20:48:35 +000062 if (now >= gd->arch.lastinc) /* normal mode (non roll) */
Tom Warren41b68382011-01-27 10:58:05 +000063 /* move stamp forward with absolute diff ticks */
Simon Glassa848da52012-12-13 20:48:35 +000064 gd->arch.tbl += (now - gd->arch.lastinc);
Tom Warren41b68382011-01-27 10:58:05 +000065 else /* we have rollover of incrementer */
Simon Glass2655ee12012-12-13 20:48:34 +000066 gd->arch.tbl += ((TIMER_LOAD_VAL / (TIMER_CLK / CONFIG_SYS_HZ))
Simon Glassa848da52012-12-13 20:48:35 +000067 - gd->arch.lastinc) + now;
68 gd->arch.lastinc = now;
Simon Glass2655ee12012-12-13 20:48:34 +000069 return gd->arch.tbl;
Tom Warren41b68382011-01-27 10:58:05 +000070}
71
72/*
73 * This function is derived from PowerPC code (read timebase as long long).
74 * On ARM it just returns the timer value.
75 */
76unsigned long long get_ticks(void)
77{
78 return get_timer(0);
79}
80
81/*
82 * This function is derived from PowerPC code (timebase clock frequency).
83 * On ARM it returns the number of timer ticks per second.
84 */
85ulong get_tbclk(void)
86{
87 return CONFIG_SYS_HZ;
88}
Simon Glass28cb1612011-08-30 06:23:12 +000089
90unsigned long timer_get_us(void)
91{
92 struct timerus *timer_base = (struct timerus *)NV_PA_TMRUS_BASE;
93
94 return readl(&timer_base->cntr_1us);
95}