21129a7901c5ef0c784bdbf2242648a238938418
[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 <cpu_func.h>
17 #include <time.h>
18 #if defined (CONFIG_IMX)
19
20 #include <asm/arch/imx-regs.h>
21
22 int timer_init (void)
23 {
24         int i;
25         /* setup GP Timer 1 */
26         TCTL1 = TCTL_SWR;
27         for ( i=0; i<100; i++) TCTL1 = 0; /* We have no udelay by now */
28         TPRER1 = get_PERCLK1() / 1000000; /* 1 MHz */
29         TCTL1 |= TCTL_FRR | (1<<1); /* Freerun Mode, PERCLK1 input */
30
31         /* Reset the timer */
32         TCTL1 &= ~TCTL_TEN;
33         TCTL1 |= TCTL_TEN; /* Enable timer */
34
35         return (0);
36 }
37
38 /*
39  * timer without interrupts
40  */
41 static ulong get_timer_masked (void)
42 {
43         return TCN1;
44 }
45
46 ulong get_timer (ulong base)
47 {
48         return get_timer_masked() - base;
49 }
50
51 void __udelay (unsigned long usec)
52 {
53         ulong endtime = get_timer_masked() + usec;
54         signed long diff;
55
56         do {
57                 ulong now = get_timer_masked ();
58                 diff = endtime - now;
59         } while (diff >= 0);
60 }
61
62 /*
63  * This function is derived from PowerPC code (read timebase as long long).
64  * On ARM it just returns the timer value.
65  */
66 unsigned long long get_ticks(void)
67 {
68         return get_timer(0);
69 }
70
71 /*
72  * This function is derived from PowerPC code (timebase clock frequency).
73  * On ARM it returns the number of timer ticks per second.
74  */
75 ulong get_tbclk(void)
76 {
77         return CONFIG_SYS_HZ;
78 }
79
80 /*
81  * Reset the cpu by setting up the watchdog timer and let him time out
82  */
83 void reset_cpu(ulong ignored)
84 {
85         /* Disable watchdog and set Time-Out field to 0 */
86         WCR = 0x00000000;
87
88         /* Write Service Sequence */
89         WSR = 0x00005555;
90         WSR = 0x0000AAAA;
91
92         /* Enable watchdog */
93         WCR = 0x00000001;
94
95         while (1);
96         /*NOTREACHED*/
97 }
98
99 #endif /* defined (CONFIG_IMX) */