blob: 805eac7fa3f233f49b33f16925395786eaf2e279 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek952d5142007-03-11 13:42:58 +01002/*
3 * (C) Copyright 2007 Michal Simek
4 *
5 * Michal SIMEK <monstr@monstr.eu>
Michal Simek952d5142007-03-11 13:42:58 +01006 */
7
8#include <common.h>
Michal Simekcf848852016-02-15 12:10:32 +01009#include <fdtdec.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070010#include <time.h>
Michal Simek952d5142007-03-11 13:42:58 +010011#include <asm/microblaze_timer.h>
Michal Simek77a1e242007-05-07 17:22:25 +020012#include <asm/microblaze_intc.h>
Michal Simek952d5142007-03-11 13:42:58 +010013
Michal Simekcf848852016-02-15 12:10:32 +010014DECLARE_GLOBAL_DATA_PTR;
15
Michal Simek952d5142007-03-11 13:42:58 +010016volatile int timestamp = 0;
Michal Simek06ac6512012-06-29 13:46:54 +020017microblaze_timer_t *tmr;
Michal Simek952d5142007-03-11 13:42:58 +010018
Michal Simek952d5142007-03-11 13:42:58 +010019ulong get_timer (ulong base)
20{
Michal Simek06ac6512012-06-29 13:46:54 +020021 if (tmr)
22 return timestamp - base;
23 return timestamp++ - base;
Michal Simek952d5142007-03-11 13:42:58 +010024}
25
Michal Simek64f96402012-06-29 13:42:28 +020026void __udelay(unsigned long usec)
27{
Michal Simek06ac6512012-06-29 13:46:54 +020028 u32 i;
Michal Simek64f96402012-06-29 13:42:28 +020029
Michal Simek06ac6512012-06-29 13:46:54 +020030 if (tmr) {
31 i = get_timer(0);
32 while ((get_timer(0) - i) < (usec / 1000))
33 ;
Michal Simek06ac6512012-06-29 13:46:54 +020034 }
Michal Simek64f96402012-06-29 13:42:28 +020035}
Michal Simek64f96402012-06-29 13:42:28 +020036
Michal Simek26acb3e2014-01-21 07:30:37 +010037#ifndef CONFIG_SPL_BUILD
Michal Simek06ac6512012-06-29 13:46:54 +020038static void timer_isr(void *arg)
Michal Simek952d5142007-03-11 13:42:58 +010039{
40 timestamp++;
41 tmr->control = tmr->control | TIMER_INTERRUPT;
42}
43
Michal Simekd1ff6c72010-04-16 11:37:41 +020044int timer_init (void)
Michal Simek952d5142007-03-11 13:42:58 +010045{
Michal Simek06ac6512012-06-29 13:46:54 +020046 int irq = -1;
47 u32 preload = 0;
48 u32 ret = 0;
Michal Simekcf848852016-02-15 12:10:32 +010049 const void *blob = gd->fdt_blob;
50 int node = 0;
51 u32 cell[2];
52
53 debug("TIMER: Initialization\n");
54
Michal Simekf54fbc82018-07-11 14:08:26 +020055 /* Do not init before relocation */
56 if (!(gd->flags & GD_FLG_RELOC))
57 return 0;
58
Michal Simekcf848852016-02-15 12:10:32 +010059 node = fdt_node_offset_by_compatible(blob, node,
60 "xlnx,xps-timer-1.00.a");
61 if (node != -1) {
62 fdt_addr_t base = fdtdec_get_addr(blob, node, "reg");
63 if (base == FDT_ADDR_T_NONE)
64 return -1;
65
66 debug("TIMER: Base addr %lx\n", base);
67 tmr = (microblaze_timer_t *)base;
68
69 ret = fdtdec_get_int_array(blob, node, "interrupts",
70 cell, ARRAY_SIZE(cell));
71 if (ret)
72 return ret;
73
74 irq = cell[0];
75 debug("TIMER: IRQ %x\n", irq);
76
77 preload = fdtdec_get_int(blob, node, "clock-frequency", 0);
78 preload /= CONFIG_SYS_HZ;
79 } else {
80 return node;
81 }
82
Michal Simek06ac6512012-06-29 13:46:54 +020083 if (tmr && preload && irq >= 0) {
84 tmr->loadreg = preload;
85 tmr->control = TIMER_INTERRUPT | TIMER_RESET;
86 tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\
87 TIMER_RELOAD | TIMER_DOWN_COUNT;
88 timestamp = 0;
89 ret = install_interrupt_handler (irq, timer_isr, (void *)tmr);
90 if (ret)
91 tmr = NULL;
92 }
Michal Simek06ac6512012-06-29 13:46:54 +020093 /* No problem if timer is not found/initialized */
Michal Simekd1ff6c72010-04-16 11:37:41 +020094 return 0;
Michal Simek952d5142007-03-11 13:42:58 +010095}
Michal Simek26acb3e2014-01-21 07:30:37 +010096#else
97int timer_init(void)
98{
99 return 0;
100}
101#endif
Stephan Linzcba5bf02012-02-22 22:39:57 +0100102
103/*
104 * This function is derived from PowerPC code (read timebase as long long).
105 * On Microblaze it just returns the timer value.
106 */
107unsigned long long get_ticks(void)
108{
109 return get_timer(0);
110}
111
112/*
113 * This function is derived from PowerPC code (timebase clock frequency).
114 * On Microblaze it returns the number of timer ticks per second.
115 */
116ulong get_tbclk(void)
117{
118 return CONFIG_SYS_HZ;
119}