blob: d822e20d2b230f2b2af2915d6dfc4fd2bbd6a954 [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 Glassa9dc0682019-12-28 10:44:59 -070034#include <time.h>
Simon Glass9bc15642020-02-03 07:36:16 -070035#include <malloc.h>
Michal Simekdea68a72012-09-13 20:23:35 +000036#include <asm/io.h>
Michal Simekad2e2b72013-04-12 16:21:26 +020037#include <asm/arch/hardware.h>
Soren Brinkmann15fff9b2013-11-21 13:38:57 -080038#include <asm/arch/clk.h>
Michal Simekdea68a72012-09-13 20:23:35 +000039
40DECLARE_GLOBAL_DATA_PTR;
41
42struct scu_timer {
43 u32 load; /* Timer Load Register */
44 u32 counter; /* Timer Counter Register */
45 u32 control; /* Timer Control Register */
46};
47
48static struct scu_timer *timer_base =
Michal Simekad2e2b72013-04-12 16:21:26 +020049 (struct scu_timer *)ZYNQ_SCUTIMER_BASEADDR;
Michal Simekdea68a72012-09-13 20:23:35 +000050
51#define SCUTIMER_CONTROL_PRESCALER_MASK 0x0000FF00 /* Prescaler */
52#define SCUTIMER_CONTROL_PRESCALER_SHIFT 8
53#define SCUTIMER_CONTROL_AUTO_RELOAD_MASK 0x00000002 /* Auto-reload */
54#define SCUTIMER_CONTROL_ENABLE_MASK 0x00000001 /* Timer enable */
55
56#define TIMER_LOAD_VAL 0xFFFFFFFF
57#define TIMER_PRESCALE 255
Michal Simekdea68a72012-09-13 20:23:35 +000058
59int timer_init(void)
60{
61 const u32 emask = SCUTIMER_CONTROL_AUTO_RELOAD_MASK |
62 (TIMER_PRESCALE << SCUTIMER_CONTROL_PRESCALER_SHIFT) |
63 SCUTIMER_CONTROL_ENABLE_MASK;
64
Stefan Herbrechtsmeier35763802017-01-17 16:27:26 +010065 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);
Stefan Herbrechtsmeier35763802017-01-17 16:27:26 +010082
Michal Simek39240122013-11-22 15:29:38 +010083 gd->arch.timer_rate_hz = (gd->cpu_clk / 2) / (TIMER_PRESCALE + 1);
Soren Brinkmann15fff9b2013-11-21 13:38:57 -080084
Michal Simekdea68a72012-09-13 20:23:35 +000085 /* Load the timer counter register */
Michal Simek38003bc2013-08-28 07:36:31 +020086 writel(0xFFFFFFFF, &timer_base->load);
Michal Simekdea68a72012-09-13 20:23:35 +000087
88 /*
89 * Start the A9Timer device
90 * Enable Auto reload mode, Clear prescaler control bits
91 * Set prescaler value, Enable the decrementer
92 */
93 clrsetbits_le32(&timer_base->control, SCUTIMER_CONTROL_PRESCALER_MASK,
94 emask);
95
96 /* Reset time */
Simon Glassa848da52012-12-13 20:48:35 +000097 gd->arch.lastinc = readl(&timer_base->counter) /
Soren Brinkmann15fff9b2013-11-21 13:38:57 -080098 (gd->arch.timer_rate_hz / CONFIG_SYS_HZ);
Simon Glass2655ee12012-12-13 20:48:34 +000099 gd->arch.tbl = 0;
Michal Simekdea68a72012-09-13 20:23:35 +0000100
101 return 0;
102}
103
104/*
Michal Simekdea68a72012-09-13 20:23:35 +0000105 * This function is derived from PowerPC code (timebase clock frequency).
106 * On ARM it returns the number of timer ticks per second.
107 */
108ulong get_tbclk(void)
109{
Michal Simek40bcb862015-04-20 12:56:24 +0200110 return gd->arch.timer_rate_hz;
Michal Simekdea68a72012-09-13 20:23:35 +0000111}