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