blob: ff42056abdd1aa2f5abd2a9438272d820bad7ab3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Patrice Chotard347cb2e2017-02-21 13:37:05 +01002/*
Patrice Chotardfb48bc42017-10-23 09:53:57 +02003 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
4 * Author(s): Patrice Chotard, <patrice.chotard@st.com> for STMicroelectronics.
Patrice Chotard347cb2e2017-02-21 13:37:05 +01005 */
6
7#include <common.h>
8#include <dm.h>
Nicolas Heemeryck5b5699c2020-03-13 23:42:44 +01009#include <clk.h>
Patrice Chotard347cb2e2017-02-21 13:37:05 +010010#include <timer.h>
Nicolas Heemeryck5b5699c2020-03-13 23:42:44 +010011#include <linux/err.h>
Patrice Chotard347cb2e2017-02-21 13:37:05 +010012
13#include <asm/io.h>
14#include <asm/arch-armv7/globaltimer.h>
15
Patrice Chotard347cb2e2017-02-21 13:37:05 +010016struct sti_timer_priv {
17 struct globaltimer *global_timer;
18};
19
20static int sti_timer_get_count(struct udevice *dev, u64 *count)
21{
22 struct sti_timer_priv *priv = dev_get_priv(dev);
23 struct globaltimer *global_timer = priv->global_timer;
24 u32 low, high;
25 u64 timer;
26 u32 old = readl(&global_timer->cnt_h);
27
28 while (1) {
29 low = readl(&global_timer->cnt_l);
30 high = readl(&global_timer->cnt_h);
31 if (old == high)
32 break;
33 else
34 old = high;
35 }
36 timer = high;
37 *count = (u64)((timer << 32) | low);
38
39 return 0;
40}
41
42static int sti_timer_probe(struct udevice *dev)
43{
44 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
45 struct sti_timer_priv *priv = dev_get_priv(dev);
Nicolas Heemeryck5b5699c2020-03-13 23:42:44 +010046 struct clk clk;
47 int err;
48 ulong ret;
Patrice Chotard347cb2e2017-02-21 13:37:05 +010049
50 /* get arm global timer base address */
Nicolas Heemeryck123123d2020-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 Chotard347cb2e2017-02-21 13:37:05 +010054
Nicolas Heemeryck5b5699c2020-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 Chotard347cb2e2017-02-21 13:37:05 +010065 /* init timer */
66 writel(0x01, &priv->global_timer->ctl);
67
68 return 0;
69}
70
71static const struct timer_ops sti_timer_ops = {
72 .get_count = sti_timer_get_count,
73};
74
75static const struct udevice_id sti_timer_ids[] = {
76 { .compatible = "arm,cortex-a9-global-timer" },
77 {}
78};
79
80U_BOOT_DRIVER(sti_timer) = {
81 .name = "sti_timer",
82 .id = UCLASS_TIMER,
83 .of_match = sti_timer_ids,
84 .priv_auto_alloc_size = sizeof(struct sti_timer_priv),
85 .probe = sti_timer_probe,
86 .ops = &sti_timer_ops,
Patrice Chotard347cb2e2017-02-21 13:37:05 +010087};