2018-11-15 02:07:56 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* MediaTek timer driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 MediaTek Inc.
|
|
|
|
* Author: Ryder Lee <ryder.lee@mediatek.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <clk.h>
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <timer.h>
|
|
|
|
#include <asm/io.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2018-11-15 02:07:56 +00:00
|
|
|
|
|
|
|
#define MTK_GPT4_CTRL 0x40
|
|
|
|
#define MTK_GPT4_CLK 0x44
|
|
|
|
#define MTK_GPT4_CNT 0x48
|
|
|
|
|
|
|
|
#define GPT4_ENABLE BIT(0)
|
|
|
|
#define GPT4_CLEAR BIT(1)
|
|
|
|
#define GPT4_FREERUN GENMASK(5, 4)
|
|
|
|
#define GPT4_CLK_SYS 0x0
|
|
|
|
#define GPT4_CLK_DIV1 0x0
|
|
|
|
|
|
|
|
struct mtk_timer_priv {
|
|
|
|
void __iomem *base;
|
|
|
|
};
|
|
|
|
|
2020-10-07 18:37:44 +00:00
|
|
|
static u64 mtk_timer_get_count(struct udevice *dev)
|
2018-11-15 02:07:56 +00:00
|
|
|
{
|
|
|
|
struct mtk_timer_priv *priv = dev_get_priv(dev);
|
|
|
|
u32 val = readl(priv->base + MTK_GPT4_CNT);
|
|
|
|
|
2020-10-07 18:37:44 +00:00
|
|
|
return timer_conv_64(val);
|
2018-11-15 02:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mtk_timer_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
|
|
|
struct mtk_timer_priv *priv = dev_get_priv(dev);
|
|
|
|
struct clk clk, parent;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
priv->base = dev_read_addr_ptr(dev);
|
|
|
|
if (!priv->base)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
ret = clk_get_by_index(dev, 0, &clk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = clk_get_by_index(dev, 1, &parent);
|
|
|
|
if (!ret) {
|
|
|
|
ret = clk_set_parent(&clk, &parent);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uc_priv->clock_rate = clk_get_rate(&clk);
|
|
|
|
if (!uc_priv->clock_rate)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2021-01-12 05:44:02 +00:00
|
|
|
/*
|
|
|
|
* Initialize the timer:
|
|
|
|
* 1. set clock source to system clock with clock divider setting to 1
|
|
|
|
* 2. set timer mode to free running
|
|
|
|
* 3. reset timer counter to 0 then enable the timer
|
|
|
|
*/
|
|
|
|
writel(GPT4_CLK_SYS | GPT4_CLK_DIV1, priv->base + MTK_GPT4_CLK);
|
|
|
|
writel(GPT4_FREERUN | GPT4_CLEAR | GPT4_ENABLE,
|
|
|
|
priv->base + MTK_GPT4_CTRL);
|
|
|
|
|
2018-11-15 02:07:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct timer_ops mtk_timer_ops = {
|
|
|
|
.get_count = mtk_timer_get_count,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id mtk_timer_ids[] = {
|
|
|
|
{ .compatible = "mediatek,timer" },
|
2020-04-25 23:17:44 +00:00
|
|
|
{ .compatible = "mediatek,mt6577-timer" },
|
2018-11-15 02:07:56 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(mtk_timer) = {
|
|
|
|
.name = "mtk_timer",
|
|
|
|
.id = UCLASS_TIMER,
|
|
|
|
.of_match = mtk_timer_ids,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct mtk_timer_priv),
|
2018-11-15 02:07:56 +00:00
|
|
|
.probe = mtk_timer_probe,
|
|
|
|
.ops = &mtk_timer_ops,
|
|
|
|
.flags = DM_FLAG_PRE_RELOC,
|
|
|
|
};
|