Merge branch '2022-06-28-Kconfig-migrations' into next
[J-u-boot.git] / arch / arm / mach-zynq / timer.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
38b343dd 2/*
c7abb824
SH
3 * Copyright (C) 2017 Weidmüller Interface GmbH & Co. KG
4 * Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
5 *
38b343dd 6 * Copyright (C) 2012 Michal Simek <monstr@monstr.eu>
3e1b61de 7 * Copyright (C) 2011-2017 Xilinx, Inc. All rights reserved.
38b343dd
MS
8 *
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>
38b343dd
MS
28 */
29
c7abb824 30#include <clk.h>
38b343dd
MS
31#include <common.h>
32#include <div64.h>
c7abb824 33#include <dm.h>
691d719d 34#include <init.h>
049f8d6f 35#include <time.h>
336d4615 36#include <malloc.h>
401d1c4f 37#include <asm/global_data.h>
38b343dd 38#include <asm/io.h>
4b21284b 39#include <asm/arch/hardware.h>
614c2725 40#include <asm/arch/clk.h>
38b343dd
MS
41
42DECLARE_GLOBAL_DATA_PTR;
43
44struct scu_timer {
45 u32 load; /* Timer Load Register */
46 u32 counter; /* Timer Counter Register */
47 u32 control; /* Timer Control Register */
48};
49
50static struct scu_timer *timer_base =
4b21284b 51 (struct scu_timer *)ZYNQ_SCUTIMER_BASEADDR;
38b343dd
MS
52
53#define SCUTIMER_CONTROL_PRESCALER_MASK 0x0000FF00 /* Prescaler */
54#define SCUTIMER_CONTROL_PRESCALER_SHIFT 8
55#define SCUTIMER_CONTROL_AUTO_RELOAD_MASK 0x00000002 /* Auto-reload */
56#define SCUTIMER_CONTROL_ENABLE_MASK 0x00000001 /* Timer enable */
57
58#define TIMER_LOAD_VAL 0xFFFFFFFF
59#define TIMER_PRESCALE 255
38b343dd
MS
60
61int timer_init(void)
62{
63 const u32 emask = SCUTIMER_CONTROL_AUTO_RELOAD_MASK |
64 (TIMER_PRESCALE << SCUTIMER_CONTROL_PRESCALER_SHIFT) |
65 SCUTIMER_CONTROL_ENABLE_MASK;
66
c7abb824
SH
67 struct udevice *dev;
68 struct clk clk;
69 int ret;
70
71 ret = uclass_get_device_by_driver(UCLASS_CLK,
65e25bea 72 DM_DRIVER_GET(zynq_clk), &dev);
c7abb824
SH
73 if (ret)
74 return ret;
75
76 clk.id = cpu_6or4x_clk;
77 ret = clk_request(dev, &clk);
78 if (ret < 0)
79 return ret;
80
81 gd->cpu_clk = clk_get_rate(&clk);
82
83 clk_free(&clk);
c7abb824 84
2826fd32 85 gd->arch.timer_rate_hz = (gd->cpu_clk / 2) / (TIMER_PRESCALE + 1);
614c2725 86
38b343dd 87 /* Load the timer counter register */
7ba69b7d 88 writel(0xFFFFFFFF, &timer_base->load);
38b343dd
MS
89
90 /*
91 * Start the A9Timer device
92 * Enable Auto reload mode, Clear prescaler control bits
93 * Set prescaler value, Enable the decrementer
94 */
95 clrsetbits_le32(&timer_base->control, SCUTIMER_CONTROL_PRESCALER_MASK,
96 emask);
97
98 /* Reset time */
582601da 99 gd->arch.lastinc = readl(&timer_base->counter) /
614c2725 100 (gd->arch.timer_rate_hz / CONFIG_SYS_HZ);
66ee6923 101 gd->arch.tbl = 0;
38b343dd
MS
102
103 return 0;
104}
105
38b343dd
MS
106/*
107 * This function is derived from PowerPC code (timebase clock frequency).
108 * On ARM it returns the number of timer ticks per second.
109 */
110ulong get_tbclk(void)
111{
a2ec7fb9 112 return gd->arch.timer_rate_hz;
38b343dd 113}
This page took 0.43056 seconds and 4 git commands to generate.