blob: a6681821f6dd78401c22f91e1ba4a280701379d0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sergey Kubushync74b2102007-08-10 20:26:18 +02002/*
3 * (C) Copyright 2003
4 * Texas Instruments <www.ti.com>
5 *
6 * (C) Copyright 2002
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Marius Groeger <mgroeger@sysgo.de>
9 *
10 * (C) Copyright 2002
11 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
12 * Alex Zuepke <azu@sysgo.de>
13 *
14 * (C) Copyright 2002-2004
Detlev Zundel792a09e2009-05-13 10:54:10 +020015 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
Sergey Kubushync74b2102007-08-10 20:26:18 +020016 *
17 * (C) Copyright 2004
18 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
19 *
20 * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
Sergey Kubushync74b2102007-08-10 20:26:18 +020021 */
22
23#include <common.h>
Simon Glass691d7192020-05-10 11:40:02 -060024#include <init.h>
Simon Glass10453152019-11-14 12:57:30 -070025#include <time.h>
Nick Thompson9868a362009-11-12 11:02:17 -050026#include <asm/io.h>
Heiko Schocherde23e722011-09-14 19:44:00 +000027#include <asm/arch/timer_defs.h>
Christian Riesche21b3dfb2011-12-09 16:54:01 +010028#include <div64.h>
Simon Glassc05ed002020-05-10 11:40:11 -060029#include <linux/delay.h>
Sergey Kubushync74b2102007-08-10 20:26:18 +020030
Nick Thompsone465cf22010-12-11 10:46:46 -050031DECLARE_GLOBAL_DATA_PTR;
32
Nick Thompson9868a362009-11-12 11:02:17 -050033static struct davinci_timer * const timer =
34 (struct davinci_timer *)CONFIG_SYS_TIMERBASE;
Sergey Kubushync74b2102007-08-10 20:26:18 +020035
Nick Thompsone465cf22010-12-11 10:46:46 -050036#define TIMER_LOAD_VAL 0xffffffff
Peter Pearseea686f52008-02-01 16:50:24 +000037
Nick Thompsone465cf22010-12-11 10:46:46 -050038#define TIM_CLK_DIV 16
Sergey Kubushync74b2102007-08-10 20:26:18 +020039
40int timer_init(void)
41{
42 /* We are using timer34 in unchained 32-bit mode, full speed */
Nick Thompson9868a362009-11-12 11:02:17 -050043 writel(0x0, &timer->tcr);
44 writel(0x0, &timer->tgcr);
45 writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &timer->tgcr);
46 writel(0x0, &timer->tim34);
47 writel(TIMER_LOAD_VAL, &timer->prd34);
Nick Thompson9868a362009-11-12 11:02:17 -050048 writel(2 << 22, &timer->tcr);
Simon Glassb3390512012-12-13 20:48:32 +000049 gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK / TIM_CLK_DIV;
Simon Glass5f707142012-12-13 20:48:36 +000050 gd->arch.timer_reset_value = 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +020051
52 return(0);
53}
54
Nick Thompsone465cf22010-12-11 10:46:46 -050055/*
56 * Get the current 64 bit timer tick count
57 */
58unsigned long long get_ticks(void)
Dirk Behme80c40b72008-03-26 09:53:29 +010059{
Nick Thompsone465cf22010-12-11 10:46:46 -050060 unsigned long now = readl(&timer->tim34);
Dirk Behme80c40b72008-03-26 09:53:29 +010061
Nick Thompsone465cf22010-12-11 10:46:46 -050062 /* increment tbu if tbl has rolled over */
Simon Glass66ee6922012-12-13 20:48:34 +000063 if (now < gd->arch.tbl)
Simon Glass8ff43b02012-12-13 20:48:33 +000064 gd->arch.tbu++;
Simon Glass66ee6922012-12-13 20:48:34 +000065 gd->arch.tbl = now;
Nick Thompsone465cf22010-12-11 10:46:46 -050066
Simon Glass66ee6922012-12-13 20:48:34 +000067 return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
Sergey Kubushync74b2102007-08-10 20:26:18 +020068}
69
70ulong get_timer(ulong base)
71{
Nick Thompsone465cf22010-12-11 10:46:46 -050072 unsigned long long timer_diff;
Sergey Kubushync74b2102007-08-10 20:26:18 +020073
Simon Glass5f707142012-12-13 20:48:36 +000074 timer_diff = get_ticks() - gd->arch.timer_reset_value;
Nick Thompsone465cf22010-12-11 10:46:46 -050075
Simon Glassb3390512012-12-13 20:48:32 +000076 return lldiv(timer_diff,
77 (gd->arch.timer_rate_hz / CONFIG_SYS_HZ)) - base;
Sergey Kubushync74b2102007-08-10 20:26:18 +020078}
79
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010080void __udelay(unsigned long usec)
Sergey Kubushync74b2102007-08-10 20:26:18 +020081{
Nick Thompsone465cf22010-12-11 10:46:46 -050082 unsigned long long endtime;
Sergey Kubushync74b2102007-08-10 20:26:18 +020083
Simon Glassb3390512012-12-13 20:48:32 +000084 endtime = lldiv((unsigned long long)usec * gd->arch.timer_rate_hz,
Christian Riesche21b3dfb2011-12-09 16:54:01 +010085 1000000UL);
Nick Thompsone465cf22010-12-11 10:46:46 -050086 endtime += get_ticks();
Sergey Kubushync74b2102007-08-10 20:26:18 +020087
Nick Thompsone465cf22010-12-11 10:46:46 -050088 while (get_ticks() < endtime)
89 ;
Sergey Kubushync74b2102007-08-10 20:26:18 +020090}
91
92/*
93 * This function is derived from PowerPC code (timebase clock frequency).
94 * On ARM it returns the number of timer ticks per second.
95 */
96ulong get_tbclk(void)
97{
Simon Glassb3390512012-12-13 20:48:32 +000098 return gd->arch.timer_rate_hz;
Sergey Kubushync74b2102007-08-10 20:26:18 +020099}
Heiko Schocherbf569ac2011-09-14 19:44:02 +0000100
101#ifdef CONFIG_HW_WATCHDOG
102static struct davinci_timer * const wdttimer =
103 (struct davinci_timer *)CONFIG_SYS_WDTTIMERBASE;
104
105/*
106 * See prufw2.pdf for using Timer as a WDT
107 */
108void davinci_hw_watchdog_enable(void)
109{
110 writel(0x0, &wdttimer->tcr);
111 writel(0x0, &wdttimer->tgcr);
112 /* TIMMODE = 2h */
113 writel(0x08 | 0x03 | ((TIM_CLK_DIV - 1) << 8), &wdttimer->tgcr);
114 writel(CONFIG_SYS_WDT_PERIOD_LOW, &wdttimer->prd12);
115 writel(CONFIG_SYS_WDT_PERIOD_HIGH, &wdttimer->prd34);
116 writel(2 << 22, &wdttimer->tcr);
117 writel(0x0, &wdttimer->tim12);
118 writel(0x0, &wdttimer->tim34);
119 /* set WDEN bit, WDKEY 0xa5c6 */
120 writel(0xa5c64000, &wdttimer->wdtcr);
121 /* clear counter register */
122 writel(0xda7e4000, &wdttimer->wdtcr);
123}
124
125void davinci_hw_watchdog_reset(void)
126{
127 writel(0xa5c64000, &wdttimer->wdtcr);
128 writel(0xda7e4000, &wdttimer->wdtcr);
129}
130#endif