Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 1 | /* |
| 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 Zundel | f1b3f2b | 2009-05-13 10:54:10 +0200 | [diff] [blame] | 14 | * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de> |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 15 | * |
| 16 | * (C) Copyright 2004 |
| 17 | * Philippe Robin, ARM Ltd. <philippe.robin@arm.com> |
| 18 | * |
| 19 | * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net> |
| 20 | * |
| 21 | * See file CREDITS for list of people who contributed to this |
| 22 | * project. |
| 23 | * |
| 24 | * This program is free software; you can redistribute it and/or |
| 25 | * modify it under the terms of the GNU General Public License as |
| 26 | * published by the Free Software Foundation; either version 2 of |
| 27 | * the License, or (at your option) any later version. |
| 28 | * |
| 29 | * This program is distributed in the hope that it will be useful, |
| 30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 32 | * GNU General Public License for more details. |
| 33 | * |
| 34 | * You should have received a copy of the GNU General Public License |
| 35 | * along with this program; if not, write to the Free Software |
| 36 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 37 | * MA 02111-1307 USA |
| 38 | */ |
| 39 | |
| 40 | #include <common.h> |
Nick Thompson | 283398b | 2009-11-12 11:02:17 -0500 | [diff] [blame] | 41 | #include <asm/io.h> |
Heiko Schocher | 420cd0a | 2011-09-14 19:44:00 +0000 | [diff] [blame] | 42 | #include <asm/arch/timer_defs.h> |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 43 | |
Nick Thompson | 1c92d8e | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 44 | DECLARE_GLOBAL_DATA_PTR; |
| 45 | |
Nick Thompson | 283398b | 2009-11-12 11:02:17 -0500 | [diff] [blame] | 46 | static struct davinci_timer * const timer = |
| 47 | (struct davinci_timer *)CONFIG_SYS_TIMERBASE; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 48 | |
Nick Thompson | 1c92d8e | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 49 | #define TIMER_LOAD_VAL 0xffffffff |
Peter Pearse | e768098 | 2008-02-01 16:50:24 +0000 | [diff] [blame] | 50 | |
Nick Thompson | 1c92d8e | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 51 | #define TIM_CLK_DIV 16 |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 52 | |
| 53 | int timer_init(void) |
| 54 | { |
| 55 | /* We are using timer34 in unchained 32-bit mode, full speed */ |
Nick Thompson | 283398b | 2009-11-12 11:02:17 -0500 | [diff] [blame] | 56 | writel(0x0, &timer->tcr); |
| 57 | writel(0x0, &timer->tgcr); |
| 58 | writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &timer->tgcr); |
| 59 | writel(0x0, &timer->tim34); |
| 60 | writel(TIMER_LOAD_VAL, &timer->prd34); |
Nick Thompson | 283398b | 2009-11-12 11:02:17 -0500 | [diff] [blame] | 61 | writel(2 << 22, &timer->tcr); |
Nick Thompson | 1c92d8e | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 62 | gd->timer_rate_hz = CONFIG_SYS_HZ_CLOCK / TIM_CLK_DIV; |
| 63 | gd->timer_reset_value = 0; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 64 | |
| 65 | return(0); |
| 66 | } |
| 67 | |
Nick Thompson | 1c92d8e | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 68 | /* |
| 69 | * Get the current 64 bit timer tick count |
| 70 | */ |
| 71 | unsigned long long get_ticks(void) |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 72 | { |
Nick Thompson | 1c92d8e | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 73 | unsigned long now = readl(&timer->tim34); |
| 74 | |
| 75 | /* increment tbu if tbl has rolled over */ |
| 76 | if (now < gd->tbl) |
| 77 | gd->tbu++; |
| 78 | gd->tbl = now; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 79 | |
Nick Thompson | 1c92d8e | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 80 | return (((unsigned long long)gd->tbu) << 32) | gd->tbl; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Dirk Behme | 866c321 | 2008-03-26 09:53:29 +0100 | [diff] [blame] | 83 | ulong get_timer(ulong base) |
| 84 | { |
Nick Thompson | 1c92d8e | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 85 | unsigned long long timer_diff; |
Dirk Behme | 866c321 | 2008-03-26 09:53:29 +0100 | [diff] [blame] | 86 | |
Nick Thompson | 1c92d8e | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 87 | timer_diff = get_ticks() - gd->timer_reset_value; |
| 88 | |
| 89 | return (timer_diff / (gd->timer_rate_hz / CONFIG_SYS_HZ)) - base; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 92 | void __udelay(unsigned long usec) |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 93 | { |
Nick Thompson | 1c92d8e | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 94 | unsigned long long endtime; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 95 | |
Nick Thompson | 1c92d8e | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 96 | endtime = ((unsigned long long)usec * gd->timer_rate_hz) / 1000000UL; |
| 97 | endtime += get_ticks(); |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 98 | |
Nick Thompson | 1c92d8e | 2010-12-11 10:46:46 -0500 | [diff] [blame] | 99 | while (get_ticks() < endtime) |
| 100 | ; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | * This function is derived from PowerPC code (timebase clock frequency). |
| 105 | * On ARM it returns the number of timer ticks per second. |
| 106 | */ |
| 107 | ulong get_tbclk(void) |
| 108 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 109 | return CONFIG_SYS_HZ; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 110 | } |