Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 2 | /* |
| 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 Zundel | 792a09e | 2009-05-13 10:54:10 +0200 | [diff] [blame] | 15 | * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de> |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 16 | * |
| 17 | * (C) Copyright 2004 |
| 18 | * Philippe Robin, ARM Ltd. <philippe.robin@arm.com> |
| 19 | * |
| 20 | * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net> |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <common.h> |
Simon Glass | 691d719 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 24 | #include <init.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 25 | #include <time.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 26 | #include <asm/global_data.h> |
Nick Thompson | 9868a36 | 2009-11-12 11:02:17 -0500 | [diff] [blame] | 27 | #include <asm/io.h> |
Heiko Schocher | de23e72 | 2011-09-14 19:44:00 +0000 | [diff] [blame] | 28 | #include <asm/arch/timer_defs.h> |
Christian Riesch | e21b3dfb | 2011-12-09 16:54:01 +0100 | [diff] [blame] | 29 | #include <div64.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 30 | #include <linux/delay.h> |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 31 | |
Nick Thompson | e465cf2 | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
Nick Thompson | 9868a36 | 2009-11-12 11:02:17 -0500 | [diff] [blame] | 34 | static struct davinci_timer * const timer = |
| 35 | (struct davinci_timer *)CONFIG_SYS_TIMERBASE; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 36 | |
Nick Thompson | e465cf2 | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 37 | #define TIMER_LOAD_VAL 0xffffffff |
Peter Pearse | ea686f5 | 2008-02-01 16:50:24 +0000 | [diff] [blame] | 38 | |
Nick Thompson | e465cf2 | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 39 | #define TIM_CLK_DIV 16 |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 40 | |
| 41 | int timer_init(void) |
| 42 | { |
| 43 | /* We are using timer34 in unchained 32-bit mode, full speed */ |
Nick Thompson | 9868a36 | 2009-11-12 11:02:17 -0500 | [diff] [blame] | 44 | writel(0x0, &timer->tcr); |
| 45 | writel(0x0, &timer->tgcr); |
| 46 | writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &timer->tgcr); |
| 47 | writel(0x0, &timer->tim34); |
| 48 | writel(TIMER_LOAD_VAL, &timer->prd34); |
Nick Thompson | 9868a36 | 2009-11-12 11:02:17 -0500 | [diff] [blame] | 49 | writel(2 << 22, &timer->tcr); |
Simon Glass | b339051 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 50 | gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK / TIM_CLK_DIV; |
Simon Glass | 5f70714 | 2012-12-13 20:48:36 +0000 | [diff] [blame] | 51 | gd->arch.timer_reset_value = 0; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 52 | |
| 53 | return(0); |
| 54 | } |
| 55 | |
Nick Thompson | e465cf2 | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 56 | /* |
| 57 | * Get the current 64 bit timer tick count |
| 58 | */ |
| 59 | unsigned long long get_ticks(void) |
Dirk Behme | 80c40b7 | 2008-03-26 09:53:29 +0100 | [diff] [blame] | 60 | { |
Nick Thompson | e465cf2 | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 61 | unsigned long now = readl(&timer->tim34); |
Dirk Behme | 80c40b7 | 2008-03-26 09:53:29 +0100 | [diff] [blame] | 62 | |
Nick Thompson | e465cf2 | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 63 | /* increment tbu if tbl has rolled over */ |
Simon Glass | 66ee692 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 64 | if (now < gd->arch.tbl) |
Simon Glass | 8ff43b0 | 2012-12-13 20:48:33 +0000 | [diff] [blame] | 65 | gd->arch.tbu++; |
Simon Glass | 66ee692 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 66 | gd->arch.tbl = now; |
Nick Thompson | e465cf2 | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 67 | |
Simon Glass | 66ee692 | 2012-12-13 20:48:34 +0000 | [diff] [blame] | 68 | return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | ulong get_timer(ulong base) |
| 72 | { |
Nick Thompson | e465cf2 | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 73 | unsigned long long timer_diff; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 74 | |
Simon Glass | 5f70714 | 2012-12-13 20:48:36 +0000 | [diff] [blame] | 75 | timer_diff = get_ticks() - gd->arch.timer_reset_value; |
Nick Thompson | e465cf2 | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 76 | |
Simon Glass | b339051 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 77 | return lldiv(timer_diff, |
| 78 | (gd->arch.timer_rate_hz / CONFIG_SYS_HZ)) - base; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 81 | void __udelay(unsigned long usec) |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 82 | { |
Nick Thompson | e465cf2 | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 83 | unsigned long long endtime; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 84 | |
Simon Glass | b339051 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 85 | endtime = lldiv((unsigned long long)usec * gd->arch.timer_rate_hz, |
Christian Riesch | e21b3dfb | 2011-12-09 16:54:01 +0100 | [diff] [blame] | 86 | 1000000UL); |
Nick Thompson | e465cf2 | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 87 | endtime += get_ticks(); |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 88 | |
Nick Thompson | e465cf2 | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 89 | while (get_ticks() < endtime) |
| 90 | ; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* |
| 94 | * This function is derived from PowerPC code (timebase clock frequency). |
| 95 | * On ARM it returns the number of timer ticks per second. |
| 96 | */ |
| 97 | ulong get_tbclk(void) |
| 98 | { |
Simon Glass | b339051 | 2012-12-13 20:48:32 +0000 | [diff] [blame] | 99 | return gd->arch.timer_rate_hz; |
Sergey Kubushyn | c74b210 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 100 | } |
Heiko Schocher | bf569ac | 2011-09-14 19:44:02 +0000 | [diff] [blame] | 101 | |
| 102 | #ifdef CONFIG_HW_WATCHDOG |
| 103 | static struct davinci_timer * const wdttimer = |
| 104 | (struct davinci_timer *)CONFIG_SYS_WDTTIMERBASE; |
| 105 | |
| 106 | /* |
| 107 | * See prufw2.pdf for using Timer as a WDT |
| 108 | */ |
| 109 | void davinci_hw_watchdog_enable(void) |
| 110 | { |
| 111 | writel(0x0, &wdttimer->tcr); |
| 112 | writel(0x0, &wdttimer->tgcr); |
| 113 | /* TIMMODE = 2h */ |
| 114 | writel(0x08 | 0x03 | ((TIM_CLK_DIV - 1) << 8), &wdttimer->tgcr); |
| 115 | writel(CONFIG_SYS_WDT_PERIOD_LOW, &wdttimer->prd12); |
| 116 | writel(CONFIG_SYS_WDT_PERIOD_HIGH, &wdttimer->prd34); |
| 117 | writel(2 << 22, &wdttimer->tcr); |
| 118 | writel(0x0, &wdttimer->tim12); |
| 119 | writel(0x0, &wdttimer->tim34); |
| 120 | /* set WDEN bit, WDKEY 0xa5c6 */ |
| 121 | writel(0xa5c64000, &wdttimer->wdtcr); |
| 122 | /* clear counter register */ |
| 123 | writel(0xda7e4000, &wdttimer->wdtcr); |
| 124 | } |
| 125 | |
| 126 | void davinci_hw_watchdog_reset(void) |
| 127 | { |
| 128 | writel(0xa5c64000, &wdttimer->wdtcr); |
| 129 | writel(0xda7e4000, &wdttimer->wdtcr); |
| 130 | } |
| 131 | #endif |