blob: eac22ae39b01c7b1c885b21f19f48bba6ff88b6e [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>
Patrice Chotard347cb2e2017-02-21 13:37:05 +01009#include <timer.h>
10
11#include <asm/io.h>
12#include <asm/arch-armv7/globaltimer.h>
13
Patrice Chotard347cb2e2017-02-21 13:37:05 +010014struct sti_timer_priv {
15 struct globaltimer *global_timer;
16};
17
18static int sti_timer_get_count(struct udevice *dev, u64 *count)
19{
20 struct sti_timer_priv *priv = dev_get_priv(dev);
21 struct globaltimer *global_timer = priv->global_timer;
22 u32 low, high;
23 u64 timer;
24 u32 old = readl(&global_timer->cnt_h);
25
26 while (1) {
27 low = readl(&global_timer->cnt_l);
28 high = readl(&global_timer->cnt_h);
29 if (old == high)
30 break;
31 else
32 old = high;
33 }
34 timer = high;
35 *count = (u64)((timer << 32) | low);
36
37 return 0;
38}
39
40static int sti_timer_probe(struct udevice *dev)
41{
42 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
43 struct sti_timer_priv *priv = dev_get_priv(dev);
Patrice Chotard347cb2e2017-02-21 13:37:05 +010044
45 uc_priv->clock_rate = CONFIG_SYS_HZ_CLOCK;
46
47 /* get arm global timer base address */
Nicolas Heemeryck123123d2020-03-13 23:42:43 +010048 priv->global_timer = (struct globaltimer *)dev_read_addr_ptr(dev);
49 if (!priv->global_timer)
50 return -ENOENT;
Patrice Chotard347cb2e2017-02-21 13:37:05 +010051
52 /* init timer */
53 writel(0x01, &priv->global_timer->ctl);
54
55 return 0;
56}
57
58static const struct timer_ops sti_timer_ops = {
59 .get_count = sti_timer_get_count,
60};
61
62static const struct udevice_id sti_timer_ids[] = {
63 { .compatible = "arm,cortex-a9-global-timer" },
64 {}
65};
66
67U_BOOT_DRIVER(sti_timer) = {
68 .name = "sti_timer",
69 .id = UCLASS_TIMER,
70 .of_match = sti_timer_ids,
71 .priv_auto_alloc_size = sizeof(struct sti_timer_priv),
72 .probe = sti_timer_probe,
73 .ops = &sti_timer_ops,
Patrice Chotard347cb2e2017-02-21 13:37:05 +010074};