blob: cc0e24cbfede723373718ebe6177db15374cffe8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simekdea68a72012-09-13 20:23:35 +00002/*
Stefan Herbrechtsmeier35763802017-01-17 16:27:26 +01003 * Copyright (C) 2017 Weidmüller Interface GmbH & Co. KG
4 * Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
5 *
Michal Simekdea68a72012-09-13 20:23:35 +00006 * Copyright (C) 2012 Michal Simek <monstr@monstr.eu>
Michal Simek98d0f1f2018-01-17 07:37:47 +01007 * Copyright (C) 2011-2017 Xilinx, Inc. All rights reserved.
Michal Simekdea68a72012-09-13 20:23:35 +00008 *
9 * (C) Copyright 2008
10 * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
11 *
12 * (C) Copyright 2004
13 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
14 *
15 * (C) Copyright 2002-2004
16 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
17 *
18 * (C) Copyright 2003
19 * Texas Instruments <www.ti.com>
20 *
21 * (C) Copyright 2002
22 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
23 * Marius Groeger <mgroeger@sysgo.de>
24 *
25 * (C) Copyright 2002
26 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
27 * Alex Zuepke <azu@sysgo.de>
Michal Simekdea68a72012-09-13 20:23:35 +000028 */
29
Stefan Herbrechtsmeier35763802017-01-17 16:27:26 +010030#include <clk.h>
Michal Simekdea68a72012-09-13 20:23:35 +000031#include <common.h>
32#include <div64.h>
Stefan Herbrechtsmeier35763802017-01-17 16:27:26 +010033#include <dm.h>
Simon Glass97589732020-05-10 11:40:02 -060034#include <init.h>
Simon Glassa9dc0682019-12-28 10:44:59 -070035#include <time.h>
Simon Glass9bc15642020-02-03 07:36:16 -070036#include <malloc.h>
Michal Simekdea68a72012-09-13 20:23:35 +000037#include <asm/io.h>
Michal Simekad2e2b72013-04-12 16:21:26 +020038#include <asm/arch/hardware.h>
Soren Brinkmann15fff9b2013-11-21 13:38:57 -080039#include <asm/arch/clk.h>
Michal Simekdea68a72012-09-13 20:23:35 +000040
41DECLARE_GLOBAL_DATA_PTR;
42
43struct scu_timer {
44 u32 load; /* Timer Load Register */
45 u32 counter; /* Timer Counter Register */
46 u32 control; /* Timer Control Register */
47};
48
49static struct scu_timer *timer_base =
Michal Simekad2e2b72013-04-12 16:21:26 +020050 (struct scu_timer *)ZYNQ_SCUTIMER_BASEADDR;
Michal Simekdea68a72012-09-13 20:23:35 +000051
52#define SCUTIMER_CONTROL_PRESCALER_MASK 0x0000FF00 /* Prescaler */
53#define SCUTIMER_CONTROL_PRESCALER_SHIFT 8
54#define SCUTIMER_CONTROL_AUTO_RELOAD_MASK 0x00000002 /* Auto-reload */
55#define SCUTIMER_CONTROL_ENABLE_MASK 0x00000001 /* Timer enable */
56
57#define TIMER_LOAD_VAL 0xFFFFFFFF
58#define TIMER_PRESCALE 255
Michal Simekdea68a72012-09-13 20:23:35 +000059
60int timer_init(void)
61{
62 const u32 emask = SCUTIMER_CONTROL_AUTO_RELOAD_MASK |
63 (TIMER_PRESCALE << SCUTIMER_CONTROL_PRESCALER_SHIFT) |
64 SCUTIMER_CONTROL_ENABLE_MASK;
65
Stefan Herbrechtsmeier35763802017-01-17 16:27:26 +010066 struct udevice *dev;
67 struct clk clk;
68 int ret;
69
70 ret = uclass_get_device_by_driver(UCLASS_CLK,
71 DM_GET_DRIVER(zynq_clk), &dev);
72 if (ret)
73 return ret;
74
75 clk.id = cpu_6or4x_clk;
76 ret = clk_request(dev, &clk);
77 if (ret < 0)
78 return ret;
79
80 gd->cpu_clk = clk_get_rate(&clk);
81
82 clk_free(&clk);
Stefan Herbrechtsmeier35763802017-01-17 16:27:26 +010083
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}