blob: 0335cbec67c3124d290866dfbf03e3ff9cfdac45 [file] [log] [blame]
Michal Simekdea68a72012-09-13 20:23:35 +00001/*
Stefan Herbrechtsmeier35763802017-01-17 16:27:26 +01002 * Copyright (C) 2017 Weidmüller Interface GmbH & Co. KG
3 * Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
4 *
Michal Simekdea68a72012-09-13 20:23:35 +00005 * Copyright (C) 2012 Michal Simek <monstr@monstr.eu>
6 * Copyright (C) 2011-2012 Xilinx, Inc. All rights reserved.
7 *
8 * (C) Copyright 2008
9 * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
10 *
11 * (C) Copyright 2004
12 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
13 *
14 * (C) Copyright 2002-2004
15 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
16 *
17 * (C) Copyright 2003
18 * Texas Instruments <www.ti.com>
19 *
20 * (C) Copyright 2002
21 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
22 * Marius Groeger <mgroeger@sysgo.de>
23 *
24 * (C) Copyright 2002
25 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
26 * Alex Zuepke <azu@sysgo.de>
27 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +020028 * SPDX-License-Identifier: GPL-2.0+
Michal Simekdea68a72012-09-13 20:23:35 +000029 */
30
Stefan Herbrechtsmeier35763802017-01-17 16:27:26 +010031#include <clk.h>
Michal Simekdea68a72012-09-13 20:23:35 +000032#include <common.h>
33#include <div64.h>
Stefan Herbrechtsmeier35763802017-01-17 16:27:26 +010034#include <dm.h>
Michal Simekdea68a72012-09-13 20:23:35 +000035#include <asm/io.h>
Michal Simekad2e2b72013-04-12 16:21:26 +020036#include <asm/arch/hardware.h>
Soren Brinkmann15fff9b2013-11-21 13:38:57 -080037#include <asm/arch/clk.h>
Michal Simekdea68a72012-09-13 20:23:35 +000038
39DECLARE_GLOBAL_DATA_PTR;
40
41struct scu_timer {
42 u32 load; /* Timer Load Register */
43 u32 counter; /* Timer Counter Register */
44 u32 control; /* Timer Control Register */
45};
46
47static struct scu_timer *timer_base =
Michal Simekad2e2b72013-04-12 16:21:26 +020048 (struct scu_timer *)ZYNQ_SCUTIMER_BASEADDR;
Michal Simekdea68a72012-09-13 20:23:35 +000049
50#define SCUTIMER_CONTROL_PRESCALER_MASK 0x0000FF00 /* Prescaler */
51#define SCUTIMER_CONTROL_PRESCALER_SHIFT 8
52#define SCUTIMER_CONTROL_AUTO_RELOAD_MASK 0x00000002 /* Auto-reload */
53#define SCUTIMER_CONTROL_ENABLE_MASK 0x00000001 /* Timer enable */
54
55#define TIMER_LOAD_VAL 0xFFFFFFFF
56#define TIMER_PRESCALE 255
Michal Simekdea68a72012-09-13 20:23:35 +000057
58int timer_init(void)
59{
60 const u32 emask = SCUTIMER_CONTROL_AUTO_RELOAD_MASK |
61 (TIMER_PRESCALE << SCUTIMER_CONTROL_PRESCALER_SHIFT) |
62 SCUTIMER_CONTROL_ENABLE_MASK;
63
Stefan Herbrechtsmeier35763802017-01-17 16:27:26 +010064#if defined(CONFIG_CLK) || defined(CONFIG_SPL_CLK)
65 struct udevice *dev;
66 struct clk clk;
67 int ret;
68
69 ret = uclass_get_device_by_driver(UCLASS_CLK,
70 DM_GET_DRIVER(zynq_clk), &dev);
71 if (ret)
72 return ret;
73
74 clk.id = cpu_6or4x_clk;
75 ret = clk_request(dev, &clk);
76 if (ret < 0)
77 return ret;
78
79 gd->cpu_clk = clk_get_rate(&clk);
80
81 clk_free(&clk);
82#endif
83
Michal Simek39240122013-11-22 15:29:38 +010084 gd->arch.timer_rate_hz = (gd->cpu_clk / 2) / (TIMER_PRESCALE + 1);
Soren Brinkmann15fff9b2013-11-21 13:38:57 -080085
Michal Simekdea68a72012-09-13 20:23:35 +000086 /* Load the timer counter register */
Michal Simek38003bc2013-08-28 07:36:31 +020087 writel(0xFFFFFFFF, &timer_base->load);
Michal Simekdea68a72012-09-13 20:23:35 +000088
89 /*
90 * Start the A9Timer device
91 * Enable Auto reload mode, Clear prescaler control bits
92 * Set prescaler value, Enable the decrementer
93 */
94 clrsetbits_le32(&timer_base->control, SCUTIMER_CONTROL_PRESCALER_MASK,
95 emask);
96
97 /* Reset time */
Simon Glassa848da52012-12-13 20:48:35 +000098 gd->arch.lastinc = readl(&timer_base->counter) /
Soren Brinkmann15fff9b2013-11-21 13:38:57 -080099 (gd->arch.timer_rate_hz / CONFIG_SYS_HZ);
Simon Glass2655ee12012-12-13 20:48:34 +0000100 gd->arch.tbl = 0;
Michal Simekdea68a72012-09-13 20:23:35 +0000101
102 return 0;
103}
104
105/*
Michal Simekdea68a72012-09-13 20:23:35 +0000106 * This function is derived from PowerPC code (timebase clock frequency).
107 * On ARM it returns the number of timer ticks per second.
108 */
109ulong get_tbclk(void)
110{
Michal Simek40bcb862015-04-20 12:56:24 +0200111 return gd->arch.timer_rate_hz;
Michal Simekdea68a72012-09-13 20:23:35 +0000112}