blob: ef229023bf08958647570f350312dc6eb9ae4965 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek76316a32007-03-11 13:42:58 +01002/*
3 * (C) Copyright 2007 Michal Simek
4 *
5 * Michal SIMEK <monstr@monstr.eu>
Michal Simek76316a32007-03-11 13:42:58 +01006 */
7
8#include <common.h>
Michal Simek9aa65ca2016-02-15 12:10:32 +01009#include <fdtdec.h>
Michal Simek76316a32007-03-11 13:42:58 +010010#include <asm/microblaze_timer.h>
Michal Simek42efed62007-05-07 17:22:25 +020011#include <asm/microblaze_intc.h>
Michal Simek76316a32007-03-11 13:42:58 +010012
Michal Simek9aa65ca2016-02-15 12:10:32 +010013DECLARE_GLOBAL_DATA_PTR;
14
Michal Simek76316a32007-03-11 13:42:58 +010015volatile int timestamp = 0;
Michal Simekbcbb0462012-06-29 13:46:54 +020016microblaze_timer_t *tmr;
Michal Simek76316a32007-03-11 13:42:58 +010017
Michal Simek76316a32007-03-11 13:42:58 +010018ulong get_timer (ulong base)
19{
Michal Simekbcbb0462012-06-29 13:46:54 +020020 if (tmr)
21 return timestamp - base;
22 return timestamp++ - base;
Michal Simek76316a32007-03-11 13:42:58 +010023}
24
Michal Simek779bf422012-06-29 13:42:28 +020025void __udelay(unsigned long usec)
26{
Michal Simekbcbb0462012-06-29 13:46:54 +020027 u32 i;
Michal Simek779bf422012-06-29 13:42:28 +020028
Michal Simekbcbb0462012-06-29 13:46:54 +020029 if (tmr) {
30 i = get_timer(0);
31 while ((get_timer(0) - i) < (usec / 1000))
32 ;
Michal Simekbcbb0462012-06-29 13:46:54 +020033 }
Michal Simek779bf422012-06-29 13:42:28 +020034}
Michal Simek779bf422012-06-29 13:42:28 +020035
Michal Simek9d242742014-01-21 07:30:37 +010036#ifndef CONFIG_SPL_BUILD
Michal Simekbcbb0462012-06-29 13:46:54 +020037static void timer_isr(void *arg)
Michal Simek76316a32007-03-11 13:42:58 +010038{
39 timestamp++;
40 tmr->control = tmr->control | TIMER_INTERRUPT;
41}
42
Michal Simek5bbcb6c2010-04-16 11:37:41 +020043int timer_init (void)
Michal Simek76316a32007-03-11 13:42:58 +010044{
Michal Simekbcbb0462012-06-29 13:46:54 +020045 int irq = -1;
46 u32 preload = 0;
47 u32 ret = 0;
Michal Simek9aa65ca2016-02-15 12:10:32 +010048 const void *blob = gd->fdt_blob;
49 int node = 0;
50 u32 cell[2];
51
52 debug("TIMER: Initialization\n");
53
54 node = fdt_node_offset_by_compatible(blob, node,
55 "xlnx,xps-timer-1.00.a");
56 if (node != -1) {
57 fdt_addr_t base = fdtdec_get_addr(blob, node, "reg");
58 if (base == FDT_ADDR_T_NONE)
59 return -1;
60
61 debug("TIMER: Base addr %lx\n", base);
62 tmr = (microblaze_timer_t *)base;
63
64 ret = fdtdec_get_int_array(blob, node, "interrupts",
65 cell, ARRAY_SIZE(cell));
66 if (ret)
67 return ret;
68
69 irq = cell[0];
70 debug("TIMER: IRQ %x\n", irq);
71
72 preload = fdtdec_get_int(blob, node, "clock-frequency", 0);
73 preload /= CONFIG_SYS_HZ;
74 } else {
75 return node;
76 }
77
Michal Simekbcbb0462012-06-29 13:46:54 +020078 if (tmr && preload && irq >= 0) {
79 tmr->loadreg = preload;
80 tmr->control = TIMER_INTERRUPT | TIMER_RESET;
81 tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\
82 TIMER_RELOAD | TIMER_DOWN_COUNT;
83 timestamp = 0;
84 ret = install_interrupt_handler (irq, timer_isr, (void *)tmr);
85 if (ret)
86 tmr = NULL;
87 }
Michal Simekbcbb0462012-06-29 13:46:54 +020088 /* No problem if timer is not found/initialized */
Michal Simek5bbcb6c2010-04-16 11:37:41 +020089 return 0;
Michal Simek76316a32007-03-11 13:42:58 +010090}
Michal Simek9d242742014-01-21 07:30:37 +010091#else
92int timer_init(void)
93{
94 return 0;
95}
96#endif
Stephan Linzb9f0b732012-02-22 22:39:57 +010097
98/*
99 * This function is derived from PowerPC code (read timebase as long long).
100 * On Microblaze it just returns the timer value.
101 */
102unsigned long long get_ticks(void)
103{
104 return get_timer(0);
105}
106
107/*
108 * This function is derived from PowerPC code (timebase clock frequency).
109 * On Microblaze it returns the number of timer ticks per second.
110 */
111ulong get_tbclk(void)
112{
113 return CONFIG_SYS_HZ;
114}