c4988f9031c59475493ab865bd55c346ea5602f3
[oweals/u-boot.git] / arch / m68k / cpu / mcf547x_8x / slicetimer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2007, 2012 Freescale Semiconductor, Inc.
4  * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
5  */
6
7 #include <common.h>
8 #include <init.h>
9 #include <irq_func.h>
10
11 #include <asm/timer.h>
12 #include <asm/immap.h>
13 #include <asm/io.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 static ulong timestamp;
18
19 #if defined(CONFIG_SLTTMR)
20 #ifndef CONFIG_SYS_UDELAY_BASE
21 #       error   "uDelay base not defined!"
22 #endif
23
24 #if !defined(CONFIG_SYS_TMR_BASE) || !defined(CONFIG_SYS_INTR_BASE) || !defined(CONFIG_SYS_TMRINTR_NO) || !defined(CONFIG_SYS_TMRINTR_MASK)
25 #       error   "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
26 #endif
27 extern void dtimer_intr_setup(void);
28
29 void __udelay(unsigned long usec)
30 {
31         slt_t *timerp = (slt_t *) (CONFIG_SYS_UDELAY_BASE);
32         u32 now, freq;
33
34         /* 1 us period */
35         freq = CONFIG_SYS_TIMER_PRESCALER;
36
37         /* Disable */
38         out_be32(&timerp->cr, 0);
39         out_be32(&timerp->tcnt, usec * freq);
40         out_be32(&timerp->cr, SLT_CR_TEN);
41
42         now = in_be32(&timerp->cnt);
43         while (now != 0)
44                 now = in_be32(&timerp->cnt);
45
46         setbits_be32(&timerp->sr, SLT_SR_ST);
47         out_be32(&timerp->cr, 0);
48 }
49
50 void dtimer_interrupt(void *not_used)
51 {
52         slt_t *timerp = (slt_t *) (CONFIG_SYS_TMR_BASE);
53
54         /* check for timer interrupt asserted */
55         if ((CONFIG_SYS_TMRPND_REG & CONFIG_SYS_TMRINTR_MASK) == CONFIG_SYS_TMRINTR_PEND) {
56                 setbits_be32(&timerp->sr, SLT_SR_ST);
57                 timestamp++;
58                 return;
59         }
60 }
61
62 int timer_init(void)
63 {
64         slt_t *timerp = (slt_t *) (CONFIG_SYS_TMR_BASE);
65
66         timestamp = 0;
67
68         /* disable timer */
69         out_be32(&timerp->cr, 0);
70         out_be32(&timerp->tcnt, 0);
71         /* clear status */
72         out_be32(&timerp->sr, SLT_SR_BE | SLT_SR_ST);
73
74         /* initialize and enable timer interrupt */
75         irq_install_handler(CONFIG_SYS_TMRINTR_NO, dtimer_interrupt, 0);
76
77         /* Interrupt every ms */
78         out_be32(&timerp->tcnt, 1000 * CONFIG_SYS_TIMER_PRESCALER);
79
80         dtimer_intr_setup();
81
82         /* set a period of 1us, set timer mode to restart and
83            enable timer and interrupt */
84         out_be32(&timerp->cr, SLT_CR_RUN | SLT_CR_IEN | SLT_CR_TEN);
85         return 0;
86 }
87
88 ulong get_timer(ulong base)
89 {
90         return (timestamp - base);
91 }
92
93 #endif                          /* CONFIG_SLTTMR */