Michal Simek | 952d514 | 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 | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | 952d514 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Michal Simek | cf84885 | 2016-02-15 12:10:32 +0100 | [diff] [blame] | 10 | #include <fdtdec.h> |
Michal Simek | 952d514 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 11 | #include <asm/microblaze_timer.h> |
Michal Simek | 77a1e24 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 12 | #include <asm/microblaze_intc.h> |
Michal Simek | 952d514 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 13 | |
Michal Simek | cf84885 | 2016-02-15 12:10:32 +0100 | [diff] [blame] | 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Michal Simek | 952d514 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 16 | volatile int timestamp = 0; |
Michal Simek | 06ac651 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 17 | microblaze_timer_t *tmr; |
Michal Simek | 952d514 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 18 | |
Michal Simek | 952d514 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 19 | ulong get_timer (ulong base) |
| 20 | { |
Michal Simek | 06ac651 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 21 | if (tmr) |
| 22 | return timestamp - base; |
| 23 | return timestamp++ - base; |
Michal Simek | 952d514 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 24 | } |
| 25 | |
Michal Simek | 64f9640 | 2012-06-29 13:42:28 +0200 | [diff] [blame] | 26 | void __udelay(unsigned long usec) |
| 27 | { |
Michal Simek | 06ac651 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 28 | u32 i; |
Michal Simek | 64f9640 | 2012-06-29 13:42:28 +0200 | [diff] [blame] | 29 | |
Michal Simek | 06ac651 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 30 | if (tmr) { |
| 31 | i = get_timer(0); |
| 32 | while ((get_timer(0) - i) < (usec / 1000)) |
| 33 | ; |
Michal Simek | 06ac651 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 34 | } |
Michal Simek | 64f9640 | 2012-06-29 13:42:28 +0200 | [diff] [blame] | 35 | } |
Michal Simek | 64f9640 | 2012-06-29 13:42:28 +0200 | [diff] [blame] | 36 | |
Michal Simek | 26acb3e | 2014-01-21 07:30:37 +0100 | [diff] [blame] | 37 | #ifndef CONFIG_SPL_BUILD |
Michal Simek | 06ac651 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 38 | static void timer_isr(void *arg) |
Michal Simek | 952d514 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 39 | { |
| 40 | timestamp++; |
| 41 | tmr->control = tmr->control | TIMER_INTERRUPT; |
| 42 | } |
| 43 | |
Michal Simek | d1ff6c7 | 2010-04-16 11:37:41 +0200 | [diff] [blame] | 44 | int timer_init (void) |
Michal Simek | 952d514 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 45 | { |
Michal Simek | 06ac651 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 46 | int irq = -1; |
| 47 | u32 preload = 0; |
| 48 | u32 ret = 0; |
Michal Simek | cf84885 | 2016-02-15 12:10:32 +0100 | [diff] [blame] | 49 | const void *blob = gd->fdt_blob; |
| 50 | int node = 0; |
| 51 | u32 cell[2]; |
| 52 | |
| 53 | debug("TIMER: Initialization\n"); |
| 54 | |
| 55 | node = fdt_node_offset_by_compatible(blob, node, |
| 56 | "xlnx,xps-timer-1.00.a"); |
| 57 | if (node != -1) { |
| 58 | fdt_addr_t base = fdtdec_get_addr(blob, node, "reg"); |
| 59 | if (base == FDT_ADDR_T_NONE) |
| 60 | return -1; |
| 61 | |
| 62 | debug("TIMER: Base addr %lx\n", base); |
| 63 | tmr = (microblaze_timer_t *)base; |
| 64 | |
| 65 | ret = fdtdec_get_int_array(blob, node, "interrupts", |
| 66 | cell, ARRAY_SIZE(cell)); |
| 67 | if (ret) |
| 68 | return ret; |
| 69 | |
| 70 | irq = cell[0]; |
| 71 | debug("TIMER: IRQ %x\n", irq); |
| 72 | |
| 73 | preload = fdtdec_get_int(blob, node, "clock-frequency", 0); |
| 74 | preload /= CONFIG_SYS_HZ; |
| 75 | } else { |
| 76 | return node; |
| 77 | } |
| 78 | |
Michal Simek | 06ac651 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 79 | if (tmr && preload && irq >= 0) { |
| 80 | tmr->loadreg = preload; |
| 81 | tmr->control = TIMER_INTERRUPT | TIMER_RESET; |
| 82 | tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\ |
| 83 | TIMER_RELOAD | TIMER_DOWN_COUNT; |
| 84 | timestamp = 0; |
| 85 | ret = install_interrupt_handler (irq, timer_isr, (void *)tmr); |
| 86 | if (ret) |
| 87 | tmr = NULL; |
| 88 | } |
Michal Simek | 06ac651 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 89 | /* No problem if timer is not found/initialized */ |
Michal Simek | d1ff6c7 | 2010-04-16 11:37:41 +0200 | [diff] [blame] | 90 | return 0; |
Michal Simek | 952d514 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 91 | } |
Michal Simek | 26acb3e | 2014-01-21 07:30:37 +0100 | [diff] [blame] | 92 | #else |
| 93 | int timer_init(void) |
| 94 | { |
| 95 | return 0; |
| 96 | } |
| 97 | #endif |
Stephan Linz | cba5bf0 | 2012-02-22 22:39:57 +0100 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * This function is derived from PowerPC code (read timebase as long long). |
| 101 | * On Microblaze it just returns the timer value. |
| 102 | */ |
| 103 | unsigned long long get_ticks(void) |
| 104 | { |
| 105 | return get_timer(0); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * This function is derived from PowerPC code (timebase clock frequency). |
| 110 | * On Microblaze it returns the number of timer ticks per second. |
| 111 | */ |
| 112 | ulong get_tbclk(void) |
| 113 | { |
| 114 | return CONFIG_SYS_HZ; |
| 115 | } |