blob: 065f10bb742b91cbb251687fdae7671df51e8bbd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Patrice Chotard200a7992017-02-21 13:37:05 +01002/*
Patrice Chotard9e216242017-10-23 09:53:57 +02003 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
Patrice Chotard5d9950d2020-12-02 18:47:30 +01004 * Author(s): Patrice Chotard, <patrice.chotard@foss.st.com> for STMicroelectronics.
William Zhang38c26de2022-08-23 21:44:32 -07005 *
6 * ARM Cortext A9 global timer driver
Patrice Chotard200a7992017-02-21 13:37:05 +01007 */
8
9#include <common.h>
10#include <dm.h>
Nicolas Heemeryck44704cb2020-03-13 23:42:44 +010011#include <clk.h>
Patrice Chotard200a7992017-02-21 13:37:05 +010012#include <timer.h>
Nicolas Heemeryck44704cb2020-03-13 23:42:44 +010013#include <linux/err.h>
Patrice Chotard200a7992017-02-21 13:37:05 +010014
15#include <asm/io.h>
16#include <asm/arch-armv7/globaltimer.h>
17
William Zhang38c26de2022-08-23 21:44:32 -070018struct arm_global_timer_priv {
Patrice Chotard200a7992017-02-21 13:37:05 +010019 struct globaltimer *global_timer;
20};
21
William Zhang38c26de2022-08-23 21:44:32 -070022static u64 arm_global_timer_get_count(struct udevice *dev)
Patrice Chotard200a7992017-02-21 13:37:05 +010023{
William Zhang38c26de2022-08-23 21:44:32 -070024 struct arm_global_timer_priv *priv = dev_get_priv(dev);
Patrice Chotard200a7992017-02-21 13:37:05 +010025 struct globaltimer *global_timer = priv->global_timer;
26 u32 low, high;
27 u64 timer;
28 u32 old = readl(&global_timer->cnt_h);
29
30 while (1) {
31 low = readl(&global_timer->cnt_l);
32 high = readl(&global_timer->cnt_h);
33 if (old == high)
34 break;
35 else
36 old = high;
37 }
38 timer = high;
Sean Anderson947fc2d2020-10-07 14:37:44 -040039 return (u64)((timer << 32) | low);
Patrice Chotard200a7992017-02-21 13:37:05 +010040}
41
William Zhang38c26de2022-08-23 21:44:32 -070042static int arm_global_timer_probe(struct udevice *dev)
Patrice Chotard200a7992017-02-21 13:37:05 +010043{
44 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
William Zhang38c26de2022-08-23 21:44:32 -070045 struct arm_global_timer_priv *priv = dev_get_priv(dev);
Nicolas Heemeryck44704cb2020-03-13 23:42:44 +010046 struct clk clk;
47 int err;
48 ulong ret;
Patrice Chotard200a7992017-02-21 13:37:05 +010049
50 /* get arm global timer base address */
Nicolas Heemeryck37881c62020-03-13 23:42:43 +010051 priv->global_timer = (struct globaltimer *)dev_read_addr_ptr(dev);
52 if (!priv->global_timer)
53 return -ENOENT;
Patrice Chotard200a7992017-02-21 13:37:05 +010054
Nicolas Heemeryck44704cb2020-03-13 23:42:44 +010055 err = clk_get_by_index(dev, 0, &clk);
56 if (!err) {
57 ret = clk_get_rate(&clk);
58 if (IS_ERR_VALUE(ret))
59 return ret;
60 uc_priv->clock_rate = ret;
61 } else {
62 uc_priv->clock_rate = CONFIG_SYS_HZ_CLOCK;
63 }
64
Patrice Chotard200a7992017-02-21 13:37:05 +010065 /* init timer */
66 writel(0x01, &priv->global_timer->ctl);
67
68 return 0;
69}
70
William Zhang38c26de2022-08-23 21:44:32 -070071static const struct timer_ops arm_global_timer_ops = {
72 .get_count = arm_global_timer_get_count,
Patrice Chotard200a7992017-02-21 13:37:05 +010073};
74
William Zhang38c26de2022-08-23 21:44:32 -070075static const struct udevice_id arm_global_timer_ids[] = {
Patrice Chotard200a7992017-02-21 13:37:05 +010076 { .compatible = "arm,cortex-a9-global-timer" },
77 {}
78};
79
William Zhang38c26de2022-08-23 21:44:32 -070080U_BOOT_DRIVER(arm_global_timer) = {
81 .name = "arm_global_timer",
Patrice Chotard200a7992017-02-21 13:37:05 +010082 .id = UCLASS_TIMER,
William Zhang38c26de2022-08-23 21:44:32 -070083 .of_match = arm_global_timer_ids,
84 .priv_auto = sizeof(struct arm_global_timer_priv),
85 .probe = arm_global_timer_probe,
86 .ops = &arm_global_timer_ops,
Patrice Chotard200a7992017-02-21 13:37:05 +010087};