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> |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <timer.h> |
| 12 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 13 | #include <linux/bitops.h> |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 14 | |
developer | 0e08eed | 2022-09-09 19:59:41 +0800 | [diff] [blame] | 15 | #define MTK_GPT4_OFFSET_V1 0x40 |
| 16 | #define MTK_GPT4_OFFSET_V2 0x80 |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 17 | |
developer | 0e08eed | 2022-09-09 19:59:41 +0800 | [diff] [blame] | 18 | #define MTK_GPT_CON 0x0 |
| 19 | #define MTK_GPT_V1_CLK 0x4 |
| 20 | #define MTK_GPT_CNT 0x8 |
| 21 | |
| 22 | #define GPT_ENABLE BIT(0) |
| 23 | #define GPT_CLEAR BIT(1) |
| 24 | #define GPT_V1_FREERUN GENMASK(5, 4) |
| 25 | #define GPT_V2_FREERUN GENMASK(6, 5) |
| 26 | |
| 27 | enum mtk_gpt_ver { |
| 28 | MTK_GPT_V1, |
| 29 | MTK_GPT_V2 |
| 30 | }; |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 31 | |
| 32 | struct mtk_timer_priv { |
| 33 | void __iomem *base; |
developer | 0e08eed | 2022-09-09 19:59:41 +0800 | [diff] [blame] | 34 | unsigned int gpt4_offset; |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 35 | }; |
| 36 | |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 37 | static u64 mtk_timer_get_count(struct udevice *dev) |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 38 | { |
| 39 | struct mtk_timer_priv *priv = dev_get_priv(dev); |
developer | 0e08eed | 2022-09-09 19:59:41 +0800 | [diff] [blame] | 40 | u32 val = readl(priv->base + priv->gpt4_offset + MTK_GPT_CNT); |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 41 | |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 42 | return timer_conv_64(val); |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static int mtk_timer_probe(struct udevice *dev) |
| 46 | { |
| 47 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 48 | struct mtk_timer_priv *priv = dev_get_priv(dev); |
| 49 | struct clk clk, parent; |
developer | 0e08eed | 2022-09-09 19:59:41 +0800 | [diff] [blame] | 50 | int ret, gpt_ver; |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 51 | |
| 52 | priv->base = dev_read_addr_ptr(dev); |
developer | 0e08eed | 2022-09-09 19:59:41 +0800 | [diff] [blame] | 53 | gpt_ver = dev_get_driver_data(dev); |
| 54 | |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 55 | if (!priv->base) |
| 56 | return -ENOENT; |
| 57 | |
developer | 0e08eed | 2022-09-09 19:59:41 +0800 | [diff] [blame] | 58 | if (gpt_ver == MTK_GPT_V2) { |
| 59 | priv->gpt4_offset = MTK_GPT4_OFFSET_V2; |
| 60 | |
| 61 | writel(GPT_V2_FREERUN | GPT_CLEAR | GPT_ENABLE, |
| 62 | priv->base + priv->gpt4_offset + MTK_GPT_CON); |
| 63 | } else { |
| 64 | priv->gpt4_offset = MTK_GPT4_OFFSET_V1; |
| 65 | |
| 66 | writel(GPT_V1_FREERUN | GPT_CLEAR | GPT_ENABLE, |
| 67 | priv->base + priv->gpt4_offset + MTK_GPT_CON); |
| 68 | writel(0, priv->base + priv->gpt4_offset + MTK_GPT_V1_CLK); |
| 69 | } |
| 70 | |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 71 | ret = clk_get_by_index(dev, 0, &clk); |
| 72 | if (ret) |
| 73 | return ret; |
| 74 | |
| 75 | ret = clk_get_by_index(dev, 1, &parent); |
| 76 | if (!ret) { |
| 77 | ret = clk_set_parent(&clk, &parent); |
| 78 | if (ret) |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | uc_priv->clock_rate = clk_get_rate(&clk); |
| 83 | if (!uc_priv->clock_rate) |
| 84 | return -EINVAL; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static const struct timer_ops mtk_timer_ops = { |
| 90 | .get_count = mtk_timer_get_count, |
| 91 | }; |
| 92 | |
| 93 | static const struct udevice_id mtk_timer_ids[] = { |
developer | 0e08eed | 2022-09-09 19:59:41 +0800 | [diff] [blame] | 94 | { .compatible = "mediatek,timer", .data = MTK_GPT_V1 }, |
| 95 | { .compatible = "mediatek,mt6577-timer", .data = MTK_GPT_V1 }, |
| 96 | { .compatible = "mediatek,mt7981-timer", .data = MTK_GPT_V2 }, |
| 97 | { .compatible = "mediatek,mt7986-timer", .data = MTK_GPT_V2 }, |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 98 | { } |
| 99 | }; |
| 100 | |
| 101 | U_BOOT_DRIVER(mtk_timer) = { |
| 102 | .name = "mtk_timer", |
| 103 | .id = UCLASS_TIMER, |
| 104 | .of_match = mtk_timer_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 105 | .priv_auto = sizeof(struct mtk_timer_priv), |
developer | 4a34735 | 2018-11-15 10:07:56 +0800 | [diff] [blame] | 106 | .probe = mtk_timer_probe, |
| 107 | .ops = &mtk_timer_ops, |
| 108 | .flags = DM_FLAG_PRE_RELOC, |
| 109 | }; |