blob: c7d0652e8301e87243bb10800719db663842bd19 [file] [log] [blame]
Sergey Kubushync74b2102007-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 Zundel792a09e2009-05-13 10:54:10 +020014 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
Sergey Kubushync74b2102007-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 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +020021 * SPDX-License-Identifier: GPL-2.0+
Sergey Kubushync74b2102007-08-10 20:26:18 +020022 */
23
24#include <common.h>
Nick Thompson9868a362009-11-12 11:02:17 -050025#include <asm/io.h>
Heiko Schocherde23e722011-09-14 19:44:00 +000026#include <asm/arch/timer_defs.h>
Christian Riesche21b3dfb2011-12-09 16:54:01 +010027#include <div64.h>
Sergey Kubushync74b2102007-08-10 20:26:18 +020028
Nick Thompsone465cf22010-12-11 10:46:46 -050029DECLARE_GLOBAL_DATA_PTR;
30
Nick Thompson9868a362009-11-12 11:02:17 -050031static struct davinci_timer * const timer =
32 (struct davinci_timer *)CONFIG_SYS_TIMERBASE;
Sergey Kubushync74b2102007-08-10 20:26:18 +020033
Nick Thompsone465cf22010-12-11 10:46:46 -050034#define TIMER_LOAD_VAL 0xffffffff
Peter Pearseea686f52008-02-01 16:50:24 +000035
Nick Thompsone465cf22010-12-11 10:46:46 -050036#define TIM_CLK_DIV 16
Sergey Kubushync74b2102007-08-10 20:26:18 +020037
38int timer_init(void)
39{
40 /* We are using timer34 in unchained 32-bit mode, full speed */
Nick Thompson9868a362009-11-12 11:02:17 -050041 writel(0x0, &timer->tcr);
42 writel(0x0, &timer->tgcr);
43 writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &timer->tgcr);
44 writel(0x0, &timer->tim34);
45 writel(TIMER_LOAD_VAL, &timer->prd34);
Nick Thompson9868a362009-11-12 11:02:17 -050046 writel(2 << 22, &timer->tcr);
Simon Glassb3390512012-12-13 20:48:32 +000047 gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK / TIM_CLK_DIV;
Simon Glass5f707142012-12-13 20:48:36 +000048 gd->arch.timer_reset_value = 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +020049
50 return(0);
51}
52
Nick Thompsone465cf22010-12-11 10:46:46 -050053/*
54 * Get the current 64 bit timer tick count
55 */
56unsigned long long get_ticks(void)
Dirk Behme80c40b72008-03-26 09:53:29 +010057{
Nick Thompsone465cf22010-12-11 10:46:46 -050058 unsigned long now = readl(&timer->tim34);
Dirk Behme80c40b72008-03-26 09:53:29 +010059
Nick Thompsone465cf22010-12-11 10:46:46 -050060 /* increment tbu if tbl has rolled over */
Simon Glass66ee6922012-12-13 20:48:34 +000061 if (now < gd->arch.tbl)
Simon Glass8ff43b02012-12-13 20:48:33 +000062 gd->arch.tbu++;
Simon Glass66ee6922012-12-13 20:48:34 +000063 gd->arch.tbl = now;
Nick Thompsone465cf22010-12-11 10:46:46 -050064
Simon Glass66ee6922012-12-13 20:48:34 +000065 return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
Sergey Kubushync74b2102007-08-10 20:26:18 +020066}
67
68ulong get_timer(ulong base)
69{
Nick Thompsone465cf22010-12-11 10:46:46 -050070 unsigned long long timer_diff;
Sergey Kubushync74b2102007-08-10 20:26:18 +020071
Simon Glass5f707142012-12-13 20:48:36 +000072 timer_diff = get_ticks() - gd->arch.timer_reset_value;
Nick Thompsone465cf22010-12-11 10:46:46 -050073
Simon Glassb3390512012-12-13 20:48:32 +000074 return lldiv(timer_diff,
75 (gd->arch.timer_rate_hz / CONFIG_SYS_HZ)) - base;
Sergey Kubushync74b2102007-08-10 20:26:18 +020076}
77
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010078void __udelay(unsigned long usec)
Sergey Kubushync74b2102007-08-10 20:26:18 +020079{
Nick Thompsone465cf22010-12-11 10:46:46 -050080 unsigned long long endtime;
Sergey Kubushync74b2102007-08-10 20:26:18 +020081
Simon Glassb3390512012-12-13 20:48:32 +000082 endtime = lldiv((unsigned long long)usec * gd->arch.timer_rate_hz,
Christian Riesche21b3dfb2011-12-09 16:54:01 +010083 1000000UL);
Nick Thompsone465cf22010-12-11 10:46:46 -050084 endtime += get_ticks();
Sergey Kubushync74b2102007-08-10 20:26:18 +020085
Nick Thompsone465cf22010-12-11 10:46:46 -050086 while (get_ticks() < endtime)
87 ;
Sergey Kubushync74b2102007-08-10 20:26:18 +020088}
89
90/*
91 * This function is derived from PowerPC code (timebase clock frequency).
92 * On ARM it returns the number of timer ticks per second.
93 */
94ulong get_tbclk(void)
95{
Simon Glassb3390512012-12-13 20:48:32 +000096 return gd->arch.timer_rate_hz;
Sergey Kubushync74b2102007-08-10 20:26:18 +020097}
Heiko Schocherbf569ac2011-09-14 19:44:02 +000098
99#ifdef CONFIG_HW_WATCHDOG
100static struct davinci_timer * const wdttimer =
101 (struct davinci_timer *)CONFIG_SYS_WDTTIMERBASE;
102
103/*
104 * See prufw2.pdf for using Timer as a WDT
105 */
106void davinci_hw_watchdog_enable(void)
107{
108 writel(0x0, &wdttimer->tcr);
109 writel(0x0, &wdttimer->tgcr);
110 /* TIMMODE = 2h */
111 writel(0x08 | 0x03 | ((TIM_CLK_DIV - 1) << 8), &wdttimer->tgcr);
112 writel(CONFIG_SYS_WDT_PERIOD_LOW, &wdttimer->prd12);
113 writel(CONFIG_SYS_WDT_PERIOD_HIGH, &wdttimer->prd34);
114 writel(2 << 22, &wdttimer->tcr);
115 writel(0x0, &wdttimer->tim12);
116 writel(0x0, &wdttimer->tim34);
117 /* set WDEN bit, WDKEY 0xa5c6 */
118 writel(0xa5c64000, &wdttimer->wdtcr);
119 /* clear counter register */
120 writel(0xda7e4000, &wdttimer->wdtcr);
121}
122
123void davinci_hw_watchdog_reset(void)
124{
125 writel(0xa5c64000, &wdttimer->wdtcr);
126 writel(0xda7e4000, &wdttimer->wdtcr);
127}
128#endif