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> |
Michal Simek | 9aa65ca | 2016-02-15 12:10:32 +0100 | [diff] [blame^] | 10 | #include <fdtdec.h> |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 11 | #include <asm/microblaze_timer.h> |
Michal Simek | 42efed6 | 2007-05-07 17:22:25 +0200 | [diff] [blame] | 12 | #include <asm/microblaze_intc.h> |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 13 | |
Michal Simek | 9aa65ca | 2016-02-15 12:10:32 +0100 | [diff] [blame^] | 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 16 | volatile int timestamp = 0; |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 17 | microblaze_timer_t *tmr; |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 18 | |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 19 | ulong get_timer (ulong base) |
| 20 | { |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 21 | if (tmr) |
| 22 | return timestamp - base; |
| 23 | return timestamp++ - base; |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 24 | } |
| 25 | |
Michal Simek | 779bf42 | 2012-06-29 13:42:28 +0200 | [diff] [blame] | 26 | void __udelay(unsigned long usec) |
| 27 | { |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 28 | u32 i; |
Michal Simek | 779bf42 | 2012-06-29 13:42:28 +0200 | [diff] [blame] | 29 | |
Michal Simek | bcbb046 | 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 | ; |
| 34 | } else { |
Michal Simek | 9aa65ca | 2016-02-15 12:10:32 +0100 | [diff] [blame^] | 35 | #ifndef CONFIG_OF_CONTROL |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 36 | for (i = 0; i < (usec * XILINX_CLOCK_FREQ / 10000000); i++) |
| 37 | ; |
Michal Simek | 9aa65ca | 2016-02-15 12:10:32 +0100 | [diff] [blame^] | 38 | #endif |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 39 | } |
Michal Simek | 779bf42 | 2012-06-29 13:42:28 +0200 | [diff] [blame] | 40 | } |
Michal Simek | 779bf42 | 2012-06-29 13:42:28 +0200 | [diff] [blame] | 41 | |
Michal Simek | 9d24274 | 2014-01-21 07:30:37 +0100 | [diff] [blame] | 42 | #ifndef CONFIG_SPL_BUILD |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 43 | static void timer_isr(void *arg) |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 44 | { |
| 45 | timestamp++; |
| 46 | tmr->control = tmr->control | TIMER_INTERRUPT; |
| 47 | } |
| 48 | |
Michal Simek | 5bbcb6c | 2010-04-16 11:37:41 +0200 | [diff] [blame] | 49 | int timer_init (void) |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 50 | { |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 51 | int irq = -1; |
| 52 | u32 preload = 0; |
| 53 | u32 ret = 0; |
| 54 | |
Michal Simek | 9aa65ca | 2016-02-15 12:10:32 +0100 | [diff] [blame^] | 55 | #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 Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 87 | #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 Simek | 9aa65ca | 2016-02-15 12:10:32 +0100 | [diff] [blame^] | 92 | #endif |
Michal Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 93 | 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 Simek | bcbb046 | 2012-06-29 13:46:54 +0200 | [diff] [blame] | 103 | /* No problem if timer is not found/initialized */ |
Michal Simek | 5bbcb6c | 2010-04-16 11:37:41 +0200 | [diff] [blame] | 104 | return 0; |
Michal Simek | 76316a3 | 2007-03-11 13:42:58 +0100 | [diff] [blame] | 105 | } |
Michal Simek | 9d24274 | 2014-01-21 07:30:37 +0100 | [diff] [blame] | 106 | #else |
| 107 | int timer_init(void) |
| 108 | { |
| 109 | return 0; |
| 110 | } |
| 111 | #endif |
Stephan Linz | b9f0b73 | 2012-02-22 22:39:57 +0100 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * This function is derived from PowerPC code (read timebase as long long). |
| 115 | * On Microblaze it just returns the timer value. |
| 116 | */ |
| 117 | unsigned 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 | */ |
| 126 | ulong get_tbclk(void) |
| 127 | { |
| 128 | return CONFIG_SYS_HZ; |
| 129 | } |