Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007 Michal Simek |
| 3 | * |
| 4 | * Michal SIMEK <monstr@monstr.eu> |
| 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <asm/microblaze_timer.h> |
Michal Simek | 42efed6 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 11 | #include <asm/microblaze_intc.h> |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 12 | |
| 13 | volatile int timestamp = 0; |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 14 | microblaze_timer_t *tmr; |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 15 | |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 16 | ulong get_timer (ulong base) |
| 17 | { |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 18 | if (tmr) |
| 19 | return timestamp - base; |
| 20 | return timestamp++ - base; |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 21 | } |
| 22 | |
Michal Simek | 779bf42 | 2012-06-29 13:42:28 +0200 | [diff] [blame] | 23 | void __udelay(unsigned long usec) |
| 24 | { |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 25 | u32 i; |
Michal Simek | 779bf42 | 2012-06-29 13:42:28 +0200 | [diff] [blame] | 26 | |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 27 | 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 Simek | 779bf42 | 2012-06-29 13:42:28 +0200 | [diff] [blame] | 35 | } |
Michal Simek | 779bf42 | 2012-06-29 13:42:28 +0200 | [diff] [blame] | 36 | |
Michal Simek | 9d24274 | 2014-01-21 07:30:37 +0100 | [diff] [blame^] | 37 | #ifndef CONFIG_SPL_BUILD |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 38 | static void timer_isr(void *arg) |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 39 | { |
| 40 | timestamp++; |
| 41 | tmr->control = tmr->control | TIMER_INTERRUPT; |
| 42 | } |
| 43 | |
Michal Simek | 5bbcb6c | 2010-04-16 11:37:41 +0200 | [diff] [blame] | 44 | int timer_init (void) |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 45 | { |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 46 | int irq = -1; |
| 47 | u32 preload = 0; |
| 48 | u32 ret = 0; |
| 49 | |
| 50 | #if defined(CONFIG_SYS_TIMER_0_ADDR) && defined(CONFIG_SYS_INTC_0_NUM) |
| 51 | preload = XILINX_CLOCK_FREQ / CONFIG_SYS_HZ; |
| 52 | irq = CONFIG_SYS_TIMER_0_IRQ; |
| 53 | tmr = (microblaze_timer_t *) (CONFIG_SYS_TIMER_0_ADDR); |
| 54 | #endif |
| 55 | |
| 56 | if (tmr && preload && irq >= 0) { |
| 57 | tmr->loadreg = preload; |
| 58 | tmr->control = TIMER_INTERRUPT | TIMER_RESET; |
| 59 | tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\ |
| 60 | TIMER_RELOAD | TIMER_DOWN_COUNT; |
| 61 | timestamp = 0; |
| 62 | ret = install_interrupt_handler (irq, timer_isr, (void *)tmr); |
| 63 | if (ret) |
| 64 | tmr = NULL; |
| 65 | } |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 66 | /* No problem if timer is not found/initialized */ |
Michal Simek | 5bbcb6c | 2010-04-16 11:37:41 +0200 | [diff] [blame] | 67 | return 0; |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 68 | } |
Michal Simek | 9d24274 | 2014-01-21 07:30:37 +0100 | [diff] [blame^] | 69 | #else |
| 70 | int timer_init(void) |
| 71 | { |
| 72 | return 0; |
| 73 | } |
| 74 | #endif |
Stephan Linz | b9f0b73 | 2012-02-22 22:39:57 +0100 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * This function is derived from PowerPC code (read timebase as long long). |
| 78 | * On Microblaze it just returns the timer value. |
| 79 | */ |
| 80 | unsigned long long get_ticks(void) |
| 81 | { |
| 82 | return get_timer(0); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * This function is derived from PowerPC code (timebase clock frequency). |
| 87 | * On Microblaze it returns the number of timer ticks per second. |
| 88 | */ |
| 89 | ulong get_tbclk(void) |
| 90 | { |
| 91 | return CONFIG_SYS_HZ; |
| 92 | } |