Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 2 | /* |
Patrice Chotard | 9e21624 | 2017-10-23 09:53:57 +0200 | [diff] [blame] | 3 | * Copyright (C) 2017, STMicroelectronics - All Rights Reserved |
Patrice Chotard | 5d9950d | 2020-12-02 18:47:30 +0100 | [diff] [blame] | 4 | * Author(s): Patrice Chotard, <patrice.chotard@foss.st.com> for STMicroelectronics. |
William Zhang | 38c26de | 2022-08-23 21:44:32 -0700 | [diff] [blame] | 5 | * |
| 6 | * ARM Cortext A9 global timer driver |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 9 | #include <config.h> |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 10 | #include <dm.h> |
Nicolas Heemeryck | 44704cb | 2020-03-13 23:42:44 +0100 | [diff] [blame] | 11 | #include <clk.h> |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 12 | #include <timer.h> |
Nicolas Heemeryck | 44704cb | 2020-03-13 23:42:44 +0100 | [diff] [blame] | 13 | #include <linux/err.h> |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 14 | |
| 15 | #include <asm/io.h> |
| 16 | #include <asm/arch-armv7/globaltimer.h> |
| 17 | |
William Zhang | 38c26de | 2022-08-23 21:44:32 -0700 | [diff] [blame] | 18 | struct arm_global_timer_priv { |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 19 | struct globaltimer *global_timer; |
| 20 | }; |
| 21 | |
William Zhang | 38c26de | 2022-08-23 21:44:32 -0700 | [diff] [blame] | 22 | static u64 arm_global_timer_get_count(struct udevice *dev) |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 23 | { |
William Zhang | 38c26de | 2022-08-23 21:44:32 -0700 | [diff] [blame] | 24 | struct arm_global_timer_priv *priv = dev_get_priv(dev); |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 25 | struct globaltimer *global_timer = priv->global_timer; |
| 26 | u32 low, high; |
| 27 | u64 timer; |
| 28 | u32 old = readl(&global_timer->cnt_h); |
| 29 | |
| 30 | while (1) { |
| 31 | low = readl(&global_timer->cnt_l); |
| 32 | high = readl(&global_timer->cnt_h); |
| 33 | if (old == high) |
| 34 | break; |
| 35 | else |
| 36 | old = high; |
| 37 | } |
| 38 | timer = high; |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 39 | return (u64)((timer << 32) | low); |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 40 | } |
| 41 | |
William Zhang | 38c26de | 2022-08-23 21:44:32 -0700 | [diff] [blame] | 42 | static int arm_global_timer_probe(struct udevice *dev) |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 43 | { |
| 44 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
William Zhang | 38c26de | 2022-08-23 21:44:32 -0700 | [diff] [blame] | 45 | struct arm_global_timer_priv *priv = dev_get_priv(dev); |
Nicolas Heemeryck | 44704cb | 2020-03-13 23:42:44 +0100 | [diff] [blame] | 46 | struct clk clk; |
| 47 | int err; |
| 48 | ulong ret; |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 49 | |
| 50 | /* get arm global timer base address */ |
Nicolas Heemeryck | 37881c6 | 2020-03-13 23:42:43 +0100 | [diff] [blame] | 51 | priv->global_timer = (struct globaltimer *)dev_read_addr_ptr(dev); |
| 52 | if (!priv->global_timer) |
| 53 | return -ENOENT; |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 54 | |
Nicolas Heemeryck | 44704cb | 2020-03-13 23:42:44 +0100 | [diff] [blame] | 55 | err = clk_get_by_index(dev, 0, &clk); |
| 56 | if (!err) { |
| 57 | ret = clk_get_rate(&clk); |
| 58 | if (IS_ERR_VALUE(ret)) |
| 59 | return ret; |
| 60 | uc_priv->clock_rate = ret; |
| 61 | } else { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 62 | uc_priv->clock_rate = CFG_SYS_HZ_CLOCK; |
Nicolas Heemeryck | 44704cb | 2020-03-13 23:42:44 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 65 | /* init timer */ |
| 66 | writel(0x01, &priv->global_timer->ctl); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
William Zhang | 38c26de | 2022-08-23 21:44:32 -0700 | [diff] [blame] | 71 | static const struct timer_ops arm_global_timer_ops = { |
| 72 | .get_count = arm_global_timer_get_count, |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
William Zhang | 38c26de | 2022-08-23 21:44:32 -0700 | [diff] [blame] | 75 | static const struct udevice_id arm_global_timer_ids[] = { |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 76 | { .compatible = "arm,cortex-a9-global-timer" }, |
| 77 | {} |
| 78 | }; |
| 79 | |
William Zhang | 38c26de | 2022-08-23 21:44:32 -0700 | [diff] [blame] | 80 | U_BOOT_DRIVER(arm_global_timer) = { |
| 81 | .name = "arm_global_timer", |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 82 | .id = UCLASS_TIMER, |
William Zhang | 38c26de | 2022-08-23 21:44:32 -0700 | [diff] [blame] | 83 | .of_match = arm_global_timer_ids, |
| 84 | .priv_auto = sizeof(struct arm_global_timer_priv), |
| 85 | .probe = arm_global_timer_probe, |
| 86 | .ops = &arm_global_timer_ops, |
Patrice Chotard | 200a799 | 2017-02-21 13:37:05 +0100 | [diff] [blame] | 87 | }; |