timer: sti: convert to livetree
[platform/kernel/u-boot.git] / drivers / timer / sti-timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
4  * Author(s): Patrice Chotard, <patrice.chotard@st.com> for STMicroelectronics.
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <timer.h>
10
11 #include <asm/io.h>
12 #include <asm/arch-armv7/globaltimer.h>
13
14 struct sti_timer_priv {
15         struct globaltimer *global_timer;
16 };
17
18 static 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
40 static 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);
44
45         uc_priv->clock_rate = CONFIG_SYS_HZ_CLOCK;
46
47         /* get arm global timer base address */
48         priv->global_timer = (struct globaltimer *)dev_read_addr_ptr(dev);
49         if (!priv->global_timer)
50                 return -ENOENT;
51
52         /* init timer */
53         writel(0x01, &priv->global_timer->ctl);
54
55         return 0;
56 }
57
58 static const struct timer_ops sti_timer_ops = {
59         .get_count = sti_timer_get_count,
60 };
61
62 static const struct udevice_id sti_timer_ids[] = {
63         { .compatible = "arm,cortex-a9-global-timer" },
64         {}
65 };
66
67 U_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,
74 };