developer | 4a34735 | 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 | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
developer | 4a34735 | 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 | |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame^] | 30 | static u64 mtk_timer_get_count(struct udevice *dev) |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 31 | { |
| 32 | struct mtk_timer_priv *priv = dev_get_priv(dev); |
| 33 | u32 val = readl(priv->base + MTK_GPT4_CNT); |
| 34 | |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame^] | 35 | return timer_conv_64(val); |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static 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 | |
| 67 | static const struct timer_ops mtk_timer_ops = { |
| 68 | .get_count = mtk_timer_get_count, |
| 69 | }; |
| 70 | |
| 71 | static const struct udevice_id mtk_timer_ids[] = { |
| 72 | { .compatible = "mediatek,timer" }, |
Matthias Brugger | fc4a6cb | 2020-04-26 01:17:44 +0200 | [diff] [blame] | 73 | { .compatible = "mediatek,mt6577-timer" }, |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 74 | { } |
| 75 | }; |
| 76 | |
| 77 | U_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 | }; |