blob: 74e9ea34ffa3fc1cad09f78da8bdd7ff2581b796 [file] [log] [blame]
Ryder Leed3c36062018-11-15 10:07:56 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * MediaTek timer driver
4 *
5 * Copyright (C) 2018 MediaTek Inc.
6 * Author: Ryder Lee <ryder.lee@mediatek.com>
7 */
8
9#include <clk.h>
10#include <common.h>
11#include <dm.h>
12#include <timer.h>
13#include <asm/io.h>
Simon Glasscd93d622020-05-10 11:40:13 -060014#include <linux/bitops.h>
Ryder Leed3c36062018-11-15 10:07:56 +080015
16#define MTK_GPT4_CTRL 0x40
17#define MTK_GPT4_CLK 0x44
18#define MTK_GPT4_CNT 0x48
19
20#define GPT4_ENABLE BIT(0)
21#define GPT4_CLEAR BIT(1)
22#define GPT4_FREERUN GENMASK(5, 4)
23#define GPT4_CLK_SYS 0x0
24#define GPT4_CLK_DIV1 0x0
25
26struct mtk_timer_priv {
27 void __iomem *base;
28};
29
Sean Anderson8af7bb92020-10-07 14:37:44 -040030static u64 mtk_timer_get_count(struct udevice *dev)
Ryder Leed3c36062018-11-15 10:07:56 +080031{
32 struct mtk_timer_priv *priv = dev_get_priv(dev);
33 u32 val = readl(priv->base + MTK_GPT4_CNT);
34
Sean Anderson8af7bb92020-10-07 14:37:44 -040035 return timer_conv_64(val);
Ryder Leed3c36062018-11-15 10:07:56 +080036}
37
38static int mtk_timer_probe(struct udevice *dev)
39{
40 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
41 struct mtk_timer_priv *priv = dev_get_priv(dev);
42 struct clk clk, parent;
43 int ret;
44
45 priv->base = dev_read_addr_ptr(dev);
46 if (!priv->base)
47 return -ENOENT;
48
49 ret = clk_get_by_index(dev, 0, &clk);
50 if (ret)
51 return ret;
52
53 ret = clk_get_by_index(dev, 1, &parent);
54 if (!ret) {
55 ret = clk_set_parent(&clk, &parent);
56 if (ret)
57 return ret;
58 }
59
60 uc_priv->clock_rate = clk_get_rate(&clk);
61 if (!uc_priv->clock_rate)
62 return -EINVAL;
63
64 return 0;
65}
66
67static const struct timer_ops mtk_timer_ops = {
68 .get_count = mtk_timer_get_count,
69};
70
71static const struct udevice_id mtk_timer_ids[] = {
72 { .compatible = "mediatek,timer" },
Matthias Brugger6609d122020-04-26 01:17:44 +020073 { .compatible = "mediatek,mt6577-timer" },
Ryder Leed3c36062018-11-15 10:07:56 +080074 { }
75};
76
77U_BOOT_DRIVER(mtk_timer) = {
78 .name = "mtk_timer",
79 .id = UCLASS_TIMER,
80 .of_match = mtk_timer_ids,
81 .priv_auto_alloc_size = sizeof(struct mtk_timer_priv),
82 .probe = mtk_timer_probe,
83 .ops = &mtk_timer_ops,
84 .flags = DM_FLAG_PRE_RELOC,
85};