blob: 69ae6d4d873eb553dbfd4541478bdc1850eaf5e1 [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>
10#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
13volatile int timestamp = 0;
Michal Simekbcbb0462012-06-29 13:46:54 +020014microblaze_timer_t *tmr;
Michal Simek76316a32007-03-11 13:42:58 +010015
Michal Simek76316a32007-03-11 13:42:58 +010016ulong get_timer (ulong base)
17{
Michal Simekbcbb0462012-06-29 13:46:54 +020018 if (tmr)
19 return timestamp - base;
20 return timestamp++ - base;
Michal Simek76316a32007-03-11 13:42:58 +010021}
22
Michal Simek779bf422012-06-29 13:42:28 +020023void __udelay(unsigned long usec)
24{
Michal Simekbcbb0462012-06-29 13:46:54 +020025 u32 i;
Michal Simek779bf422012-06-29 13:42:28 +020026
Michal Simekbcbb0462012-06-29 13:46:54 +020027 if (tmr) {
28 i = get_timer(0);
29 while ((get_timer(0) - i) < (usec / 1000))
30 ;
31 } else {
32 for (i = 0; i < (usec * XILINX_CLOCK_FREQ / 10000000); i++)
33 ;
34 }
Michal Simek779bf422012-06-29 13:42:28 +020035}
Michal Simek779bf422012-06-29 13:42:28 +020036
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;
48
49#if defined(CONFIG_SYS_TIMER_0_ADDR) && defined(CONFIG_SYS_INTC_0_NUM)
50 preload = XILINX_CLOCK_FREQ / CONFIG_SYS_HZ;
51 irq = CONFIG_SYS_TIMER_0_IRQ;
52 tmr = (microblaze_timer_t *) (CONFIG_SYS_TIMER_0_ADDR);
53#endif
54
55 if (tmr && preload && irq >= 0) {
56 tmr->loadreg = preload;
57 tmr->control = TIMER_INTERRUPT | TIMER_RESET;
58 tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\
59 TIMER_RELOAD | TIMER_DOWN_COUNT;
60 timestamp = 0;
61 ret = install_interrupt_handler (irq, timer_isr, (void *)tmr);
62 if (ret)
63 tmr = NULL;
64 }
65
66 /* No problem if timer is not found/initialized */
Michal Simek5bbcb6c2010-04-16 11:37:41 +020067 return 0;
Michal Simek76316a32007-03-11 13:42:58 +010068}
Stephan Linzb9f0b732012-02-22 22:39:57 +010069
70/*
71 * This function is derived from PowerPC code (read timebase as long long).
72 * On Microblaze it just returns the timer value.
73 */
74unsigned long long get_ticks(void)
75{
76 return get_timer(0);
77}
78
79/*
80 * This function is derived from PowerPC code (timebase clock frequency).
81 * On Microblaze it returns the number of timer ticks per second.
82 */
83ulong get_tbclk(void)
84{
85 return CONFIG_SYS_HZ;
86}