ARM: omap3_logic boards: Convert to DM_ETH
[oweals/u-boot.git] / arch / arm / mach-zynq / timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Weidmüller Interface GmbH & Co. KG
4  * Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
5  *
6  * Copyright (C) 2012 Michal Simek <monstr@monstr.eu>
7  * Copyright (C) 2011-2017 Xilinx, Inc. All rights reserved.
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>
28  */
29
30 #include <clk.h>
31 #include <common.h>
32 #include <div64.h>
33 #include <dm.h>
34 #include <time.h>
35 #include <malloc.h>
36 #include <asm/io.h>
37 #include <asm/arch/hardware.h>
38 #include <asm/arch/clk.h>
39
40 DECLARE_GLOBAL_DATA_PTR;
41
42 struct scu_timer {
43         u32 load; /* Timer Load Register */
44         u32 counter; /* Timer Counter Register */
45         u32 control; /* Timer Control Register */
46 };
47
48 static struct scu_timer *timer_base =
49                               (struct scu_timer *)ZYNQ_SCUTIMER_BASEADDR;
50
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
58
59 int 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
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
83         gd->arch.timer_rate_hz = (gd->cpu_clk / 2) / (TIMER_PRESCALE + 1);
84
85         /* Load the timer counter register */
86         writel(0xFFFFFFFF, &timer_base->load);
87
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 */
97         gd->arch.lastinc = readl(&timer_base->counter) /
98                                 (gd->arch.timer_rate_hz / CONFIG_SYS_HZ);
99         gd->arch.tbl = 0;
100
101         return 0;
102 }
103
104 /*
105  * This function is derived from PowerPC code (timebase clock frequency).
106  * On ARM it returns the number of timer ticks per second.
107  */
108 ulong get_tbclk(void)
109 {
110         return gd->arch.timer_rate_hz;
111 }