wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 4 | * Marius Groeger <mgroeger@sysgo.de> |
| 5 | * |
| 6 | * (C) Copyright 2002 |
| 7 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 8 | * Alex Zuepke <azu@sysgo.de> |
| 9 | * |
| 10 | * (C) Copyright 2002 |
Detlev Zundel | 792a09e | 2009-05-13 10:54:10 +0200 | [diff] [blame] | 11 | * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de> |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 12 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 13 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <common.h> |
| 17 | #if defined (CONFIG_IMX) |
| 18 | |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 19 | #include <asm/arch/imx-regs.h> |
| 20 | |
Jean-Christophe PLAGNIOL-VILLARD | b54384e | 2009-05-15 23:47:02 +0200 | [diff] [blame] | 21 | int timer_init (void) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 22 | { |
| 23 | int i; |
| 24 | /* setup GP Timer 1 */ |
| 25 | TCTL1 = TCTL_SWR; |
| 26 | for ( i=0; i<100; i++) TCTL1 = 0; /* We have no udelay by now */ |
| 27 | TPRER1 = get_PERCLK1() / 1000000; /* 1 MHz */ |
| 28 | TCTL1 |= TCTL_FRR | (1<<1); /* Freerun Mode, PERCLK1 input */ |
| 29 | |
Graeme Russ | 17659d7 | 2011-07-15 02:21:14 +0000 | [diff] [blame] | 30 | /* Reset the timer */ |
| 31 | TCTL1 &= ~TCTL_TEN; |
| 32 | TCTL1 |= TCTL_TEN; /* Enable timer */ |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 33 | |
| 34 | return (0); |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * timer without interrupts |
| 39 | */ |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 40 | ulong get_timer (ulong base) |
| 41 | { |
Andrew Dyer | 274737e | 2008-09-12 02:20:46 +0200 | [diff] [blame] | 42 | return get_timer_masked() - base; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 43 | } |
| 44 | |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 45 | ulong get_timer_masked (void) |
| 46 | { |
| 47 | return TCN1; |
| 48 | } |
| 49 | |
| 50 | void udelay_masked (unsigned long usec) |
| 51 | { |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 52 | ulong endtime = get_timer_masked() + usec; |
| 53 | signed long diff; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 54 | |
wdenk | 101e8df | 2005-04-04 12:08:28 +0000 | [diff] [blame] | 55 | do { |
| 56 | ulong now = get_timer_masked (); |
| 57 | diff = endtime - now; |
| 58 | } while (diff >= 0); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 61 | void __udelay (unsigned long usec) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 62 | { |
| 63 | udelay_masked(usec); |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * This function is derived from PowerPC code (read timebase as long long). |
| 68 | * On ARM it just returns the timer value. |
| 69 | */ |
| 70 | unsigned long long get_ticks(void) |
| 71 | { |
| 72 | return get_timer(0); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * This function is derived from PowerPC code (timebase clock frequency). |
| 77 | * On ARM it returns the number of timer ticks per second. |
| 78 | */ |
| 79 | ulong get_tbclk (void) |
| 80 | { |
| 81 | ulong tbclk; |
| 82 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 83 | tbclk = CONFIG_SYS_HZ; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 84 | |
| 85 | return tbclk; |
| 86 | } |
| 87 | |
wdenk | b304c96 | 2005-04-05 22:30:50 +0000 | [diff] [blame] | 88 | /* |
| 89 | * Reset the cpu by setting up the watchdog timer and let him time out |
| 90 | */ |
| 91 | void reset_cpu (ulong ignored) |
| 92 | { |
| 93 | /* Disable watchdog and set Time-Out field to 0 */ |
| 94 | WCR = 0x00000000; |
| 95 | |
| 96 | /* Write Service Sequence */ |
| 97 | WSR = 0x00005555; |
| 98 | WSR = 0x0000AAAA; |
| 99 | |
| 100 | /* Enable watchdog */ |
| 101 | WCR = 0x00000001; |
| 102 | |
| 103 | while (1); |
| 104 | /*NOTREACHED*/ |
| 105 | } |
| 106 | |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 107 | #endif /* defined (CONFIG_IMX) */ |