Ryder Lee | d3c3606 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 1 | // 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 Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Ryder Lee | d3c3606 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 15 | |
| 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 | |
| 26 | struct mtk_timer_priv { |
| 27 | void __iomem *base; |
| 28 | }; |
| 29 | |
| 30 | static int mtk_timer_get_count(struct udevice *dev, u64 *count) |
| 31 | { |
| 32 | struct mtk_timer_priv *priv = dev_get_priv(dev); |
| 33 | u32 val = readl(priv->base + MTK_GPT4_CNT); |
| 34 | |
| 35 | *count = timer_conv_64(val); |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static int mtk_timer_probe(struct udevice *dev) |
| 41 | { |
| 42 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 43 | struct mtk_timer_priv *priv = dev_get_priv(dev); |
| 44 | struct clk clk, parent; |
| 45 | int ret; |
| 46 | |
| 47 | priv->base = dev_read_addr_ptr(dev); |
| 48 | if (!priv->base) |
| 49 | return -ENOENT; |
| 50 | |
| 51 | ret = clk_get_by_index(dev, 0, &clk); |
| 52 | if (ret) |
| 53 | return ret; |
| 54 | |
| 55 | ret = clk_get_by_index(dev, 1, &parent); |
| 56 | if (!ret) { |
| 57 | ret = clk_set_parent(&clk, &parent); |
| 58 | if (ret) |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | uc_priv->clock_rate = clk_get_rate(&clk); |
| 63 | if (!uc_priv->clock_rate) |
| 64 | return -EINVAL; |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static const struct timer_ops mtk_timer_ops = { |
| 70 | .get_count = mtk_timer_get_count, |
| 71 | }; |
| 72 | |
| 73 | static const struct udevice_id mtk_timer_ids[] = { |
| 74 | { .compatible = "mediatek,timer" }, |
Matthias Brugger | 6609d12 | 2020-04-26 01:17:44 +0200 | [diff] [blame] | 75 | { .compatible = "mediatek,mt6577-timer" }, |
Ryder Lee | d3c3606 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 76 | { } |
| 77 | }; |
| 78 | |
| 79 | U_BOOT_DRIVER(mtk_timer) = { |
| 80 | .name = "mtk_timer", |
| 81 | .id = UCLASS_TIMER, |
| 82 | .of_match = mtk_timer_ids, |
| 83 | .priv_auto_alloc_size = sizeof(struct mtk_timer_priv), |
| 84 | .probe = mtk_timer_probe, |
| 85 | .ops = &mtk_timer_ops, |
| 86 | .flags = DM_FLAG_PRE_RELOC, |
| 87 | }; |