common: Drop init.h from common header
[pandora-u-boot.git] / arch / m68k / lib / time.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2003 Josef Baumgartner <josef.baumgartner@telex.de>
4  *
5  * (C) Copyright 2000
6  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7  */
8
9 #include <common.h>
10 #include <init.h>
11 #include <irq_func.h>
12 #include <time.h>
13
14 #include <asm/timer.h>
15 #include <asm/immap.h>
16 #include <watchdog.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 static volatile ulong timestamp = 0;
21
22 #ifndef CONFIG_SYS_WATCHDOG_FREQ
23 #define CONFIG_SYS_WATCHDOG_FREQ (CONFIG_SYS_HZ / 2)
24 #endif
25
26 #if defined(CONFIG_MCFTMR)
27 #ifndef CONFIG_SYS_UDELAY_BASE
28 #       error   "uDelay base not defined!"
29 #endif
30
31 #if !defined(CONFIG_SYS_TMR_BASE) || !defined(CONFIG_SYS_INTR_BASE) || !defined(CONFIG_SYS_TMRINTR_NO) || !defined(CONFIG_SYS_TMRINTR_MASK)
32 #       error   "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
33 #endif
34 extern void dtimer_intr_setup(void);
35
36 void __udelay(unsigned long usec)
37 {
38         volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_UDELAY_BASE);
39         uint start, now, tmp;
40
41         while (usec > 0) {
42                 if (usec > 65000)
43                         tmp = 65000;
44                 else
45                         tmp = usec;
46                 usec = usec - tmp;
47
48                 /* Set up TIMER 3 as timebase clock */
49                 timerp->tmr = DTIM_DTMR_RST_RST;
50                 timerp->tcn = 0;
51                 /* set period to 1 us */
52                 timerp->tmr =
53                     CONFIG_SYS_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 | DTIM_DTMR_FRR |
54                     DTIM_DTMR_RST_EN;
55
56                 start = now = timerp->tcn;
57                 while (now < start + tmp)
58                         now = timerp->tcn;
59         }
60 }
61
62 void dtimer_interrupt(void *not_used)
63 {
64         volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_TMR_BASE);
65
66         /* check for timer interrupt asserted */
67         if ((CONFIG_SYS_TMRPND_REG & CONFIG_SYS_TMRINTR_MASK) == CONFIG_SYS_TMRINTR_PEND) {
68                 timerp->ter = (DTIM_DTER_CAP | DTIM_DTER_REF);
69                 timestamp++;
70
71                 #if defined(CONFIG_WATCHDOG) || defined (CONFIG_HW_WATCHDOG)
72                 if ((timestamp % (CONFIG_SYS_WATCHDOG_FREQ)) == 0) {
73                         WATCHDOG_RESET ();
74                 }
75                 #endif    /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */
76                 return;
77         }
78 }
79
80 int timer_init(void)
81 {
82         volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_TMR_BASE);
83
84         timestamp = 0;
85
86         timerp->tcn = 0;
87         timerp->trr = 0;
88
89         /* Set up TIMER 4 as clock */
90         timerp->tmr = DTIM_DTMR_RST_RST;
91
92         /* initialize and enable timer interrupt */
93         irq_install_handler(CONFIG_SYS_TMRINTR_NO, dtimer_interrupt, 0);
94
95         timerp->tcn = 0;
96         timerp->trr = 1000;     /* Interrupt every ms */
97
98         dtimer_intr_setup();
99
100         /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
101         timerp->tmr = CONFIG_SYS_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 |
102             DTIM_DTMR_FRR | DTIM_DTMR_ORRI | DTIM_DTMR_RST_EN;
103
104         return 0;
105 }
106
107 ulong get_timer(ulong base)
108 {
109         return (timestamp - base);
110 }
111
112 #endif                          /* CONFIG_MCFTMR */
113
114 /*
115  * This function is derived from PowerPC code (read timebase as long long).
116  * On M68K it just returns the timer value.
117  */
118 unsigned long long get_ticks(void)
119 {
120         return get_timer(0);
121 }
122
123 unsigned long usec2ticks(unsigned long usec)
124 {
125         return get_timer(usec);
126 }
127
128 /*
129  * This function is derived from PowerPC code (timebase clock frequency).
130  * On M68K it returns the number of timer ticks per second.
131  */
132 ulong get_tbclk(void)
133 {
134         return CONFIG_SYS_HZ;
135 }