blob: 647bdcd5ba5295729a5a0d92aacdc13b11332f1d [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 Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070012#include <time.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Michal Simek952d5142007-03-11 13:42:58 +010014#include <asm/microblaze_timer.h>
Michal Simek77a1e242007-05-07 17:22:25 +020015#include <asm/microblaze_intc.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Michal Simek952d5142007-03-11 13:42:58 +010017
Michal Simekcf848852016-02-15 12:10:32 +010018DECLARE_GLOBAL_DATA_PTR;
19
Michal Simek952d5142007-03-11 13:42:58 +010020volatile int timestamp = 0;
Michal Simek06ac6512012-06-29 13:46:54 +020021microblaze_timer_t *tmr;
Michal Simek952d5142007-03-11 13:42:58 +010022
Michal Simek952d5142007-03-11 13:42:58 +010023ulong get_timer (ulong base)
24{
Michal Simek06ac6512012-06-29 13:46:54 +020025 if (tmr)
26 return timestamp - base;
27 return timestamp++ - base;
Michal Simek952d5142007-03-11 13:42:58 +010028}
29
Michal Simek64f96402012-06-29 13:42:28 +020030void __udelay(unsigned long usec)
31{
Michal Simek06ac6512012-06-29 13:46:54 +020032 u32 i;
Michal Simek64f96402012-06-29 13:42:28 +020033
Michal Simek06ac6512012-06-29 13:46:54 +020034 if (tmr) {
35 i = get_timer(0);
36 while ((get_timer(0) - i) < (usec / 1000))
37 ;
Michal Simek06ac6512012-06-29 13:46:54 +020038 }
Michal Simek64f96402012-06-29 13:42:28 +020039}
Michal Simek64f96402012-06-29 13:42:28 +020040
Michal Simek26acb3e2014-01-21 07:30:37 +010041#ifndef CONFIG_SPL_BUILD
Michal Simek06ac6512012-06-29 13:46:54 +020042static void timer_isr(void *arg)
Michal Simek952d5142007-03-11 13:42:58 +010043{
44 timestamp++;
45 tmr->control = tmr->control | TIMER_INTERRUPT;
46}
47
Michal Simekd1ff6c72010-04-16 11:37:41 +020048int timer_init (void)
Michal Simek952d5142007-03-11 13:42:58 +010049{
Michal Simek06ac6512012-06-29 13:46:54 +020050 int irq = -1;
51 u32 preload = 0;
52 u32 ret = 0;
Michal Simekcf848852016-02-15 12:10:32 +010053 const void *blob = gd->fdt_blob;
54 int node = 0;
55 u32 cell[2];
56
57 debug("TIMER: Initialization\n");
58
Michal Simekf54fbc82018-07-11 14:08:26 +020059 /* Do not init before relocation */
60 if (!(gd->flags & GD_FLG_RELOC))
61 return 0;
62
Michal Simekcf848852016-02-15 12:10:32 +010063 node = fdt_node_offset_by_compatible(blob, node,
64 "xlnx,xps-timer-1.00.a");
65 if (node != -1) {
66 fdt_addr_t base = fdtdec_get_addr(blob, node, "reg");
67 if (base == FDT_ADDR_T_NONE)
68 return -1;
69
70 debug("TIMER: Base addr %lx\n", base);
71 tmr = (microblaze_timer_t *)base;
72
73 ret = fdtdec_get_int_array(blob, node, "interrupts",
74 cell, ARRAY_SIZE(cell));
75 if (ret)
76 return ret;
77
78 irq = cell[0];
79 debug("TIMER: IRQ %x\n", irq);
80
81 preload = fdtdec_get_int(blob, node, "clock-frequency", 0);
82 preload /= CONFIG_SYS_HZ;
83 } else {
84 return node;
85 }
86
Michal Simek06ac6512012-06-29 13:46:54 +020087 if (tmr && preload && irq >= 0) {
88 tmr->loadreg = preload;
89 tmr->control = TIMER_INTERRUPT | TIMER_RESET;
90 tmr->control = TIMER_ENABLE | TIMER_ENABLE_INTR |\
91 TIMER_RELOAD | TIMER_DOWN_COUNT;
92 timestamp = 0;
93 ret = install_interrupt_handler (irq, timer_isr, (void *)tmr);
94 if (ret)
95 tmr = NULL;
96 }
Michal Simek06ac6512012-06-29 13:46:54 +020097 /* No problem if timer is not found/initialized */
Michal Simekd1ff6c72010-04-16 11:37:41 +020098 return 0;
Michal Simek952d5142007-03-11 13:42:58 +010099}
Michal Simek26acb3e2014-01-21 07:30:37 +0100100#else
101int timer_init(void)
102{
103 return 0;
104}
105#endif
Stephan Linzcba5bf02012-02-22 22:39:57 +0100106
107/*
108 * This function is derived from PowerPC code (read timebase as long long).
109 * On Microblaze it just returns the timer value.
110 */
111unsigned long long get_ticks(void)
112{
113 return get_timer(0);
114}
115
116/*
117 * This function is derived from PowerPC code (timebase clock frequency).
118 * On Microblaze it returns the number of timer ticks per second.
119 */
120ulong get_tbclk(void)
121{
122 return CONFIG_SYS_HZ;
123}