blob: e07fc628d164f4c46950ae5ae1aa57ef529539fc [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 Glass97589732020-05-10 11:40:02 -060010#include <init.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070011#include <time.h>
Michal Simek952d5142007-03-11 13:42:58 +010012#include <asm/microblaze_timer.h>
Michal Simek77a1e242007-05-07 17:22:25 +020013#include <asm/microblaze_intc.h>
Michal Simek952d5142007-03-11 13:42:58 +010014
Michal Simekcf848852016-02-15 12:10:32 +010015DECLARE_GLOBAL_DATA_PTR;
16
Michal Simek952d5142007-03-11 13:42:58 +010017volatile int timestamp = 0;
Michal Simek06ac6512012-06-29 13:46:54 +020018microblaze_timer_t *tmr;
Michal Simek952d5142007-03-11 13:42:58 +010019
Michal Simek952d5142007-03-11 13:42:58 +010020ulong get_timer (ulong base)
21{
Michal Simek06ac6512012-06-29 13:46:54 +020022 if (tmr)
23 return timestamp - base;
24 return timestamp++ - base;
Michal Simek952d5142007-03-11 13:42:58 +010025}
26
Michal Simek64f96402012-06-29 13:42:28 +020027void __udelay(unsigned long usec)
28{
Michal Simek06ac6512012-06-29 13:46:54 +020029 u32 i;
Michal Simek64f96402012-06-29 13:42:28 +020030
Michal Simek06ac6512012-06-29 13:46:54 +020031 if (tmr) {
32 i = get_timer(0);
33 while ((get_timer(0) - i) < (usec / 1000))
34 ;
Michal Simek06ac6512012-06-29 13:46:54 +020035 }
Michal Simek64f96402012-06-29 13:42:28 +020036}
Michal Simek64f96402012-06-29 13:42:28 +020037
Michal Simek26acb3e2014-01-21 07:30:37 +010038#ifndef CONFIG_SPL_BUILD
Michal Simek06ac6512012-06-29 13:46:54 +020039static void timer_isr(void *arg)
Michal Simek952d5142007-03-11 13:42:58 +010040{
41 timestamp++;
42 tmr->control = tmr->control | TIMER_INTERRUPT;
43}
44
Michal Simekd1ff6c72010-04-16 11:37:41 +020045int timer_init (void)
Michal Simek952d5142007-03-11 13:42:58 +010046{
Michal Simek06ac6512012-06-29 13:46:54 +020047 int irq = -1;
48 u32 preload = 0;
49 u32 ret = 0;
Michal Simekcf848852016-02-15 12:10:32 +010050 const void *blob = gd->fdt_blob;
51 int node = 0;
52 u32 cell[2];
53
54 debug("TIMER: Initialization\n");
55
Michal Simekf54fbc82018-07-11 14:08:26 +020056 /* Do not init before relocation */
57 if (!(gd->flags & GD_FLG_RELOC))
58 return 0;
59
Michal Simekcf848852016-02-15 12:10:32 +010060 node = fdt_node_offset_by_compatible(blob, node,
61 "xlnx,xps-timer-1.00.a");
62 if (node != -1) {
63 fdt_addr_t base = fdtdec_get_addr(blob, node, "reg");
64 if (base == FDT_ADDR_T_NONE)
65 return -1;
66
67 debug("TIMER: Base addr %lx\n", base);
68 tmr = (microblaze_timer_t *)base;
69
70 ret = fdtdec_get_int_array(blob, node, "interrupts",
71 cell, ARRAY_SIZE(cell));
72 if (ret)
73 return ret;
74
75 irq = cell[0];
76 debug("TIMER: IRQ %x\n", irq);
77
78 preload = fdtdec_get_int(blob, node, "clock-frequency", 0);
79 preload /= CONFIG_SYS_HZ;
80 } else {
81 return node;
82 }
83
Michal Simek06ac6512012-06-29 13:46:54 +020084 if (tmr && preload && irq >= 0) {
85 tmr->loadreg = preload;
86 tmr->control = TIMER_INTERRUPT | TIMER_RESET;
87 tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\
88 TIMER_RELOAD | TIMER_DOWN_COUNT;
89 timestamp = 0;
90 ret = install_interrupt_handler (irq, timer_isr, (void *)tmr);
91 if (ret)
92 tmr = NULL;
93 }
Michal Simek06ac6512012-06-29 13:46:54 +020094 /* No problem if timer is not found/initialized */
Michal Simekd1ff6c72010-04-16 11:37:41 +020095 return 0;
Michal Simek952d5142007-03-11 13:42:58 +010096}
Michal Simek26acb3e2014-01-21 07:30:37 +010097#else
98int timer_init(void)
99{
100 return 0;
101}
102#endif
Stephan Linzcba5bf02012-02-22 22:39:57 +0100103
104/*
105 * This function is derived from PowerPC code (read timebase as long long).
106 * On Microblaze it just returns the timer value.
107 */
108unsigned long long get_ticks(void)
109{
110 return get_timer(0);
111}
112
113/*
114 * This function is derived from PowerPC code (timebase clock frequency).
115 * On Microblaze it returns the number of timer ticks per second.
116 */
117ulong get_tbclk(void)
118{
119 return CONFIG_SYS_HZ;
120}