blob: c0fc7c0f3ca189cabfd2ff378a824d40e71cdd3a [file] [log] [blame]
Michal Simek76316a32007-03-11 13:42:58 +01001/*
2 * (C) Copyright 2007 Michal Simek
3 *
4 * Michal SIMEK <monstr@monstr.eu>
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Michal Simek76316a32007-03-11 13:42:58 +01007 */
8
9#include <common.h>
Michal Simek9aa65ca2016-02-15 12:10:32 +010010#include <fdtdec.h>
Michal Simek76316a32007-03-11 13:42:58 +010011#include <asm/microblaze_timer.h>
Michal Simek42efed62007-05-07 17:22:25 +020012#include <asm/microblaze_intc.h>
Michal Simek76316a32007-03-11 13:42:58 +010013
Michal Simek9aa65ca2016-02-15 12:10:32 +010014DECLARE_GLOBAL_DATA_PTR;
15
Michal Simek76316a32007-03-11 13:42:58 +010016volatile int timestamp = 0;
Michal Simekbcbb0462012-06-29 13:46:54 +020017microblaze_timer_t *tmr;
Michal Simek76316a32007-03-11 13:42:58 +010018
Michal Simek76316a32007-03-11 13:42:58 +010019ulong get_timer (ulong base)
20{
Michal Simekbcbb0462012-06-29 13:46:54 +020021 if (tmr)
22 return timestamp - base;
23 return timestamp++ - base;
Michal Simek76316a32007-03-11 13:42:58 +010024}
25
Michal Simek779bf422012-06-29 13:42:28 +020026void __udelay(unsigned long usec)
27{
Michal Simekbcbb0462012-06-29 13:46:54 +020028 u32 i;
Michal Simek779bf422012-06-29 13:42:28 +020029
Michal Simekbcbb0462012-06-29 13:46:54 +020030 if (tmr) {
31 i = get_timer(0);
32 while ((get_timer(0) - i) < (usec / 1000))
33 ;
34 } else {
Michal Simek9aa65ca2016-02-15 12:10:32 +010035#ifndef CONFIG_OF_CONTROL
Michal Simekbcbb0462012-06-29 13:46:54 +020036 for (i = 0; i < (usec * XILINX_CLOCK_FREQ / 10000000); i++)
37 ;
Michal Simek9aa65ca2016-02-15 12:10:32 +010038#endif
Michal Simekbcbb0462012-06-29 13:46:54 +020039 }
Michal Simek779bf422012-06-29 13:42:28 +020040}
Michal Simek779bf422012-06-29 13:42:28 +020041
Michal Simek9d242742014-01-21 07:30:37 +010042#ifndef CONFIG_SPL_BUILD
Michal Simekbcbb0462012-06-29 13:46:54 +020043static void timer_isr(void *arg)
Michal Simek76316a32007-03-11 13:42:58 +010044{
45 timestamp++;
46 tmr->control = tmr->control | TIMER_INTERRUPT;
47}
48
Michal Simek5bbcb6c2010-04-16 11:37:41 +020049int timer_init (void)
Michal Simek76316a32007-03-11 13:42:58 +010050{
Michal Simekbcbb0462012-06-29 13:46:54 +020051 int irq = -1;
52 u32 preload = 0;
53 u32 ret = 0;
54
Michal Simek9aa65ca2016-02-15 12:10:32 +010055#ifdef CONFIG_OF_CONTROL
56 const void *blob = gd->fdt_blob;
57 int node = 0;
58 u32 cell[2];
59
60 debug("TIMER: Initialization\n");
61
62 node = fdt_node_offset_by_compatible(blob, node,
63 "xlnx,xps-timer-1.00.a");
64 if (node != -1) {
65 fdt_addr_t base = fdtdec_get_addr(blob, node, "reg");
66 if (base == FDT_ADDR_T_NONE)
67 return -1;
68
69 debug("TIMER: Base addr %lx\n", base);
70 tmr = (microblaze_timer_t *)base;
71
72 ret = fdtdec_get_int_array(blob, node, "interrupts",
73 cell, ARRAY_SIZE(cell));
74 if (ret)
75 return ret;
76
77 irq = cell[0];
78 debug("TIMER: IRQ %x\n", irq);
79
80 preload = fdtdec_get_int(blob, node, "clock-frequency", 0);
81 preload /= CONFIG_SYS_HZ;
82 } else {
83 return node;
84 }
85
86#else
Michal Simekbcbb0462012-06-29 13:46:54 +020087#if defined(CONFIG_SYS_TIMER_0_ADDR) && defined(CONFIG_SYS_INTC_0_NUM)
88 preload = XILINX_CLOCK_FREQ / CONFIG_SYS_HZ;
89 irq = CONFIG_SYS_TIMER_0_IRQ;
90 tmr = (microblaze_timer_t *) (CONFIG_SYS_TIMER_0_ADDR);
91#endif
Michal Simek9aa65ca2016-02-15 12:10:32 +010092#endif
Michal Simekbcbb0462012-06-29 13:46:54 +020093 if (tmr && preload && irq >= 0) {
94 tmr->loadreg = preload;
95 tmr->control = TIMER_INTERRUPT | TIMER_RESET;
96 tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\
97 TIMER_RELOAD | TIMER_DOWN_COUNT;
98 timestamp = 0;
99 ret = install_interrupt_handler (irq, timer_isr, (void *)tmr);
100 if (ret)
101 tmr = NULL;
102 }
Michal Simekbcbb0462012-06-29 13:46:54 +0200103 /* No problem if timer is not found/initialized */
Michal Simek5bbcb6c2010-04-16 11:37:41 +0200104 return 0;
Michal Simek76316a32007-03-11 13:42:58 +0100105}
Michal Simek9d242742014-01-21 07:30:37 +0100106#else
107int timer_init(void)
108{
109 return 0;
110}
111#endif
Stephan Linzb9f0b732012-02-22 22:39:57 +0100112
113/*
114 * This function is derived from PowerPC code (read timebase as long long).
115 * On Microblaze it just returns the timer value.
116 */
117unsigned long long get_ticks(void)
118{
119 return get_timer(0);
120}
121
122/*
123 * This function is derived from PowerPC code (timebase clock frequency).
124 * On Microblaze it returns the number of timer ticks per second.
125 */
126ulong get_tbclk(void)
127{
128 return CONFIG_SYS_HZ;
129}