common: Move get_tbclk() to time.h
[oweals/u-boot.git] / arch / arm / cpu / arm920t / imx / timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2002
4  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
5  * Marius Groeger <mgroeger@sysgo.de>
6  *
7  * (C) Copyright 2002
8  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9  * Alex Zuepke <azu@sysgo.de>
10  *
11  * (C) Copyright 2002
12  * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
13  */
14
15 #include <common.h>
16 #include <time.h>
17 #if defined (CONFIG_IMX)
18
19 #include <asm/arch/imx-regs.h>
20
21 int timer_init (void)
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
30         /* Reset the timer */
31         TCTL1 &= ~TCTL_TEN;
32         TCTL1 |= TCTL_TEN; /* Enable timer */
33
34         return (0);
35 }
36
37 /*
38  * timer without interrupts
39  */
40 static ulong get_timer_masked (void)
41 {
42         return TCN1;
43 }
44
45 ulong get_timer (ulong base)
46 {
47         return get_timer_masked() - base;
48 }
49
50 void __udelay (unsigned long usec)
51 {
52         ulong endtime = get_timer_masked() + usec;
53         signed long diff;
54
55         do {
56                 ulong now = get_timer_masked ();
57                 diff = endtime - now;
58         } while (diff >= 0);
59 }
60
61 /*
62  * This function is derived from PowerPC code (read timebase as long long).
63  * On ARM it just returns the timer value.
64  */
65 unsigned long long get_ticks(void)
66 {
67         return get_timer(0);
68 }
69
70 /*
71  * This function is derived from PowerPC code (timebase clock frequency).
72  * On ARM it returns the number of timer ticks per second.
73  */
74 ulong get_tbclk(void)
75 {
76         return CONFIG_SYS_HZ;
77 }
78
79 /*
80  * Reset the cpu by setting up the watchdog timer and let him time out
81  */
82 void reset_cpu (ulong ignored)
83 {
84         /* Disable watchdog and set Time-Out field to 0 */
85         WCR = 0x00000000;
86
87         /* Write Service Sequence */
88         WSR = 0x00005555;
89         WSR = 0x0000AAAA;
90
91         /* Enable watchdog */
92         WCR = 0x00000001;
93
94         while (1);
95         /*NOTREACHED*/
96 }
97
98 #endif /* defined (CONFIG_IMX) */