blob: 2be253c2c3d5ec29d2e7a50da9542701728d0340 [file] [log] [blame]
Michal Simekdea68a72012-09-13 20:23:35 +00001/*
2 * Copyright (C) 2012 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2011-2012 Xilinx, Inc. All rights reserved.
4 *
5 * (C) Copyright 2008
6 * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
7 *
8 * (C) Copyright 2004
9 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
10 *
11 * (C) Copyright 2002-2004
12 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
13 *
14 * (C) Copyright 2003
15 * Texas Instruments <www.ti.com>
16 *
17 * (C) Copyright 2002
18 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
19 * Marius Groeger <mgroeger@sysgo.de>
20 *
21 * (C) Copyright 2002
22 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
23 * Alex Zuepke <azu@sysgo.de>
24 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +020025 * SPDX-License-Identifier: GPL-2.0+
Michal Simekdea68a72012-09-13 20:23:35 +000026 */
27
28#include <common.h>
29#include <div64.h>
30#include <asm/io.h>
Michal Simekad2e2b72013-04-12 16:21:26 +020031#include <asm/arch/hardware.h>
Michal Simekdea68a72012-09-13 20:23:35 +000032
33DECLARE_GLOBAL_DATA_PTR;
34
35struct scu_timer {
36 u32 load; /* Timer Load Register */
37 u32 counter; /* Timer Counter Register */
38 u32 control; /* Timer Control Register */
39};
40
41static struct scu_timer *timer_base =
Michal Simekad2e2b72013-04-12 16:21:26 +020042 (struct scu_timer *)ZYNQ_SCUTIMER_BASEADDR;
Michal Simekdea68a72012-09-13 20:23:35 +000043
44#define SCUTIMER_CONTROL_PRESCALER_MASK 0x0000FF00 /* Prescaler */
45#define SCUTIMER_CONTROL_PRESCALER_SHIFT 8
46#define SCUTIMER_CONTROL_AUTO_RELOAD_MASK 0x00000002 /* Auto-reload */
47#define SCUTIMER_CONTROL_ENABLE_MASK 0x00000001 /* Timer enable */
48
49#define TIMER_LOAD_VAL 0xFFFFFFFF
50#define TIMER_PRESCALE 255
51#define TIMER_TICK_HZ (CONFIG_CPU_FREQ_HZ / 2 / TIMER_PRESCALE)
52
53int timer_init(void)
54{
55 const u32 emask = SCUTIMER_CONTROL_AUTO_RELOAD_MASK |
56 (TIMER_PRESCALE << SCUTIMER_CONTROL_PRESCALER_SHIFT) |
57 SCUTIMER_CONTROL_ENABLE_MASK;
58
59 /* Load the timer counter register */
Michal Simek38003bc2013-08-28 07:36:31 +020060 writel(0xFFFFFFFF, &timer_base->load);
Michal Simekdea68a72012-09-13 20:23:35 +000061
62 /*
63 * Start the A9Timer device
64 * Enable Auto reload mode, Clear prescaler control bits
65 * Set prescaler value, Enable the decrementer
66 */
67 clrsetbits_le32(&timer_base->control, SCUTIMER_CONTROL_PRESCALER_MASK,
68 emask);
69
70 /* Reset time */
Simon Glassa848da52012-12-13 20:48:35 +000071 gd->arch.lastinc = readl(&timer_base->counter) /
Michal Simekdea68a72012-09-13 20:23:35 +000072 (TIMER_TICK_HZ / CONFIG_SYS_HZ);
Simon Glass2655ee12012-12-13 20:48:34 +000073 gd->arch.tbl = 0;
Michal Simekdea68a72012-09-13 20:23:35 +000074
75 return 0;
76}
77
78/*
79 * This function is derived from PowerPC code (read timebase as long long).
80 * On ARM it just returns the timer value.
81 */
82ulong get_timer_masked(void)
83{
84 ulong now;
85
86 now = readl(&timer_base->counter) / (TIMER_TICK_HZ / CONFIG_SYS_HZ);
87
Simon Glassa848da52012-12-13 20:48:35 +000088 if (gd->arch.lastinc >= now) {
Michal Simekdea68a72012-09-13 20:23:35 +000089 /* Normal mode */
Simon Glassa848da52012-12-13 20:48:35 +000090 gd->arch.tbl += gd->arch.lastinc - now;
Michal Simekdea68a72012-09-13 20:23:35 +000091 } else {
92 /* We have an overflow ... */
Simon Glassa848da52012-12-13 20:48:35 +000093 gd->arch.tbl += gd->arch.lastinc + TIMER_LOAD_VAL - now;
Michal Simekdea68a72012-09-13 20:23:35 +000094 }
Simon Glassa848da52012-12-13 20:48:35 +000095 gd->arch.lastinc = now;
Michal Simekdea68a72012-09-13 20:23:35 +000096
Simon Glass2655ee12012-12-13 20:48:34 +000097 return gd->arch.tbl;
Michal Simekdea68a72012-09-13 20:23:35 +000098}
99
100void __udelay(unsigned long usec)
101{
David Andrey8f1d0a52012-12-07 16:51:32 +0100102 u32 countticks;
103 u32 timeend;
104 u32 timediff;
105 u32 timenow;
Michal Simekdea68a72012-09-13 20:23:35 +0000106
David Andrey8f1d0a52012-12-07 16:51:32 +0100107 if (usec == 0)
108 return;
109
Tom Rini19251142013-12-05 14:48:36 -0500110 countticks = lldiv(TIMER_TICK_HZ * usec, 1000000);
David Andrey8f1d0a52012-12-07 16:51:32 +0100111
112 /* decrementing timer */
113 timeend = readl(&timer_base->counter) - countticks;
114
115#if TIMER_LOAD_VAL != 0xFFFFFFFF
116 /* do not manage multiple overflow */
117 if (countticks >= TIMER_LOAD_VAL)
118 countticks = TIMER_LOAD_VAL - 1;
119#endif
120
121 do {
122 timenow = readl(&timer_base->counter);
Michal Simekdea68a72012-09-13 20:23:35 +0000123
David Andrey8f1d0a52012-12-07 16:51:32 +0100124 if (timenow >= timeend) {
125 /* normal case */
126 timediff = timenow - timeend;
127 } else {
128 if ((TIMER_LOAD_VAL - timeend + timenow) <=
129 countticks) {
130 /* overflow */
131 timediff = TIMER_LOAD_VAL - timeend + timenow;
132 } else {
133 /* missed the exact match */
134 break;
135 }
136 }
137 } while (timediff > 0);
Michal Simekdea68a72012-09-13 20:23:35 +0000138}
139
140/* Timer without interrupts */
141ulong get_timer(ulong base)
142{
143 return get_timer_masked() - base;
144}
145
146/*
147 * This function is derived from PowerPC code (read timebase as long long).
148 * On ARM it just returns the timer value.
149 */
150unsigned long long get_ticks(void)
151{
152 return get_timer(0);
153}
154
155/*
156 * This function is derived from PowerPC code (timebase clock frequency).
157 * On ARM it returns the number of timer ticks per second.
158 */
159ulong get_tbclk(void)
160{
161 return CONFIG_SYS_HZ;
162}