cd4aba667edf2d2a6958de0d0ee5da6f5d77b1f3
[oweals/u-boot.git] / arch / microblaze / cpu / timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2007 Michal Simek
4  *
5  * Michal  SIMEK <monstr@monstr.eu>
6  */
7
8 #include <common.h>
9 #include <fdtdec.h>
10 #include <init.h>
11 #include <log.h>
12 #include <time.h>
13 #include <asm/microblaze_timer.h>
14 #include <asm/microblaze_intc.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 volatile int timestamp = 0;
19 microblaze_timer_t *tmr;
20
21 ulong get_timer (ulong base)
22 {
23         if (tmr)
24                 return timestamp - base;
25         return timestamp++ - base;
26 }
27
28 void __udelay(unsigned long usec)
29 {
30         u32 i;
31
32         if (tmr) {
33                 i = get_timer(0);
34                 while ((get_timer(0) - i) < (usec / 1000))
35                         ;
36         }
37 }
38
39 #ifndef CONFIG_SPL_BUILD
40 static void timer_isr(void *arg)
41 {
42         timestamp++;
43         tmr->control = tmr->control | TIMER_INTERRUPT;
44 }
45
46 int timer_init (void)
47 {
48         int irq = -1;
49         u32 preload = 0;
50         u32 ret = 0;
51         const void *blob = gd->fdt_blob;
52         int node = 0;
53         u32 cell[2];
54
55         debug("TIMER: Initialization\n");
56
57         /* Do not init before relocation */
58         if (!(gd->flags & GD_FLG_RELOC))
59                 return 0;
60
61         node = fdt_node_offset_by_compatible(blob, node,
62                                 "xlnx,xps-timer-1.00.a");
63         if (node != -1) {
64                 fdt_addr_t base = fdtdec_get_addr(blob, node, "reg");
65                 if (base == FDT_ADDR_T_NONE)
66                         return -1;
67
68                 debug("TIMER: Base addr %lx\n", base);
69                 tmr = (microblaze_timer_t *)base;
70
71                 ret = fdtdec_get_int_array(blob, node, "interrupts",
72                                             cell, ARRAY_SIZE(cell));
73                 if (ret)
74                         return ret;
75
76                 irq = cell[0];
77                 debug("TIMER: IRQ %x\n", irq);
78
79                 preload = fdtdec_get_int(blob, node, "clock-frequency", 0);
80                 preload /= CONFIG_SYS_HZ;
81         } else {
82                 return node;
83         }
84
85         if (tmr && preload && irq >= 0) {
86                 tmr->loadreg = preload;
87                 tmr->control = TIMER_INTERRUPT | TIMER_RESET;
88                 tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\
89                                         TIMER_RELOAD | TIMER_DOWN_COUNT;
90                 timestamp = 0;
91                 ret = install_interrupt_handler (irq, timer_isr, (void *)tmr);
92                 if (ret)
93                         tmr = NULL;
94         }
95         /* No problem if timer is not found/initialized */
96         return 0;
97 }
98 #else
99 int timer_init(void)
100 {
101         return 0;
102 }
103 #endif
104
105 /*
106  * This function is derived from PowerPC code (read timebase as long long).
107  * On Microblaze it just returns the timer value.
108  */
109 unsigned long long get_ticks(void)
110 {
111         return get_timer(0);
112 }
113
114 /*
115  * This function is derived from PowerPC code (timebase clock frequency).
116  * On Microblaze it returns the number of timer ticks per second.
117  */
118 ulong get_tbclk(void)
119 {
120         return CONFIG_SYS_HZ;
121 }